]>
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 | { | |
525 | debugs(93, 5, "have " << readBuf.contentSize() << " bytes to parse" << | |
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 | ||
577 | switch (icapReply->sline.status) { | |
578 | ||
579 | case 100: | |
580 | handle100Continue(); | |
581 | break; | |
582 | ||
583 | case 200: | |
584 | handle200Ok(); | |
585 | break; | |
586 | ||
587 | case 204: | |
588 | handle204NoContent(); | |
589 | break; | |
590 | ||
591 | default: | |
592 | handleUnknownScode(); | |
593 | break; | |
594 | } | |
595 | ||
596 | // handle100Continue() manages state.writing on its own. | |
597 | // Non-100 status means the server needs no postPreview data from us. | |
598 | if (state.writing == State::writingPaused) | |
599 | stopWriting(); | |
600 | ||
601 | // TODO: Consider applying a Squid 2.5 patch to recognize 201 responses | |
602 | } | |
603 | ||
604 | void ICAPModXact::handle100Continue() | |
605 | { | |
606 | Must(state.writing == State::writingPaused); | |
607 | Must(preview.enabled() && preview.done() && !preview.ieof()); | |
608 | Must(virginSendClaim.active()); | |
609 | ||
610 | if (virginSendClaim.limited()) // preview only | |
611 | stopBackup(); | |
612 | ||
613 | state.parsing = State::psHttpHeader; // eventually | |
614 | ||
615 | state.writing = State::writingPrime; | |
616 | ||
617 | writeMore(); | |
618 | } | |
619 | ||
620 | void ICAPModXact::handle200Ok() | |
621 | { | |
622 | state.parsing = State::psHttpHeader; | |
623 | state.sending = State::sendingAdapted; | |
624 | stopBackup(); | |
625 | } | |
626 | ||
627 | void ICAPModXact::handle204NoContent() | |
628 | { | |
629 | stopParsing(); | |
630 | Must(virginSendClaim.active()); | |
631 | virginSendClaim.protectAll(); // extends protection if needed | |
632 | state.sending = State::sendingVirgin; | |
633 | ||
634 | // We want to clone the HTTP message, but we do not want | |
635 | // to copy non-HTTP state parts that HttpMsg kids carry in them. | |
636 | // Thus, we cannot use a smart pointer, copy constructor, or equivalent. | |
637 | // Instead, we simply write the HTTP message and "clone" it by parsing. | |
638 | ||
639 | HttpMsg *oldHead = virgin->data->header; | |
640 | debugs(93, 7, "ICAPModXact cloning virgin message " << oldHead); | |
641 | ||
642 | MemBuf httpBuf; | |
643 | ||
644 | // write the virgin message into a memory buffer | |
645 | httpBuf.init(); | |
646 | packHead(httpBuf, oldHead); | |
647 | ||
648 | // allocate the adapted message | |
649 | HttpMsg *&newHead = adapted->data->header; | |
650 | Must(!newHead); | |
651 | ||
652 | if (dynamic_cast<const HttpRequest*>(oldHead)) | |
653 | newHead = new HttpRequest; | |
654 | else | |
655 | if (dynamic_cast<const HttpReply*>(oldHead)) | |
656 | newHead = new HttpReply; | |
657 | ||
658 | Must(newHead); | |
659 | ||
660 | // parse the buffer back | |
661 | http_status error = HTTP_STATUS_NONE; | |
662 | ||
663 | Must(newHead->parse(&httpBuf, true, &error)); | |
664 | ||
665 | Must(newHead->hdr_sz == httpBuf.contentSize()); // no leftovers | |
666 | ||
667 | httpBuf.clean(); | |
668 | ||
669 | debugs(93, 7, "ICAPModXact cloned virgin message " << oldHead << " to " << newHead); | |
670 | } | |
671 | ||
672 | void ICAPModXact::handleUnknownScode() | |
673 | { | |
674 | stopParsing(); | |
675 | stopBackup(); | |
676 | // TODO: mark connection as "bad" | |
677 | ||
678 | // Terminate the transaction; we do not know how to handle this response. | |
679 | throw TexcHere("Unsupported ICAP status code"); | |
680 | } | |
681 | ||
682 | void ICAPModXact::parseHttpHead() | |
683 | { | |
684 | if (gotEncapsulated("res-hdr") || gotEncapsulated("req-hdr")) { | |
685 | maybeAllocateHttpMsg(); | |
686 | ||
687 | if (!parseHead(adapted->data->header)) | |
688 | return; | |
689 | } | |
690 | ||
691 | state.parsing = State::psBody; | |
692 | } | |
693 | ||
694 | bool ICAPModXact::parseHead(HttpMsg *head) | |
695 | { | |
696 | assert(head); | |
def17b6a | 697 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " head bytes to parse" << |
774c051c | 698 | "; state: " << state.parsing); |
699 | ||
700 | http_status error = HTTP_STATUS_NONE; | |
701 | const bool parsed = head->parse(&readBuf, commEof, &error); | |
702 | Must(parsed || !error); // success or need more data | |
703 | ||
704 | if (!parsed) { // need more data | |
705 | head->reset(); | |
706 | return false; | |
707 | } | |
708 | ||
2dfede9e | 709 | if (httpHeaderHasConnDir(&head->header, "close")) { |
781ce8ff | 710 | debug(93,5)("%s(%d) found connection close\n", __FILE__,__LINE__); |
2dfede9e | 711 | reuseConnection = false; |
712 | } | |
713 | ||
774c051c | 714 | readBuf.consume(head->hdr_sz); |
715 | return true; | |
716 | } | |
717 | ||
718 | void ICAPModXact::parseBody() | |
719 | { | |
720 | Must(state.parsing == State::psBody); | |
721 | ||
722 | debugs(93, 5, "have " << readBuf.contentSize() << " body bytes to parse"); | |
723 | ||
724 | if (gotEncapsulated("res-body")) { | |
725 | if (!parsePresentBody()) // need more body data | |
726 | return; | |
727 | } else { | |
728 | debugs(93, 5, "not expecting a body"); | |
729 | } | |
730 | ||
731 | stopParsing(); | |
732 | stopSending(true); | |
733 | } | |
734 | ||
735 | // returns true iff complete body was parsed | |
736 | bool ICAPModXact::parsePresentBody() | |
737 | { | |
738 | if (!bodyParser) | |
739 | bodyParser = new ChunkedCodingParser; | |
740 | ||
741 | // the parser will throw on errors | |
742 | const bool parsed = bodyParser->parse(&readBuf, adapted->data->body); | |
743 | ||
744 | adapted->sendSourceProgress(); // TODO: do not send if parsed nothing | |
745 | ||
746 | debugs(93, 5, "have " << readBuf.contentSize() << " body bytes after " << | |
747 | "parse; parsed all: " << parsed); | |
748 | ||
749 | if (parsed) | |
750 | return true; | |
751 | ||
752 | if (bodyParser->needsMoreData()) | |
753 | Must(mayReadMore()); | |
754 | ||
755 | if (bodyParser->needsMoreSpace()) { | |
756 | Must(!doneSending()); // can hope for more space | |
757 | Must(adapted->data->body->hasContent()); // paranoid | |
758 | // TODO: there should be a timeout in case the sink is broken. | |
759 | } | |
760 | ||
761 | return false; | |
762 | } | |
763 | ||
764 | void ICAPModXact::stopParsing() | |
765 | { | |
766 | if (state.parsing == State::psDone) | |
767 | return; | |
768 | ||
769 | debugs(93, 7, "ICAPModXact will no longer parse " << status()); | |
770 | ||
771 | delete bodyParser; | |
772 | ||
773 | bodyParser = NULL; | |
774 | ||
775 | state.parsing = State::psDone; | |
776 | } | |
777 | ||
778 | // HTTP side added virgin body data | |
779 | void ICAPModXact::noteSourceProgress(MsgPipe *p) | |
780 | { | |
781 | ICAPXaction_Enter(noteSourceProgress); | |
782 | ||
783 | Must(!state.doneReceiving); | |
784 | writeMore(); | |
785 | ||
786 | if (state.sending == State::sendingVirgin) | |
787 | echoMore(); | |
788 | ||
789 | ICAPXaction_Exit(); | |
790 | } | |
791 | ||
792 | // HTTP side sent us all virgin info | |
793 | void ICAPModXact::noteSourceFinish(MsgPipe *p) | |
794 | { | |
795 | ICAPXaction_Enter(noteSourceFinish); | |
796 | ||
797 | Must(!state.doneReceiving); | |
798 | stopReceiving(); | |
799 | ||
800 | // push writer and sender in case we were waiting for the last-chunk | |
801 | writeMore(); | |
802 | ||
803 | if (state.sending == State::sendingVirgin) | |
804 | echoMore(); | |
805 | ||
806 | ICAPXaction_Exit(); | |
807 | } | |
808 | ||
809 | // HTTP side is aborting | |
810 | void ICAPModXact::noteSourceAbort(MsgPipe *p) | |
811 | { | |
812 | ICAPXaction_Enter(noteSourceAbort); | |
813 | ||
814 | Must(!state.doneReceiving); | |
815 | stopReceiving(); | |
816 | mustStop("HTTP source quit"); | |
817 | ||
818 | ICAPXaction_Exit(); | |
819 | } | |
820 | ||
821 | // HTTP side wants more adapted data and possibly freed some buffer space | |
822 | void ICAPModXact::noteSinkNeed(MsgPipe *p) | |
823 | { | |
824 | ICAPXaction_Enter(noteSinkNeed); | |
825 | ||
826 | if (state.sending == State::sendingVirgin) | |
827 | echoMore(); | |
828 | else | |
829 | if (state.sending == State::sendingAdapted) | |
830 | parseMore(); | |
831 | else | |
832 | Must(state.sending == State::sendingUndecided); | |
833 | ||
834 | ICAPXaction_Exit(); | |
835 | } | |
836 | ||
837 | // HTTP side aborted | |
838 | void ICAPModXact::noteSinkAbort(MsgPipe *p) | |
839 | { | |
840 | ICAPXaction_Enter(noteSinkAbort); | |
841 | ||
842 | mustStop("HTTP sink quit"); | |
843 | ||
844 | ICAPXaction_Exit(); | |
845 | } | |
846 | ||
847 | // internal cleanup | |
848 | void ICAPModXact::doStop() | |
849 | { | |
850 | ICAPXaction::doStop(); | |
851 | ||
852 | stopWriting(); | |
853 | stopBackup(); | |
854 | ||
855 | if (icapReply) { | |
856 | delete icapReply; | |
857 | icapReply = NULL; | |
858 | } | |
859 | ||
860 | stopSending(false); | |
861 | ||
862 | // see stopReceiving() for reasons it cannot NULLify virgin there | |
863 | ||
864 | if (virgin != NULL) { | |
865 | if (!state.doneReceiving) | |
866 | virgin->sendSinkAbort(); | |
867 | else | |
868 | virgin->sink = NULL; | |
869 | ||
870 | virgin = NULL; // refcounted | |
871 | } | |
872 | ||
873 | if (self != NULL) { | |
874 | Pointer s = self; | |
875 | self = NULL; | |
876 | ICAPNoteXactionDone(s); | |
877 | /* this object may be destroyed when 's' is cleared */ | |
878 | } | |
879 | } | |
880 | ||
881 | void ICAPModXact::makeRequestHeaders(MemBuf &buf) | |
882 | { | |
12b91c99 | 883 | /* |
884 | * XXX These should use HttpHdr interfaces instead of Printfs | |
885 | */ | |
774c051c | 886 | const ICAPServiceRep &s = service(); |
887 | buf.Printf("%s %s ICAP/1.0\r\n", s.methodStr(), s.uri.buf()); | |
888 | buf.Printf("Host: %s:%d\r\n", s.host.buf(), s.port); | |
12b91c99 | 889 | buf.Printf("Date: %s\r\n", mkrfc1123(squid_curtime)); |
890 | ||
891 | if (!TheICAPConfig.reuse_connections) | |
892 | buf.Printf("Connection: close\r\n"); | |
893 | ||
774c051c | 894 | buf.Printf("Encapsulated: "); |
895 | ||
896 | MemBuf httpBuf; | |
12b91c99 | 897 | |
774c051c | 898 | httpBuf.init(); |
899 | ||
900 | // build HTTP request header, if any | |
901 | ICAP::Method m = s.method; | |
902 | ||
903 | if (ICAP::methodRespmod == m && virgin->data->cause) | |
904 | encapsulateHead(buf, "req-hdr", httpBuf, virgin->data->cause); | |
905 | else if (ICAP::methodReqmod == m) | |
906 | encapsulateHead(buf, "req-hdr", httpBuf, virgin->data->header); | |
907 | ||
908 | if (ICAP::methodRespmod == m) | |
909 | if (const MsgPipeData::Header *prime = virgin->data->header) | |
910 | encapsulateHead(buf, "res-hdr", httpBuf, prime); | |
911 | ||
912 | if (!virginBody.expected()) | |
1dd6edf2 | 913 | buf.Printf("null-body=%d", (int) httpBuf.contentSize()); |
774c051c | 914 | else if (ICAP::methodReqmod == m) |
1dd6edf2 | 915 | buf.Printf("req-body=%d", (int) httpBuf.contentSize()); |
774c051c | 916 | else |
1dd6edf2 | 917 | buf.Printf("res-body=%d", (int) httpBuf.contentSize()); |
774c051c | 918 | |
919 | buf.append(ICAP::crlf, 2); // terminate Encapsulated line | |
920 | ||
921 | if (shouldPreview()) { | |
922 | buf.Printf("Preview: %d\r\n", (int)preview.ad()); | |
923 | virginSendClaim.protectUpTo(preview.ad()); | |
924 | } | |
925 | ||
926 | if (shouldAllow204()) { | |
927 | buf.Printf("Allow: 204\r\n"); | |
928 | // be robust: do not rely on the expected body size | |
929 | virginSendClaim.protectAll(); | |
930 | } | |
931 | ||
a97e82a8 | 932 | const HttpRequest *request = virgin->data->cause ? |
933 | virgin->data->cause : | |
934 | dynamic_cast<const HttpRequest*>(virgin->data->header); | |
935 | ||
12b91c99 | 936 | if (TheICAPConfig.send_client_ip) |
937 | if (request->client_addr.s_addr != any_addr.s_addr) | |
938 | buf.Printf("X-Client-IP: %s\r\n", inet_ntoa(request->client_addr)); | |
a97e82a8 | 939 | |
12b91c99 | 940 | if (TheICAPConfig.send_client_username) |
941 | if (request->auth_user_request) | |
942 | if (request->auth_user_request->username()) | |
943 | buf.Printf("X-Client-Username: %s\r\n", request->auth_user_request->username()); | |
a97e82a8 | 944 | |
2dfede9e | 945 | // fprintf(stderr, "%s\n", buf.content()); |
a97e82a8 | 946 | |
774c051c | 947 | buf.append(ICAP::crlf, 2); // terminate ICAP header |
948 | ||
949 | // start ICAP request body with encapsulated HTTP headers | |
950 | buf.append(httpBuf.content(), httpBuf.contentSize()); | |
951 | ||
952 | httpBuf.clean(); | |
953 | } | |
954 | ||
955 | void ICAPModXact::encapsulateHead(MemBuf &icapBuf, const char *section, MemBuf &httpBuf, const HttpMsg *head) | |
956 | { | |
957 | // update ICAP header | |
1dd6edf2 | 958 | icapBuf.Printf("%s=%d,", section, (int) httpBuf.contentSize()); |
774c051c | 959 | |
960 | // pack HTTP head | |
961 | packHead(httpBuf, head); | |
962 | } | |
963 | ||
964 | void ICAPModXact::packHead(MemBuf &httpBuf, const HttpMsg *head) | |
965 | { | |
966 | Packer p; | |
967 | packerToMemInit(&p, &httpBuf); | |
968 | head->packInto(&p, true); | |
969 | packerClean(&p); | |
970 | } | |
971 | ||
972 | // decides whether to offer a preview and calculates its size | |
973 | bool ICAPModXact::shouldPreview() | |
974 | { | |
975 | size_t wantedSize; | |
976 | ||
977 | if (!service().wantsPreview(wantedSize)) { | |
978 | debugs(93, 5, "ICAPModXact should not offer preview"); | |
979 | return false; | |
980 | } | |
981 | ||
982 | Must(wantedSize >= 0); | |
983 | ||
984 | // cannot preview more than we can backup | |
985 | size_t ad = XMIN(wantedSize, TheBackupLimit); | |
986 | ||
987 | if (virginBody.expected() && virginBody.knownSize()) | |
988 | ad = XMIN(ad, virginBody.size()); // not more than we have | |
989 | else | |
990 | ad = 0; // questionable optimization? | |
991 | ||
992 | debugs(93, 5, "ICAPModXact should offer " << ad << "-byte preview " << | |
993 | "(service wanted " << wantedSize << ")"); | |
994 | ||
995 | preview.enable(ad); | |
996 | ||
997 | return preview.enabled(); | |
998 | } | |
999 | ||
1000 | // decides whether to allow 204 responses | |
1001 | bool ICAPModXact::shouldAllow204() | |
1002 | { | |
1003 | if (!service().allows204()) | |
1004 | return false; | |
1005 | ||
1006 | if (!virginBody.expected()) | |
1007 | return true; // no body means no problems with supporting 204s. | |
1008 | ||
1009 | // if there is a body, make sure we can backup it all | |
1010 | ||
1011 | if (!virginBody.knownSize()) | |
1012 | return false; | |
1013 | ||
1014 | // or should we have a different backup limit? | |
1015 | // note that '<' allows for 0-termination of the "full" backup buffer | |
1016 | return virginBody.size() < TheBackupLimit; | |
1017 | } | |
1018 | ||
1019 | // returns a temporary string depicting transaction status, for debugging | |
1020 | void ICAPModXact::fillPendingStatus(MemBuf &buf) const | |
1021 | { | |
1022 | if (state.serviceWaiting) | |
1023 | buf.append("U", 1); | |
1024 | ||
1025 | if (!state.doneWriting() && state.writing != State::writingInit) | |
1026 | buf.Printf("w(%d)", state.writing); | |
1027 | ||
1028 | if (preview.enabled()) { | |
1029 | if (!preview.done()) | |
1dd6edf2 | 1030 | buf.Printf("P(%d)", (int) preview.debt()); |
774c051c | 1031 | } |
1032 | ||
1033 | if (virginSendClaim.active()) | |
1034 | buf.append("B", 1); | |
1035 | ||
1036 | if (!state.doneParsing() && state.parsing != State::psIcapHeader) | |
1037 | buf.Printf("p(%d)", state.parsing); | |
1038 | ||
1039 | if (!doneSending() && state.sending != State::sendingUndecided) | |
1040 | buf.Printf("S(%d)", state.sending); | |
1041 | } | |
1042 | ||
1043 | void ICAPModXact::fillDoneStatus(MemBuf &buf) const | |
1044 | { | |
1045 | if (state.doneReceiving) | |
1046 | buf.append("R", 1); | |
1047 | ||
1048 | if (state.doneWriting()) | |
1049 | buf.append("w", 1); | |
1050 | ||
1051 | if (preview.enabled()) { | |
1052 | if (preview.done()) | |
1053 | buf.Printf("P%s", preview.ieof() ? "(ieof)" : ""); | |
1054 | } | |
1055 | ||
1056 | if (doneReading()) | |
1057 | buf.append("r", 1); | |
1058 | ||
1059 | if (state.doneParsing()) | |
1060 | buf.append("p", 1); | |
1061 | ||
1062 | if (doneSending()) | |
1063 | buf.append("S", 1); | |
1064 | } | |
1065 | ||
1066 | bool ICAPModXact::gotEncapsulated(const char *section) const | |
1067 | { | |
1068 | return httpHeaderGetByNameListMember(&icapReply->header, "Encapsulated", | |
1069 | section, ',').size() > 0; | |
1070 | } | |
1071 | ||
1072 | // calculate whether there is a virgin HTTP body and | |
1073 | // whether its expected size is known | |
1074 | void ICAPModXact::estimateVirginBody() | |
1075 | { | |
1076 | // note: defaults should be fine but will disable previews and 204s | |
1077 | ||
1078 | Must(virgin != NULL && virgin->data->header); | |
1079 | ||
1080 | method_t method; | |
1081 | ||
1082 | if (virgin->data->cause) | |
1083 | method = virgin->data->cause->method; | |
1084 | else | |
a97e82a8 | 1085 | if (HttpRequest *req = dynamic_cast<HttpRequest*>(virgin->data-> |
1086 | header)) | |
774c051c | 1087 | method = req->method; |
1088 | else | |
1089 | return; | |
1090 | ||
1091 | ssize_t size; | |
1092 | if (virgin->data->header->expectingBody(method, size)) { | |
1093 | virginBody.expect(size) | |
1094 | ; | |
1095 | debugs(93, 6, "ICAPModXact expects virgin body; size: " << size); | |
1096 | } else { | |
1097 | debugs(93, 6, "ICAPModXact does not expect virgin body"); | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | ||
1102 | // TODO: Move SizedEstimate, MemBufBackup, and ICAPPreview elsewhere | |
1103 | ||
1104 | SizedEstimate::SizedEstimate() | |
1105 | : theData(dtUnexpected) | |
1106 | {} | |
1107 | ||
1108 | void SizedEstimate::expect(ssize_t aSize) | |
1109 | { | |
1110 | theData = (aSize >= 0) ? aSize : (ssize_t)dtUnknown; | |
1111 | } | |
1112 | ||
1113 | bool SizedEstimate::expected() const | |
1114 | { | |
1115 | return theData != dtUnexpected; | |
1116 | } | |
1117 | ||
1118 | bool SizedEstimate::knownSize() const | |
1119 | { | |
1120 | Must(expected()); | |
1121 | return theData != dtUnknown; | |
1122 | } | |
1123 | ||
1124 | size_t SizedEstimate::size() const | |
1125 | { | |
1126 | Must(knownSize()); | |
1127 | return static_cast<size_t>(theData); | |
1128 | } | |
1129 | ||
1130 | ||
1131 | ||
1132 | MemBufClaim::MemBufClaim(): theStart(-1), theGoal(-1) | |
1133 | {} | |
1134 | ||
1135 | void MemBufClaim::protectAll() | |
1136 | { | |
1137 | if (theStart < 0) | |
1138 | theStart = 0; | |
1139 | ||
1140 | theGoal = -1; // no specific goal | |
1141 | } | |
1142 | ||
1143 | void MemBufClaim::protectUpTo(size_t aGoal) | |
1144 | { | |
1145 | if (theStart < 0) | |
1146 | theStart = 0; | |
1147 | ||
1148 | Must(aGoal >= 0); | |
1149 | ||
1150 | theGoal = (theGoal < 0) ? static_cast<ssize_t>(aGoal) : | |
1151 | XMIN(static_cast<ssize_t>(aGoal), theGoal); | |
1152 | } | |
1153 | ||
1154 | void MemBufClaim::disable() | |
1155 | { | |
1156 | theStart = -1; | |
1157 | } | |
1158 | ||
1159 | void MemBufClaim::release(size_t size) | |
1160 | { | |
1161 | Must(active()); | |
1162 | Must(size >= 0); | |
1163 | theStart += static_cast<ssize_t>(size); | |
1164 | ||
1165 | if (limited() && theStart >= theGoal) | |
1166 | disable(); | |
1167 | } | |
1168 | ||
1169 | size_t MemBufClaim::offset() const | |
1170 | { | |
1171 | Must(active()); | |
1172 | return static_cast<size_t>(theStart); | |
1173 | } | |
1174 | ||
1175 | bool MemBufClaim::limited() const | |
1176 | { | |
1177 | Must(active()); | |
1178 | return theGoal >= 0; | |
1179 | } | |
1180 | ||
1181 | ||
1182 | ICAPPreview::ICAPPreview(): theWritten(0), theAd(0), theState(stDisabled) | |
1183 | {} | |
1184 | ||
1185 | void ICAPPreview::enable(size_t anAd) | |
1186 | { | |
1187 | // TODO: check for anAd not exceeding preview size limit | |
1188 | Must(anAd >= 0); | |
1189 | Must(!enabled()); | |
1190 | theAd = anAd; | |
1191 | theState = stWriting; | |
1192 | } | |
1193 | ||
1194 | bool ICAPPreview::enabled() const | |
1195 | { | |
1196 | return theState != stDisabled; | |
1197 | } | |
1198 | ||
1199 | size_t ICAPPreview::ad() const | |
1200 | { | |
1201 | Must(enabled()); | |
1202 | return theAd; | |
1203 | } | |
1204 | ||
1205 | bool ICAPPreview::done() const | |
1206 | { | |
1207 | Must(enabled()); | |
1208 | return theState >= stIeof; | |
1209 | } | |
1210 | ||
1211 | bool ICAPPreview::ieof() const | |
1212 | { | |
1213 | Must(enabled()); | |
1214 | return theState == stIeof; | |
1215 | } | |
1216 | ||
1217 | size_t ICAPPreview::debt() const | |
1218 | { | |
1219 | Must(enabled()); | |
1220 | return done() ? 0 : (theAd - theWritten); | |
1221 | } | |
1222 | ||
1223 | void ICAPPreview::wrote(size_t size, bool sawEof) | |
1224 | { | |
1225 | Must(enabled()); | |
1226 | theWritten += size; | |
1227 | ||
1228 | if (theWritten >= theAd) | |
1229 | theState = stDone; // sawEof is irrelevant | |
1230 | else | |
1231 | if (sawEof) | |
1232 | theState = stIeof; | |
1233 | } | |
1234 |