5 * DEBUG: section 85 Client-side Request Routines
6 * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
38 * General logic of request processing:
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.
46 #include "clientStream.h"
47 #include "client_side_request.h"
48 #include "auth/UserRequest.h"
49 #include "HttpRequest.h"
50 #include "ProtoPort.h"
51 #include "acl/FilledChecklist.h"
52 #include "acl/Gadgets.h"
53 #include "client_side.h"
54 #include "client_side_reply.h"
56 #include "HttpReply.h"
57 #include "MemObject.h"
58 #include "ClientRequestContext.h"
59 #include "SquidTime.h"
61 #include "inet_pton.h"
65 #include "adaptation/AccessCheck.h"
66 #include "adaptation/Iterator.h"
67 #include "adaptation/Service.h"
69 #include "adaptation/icap/History.h"
71 //static void adaptationAclCheckDoneWrapper(Adaptation::ServicePointer service, void *data);
77 #define comm_close comm_lingering_close
80 static const char *const crlf
= "\r\n";
82 #if FOLLOW_X_FORWARDED_FOR
84 clientFollowXForwardedForCheck(int answer
, void *data
);
85 #endif /* FOLLOW_X_FORWARDED_FOR */
87 CBDATA_CLASS_INIT(ClientRequestContext
);
90 ClientRequestContext::operator new (size_t size
)
92 assert (size
== sizeof(ClientRequestContext
));
93 CBDATA_INIT_TYPE(ClientRequestContext
);
94 ClientRequestContext
*result
= cbdataAlloc(ClientRequestContext
);
99 ClientRequestContext::operator delete (void *address
)
101 ClientRequestContext
*t
= static_cast<ClientRequestContext
*>(address
);
105 /* Local functions */
107 static void clientAccessCheckDoneWrapper(int, void *);
108 static int clientHierarchical(ClientHttpRequest
* http
);
109 static void clientInterpretRequestHeaders(ClientHttpRequest
* http
);
110 static RH clientRedirectDoneWrapper
;
111 static PF checkNoCacheDoneWrapper
;
112 extern "C" CSR clientGetMoreData
;
113 extern "C" CSS clientReplyStatus
;
114 extern "C" CSD clientReplyDetach
;
115 static void checkFailureRatio(err_type
, hier_code
);
117 ClientRequestContext::~ClientRequestContext()
120 * Release our "lock" on our parent, ClientHttpRequest, if we
125 cbdataReferenceDone(http
);
127 debugs(85,3, HERE
<< this << " ClientRequestContext destructed");
130 ClientRequestContext::ClientRequestContext(ClientHttpRequest
*anHttp
) : http(cbdataReference(anHttp
)), acl_checklist (NULL
), redirect_state (REDIRECT_NONE
)
132 http_access_done
= false;
133 redirect_done
= false;
134 no_cache_done
= false;
135 interpreted_req_hdrs
= false;
136 debugs(85,3, HERE
<< this << " ClientRequestContext constructed");
139 CBDATA_CLASS_INIT(ClientHttpRequest
);
142 ClientHttpRequest::operator new (size_t size
)
144 assert (size
== sizeof (ClientHttpRequest
));
145 CBDATA_INIT_TYPE(ClientHttpRequest
);
146 ClientHttpRequest
*result
= cbdataAlloc(ClientHttpRequest
);
151 ClientHttpRequest::operator delete (void *address
)
153 ClientHttpRequest
*t
= static_cast<ClientHttpRequest
*>(address
);
157 ClientHttpRequest::ClientHttpRequest(ConnStateData
* aConn
) :
159 AsyncJob("ClientHttpRequest"),
163 start_time
= current_time
;
165 dlinkAdd(this, &active
, &ClientActiveRequests
);
167 request_satisfaction_mode
= false;
172 * returns true if client specified that the object must come from the cache
173 * without contacting origin server
176 ClientHttpRequest::onlyIfCached()const
179 return request
->cache_control
&&
180 EBIT_TEST(request
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
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.
193 * Duane W., Sept 16, 1996
196 #define FAILURE_MODE_TIME 300
199 checkFailureRatio(err_type etype
, hier_code hcode
)
201 static double magic_factor
= 100.0;
205 if (hcode
== HIER_NONE
)
208 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
210 n_bad
= magic_factor
- n_good
;
216 case ERR_CONNECT_FAIL
:
217 case ERR_SECURE_CONNECT_FAIL
:
227 request_failure_ratio
= n_bad
/ n_good
;
229 if (hit_only_mode_until
> squid_curtime
)
232 if (request_failure_ratio
< 1.0)
235 debugs(33, 0, "Failure Ratio at "<< std::setw(4)<<
236 std::setprecision(3) << request_failure_ratio
);
238 debugs(33, 0, "Going into hit-only-mode for " <<
239 FAILURE_MODE_TIME
/ 60 << " minutes...");
241 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
243 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
246 ClientHttpRequest::~ClientHttpRequest()
248 debugs(33, 3, "httpRequestFree: " << uri
);
249 PROF_start(httpRequestFree
);
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)
255 /* the ICP check here was erroneous
256 * - StoreEntry::releaseRequest was always called if entry was valid
258 assert(logType
< LOG_TYPE_MAX
);
265 checkFailureRatio(request
->errType
, al
.hier
.code
);
270 announceInitiatorAbort(virginHeadSource
);
272 if (adaptedBodySource
!= NULL
)
273 stopConsumingFrom(adaptedBodySource
);
277 delete calloutContext
;
280 cbdataReferenceDone(conn_
);
282 /* moving to the next connection is handled by the context free */
283 dlinkDelete(&active
, &ClientActiveRequests
);
285 PROF_stop(httpRequestFree
);
288 /* Create a request and kick it off */
290 * TODO: Pass in the buffers to be used in the inital Read request, as they are
291 * determined by the user
293 int /* returns nonzero on failure */
294 clientBeginRequest(const HttpRequestMethod
& method
, char const *url
, CSCB
* streamcallback
,
295 CSD
* streamdetach
, ClientStreamData streamdata
, HttpHeader
const *header
,
296 char *tailbuf
, size_t taillen
)
299 HttpVersion
http_ver (1, 0);
300 ClientHttpRequest
*http
= new ClientHttpRequest(NULL
);
301 HttpRequest
*request
;
302 StoreIOBuffer tempBuffer
;
303 http
->start_time
= current_time
;
304 /* this is only used to adjust the connection offset in client_side.c */
306 tempBuffer
.length
= taillen
;
307 tempBuffer
.data
= tailbuf
;
308 /* client stream setup */
309 clientStreamInit(&http
->client_stream
, clientGetMoreData
, clientReplyDetach
,
310 clientReplyStatus
, new clientReplyContext(http
), streamcallback
,
311 streamdetach
, streamdata
, tempBuffer
);
312 /* make it visible in the 'current acctive requests list' */
314 /* internal requests only makes sense in an
315 * accelerator today. TODO: accept flags ? */
316 http
->flags
.accel
= 1;
317 /* allow size for url rewriting */
318 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
319 http
->uri
= (char *)xcalloc(url_sz
, 1);
320 strcpy(http
->uri
, url
);
322 if ((request
= HttpRequest::CreateFromUrlAndMethod(http
->uri
, method
)) == NULL
) {
323 debugs(85, 5, "Invalid URL: " << http
->uri
);
328 * now update the headers in request with our supplied headers. urLParse
329 * should return a blank header set, but we use Update to be sure of
333 request
->header
.update(header
, NULL
);
335 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
337 /* http struct now ready */
340 * build new header list *? TODO
342 request
->flags
.accelerated
= http
->flags
.accel
;
344 request
->flags
.internalclient
= 1;
346 /* this is an internally created
347 * request, not subject to acceleration
348 * target overrides */
350 * FIXME? Do we want to detect and handle internal requests of internal
354 /* Internally created requests cannot have bodies today */
355 request
->content_length
= 0;
357 request
->client_addr
.SetNoAddr();
359 #if FOLLOW_X_FORWARDED_FOR
360 request
->indirect_client_addr
.SetNoAddr();
361 #endif /* FOLLOW_X_FORWARDED_FOR */
363 request
->my_addr
.SetNoAddr(); /* undefined for internal requests */
365 request
->my_addr
.SetPort(0);
367 request
->http_ver
= http_ver
;
369 http
->request
= HTTPMSGLOCK(request
);
371 /* optional - skip the access check ? */
372 http
->calloutContext
= new ClientRequestContext(http
);
374 http
->calloutContext
->http_access_done
= false;
376 http
->calloutContext
->redirect_done
= true;
378 http
->calloutContext
->no_cache_done
= true;
386 ClientRequestContext::httpStateIsValid()
388 ClientHttpRequest
*http_
= http
;
390 if (cbdataReferenceValid(http_
))
395 cbdataReferenceDone(http_
);
400 #if FOLLOW_X_FORWARDED_FOR
402 * clientFollowXForwardedForCheck() checks the indirect_client_addr
403 * against the followXFF ACL, or cleans up and passes control to
404 * clientAccessCheck().
408 clientFollowXForwardedForCheck(int answer
, void *data
)
410 ClientRequestContext
*calloutContext
= (ClientRequestContext
*) data
;
411 ClientHttpRequest
*http
= NULL
;
412 HttpRequest
*request
= NULL
;
414 if (!calloutContext
->httpStateIsValid())
417 http
= calloutContext
->http
;
418 request
= http
->request
;
420 * answer should be be ACCESS_ALLOWED or ACCESS_DENIED if we are
421 * called as a result of ACL checks, or -1 if we are called when
422 * there's nothing left to do.
424 if (answer
== ACCESS_ALLOWED
&&
425 request
->x_forwarded_for_iterator
.size () != 0) {
427 * The IP address currently in request->indirect_client_addr
428 * is trusted to use X-Forwarded-For. Remove the last
429 * comma-delimited element from x_forwarded_for_iterator and use
430 * it to to replace indirect_client_addr, then repeat the cycle.
433 const char *asciiaddr
;
436 p
= request
->x_forwarded_for_iterator
.termedBuf();
437 l
= request
->x_forwarded_for_iterator
.size();
440 * XXX x_forwarded_for_iterator should really be a list of
441 * IP addresses, but it's a String instead. We have to
442 * walk backwards through the String, biting off the last
443 * comma-delimited part each time. As long as the data is in
444 * a String, we should probably implement and use a variant of
445 * strListGetItem() that walks backwards instead of forwards
446 * through a comma-separated list. But we don't even do that;
447 * we just do the work in-line here.
449 /* skip trailing space and commas */
450 while (l
> 0 && (p
[l
-1] == ',' || xisspace(p
[l
-1])))
452 request
->x_forwarded_for_iterator
.cut(l
);
453 /* look for start of last item in list */
454 while (l
> 0 && ! (p
[l
-1] == ',' || xisspace(p
[l
-1])))
457 if (xinet_pton(AF_INET
, asciiaddr
, &addr
) != 0) {
458 request
->indirect_client_addr
= addr
;
459 request
->x_forwarded_for_iterator
.cut(l
);
460 if (! Config
.onoff
.acl_uses_indirect_client
) {
462 * If acl_uses_indirect_client is off, then it's impossible
463 * to follow more than one level of X-Forwarded-For.
465 request
->x_forwarded_for_iterator
.clean();
467 calloutContext
->acl_checklist
=
468 clientAclChecklistCreate(Config
.accessList
.followXFF
, http
);
469 calloutContext
->acl_checklist
->
470 nonBlockingCheck(clientFollowXForwardedForCheck
, data
);
473 } /*if (answer == ACCESS_ALLOWED &&
474 request->x_forwarded_for_iterator.size () != 0)*/
476 /* clean up, and pass control to clientAccessCheck */
477 if (Config
.onoff
.log_uses_indirect_client
) {
479 * Ensure that the access log shows the indirect client
480 * instead of the direct client.
482 ConnStateData
*conn
= http
->getConn();
483 conn
->log_addr
= request
->indirect_client_addr
;
485 request
->x_forwarded_for_iterator
.clean();
486 request
->flags
.done_follow_x_forwarded_for
= 1;
488 /* If follow XFF is denied, we reset the indirect_client_addr
489 to the direct client. Thats the one we are configured to check for */
490 if (answer
== ACCESS_DENIED
) {
491 request
->indirect_client_addr
= request
->client_addr
;
493 /* on a failure, leave it as undefined state ?? */
494 else if (answer
!= ACCESS_ALLOWED
) {
495 debugs(28, DBG_CRITICAL
, "Follow X-Forwarded-For encountered an error. Ignoring address: " << request
->indirect_client_addr
);
496 request
->indirect_client_addr
= request
->client_addr
;
499 /* process actual access ACL as normal. */
500 calloutContext
->clientAccessCheck();
502 #endif /* FOLLOW_X_FORWARDED_FOR */
504 /* This is the entry point for external users of the client_side routines */
506 ClientRequestContext::clientAccessCheck()
508 #if FOLLOW_X_FORWARDED_FOR
509 if (!http
->request
->flags
.done_follow_x_forwarded_for
&&
510 Config
.accessList
.followXFF
&&
511 http
->request
->header
.has(HDR_X_FORWARDED_FOR
)) {
512 http
->request
->x_forwarded_for_iterator
=
513 http
->request
->header
.getList(HDR_X_FORWARDED_FOR
);
514 clientFollowXForwardedForCheck(ACCESS_ALLOWED
, this);
517 #endif /* FOLLOW_X_FORWARDED_FOR */
519 if (Config
.accessList
.http
) {
520 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.http
, http
);
521 acl_checklist
->nonBlockingCheck(clientAccessCheckDoneWrapper
, this);
523 debugs(0, DBG_CRITICAL
, "No http_access configuration found. This will block ALL traffic");
524 clientAccessCheckDone(ACCESS_DENIED
);
529 clientAccessCheckDoneWrapper(int answer
, void *data
)
531 ClientRequestContext
*calloutContext
= (ClientRequestContext
*) data
;
533 if (!calloutContext
->httpStateIsValid())
536 calloutContext
->clientAccessCheckDone(answer
);
540 ClientRequestContext::clientAccessCheckDone(int answer
)
542 acl_checklist
= NULL
;
545 debugs(85, 2, "The request " <<
546 RequestMethodStr(http
->request
->method
) << " " <<
547 http
->uri
<< " is " <<
548 (answer
== ACCESS_ALLOWED
? "ALLOWED" : "DENIED") <<
549 ", because it matched '" <<
550 (AclMatchedName
? AclMatchedName
: "NO ACL's") << "'" );
551 char const *proxy_auth_msg
= "<null>";
553 if (http
->getConn() != NULL
&& http
->getConn()->auth_user_request
!= NULL
)
554 proxy_auth_msg
= http
->getConn()->auth_user_request
->denyMessage("<null>");
555 else if (http
->request
->auth_user_request
!= NULL
)
556 proxy_auth_msg
= http
->request
->auth_user_request
->denyMessage("<null>");
558 if (answer
!= ACCESS_ALLOWED
) {
560 int require_auth
= (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
));
561 debugs(85, 5, "Access Denied: " << http
->uri
);
562 debugs(85, 5, "AclMatchedName = " << (AclMatchedName
? AclMatchedName
: "<null>"));
565 debugs(33, 5, "Proxy Auth Message = " << (proxy_auth_msg
? proxy_auth_msg
: "<null>"));
568 * NOTE: get page_id here, based on AclMatchedName because if
569 * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in
570 * the clientCreateStoreEntry() call just below. Pedro Ribeiro
573 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
, answer
!= ACCESS_REQ_PROXY_AUTH
);
575 http
->logType
= LOG_TCP_DENIED
;
578 if (!http
->flags
.accel
) {
579 /* Proxy authorisation needed */
580 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
582 /* WWW authorisation needed */
583 status
= HTTP_UNAUTHORIZED
;
586 if (page_id
== ERR_NONE
)
587 page_id
= ERR_CACHE_ACCESS_DENIED
;
589 status
= HTTP_FORBIDDEN
;
591 if (page_id
== ERR_NONE
)
592 page_id
= ERR_ACCESS_DENIED
;
595 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->prev
->data
;
596 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
599 tmpnoaddr
.SetNoAddr();
600 repContext
->setReplyToError(page_id
, status
,
601 http
->request
->method
, NULL
,
602 http
->getConn() != NULL
? http
->getConn()->peer
: tmpnoaddr
,
605 http
->getConn() != NULL
&& http
->getConn()->auth_user_request
?
606 http
->getConn()->auth_user_request
: http
->request
->auth_user_request
);
608 node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
609 clientStreamRead(node
, http
, node
->readBuffer
);
613 /* ACCESS_ALLOWED continues here ... */
614 safe_free(http
->uri
);
616 http
->uri
= xstrdup(urlCanonical(http
->request
));
623 adaptationAclCheckDoneWrapper(Adaptation::ServiceGroupPointer g
, void *data
)
625 ClientRequestContext
*calloutContext
= (ClientRequestContext
*)data
;
627 if (!calloutContext
->httpStateIsValid())
630 calloutContext
->adaptationAclCheckDone(g
);
634 ClientRequestContext::adaptationAclCheckDone(Adaptation::ServiceGroupPointer g
)
636 debugs(93,3,HERE
<< this << " adaptationAclCheckDone called");
640 Adaptation::Icap::History::Pointer ih
= http
->request
->icapHistory();
642 if (http
->getConn() != NULL
) {
643 ih
->rfc931
= http
->getConn()->rfc931
;
645 ih
->ssluser
= sslGetUserEmail(fd_table
[http
->getConn()->fd
].ssl
);
648 ih
->log_uri
= http
->log_uri
;
649 ih
->req_sz
= http
->req_sz
;
654 debugs(85,3, HERE
<< "no adaptation needed");
659 http
->startAdaptation(g
);
665 clientRedirectAccessCheckDone(int answer
, void *data
)
667 ClientRequestContext
*context
= (ClientRequestContext
*)data
;
668 ClientHttpRequest
*http
= context
->http
;
669 context
->acl_checklist
= NULL
;
671 if (answer
== ACCESS_ALLOWED
)
672 redirectStart(http
, clientRedirectDoneWrapper
, context
);
674 context
->clientRedirectDone(NULL
);
678 ClientRequestContext::clientRedirectStart()
680 debugs(33, 5, "clientRedirectStart: '" << http
->uri
<< "'");
682 if (Config
.accessList
.redirector
) {
683 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.redirector
, http
);
684 acl_checklist
->nonBlockingCheck(clientRedirectAccessCheckDone
, this);
686 redirectStart(http
, clientRedirectDoneWrapper
, this);
690 clientHierarchical(ClientHttpRequest
* http
)
692 const char *url
= http
->uri
;
693 HttpRequest
*request
= http
->request
;
694 HttpRequestMethod method
= request
->method
;
695 const wordlist
*p
= NULL
;
698 * IMS needs a private key, so we can use the hierarchy for IMS only if our
699 * neighbors support private keys
702 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
706 * This is incorrect: authenticating requests can be sent via a hierarchy
707 * (they can even be cached if the correct headers are set on the reply)
709 if (request
->flags
.auth
)
712 if (method
== METHOD_TRACE
)
715 if (method
!= METHOD_GET
)
718 /* scan hierarchy_stoplist */
719 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
720 if (strstr(url
, p
->key
))
723 if (request
->flags
.loopdetect
)
726 if (request
->protocol
== PROTO_HTTP
)
727 return httpCachable(method
);
729 if (request
->protocol
== PROTO_GOPHER
)
730 return gopherCachable(request
);
732 if (request
->protocol
== PROTO_CACHEOBJ
)
740 clientInterpretRequestHeaders(ClientHttpRequest
* http
)
742 HttpRequest
*request
= http
->request
;
743 HttpHeader
*req_hdr
= &request
->header
;
745 #if !(USE_SQUID_ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
750 request
->imslen
= -1;
751 request
->ims
= req_hdr
->getTime(HDR_IF_MODIFIED_SINCE
);
753 if (request
->ims
> 0)
754 request
->flags
.ims
= 1;
758 * We ignore Cache-Control as per the Edge Architecture Section 3. See
759 * www.esi.org for more information.
763 if (req_hdr
->has(HDR_PRAGMA
)) {
764 String s
= req_hdr
->getList(HDR_PRAGMA
);
766 if (strListIsMember(&s
, "no-cache", ','))
772 if (request
->cache_control
)
773 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
777 * Work around for supporting the Reload button in IE browsers when Squid
778 * is used as an accelerator or transparent proxy, by turning accelerated
779 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
780 * actually only fixed in SP1, but we can't tell whether we are talking to
781 * SP1 or not so all 5.5 versions are treated 'normally').
783 if (Config
.onoff
.ie_refresh
) {
784 if (http
->flags
.accel
&& request
->flags
.ims
) {
785 if ((str
= req_hdr
->getStr(HDR_USER_AGENT
))) {
786 if (strstr(str
, "MSIE 5.01") != NULL
)
788 else if (strstr(str
, "MSIE 5.0") != NULL
)
790 else if (strstr(str
, "MSIE 4.") != NULL
)
792 else if (strstr(str
, "MSIE 3.") != NULL
)
798 if (request
->method
== METHOD_OTHER
) {
806 if (Config
.onoff
.reload_into_ims
)
807 request
->flags
.nocache_hack
= 1;
808 else if (refresh_nocache_hack
)
809 request
->flags
.nocache_hack
= 1;
813 request
->flags
.nocache
= 1;
816 /* ignore range header in non-GETs or non-HEADs */
817 if (request
->method
== METHOD_GET
|| request
->method
== METHOD_HEAD
) {
818 request
->range
= req_hdr
->getRange();
820 if (request
->range
) {
821 request
->flags
.range
= 1;
822 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
823 /* XXX: This is suboptimal. We should give the stream the range set,
824 * and thereby let the top of the stream set the offset when the
825 * size becomes known. As it is, we will end up requesting from 0
826 * for evey -X range specification.
827 * RBC - this may be somewhat wrong. We should probably set the range
828 * iter up at this point.
830 node
->readBuffer
.offset
= request
->range
->lowestOffset(0);
831 http
->range_iter
.pos
= request
->range
->begin();
832 http
->range_iter
.valid
= true;
836 /* Only HEAD and GET requests permit a Range or Request-Range header.
837 * If these headers appear on any other type of request, delete them now.
840 req_hdr
->delById(HDR_RANGE
);
841 req_hdr
->delById(HDR_REQUEST_RANGE
);
842 request
->range
= NULL
;
845 if (req_hdr
->has(HDR_AUTHORIZATION
))
846 request
->flags
.auth
= 1;
848 ConnStateData
*http_conn
= http
->getConn();
850 request
->flags
.connection_auth_disabled
= http_conn
->port
->connection_auth_disabled
;
851 if (!request
->flags
.connection_auth_disabled
) {
852 if (http_conn
->pinning
.fd
!= -1) {
853 if (http_conn
->pinning
.auth
) {
854 request
->flags
.connection_auth
= 1;
855 request
->flags
.auth
= 1;
857 request
->flags
.connection_proxy_auth
= 1;
859 request
->setPinnedConnection(http_conn
);
863 /* check if connection auth is used, and flag as candidate for pinning
865 * Note: we may need to set flags.connection_auth even if the connection
866 * is already pinned if it was pinned earlier due to proxy auth
868 if (!request
->flags
.connection_auth
) {
869 if (req_hdr
->has(HDR_AUTHORIZATION
) || req_hdr
->has(HDR_PROXY_AUTHORIZATION
)) {
870 HttpHeaderPos pos
= HttpHeaderInitPos
;
873 while ((e
= req_hdr
->getEntry(&pos
))) {
874 if (e
->id
== HDR_AUTHORIZATION
|| e
->id
== HDR_PROXY_AUTHORIZATION
) {
875 const char *value
= e
->value
.rawBuf();
876 if (strncasecmp(value
, "NTLM ", 5) == 0
878 strncasecmp(value
, "Negotiate ", 10) == 0
880 strncasecmp(value
, "Kerberos ", 9) == 0) {
881 if (e
->id
== HDR_AUTHORIZATION
) {
882 request
->flags
.connection_auth
= 1;
885 request
->flags
.connection_proxy_auth
= 1;
891 if (may_pin
&& !request
->pinnedConnection()) {
892 request
->setPinnedConnection(http
->getConn());
898 if (request
->login
[0] != '\0')
899 request
->flags
.auth
= 1;
901 if (req_hdr
->has(HDR_VIA
)) {
902 String s
= req_hdr
->getList(HDR_VIA
);
904 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
905 * Note ThisCache2 has a space prepended to the hostname so we don't
906 * accidentally match super-domains.
909 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
910 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
911 request
, (ObjPackMethod
) & httpRequestPack
);
912 request
->flags
.loopdetect
= 1;
916 fvdbCountVia(s
.termedBuf());
924 \todo --enable-useragent-log and --enable-referer-log. We should
925 probably drop those two as the custom log formats accomplish pretty much the same thing..
927 #if USE_USERAGENT_LOG
928 if ((str
= req_hdr
->getStr(HDR_USER_AGENT
)))
929 logUserAgent(fqdnFromAddr(http
->getConn()->log_addr
), str
);
934 if ((str
= req_hdr
->getStr(HDR_REFERER
)))
935 logReferer(fqdnFromAddr(http
->getConn()->log_addr
), str
, http
->log_uri
);
940 if (req_hdr
->has(HDR_X_FORWARDED_FOR
)) {
941 String s
= req_hdr
->getList(HDR_X_FORWARDED_FOR
);
942 fvdbCountForw(s
.termedBuf());
947 if (request
->method
== METHOD_TRACE
|| request
->method
== METHOD_OPTIONS
) {
948 request
->max_forwards
= req_hdr
->getInt64(HDR_MAX_FORWARDS
);
951 request
->flags
.cachable
= http
->request
->cacheable();
953 if (clientHierarchical(http
))
954 request
->flags
.hierarchical
= 1;
956 debugs(85, 5, "clientInterpretRequestHeaders: REQ_NOCACHE = " <<
957 (request
->flags
.nocache
? "SET" : "NOT SET"));
958 debugs(85, 5, "clientInterpretRequestHeaders: REQ_CACHABLE = " <<
959 (request
->flags
.cachable
? "SET" : "NOT SET"));
960 debugs(85, 5, "clientInterpretRequestHeaders: REQ_HIERARCHICAL = " <<
961 (request
->flags
.hierarchical
? "SET" : "NOT SET"));
966 clientRedirectDoneWrapper(void *data
, char *result
)
968 ClientRequestContext
*calloutContext
= (ClientRequestContext
*)data
;
970 if (!calloutContext
->httpStateIsValid())
973 calloutContext
->clientRedirectDone(result
);
977 ClientRequestContext::clientRedirectDone(char *result
)
979 HttpRequest
*new_request
= NULL
;
980 HttpRequest
*old_request
= http
->request
;
981 debugs(85, 5, "clientRedirectDone: '" << http
->uri
<< "' result=" << (result
? result
: "NULL"));
982 assert(redirect_state
== REDIRECT_PENDING
);
983 redirect_state
= REDIRECT_DONE
;
986 http_status status
= (http_status
) atoi(result
);
988 if (status
== HTTP_MOVED_PERMANENTLY
989 || status
== HTTP_MOVED_TEMPORARILY
990 || status
== HTTP_SEE_OTHER
991 || status
== HTTP_TEMPORARY_REDIRECT
) {
994 if ((t
= strchr(result
, ':')) != NULL
) {
995 http
->redirect
.status
= status
;
996 http
->redirect
.location
= xstrdup(t
+ 1);
998 debugs(85, 1, "clientRedirectDone: bad input: " << result
);
1000 } else if (strcmp(result
, http
->uri
))
1001 new_request
= HttpRequest::CreateFromUrlAndMethod(result
, old_request
->method
);
1005 safe_free(http
->uri
);
1006 http
->uri
= xstrdup(urlCanonical(new_request
));
1007 new_request
->http_ver
= old_request
->http_ver
;
1008 new_request
->header
.append(&old_request
->header
);
1009 new_request
->client_addr
= old_request
->client_addr
;
1010 #if FOLLOW_X_FORWARDED_FOR
1011 new_request
->indirect_client_addr
= old_request
->indirect_client_addr
;
1012 #endif /* FOLLOW_X_FORWARDED_FOR */
1013 new_request
->my_addr
= old_request
->my_addr
;
1014 new_request
->flags
= old_request
->flags
;
1015 new_request
->flags
.redirected
= 1;
1017 if (old_request
->auth_user_request
) {
1018 new_request
->auth_user_request
= old_request
->auth_user_request
;
1019 AUTHUSERREQUESTLOCK(new_request
->auth_user_request
, "new request");
1022 if (old_request
->body_pipe
!= NULL
) {
1023 new_request
->body_pipe
= old_request
->body_pipe
;
1024 old_request
->body_pipe
= NULL
;
1025 debugs(0,0,HERE
<< "redirecting body_pipe " << new_request
->body_pipe
<< " from request " << old_request
<< " to " << new_request
);
1028 new_request
->content_length
= old_request
->content_length
;
1029 new_request
->extacl_user
= old_request
->extacl_user
;
1030 new_request
->extacl_passwd
= old_request
->extacl_passwd
;
1031 new_request
->flags
.proxy_keepalive
= old_request
->flags
.proxy_keepalive
;
1032 HTTPMSGUNLOCK(old_request
);
1033 http
->request
= HTTPMSGLOCK(new_request
);
1036 /* FIXME PIPELINE: This is innacurate during pipelining */
1038 if (http
->getConn() != NULL
)
1039 fd_note(http
->getConn()->fd
, http
->uri
);
1046 /** Test cache allow/deny configuration
1047 * Sets flags.cachable=1 if caching is not denied.
1050 ClientRequestContext::checkNoCache()
1052 if (Config
.accessList
.noCache
) {
1053 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.noCache
, http
);
1054 acl_checklist
->nonBlockingCheck(checkNoCacheDoneWrapper
, this);
1056 /* unless otherwise specified, we try to cache. */
1057 checkNoCacheDone(1);
1062 checkNoCacheDoneWrapper(int answer
, void *data
)
1064 ClientRequestContext
*calloutContext
= (ClientRequestContext
*) data
;
1066 if (!calloutContext
->httpStateIsValid())
1069 calloutContext
->checkNoCacheDone(answer
);
1073 ClientRequestContext::checkNoCacheDone(int answer
)
1075 acl_checklist
= NULL
;
1076 http
->request
->flags
.cachable
= answer
;
1081 * Identify requests that do not go through the store and client side stream
1082 * and forward them to the appropriate location. All other requests, request
1086 ClientHttpRequest::processRequest()
1088 debugs(85, 4, "clientProcessRequest: " << RequestMethodStr(request
->method
) << " '" << uri
<< "'");
1091 if (request
->method
== METHOD_CONNECT
&& sslBumpNeeded()) {
1097 if (request
->method
== METHOD_CONNECT
&& !redirect
.status
) {
1098 logType
= LOG_TCP_MISS
;
1099 tunnelStart(this, &out
.size
, &al
.http
.code
);
1107 ClientHttpRequest::httpStart()
1109 PROF_start(httpStart
);
1110 logType
= LOG_TAG_NONE
;
1111 debugs(85, 4, "ClientHttpRequest::httpStart: " << log_tags
[logType
] << " for '" << uri
<< "'");
1113 /* no one should have touched this */
1114 assert(out
.offset
== 0);
1115 /* Use the Stream Luke */
1116 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->data
;
1117 clientStreamRead(node
, this, node
->readBuffer
);
1118 PROF_stop(httpStart
);
1123 // determines whether we should bump the CONNECT request
1125 ClientHttpRequest::sslBumpNeeded() const
1127 if (!getConn()->port
->sslBump
|| !Config
.accessList
.ssl_bump
)
1130 debugs(85, 5, HERE
<< "SslBump possible, checking ACL");
1132 ACLFilledChecklist
check(Config
.accessList
.ssl_bump
, request
, NULL
);
1133 check
.src_addr
= request
->client_addr
;
1134 check
.my_addr
= request
->my_addr
;
1135 return check
.fastCheck() == 1;
1138 // called when comm_write has completed
1140 SslBumpEstablish(int, char *, size_t, comm_err_t errflag
, int, void *data
)
1142 ClientHttpRequest
*r
= static_cast<ClientHttpRequest
*>(data
);
1143 debugs(85, 5, HERE
<< "responded to CONNECT: " << r
<< " ? " << errflag
);
1145 assert(r
&& cbdataReferenceValid(r
));
1146 r
->sslBumpEstablish(errflag
);
1150 ClientHttpRequest::sslBumpEstablish(comm_err_t errflag
)
1152 // Bail out quickly on COMM_ERR_CLOSING - close handlers will tidy up
1153 if (errflag
== COMM_ERR_CLOSING
)
1157 getConn()->startClosing("CONNECT response failure in SslBump");
1161 getConn()->switchToHttps();
1165 ClientHttpRequest::sslBumpStart()
1167 debugs(85, 5, HERE
<< "ClientHttpRequest::sslBumpStart");
1169 // send an HTTP 200 response to kick client SSL negotiation
1170 const int fd
= getConn()->fd
;
1171 debugs(33, 7, HERE
<< "Confirming CONNECT tunnel on FD " << fd
);
1173 // TODO: Unify with tunnel.cc and add a Server(?) header
1174 static const char *const conn_established
=
1175 "HTTP/1.0 200 Connection established\r\n\r\n";
1176 comm_write(fd
, conn_established
, strlen(conn_established
),
1177 &SslBumpEstablish
, this, NULL
);
1183 ClientHttpRequest::gotEnough() const
1185 /** TODO: should be querying the stream. */
1186 int64_t contentLength
=
1187 memObject()->getReply()->bodySize(request
->method
);
1188 assert(contentLength
>= 0);
1190 if (out
.offset
< contentLength
)
1197 ClientHttpRequest::storeEntry(StoreEntry
*newEntry
)
1203 ClientHttpRequest::loggingEntry(StoreEntry
*newEntry
)
1206 loggingEntry_
->unlock();
1208 loggingEntry_
= newEntry
;
1211 loggingEntry_
->lock();
1215 * doCallouts() - This function controls the order of "callout"
1216 * executions, including non-blocking access control checks, the
1217 * redirector, and ICAP. Previously, these callouts were chained
1218 * together such that "clientAccessCheckDone()" would call
1219 * "clientRedirectStart()" and so on.
1221 * The ClientRequestContext (aka calloutContext) class holds certain
1222 * state data for the callout/callback operations. Previously
1223 * ClientHttpRequest would sort of hand off control to ClientRequestContext
1224 * for a short time. ClientRequestContext would then delete itself
1225 * and pass control back to ClientHttpRequest when all callouts
1228 * This caused some problems for ICAP because we want to make the
1229 * ICAP callout after checking ACLs, but before checking the no_cache
1230 * list. We can't stuff the ICAP state into the ClientRequestContext
1231 * class because we still need the ICAP state after ClientRequestContext
1234 * Note that ClientRequestContext is created before the first call
1237 * If one of the callouts notices that ClientHttpRequest is no
1238 * longer valid, it should call cbdataReferenceDone() so that
1239 * ClientHttpRequest's reference count goes to zero and it will get
1240 * deleted. ClientHttpRequest will then delete ClientRequestContext.
1242 * Note that we set the _done flags here before actually starting
1243 * the callout. This is strictly for convenience.
1246 extern int aclMapTOS (acl_tos
* head
, ACLChecklist
* ch
);
1249 ClientHttpRequest::doCallouts()
1251 assert(calloutContext
);
1253 if (!calloutContext
->http_access_done
) {
1254 debugs(83, 3, HERE
<< "Doing calloutContext->clientAccessCheck()");
1255 calloutContext
->http_access_done
= true;
1256 calloutContext
->clientAccessCheck();
1261 if (!calloutContext
->adaptation_acl_check_done
) {
1262 calloutContext
->adaptation_acl_check_done
= true;
1263 if (Adaptation::AccessCheck::Start(
1264 Adaptation::methodReqmod
, Adaptation::pointPreCache
,
1265 request
, NULL
, adaptationAclCheckDoneWrapper
, calloutContext
))
1266 return; // will call callback
1270 if (!calloutContext
->redirect_done
) {
1271 calloutContext
->redirect_done
= true;
1272 assert(calloutContext
->redirect_state
== REDIRECT_NONE
);
1274 if (Config
.Program
.redirect
) {
1275 debugs(83, 3, HERE
<< "Doing calloutContext->clientRedirectStart()");
1276 calloutContext
->redirect_state
= REDIRECT_PENDING
;
1277 calloutContext
->clientRedirectStart();
1282 if (!calloutContext
->interpreted_req_hdrs
) {
1283 debugs(83, 3, HERE
<< "Doing clientInterpretRequestHeaders()");
1284 calloutContext
->interpreted_req_hdrs
= 1;
1285 clientInterpretRequestHeaders(this);
1288 if (!calloutContext
->no_cache_done
) {
1289 calloutContext
->no_cache_done
= true;
1291 if (Config
.accessList
.noCache
&& request
->flags
.cachable
) {
1292 debugs(83, 3, HERE
<< "Doing calloutContext->checkNoCache()");
1293 calloutContext
->checkNoCache();
1298 if (!calloutContext
->clientside_tos_done
) {
1299 calloutContext
->clientside_tos_done
= true;
1300 if (getConn() != NULL
) {
1301 ACLFilledChecklist
ch(NULL
, request
, NULL
);
1302 ch
.src_addr
= request
->client_addr
;
1303 ch
.my_addr
= request
->my_addr
;
1304 int tos
= aclMapTOS(Config
.accessList
.clientside_tos
, &ch
);
1306 comm_set_tos(getConn()->fd
, tos
);
1310 cbdataReferenceDone(calloutContext
->http
);
1311 delete calloutContext
;
1312 calloutContext
= NULL
;
1315 headersLog(0, 1, request
->method
, request
);
1318 debugs(83, 3, HERE
<< "calling processRequest()");
1322 Adaptation::Icap::History::Pointer ih
= request
->icapHistory();
1324 ih
->logType
= logType
;
1328 #ifndef _USE_INLINE_
1329 #include "client_side_request.cci"
1333 /// Initiate an asynchronous adaptation transaction which will call us back.
1335 ClientHttpRequest::startAdaptation(const Adaptation::ServiceGroupPointer
&g
)
1337 debugs(85, 3, HERE
<< "adaptation needed for " << this);
1338 assert(!virginHeadSource
);
1339 assert(!adaptedBodySource
);
1340 virginHeadSource
= initiateAdaptation(
1341 new Adaptation::Iterator(this, request
, NULL
, g
));
1343 // we could try to guess whether we can bypass this adaptation
1344 // initiation failure, but it should not really happen
1345 assert(virginHeadSource
!= NULL
); // Must, really
1349 ClientHttpRequest::noteAdaptationAnswer(HttpMsg
*msg
)
1351 assert(cbdataReferenceValid(this)); // indicates bug
1354 if (HttpRequest
*new_req
= dynamic_cast<HttpRequest
*>(msg
)) {
1356 * Replace the old request with the new request.
1358 HTTPMSGUNLOCK(request
);
1359 request
= HTTPMSGLOCK(new_req
);
1361 * Store the new URI for logging
1364 uri
= xstrdup(urlCanonical(request
));
1365 setLogUri(this, urlCanonicalClean(request
));
1366 assert(request
->method
.id());
1367 } else if (HttpReply
*new_rep
= dynamic_cast<HttpReply
*>(msg
)) {
1368 debugs(85,3,HERE
<< "REQMOD reply is HTTP reply");
1370 // subscribe to receive reply body
1371 if (new_rep
->body_pipe
!= NULL
) {
1372 adaptedBodySource
= new_rep
->body_pipe
;
1373 assert(adaptedBodySource
->setConsumerIfNotLate(this));
1376 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->prev
->data
;
1377 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1378 repContext
->createStoreEntry(request
->method
, request
->flags
);
1380 EBIT_CLR(storeEntry()->flags
, ENTRY_FWD_HDR_WAIT
);
1381 request_satisfaction_mode
= true;
1382 request_satisfaction_offset
= 0;
1383 storeEntry()->replaceHttpReply(new_rep
);
1385 if (!adaptedBodySource
) // no body
1386 storeEntry()->complete();
1387 clientGetMoreData(node
, this);
1390 // we are done with getting headers (but may be receiving body)
1391 clearAdaptation(virginHeadSource
);
1393 if (!request_satisfaction_mode
)
1398 ClientHttpRequest::noteAdaptationQueryAbort(bool final
)
1400 clearAdaptation(virginHeadSource
);
1401 assert(!adaptedBodySource
);
1402 handleAdaptationFailure(!final
);
1406 ClientHttpRequest::noteMoreBodyDataAvailable(BodyPipe::Pointer
)
1408 assert(request_satisfaction_mode
);
1409 assert(adaptedBodySource
!= NULL
);
1411 if (const size_t contentSize
= adaptedBodySource
->buf().contentSize()) {
1412 BodyPipeCheckout
bpc(*adaptedBodySource
);
1413 const StoreIOBuffer
ioBuf(&bpc
.buf
, request_satisfaction_offset
);
1414 storeEntry()->write(ioBuf
);
1415 // assume can write everything
1416 request_satisfaction_offset
+= contentSize
;
1417 bpc
.buf
.consume(contentSize
);
1421 if (adaptedBodySource
->exhausted())
1422 endRequestSatisfaction();
1423 // else wait for more body data
1427 ClientHttpRequest::noteBodyProductionEnded(BodyPipe::Pointer
)
1429 assert(!virginHeadSource
);
1430 if (adaptedBodySource
!= NULL
) { // did not end request satisfaction yet
1431 // We do not expect more because noteMoreBodyDataAvailable always
1432 // consumes everything. We do not even have a mechanism to consume
1433 // leftovers after noteMoreBodyDataAvailable notifications seize.
1434 assert(adaptedBodySource
->exhausted());
1435 endRequestSatisfaction();
1440 ClientHttpRequest::endRequestSatisfaction()
1442 debugs(85,4, HERE
<< this << " ends request satisfaction");
1443 assert(request_satisfaction_mode
);
1444 stopConsumingFrom(adaptedBodySource
);
1446 // TODO: anything else needed to end store entry formation correctly?
1447 storeEntry()->complete();
1451 ClientHttpRequest::noteBodyProducerAborted(BodyPipe::Pointer
)
1453 assert(!virginHeadSource
);
1454 stopConsumingFrom(adaptedBodySource
);
1455 handleAdaptationFailure();
1459 ClientHttpRequest::handleAdaptationFailure(bool bypassable
)
1461 debugs(85,3, HERE
<< "handleAdaptationFailure(" << bypassable
<< ")");
1463 const bool usedStore
= storeEntry() && !storeEntry()->isEmpty();
1464 const bool usedPipe
= request
->body_pipe
!= NULL
&&
1465 request
->body_pipe
->consumedSize() > 0;
1467 if (bypassable
&& !usedStore
&& !usedPipe
) {
1468 debugs(85,3, HERE
<< "ICAP REQMOD callout failed, bypassing: " << calloutContext
);
1474 debugs(85,3, HERE
<< "ICAP REQMOD callout failed, responding with error");
1476 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->prev
->data
;
1477 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1480 // The original author of the code also wanted to pass an errno to
1481 // setReplyToError, but it seems unlikely that the errno reflects the
1482 // true cause of the error at this point, so I did not pass it.
1485 ConnStateData
* c
= getConn();
1486 repContext
->setReplyToError(ERR_ICAP_FAILURE
, HTTP_INTERNAL_SERVER_ERROR
,
1487 request
->method
, NULL
,
1488 (c
!= NULL
? c
->peer
: noAddr
), request
, NULL
,
1489 (c
!= NULL
&& c
->auth_user_request
?
1490 c
->auth_user_request
: request
->auth_user_request
));
1492 node
= (clientStreamNode
*)client_stream
.tail
->data
;
1493 clientStreamRead(node
, this, node
->readBuffer
);