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