]> git.ipfire.org Git - thirdparty/squid.git/blob - src/client_side_request.cc
Import IPv6 support from squid3-ipv6 branch to 3-HEAD.
[thirdparty/squid.git] / src / client_side_request.cc
1
2 /*
3 * $Id: client_side_request.cc,v 1.97 2007/12/14 23:11:46 amosjeffries Exp $
4 *
5 * DEBUG: section 85 Client-side Request Routines
6 * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36
37 /*
38 * General logic of request processing:
39 *
40 * We run a series of tests to determine if access will be permitted, and to do
41 * any redirection. Then we call into the result clientStream to retrieve data.
42 * From that point on it's up to reply management.
43 */
44
45 #include "squid.h"
46 #include "clientStream.h"
47 #include "client_side_request.h"
48 #include "AuthUserRequest.h"
49 #include "HttpRequest.h"
50 #include "ACLChecklist.h"
51 #include "ACL.h"
52 #include "client_side.h"
53 #include "client_side_reply.h"
54 #include "Store.h"
55 #include "HttpReply.h"
56 #include "MemObject.h"
57 #include "ClientRequestContext.h"
58 #include "SquidTime.h"
59 #include "wordlist.h"
60
61 #if ICAP_CLIENT
62 #include "ICAP/ICAPModXact.h"
63 #include "ICAP/ICAPElements.h"
64 #include "ICAP/ICAPConfig.h"
65 static void icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service, void *data);
66 extern ICAPConfig TheICAPConfig;
67 #endif
68
69 #if LINGERING_CLOSE
70 #define comm_close comm_lingering_close
71 #endif
72
73 static const char *const crlf = "\r\n";
74
75 CBDATA_CLASS_INIT(ClientRequestContext);
76
77 void *
78 ClientRequestContext::operator new (size_t size)
79 {
80 assert (size == sizeof(ClientRequestContext));
81 CBDATA_INIT_TYPE(ClientRequestContext);
82 ClientRequestContext *result = cbdataAlloc(ClientRequestContext);
83 return result;
84 }
85
86 void
87 ClientRequestContext::operator delete (void *address)
88 {
89 ClientRequestContext *t = static_cast<ClientRequestContext *>(address);
90 cbdataFree(t);
91 }
92
93 /* Local functions */
94 /* other */
95 static void clientAccessCheckDoneWrapper(int, void *);
96 static int clientHierarchical(ClientHttpRequest * http);
97 static void clientInterpretRequestHeaders(ClientHttpRequest * http);
98 static RH clientRedirectDoneWrapper;
99 static PF checkNoCacheDoneWrapper;
100 extern "C" CSR clientGetMoreData;
101 extern "C" CSS clientReplyStatus;
102 extern "C" CSD clientReplyDetach;
103 static void checkFailureRatio(err_type, hier_code);
104
105 ClientRequestContext::~ClientRequestContext()
106 {
107 /*
108 * Release our "lock" on our parent, ClientHttpRequest, if we
109 * still have one
110 */
111
112 if (http)
113 cbdataReferenceDone(http);
114
115 debugs(85,3, HERE << this << " ClientRequestContext destructed");
116 }
117
118 ClientRequestContext::ClientRequestContext(ClientHttpRequest *anHttp) : http(cbdataReference(anHttp)), acl_checklist (NULL), redirect_state (REDIRECT_NONE)
119 {
120 http_access_done = false;
121 redirect_done = false;
122 no_cache_done = false;
123 interpreted_req_hdrs = false;
124 debugs(85,3, HERE << this << " ClientRequestContext constructed");
125 }
126
127 CBDATA_CLASS_INIT(ClientHttpRequest);
128
129 void *
130 ClientHttpRequest::operator new (size_t size)
131 {
132 assert (size == sizeof (ClientHttpRequest));
133 CBDATA_INIT_TYPE(ClientHttpRequest);
134 ClientHttpRequest *result = cbdataAlloc(ClientHttpRequest);
135 return result;
136 }
137
138 void
139 ClientHttpRequest::operator delete (void *address)
140 {
141 ClientHttpRequest *t = static_cast<ClientHttpRequest *>(address);
142 cbdataFree(t);
143 }
144
145 ClientHttpRequest::ClientHttpRequest(ConnStateData::Pointer aConn) : loggingEntry_(NULL)
146 {
147 start = current_time;
148 setConn(aConn);
149 dlinkAdd(this, &active, &ClientActiveRequests);
150 #if ICAP_CLIENT
151 request_satisfaction_mode = false;
152 #endif
153 }
154
155 /*
156 * returns true if client specified that the object must come from the cache
157 * without contacting origin server
158 */
159 bool
160 ClientHttpRequest::onlyIfCached()const
161 {
162 assert(request);
163 return request->cache_control &&
164 EBIT_TEST(request->cache_control->mask, CC_ONLY_IF_CACHED);
165 }
166
167 /*
168 * This function is designed to serve a fairly specific purpose.
169 * Occasionally our vBNS-connected caches can talk to each other, but not
170 * the rest of the world. Here we try to detect frequent failures which
171 * make the cache unusable (e.g. DNS lookup and connect() failures). If
172 * the failure:success ratio goes above 1.0 then we go into "hit only"
173 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
174 * will only fetch HITs from us if they are using the ICP protocol. We
175 * stay in this mode for 5 minutes.
176 *
177 * Duane W., Sept 16, 1996
178 */
179
180 #define FAILURE_MODE_TIME 300
181
182 static void
183 checkFailureRatio(err_type etype, hier_code hcode)
184 {
185 static double magic_factor = 100.0;
186 double n_good;
187 double n_bad;
188
189 if (hcode == HIER_NONE)
190 return;
191
192 n_good = magic_factor / (1.0 + request_failure_ratio);
193
194 n_bad = magic_factor - n_good;
195
196 switch (etype) {
197
198 case ERR_DNS_FAIL:
199
200 case ERR_CONNECT_FAIL:
201
202 case ERR_READ_ERROR:
203 n_bad++;
204 break;
205
206 default:
207 n_good++;
208 }
209
210 request_failure_ratio = n_bad / n_good;
211
212 if (hit_only_mode_until > squid_curtime)
213 return;
214
215 if (request_failure_ratio < 1.0)
216 return;
217
218 debugs(33, 0, "Failure Ratio at "<< std::setw(4)<<
219 std::setprecision(3) << request_failure_ratio);
220
221 debugs(33, 0, "Going into hit-only-mode for " <<
222 FAILURE_MODE_TIME / 60 << " minutes...");
223
224 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
225
226 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
227 }
228
229 ClientHttpRequest::~ClientHttpRequest()
230 {
231 debugs(33, 3, "httpRequestFree: " << uri);
232 PROF_start(httpRequestFree);
233
234 // Even though freeResources() below may destroy the request,
235 // we no longer set request->body_pipe to NULL here
236 // because we did not initiate that pipe (ConnStateData did)
237
238 /* the ICP check here was erroneous
239 * - StoreEntry::releaseRequest was always called if entry was valid
240 */
241 assert(logType < LOG_TYPE_MAX);
242
243 logRequest();
244
245 loggingEntry(NULL);
246
247 if (request)
248 checkFailureRatio(request->errType, al.hier.code);
249
250 freeResources();
251
252 #if ICAP_CLIENT
253 announceInitiatorAbort(icapHeadSource);
254
255 if (icapBodySource != NULL)
256 stopConsumingFrom(icapBodySource);
257 #endif
258
259 if (calloutContext)
260 delete calloutContext;
261
262 /* moving to the next connection is handled by the context free */
263 dlinkDelete(&active, &ClientActiveRequests);
264
265 PROF_stop(httpRequestFree);
266 }
267
268 /* Create a request and kick it off */
269 /*
270 * TODO: Pass in the buffers to be used in the inital Read request, as they are
271 * determined by the user
272 */
273 int /* returns nonzero on failure */
274 clientBeginRequest(method_t method, char const *url, CSCB * streamcallback,
275 CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header,
276 char *tailbuf, size_t taillen)
277 {
278 size_t url_sz;
279 HttpVersion http_ver (1, 0);
280 ClientHttpRequest *http = new ClientHttpRequest(NULL);
281 HttpRequest *request;
282 StoreIOBuffer tempBuffer;
283 http->start = current_time;
284 /* this is only used to adjust the connection offset in client_side.c */
285 http->req_sz = 0;
286 tempBuffer.length = taillen;
287 tempBuffer.data = tailbuf;
288 /* client stream setup */
289 clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach,
290 clientReplyStatus, new clientReplyContext(http), streamcallback,
291 streamdetach, streamdata, tempBuffer);
292 /* make it visible in the 'current acctive requests list' */
293 /* Set flags */
294 /* internal requests only makes sense in an
295 * accelerator today. TODO: accept flags ? */
296 http->flags.accel = 1;
297 /* allow size for url rewriting */
298 url_sz = strlen(url) + Config.appendDomainLen + 5;
299 http->uri = (char *)xcalloc(url_sz, 1);
300 strcpy(http->uri, url);
301
302 if ((request = HttpRequest::CreateFromUrlAndMethod(http->uri, method)) == NULL) {
303 debugs(85, 5, "Invalid URL: " << http->uri);
304 return -1;
305 }
306
307 /*
308 * now update the headers in request with our supplied headers. urLParse
309 * should return a blank header set, but we use Update to be sure of
310 * correctness.
311 */
312 if (header)
313 request->header.update(header, NULL);
314
315 http->log_uri = xstrdup(urlCanonicalClean(request));
316
317 /* http struct now ready */
318
319 /*
320 * build new header list *? TODO
321 */
322 request->flags.accelerated = http->flags.accel;
323
324 request->flags.internalclient = 1;
325
326 /* this is an internally created
327 * request, not subject to acceleration
328 * target overrides */
329 /*
330 * FIXME? Do we want to detect and handle internal requests of internal
331 * objects ?
332 */
333
334 /* Internally created requests cannot have bodies today */
335 request->content_length = 0;
336
337 request->client_addr.SetNoAddr();
338
339 request->my_addr.SetNoAddr(); /* undefined for internal requests */
340
341 request->my_addr.SetPort(0);
342
343 request->http_ver = http_ver;
344
345 http->request = HTTPMSGLOCK(request);
346
347 /* optional - skip the access check ? */
348 http->calloutContext = new ClientRequestContext(http);
349
350 http->calloutContext->http_access_done = false;
351
352 http->calloutContext->redirect_done = true;
353
354 http->calloutContext->no_cache_done = true;
355
356 http->doCallouts();
357
358 return 0;
359 }
360
361 bool
362 ClientRequestContext::httpStateIsValid()
363 {
364 ClientHttpRequest *http_ = http;
365
366 if (cbdataReferenceValid(http_))
367 return true;
368
369 http = NULL;
370
371 cbdataReferenceDone(http_);
372
373 return false;
374 }
375
376 /* This is the entry point for external users of the client_side routines */
377 void
378 ClientRequestContext::clientAccessCheck()
379 {
380 acl_checklist =
381 clientAclChecklistCreate(Config.accessList.http, http);
382 acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this);
383 }
384
385 void
386 clientAccessCheckDoneWrapper(int answer, void *data)
387 {
388 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
389
390 if (!calloutContext->httpStateIsValid())
391 return;
392
393 calloutContext->clientAccessCheckDone(answer);
394 }
395
396 void
397 ClientRequestContext::clientAccessCheckDone(int answer)
398 {
399 acl_checklist = NULL;
400 err_type page_id;
401 http_status status;
402 debugs(85, 2, "The request " <<
403 RequestMethodStr[http->request->method] << " " <<
404 http->uri << " is " <<
405 (answer == ACCESS_ALLOWED ? "ALLOWED" : "DENIED") <<
406 ", because it matched '" <<
407 (AclMatchedName ? AclMatchedName : "NO ACL's") << "'" );
408 char const *proxy_auth_msg = "<null>";
409
410 if (http->getConn() != NULL && http->getConn()->auth_user_request != NULL)
411 proxy_auth_msg = http->getConn()->auth_user_request->denyMessage("<null>");
412 else if (http->request->auth_user_request != NULL)
413 proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>");
414
415 if (answer != ACCESS_ALLOWED) {
416 /* Send an error */
417 int require_auth = (answer == ACCESS_REQ_PROXY_AUTH || aclIsProxyAuth(AclMatchedName));
418 debugs(85, 5, "Access Denied: " << http->uri);
419 debugs(85, 5, "AclMatchedName = " << (AclMatchedName ? AclMatchedName : "<null>"));
420
421 if (require_auth)
422 debugs(33, 5, "Proxy Auth Message = " << (proxy_auth_msg ? proxy_auth_msg : "<null>"));
423
424 /*
425 * NOTE: get page_id here, based on AclMatchedName because if
426 * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in
427 * the clientCreateStoreEntry() call just below. Pedro Ribeiro
428 * <pribeiro@isel.pt>
429 */
430 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, answer != ACCESS_REQ_PROXY_AUTH);
431
432 http->logType = LOG_TCP_DENIED;
433
434 if (require_auth) {
435 if (!http->flags.accel) {
436 /* Proxy authorisation needed */
437 status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
438 } else {
439 /* WWW authorisation needed */
440 status = HTTP_UNAUTHORIZED;
441 }
442
443 if (page_id == ERR_NONE)
444 page_id = ERR_CACHE_ACCESS_DENIED;
445 } else {
446 status = HTTP_FORBIDDEN;
447
448 if (page_id == ERR_NONE)
449 page_id = ERR_ACCESS_DENIED;
450 }
451
452 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->prev->data;
453 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
454 assert (repContext);
455 IPAddress tmpnoaddr; tmpnoaddr.SetNoAddr();
456 repContext->setReplyToError(page_id, status,
457 http->request->method, NULL,
458 http->getConn() != NULL ? http->getConn()->peer : tmpnoaddr,
459 http->request,
460 NULL,
461 http->getConn() != NULL && http->getConn()->auth_user_request ?
462 http->getConn()->auth_user_request : http->request->auth_user_request);
463
464 node = (clientStreamNode *)http->client_stream.tail->data;
465 clientStreamRead(node, http, node->readBuffer);
466 return;
467 }
468
469 /* ACCESS_ALLOWED continues here ... */
470 safe_free(http->uri);
471
472 http->uri = xstrdup(urlCanonical(http->request));
473
474 http->doCallouts();
475 }
476
477 #if ICAP_CLIENT
478 void
479 ClientRequestContext::icapAccessCheck()
480 {
481 ICAPAccessCheck *icap_access_check;
482
483 icap_access_check = new ICAPAccessCheck(ICAP::methodReqmod, ICAP::pointPreCache, http->request, NULL, icapAclCheckDoneWrapper, this);
484
485 if (icap_access_check != NULL) {
486 icap_access_check->check();
487 return;
488 }
489
490 http->doCallouts();
491 }
492
493 static void
494 icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service, void *data)
495 {
496 ClientRequestContext *calloutContext = (ClientRequestContext *)data;
497
498 if (!calloutContext->httpStateIsValid())
499 return;
500
501 calloutContext->icapAclCheckDone(service);
502 }
503
504 void
505 ClientRequestContext::icapAclCheckDone(ICAPServiceRep::Pointer service)
506 {
507 debugs(93,3,HERE << this << " icapAclCheckDone called");
508 assert(http);
509
510 if (http->startIcap(service))
511 return;
512
513 if (!service || service->bypass) {
514 // handle ICAP start failure when no service was selected
515 // or where the selected service was optional
516 http->doCallouts();
517 return;
518 }
519
520 // handle start failure for an essential ICAP service
521 http->handleIcapFailure();
522 }
523
524 #endif
525
526 static void
527 clientRedirectAccessCheckDone(int answer, void *data)
528 {
529 ClientRequestContext *context = (ClientRequestContext *)data;
530 ClientHttpRequest *http = context->http;
531 context->acl_checklist = NULL;
532
533 if (answer == ACCESS_ALLOWED)
534 redirectStart(http, clientRedirectDoneWrapper, context);
535 else
536 context->clientRedirectDone(NULL);
537 }
538
539 void
540 ClientRequestContext::clientRedirectStart()
541 {
542 debugs(33, 5, "clientRedirectStart: '" << http->uri << "'");
543
544 if (Config.accessList.redirector) {
545 acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http);
546 acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, this);
547 } else
548 redirectStart(http, clientRedirectDoneWrapper, this);
549 }
550
551 static int
552 clientHierarchical(ClientHttpRequest * http)
553 {
554 const char *url = http->uri;
555 HttpRequest *request = http->request;
556 method_t method = request->method;
557 const wordlist *p = NULL;
558
559 /*
560 * IMS needs a private key, so we can use the hierarchy for IMS only if our
561 * neighbors support private keys
562 */
563
564 if (request->flags.ims && !neighbors_do_private_keys)
565 return 0;
566
567 /*
568 * This is incorrect: authenticating requests can be sent via a hierarchy
569 * (they can even be cached if the correct headers are set on the reply)
570 */
571 if (request->flags.auth)
572 return 0;
573
574 if (method == METHOD_TRACE)
575 return 1;
576
577 if (method != METHOD_GET)
578 return 0;
579
580 /* scan hierarchy_stoplist */
581 for (p = Config.hierarchy_stoplist; p; p = p->next)
582 if (strstr(url, p->key))
583 return 0;
584
585 if (request->flags.loopdetect)
586 return 0;
587
588 if (request->protocol == PROTO_HTTP)
589 return httpCachable(method);
590
591 if (request->protocol == PROTO_GOPHER)
592 return gopherCachable(request);
593
594 if (request->protocol == PROTO_CACHEOBJ)
595 return 0;
596
597 return 1;
598 }
599
600
601 static void
602 clientInterpretRequestHeaders(ClientHttpRequest * http)
603 {
604 HttpRequest *request = http->request;
605 HttpHeader *req_hdr = &request->header;
606 int no_cache = 0;
607 #if !(USE_SQUID_ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
608
609 const char *str;
610 #endif
611
612 request->imslen = -1;
613 request->ims = req_hdr->getTime(HDR_IF_MODIFIED_SINCE);
614
615 if (request->ims > 0)
616 request->flags.ims = 1;
617
618 #if USE_SQUID_ESI
619 /*
620 * We ignore Cache-Control as per the Edge Architecture Section 3. See
621 * www.esi.org for more information.
622 */
623 #else
624
625 if (req_hdr->has(HDR_PRAGMA)) {
626 String s = req_hdr->getList(HDR_PRAGMA);
627
628 if (strListIsMember(&s, "no-cache", ','))
629 no_cache++;
630
631 s.clean();
632 }
633
634 if (request->cache_control)
635 if (EBIT_TEST(request->cache_control->mask, CC_NO_CACHE))
636 no_cache++;
637
638 /*
639 * Work around for supporting the Reload button in IE browsers when Squid
640 * is used as an accelerator or transparent proxy, by turning accelerated
641 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
642 * actually only fixed in SP1, but we can't tell whether we are talking to
643 * SP1 or not so all 5.5 versions are treated 'normally').
644 */
645 if (Config.onoff.ie_refresh) {
646 if (http->flags.accel && request->flags.ims) {
647 if ((str = req_hdr->getStr(HDR_USER_AGENT))) {
648 if (strstr(str, "MSIE 5.01") != NULL)
649 no_cache++;
650 else if (strstr(str, "MSIE 5.0") != NULL)
651 no_cache++;
652 else if (strstr(str, "MSIE 4.") != NULL)
653 no_cache++;
654 else if (strstr(str, "MSIE 3.") != NULL)
655 no_cache++;
656 }
657 }
658 }
659
660 #endif
661 if (no_cache) {
662 #if HTTP_VIOLATIONS
663
664 if (Config.onoff.reload_into_ims)
665 request->flags.nocache_hack = 1;
666 else if (refresh_nocache_hack)
667 request->flags.nocache_hack = 1;
668 else
669 #endif
670
671 request->flags.nocache = 1;
672 }
673
674 /* ignore range header in non-GETs or non-HEADs */
675 if (request->method == METHOD_GET || request->method == METHOD_HEAD) {
676 request->range = req_hdr->getRange();
677
678 if (request->range) {
679 request->flags.range = 1;
680 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data;
681 /* XXX: This is suboptimal. We should give the stream the range set,
682 * and thereby let the top of the stream set the offset when the
683 * size becomes known. As it is, we will end up requesting from 0
684 * for evey -X range specification.
685 * RBC - this may be somewhat wrong. We should probably set the range
686 * iter up at this point.
687 */
688 node->readBuffer.offset = request->range->lowestOffset(0);
689 http->range_iter.pos = request->range->begin();
690 http->range_iter.valid = true;
691 }
692 }
693
694 /* Only HEAD and GET requests permit a Range or Request-Range header.
695 * If these headers appear on any other type of request, delete them now.
696 */
697 else {
698 req_hdr->delById(HDR_RANGE);
699 req_hdr->delById(HDR_REQUEST_RANGE);
700 request->range = NULL;
701 }
702
703 if (req_hdr->has(HDR_AUTHORIZATION))
704 request->flags.auth = 1;
705
706 if (request->login[0] != '\0')
707 request->flags.auth = 1;
708
709 if (req_hdr->has(HDR_VIA)) {
710 String s = req_hdr->getList(HDR_VIA);
711 /*
712 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
713 * Note ThisCache2 has a space prepended to the hostname so we don't
714 * accidentally match super-domains.
715 */
716
717 if (strListIsSubstr(&s, ThisCache2, ',')) {
718 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
719 request, (ObjPackMethod) & httpRequestPack);
720 request->flags.loopdetect = 1;
721 }
722
723 #if FORW_VIA_DB
724 fvdbCountVia(s.buf());
725
726 #endif
727
728 s.clean();
729 }
730
731 #if USE_USERAGENT_LOG
732 if ((str = req_hdr->getStr(HDR_USER_AGENT)))
733 logUserAgent(fqdnFromAddr(http->getConn() != NULL ? http->getConn()->log_addr : no_addr), str);
734
735 #endif
736 #if USE_REFERER_LOG
737
738 if ((str = req_hdr->getStr(HDR_REFERER)))
739 logReferer(fqdnFromAddr(http->getConn() != NULL ? http->getConn()->log_addr : no_addr), str, http->log_uri);
740
741 #endif
742 #if FORW_VIA_DB
743
744 if (req_hdr->has(HDR_X_FORWARDED_FOR)) {
745 String s = req_hdr->getList(HDR_X_FORWARDED_FOR);
746 fvdbCountForw(s.buf());
747 s.clean();
748 }
749
750 #endif
751 if (request->method == METHOD_TRACE) {
752 request->max_forwards = req_hdr->getInt(HDR_MAX_FORWARDS);
753 }
754
755 request->flags.cachable = http->request->cacheable();
756
757 if (clientHierarchical(http))
758 request->flags.hierarchical = 1;
759
760 debugs(85, 5, "clientInterpretRequestHeaders: REQ_NOCACHE = " <<
761 (request->flags.nocache ? "SET" : "NOT SET"));
762 debugs(85, 5, "clientInterpretRequestHeaders: REQ_CACHABLE = " <<
763 (request->flags.cachable ? "SET" : "NOT SET"));
764 debugs(85, 5, "clientInterpretRequestHeaders: REQ_HIERARCHICAL = " <<
765 (request->flags.hierarchical ? "SET" : "NOT SET"));
766
767 }
768
769 void
770 clientRedirectDoneWrapper(void *data, char *result)
771 {
772 ClientRequestContext *calloutContext = (ClientRequestContext *)data;
773
774 if (!calloutContext->httpStateIsValid())
775 return;
776
777 calloutContext->clientRedirectDone(result);
778 }
779
780 void
781 ClientRequestContext::clientRedirectDone(char *result)
782 {
783 HttpRequest *new_request = NULL;
784 HttpRequest *old_request = http->request;
785 debugs(85, 5, "clientRedirectDone: '" << http->uri << "' result=" << (result ? result : "NULL"));
786 assert(redirect_state == REDIRECT_PENDING);
787 redirect_state = REDIRECT_DONE;
788
789 if (result) {
790 http_status status = (http_status) atoi(result);
791
792 if (status == HTTP_MOVED_PERMANENTLY
793 || status == HTTP_MOVED_TEMPORARILY
794 || status == HTTP_SEE_OTHER
795 || status == HTTP_TEMPORARY_REDIRECT) {
796 char *t = result;
797
798 if ((t = strchr(result, ':')) != NULL) {
799 http->redirect.status = status;
800 http->redirect.location = xstrdup(t + 1);
801 } else {
802 debugs(85, 1, "clientRedirectDone: bad input: " << result);
803 }
804 } else if (strcmp(result, http->uri))
805 new_request = HttpRequest::CreateFromUrlAndMethod(result, old_request->method);
806 }
807
808 if (new_request) {
809 safe_free(http->uri);
810 http->uri = xstrdup(urlCanonical(new_request));
811 new_request->http_ver = old_request->http_ver;
812 new_request->header.append(&old_request->header);
813 new_request->client_addr = old_request->client_addr;
814 new_request->my_addr = old_request->my_addr;
815 new_request->flags = old_request->flags;
816 new_request->flags.redirected = 1;
817
818 if (old_request->auth_user_request) {
819 new_request->auth_user_request = old_request->auth_user_request;
820 AUTHUSERREQUESTLOCK(new_request->auth_user_request, "new request");
821 }
822
823 if (old_request->body_pipe != NULL) {
824 new_request->body_pipe = old_request->body_pipe;
825 old_request->body_pipe = NULL;
826 debugs(0,0,HERE << "redirecting body_pipe " << new_request->body_pipe << " from request " << old_request << " to " << new_request);
827 }
828
829 new_request->content_length = old_request->content_length;
830 new_request->extacl_user = old_request->extacl_user;
831 new_request->extacl_passwd = old_request->extacl_passwd;
832 new_request->flags.proxy_keepalive = old_request->flags.proxy_keepalive;
833 HTTPMSGUNLOCK(old_request);
834 http->request = HTTPMSGLOCK(new_request);
835 }
836
837 /* FIXME PIPELINE: This is innacurate during pipelining */
838
839 if (http->getConn() != NULL)
840 fd_note(http->getConn()->fd, http->uri);
841
842 assert(http->uri);
843
844 http->doCallouts();
845 }
846
847 void
848 ClientRequestContext::checkNoCache()
849 {
850 acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http);
851 acl_checklist->nonBlockingCheck(checkNoCacheDoneWrapper, this);
852 }
853
854 static void
855 checkNoCacheDoneWrapper(int answer, void *data)
856 {
857 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
858
859 if (!calloutContext->httpStateIsValid())
860 return;
861
862 calloutContext->checkNoCacheDone(answer);
863 }
864
865 void
866 ClientRequestContext::checkNoCacheDone(int answer)
867 {
868 acl_checklist = NULL;
869 http->request->flags.cachable = answer;
870 http->doCallouts();
871 }
872
873 /*
874 * Identify requests that do not go through the store and client side stream
875 * and forward them to the appropriate location. All other requests, request
876 * them.
877 */
878 void
879 ClientHttpRequest::processRequest()
880 {
881 debugs(85, 4, "clientProcessRequest: " << RequestMethodStr[request->method] << " '" << uri << "'");
882
883 if (request->method == METHOD_CONNECT && !redirect.status) {
884 logType = LOG_TCP_MISS;
885 tunnelStart(this, &out.size, &al.http.code);
886 return;
887 }
888
889 httpStart();
890 }
891
892 void
893 ClientHttpRequest::httpStart()
894 {
895 PROF_start(httpStart);
896 logType = LOG_TAG_NONE;
897 debugs(85, 4, "ClientHttpRequest::httpStart: " << log_tags[logType] << " for '" << uri << "'");
898
899 /* no one should have touched this */
900 assert(out.offset == 0);
901 /* Use the Stream Luke */
902 clientStreamNode *node = (clientStreamNode *)client_stream.tail->data;
903 clientStreamRead(node, this, node->readBuffer);
904 PROF_stop(httpStart);
905 }
906
907 bool
908 ClientHttpRequest::gotEnough() const
909 {
910 /** TODO: should be querying the stream. */
911 int64_t contentLength =
912 memObject()->getReply()->bodySize(request->method);
913 assert(contentLength >= 0);
914
915 if (out.offset < contentLength)
916 return false;
917
918 return true;
919 }
920
921 void
922 ClientHttpRequest::maxReplyBodySize(int64_t clen)
923 {
924 maxReplyBodySize_ = clen;
925 }
926
927 int64_t
928 ClientHttpRequest::maxReplyBodySize() const
929 {
930 return maxReplyBodySize_;
931 }
932
933 bool
934 ClientHttpRequest::isReplyBodyTooLarge(int64_t clen) const
935 {
936 if (0 == maxReplyBodySize())
937 return 0; /* disabled */
938
939 if (clen < 0)
940 return 0; /* unknown */
941
942 return clen > maxReplyBodySize();
943 }
944
945 void
946 ClientHttpRequest::storeEntry(StoreEntry *newEntry)
947 {
948 entry_ = newEntry;
949 }
950
951 void
952 ClientHttpRequest::loggingEntry(StoreEntry *newEntry)
953 {
954 if (loggingEntry_)
955 loggingEntry_->unlock();
956
957 loggingEntry_ = newEntry;
958
959 if (loggingEntry_)
960 loggingEntry_->lock()
961
962 ;
963 }
964
965 /*
966 * doCallouts() - This function controls the order of "callout"
967 * executions, including non-blocking access control checks, the
968 * redirector, and ICAP. Previously, these callouts were chained
969 * together such that "clientAccessCheckDone()" would call
970 * "clientRedirectStart()" and so on.
971 *
972 * The ClientRequestContext (aka calloutContext) class holds certain
973 * state data for the callout/callback operations. Previously
974 * ClientHttpRequest would sort of hand off control to ClientRequestContext
975 * for a short time. ClientRequestContext would then delete itself
976 * and pass control back to ClientHttpRequest when all callouts
977 * were finished.
978 *
979 * This caused some problems for ICAP because we want to make the
980 * ICAP callout after checking ACLs, but before checking the no_cache
981 * list. We can't stuff the ICAP state into the ClientRequestContext
982 * class because we still need the ICAP state after ClientRequestContext
983 * goes away.
984 *
985 * Note that ClientRequestContext is created before the first call
986 * to doCallouts().
987 *
988 * If one of the callouts notices that ClientHttpRequest is no
989 * longer valid, it should call cbdataReferenceDone() so that
990 * ClientHttpRequest's reference count goes to zero and it will get
991 * deleted. ClientHttpRequest will then delete ClientRequestContext.
992 *
993 * Note that we set the _done flags here before actually starting
994 * the callout. This is strictly for convenience.
995 */
996
997 extern int aclMapTOS (acl_tos * head, ACLChecklist * ch);
998
999 void
1000 ClientHttpRequest::doCallouts()
1001 {
1002 assert(calloutContext);
1003
1004 if (!calloutContext->http_access_done) {
1005 debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck()");
1006 calloutContext->http_access_done = true;
1007 calloutContext->clientAccessCheck();
1008 return;
1009 }
1010
1011 #if ICAP_CLIENT
1012 if (TheICAPConfig.onoff && !calloutContext->icap_acl_check_done) {
1013 debugs(83, 3, HERE << "Doing calloutContext->icapAccessCheck()");
1014 calloutContext->icap_acl_check_done = true;
1015 calloutContext->icapAccessCheck();
1016 return;
1017 }
1018
1019 #endif
1020
1021 if (!calloutContext->redirect_done) {
1022 calloutContext->redirect_done = true;
1023 assert(calloutContext->redirect_state == REDIRECT_NONE);
1024
1025 if (Config.Program.redirect) {
1026 debugs(83, 3, HERE << "Doing calloutContext->clientRedirectStart()");
1027 calloutContext->redirect_state = REDIRECT_PENDING;
1028 calloutContext->clientRedirectStart();
1029 return;
1030 }
1031 }
1032
1033 if (!calloutContext->interpreted_req_hdrs) {
1034 debugs(83, 3, HERE << "Doing clientInterpretRequestHeaders()");
1035 calloutContext->interpreted_req_hdrs = 1;
1036 clientInterpretRequestHeaders(this);
1037 }
1038
1039 if (!calloutContext->no_cache_done) {
1040 calloutContext->no_cache_done = true;
1041
1042 if (Config.accessList.noCache && request->flags.cachable) {
1043 debugs(83, 3, HERE << "Doing calloutContext->checkNoCache()");
1044 calloutContext->checkNoCache();
1045 return;
1046 }
1047 }
1048
1049 if (!calloutContext->clientside_tos_done) {
1050 calloutContext->clientside_tos_done = true;
1051 if (getConn() != NULL) {
1052 ACLChecklist ch;
1053 ch.src_addr = request->client_addr;
1054 ch.my_addr = request->my_addr;
1055 ch.request = HTTPMSGLOCK(request);
1056 int tos = aclMapTOS(Config.accessList.clientside_tos, &ch);
1057 if (tos)
1058 comm_set_tos(getConn()->fd, tos);
1059 }
1060 }
1061
1062 cbdataReferenceDone(calloutContext->http);
1063 delete calloutContext;
1064 calloutContext = NULL;
1065 #if HEADERS_LOG
1066
1067 headersLog(0, 1, request->method, request);
1068 #endif
1069
1070 debugs(83, 3, HERE << "calling processRequest()");
1071 processRequest();
1072 }
1073
1074 #ifndef _USE_INLINE_
1075 #include "client_side_request.cci"
1076 #endif
1077
1078 #if ICAP_CLIENT
1079 /*
1080 * Initiate an ICAP transaction. Return false on errors.
1081 * The caller must handle errors.
1082 */
1083 bool
1084 ClientHttpRequest::startIcap(ICAPServiceRep::Pointer service)
1085 {
1086 debugs(85, 3, HERE << this << " ClientHttpRequest::startIcap() called");
1087 if (!service) {
1088 debugs(85, 3, "ClientHttpRequest::startIcap fails: lack of service");
1089 return false;
1090 }
1091 if (service->broken()) {
1092 debugs(85, 3, "ClientHttpRequest::startIcap fails: broken service");
1093 return false;
1094 }
1095
1096 assert(!icapHeadSource);
1097 assert(!icapBodySource);
1098 icapHeadSource = initiateIcap(
1099 new ICAPModXactLauncher(this, request, NULL, service));
1100 return true;
1101 }
1102
1103 void
1104 ClientHttpRequest::noteIcapAnswer(HttpMsg *msg)
1105 {
1106 assert(cbdataReferenceValid(this)); // indicates bug
1107 assert(msg);
1108
1109 if (HttpRequest *new_req = dynamic_cast<HttpRequest*>(msg)) {
1110 /*
1111 * Replace the old request with the new request.
1112 */
1113 HTTPMSGUNLOCK(request);
1114 request = HTTPMSGLOCK(new_req);
1115 /*
1116 * Store the new URI for logging
1117 */
1118 xfree(uri);
1119 uri = xstrdup(urlCanonical(request));
1120 setLogUri(this, urlCanonicalClean(request));
1121 assert(request->method);
1122 } else if (HttpReply *new_rep = dynamic_cast<HttpReply*>(msg)) {
1123 debugs(85,3,HERE << "REQMOD reply is HTTP reply");
1124
1125 // subscribe to receive reply body
1126 if (new_rep->body_pipe != NULL) {
1127 icapBodySource = new_rep->body_pipe;
1128 assert(icapBodySource->setConsumerIfNotLate(this));
1129 }
1130
1131 clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data;
1132 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
1133 repContext->createStoreEntry(request->method, request->flags);
1134
1135 EBIT_CLR(storeEntry()->flags, ENTRY_FWD_HDR_WAIT);
1136 request_satisfaction_mode = true;
1137 request_satisfaction_offset = 0;
1138 storeEntry()->replaceHttpReply(new_rep);
1139
1140 if (!icapBodySource) // no body
1141 storeEntry()->complete();
1142 clientGetMoreData(node, this);
1143 }
1144
1145 // we are done with getting headers (but may be receiving body)
1146 clearIcap(icapHeadSource);
1147
1148 if (!request_satisfaction_mode)
1149 doCallouts();
1150 }
1151
1152 void
1153 ClientHttpRequest::noteIcapQueryAbort(bool final)
1154 {
1155 clearIcap(icapHeadSource);
1156 assert(!icapBodySource);
1157 handleIcapFailure(!final);
1158 }
1159
1160 void
1161 ClientHttpRequest::noteMoreBodyDataAvailable(BodyPipe &)
1162 {
1163 assert(request_satisfaction_mode);
1164 assert(icapBodySource != NULL);
1165
1166 if (const size_t contentSize = icapBodySource->buf().contentSize()) {
1167 BodyPipeCheckout bpc(*icapBodySource);
1168 const StoreIOBuffer ioBuf(&bpc.buf, request_satisfaction_offset);
1169 storeEntry()->write(ioBuf);
1170 // assume can write everything
1171 request_satisfaction_offset += contentSize;
1172 bpc.buf.consume(contentSize);
1173 bpc.checkIn();
1174 }
1175
1176 if (icapBodySource->exhausted())
1177 endRequestSatisfaction();
1178 // else wait for more body data
1179 }
1180
1181 void
1182 ClientHttpRequest::noteBodyProductionEnded(BodyPipe &)
1183 {
1184 assert(!icapHeadSource);
1185 if (icapBodySource != NULL) { // did not end request satisfaction yet
1186 // We do not expect more because noteMoreBodyDataAvailable always
1187 // consumes everything. We do not even have a mechanism to consume
1188 // leftovers after noteMoreBodyDataAvailable notifications seize.
1189 assert(icapBodySource->exhausted());
1190 endRequestSatisfaction();
1191 }
1192 }
1193
1194 void
1195 ClientHttpRequest::endRequestSatisfaction() {
1196 debugs(85,4, HERE << this << " ends request satisfaction");
1197 assert(request_satisfaction_mode);
1198 stopConsumingFrom(icapBodySource);
1199
1200 // TODO: anything else needed to end store entry formation correctly?
1201 storeEntry()->complete();
1202 }
1203
1204 void
1205 ClientHttpRequest::noteBodyProducerAborted(BodyPipe &)
1206 {
1207 assert(!icapHeadSource);
1208 stopConsumingFrom(icapBodySource);
1209 handleIcapFailure();
1210 }
1211
1212 void
1213 ClientHttpRequest::handleIcapFailure(bool bypassable)
1214 {
1215 debugs(85,3, HERE << "handleIcapFailure(" << bypassable << ")");
1216
1217 const bool usedStore = storeEntry() && !storeEntry()->isEmpty();
1218 const bool usedPipe = request->body_pipe != NULL &&
1219 request->body_pipe->consumedSize() > 0;
1220
1221 if (bypassable && !usedStore && !usedPipe) {
1222 debugs(85,3, HERE << "ICAP REQMOD callout failed, bypassing: " << calloutContext);
1223 if (calloutContext)
1224 doCallouts();
1225 return;
1226 }
1227
1228 debugs(85,3, HERE << "ICAP REQMOD callout failed, responding with error");
1229
1230 clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data;
1231 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
1232 assert(repContext);
1233
1234 // The original author of the code also wanted to pass an errno to
1235 // setReplyToError, but it seems unlikely that the errno reflects the
1236 // true cause of the error at this point, so I did not pass it.
1237 ConnStateData::Pointer c = getConn();
1238 repContext->setReplyToError(ERR_ICAP_FAILURE, HTTP_INTERNAL_SERVER_ERROR,
1239 request->method, NULL,
1240 (c != NULL ? &c->peer.sin_addr : &no_addr), request, NULL,
1241 (c != NULL && c->auth_user_request ?
1242 c->auth_user_request : request->auth_user_request));
1243
1244 node = (clientStreamNode *)client_stream.tail->data;
1245 clientStreamRead(node, this, node->readBuffer);
1246 }
1247
1248 #endif