]>
Commit | Line | Data |
---|---|---|
774c051c | 1 | /* |
2 | * DEBUG: section 93 ICAP (RFC 3507) Client | |
3 | */ | |
4 | ||
5 | #include "squid.h" | |
6 | #include "comm.h" | |
7 | #include "MsgPipe.h" | |
8 | #include "MsgPipeData.h" | |
9 | #include "HttpRequest.h" | |
10 | #include "HttpReply.h" | |
11 | #include "ICAPServiceRep.h" | |
12 | #include "ICAPModXact.h" | |
13 | #include "ICAPClient.h" | |
14 | #include "ChunkedCodingParser.h" | |
15 | #include "TextException.h" | |
a97e82a8 | 16 | #include "AuthUserRequest.h" |
12b91c99 | 17 | #include "ICAPConfig.h" |
774c051c | 18 | |
19 | // flow and terminology: | |
20 | // HTTP| --> receive --> encode --> write --> |network | |
21 | // end | <-- send <-- parse <-- read <-- |end | |
22 | ||
23 | // TODO: doneSending()/doneReceving() data members should probably be in sync | |
24 | // with this->adapted/virgin pointers. Make adapted/virgin methods? | |
25 | ||
26 | // TODO: replace gotEncapsulated() with something faster; we call it often | |
27 | ||
28 | CBDATA_CLASS_INIT(ICAPModXact); | |
29 | ||
30 | static const size_t TheBackupLimit = ICAP::MsgPipeBufSizeMax; | |
31 | ||
12b91c99 | 32 | extern ICAPConfig TheICAPConfig; |
33 | ||
774c051c | 34 | |
35 | ICAPModXact::State::State() | |
36 | { | |
37 | memset(this, sizeof(*this), 0); | |
38 | } | |
39 | ||
40 | ICAPModXact::ICAPModXact(): ICAPXaction("ICAPModXact"), | |
41 | self(NULL), virgin(NULL), adapted(NULL), | |
42 | icapReply(NULL), virginConsumed(0), | |
43 | bodyParser(NULL) | |
44 | {} | |
45 | ||
46 | void ICAPModXact::init(ICAPServiceRep::Pointer &aService, MsgPipe::Pointer &aVirgin, MsgPipe::Pointer &anAdapted, Pointer &aSelf) | |
47 | { | |
48 | assert(!self.getRaw() && !virgin.getRaw() && !adapted.getRaw()); | |
49 | assert(aSelf.getRaw() && aVirgin.getRaw() && anAdapted.getRaw()); | |
50 | ||
51 | self = aSelf; | |
52 | service(aService); | |
53 | ||
54 | virgin = aVirgin; | |
55 | adapted = anAdapted; | |
56 | ||
57 | // receiving end | |
58 | virgin->sink = this; // should be 'self' and refcounted | |
59 | // virgin pipe data is initiated by the source | |
60 | ||
61 | // sending end | |
62 | adapted->source = this; // should be 'self' and refcounted | |
63 | adapted->data = new MsgPipeData; | |
64 | ||
65 | adapted->data->body = new MemBuf; // XXX: make body a non-pointer? | |
66 | adapted->data->body->init(ICAP::MsgPipeBufSizeMin, ICAP::MsgPipeBufSizeMax); | |
67 | // headers are initialized when we parse them | |
68 | ||
69 | // writing and reading ends are handled by ICAPXaction | |
70 | ||
71 | // encoding | |
72 | // nothing to do because we are using temporary buffers | |
73 | ||
74 | // parsing | |
75 | icapReply = new HttpReply; | |
76 | icapReply->protoPrefix = "ICAP/"; // TODO: make an IcapReply class? | |
77 | ||
78 | // XXX: make sure stop() cleans all buffers | |
79 | } | |
80 | ||
81 | // HTTP side starts sending virgin data | |
82 | void ICAPModXact::noteSourceStart(MsgPipe *p) | |
83 | { | |
84 | ICAPXaction_Enter(noteSourceStart); | |
85 | ||
86 | // make sure TheBackupLimit is in-sync with the buffer size | |
87 | Must(TheBackupLimit <= static_cast<size_t>(virgin->data->body->max_capacity)); | |
88 | ||
89 | estimateVirginBody(); // before virgin disappears! | |
90 | ||
91 | // it is an ICAP violation to send request to a service w/o known OPTIONS | |
92 | ||
93 | if (service().up()) | |
94 | startWriting(); | |
95 | else | |
96 | waitForService(); | |
97 | ||
98 | // XXX: but this has to be here to catch other errors. Thus, if | |
99 | // commConnectStart in startWriting fails, we may get here | |
100 | //_after_ the object got destroyed. Somebody please fix commConnectStart! | |
101 | ICAPXaction_Exit(); | |
102 | } | |
103 | ||
104 | static | |
105 | void ICAPModXact_noteServiceReady(void *data, ICAPServiceRep::Pointer &) | |
106 | { | |
107 | ICAPModXact *x = static_cast<ICAPModXact*>(data); | |
108 | assert(x); | |
109 | x->noteServiceReady(); | |
110 | } | |
111 | ||
112 | void ICAPModXact::waitForService() | |
113 | { | |
114 | Must(!state.serviceWaiting); | |
115 | debugs(93, 7, "ICAPModXact will wait for the ICAP service " << status()); | |
116 | state.serviceWaiting = true; | |
117 | service().callWhenReady(&ICAPModXact_noteServiceReady, this); | |
118 | } | |
119 | ||
120 | void ICAPModXact::noteServiceReady() | |
121 | { | |
122 | ICAPXaction_Enter(noteServiceReady); | |
123 | ||
124 | Must(state.serviceWaiting); | |
125 | state.serviceWaiting = false; | |
126 | startWriting(); // will throw if service is not up | |
127 | ||
128 | ICAPXaction_Exit(); | |
129 | } | |
130 | ||
131 | void ICAPModXact::startWriting() | |
132 | { | |
133 | Must(service().up()); | |
134 | ||
135 | state.writing = State::writingConnect; | |
136 | openConnection(); | |
137 | // put nothing here as openConnection calls commConnectStart | |
138 | // and that may call us back without waiting for the next select loop | |
139 | } | |
140 | ||
141 | // connection with the ICAP service established | |
142 | void ICAPModXact::handleCommConnected() | |
143 | { | |
144 | Must(state.writing == State::writingConnect); | |
145 | ||
146 | startReading(); // wait for early errors from the ICAP server | |
147 | ||
148 | MemBuf requestBuf; | |
149 | requestBuf.init(); | |
150 | ||
151 | makeRequestHeaders(requestBuf); | |
152 | debugs(93, 9, "ICAPModXact ICAP request prefix " << status() << ":\n" << | |
153 | (requestBuf.terminate(), requestBuf.content())); | |
154 | ||
155 | // write headers | |
156 | state.writing = State::writingHeaders; | |
157 | scheduleWrite(requestBuf); | |
158 | } | |
159 | ||
160 | void ICAPModXact::handleCommWrote(size_t) | |
161 | { | |
162 | if (state.writing == State::writingHeaders) | |
163 | handleCommWroteHeaders(); | |
164 | else | |
165 | handleCommWroteBody(); | |
166 | } | |
167 | ||
168 | void ICAPModXact::handleCommWroteHeaders() | |
169 | { | |
170 | Must(state.writing == State::writingHeaders); | |
171 | ||
172 | if (virginBody.expected()) { | |
173 | state.writing = preview.enabled() ? | |
174 | State::writingPreview : State::writingPrime; | |
175 | virginWriteClaim.protectAll(); | |
176 | writeMore(); | |
177 | } else { | |
178 | stopWriting(); | |
179 | } | |
180 | } | |
181 | ||
182 | void ICAPModXact::writeMore() | |
183 | { | |
184 | if (writer) // already writing something | |
185 | return; | |
186 | ||
187 | switch (state.writing) { | |
188 | ||
189 | case State::writingInit: // waiting for service OPTIONS | |
190 | Must(state.serviceWaiting); | |
191 | ||
192 | case State::writingConnect: // waiting for the connection to establish | |
193 | ||
194 | case State::writingHeaders: // waiting for the headers to be written | |
195 | ||
196 | case State::writingPaused: // waiting for the ICAP server response | |
197 | ||
198 | case State::writingDone: // nothing more to write | |
199 | return; | |
200 | ||
201 | case State::writingPreview: | |
202 | writePriviewBody(); | |
203 | return; | |
204 | ||
205 | case State::writingPrime: | |
206 | writePrimeBody(); | |
207 | return; | |
208 | ||
209 | default: | |
210 | throw TexcHere("ICAPModXact in bad writing state"); | |
211 | } | |
212 | } | |
213 | ||
214 | void ICAPModXact::writePriviewBody() | |
215 | { | |
216 | debugs(93, 8, "ICAPModXact will write Preview body " << status()); | |
217 | Must(state.writing == State::writingPreview); | |
218 | ||
219 | MsgPipeData::Body *body = virgin->data->body; | |
220 | const size_t size = XMIN(preview.debt(), (size_t)body->contentSize()); | |
221 | writeSomeBody("preview body", size); | |
222 | ||
223 | // change state once preview is written | |
224 | ||
225 | if (preview.done()) { | |
226 | debugs(93, 7, "ICAPModXact wrote entire Preview body " << status()); | |
227 | ||
228 | if (preview.ieof()) | |
229 | stopWriting(); | |
230 | else | |
231 | state.writing = State::writingPaused; | |
232 | } | |
233 | } | |
234 | ||
235 | void ICAPModXact::writePrimeBody() | |
236 | { | |
237 | Must(state.writing == State::writingPrime); | |
238 | Must(virginWriteClaim.active()); | |
239 | ||
240 | MsgPipeData::Body *body = virgin->data->body; | |
241 | const size_t size = body->contentSize(); | |
242 | writeSomeBody("prime virgin body", size); | |
243 | ||
244 | if (state.doneReceiving) | |
245 | stopWriting(); | |
246 | } | |
247 | ||
248 | void ICAPModXact::writeSomeBody(const char *label, size_t size) | |
249 | { | |
250 | Must(!writer && !state.doneWriting()); | |
251 | debugs(93, 8, "ICAPModXact will write up to " << size << " bytes of " << | |
252 | label); | |
253 | ||
254 | MemBuf writeBuf; // TODO: suggest a min size based on size and lastChunk | |
255 | ||
256 | writeBuf.init(); // note: we assume that last-chunk will fit | |
257 | ||
258 | const size_t writeableSize = claimSize(virginWriteClaim); | |
259 | const size_t chunkSize = XMIN(writeableSize, size); | |
260 | ||
261 | if (chunkSize) { | |
262 | debugs(93, 7, "ICAPModXact will write " << chunkSize << | |
263 | "-byte chunk of " << label); | |
264 | } else { | |
265 | debugs(93, 7, "ICAPModXact has no writeable " << label << " content"); | |
266 | } | |
267 | ||
268 | moveRequestChunk(writeBuf, chunkSize); | |
269 | ||
270 | const bool lastChunk = | |
271 | (state.writing == State::writingPreview && preview.done()) || | |
272 | (state.doneReceiving && claimSize(virginWriteClaim) <= 0); | |
273 | ||
274 | if (lastChunk && virginBody.expected()) { | |
275 | debugs(93, 8, "ICAPModXact will write last-chunk of " << label); | |
276 | addLastRequestChunk(writeBuf); | |
277 | } | |
278 | ||
279 | debugs(93, 7, "ICAPModXact will write " << writeBuf.contentSize() | |
280 | << " raw bytes of " << label); | |
281 | ||
282 | if (writeBuf.hasContent()) { | |
283 | scheduleWrite(writeBuf); // comm will free the chunk | |
284 | } else { | |
285 | writeBuf.clean(); | |
286 | } | |
287 | } | |
288 | ||
289 | void ICAPModXact::moveRequestChunk(MemBuf &buf, size_t chunkSize) | |
290 | { | |
291 | if (chunkSize > 0) { | |
292 | openChunk(buf, chunkSize); | |
293 | buf.append(claimContent(virginWriteClaim), chunkSize); | |
294 | closeChunk(buf, false); | |
295 | ||
296 | virginWriteClaim.release(chunkSize); | |
297 | virginConsume(); | |
298 | } | |
299 | ||
300 | if (state.writing == State::writingPreview) | |
301 | preview.wrote(chunkSize, state.doneReceiving); // even if wrote nothing | |
302 | } | |
303 | ||
304 | void ICAPModXact::addLastRequestChunk(MemBuf &buf) | |
305 | { | |
306 | openChunk(buf, 0); | |
307 | closeChunk(buf, state.writing == State::writingPreview && preview.ieof()); | |
308 | } | |
309 | ||
310 | void ICAPModXact::openChunk(MemBuf &buf, size_t chunkSize) | |
311 | { | |
1dd6edf2 | 312 | buf.Printf("%x\r\n", (int) chunkSize); |
774c051c | 313 | } |
314 | ||
315 | void ICAPModXact::closeChunk(MemBuf &buf, bool ieof) | |
316 | { | |
317 | if (ieof) | |
318 | buf.append("; ieof", 6); | |
319 | ||
320 | buf.append(ICAP::crlf, 2); // chunk-terminating CRLF | |
321 | } | |
322 | ||
323 | size_t ICAPModXact::claimSize(const MemBufClaim &claim) const | |
324 | { | |
325 | Must(claim.active()); | |
326 | const size_t start = claim.offset(); | |
327 | const size_t end = virginConsumed + virgin->data->body->contentSize(); | |
328 | Must(virginConsumed <= start && start <= end); | |
329 | return end - start; | |
330 | } | |
331 | ||
332 | const char *ICAPModXact::claimContent(const MemBufClaim &claim) const | |
333 | { | |
334 | Must(claim.active()); | |
335 | const size_t start = claim.offset(); | |
336 | Must(virginConsumed <= start); | |
337 | return virgin->data->body->content() + (start - virginConsumed); | |
338 | } | |
339 | ||
340 | void ICAPModXact::virginConsume() | |
341 | { | |
342 | MemBuf &buf = *virgin->data->body; | |
343 | const size_t have = static_cast<size_t>(buf.contentSize()); | |
344 | const size_t end = virginConsumed + have; | |
345 | size_t offset = end; | |
346 | ||
347 | if (virginWriteClaim.active()) | |
348 | offset = XMIN(virginWriteClaim.offset(), offset); | |
349 | ||
350 | if (virginSendClaim.active()) | |
351 | offset = XMIN(virginSendClaim.offset(), offset); | |
352 | ||
353 | Must(virginConsumed <= offset && offset <= end); | |
354 | ||
355 | if (const size_t size = offset - virginConsumed) { | |
356 | debugs(93, 8, "ICAPModXact consumes " << size << " out of " << have << | |
357 | " virgin body bytes"); | |
358 | buf.consume(size); | |
359 | virginConsumed += size; | |
360 | ||
361 | if (!state.doneReceiving) | |
362 | virgin->sendSinkNeed(); | |
363 | } | |
364 | } | |
365 | ||
366 | void ICAPModXact::handleCommWroteBody() | |
367 | { | |
368 | writeMore(); | |
369 | } | |
370 | ||
371 | void ICAPModXact::stopWriting() | |
372 | { | |
373 | if (state.writing == State::writingDone) | |
374 | return; | |
375 | ||
376 | debugs(93, 7, "ICAPModXact will no longer write " << status()); | |
377 | ||
378 | state.writing = State::writingDone; | |
379 | ||
380 | virginWriteClaim.disable(); | |
381 | ||
382 | virginConsume(); | |
383 | ||
384 | // Comm does not have an interface to clear the writer, but | |
385 | // writeMore() will not write if our write callback is called | |
386 | // when state.writing == State::writingDone; | |
387 | } | |
388 | ||
389 | void ICAPModXact::stopBackup() | |
390 | { | |
391 | if (!virginSendClaim.active()) | |
392 | return; | |
393 | ||
394 | debugs(93, 7, "ICAPModXact will no longer backup " << status()); | |
395 | ||
396 | virginSendClaim.disable(); | |
397 | ||
398 | virginConsume(); | |
399 | } | |
400 | ||
401 | bool ICAPModXact::doneAll() const | |
402 | { | |
403 | return ICAPXaction::doneAll() && !state.serviceWaiting && | |
404 | state.doneReceiving && doneSending() && | |
405 | doneReading() && state.doneWriting(); | |
406 | } | |
407 | ||
408 | void ICAPModXact::startReading() | |
409 | { | |
410 | Must(connection >= 0); | |
411 | Must(!reader); | |
412 | Must(adapted.getRaw()); | |
413 | Must(adapted->data); | |
414 | Must(adapted->data->body); | |
415 | ||
416 | // we use the same buffer for headers and body and then consume headers | |
417 | readMore(); | |
418 | } | |
419 | ||
420 | void ICAPModXact::readMore() | |
421 | { | |
422 | if (reader || doneReading()) | |
423 | return; | |
424 | ||
425 | // do not fill readBuf if we have no space to store the result | |
426 | if (!adapted->data->body->hasPotentialSpace()) | |
427 | return; | |
428 | ||
429 | if (readBuf.hasSpace()) | |
430 | scheduleRead(); | |
431 | } | |
432 | ||
433 | // comm module read a portion of the ICAP response for us | |
434 | void ICAPModXact::handleCommRead(size_t) | |
435 | { | |
436 | Must(!state.doneParsing()); | |
437 | parseMore(); | |
438 | readMore(); | |
439 | } | |
440 | ||
441 | void ICAPModXact::echoMore() | |
442 | { | |
443 | Must(state.sending == State::sendingVirgin); | |
444 | Must(virginSendClaim.active()); | |
445 | ||
446 | MemBuf &from = *virgin->data->body; | |
447 | MemBuf &to = *adapted->data->body; | |
448 | ||
449 | const size_t sizeMax = claimSize(virginSendClaim); | |
450 | const size_t size = XMIN(static_cast<size_t>(to.potentialSpaceSize()), | |
451 | sizeMax); | |
452 | debugs(93, 5, "ICAPModXact echos " << size << " out of " << sizeMax << | |
453 | " bytes"); | |
454 | ||
455 | if (size > 0) { | |
456 | to.append(claimContent(virginSendClaim), size); | |
457 | virginSendClaim.release(size); | |
458 | virginConsume(); | |
459 | adapted->sendSourceProgress(); | |
460 | } | |
461 | ||
462 | if (!from.hasContent() && state.doneReceiving) { | |
463 | debugs(93, 5, "ICAPModXact echoed all " << status()); | |
464 | stopSending(true); | |
465 | } else { | |
466 | debugs(93, 5, "ICAPModXact has " << from.contentSize() << " bytes " << | |
467 | "and expects more to echo " << status()); | |
468 | virgin->sendSinkNeed(); // TODO: timeout if sink is broken | |
469 | } | |
470 | } | |
471 | ||
472 | bool ICAPModXact::doneSending() const | |
473 | { | |
474 | Must((state.sending == State::sendingDone) == (!adapted)); | |
475 | return state.sending == State::sendingDone; | |
476 | } | |
477 | ||
478 | void ICAPModXact::stopSending(bool nicely) | |
479 | { | |
480 | if (doneSending()) | |
481 | return; | |
482 | ||
483 | if (state.sending != State::sendingUndecided) { | |
484 | debugs(93, 7, "ICAPModXact will no longer send " << status()); | |
485 | ||
486 | if (nicely) | |
487 | adapted->sendSourceFinish(); | |
488 | else | |
489 | adapted->sendSourceAbort(); | |
490 | } else { | |
491 | debugs(93, 7, "ICAPModXact will not start sending " << status()); | |
492 | adapted->sendSourceAbort(); // or the sink may wait forever | |
493 | } | |
494 | ||
495 | state.sending = State::sendingDone; | |
496 | ||
497 | /* | |
498 | * Note on adapted->data->header: we created the header, but allow the | |
bab98cf3 | 499 | * other side (ICAPClientRespmodPrecache) to take control of it. We won't touch it here |
774c051c | 500 | * and instead rely on the Anchor-side to make sure it is properly freed. |
501 | */ | |
502 | adapted = NULL; // refcounted | |
503 | } | |
504 | ||
505 | void ICAPModXact::stopReceiving() | |
506 | { | |
507 | // stopSending NULLifies adapted but we do not NULLify virgin. | |
508 | // This is assymetric because we want to keep virgin->data even | |
509 | // though we are not expecting any more virgin->data->body. | |
510 | // TODO: can we cache just the needed headers info instead? | |
511 | ||
512 | // If they closed first, there is not point (or means) to notify them. | |
513 | ||
514 | if (state.doneReceiving) | |
515 | return; | |
516 | ||
517 | // There is no sendSinkFinished() to notify the other side. | |
518 | debugs(93, 7, "ICAPModXact will not receive " << status()); | |
519 | ||
520 | state.doneReceiving = true; | |
521 | } | |
522 | ||
523 | void ICAPModXact::parseMore() | |
524 | { | |
aa761e5f | 525 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " bytes to parse" << |
774c051c | 526 | status()); |
527 | ||
528 | if (state.parsingHeaders()) | |
529 | parseHeaders(); | |
530 | ||
531 | if (state.parsing == State::psBody) | |
532 | parseBody(); | |
533 | } | |
534 | ||
535 | // note that allocation for echoing is done in handle204NoContent() | |
536 | void ICAPModXact::maybeAllocateHttpMsg() | |
537 | { | |
538 | if (adapted->data->header) // already allocated | |
539 | return; | |
540 | ||
541 | if (gotEncapsulated("res-hdr")) { | |
542 | adapted->data->header = new HttpReply; | |
543 | } else if (gotEncapsulated("req-hdr")) { | |
544 | adapted->data->header = new HttpRequest; | |
545 | } else | |
546 | throw TexcHere("Neither res-hdr nor req-hdr in maybeAllocateHttpMsg()"); | |
547 | } | |
548 | ||
549 | void ICAPModXact::parseHeaders() | |
550 | { | |
551 | Must(state.parsingHeaders()); | |
552 | ||
553 | if (state.parsing == State::psIcapHeader) | |
554 | parseIcapHead(); | |
555 | ||
556 | if (state.parsing == State::psHttpHeader) | |
557 | parseHttpHead(); | |
558 | ||
559 | if (state.parsingHeaders()) { // need more data | |
560 | Must(mayReadMore()); | |
561 | return; | |
562 | } | |
563 | ||
564 | adapted->sendSourceStart(); | |
565 | ||
566 | if (state.sending == State::sendingVirgin) | |
567 | echoMore(); | |
568 | } | |
569 | ||
570 | void ICAPModXact::parseIcapHead() | |
571 | { | |
572 | Must(state.sending == State::sendingUndecided); | |
573 | ||
574 | if (!parseHead(icapReply)) | |
575 | return; | |
576 | ||
fc764d26 | 577 | if (httpHeaderHasConnDir(&icapReply->header, "close")) { |
578 | debugs(93, 5, HERE << "found connection close"); | |
579 | reuseConnection = false; | |
580 | } | |
581 | ||
774c051c | 582 | switch (icapReply->sline.status) { |
583 | ||
584 | case 100: | |
585 | handle100Continue(); | |
586 | break; | |
587 | ||
588 | case 200: | |
589 | handle200Ok(); | |
590 | break; | |
591 | ||
592 | case 204: | |
593 | handle204NoContent(); | |
594 | break; | |
595 | ||
596 | default: | |
597 | handleUnknownScode(); | |
598 | break; | |
599 | } | |
600 | ||
601 | // handle100Continue() manages state.writing on its own. | |
602 | // Non-100 status means the server needs no postPreview data from us. | |
603 | if (state.writing == State::writingPaused) | |
604 | stopWriting(); | |
605 | ||
606 | // TODO: Consider applying a Squid 2.5 patch to recognize 201 responses | |
607 | } | |
608 | ||
609 | void ICAPModXact::handle100Continue() | |
610 | { | |
611 | Must(state.writing == State::writingPaused); | |
612 | Must(preview.enabled() && preview.done() && !preview.ieof()); | |
613 | Must(virginSendClaim.active()); | |
614 | ||
615 | if (virginSendClaim.limited()) // preview only | |
616 | stopBackup(); | |
617 | ||
618 | state.parsing = State::psHttpHeader; // eventually | |
619 | ||
620 | state.writing = State::writingPrime; | |
621 | ||
622 | writeMore(); | |
623 | } | |
624 | ||
625 | void ICAPModXact::handle200Ok() | |
626 | { | |
627 | state.parsing = State::psHttpHeader; | |
628 | state.sending = State::sendingAdapted; | |
629 | stopBackup(); | |
630 | } | |
631 | ||
632 | void ICAPModXact::handle204NoContent() | |
633 | { | |
634 | stopParsing(); | |
635 | Must(virginSendClaim.active()); | |
636 | virginSendClaim.protectAll(); // extends protection if needed | |
637 | state.sending = State::sendingVirgin; | |
638 | ||
639 | // We want to clone the HTTP message, but we do not want | |
640 | // to copy non-HTTP state parts that HttpMsg kids carry in them. | |
641 | // Thus, we cannot use a smart pointer, copy constructor, or equivalent. | |
642 | // Instead, we simply write the HTTP message and "clone" it by parsing. | |
643 | ||
644 | HttpMsg *oldHead = virgin->data->header; | |
645 | debugs(93, 7, "ICAPModXact cloning virgin message " << oldHead); | |
646 | ||
647 | MemBuf httpBuf; | |
648 | ||
649 | // write the virgin message into a memory buffer | |
650 | httpBuf.init(); | |
651 | packHead(httpBuf, oldHead); | |
652 | ||
653 | // allocate the adapted message | |
654 | HttpMsg *&newHead = adapted->data->header; | |
655 | Must(!newHead); | |
656 | ||
657 | if (dynamic_cast<const HttpRequest*>(oldHead)) | |
658 | newHead = new HttpRequest; | |
659 | else | |
660 | if (dynamic_cast<const HttpReply*>(oldHead)) | |
661 | newHead = new HttpReply; | |
662 | ||
663 | Must(newHead); | |
664 | ||
665 | // parse the buffer back | |
666 | http_status error = HTTP_STATUS_NONE; | |
667 | ||
668 | Must(newHead->parse(&httpBuf, true, &error)); | |
669 | ||
670 | Must(newHead->hdr_sz == httpBuf.contentSize()); // no leftovers | |
671 | ||
672 | httpBuf.clean(); | |
673 | ||
674 | debugs(93, 7, "ICAPModXact cloned virgin message " << oldHead << " to " << newHead); | |
675 | } | |
676 | ||
677 | void ICAPModXact::handleUnknownScode() | |
678 | { | |
679 | stopParsing(); | |
680 | stopBackup(); | |
681 | // TODO: mark connection as "bad" | |
682 | ||
683 | // Terminate the transaction; we do not know how to handle this response. | |
684 | throw TexcHere("Unsupported ICAP status code"); | |
685 | } | |
686 | ||
687 | void ICAPModXact::parseHttpHead() | |
688 | { | |
689 | if (gotEncapsulated("res-hdr") || gotEncapsulated("req-hdr")) { | |
690 | maybeAllocateHttpMsg(); | |
691 | ||
692 | if (!parseHead(adapted->data->header)) | |
200ac359 | 693 | return; // need more header data |
774c051c | 694 | } |
695 | ||
696 | state.parsing = State::psBody; | |
697 | } | |
698 | ||
fc764d26 | 699 | /* |
700 | * Common routine used to parse both HTTP and ICAP headers | |
701 | */ | |
774c051c | 702 | bool ICAPModXact::parseHead(HttpMsg *head) |
703 | { | |
704 | assert(head); | |
def17b6a | 705 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " head bytes to parse" << |
774c051c | 706 | "; state: " << state.parsing); |
707 | ||
708 | http_status error = HTTP_STATUS_NONE; | |
709 | const bool parsed = head->parse(&readBuf, commEof, &error); | |
710 | Must(parsed || !error); // success or need more data | |
711 | ||
712 | if (!parsed) { // need more data | |
713 | head->reset(); | |
714 | return false; | |
715 | } | |
716 | ||
717 | readBuf.consume(head->hdr_sz); | |
718 | return true; | |
719 | } | |
720 | ||
721 | void ICAPModXact::parseBody() | |
722 | { | |
723 | Must(state.parsing == State::psBody); | |
724 | ||
aa761e5f | 725 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " body bytes to parse"); |
774c051c | 726 | |
200ac359 | 727 | if (gotEncapsulated("res-body") || gotEncapsulated("req-body")) { |
774c051c | 728 | if (!parsePresentBody()) // need more body data |
729 | return; | |
730 | } else { | |
731 | debugs(93, 5, "not expecting a body"); | |
732 | } | |
733 | ||
734 | stopParsing(); | |
735 | stopSending(true); | |
736 | } | |
737 | ||
738 | // returns true iff complete body was parsed | |
739 | bool ICAPModXact::parsePresentBody() | |
740 | { | |
741 | if (!bodyParser) | |
742 | bodyParser = new ChunkedCodingParser; | |
743 | ||
744 | // the parser will throw on errors | |
745 | const bool parsed = bodyParser->parse(&readBuf, adapted->data->body); | |
746 | ||
747 | adapted->sendSourceProgress(); // TODO: do not send if parsed nothing | |
748 | ||
aa761e5f | 749 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " body bytes after " << |
774c051c | 750 | "parse; parsed all: " << parsed); |
751 | ||
752 | if (parsed) | |
753 | return true; | |
754 | ||
755 | if (bodyParser->needsMoreData()) | |
756 | Must(mayReadMore()); | |
757 | ||
758 | if (bodyParser->needsMoreSpace()) { | |
759 | Must(!doneSending()); // can hope for more space | |
760 | Must(adapted->data->body->hasContent()); // paranoid | |
761 | // TODO: there should be a timeout in case the sink is broken. | |
762 | } | |
763 | ||
764 | return false; | |
765 | } | |
766 | ||
767 | void ICAPModXact::stopParsing() | |
768 | { | |
769 | if (state.parsing == State::psDone) | |
770 | return; | |
771 | ||
772 | debugs(93, 7, "ICAPModXact will no longer parse " << status()); | |
773 | ||
774 | delete bodyParser; | |
775 | ||
776 | bodyParser = NULL; | |
777 | ||
778 | state.parsing = State::psDone; | |
779 | } | |
780 | ||
781 | // HTTP side added virgin body data | |
782 | void ICAPModXact::noteSourceProgress(MsgPipe *p) | |
783 | { | |
784 | ICAPXaction_Enter(noteSourceProgress); | |
785 | ||
786 | Must(!state.doneReceiving); | |
787 | writeMore(); | |
788 | ||
789 | if (state.sending == State::sendingVirgin) | |
790 | echoMore(); | |
791 | ||
792 | ICAPXaction_Exit(); | |
793 | } | |
794 | ||
795 | // HTTP side sent us all virgin info | |
796 | void ICAPModXact::noteSourceFinish(MsgPipe *p) | |
797 | { | |
798 | ICAPXaction_Enter(noteSourceFinish); | |
799 | ||
800 | Must(!state.doneReceiving); | |
801 | stopReceiving(); | |
802 | ||
803 | // push writer and sender in case we were waiting for the last-chunk | |
804 | writeMore(); | |
805 | ||
806 | if (state.sending == State::sendingVirgin) | |
807 | echoMore(); | |
808 | ||
809 | ICAPXaction_Exit(); | |
810 | } | |
811 | ||
812 | // HTTP side is aborting | |
813 | void ICAPModXact::noteSourceAbort(MsgPipe *p) | |
814 | { | |
815 | ICAPXaction_Enter(noteSourceAbort); | |
816 | ||
817 | Must(!state.doneReceiving); | |
818 | stopReceiving(); | |
819 | mustStop("HTTP source quit"); | |
820 | ||
821 | ICAPXaction_Exit(); | |
822 | } | |
823 | ||
824 | // HTTP side wants more adapted data and possibly freed some buffer space | |
825 | void ICAPModXact::noteSinkNeed(MsgPipe *p) | |
826 | { | |
827 | ICAPXaction_Enter(noteSinkNeed); | |
828 | ||
829 | if (state.sending == State::sendingVirgin) | |
830 | echoMore(); | |
831 | else | |
832 | if (state.sending == State::sendingAdapted) | |
833 | parseMore(); | |
834 | else | |
835 | Must(state.sending == State::sendingUndecided); | |
836 | ||
837 | ICAPXaction_Exit(); | |
838 | } | |
839 | ||
840 | // HTTP side aborted | |
841 | void ICAPModXact::noteSinkAbort(MsgPipe *p) | |
842 | { | |
843 | ICAPXaction_Enter(noteSinkAbort); | |
844 | ||
845 | mustStop("HTTP sink quit"); | |
846 | ||
847 | ICAPXaction_Exit(); | |
848 | } | |
849 | ||
850 | // internal cleanup | |
851 | void ICAPModXact::doStop() | |
852 | { | |
853 | ICAPXaction::doStop(); | |
854 | ||
855 | stopWriting(); | |
856 | stopBackup(); | |
857 | ||
858 | if (icapReply) { | |
859 | delete icapReply; | |
860 | icapReply = NULL; | |
861 | } | |
862 | ||
863 | stopSending(false); | |
864 | ||
865 | // see stopReceiving() for reasons it cannot NULLify virgin there | |
866 | ||
867 | if (virgin != NULL) { | |
868 | if (!state.doneReceiving) | |
869 | virgin->sendSinkAbort(); | |
870 | else | |
871 | virgin->sink = NULL; | |
872 | ||
873 | virgin = NULL; // refcounted | |
874 | } | |
875 | ||
876 | if (self != NULL) { | |
877 | Pointer s = self; | |
878 | self = NULL; | |
879 | ICAPNoteXactionDone(s); | |
880 | /* this object may be destroyed when 's' is cleared */ | |
881 | } | |
882 | } | |
883 | ||
884 | void ICAPModXact::makeRequestHeaders(MemBuf &buf) | |
885 | { | |
12b91c99 | 886 | /* |
887 | * XXX These should use HttpHdr interfaces instead of Printfs | |
888 | */ | |
774c051c | 889 | const ICAPServiceRep &s = service(); |
890 | buf.Printf("%s %s ICAP/1.0\r\n", s.methodStr(), s.uri.buf()); | |
891 | buf.Printf("Host: %s:%d\r\n", s.host.buf(), s.port); | |
12b91c99 | 892 | buf.Printf("Date: %s\r\n", mkrfc1123(squid_curtime)); |
893 | ||
894 | if (!TheICAPConfig.reuse_connections) | |
895 | buf.Printf("Connection: close\r\n"); | |
896 | ||
774c051c | 897 | buf.Printf("Encapsulated: "); |
898 | ||
899 | MemBuf httpBuf; | |
12b91c99 | 900 | |
774c051c | 901 | httpBuf.init(); |
902 | ||
903 | // build HTTP request header, if any | |
904 | ICAP::Method m = s.method; | |
905 | ||
906 | if (ICAP::methodRespmod == m && virgin->data->cause) | |
907 | encapsulateHead(buf, "req-hdr", httpBuf, virgin->data->cause); | |
908 | else if (ICAP::methodReqmod == m) | |
909 | encapsulateHead(buf, "req-hdr", httpBuf, virgin->data->header); | |
910 | ||
911 | if (ICAP::methodRespmod == m) | |
912 | if (const MsgPipeData::Header *prime = virgin->data->header) | |
913 | encapsulateHead(buf, "res-hdr", httpBuf, prime); | |
914 | ||
915 | if (!virginBody.expected()) | |
1dd6edf2 | 916 | buf.Printf("null-body=%d", (int) httpBuf.contentSize()); |
774c051c | 917 | else if (ICAP::methodReqmod == m) |
1dd6edf2 | 918 | buf.Printf("req-body=%d", (int) httpBuf.contentSize()); |
774c051c | 919 | else |
1dd6edf2 | 920 | buf.Printf("res-body=%d", (int) httpBuf.contentSize()); |
774c051c | 921 | |
922 | buf.append(ICAP::crlf, 2); // terminate Encapsulated line | |
923 | ||
924 | if (shouldPreview()) { | |
925 | buf.Printf("Preview: %d\r\n", (int)preview.ad()); | |
926 | virginSendClaim.protectUpTo(preview.ad()); | |
927 | } | |
928 | ||
929 | if (shouldAllow204()) { | |
930 | buf.Printf("Allow: 204\r\n"); | |
931 | // be robust: do not rely on the expected body size | |
932 | virginSendClaim.protectAll(); | |
933 | } | |
934 | ||
a97e82a8 | 935 | const HttpRequest *request = virgin->data->cause ? |
936 | virgin->data->cause : | |
937 | dynamic_cast<const HttpRequest*>(virgin->data->header); | |
938 | ||
12b91c99 | 939 | if (TheICAPConfig.send_client_ip) |
940 | if (request->client_addr.s_addr != any_addr.s_addr) | |
941 | buf.Printf("X-Client-IP: %s\r\n", inet_ntoa(request->client_addr)); | |
a97e82a8 | 942 | |
12b91c99 | 943 | if (TheICAPConfig.send_client_username) |
944 | if (request->auth_user_request) | |
945 | if (request->auth_user_request->username()) | |
946 | buf.Printf("X-Client-Username: %s\r\n", request->auth_user_request->username()); | |
a97e82a8 | 947 | |
2dfede9e | 948 | // fprintf(stderr, "%s\n", buf.content()); |
a97e82a8 | 949 | |
774c051c | 950 | buf.append(ICAP::crlf, 2); // terminate ICAP header |
951 | ||
952 | // start ICAP request body with encapsulated HTTP headers | |
953 | buf.append(httpBuf.content(), httpBuf.contentSize()); | |
954 | ||
955 | httpBuf.clean(); | |
956 | } | |
957 | ||
958 | void ICAPModXact::encapsulateHead(MemBuf &icapBuf, const char *section, MemBuf &httpBuf, const HttpMsg *head) | |
959 | { | |
960 | // update ICAP header | |
7cab7e9f | 961 | icapBuf.Printf("%s=%d, ", section, (int) httpBuf.contentSize()); |
774c051c | 962 | |
963 | // pack HTTP head | |
964 | packHead(httpBuf, head); | |
965 | } | |
966 | ||
967 | void ICAPModXact::packHead(MemBuf &httpBuf, const HttpMsg *head) | |
968 | { | |
969 | Packer p; | |
970 | packerToMemInit(&p, &httpBuf); | |
971 | head->packInto(&p, true); | |
972 | packerClean(&p); | |
973 | } | |
974 | ||
975 | // decides whether to offer a preview and calculates its size | |
976 | bool ICAPModXact::shouldPreview() | |
977 | { | |
978 | size_t wantedSize; | |
979 | ||
980 | if (!service().wantsPreview(wantedSize)) { | |
981 | debugs(93, 5, "ICAPModXact should not offer preview"); | |
982 | return false; | |
983 | } | |
984 | ||
985 | Must(wantedSize >= 0); | |
986 | ||
987 | // cannot preview more than we can backup | |
988 | size_t ad = XMIN(wantedSize, TheBackupLimit); | |
989 | ||
990 | if (virginBody.expected() && virginBody.knownSize()) | |
991 | ad = XMIN(ad, virginBody.size()); // not more than we have | |
992 | else | |
993 | ad = 0; // questionable optimization? | |
994 | ||
995 | debugs(93, 5, "ICAPModXact should offer " << ad << "-byte preview " << | |
996 | "(service wanted " << wantedSize << ")"); | |
997 | ||
998 | preview.enable(ad); | |
999 | ||
1000 | return preview.enabled(); | |
1001 | } | |
1002 | ||
1003 | // decides whether to allow 204 responses | |
1004 | bool ICAPModXact::shouldAllow204() | |
1005 | { | |
1006 | if (!service().allows204()) | |
1007 | return false; | |
1008 | ||
1009 | if (!virginBody.expected()) | |
1010 | return true; // no body means no problems with supporting 204s. | |
1011 | ||
1012 | // if there is a body, make sure we can backup it all | |
1013 | ||
1014 | if (!virginBody.knownSize()) | |
1015 | return false; | |
1016 | ||
1017 | // or should we have a different backup limit? | |
1018 | // note that '<' allows for 0-termination of the "full" backup buffer | |
1019 | return virginBody.size() < TheBackupLimit; | |
1020 | } | |
1021 | ||
1022 | // returns a temporary string depicting transaction status, for debugging | |
1023 | void ICAPModXact::fillPendingStatus(MemBuf &buf) const | |
1024 | { | |
1025 | if (state.serviceWaiting) | |
1026 | buf.append("U", 1); | |
1027 | ||
1028 | if (!state.doneWriting() && state.writing != State::writingInit) | |
1029 | buf.Printf("w(%d)", state.writing); | |
1030 | ||
1031 | if (preview.enabled()) { | |
1032 | if (!preview.done()) | |
1dd6edf2 | 1033 | buf.Printf("P(%d)", (int) preview.debt()); |
774c051c | 1034 | } |
1035 | ||
1036 | if (virginSendClaim.active()) | |
1037 | buf.append("B", 1); | |
1038 | ||
1039 | if (!state.doneParsing() && state.parsing != State::psIcapHeader) | |
1040 | buf.Printf("p(%d)", state.parsing); | |
1041 | ||
1042 | if (!doneSending() && state.sending != State::sendingUndecided) | |
1043 | buf.Printf("S(%d)", state.sending); | |
1044 | } | |
1045 | ||
1046 | void ICAPModXact::fillDoneStatus(MemBuf &buf) const | |
1047 | { | |
1048 | if (state.doneReceiving) | |
1049 | buf.append("R", 1); | |
1050 | ||
1051 | if (state.doneWriting()) | |
1052 | buf.append("w", 1); | |
1053 | ||
1054 | if (preview.enabled()) { | |
1055 | if (preview.done()) | |
1056 | buf.Printf("P%s", preview.ieof() ? "(ieof)" : ""); | |
1057 | } | |
1058 | ||
1059 | if (doneReading()) | |
1060 | buf.append("r", 1); | |
1061 | ||
1062 | if (state.doneParsing()) | |
1063 | buf.append("p", 1); | |
1064 | ||
1065 | if (doneSending()) | |
1066 | buf.append("S", 1); | |
1067 | } | |
1068 | ||
1069 | bool ICAPModXact::gotEncapsulated(const char *section) const | |
1070 | { | |
1071 | return httpHeaderGetByNameListMember(&icapReply->header, "Encapsulated", | |
1072 | section, ',').size() > 0; | |
1073 | } | |
1074 | ||
1075 | // calculate whether there is a virgin HTTP body and | |
1076 | // whether its expected size is known | |
1077 | void ICAPModXact::estimateVirginBody() | |
1078 | { | |
1079 | // note: defaults should be fine but will disable previews and 204s | |
1080 | ||
1081 | Must(virgin != NULL && virgin->data->header); | |
1082 | ||
1083 | method_t method; | |
1084 | ||
1085 | if (virgin->data->cause) | |
1086 | method = virgin->data->cause->method; | |
1087 | else | |
a97e82a8 | 1088 | if (HttpRequest *req = dynamic_cast<HttpRequest*>(virgin->data-> |
1089 | header)) | |
774c051c | 1090 | method = req->method; |
1091 | else | |
1092 | return; | |
1093 | ||
1094 | ssize_t size; | |
1095 | if (virgin->data->header->expectingBody(method, size)) { | |
1096 | virginBody.expect(size) | |
1097 | ; | |
1098 | debugs(93, 6, "ICAPModXact expects virgin body; size: " << size); | |
1099 | } else { | |
1100 | debugs(93, 6, "ICAPModXact does not expect virgin body"); | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | ||
1105 | // TODO: Move SizedEstimate, MemBufBackup, and ICAPPreview elsewhere | |
1106 | ||
1107 | SizedEstimate::SizedEstimate() | |
1108 | : theData(dtUnexpected) | |
1109 | {} | |
1110 | ||
1111 | void SizedEstimate::expect(ssize_t aSize) | |
1112 | { | |
1113 | theData = (aSize >= 0) ? aSize : (ssize_t)dtUnknown; | |
1114 | } | |
1115 | ||
1116 | bool SizedEstimate::expected() const | |
1117 | { | |
1118 | return theData != dtUnexpected; | |
1119 | } | |
1120 | ||
1121 | bool SizedEstimate::knownSize() const | |
1122 | { | |
1123 | Must(expected()); | |
1124 | return theData != dtUnknown; | |
1125 | } | |
1126 | ||
1127 | size_t SizedEstimate::size() const | |
1128 | { | |
1129 | Must(knownSize()); | |
1130 | return static_cast<size_t>(theData); | |
1131 | } | |
1132 | ||
1133 | ||
1134 | ||
1135 | MemBufClaim::MemBufClaim(): theStart(-1), theGoal(-1) | |
1136 | {} | |
1137 | ||
1138 | void MemBufClaim::protectAll() | |
1139 | { | |
1140 | if (theStart < 0) | |
1141 | theStart = 0; | |
1142 | ||
1143 | theGoal = -1; // no specific goal | |
1144 | } | |
1145 | ||
1146 | void MemBufClaim::protectUpTo(size_t aGoal) | |
1147 | { | |
1148 | if (theStart < 0) | |
1149 | theStart = 0; | |
1150 | ||
1151 | Must(aGoal >= 0); | |
1152 | ||
1153 | theGoal = (theGoal < 0) ? static_cast<ssize_t>(aGoal) : | |
1154 | XMIN(static_cast<ssize_t>(aGoal), theGoal); | |
1155 | } | |
1156 | ||
1157 | void MemBufClaim::disable() | |
1158 | { | |
1159 | theStart = -1; | |
1160 | } | |
1161 | ||
1162 | void MemBufClaim::release(size_t size) | |
1163 | { | |
1164 | Must(active()); | |
1165 | Must(size >= 0); | |
1166 | theStart += static_cast<ssize_t>(size); | |
1167 | ||
1168 | if (limited() && theStart >= theGoal) | |
1169 | disable(); | |
1170 | } | |
1171 | ||
1172 | size_t MemBufClaim::offset() const | |
1173 | { | |
1174 | Must(active()); | |
1175 | return static_cast<size_t>(theStart); | |
1176 | } | |
1177 | ||
1178 | bool MemBufClaim::limited() const | |
1179 | { | |
1180 | Must(active()); | |
1181 | return theGoal >= 0; | |
1182 | } | |
1183 | ||
1184 | ||
1185 | ICAPPreview::ICAPPreview(): theWritten(0), theAd(0), theState(stDisabled) | |
1186 | {} | |
1187 | ||
1188 | void ICAPPreview::enable(size_t anAd) | |
1189 | { | |
1190 | // TODO: check for anAd not exceeding preview size limit | |
1191 | Must(anAd >= 0); | |
1192 | Must(!enabled()); | |
1193 | theAd = anAd; | |
1194 | theState = stWriting; | |
1195 | } | |
1196 | ||
1197 | bool ICAPPreview::enabled() const | |
1198 | { | |
1199 | return theState != stDisabled; | |
1200 | } | |
1201 | ||
1202 | size_t ICAPPreview::ad() const | |
1203 | { | |
1204 | Must(enabled()); | |
1205 | return theAd; | |
1206 | } | |
1207 | ||
1208 | bool ICAPPreview::done() const | |
1209 | { | |
1210 | Must(enabled()); | |
1211 | return theState >= stIeof; | |
1212 | } | |
1213 | ||
1214 | bool ICAPPreview::ieof() const | |
1215 | { | |
1216 | Must(enabled()); | |
1217 | return theState == stIeof; | |
1218 | } | |
1219 | ||
1220 | size_t ICAPPreview::debt() const | |
1221 | { | |
1222 | Must(enabled()); | |
1223 | return done() ? 0 : (theAd - theWritten); | |
1224 | } | |
1225 | ||
1226 | void ICAPPreview::wrote(size_t size, bool sawEof) | |
1227 | { | |
1228 | Must(enabled()); | |
1229 | theWritten += size; | |
1230 | ||
1231 | if (theWritten >= theAd) | |
1232 | theState = stDone; // sawEof is irrelevant | |
1233 | else | |
1234 | if (sawEof) | |
1235 | theState = stIeof; | |
1236 | } | |
1237 |