2 * DEBUG: section 93 ICAP (RFC 3507) Client
6 #include "adaptation/icap/Config.h"
7 #include "adaptation/icap/Launcher.h"
8 #include "adaptation/icap/Xaction.h"
9 #include "base/TextException.h"
11 #include "comm/Connection.h"
12 #include "comm/ConnOpener.h"
13 #include "comm/Write.h"
14 #include "CommCalls.h"
15 #include "err_detail_type.h"
19 #include "HttpReply.h"
20 #include "HttpRequest.h"
25 #include "SquidConfig.h"
26 #include "SquidTime.h"
28 //CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, Xaction);
30 Adaptation::Icap::Xaction::Xaction(const char *aTypeName
, Adaptation::Icap::ServiceRep::Pointer
&aService
):
32 Adaptation::Initiate(aTypeName
),
38 commBuf(NULL
), commBufSize(0),
40 reuseConnection(true),
43 ignoreLastWrite(false),
44 connector(NULL
), reader(NULL
), writer(NULL
), closer(NULL
),
45 alep(new AccessLogEntry
),
49 debugs(93,3, typeName
<< " constructed, this=" << this <<
50 " [icapx" << id
<< ']'); // we should not call virtual status() here
51 icapRequest
= new HttpRequest
;
52 HTTPMSGLOCK(icapRequest
);
53 icap_tr_start
= current_time
;
56 Adaptation::Icap::Xaction::~Xaction()
58 debugs(93,3, typeName
<< " destructed, this=" << this <<
59 " [icapx" << id
<< ']'); // we should not call virtual status() here
60 HTTPMSGUNLOCK(icapRequest
);
63 Adaptation::Icap::ServiceRep
&
64 Adaptation::Icap::Xaction::service()
66 Must(theService
!= NULL
);
70 void Adaptation::Icap::Xaction::disableRetries()
72 debugs(93,5, typeName
<< (isRetriable
? " from now on" : " still") <<
73 " cannot be retried " << status());
77 void Adaptation::Icap::Xaction::disableRepeats(const char *reason
)
79 debugs(93,5, typeName
<< (isRepeatable
? " from now on" : " still") <<
80 " cannot be repeated because " << reason
<< status());
84 void Adaptation::Icap::Xaction::start()
86 Adaptation::Initiate::start();
88 readBuf
.init(SQUID_TCP_SO_RCVBUF
, SQUID_TCP_SO_RCVBUF
);
89 commBuf
= (char*)memAllocBuf(SQUID_TCP_SO_RCVBUF
, &commBufSize
);
90 // make sure maximum readBuf space does not exceed commBuf size
91 Must(static_cast<size_t>(readBuf
.potentialSpaceSize()) <= commBufSize
);
95 icapLookupDnsResults(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
97 Adaptation::Icap::Xaction
*xa
= static_cast<Adaptation::Icap::Xaction
*>(data
);
98 xa
->dnsLookupDone(ia
);
101 // TODO: obey service-specific, OPTIONS-reported connection limit
103 Adaptation::Icap::Xaction::openConnection()
105 Must(!haveConnection());
107 Adaptation::Icap::ServiceRep
&s
= service();
109 if (!TheConfig
.reuse_connections
)
110 disableRetries(); // this will also safely drain pconn pool
112 bool wasReused
= false;
113 connection
= s
.getConnection(isRetriable
, wasReused
);
115 if (wasReused
&& Comm::IsConnOpen(connection
)) {
116 // Set comm Close handler
117 // fake the connect callback
118 // TODO: can we sync call Adaptation::Icap::Xaction::noteCommConnected here instead?
119 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommConnectCbParams
> Dialer
;
120 CbcPointer
<Xaction
> self(this);
121 Dialer
dialer(self
, &Adaptation::Icap::Xaction::noteCommConnected
);
122 dialer
.params
.conn
= connection
;
123 dialer
.params
.flag
= COMM_OK
;
124 // fake other parameters by copying from the existing connection
125 connector
= asyncCall(93,3, "Adaptation::Icap::Xaction::noteCommConnected", dialer
);
126 ScheduleCallHere(connector
);
130 disableRetries(); // we only retry pconn failures
132 // Attempt to open a new connection...
133 debugs(93,3, typeName
<< " opens connection to " << s
.cfg().host
.termedBuf() << ":" << s
.cfg().port
);
135 // Locate the Service IP(s) to open
136 ipcache_nbgethostbyname(s
.cfg().host
.termedBuf(), icapLookupDnsResults
, this);
140 Adaptation::Icap::Xaction::dnsLookupDone(const ipcache_addrs
*ia
)
142 Adaptation::Icap::ServiceRep
&s
= service();
145 debugs(44, DBG_IMPORTANT
, "ICAP: Unknown service host: " << s
.cfg().host
);
147 #if WHEN_IPCACHE_NBGETHOSTBYNAME_USES_ASYNC_CALLS
148 dieOnConnectionFailure(); // throws
149 #else // take a step back into protected Async call dialing.
150 // fake the connect callback
151 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommConnectCbParams
> Dialer
;
152 CbcPointer
<Xaction
> self(this);
153 Dialer
dialer(self
, &Adaptation::Icap::Xaction::noteCommConnected
);
154 dialer
.params
.conn
= connection
;
155 dialer
.params
.flag
= COMM_ERROR
;
156 // fake other parameters by copying from the existing connection
157 connector
= asyncCall(93,3, "Adaptation::Icap::Xaction::noteCommConnected", dialer
);
158 ScheduleCallHere(connector
);
163 assert(ia
->cur
< ia
->count
);
165 connection
= new Comm::Connection
;
166 connection
->remote
= ia
->in_addrs
[ia
->cur
];
167 connection
->remote
.port(s
.cfg().port
);
168 getOutgoingAddress(NULL
, connection
);
170 // TODO: service bypass status may differ from that of a transaction
171 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommConnectCbParams
> ConnectDialer
;
172 connector
= JobCallback(93,3, ConnectDialer
, this, Adaptation::Icap::Xaction::noteCommConnected
);
173 cs
= new Comm::ConnOpener(connection
, connector
, TheConfig
.connect_timeout(service().cfg().bypass
));
174 cs
->setHost(s
.cfg().host
.termedBuf());
179 * This event handler is necessary to work around the no-rentry policy
180 * of Adaptation::Icap::Xaction::callStart()
184 Adaptation::Icap::Xaction::reusedConnection(void *data
)
186 debugs(93, 5, HERE
<< "reused connection");
187 Adaptation::Icap::Xaction
*x
= (Adaptation::Icap::Xaction
*)data
;
188 x
->noteCommConnected(COMM_OK
);
192 void Adaptation::Icap::Xaction::closeConnection()
194 if (haveConnection()) {
196 if (closer
!= NULL
) {
197 comm_remove_close_handler(connection
->fd
, closer
);
201 cancelRead(); // may not work
203 if (reuseConnection
&& !doneWithIo()) {
204 //status() adds leading spaces.
205 debugs(93,5, HERE
<< "not reusing pconn due to pending I/O" << status());
206 reuseConnection
= false;
212 const bool reset
= !reuseConnection
&&
213 (al
.icap
.outcome
== xoGone
|| al
.icap
.outcome
== xoError
);
215 Adaptation::Icap::ServiceRep
&s
= service();
216 s
.putConnection(connection
, reuseConnection
, reset
, status());
225 // connection with the ICAP service established
226 void Adaptation::Icap::Xaction::noteCommConnected(const CommConnectCbParams
&io
)
230 if (io
.flag
== COMM_TIMEOUT
) {
231 handleCommTimedout();
235 Must(connector
!= NULL
);
238 if (io
.flag
!= COMM_OK
)
239 dieOnConnectionFailure(); // throws
241 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommTimeoutCbParams
> TimeoutDialer
;
242 AsyncCall::Pointer timeoutCall
= asyncCall(93, 5, "Adaptation::Icap::Xaction::noteCommTimedout",
243 TimeoutDialer(this,&Adaptation::Icap::Xaction::noteCommTimedout
));
244 commSetConnTimeout(io
.conn
, TheConfig
.connect_timeout(service().cfg().bypass
), timeoutCall
);
246 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommCloseCbParams
> CloseDialer
;
247 closer
= asyncCall(93, 5, "Adaptation::Icap::Xaction::noteCommClosed",
248 CloseDialer(this,&Adaptation::Icap::Xaction::noteCommClosed
));
249 comm_add_close_handler(io
.conn
->fd
, closer
);
251 // ?? fd_table[io.conn->fd].noteUse(icapPconnPool);
252 service().noteConnectionUse(connection
);
254 handleCommConnected();
257 void Adaptation::Icap::Xaction::dieOnConnectionFailure()
259 debugs(93, 2, HERE
<< typeName
<<
260 " failed to connect to " << service().cfg().uri
);
261 service().noteConnectionFailed("failure");
262 detailError(ERR_DETAIL_ICAP_XACT_START
);
263 throw TexcHere("cannot connect to the ICAP service");
266 void Adaptation::Icap::Xaction::scheduleWrite(MemBuf
&buf
)
268 Must(haveConnection());
270 // comm module will free the buffer
271 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommIoCbParams
> Dialer
;
272 writer
= JobCallback(93, 3,
273 Dialer
, this, Adaptation::Icap::Xaction::noteCommWrote
);
275 Comm::Write(connection
, &buf
, writer
);
279 void Adaptation::Icap::Xaction::noteCommWrote(const CommIoCbParams
&io
)
281 Must(writer
!= NULL
);
284 if (ignoreLastWrite
) {
285 // a hack due to comm inability to cancel a pending write
286 ignoreLastWrite
= false;
287 debugs(93, 7, HERE
<< "ignoring last write; status: " << io
.flag
);
289 Must(io
.flag
== COMM_OK
);
290 al
.icap
.bytesSent
+= io
.size
;
292 handleCommWrote(io
.size
);
296 // communication timeout with the ICAP service
297 void Adaptation::Icap::Xaction::noteCommTimedout(const CommTimeoutCbParams
&io
)
299 handleCommTimedout();
302 void Adaptation::Icap::Xaction::handleCommTimedout()
304 debugs(93, 2, HERE
<< typeName
<< " failed: timeout with " <<
305 theService
->cfg().methodStr() << " " <<
306 theService
->cfg().uri
<< status());
307 reuseConnection
= false;
308 const bool whileConnecting
= connector
!= NULL
;
309 if (whileConnecting
) {
310 assert(!haveConnection());
311 theService
->noteConnectionFailed("timedout");
313 closeConnection(); // so that late Comm callbacks do not disturb bypass
314 throw TexcHere(whileConnecting
?
315 "timed out while connecting to the ICAP service" :
316 "timed out while talking to the ICAP service");
319 // unexpected connection close while talking to the ICAP service
320 void Adaptation::Icap::Xaction::noteCommClosed(const CommCloseCbParams
&io
)
326 void Adaptation::Icap::Xaction::handleCommClosed()
328 detailError(ERR_DETAIL_ICAP_XACT_CLOSE
);
329 mustStop("ICAP service connection externally closed");
332 void Adaptation::Icap::Xaction::callException(const std::exception
&e
)
335 service().noteFailure();
336 Adaptation::Initiate::callException(e
);
339 void Adaptation::Icap::Xaction::callEnd()
342 debugs(93, 5, HERE
<< typeName
<< " done with I/O" << status());
345 Adaptation::Initiate::callEnd(); // may destroy us
348 bool Adaptation::Icap::Xaction::doneAll() const
350 return !connector
&& !reader
&& !writer
&& Adaptation::Initiate::doneAll();
353 void Adaptation::Icap::Xaction::updateTimeout()
355 Must(haveConnection());
357 if (reader
!= NULL
|| writer
!= NULL
) {
358 // restart the timeout before each I/O
359 // XXX: why does Config.Timeout lacks a write timeout?
360 // TODO: service bypass status may differ from that of a transaction
361 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommTimeoutCbParams
> TimeoutDialer
;
362 AsyncCall::Pointer call
= JobCallback(93, 5, TimeoutDialer
, this, Adaptation::Icap::Xaction::noteCommTimedout
);
363 commSetConnTimeout(connection
, TheConfig
.io_timeout(service().cfg().bypass
), call
);
365 // clear timeout when there is no I/O
366 // Do we need a lifetime timeout?
367 commUnsetConnTimeout(connection
);
371 void Adaptation::Icap::Xaction::scheduleRead()
373 Must(haveConnection());
375 Must(readBuf
.hasSpace());
378 * See comments in Adaptation::Icap::Xaction.h about why we use commBuf
379 * here instead of reading directly into readBuf.buf.
381 typedef CommCbMemFunT
<Adaptation::Icap::Xaction
, CommIoCbParams
> Dialer
;
382 reader
= JobCallback(93, 3,
383 Dialer
, this, Adaptation::Icap::Xaction::noteCommRead
);
385 comm_read(connection
, commBuf
, readBuf
.spaceSize(), reader
);
389 // comm module read a portion of the ICAP response for us
390 void Adaptation::Icap::Xaction::noteCommRead(const CommIoCbParams
&io
)
392 Must(reader
!= NULL
);
395 Must(io
.flag
== COMM_OK
);
399 reuseConnection
= false;
401 // detect a pconn race condition: eof on the first pconn read
402 if (!al
.icap
.bytesRead
&& retriable()) {
404 mustStop("pconn race");
409 al
.icap
.bytesRead
+=io
.size
;
413 debugs(93, 3, HERE
<< "read " << io
.size
<< " bytes");
416 * See comments in Adaptation::Icap::Xaction.h about why we use commBuf
417 * here instead of reading directly into readBuf.buf.
420 readBuf
.append(commBuf
, io
.size
);
421 disableRetries(); // because pconn did not fail
424 handleCommRead(io
.size
);
427 void Adaptation::Icap::Xaction::cancelRead()
429 if (reader
!= NULL
) {
430 Must(haveConnection());
431 comm_read_cancel(connection
->fd
, reader
);
436 bool Adaptation::Icap::Xaction::parseHttpMsg(HttpMsg
*msg
)
438 debugs(93, 5, HERE
<< "have " << readBuf
.contentSize() << " head bytes to parse");
440 Http::StatusCode error
= Http::scNone
;
441 const bool parsed
= msg
->parse(&readBuf
, commEof
, &error
);
442 Must(parsed
|| !error
); // success or need more data
444 if (!parsed
) { // need more data
450 readBuf
.consume(msg
->hdr_sz
);
454 bool Adaptation::Icap::Xaction::mayReadMore() const
456 return !doneReading() && // will read more data
457 readBuf
.hasSpace(); // have space for more data
460 bool Adaptation::Icap::Xaction::doneReading() const
465 bool Adaptation::Icap::Xaction::doneWriting() const
470 bool Adaptation::Icap::Xaction::doneWithIo() const
472 return haveConnection() &&
473 !connector
&& !reader
&& !writer
&& // fast checks, some redundant
474 doneReading() && doneWriting();
477 bool Adaptation::Icap::Xaction::haveConnection() const
479 return connection
!= NULL
&& connection
->isOpen();
483 void Adaptation::Icap::Xaction::noteInitiatorAborted()
486 if (theInitiator
.set()) {
487 debugs(93,4, HERE
<< "Initiator gone before ICAP transaction ended");
489 detailError(ERR_DETAIL_ICAP_INIT_GONE
);
491 mustStop("initiator aborted");
496 void Adaptation::Icap::Xaction::setOutcome(const Adaptation::Icap::XactOutcome
&xo
)
498 if (al
.icap
.outcome
!= xoUnknown
) {
499 debugs(93, 3, HERE
<< "Warning: reseting outcome: from " <<
500 al
.icap
.outcome
<< " to " << xo
);
502 debugs(93, 4, HERE
<< xo
);
504 al
.icap
.outcome
= xo
;
507 // This 'last chance' method is called before a 'done' transaction is deleted.
508 // It is wrong to call virtual methods from a destructor. Besides, this call
509 // indicates that the transaction will terminate as planned.
510 void Adaptation::Icap::Xaction::swanSong()
512 // kids should sing first and then call the parent method.
514 debugs(93,6, HERE
<< id
<< " about to notify ConnOpener!");
515 CallJobHere(93, 3, cs
, Comm::ConnOpener
, noteAbort
);
517 service().noteConnectionFailed("abort");
520 closeConnection(); // TODO: rename because we do not always close
522 if (!readBuf
.isNull())
526 memFreeBuf(commBufSize
, commBuf
);
532 Adaptation::Initiate::swanSong();
535 void Adaptation::Icap::Xaction::tellQueryAborted()
537 if (theInitiator
.set()) {
538 Adaptation::Icap::XactAbortInfo
abortInfo(icapRequest
, icapReply
.getRaw(),
539 retriable(), repeatable());
540 Launcher
*launcher
= dynamic_cast<Launcher
*>(theInitiator
.get());
541 // launcher may be nil if initiator is invalid
542 CallJobHere1(91,5, CbcPointer
<Launcher
>(launcher
),
543 Launcher
, noteXactAbort
, abortInfo
);
548 void Adaptation::Icap::Xaction::maybeLog()
550 if (IcapLogfileStatus
== LOG_ENABLE
) {
556 void Adaptation::Icap::Xaction::finalizeLogInfo()
559 al
.icp
.opcode
= ICP_INVALID
;
561 const Adaptation::Icap::ServiceRep
&s
= service();
562 al
.icap
.hostAddr
= s
.cfg().host
.termedBuf();
563 al
.icap
.serviceName
= s
.cfg().key
;
564 al
.icap
.reqUri
= s
.cfg().uri
;
566 al
.icap
.ioTime
= tvSubMsec(icap_tio_start
, icap_tio_finish
);
567 al
.icap
.trTime
= tvSubMsec(icap_tr_start
, current_time
);
569 al
.icap
.request
= icapRequest
;
570 HTTPMSGLOCK(al
.icap
.request
);
571 if (icapReply
!= NULL
) {
572 al
.icap
.reply
= icapReply
.getRaw();
573 HTTPMSGLOCK(al
.icap
.reply
);
574 al
.icap
.resStatus
= icapReply
->sline
.status();
578 // returns a temporary string depicting transaction status, for debugging
579 const char *Adaptation::Icap::Xaction::status() const
586 fillPendingStatus(buf
);
590 buf
.Printf(" %s%u]", id
.Prefix
, id
.value
);
594 return buf
.content();
597 void Adaptation::Icap::Xaction::fillPendingStatus(MemBuf
&buf
) const
599 if (haveConnection()) {
600 buf
.Printf("FD %d", connection
->fd
);
612 void Adaptation::Icap::Xaction::fillDoneStatus(MemBuf
&buf
) const
614 if (haveConnection() && commEof
)
615 buf
.Printf("Comm(%d)", connection
->fd
);
617 if (stopReason
!= NULL
)
618 buf
.Printf("Stopped");
621 bool Adaptation::Icap::Xaction::fillVirginHttpHeader(MemBuf
&buf
) const