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