3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "acl/Gadgets.h"
35 #include "base/TextException.h"
36 #include "comm/Connection.h"
37 #include "comm/forward.h"
38 #include "comm/Write.h"
39 #include "err_detail_type.h"
40 #include "errorpage.h"
42 #include "HttpHdrContRange.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
46 #include "SquidTime.h"
47 #include "StatCounters.h"
53 #include "adaptation/AccessCheck.h"
54 #include "adaptation/Answer.h"
55 #include "adaptation/Iterator.h"
56 #include "base/AsyncCall.h"
57 #include "SquidConfig.h"
60 // implemented in client_side_reply.cc until sides have a common parent
61 void purgeEntriesByUrl(HttpRequest
* req
, const char *url
);
63 ServerStateData::ServerStateData(FwdState
*theFwdState
): AsyncJob("ServerStateData"),
66 adaptedHeadSource(NULL
),
67 adaptationAccessCheckPending(false),
68 startedAdaptation(false),
70 receivedWholeRequestBody(false),
77 entry
->lock("ServerStateData");
79 request
= fwd
->request
;
83 ServerStateData::~ServerStateData()
85 // paranoid: check that swanSong has been called
86 assert(!requestBodySource
);
88 assert(!virginBodyDestination
);
89 assert(!adaptedBodySource
);
92 entry
->unlock("ServerStateData");
94 HTTPMSGUNLOCK(request
);
95 HTTPMSGUNLOCK(theVirginReply
);
96 HTTPMSGUNLOCK(theFinalReply
);
98 fwd
= NULL
; // refcounted
100 if (responseBodyBuffer
!= NULL
) {
101 delete responseBodyBuffer
;
102 responseBodyBuffer
= NULL
;
107 ServerStateData::swanSong()
109 // get rid of our piping obligations
110 if (requestBodySource
!= NULL
)
111 stopConsumingFrom(requestBodySource
);
117 BodyConsumer::swanSong();
119 Initiator::swanSong();
120 BodyProducer::swanSong();
123 // paranoid: check that swanSong has been called
124 // extra paranoid: yeah, I really mean it. they MUST pass here.
125 assert(!requestBodySource
);
127 assert(!virginBodyDestination
);
128 assert(!adaptedBodySource
);
133 ServerStateData::virginReply()
135 assert(theVirginReply
);
136 return theVirginReply
;
140 ServerStateData::virginReply() const
142 assert(theVirginReply
);
143 return theVirginReply
;
147 ServerStateData::setVirginReply(HttpReply
*rep
)
149 debugs(11,5, HERE
<< this << " setting virgin reply to " << rep
);
150 assert(!theVirginReply
);
152 theVirginReply
= rep
;
153 HTTPMSGLOCK(theVirginReply
);
154 return theVirginReply
;
158 ServerStateData::finalReply()
160 assert(theFinalReply
);
161 return theFinalReply
;
165 ServerStateData::setFinalReply(HttpReply
*rep
)
167 debugs(11,5, HERE
<< this << " setting final reply to " << rep
);
169 assert(!theFinalReply
);
172 HTTPMSGLOCK(theFinalReply
);
174 // give entry the reply because haveParsedReplyHeaders() expects it there
175 entry
->replaceHttpReply(theFinalReply
, false); // but do not write yet
176 haveParsedReplyHeaders(); // update the entry/reply (e.g., set timestamps)
177 entry
->startWriting(); // write the updated entry to store
179 return theFinalReply
;
182 // called when no more server communication is expected; may quit
184 ServerStateData::serverComplete()
186 debugs(11,5,HERE
<< "serverComplete " << this);
188 if (!doneWithServer()) {
190 assert(doneWithServer());
195 HttpRequest
*r
= originalRequest();
196 r
->hier
.total_response_time
= r
->hier
.first_conn_start
.tv_sec
?
197 tvSubMsec(r
->hier
.first_conn_start
, current_time
) : -1;
199 if (requestBodySource
!= NULL
)
200 stopConsumingFrom(requestBodySource
);
202 if (responseBodyBuffer
!= NULL
)
209 ServerStateData::serverComplete2()
211 debugs(11,5,HERE
<< "serverComplete2 " << this);
214 if (virginBodyDestination
!= NULL
)
215 stopProducingFor(virginBodyDestination
, true);
217 if (!doneWithAdaptation())
221 completeForwarding();
224 bool ServerStateData::doneAll() const
226 return doneWithServer() &&
228 doneWithAdaptation() &&
229 Adaptation::Initiator::doneAll() &&
230 BodyProducer::doneAll() &&
232 BodyConsumer::doneAll();
235 // FTP side overloads this to work around multiple calls to fwd->complete
237 ServerStateData::completeForwarding()
239 debugs(11,5, HERE
<< "completing forwarding for " << fwd
);
244 // Register to receive request body
245 bool ServerStateData::startRequestBodyFlow()
247 HttpRequest
*r
= originalRequest();
248 assert(r
->body_pipe
!= NULL
);
249 requestBodySource
= r
->body_pipe
;
250 if (requestBodySource
->setConsumerIfNotLate(this)) {
251 debugs(11,3, HERE
<< "expecting request body from " <<
252 requestBodySource
->status());
256 debugs(11,3, HERE
<< "aborting on partially consumed request body: " <<
257 requestBodySource
->status());
258 requestBodySource
= NULL
;
262 // Entry-dependent callbacks use this check to quit if the entry went bad
264 ServerStateData::abortOnBadEntry(const char *abortReason
)
266 if (entry
->isAccepting())
269 debugs(11,5, HERE
<< "entry is not Accepting!");
270 abortTransaction(abortReason
);
274 // more request or adapted response body is available
276 ServerStateData::noteMoreBodyDataAvailable(BodyPipe::Pointer bp
)
279 if (adaptedBodySource
== bp
) {
280 handleMoreAdaptedBodyAvailable();
284 if (requestBodySource
== bp
)
285 handleMoreRequestBodyAvailable();
288 // the entire request or adapted response body was provided, successfully
290 ServerStateData::noteBodyProductionEnded(BodyPipe::Pointer bp
)
293 if (adaptedBodySource
== bp
) {
294 handleAdaptedBodyProductionEnded();
298 if (requestBodySource
== bp
)
299 handleRequestBodyProductionEnded();
302 // premature end of the request or adapted response body production
304 ServerStateData::noteBodyProducerAborted(BodyPipe::Pointer bp
)
307 if (adaptedBodySource
== bp
) {
308 handleAdaptedBodyProducerAborted();
312 if (requestBodySource
== bp
)
313 handleRequestBodyProducerAborted();
316 // more origin request body data is available
318 ServerStateData::handleMoreRequestBodyAvailable()
321 sendMoreRequestBody();
323 debugs(9,3, HERE
<< "waiting for request body write to complete");
326 // there will be no more handleMoreRequestBodyAvailable calls
328 ServerStateData::handleRequestBodyProductionEnded()
330 receivedWholeRequestBody
= true;
332 doneSendingRequestBody();
334 debugs(9,3, HERE
<< "waiting for request body write to complete");
337 // called when we are done sending request body; kids extend this
339 ServerStateData::doneSendingRequestBody()
341 debugs(9,3, HERE
<< "done sending request body");
342 assert(requestBodySource
!= NULL
);
343 stopConsumingFrom(requestBodySource
);
348 // called when body producers aborts; kids extend this
350 ServerStateData::handleRequestBodyProducerAborted()
352 if (requestSender
!= NULL
)
353 debugs(9,3, HERE
<< "fyi: request body aborted while we were sending");
355 fwd
->dontRetry(true); // the problem is not with the server
356 stopConsumingFrom(requestBodySource
); // requestSender, if any, will notice
361 // called when we wrote request headers(!) or a part of the body
363 ServerStateData::sentRequestBody(const CommIoCbParams
&io
)
365 debugs(11, 5, "sentRequestBody: FD " << io
.fd
<< ": size " << io
.size
<< ": errflag " << io
.flag
<< ".");
366 debugs(32,3,HERE
<< "sentRequestBody called");
368 requestSender
= NULL
;
371 fd_bytes(io
.fd
, io
.size
, FD_WRITE
);
372 kb_incr(&(statCounter
.server
.all
.kbytes_out
), io
.size
);
373 // kids should increment their counters
376 if (io
.flag
== COMM_ERR_CLOSING
)
379 if (!requestBodySource
) {
380 debugs(9,3, HERE
<< "detected while-we-were-sending abort");
381 return; // do nothing;
385 debugs(11, DBG_IMPORTANT
, "sentRequestBody error: FD " << io
.fd
<< ": " << xstrerr(io
.xerrno
));
387 err
= new ErrorState(ERR_WRITE_ERROR
, Http::scBadGateway
, fwd
->request
);
388 err
->xerrno
= io
.xerrno
;
390 abortTransaction("I/O error while sending request body");
394 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
395 abortTransaction("store entry aborted while sending request body");
399 if (!requestBodySource
->exhausted())
400 sendMoreRequestBody();
401 else if (receivedWholeRequestBody
)
402 doneSendingRequestBody();
404 debugs(9,3, HERE
<< "waiting for body production end or abort");
408 ServerStateData::sendMoreRequestBody()
410 assert(requestBodySource
!= NULL
);
411 assert(!requestSender
);
413 const Comm::ConnectionPointer conn
= dataConnection();
415 if (!Comm::IsConnOpen(conn
)) {
416 debugs(9,3, HERE
<< "cannot send request body to closing " << conn
);
417 return; // wait for the kid's close handler; TODO: assert(closer);
421 if (getMoreRequestBody(buf
) && buf
.contentSize() > 0) {
422 debugs(9,3, HERE
<< "will write " << buf
.contentSize() << " request body bytes");
423 typedef CommCbMemFunT
<ServerStateData
, CommIoCbParams
> Dialer
;
424 requestSender
= JobCallback(93,3, Dialer
, this, ServerStateData::sentRequestBody
);
425 Comm::Write(conn
, &buf
, requestSender
);
427 debugs(9,3, HERE
<< "will wait for more request body bytes or eof");
428 requestSender
= NULL
;
432 /// either fill buf with available [encoded] request body bytes or return false
434 ServerStateData::getMoreRequestBody(MemBuf
&buf
)
436 // default implementation does not encode request body content
437 Must(requestBodySource
!= NULL
);
438 return requestBodySource
->getMoreData(buf
);
441 // Compares hosts in urls, returns false if different, no sheme, or no host.
443 sameUrlHosts(const char *url1
, const char *url2
)
445 // XXX: Want urlHostname() here, but it uses static storage and copying
446 const char *host1
= strchr(url1
, ':');
447 const char *host2
= strchr(url2
, ':');
449 if (host1
&& host2
) {
450 // skip scheme slashes
454 } while (*host1
== '/' && *host2
== '/');
457 return false; // no host
459 // increment while the same until we reach the end of the URL/host
460 while (*host1
&& *host1
!= '/' && *host1
== *host2
) {
464 return *host1
== *host2
;
467 return false; // no URL scheme
470 // purges entries that match the value of a given HTTP [response] header
472 purgeEntriesByHeader(HttpRequest
*req
, const char *reqUrl
, HttpMsg
*rep
, http_hdr_type hdr
)
474 const char *hdrUrl
, *absUrl
;
477 hdrUrl
= rep
->header
.getStr(hdr
);
478 if (hdrUrl
== NULL
) {
483 * If the URL is relative, make it absolute so we can find it.
484 * If it's absolute, make sure the host parts match to avoid DOS attacks
485 * as per RFC 2616 13.10.
487 if (urlIsRelative(hdrUrl
)) {
488 absUrl
= urlMakeAbsolute(req
, hdrUrl
);
489 if (absUrl
!= NULL
) {
492 } else if (!sameUrlHosts(reqUrl
, hdrUrl
)) {
496 purgeEntriesByUrl(req
, hdrUrl
);
498 if (absUrl
!= NULL
) {
503 // some HTTP methods should purge matching cache entries
505 ServerStateData::maybePurgeOthers()
507 // only some HTTP methods should purge matching cache entries
508 if (!request
->method
.purgesOthers())
511 // and probably only if the response was successful
512 if (theFinalReply
->sline
.status() >= 400)
515 // XXX: should we use originalRequest() here?
516 const char *reqUrl
= urlCanonical(request
);
517 debugs(88, 5, "maybe purging due to " << RequestMethodStr(request
->method
) << ' ' << reqUrl
);
518 purgeEntriesByUrl(request
, reqUrl
);
519 purgeEntriesByHeader(request
, reqUrl
, theFinalReply
, HDR_LOCATION
);
520 purgeEntriesByHeader(request
, reqUrl
, theFinalReply
, HDR_CONTENT_LOCATION
);
523 /// called when we have final (possibly adapted) reply headers; kids extend
525 ServerStateData::haveParsedReplyHeaders()
530 // adaptation may overwrite old offset computed using the virgin response
531 const bool partial
= theFinalReply
->content_range
&&
532 theFinalReply
->sline
.status() == Http::scPartialContent
;
533 currentOffset
= partial
? theFinalReply
->content_range
->spec
.offset
: 0;
537 ServerStateData::originalRequest()
543 /// Initiate an asynchronous adaptation transaction which will call us back.
545 ServerStateData::startAdaptation(const Adaptation::ServiceGroupPointer
&group
, HttpRequest
*cause
)
547 debugs(11, 5, "ServerStateData::startAdaptation() called");
548 // check whether we should be sending a body as well
549 // start body pipe to feed ICAP transaction if needed
550 assert(!virginBodyDestination
);
551 HttpReply
*vrep
= virginReply();
552 assert(!vrep
->body_pipe
);
554 if (vrep
->expectingBody(cause
->method
, size
) && size
) {
555 virginBodyDestination
= new BodyPipe(this);
556 vrep
->body_pipe
= virginBodyDestination
;
557 debugs(93, 6, HERE
<< "will send virgin reply body to " <<
558 virginBodyDestination
<< "; size: " << size
);
560 virginBodyDestination
->setBodySize(size
);
563 adaptedHeadSource
= initiateAdaptation(
564 new Adaptation::Iterator(vrep
, cause
, fwd
->al
, group
));
565 startedAdaptation
= initiated(adaptedHeadSource
);
566 Must(startedAdaptation
);
569 // properly cleans up ICAP-related state
570 // may be called multiple times
571 void ServerStateData::cleanAdaptation()
573 debugs(11,5, HERE
<< "cleaning ICAP; ACL: " << adaptationAccessCheckPending
);
575 if (virginBodyDestination
!= NULL
)
576 stopProducingFor(virginBodyDestination
, false);
578 announceInitiatorAbort(adaptedHeadSource
);
580 if (adaptedBodySource
!= NULL
)
581 stopConsumingFrom(adaptedBodySource
);
583 if (!adaptationAccessCheckPending
) // we cannot cancel a pending callback
584 assert(doneWithAdaptation()); // make sure the two methods are in sync
588 ServerStateData::doneWithAdaptation() const
590 return !adaptationAccessCheckPending
&&
591 !virginBodyDestination
&& !adaptedHeadSource
&& !adaptedBodySource
;
594 // sends virgin reply body to ICAP, buffering excesses if needed
596 ServerStateData::adaptVirginReplyBody(const char *data
, ssize_t len
)
598 assert(startedAdaptation
);
600 if (!virginBodyDestination
) {
601 debugs(11,3, HERE
<< "ICAP does not want more virgin body");
605 // grow overflow area if already overflowed
606 if (responseBodyBuffer
) {
607 responseBodyBuffer
->append(data
, len
);
608 data
= responseBodyBuffer
->content();
609 len
= responseBodyBuffer
->contentSize();
612 const ssize_t putSize
= virginBodyDestination
->putMoreData(data
, len
);
616 // if we had overflow area, shrink it as necessary
617 if (responseBodyBuffer
) {
618 if (putSize
== responseBodyBuffer
->contentSize()) {
619 delete responseBodyBuffer
;
620 responseBodyBuffer
= NULL
;
622 responseBodyBuffer
->consume(putSize
);
627 // if we did not have an overflow area, create it as needed
629 assert(!responseBodyBuffer
);
630 responseBodyBuffer
= new MemBuf
;
631 responseBodyBuffer
->init(4096, SQUID_TCP_SO_RCVBUF
* 10);
632 responseBodyBuffer
->append(data
, len
);
636 // can supply more virgin response body data
638 ServerStateData::noteMoreBodySpaceAvailable(BodyPipe::Pointer
)
640 if (responseBodyBuffer
) {
641 addVirginReplyBody(NULL
, 0); // kick the buffered fragment alive again
642 if (completed
&& !responseBodyBuffer
) {
647 maybeReadVirginBody();
650 // the consumer of our virgin response body aborted
652 ServerStateData::noteBodyConsumerAborted(BodyPipe::Pointer
)
654 stopProducingFor(virginBodyDestination
, false);
656 // do not force closeServer here in case we need to bypass AdaptationQueryAbort
658 if (doneWithAdaptation()) // we may still be receiving adapted response
659 handleAdaptationCompleted();
662 // received adapted response headers (body may follow)
664 ServerStateData::noteAdaptationAnswer(const Adaptation::Answer
&answer
)
666 clearAdaptation(adaptedHeadSource
); // we do not expect more messages
668 switch (answer
.kind
) {
669 case Adaptation::Answer::akForward
:
670 handleAdaptedHeader(const_cast<HttpMsg
*>(answer
.message
.getRaw()));
673 case Adaptation::Answer::akBlock
:
674 handleAdaptationBlocked(answer
);
677 case Adaptation::Answer::akError
:
678 handleAdaptationAborted(!answer
.final
);
684 ServerStateData::handleAdaptedHeader(HttpMsg
*msg
)
686 if (abortOnBadEntry("entry went bad while waiting for adapted headers")) {
687 // If the adapted response has a body, the ICAP side needs to know
688 // that nobody will consume that body. We will be destroyed upon
689 // return. Tell the ICAP side that it is on its own.
690 HttpReply
*rep
= dynamic_cast<HttpReply
*>(msg
);
692 if (rep
->body_pipe
!= NULL
)
693 rep
->body_pipe
->expectNoConsumption();
698 HttpReply
*rep
= dynamic_cast<HttpReply
*>(msg
);
700 debugs(11,5, HERE
<< this << " setting adapted reply to " << rep
);
703 assert(!adaptedBodySource
);
704 if (rep
->body_pipe
!= NULL
) {
705 // subscribe to receive adapted body
706 adaptedBodySource
= rep
->body_pipe
;
707 // assume that ICAP does not auto-consume on failures
708 const bool result
= adaptedBodySource
->setConsumerIfNotLate(this);
712 if (doneWithAdaptation()) // we may still be sending virgin response
713 handleAdaptationCompleted();
718 ServerStateData::resumeBodyStorage()
720 if (abortOnBadEntry("store entry aborted while kick producer callback"))
723 if (!adaptedBodySource
)
726 handleMoreAdaptedBodyAvailable();
728 if (adaptedBodySource
!= NULL
&& adaptedBodySource
->exhausted())
729 endAdaptedBodyConsumption();
732 // more adapted response body is available
734 ServerStateData::handleMoreAdaptedBodyAvailable()
736 if (abortOnBadEntry("entry refuses adapted body"))
741 size_t contentSize
= adaptedBodySource
->buf().contentSize();
744 return; // XXX: bytesWanted asserts on zero-size ranges
746 const size_t spaceAvailable
= entry
->bytesWanted(Range
<size_t>(0, contentSize
), true);
748 if (spaceAvailable
< contentSize
) {
749 // No or partial body data consuming
750 typedef NullaryMemFunT
<ServerStateData
> Dialer
;
751 AsyncCall::Pointer call
= asyncCall(93, 5, "ServerStateData::resumeBodyStorage",
752 Dialer(this, &ServerStateData::resumeBodyStorage
));
753 entry
->deferProducer(call
);
756 if (!spaceAvailable
) {
757 debugs(11, 5, HERE
<< "NOT storing " << contentSize
<< " bytes of adapted " <<
758 "response body at offset " << adaptedBodySource
->consumedSize());
762 if (spaceAvailable
< contentSize
) {
763 debugs(11, 5, HERE
<< "postponing storage of " <<
764 (contentSize
- spaceAvailable
) << " body bytes");
765 contentSize
= spaceAvailable
;
768 debugs(11,5, HERE
<< "storing " << contentSize
<< " bytes of adapted " <<
769 "response body at offset " << adaptedBodySource
->consumedSize());
771 BodyPipeCheckout
bpc(*adaptedBodySource
);
772 const StoreIOBuffer
ioBuf(&bpc
.buf
, currentOffset
, contentSize
);
773 currentOffset
+= ioBuf
.length
;
775 bpc
.buf
.consume(contentSize
);
779 // the entire adapted response body was produced, successfully
781 ServerStateData::handleAdaptedBodyProductionEnded()
783 if (abortOnBadEntry("entry went bad while waiting for adapted body eof"))
786 // end consumption if we consumed everything
787 if (adaptedBodySource
!= NULL
&& adaptedBodySource
->exhausted())
788 endAdaptedBodyConsumption();
789 // else resumeBodyStorage() will eventually consume the rest
793 ServerStateData::endAdaptedBodyConsumption()
795 stopConsumingFrom(adaptedBodySource
);
796 handleAdaptationCompleted();
799 // premature end of the adapted response body
800 void ServerStateData::handleAdaptedBodyProducerAborted()
802 stopConsumingFrom(adaptedBodySource
);
803 handleAdaptationAborted();
806 // common part of noteAdaptationAnswer and handleAdaptedBodyProductionEnded
808 ServerStateData::handleAdaptationCompleted()
810 debugs(11,5, HERE
<< "handleAdaptationCompleted");
813 // We stop reading origin response because we have no place to put it and
814 // cannot use it. If some origin servers do not like that or if we want to
815 // reuse more pconns, we can add code to discard unneeded origin responses.
816 if (!doneWithServer()) {
817 debugs(11,3, HERE
<< "closing origin conn due to ICAP completion");
821 completeForwarding();
824 // common part of noteAdaptation*Aborted and noteBodyConsumerAborted methods
826 ServerStateData::handleAdaptationAborted(bool bypassable
)
828 debugs(11,5, HERE
<< "handleAdaptationAborted; bypassable: " << bypassable
<<
829 ", entry empty: " << entry
->isEmpty());
831 if (abortOnBadEntry("entry went bad while ICAP aborted"))
834 // TODO: bypass if possible
836 if (entry
->isEmpty()) {
837 debugs(11,9, HERE
<< "creating ICAP error entry after ICAP failure");
838 ErrorState
*err
= new ErrorState(ERR_ICAP_FAILURE
, Http::scInternalServerError
, request
);
839 err
->detailError(ERR_DETAIL_ICAP_RESPMOD_EARLY
);
841 fwd
->dontRetry(true);
842 } else if (request
) { // update logged info directly
843 request
->detailError(ERR_ICAP_FAILURE
, ERR_DETAIL_ICAP_RESPMOD_LATE
);
846 abortTransaction("ICAP failure");
849 // adaptation service wants us to deny HTTP client access to this response
851 ServerStateData::handleAdaptationBlocked(const Adaptation::Answer
&answer
)
853 debugs(11,5, HERE
<< answer
.ruleId
);
855 if (abortOnBadEntry("entry went bad while ICAP aborted"))
858 if (!entry
->isEmpty()) { // too late to block (should not really happen)
860 request
->detailError(ERR_ICAP_FAILURE
, ERR_DETAIL_RESPMOD_BLOCK_LATE
);
861 abortTransaction("late adaptation block");
865 debugs(11,7, HERE
<< "creating adaptation block response");
868 aclGetDenyInfoPage(&Config
.denyInfoList
, answer
.ruleId
.termedBuf(), 1);
869 if (page_id
== ERR_NONE
)
870 page_id
= ERR_ACCESS_DENIED
;
872 ErrorState
*err
= new ErrorState(page_id
, Http::scForbidden
, request
);
873 err
->detailError(ERR_DETAIL_RESPMOD_BLOCK_EARLY
);
875 fwd
->dontRetry(true);
877 abortTransaction("timely adaptation block");
881 ServerStateData::noteAdaptationAclCheckDone(Adaptation::ServiceGroupPointer group
)
883 adaptationAccessCheckPending
= false;
885 if (abortOnBadEntry("entry went bad while waiting for ICAP ACL check"))
888 // TODO: Should nonICAP and postICAP path check this on the server-side?
889 // That check now only happens on client-side, in processReplyAccess().
890 if (virginReply()->expectedBodyTooLarge(*request
)) {
891 sendBodyIsTooLargeError();
894 // TODO: Should we check receivedBodyTooLarge on the server-side as well?
897 debugs(11,3, HERE
<< "no adapation needed");
898 setFinalReply(virginReply());
903 startAdaptation(group
, originalRequest());
909 ServerStateData::sendBodyIsTooLargeError()
911 ErrorState
*err
= new ErrorState(ERR_TOO_BIG
, Http::scForbidden
, request
);
913 fwd
->dontRetry(true);
914 abortTransaction("Virgin body too large.");
917 // TODO: when HttpStateData sends all errors to ICAP,
918 // we should be able to move this at the end of setVirginReply().
920 ServerStateData::adaptOrFinalizeReply()
923 // TODO: merge with client side and return void to hide the on/off logic?
924 // The callback can be called with a NULL service if adaptation is off.
925 adaptationAccessCheckPending
= Adaptation::AccessCheck::Start(
926 Adaptation::methodRespmod
, Adaptation::pointPreCache
,
927 originalRequest(), virginReply(), fwd
->al
, this);
928 debugs(11,5, HERE
<< "adaptationAccessCheckPending=" << adaptationAccessCheckPending
);
929 if (adaptationAccessCheckPending
)
933 setFinalReply(virginReply());
936 /// initializes bodyBytesRead stats if needed and applies delta
938 ServerStateData::adjustBodyBytesRead(const int64_t delta
)
940 int64_t &bodyBytesRead
= originalRequest()->hier
.bodyBytesRead
;
942 // if we got here, do not log a dash even if we got nothing from the server
943 if (bodyBytesRead
< 0)
946 bodyBytesRead
+= delta
; // supports negative and zero deltas
948 // check for overflows ("infinite" response?) and undeflows (a bug)
949 Must(bodyBytesRead
>= 0);
953 ServerStateData::addVirginReplyBody(const char *data
, ssize_t len
)
955 adjustBodyBytesRead(len
);
958 assert(!adaptationAccessCheckPending
); // or would need to buffer while waiting
959 if (startedAdaptation
) {
960 adaptVirginReplyBody(data
, len
);
964 storeReplyBody(data
, len
);
967 // writes virgin or adapted reply body to store
969 ServerStateData::storeReplyBody(const char *data
, ssize_t len
)
971 // write even if len is zero to push headers towards the client side
972 entry
->write (StoreIOBuffer(len
, currentOffset
, (char*)data
));
974 currentOffset
+= len
;
977 size_t ServerStateData::replyBodySpace(const MemBuf
&readBuf
,
978 const size_t minSpace
) const
980 size_t space
= readBuf
.spaceSize(); // available space w/o heroic measures
981 if (space
< minSpace
) {
982 const size_t maxSpace
= readBuf
.potentialSpaceSize(); // absolute best
983 space
= min(minSpace
, maxSpace
); // do not promise more than asked
987 if (responseBodyBuffer
) {
988 return 0; // Stop reading if already overflowed waiting for ICAP to catch up
991 if (virginBodyDestination
!= NULL
) {
993 * BodyPipe buffer has a finite size limit. We
994 * should not read more data from the network than will fit
995 * into the pipe buffer or we _lose_ what did not fit if
996 * the response ends sooner that BodyPipe frees up space:
997 * There is no code to keep pumping data into the pipe once
998 * response ends and serverComplete() is called.
1000 * If the pipe is totally full, don't register the read handler.
1001 * The BodyPipe will call our noteMoreBodySpaceAvailable() method
1002 * when it has free space again.
1004 size_t adaptation_space
=
1005 virginBodyDestination
->buf().potentialSpaceSize();
1007 debugs(11,9, "ServerStateData may read up to min(" <<
1008 adaptation_space
<< ", " << space
<< ") bytes");
1010 if (adaptation_space
< space
)
1011 space
= adaptation_space
;