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