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