5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "acl/Gadgets.h"
37 #include "base/TextException.h"
38 #include "comm/Connection.h"
39 #include "comm/forward.h"
40 #include "comm/Write.h"
41 #include "err_detail_type.h"
42 #include "errorpage.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
47 #include "SquidTime.h"
48 #include "StatCounters.h"
52 #include "adaptation/AccessCheck.h"
53 #include "adaptation/Answer.h"
54 #include "adaptation/Iterator.h"
55 #include "base/AsyncCall.h"
58 // implemented in client_side_reply.cc until sides have a common parent
59 extern void purgeEntriesByUrl(HttpRequest
* req
, const char *url
);
62 ServerStateData::ServerStateData(FwdState
*theFwdState
): AsyncJob("ServerStateData"),
65 adaptedHeadSource(NULL
),
66 adaptationAccessCheckPending(false),
67 startedAdaptation(false),
69 receivedWholeRequestBody(false),
78 request
= HTTPMSGLOCK(fwd
->request
);
81 ServerStateData::~ServerStateData()
83 // paranoid: check that swanSong has been called
84 assert(!requestBodySource
);
86 assert(!virginBodyDestination
);
87 assert(!adaptedBodySource
);
92 HTTPMSGUNLOCK(request
);
93 HTTPMSGUNLOCK(theVirginReply
);
94 HTTPMSGUNLOCK(theFinalReply
);
96 fwd
= NULL
; // refcounted
98 if (responseBodyBuffer
!= NULL
) {
99 delete responseBodyBuffer
;
100 responseBodyBuffer
= NULL
;
105 ServerStateData::swanSong()
107 // get rid of our piping obligations
108 if (requestBodySource
!= NULL
)
109 stopConsumingFrom(requestBodySource
);
115 BodyConsumer::swanSong();
117 Initiator::swanSong();
118 BodyProducer::swanSong();
121 // paranoid: check that swanSong has been called
122 // extra paranoid: yeah, I really mean it. they MUST pass here.
123 assert(!requestBodySource
);
125 assert(!virginBodyDestination
);
126 assert(!adaptedBodySource
);
132 ServerStateData::virginReply()
134 assert(theVirginReply
);
135 return theVirginReply
;
139 ServerStateData::virginReply() const
141 assert(theVirginReply
);
142 return theVirginReply
;
146 ServerStateData::setVirginReply(HttpReply
*rep
)
148 debugs(11,5, HERE
<< this << " setting virgin reply to " << rep
);
149 assert(!theVirginReply
);
151 theVirginReply
= HTTPMSGLOCK(rep
);
152 return theVirginReply
;
156 ServerStateData::finalReply()
158 assert(theFinalReply
);
159 return theFinalReply
;
163 ServerStateData::setFinalReply(HttpReply
*rep
)
165 debugs(11,5, HERE
<< this << " setting final reply to " << rep
);
167 assert(!theFinalReply
);
169 theFinalReply
= HTTPMSGLOCK(rep
);
171 // give entry the reply because haveParsedReplyHeaders() expects it there
172 entry
->replaceHttpReply(theFinalReply
, false); // but do not write yet
173 haveParsedReplyHeaders(); // update the entry/reply (e.g., set timestamps)
174 entry
->startWriting(); // write the updated entry to store
176 return theFinalReply
;
179 // called when no more server communication is expected; may quit
181 ServerStateData::serverComplete()
183 debugs(11,5,HERE
<< "serverComplete " << this);
185 if (!doneWithServer()) {
187 assert(doneWithServer());
192 HttpRequest
*r
= originalRequest();
193 r
->hier
.total_response_time
= r
->hier
.first_conn_start
.tv_sec
?
194 tvSubMsec(r
->hier
.first_conn_start
, current_time
) : -1;
196 if (requestBodySource
!= NULL
)
197 stopConsumingFrom(requestBodySource
);
199 if (responseBodyBuffer
!= NULL
)
206 ServerStateData::serverComplete2()
208 debugs(11,5,HERE
<< "serverComplete2 " << this);
211 if (virginBodyDestination
!= NULL
)
212 stopProducingFor(virginBodyDestination
, true);
214 if (!doneWithAdaptation())
218 completeForwarding();
221 bool ServerStateData::doneAll() const
223 return doneWithServer() &&
225 doneWithAdaptation() &&
226 Adaptation::Initiator::doneAll() &&
227 BodyProducer::doneAll() &&
229 BodyConsumer::doneAll();
232 // FTP side overloads this to work around multiple calls to fwd->complete
234 ServerStateData::completeForwarding()
236 debugs(11,5, HERE
<< "completing forwarding for " << fwd
);
241 // Register to receive request body
242 bool ServerStateData::startRequestBodyFlow()
244 HttpRequest
*r
= originalRequest();
245 assert(r
->body_pipe
!= NULL
);
246 requestBodySource
= r
->body_pipe
;
247 if (requestBodySource
->setConsumerIfNotLate(this)) {
248 debugs(11,3, HERE
<< "expecting request body from " <<
249 requestBodySource
->status());
253 debugs(11,3, HERE
<< "aborting on partially consumed request body: " <<
254 requestBodySource
->status());
255 requestBodySource
= NULL
;
259 // Entry-dependent callbacks use this check to quit if the entry went bad
261 ServerStateData::abortOnBadEntry(const char *abortReason
)
263 if (entry
->isAccepting())
266 debugs(11,5, HERE
<< "entry is not Accepting!");
267 abortTransaction(abortReason
);
271 // more request or adapted response body is available
273 ServerStateData::noteMoreBodyDataAvailable(BodyPipe::Pointer bp
)
276 if (adaptedBodySource
== bp
) {
277 handleMoreAdaptedBodyAvailable();
281 if (requestBodySource
== bp
)
282 handleMoreRequestBodyAvailable();
285 // the entire request or adapted response body was provided, successfully
287 ServerStateData::noteBodyProductionEnded(BodyPipe::Pointer bp
)
290 if (adaptedBodySource
== bp
) {
291 handleAdaptedBodyProductionEnded();
295 if (requestBodySource
== bp
)
296 handleRequestBodyProductionEnded();
299 // premature end of the request or adapted response body production
301 ServerStateData::noteBodyProducerAborted(BodyPipe::Pointer bp
)
304 if (adaptedBodySource
== bp
) {
305 handleAdaptedBodyProducerAborted();
309 if (requestBodySource
== bp
)
310 handleRequestBodyProducerAborted();
314 // more origin request body data is available
316 ServerStateData::handleMoreRequestBodyAvailable()
319 sendMoreRequestBody();
321 debugs(9,3, HERE
<< "waiting for request body write to complete");
324 // there will be no more handleMoreRequestBodyAvailable calls
326 ServerStateData::handleRequestBodyProductionEnded()
328 receivedWholeRequestBody
= true;
330 doneSendingRequestBody();
332 debugs(9,3, HERE
<< "waiting for request body write to complete");
335 // called when we are done sending request body; kids extend this
337 ServerStateData::doneSendingRequestBody()
339 debugs(9,3, HERE
<< "done sending request body");
340 assert(requestBodySource
!= NULL
);
341 stopConsumingFrom(requestBodySource
);
346 // called when body producers aborts; kids extend this
348 ServerStateData::handleRequestBodyProducerAborted()
350 if (requestSender
!= NULL
)
351 debugs(9,3, HERE
<< "fyi: request body aborted while we were sending");
353 fwd
->dontRetry(true); // the problem is not with the server
354 stopConsumingFrom(requestBodySource
); // requestSender, if any, will notice
359 // called when we wrote request headers(!) or a part of the body
361 ServerStateData::sentRequestBody(const CommIoCbParams
&io
)
363 debugs(11, 5, "sentRequestBody: FD " << io
.fd
<< ": size " << io
.size
<< ": errflag " << io
.flag
<< ".");
364 debugs(32,3,HERE
<< "sentRequestBody called");
366 requestSender
= NULL
;
369 fd_bytes(io
.fd
, io
.size
, FD_WRITE
);
370 kb_incr(&(statCounter
.server
.all
.kbytes_out
), io
.size
);
371 // kids should increment their counters
374 if (io
.flag
== COMM_ERR_CLOSING
)
377 if (!requestBodySource
) {
378 debugs(9,3, HERE
<< "detected while-we-were-sending abort");
379 return; // do nothing;
383 debugs(11, DBG_IMPORTANT
, "sentRequestBody error: FD " << io
.fd
<< ": " << xstrerr(io
.xerrno
));
385 err
= new ErrorState(ERR_WRITE_ERROR
, HTTP_BAD_GATEWAY
, fwd
->request
);
386 err
->xerrno
= io
.xerrno
;
388 abortTransaction("I/O error while sending request body");
392 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
393 abortTransaction("store entry aborted while sending request body");
397 if (!requestBodySource
->exhausted())
398 sendMoreRequestBody();
399 else if (receivedWholeRequestBody
)
400 doneSendingRequestBody();
402 debugs(9,3, HERE
<< "waiting for body production end or abort");
406 ServerStateData::sendMoreRequestBody()
408 assert(requestBodySource
!= NULL
);
409 assert(!requestSender
);
411 const Comm::ConnectionPointer conn
= dataConnection();
413 if (!Comm::IsConnOpen(conn
)) {
414 debugs(9,3, HERE
<< "cannot send request body to closing " << conn
);
415 return; // wait for the kid's close handler; TODO: assert(closer);
419 if (getMoreRequestBody(buf
) && buf
.contentSize() > 0) {
420 debugs(9,3, HERE
<< "will write " << buf
.contentSize() << " request body bytes");
421 typedef CommCbMemFunT
<ServerStateData
, CommIoCbParams
> Dialer
;
422 requestSender
= JobCallback(93,3, Dialer
, this, ServerStateData::sentRequestBody
);
423 Comm::Write(conn
, &buf
, requestSender
);
425 debugs(9,3, HERE
<< "will wait for more request body bytes or eof");
426 requestSender
= NULL
;
430 /// either fill buf with available [encoded] request body bytes or return false
432 ServerStateData::getMoreRequestBody(MemBuf
&buf
)
434 // default implementation does not encode request body content
435 Must(requestBodySource
!= NULL
);
436 return requestBodySource
->getMoreData(buf
);
439 // Compares hosts in urls, returns false if different, no sheme, or no host.
441 sameUrlHosts(const char *url1
, const char *url2
)
443 // XXX: Want urlHostname() here, but it uses static storage and copying
444 const char *host1
= strchr(url1
, ':');
445 const char *host2
= strchr(url2
, ':');
447 if (host1
&& host2
) {
448 // skip scheme slashes
452 } while (*host1
== '/' && *host2
== '/');
455 return false; // no host
457 // increment while the same until we reach the end of the URL/host
458 while (*host1
&& *host1
!= '/' && *host1
== *host2
) {
462 return *host1
== *host2
;
465 return false; // no URL scheme
468 // purges entries that match the value of a given HTTP [response] header
470 purgeEntriesByHeader(HttpRequest
*req
, const char *reqUrl
, HttpMsg
*rep
, http_hdr_type hdr
)
472 const char *hdrUrl
, *absUrl
;
475 hdrUrl
= rep
->header
.getStr(hdr
);
476 if (hdrUrl
== NULL
) {
481 * If the URL is relative, make it absolute so we can find it.
482 * If it's absolute, make sure the host parts match to avoid DOS attacks
483 * as per RFC 2616 13.10.
485 if (urlIsRelative(hdrUrl
)) {
486 absUrl
= urlMakeAbsolute(req
, hdrUrl
);
487 if (absUrl
!= NULL
) {
490 } else if (!sameUrlHosts(reqUrl
, hdrUrl
)) {
494 purgeEntriesByUrl(req
, hdrUrl
);
496 if (absUrl
!= NULL
) {
501 // some HTTP methods should purge matching cache entries
503 ServerStateData::maybePurgeOthers()
505 // only some HTTP methods should purge matching cache entries
506 if (!request
->method
.purgesOthers())
509 // and probably only if the response was successful
510 if (theFinalReply
->sline
.status
>= 400)
513 // XXX: should we use originalRequest() here?
514 const char *reqUrl
= urlCanonical(request
);
515 debugs(88, 5, "maybe purging due to " << RequestMethodStr(request
->method
) << ' ' << reqUrl
);
516 purgeEntriesByUrl(request
, reqUrl
);
517 purgeEntriesByHeader(request
, reqUrl
, theFinalReply
, HDR_LOCATION
);
518 purgeEntriesByHeader(request
, reqUrl
, theFinalReply
, HDR_CONTENT_LOCATION
);
521 /// called when we have final (possibly adapted) reply headers; kids extend
523 ServerStateData::haveParsedReplyHeaders()
530 ServerStateData::originalRequest()
536 /// Initiate an asynchronous adaptation transaction which will call us back.
538 ServerStateData::startAdaptation(const Adaptation::ServiceGroupPointer
&group
, HttpRequest
*cause
)
540 debugs(11, 5, "ServerStateData::startAdaptation() called");
541 // check whether we should be sending a body as well
542 // start body pipe to feed ICAP transaction if needed
543 assert(!virginBodyDestination
);
544 HttpReply
*vrep
= virginReply();
545 assert(!vrep
->body_pipe
);
547 if (vrep
->expectingBody(cause
->method
, size
) && size
) {
548 virginBodyDestination
= new BodyPipe(this);
549 vrep
->body_pipe
= virginBodyDestination
;
550 debugs(93, 6, HERE
<< "will send virgin reply body to " <<
551 virginBodyDestination
<< "; size: " << size
);
553 virginBodyDestination
->setBodySize(size
);
556 adaptedHeadSource
= initiateAdaptation(
557 new Adaptation::Iterator(vrep
, cause
, group
));
558 startedAdaptation
= initiated(adaptedHeadSource
);
559 Must(startedAdaptation
);
562 // properly cleans up ICAP-related state
563 // may be called multiple times
564 void ServerStateData::cleanAdaptation()
566 debugs(11,5, HERE
<< "cleaning ICAP; ACL: " << adaptationAccessCheckPending
);
568 if (virginBodyDestination
!= NULL
)
569 stopProducingFor(virginBodyDestination
, false);
571 announceInitiatorAbort(adaptedHeadSource
);
573 if (adaptedBodySource
!= NULL
)
574 stopConsumingFrom(adaptedBodySource
);
576 if (!adaptationAccessCheckPending
) // we cannot cancel a pending callback
577 assert(doneWithAdaptation()); // make sure the two methods are in sync
581 ServerStateData::doneWithAdaptation() const
583 return !adaptationAccessCheckPending
&&
584 !virginBodyDestination
&& !adaptedHeadSource
&& !adaptedBodySource
;
587 // sends virgin reply body to ICAP, buffering excesses if needed
589 ServerStateData::adaptVirginReplyBody(const char *data
, ssize_t len
)
591 assert(startedAdaptation
);
593 if (!virginBodyDestination
) {
594 debugs(11,3, HERE
<< "ICAP does not want more virgin body");
598 // grow overflow area if already overflowed
599 if (responseBodyBuffer
) {
600 responseBodyBuffer
->append(data
, len
);
601 data
= responseBodyBuffer
->content();
602 len
= responseBodyBuffer
->contentSize();
605 const ssize_t putSize
= virginBodyDestination
->putMoreData(data
, len
);
609 // if we had overflow area, shrink it as necessary
610 if (responseBodyBuffer
) {
611 if (putSize
== responseBodyBuffer
->contentSize()) {
612 delete responseBodyBuffer
;
613 responseBodyBuffer
= NULL
;
615 responseBodyBuffer
->consume(putSize
);
620 // if we did not have an overflow area, create it as needed
622 assert(!responseBodyBuffer
);
623 responseBodyBuffer
= new MemBuf
;
624 responseBodyBuffer
->init(4096, SQUID_TCP_SO_RCVBUF
* 10);
625 responseBodyBuffer
->append(data
, len
);
629 // can supply more virgin response body data
631 ServerStateData::noteMoreBodySpaceAvailable(BodyPipe::Pointer
)
633 if (responseBodyBuffer
) {
634 addVirginReplyBody(NULL
, 0); // kick the buffered fragment alive again
635 if (completed
&& !responseBodyBuffer
) {
640 maybeReadVirginBody();
643 // the consumer of our virgin response body aborted
645 ServerStateData::noteBodyConsumerAborted(BodyPipe::Pointer
)
647 stopProducingFor(virginBodyDestination
, false);
649 // do not force closeServer here in case we need to bypass AdaptationQueryAbort
651 if (doneWithAdaptation()) // we may still be receiving adapted response
652 handleAdaptationCompleted();
655 // received adapted response headers (body may follow)
657 ServerStateData::noteAdaptationAnswer(const Adaptation::Answer
&answer
)
659 clearAdaptation(adaptedHeadSource
); // we do not expect more messages
661 switch (answer
.kind
) {
662 case Adaptation::Answer::akForward
:
663 handleAdaptedHeader(answer
.message
);
666 case Adaptation::Answer::akBlock
:
667 handleAdaptationBlocked(answer
);
670 case Adaptation::Answer::akError
:
671 handleAdaptationAborted(!answer
.final
);
677 ServerStateData::handleAdaptedHeader(HttpMsg
*msg
)
679 if (abortOnBadEntry("entry went bad while waiting for adapted headers")) {
680 // If the adapted response has a body, the ICAP side needs to know
681 // that nobody will consume that body. We will be destroyed upon
682 // return. Tell the ICAP side that it is on its own.
683 HttpReply
*rep
= dynamic_cast<HttpReply
*>(msg
);
685 if (rep
->body_pipe
!= NULL
)
686 rep
->body_pipe
->expectNoConsumption();
691 HttpReply
*rep
= dynamic_cast<HttpReply
*>(msg
);
693 debugs(11,5, HERE
<< this << " setting adapted reply to " << rep
);
696 assert(!adaptedBodySource
);
697 if (rep
->body_pipe
!= NULL
) {
698 // subscribe to receive adapted body
699 adaptedBodySource
= rep
->body_pipe
;
700 // assume that ICAP does not auto-consume on failures
701 assert(adaptedBodySource
->setConsumerIfNotLate(this));
704 if (doneWithAdaptation()) // we may still be sending virgin response
705 handleAdaptationCompleted();
710 ServerStateData::resumeBodyStorage()
712 if (abortOnBadEntry("store entry aborted while kick producer callback"))
715 if (!adaptedBodySource
)
718 handleMoreAdaptedBodyAvailable();
720 if (adaptedBodySource
!= NULL
&& adaptedBodySource
->exhausted())
721 endAdaptedBodyConsumption();
724 // more adapted response body is available
726 ServerStateData::handleMoreAdaptedBodyAvailable()
728 if (abortOnBadEntry("entry refuses adapted body"))
733 size_t contentSize
= adaptedBodySource
->buf().contentSize();
736 return; // XXX: bytesWanted asserts on zero-size ranges
738 const size_t spaceAvailable
= entry
->bytesWanted(Range
<size_t>(0, contentSize
), true);
740 if (spaceAvailable
< contentSize
) {
741 // No or partial body data consuming
742 typedef NullaryMemFunT
<ServerStateData
> Dialer
;
743 AsyncCall::Pointer call
= asyncCall(93, 5, "ServerStateData::resumeBodyStorage",
744 Dialer(this, &ServerStateData::resumeBodyStorage
));
745 entry
->deferProducer(call
);
748 if (!spaceAvailable
) {
749 debugs(11, 5, HERE
<< "NOT storing " << contentSize
<< " bytes of adapted " <<
750 "response body at offset " << adaptedBodySource
->consumedSize());
754 if (spaceAvailable
< contentSize
) {
755 debugs(11, 5, HERE
<< "postponing storage of " <<
756 (contentSize
- spaceAvailable
) << " body bytes");
757 contentSize
= spaceAvailable
;
760 debugs(11,5, HERE
<< "storing " << contentSize
<< " bytes of adapted " <<
761 "response body at offset " << adaptedBodySource
->consumedSize());
763 BodyPipeCheckout
bpc(*adaptedBodySource
);
764 const StoreIOBuffer
ioBuf(&bpc
.buf
, currentOffset
, contentSize
);
765 currentOffset
+= ioBuf
.length
;
767 bpc
.buf
.consume(contentSize
);
771 // the entire adapted response body was produced, successfully
773 ServerStateData::handleAdaptedBodyProductionEnded()
775 if (abortOnBadEntry("entry went bad while waiting for adapted body eof"))
778 // end consumption if we consumed everything
779 if (adaptedBodySource
!= NULL
&& adaptedBodySource
->exhausted())
780 endAdaptedBodyConsumption();
781 // else resumeBodyStorage() will eventually consume the rest
785 ServerStateData::endAdaptedBodyConsumption()
787 stopConsumingFrom(adaptedBodySource
);
788 handleAdaptationCompleted();
791 // premature end of the adapted response body
792 void ServerStateData::handleAdaptedBodyProducerAborted()
794 stopConsumingFrom(adaptedBodySource
);
795 handleAdaptationAborted();
798 // common part of noteAdaptationAnswer and handleAdaptedBodyProductionEnded
800 ServerStateData::handleAdaptationCompleted()
802 debugs(11,5, HERE
<< "handleAdaptationCompleted");
805 // We stop reading origin response because we have no place to put it and
806 // cannot use it. If some origin servers do not like that or if we want to
807 // reuse more pconns, we can add code to discard unneeded origin responses.
808 if (!doneWithServer()) {
809 debugs(11,3, HERE
<< "closing origin conn due to ICAP completion");
813 completeForwarding();
817 // common part of noteAdaptation*Aborted and noteBodyConsumerAborted methods
819 ServerStateData::handleAdaptationAborted(bool bypassable
)
821 debugs(11,5, HERE
<< "handleAdaptationAborted; bypassable: " << bypassable
<<
822 ", entry empty: " << entry
->isEmpty());
824 if (abortOnBadEntry("entry went bad while ICAP aborted"))
827 // TODO: bypass if possible
829 if (entry
->isEmpty()) {
830 debugs(11,9, HERE
<< "creating ICAP error entry after ICAP failure");
831 ErrorState
*err
= new ErrorState(ERR_ICAP_FAILURE
, HTTP_INTERNAL_SERVER_ERROR
, request
);
832 err
->detailError(ERR_DETAIL_ICAP_RESPMOD_EARLY
);
834 fwd
->dontRetry(true);
835 } else if (request
) { // update logged info directly
836 request
->detailError(ERR_ICAP_FAILURE
, ERR_DETAIL_ICAP_RESPMOD_LATE
);
839 abortTransaction("ICAP failure");
842 // adaptation service wants us to deny HTTP client access to this response
844 ServerStateData::handleAdaptationBlocked(const Adaptation::Answer
&answer
)
846 debugs(11,5, HERE
<< answer
.ruleId
);
848 if (abortOnBadEntry("entry went bad while ICAP aborted"))
851 if (!entry
->isEmpty()) { // too late to block (should not really happen)
853 request
->detailError(ERR_ICAP_FAILURE
, ERR_DETAIL_RESPMOD_BLOCK_LATE
);
854 abortTransaction("late adaptation block");
858 debugs(11,7, HERE
<< "creating adaptation block response");
861 aclGetDenyInfoPage(&Config
.denyInfoList
, answer
.ruleId
.termedBuf(), 1);
862 if (page_id
== ERR_NONE
)
863 page_id
= ERR_ACCESS_DENIED
;
865 ErrorState
*err
= new ErrorState(page_id
, HTTP_FORBIDDEN
, request
);
866 err
->detailError(ERR_DETAIL_RESPMOD_BLOCK_EARLY
);
868 fwd
->dontRetry(true);
870 abortTransaction("timely adaptation block");
874 ServerStateData::noteAdaptationAclCheckDone(Adaptation::ServiceGroupPointer group
)
876 adaptationAccessCheckPending
= false;
878 if (abortOnBadEntry("entry went bad while waiting for ICAP ACL check"))
881 // TODO: Should nonICAP and postICAP path check this on the server-side?
882 // That check now only happens on client-side, in processReplyAccess().
883 if (virginReply()->expectedBodyTooLarge(*request
)) {
884 sendBodyIsTooLargeError();
887 // TODO: Should we check receivedBodyTooLarge on the server-side as well?
890 debugs(11,3, HERE
<< "no adapation needed");
891 setFinalReply(virginReply());
896 startAdaptation(group
, originalRequest());
902 ServerStateData::sendBodyIsTooLargeError()
904 ErrorState
*err
= new ErrorState(ERR_TOO_BIG
, HTTP_FORBIDDEN
, request
);
906 fwd
->dontRetry(true);
907 abortTransaction("Virgin body too large.");
910 // TODO: when HttpStateData sends all errors to ICAP,
911 // we should be able to move this at the end of setVirginReply().
913 ServerStateData::adaptOrFinalizeReply()
916 // TODO: merge with client side and return void to hide the on/off logic?
917 // The callback can be called with a NULL service if adaptation is off.
918 adaptationAccessCheckPending
= Adaptation::AccessCheck::Start(
919 Adaptation::methodRespmod
, Adaptation::pointPreCache
,
920 originalRequest(), virginReply(), this);
921 debugs(11,5, HERE
<< "adaptationAccessCheckPending=" << adaptationAccessCheckPending
);
922 if (adaptationAccessCheckPending
)
926 setFinalReply(virginReply());
929 /// initializes bodyBytesRead stats if needed and applies delta
931 ServerStateData::adjustBodyBytesRead(const int64_t delta
)
933 int64_t &bodyBytesRead
= originalRequest()->hier
.bodyBytesRead
;
935 // if we got here, do not log a dash even if we got nothing from the server
936 if (bodyBytesRead
< 0)
939 bodyBytesRead
+= delta
; // supports negative and zero deltas
941 // check for overflows ("infinite" response?) and undeflows (a bug)
942 Must(bodyBytesRead
>= 0);
946 ServerStateData::addVirginReplyBody(const char *data
, ssize_t len
)
948 adjustBodyBytesRead(len
);
951 assert(!adaptationAccessCheckPending
); // or would need to buffer while waiting
952 if (startedAdaptation
) {
953 adaptVirginReplyBody(data
, len
);
957 storeReplyBody(data
, len
);
960 // writes virgin or adapted reply body to store
962 ServerStateData::storeReplyBody(const char *data
, ssize_t len
)
964 // write even if len is zero to push headers towards the client side
965 entry
->write (StoreIOBuffer(len
, currentOffset
, (char*)data
));
967 currentOffset
+= len
;
970 size_t ServerStateData::replyBodySpace(const MemBuf
&readBuf
,
971 const size_t minSpace
) const
973 size_t space
= readBuf
.spaceSize(); // available space w/o heroic measures
974 if (space
< minSpace
) {
975 const size_t maxSpace
= readBuf
.potentialSpaceSize(); // absolute best
976 space
= min(minSpace
, maxSpace
); // do not promise more than asked
980 if (responseBodyBuffer
) {
981 return 0; // Stop reading if already overflowed waiting for ICAP to catch up
984 if (virginBodyDestination
!= NULL
) {
986 * BodyPipe buffer has a finite size limit. We
987 * should not read more data from the network than will fit
988 * into the pipe buffer or we _lose_ what did not fit if
989 * the response ends sooner that BodyPipe frees up space:
990 * There is no code to keep pumping data into the pipe once
991 * response ends and serverComplete() is called.
993 * If the pipe is totally full, don't register the read handler.
994 * The BodyPipe will call our noteMoreBodySpaceAvailable() method
995 * when it has free space again.
997 size_t adaptation_space
=
998 virginBodyDestination
->buf().potentialSpaceSize();
1000 debugs(11,9, "ServerStateData may read up to min(" <<
1001 adaptation_space
<< ", " << space
<< ") bytes");
1003 if (adaptation_space
< space
)
1004 space
= adaptation_space
;