]>
Commit | Line | Data |
---|---|---|
774c051c | 1 | /* |
507d0a78 | 2 | * DEBUG: section 93 ICAP (RFC 3507) Client |
774c051c | 3 | */ |
4 | ||
582c2af2 | 5 | #include "squid.h" |
3d93a84d | 6 | #include "AccessLogEntry.h" |
1adcebc3 | 7 | #include "adaptation/Answer.h" |
3d93a84d AJ |
8 | #include "adaptation/History.h" |
9 | #include "adaptation/icap/Client.h" | |
10 | #include "adaptation/icap/Config.h" | |
11 | #include "adaptation/icap/History.h" | |
12 | #include "adaptation/icap/Launcher.h" | |
13 | #include "adaptation/icap/ModXact.h" | |
14 | #include "adaptation/icap/ServiceRep.h" | |
15 | #include "adaptation/Initiator.h" | |
16 | #include "auth/UserRequest.h" | |
17 | #include "base/TextException.h" | |
25f98340 | 18 | #include "base64.h" |
3d93a84d | 19 | #include "ChunkedCodingParser.h" |
774c051c | 20 | #include "comm.h" |
d6327017 | 21 | #include "comm/Connection.h" |
582c2af2 | 22 | #include "err_detail_type.h" |
5f8252d2 | 23 | #include "HttpMsg.h" |
774c051c | 24 | #include "HttpReply.h" |
582c2af2 FC |
25 | #include "HttpRequest.h" |
26 | #include "protos.h" | |
985c86bc | 27 | #include "SquidTime.h" |
774c051c | 28 | |
29 | // flow and terminology: | |
30 | // HTTP| --> receive --> encode --> write --> |network | |
31 | // end | <-- send <-- parse <-- read <-- |end | |
32 | ||
774c051c | 33 | // TODO: replace gotEncapsulated() with something faster; we call it often |
34 | ||
26cc52cb AR |
35 | CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, ModXact); |
36 | CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, ModXactLauncher); | |
774c051c | 37 | |
5f8252d2 | 38 | static const size_t TheBackupLimit = BodyPipe::MaxCapacity; |
774c051c | 39 | |
26cc52cb | 40 | Adaptation::Icap::ModXact::State::State() |
774c051c | 41 | { |
09bfe95f | 42 | memset(this, 0, sizeof(*this)); |
774c051c | 43 | } |
44 | ||
4299f876 | 45 | Adaptation::Icap::ModXact::ModXact(HttpMsg *virginHeader, |
af6a12ee | 46 | HttpRequest *virginCause, Adaptation::Icap::ServiceRep::Pointer &aService): |
26cc52cb | 47 | AsyncJob("Adaptation::Icap::ModXact"), |
4299f876 | 48 | Adaptation::Icap::Xaction("Adaptation::Icap::ModXact", aService), |
9e008dda AJ |
49 | virginConsumed(0), |
50 | bodyParser(NULL), | |
3ff65596 | 51 | canStartBypass(false), // too early |
a22e6cd3 | 52 | protectGroupBypass(true), |
bae917ac CT |
53 | replyHttpHeaderSize(-1), |
54 | replyHttpBodySize(-1), | |
3ff65596 | 55 | adaptHistoryId(-1) |
774c051c | 56 | { |
5f8252d2 | 57 | assert(virginHeader); |
774c051c | 58 | |
5f8252d2 | 59 | virgin.setHeader(virginHeader); // sets virgin.body_pipe if needed |
60 | virgin.setCause(virginCause); // may be NULL | |
774c051c | 61 | |
5f8252d2 | 62 | // adapted header and body are initialized when we parse them |
774c051c | 63 | |
26cc52cb | 64 | // writing and reading ends are handled by Adaptation::Icap::Xaction |
774c051c | 65 | |
66 | // encoding | |
67 | // nothing to do because we are using temporary buffers | |
68 | ||
3ff65596 | 69 | // parsing; TODO: do not set until we parse, see ICAPOptXact |
c7d51c86 | 70 | icapReply = new HttpReply; |
774c051c | 71 | icapReply->protoPrefix = "ICAP/"; // TODO: make an IcapReply class? |
72 | ||
192378eb | 73 | debugs(93,7, HERE << "initialized." << status()); |
774c051c | 74 | } |
75 | ||
5f8252d2 | 76 | // initiator wants us to start |
26cc52cb | 77 | void Adaptation::Icap::ModXact::start() |
774c051c | 78 | { |
26cc52cb | 79 | Adaptation::Icap::Xaction::start(); |
774c051c | 80 | |
3ff65596 | 81 | // reserve an adaptation history slot (attempts are known at this time) |
a22e6cd3 | 82 | Adaptation::History::Pointer ah = virginRequest().adaptLogHistory(); |
3ff65596 AR |
83 | if (ah != NULL) |
84 | adaptHistoryId = ah->recordXactStart(service().cfg().key, icap_tr_start, attempts > 1); | |
85 | ||
774c051c | 86 | estimateVirginBody(); // before virgin disappears! |
87 | ||
0bef8dd7 | 88 | canStartBypass = service().cfg().bypass; |
478cfe99 | 89 | |
774c051c | 90 | // it is an ICAP violation to send request to a service w/o known OPTIONS |
2dba5b8e CT |
91 | // and the service may is too busy for us: honor Max-Connections and such |
92 | if (service().up() && service().availableForNew()) | |
774c051c | 93 | startWriting(); |
94 | else | |
95 | waitForService(); | |
774c051c | 96 | } |
97 | ||
26cc52cb | 98 | void Adaptation::Icap::ModXact::waitForService() |
774c051c | 99 | { |
2dba5b8e | 100 | const char *comment; |
774c051c | 101 | Must(!state.serviceWaiting); |
2dba5b8e CT |
102 | |
103 | if (!service().up()) { | |
104 | AsyncCall::Pointer call = JobCallback(93,5, | |
105 | ConnWaiterDialer, this, Adaptation::Icap::ModXact::noteServiceReady); | |
106 | ||
107 | service().callWhenReady(call); | |
108 | comment = "to be up"; | |
109 | } else { | |
110 | //The service is unavailable because of max-connection or other reason | |
111 | ||
112 | if (service().cfg().onOverload != srvWait) { | |
113 | // The service is overloaded, but waiting to be available prohibited by | |
114 | // user configuration (onOverload is set to "block" or "bypass") | |
115 | if (service().cfg().onOverload == srvBlock) | |
116 | disableBypass("not available", true); | |
117 | else //if (service().cfg().onOverload == srvBypass) | |
118 | canStartBypass = true; | |
119 | ||
120 | disableRetries(); | |
121 | disableRepeats("ICAP service is not available"); | |
122 | ||
123 | debugs(93, 7, HERE << "will not wait for the service to be available" << | |
124 | status()); | |
125 | ||
126 | throw TexcHere("ICAP service is not available"); | |
127 | } | |
128 | ||
129 | AsyncCall::Pointer call = JobCallback(93,5, | |
130 | ConnWaiterDialer, this, Adaptation::Icap::ModXact::noteServiceAvailable); | |
131 | service().callWhenAvailable(call, state.waitedForService); | |
132 | comment = "to be available"; | |
133 | } | |
134 | ||
135 | debugs(93, 7, HERE << "will wait for the service " << comment << status()); | |
c7d51c86 | 136 | state.serviceWaiting = true; // after callWhenReady() which may throw |
2dba5b8e | 137 | state.waitedForService = true; |
774c051c | 138 | } |
139 | ||
26cc52cb | 140 | void Adaptation::Icap::ModXact::noteServiceReady() |
774c051c | 141 | { |
774c051c | 142 | Must(state.serviceWaiting); |
143 | state.serviceWaiting = false; | |
c99de607 | 144 | |
2dba5b8e | 145 | if (!service().up()) { |
c824c43b | 146 | disableRetries(); |
3ff65596 | 147 | disableRepeats("ICAP service is unusable"); |
478cfe99 | 148 | throw TexcHere("ICAP service is unusable"); |
c824c43b | 149 | } |
2dba5b8e CT |
150 | |
151 | if (service().availableForOld()) | |
152 | startWriting(); | |
153 | else | |
154 | waitForService(); | |
155 | } | |
156 | ||
157 | void Adaptation::Icap::ModXact::noteServiceAvailable() | |
158 | { | |
159 | Must(state.serviceWaiting); | |
160 | state.serviceWaiting = false; | |
161 | ||
162 | if (service().up() && service().availableForOld()) | |
163 | startWriting(); | |
164 | else | |
165 | waitForService(); | |
774c051c | 166 | } |
167 | ||
26cc52cb | 168 | void Adaptation::Icap::ModXact::startWriting() |
774c051c | 169 | { |
774c051c | 170 | state.writing = State::writingConnect; |
c824c43b | 171 | |
172 | decideOnPreview(); // must be decided before we decideOnRetries | |
173 | decideOnRetries(); | |
174 | ||
774c051c | 175 | openConnection(); |
774c051c | 176 | } |
177 | ||
178 | // connection with the ICAP service established | |
26cc52cb | 179 | void Adaptation::Icap::ModXact::handleCommConnected() |
774c051c | 180 | { |
181 | Must(state.writing == State::writingConnect); | |
182 | ||
183 | startReading(); // wait for early errors from the ICAP server | |
184 | ||
185 | MemBuf requestBuf; | |
186 | requestBuf.init(); | |
187 | ||
188 | makeRequestHeaders(requestBuf); | |
192378eb | 189 | debugs(93, 9, HERE << "will write" << status() << ":\n" << |
774c051c | 190 | (requestBuf.terminate(), requestBuf.content())); |
191 | ||
192 | // write headers | |
193 | state.writing = State::writingHeaders; | |
3ff65596 | 194 | icap_tio_start = current_time; |
774c051c | 195 | scheduleWrite(requestBuf); |
196 | } | |
197 | ||
26cc52cb | 198 | void Adaptation::Icap::ModXact::handleCommWrote(size_t sz) |
774c051c | 199 | { |
b107a5a5 | 200 | debugs(93, 5, HERE << "Wrote " << sz << " bytes"); |
201 | ||
774c051c | 202 | if (state.writing == State::writingHeaders) |
203 | handleCommWroteHeaders(); | |
204 | else | |
205 | handleCommWroteBody(); | |
206 | } | |
207 | ||
26cc52cb | 208 | void Adaptation::Icap::ModXact::handleCommWroteHeaders() |
774c051c | 209 | { |
210 | Must(state.writing == State::writingHeaders); | |
211 | ||
5f8252d2 | 212 | // determine next step |
23e05fb1 AR |
213 | if (preview.enabled()) { |
214 | if (preview.done()) | |
215 | decideWritingAfterPreview("zero-size"); | |
216 | else | |
217 | state.writing = State::writingPreview; | |
218 | } else if (virginBody.expected()) { | |
e1381638 | 219 | state.writing = State::writingPrime; |
23e05fb1 | 220 | } else { |
e1381638 AJ |
221 | stopWriting(true); |
222 | return; | |
223 | } | |
5f8252d2 | 224 | |
225 | writeMore(); | |
774c051c | 226 | } |
227 | ||
26cc52cb | 228 | void Adaptation::Icap::ModXact::writeMore() |
774c051c | 229 | { |
5f8252d2 | 230 | debugs(93, 5, HERE << "checking whether to write more" << status()); |
231 | ||
bd7f2ede | 232 | if (writer != NULL) // already writing something |
774c051c | 233 | return; |
234 | ||
235 | switch (state.writing) { | |
236 | ||
237 | case State::writingInit: // waiting for service OPTIONS | |
238 | Must(state.serviceWaiting); | |
239 | ||
240 | case State::writingConnect: // waiting for the connection to establish | |
241 | ||
242 | case State::writingHeaders: // waiting for the headers to be written | |
243 | ||
244 | case State::writingPaused: // waiting for the ICAP server response | |
245 | ||
c99de607 | 246 | case State::writingReallyDone: // nothing more to write |
247 | return; | |
248 | ||
249 | case State::writingAlmostDone: // was waiting for the last write | |
250 | stopWriting(false); | |
774c051c | 251 | return; |
252 | ||
253 | case State::writingPreview: | |
5f8252d2 | 254 | writePreviewBody(); |
774c051c | 255 | return; |
256 | ||
257 | case State::writingPrime: | |
258 | writePrimeBody(); | |
259 | return; | |
260 | ||
261 | default: | |
26cc52cb | 262 | throw TexcHere("Adaptation::Icap::ModXact in bad writing state"); |
774c051c | 263 | } |
264 | } | |
265 | ||
26cc52cb | 266 | void Adaptation::Icap::ModXact::writePreviewBody() |
774c051c | 267 | { |
5f8252d2 | 268 | debugs(93, 8, HERE << "will write Preview body from " << |
9e008dda | 269 | virgin.body_pipe << status()); |
774c051c | 270 | Must(state.writing == State::writingPreview); |
5f8252d2 | 271 | Must(virgin.body_pipe != NULL); |
774c051c | 272 | |
5f8252d2 | 273 | const size_t sizeMax = (size_t)virgin.body_pipe->buf().contentSize(); |
d85c3078 | 274 | const size_t size = min(preview.debt(), sizeMax); |
774c051c | 275 | writeSomeBody("preview body", size); |
276 | ||
277 | // change state once preview is written | |
278 | ||
23e05fb1 AR |
279 | if (preview.done()) |
280 | decideWritingAfterPreview("body"); | |
281 | } | |
774c051c | 282 | |
23e05fb1 AR |
283 | /// determine state.writing after we wrote the entire preview |
284 | void Adaptation::Icap::ModXact::decideWritingAfterPreview(const char *kind) | |
285 | { | |
286 | if (preview.ieof()) // nothing more to write | |
287 | stopWriting(true); | |
e81d21e0 | 288 | else if (state.parsing == State::psIcapHeader) // did not get a reply yet |
23e05fb1 AR |
289 | state.writing = State::writingPaused; // wait for the ICAP server reply |
290 | else | |
291 | stopWriting(true); // ICAP server reply implies no post-preview writing | |
292 | ||
293 | debugs(93, 6, HERE << "decided on writing after " << kind << " preview" << | |
e81d21e0 | 294 | status()); |
774c051c | 295 | } |
296 | ||
26cc52cb | 297 | void Adaptation::Icap::ModXact::writePrimeBody() |
774c051c | 298 | { |
299 | Must(state.writing == State::writingPrime); | |
5f8252d2 | 300 | Must(virginBodyWriting.active()); |
774c051c | 301 | |
5f8252d2 | 302 | const size_t size = (size_t)virgin.body_pipe->buf().contentSize(); |
774c051c | 303 | writeSomeBody("prime virgin body", size); |
304 | ||
5f8252d2 | 305 | if (virginBodyEndReached(virginBodyWriting)) { |
306 | debugs(93, 5, HERE << "wrote entire body"); | |
c99de607 | 307 | stopWriting(true); |
b107a5a5 | 308 | } |
774c051c | 309 | } |
310 | ||
26cc52cb | 311 | void Adaptation::Icap::ModXact::writeSomeBody(const char *label, size_t size) |
774c051c | 312 | { |
c99de607 | 313 | Must(!writer && state.writing < state.writingAlmostDone); |
5f8252d2 | 314 | Must(virgin.body_pipe != NULL); |
12f4b710 | 315 | debugs(93, 8, HERE << "will write up to " << size << " bytes of " << |
774c051c | 316 | label); |
317 | ||
318 | MemBuf writeBuf; // TODO: suggest a min size based on size and lastChunk | |
319 | ||
320 | writeBuf.init(); // note: we assume that last-chunk will fit | |
321 | ||
5f8252d2 | 322 | const size_t writableSize = virginContentSize(virginBodyWriting); |
d85c3078 | 323 | const size_t chunkSize = min(writableSize, size); |
774c051c | 324 | |
325 | if (chunkSize) { | |
12f4b710 | 326 | debugs(93, 7, HERE << "will write " << chunkSize << |
774c051c | 327 | "-byte chunk of " << label); |
5f8252d2 | 328 | |
329 | openChunk(writeBuf, chunkSize, false); | |
330 | writeBuf.append(virginContentData(virginBodyWriting), chunkSize); | |
331 | closeChunk(writeBuf); | |
332 | ||
333 | virginBodyWriting.progress(chunkSize); | |
334 | virginConsume(); | |
774c051c | 335 | } else { |
192378eb | 336 | debugs(93, 7, HERE << "has no writable " << label << " content"); |
774c051c | 337 | } |
338 | ||
5f8252d2 | 339 | const bool wroteEof = virginBodyEndReached(virginBodyWriting); |
340 | bool lastChunk = wroteEof; | |
341 | if (state.writing == State::writingPreview) { | |
342 | preview.wrote(chunkSize, wroteEof); // even if wrote nothing | |
343 | lastChunk = lastChunk || preview.done(); | |
344 | } | |
774c051c | 345 | |
5f8252d2 | 346 | if (lastChunk) { |
12f4b710 | 347 | debugs(93, 8, HERE << "will write last-chunk of " << label); |
774c051c | 348 | addLastRequestChunk(writeBuf); |
349 | } | |
350 | ||
12f4b710 | 351 | debugs(93, 7, HERE << "will write " << writeBuf.contentSize() |
774c051c | 352 | << " raw bytes of " << label); |
353 | ||
354 | if (writeBuf.hasContent()) { | |
355 | scheduleWrite(writeBuf); // comm will free the chunk | |
356 | } else { | |
357 | writeBuf.clean(); | |
358 | } | |
359 | } | |
360 | ||
26cc52cb | 361 | void Adaptation::Icap::ModXact::addLastRequestChunk(MemBuf &buf) |
774c051c | 362 | { |
c99de607 | 363 | const bool ieof = state.writing == State::writingPreview && preview.ieof(); |
364 | openChunk(buf, 0, ieof); | |
365 | closeChunk(buf); | |
774c051c | 366 | } |
367 | ||
26cc52cb | 368 | void Adaptation::Icap::ModXact::openChunk(MemBuf &buf, size_t chunkSize, bool ieof) |
774c051c | 369 | { |
c99de607 | 370 | buf.Printf((ieof ? "%x; ieof\r\n" : "%x\r\n"), (int) chunkSize); |
774c051c | 371 | } |
372 | ||
26cc52cb | 373 | void Adaptation::Icap::ModXact::closeChunk(MemBuf &buf) |
774c051c | 374 | { |
774c051c | 375 | buf.append(ICAP::crlf, 2); // chunk-terminating CRLF |
376 | } | |
377 | ||
3ff65596 AR |
378 | const HttpRequest &Adaptation::Icap::ModXact::virginRequest() const |
379 | { | |
380 | const HttpRequest *request = virgin.cause ? | |
e1381638 | 381 | virgin.cause : dynamic_cast<const HttpRequest*>(virgin.header); |
3ff65596 AR |
382 | Must(request); |
383 | return *request; | |
384 | } | |
385 | ||
5f8252d2 | 386 | // did the activity reached the end of the virgin body? |
26cc52cb | 387 | bool Adaptation::Icap::ModXact::virginBodyEndReached(const Adaptation::Icap::VirginBodyAct &act) const |
5f8252d2 | 388 | { |
9e008dda | 389 | return |
5f8252d2 | 390 | !act.active() || // did all (assuming it was originally planned) |
391 | !virgin.body_pipe->expectMoreAfter(act.offset()); // wont have more | |
392 | } | |
393 | ||
394 | // the size of buffered virgin body data available for the specified activity | |
395 | // if this size is zero, we may be done or may be waiting for more data | |
26cc52cb | 396 | size_t Adaptation::Icap::ModXact::virginContentSize(const Adaptation::Icap::VirginBodyAct &act) const |
774c051c | 397 | { |
5f8252d2 | 398 | Must(act.active()); |
399 | // asbolute start of unprocessed data | |
b0365bd9 | 400 | const uint64_t dataStart = act.offset(); |
5f8252d2 | 401 | // absolute end of buffered data |
b0365bd9 FC |
402 | const uint64_t dataEnd = virginConsumed + virgin.body_pipe->buf().contentSize(); |
403 | Must(virginConsumed <= dataStart && dataStart <= dataEnd); | |
404 | return static_cast<size_t>(dataEnd - dataStart); | |
774c051c | 405 | } |
406 | ||
5f8252d2 | 407 | // pointer to buffered virgin body data available for the specified activity |
26cc52cb | 408 | const char *Adaptation::Icap::ModXact::virginContentData(const Adaptation::Icap::VirginBodyAct &act) const |
774c051c | 409 | { |
5f8252d2 | 410 | Must(act.active()); |
b0365bd9 FC |
411 | const uint64_t dataStart = act.offset(); |
412 | Must(virginConsumed <= dataStart); | |
413 | return virgin.body_pipe->buf().content() + static_cast<size_t>(dataStart-virginConsumed); | |
774c051c | 414 | } |
415 | ||
26cc52cb | 416 | void Adaptation::Icap::ModXact::virginConsume() |
774c051c | 417 | { |
3ff65596 | 418 | debugs(93, 9, HERE << "consumption guards: " << !virgin.body_pipe << isRetriable << |
a22e6cd3 | 419 | isRepeatable << canStartBypass << protectGroupBypass); |
478cfe99 | 420 | |
5f8252d2 | 421 | if (!virgin.body_pipe) |
c824c43b | 422 | return; // nothing to consume |
423 | ||
424 | if (isRetriable) | |
425 | return; // do not consume if we may have to retry later | |
5f8252d2 | 426 | |
427 | BodyPipe &bp = *virgin.body_pipe; | |
a22e6cd3 | 428 | const bool wantToPostpone = isRepeatable || canStartBypass || protectGroupBypass; |
478cfe99 | 429 | |
430 | // Why > 2? HttpState does not use the last bytes in the buffer | |
9e008dda | 431 | // because delayAwareRead() is arguably broken. See |
478cfe99 | 432 | // HttpStateData::maybeReadVirginBody for more details. |
3ff65596 | 433 | if (wantToPostpone && bp.buf().spaceSize() > 2) { |
478cfe99 | 434 | // Postponing may increase memory footprint and slow the HTTP side |
9e008dda | 435 | // down. Not postponing may increase the number of ICAP errors |
478cfe99 | 436 | // if the ICAP service fails. We may also use "potential" space to |
437 | // postpone more aggressively. Should the trade-off be configurable? | |
438 | debugs(93, 8, HERE << "postponing consumption from " << bp.status()); | |
439 | return; | |
440 | } | |
441 | ||
5f8252d2 | 442 | const size_t have = static_cast<size_t>(bp.buf().contentSize()); |
47f6e231 | 443 | const uint64_t end = virginConsumed + have; |
444 | uint64_t offset = end; | |
774c051c | 445 | |
478cfe99 | 446 | debugs(93, 9, HERE << "max virgin consumption offset=" << offset << |
9e008dda AJ |
447 | " acts " << virginBodyWriting.active() << virginBodySending.active() << |
448 | " consumed=" << virginConsumed << | |
449 | " from " << virgin.body_pipe->status()); | |
478cfe99 | 450 | |
5f8252d2 | 451 | if (virginBodyWriting.active()) |
d85c3078 | 452 | offset = min(virginBodyWriting.offset(), offset); |
774c051c | 453 | |
5f8252d2 | 454 | if (virginBodySending.active()) |
d85c3078 | 455 | offset = min(virginBodySending.offset(), offset); |
774c051c | 456 | |
457 | Must(virginConsumed <= offset && offset <= end); | |
458 | ||
47f6e231 | 459 | if (const size_t size = static_cast<size_t>(offset - virginConsumed)) { |
b107a5a5 | 460 | debugs(93, 8, HERE << "consuming " << size << " out of " << have << |
774c051c | 461 | " virgin body bytes"); |
5f8252d2 | 462 | bp.consume(size); |
774c051c | 463 | virginConsumed += size; |
c824c43b | 464 | Must(!isRetriable); // or we should not be consuming |
3ff65596 | 465 | disableRepeats("consumed content"); |
a22e6cd3 | 466 | disableBypass("consumed content", true); |
774c051c | 467 | } |
468 | } | |
469 | ||
26cc52cb | 470 | void Adaptation::Icap::ModXact::handleCommWroteBody() |
774c051c | 471 | { |
472 | writeMore(); | |
473 | } | |
474 | ||
c99de607 | 475 | // Called when we do not expect to call comm_write anymore. |
476 | // We may have a pending write though. | |
477 | // If stopping nicely, we will just wait for that pending write, if any. | |
26cc52cb | 478 | void Adaptation::Icap::ModXact::stopWriting(bool nicely) |
774c051c | 479 | { |
c99de607 | 480 | if (state.writing == State::writingReallyDone) |
774c051c | 481 | return; |
482 | ||
bd7f2ede | 483 | if (writer != NULL) { |
c99de607 | 484 | if (nicely) { |
5f8252d2 | 485 | debugs(93, 7, HERE << "will wait for the last write" << status()); |
c99de607 | 486 | state.writing = State::writingAlmostDone; // may already be set |
5f8252d2 | 487 | checkConsuming(); |
c99de607 | 488 | return; |
489 | } | |
4932ad93 | 490 | debugs(93, 3, HERE << "will NOT wait for the last write" << status()); |
774c051c | 491 | |
c99de607 | 492 | // Comm does not have an interface to clear the writer callback nicely, |
493 | // but without clearing the writer we cannot recycle the connection. | |
494 | // We prevent connection reuse and hope that we can handle a callback | |
5f8252d2 | 495 | // call at any time, usually in the middle of the destruction sequence! |
496 | // Somebody should add comm_remove_write_handler() to comm API. | |
c99de607 | 497 | reuseConnection = false; |
478cfe99 | 498 | ignoreLastWrite = true; |
c99de607 | 499 | } |
500 | ||
5f8252d2 | 501 | debugs(93, 7, HERE << "will no longer write" << status()); |
5f8252d2 | 502 | if (virginBodyWriting.active()) { |
503 | virginBodyWriting.disable(); | |
504 | virginConsume(); | |
505 | } | |
478cfe99 | 506 | state.writing = State::writingReallyDone; |
507 | checkConsuming(); | |
774c051c | 508 | } |
509 | ||
26cc52cb | 510 | void Adaptation::Icap::ModXact::stopBackup() |
774c051c | 511 | { |
5f8252d2 | 512 | if (!virginBodySending.active()) |
774c051c | 513 | return; |
514 | ||
192378eb | 515 | debugs(93, 7, HERE << "will no longer backup" << status()); |
5f8252d2 | 516 | virginBodySending.disable(); |
774c051c | 517 | virginConsume(); |
518 | } | |
519 | ||
26cc52cb | 520 | bool Adaptation::Icap::ModXact::doneAll() const |
774c051c | 521 | { |
26cc52cb | 522 | return Adaptation::Icap::Xaction::doneAll() && !state.serviceWaiting && |
5f8252d2 | 523 | doneSending() && |
774c051c | 524 | doneReading() && state.doneWriting(); |
525 | } | |
526 | ||
26cc52cb | 527 | void Adaptation::Icap::ModXact::startReading() |
774c051c | 528 | { |
aed188fd | 529 | Must(haveConnection()); |
774c051c | 530 | Must(!reader); |
5f8252d2 | 531 | Must(!adapted.header); |
532 | Must(!adapted.body_pipe); | |
774c051c | 533 | |
534 | // we use the same buffer for headers and body and then consume headers | |
535 | readMore(); | |
536 | } | |
537 | ||
26cc52cb | 538 | void Adaptation::Icap::ModXact::readMore() |
774c051c | 539 | { |
bd7f2ede | 540 | if (reader != NULL || doneReading()) { |
c99de607 | 541 | debugs(93,3,HERE << "returning from readMore because reader or doneReading()"); |
774c051c | 542 | return; |
3b299123 | 543 | } |
774c051c | 544 | |
545 | // do not fill readBuf if we have no space to store the result | |
5f8252d2 | 546 | if (adapted.body_pipe != NULL && |
9e008dda | 547 | !adapted.body_pipe->buf().hasPotentialSpace()) { |
5f8252d2 | 548 | debugs(93,3,HERE << "not reading because ICAP reply pipe is full"); |
774c051c | 549 | return; |
3b299123 | 550 | } |
774c051c | 551 | |
552 | if (readBuf.hasSpace()) | |
553 | scheduleRead(); | |
3b299123 | 554 | else |
c99de607 | 555 | debugs(93,3,HERE << "nothing to do because !readBuf.hasSpace()"); |
774c051c | 556 | } |
557 | ||
558 | // comm module read a portion of the ICAP response for us | |
26cc52cb | 559 | void Adaptation::Icap::ModXact::handleCommRead(size_t) |
774c051c | 560 | { |
561 | Must(!state.doneParsing()); | |
3ff65596 | 562 | icap_tio_finish = current_time; |
774c051c | 563 | parseMore(); |
564 | readMore(); | |
565 | } | |
566 | ||
26cc52cb | 567 | void Adaptation::Icap::ModXact::echoMore() |
774c051c | 568 | { |
569 | Must(state.sending == State::sendingVirgin); | |
5f8252d2 | 570 | Must(adapted.body_pipe != NULL); |
571 | Must(virginBodySending.active()); | |
572 | ||
573 | const size_t sizeMax = virginContentSize(virginBodySending); | |
574 | debugs(93,5, HERE << "will echo up to " << sizeMax << " bytes from " << | |
9e008dda | 575 | virgin.body_pipe->status()); |
5f8252d2 | 576 | debugs(93,5, HERE << "will echo up to " << sizeMax << " bytes to " << |
9e008dda | 577 | adapted.body_pipe->status()); |
5f8252d2 | 578 | |
579 | if (sizeMax > 0) { | |
580 | const size_t size = adapted.body_pipe->putMoreData(virginContentData(virginBodySending), sizeMax); | |
581 | debugs(93,5, HERE << "echoed " << size << " out of " << sizeMax << | |
9e008dda | 582 | " bytes"); |
5f8252d2 | 583 | virginBodySending.progress(size); |
3ff65596 | 584 | disableRepeats("echoed content"); |
a22e6cd3 AR |
585 | disableBypass("echoed content", true); |
586 | virginConsume(); | |
774c051c | 587 | } |
588 | ||
5f8252d2 | 589 | if (virginBodyEndReached(virginBodySending)) { |
192378eb | 590 | debugs(93, 5, HERE << "echoed all" << status()); |
774c051c | 591 | stopSending(true); |
592 | } else { | |
192378eb | 593 | debugs(93, 5, HERE << "has " << |
9e008dda AJ |
594 | virgin.body_pipe->buf().contentSize() << " bytes " << |
595 | "and expects more to echo" << status()); | |
5f8252d2 | 596 | // TODO: timeout if virgin or adapted pipes are broken |
774c051c | 597 | } |
598 | } | |
599 | ||
26cc52cb | 600 | bool Adaptation::Icap::ModXact::doneSending() const |
774c051c | 601 | { |
774c051c | 602 | return state.sending == State::sendingDone; |
603 | } | |
604 | ||
478cfe99 | 605 | // stop (or do not start) sending adapted message body |
26cc52cb | 606 | void Adaptation::Icap::ModXact::stopSending(bool nicely) |
774c051c | 607 | { |
3ff65596 | 608 | debugs(93, 7, HERE << "Enter stop sending "); |
774c051c | 609 | if (doneSending()) |
610 | return; | |
3ff65596 | 611 | debugs(93, 7, HERE << "Proceed with stop sending "); |
774c051c | 612 | |
613 | if (state.sending != State::sendingUndecided) { | |
192378eb | 614 | debugs(93, 7, HERE << "will no longer send" << status()); |
5f8252d2 | 615 | if (adapted.body_pipe != NULL) { |
616 | virginBodySending.disable(); | |
617 | // we may leave debts if we were echoing and the virgin | |
618 | // body_pipe got exhausted before we echoed all planned bytes | |
619 | const bool leftDebts = adapted.body_pipe->needsMoreData(); | |
620 | stopProducingFor(adapted.body_pipe, nicely && !leftDebts); | |
621 | } | |
774c051c | 622 | } else { |
192378eb | 623 | debugs(93, 7, HERE << "will not start sending" << status()); |
5f8252d2 | 624 | Must(!adapted.body_pipe); |
774c051c | 625 | } |
626 | ||
627 | state.sending = State::sendingDone; | |
5f8252d2 | 628 | checkConsuming(); |
774c051c | 629 | } |
630 | ||
5f8252d2 | 631 | // should be called after certain state.writing or state.sending changes |
26cc52cb | 632 | void Adaptation::Icap::ModXact::checkConsuming() |
774c051c | 633 | { |
5f8252d2 | 634 | // quit if we already stopped or are still using the pipe |
635 | if (!virgin.body_pipe || !state.doneConsumingVirgin()) | |
774c051c | 636 | return; |
637 | ||
5f8252d2 | 638 | debugs(93, 7, HERE << "will stop consuming" << status()); |
639 | stopConsumingFrom(virgin.body_pipe); | |
774c051c | 640 | } |
641 | ||
26cc52cb | 642 | void Adaptation::Icap::ModXact::parseMore() |
774c051c | 643 | { |
aa761e5f | 644 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " bytes to parse" << |
774c051c | 645 | status()); |
d5cfacfb | 646 | debugs(93, 5, HERE << "\n" << readBuf.content()); |
774c051c | 647 | |
648 | if (state.parsingHeaders()) | |
649 | parseHeaders(); | |
650 | ||
651 | if (state.parsing == State::psBody) | |
652 | parseBody(); | |
653 | } | |
654 | ||
26cc52cb | 655 | void Adaptation::Icap::ModXact::callException(const std::exception &e) |
478cfe99 | 656 | { |
657 | if (!canStartBypass || isRetriable) { | |
64b66b76 CT |
658 | if (!isRetriable) { |
659 | if (const TextException *te = dynamic_cast<const TextException *>(&e)) | |
660 | detailError(ERR_DETAIL_EXCEPTION_START + te->id()); | |
661 | else | |
662 | detailError(ERR_DETAIL_EXCEPTION_OTHER); | |
663 | } | |
26cc52cb | 664 | Adaptation::Icap::Xaction::callException(e); |
478cfe99 | 665 | return; |
666 | } | |
667 | ||
668 | try { | |
192378eb | 669 | debugs(93, 3, HERE << "bypassing " << inCall << " exception: " << |
af6a12ee | 670 | e.what() << ' ' << status()); |
478cfe99 | 671 | bypassFailure(); |
64b66b76 CT |
672 | } catch (const TextException &bypassTe) { |
673 | detailError(ERR_DETAIL_EXCEPTION_START + bypassTe.id()); | |
674 | Adaptation::Icap::Xaction::callException(bypassTe); | |
9e008dda | 675 | } catch (const std::exception &bypassE) { |
64b66b76 | 676 | detailError(ERR_DETAIL_EXCEPTION_OTHER); |
26cc52cb | 677 | Adaptation::Icap::Xaction::callException(bypassE); |
478cfe99 | 678 | } |
679 | } | |
680 | ||
26cc52cb | 681 | void Adaptation::Icap::ModXact::bypassFailure() |
478cfe99 | 682 | { |
a22e6cd3 | 683 | disableBypass("already started to bypass", false); |
478cfe99 | 684 | |
685 | Must(!isRetriable); // or we should not be bypassing | |
3ff65596 | 686 | // TODO: should the same be enforced for isRepeatable? Check icap_repeat?? |
478cfe99 | 687 | |
688 | prepEchoing(); | |
689 | ||
690 | startSending(); | |
691 | ||
692 | // end all activities associated with the ICAP server | |
693 | ||
694 | stopParsing(); | |
695 | ||
696 | stopWriting(true); // or should we force it? | |
aed188fd | 697 | if (haveConnection()) { |
478cfe99 | 698 | reuseConnection = false; // be conservative |
699 | cancelRead(); // may not work; and we cannot stop connecting either | |
700 | if (!doneWithIo()) | |
192378eb | 701 | debugs(93, 7, HERE << "Warning: bypass failed to stop I/O" << status()); |
478cfe99 | 702 | } |
61cb3565 AR |
703 | |
704 | service().noteFailure(); // we are bypassing, but this is still a failure | |
478cfe99 | 705 | } |
706 | ||
a22e6cd3 | 707 | void Adaptation::Icap::ModXact::disableBypass(const char *reason, bool includingGroupBypass) |
478cfe99 | 708 | { |
709 | if (canStartBypass) { | |
710 | debugs(93,7, HERE << "will never start bypass because " << reason); | |
711 | canStartBypass = false; | |
712 | } | |
a22e6cd3 AR |
713 | if (protectGroupBypass && includingGroupBypass) { |
714 | debugs(93,7, HERE << "not protecting group bypass because " << reason); | |
715 | protectGroupBypass = false; | |
716 | } | |
478cfe99 | 717 | } |
718 | ||
774c051c | 719 | // note that allocation for echoing is done in handle204NoContent() |
26cc52cb | 720 | void Adaptation::Icap::ModXact::maybeAllocateHttpMsg() |
774c051c | 721 | { |
5f8252d2 | 722 | if (adapted.header) // already allocated |
774c051c | 723 | return; |
724 | ||
725 | if (gotEncapsulated("res-hdr")) { | |
5f8252d2 | 726 | adapted.setHeader(new HttpReply); |
3ff65596 | 727 | setOutcome(service().cfg().method == ICAP::methodReqmod ? |
e1381638 | 728 | xoSatisfied : xoModified); |
774c051c | 729 | } else if (gotEncapsulated("req-hdr")) { |
5f8252d2 | 730 | adapted.setHeader(new HttpRequest); |
3ff65596 | 731 | setOutcome(xoModified); |
774c051c | 732 | } else |
733 | throw TexcHere("Neither res-hdr nor req-hdr in maybeAllocateHttpMsg()"); | |
734 | } | |
735 | ||
26cc52cb | 736 | void Adaptation::Icap::ModXact::parseHeaders() |
774c051c | 737 | { |
738 | Must(state.parsingHeaders()); | |
739 | ||
b107a5a5 | 740 | if (state.parsing == State::psIcapHeader) { |
741 | debugs(93, 5, HERE << "parse ICAP headers"); | |
774c051c | 742 | parseIcapHead(); |
b107a5a5 | 743 | } |
774c051c | 744 | |
b107a5a5 | 745 | if (state.parsing == State::psHttpHeader) { |
746 | debugs(93, 5, HERE << "parse HTTP headers"); | |
774c051c | 747 | parseHttpHead(); |
b107a5a5 | 748 | } |
774c051c | 749 | |
750 | if (state.parsingHeaders()) { // need more data | |
751 | Must(mayReadMore()); | |
752 | return; | |
753 | } | |
754 | ||
478cfe99 | 755 | startSending(); |
756 | } | |
757 | ||
758 | // called after parsing all headers or when bypassing an exception | |
26cc52cb | 759 | void Adaptation::Icap::ModXact::startSending() |
478cfe99 | 760 | { |
3ff65596 | 761 | disableRepeats("sent headers"); |
a22e6cd3 | 762 | disableBypass("sent headers", true); |
3af10ac0 | 763 | sendAnswer(Answer::Forward(adapted.header)); |
774c051c | 764 | |
765 | if (state.sending == State::sendingVirgin) | |
766 | echoMore(); | |
767 | } | |
768 | ||
26cc52cb | 769 | void Adaptation::Icap::ModXact::parseIcapHead() |
774c051c | 770 | { |
771 | Must(state.sending == State::sendingUndecided); | |
772 | ||
773 | if (!parseHead(icapReply)) | |
774 | return; | |
775 | ||
fc764d26 | 776 | if (httpHeaderHasConnDir(&icapReply->header, "close")) { |
777 | debugs(93, 5, HERE << "found connection close"); | |
778 | reuseConnection = false; | |
779 | } | |
780 | ||
774c051c | 781 | switch (icapReply->sline.status) { |
782 | ||
783 | case 100: | |
784 | handle100Continue(); | |
785 | break; | |
786 | ||
787 | case 200: | |
5bd21e1d | 788 | case 201: // Symantec Scan Engine 5.0 and later when modifying HTTP msg |
b559db5d | 789 | |
790 | if (!validate200Ok()) { | |
791 | throw TexcHere("Invalid ICAP Response"); | |
792 | } else { | |
793 | handle200Ok(); | |
794 | } | |
795 | ||
774c051c | 796 | break; |
797 | ||
798 | case 204: | |
799 | handle204NoContent(); | |
800 | break; | |
801 | ||
83c51da9 CT |
802 | case 206: |
803 | handle206PartialContent(); | |
804 | break; | |
805 | ||
774c051c | 806 | default: |
b559db5d | 807 | debugs(93, 5, HERE << "ICAP status " << icapReply->sline.status); |
774c051c | 808 | handleUnknownScode(); |
809 | break; | |
810 | } | |
811 | ||
3ff65596 AR |
812 | const HttpRequest *request = dynamic_cast<HttpRequest*>(adapted.header); |
813 | if (!request) | |
814 | request = &virginRequest(); | |
815 | ||
816 | // update the cross-transactional database if needed (all status codes!) | |
817 | if (const char *xxName = Adaptation::Config::masterx_shared_name) { | |
a22e6cd3 | 818 | Adaptation::History::Pointer ah = request->adaptHistory(true); |
5038f9d8 | 819 | if (ah != NULL) { // TODO: reorder checks to avoid creating history |
3ff65596 AR |
820 | const String val = icapReply->header.getByName(xxName); |
821 | if (val.size() > 0) // XXX: HttpHeader lacks empty value detection | |
822 | ah->updateXxRecord(xxName, val); | |
823 | } | |
824 | } | |
825 | ||
a22e6cd3 AR |
826 | // update the adaptation plan if needed (all status codes!) |
827 | if (service().cfg().routing) { | |
828 | String services; | |
829 | if (icapReply->header.getList(HDR_X_NEXT_SERVICES, &services)) { | |
830 | Adaptation::History::Pointer ah = request->adaptHistory(true); | |
831 | if (ah != NULL) | |
832 | ah->updateNextServices(services); | |
833 | } | |
834 | } // TODO: else warn (occasionally!) if we got HDR_X_NEXT_SERVICES | |
835 | ||
3ff65596 AR |
836 | // We need to store received ICAP headers for <icapLastHeader logformat option. |
837 | // If we already have stored headers from previous ICAP transaction related to this | |
838 | // request, old headers will be replaced with the new one. | |
e1381638 | 839 | |
5038f9d8 AR |
840 | Adaptation::History::Pointer ah = request->adaptLogHistory(); |
841 | if (ah != NULL) | |
842 | ah->recordMeta(&icapReply->header); | |
3ff65596 | 843 | |
774c051c | 844 | // handle100Continue() manages state.writing on its own. |
845 | // Non-100 status means the server needs no postPreview data from us. | |
846 | if (state.writing == State::writingPaused) | |
c99de607 | 847 | stopWriting(true); |
774c051c | 848 | } |
849 | ||
26cc52cb | 850 | bool Adaptation::Icap::ModXact::validate200Ok() |
b559db5d | 851 | { |
0bef8dd7 | 852 | if (ICAP::methodRespmod == service().cfg().method) { |
b559db5d | 853 | if (!gotEncapsulated("res-hdr")) |
854 | return false; | |
855 | ||
856 | return true; | |
857 | } | |
858 | ||
0bef8dd7 | 859 | if (ICAP::methodReqmod == service().cfg().method) { |
b559db5d | 860 | if (!gotEncapsulated("res-hdr") && !gotEncapsulated("req-hdr")) |
861 | return false; | |
862 | ||
863 | return true; | |
864 | } | |
865 | ||
866 | return false; | |
867 | } | |
868 | ||
26cc52cb | 869 | void Adaptation::Icap::ModXact::handle100Continue() |
774c051c | 870 | { |
871 | Must(state.writing == State::writingPaused); | |
5f8252d2 | 872 | // server must not respond before the end of preview: we may send ieof |
774c051c | 873 | Must(preview.enabled() && preview.done() && !preview.ieof()); |
774c051c | 874 | |
83c51da9 CT |
875 | // 100 "Continue" cancels our Preview commitment, |
876 | // but not commitment to handle 204 or 206 outside Preview | |
877 | if (!state.allowedPostview204 && !state.allowedPostview206) | |
774c051c | 878 | stopBackup(); |
879 | ||
c99de607 | 880 | state.parsing = State::psIcapHeader; // eventually |
881 | icapReply->reset(); | |
774c051c | 882 | |
883 | state.writing = State::writingPrime; | |
884 | ||
885 | writeMore(); | |
886 | } | |
887 | ||
26cc52cb | 888 | void Adaptation::Icap::ModXact::handle200Ok() |
774c051c | 889 | { |
890 | state.parsing = State::psHttpHeader; | |
891 | state.sending = State::sendingAdapted; | |
892 | stopBackup(); | |
5f8252d2 | 893 | checkConsuming(); |
774c051c | 894 | } |
895 | ||
26cc52cb | 896 | void Adaptation::Icap::ModXact::handle204NoContent() |
774c051c | 897 | { |
898 | stopParsing(); | |
478cfe99 | 899 | prepEchoing(); |
900 | } | |
901 | ||
83c51da9 CT |
902 | void Adaptation::Icap::ModXact::handle206PartialContent() |
903 | { | |
904 | if (state.writing == State::writingPaused) { | |
905 | Must(preview.enabled()); | |
906 | Must(state.allowedPreview206); | |
907 | debugs(93, 7, HERE << "206 inside preview"); | |
908 | } else { | |
909 | Must(state.writing > State::writingPaused); | |
910 | Must(state.allowedPostview206); | |
911 | debugs(93, 7, HERE << "206 outside preview"); | |
912 | } | |
913 | state.parsing = State::psHttpHeader; | |
914 | state.sending = State::sendingAdapted; | |
915 | state.readyForUob = true; | |
916 | checkConsuming(); | |
917 | } | |
918 | ||
478cfe99 | 919 | // Called when we receive a 204 No Content response and |
920 | // when we are trying to bypass a service failure. | |
921 | // We actually start sending (echoig or not) in startSending. | |
26cc52cb | 922 | void Adaptation::Icap::ModXact::prepEchoing() |
478cfe99 | 923 | { |
3ff65596 | 924 | disableRepeats("preparing to echo content"); |
a22e6cd3 | 925 | disableBypass("preparing to echo content", true); |
3ff65596 | 926 | setOutcome(xoEcho); |
774c051c | 927 | |
928 | // We want to clone the HTTP message, but we do not want | |
5f8252d2 | 929 | // to copy some non-HTTP state parts that HttpMsg kids carry in them. |
774c051c | 930 | // Thus, we cannot use a smart pointer, copy constructor, or equivalent. |
931 | // Instead, we simply write the HTTP message and "clone" it by parsing. | |
a22e6cd3 | 932 | // TODO: use HttpMsg::clone()! |
774c051c | 933 | |
5f8252d2 | 934 | HttpMsg *oldHead = virgin.header; |
192378eb | 935 | debugs(93, 7, HERE << "cloning virgin message " << oldHead); |
774c051c | 936 | |
937 | MemBuf httpBuf; | |
938 | ||
939 | // write the virgin message into a memory buffer | |
940 | httpBuf.init(); | |
941 | packHead(httpBuf, oldHead); | |
942 | ||
c99de607 | 943 | // allocate the adapted message and copy metainfo |
5f8252d2 | 944 | Must(!adapted.header); |
c7d51c86 | 945 | { |
e6f9e263 | 946 | HttpMsg::Pointer newHead; |
73a805c2 | 947 | if (dynamic_cast<const HttpRequest*>(oldHead)) { |
e6f9e263 | 948 | HttpRequest::Pointer newR(new HttpRequest); |
e6f9e263 A |
949 | newHead = newR; |
950 | } else if (dynamic_cast<const HttpReply*>(oldHead)) { | |
951 | newHead = new HttpReply; | |
952 | } | |
953 | Must(newHead != NULL); | |
774c051c | 954 | |
e6f9e263 | 955 | newHead->inheritProperties(oldHead); |
774c051c | 956 | |
e6f9e263 | 957 | adapted.setHeader(newHead); |
c7d51c86 | 958 | } |
7514268e | 959 | |
774c051c | 960 | // parse the buffer back |
961 | http_status error = HTTP_STATUS_NONE; | |
962 | ||
c7d51c86 | 963 | Must(adapted.header->parse(&httpBuf, true, &error)); |
774c051c | 964 | |
73a805c2 CT |
965 | if (HttpRequest *r = dynamic_cast<HttpRequest*>(adapted.header)) |
966 | urlCanonical(r); // parse does not set HttpRequest::canonical | |
967 | ||
c7d51c86 | 968 | Must(adapted.header->hdr_sz == httpBuf.contentSize()); // no leftovers |
774c051c | 969 | |
970 | httpBuf.clean(); | |
971 | ||
192378eb | 972 | debugs(93, 7, HERE << "cloned virgin message " << oldHead << " to " << |
c7d51c86 | 973 | adapted.header); |
5f8252d2 | 974 | |
975 | // setup adapted body pipe if needed | |
976 | if (oldHead->body_pipe != NULL) { | |
977 | debugs(93, 7, HERE << "will echo virgin body from " << | |
9e008dda | 978 | oldHead->body_pipe); |
478cfe99 | 979 | if (!virginBodySending.active()) |
980 | virginBodySending.plan(); // will throw if not possible | |
5f8252d2 | 981 | state.sending = State::sendingVirgin; |
982 | checkConsuming(); | |
478cfe99 | 983 | |
5f8252d2 | 984 | // TODO: optimize: is it possible to just use the oldHead pipe and |
985 | // remove ICAP from the loop? This echoing is probably a common case! | |
986 | makeAdaptedBodyPipe("echoed virgin response"); | |
987 | if (oldHead->body_pipe->bodySizeKnown()) | |
988 | adapted.body_pipe->setBodySize(oldHead->body_pipe->bodySize()); | |
989 | debugs(93, 7, HERE << "will echo virgin body to " << | |
9e008dda | 990 | adapted.body_pipe); |
5f8252d2 | 991 | } else { |
992 | debugs(93, 7, HERE << "no virgin body to echo"); | |
993 | stopSending(true); | |
994 | } | |
774c051c | 995 | } |
996 | ||
83c51da9 CT |
997 | /// Called when we received use-original-body chunk extension in 206 response. |
998 | /// We actually start sending (echoing or not) in startSending(). | |
999 | void Adaptation::Icap::ModXact::prepPartialBodyEchoing(uint64_t pos) | |
1000 | { | |
1001 | Must(virginBodySending.active()); | |
1002 | Must(virgin.header->body_pipe != NULL); | |
1003 | ||
1004 | setOutcome(xoPartEcho); | |
1005 | ||
1006 | debugs(93, 7, HERE << "will echo virgin body suffix from " << | |
1007 | virgin.header->body_pipe << " offset " << pos ); | |
1008 | ||
1009 | // check that use-original-body=N does not point beyond buffered data | |
1010 | const uint64_t virginDataEnd = virginConsumed + | |
7ddcfbab | 1011 | virgin.body_pipe->buf().contentSize(); |
83c51da9 CT |
1012 | Must(pos <= virginDataEnd); |
1013 | virginBodySending.progress(static_cast<size_t>(pos)); | |
1014 | ||
1015 | state.sending = State::sendingVirgin; | |
1016 | checkConsuming(); | |
1017 | ||
1018 | if (virgin.header->body_pipe->bodySizeKnown()) | |
1019 | adapted.body_pipe->expectProductionEndAfter(virgin.header->body_pipe->bodySize() - pos); | |
1020 | ||
1021 | debugs(93, 7, HERE << "will echo virgin body suffix to " << | |
1022 | adapted.body_pipe); | |
1023 | ||
1024 | // Start echoing data | |
1025 | echoMore(); | |
1026 | } | |
1027 | ||
26cc52cb | 1028 | void Adaptation::Icap::ModXact::handleUnknownScode() |
774c051c | 1029 | { |
1030 | stopParsing(); | |
1031 | stopBackup(); | |
1032 | // TODO: mark connection as "bad" | |
1033 | ||
1034 | // Terminate the transaction; we do not know how to handle this response. | |
1035 | throw TexcHere("Unsupported ICAP status code"); | |
1036 | } | |
1037 | ||
26cc52cb | 1038 | void Adaptation::Icap::ModXact::parseHttpHead() |
774c051c | 1039 | { |
1040 | if (gotEncapsulated("res-hdr") || gotEncapsulated("req-hdr")) { | |
bae917ac | 1041 | replyHttpHeaderSize = 0; |
774c051c | 1042 | maybeAllocateHttpMsg(); |
1043 | ||
5f8252d2 | 1044 | if (!parseHead(adapted.header)) |
c99de607 | 1045 | return; // need more header data |
5f8252d2 | 1046 | |
bae917ac CT |
1047 | if (adapted.header) |
1048 | replyHttpHeaderSize = adapted.header->hdr_sz; | |
1049 | ||
d67acb4e | 1050 | if (dynamic_cast<HttpRequest*>(adapted.header)) { |
5f8252d2 | 1051 | const HttpRequest *oldR = dynamic_cast<const HttpRequest*>(virgin.header); |
1052 | Must(oldR); | |
9e008dda AJ |
1053 | // TODO: the adapted request did not really originate from the |
1054 | // client; give proxy admin an option to prevent copying of | |
5f8252d2 | 1055 | // sensitive client information here. See the following thread: |
1056 | // http://www.squid-cache.org/mail-archive/squid-dev/200703/0040.html | |
5f8252d2 | 1057 | } |
d67acb4e | 1058 | |
9e008dda AJ |
1059 | // Maybe adapted.header==NULL if HttpReply and have Http 0.9 .... |
1060 | if (adapted.header) | |
1061 | adapted.header->inheritProperties(virgin.header); | |
774c051c | 1062 | } |
1063 | ||
5f8252d2 | 1064 | decideOnParsingBody(); |
774c051c | 1065 | } |
1066 | ||
c99de607 | 1067 | // parses both HTTP and ICAP headers |
26cc52cb | 1068 | bool Adaptation::Icap::ModXact::parseHead(HttpMsg *head) |
774c051c | 1069 | { |
c99de607 | 1070 | Must(head); |
def17b6a | 1071 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " head bytes to parse" << |
774c051c | 1072 | "; state: " << state.parsing); |
1073 | ||
1074 | http_status error = HTTP_STATUS_NONE; | |
1075 | const bool parsed = head->parse(&readBuf, commEof, &error); | |
1076 | Must(parsed || !error); // success or need more data | |
1077 | ||
c99de607 | 1078 | if (!parsed) { // need more data |
b107a5a5 | 1079 | debugs(93, 5, HERE << "parse failed, need more data, return false"); |
774c051c | 1080 | head->reset(); |
1081 | return false; | |
1082 | } | |
1083 | ||
a22e6cd3 AR |
1084 | if (HttpRequest *r = dynamic_cast<HttpRequest*>(head)) |
1085 | urlCanonical(r); // parse does not set HttpRequest::canonical | |
1086 | ||
b107a5a5 | 1087 | debugs(93, 5, HERE << "parse success, consume " << head->hdr_sz << " bytes, return true"); |
774c051c | 1088 | readBuf.consume(head->hdr_sz); |
1089 | return true; | |
1090 | } | |
1091 | ||
26cc52cb | 1092 | void Adaptation::Icap::ModXact::decideOnParsingBody() |
9e008dda | 1093 | { |
200ac359 | 1094 | if (gotEncapsulated("res-body") || gotEncapsulated("req-body")) { |
5f8252d2 | 1095 | debugs(93, 5, HERE << "expecting a body"); |
1096 | state.parsing = State::psBody; | |
bae917ac | 1097 | replyHttpBodySize = 0; |
5f8252d2 | 1098 | bodyParser = new ChunkedCodingParser; |
1099 | makeAdaptedBodyPipe("adapted response from the ICAP server"); | |
1100 | Must(state.sending == State::sendingAdapted); | |
774c051c | 1101 | } else { |
b559db5d | 1102 | debugs(93, 5, HERE << "not expecting a body"); |
5f8252d2 | 1103 | stopParsing(); |
1104 | stopSending(true); | |
774c051c | 1105 | } |
774c051c | 1106 | } |
1107 | ||
26cc52cb | 1108 | void Adaptation::Icap::ModXact::parseBody() |
774c051c | 1109 | { |
5f8252d2 | 1110 | Must(state.parsing == State::psBody); |
1111 | Must(bodyParser); | |
774c051c | 1112 | |
5f8252d2 | 1113 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " body bytes to parse"); |
774c051c | 1114 | |
5f8252d2 | 1115 | // the parser will throw on errors |
1116 | BodyPipeCheckout bpc(*adapted.body_pipe); | |
1117 | const bool parsed = bodyParser->parse(&readBuf, &bpc.buf); | |
1118 | bpc.checkIn(); | |
774c051c | 1119 | |
aa761e5f | 1120 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " body bytes after " << |
774c051c | 1121 | "parse; parsed all: " << parsed); |
bae917ac | 1122 | replyHttpBodySize += adapted.body_pipe->buf().contentSize(); |
774c051c | 1123 | |
478cfe99 | 1124 | // TODO: expose BodyPipe::putSize() to make this check simpler and clearer |
3ff65596 AR |
1125 | // TODO: do we really need this if we disable when sending headers? |
1126 | if (adapted.body_pipe->buf().contentSize() > 0) { // parsed something sometime | |
1127 | disableRepeats("sent adapted content"); | |
a22e6cd3 | 1128 | disableBypass("sent adapted content", true); |
3ff65596 | 1129 | } |
478cfe99 | 1130 | |
5f8252d2 | 1131 | if (parsed) { |
83c51da9 CT |
1132 | if (state.readyForUob && bodyParser->useOriginBody >= 0) { |
1133 | prepPartialBodyEchoing( | |
1134 | static_cast<uint64_t>(bodyParser->useOriginBody)); | |
1135 | stopParsing(); | |
1136 | return; | |
1137 | } | |
1138 | ||
5f8252d2 | 1139 | stopParsing(); |
1140 | stopSending(true); // the parser succeeds only if all parsed data fits | |
1141 | return; | |
1142 | } | |
774c051c | 1143 | |
c99de607 | 1144 | debugs(93,3,HERE << this << " needsMoreData = " << bodyParser->needsMoreData()); |
3b299123 | 1145 | |
1146 | if (bodyParser->needsMoreData()) { | |
c99de607 | 1147 | debugs(93,3,HERE << this); |
774c051c | 1148 | Must(mayReadMore()); |
3b299123 | 1149 | readMore(); |
1150 | } | |
774c051c | 1151 | |
1152 | if (bodyParser->needsMoreSpace()) { | |
1153 | Must(!doneSending()); // can hope for more space | |
5f8252d2 | 1154 | Must(adapted.body_pipe->buf().contentSize() > 0); // paranoid |
1155 | // TODO: there should be a timeout in case the sink is broken | |
1156 | // or cannot consume partial content (while we need more space) | |
774c051c | 1157 | } |
774c051c | 1158 | } |
1159 | ||
26cc52cb | 1160 | void Adaptation::Icap::ModXact::stopParsing() |
774c051c | 1161 | { |
1162 | if (state.parsing == State::psDone) | |
1163 | return; | |
1164 | ||
192378eb | 1165 | debugs(93, 7, HERE << "will no longer parse" << status()); |
774c051c | 1166 | |
1167 | delete bodyParser; | |
1168 | ||
1169 | bodyParser = NULL; | |
1170 | ||
1171 | state.parsing = State::psDone; | |
1172 | } | |
1173 | ||
1174 | // HTTP side added virgin body data | |
26cc52cb | 1175 | void Adaptation::Icap::ModXact::noteMoreBodyDataAvailable(BodyPipe::Pointer) |
774c051c | 1176 | { |
774c051c | 1177 | writeMore(); |
1178 | ||
1179 | if (state.sending == State::sendingVirgin) | |
1180 | echoMore(); | |
774c051c | 1181 | } |
1182 | ||
1183 | // HTTP side sent us all virgin info | |
26cc52cb | 1184 | void Adaptation::Icap::ModXact::noteBodyProductionEnded(BodyPipe::Pointer) |
774c051c | 1185 | { |
5f8252d2 | 1186 | Must(virgin.body_pipe->productionEnded()); |
774c051c | 1187 | |
1188 | // push writer and sender in case we were waiting for the last-chunk | |
1189 | writeMore(); | |
1190 | ||
1191 | if (state.sending == State::sendingVirgin) | |
1192 | echoMore(); | |
774c051c | 1193 | } |
1194 | ||
9e008dda | 1195 | // body producer aborted, but the initiator may still want to know |
585ab260 | 1196 | // the answer, even though the HTTP message has been truncated |
26cc52cb | 1197 | void Adaptation::Icap::ModXact::noteBodyProducerAborted(BodyPipe::Pointer) |
774c051c | 1198 | { |
585ab260 | 1199 | Must(virgin.body_pipe->productionEnded()); |
1200 | ||
1201 | // push writer and sender in case we were waiting for the last-chunk | |
1202 | writeMore(); | |
1203 | ||
1204 | if (state.sending == State::sendingVirgin) | |
1205 | echoMore(); | |
5f8252d2 | 1206 | } |
1207 | ||
9e008dda | 1208 | // adapted body consumer wants more adapted data and |
5f8252d2 | 1209 | // possibly freed some buffer space |
26cc52cb | 1210 | void Adaptation::Icap::ModXact::noteMoreBodySpaceAvailable(BodyPipe::Pointer) |
774c051c | 1211 | { |
774c051c | 1212 | if (state.sending == State::sendingVirgin) |
1213 | echoMore(); | |
3b299123 | 1214 | else if (state.sending == State::sendingAdapted) |
1215 | parseMore(); | |
774c051c | 1216 | else |
3b299123 | 1217 | Must(state.sending == State::sendingUndecided); |
774c051c | 1218 | } |
1219 | ||
5f8252d2 | 1220 | // adapted body consumer aborted |
26cc52cb | 1221 | void Adaptation::Icap::ModXact::noteBodyConsumerAborted(BodyPipe::Pointer) |
774c051c | 1222 | { |
64b66b76 | 1223 | detailError(ERR_DETAIL_ICAP_XACT_BODY_CONSUMER_ABORT); |
5f8252d2 | 1224 | mustStop("adapted body consumer aborted"); |
774c051c | 1225 | } |
1226 | ||
c7d51c86 AR |
1227 | Adaptation::Icap::ModXact::~ModXact() |
1228 | { | |
1229 | delete bodyParser; | |
1230 | } | |
1231 | ||
774c051c | 1232 | // internal cleanup |
26cc52cb | 1233 | void Adaptation::Icap::ModXact::swanSong() |
774c051c | 1234 | { |
5f8252d2 | 1235 | debugs(93, 5, HERE << "swan sings" << status()); |
1236 | ||
c99de607 | 1237 | stopWriting(false); |
c824c43b | 1238 | stopSending(false); |
774c051c | 1239 | |
64b66b76 CT |
1240 | if (theInitiator.set()) // we have not sent the answer to the initiator |
1241 | detailError(ERR_DETAIL_ICAP_XACT_OTHER); | |
1242 | ||
3ff65596 | 1243 | // update adaptation history if start was called and we reserved a slot |
a22e6cd3 | 1244 | Adaptation::History::Pointer ah = virginRequest().adaptLogHistory(); |
3ff65596 AR |
1245 | if (ah != NULL && adaptHistoryId >= 0) |
1246 | ah->recordXactFinish(adaptHistoryId); | |
774c051c | 1247 | |
26cc52cb | 1248 | Adaptation::Icap::Xaction::swanSong(); |
774c051c | 1249 | } |
1250 | ||
41ebd397 | 1251 | void prepareLogWithRequestDetails(HttpRequest *, AccessLogEntry::Pointer &); |
3ff65596 AR |
1252 | |
1253 | void Adaptation::Icap::ModXact::finalizeLogInfo() | |
1254 | { | |
1255 | HttpRequest * request_ = NULL; | |
1256 | HttpReply * reply_ = NULL; | |
e1381638 | 1257 | if (!(request_ = dynamic_cast<HttpRequest*>(adapted.header))) { |
3ff65596 AR |
1258 | request_ = (virgin.cause? virgin.cause: dynamic_cast<HttpRequest*>(virgin.header)); |
1259 | reply_ = dynamic_cast<HttpReply*>(adapted.header); | |
1260 | } | |
1261 | ||
1262 | Adaptation::Icap::History::Pointer h = request_->icapHistory(); | |
e1381638 AJ |
1263 | Must(h != NULL); // ICAPXaction::maybeLog calls only if there is a log |
1264 | al.icp.opcode = ICP_INVALID; | |
1265 | al.url = h->log_uri.termedBuf(); | |
1266 | const Adaptation::Icap::ServiceRep &s = service(); | |
1267 | al.icap.reqMethod = s.cfg().method; | |
3ff65596 | 1268 | |
e1381638 | 1269 | al.cache.caddr = request_->client_addr; |
3ff65596 | 1270 | |
e1381638 AJ |
1271 | al.request = HTTPMSGLOCK(request_); |
1272 | if (reply_) | |
1273 | al.reply = HTTPMSGLOCK(reply_); | |
1274 | else | |
1275 | al.reply = NULL; | |
3ff65596 | 1276 | |
e1381638 AJ |
1277 | if (h->rfc931.size()) |
1278 | al.cache.rfc931 = h->rfc931.termedBuf(); | |
3ff65596 AR |
1279 | |
1280 | #if USE_SSL | |
e1381638 AJ |
1281 | if (h->ssluser.size()) |
1282 | al.cache.ssluser = h->ssluser.termedBuf(); | |
3ff65596 | 1283 | #endif |
e1381638 AJ |
1284 | al.cache.code = h->logType; |
1285 | al.cache.requestSize = h->req_sz; | |
bae917ac CT |
1286 | |
1287 | // leave al.icap.bodyBytesRead negative if no body | |
1288 | if (replyHttpHeaderSize >= 0 || replyHttpBodySize >= 0) { | |
1289 | const int64_t zero = 0; // to make max() argument types the same | |
1290 | al.icap.bodyBytesRead = | |
1291 | max(zero, replyHttpHeaderSize) + max(zero, replyHttpBodySize); | |
1292 | } | |
1293 | ||
e1381638 AJ |
1294 | if (reply_) { |
1295 | al.http.code = reply_->sline.status; | |
1296 | al.http.content_type = reply_->content_type.termedBuf(); | |
bae917ac CT |
1297 | if (replyHttpBodySize >= 0) { |
1298 | al.cache.replySize = replyHttpBodySize + reply_->hdr_sz; | |
1299 | al.cache.highOffset = replyHttpBodySize; | |
1300 | } | |
e1381638 AJ |
1301 | //don't set al.cache.objectSize because it hasn't exist yet |
1302 | ||
1303 | Packer p; | |
1304 | MemBuf mb; | |
1305 | ||
1306 | mb.init(); | |
1307 | packerToMemInit(&p, &mb); | |
1308 | ||
1309 | reply_->header.packInto(&p); | |
1310 | al.headers.reply = xstrdup(mb.buf); | |
1311 | ||
1312 | packerClean(&p); | |
1313 | mb.clean(); | |
1314 | } | |
41ebd397 | 1315 | prepareLogWithRequestDetails(request_, alep); |
e1381638 | 1316 | Xaction::finalizeLogInfo(); |
3ff65596 AR |
1317 | } |
1318 | ||
26cc52cb | 1319 | void Adaptation::Icap::ModXact::makeRequestHeaders(MemBuf &buf) |
774c051c | 1320 | { |
cc192b50 | 1321 | char ntoabuf[MAX_IPSTRLEN]; |
12b91c99 | 1322 | /* |
1323 | * XXX These should use HttpHdr interfaces instead of Printfs | |
1324 | */ | |
0bef8dd7 | 1325 | const Adaptation::ServiceConfig &s = service().cfg(); |
2c1fd837 | 1326 | buf.Printf("%s " SQUIDSTRINGPH " ICAP/1.0\r\n", s.methodStr(), SQUIDSTRINGPRINT(s.uri)); |
826a1fed | 1327 | buf.Printf("Host: " SQUIDSTRINGPH ":%d\r\n", SQUIDSTRINGPRINT(s.host), s.port); |
12b91c99 | 1328 | buf.Printf("Date: %s\r\n", mkrfc1123(squid_curtime)); |
1329 | ||
26cc52cb | 1330 | if (!TheConfig.reuse_connections) |
12b91c99 | 1331 | buf.Printf("Connection: close\r\n"); |
1332 | ||
cb72cd25 CT |
1333 | const HttpRequest *request = &virginRequest(); |
1334 | ||
2cdeea82 | 1335 | // we must forward "Proxy-Authenticate" and "Proxy-Authorization" |
1336 | // as ICAP headers. | |
4232c626 FC |
1337 | if (virgin.header->header.has(HDR_PROXY_AUTHENTICATE)) { |
1338 | String vh=virgin.header->header.getByName("Proxy-Authenticate"); | |
826a1fed | 1339 | buf.Printf("Proxy-Authenticate: " SQUIDSTRINGPH "\r\n",SQUIDSTRINGPRINT(vh)); |
4232c626 | 1340 | } |
9e008dda | 1341 | |
4232c626 FC |
1342 | if (virgin.header->header.has(HDR_PROXY_AUTHORIZATION)) { |
1343 | String vh=virgin.header->header.getByName("Proxy-Authorization"); | |
826a1fed | 1344 | buf.Printf("Proxy-Authorization: " SQUIDSTRINGPH "\r\n", SQUIDSTRINGPRINT(vh)); |
cb72cd25 | 1345 | } else if (request->extacl_user.defined() && request->extacl_user.size() && request->extacl_passwd.defined() && request->extacl_passwd.size()) { |
8661a1d0 A |
1346 | char loginbuf[256]; |
1347 | snprintf(loginbuf, sizeof(loginbuf), SQUIDSTRINGPH ":" SQUIDSTRINGPH, | |
1348 | SQUIDSTRINGPRINT(request->extacl_user), | |
1349 | SQUIDSTRINGPRINT(request->extacl_passwd)); | |
1350 | buf.Printf("Proxy-Authorization: Basic %s\r\n", old_base64_encode(loginbuf)); | |
4232c626 | 1351 | } |
2cdeea82 | 1352 | |
3ff65596 AR |
1353 | // share the cross-transactional database records if needed |
1354 | if (Adaptation::Config::masterx_shared_name) { | |
ba8a478d | 1355 | Adaptation::History::Pointer ah = request->adaptHistory(false); |
3ff65596 AR |
1356 | if (ah != NULL) { |
1357 | String name, value; | |
1358 | if (ah->getXxRecord(name, value)) { | |
e1381638 AJ |
1359 | buf.Printf(SQUIDSTRINGPH ": " SQUIDSTRINGPH "\r\n", |
1360 | SQUIDSTRINGPRINT(name), SQUIDSTRINGPRINT(value)); | |
3ff65596 AR |
1361 | } |
1362 | } | |
1363 | } | |
e1381638 | 1364 | |
774c051c | 1365 | buf.Printf("Encapsulated: "); |
1366 | ||
1367 | MemBuf httpBuf; | |
12b91c99 | 1368 | |
774c051c | 1369 | httpBuf.init(); |
1370 | ||
1371 | // build HTTP request header, if any | |
1372 | ICAP::Method m = s.method; | |
1373 | ||
5f8252d2 | 1374 | // to simplify, we could assume that request is always available |
c99de607 | 1375 | |
30abd221 | 1376 | String urlPath; |
c99de607 | 1377 | if (request) { |
1378 | urlPath = request->urlpath; | |
1379 | if (ICAP::methodRespmod == m) | |
1380 | encapsulateHead(buf, "req-hdr", httpBuf, request); | |
e1381638 AJ |
1381 | else if (ICAP::methodReqmod == m) |
1382 | encapsulateHead(buf, "req-hdr", httpBuf, virgin.header); | |
c99de607 | 1383 | } |
774c051c | 1384 | |
1385 | if (ICAP::methodRespmod == m) | |
5f8252d2 | 1386 | if (const HttpMsg *prime = virgin.header) |
774c051c | 1387 | encapsulateHead(buf, "res-hdr", httpBuf, prime); |
1388 | ||
1389 | if (!virginBody.expected()) | |
1dd6edf2 | 1390 | buf.Printf("null-body=%d", (int) httpBuf.contentSize()); |
774c051c | 1391 | else if (ICAP::methodReqmod == m) |
1dd6edf2 | 1392 | buf.Printf("req-body=%d", (int) httpBuf.contentSize()); |
774c051c | 1393 | else |
1dd6edf2 | 1394 | buf.Printf("res-body=%d", (int) httpBuf.contentSize()); |
774c051c | 1395 | |
1396 | buf.append(ICAP::crlf, 2); // terminate Encapsulated line | |
1397 | ||
c824c43b | 1398 | if (preview.enabled()) { |
774c051c | 1399 | buf.Printf("Preview: %d\r\n", (int)preview.ad()); |
83c51da9 | 1400 | if (!virginBody.expected()) // there is no body to preview |
5f8252d2 | 1401 | finishNullOrEmptyBodyPreview(httpBuf); |
774c051c | 1402 | } |
1403 | ||
83c51da9 | 1404 | makeAllowHeader(buf); |
774c051c | 1405 | |
a9044668 | 1406 | if (TheConfig.send_client_ip && request) { |
b7ac5457 | 1407 | Ip::Address client_addr; |
57d76dd4 | 1408 | #if FOLLOW_X_FORWARDED_FOR |
22fff3bf | 1409 | if (TheConfig.use_indirect_client) { |
a9044668 A |
1410 | client_addr = request->indirect_client_addr; |
1411 | } else | |
57d76dd4 | 1412 | #endif |
a9044668 A |
1413 | client_addr = request->client_addr; |
1414 | if (!client_addr.IsAnyAddr() && !client_addr.IsNoAddr()) | |
1415 | buf.Printf("X-Client-IP: %s\r\n", client_addr.NtoA(ntoabuf,MAX_IPSTRLEN)); | |
57d76dd4 | 1416 | } |
a97e82a8 | 1417 | |
22fff3bf | 1418 | if (TheConfig.send_username && request) |
5f8252d2 | 1419 | makeUsernameHeader(request, buf); |
a97e82a8 | 1420 | |
71be37e0 CT |
1421 | // Adaptation::Config::metaHeaders |
1422 | typedef Adaptation::Config::MetaHeaders::iterator ACAMLI; | |
71ee0835 | 1423 | for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) { |
71be37e0 | 1424 | HttpRequest *r = virgin.cause ? |
71ee0835 | 1425 | virgin.cause : dynamic_cast<HttpRequest*>(virgin.header); |
71be37e0 CT |
1426 | Must(r); |
1427 | ||
1428 | HttpReply *reply = dynamic_cast<HttpReply*>(virgin.header); | |
1429 | ||
1430 | if (const char *value = (*i)->match(r, reply)) | |
1431 | buf.Printf("%s: %s\r\n", (*i)->name.termedBuf(), value); | |
1432 | } | |
1433 | ||
bb790702 | 1434 | // fprintf(stderr, "%s\n", buf.content()); |
a97e82a8 | 1435 | |
774c051c | 1436 | buf.append(ICAP::crlf, 2); // terminate ICAP header |
1437 | ||
a22e6cd3 AR |
1438 | // fill icapRequest for logging |
1439 | Must(icapRequest->parseCharBuf(buf.content(), buf.contentSize())); | |
1440 | ||
774c051c | 1441 | // start ICAP request body with encapsulated HTTP headers |
1442 | buf.append(httpBuf.content(), httpBuf.contentSize()); | |
1443 | ||
1444 | httpBuf.clean(); | |
1445 | } | |
1446 | ||
83c51da9 CT |
1447 | // decides which Allow values to write and updates the request buffer |
1448 | void Adaptation::Icap::ModXact::makeAllowHeader(MemBuf &buf) | |
1449 | { | |
1450 | const bool allow204in = preview.enabled(); // TODO: add shouldAllow204in() | |
1451 | const bool allow204out = state.allowedPostview204 = shouldAllow204(); | |
1452 | const bool allow206in = state.allowedPreview206 = shouldAllow206in(); | |
1453 | const bool allow206out = state.allowedPostview206 = shouldAllow206out(); | |
1454 | ||
1455 | debugs(93,9, HERE << "Allows: " << allow204in << allow204out << | |
7ddcfbab | 1456 | allow206in << allow206out); |
83c51da9 CT |
1457 | |
1458 | const bool allow204 = allow204in || allow204out; | |
1459 | const bool allow206 = allow206in || allow206out; | |
1460 | ||
1461 | if (!allow204 && !allow206) | |
1462 | return; // nothing to do | |
1463 | ||
1464 | if (virginBody.expected()) // if there is a virgin body, plan to send it | |
1465 | virginBodySending.plan(); | |
1466 | ||
1467 | // writing Preview:... means we will honor 204 inside preview | |
1468 | // writing Allow/204 means we will honor 204 outside preview | |
1469 | // writing Allow:206 means we will honor 206 inside preview | |
1470 | // writing Allow:204,206 means we will honor 206 outside preview | |
1471 | const char *allowHeader = NULL; | |
1472 | if (allow204out && allow206) | |
1473 | allowHeader = "Allow: 204, 206\r\n"; | |
1474 | else if (allow204out) | |
1475 | allowHeader = "Allow: 204\r\n"; | |
1476 | else if (allow206) | |
1477 | allowHeader = "Allow: 206\r\n"; | |
7ddcfbab | 1478 | |
83c51da9 CT |
1479 | if (allowHeader) { // may be nil if only allow204in is true |
1480 | buf.append(allowHeader, strlen(allowHeader)); | |
1481 | debugs(93,5, HERE << "Will write " << allowHeader); | |
1482 | } | |
1483 | } | |
1484 | ||
26cc52cb | 1485 | void Adaptation::Icap::ModXact::makeUsernameHeader(const HttpRequest *request, MemBuf &buf) |
9e008dda | 1486 | { |
74f6c040 | 1487 | #if USE_AUTH |
a33a428a | 1488 | if (request->auth_user_request != NULL) { |
9b275e99 | 1489 | char const *name = request->auth_user_request->username(); |
a33a428a | 1490 | if (name) { |
8bdd0cec | 1491 | const char *value = TheConfig.client_username_encode ? old_base64_encode(name) : name; |
a33a428a | 1492 | buf.Printf("%s: %s\r\n", TheConfig.client_username_header, value); |
5f8252d2 | 1493 | } |
cb72cd25 CT |
1494 | } else if (request->extacl_user.defined() && request->extacl_user.size()) { |
1495 | const char *value = TheConfig.client_username_encode ? old_base64_encode(request->extacl_user.termedBuf()) : request->extacl_user.termedBuf(); | |
1496 | buf.Printf("%s: %s\r\n", TheConfig.client_username_header, value); | |
5f8252d2 | 1497 | } |
74f6c040 | 1498 | #endif |
5f8252d2 | 1499 | } |
1500 | ||
26cc52cb | 1501 | void Adaptation::Icap::ModXact::encapsulateHead(MemBuf &icapBuf, const char *section, MemBuf &httpBuf, const HttpMsg *head) |
774c051c | 1502 | { |
1503 | // update ICAP header | |
7cab7e9f | 1504 | icapBuf.Printf("%s=%d, ", section, (int) httpBuf.contentSize()); |
774c051c | 1505 | |
2cdeea82 | 1506 | // begin cloning |
c7d51c86 | 1507 | HttpMsg::Pointer headClone; |
9e008dda | 1508 | |
2cdeea82 | 1509 | if (const HttpRequest* old_request = dynamic_cast<const HttpRequest*>(head)) { |
c7d51c86 AR |
1510 | HttpRequest::Pointer new_request(new HttpRequest); |
1511 | Must(old_request->canonical); | |
a22e6cd3 | 1512 | urlParse(old_request->method, old_request->canonical, new_request); |
2cdeea82 | 1513 | new_request->http_ver = old_request->http_ver; |
2cdeea82 | 1514 | headClone = new_request; |
9e008dda | 1515 | } else if (const HttpReply *old_reply = dynamic_cast<const HttpReply*>(head)) { |
c7d51c86 | 1516 | HttpReply::Pointer new_reply(new HttpReply); |
2cdeea82 | 1517 | new_reply->sline = old_reply->sline; |
1518 | headClone = new_reply; | |
1519 | } | |
c7d51c86 | 1520 | Must(headClone != NULL); |
d67acb4e | 1521 | headClone->inheritProperties(head); |
9e008dda | 1522 | |
2cdeea82 | 1523 | HttpHeaderPos pos = HttpHeaderInitPos; |
1524 | HttpHeaderEntry* p_head_entry = NULL; | |
1525 | while (NULL != (p_head_entry = head->header.getEntry(&pos)) ) | |
1526 | headClone->header.addEntry(p_head_entry->clone()); | |
1527 | ||
1528 | // end cloning | |
9e008dda | 1529 | |
2cdeea82 | 1530 | // remove all hop-by-hop headers from the clone |
dcf3665b | 1531 | headClone->header.delById(HDR_PROXY_AUTHENTICATE); |
2cdeea82 | 1532 | headClone->header.removeHopByHopEntries(); |
1533 | ||
1534 | // pack polished HTTP header | |
1535 | packHead(httpBuf, headClone); | |
1536 | ||
c7d51c86 | 1537 | // headClone unlocks and, hence, deletes the message we packed |
774c051c | 1538 | } |
1539 | ||
26cc52cb | 1540 | void Adaptation::Icap::ModXact::packHead(MemBuf &httpBuf, const HttpMsg *head) |
774c051c | 1541 | { |
1542 | Packer p; | |
1543 | packerToMemInit(&p, &httpBuf); | |
1544 | head->packInto(&p, true); | |
1545 | packerClean(&p); | |
1546 | } | |
1547 | ||
1548 | // decides whether to offer a preview and calculates its size | |
26cc52cb | 1549 | void Adaptation::Icap::ModXact::decideOnPreview() |
774c051c | 1550 | { |
26cc52cb | 1551 | if (!TheConfig.preview_enable) { |
7cdbbd47 | 1552 | debugs(93, 5, HERE << "preview disabled by squid.conf"); |
c824c43b | 1553 | return; |
7cdbbd47 | 1554 | } |
1555 | ||
3ff65596 | 1556 | const String urlPath = virginRequest().urlpath; |
5f8252d2 | 1557 | size_t wantedSize; |
c99de607 | 1558 | if (!service().wantsPreview(urlPath, wantedSize)) { |
192378eb | 1559 | debugs(93, 5, HERE << "should not offer preview for " << urlPath); |
c824c43b | 1560 | return; |
774c051c | 1561 | } |
1562 | ||
c824c43b | 1563 | // we decided to do preview, now compute its size |
1564 | ||
774c051c | 1565 | // cannot preview more than we can backup |
d85c3078 | 1566 | size_t ad = min(wantedSize, TheBackupLimit); |
774c051c | 1567 | |
5f8252d2 | 1568 | if (!virginBody.expected()) |
1569 | ad = 0; | |
e1381638 AJ |
1570 | else if (virginBody.knownSize()) |
1571 | ad = min(static_cast<uint64_t>(ad), virginBody.size()); // not more than we have | |
774c051c | 1572 | |
192378eb | 1573 | debugs(93, 5, HERE << "should offer " << ad << "-byte preview " << |
774c051c | 1574 | "(service wanted " << wantedSize << ")"); |
1575 | ||
1576 | preview.enable(ad); | |
5f8252d2 | 1577 | Must(preview.enabled()); |
774c051c | 1578 | } |
1579 | ||
1580 | // decides whether to allow 204 responses | |
26cc52cb | 1581 | bool Adaptation::Icap::ModXact::shouldAllow204() |
774c051c | 1582 | { |
1583 | if (!service().allows204()) | |
1584 | return false; | |
1585 | ||
c824c43b | 1586 | return canBackupEverything(); |
1587 | } | |
1588 | ||
83c51da9 CT |
1589 | // decides whether to allow 206 responses in some mode |
1590 | bool Adaptation::Icap::ModXact::shouldAllow206any() | |
1591 | { | |
1592 | return TheConfig.allow206_enable && service().allows206() && | |
7ddcfbab | 1593 | virginBody.expected(); // no need for 206 without a body |
83c51da9 CT |
1594 | } |
1595 | ||
1596 | // decides whether to allow 206 responses in preview mode | |
1597 | bool Adaptation::Icap::ModXact::shouldAllow206in() | |
1598 | { | |
1599 | return shouldAllow206any() && preview.enabled(); | |
1600 | } | |
1601 | ||
1602 | // decides whether to allow 206 responses outside of preview | |
1603 | bool Adaptation::Icap::ModXact::shouldAllow206out() | |
1604 | { | |
1605 | return shouldAllow206any() && canBackupEverything(); | |
1606 | } | |
1607 | ||
c824c43b | 1608 | // used by shouldAllow204 and decideOnRetries |
26cc52cb | 1609 | bool Adaptation::Icap::ModXact::canBackupEverything() const |
c824c43b | 1610 | { |
774c051c | 1611 | if (!virginBody.expected()) |
c824c43b | 1612 | return true; // no body means no problems with backup |
774c051c | 1613 | |
c824c43b | 1614 | // if there is a body, check whether we can backup it all |
774c051c | 1615 | |
1616 | if (!virginBody.knownSize()) | |
1617 | return false; | |
1618 | ||
1619 | // or should we have a different backup limit? | |
1620 | // note that '<' allows for 0-termination of the "full" backup buffer | |
1621 | return virginBody.size() < TheBackupLimit; | |
1622 | } | |
1623 | ||
c824c43b | 1624 | // Decide whether this transaction can be retried if pconn fails |
1625 | // Must be called after decideOnPreview and before openConnection() | |
26cc52cb | 1626 | void Adaptation::Icap::ModXact::decideOnRetries() |
c824c43b | 1627 | { |
1628 | if (!isRetriable) | |
1629 | return; // no, already decided | |
1630 | ||
1631 | if (preview.enabled()) | |
1632 | return; // yes, because preview provides enough guarantees | |
1633 | ||
1634 | if (canBackupEverything()) | |
1635 | return; // yes, because we can back everything up | |
1636 | ||
1637 | disableRetries(); // no, because we cannot back everything up | |
1638 | } | |
1639 | ||
5f8252d2 | 1640 | // Normally, the body-writing code handles preview body. It can deal with |
1641 | // bodies of unexpected size, including those that turn out to be empty. | |
1642 | // However, that code assumes that the body was expected and body control | |
1643 | // structures were initialized. This is not the case when there is no body | |
1644 | // or the body is known to be empty, because the virgin message will lack a | |
1645 | // body_pipe. So we handle preview of null-body and zero-size bodies here. | |
26cc52cb | 1646 | void Adaptation::Icap::ModXact::finishNullOrEmptyBodyPreview(MemBuf &buf) |
5f8252d2 | 1647 | { |
1648 | Must(!virginBodyWriting.active()); // one reason we handle it here | |
1649 | Must(!virgin.body_pipe); // another reason we handle it here | |
1650 | Must(!preview.ad()); | |
1651 | ||
1652 | // do not add last-chunk because our Encapsulated header says null-body | |
bb790702 | 1653 | // addLastRequestChunk(buf); |
5f8252d2 | 1654 | preview.wrote(0, true); |
1655 | ||
1656 | Must(preview.done()); | |
1657 | Must(preview.ieof()); | |
1658 | } | |
1659 | ||
26cc52cb | 1660 | void Adaptation::Icap::ModXact::fillPendingStatus(MemBuf &buf) const |
774c051c | 1661 | { |
26cc52cb | 1662 | Adaptation::Icap::Xaction::fillPendingStatus(buf); |
c99de607 | 1663 | |
774c051c | 1664 | if (state.serviceWaiting) |
1665 | buf.append("U", 1); | |
1666 | ||
5f8252d2 | 1667 | if (virgin.body_pipe != NULL) |
c99de607 | 1668 | buf.append("R", 1); |
1669 | ||
aed188fd | 1670 | if (haveConnection() && !doneReading()) |
c99de607 | 1671 | buf.append("r", 1); |
1672 | ||
774c051c | 1673 | if (!state.doneWriting() && state.writing != State::writingInit) |
1674 | buf.Printf("w(%d)", state.writing); | |
1675 | ||
1676 | if (preview.enabled()) { | |
1677 | if (!preview.done()) | |
1dd6edf2 | 1678 | buf.Printf("P(%d)", (int) preview.debt()); |
774c051c | 1679 | } |
1680 | ||
5f8252d2 | 1681 | if (virginBodySending.active()) |
774c051c | 1682 | buf.append("B", 1); |
1683 | ||
1684 | if (!state.doneParsing() && state.parsing != State::psIcapHeader) | |
1685 | buf.Printf("p(%d)", state.parsing); | |
1686 | ||
1687 | if (!doneSending() && state.sending != State::sendingUndecided) | |
1688 | buf.Printf("S(%d)", state.sending); | |
478cfe99 | 1689 | |
83c51da9 CT |
1690 | if (state.readyForUob) |
1691 | buf.append("6", 1); | |
1692 | ||
478cfe99 | 1693 | if (canStartBypass) |
9e008dda | 1694 | buf.append("Y", 1); |
a22e6cd3 AR |
1695 | |
1696 | if (protectGroupBypass) | |
1697 | buf.append("G", 1); | |
774c051c | 1698 | } |
1699 | ||
26cc52cb | 1700 | void Adaptation::Icap::ModXact::fillDoneStatus(MemBuf &buf) const |
774c051c | 1701 | { |
26cc52cb | 1702 | Adaptation::Icap::Xaction::fillDoneStatus(buf); |
c99de607 | 1703 | |
5f8252d2 | 1704 | if (!virgin.body_pipe) |
774c051c | 1705 | buf.append("R", 1); |
1706 | ||
1707 | if (state.doneWriting()) | |
1708 | buf.append("w", 1); | |
1709 | ||
1710 | if (preview.enabled()) { | |
1711 | if (preview.done()) | |
1712 | buf.Printf("P%s", preview.ieof() ? "(ieof)" : ""); | |
1713 | } | |
1714 | ||
1715 | if (doneReading()) | |
1716 | buf.append("r", 1); | |
1717 | ||
1718 | if (state.doneParsing()) | |
1719 | buf.append("p", 1); | |
1720 | ||
1721 | if (doneSending()) | |
1722 | buf.append("S", 1); | |
1723 | } | |
1724 | ||
26cc52cb | 1725 | bool Adaptation::Icap::ModXact::gotEncapsulated(const char *section) const |
774c051c | 1726 | { |
a9925b40 | 1727 | return icapReply->header.getByNameListMember("Encapsulated", |
1728 | section, ',').size() > 0; | |
774c051c | 1729 | } |
1730 | ||
1731 | // calculate whether there is a virgin HTTP body and | |
1732 | // whether its expected size is known | |
5f8252d2 | 1733 | // TODO: rename because we do not just estimate |
26cc52cb | 1734 | void Adaptation::Icap::ModXact::estimateVirginBody() |
774c051c | 1735 | { |
5f8252d2 | 1736 | // note: lack of size info may disable previews and 204s |
774c051c | 1737 | |
5f8252d2 | 1738 | HttpMsg *msg = virgin.header; |
1739 | Must(msg); | |
774c051c | 1740 | |
60745f24 | 1741 | HttpRequestMethod method; |
774c051c | 1742 | |
5f8252d2 | 1743 | if (virgin.cause) |
1744 | method = virgin.cause->method; | |
e1381638 AJ |
1745 | else if (HttpRequest *req = dynamic_cast<HttpRequest*>(msg)) |
1746 | method = req->method; | |
774c051c | 1747 | else |
e1381638 | 1748 | method = METHOD_NONE; |
774c051c | 1749 | |
47f6e231 | 1750 | int64_t size; |
5f8252d2 | 1751 | // expectingBody returns true for zero-sized bodies, but we will not |
1752 | // get a pipe for that body, so we treat the message as bodyless | |
1753 | if (method != METHOD_NONE && msg->expectingBody(method, size) && size) { | |
192378eb | 1754 | debugs(93, 6, HERE << "expects virgin body from " << |
9e008dda | 1755 | virgin.body_pipe << "; size: " << size); |
5f8252d2 | 1756 | |
1757 | virginBody.expect(size); | |
1758 | virginBodyWriting.plan(); | |
1759 | ||
1760 | // sign up as a body consumer | |
1761 | Must(msg->body_pipe != NULL); | |
1762 | Must(msg->body_pipe == virgin.body_pipe); | |
1763 | Must(virgin.body_pipe->setConsumerIfNotLate(this)); | |
1764 | ||
1765 | // make sure TheBackupLimit is in-sync with the buffer size | |
9c175897 | 1766 | Must(TheBackupLimit <= static_cast<size_t>(msg->body_pipe->buf().max_capacity)); |
774c051c | 1767 | } else { |
192378eb | 1768 | debugs(93, 6, HERE << "does not expect virgin body"); |
5f8252d2 | 1769 | Must(msg->body_pipe == NULL); |
1770 | checkConsuming(); | |
774c051c | 1771 | } |
1772 | } | |
1773 | ||
26cc52cb | 1774 | void Adaptation::Icap::ModXact::makeAdaptedBodyPipe(const char *what) |
9e008dda | 1775 | { |
5f8252d2 | 1776 | Must(!adapted.body_pipe); |
1777 | Must(!adapted.header->body_pipe); | |
1778 | adapted.header->body_pipe = new BodyPipe(this); | |
1779 | adapted.body_pipe = adapted.header->body_pipe; | |
1780 | debugs(93, 7, HERE << "will supply " << what << " via " << | |
9e008dda | 1781 | adapted.body_pipe << " pipe"); |
5f8252d2 | 1782 | } |
1783 | ||
26cc52cb | 1784 | // TODO: Move SizedEstimate and Preview elsewhere |
774c051c | 1785 | |
26cc52cb | 1786 | Adaptation::Icap::SizedEstimate::SizedEstimate() |
774c051c | 1787 | : theData(dtUnexpected) |
1788 | {} | |
1789 | ||
26cc52cb | 1790 | void Adaptation::Icap::SizedEstimate::expect(int64_t aSize) |
774c051c | 1791 | { |
47f6e231 | 1792 | theData = (aSize >= 0) ? aSize : (int64_t)dtUnknown; |
774c051c | 1793 | } |
1794 | ||
26cc52cb | 1795 | bool Adaptation::Icap::SizedEstimate::expected() const |
774c051c | 1796 | { |
1797 | return theData != dtUnexpected; | |
1798 | } | |
1799 | ||
26cc52cb | 1800 | bool Adaptation::Icap::SizedEstimate::knownSize() const |
774c051c | 1801 | { |
1802 | Must(expected()); | |
1803 | return theData != dtUnknown; | |
1804 | } | |
1805 | ||
26cc52cb | 1806 | uint64_t Adaptation::Icap::SizedEstimate::size() const |
774c051c | 1807 | { |
1808 | Must(knownSize()); | |
47f6e231 | 1809 | return static_cast<uint64_t>(theData); |
774c051c | 1810 | } |
1811 | ||
26cc52cb | 1812 | Adaptation::Icap::VirginBodyAct::VirginBodyAct(): theStart(0), theState(stUndecided) |
774c051c | 1813 | {} |
1814 | ||
26cc52cb | 1815 | void Adaptation::Icap::VirginBodyAct::plan() |
774c051c | 1816 | { |
478cfe99 | 1817 | Must(!disabled()); |
1818 | Must(!theStart); // not started | |
1819 | theState = stActive; | |
774c051c | 1820 | } |
1821 | ||
26cc52cb | 1822 | void Adaptation::Icap::VirginBodyAct::disable() |
774c051c | 1823 | { |
478cfe99 | 1824 | theState = stDisabled; |
774c051c | 1825 | } |
1826 | ||
26cc52cb | 1827 | void Adaptation::Icap::VirginBodyAct::progress(size_t size) |
774c051c | 1828 | { |
1829 | Must(active()); | |
46e172d9 FC |
1830 | #if SIZEOF_SIZE_T > 4 |
1831 | /* always true for smaller size_t's */ | |
d897ae49 | 1832 | Must(static_cast<int64_t>(size) >= 0); |
46e172d9 | 1833 | #endif |
47f6e231 | 1834 | theStart += static_cast<int64_t>(size); |
774c051c | 1835 | } |
1836 | ||
26cc52cb | 1837 | uint64_t Adaptation::Icap::VirginBodyAct::offset() const |
774c051c | 1838 | { |
1839 | Must(active()); | |
47f6e231 | 1840 | return static_cast<uint64_t>(theStart); |
774c051c | 1841 | } |
1842 | ||
26cc52cb | 1843 | Adaptation::Icap::Preview::Preview(): theWritten(0), theAd(0), theState(stDisabled) |
774c051c | 1844 | {} |
1845 | ||
26cc52cb | 1846 | void Adaptation::Icap::Preview::enable(size_t anAd) |
774c051c | 1847 | { |
1848 | // TODO: check for anAd not exceeding preview size limit | |
774c051c | 1849 | Must(!enabled()); |
1850 | theAd = anAd; | |
1851 | theState = stWriting; | |
1852 | } | |
1853 | ||
26cc52cb | 1854 | bool Adaptation::Icap::Preview::enabled() const |
774c051c | 1855 | { |
1856 | return theState != stDisabled; | |
1857 | } | |
1858 | ||
26cc52cb | 1859 | size_t Adaptation::Icap::Preview::ad() const |
774c051c | 1860 | { |
1861 | Must(enabled()); | |
1862 | return theAd; | |
1863 | } | |
1864 | ||
26cc52cb | 1865 | bool Adaptation::Icap::Preview::done() const |
774c051c | 1866 | { |
1867 | Must(enabled()); | |
1868 | return theState >= stIeof; | |
1869 | } | |
1870 | ||
26cc52cb | 1871 | bool Adaptation::Icap::Preview::ieof() const |
774c051c | 1872 | { |
1873 | Must(enabled()); | |
1874 | return theState == stIeof; | |
1875 | } | |
1876 | ||
26cc52cb | 1877 | size_t Adaptation::Icap::Preview::debt() const |
774c051c | 1878 | { |
1879 | Must(enabled()); | |
1880 | return done() ? 0 : (theAd - theWritten); | |
1881 | } | |
1882 | ||
26cc52cb | 1883 | void Adaptation::Icap::Preview::wrote(size_t size, bool wroteEof) |
774c051c | 1884 | { |
1885 | Must(enabled()); | |
5f8252d2 | 1886 | |
774c051c | 1887 | theWritten += size; |
1888 | ||
9e008dda | 1889 | Must(theWritten <= theAd); |
5f8252d2 | 1890 | |
9e008dda AJ |
1891 | if (wroteEof) |
1892 | theState = stIeof; // written size is irrelevant | |
e1381638 AJ |
1893 | else if (theWritten >= theAd) |
1894 | theState = stDone; | |
774c051c | 1895 | } |
1896 | ||
26cc52cb | 1897 | bool Adaptation::Icap::ModXact::fillVirginHttpHeader(MemBuf &mb) const |
3cfc19b3 | 1898 | { |
5f8252d2 | 1899 | if (virgin.header == NULL) |
3cfc19b3 | 1900 | return false; |
1901 | ||
5f8252d2 | 1902 | virgin.header->firstLineBuf(mb); |
3cfc19b3 | 1903 | |
1904 | return true; | |
1905 | } | |
c824c43b | 1906 | |
64b66b76 CT |
1907 | void Adaptation::Icap::ModXact::detailError(int errDetail) |
1908 | { | |
e9fa5493 AR |
1909 | HttpRequest *request = dynamic_cast<HttpRequest*>(adapted.header); |
1910 | // if no adapted request, update virgin (and inherit its properties later) | |
1911 | // TODO: make this and HttpRequest::detailError constant, like adaptHistory | |
1912 | if (!request) | |
1913 | request = const_cast<HttpRequest*>(&virginRequest()); | |
1914 | ||
1915 | if (request) | |
64b66b76 | 1916 | request->detailError(ERR_ICAP_FAILURE, errDetail); |
64b66b76 | 1917 | } |
c824c43b | 1918 | |
129fe2a1 CT |
1919 | void Adaptation::Icap::ModXact::clearError() |
1920 | { | |
87f237a9 | 1921 | HttpRequest *request = dynamic_cast<HttpRequest*>(adapted.header); |
129fe2a1 CT |
1922 | // if no adapted request, update virgin (and inherit its properties later) |
1923 | if (!request) | |
1924 | request = const_cast<HttpRequest*>(&virginRequest()); | |
1925 | ||
1926 | if (request) | |
1927 | request->clearError(); | |
1928 | } | |
1929 | ||
26cc52cb | 1930 | /* Adaptation::Icap::ModXactLauncher */ |
c824c43b | 1931 | |
4299f876 | 1932 | Adaptation::Icap::ModXactLauncher::ModXactLauncher(HttpMsg *virginHeader, HttpRequest *virginCause, Adaptation::ServicePointer aService): |
26cc52cb | 1933 | AsyncJob("Adaptation::Icap::ModXactLauncher"), |
4299f876 | 1934 | Adaptation::Icap::Launcher("Adaptation::Icap::ModXactLauncher", aService) |
c824c43b | 1935 | { |
1936 | virgin.setHeader(virginHeader); | |
1937 | virgin.setCause(virginCause); | |
3ff65596 | 1938 | updateHistory(true); |
c824c43b | 1939 | } |
1940 | ||
26cc52cb | 1941 | Adaptation::Icap::Xaction *Adaptation::Icap::ModXactLauncher::createXaction() |
c824c43b | 1942 | { |
26cc52cb AR |
1943 | Adaptation::Icap::ServiceRep::Pointer s = |
1944 | dynamic_cast<Adaptation::Icap::ServiceRep*>(theService.getRaw()); | |
0bef8dd7 | 1945 | Must(s != NULL); |
4299f876 | 1946 | return new Adaptation::Icap::ModXact(virgin.header, virgin.cause, s); |
c824c43b | 1947 | } |
3ff65596 | 1948 | |
e1381638 AJ |
1949 | void Adaptation::Icap::ModXactLauncher::swanSong() |
1950 | { | |
3ff65596 AR |
1951 | debugs(93, 5, HERE << "swan sings"); |
1952 | updateHistory(false); | |
1953 | Adaptation::Icap::Launcher::swanSong(); | |
1954 | } | |
1955 | ||
b0365bd9 | 1956 | void Adaptation::Icap::ModXactLauncher::updateHistory(bool doStart) |
e1381638 AJ |
1957 | { |
1958 | HttpRequest *r = virgin.cause ? | |
1959 | virgin.cause : dynamic_cast<HttpRequest*>(virgin.header); | |
1960 | ||
1961 | // r should never be NULL but we play safe; TODO: add Should() | |
1962 | if (r) { | |
1963 | Adaptation::Icap::History::Pointer h = r->icapHistory(); | |
1964 | if (h != NULL) { | |
b0365bd9 | 1965 | if (doStart) |
e1381638 AJ |
1966 | h->start("ICAPModXactLauncher"); |
1967 | else | |
1968 | h->stop("ICAPModXactLauncher"); | |
1969 | } | |
1970 | } | |
3ff65596 | 1971 | } |