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