]> git.ipfire.org Git - thirdparty/squid.git/blob - src/client_side_request.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / client_side_request.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 85 Client-side Request Routines
6 * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36
37 /*
38 * General logic of request processing:
39 *
40 * We run a series of tests to determine if access will be permitted, and to do
41 * any redirection. Then we call into the result clientStream to retrieve data.
42 * From that point on it's up to reply management.
43 */
44
45 #include "squid.h"
46 #include "clientStream.h"
47 #include "client_side_request.h"
48 #include "auth/UserRequest.h"
49 #include "HttpRequest.h"
50 #include "ProtoPort.h"
51 #include "acl/FilledChecklist.h"
52 #include "acl/Gadgets.h"
53 #include "client_side.h"
54 #include "client_side_reply.h"
55 #include "Store.h"
56 #include "HttpReply.h"
57 #include "MemObject.h"
58 #include "ClientRequestContext.h"
59 #include "SquidTime.h"
60 #include "wordlist.h"
61 #include "inet_pton.h"
62 #include "fde.h"
63
64 #if USE_ADAPTATION
65 #include "adaptation/AccessCheck.h"
66 #include "adaptation/Iterator.h"
67 #include "adaptation/Service.h"
68 #if ICAP_CLIENT
69 #include "adaptation/icap/History.h"
70 #endif
71 //static void adaptationAclCheckDoneWrapper(Adaptation::ServicePointer service, void *data);
72 #endif
73
74
75
76 #if LINGERING_CLOSE
77 #define comm_close comm_lingering_close
78 #endif
79
80 static const char *const crlf = "\r\n";
81
82 #if FOLLOW_X_FORWARDED_FOR
83 static void
84 clientFollowXForwardedForCheck(int answer, void *data);
85 #endif /* FOLLOW_X_FORWARDED_FOR */
86
87 CBDATA_CLASS_INIT(ClientRequestContext);
88
89 void *
90 ClientRequestContext::operator new (size_t size)
91 {
92 assert (size == sizeof(ClientRequestContext));
93 CBDATA_INIT_TYPE(ClientRequestContext);
94 ClientRequestContext *result = cbdataAlloc(ClientRequestContext);
95 return result;
96 }
97
98 void
99 ClientRequestContext::operator delete (void *address)
100 {
101 ClientRequestContext *t = static_cast<ClientRequestContext *>(address);
102 cbdataFree(t);
103 }
104
105 /* Local functions */
106 /* other */
107 static void clientAccessCheckDoneWrapper(int, void *);
108 static int clientHierarchical(ClientHttpRequest * http);
109 static void clientInterpretRequestHeaders(ClientHttpRequest * http);
110 static RH clientRedirectDoneWrapper;
111 static PF checkNoCacheDoneWrapper;
112 extern "C" CSR clientGetMoreData;
113 extern "C" CSS clientReplyStatus;
114 extern "C" CSD clientReplyDetach;
115 static void checkFailureRatio(err_type, hier_code);
116
117 ClientRequestContext::~ClientRequestContext()
118 {
119 /*
120 * Release our "lock" on our parent, ClientHttpRequest, if we
121 * still have one
122 */
123
124 if (http)
125 cbdataReferenceDone(http);
126
127 debugs(85,3, HERE << this << " ClientRequestContext destructed");
128 }
129
130 ClientRequestContext::ClientRequestContext(ClientHttpRequest *anHttp) : http(cbdataReference(anHttp)), acl_checklist (NULL), redirect_state (REDIRECT_NONE)
131 {
132 http_access_done = false;
133 redirect_done = false;
134 no_cache_done = false;
135 interpreted_req_hdrs = false;
136 debugs(85,3, HERE << this << " ClientRequestContext constructed");
137 }
138
139 CBDATA_CLASS_INIT(ClientHttpRequest);
140
141 void *
142 ClientHttpRequest::operator new (size_t size)
143 {
144 assert (size == sizeof (ClientHttpRequest));
145 CBDATA_INIT_TYPE(ClientHttpRequest);
146 ClientHttpRequest *result = cbdataAlloc(ClientHttpRequest);
147 return result;
148 }
149
150 void
151 ClientHttpRequest::operator delete (void *address)
152 {
153 ClientHttpRequest *t = static_cast<ClientHttpRequest *>(address);
154 cbdataFree(t);
155 }
156
157 ClientHttpRequest::ClientHttpRequest(ConnStateData * aConn) :
158 #if USE_ADAPTATION
159 AsyncJob("ClientHttpRequest"),
160 #endif
161 loggingEntry_(NULL)
162 {
163 start_time = current_time;
164 setConn(aConn);
165 dlinkAdd(this, &active, &ClientActiveRequests);
166 #if USE_ADAPTATION
167 request_satisfaction_mode = false;
168 #endif
169 }
170
171 /*
172 * returns true if client specified that the object must come from the cache
173 * without contacting origin server
174 */
175 bool
176 ClientHttpRequest::onlyIfCached()const
177 {
178 assert(request);
179 return request->cache_control &&
180 EBIT_TEST(request->cache_control->mask, CC_ONLY_IF_CACHED);
181 }
182
183 /*
184 * This function is designed to serve a fairly specific purpose.
185 * Occasionally our vBNS-connected caches can talk to each other, but not
186 * the rest of the world. Here we try to detect frequent failures which
187 * make the cache unusable (e.g. DNS lookup and connect() failures). If
188 * the failure:success ratio goes above 1.0 then we go into "hit only"
189 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
190 * will only fetch HITs from us if they are using the ICP protocol. We
191 * stay in this mode for 5 minutes.
192 *
193 * Duane W., Sept 16, 1996
194 */
195
196 #define FAILURE_MODE_TIME 300
197
198 static void
199 checkFailureRatio(err_type etype, hier_code hcode)
200 {
201 static double magic_factor = 100.0;
202 double n_good;
203 double n_bad;
204
205 if (hcode == HIER_NONE)
206 return;
207
208 n_good = magic_factor / (1.0 + request_failure_ratio);
209
210 n_bad = magic_factor - n_good;
211
212 switch (etype) {
213
214 case ERR_DNS_FAIL:
215
216 case ERR_CONNECT_FAIL:
217 case ERR_SECURE_CONNECT_FAIL:
218
219 case ERR_READ_ERROR:
220 n_bad++;
221 break;
222
223 default:
224 n_good++;
225 }
226
227 request_failure_ratio = n_bad / n_good;
228
229 if (hit_only_mode_until > squid_curtime)
230 return;
231
232 if (request_failure_ratio < 1.0)
233 return;
234
235 debugs(33, 0, "Failure Ratio at "<< std::setw(4)<<
236 std::setprecision(3) << request_failure_ratio);
237
238 debugs(33, 0, "Going into hit-only-mode for " <<
239 FAILURE_MODE_TIME / 60 << " minutes...");
240
241 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
242
243 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
244 }
245
246 ClientHttpRequest::~ClientHttpRequest()
247 {
248 debugs(33, 3, "httpRequestFree: " << uri);
249 PROF_start(httpRequestFree);
250
251 // Even though freeResources() below may destroy the request,
252 // we no longer set request->body_pipe to NULL here
253 // because we did not initiate that pipe (ConnStateData did)
254
255 /* the ICP check here was erroneous
256 * - StoreEntry::releaseRequest was always called if entry was valid
257 */
258 assert(logType < LOG_TYPE_MAX);
259
260 logRequest();
261
262 loggingEntry(NULL);
263
264 if (request)
265 checkFailureRatio(request->errType, al.hier.code);
266
267 freeResources();
268
269 #if USE_ADAPTATION
270 announceInitiatorAbort(virginHeadSource);
271
272 if (adaptedBodySource != NULL)
273 stopConsumingFrom(adaptedBodySource);
274 #endif
275
276 if (calloutContext)
277 delete calloutContext;
278
279 if (conn_)
280 cbdataReferenceDone(conn_);
281
282 /* moving to the next connection is handled by the context free */
283 dlinkDelete(&active, &ClientActiveRequests);
284
285 PROF_stop(httpRequestFree);
286 }
287
288 /* Create a request and kick it off */
289 /*
290 * TODO: Pass in the buffers to be used in the inital Read request, as they are
291 * determined by the user
292 */
293 int /* returns nonzero on failure */
294 clientBeginRequest(const HttpRequestMethod& method, char const *url, CSCB * streamcallback,
295 CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header,
296 char *tailbuf, size_t taillen)
297 {
298 size_t url_sz;
299 HttpVersion http_ver (1, 0);
300 ClientHttpRequest *http = new ClientHttpRequest(NULL);
301 HttpRequest *request;
302 StoreIOBuffer tempBuffer;
303 http->start_time = current_time;
304 /* this is only used to adjust the connection offset in client_side.c */
305 http->req_sz = 0;
306 tempBuffer.length = taillen;
307 tempBuffer.data = tailbuf;
308 /* client stream setup */
309 clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach,
310 clientReplyStatus, new clientReplyContext(http), streamcallback,
311 streamdetach, streamdata, tempBuffer);
312 /* make it visible in the 'current acctive requests list' */
313 /* Set flags */
314 /* internal requests only makes sense in an
315 * accelerator today. TODO: accept flags ? */
316 http->flags.accel = 1;
317 /* allow size for url rewriting */
318 url_sz = strlen(url) + Config.appendDomainLen + 5;
319 http->uri = (char *)xcalloc(url_sz, 1);
320 strcpy(http->uri, url);
321
322 if ((request = HttpRequest::CreateFromUrlAndMethod(http->uri, method)) == NULL) {
323 debugs(85, 5, "Invalid URL: " << http->uri);
324 return -1;
325 }
326
327 /*
328 * now update the headers in request with our supplied headers. urLParse
329 * should return a blank header set, but we use Update to be sure of
330 * correctness.
331 */
332 if (header)
333 request->header.update(header, NULL);
334
335 http->log_uri = xstrdup(urlCanonicalClean(request));
336
337 /* http struct now ready */
338
339 /*
340 * build new header list *? TODO
341 */
342 request->flags.accelerated = http->flags.accel;
343
344 request->flags.internalclient = 1;
345
346 /* this is an internally created
347 * request, not subject to acceleration
348 * target overrides */
349 /*
350 * FIXME? Do we want to detect and handle internal requests of internal
351 * objects ?
352 */
353
354 /* Internally created requests cannot have bodies today */
355 request->content_length = 0;
356
357 request->client_addr.SetNoAddr();
358
359 #if FOLLOW_X_FORWARDED_FOR
360 request->indirect_client_addr.SetNoAddr();
361 #endif /* FOLLOW_X_FORWARDED_FOR */
362
363 request->my_addr.SetNoAddr(); /* undefined for internal requests */
364
365 request->my_addr.SetPort(0);
366
367 request->http_ver = http_ver;
368
369 http->request = HTTPMSGLOCK(request);
370
371 /* optional - skip the access check ? */
372 http->calloutContext = new ClientRequestContext(http);
373
374 http->calloutContext->http_access_done = false;
375
376 http->calloutContext->redirect_done = true;
377
378 http->calloutContext->no_cache_done = true;
379
380 http->doCallouts();
381
382 return 0;
383 }
384
385 bool
386 ClientRequestContext::httpStateIsValid()
387 {
388 ClientHttpRequest *http_ = http;
389
390 if (cbdataReferenceValid(http_))
391 return true;
392
393 http = NULL;
394
395 cbdataReferenceDone(http_);
396
397 return false;
398 }
399
400 #if FOLLOW_X_FORWARDED_FOR
401 /**
402 * clientFollowXForwardedForCheck() checks the content of X-Forwarded-For:
403 * against the followXFF ACL, or cleans up and passes control to
404 * clientAccessCheck().
405 *
406 * The trust model here is a little ambiguous. So to clarify the logic:
407 * - we may always use the direct client address as the client IP.
408 * - these trust tests merey tell whether we trust given IP enough to believe the
409 * IP string which it appended to the X-Forwarded-For: header.
410 * - if at any point we don't trust what an IP adds we stop looking.
411 * - at that point the current contents of indirect_client_addr are the value set
412 * by the last previously trusted IP.
413 * ++ indirect_client_addr contains the remote direct client from the trusted peers viewpoint.
414 */
415 static void
416 clientFollowXForwardedForCheck(int answer, void *data)
417 {
418 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
419
420 if (!calloutContext->httpStateIsValid())
421 return;
422
423 ClientHttpRequest *http = calloutContext->http;
424 HttpRequest *request = http->request;
425
426 /*
427 * answer should be be ACCESS_ALLOWED or ACCESS_DENIED if we are
428 * called as a result of ACL checks, or -1 if we are called when
429 * there's nothing left to do.
430 */
431 if (answer == ACCESS_ALLOWED &&
432 request->x_forwarded_for_iterator.size () != 0) {
433
434 /*
435 * Remove the last comma-delimited element from the
436 * x_forwarded_for_iterator and use it to repeat the cycle.
437 */
438 const char *p;
439 const char *asciiaddr;
440 int l;
441 IpAddress addr;
442 p = request->x_forwarded_for_iterator.termedBuf();
443 l = request->x_forwarded_for_iterator.size();
444
445 /*
446 * XXX x_forwarded_for_iterator should really be a list of
447 * IP addresses, but it's a String instead. We have to
448 * walk backwards through the String, biting off the last
449 * comma-delimited part each time. As long as the data is in
450 * a String, we should probably implement and use a variant of
451 * strListGetItem() that walks backwards instead of forwards
452 * through a comma-separated list. But we don't even do that;
453 * we just do the work in-line here.
454 */
455 /* skip trailing space and commas */
456 while (l > 0 && (p[l-1] == ',' || xisspace(p[l-1])))
457 l--;
458 request->x_forwarded_for_iterator.cut(l);
459 /* look for start of last item in list */
460 while (l > 0 && ! (p[l-1] == ',' || xisspace(p[l-1])))
461 l--;
462 asciiaddr = p+l;
463 if ((addr = asciiaddr)) {
464 request->indirect_client_addr = addr;
465 request->x_forwarded_for_iterator.cut(l);
466 calloutContext->acl_checklist = clientAclChecklistCreate(Config.accessList.followXFF, http);
467 if (!Config.onoff.acl_uses_indirect_client) {
468 /* override the default src_addr tested if we have to go deeper than one level into XFF */
469 Filled(calloutContext->acl_checklist)->src_addr = request->indirect_client_addr;
470 }
471 calloutContext->acl_checklist->nonBlockingCheck(clientFollowXForwardedForCheck, data);
472 return;
473 }
474 } /*if (answer == ACCESS_ALLOWED &&
475 request->x_forwarded_for_iterator.size () != 0)*/
476
477 /* clean up, and pass control to clientAccessCheck */
478 if (Config.onoff.log_uses_indirect_client) {
479 /*
480 * Ensure that the access log shows the indirect client
481 * instead of the direct client.
482 */
483 ConnStateData *conn = http->getConn();
484 conn->log_addr = request->indirect_client_addr;
485 }
486 request->x_forwarded_for_iterator.clean();
487 request->flags.done_follow_x_forwarded_for = 1;
488
489 if (answer != ACCESS_ALLOWED && answer != ACCESS_DENIED) {
490 debugs(28, DBG_CRITICAL, "ERROR: Processing X-Forwarded-For. Stopping at IP address: " << request->indirect_client_addr );
491 }
492
493 /* process actual access ACL as normal. */
494 calloutContext->clientAccessCheck();
495 }
496 #endif /* FOLLOW_X_FORWARDED_FOR */
497
498 /* This is the entry point for external users of the client_side routines */
499 void
500 ClientRequestContext::clientAccessCheck()
501 {
502 #if FOLLOW_X_FORWARDED_FOR
503 if (!http->request->flags.done_follow_x_forwarded_for &&
504 Config.accessList.followXFF &&
505 http->request->header.has(HDR_X_FORWARDED_FOR)) {
506
507 /* we always trust the direct client address for actual use */
508 http->request->indirect_client_addr = http->request->client_addr;
509 http->request->indirect_client_addr.SetPort(0);
510
511 /* setup the XFF iterator for processing */
512 http->request->x_forwarded_for_iterator = http->request->header.getList(HDR_X_FORWARDED_FOR);
513
514 /* begin by checking to see if we trust direct client enough to walk XFF */
515 acl_checklist = clientAclChecklistCreate(Config.accessList.followXFF, http);
516 acl_checklist->nonBlockingCheck(clientFollowXForwardedForCheck, this);
517 return;
518 }
519 #endif /* FOLLOW_X_FORWARDED_FOR */
520
521 if (Config.accessList.http) {
522 acl_checklist = clientAclChecklistCreate(Config.accessList.http, http);
523 acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this);
524 } else {
525 debugs(0, DBG_CRITICAL, "No http_access configuration found. This will block ALL traffic");
526 clientAccessCheckDone(ACCESS_DENIED);
527 }
528 }
529
530 void
531 clientAccessCheckDoneWrapper(int answer, void *data)
532 {
533 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
534
535 if (!calloutContext->httpStateIsValid())
536 return;
537
538 calloutContext->clientAccessCheckDone(answer);
539 }
540
541 void
542 ClientRequestContext::clientAccessCheckDone(int answer)
543 {
544 acl_checklist = NULL;
545 err_type page_id;
546 http_status status;
547 debugs(85, 2, "The request " <<
548 RequestMethodStr(http->request->method) << " " <<
549 http->uri << " is " <<
550 (answer == ACCESS_ALLOWED ? "ALLOWED" : "DENIED") <<
551 ", because it matched '" <<
552 (AclMatchedName ? AclMatchedName : "NO ACL's") << "'" );
553 char const *proxy_auth_msg = "<null>";
554
555 if (http->getConn() != NULL && http->getConn()->auth_user_request != NULL)
556 proxy_auth_msg = http->getConn()->auth_user_request->denyMessage("<null>");
557 else if (http->request->auth_user_request != NULL)
558 proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>");
559
560 if (answer != ACCESS_ALLOWED) {
561 /* Send an error */
562 int require_auth = (answer == ACCESS_REQ_PROXY_AUTH || aclIsProxyAuth(AclMatchedName));
563 debugs(85, 5, "Access Denied: " << http->uri);
564 debugs(85, 5, "AclMatchedName = " << (AclMatchedName ? AclMatchedName : "<null>"));
565
566 if (require_auth)
567 debugs(33, 5, "Proxy Auth Message = " << (proxy_auth_msg ? proxy_auth_msg : "<null>"));
568
569 /*
570 * NOTE: get page_id here, based on AclMatchedName because if
571 * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in
572 * the clientCreateStoreEntry() call just below. Pedro Ribeiro
573 * <pribeiro@isel.pt>
574 */
575 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, answer != ACCESS_REQ_PROXY_AUTH);
576
577 http->logType = LOG_TCP_DENIED;
578
579 if (require_auth) {
580 if (!http->flags.accel) {
581 /* Proxy authorisation needed */
582 status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
583 } else {
584 /* WWW authorisation needed */
585 status = HTTP_UNAUTHORIZED;
586 }
587
588 if (page_id == ERR_NONE)
589 page_id = ERR_CACHE_ACCESS_DENIED;
590 } else {
591 status = HTTP_FORBIDDEN;
592
593 if (page_id == ERR_NONE)
594 page_id = ERR_ACCESS_DENIED;
595 }
596
597 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->prev->data;
598 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
599 assert (repContext);
600 IpAddress tmpnoaddr;
601 tmpnoaddr.SetNoAddr();
602 repContext->setReplyToError(page_id, status,
603 http->request->method, NULL,
604 http->getConn() != NULL ? http->getConn()->peer : tmpnoaddr,
605 http->request,
606 NULL,
607 http->getConn() != NULL && http->getConn()->auth_user_request ?
608 http->getConn()->auth_user_request : http->request->auth_user_request);
609
610 node = (clientStreamNode *)http->client_stream.tail->data;
611 clientStreamRead(node, http, node->readBuffer);
612 return;
613 }
614
615 /* ACCESS_ALLOWED continues here ... */
616 safe_free(http->uri);
617
618 http->uri = xstrdup(urlCanonical(http->request));
619
620 http->doCallouts();
621 }
622
623 #if USE_ADAPTATION
624 static void
625 adaptationAclCheckDoneWrapper(Adaptation::ServiceGroupPointer g, void *data)
626 {
627 ClientRequestContext *calloutContext = (ClientRequestContext *)data;
628
629 if (!calloutContext->httpStateIsValid())
630 return;
631
632 calloutContext->adaptationAclCheckDone(g);
633 }
634
635 void
636 ClientRequestContext::adaptationAclCheckDone(Adaptation::ServiceGroupPointer g)
637 {
638 debugs(93,3,HERE << this << " adaptationAclCheckDone called");
639 assert(http);
640
641 #if ICAP_CLIENT
642 Adaptation::Icap::History::Pointer ih = http->request->icapHistory();
643 if (ih != NULL) {
644 if (http->getConn() != NULL) {
645 ih->rfc931 = http->getConn()->rfc931;
646 #if USE_SSL
647 ih->ssluser = sslGetUserEmail(fd_table[http->getConn()->fd].ssl);
648 #endif
649 }
650 ih->log_uri = http->log_uri;
651 ih->req_sz = http->req_sz;
652 }
653 #endif
654
655 if (!g) {
656 debugs(85,3, HERE << "no adaptation needed");
657 http->doCallouts();
658 return;
659 }
660
661 http->startAdaptation(g);
662 }
663
664 #endif
665
666 static void
667 clientRedirectAccessCheckDone(int answer, void *data)
668 {
669 ClientRequestContext *context = (ClientRequestContext *)data;
670 ClientHttpRequest *http = context->http;
671 context->acl_checklist = NULL;
672
673 if (answer == ACCESS_ALLOWED)
674 redirectStart(http, clientRedirectDoneWrapper, context);
675 else
676 context->clientRedirectDone(NULL);
677 }
678
679 void
680 ClientRequestContext::clientRedirectStart()
681 {
682 debugs(33, 5, "clientRedirectStart: '" << http->uri << "'");
683
684 if (Config.accessList.redirector) {
685 acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http);
686 acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, this);
687 } else
688 redirectStart(http, clientRedirectDoneWrapper, this);
689 }
690
691 static int
692 clientHierarchical(ClientHttpRequest * http)
693 {
694 const char *url = http->uri;
695 HttpRequest *request = http->request;
696 HttpRequestMethod method = request->method;
697 const wordlist *p = NULL;
698
699 /*
700 * IMS needs a private key, so we can use the hierarchy for IMS only if our
701 * neighbors support private keys
702 */
703
704 if (request->flags.ims && !neighbors_do_private_keys)
705 return 0;
706
707 /*
708 * This is incorrect: authenticating requests can be sent via a hierarchy
709 * (they can even be cached if the correct headers are set on the reply)
710 */
711 if (request->flags.auth)
712 return 0;
713
714 if (method == METHOD_TRACE)
715 return 1;
716
717 if (method != METHOD_GET)
718 return 0;
719
720 /* scan hierarchy_stoplist */
721 for (p = Config.hierarchy_stoplist; p; p = p->next)
722 if (strstr(url, p->key))
723 return 0;
724
725 if (request->flags.loopdetect)
726 return 0;
727
728 if (request->protocol == PROTO_HTTP)
729 return httpCachable(method);
730
731 if (request->protocol == PROTO_GOPHER)
732 return gopherCachable(request);
733
734 if (request->protocol == PROTO_CACHEOBJ)
735 return 0;
736
737 return 1;
738 }
739
740
741 static void
742 clientCheckPinning(ClientHttpRequest * http)
743 {
744 HttpRequest *request = http->request;
745 HttpHeader *req_hdr = &request->header;
746 ConnStateData *http_conn = http->getConn();
747
748 /* Internal requests such as those from ESI includes may be without
749 * a client connection
750 */
751 if (!http_conn)
752 return;
753
754 request->flags.connection_auth_disabled = http_conn->port->connection_auth_disabled;
755 if (!request->flags.connection_auth_disabled) {
756 if (http_conn->pinning.fd != -1) {
757 if (http_conn->pinning.auth) {
758 request->flags.connection_auth = 1;
759 request->flags.auth = 1;
760 } else {
761 request->flags.connection_proxy_auth = 1;
762 }
763 request->setPinnedConnection(http_conn);
764 }
765 }
766
767 /* check if connection auth is used, and flag as candidate for pinning
768 * in such case.
769 * Note: we may need to set flags.connection_auth even if the connection
770 * is already pinned if it was pinned earlier due to proxy auth
771 */
772 if (!request->flags.connection_auth) {
773 if (req_hdr->has(HDR_AUTHORIZATION) || req_hdr->has(HDR_PROXY_AUTHORIZATION)) {
774 HttpHeaderPos pos = HttpHeaderInitPos;
775 HttpHeaderEntry *e;
776 int may_pin = 0;
777 while ((e = req_hdr->getEntry(&pos))) {
778 if (e->id == HDR_AUTHORIZATION || e->id == HDR_PROXY_AUTHORIZATION) {
779 const char *value = e->value.rawBuf();
780 if (strncasecmp(value, "NTLM ", 5) == 0
781 ||
782 strncasecmp(value, "Negotiate ", 10) == 0
783 ||
784 strncasecmp(value, "Kerberos ", 9) == 0) {
785 if (e->id == HDR_AUTHORIZATION) {
786 request->flags.connection_auth = 1;
787 may_pin = 1;
788 } else {
789 request->flags.connection_proxy_auth = 1;
790 may_pin = 1;
791 }
792 }
793 }
794 }
795 if (may_pin && !request->pinnedConnection()) {
796 request->setPinnedConnection(http->getConn());
797 }
798 }
799 }
800 }
801
802 static void
803 clientInterpretRequestHeaders(ClientHttpRequest * http)
804 {
805 HttpRequest *request = http->request;
806 HttpHeader *req_hdr = &request->header;
807 int no_cache = 0;
808 const char *str;
809
810 request->imslen = -1;
811 request->ims = req_hdr->getTime(HDR_IF_MODIFIED_SINCE);
812
813 if (request->ims > 0)
814 request->flags.ims = 1;
815
816 if (!request->flags.ignore_cc) {
817 if (req_hdr->has(HDR_PRAGMA)) {
818 String s = req_hdr->getList(HDR_PRAGMA);
819
820 if (strListIsMember(&s, "no-cache", ','))
821 no_cache++;
822
823 s.clean();
824 }
825
826 if (request->cache_control)
827 if (EBIT_TEST(request->cache_control->mask, CC_NO_CACHE))
828 no_cache++;
829
830 /*
831 * Work around for supporting the Reload button in IE browsers when Squid
832 * is used as an accelerator or transparent proxy, by turning accelerated
833 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
834 * actually only fixed in SP1, but we can't tell whether we are talking to
835 * SP1 or not so all 5.5 versions are treated 'normally').
836 */
837 if (Config.onoff.ie_refresh) {
838 if (http->flags.accel && request->flags.ims) {
839 if ((str = req_hdr->getStr(HDR_USER_AGENT))) {
840 if (strstr(str, "MSIE 5.01") != NULL)
841 no_cache++;
842 else if (strstr(str, "MSIE 5.0") != NULL)
843 no_cache++;
844 else if (strstr(str, "MSIE 4.") != NULL)
845 no_cache++;
846 else if (strstr(str, "MSIE 3.") != NULL)
847 no_cache++;
848 }
849 }
850 }
851 }
852
853 if (request->method == METHOD_OTHER) {
854 no_cache++;
855 }
856
857 if (no_cache) {
858 #if HTTP_VIOLATIONS
859
860 if (Config.onoff.reload_into_ims)
861 request->flags.nocache_hack = 1;
862 else if (refresh_nocache_hack)
863 request->flags.nocache_hack = 1;
864 else
865 #endif
866
867 request->flags.nocache = 1;
868 }
869
870 /* ignore range header in non-GETs or non-HEADs */
871 if (request->method == METHOD_GET || request->method == METHOD_HEAD) {
872 request->range = req_hdr->getRange();
873
874 if (request->range) {
875 request->flags.range = 1;
876 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data;
877 /* XXX: This is suboptimal. We should give the stream the range set,
878 * and thereby let the top of the stream set the offset when the
879 * size becomes known. As it is, we will end up requesting from 0
880 * for evey -X range specification.
881 * RBC - this may be somewhat wrong. We should probably set the range
882 * iter up at this point.
883 */
884 node->readBuffer.offset = request->range->lowestOffset(0);
885 http->range_iter.pos = request->range->begin();
886 http->range_iter.valid = true;
887 }
888 }
889
890 /* Only HEAD and GET requests permit a Range or Request-Range header.
891 * If these headers appear on any other type of request, delete them now.
892 */
893 else {
894 req_hdr->delById(HDR_RANGE);
895 req_hdr->delById(HDR_REQUEST_RANGE);
896 request->range = NULL;
897 }
898
899 if (req_hdr->has(HDR_AUTHORIZATION))
900 request->flags.auth = 1;
901
902 clientCheckPinning(http);
903
904 if (request->login[0] != '\0')
905 request->flags.auth = 1;
906
907 if (req_hdr->has(HDR_VIA)) {
908 String s = req_hdr->getList(HDR_VIA);
909 /*
910 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
911 * Note ThisCache2 has a space prepended to the hostname so we don't
912 * accidentally match super-domains.
913 */
914
915 if (strListIsSubstr(&s, ThisCache2, ',')) {
916 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
917 request, (ObjPackMethod) & httpRequestPack);
918 request->flags.loopdetect = 1;
919 }
920
921 #if FORW_VIA_DB
922 fvdbCountVia(s.termedBuf());
923
924 #endif
925
926 s.clean();
927 }
928
929 /**
930 \todo --enable-useragent-log and --enable-referer-log. We should
931 probably drop those two as the custom log formats accomplish pretty much the same thing..
932 */
933 #if USE_USERAGENT_LOG
934 if ((str = req_hdr->getStr(HDR_USER_AGENT)))
935 logUserAgent(fqdnFromAddr(http->getConn()->log_addr), str);
936
937 #endif
938 #if USE_REFERER_LOG
939
940 if ((str = req_hdr->getStr(HDR_REFERER)))
941 logReferer(fqdnFromAddr(http->getConn()->log_addr), str, http->log_uri);
942
943 #endif
944 #if FORW_VIA_DB
945
946 if (req_hdr->has(HDR_X_FORWARDED_FOR)) {
947 String s = req_hdr->getList(HDR_X_FORWARDED_FOR);
948 fvdbCountForw(s.termedBuf());
949 s.clean();
950 }
951
952 #endif
953 if (request->method == METHOD_TRACE || request->method == METHOD_OPTIONS) {
954 request->max_forwards = req_hdr->getInt64(HDR_MAX_FORWARDS);
955 }
956
957 request->flags.cachable = http->request->cacheable();
958
959 if (clientHierarchical(http))
960 request->flags.hierarchical = 1;
961
962 debugs(85, 5, "clientInterpretRequestHeaders: REQ_NOCACHE = " <<
963 (request->flags.nocache ? "SET" : "NOT SET"));
964 debugs(85, 5, "clientInterpretRequestHeaders: REQ_CACHABLE = " <<
965 (request->flags.cachable ? "SET" : "NOT SET"));
966 debugs(85, 5, "clientInterpretRequestHeaders: REQ_HIERARCHICAL = " <<
967 (request->flags.hierarchical ? "SET" : "NOT SET"));
968
969 }
970
971 void
972 clientRedirectDoneWrapper(void *data, char *result)
973 {
974 ClientRequestContext *calloutContext = (ClientRequestContext *)data;
975
976 if (!calloutContext->httpStateIsValid())
977 return;
978
979 calloutContext->clientRedirectDone(result);
980 }
981
982 void
983 ClientRequestContext::clientRedirectDone(char *result)
984 {
985 HttpRequest *new_request = NULL;
986 HttpRequest *old_request = http->request;
987 debugs(85, 5, "clientRedirectDone: '" << http->uri << "' result=" << (result ? result : "NULL"));
988 assert(redirect_state == REDIRECT_PENDING);
989 redirect_state = REDIRECT_DONE;
990
991 if (result) {
992 http_status status = (http_status) atoi(result);
993
994 if (status == HTTP_MOVED_PERMANENTLY
995 || status == HTTP_MOVED_TEMPORARILY
996 || status == HTTP_SEE_OTHER
997 || status == HTTP_TEMPORARY_REDIRECT) {
998 char *t = result;
999
1000 if ((t = strchr(result, ':')) != NULL) {
1001 http->redirect.status = status;
1002 http->redirect.location = xstrdup(t + 1);
1003 } else {
1004 debugs(85, 1, "clientRedirectDone: bad input: " << result);
1005 }
1006 } else if (strcmp(result, http->uri))
1007 new_request = HttpRequest::CreateFromUrlAndMethod(result, old_request->method);
1008 }
1009
1010 if (new_request) {
1011 safe_free(http->uri);
1012 http->uri = xstrdup(urlCanonical(new_request));
1013 new_request->http_ver = old_request->http_ver;
1014 new_request->header.append(&old_request->header);
1015 new_request->client_addr = old_request->client_addr;
1016 #if FOLLOW_X_FORWARDED_FOR
1017 new_request->indirect_client_addr = old_request->indirect_client_addr;
1018 #endif /* FOLLOW_X_FORWARDED_FOR */
1019 new_request->my_addr = old_request->my_addr;
1020 new_request->flags = old_request->flags;
1021 new_request->flags.redirected = 1;
1022
1023 if (old_request->auth_user_request) {
1024 new_request->auth_user_request = old_request->auth_user_request;
1025 AUTHUSERREQUESTLOCK(new_request->auth_user_request, "new request");
1026 }
1027
1028 if (old_request->body_pipe != NULL) {
1029 new_request->body_pipe = old_request->body_pipe;
1030 old_request->body_pipe = NULL;
1031 debugs(0,0,HERE << "redirecting body_pipe " << new_request->body_pipe << " from request " << old_request << " to " << new_request);
1032 }
1033
1034 new_request->content_length = old_request->content_length;
1035 new_request->extacl_user = old_request->extacl_user;
1036 new_request->extacl_passwd = old_request->extacl_passwd;
1037 new_request->flags.proxy_keepalive = old_request->flags.proxy_keepalive;
1038 HTTPMSGUNLOCK(old_request);
1039 http->request = HTTPMSGLOCK(new_request);
1040 }
1041
1042 /* FIXME PIPELINE: This is innacurate during pipelining */
1043
1044 if (http->getConn() != NULL)
1045 fd_note(http->getConn()->fd, http->uri);
1046
1047 assert(http->uri);
1048
1049 http->doCallouts();
1050 }
1051
1052 /** Test cache allow/deny configuration
1053 * Sets flags.cachable=1 if caching is not denied.
1054 */
1055 void
1056 ClientRequestContext::checkNoCache()
1057 {
1058 if (Config.accessList.noCache) {
1059 acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http);
1060 acl_checklist->nonBlockingCheck(checkNoCacheDoneWrapper, this);
1061 } else {
1062 /* unless otherwise specified, we try to cache. */
1063 checkNoCacheDone(1);
1064 }
1065 }
1066
1067 static void
1068 checkNoCacheDoneWrapper(int answer, void *data)
1069 {
1070 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
1071
1072 if (!calloutContext->httpStateIsValid())
1073 return;
1074
1075 calloutContext->checkNoCacheDone(answer);
1076 }
1077
1078 void
1079 ClientRequestContext::checkNoCacheDone(int answer)
1080 {
1081 acl_checklist = NULL;
1082 http->request->flags.cachable = answer;
1083 http->doCallouts();
1084 }
1085
1086 /*
1087 * Identify requests that do not go through the store and client side stream
1088 * and forward them to the appropriate location. All other requests, request
1089 * them.
1090 */
1091 void
1092 ClientHttpRequest::processRequest()
1093 {
1094 debugs(85, 4, "clientProcessRequest: " << RequestMethodStr(request->method) << " '" << uri << "'");
1095
1096 #if USE_SSL
1097 if (request->method == METHOD_CONNECT && sslBumpNeeded()) {
1098 sslBumpStart();
1099 return;
1100 }
1101 #endif
1102
1103 if (request->method == METHOD_CONNECT && !redirect.status) {
1104 logType = LOG_TCP_MISS;
1105 tunnelStart(this, &out.size, &al.http.code);
1106 return;
1107 }
1108
1109 httpStart();
1110 }
1111
1112 void
1113 ClientHttpRequest::httpStart()
1114 {
1115 PROF_start(httpStart);
1116 logType = LOG_TAG_NONE;
1117 debugs(85, 4, "ClientHttpRequest::httpStart: " << log_tags[logType] << " for '" << uri << "'");
1118
1119 /* no one should have touched this */
1120 assert(out.offset == 0);
1121 /* Use the Stream Luke */
1122 clientStreamNode *node = (clientStreamNode *)client_stream.tail->data;
1123 clientStreamRead(node, this, node->readBuffer);
1124 PROF_stop(httpStart);
1125 }
1126
1127 #if USE_SSL
1128
1129 // determines whether we should bump the CONNECT request
1130 bool
1131 ClientHttpRequest::sslBumpNeeded() const
1132 {
1133 if (!getConn()->port->sslBump || !Config.accessList.ssl_bump)
1134 return false;
1135
1136 debugs(85, 5, HERE << "SslBump possible, checking ACL");
1137
1138 ACLFilledChecklist check(Config.accessList.ssl_bump, request, NULL);
1139 check.src_addr = request->client_addr;
1140 check.my_addr = request->my_addr;
1141 return check.fastCheck() == 1;
1142 }
1143
1144 // called when comm_write has completed
1145 static void
1146 SslBumpEstablish(int, char *, size_t, comm_err_t errflag, int, void *data)
1147 {
1148 ClientHttpRequest *r = static_cast<ClientHttpRequest*>(data);
1149 debugs(85, 5, HERE << "responded to CONNECT: " << r << " ? " << errflag);
1150
1151 assert(r && cbdataReferenceValid(r));
1152 r->sslBumpEstablish(errflag);
1153 }
1154
1155 void
1156 ClientHttpRequest::sslBumpEstablish(comm_err_t errflag)
1157 {
1158 // Bail out quickly on COMM_ERR_CLOSING - close handlers will tidy up
1159 if (errflag == COMM_ERR_CLOSING)
1160 return;
1161
1162 if (errflag) {
1163 getConn()->startClosing("CONNECT response failure in SslBump");
1164 return;
1165 }
1166
1167 getConn()->switchToHttps();
1168 }
1169
1170 void
1171 ClientHttpRequest::sslBumpStart()
1172 {
1173 debugs(85, 5, HERE << "ClientHttpRequest::sslBumpStart");
1174
1175 // send an HTTP 200 response to kick client SSL negotiation
1176 const int fd = getConn()->fd;
1177 debugs(33, 7, HERE << "Confirming CONNECT tunnel on FD " << fd);
1178
1179 // TODO: Unify with tunnel.cc and add a Server(?) header
1180 static const char *const conn_established =
1181 "HTTP/1.0 200 Connection established\r\n\r\n";
1182 comm_write(fd, conn_established, strlen(conn_established),
1183 &SslBumpEstablish, this, NULL);
1184 }
1185
1186 #endif
1187
1188 bool
1189 ClientHttpRequest::gotEnough() const
1190 {
1191 /** TODO: should be querying the stream. */
1192 int64_t contentLength =
1193 memObject()->getReply()->bodySize(request->method);
1194 assert(contentLength >= 0);
1195
1196 if (out.offset < contentLength)
1197 return false;
1198
1199 return true;
1200 }
1201
1202 void
1203 ClientHttpRequest::storeEntry(StoreEntry *newEntry)
1204 {
1205 entry_ = newEntry;
1206 }
1207
1208 void
1209 ClientHttpRequest::loggingEntry(StoreEntry *newEntry)
1210 {
1211 if (loggingEntry_)
1212 loggingEntry_->unlock();
1213
1214 loggingEntry_ = newEntry;
1215
1216 if (loggingEntry_)
1217 loggingEntry_->lock();
1218 }
1219
1220 /*
1221 * doCallouts() - This function controls the order of "callout"
1222 * executions, including non-blocking access control checks, the
1223 * redirector, and ICAP. Previously, these callouts were chained
1224 * together such that "clientAccessCheckDone()" would call
1225 * "clientRedirectStart()" and so on.
1226 *
1227 * The ClientRequestContext (aka calloutContext) class holds certain
1228 * state data for the callout/callback operations. Previously
1229 * ClientHttpRequest would sort of hand off control to ClientRequestContext
1230 * for a short time. ClientRequestContext would then delete itself
1231 * and pass control back to ClientHttpRequest when all callouts
1232 * were finished.
1233 *
1234 * This caused some problems for ICAP because we want to make the
1235 * ICAP callout after checking ACLs, but before checking the no_cache
1236 * list. We can't stuff the ICAP state into the ClientRequestContext
1237 * class because we still need the ICAP state after ClientRequestContext
1238 * goes away.
1239 *
1240 * Note that ClientRequestContext is created before the first call
1241 * to doCallouts().
1242 *
1243 * If one of the callouts notices that ClientHttpRequest is no
1244 * longer valid, it should call cbdataReferenceDone() so that
1245 * ClientHttpRequest's reference count goes to zero and it will get
1246 * deleted. ClientHttpRequest will then delete ClientRequestContext.
1247 *
1248 * Note that we set the _done flags here before actually starting
1249 * the callout. This is strictly for convenience.
1250 */
1251
1252 extern int aclMapTOS (acl_tos * head, ACLChecklist * ch);
1253
1254 void
1255 ClientHttpRequest::doCallouts()
1256 {
1257 assert(calloutContext);
1258
1259 if (!calloutContext->http_access_done) {
1260 debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck()");
1261 calloutContext->http_access_done = true;
1262 calloutContext->clientAccessCheck();
1263 return;
1264 }
1265
1266 #if USE_ADAPTATION
1267 if (!calloutContext->adaptation_acl_check_done) {
1268 calloutContext->adaptation_acl_check_done = true;
1269 if (Adaptation::AccessCheck::Start(
1270 Adaptation::methodReqmod, Adaptation::pointPreCache,
1271 request, NULL, adaptationAclCheckDoneWrapper, calloutContext))
1272 return; // will call callback
1273 }
1274 #endif
1275
1276 if (!calloutContext->redirect_done) {
1277 calloutContext->redirect_done = true;
1278 assert(calloutContext->redirect_state == REDIRECT_NONE);
1279
1280 if (Config.Program.redirect) {
1281 debugs(83, 3, HERE << "Doing calloutContext->clientRedirectStart()");
1282 calloutContext->redirect_state = REDIRECT_PENDING;
1283 calloutContext->clientRedirectStart();
1284 return;
1285 }
1286 }
1287
1288 if (!calloutContext->interpreted_req_hdrs) {
1289 debugs(83, 3, HERE << "Doing clientInterpretRequestHeaders()");
1290 calloutContext->interpreted_req_hdrs = 1;
1291 clientInterpretRequestHeaders(this);
1292 }
1293
1294 if (!calloutContext->no_cache_done) {
1295 calloutContext->no_cache_done = true;
1296
1297 if (Config.accessList.noCache && request->flags.cachable) {
1298 debugs(83, 3, HERE << "Doing calloutContext->checkNoCache()");
1299 calloutContext->checkNoCache();
1300 return;
1301 }
1302 }
1303
1304 if (!calloutContext->clientside_tos_done) {
1305 calloutContext->clientside_tos_done = true;
1306 if (getConn() != NULL) {
1307 ACLFilledChecklist ch(NULL, request, NULL);
1308 ch.src_addr = request->client_addr;
1309 ch.my_addr = request->my_addr;
1310 int tos = aclMapTOS(Config.accessList.clientside_tos, &ch);
1311 if (tos)
1312 comm_set_tos(getConn()->fd, tos);
1313 }
1314 }
1315
1316 cbdataReferenceDone(calloutContext->http);
1317 delete calloutContext;
1318 calloutContext = NULL;
1319 #if HEADERS_LOG
1320
1321 headersLog(0, 1, request->method, request);
1322 #endif
1323
1324 debugs(83, 3, HERE << "calling processRequest()");
1325 processRequest();
1326
1327 #if ICAP_CLIENT
1328 Adaptation::Icap::History::Pointer ih = request->icapHistory();
1329 if (ih != NULL)
1330 ih->logType = logType;
1331 #endif
1332 }
1333
1334 #ifndef _USE_INLINE_
1335 #include "client_side_request.cci"
1336 #endif
1337
1338 #if USE_ADAPTATION
1339 /// Initiate an asynchronous adaptation transaction which will call us back.
1340 void
1341 ClientHttpRequest::startAdaptation(const Adaptation::ServiceGroupPointer &g)
1342 {
1343 debugs(85, 3, HERE << "adaptation needed for " << this);
1344 assert(!virginHeadSource);
1345 assert(!adaptedBodySource);
1346 virginHeadSource = initiateAdaptation(
1347 new Adaptation::Iterator(this, request, NULL, g));
1348
1349 // we could try to guess whether we can bypass this adaptation
1350 // initiation failure, but it should not really happen
1351 assert(virginHeadSource != NULL); // Must, really
1352 }
1353
1354 void
1355 ClientHttpRequest::noteAdaptationAnswer(HttpMsg *msg)
1356 {
1357 assert(cbdataReferenceValid(this)); // indicates bug
1358 assert(msg);
1359
1360 if (HttpRequest *new_req = dynamic_cast<HttpRequest*>(msg)) {
1361 /*
1362 * Replace the old request with the new request.
1363 */
1364 HTTPMSGUNLOCK(request);
1365 request = HTTPMSGLOCK(new_req);
1366 /*
1367 * Store the new URI for logging
1368 */
1369 xfree(uri);
1370 uri = xstrdup(urlCanonical(request));
1371 setLogUri(this, urlCanonicalClean(request));
1372 assert(request->method.id());
1373 } else if (HttpReply *new_rep = dynamic_cast<HttpReply*>(msg)) {
1374 debugs(85,3,HERE << "REQMOD reply is HTTP reply");
1375
1376 // subscribe to receive reply body
1377 if (new_rep->body_pipe != NULL) {
1378 adaptedBodySource = new_rep->body_pipe;
1379 int consumer_ok = adaptedBodySource->setConsumerIfNotLate(this);
1380 assert(consumer_ok);
1381 }
1382
1383 clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data;
1384 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
1385 repContext->createStoreEntry(request->method, request->flags);
1386
1387 EBIT_CLR(storeEntry()->flags, ENTRY_FWD_HDR_WAIT);
1388 request_satisfaction_mode = true;
1389 request_satisfaction_offset = 0;
1390 storeEntry()->replaceHttpReply(new_rep);
1391 storeEntry()->timestampsSet();
1392
1393 if (!adaptedBodySource) // no body
1394 storeEntry()->complete();
1395 clientGetMoreData(node, this);
1396 }
1397
1398 // we are done with getting headers (but may be receiving body)
1399 clearAdaptation(virginHeadSource);
1400
1401 if (!request_satisfaction_mode)
1402 doCallouts();
1403 }
1404
1405 void
1406 ClientHttpRequest::noteAdaptationQueryAbort(bool final)
1407 {
1408 clearAdaptation(virginHeadSource);
1409 assert(!adaptedBodySource);
1410 handleAdaptationFailure(!final);
1411 }
1412
1413 void
1414 ClientHttpRequest::noteMoreBodyDataAvailable(BodyPipe::Pointer)
1415 {
1416 assert(request_satisfaction_mode);
1417 assert(adaptedBodySource != NULL);
1418
1419 if (const size_t contentSize = adaptedBodySource->buf().contentSize()) {
1420 BodyPipeCheckout bpc(*adaptedBodySource);
1421 const StoreIOBuffer ioBuf(&bpc.buf, request_satisfaction_offset);
1422 storeEntry()->write(ioBuf);
1423 // assume can write everything
1424 request_satisfaction_offset += contentSize;
1425 bpc.buf.consume(contentSize);
1426 bpc.checkIn();
1427 }
1428
1429 if (adaptedBodySource->exhausted())
1430 endRequestSatisfaction();
1431 // else wait for more body data
1432 }
1433
1434 void
1435 ClientHttpRequest::noteBodyProductionEnded(BodyPipe::Pointer)
1436 {
1437 assert(!virginHeadSource);
1438 if (adaptedBodySource != NULL) { // did not end request satisfaction yet
1439 // We do not expect more because noteMoreBodyDataAvailable always
1440 // consumes everything. We do not even have a mechanism to consume
1441 // leftovers after noteMoreBodyDataAvailable notifications seize.
1442 assert(adaptedBodySource->exhausted());
1443 endRequestSatisfaction();
1444 }
1445 }
1446
1447 void
1448 ClientHttpRequest::endRequestSatisfaction()
1449 {
1450 debugs(85,4, HERE << this << " ends request satisfaction");
1451 assert(request_satisfaction_mode);
1452 stopConsumingFrom(adaptedBodySource);
1453
1454 // TODO: anything else needed to end store entry formation correctly?
1455 storeEntry()->complete();
1456 }
1457
1458 void
1459 ClientHttpRequest::noteBodyProducerAborted(BodyPipe::Pointer)
1460 {
1461 assert(!virginHeadSource);
1462 stopConsumingFrom(adaptedBodySource);
1463 handleAdaptationFailure();
1464 }
1465
1466 void
1467 ClientHttpRequest::handleAdaptationFailure(bool bypassable)
1468 {
1469 debugs(85,3, HERE << "handleAdaptationFailure(" << bypassable << ")");
1470
1471 const bool usedStore = storeEntry() && !storeEntry()->isEmpty();
1472 const bool usedPipe = request->body_pipe != NULL &&
1473 request->body_pipe->consumedSize() > 0;
1474
1475 if (bypassable && !usedStore && !usedPipe) {
1476 debugs(85,3, HERE << "ICAP REQMOD callout failed, bypassing: " << calloutContext);
1477 if (calloutContext)
1478 doCallouts();
1479 return;
1480 }
1481
1482 debugs(85,3, HERE << "ICAP REQMOD callout failed, responding with error");
1483
1484 clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data;
1485 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
1486 assert(repContext);
1487
1488 // The original author of the code also wanted to pass an errno to
1489 // setReplyToError, but it seems unlikely that the errno reflects the
1490 // true cause of the error at this point, so I did not pass it.
1491 IpAddress noAddr;
1492 noAddr.SetNoAddr();
1493 ConnStateData * c = getConn();
1494 repContext->setReplyToError(ERR_ICAP_FAILURE, HTTP_INTERNAL_SERVER_ERROR,
1495 request->method, NULL,
1496 (c != NULL ? c->peer : noAddr), request, NULL,
1497 (c != NULL && c->auth_user_request ?
1498 c->auth_user_request : request->auth_user_request));
1499
1500 node = (clientStreamNode *)client_stream.tail->data;
1501 clientStreamRead(node, this, node->readBuffer);
1502 }
1503
1504 #endif