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