]>
Commit | Line | Data |
---|---|---|
774c051c | 1 | /* |
507d0a78 | 2 | * DEBUG: section 93 ICAP (RFC 3507) Client |
774c051c | 3 | */ |
4 | ||
5 | #include "squid.h" | |
6 | #include "comm.h" | |
5f8252d2 | 7 | #include "HttpMsg.h" |
774c051c | 8 | #include "HttpRequest.h" |
9 | #include "HttpReply.h" | |
0bef8dd7 | 10 | #include "adaptation/Initiator.h" |
774c051c | 11 | #include "ICAPServiceRep.h" |
c824c43b | 12 | #include "ICAPLauncher.h" |
774c051c | 13 | #include "ICAPModXact.h" |
14 | #include "ICAPClient.h" | |
15 | #include "ChunkedCodingParser.h" | |
16 | #include "TextException.h" | |
a97e82a8 | 17 | #include "AuthUserRequest.h" |
12b91c99 | 18 | #include "ICAPConfig.h" |
985c86bc | 19 | #include "SquidTime.h" |
774c051c | 20 | |
21 | // flow and terminology: | |
22 | // HTTP| --> receive --> encode --> write --> |network | |
23 | // end | <-- send <-- parse <-- read <-- |end | |
24 | ||
774c051c | 25 | // TODO: replace gotEncapsulated() with something faster; we call it often |
26 | ||
27 | CBDATA_CLASS_INIT(ICAPModXact); | |
c824c43b | 28 | CBDATA_CLASS_INIT(ICAPModXactLauncher); |
774c051c | 29 | |
5f8252d2 | 30 | static const size_t TheBackupLimit = BodyPipe::MaxCapacity; |
774c051c | 31 | |
12b91c99 | 32 | extern ICAPConfig TheICAPConfig; |
33 | ||
774c051c | 34 | |
35 | ICAPModXact::State::State() | |
36 | { | |
09bfe95f | 37 | memset(this, 0, sizeof(*this)); |
774c051c | 38 | } |
39 | ||
0bef8dd7 | 40 | ICAPModXact::ICAPModXact(Adaptation::Initiator *anInitiator, HttpMsg *virginHeader, |
9e008dda AJ |
41 | HttpRequest *virginCause, ICAPServiceRep::Pointer &aService): |
42 | AsyncJob("ICAPModXact"), | |
43 | ICAPXaction("ICAPModXact", anInitiator, aService), | |
44 | icapReply(NULL), | |
45 | virginConsumed(0), | |
46 | bodyParser(NULL), | |
47 | canStartBypass(false) // too early | |
774c051c | 48 | { |
5f8252d2 | 49 | assert(virginHeader); |
774c051c | 50 | |
5f8252d2 | 51 | virgin.setHeader(virginHeader); // sets virgin.body_pipe if needed |
52 | virgin.setCause(virginCause); // may be NULL | |
774c051c | 53 | |
5f8252d2 | 54 | // adapted header and body are initialized when we parse them |
774c051c | 55 | |
56 | // writing and reading ends are handled by ICAPXaction | |
57 | ||
58 | // encoding | |
59 | // nothing to do because we are using temporary buffers | |
60 | ||
61 | // parsing | |
62 | icapReply = new HttpReply; | |
63 | icapReply->protoPrefix = "ICAP/"; // TODO: make an IcapReply class? | |
64 | ||
5f8252d2 | 65 | debugs(93,7, "ICAPModXact initialized." << status()); |
774c051c | 66 | } |
67 | ||
5f8252d2 | 68 | // initiator wants us to start |
69 | void ICAPModXact::start() | |
774c051c | 70 | { |
5f8252d2 | 71 | ICAPXaction::start(); |
774c051c | 72 | |
73 | estimateVirginBody(); // before virgin disappears! | |
74 | ||
0bef8dd7 | 75 | canStartBypass = service().cfg().bypass; |
478cfe99 | 76 | |
774c051c | 77 | // it is an ICAP violation to send request to a service w/o known OPTIONS |
78 | ||
79 | if (service().up()) | |
80 | startWriting(); | |
81 | else | |
82 | waitForService(); | |
774c051c | 83 | } |
84 | ||
85 | void ICAPModXact::waitForService() | |
86 | { | |
87 | Must(!state.serviceWaiting); | |
5f8252d2 | 88 | debugs(93, 7, "ICAPModXact will wait for the ICAP service" << status()); |
774c051c | 89 | state.serviceWaiting = true; |
bd7f2ede | 90 | AsyncCall::Pointer call = asyncCall(93,5, "ICAPModXact::noteServiceReady", |
9e008dda | 91 | MemFun(this, &ICAPModXact::noteServiceReady)); |
bd7f2ede | 92 | service().callWhenReady(call); |
774c051c | 93 | } |
94 | ||
95 | void ICAPModXact::noteServiceReady() | |
96 | { | |
774c051c | 97 | Must(state.serviceWaiting); |
98 | state.serviceWaiting = false; | |
c99de607 | 99 | |
c824c43b | 100 | if (service().up()) { |
101 | startWriting(); | |
102 | } else { | |
103 | disableRetries(); | |
478cfe99 | 104 | throw TexcHere("ICAP service is unusable"); |
c824c43b | 105 | } |
774c051c | 106 | } |
107 | ||
108 | void ICAPModXact::startWriting() | |
109 | { | |
774c051c | 110 | state.writing = State::writingConnect; |
c824c43b | 111 | |
112 | decideOnPreview(); // must be decided before we decideOnRetries | |
113 | decideOnRetries(); | |
114 | ||
774c051c | 115 | openConnection(); |
774c051c | 116 | } |
117 | ||
118 | // connection with the ICAP service established | |
119 | void ICAPModXact::handleCommConnected() | |
120 | { | |
121 | Must(state.writing == State::writingConnect); | |
122 | ||
123 | startReading(); // wait for early errors from the ICAP server | |
124 | ||
125 | MemBuf requestBuf; | |
126 | requestBuf.init(); | |
127 | ||
128 | makeRequestHeaders(requestBuf); | |
5f8252d2 | 129 | debugs(93, 9, "ICAPModXact ICAP will write" << status() << ":\n" << |
774c051c | 130 | (requestBuf.terminate(), requestBuf.content())); |
131 | ||
132 | // write headers | |
133 | state.writing = State::writingHeaders; | |
134 | scheduleWrite(requestBuf); | |
135 | } | |
136 | ||
b107a5a5 | 137 | void ICAPModXact::handleCommWrote(size_t sz) |
774c051c | 138 | { |
b107a5a5 | 139 | debugs(93, 5, HERE << "Wrote " << sz << " bytes"); |
140 | ||
774c051c | 141 | if (state.writing == State::writingHeaders) |
142 | handleCommWroteHeaders(); | |
143 | else | |
144 | handleCommWroteBody(); | |
145 | } | |
146 | ||
147 | void ICAPModXact::handleCommWroteHeaders() | |
148 | { | |
149 | Must(state.writing == State::writingHeaders); | |
150 | ||
5f8252d2 | 151 | // determine next step |
152 | if (preview.enabled()) | |
153 | state.writing = preview.done() ? State::writingPaused : State::writingPreview; | |
154 | else | |
9e008dda AJ |
155 | if (virginBody.expected()) |
156 | state.writing = State::writingPrime; | |
157 | else { | |
158 | stopWriting(true); | |
159 | return; | |
160 | } | |
5f8252d2 | 161 | |
162 | writeMore(); | |
774c051c | 163 | } |
164 | ||
165 | void ICAPModXact::writeMore() | |
166 | { | |
5f8252d2 | 167 | debugs(93, 5, HERE << "checking whether to write more" << status()); |
168 | ||
bd7f2ede | 169 | if (writer != NULL) // already writing something |
774c051c | 170 | return; |
171 | ||
172 | switch (state.writing) { | |
173 | ||
174 | case State::writingInit: // waiting for service OPTIONS | |
175 | Must(state.serviceWaiting); | |
176 | ||
177 | case State::writingConnect: // waiting for the connection to establish | |
178 | ||
179 | case State::writingHeaders: // waiting for the headers to be written | |
180 | ||
181 | case State::writingPaused: // waiting for the ICAP server response | |
182 | ||
c99de607 | 183 | case State::writingReallyDone: // nothing more to write |
184 | return; | |
185 | ||
186 | case State::writingAlmostDone: // was waiting for the last write | |
187 | stopWriting(false); | |
774c051c | 188 | return; |
189 | ||
190 | case State::writingPreview: | |
5f8252d2 | 191 | writePreviewBody(); |
774c051c | 192 | return; |
193 | ||
194 | case State::writingPrime: | |
195 | writePrimeBody(); | |
196 | return; | |
197 | ||
198 | default: | |
199 | throw TexcHere("ICAPModXact in bad writing state"); | |
200 | } | |
201 | } | |
202 | ||
5f8252d2 | 203 | void ICAPModXact::writePreviewBody() |
774c051c | 204 | { |
5f8252d2 | 205 | debugs(93, 8, HERE << "will write Preview body from " << |
9e008dda | 206 | virgin.body_pipe << status()); |
774c051c | 207 | Must(state.writing == State::writingPreview); |
5f8252d2 | 208 | Must(virgin.body_pipe != NULL); |
774c051c | 209 | |
5f8252d2 | 210 | const size_t sizeMax = (size_t)virgin.body_pipe->buf().contentSize(); |
211 | const size_t size = XMIN(preview.debt(), sizeMax); | |
774c051c | 212 | writeSomeBody("preview body", size); |
213 | ||
214 | // change state once preview is written | |
215 | ||
216 | if (preview.done()) { | |
5f8252d2 | 217 | debugs(93, 7, "ICAPModXact wrote entire Preview body" << status()); |
774c051c | 218 | |
219 | if (preview.ieof()) | |
c99de607 | 220 | stopWriting(true); |
774c051c | 221 | else |
222 | state.writing = State::writingPaused; | |
223 | } | |
224 | } | |
225 | ||
226 | void ICAPModXact::writePrimeBody() | |
227 | { | |
228 | Must(state.writing == State::writingPrime); | |
5f8252d2 | 229 | Must(virginBodyWriting.active()); |
774c051c | 230 | |
5f8252d2 | 231 | const size_t size = (size_t)virgin.body_pipe->buf().contentSize(); |
774c051c | 232 | writeSomeBody("prime virgin body", size); |
233 | ||
5f8252d2 | 234 | if (virginBodyEndReached(virginBodyWriting)) { |
235 | debugs(93, 5, HERE << "wrote entire body"); | |
c99de607 | 236 | stopWriting(true); |
b107a5a5 | 237 | } |
774c051c | 238 | } |
239 | ||
240 | void ICAPModXact::writeSomeBody(const char *label, size_t size) | |
241 | { | |
c99de607 | 242 | Must(!writer && state.writing < state.writingAlmostDone); |
5f8252d2 | 243 | Must(virgin.body_pipe != NULL); |
12f4b710 | 244 | debugs(93, 8, HERE << "will write up to " << size << " bytes of " << |
774c051c | 245 | label); |
246 | ||
247 | MemBuf writeBuf; // TODO: suggest a min size based on size and lastChunk | |
248 | ||
249 | writeBuf.init(); // note: we assume that last-chunk will fit | |
250 | ||
5f8252d2 | 251 | const size_t writableSize = virginContentSize(virginBodyWriting); |
c99de607 | 252 | const size_t chunkSize = XMIN(writableSize, size); |
774c051c | 253 | |
254 | if (chunkSize) { | |
12f4b710 | 255 | debugs(93, 7, HERE << "will write " << chunkSize << |
774c051c | 256 | "-byte chunk of " << label); |
5f8252d2 | 257 | |
258 | openChunk(writeBuf, chunkSize, false); | |
259 | writeBuf.append(virginContentData(virginBodyWriting), chunkSize); | |
260 | closeChunk(writeBuf); | |
261 | ||
262 | virginBodyWriting.progress(chunkSize); | |
263 | virginConsume(); | |
774c051c | 264 | } else { |
c99de607 | 265 | debugs(93, 7, "ICAPModXact has no writable " << label << " content"); |
774c051c | 266 | } |
267 | ||
5f8252d2 | 268 | const bool wroteEof = virginBodyEndReached(virginBodyWriting); |
269 | bool lastChunk = wroteEof; | |
270 | if (state.writing == State::writingPreview) { | |
271 | preview.wrote(chunkSize, wroteEof); // even if wrote nothing | |
272 | lastChunk = lastChunk || preview.done(); | |
273 | } | |
774c051c | 274 | |
5f8252d2 | 275 | if (lastChunk) { |
12f4b710 | 276 | debugs(93, 8, HERE << "will write last-chunk of " << label); |
774c051c | 277 | addLastRequestChunk(writeBuf); |
278 | } | |
279 | ||
12f4b710 | 280 | debugs(93, 7, HERE << "will write " << writeBuf.contentSize() |
774c051c | 281 | << " raw bytes of " << label); |
282 | ||
283 | if (writeBuf.hasContent()) { | |
284 | scheduleWrite(writeBuf); // comm will free the chunk | |
285 | } else { | |
286 | writeBuf.clean(); | |
287 | } | |
288 | } | |
289 | ||
774c051c | 290 | void ICAPModXact::addLastRequestChunk(MemBuf &buf) |
291 | { | |
c99de607 | 292 | const bool ieof = state.writing == State::writingPreview && preview.ieof(); |
293 | openChunk(buf, 0, ieof); | |
294 | closeChunk(buf); | |
774c051c | 295 | } |
296 | ||
c99de607 | 297 | void ICAPModXact::openChunk(MemBuf &buf, size_t chunkSize, bool ieof) |
774c051c | 298 | { |
c99de607 | 299 | buf.Printf((ieof ? "%x; ieof\r\n" : "%x\r\n"), (int) chunkSize); |
774c051c | 300 | } |
301 | ||
c99de607 | 302 | void ICAPModXact::closeChunk(MemBuf &buf) |
774c051c | 303 | { |
774c051c | 304 | buf.append(ICAP::crlf, 2); // chunk-terminating CRLF |
305 | } | |
306 | ||
5f8252d2 | 307 | // did the activity reached the end of the virgin body? |
308 | bool ICAPModXact::virginBodyEndReached(const VirginBodyAct &act) const | |
309 | { | |
9e008dda | 310 | return |
5f8252d2 | 311 | !act.active() || // did all (assuming it was originally planned) |
312 | !virgin.body_pipe->expectMoreAfter(act.offset()); // wont have more | |
313 | } | |
314 | ||
315 | // the size of buffered virgin body data available for the specified activity | |
316 | // if this size is zero, we may be done or may be waiting for more data | |
317 | size_t ICAPModXact::virginContentSize(const VirginBodyAct &act) const | |
774c051c | 318 | { |
5f8252d2 | 319 | Must(act.active()); |
320 | // asbolute start of unprocessed data | |
47f6e231 | 321 | const uint64_t start = act.offset(); |
5f8252d2 | 322 | // absolute end of buffered data |
47f6e231 | 323 | const uint64_t end = virginConsumed + virgin.body_pipe->buf().contentSize(); |
774c051c | 324 | Must(virginConsumed <= start && start <= end); |
47f6e231 | 325 | return static_cast<size_t>(end - start); |
774c051c | 326 | } |
327 | ||
5f8252d2 | 328 | // pointer to buffered virgin body data available for the specified activity |
329 | const char *ICAPModXact::virginContentData(const VirginBodyAct &act) const | |
774c051c | 330 | { |
5f8252d2 | 331 | Must(act.active()); |
47f6e231 | 332 | const uint64_t start = act.offset(); |
774c051c | 333 | Must(virginConsumed <= start); |
47f6e231 | 334 | return virgin.body_pipe->buf().content() + static_cast<size_t>(start-virginConsumed); |
774c051c | 335 | } |
336 | ||
337 | void ICAPModXact::virginConsume() | |
338 | { | |
478cfe99 | 339 | debugs(93, 9, "consumption guards: " << !virgin.body_pipe << isRetriable); |
340 | ||
5f8252d2 | 341 | if (!virgin.body_pipe) |
c824c43b | 342 | return; // nothing to consume |
343 | ||
344 | if (isRetriable) | |
345 | return; // do not consume if we may have to retry later | |
5f8252d2 | 346 | |
347 | BodyPipe &bp = *virgin.body_pipe; | |
478cfe99 | 348 | |
349 | // Why > 2? HttpState does not use the last bytes in the buffer | |
9e008dda | 350 | // because delayAwareRead() is arguably broken. See |
478cfe99 | 351 | // HttpStateData::maybeReadVirginBody for more details. |
352 | if (canStartBypass && bp.buf().spaceSize() > 2) { | |
353 | // Postponing may increase memory footprint and slow the HTTP side | |
9e008dda | 354 | // down. Not postponing may increase the number of ICAP errors |
478cfe99 | 355 | // if the ICAP service fails. We may also use "potential" space to |
356 | // postpone more aggressively. Should the trade-off be configurable? | |
357 | debugs(93, 8, HERE << "postponing consumption from " << bp.status()); | |
358 | return; | |
359 | } | |
360 | ||
5f8252d2 | 361 | const size_t have = static_cast<size_t>(bp.buf().contentSize()); |
47f6e231 | 362 | const uint64_t end = virginConsumed + have; |
363 | uint64_t offset = end; | |
774c051c | 364 | |
478cfe99 | 365 | debugs(93, 9, HERE << "max virgin consumption offset=" << offset << |
9e008dda AJ |
366 | " acts " << virginBodyWriting.active() << virginBodySending.active() << |
367 | " consumed=" << virginConsumed << | |
368 | " from " << virgin.body_pipe->status()); | |
478cfe99 | 369 | |
5f8252d2 | 370 | if (virginBodyWriting.active()) |
371 | offset = XMIN(virginBodyWriting.offset(), offset); | |
774c051c | 372 | |
5f8252d2 | 373 | if (virginBodySending.active()) |
374 | offset = XMIN(virginBodySending.offset(), offset); | |
774c051c | 375 | |
376 | Must(virginConsumed <= offset && offset <= end); | |
377 | ||
47f6e231 | 378 | if (const size_t size = static_cast<size_t>(offset - virginConsumed)) { |
b107a5a5 | 379 | debugs(93, 8, HERE << "consuming " << size << " out of " << have << |
774c051c | 380 | " virgin body bytes"); |
5f8252d2 | 381 | bp.consume(size); |
774c051c | 382 | virginConsumed += size; |
c824c43b | 383 | Must(!isRetriable); // or we should not be consuming |
478cfe99 | 384 | disableBypass("consumed content"); |
774c051c | 385 | } |
386 | } | |
387 | ||
388 | void ICAPModXact::handleCommWroteBody() | |
389 | { | |
390 | writeMore(); | |
391 | } | |
392 | ||
c99de607 | 393 | // Called when we do not expect to call comm_write anymore. |
394 | // We may have a pending write though. | |
395 | // If stopping nicely, we will just wait for that pending write, if any. | |
396 | void ICAPModXact::stopWriting(bool nicely) | |
774c051c | 397 | { |
c99de607 | 398 | if (state.writing == State::writingReallyDone) |
774c051c | 399 | return; |
400 | ||
bd7f2ede | 401 | if (writer != NULL) { |
c99de607 | 402 | if (nicely) { |
5f8252d2 | 403 | debugs(93, 7, HERE << "will wait for the last write" << status()); |
c99de607 | 404 | state.writing = State::writingAlmostDone; // may already be set |
5f8252d2 | 405 | checkConsuming(); |
c99de607 | 406 | return; |
407 | } | |
4932ad93 | 408 | debugs(93, 3, HERE << "will NOT wait for the last write" << status()); |
774c051c | 409 | |
c99de607 | 410 | // Comm does not have an interface to clear the writer callback nicely, |
411 | // but without clearing the writer we cannot recycle the connection. | |
412 | // We prevent connection reuse and hope that we can handle a callback | |
5f8252d2 | 413 | // call at any time, usually in the middle of the destruction sequence! |
414 | // Somebody should add comm_remove_write_handler() to comm API. | |
c99de607 | 415 | reuseConnection = false; |
478cfe99 | 416 | ignoreLastWrite = true; |
c99de607 | 417 | } |
418 | ||
5f8252d2 | 419 | debugs(93, 7, HERE << "will no longer write" << status()); |
5f8252d2 | 420 | if (virginBodyWriting.active()) { |
421 | virginBodyWriting.disable(); | |
422 | virginConsume(); | |
423 | } | |
478cfe99 | 424 | state.writing = State::writingReallyDone; |
425 | checkConsuming(); | |
774c051c | 426 | } |
427 | ||
428 | void ICAPModXact::stopBackup() | |
429 | { | |
5f8252d2 | 430 | if (!virginBodySending.active()) |
774c051c | 431 | return; |
432 | ||
5f8252d2 | 433 | debugs(93, 7, "ICAPModXact will no longer backup" << status()); |
434 | virginBodySending.disable(); | |
774c051c | 435 | virginConsume(); |
436 | } | |
437 | ||
438 | bool ICAPModXact::doneAll() const | |
439 | { | |
440 | return ICAPXaction::doneAll() && !state.serviceWaiting && | |
5f8252d2 | 441 | doneSending() && |
774c051c | 442 | doneReading() && state.doneWriting(); |
443 | } | |
444 | ||
445 | void ICAPModXact::startReading() | |
446 | { | |
447 | Must(connection >= 0); | |
448 | Must(!reader); | |
5f8252d2 | 449 | Must(!adapted.header); |
450 | Must(!adapted.body_pipe); | |
774c051c | 451 | |
452 | // we use the same buffer for headers and body and then consume headers | |
453 | readMore(); | |
454 | } | |
455 | ||
456 | void ICAPModXact::readMore() | |
457 | { | |
bd7f2ede | 458 | if (reader != NULL || doneReading()) { |
c99de607 | 459 | debugs(93,3,HERE << "returning from readMore because reader or doneReading()"); |
774c051c | 460 | return; |
3b299123 | 461 | } |
774c051c | 462 | |
463 | // do not fill readBuf if we have no space to store the result | |
5f8252d2 | 464 | if (adapted.body_pipe != NULL && |
9e008dda | 465 | !adapted.body_pipe->buf().hasPotentialSpace()) { |
5f8252d2 | 466 | debugs(93,3,HERE << "not reading because ICAP reply pipe is full"); |
774c051c | 467 | return; |
3b299123 | 468 | } |
774c051c | 469 | |
470 | if (readBuf.hasSpace()) | |
471 | scheduleRead(); | |
3b299123 | 472 | else |
c99de607 | 473 | debugs(93,3,HERE << "nothing to do because !readBuf.hasSpace()"); |
774c051c | 474 | } |
475 | ||
476 | // comm module read a portion of the ICAP response for us | |
477 | void ICAPModXact::handleCommRead(size_t) | |
478 | { | |
479 | Must(!state.doneParsing()); | |
480 | parseMore(); | |
481 | readMore(); | |
482 | } | |
483 | ||
484 | void ICAPModXact::echoMore() | |
485 | { | |
486 | Must(state.sending == State::sendingVirgin); | |
5f8252d2 | 487 | Must(adapted.body_pipe != NULL); |
488 | Must(virginBodySending.active()); | |
489 | ||
490 | const size_t sizeMax = virginContentSize(virginBodySending); | |
491 | debugs(93,5, HERE << "will echo up to " << sizeMax << " bytes from " << | |
9e008dda | 492 | virgin.body_pipe->status()); |
5f8252d2 | 493 | debugs(93,5, HERE << "will echo up to " << sizeMax << " bytes to " << |
9e008dda | 494 | adapted.body_pipe->status()); |
5f8252d2 | 495 | |
496 | if (sizeMax > 0) { | |
497 | const size_t size = adapted.body_pipe->putMoreData(virginContentData(virginBodySending), sizeMax); | |
498 | debugs(93,5, HERE << "echoed " << size << " out of " << sizeMax << | |
9e008dda | 499 | " bytes"); |
5f8252d2 | 500 | virginBodySending.progress(size); |
774c051c | 501 | virginConsume(); |
478cfe99 | 502 | disableBypass("echoed content"); |
774c051c | 503 | } |
504 | ||
5f8252d2 | 505 | if (virginBodyEndReached(virginBodySending)) { |
506 | debugs(93, 5, "ICAPModXact echoed all" << status()); | |
774c051c | 507 | stopSending(true); |
508 | } else { | |
5f8252d2 | 509 | debugs(93, 5, "ICAPModXact has " << |
9e008dda AJ |
510 | virgin.body_pipe->buf().contentSize() << " bytes " << |
511 | "and expects more to echo" << status()); | |
5f8252d2 | 512 | // TODO: timeout if virgin or adapted pipes are broken |
774c051c | 513 | } |
514 | } | |
515 | ||
516 | bool ICAPModXact::doneSending() const | |
517 | { | |
774c051c | 518 | return state.sending == State::sendingDone; |
519 | } | |
520 | ||
478cfe99 | 521 | // stop (or do not start) sending adapted message body |
774c051c | 522 | void ICAPModXact::stopSending(bool nicely) |
523 | { | |
524 | if (doneSending()) | |
525 | return; | |
526 | ||
527 | if (state.sending != State::sendingUndecided) { | |
5f8252d2 | 528 | debugs(93, 7, "ICAPModXact will no longer send" << status()); |
529 | if (adapted.body_pipe != NULL) { | |
530 | virginBodySending.disable(); | |
531 | // we may leave debts if we were echoing and the virgin | |
532 | // body_pipe got exhausted before we echoed all planned bytes | |
533 | const bool leftDebts = adapted.body_pipe->needsMoreData(); | |
534 | stopProducingFor(adapted.body_pipe, nicely && !leftDebts); | |
535 | } | |
774c051c | 536 | } else { |
5f8252d2 | 537 | debugs(93, 7, "ICAPModXact will not start sending" << status()); |
538 | Must(!adapted.body_pipe); | |
774c051c | 539 | } |
540 | ||
541 | state.sending = State::sendingDone; | |
5f8252d2 | 542 | checkConsuming(); |
774c051c | 543 | } |
544 | ||
5f8252d2 | 545 | // should be called after certain state.writing or state.sending changes |
546 | void ICAPModXact::checkConsuming() | |
774c051c | 547 | { |
5f8252d2 | 548 | // quit if we already stopped or are still using the pipe |
549 | if (!virgin.body_pipe || !state.doneConsumingVirgin()) | |
774c051c | 550 | return; |
551 | ||
5f8252d2 | 552 | debugs(93, 7, HERE << "will stop consuming" << status()); |
553 | stopConsumingFrom(virgin.body_pipe); | |
774c051c | 554 | } |
555 | ||
556 | void ICAPModXact::parseMore() | |
557 | { | |
aa761e5f | 558 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " bytes to parse" << |
774c051c | 559 | status()); |
d5cfacfb | 560 | debugs(93, 5, HERE << "\n" << readBuf.content()); |
774c051c | 561 | |
562 | if (state.parsingHeaders()) | |
563 | parseHeaders(); | |
564 | ||
565 | if (state.parsing == State::psBody) | |
566 | parseBody(); | |
567 | } | |
568 | ||
0a8bbeeb | 569 | void ICAPModXact::callException(const std::exception &e) |
478cfe99 | 570 | { |
571 | if (!canStartBypass || isRetriable) { | |
572 | ICAPXaction::callException(e); | |
573 | return; | |
574 | } | |
575 | ||
576 | try { | |
4932ad93 | 577 | debugs(93, 3, "bypassing ICAPModXact::" << inCall << " exception: " << |
9e008dda | 578 | e.what() << ' ' << status()); |
478cfe99 | 579 | bypassFailure(); |
9e008dda | 580 | } catch (const std::exception &bypassE) { |
478cfe99 | 581 | ICAPXaction::callException(bypassE); |
582 | } | |
583 | } | |
584 | ||
585 | void ICAPModXact::bypassFailure() | |
586 | { | |
587 | disableBypass("already started to bypass"); | |
588 | ||
589 | Must(!isRetriable); // or we should not be bypassing | |
590 | ||
591 | prepEchoing(); | |
592 | ||
593 | startSending(); | |
594 | ||
595 | // end all activities associated with the ICAP server | |
596 | ||
597 | stopParsing(); | |
598 | ||
599 | stopWriting(true); // or should we force it? | |
600 | if (connection >= 0) { | |
601 | reuseConnection = false; // be conservative | |
602 | cancelRead(); // may not work; and we cannot stop connecting either | |
603 | if (!doneWithIo()) | |
604 | debugs(93, 7, "Warning: bypass failed to stop I/O" << status()); | |
605 | } | |
606 | } | |
607 | ||
608 | void ICAPModXact::disableBypass(const char *reason) | |
609 | { | |
610 | if (canStartBypass) { | |
611 | debugs(93,7, HERE << "will never start bypass because " << reason); | |
612 | canStartBypass = false; | |
613 | } | |
614 | } | |
615 | ||
616 | ||
617 | ||
774c051c | 618 | // note that allocation for echoing is done in handle204NoContent() |
619 | void ICAPModXact::maybeAllocateHttpMsg() | |
620 | { | |
5f8252d2 | 621 | if (adapted.header) // already allocated |
774c051c | 622 | return; |
623 | ||
624 | if (gotEncapsulated("res-hdr")) { | |
5f8252d2 | 625 | adapted.setHeader(new HttpReply); |
774c051c | 626 | } else if (gotEncapsulated("req-hdr")) { |
5f8252d2 | 627 | adapted.setHeader(new HttpRequest); |
774c051c | 628 | } else |
629 | throw TexcHere("Neither res-hdr nor req-hdr in maybeAllocateHttpMsg()"); | |
630 | } | |
631 | ||
632 | void ICAPModXact::parseHeaders() | |
633 | { | |
634 | Must(state.parsingHeaders()); | |
635 | ||
b107a5a5 | 636 | if (state.parsing == State::psIcapHeader) { |
637 | debugs(93, 5, HERE << "parse ICAP headers"); | |
774c051c | 638 | parseIcapHead(); |
b107a5a5 | 639 | } |
774c051c | 640 | |
b107a5a5 | 641 | if (state.parsing == State::psHttpHeader) { |
642 | debugs(93, 5, HERE << "parse HTTP headers"); | |
774c051c | 643 | parseHttpHead(); |
b107a5a5 | 644 | } |
774c051c | 645 | |
646 | if (state.parsingHeaders()) { // need more data | |
647 | Must(mayReadMore()); | |
648 | return; | |
649 | } | |
650 | ||
478cfe99 | 651 | startSending(); |
652 | } | |
653 | ||
654 | // called after parsing all headers or when bypassing an exception | |
655 | void ICAPModXact::startSending() | |
656 | { | |
657 | disableBypass("sent headers"); | |
c824c43b | 658 | sendAnswer(adapted.header); |
774c051c | 659 | |
660 | if (state.sending == State::sendingVirgin) | |
661 | echoMore(); | |
662 | } | |
663 | ||
664 | void ICAPModXact::parseIcapHead() | |
665 | { | |
666 | Must(state.sending == State::sendingUndecided); | |
667 | ||
668 | if (!parseHead(icapReply)) | |
669 | return; | |
670 | ||
fc764d26 | 671 | if (httpHeaderHasConnDir(&icapReply->header, "close")) { |
672 | debugs(93, 5, HERE << "found connection close"); | |
673 | reuseConnection = false; | |
674 | } | |
675 | ||
774c051c | 676 | switch (icapReply->sline.status) { |
677 | ||
678 | case 100: | |
679 | handle100Continue(); | |
680 | break; | |
681 | ||
682 | case 200: | |
5bd21e1d | 683 | case 201: // Symantec Scan Engine 5.0 and later when modifying HTTP msg |
b559db5d | 684 | |
685 | if (!validate200Ok()) { | |
686 | throw TexcHere("Invalid ICAP Response"); | |
687 | } else { | |
688 | handle200Ok(); | |
689 | } | |
690 | ||
774c051c | 691 | break; |
692 | ||
693 | case 204: | |
694 | handle204NoContent(); | |
695 | break; | |
696 | ||
697 | default: | |
b559db5d | 698 | debugs(93, 5, HERE << "ICAP status " << icapReply->sline.status); |
774c051c | 699 | handleUnknownScode(); |
700 | break; | |
701 | } | |
702 | ||
703 | // handle100Continue() manages state.writing on its own. | |
704 | // Non-100 status means the server needs no postPreview data from us. | |
705 | if (state.writing == State::writingPaused) | |
c99de607 | 706 | stopWriting(true); |
774c051c | 707 | } |
708 | ||
b559db5d | 709 | bool ICAPModXact::validate200Ok() |
710 | { | |
0bef8dd7 | 711 | if (ICAP::methodRespmod == service().cfg().method) { |
b559db5d | 712 | if (!gotEncapsulated("res-hdr")) |
713 | return false; | |
714 | ||
715 | return true; | |
716 | } | |
717 | ||
0bef8dd7 | 718 | if (ICAP::methodReqmod == service().cfg().method) { |
b559db5d | 719 | if (!gotEncapsulated("res-hdr") && !gotEncapsulated("req-hdr")) |
720 | return false; | |
721 | ||
722 | return true; | |
723 | } | |
724 | ||
725 | return false; | |
726 | } | |
727 | ||
774c051c | 728 | void ICAPModXact::handle100Continue() |
729 | { | |
730 | Must(state.writing == State::writingPaused); | |
5f8252d2 | 731 | // server must not respond before the end of preview: we may send ieof |
774c051c | 732 | Must(preview.enabled() && preview.done() && !preview.ieof()); |
774c051c | 733 | |
5f8252d2 | 734 | // 100 "Continue" cancels our preview commitment, not 204s outside preview |
735 | if (!state.allowedPostview204) | |
774c051c | 736 | stopBackup(); |
737 | ||
c99de607 | 738 | state.parsing = State::psIcapHeader; // eventually |
739 | icapReply->reset(); | |
774c051c | 740 | |
741 | state.writing = State::writingPrime; | |
742 | ||
743 | writeMore(); | |
744 | } | |
745 | ||
746 | void ICAPModXact::handle200Ok() | |
747 | { | |
748 | state.parsing = State::psHttpHeader; | |
749 | state.sending = State::sendingAdapted; | |
750 | stopBackup(); | |
5f8252d2 | 751 | checkConsuming(); |
774c051c | 752 | } |
753 | ||
754 | void ICAPModXact::handle204NoContent() | |
755 | { | |
756 | stopParsing(); | |
478cfe99 | 757 | prepEchoing(); |
758 | } | |
759 | ||
760 | // Called when we receive a 204 No Content response and | |
761 | // when we are trying to bypass a service failure. | |
762 | // We actually start sending (echoig or not) in startSending. | |
763 | void ICAPModXact::prepEchoing() | |
764 | { | |
765 | disableBypass("preparing to echo content"); | |
774c051c | 766 | |
767 | // We want to clone the HTTP message, but we do not want | |
5f8252d2 | 768 | // to copy some non-HTTP state parts that HttpMsg kids carry in them. |
774c051c | 769 | // Thus, we cannot use a smart pointer, copy constructor, or equivalent. |
770 | // Instead, we simply write the HTTP message and "clone" it by parsing. | |
771 | ||
5f8252d2 | 772 | HttpMsg *oldHead = virgin.header; |
774c051c | 773 | debugs(93, 7, "ICAPModXact cloning virgin message " << oldHead); |
774 | ||
775 | MemBuf httpBuf; | |
776 | ||
777 | // write the virgin message into a memory buffer | |
778 | httpBuf.init(); | |
779 | packHead(httpBuf, oldHead); | |
780 | ||
c99de607 | 781 | // allocate the adapted message and copy metainfo |
5f8252d2 | 782 | Must(!adapted.header); |
7514268e | 783 | HttpMsg *newHead = NULL; |
d67acb4e | 784 | if (dynamic_cast<const HttpRequest*>(oldHead)) { |
c99de607 | 785 | HttpRequest *newR = new HttpRequest; |
c99de607 | 786 | newHead = newR; |
9e008dda AJ |
787 | } else if (dynamic_cast<const HttpReply*>(oldHead)) { |
788 | HttpReply *newRep = new HttpReply; | |
789 | newHead = newRep; | |
d67acb4e | 790 | } |
774c051c | 791 | Must(newHead); |
d67acb4e | 792 | newHead->inheritProperties(oldHead); |
774c051c | 793 | |
5f8252d2 | 794 | adapted.setHeader(newHead); |
7514268e | 795 | |
774c051c | 796 | // parse the buffer back |
797 | http_status error = HTTP_STATUS_NONE; | |
798 | ||
799 | Must(newHead->parse(&httpBuf, true, &error)); | |
800 | ||
801 | Must(newHead->hdr_sz == httpBuf.contentSize()); // no leftovers | |
802 | ||
803 | httpBuf.clean(); | |
804 | ||
5f8252d2 | 805 | debugs(93, 7, "ICAPModXact cloned virgin message " << oldHead << " to " << |
9e008dda | 806 | newHead); |
5f8252d2 | 807 | |
808 | // setup adapted body pipe if needed | |
809 | if (oldHead->body_pipe != NULL) { | |
810 | debugs(93, 7, HERE << "will echo virgin body from " << | |
9e008dda | 811 | oldHead->body_pipe); |
478cfe99 | 812 | if (!virginBodySending.active()) |
813 | virginBodySending.plan(); // will throw if not possible | |
5f8252d2 | 814 | state.sending = State::sendingVirgin; |
815 | checkConsuming(); | |
478cfe99 | 816 | |
5f8252d2 | 817 | // TODO: optimize: is it possible to just use the oldHead pipe and |
818 | // remove ICAP from the loop? This echoing is probably a common case! | |
819 | makeAdaptedBodyPipe("echoed virgin response"); | |
820 | if (oldHead->body_pipe->bodySizeKnown()) | |
821 | adapted.body_pipe->setBodySize(oldHead->body_pipe->bodySize()); | |
822 | debugs(93, 7, HERE << "will echo virgin body to " << | |
9e008dda | 823 | adapted.body_pipe); |
5f8252d2 | 824 | } else { |
825 | debugs(93, 7, HERE << "no virgin body to echo"); | |
826 | stopSending(true); | |
827 | } | |
774c051c | 828 | } |
829 | ||
830 | void ICAPModXact::handleUnknownScode() | |
831 | { | |
832 | stopParsing(); | |
833 | stopBackup(); | |
834 | // TODO: mark connection as "bad" | |
835 | ||
836 | // Terminate the transaction; we do not know how to handle this response. | |
837 | throw TexcHere("Unsupported ICAP status code"); | |
838 | } | |
839 | ||
840 | void ICAPModXact::parseHttpHead() | |
841 | { | |
842 | if (gotEncapsulated("res-hdr") || gotEncapsulated("req-hdr")) { | |
843 | maybeAllocateHttpMsg(); | |
844 | ||
5f8252d2 | 845 | if (!parseHead(adapted.header)) |
c99de607 | 846 | return; // need more header data |
5f8252d2 | 847 | |
d67acb4e | 848 | if (dynamic_cast<HttpRequest*>(adapted.header)) { |
5f8252d2 | 849 | const HttpRequest *oldR = dynamic_cast<const HttpRequest*>(virgin.header); |
850 | Must(oldR); | |
9e008dda AJ |
851 | // TODO: the adapted request did not really originate from the |
852 | // client; give proxy admin an option to prevent copying of | |
5f8252d2 | 853 | // sensitive client information here. See the following thread: |
854 | // http://www.squid-cache.org/mail-archive/squid-dev/200703/0040.html | |
5f8252d2 | 855 | } |
d67acb4e | 856 | |
9e008dda AJ |
857 | // Maybe adapted.header==NULL if HttpReply and have Http 0.9 .... |
858 | if (adapted.header) | |
859 | adapted.header->inheritProperties(virgin.header); | |
774c051c | 860 | } |
861 | ||
5f8252d2 | 862 | decideOnParsingBody(); |
774c051c | 863 | } |
864 | ||
c99de607 | 865 | // parses both HTTP and ICAP headers |
774c051c | 866 | bool ICAPModXact::parseHead(HttpMsg *head) |
867 | { | |
c99de607 | 868 | Must(head); |
def17b6a | 869 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " head bytes to parse" << |
774c051c | 870 | "; state: " << state.parsing); |
871 | ||
872 | http_status error = HTTP_STATUS_NONE; | |
873 | const bool parsed = head->parse(&readBuf, commEof, &error); | |
874 | Must(parsed || !error); // success or need more data | |
875 | ||
c99de607 | 876 | if (!parsed) { // need more data |
b107a5a5 | 877 | debugs(93, 5, HERE << "parse failed, need more data, return false"); |
774c051c | 878 | head->reset(); |
879 | return false; | |
880 | } | |
881 | ||
b107a5a5 | 882 | debugs(93, 5, HERE << "parse success, consume " << head->hdr_sz << " bytes, return true"); |
774c051c | 883 | readBuf.consume(head->hdr_sz); |
884 | return true; | |
885 | } | |
886 | ||
9e008dda AJ |
887 | void ICAPModXact::decideOnParsingBody() |
888 | { | |
200ac359 | 889 | if (gotEncapsulated("res-body") || gotEncapsulated("req-body")) { |
5f8252d2 | 890 | debugs(93, 5, HERE << "expecting a body"); |
891 | state.parsing = State::psBody; | |
892 | bodyParser = new ChunkedCodingParser; | |
893 | makeAdaptedBodyPipe("adapted response from the ICAP server"); | |
894 | Must(state.sending == State::sendingAdapted); | |
774c051c | 895 | } else { |
b559db5d | 896 | debugs(93, 5, HERE << "not expecting a body"); |
5f8252d2 | 897 | stopParsing(); |
898 | stopSending(true); | |
774c051c | 899 | } |
774c051c | 900 | } |
901 | ||
5f8252d2 | 902 | void ICAPModXact::parseBody() |
774c051c | 903 | { |
5f8252d2 | 904 | Must(state.parsing == State::psBody); |
905 | Must(bodyParser); | |
774c051c | 906 | |
5f8252d2 | 907 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " body bytes to parse"); |
774c051c | 908 | |
5f8252d2 | 909 | // the parser will throw on errors |
910 | BodyPipeCheckout bpc(*adapted.body_pipe); | |
911 | const bool parsed = bodyParser->parse(&readBuf, &bpc.buf); | |
912 | bpc.checkIn(); | |
774c051c | 913 | |
aa761e5f | 914 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " body bytes after " << |
774c051c | 915 | "parse; parsed all: " << parsed); |
916 | ||
478cfe99 | 917 | // TODO: expose BodyPipe::putSize() to make this check simpler and clearer |
918 | if (adapted.body_pipe->buf().contentSize() > 0) // parsed something sometime | |
919 | disableBypass("sent adapted content"); | |
920 | ||
5f8252d2 | 921 | if (parsed) { |
922 | stopParsing(); | |
923 | stopSending(true); // the parser succeeds only if all parsed data fits | |
924 | return; | |
925 | } | |
774c051c | 926 | |
c99de607 | 927 | debugs(93,3,HERE << this << " needsMoreData = " << bodyParser->needsMoreData()); |
3b299123 | 928 | |
929 | if (bodyParser->needsMoreData()) { | |
c99de607 | 930 | debugs(93,3,HERE << this); |
774c051c | 931 | Must(mayReadMore()); |
3b299123 | 932 | readMore(); |
933 | } | |
774c051c | 934 | |
935 | if (bodyParser->needsMoreSpace()) { | |
936 | Must(!doneSending()); // can hope for more space | |
5f8252d2 | 937 | Must(adapted.body_pipe->buf().contentSize() > 0); // paranoid |
938 | // TODO: there should be a timeout in case the sink is broken | |
939 | // or cannot consume partial content (while we need more space) | |
774c051c | 940 | } |
774c051c | 941 | } |
942 | ||
943 | void ICAPModXact::stopParsing() | |
944 | { | |
945 | if (state.parsing == State::psDone) | |
946 | return; | |
947 | ||
5f8252d2 | 948 | debugs(93, 7, "ICAPModXact will no longer parse" << status()); |
774c051c | 949 | |
950 | delete bodyParser; | |
951 | ||
952 | bodyParser = NULL; | |
953 | ||
954 | state.parsing = State::psDone; | |
955 | } | |
956 | ||
957 | // HTTP side added virgin body data | |
bd7f2ede | 958 | void ICAPModXact::noteMoreBodyDataAvailable(BodyPipe::Pointer) |
774c051c | 959 | { |
774c051c | 960 | writeMore(); |
961 | ||
962 | if (state.sending == State::sendingVirgin) | |
963 | echoMore(); | |
774c051c | 964 | } |
965 | ||
966 | // HTTP side sent us all virgin info | |
bd7f2ede | 967 | void ICAPModXact::noteBodyProductionEnded(BodyPipe::Pointer) |
774c051c | 968 | { |
5f8252d2 | 969 | Must(virgin.body_pipe->productionEnded()); |
774c051c | 970 | |
971 | // push writer and sender in case we were waiting for the last-chunk | |
972 | writeMore(); | |
973 | ||
974 | if (state.sending == State::sendingVirgin) | |
975 | echoMore(); | |
774c051c | 976 | } |
977 | ||
9e008dda | 978 | // body producer aborted, but the initiator may still want to know |
585ab260 | 979 | // the answer, even though the HTTP message has been truncated |
bd7f2ede | 980 | void ICAPModXact::noteBodyProducerAborted(BodyPipe::Pointer) |
774c051c | 981 | { |
585ab260 | 982 | Must(virgin.body_pipe->productionEnded()); |
983 | ||
984 | // push writer and sender in case we were waiting for the last-chunk | |
985 | writeMore(); | |
986 | ||
987 | if (state.sending == State::sendingVirgin) | |
988 | echoMore(); | |
5f8252d2 | 989 | } |
990 | ||
9e008dda | 991 | // adapted body consumer wants more adapted data and |
5f8252d2 | 992 | // possibly freed some buffer space |
bd7f2ede | 993 | void ICAPModXact::noteMoreBodySpaceAvailable(BodyPipe::Pointer) |
774c051c | 994 | { |
774c051c | 995 | if (state.sending == State::sendingVirgin) |
996 | echoMore(); | |
3b299123 | 997 | else if (state.sending == State::sendingAdapted) |
998 | parseMore(); | |
774c051c | 999 | else |
3b299123 | 1000 | Must(state.sending == State::sendingUndecided); |
774c051c | 1001 | } |
1002 | ||
5f8252d2 | 1003 | // adapted body consumer aborted |
bd7f2ede | 1004 | void ICAPModXact::noteBodyConsumerAborted(BodyPipe::Pointer) |
774c051c | 1005 | { |
5f8252d2 | 1006 | mustStop("adapted body consumer aborted"); |
774c051c | 1007 | } |
1008 | ||
1009 | // internal cleanup | |
5f8252d2 | 1010 | void ICAPModXact::swanSong() |
774c051c | 1011 | { |
5f8252d2 | 1012 | debugs(93, 5, HERE << "swan sings" << status()); |
1013 | ||
c99de607 | 1014 | stopWriting(false); |
c824c43b | 1015 | stopSending(false); |
774c051c | 1016 | |
1017 | if (icapReply) { | |
1018 | delete icapReply; | |
1019 | icapReply = NULL; | |
1020 | } | |
1021 | ||
5f8252d2 | 1022 | ICAPXaction::swanSong(); |
774c051c | 1023 | } |
1024 | ||
1025 | void ICAPModXact::makeRequestHeaders(MemBuf &buf) | |
1026 | { | |
cc192b50 | 1027 | char ntoabuf[MAX_IPSTRLEN]; |
12b91c99 | 1028 | /* |
1029 | * XXX These should use HttpHdr interfaces instead of Printfs | |
1030 | */ | |
0bef8dd7 | 1031 | const Adaptation::ServiceConfig &s = service().cfg(); |
2c1fd837 FC |
1032 | buf.Printf("%s " SQUIDSTRINGPH " ICAP/1.0\r\n", s.methodStr(), SQUIDSTRINGPRINT(s.uri)); |
1033 | buf.Printf("Host: " SQUIDSTRINGPPH ":%d\r\n", SQUIDSTRINGPRINT(s.host), s.port); | |
12b91c99 | 1034 | buf.Printf("Date: %s\r\n", mkrfc1123(squid_curtime)); |
1035 | ||
1036 | if (!TheICAPConfig.reuse_connections) | |
1037 | buf.Printf("Connection: close\r\n"); | |
1038 | ||
2cdeea82 | 1039 | // we must forward "Proxy-Authenticate" and "Proxy-Authorization" |
1040 | // as ICAP headers. | |
4232c626 FC |
1041 | if (virgin.header->header.has(HDR_PROXY_AUTHENTICATE)) { |
1042 | String vh=virgin.header->header.getByName("Proxy-Authenticate"); | |
1043 | buf.Printf("Proxy-Authenticate: %.*s\r\n", | |
1044 | vh.size(), vh.rawBuf()); | |
1045 | } | |
9e008dda | 1046 | |
4232c626 FC |
1047 | if (virgin.header->header.has(HDR_PROXY_AUTHORIZATION)) { |
1048 | String vh=virgin.header->header.getByName("Proxy-Authorization"); | |
1049 | buf.Printf("Proxy-Authorization: %.*s\r\n", | |
1050 | vh.size(), vh.rawBuf()); | |
1051 | } | |
2cdeea82 | 1052 | |
774c051c | 1053 | buf.Printf("Encapsulated: "); |
1054 | ||
1055 | MemBuf httpBuf; | |
12b91c99 | 1056 | |
774c051c | 1057 | httpBuf.init(); |
1058 | ||
1059 | // build HTTP request header, if any | |
1060 | ICAP::Method m = s.method; | |
1061 | ||
5f8252d2 | 1062 | const HttpRequest *request = virgin.cause ? |
9e008dda AJ |
1063 | virgin.cause : |
1064 | dynamic_cast<const HttpRequest*>(virgin.header); | |
c99de607 | 1065 | |
5f8252d2 | 1066 | // to simplify, we could assume that request is always available |
c99de607 | 1067 | |
30abd221 | 1068 | String urlPath; |
c99de607 | 1069 | if (request) { |
1070 | urlPath = request->urlpath; | |
1071 | if (ICAP::methodRespmod == m) | |
1072 | encapsulateHead(buf, "req-hdr", httpBuf, request); | |
1073 | else | |
9e008dda AJ |
1074 | if (ICAP::methodReqmod == m) |
1075 | encapsulateHead(buf, "req-hdr", httpBuf, virgin.header); | |
c99de607 | 1076 | } |
774c051c | 1077 | |
1078 | if (ICAP::methodRespmod == m) | |
5f8252d2 | 1079 | if (const HttpMsg *prime = virgin.header) |
774c051c | 1080 | encapsulateHead(buf, "res-hdr", httpBuf, prime); |
1081 | ||
1082 | if (!virginBody.expected()) | |
1dd6edf2 | 1083 | buf.Printf("null-body=%d", (int) httpBuf.contentSize()); |
774c051c | 1084 | else if (ICAP::methodReqmod == m) |
1dd6edf2 | 1085 | buf.Printf("req-body=%d", (int) httpBuf.contentSize()); |
774c051c | 1086 | else |
1dd6edf2 | 1087 | buf.Printf("res-body=%d", (int) httpBuf.contentSize()); |
774c051c | 1088 | |
1089 | buf.append(ICAP::crlf, 2); // terminate Encapsulated line | |
1090 | ||
c824c43b | 1091 | if (preview.enabled()) { |
774c051c | 1092 | buf.Printf("Preview: %d\r\n", (int)preview.ad()); |
5f8252d2 | 1093 | if (virginBody.expected()) // there is a body to preview |
1094 | virginBodySending.plan(); | |
1095 | else | |
1096 | finishNullOrEmptyBodyPreview(httpBuf); | |
774c051c | 1097 | } |
1098 | ||
1099 | if (shouldAllow204()) { | |
5f8252d2 | 1100 | debugs(93,5, HERE << "will allow 204s outside of preview"); |
1101 | state.allowedPostview204 = true; | |
774c051c | 1102 | buf.Printf("Allow: 204\r\n"); |
5f8252d2 | 1103 | if (virginBody.expected()) // there is a body to echo |
1104 | virginBodySending.plan(); | |
774c051c | 1105 | } |
1106 | ||
c99de607 | 1107 | if (TheICAPConfig.send_client_ip && request) |
cc192b50 | 1108 | if (!request->client_addr.IsAnyAddr() && !request->client_addr.IsNoAddr()) |
1109 | buf.Printf("X-Client-IP: %s\r\n", request->client_addr.NtoA(ntoabuf,MAX_IPSTRLEN)); | |
a97e82a8 | 1110 | |
c99de607 | 1111 | if (TheICAPConfig.send_client_username && request) |
5f8252d2 | 1112 | makeUsernameHeader(request, buf); |
a97e82a8 | 1113 | |
bb790702 | 1114 | // fprintf(stderr, "%s\n", buf.content()); |
a97e82a8 | 1115 | |
774c051c | 1116 | buf.append(ICAP::crlf, 2); // terminate ICAP header |
1117 | ||
1118 | // start ICAP request body with encapsulated HTTP headers | |
1119 | buf.append(httpBuf.content(), httpBuf.contentSize()); | |
1120 | ||
1121 | httpBuf.clean(); | |
1122 | } | |
1123 | ||
9e008dda AJ |
1124 | void ICAPModXact::makeUsernameHeader(const HttpRequest *request, MemBuf &buf) |
1125 | { | |
76f142cd | 1126 | if (const AuthUserRequest *auth = request->auth_user_request) { |
5f8252d2 | 1127 | if (char const *name = auth->username()) { |
1128 | const char *value = TheICAPConfig.client_username_encode ? | |
9e008dda | 1129 | base64_encode(name) : name; |
5f8252d2 | 1130 | buf.Printf("%s: %s\r\n", TheICAPConfig.client_username_header, |
9e008dda | 1131 | value); |
5f8252d2 | 1132 | } |
1133 | } | |
1134 | } | |
1135 | ||
774c051c | 1136 | void ICAPModXact::encapsulateHead(MemBuf &icapBuf, const char *section, MemBuf &httpBuf, const HttpMsg *head) |
1137 | { | |
1138 | // update ICAP header | |
7cab7e9f | 1139 | icapBuf.Printf("%s=%d, ", section, (int) httpBuf.contentSize()); |
774c051c | 1140 | |
2cdeea82 | 1141 | // begin cloning |
1142 | HttpMsg *headClone = NULL; | |
9e008dda | 1143 | |
2cdeea82 | 1144 | if (const HttpRequest* old_request = dynamic_cast<const HttpRequest*>(head)) { |
1145 | HttpRequest* new_request = new HttpRequest; | |
1146 | urlParse(old_request->method, old_request->canonical,new_request); | |
1147 | new_request->http_ver = old_request->http_ver; | |
2cdeea82 | 1148 | headClone = new_request; |
9e008dda | 1149 | } else if (const HttpReply *old_reply = dynamic_cast<const HttpReply*>(head)) { |
2cdeea82 | 1150 | HttpReply* new_reply = new HttpReply; |
1151 | new_reply->sline = old_reply->sline; | |
1152 | headClone = new_reply; | |
1153 | } | |
9e008dda | 1154 | |
2cdeea82 | 1155 | Must(headClone); |
d67acb4e | 1156 | headClone->inheritProperties(head); |
9e008dda | 1157 | |
2cdeea82 | 1158 | HttpHeaderPos pos = HttpHeaderInitPos; |
1159 | HttpHeaderEntry* p_head_entry = NULL; | |
1160 | while (NULL != (p_head_entry = head->header.getEntry(&pos)) ) | |
1161 | headClone->header.addEntry(p_head_entry->clone()); | |
1162 | ||
1163 | // end cloning | |
9e008dda | 1164 | |
2cdeea82 | 1165 | // remove all hop-by-hop headers from the clone |
dcf3665b | 1166 | headClone->header.delById(HDR_PROXY_AUTHENTICATE); |
2cdeea82 | 1167 | headClone->header.removeHopByHopEntries(); |
1168 | ||
1169 | // pack polished HTTP header | |
1170 | packHead(httpBuf, headClone); | |
1171 | ||
1172 | delete headClone; | |
774c051c | 1173 | } |
1174 | ||
1175 | void ICAPModXact::packHead(MemBuf &httpBuf, const HttpMsg *head) | |
1176 | { | |
1177 | Packer p; | |
1178 | packerToMemInit(&p, &httpBuf); | |
1179 | head->packInto(&p, true); | |
1180 | packerClean(&p); | |
1181 | } | |
1182 | ||
1183 | // decides whether to offer a preview and calculates its size | |
c824c43b | 1184 | void ICAPModXact::decideOnPreview() |
774c051c | 1185 | { |
7cdbbd47 | 1186 | if (!TheICAPConfig.preview_enable) { |
1187 | debugs(93, 5, HERE << "preview disabled by squid.conf"); | |
c824c43b | 1188 | return; |
7cdbbd47 | 1189 | } |
1190 | ||
c824c43b | 1191 | const HttpRequest *request = virgin.cause ? |
9e008dda AJ |
1192 | virgin.cause : |
1193 | dynamic_cast<const HttpRequest*>(virgin.header); | |
30abd221 | 1194 | const String urlPath = request ? request->urlpath : String(); |
5f8252d2 | 1195 | size_t wantedSize; |
c99de607 | 1196 | if (!service().wantsPreview(urlPath, wantedSize)) { |
1197 | debugs(93, 5, "ICAPModXact should not offer preview for " << urlPath); | |
c824c43b | 1198 | return; |
774c051c | 1199 | } |
1200 | ||
c824c43b | 1201 | // we decided to do preview, now compute its size |
1202 | ||
774c051c | 1203 | Must(wantedSize >= 0); |
1204 | ||
1205 | // cannot preview more than we can backup | |
1206 | size_t ad = XMIN(wantedSize, TheBackupLimit); | |
1207 | ||
5f8252d2 | 1208 | if (!virginBody.expected()) |
1209 | ad = 0; | |
774c051c | 1210 | else |
9e008dda AJ |
1211 | if (virginBody.knownSize()) |
1212 | ad = XMIN(static_cast<uint64_t>(ad), virginBody.size()); // not more than we have | |
774c051c | 1213 | |
1214 | debugs(93, 5, "ICAPModXact should offer " << ad << "-byte preview " << | |
1215 | "(service wanted " << wantedSize << ")"); | |
1216 | ||
1217 | preview.enable(ad); | |
5f8252d2 | 1218 | Must(preview.enabled()); |
774c051c | 1219 | } |
1220 | ||
1221 | // decides whether to allow 204 responses | |
1222 | bool ICAPModXact::shouldAllow204() | |
1223 | { | |
1224 | if (!service().allows204()) | |
1225 | return false; | |
1226 | ||
c824c43b | 1227 | return canBackupEverything(); |
1228 | } | |
1229 | ||
1230 | // used by shouldAllow204 and decideOnRetries | |
1231 | bool ICAPModXact::canBackupEverything() const | |
1232 | { | |
774c051c | 1233 | if (!virginBody.expected()) |
c824c43b | 1234 | return true; // no body means no problems with backup |
774c051c | 1235 | |
c824c43b | 1236 | // if there is a body, check whether we can backup it all |
774c051c | 1237 | |
1238 | if (!virginBody.knownSize()) | |
1239 | return false; | |
1240 | ||
1241 | // or should we have a different backup limit? | |
1242 | // note that '<' allows for 0-termination of the "full" backup buffer | |
1243 | return virginBody.size() < TheBackupLimit; | |
1244 | } | |
1245 | ||
c824c43b | 1246 | // Decide whether this transaction can be retried if pconn fails |
1247 | // Must be called after decideOnPreview and before openConnection() | |
1248 | void ICAPModXact::decideOnRetries() | |
1249 | { | |
1250 | if (!isRetriable) | |
1251 | return; // no, already decided | |
1252 | ||
1253 | if (preview.enabled()) | |
1254 | return; // yes, because preview provides enough guarantees | |
1255 | ||
1256 | if (canBackupEverything()) | |
1257 | return; // yes, because we can back everything up | |
1258 | ||
1259 | disableRetries(); // no, because we cannot back everything up | |
1260 | } | |
1261 | ||
5f8252d2 | 1262 | // Normally, the body-writing code handles preview body. It can deal with |
1263 | // bodies of unexpected size, including those that turn out to be empty. | |
1264 | // However, that code assumes that the body was expected and body control | |
1265 | // structures were initialized. This is not the case when there is no body | |
1266 | // or the body is known to be empty, because the virgin message will lack a | |
1267 | // body_pipe. So we handle preview of null-body and zero-size bodies here. | |
1268 | void ICAPModXact::finishNullOrEmptyBodyPreview(MemBuf &buf) | |
1269 | { | |
1270 | Must(!virginBodyWriting.active()); // one reason we handle it here | |
1271 | Must(!virgin.body_pipe); // another reason we handle it here | |
1272 | Must(!preview.ad()); | |
1273 | ||
1274 | // do not add last-chunk because our Encapsulated header says null-body | |
bb790702 | 1275 | // addLastRequestChunk(buf); |
5f8252d2 | 1276 | preview.wrote(0, true); |
1277 | ||
1278 | Must(preview.done()); | |
1279 | Must(preview.ieof()); | |
1280 | } | |
1281 | ||
774c051c | 1282 | void ICAPModXact::fillPendingStatus(MemBuf &buf) const |
1283 | { | |
c99de607 | 1284 | ICAPXaction::fillPendingStatus(buf); |
1285 | ||
774c051c | 1286 | if (state.serviceWaiting) |
1287 | buf.append("U", 1); | |
1288 | ||
5f8252d2 | 1289 | if (virgin.body_pipe != NULL) |
c99de607 | 1290 | buf.append("R", 1); |
1291 | ||
5f8252d2 | 1292 | if (connection > 0 && !doneReading()) |
c99de607 | 1293 | buf.append("r", 1); |
1294 | ||
774c051c | 1295 | if (!state.doneWriting() && state.writing != State::writingInit) |
1296 | buf.Printf("w(%d)", state.writing); | |
1297 | ||
1298 | if (preview.enabled()) { | |
1299 | if (!preview.done()) | |
1dd6edf2 | 1300 | buf.Printf("P(%d)", (int) preview.debt()); |
774c051c | 1301 | } |
1302 | ||
5f8252d2 | 1303 | if (virginBodySending.active()) |
774c051c | 1304 | buf.append("B", 1); |
1305 | ||
1306 | if (!state.doneParsing() && state.parsing != State::psIcapHeader) | |
1307 | buf.Printf("p(%d)", state.parsing); | |
1308 | ||
1309 | if (!doneSending() && state.sending != State::sendingUndecided) | |
1310 | buf.Printf("S(%d)", state.sending); | |
478cfe99 | 1311 | |
1312 | if (canStartBypass) | |
9e008dda | 1313 | buf.append("Y", 1); |
774c051c | 1314 | } |
1315 | ||
1316 | void ICAPModXact::fillDoneStatus(MemBuf &buf) const | |
1317 | { | |
c99de607 | 1318 | ICAPXaction::fillDoneStatus(buf); |
1319 | ||
5f8252d2 | 1320 | if (!virgin.body_pipe) |
774c051c | 1321 | buf.append("R", 1); |
1322 | ||
1323 | if (state.doneWriting()) | |
1324 | buf.append("w", 1); | |
1325 | ||
1326 | if (preview.enabled()) { | |
1327 | if (preview.done()) | |
1328 | buf.Printf("P%s", preview.ieof() ? "(ieof)" : ""); | |
1329 | } | |
1330 | ||
1331 | if (doneReading()) | |
1332 | buf.append("r", 1); | |
1333 | ||
1334 | if (state.doneParsing()) | |
1335 | buf.append("p", 1); | |
1336 | ||
1337 | if (doneSending()) | |
1338 | buf.append("S", 1); | |
1339 | } | |
1340 | ||
1341 | bool ICAPModXact::gotEncapsulated(const char *section) const | |
1342 | { | |
a9925b40 | 1343 | return icapReply->header.getByNameListMember("Encapsulated", |
1344 | section, ',').size() > 0; | |
774c051c | 1345 | } |
1346 | ||
1347 | // calculate whether there is a virgin HTTP body and | |
1348 | // whether its expected size is known | |
5f8252d2 | 1349 | // TODO: rename because we do not just estimate |
774c051c | 1350 | void ICAPModXact::estimateVirginBody() |
1351 | { | |
5f8252d2 | 1352 | // note: lack of size info may disable previews and 204s |
774c051c | 1353 | |
5f8252d2 | 1354 | HttpMsg *msg = virgin.header; |
1355 | Must(msg); | |
774c051c | 1356 | |
60745f24 | 1357 | HttpRequestMethod method; |
774c051c | 1358 | |
5f8252d2 | 1359 | if (virgin.cause) |
1360 | method = virgin.cause->method; | |
774c051c | 1361 | else |
9e008dda AJ |
1362 | if (HttpRequest *req = dynamic_cast<HttpRequest*>(msg)) |
1363 | method = req->method; | |
1364 | else | |
1365 | method = METHOD_NONE; | |
774c051c | 1366 | |
47f6e231 | 1367 | int64_t size; |
5f8252d2 | 1368 | // expectingBody returns true for zero-sized bodies, but we will not |
1369 | // get a pipe for that body, so we treat the message as bodyless | |
1370 | if (method != METHOD_NONE && msg->expectingBody(method, size) && size) { | |
9e008dda AJ |
1371 | debugs(93, 6, "ICAPModXact expects virgin body from " << |
1372 | virgin.body_pipe << "; size: " << size); | |
5f8252d2 | 1373 | |
1374 | virginBody.expect(size); | |
1375 | virginBodyWriting.plan(); | |
1376 | ||
1377 | // sign up as a body consumer | |
1378 | Must(msg->body_pipe != NULL); | |
1379 | Must(msg->body_pipe == virgin.body_pipe); | |
1380 | Must(virgin.body_pipe->setConsumerIfNotLate(this)); | |
1381 | ||
1382 | // make sure TheBackupLimit is in-sync with the buffer size | |
9c175897 | 1383 | Must(TheBackupLimit <= static_cast<size_t>(msg->body_pipe->buf().max_capacity)); |
774c051c | 1384 | } else { |
1385 | debugs(93, 6, "ICAPModXact does not expect virgin body"); | |
5f8252d2 | 1386 | Must(msg->body_pipe == NULL); |
1387 | checkConsuming(); | |
774c051c | 1388 | } |
1389 | } | |
1390 | ||
9e008dda AJ |
1391 | void ICAPModXact::makeAdaptedBodyPipe(const char *what) |
1392 | { | |
5f8252d2 | 1393 | Must(!adapted.body_pipe); |
1394 | Must(!adapted.header->body_pipe); | |
1395 | adapted.header->body_pipe = new BodyPipe(this); | |
1396 | adapted.body_pipe = adapted.header->body_pipe; | |
1397 | debugs(93, 7, HERE << "will supply " << what << " via " << | |
9e008dda | 1398 | adapted.body_pipe << " pipe"); |
5f8252d2 | 1399 | } |
1400 | ||
774c051c | 1401 | |
1402 | // TODO: Move SizedEstimate, MemBufBackup, and ICAPPreview elsewhere | |
1403 | ||
1404 | SizedEstimate::SizedEstimate() | |
1405 | : theData(dtUnexpected) | |
1406 | {} | |
1407 | ||
47f6e231 | 1408 | void SizedEstimate::expect(int64_t aSize) |
774c051c | 1409 | { |
47f6e231 | 1410 | theData = (aSize >= 0) ? aSize : (int64_t)dtUnknown; |
774c051c | 1411 | } |
1412 | ||
1413 | bool SizedEstimate::expected() const | |
1414 | { | |
1415 | return theData != dtUnexpected; | |
1416 | } | |
1417 | ||
1418 | bool SizedEstimate::knownSize() const | |
1419 | { | |
1420 | Must(expected()); | |
1421 | return theData != dtUnknown; | |
1422 | } | |
1423 | ||
47f6e231 | 1424 | uint64_t SizedEstimate::size() const |
774c051c | 1425 | { |
1426 | Must(knownSize()); | |
47f6e231 | 1427 | return static_cast<uint64_t>(theData); |
774c051c | 1428 | } |
1429 | ||
1430 | ||
1431 | ||
478cfe99 | 1432 | VirginBodyAct::VirginBodyAct(): theStart(0), theState(stUndecided) |
774c051c | 1433 | {} |
1434 | ||
5f8252d2 | 1435 | void VirginBodyAct::plan() |
774c051c | 1436 | { |
478cfe99 | 1437 | Must(!disabled()); |
1438 | Must(!theStart); // not started | |
1439 | theState = stActive; | |
774c051c | 1440 | } |
1441 | ||
5f8252d2 | 1442 | void VirginBodyAct::disable() |
774c051c | 1443 | { |
478cfe99 | 1444 | theState = stDisabled; |
774c051c | 1445 | } |
1446 | ||
5f8252d2 | 1447 | void VirginBodyAct::progress(size_t size) |
774c051c | 1448 | { |
1449 | Must(active()); | |
1450 | Must(size >= 0); | |
47f6e231 | 1451 | theStart += static_cast<int64_t>(size); |
774c051c | 1452 | } |
1453 | ||
47f6e231 | 1454 | uint64_t VirginBodyAct::offset() const |
774c051c | 1455 | { |
1456 | Must(active()); | |
47f6e231 | 1457 | return static_cast<uint64_t>(theStart); |
774c051c | 1458 | } |
1459 | ||
774c051c | 1460 | |
1461 | ICAPPreview::ICAPPreview(): theWritten(0), theAd(0), theState(stDisabled) | |
1462 | {} | |
1463 | ||
1464 | void ICAPPreview::enable(size_t anAd) | |
1465 | { | |
1466 | // TODO: check for anAd not exceeding preview size limit | |
1467 | Must(anAd >= 0); | |
1468 | Must(!enabled()); | |
1469 | theAd = anAd; | |
1470 | theState = stWriting; | |
1471 | } | |
1472 | ||
1473 | bool ICAPPreview::enabled() const | |
1474 | { | |
1475 | return theState != stDisabled; | |
1476 | } | |
1477 | ||
1478 | size_t ICAPPreview::ad() const | |
1479 | { | |
1480 | Must(enabled()); | |
1481 | return theAd; | |
1482 | } | |
1483 | ||
1484 | bool ICAPPreview::done() const | |
1485 | { | |
1486 | Must(enabled()); | |
1487 | return theState >= stIeof; | |
1488 | } | |
1489 | ||
1490 | bool ICAPPreview::ieof() const | |
1491 | { | |
1492 | Must(enabled()); | |
1493 | return theState == stIeof; | |
1494 | } | |
1495 | ||
1496 | size_t ICAPPreview::debt() const | |
1497 | { | |
1498 | Must(enabled()); | |
1499 | return done() ? 0 : (theAd - theWritten); | |
1500 | } | |
1501 | ||
c99de607 | 1502 | void ICAPPreview::wrote(size_t size, bool wroteEof) |
774c051c | 1503 | { |
1504 | Must(enabled()); | |
5f8252d2 | 1505 | |
774c051c | 1506 | theWritten += size; |
1507 | ||
9e008dda | 1508 | Must(theWritten <= theAd); |
5f8252d2 | 1509 | |
9e008dda AJ |
1510 | if (wroteEof) |
1511 | theState = stIeof; // written size is irrelevant | |
1512 | else | |
1513 | if (theWritten >= theAd) | |
1514 | theState = stDone; | |
774c051c | 1515 | } |
1516 | ||
3cfc19b3 | 1517 | bool ICAPModXact::fillVirginHttpHeader(MemBuf &mb) const |
1518 | { | |
5f8252d2 | 1519 | if (virgin.header == NULL) |
3cfc19b3 | 1520 | return false; |
1521 | ||
5f8252d2 | 1522 | virgin.header->firstLineBuf(mb); |
3cfc19b3 | 1523 | |
1524 | return true; | |
1525 | } | |
c824c43b | 1526 | |
1527 | ||
1528 | /* ICAPModXactLauncher */ | |
1529 | ||
0bef8dd7 | 1530 | ICAPModXactLauncher::ICAPModXactLauncher(Adaptation::Initiator *anInitiator, HttpMsg *virginHeader, HttpRequest *virginCause, Adaptation::ServicePointer aService): |
9e008dda AJ |
1531 | AsyncJob("ICAPModXactLauncher"), |
1532 | ICAPLauncher("ICAPModXactLauncher", anInitiator, aService) | |
c824c43b | 1533 | { |
1534 | virgin.setHeader(virginHeader); | |
1535 | virgin.setCause(virginCause); | |
1536 | } | |
1537 | ||
1538 | ICAPXaction *ICAPModXactLauncher::createXaction() | |
1539 | { | |
0bef8dd7 AR |
1540 | ICAPServiceRep::Pointer s = |
1541 | dynamic_cast<ICAPServiceRep*>(theService.getRaw()); | |
1542 | Must(s != NULL); | |
1543 | return new ICAPModXact(this, virgin.header, virgin.cause, s); | |
c824c43b | 1544 | } |