]>
Commit | Line | Data |
---|---|---|
cd304fc2 | 1 | /* |
262a0e14 | 2 | * $Id$ |
cd304fc2 | 3 | * |
4 | * DEBUG: | |
5 | * AUTHOR: Duane Wessels | |
6 | * | |
7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
8 | * ---------------------------------------------------------- | |
9 | * | |
10 | * Squid is the result of efforts by numerous individuals from | |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
18 | * | |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
9e008dda | 23 | * |
cd304fc2 | 24 | * This program is distributed in the hope that it will be useful, |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
9e008dda | 28 | * |
cd304fc2 | 29 | * You should have received a copy of the GNU General Public License |
30 | * along with this program; if not, write to the Free Software | |
31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
32 | * | |
33 | */ | |
34 | ||
582c2af2 | 35 | #include "squid.h" |
3af10ac0 | 36 | #include "acl/Gadgets.h" |
3d93a84d | 37 | #include "base/TextException.h" |
6b679a01 AJ |
38 | #include "comm/Connection.h" |
39 | #include "comm/forward.h" | |
ec41b64c | 40 | #include "comm/Write.h" |
64b66b76 | 41 | #include "err_detail_type.h" |
582c2af2 FC |
42 | #include "errorpage.h" |
43 | #include "HttpReply.h" | |
44 | #include "HttpRequest.h" | |
45 | #include "protos.h" | |
46 | #include "Server.h" | |
3ff65596 | 47 | #include "SquidTime.h" |
582c2af2 FC |
48 | #include "StatCounters.h" |
49 | #include "Store.h" | |
5f8252d2 | 50 | |
a83c6ed6 | 51 | #if USE_ADAPTATION |
62c7f90e | 52 | #include "adaptation/AccessCheck.h" |
1adcebc3 | 53 | #include "adaptation/Answer.h" |
a22e6cd3 | 54 | #include "adaptation/Iterator.h" |
0ad2b63b | 55 | #include "base/AsyncCall.h" |
0f283edf | 56 | #endif |
cd304fc2 | 57 | |
c1520b67 | 58 | // implemented in client_side_reply.cc until sides have a common parent |
90bd689c | 59 | extern void purgeEntriesByUrl(HttpRequest * req, const char *url); |
c1520b67 | 60 | |
39cb8c41 AR |
61 | ServerStateData::ServerStateData(FwdState *theFwdState): AsyncJob("ServerStateData"), |
62 | requestSender(NULL), | |
a83c6ed6 | 63 | #if USE_ADAPTATION |
39cb8c41 AR |
64 | adaptedHeadSource(NULL), |
65 | adaptationAccessCheckPending(false), | |
66 | startedAdaptation(false), | |
77089558 | 67 | #endif |
0ad2b63b CT |
68 | receivedWholeRequestBody(false), |
69 | theVirginReply(NULL), | |
70 | theFinalReply(NULL) | |
cd304fc2 | 71 | { |
72 | fwd = theFwdState; | |
73 | entry = fwd->entry; | |
34266cde | 74 | |
5f8252d2 | 75 | entry->lock(); |
34266cde | 76 | |
6dd9f4bd | 77 | request = HTTPMSGLOCK(fwd->request); |
cd304fc2 | 78 | } |
79 | ||
80 | ServerStateData::~ServerStateData() | |
81 | { | |
49918309 AR |
82 | // paranoid: check that swanSong has been called |
83 | assert(!requestBodySource); | |
84 | #if USE_ADAPTATION | |
85 | assert(!virginBodyDestination); | |
86 | assert(!adaptedBodySource); | |
87 | #endif | |
88 | ||
97b5e68f | 89 | entry->unlock(); |
cd304fc2 | 90 | |
6dd9f4bd | 91 | HTTPMSGUNLOCK(request); |
585ab260 | 92 | HTTPMSGUNLOCK(theVirginReply); |
93 | HTTPMSGUNLOCK(theFinalReply); | |
cd304fc2 | 94 | |
95 | fwd = NULL; // refcounted | |
96 | ||
49918309 | 97 | if (responseBodyBuffer != NULL) { |
9e008dda AJ |
98 | delete responseBodyBuffer; |
99 | responseBodyBuffer = NULL; | |
49918309 AR |
100 | } |
101 | } | |
102 | ||
103 | void | |
104 | ServerStateData::swanSong() | |
105 | { | |
106 | // get rid of our piping obligations | |
5f8252d2 | 107 | if (requestBodySource != NULL) |
279152e7 | 108 | stopConsumingFrom(requestBodySource); |
5f8252d2 | 109 | |
a83c6ed6 AR |
110 | #if USE_ADAPTATION |
111 | cleanAdaptation(); | |
5f8252d2 | 112 | #endif |
7dc79973 | 113 | |
49918309 AR |
114 | BodyConsumer::swanSong(); |
115 | #if USE_ADAPTATION | |
116 | Initiator::swanSong(); | |
117 | BodyProducer::swanSong(); | |
118 | #endif | |
b2c251cc AJ |
119 | |
120 | // paranoid: check that swanSong has been called | |
121 | // extra paranoid: yeah, I really mean it. they MUST pass here. | |
122 | assert(!requestBodySource); | |
123 | #if USE_ADAPTATION | |
124 | assert(!virginBodyDestination); | |
125 | assert(!adaptedBodySource); | |
126 | #endif | |
5f8252d2 | 127 | } |
128 | ||
585ab260 | 129 | HttpReply * |
9e008dda AJ |
130 | ServerStateData::virginReply() |
131 | { | |
585ab260 | 132 | assert(theVirginReply); |
133 | return theVirginReply; | |
134 | } | |
135 | ||
136 | const HttpReply * | |
9e008dda AJ |
137 | ServerStateData::virginReply() const |
138 | { | |
585ab260 | 139 | assert(theVirginReply); |
140 | return theVirginReply; | |
141 | } | |
142 | ||
143 | HttpReply * | |
9e008dda AJ |
144 | ServerStateData::setVirginReply(HttpReply *rep) |
145 | { | |
585ab260 | 146 | debugs(11,5, HERE << this << " setting virgin reply to " << rep); |
147 | assert(!theVirginReply); | |
148 | assert(rep); | |
149 | theVirginReply = HTTPMSGLOCK(rep); | |
9e008dda | 150 | return theVirginReply; |
585ab260 | 151 | } |
152 | ||
153 | HttpReply * | |
9e008dda AJ |
154 | ServerStateData::finalReply() |
155 | { | |
585ab260 | 156 | assert(theFinalReply); |
157 | return theFinalReply; | |
158 | } | |
159 | ||
160 | HttpReply * | |
9e008dda AJ |
161 | ServerStateData::setFinalReply(HttpReply *rep) |
162 | { | |
585ab260 | 163 | debugs(11,5, HERE << this << " setting final reply to " << rep); |
164 | ||
165 | assert(!theFinalReply); | |
166 | assert(rep); | |
167 | theFinalReply = HTTPMSGLOCK(rep); | |
168 | ||
3756e5c0 AR |
169 | // give entry the reply because haveParsedReplyHeaders() expects it there |
170 | entry->replaceHttpReply(theFinalReply, false); // but do not write yet | |
171 | haveParsedReplyHeaders(); // update the entry/reply (e.g., set timestamps) | |
172 | entry->startWriting(); // write the updated entry to store | |
585ab260 | 173 | |
174 | return theFinalReply; | |
175 | } | |
176 | ||
5f8252d2 | 177 | // called when no more server communication is expected; may quit |
178 | void | |
179 | ServerStateData::serverComplete() | |
180 | { | |
181 | debugs(11,5,HERE << "serverComplete " << this); | |
182 | ||
183 | if (!doneWithServer()) { | |
184 | closeServer(); | |
185 | assert(doneWithServer()); | |
186 | } | |
187 | ||
7dc79973 | 188 | completed = true; |
189 | ||
3ff65596 AR |
190 | HttpRequest *r = originalRequest(); |
191 | r->hier.total_response_time = r->hier.first_conn_start.tv_sec ? | |
e1381638 AJ |
192 | tvSubMsec(r->hier.first_conn_start, current_time) : -1; |
193 | ||
5f8252d2 | 194 | if (requestBodySource != NULL) |
195 | stopConsumingFrom(requestBodySource); | |
196 | ||
7dc79973 | 197 | if (responseBodyBuffer != NULL) |
9e008dda | 198 | return; |
7dc79973 | 199 | |
200 | serverComplete2(); | |
201 | } | |
202 | ||
203 | void | |
204 | ServerStateData::serverComplete2() | |
205 | { | |
206 | debugs(11,5,HERE << "serverComplete2 " << this); | |
207 | ||
a83c6ed6 | 208 | #if USE_ADAPTATION |
5f8252d2 | 209 | if (virginBodyDestination != NULL) |
210 | stopProducingFor(virginBodyDestination, true); | |
211 | ||
a83c6ed6 | 212 | if (!doneWithAdaptation()) |
5f8252d2 | 213 | return; |
214 | #endif | |
215 | ||
216 | completeForwarding(); | |
5f8252d2 | 217 | } |
218 | ||
79628299 | 219 | bool ServerStateData::doneAll() const |
9e008dda | 220 | { |
79628299 | 221 | return doneWithServer() && |
a83c6ed6 | 222 | #if USE_ADAPTATION |
b692311b A |
223 | doneWithAdaptation() && |
224 | Adaptation::Initiator::doneAll() && | |
225 | BodyProducer::doneAll() && | |
5f8252d2 | 226 | #endif |
b692311b | 227 | BodyConsumer::doneAll(); |
5f8252d2 | 228 | } |
229 | ||
230 | // FTP side overloads this to work around multiple calls to fwd->complete | |
231 | void | |
9e008dda AJ |
232 | ServerStateData::completeForwarding() |
233 | { | |
5f8252d2 | 234 | debugs(11,5, HERE << "completing forwarding for " << fwd); |
235 | assert(fwd != NULL); | |
236 | fwd->complete(); | |
237 | } | |
238 | ||
123ec4de | 239 | // Register to receive request body |
240 | bool ServerStateData::startRequestBodyFlow() | |
241 | { | |
242 | HttpRequest *r = originalRequest(); | |
243 | assert(r->body_pipe != NULL); | |
244 | requestBodySource = r->body_pipe; | |
245 | if (requestBodySource->setConsumerIfNotLate(this)) { | |
246 | debugs(11,3, HERE << "expecting request body from " << | |
9e008dda | 247 | requestBodySource->status()); |
123ec4de | 248 | return true; |
249 | } | |
250 | ||
251 | debugs(11,3, HERE << "aborting on partially consumed request body: " << | |
9e008dda | 252 | requestBodySource->status()); |
123ec4de | 253 | requestBodySource = NULL; |
254 | return false; | |
255 | } | |
256 | ||
5f8252d2 | 257 | // Entry-dependent callbacks use this check to quit if the entry went bad |
258 | bool | |
259 | ServerStateData::abortOnBadEntry(const char *abortReason) | |
260 | { | |
261 | if (entry->isAccepting()) | |
262 | return false; | |
263 | ||
264 | debugs(11,5, HERE << "entry is not Accepting!"); | |
265 | abortTransaction(abortReason); | |
266 | return true; | |
267 | } | |
268 | ||
269 | // more request or adapted response body is available | |
270 | void | |
dc56a9b1 | 271 | ServerStateData::noteMoreBodyDataAvailable(BodyPipe::Pointer bp) |
5f8252d2 | 272 | { |
a83c6ed6 | 273 | #if USE_ADAPTATION |
dc56a9b1 | 274 | if (adaptedBodySource == bp) { |
5f8252d2 | 275 | handleMoreAdaptedBodyAvailable(); |
276 | return; | |
277 | } | |
278 | #endif | |
0ad2b63b CT |
279 | if (requestBodySource == bp) |
280 | handleMoreRequestBodyAvailable(); | |
5f8252d2 | 281 | } |
282 | ||
283 | // the entire request or adapted response body was provided, successfully | |
284 | void | |
dc56a9b1 | 285 | ServerStateData::noteBodyProductionEnded(BodyPipe::Pointer bp) |
5f8252d2 | 286 | { |
a83c6ed6 | 287 | #if USE_ADAPTATION |
dc56a9b1 | 288 | if (adaptedBodySource == bp) { |
5f8252d2 | 289 | handleAdaptedBodyProductionEnded(); |
290 | return; | |
c99de607 | 291 | } |
cd304fc2 | 292 | #endif |
0ad2b63b CT |
293 | if (requestBodySource == bp) |
294 | handleRequestBodyProductionEnded(); | |
5f8252d2 | 295 | } |
296 | ||
297 | // premature end of the request or adapted response body production | |
298 | void | |
dc56a9b1 | 299 | ServerStateData::noteBodyProducerAborted(BodyPipe::Pointer bp) |
5f8252d2 | 300 | { |
a83c6ed6 | 301 | #if USE_ADAPTATION |
dc56a9b1 | 302 | if (adaptedBodySource == bp) { |
5f8252d2 | 303 | handleAdaptedBodyProducerAborted(); |
304 | return; | |
305 | } | |
306 | #endif | |
0ad2b63b CT |
307 | if (requestBodySource == bp) |
308 | handleRequestBodyProducerAborted(); | |
5f8252d2 | 309 | } |
310 | ||
5f8252d2 | 311 | // more origin request body data is available |
312 | void | |
313 | ServerStateData::handleMoreRequestBodyAvailable() | |
314 | { | |
315 | if (!requestSender) | |
316 | sendMoreRequestBody(); | |
317 | else | |
318 | debugs(9,3, HERE << "waiting for request body write to complete"); | |
319 | } | |
320 | ||
321 | // there will be no more handleMoreRequestBodyAvailable calls | |
322 | void | |
323 | ServerStateData::handleRequestBodyProductionEnded() | |
324 | { | |
39cb8c41 | 325 | receivedWholeRequestBody = true; |
5f8252d2 | 326 | if (!requestSender) |
327 | doneSendingRequestBody(); | |
328 | else | |
329 | debugs(9,3, HERE << "waiting for request body write to complete"); | |
330 | } | |
331 | ||
332 | // called when we are done sending request body; kids extend this | |
333 | void | |
9e008dda AJ |
334 | ServerStateData::doneSendingRequestBody() |
335 | { | |
5f8252d2 | 336 | debugs(9,3, HERE << "done sending request body"); |
337 | assert(requestBodySource != NULL); | |
338 | stopConsumingFrom(requestBodySource); | |
339 | ||
340 | // kids extend this | |
341 | } | |
342 | ||
343 | // called when body producers aborts; kids extend this | |
344 | void | |
345 | ServerStateData::handleRequestBodyProducerAborted() | |
346 | { | |
347 | if (requestSender != NULL) | |
348 | debugs(9,3, HERE << "fyi: request body aborted while we were sending"); | |
349 | ||
0919c51e | 350 | fwd->dontRetry(true); // the problem is not with the server |
5f8252d2 | 351 | stopConsumingFrom(requestBodySource); // requestSender, if any, will notice |
352 | ||
353 | // kids extend this | |
354 | } | |
355 | ||
5f8252d2 | 356 | // called when we wrote request headers(!) or a part of the body |
357 | void | |
dc56a9b1 | 358 | ServerStateData::sentRequestBody(const CommIoCbParams &io) |
5f8252d2 | 359 | { |
dc56a9b1 | 360 | debugs(11, 5, "sentRequestBody: FD " << io.fd << ": size " << io.size << ": errflag " << io.flag << "."); |
5f8252d2 | 361 | debugs(32,3,HERE << "sentRequestBody called"); |
362 | ||
363 | requestSender = NULL; | |
364 | ||
dc56a9b1 | 365 | if (io.size > 0) { |
366 | fd_bytes(io.fd, io.size, FD_WRITE); | |
e4f1fdae | 367 | kb_incr(&(statCounter.server.all.kbytes_out), io.size); |
5f8252d2 | 368 | // kids should increment their counters |
369 | } | |
370 | ||
dc56a9b1 | 371 | if (io.flag == COMM_ERR_CLOSING) |
5f8252d2 | 372 | return; |
373 | ||
374 | if (!requestBodySource) { | |
375 | debugs(9,3, HERE << "detected while-we-were-sending abort"); | |
376 | return; // do nothing; | |
377 | } | |
378 | ||
dc56a9b1 | 379 | if (io.flag) { |
e0236918 | 380 | debugs(11, DBG_IMPORTANT, "sentRequestBody error: FD " << io.fd << ": " << xstrerr(io.xerrno)); |
5f8252d2 | 381 | ErrorState *err; |
913524f0 | 382 | err = new ErrorState(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY, fwd->request); |
f5f9e44c | 383 | err->xerrno = io.xerrno; |
5f8252d2 | 384 | fwd->fail(err); |
385 | abortTransaction("I/O error while sending request body"); | |
386 | return; | |
387 | } | |
388 | ||
389 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { | |
390 | abortTransaction("store entry aborted while sending request body"); | |
391 | return; | |
392 | } | |
393 | ||
39cb8c41 AR |
394 | if (!requestBodySource->exhausted()) |
395 | sendMoreRequestBody(); | |
de48b288 | 396 | else if (receivedWholeRequestBody) |
5f8252d2 | 397 | doneSendingRequestBody(); |
398 | else | |
39cb8c41 | 399 | debugs(9,3, HERE << "waiting for body production end or abort"); |
cd304fc2 | 400 | } |
401 | ||
5f8252d2 | 402 | void |
403 | ServerStateData::sendMoreRequestBody() | |
404 | { | |
405 | assert(requestBodySource != NULL); | |
406 | assert(!requestSender); | |
a0297974 | 407 | |
e83cc785 | 408 | const Comm::ConnectionPointer conn = dataConnection(); |
a0297974 | 409 | |
6b679a01 | 410 | if (!Comm::IsConnOpen(conn)) { |
e0d28505 | 411 | debugs(9,3, HERE << "cannot send request body to closing " << conn); |
a0297974 AR |
412 | return; // wait for the kid's close handler; TODO: assert(closer); |
413 | } | |
414 | ||
5f8252d2 | 415 | MemBuf buf; |
39cb8c41 | 416 | if (getMoreRequestBody(buf) && buf.contentSize() > 0) { |
5f8252d2 | 417 | debugs(9,3, HERE << "will write " << buf.contentSize() << " request body bytes"); |
9e008dda | 418 | typedef CommCbMemFunT<ServerStateData, CommIoCbParams> Dialer; |
e0d28505 | 419 | requestSender = JobCallback(93,3, Dialer, this, ServerStateData::sentRequestBody); |
b0388924 | 420 | Comm::Write(conn, &buf, requestSender); |
5f8252d2 | 421 | } else { |
422 | debugs(9,3, HERE << "will wait for more request body bytes or eof"); | |
423 | requestSender = NULL; | |
424 | } | |
425 | } | |
426 | ||
39cb8c41 AR |
427 | /// either fill buf with available [encoded] request body bytes or return false |
428 | bool | |
429 | ServerStateData::getMoreRequestBody(MemBuf &buf) | |
430 | { | |
431 | // default implementation does not encode request body content | |
432 | Must(requestBodySource != NULL); | |
433 | return requestBodySource->getMoreData(buf); | |
434 | } | |
435 | ||
c1520b67 AJ |
436 | // Compares hosts in urls, returns false if different, no sheme, or no host. |
437 | static bool | |
438 | sameUrlHosts(const char *url1, const char *url2) | |
439 | { | |
440 | // XXX: Want urlHostname() here, but it uses static storage and copying | |
441 | const char *host1 = strchr(url1, ':'); | |
442 | const char *host2 = strchr(url2, ':'); | |
443 | ||
444 | if (host1 && host2) { | |
445 | // skip scheme slashes | |
446 | do { | |
447 | ++host1; | |
448 | ++host2; | |
449 | } while (*host1 == '/' && *host2 == '/'); | |
450 | ||
451 | if (!*host1) | |
452 | return false; // no host | |
453 | ||
454 | // increment while the same until we reach the end of the URL/host | |
455 | while (*host1 && *host1 != '/' && *host1 == *host2) { | |
456 | ++host1; | |
457 | ++host2; | |
458 | } | |
459 | return *host1 == *host2; | |
460 | } | |
461 | ||
462 | return false; // no URL scheme | |
463 | } | |
464 | ||
465 | // purges entries that match the value of a given HTTP [response] header | |
466 | static void | |
90bd689c | 467 | purgeEntriesByHeader(HttpRequest *req, const char *reqUrl, HttpMsg *rep, http_hdr_type hdr) |
c1520b67 | 468 | { |
bf956b0a | 469 | const char *hdrUrl, *absUrl; |
9e008dda | 470 | |
71051277 BR |
471 | absUrl = NULL; |
472 | hdrUrl = rep->header.getStr(hdr); | |
473 | if (hdrUrl == NULL) { | |
474 | return; | |
475 | } | |
9e008dda | 476 | |
71051277 BR |
477 | /* |
478 | * If the URL is relative, make it absolute so we can find it. | |
479 | * If it's absolute, make sure the host parts match to avoid DOS attacks | |
480 | * as per RFC 2616 13.10. | |
481 | */ | |
482 | if (urlIsRelative(hdrUrl)) { | |
483 | absUrl = urlMakeAbsolute(req, hdrUrl); | |
3cbbd242 | 484 | if (absUrl != NULL) { |
71051277 | 485 | hdrUrl = absUrl; |
3cbbd242 | 486 | } |
71051277 BR |
487 | } else if (!sameUrlHosts(reqUrl, hdrUrl)) { |
488 | return; | |
489 | } | |
9e008dda | 490 | |
8dceeee3 | 491 | purgeEntriesByUrl(req, hdrUrl); |
9e008dda | 492 | |
71051277 BR |
493 | if (absUrl != NULL) { |
494 | safe_free(absUrl); | |
3cbbd242 | 495 | } |
c1520b67 AJ |
496 | } |
497 | ||
498 | // some HTTP methods should purge matching cache entries | |
499 | void | |
500 | ServerStateData::maybePurgeOthers() | |
501 | { | |
9e008dda AJ |
502 | // only some HTTP methods should purge matching cache entries |
503 | if (!request->method.purgesOthers()) | |
504 | return; | |
c1520b67 | 505 | |
9e008dda AJ |
506 | // and probably only if the response was successful |
507 | if (theFinalReply->sline.status >= 400) | |
508 | return; | |
c1520b67 | 509 | |
9e008dda AJ |
510 | // XXX: should we use originalRequest() here? |
511 | const char *reqUrl = urlCanonical(request); | |
512 | debugs(88, 5, "maybe purging due to " << RequestMethodStr(request->method) << ' ' << reqUrl); | |
513 | purgeEntriesByUrl(request, reqUrl); | |
514 | purgeEntriesByHeader(request, reqUrl, theFinalReply, HDR_LOCATION); | |
515 | purgeEntriesByHeader(request, reqUrl, theFinalReply, HDR_CONTENT_LOCATION); | |
c1520b67 AJ |
516 | } |
517 | ||
fad1a21e | 518 | /// called when we have final (possibly adapted) reply headers; kids extend |
5f8252d2 | 519 | void |
520 | ServerStateData::haveParsedReplyHeaders() | |
521 | { | |
9e008dda AJ |
522 | Must(theFinalReply); |
523 | maybePurgeOthers(); | |
5f8252d2 | 524 | } |
525 | ||
7dc79973 | 526 | HttpRequest * |
527 | ServerStateData::originalRequest() | |
528 | { | |
529 | return request; | |
530 | } | |
5f8252d2 | 531 | |
a83c6ed6 | 532 | #if USE_ADAPTATION |
a22e6cd3 AR |
533 | /// Initiate an asynchronous adaptation transaction which will call us back. |
534 | void | |
535 | ServerStateData::startAdaptation(const Adaptation::ServiceGroupPointer &group, HttpRequest *cause) | |
cd304fc2 | 536 | { |
a83c6ed6 | 537 | debugs(11, 5, "ServerStateData::startAdaptation() called"); |
5f8252d2 | 538 | // check whether we should be sending a body as well |
5f8252d2 | 539 | // start body pipe to feed ICAP transaction if needed |
585ab260 | 540 | assert(!virginBodyDestination); |
9e008dda | 541 | HttpReply *vrep = virginReply(); |
585ab260 | 542 | assert(!vrep->body_pipe); |
47f6e231 | 543 | int64_t size = 0; |
585ab260 | 544 | if (vrep->expectingBody(cause->method, size) && size) { |
5f8252d2 | 545 | virginBodyDestination = new BodyPipe(this); |
585ab260 | 546 | vrep->body_pipe = virginBodyDestination; |
9e008dda AJ |
547 | debugs(93, 6, HERE << "will send virgin reply body to " << |
548 | virginBodyDestination << "; size: " << size); | |
c2eef5bd | 549 | if (size > 0) |
550 | virginBodyDestination->setBodySize(size); | |
5f8252d2 | 551 | } |
552 | ||
a22e6cd3 | 553 | adaptedHeadSource = initiateAdaptation( |
4cb2536f | 554 | new Adaptation::Iterator(vrep, cause, group)); |
4299f876 | 555 | startedAdaptation = initiated(adaptedHeadSource); |
a22e6cd3 | 556 | Must(startedAdaptation); |
cd304fc2 | 557 | } |
0c25e715 | 558 | |
5f8252d2 | 559 | // properly cleans up ICAP-related state |
560 | // may be called multiple times | |
9e008dda AJ |
561 | void ServerStateData::cleanAdaptation() |
562 | { | |
a83c6ed6 | 563 | debugs(11,5, HERE << "cleaning ICAP; ACL: " << adaptationAccessCheckPending); |
5f8252d2 | 564 | |
565 | if (virginBodyDestination != NULL) | |
566 | stopProducingFor(virginBodyDestination, false); | |
567 | ||
0f283edf | 568 | announceInitiatorAbort(adaptedHeadSource); |
5f8252d2 | 569 | |
570 | if (adaptedBodySource != NULL) | |
571 | stopConsumingFrom(adaptedBodySource); | |
572 | ||
a83c6ed6 AR |
573 | if (!adaptationAccessCheckPending) // we cannot cancel a pending callback |
574 | assert(doneWithAdaptation()); // make sure the two methods are in sync | |
5f8252d2 | 575 | } |
576 | ||
577 | bool | |
9e008dda AJ |
578 | ServerStateData::doneWithAdaptation() const |
579 | { | |
a83c6ed6 | 580 | return !adaptationAccessCheckPending && |
9e008dda | 581 | !virginBodyDestination && !adaptedHeadSource && !adaptedBodySource; |
5f8252d2 | 582 | } |
583 | ||
bc81cb2b | 584 | // sends virgin reply body to ICAP, buffering excesses if needed |
585 | void | |
586 | ServerStateData::adaptVirginReplyBody(const char *data, ssize_t len) | |
587 | { | |
a83c6ed6 | 588 | assert(startedAdaptation); |
bc81cb2b | 589 | |
590 | if (!virginBodyDestination) { | |
591 | debugs(11,3, HERE << "ICAP does not want more virgin body"); | |
592 | return; | |
593 | } | |
594 | ||
595 | // grow overflow area if already overflowed | |
596 | if (responseBodyBuffer) { | |
597 | responseBodyBuffer->append(data, len); | |
598 | data = responseBodyBuffer->content(); | |
599 | len = responseBodyBuffer->contentSize(); | |
600 | } | |
601 | ||
602 | const ssize_t putSize = virginBodyDestination->putMoreData(data, len); | |
603 | data += putSize; | |
604 | len -= putSize; | |
605 | ||
606 | // if we had overflow area, shrink it as necessary | |
607 | if (responseBodyBuffer) { | |
608 | if (putSize == responseBodyBuffer->contentSize()) { | |
609 | delete responseBodyBuffer; | |
610 | responseBodyBuffer = NULL; | |
611 | } else { | |
612 | responseBodyBuffer->consume(putSize); | |
9e008dda | 613 | } |
bc81cb2b | 614 | return; |
615 | } | |
616 | ||
617 | // if we did not have an overflow area, create it as needed | |
618 | if (len > 0) { | |
619 | assert(!responseBodyBuffer); | |
620 | responseBodyBuffer = new MemBuf; | |
621 | responseBodyBuffer->init(4096, SQUID_TCP_SO_RCVBUF * 10); | |
622 | responseBodyBuffer->append(data, len); | |
623 | } | |
624 | } | |
625 | ||
5f8252d2 | 626 | // can supply more virgin response body data |
627 | void | |
dc56a9b1 | 628 | ServerStateData::noteMoreBodySpaceAvailable(BodyPipe::Pointer) |
5f8252d2 | 629 | { |
7dc79973 | 630 | if (responseBodyBuffer) { |
bc81cb2b | 631 | addVirginReplyBody(NULL, 0); // kick the buffered fragment alive again |
632 | if (completed && !responseBodyBuffer) { | |
633 | serverComplete2(); | |
634 | return; | |
635 | } | |
7dc79973 | 636 | } |
5f8252d2 | 637 | maybeReadVirginBody(); |
638 | } | |
639 | ||
bc81cb2b | 640 | // the consumer of our virgin response body aborted |
5f8252d2 | 641 | void |
dc56a9b1 | 642 | ServerStateData::noteBodyConsumerAborted(BodyPipe::Pointer) |
5f8252d2 | 643 | { |
644 | stopProducingFor(virginBodyDestination, false); | |
bc81cb2b | 645 | |
a83c6ed6 | 646 | // do not force closeServer here in case we need to bypass AdaptationQueryAbort |
bc81cb2b | 647 | |
a83c6ed6 AR |
648 | if (doneWithAdaptation()) // we may still be receiving adapted response |
649 | handleAdaptationCompleted(); | |
5f8252d2 | 650 | } |
651 | ||
652 | // received adapted response headers (body may follow) | |
653 | void | |
3af10ac0 | 654 | ServerStateData::noteAdaptationAnswer(const Adaptation::Answer &answer) |
5f8252d2 | 655 | { |
a83c6ed6 | 656 | clearAdaptation(adaptedHeadSource); // we do not expect more messages |
5f8252d2 | 657 | |
3af10ac0 AR |
658 | switch (answer.kind) { |
659 | case Adaptation::Answer::akForward: | |
660 | handleAdaptedHeader(answer.message); | |
661 | break; | |
662 | ||
663 | case Adaptation::Answer::akBlock: | |
664 | handleAdaptationBlocked(answer); | |
665 | break; | |
666 | ||
667 | case Adaptation::Answer::akError: | |
668 | handleAdaptationAborted(!answer.final); | |
669 | break; | |
670 | } | |
671 | } | |
672 | ||
673 | void | |
674 | ServerStateData::handleAdaptedHeader(HttpMsg *msg) | |
675 | { | |
1733bbba CT |
676 | if (abortOnBadEntry("entry went bad while waiting for adapted headers")) { |
677 | // If the adapted response has a body, the ICAP side needs to know | |
678 | // that nobody will consume that body. We will be destroyed upon | |
679 | // return. Tell the ICAP side that it is on its own. | |
680 | HttpReply *rep = dynamic_cast<HttpReply*>(msg); | |
681 | assert(rep); | |
682 | if (rep->body_pipe != NULL) | |
683 | rep->body_pipe->expectNoConsumption(); | |
684 | ||
5f8252d2 | 685 | return; |
1733bbba | 686 | } |
5f8252d2 | 687 | |
585ab260 | 688 | HttpReply *rep = dynamic_cast<HttpReply*>(msg); |
5f8252d2 | 689 | assert(rep); |
585ab260 | 690 | debugs(11,5, HERE << this << " setting adapted reply to " << rep); |
691 | setFinalReply(rep); | |
5f8252d2 | 692 | |
693 | assert(!adaptedBodySource); | |
585ab260 | 694 | if (rep->body_pipe != NULL) { |
5f8252d2 | 695 | // subscribe to receive adapted body |
585ab260 | 696 | adaptedBodySource = rep->body_pipe; |
5f8252d2 | 697 | // assume that ICAP does not auto-consume on failures |
698 | assert(adaptedBodySource->setConsumerIfNotLate(this)); | |
699 | } else { | |
700 | // no body | |
a83c6ed6 AR |
701 | if (doneWithAdaptation()) // we may still be sending virgin response |
702 | handleAdaptationCompleted(); | |
5f8252d2 | 703 | } |
5f8252d2 | 704 | } |
705 | ||
5f8252d2 | 706 | void |
0ad2b63b | 707 | ServerStateData::resumeBodyStorage() |
5f8252d2 | 708 | { |
0ad2b63b CT |
709 | if (abortOnBadEntry("store entry aborted while kick producer callback")) |
710 | return; | |
5f8252d2 | 711 | |
e83cdc25 | 712 | if (!adaptedBodySource) |
0ad2b63b CT |
713 | return; |
714 | ||
715 | handleMoreAdaptedBodyAvailable(); | |
716 | ||
717 | if (adaptedBodySource != NULL && adaptedBodySource->exhausted()) | |
718 | endAdaptedBodyConsumption(); | |
719 | } | |
5f8252d2 | 720 | |
0ad2b63b CT |
721 | // more adapted response body is available |
722 | void | |
723 | ServerStateData::handleMoreAdaptedBodyAvailable() | |
724 | { | |
5f8252d2 | 725 | if (abortOnBadEntry("entry refuses adapted body")) |
726 | return; | |
727 | ||
728 | assert(entry); | |
0ad2b63b CT |
729 | |
730 | size_t contentSize = adaptedBodySource->buf().contentSize(); | |
0ad2b63b CT |
731 | |
732 | if (!contentSize) | |
733 | return; // XXX: bytesWanted asserts on zero-size ranges | |
734 | ||
384a7590 | 735 | const size_t spaceAvailable = entry->bytesWanted(Range<size_t>(0, contentSize), true); |
0ad2b63b | 736 | |
e83cdc25 | 737 | if (spaceAvailable < contentSize ) { |
0ad2b63b CT |
738 | // No or partial body data consuming |
739 | typedef NullaryMemFunT<ServerStateData> Dialer; | |
740 | AsyncCall::Pointer call = asyncCall(93, 5, "ServerStateData::resumeBodyStorage", | |
741 | Dialer(this, &ServerStateData::resumeBodyStorage)); | |
742 | entry->deferProducer(call); | |
743 | } | |
744 | ||
4dc2b072 | 745 | if (!spaceAvailable) { |
0ad2b63b CT |
746 | debugs(11, 5, HERE << "NOT storing " << contentSize << " bytes of adapted " << |
747 | "response body at offset " << adaptedBodySource->consumedSize()); | |
748 | return; | |
749 | } | |
e83cdc25 | 750 | |
0ad2b63b CT |
751 | if (spaceAvailable < contentSize ) { |
752 | debugs(11, 5, HERE << "postponing storage of " << | |
753 | (contentSize - spaceAvailable) << " body bytes"); | |
754 | contentSize = spaceAvailable; | |
0ad2b63b | 755 | } |
e83cdc25 | 756 | |
0ad2b63b CT |
757 | debugs(11,5, HERE << "storing " << contentSize << " bytes of adapted " << |
758 | "response body at offset " << adaptedBodySource->consumedSize()); | |
e83cdc25 | 759 | |
5f8252d2 | 760 | BodyPipeCheckout bpc(*adaptedBodySource); |
0ad2b63b CT |
761 | const StoreIOBuffer ioBuf(&bpc.buf, currentOffset, contentSize); |
762 | currentOffset += ioBuf.length; | |
5f8252d2 | 763 | entry->write(ioBuf); |
764 | bpc.buf.consume(contentSize); | |
765 | bpc.checkIn(); | |
766 | } | |
767 | ||
768 | // the entire adapted response body was produced, successfully | |
769 | void | |
770 | ServerStateData::handleAdaptedBodyProductionEnded() | |
771 | { | |
5f8252d2 | 772 | if (abortOnBadEntry("entry went bad while waiting for adapted body eof")) |
773 | return; | |
e83cdc25 A |
774 | |
775 | // end consumption if we consumed everything | |
0ad2b63b CT |
776 | if (adaptedBodySource != NULL && adaptedBodySource->exhausted()) |
777 | endAdaptedBodyConsumption(); | |
778 | // else resumeBodyStorage() will eventually consume the rest | |
779 | } | |
5f8252d2 | 780 | |
0ad2b63b CT |
781 | void |
782 | ServerStateData::endAdaptedBodyConsumption() | |
783 | { | |
784 | stopConsumingFrom(adaptedBodySource); | |
a83c6ed6 | 785 | handleAdaptationCompleted(); |
5f8252d2 | 786 | } |
787 | ||
788 | // premature end of the adapted response body | |
789 | void ServerStateData::handleAdaptedBodyProducerAborted() | |
790 | { | |
791 | stopConsumingFrom(adaptedBodySource); | |
a83c6ed6 | 792 | handleAdaptationAborted(); |
5f8252d2 | 793 | } |
794 | ||
a83c6ed6 | 795 | // common part of noteAdaptationAnswer and handleAdaptedBodyProductionEnded |
5f8252d2 | 796 | void |
a83c6ed6 | 797 | ServerStateData::handleAdaptationCompleted() |
5f8252d2 | 798 | { |
a83c6ed6 AR |
799 | debugs(11,5, HERE << "handleAdaptationCompleted"); |
800 | cleanAdaptation(); | |
bc81cb2b | 801 | |
802 | // We stop reading origin response because we have no place to put it and | |
803 | // cannot use it. If some origin servers do not like that or if we want to | |
804 | // reuse more pconns, we can add code to discard unneeded origin responses. | |
805 | if (!doneWithServer()) { | |
806 | debugs(11,3, HERE << "closing origin conn due to ICAP completion"); | |
807 | closeServer(); | |
808 | } | |
809 | ||
5f8252d2 | 810 | completeForwarding(); |
5f8252d2 | 811 | } |
812 | ||
a83c6ed6 | 813 | // common part of noteAdaptation*Aborted and noteBodyConsumerAborted methods |
5f8252d2 | 814 | void |
a83c6ed6 | 815 | ServerStateData::handleAdaptationAborted(bool bypassable) |
5f8252d2 | 816 | { |
a83c6ed6 | 817 | debugs(11,5, HERE << "handleAdaptationAborted; bypassable: " << bypassable << |
9e008dda | 818 | ", entry empty: " << entry->isEmpty()); |
5f8252d2 | 819 | |
820 | if (abortOnBadEntry("entry went bad while ICAP aborted")) | |
821 | return; | |
822 | ||
0f283edf | 823 | // TODO: bypass if possible |
824 | ||
5f8252d2 | 825 | if (entry->isEmpty()) { |
826 | debugs(11,9, HERE << "creating ICAP error entry after ICAP failure"); | |
913524f0 | 827 | ErrorState *err = new ErrorState(ERR_ICAP_FAILURE, HTTP_INTERNAL_SERVER_ERROR, request); |
129fe2a1 | 828 | err->detailError(ERR_DETAIL_ICAP_RESPMOD_EARLY); |
5f8252d2 | 829 | fwd->fail(err); |
830 | fwd->dontRetry(true); | |
b3c9f64a | 831 | } else if (request) { // update logged info directly |
64b66b76 CT |
832 | request->detailError(ERR_ICAP_FAILURE, ERR_DETAIL_ICAP_RESPMOD_LATE); |
833 | } | |
5f8252d2 | 834 | |
0f283edf | 835 | abortTransaction("ICAP failure"); |
5f8252d2 | 836 | } |
837 | ||
3af10ac0 AR |
838 | // adaptation service wants us to deny HTTP client access to this response |
839 | void | |
840 | ServerStateData::handleAdaptationBlocked(const Adaptation::Answer &answer) | |
841 | { | |
6666da11 | 842 | debugs(11,5, HERE << answer.ruleId); |
3af10ac0 AR |
843 | |
844 | if (abortOnBadEntry("entry went bad while ICAP aborted")) | |
845 | return; | |
846 | ||
847 | if (!entry->isEmpty()) { // too late to block (should not really happen) | |
ec4d1a1d A |
848 | if (request) |
849 | request->detailError(ERR_ICAP_FAILURE, ERR_DETAIL_RESPMOD_BLOCK_LATE); | |
850 | abortTransaction("late adaptation block"); | |
851 | return; | |
3af10ac0 | 852 | } |
ec4d1a1d | 853 | |
3af10ac0 AR |
854 | debugs(11,7, HERE << "creating adaptation block response"); |
855 | ||
856 | err_type page_id = | |
857 | aclGetDenyInfoPage(&Config.denyInfoList, answer.ruleId.termedBuf(), 1); | |
858 | if (page_id == ERR_NONE) | |
859 | page_id = ERR_ACCESS_DENIED; | |
860 | ||
913524f0 | 861 | ErrorState *err = new ErrorState(page_id, HTTP_FORBIDDEN, request); |
129fe2a1 | 862 | err->detailError(ERR_DETAIL_RESPMOD_BLOCK_EARLY); |
3af10ac0 AR |
863 | fwd->fail(err); |
864 | fwd->dontRetry(true); | |
865 | ||
866 | abortTransaction("timely adaptation block"); | |
867 | } | |
868 | ||
7c4e4e7f | 869 | void |
79628299 | 870 | ServerStateData::noteAdaptationAclCheckDone(Adaptation::ServiceGroupPointer group) |
7c4e4e7f | 871 | { |
a83c6ed6 | 872 | adaptationAccessCheckPending = false; |
7c4e4e7f | 873 | |
874 | if (abortOnBadEntry("entry went bad while waiting for ICAP ACL check")) | |
875 | return; | |
876 | ||
47416555 | 877 | // TODO: Should nonICAP and postICAP path check this on the server-side? |
878 | // That check now only happens on client-side, in processReplyAccess(). | |
879 | if (virginReply()->expectedBodyTooLarge(*request)) { | |
880 | sendBodyIsTooLargeError(); | |
881 | return; | |
882 | } | |
abd4b611 | 883 | // TODO: Should we check receivedBodyTooLarge on the server-side as well? |
47416555 | 884 | |
a22e6cd3 AR |
885 | if (!group) { |
886 | debugs(11,3, HERE << "no adapation needed"); | |
585ab260 | 887 | setFinalReply(virginReply()); |
7c4e4e7f | 888 | processReplyBody(); |
7c4e4e7f | 889 | return; |
890 | } | |
891 | ||
a22e6cd3 | 892 | startAdaptation(group, originalRequest()); |
7c4e4e7f | 893 | processReplyBody(); |
894 | } | |
0c25e715 | 895 | #endif |
7dc79973 | 896 | |
47416555 | 897 | void |
898 | ServerStateData::sendBodyIsTooLargeError() | |
899 | { | |
913524f0 | 900 | ErrorState *err = new ErrorState(ERR_TOO_BIG, HTTP_FORBIDDEN, request); |
47416555 | 901 | fwd->fail(err); |
902 | fwd->dontRetry(true); | |
903 | abortTransaction("Virgin body too large."); | |
904 | } | |
905 | ||
9e008dda | 906 | // TODO: when HttpStateData sends all errors to ICAP, |
585ab260 | 907 | // we should be able to move this at the end of setVirginReply(). |
7dc79973 | 908 | void |
585ab260 | 909 | ServerStateData::adaptOrFinalizeReply() |
7dc79973 | 910 | { |
a83c6ed6 | 911 | #if USE_ADAPTATION |
abd4b611 AR |
912 | // TODO: merge with client side and return void to hide the on/off logic? |
913 | // The callback can be called with a NULL service if adaptation is off. | |
914 | adaptationAccessCheckPending = Adaptation::AccessCheck::Start( | |
9e008dda | 915 | Adaptation::methodRespmod, Adaptation::pointPreCache, |
79628299 | 916 | originalRequest(), virginReply(), this); |
c30ac6ea | 917 | debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending); |
abd4b611 | 918 | if (adaptationAccessCheckPending) |
7dc79973 | 919 | return; |
7dc79973 | 920 | #endif |
921 | ||
585ab260 | 922 | setFinalReply(virginReply()); |
7dc79973 | 923 | } |
924 | ||
bae917ac CT |
925 | /// initializes bodyBytesRead stats if needed and applies delta |
926 | void | |
927 | ServerStateData::adjustBodyBytesRead(const int64_t delta) | |
928 | { | |
929 | int64_t &bodyBytesRead = originalRequest()->hier.bodyBytesRead; | |
930 | ||
931 | // if we got here, do not log a dash even if we got nothing from the server | |
932 | if (bodyBytesRead < 0) | |
933 | bodyBytesRead = 0; | |
934 | ||
935 | bodyBytesRead += delta; // supports negative and zero deltas | |
936 | ||
937 | // check for overflows ("infinite" response?) and undeflows (a bug) | |
938 | Must(bodyBytesRead >= 0); | |
939 | } | |
940 | ||
7dc79973 | 941 | void |
bc81cb2b | 942 | ServerStateData::addVirginReplyBody(const char *data, ssize_t len) |
7dc79973 | 943 | { |
bae917ac CT |
944 | adjustBodyBytesRead(len); |
945 | ||
a83c6ed6 AR |
946 | #if USE_ADAPTATION |
947 | assert(!adaptationAccessCheckPending); // or would need to buffer while waiting | |
948 | if (startedAdaptation) { | |
bc81cb2b | 949 | adaptVirginReplyBody(data, len); |
7dc79973 | 950 | return; |
951 | } | |
7dc79973 | 952 | #endif |
bc81cb2b | 953 | storeReplyBody(data, len); |
954 | } | |
7dc79973 | 955 | |
bc81cb2b | 956 | // writes virgin or adapted reply body to store |
957 | void | |
958 | ServerStateData::storeReplyBody(const char *data, ssize_t len) | |
959 | { | |
2d1a172f | 960 | // write even if len is zero to push headers towards the client side |
7dc79973 | 961 | entry->write (StoreIOBuffer(len, currentOffset, (char*)data)); |
962 | ||
963 | currentOffset += len; | |
964 | } | |
965 | ||
52edecde | 966 | size_t ServerStateData::replyBodySpace(const MemBuf &readBuf, |
1c9605c5 | 967 | const size_t minSpace) const |
7dc79973 | 968 | { |
52edecde AJ |
969 | size_t space = readBuf.spaceSize(); // available space w/o heroic measures |
970 | if (space < minSpace) { | |
971 | const size_t maxSpace = readBuf.potentialSpaceSize(); // absolute best | |
972 | space = min(minSpace, maxSpace); // do not promise more than asked | |
973 | } | |
974 | ||
a83c6ed6 | 975 | #if USE_ADAPTATION |
7dc79973 | 976 | if (responseBodyBuffer) { |
9e008dda | 977 | return 0; // Stop reading if already overflowed waiting for ICAP to catch up |
7dc79973 | 978 | } |
979 | ||
980 | if (virginBodyDestination != NULL) { | |
981 | /* | |
982 | * BodyPipe buffer has a finite size limit. We | |
983 | * should not read more data from the network than will fit | |
984 | * into the pipe buffer or we _lose_ what did not fit if | |
985 | * the response ends sooner that BodyPipe frees up space: | |
986 | * There is no code to keep pumping data into the pipe once | |
987 | * response ends and serverComplete() is called. | |
988 | * | |
989 | * If the pipe is totally full, don't register the read handler. | |
990 | * The BodyPipe will call our noteMoreBodySpaceAvailable() method | |
991 | * when it has free space again. | |
992 | */ | |
a83c6ed6 AR |
993 | size_t adaptation_space = |
994 | virginBodyDestination->buf().potentialSpaceSize(); | |
7dc79973 | 995 | |
a83c6ed6 | 996 | debugs(11,9, "ServerStateData may read up to min(" << |
9e008dda | 997 | adaptation_space << ", " << space << ") bytes"); |
7dc79973 | 998 | |
a83c6ed6 AR |
999 | if (adaptation_space < space) |
1000 | space = adaptation_space; | |
7dc79973 | 1001 | } |
1002 | #endif | |
1003 | ||
1004 | return space; | |
1005 | } |