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