2 * DEBUG: section 93 ICAP (RFC 3507) Client
6 #include "AccessLogEntry.h"
7 #include "adaptation/History.h"
8 #include "adaptation/icap/Client.h"
9 #include "adaptation/icap/Config.h"
10 #include "adaptation/icap/History.h"
11 #include "adaptation/icap/Launcher.h"
12 #include "adaptation/icap/ModXact.h"
13 #include "adaptation/icap/ServiceRep.h"
14 #include "adaptation/Initiator.h"
15 #include "auth/UserRequest.h"
16 #include "base/TextException.h"
17 #include "ChunkedCodingParser.h"
20 #include "HttpRequest.h"
21 #include "HttpReply.h"
22 #include "SquidTime.h"
24 // flow and terminology:
25 // HTTP| --> receive --> encode --> write --> |network
26 // end | <-- send <-- parse <-- read <-- |end
28 // TODO: replace gotEncapsulated() with something faster; we call it often
30 CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap
, ModXact
);
31 CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap
, ModXactLauncher
);
33 static const size_t TheBackupLimit
= BodyPipe::MaxCapacity
;
35 Adaptation::Icap::ModXact::State::State()
37 memset(this, 0, sizeof(*this));
40 Adaptation::Icap::ModXact::ModXact(Adaptation::Initiator
*anInitiator
, HttpMsg
*virginHeader
,
41 HttpRequest
*virginCause
, Adaptation::Icap::ServiceRep::Pointer
&aService
):
42 AsyncJob("Adaptation::Icap::ModXact"),
43 Adaptation::Icap::Xaction("Adaptation::Icap::ModXact", anInitiator
, aService
),
46 canStartBypass(false), // too early
47 protectGroupBypass(true),
53 virgin
.setHeader(virginHeader
); // sets virgin.body_pipe if needed
54 virgin
.setCause(virginCause
); // may be NULL
56 // adapted header and body are initialized when we parse them
58 // writing and reading ends are handled by Adaptation::Icap::Xaction
61 // nothing to do because we are using temporary buffers
63 // parsing; TODO: do not set until we parse, see ICAPOptXact
64 icapReply
= HTTPMSGLOCK(new HttpReply
);
65 icapReply
->protoPrefix
= "ICAP/"; // TODO: make an IcapReply class?
67 debugs(93,7, HERE
<< "initialized." << status());
70 // initiator wants us to start
71 void Adaptation::Icap::ModXact::start()
73 Adaptation::Icap::Xaction::start();
75 // reserve an adaptation history slot (attempts are known at this time)
76 Adaptation::History::Pointer ah
= virginRequest().adaptLogHistory();
78 adaptHistoryId
= ah
->recordXactStart(service().cfg().key
, icap_tr_start
, attempts
> 1);
80 estimateVirginBody(); // before virgin disappears!
82 canStartBypass
= service().cfg().bypass
;
84 // it is an ICAP violation to send request to a service w/o known OPTIONS
92 void Adaptation::Icap::ModXact::waitForService()
94 Must(!state
.serviceWaiting
);
95 debugs(93, 7, HERE
<< "will wait for the ICAP service" << status());
96 state
.serviceWaiting
= true;
97 AsyncCall::Pointer call
= asyncCall(93,5, "Adaptation::Icap::ModXact::noteServiceReady",
98 MemFun(this, &Adaptation::Icap::ModXact::noteServiceReady
));
99 service().callWhenReady(call
);
102 void Adaptation::Icap::ModXact::noteServiceReady()
104 Must(state
.serviceWaiting
);
105 state
.serviceWaiting
= false;
107 if (service().up()) {
111 disableRepeats("ICAP service is unusable");
112 throw TexcHere("ICAP service is unusable");
116 void Adaptation::Icap::ModXact::startWriting()
118 state
.writing
= State::writingConnect
;
120 decideOnPreview(); // must be decided before we decideOnRetries
126 // connection with the ICAP service established
127 void Adaptation::Icap::ModXact::handleCommConnected()
129 Must(state
.writing
== State::writingConnect
);
131 startReading(); // wait for early errors from the ICAP server
136 makeRequestHeaders(requestBuf
);
137 debugs(93, 9, HERE
<< "will write" << status() << ":\n" <<
138 (requestBuf
.terminate(), requestBuf
.content()));
141 state
.writing
= State::writingHeaders
;
142 icap_tio_start
= current_time
;
143 scheduleWrite(requestBuf
);
146 void Adaptation::Icap::ModXact::handleCommWrote(size_t sz
)
148 debugs(93, 5, HERE
<< "Wrote " << sz
<< " bytes");
150 if (state
.writing
== State::writingHeaders
)
151 handleCommWroteHeaders();
153 handleCommWroteBody();
156 void Adaptation::Icap::ModXact::handleCommWroteHeaders()
158 Must(state
.writing
== State::writingHeaders
);
160 // determine next step
161 if (preview
.enabled())
162 state
.writing
= preview
.done() ? State::writingPaused
: State::writingPreview
;
163 else if (virginBody
.expected())
164 state
.writing
= State::writingPrime
;
173 void Adaptation::Icap::ModXact::writeMore()
175 debugs(93, 5, HERE
<< "checking whether to write more" << status());
177 if (writer
!= NULL
) // already writing something
180 switch (state
.writing
) {
182 case State::writingInit
: // waiting for service OPTIONS
183 Must(state
.serviceWaiting
);
185 case State::writingConnect
: // waiting for the connection to establish
187 case State::writingHeaders
: // waiting for the headers to be written
189 case State::writingPaused
: // waiting for the ICAP server response
191 case State::writingReallyDone
: // nothing more to write
194 case State::writingAlmostDone
: // was waiting for the last write
198 case State::writingPreview
:
202 case State::writingPrime
:
207 throw TexcHere("Adaptation::Icap::ModXact in bad writing state");
211 void Adaptation::Icap::ModXact::writePreviewBody()
213 debugs(93, 8, HERE
<< "will write Preview body from " <<
214 virgin
.body_pipe
<< status());
215 Must(state
.writing
== State::writingPreview
);
216 Must(virgin
.body_pipe
!= NULL
);
218 const size_t sizeMax
= (size_t)virgin
.body_pipe
->buf().contentSize();
219 const size_t size
= min(preview
.debt(), sizeMax
);
220 writeSomeBody("preview body", size
);
222 // change state once preview is written
224 if (preview
.done()) {
225 debugs(93, 7, HERE
<< "wrote entire Preview body" << status());
230 state
.writing
= State::writingPaused
;
234 void Adaptation::Icap::ModXact::writePrimeBody()
236 Must(state
.writing
== State::writingPrime
);
237 Must(virginBodyWriting
.active());
239 const size_t size
= (size_t)virgin
.body_pipe
->buf().contentSize();
240 writeSomeBody("prime virgin body", size
);
242 if (virginBodyEndReached(virginBodyWriting
)) {
243 debugs(93, 5, HERE
<< "wrote entire body");
248 void Adaptation::Icap::ModXact::writeSomeBody(const char *label
, size_t size
)
250 Must(!writer
&& state
.writing
< state
.writingAlmostDone
);
251 Must(virgin
.body_pipe
!= NULL
);
252 debugs(93, 8, HERE
<< "will write up to " << size
<< " bytes of " <<
255 MemBuf writeBuf
; // TODO: suggest a min size based on size and lastChunk
257 writeBuf
.init(); // note: we assume that last-chunk will fit
259 const size_t writableSize
= virginContentSize(virginBodyWriting
);
260 const size_t chunkSize
= min(writableSize
, size
);
263 debugs(93, 7, HERE
<< "will write " << chunkSize
<<
264 "-byte chunk of " << label
);
266 openChunk(writeBuf
, chunkSize
, false);
267 writeBuf
.append(virginContentData(virginBodyWriting
), chunkSize
);
268 closeChunk(writeBuf
);
270 virginBodyWriting
.progress(chunkSize
);
273 debugs(93, 7, HERE
<< "has no writable " << label
<< " content");
276 const bool wroteEof
= virginBodyEndReached(virginBodyWriting
);
277 bool lastChunk
= wroteEof
;
278 if (state
.writing
== State::writingPreview
) {
279 preview
.wrote(chunkSize
, wroteEof
); // even if wrote nothing
280 lastChunk
= lastChunk
|| preview
.done();
284 debugs(93, 8, HERE
<< "will write last-chunk of " << label
);
285 addLastRequestChunk(writeBuf
);
288 debugs(93, 7, HERE
<< "will write " << writeBuf
.contentSize()
289 << " raw bytes of " << label
);
291 if (writeBuf
.hasContent()) {
292 scheduleWrite(writeBuf
); // comm will free the chunk
298 void Adaptation::Icap::ModXact::addLastRequestChunk(MemBuf
&buf
)
300 const bool ieof
= state
.writing
== State::writingPreview
&& preview
.ieof();
301 openChunk(buf
, 0, ieof
);
305 void Adaptation::Icap::ModXact::openChunk(MemBuf
&buf
, size_t chunkSize
, bool ieof
)
307 buf
.Printf((ieof
? "%x; ieof\r\n" : "%x\r\n"), (int) chunkSize
);
310 void Adaptation::Icap::ModXact::closeChunk(MemBuf
&buf
)
312 buf
.append(ICAP::crlf
, 2); // chunk-terminating CRLF
315 const HttpRequest
&Adaptation::Icap::ModXact::virginRequest() const
317 const HttpRequest
*request
= virgin
.cause
?
318 virgin
.cause
: dynamic_cast<const HttpRequest
*>(virgin
.header
);
323 // did the activity reached the end of the virgin body?
324 bool Adaptation::Icap::ModXact::virginBodyEndReached(const Adaptation::Icap::VirginBodyAct
&act
) const
327 !act
.active() || // did all (assuming it was originally planned)
328 !virgin
.body_pipe
->expectMoreAfter(act
.offset()); // wont have more
331 // the size of buffered virgin body data available for the specified activity
332 // if this size is zero, we may be done or may be waiting for more data
333 size_t Adaptation::Icap::ModXact::virginContentSize(const Adaptation::Icap::VirginBodyAct
&act
) const
336 // asbolute start of unprocessed data
337 const uint64_t dataStart
= act
.offset();
338 // absolute end of buffered data
339 const uint64_t dataEnd
= virginConsumed
+ virgin
.body_pipe
->buf().contentSize();
340 Must(virginConsumed
<= dataStart
&& dataStart
<= dataEnd
);
341 return static_cast<size_t>(dataEnd
- dataStart
);
344 // pointer to buffered virgin body data available for the specified activity
345 const char *Adaptation::Icap::ModXact::virginContentData(const Adaptation::Icap::VirginBodyAct
&act
) const
348 const uint64_t dataStart
= act
.offset();
349 Must(virginConsumed
<= dataStart
);
350 return virgin
.body_pipe
->buf().content() + static_cast<size_t>(dataStart
-virginConsumed
);
353 void Adaptation::Icap::ModXact::virginConsume()
355 debugs(93, 9, HERE
<< "consumption guards: " << !virgin
.body_pipe
<< isRetriable
<<
356 isRepeatable
<< canStartBypass
<< protectGroupBypass
);
358 if (!virgin
.body_pipe
)
359 return; // nothing to consume
362 return; // do not consume if we may have to retry later
364 BodyPipe
&bp
= *virgin
.body_pipe
;
365 const bool wantToPostpone
= isRepeatable
|| canStartBypass
|| protectGroupBypass
;
367 // Why > 2? HttpState does not use the last bytes in the buffer
368 // because delayAwareRead() is arguably broken. See
369 // HttpStateData::maybeReadVirginBody for more details.
370 if (wantToPostpone
&& bp
.buf().spaceSize() > 2) {
371 // Postponing may increase memory footprint and slow the HTTP side
372 // down. Not postponing may increase the number of ICAP errors
373 // if the ICAP service fails. We may also use "potential" space to
374 // postpone more aggressively. Should the trade-off be configurable?
375 debugs(93, 8, HERE
<< "postponing consumption from " << bp
.status());
379 const size_t have
= static_cast<size_t>(bp
.buf().contentSize());
380 const uint64_t end
= virginConsumed
+ have
;
381 uint64_t offset
= end
;
383 debugs(93, 9, HERE
<< "max virgin consumption offset=" << offset
<<
384 " acts " << virginBodyWriting
.active() << virginBodySending
.active() <<
385 " consumed=" << virginConsumed
<<
386 " from " << virgin
.body_pipe
->status());
388 if (virginBodyWriting
.active())
389 offset
= min(virginBodyWriting
.offset(), offset
);
391 if (virginBodySending
.active())
392 offset
= min(virginBodySending
.offset(), offset
);
394 Must(virginConsumed
<= offset
&& offset
<= end
);
396 if (const size_t size
= static_cast<size_t>(offset
- virginConsumed
)) {
397 debugs(93, 8, HERE
<< "consuming " << size
<< " out of " << have
<<
398 " virgin body bytes");
400 virginConsumed
+= size
;
401 Must(!isRetriable
); // or we should not be consuming
402 disableRepeats("consumed content");
403 disableBypass("consumed content", true);
407 void Adaptation::Icap::ModXact::handleCommWroteBody()
412 // Called when we do not expect to call comm_write anymore.
413 // We may have a pending write though.
414 // If stopping nicely, we will just wait for that pending write, if any.
415 void Adaptation::Icap::ModXact::stopWriting(bool nicely
)
417 if (state
.writing
== State::writingReallyDone
)
420 if (writer
!= NULL
) {
422 debugs(93, 7, HERE
<< "will wait for the last write" << status());
423 state
.writing
= State::writingAlmostDone
; // may already be set
427 debugs(93, 3, HERE
<< "will NOT wait for the last write" << status());
429 // Comm does not have an interface to clear the writer callback nicely,
430 // but without clearing the writer we cannot recycle the connection.
431 // We prevent connection reuse and hope that we can handle a callback
432 // call at any time, usually in the middle of the destruction sequence!
433 // Somebody should add comm_remove_write_handler() to comm API.
434 reuseConnection
= false;
435 ignoreLastWrite
= true;
438 debugs(93, 7, HERE
<< "will no longer write" << status());
439 if (virginBodyWriting
.active()) {
440 virginBodyWriting
.disable();
443 state
.writing
= State::writingReallyDone
;
447 void Adaptation::Icap::ModXact::stopBackup()
449 if (!virginBodySending
.active())
452 debugs(93, 7, HERE
<< "will no longer backup" << status());
453 virginBodySending
.disable();
457 bool Adaptation::Icap::ModXact::doneAll() const
459 return Adaptation::Icap::Xaction::doneAll() && !state
.serviceWaiting
&&
461 doneReading() && state
.doneWriting();
464 void Adaptation::Icap::ModXact::startReading()
466 Must(connection
>= 0);
468 Must(!adapted
.header
);
469 Must(!adapted
.body_pipe
);
471 // we use the same buffer for headers and body and then consume headers
475 void Adaptation::Icap::ModXact::readMore()
477 if (reader
!= NULL
|| doneReading()) {
478 debugs(93,3,HERE
<< "returning from readMore because reader or doneReading()");
482 // do not fill readBuf if we have no space to store the result
483 if (adapted
.body_pipe
!= NULL
&&
484 !adapted
.body_pipe
->buf().hasPotentialSpace()) {
485 debugs(93,3,HERE
<< "not reading because ICAP reply pipe is full");
489 if (readBuf
.hasSpace())
492 debugs(93,3,HERE
<< "nothing to do because !readBuf.hasSpace()");
495 // comm module read a portion of the ICAP response for us
496 void Adaptation::Icap::ModXact::handleCommRead(size_t)
498 Must(!state
.doneParsing());
499 icap_tio_finish
= current_time
;
504 void Adaptation::Icap::ModXact::echoMore()
506 Must(state
.sending
== State::sendingVirgin
);
507 Must(adapted
.body_pipe
!= NULL
);
508 Must(virginBodySending
.active());
510 const size_t sizeMax
= virginContentSize(virginBodySending
);
511 debugs(93,5, HERE
<< "will echo up to " << sizeMax
<< " bytes from " <<
512 virgin
.body_pipe
->status());
513 debugs(93,5, HERE
<< "will echo up to " << sizeMax
<< " bytes to " <<
514 adapted
.body_pipe
->status());
517 const size_t size
= adapted
.body_pipe
->putMoreData(virginContentData(virginBodySending
), sizeMax
);
518 debugs(93,5, HERE
<< "echoed " << size
<< " out of " << sizeMax
<<
520 virginBodySending
.progress(size
);
521 disableRepeats("echoed content");
522 disableBypass("echoed content", true);
526 if (virginBodyEndReached(virginBodySending
)) {
527 debugs(93, 5, HERE
<< "echoed all" << status());
530 debugs(93, 5, HERE
<< "has " <<
531 virgin
.body_pipe
->buf().contentSize() << " bytes " <<
532 "and expects more to echo" << status());
533 // TODO: timeout if virgin or adapted pipes are broken
537 bool Adaptation::Icap::ModXact::doneSending() const
539 return state
.sending
== State::sendingDone
;
542 // stop (or do not start) sending adapted message body
543 void Adaptation::Icap::ModXact::stopSending(bool nicely
)
545 debugs(93, 7, HERE
<< "Enter stop sending ");
548 debugs(93, 7, HERE
<< "Proceed with stop sending ");
550 if (state
.sending
!= State::sendingUndecided
) {
551 debugs(93, 7, HERE
<< "will no longer send" << status());
552 if (adapted
.body_pipe
!= NULL
) {
553 virginBodySending
.disable();
554 // we may leave debts if we were echoing and the virgin
555 // body_pipe got exhausted before we echoed all planned bytes
556 const bool leftDebts
= adapted
.body_pipe
->needsMoreData();
557 stopProducingFor(adapted
.body_pipe
, nicely
&& !leftDebts
);
560 debugs(93, 7, HERE
<< "will not start sending" << status());
561 Must(!adapted
.body_pipe
);
564 state
.sending
= State::sendingDone
;
568 // should be called after certain state.writing or state.sending changes
569 void Adaptation::Icap::ModXact::checkConsuming()
571 // quit if we already stopped or are still using the pipe
572 if (!virgin
.body_pipe
|| !state
.doneConsumingVirgin())
575 debugs(93, 7, HERE
<< "will stop consuming" << status());
576 stopConsumingFrom(virgin
.body_pipe
);
579 void Adaptation::Icap::ModXact::parseMore()
581 debugs(93, 5, HERE
<< "have " << readBuf
.contentSize() << " bytes to parse" <<
583 debugs(93, 5, HERE
<< "\n" << readBuf
.content());
585 if (state
.parsingHeaders())
588 if (state
.parsing
== State::psBody
)
592 void Adaptation::Icap::ModXact::callException(const std::exception
&e
)
594 if (!canStartBypass
|| isRetriable
) {
595 Adaptation::Icap::Xaction::callException(e
);
600 debugs(93, 3, HERE
<< "bypassing " << inCall
<< " exception: " <<
601 e
.what() << ' ' << status());
603 } catch (const std::exception
&bypassE
) {
604 Adaptation::Icap::Xaction::callException(bypassE
);
608 void Adaptation::Icap::ModXact::bypassFailure()
610 disableBypass("already started to bypass", false);
612 Must(!isRetriable
); // or we should not be bypassing
613 // TODO: should the same be enforced for isRepeatable? Check icap_repeat??
619 // end all activities associated with the ICAP server
623 stopWriting(true); // or should we force it?
624 if (connection
>= 0) {
625 reuseConnection
= false; // be conservative
626 cancelRead(); // may not work; and we cannot stop connecting either
628 debugs(93, 7, HERE
<< "Warning: bypass failed to stop I/O" << status());
632 void Adaptation::Icap::ModXact::disableBypass(const char *reason
, bool includingGroupBypass
)
634 if (canStartBypass
) {
635 debugs(93,7, HERE
<< "will never start bypass because " << reason
);
636 canStartBypass
= false;
638 if (protectGroupBypass
&& includingGroupBypass
) {
639 debugs(93,7, HERE
<< "not protecting group bypass because " << reason
);
640 protectGroupBypass
= false;
646 // note that allocation for echoing is done in handle204NoContent()
647 void Adaptation::Icap::ModXact::maybeAllocateHttpMsg()
649 if (adapted
.header
) // already allocated
652 if (gotEncapsulated("res-hdr")) {
653 adapted
.setHeader(new HttpReply
);
654 setOutcome(service().cfg().method
== ICAP::methodReqmod
?
655 xoSatisfied
: xoModified
);
656 } else if (gotEncapsulated("req-hdr")) {
657 adapted
.setHeader(new HttpRequest
);
658 setOutcome(xoModified
);
660 throw TexcHere("Neither res-hdr nor req-hdr in maybeAllocateHttpMsg()");
663 void Adaptation::Icap::ModXact::parseHeaders()
665 Must(state
.parsingHeaders());
667 if (state
.parsing
== State::psIcapHeader
) {
668 debugs(93, 5, HERE
<< "parse ICAP headers");
672 if (state
.parsing
== State::psHttpHeader
) {
673 debugs(93, 5, HERE
<< "parse HTTP headers");
677 if (state
.parsingHeaders()) { // need more data
685 // called after parsing all headers or when bypassing an exception
686 void Adaptation::Icap::ModXact::startSending()
688 disableRepeats("sent headers");
689 disableBypass("sent headers", true);
690 sendAnswer(adapted
.header
);
692 if (state
.sending
== State::sendingVirgin
)
696 void Adaptation::Icap::ModXact::parseIcapHead()
698 Must(state
.sending
== State::sendingUndecided
);
700 if (!parseHead(icapReply
))
703 if (httpHeaderHasConnDir(&icapReply
->header
, "close")) {
704 debugs(93, 5, HERE
<< "found connection close");
705 reuseConnection
= false;
708 switch (icapReply
->sline
.status
) {
715 case 201: // Symantec Scan Engine 5.0 and later when modifying HTTP msg
717 if (!validate200Ok()) {
718 throw TexcHere("Invalid ICAP Response");
726 handle204NoContent();
730 debugs(93, 5, HERE
<< "ICAP status " << icapReply
->sline
.status
);
731 handleUnknownScode();
735 const HttpRequest
*request
= dynamic_cast<HttpRequest
*>(adapted
.header
);
737 request
= &virginRequest();
739 // update the cross-transactional database if needed (all status codes!)
740 if (const char *xxName
= Adaptation::Config::masterx_shared_name
) {
741 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
743 const String val
= icapReply
->header
.getByName(xxName
);
744 if (val
.size() > 0) // XXX: HttpHeader lacks empty value detection
745 ah
->updateXxRecord(xxName
, val
);
749 // update the adaptation plan if needed (all status codes!)
750 if (service().cfg().routing
) {
752 if (icapReply
->header
.getList(HDR_X_NEXT_SERVICES
, &services
)) {
753 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
755 ah
->updateNextServices(services
);
757 } // TODO: else warn (occasionally!) if we got HDR_X_NEXT_SERVICES
759 // We need to store received ICAP headers for <icapLastHeader logformat option.
760 // If we already have stored headers from previous ICAP transaction related to this
761 // request, old headers will be replaced with the new one.
763 Adaptation::Icap::History::Pointer h
= request
->icapHistory();
765 h
->mergeIcapHeaders(&icapReply
->header
);
766 h
->setIcapLastHeader(&icapReply
->header
);
769 // handle100Continue() manages state.writing on its own.
770 // Non-100 status means the server needs no postPreview data from us.
771 if (state
.writing
== State::writingPaused
)
775 bool Adaptation::Icap::ModXact::validate200Ok()
777 if (ICAP::methodRespmod
== service().cfg().method
) {
778 if (!gotEncapsulated("res-hdr"))
784 if (ICAP::methodReqmod
== service().cfg().method
) {
785 if (!gotEncapsulated("res-hdr") && !gotEncapsulated("req-hdr"))
794 void Adaptation::Icap::ModXact::handle100Continue()
796 Must(state
.writing
== State::writingPaused
);
797 // server must not respond before the end of preview: we may send ieof
798 Must(preview
.enabled() && preview
.done() && !preview
.ieof());
800 // 100 "Continue" cancels our preview commitment, not 204s outside preview
801 if (!state
.allowedPostview204
)
804 state
.parsing
= State::psIcapHeader
; // eventually
807 state
.writing
= State::writingPrime
;
812 void Adaptation::Icap::ModXact::handle200Ok()
814 state
.parsing
= State::psHttpHeader
;
815 state
.sending
= State::sendingAdapted
;
820 void Adaptation::Icap::ModXact::handle204NoContent()
826 // Called when we receive a 204 No Content response and
827 // when we are trying to bypass a service failure.
828 // We actually start sending (echoig or not) in startSending.
829 void Adaptation::Icap::ModXact::prepEchoing()
831 disableRepeats("preparing to echo content");
832 disableBypass("preparing to echo content", true);
835 // We want to clone the HTTP message, but we do not want
836 // to copy some non-HTTP state parts that HttpMsg kids carry in them.
837 // Thus, we cannot use a smart pointer, copy constructor, or equivalent.
838 // Instead, we simply write the HTTP message and "clone" it by parsing.
839 // TODO: use HttpMsg::clone()!
841 HttpMsg
*oldHead
= virgin
.header
;
842 debugs(93, 7, HERE
<< "cloning virgin message " << oldHead
);
846 // write the virgin message into a memory buffer
848 packHead(httpBuf
, oldHead
);
850 // allocate the adapted message and copy metainfo
851 Must(!adapted
.header
);
852 HttpMsg
*newHead
= NULL
;
853 if (const HttpRequest
*oldR
= dynamic_cast<const HttpRequest
*>(oldHead
)) {
854 HttpRequest
*newR
= new HttpRequest
;
855 newR
->canonical
= oldR
->canonical
?
856 xstrdup(oldR
->canonical
) : NULL
; // parse() does not set it
858 } else if (dynamic_cast<const HttpReply
*>(oldHead
)) {
859 HttpReply
*newRep
= new HttpReply
;
863 newHead
->inheritProperties(oldHead
);
865 adapted
.setHeader(newHead
);
867 // parse the buffer back
868 http_status error
= HTTP_STATUS_NONE
;
870 Must(newHead
->parse(&httpBuf
, true, &error
));
872 Must(newHead
->hdr_sz
== httpBuf
.contentSize()); // no leftovers
876 debugs(93, 7, HERE
<< "cloned virgin message " << oldHead
<< " to " <<
879 // setup adapted body pipe if needed
880 if (oldHead
->body_pipe
!= NULL
) {
881 debugs(93, 7, HERE
<< "will echo virgin body from " <<
883 if (!virginBodySending
.active())
884 virginBodySending
.plan(); // will throw if not possible
885 state
.sending
= State::sendingVirgin
;
888 // TODO: optimize: is it possible to just use the oldHead pipe and
889 // remove ICAP from the loop? This echoing is probably a common case!
890 makeAdaptedBodyPipe("echoed virgin response");
891 if (oldHead
->body_pipe
->bodySizeKnown())
892 adapted
.body_pipe
->setBodySize(oldHead
->body_pipe
->bodySize());
893 debugs(93, 7, HERE
<< "will echo virgin body to " <<
896 debugs(93, 7, HERE
<< "no virgin body to echo");
901 void Adaptation::Icap::ModXact::handleUnknownScode()
905 // TODO: mark connection as "bad"
907 // Terminate the transaction; we do not know how to handle this response.
908 throw TexcHere("Unsupported ICAP status code");
911 void Adaptation::Icap::ModXact::parseHttpHead()
913 if (gotEncapsulated("res-hdr") || gotEncapsulated("req-hdr")) {
914 maybeAllocateHttpMsg();
916 if (!parseHead(adapted
.header
))
917 return; // need more header data
919 if (dynamic_cast<HttpRequest
*>(adapted
.header
)) {
920 const HttpRequest
*oldR
= dynamic_cast<const HttpRequest
*>(virgin
.header
);
922 // TODO: the adapted request did not really originate from the
923 // client; give proxy admin an option to prevent copying of
924 // sensitive client information here. See the following thread:
925 // http://www.squid-cache.org/mail-archive/squid-dev/200703/0040.html
928 // Maybe adapted.header==NULL if HttpReply and have Http 0.9 ....
930 adapted
.header
->inheritProperties(virgin
.header
);
933 decideOnParsingBody();
936 // parses both HTTP and ICAP headers
937 bool Adaptation::Icap::ModXact::parseHead(HttpMsg
*head
)
940 debugs(93, 5, HERE
<< "have " << readBuf
.contentSize() << " head bytes to parse" <<
941 "; state: " << state
.parsing
);
943 http_status error
= HTTP_STATUS_NONE
;
944 const bool parsed
= head
->parse(&readBuf
, commEof
, &error
);
945 Must(parsed
|| !error
); // success or need more data
947 if (!parsed
) { // need more data
948 debugs(93, 5, HERE
<< "parse failed, need more data, return false");
953 if (HttpRequest
*r
= dynamic_cast<HttpRequest
*>(head
))
954 urlCanonical(r
); // parse does not set HttpRequest::canonical
956 debugs(93, 5, HERE
<< "parse success, consume " << head
->hdr_sz
<< " bytes, return true");
957 readBuf
.consume(head
->hdr_sz
);
961 void Adaptation::Icap::ModXact::decideOnParsingBody()
963 if (gotEncapsulated("res-body") || gotEncapsulated("req-body")) {
964 debugs(93, 5, HERE
<< "expecting a body");
965 state
.parsing
= State::psBody
;
966 bodyParser
= new ChunkedCodingParser
;
967 makeAdaptedBodyPipe("adapted response from the ICAP server");
968 Must(state
.sending
== State::sendingAdapted
);
970 debugs(93, 5, HERE
<< "not expecting a body");
976 void Adaptation::Icap::ModXact::parseBody()
978 Must(state
.parsing
== State::psBody
);
981 debugs(93, 5, HERE
<< "have " << readBuf
.contentSize() << " body bytes to parse");
983 // the parser will throw on errors
984 BodyPipeCheckout
bpc(*adapted
.body_pipe
);
985 const bool parsed
= bodyParser
->parse(&readBuf
, &bpc
.buf
);
988 debugs(93, 5, HERE
<< "have " << readBuf
.contentSize() << " body bytes after " <<
989 "parse; parsed all: " << parsed
);
990 replyBodySize
+= adapted
.body_pipe
->buf().contentSize();
992 // TODO: expose BodyPipe::putSize() to make this check simpler and clearer
993 // TODO: do we really need this if we disable when sending headers?
994 if (adapted
.body_pipe
->buf().contentSize() > 0) { // parsed something sometime
995 disableRepeats("sent adapted content");
996 disableBypass("sent adapted content", true);
1001 stopSending(true); // the parser succeeds only if all parsed data fits
1005 debugs(93,3,HERE
<< this << " needsMoreData = " << bodyParser
->needsMoreData());
1007 if (bodyParser
->needsMoreData()) {
1008 debugs(93,3,HERE
<< this);
1009 Must(mayReadMore());
1013 if (bodyParser
->needsMoreSpace()) {
1014 Must(!doneSending()); // can hope for more space
1015 Must(adapted
.body_pipe
->buf().contentSize() > 0); // paranoid
1016 // TODO: there should be a timeout in case the sink is broken
1017 // or cannot consume partial content (while we need more space)
1021 void Adaptation::Icap::ModXact::stopParsing()
1023 if (state
.parsing
== State::psDone
)
1026 debugs(93, 7, HERE
<< "will no longer parse" << status());
1032 state
.parsing
= State::psDone
;
1035 // HTTP side added virgin body data
1036 void Adaptation::Icap::ModXact::noteMoreBodyDataAvailable(BodyPipe::Pointer
)
1040 if (state
.sending
== State::sendingVirgin
)
1044 // HTTP side sent us all virgin info
1045 void Adaptation::Icap::ModXact::noteBodyProductionEnded(BodyPipe::Pointer
)
1047 Must(virgin
.body_pipe
->productionEnded());
1049 // push writer and sender in case we were waiting for the last-chunk
1052 if (state
.sending
== State::sendingVirgin
)
1056 // body producer aborted, but the initiator may still want to know
1057 // the answer, even though the HTTP message has been truncated
1058 void Adaptation::Icap::ModXact::noteBodyProducerAborted(BodyPipe::Pointer
)
1060 Must(virgin
.body_pipe
->productionEnded());
1062 // push writer and sender in case we were waiting for the last-chunk
1065 if (state
.sending
== State::sendingVirgin
)
1069 // adapted body consumer wants more adapted data and
1070 // possibly freed some buffer space
1071 void Adaptation::Icap::ModXact::noteMoreBodySpaceAvailable(BodyPipe::Pointer
)
1073 if (state
.sending
== State::sendingVirgin
)
1075 else if (state
.sending
== State::sendingAdapted
)
1078 Must(state
.sending
== State::sendingUndecided
);
1081 // adapted body consumer aborted
1082 void Adaptation::Icap::ModXact::noteBodyConsumerAborted(BodyPipe::Pointer
)
1084 mustStop("adapted body consumer aborted");
1088 void Adaptation::Icap::ModXact::swanSong()
1090 debugs(93, 5, HERE
<< "swan sings" << status());
1095 // update adaptation history if start was called and we reserved a slot
1096 Adaptation::History::Pointer ah
= virginRequest().adaptLogHistory();
1097 if (ah
!= NULL
&& adaptHistoryId
>= 0)
1098 ah
->recordXactFinish(adaptHistoryId
);
1100 Adaptation::Icap::Xaction::swanSong();
1103 void prepareLogWithRequestDetails(HttpRequest
*, AccessLogEntry
*);
1105 void Adaptation::Icap::ModXact::finalizeLogInfo()
1107 HttpRequest
* request_
= NULL
;
1108 HttpReply
* reply_
= NULL
;
1109 if (!(request_
= dynamic_cast<HttpRequest
*>(adapted
.header
))) {
1110 request_
= (virgin
.cause
? virgin
.cause
: dynamic_cast<HttpRequest
*>(virgin
.header
));
1111 reply_
= dynamic_cast<HttpReply
*>(adapted
.header
);
1114 Adaptation::Icap::History::Pointer h
= request_
->icapHistory();
1115 Must(h
!= NULL
); // ICAPXaction::maybeLog calls only if there is a log
1116 al
.icp
.opcode
= ICP_INVALID
;
1117 al
.url
= h
->log_uri
.termedBuf();
1118 const Adaptation::Icap::ServiceRep
&s
= service();
1119 al
.icap
.reqMethod
= s
.cfg().method
;
1121 al
.cache
.caddr
= request_
->client_addr
;
1123 al
.request
= HTTPMSGLOCK(request_
);
1125 al
.reply
= HTTPMSGLOCK(reply_
);
1129 if (h
->rfc931
.size())
1130 al
.cache
.rfc931
= h
->rfc931
.termedBuf();
1133 if (h
->ssluser
.size())
1134 al
.cache
.ssluser
= h
->ssluser
.termedBuf();
1136 al
.cache
.code
= h
->logType
;
1137 al
.cache
.requestSize
= h
->req_sz
;
1139 al
.http
.code
= reply_
->sline
.status
;
1140 al
.http
.content_type
= reply_
->content_type
.termedBuf();
1141 al
.cache
.replySize
= replyBodySize
+ reply_
->hdr_sz
;
1142 al
.cache
.highOffset
= replyBodySize
;
1143 //don't set al.cache.objectSize because it hasn't exist yet
1149 packerToMemInit(&p
, &mb
);
1151 reply_
->header
.packInto(&p
);
1152 al
.headers
.reply
= xstrdup(mb
.buf
);
1157 prepareLogWithRequestDetails(request_
, &al
);
1158 Xaction::finalizeLogInfo();
1162 void Adaptation::Icap::ModXact::makeRequestHeaders(MemBuf
&buf
)
1164 char ntoabuf
[MAX_IPSTRLEN
];
1166 * XXX These should use HttpHdr interfaces instead of Printfs
1168 const Adaptation::ServiceConfig
&s
= service().cfg();
1169 buf
.Printf("%s " SQUIDSTRINGPH
" ICAP/1.0\r\n", s
.methodStr(), SQUIDSTRINGPRINT(s
.uri
));
1170 buf
.Printf("Host: " SQUIDSTRINGPH
":%d\r\n", SQUIDSTRINGPRINT(s
.host
), s
.port
);
1171 buf
.Printf("Date: %s\r\n", mkrfc1123(squid_curtime
));
1173 if (!TheConfig
.reuse_connections
)
1174 buf
.Printf("Connection: close\r\n");
1176 // we must forward "Proxy-Authenticate" and "Proxy-Authorization"
1178 if (virgin
.header
->header
.has(HDR_PROXY_AUTHENTICATE
)) {
1179 String vh
=virgin
.header
->header
.getByName("Proxy-Authenticate");
1180 buf
.Printf("Proxy-Authenticate: " SQUIDSTRINGPH
"\r\n",SQUIDSTRINGPRINT(vh
));
1183 if (virgin
.header
->header
.has(HDR_PROXY_AUTHORIZATION
)) {
1184 String vh
=virgin
.header
->header
.getByName("Proxy-Authorization");
1185 buf
.Printf("Proxy-Authorization: " SQUIDSTRINGPH
"\r\n", SQUIDSTRINGPRINT(vh
));
1188 const HttpRequest
*request
= &virginRequest();
1190 // share the cross-transactional database records if needed
1191 if (Adaptation::Config::masterx_shared_name
) {
1192 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
1195 if (ah
->getXxRecord(name
, value
)) {
1196 buf
.Printf(SQUIDSTRINGPH
": " SQUIDSTRINGPH
"\r\n",
1197 SQUIDSTRINGPRINT(name
), SQUIDSTRINGPRINT(value
));
1203 buf
.Printf("Encapsulated: ");
1209 // build HTTP request header, if any
1210 ICAP::Method m
= s
.method
;
1212 // to simplify, we could assume that request is always available
1216 urlPath
= request
->urlpath
;
1217 if (ICAP::methodRespmod
== m
)
1218 encapsulateHead(buf
, "req-hdr", httpBuf
, request
);
1219 else if (ICAP::methodReqmod
== m
)
1220 encapsulateHead(buf
, "req-hdr", httpBuf
, virgin
.header
);
1223 if (ICAP::methodRespmod
== m
)
1224 if (const HttpMsg
*prime
= virgin
.header
)
1225 encapsulateHead(buf
, "res-hdr", httpBuf
, prime
);
1227 if (!virginBody
.expected())
1228 buf
.Printf("null-body=%d", (int) httpBuf
.contentSize());
1229 else if (ICAP::methodReqmod
== m
)
1230 buf
.Printf("req-body=%d", (int) httpBuf
.contentSize());
1232 buf
.Printf("res-body=%d", (int) httpBuf
.contentSize());
1234 buf
.append(ICAP::crlf
, 2); // terminate Encapsulated line
1236 if (preview
.enabled()) {
1237 buf
.Printf("Preview: %d\r\n", (int)preview
.ad());
1238 if (virginBody
.expected()) // there is a body to preview
1239 virginBodySending
.plan();
1241 finishNullOrEmptyBodyPreview(httpBuf
);
1244 if (shouldAllow204()) {
1245 debugs(93,5, HERE
<< "will allow 204s outside of preview");
1246 state
.allowedPostview204
= true;
1247 buf
.Printf("Allow: 204\r\n");
1248 if (virginBody
.expected()) // there is a body to echo
1249 virginBodySending
.plan();
1252 if (TheConfig
.send_client_ip
&& request
) {
1253 Ip::Address client_addr
;
1254 #if FOLLOW_X_FORWARDED_FOR
1255 if (TheConfig
.icap_uses_indirect_client
) {
1256 client_addr
= request
->indirect_client_addr
;
1259 client_addr
= request
->client_addr
;
1260 if (!client_addr
.IsAnyAddr() && !client_addr
.IsNoAddr())
1261 buf
.Printf("X-Client-IP: %s\r\n", client_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
1264 if (TheConfig
.send_client_username
&& request
)
1265 makeUsernameHeader(request
, buf
);
1267 // fprintf(stderr, "%s\n", buf.content());
1269 buf
.append(ICAP::crlf
, 2); // terminate ICAP header
1271 // fill icapRequest for logging
1272 Must(icapRequest
->parseCharBuf(buf
.content(), buf
.contentSize()));
1274 // start ICAP request body with encapsulated HTTP headers
1275 buf
.append(httpBuf
.content(), httpBuf
.contentSize());
1280 void Adaptation::Icap::ModXact::makeUsernameHeader(const HttpRequest
*request
, MemBuf
&buf
)
1282 if (request
->auth_user_request
!= NULL
) {
1283 char const *name
= request
->auth_user_request
->username();
1285 const char *value
= TheConfig
.client_username_encode
? base64_encode(name
) : name
;
1286 buf
.Printf("%s: %s\r\n", TheConfig
.client_username_header
, value
);
1291 void Adaptation::Icap::ModXact::encapsulateHead(MemBuf
&icapBuf
, const char *section
, MemBuf
&httpBuf
, const HttpMsg
*head
)
1293 // update ICAP header
1294 icapBuf
.Printf("%s=%d, ", section
, (int) httpBuf
.contentSize());
1297 HttpMsg
*headClone
= NULL
;
1299 if (const HttpRequest
* old_request
= dynamic_cast<const HttpRequest
*>(head
)) {
1300 HttpRequest
* new_request
= new HttpRequest
;
1301 assert(old_request
->canonical
);
1302 urlParse(old_request
->method
, old_request
->canonical
, new_request
);
1303 new_request
->http_ver
= old_request
->http_ver
;
1304 headClone
= new_request
;
1305 } else if (const HttpReply
*old_reply
= dynamic_cast<const HttpReply
*>(head
)) {
1306 HttpReply
* new_reply
= new HttpReply
;
1307 new_reply
->sline
= old_reply
->sline
;
1308 headClone
= new_reply
;
1312 headClone
->inheritProperties(head
);
1314 HttpHeaderPos pos
= HttpHeaderInitPos
;
1315 HttpHeaderEntry
* p_head_entry
= NULL
;
1316 while (NULL
!= (p_head_entry
= head
->header
.getEntry(&pos
)) )
1317 headClone
->header
.addEntry(p_head_entry
->clone());
1321 // remove all hop-by-hop headers from the clone
1322 headClone
->header
.delById(HDR_PROXY_AUTHENTICATE
);
1323 headClone
->header
.removeHopByHopEntries();
1325 // pack polished HTTP header
1326 packHead(httpBuf
, headClone
);
1331 void Adaptation::Icap::ModXact::packHead(MemBuf
&httpBuf
, const HttpMsg
*head
)
1334 packerToMemInit(&p
, &httpBuf
);
1335 head
->packInto(&p
, true);
1339 // decides whether to offer a preview and calculates its size
1340 void Adaptation::Icap::ModXact::decideOnPreview()
1342 if (!TheConfig
.preview_enable
) {
1343 debugs(93, 5, HERE
<< "preview disabled by squid.conf");
1347 const String urlPath
= virginRequest().urlpath
;
1349 if (!service().wantsPreview(urlPath
, wantedSize
)) {
1350 debugs(93, 5, HERE
<< "should not offer preview for " << urlPath
);
1354 // we decided to do preview, now compute its size
1356 Must(wantedSize
>= 0);
1358 // cannot preview more than we can backup
1359 size_t ad
= min(wantedSize
, TheBackupLimit
);
1361 if (!virginBody
.expected())
1363 else if (virginBody
.knownSize())
1364 ad
= min(static_cast<uint64_t>(ad
), virginBody
.size()); // not more than we have
1366 debugs(93, 5, HERE
<< "should offer " << ad
<< "-byte preview " <<
1367 "(service wanted " << wantedSize
<< ")");
1370 Must(preview
.enabled());
1373 // decides whether to allow 204 responses
1374 bool Adaptation::Icap::ModXact::shouldAllow204()
1376 if (!service().allows204())
1379 return canBackupEverything();
1382 // used by shouldAllow204 and decideOnRetries
1383 bool Adaptation::Icap::ModXact::canBackupEverything() const
1385 if (!virginBody
.expected())
1386 return true; // no body means no problems with backup
1388 // if there is a body, check whether we can backup it all
1390 if (!virginBody
.knownSize())
1393 // or should we have a different backup limit?
1394 // note that '<' allows for 0-termination of the "full" backup buffer
1395 return virginBody
.size() < TheBackupLimit
;
1398 // Decide whether this transaction can be retried if pconn fails
1399 // Must be called after decideOnPreview and before openConnection()
1400 void Adaptation::Icap::ModXact::decideOnRetries()
1403 return; // no, already decided
1405 if (preview
.enabled())
1406 return; // yes, because preview provides enough guarantees
1408 if (canBackupEverything())
1409 return; // yes, because we can back everything up
1411 disableRetries(); // no, because we cannot back everything up
1414 // Normally, the body-writing code handles preview body. It can deal with
1415 // bodies of unexpected size, including those that turn out to be empty.
1416 // However, that code assumes that the body was expected and body control
1417 // structures were initialized. This is not the case when there is no body
1418 // or the body is known to be empty, because the virgin message will lack a
1419 // body_pipe. So we handle preview of null-body and zero-size bodies here.
1420 void Adaptation::Icap::ModXact::finishNullOrEmptyBodyPreview(MemBuf
&buf
)
1422 Must(!virginBodyWriting
.active()); // one reason we handle it here
1423 Must(!virgin
.body_pipe
); // another reason we handle it here
1424 Must(!preview
.ad());
1426 // do not add last-chunk because our Encapsulated header says null-body
1427 // addLastRequestChunk(buf);
1428 preview
.wrote(0, true);
1430 Must(preview
.done());
1431 Must(preview
.ieof());
1434 void Adaptation::Icap::ModXact::fillPendingStatus(MemBuf
&buf
) const
1436 Adaptation::Icap::Xaction::fillPendingStatus(buf
);
1438 if (state
.serviceWaiting
)
1441 if (virgin
.body_pipe
!= NULL
)
1444 if (connection
> 0 && !doneReading())
1447 if (!state
.doneWriting() && state
.writing
!= State::writingInit
)
1448 buf
.Printf("w(%d)", state
.writing
);
1450 if (preview
.enabled()) {
1451 if (!preview
.done())
1452 buf
.Printf("P(%d)", (int) preview
.debt());
1455 if (virginBodySending
.active())
1458 if (!state
.doneParsing() && state
.parsing
!= State::psIcapHeader
)
1459 buf
.Printf("p(%d)", state
.parsing
);
1461 if (!doneSending() && state
.sending
!= State::sendingUndecided
)
1462 buf
.Printf("S(%d)", state
.sending
);
1467 if (protectGroupBypass
)
1471 void Adaptation::Icap::ModXact::fillDoneStatus(MemBuf
&buf
) const
1473 Adaptation::Icap::Xaction::fillDoneStatus(buf
);
1475 if (!virgin
.body_pipe
)
1478 if (state
.doneWriting())
1481 if (preview
.enabled()) {
1483 buf
.Printf("P%s", preview
.ieof() ? "(ieof)" : "");
1489 if (state
.doneParsing())
1496 bool Adaptation::Icap::ModXact::gotEncapsulated(const char *section
) const
1498 return icapReply
->header
.getByNameListMember("Encapsulated",
1499 section
, ',').size() > 0;
1502 // calculate whether there is a virgin HTTP body and
1503 // whether its expected size is known
1504 // TODO: rename because we do not just estimate
1505 void Adaptation::Icap::ModXact::estimateVirginBody()
1507 // note: lack of size info may disable previews and 204s
1509 HttpMsg
*msg
= virgin
.header
;
1512 HttpRequestMethod method
;
1515 method
= virgin
.cause
->method
;
1516 else if (HttpRequest
*req
= dynamic_cast<HttpRequest
*>(msg
))
1517 method
= req
->method
;
1519 method
= METHOD_NONE
;
1522 // expectingBody returns true for zero-sized bodies, but we will not
1523 // get a pipe for that body, so we treat the message as bodyless
1524 if (method
!= METHOD_NONE
&& msg
->expectingBody(method
, size
) && size
) {
1525 debugs(93, 6, HERE
<< "expects virgin body from " <<
1526 virgin
.body_pipe
<< "; size: " << size
);
1528 virginBody
.expect(size
);
1529 virginBodyWriting
.plan();
1531 // sign up as a body consumer
1532 Must(msg
->body_pipe
!= NULL
);
1533 Must(msg
->body_pipe
== virgin
.body_pipe
);
1534 Must(virgin
.body_pipe
->setConsumerIfNotLate(this));
1536 // make sure TheBackupLimit is in-sync with the buffer size
1537 Must(TheBackupLimit
<= static_cast<size_t>(msg
->body_pipe
->buf().max_capacity
));
1539 debugs(93, 6, HERE
<< "does not expect virgin body");
1540 Must(msg
->body_pipe
== NULL
);
1545 void Adaptation::Icap::ModXact::makeAdaptedBodyPipe(const char *what
)
1547 Must(!adapted
.body_pipe
);
1548 Must(!adapted
.header
->body_pipe
);
1549 adapted
.header
->body_pipe
= new BodyPipe(this);
1550 adapted
.body_pipe
= adapted
.header
->body_pipe
;
1551 debugs(93, 7, HERE
<< "will supply " << what
<< " via " <<
1552 adapted
.body_pipe
<< " pipe");
1556 // TODO: Move SizedEstimate and Preview elsewhere
1558 Adaptation::Icap::SizedEstimate::SizedEstimate()
1559 : theData(dtUnexpected
)
1562 void Adaptation::Icap::SizedEstimate::expect(int64_t aSize
)
1564 theData
= (aSize
>= 0) ? aSize
: (int64_t)dtUnknown
;
1567 bool Adaptation::Icap::SizedEstimate::expected() const
1569 return theData
!= dtUnexpected
;
1572 bool Adaptation::Icap::SizedEstimate::knownSize() const
1575 return theData
!= dtUnknown
;
1578 uint64_t Adaptation::Icap::SizedEstimate::size() const
1581 return static_cast<uint64_t>(theData
);
1586 Adaptation::Icap::VirginBodyAct::VirginBodyAct(): theStart(0), theState(stUndecided
)
1589 void Adaptation::Icap::VirginBodyAct::plan()
1592 Must(!theStart
); // not started
1593 theState
= stActive
;
1596 void Adaptation::Icap::VirginBodyAct::disable()
1598 theState
= stDisabled
;
1601 void Adaptation::Icap::VirginBodyAct::progress(size_t size
)
1605 theStart
+= static_cast<int64_t>(size
);
1608 uint64_t Adaptation::Icap::VirginBodyAct::offset() const
1611 return static_cast<uint64_t>(theStart
);
1615 Adaptation::Icap::Preview::Preview(): theWritten(0), theAd(0), theState(stDisabled
)
1618 void Adaptation::Icap::Preview::enable(size_t anAd
)
1620 // TODO: check for anAd not exceeding preview size limit
1624 theState
= stWriting
;
1627 bool Adaptation::Icap::Preview::enabled() const
1629 return theState
!= stDisabled
;
1632 size_t Adaptation::Icap::Preview::ad() const
1638 bool Adaptation::Icap::Preview::done() const
1641 return theState
>= stIeof
;
1644 bool Adaptation::Icap::Preview::ieof() const
1647 return theState
== stIeof
;
1650 size_t Adaptation::Icap::Preview::debt() const
1653 return done() ? 0 : (theAd
- theWritten
);
1656 void Adaptation::Icap::Preview::wrote(size_t size
, bool wroteEof
)
1662 Must(theWritten
<= theAd
);
1665 theState
= stIeof
; // written size is irrelevant
1666 else if (theWritten
>= theAd
)
1670 bool Adaptation::Icap::ModXact::fillVirginHttpHeader(MemBuf
&mb
) const
1672 if (virgin
.header
== NULL
)
1675 virgin
.header
->firstLineBuf(mb
);
1681 /* Adaptation::Icap::ModXactLauncher */
1683 Adaptation::Icap::ModXactLauncher::ModXactLauncher(Adaptation::Initiator
*anInitiator
, HttpMsg
*virginHeader
, HttpRequest
*virginCause
, Adaptation::ServicePointer aService
):
1684 AsyncJob("Adaptation::Icap::ModXactLauncher"),
1685 Adaptation::Icap::Launcher("Adaptation::Icap::ModXactLauncher", anInitiator
, aService
)
1687 virgin
.setHeader(virginHeader
);
1688 virgin
.setCause(virginCause
);
1689 updateHistory(true);
1692 Adaptation::Icap::Xaction
*Adaptation::Icap::ModXactLauncher::createXaction()
1694 Adaptation::Icap::ServiceRep::Pointer s
=
1695 dynamic_cast<Adaptation::Icap::ServiceRep
*>(theService
.getRaw());
1697 return new Adaptation::Icap::ModXact(this, virgin
.header
, virgin
.cause
, s
);
1700 void Adaptation::Icap::ModXactLauncher::swanSong()
1702 debugs(93, 5, HERE
<< "swan sings");
1703 updateHistory(false);
1704 Adaptation::Icap::Launcher::swanSong();
1707 void Adaptation::Icap::ModXactLauncher::updateHistory(bool doStart
)
1709 HttpRequest
*r
= virgin
.cause
?
1710 virgin
.cause
: dynamic_cast<HttpRequest
*>(virgin
.header
);
1712 // r should never be NULL but we play safe; TODO: add Should()
1714 Adaptation::Icap::History::Pointer h
= r
->icapHistory();
1717 h
->start("ICAPModXactLauncher");
1719 h
->stop("ICAPModXactLauncher");