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