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