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