3 * $Id: client_side_request.cc,v 1.60 2006/04/02 11:58:38 serassio Exp $
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 the Internet
12 * community; see the CONTRIBUTORS file for full details. Many organizations
13 * have provided support for Squid's development; see the SPONSORS file for
14 * full details. Squid is Copyrighted (C) 2001 by the Regents of the
15 * University of California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other sources; see the
17 * CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify it under
20 * the terms of the GNU General Public License as published by the Free
21 * Software Foundation; either version 2 of the License, or (at your option)
24 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
25 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
29 * You should have received a copy of the GNU General Public License along with
30 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
31 * Place, Suite 330, Boston, MA 02111, USA.
37 * General logic of request processing:
39 * We run a series of tests to determine if access will be permitted, and to do
40 * any redirection. Then we call into the result clientStream to retrieve data.
41 * From that point on it's up to reply management.
45 #include "clientStream.h"
46 #include "client_side_request.h"
47 #include "AuthUserRequest.h"
48 #include "HttpRequest.h"
49 #include "ACLChecklist.h"
51 #include "client_side.h"
52 #include "client_side_reply.h"
54 #include "HttpReply.h"
55 #include "MemObject.h"
56 #include "ClientRequestContext.h"
59 #include "ICAP/ICAPClientReqmodPrecache.h"
60 #include "ICAP/ICAPElements.h"
61 #include "ICAP/ICAPConfig.h"
62 static void icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service
, void *data
);
63 extern ICAPConfig TheICAPConfig
;
67 #define comm_close comm_lingering_close
70 static const char *const crlf
= "\r\n";
72 CBDATA_CLASS_INIT(ClientRequestContext
);
75 ClientRequestContext::operator new (size_t size
)
77 assert (size
== sizeof(ClientRequestContext
));
78 CBDATA_INIT_TYPE(ClientRequestContext
);
79 ClientRequestContext
*result
= cbdataAlloc(ClientRequestContext
);
84 ClientRequestContext::operator delete (void *address
)
86 ClientRequestContext
*t
= static_cast<ClientRequestContext
*>(address
);
92 static void clientAccessCheckDoneWrapper(int, void *);
93 static int clientCachable(ClientHttpRequest
* http
);
94 static int clientHierarchical(ClientHttpRequest
* http
);
95 static void clientInterpretRequestHeaders(ClientHttpRequest
* http
);
96 static RH clientRedirectDoneWrapper
;
97 static PF checkNoCacheDoneWrapper
;
98 extern "C" CSR clientGetMoreData
;
99 extern "C" CSS clientReplyStatus
;
100 extern "C" CSD clientReplyDetach
;
101 static void checkFailureRatio(err_type
, hier_code
);
103 ClientRequestContext::~ClientRequestContext()
106 * Release our "lock" on our parent, ClientHttpRequest, if we
111 cbdataReferenceDone(http
);
114 if (acl_checklist
->asyncInProgress()) {
115 acl_checklist
->markDeleteWhenDone();
117 delete acl_checklist
;
122 ClientRequestContext::ClientRequestContext(ClientHttpRequest
*anHttp
) : http(anHttp
), acl_checklist (NULL
), redirect_state (REDIRECT_NONE
)
124 (void) cbdataReference(http
);
125 http_access_done
= false;
126 redirect_done
= false;
127 no_cache_done
= false;
128 interpreted_req_hdrs
= false;
131 CBDATA_CLASS_INIT(ClientHttpRequest
);
134 ClientHttpRequest::operator new (size_t size
)
136 assert (size
== sizeof (ClientHttpRequest
));
137 CBDATA_INIT_TYPE(ClientHttpRequest
);
138 ClientHttpRequest
*result
= cbdataAlloc(ClientHttpRequest
);
143 ClientHttpRequest::operator delete (void *address
)
145 ClientHttpRequest
*t
= static_cast<ClientHttpRequest
*>(address
);
149 ClientHttpRequest::ClientHttpRequest(ConnStateData::Pointer aConn
) : loggingEntry_(NULL
)
151 start
= current_time
;
153 dlinkAdd(this, &active
, &ClientActiveRequests
);
156 request_satisfaction_mode
= false;
161 * returns true if client specified that the object must come from the cache
162 * without contacting origin server
165 ClientHttpRequest::onlyIfCached()const
168 return request
->cache_control
&&
169 EBIT_TEST(request
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
173 * This function is designed to serve a fairly specific purpose.
174 * Occasionally our vBNS-connected caches can talk to each other, but not
175 * the rest of the world. Here we try to detect frequent failures which
176 * make the cache unusable (e.g. DNS lookup and connect() failures). If
177 * the failure:success ratio goes above 1.0 then we go into "hit only"
178 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
179 * will only fetch HITs from us if they are using the ICP protocol. We
180 * stay in this mode for 5 minutes.
182 * Duane W., Sept 16, 1996
185 #define FAILURE_MODE_TIME 300
188 checkFailureRatio(err_type etype
, hier_code hcode
)
190 static double magic_factor
= 100.0;
194 if (hcode
== HIER_NONE
)
197 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
199 n_bad
= magic_factor
- n_good
;
205 case ERR_CONNECT_FAIL
:
215 request_failure_ratio
= n_bad
/ n_good
;
217 if (hit_only_mode_until
> squid_curtime
)
220 if (request_failure_ratio
< 1.0)
223 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
225 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
226 FAILURE_MODE_TIME
/ 60);
228 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
230 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
233 ClientHttpRequest::~ClientHttpRequest()
235 debug(33, 3) ("httpRequestFree: %s\n", uri
);
236 /* if body_connection !NULL, then ProcessBody has not
237 * found the end of the body yet
240 if (request
&& request
->body_connection
.getRaw() != NULL
) {
241 clientAbortBody(request
); /* abort body transter */
242 request
->body_connection
= NULL
;
245 /* the ICP check here was erroneous
246 * - storeReleaseRequest was always called if entry was valid
248 assert(logType
< LOG_TYPE_MAX
);
255 checkFailureRatio(request
->errType
, al
.hier
.code
);
263 cbdataReferenceDone(icap
);
268 delete calloutContext
;
270 /* moving to the next connection is handled by the context free */
271 dlinkDelete(&active
, &ClientActiveRequests
);
274 /* Create a request and kick it off */
276 * TODO: Pass in the buffers to be used in the inital Read request, as they are
277 * determined by the user
279 int /* returns nonzero on failure */
280 clientBeginRequest(method_t method
, char const *url
, CSCB
* streamcallback
,
281 CSD
* streamdetach
, ClientStreamData streamdata
, HttpHeader
const *header
,
282 char *tailbuf
, size_t taillen
)
285 HttpVersion
http_ver (1, 0);
286 ClientHttpRequest
*http
= new ClientHttpRequest(NULL
);
287 HttpRequest
*request
;
288 StoreIOBuffer tempBuffer
;
289 http
->start
= current_time
;
290 /* this is only used to adjust the connection offset in client_side.c */
292 tempBuffer
.length
= taillen
;
293 tempBuffer
.data
= tailbuf
;
294 /* client stream setup */
295 clientStreamInit(&http
->client_stream
, clientGetMoreData
, clientReplyDetach
,
296 clientReplyStatus
, new clientReplyContext(http
), streamcallback
,
297 streamdetach
, streamdata
, tempBuffer
);
298 /* make it visible in the 'current acctive requests list' */
300 /* internal requests only makes sense in an
301 * accelerator today. TODO: accept flags ? */
302 http
->flags
.accel
= 1;
303 /* allow size for url rewriting */
304 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
305 http
->uri
= (char *)xcalloc(url_sz
, 1);
306 strcpy(http
->uri
, url
);
308 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
309 debug(85, 5) ("Invalid URL: %s\n", http
->uri
);
314 * now update the headers in request with our supplied headers. urLParse
315 * should return a blank header set, but we use Update to be sure of
319 httpHeaderUpdate(&request
->header
, header
, NULL
);
321 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
323 /* http struct now ready */
326 * build new header list *? TODO
328 request
->flags
.accelerated
= http
->flags
.accel
;
330 request
->flags
.internalclient
= 1;
332 /* this is an internally created
333 * request, not subject to acceleration
334 * target overrides */
336 * FIXME? Do we want to detect and handle internal requests of internal
340 /* Internally created requests cannot have bodies today */
341 request
->content_length
= 0;
343 request
->client_addr
= no_addr
;
345 request
->my_addr
= no_addr
; /* undefined for internal requests */
347 request
->my_port
= 0;
349 request
->http_ver
= http_ver
;
351 http
->request
= HTTPMSGLOCK(request
);
353 /* optional - skip the access check ? */
354 http
->calloutContext
= new ClientRequestContext(http
);
356 http
->calloutContext
->http_access_done
= false;
358 http
->calloutContext
->redirect_done
= true;
360 http
->calloutContext
->no_cache_done
= true;
368 ClientRequestContext::httpStateIsValid()
370 ClientHttpRequest
*http_
= http
;
372 if (cbdataReferenceValid(http_
))
377 cbdataReferenceDone(http_
);
382 /* This is the entry point for external users of the client_side routines */
384 ClientRequestContext::clientAccessCheck()
387 clientAclChecklistCreate(Config
.accessList
.http
, http
);
388 acl_checklist
->nonBlockingCheck(clientAccessCheckDoneWrapper
, this);
392 clientAccessCheckDoneWrapper(int answer
, void *data
)
394 ClientRequestContext
*calloutContext
= (ClientRequestContext
*) data
;
396 if (!calloutContext
->httpStateIsValid())
399 calloutContext
->clientAccessCheckDone(answer
);
403 ClientRequestContext::clientAccessCheckDone(int answer
)
405 acl_checklist
= NULL
;
408 debug(85, 2) ("The request %s %s is %s, because it matched '%s'\n",
409 RequestMethodStr
[http
->request
->method
], http
->uri
,
410 answer
== ACCESS_ALLOWED
? "ALLOWED" : "DENIED",
411 AclMatchedName
? AclMatchedName
: "NO ACL's");
412 char const *proxy_auth_msg
= "<null>";
414 if (http
->getConn().getRaw() != NULL
&& http
->getConn()->auth_user_request
!= NULL
)
415 proxy_auth_msg
= http
->getConn()->auth_user_request
->denyMessage("<null>");
416 else if (http
->request
->auth_user_request
!= NULL
)
417 proxy_auth_msg
= http
->request
->auth_user_request
->denyMessage("<null>");
419 if (answer
!= ACCESS_ALLOWED
) {
421 debug(85, 5) ("Access Denied: %s\n", http
->uri
);
422 debug(85, 5) ("AclMatchedName = %s\n",
423 AclMatchedName
? AclMatchedName
: "<null>");
424 debug(85, 5) ("Proxy Auth Message = %s\n",
425 proxy_auth_msg
? proxy_auth_msg
: "<null>");
427 * NOTE: get page_id here, based on AclMatchedName because if
428 * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in
429 * the clientCreateStoreEntry() call just below. Pedro Ribeiro
432 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
433 http
->logType
= LOG_TCP_DENIED
;
435 if (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
)) {
436 if (!http
->flags
.accel
) {
437 /* Proxy authorisation needed */
438 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
440 /* WWW authorisation needed */
441 status
= HTTP_UNAUTHORIZED
;
444 if (page_id
== ERR_NONE
)
445 page_id
= ERR_CACHE_ACCESS_DENIED
;
447 status
= HTTP_FORBIDDEN
;
449 if (page_id
== ERR_NONE
)
450 page_id
= ERR_ACCESS_DENIED
;
453 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->prev
->data
;
454 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
456 repContext
->setReplyToError(page_id
, status
,
457 http
->request
->method
, NULL
,
458 http
->getConn().getRaw() != NULL
? &http
->getConn()->peer
.sin_addr
: &no_addr
, http
->request
,
459 NULL
, http
->getConn().getRaw() != NULL
460 && http
->getConn()->auth_user_request
? http
->getConn()->
461 auth_user_request
: http
->request
->auth_user_request
);
462 node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
463 clientStreamRead(node
, http
, node
->readBuffer
);
467 /* ACCESS_ALLOWED continues here ... */
468 safe_free(http
->uri
);
470 http
->uri
= xstrdup(urlCanonical(http
->request
));
477 ClientRequestContext::icapAccessCheck()
479 ICAPAccessCheck
*icap_access_check
;
481 icap_access_check
= new ICAPAccessCheck(ICAP::methodReqmod
, ICAP::pointPreCache
, http
->request
, NULL
, icapAclCheckDoneWrapper
, this);
483 if (icap_access_check
!= NULL
) {
484 icap_access_check
->check();
492 icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service
, void *data
)
494 ClientRequestContext
*calloutContext
= (ClientRequestContext
*)data
;
496 if (!calloutContext
->httpStateIsValid())
499 calloutContext
->icapAclCheckDone(service
);
503 ClientRequestContext::icapAclCheckDone(ICAPServiceRep::Pointer service
)
506 * No matching ICAP service in the config file
509 if (service
== NULL
) {
515 * Setup ICAP state and such. If successful, just return.
516 * We'll get back to doCallouts() after REQMOD is done.
518 if (0 == http
->doIcap(service
))
522 * If doIcap() fails, then we have to either return an error
523 * to the user, or keep going without ICAP.
525 fatal("Fix this case in ClientRequestContext::icapAclCheckDone()");
533 clientRedirectAccessCheckDone(int answer
, void *data
)
535 ClientRequestContext
*context
= (ClientRequestContext
*)data
;
536 ClientHttpRequest
*http
= context
->http
;
537 context
->acl_checklist
= NULL
;
539 if (answer
== ACCESS_ALLOWED
)
540 redirectStart(http
, clientRedirectDoneWrapper
, context
);
542 context
->clientRedirectDone(NULL
);
546 ClientRequestContext::clientRedirectStart()
548 debug(33, 5) ("clientRedirectStart: '%s'\n", http
->uri
);
550 if (Config
.accessList
.redirector
) {
551 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.redirector
, http
);
552 acl_checklist
->nonBlockingCheck(clientRedirectAccessCheckDone
, this);
554 redirectStart(http
, clientRedirectDoneWrapper
, this);
558 clientCachable(ClientHttpRequest
* http
)
560 HttpRequest
*req
= http
->request
;
561 method_t method
= req
->method
;
563 if (req
->protocol
== PROTO_HTTP
)
564 return httpCachable(method
);
566 /* FTP is always cachable */
567 if (req
->protocol
== PROTO_WAIS
)
571 * The below looks questionable: what non HTTP protocols use connect,
572 * trace, put and post? RC
574 if (method
== METHOD_CONNECT
)
577 if (method
== METHOD_TRACE
)
580 if (method
== METHOD_PUT
)
583 if (method
== METHOD_POST
)
586 /* XXX POST may be cached sometimes.. ignored
589 if (req
->protocol
== PROTO_GOPHER
)
590 return gopherCachable(req
);
592 if (req
->protocol
== PROTO_CACHEOBJ
)
599 clientHierarchical(ClientHttpRequest
* http
)
601 const char *url
= http
->uri
;
602 HttpRequest
*request
= http
->request
;
603 method_t method
= request
->method
;
604 const wordlist
*p
= NULL
;
607 * IMS needs a private key, so we can use the hierarchy for IMS only if our
608 * neighbors support private keys
611 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
615 * This is incorrect: authenticating requests can be sent via a hierarchy
616 * (they can even be cached if the correct headers are set on the reply)
618 if (request
->flags
.auth
)
621 if (method
== METHOD_TRACE
)
624 if (method
!= METHOD_GET
)
627 /* scan hierarchy_stoplist */
628 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
629 if (strstr(url
, p
->key
))
632 if (request
->flags
.loopdetect
)
635 if (request
->protocol
== PROTO_HTTP
)
636 return httpCachable(method
);
638 if (request
->protocol
== PROTO_GOPHER
)
639 return gopherCachable(request
);
641 if (request
->protocol
== PROTO_WAIS
)
644 if (request
->protocol
== PROTO_CACHEOBJ
)
652 clientInterpretRequestHeaders(ClientHttpRequest
* http
)
654 HttpRequest
*request
= http
->request
;
655 const HttpHeader
*req_hdr
= &request
->header
;
657 #if !(ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
662 request
->imslen
= -1;
663 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
665 if (request
->ims
> 0)
666 request
->flags
.ims
= 1;
670 * We ignore Cache-Control as per the Edge Architecture Section 3. See
671 * www.esi.org for more information.
675 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
676 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
678 if (strListIsMember(&s
, "no-cache", ','))
684 if (request
->cache_control
)
685 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
689 * Work around for supporting the Reload button in IE browsers when Squid
690 * is used as an accelerator or transparent proxy, by turning accelerated
691 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
692 * actually only fixed in SP1, but we can't tell whether we are talking to
693 * SP1 or not so all 5.5 versions are treated 'normally').
695 if (Config
.onoff
.ie_refresh
) {
696 if (http
->flags
.accel
&& request
->flags
.ims
) {
697 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
))) {
698 if (strstr(str
, "MSIE 5.01") != NULL
)
700 else if (strstr(str
, "MSIE 5.0") != NULL
)
702 else if (strstr(str
, "MSIE 4.") != NULL
)
704 else if (strstr(str
, "MSIE 3.") != NULL
)
714 if (Config
.onoff
.reload_into_ims
)
715 request
->flags
.nocache_hack
= 1;
716 else if (refresh_nocache_hack
)
717 request
->flags
.nocache_hack
= 1;
721 request
->flags
.nocache
= 1;
724 /* ignore range header in non-GETs */
725 if (request
->method
== METHOD_GET
) {
726 request
->range
= httpHeaderGetRange(req_hdr
);
728 if (request
->range
) {
729 request
->flags
.range
= 1;
730 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
731 /* XXX: This is suboptimal. We should give the stream the range set,
732 * and thereby let the top of the stream set the offset when the
733 * size becomes known. As it is, we will end up requesting from 0
734 * for evey -X range specification.
735 * RBC - this may be somewhat wrong. We should probably set the range
736 * iter up at this point.
738 node
->readBuffer
.offset
= request
->range
->lowestOffset(0);
739 http
->range_iter
.pos
= request
->range
->begin();
740 http
->range_iter
.valid
= true;
744 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
745 request
->flags
.auth
= 1;
747 if (request
->login
[0] != '\0')
748 request
->flags
.auth
= 1;
750 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
751 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
753 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
754 * Note ThisCache2 has a space prepended to the hostname so we don't
755 * accidentally match super-domains.
758 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
759 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
760 request
, (ObjPackMethod
) & httpRequestPack
);
761 request
->flags
.loopdetect
= 1;
765 fvdbCountVia(s
.buf());
772 #if USE_USERAGENT_LOG
773 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
774 logUserAgent(fqdnFromAddr(http
->getConn().getRaw() ? http
->getConn()->log_addr
: no_addr
), str
);
779 if ((str
= httpHeaderGetStr(req_hdr
, HDR_REFERER
)))
780 logReferer(fqdnFromAddr(http
->getConn().getRaw() ? http
->getConn()->log_addr
: no_addr
), str
, http
->log_uri
);
785 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
786 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
787 fvdbCountForw(s
.buf());
792 if (request
->method
== METHOD_TRACE
) {
793 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
796 if (clientCachable(http
))
797 request
->flags
.cachable
= 1;
799 if (clientHierarchical(http
))
800 request
->flags
.hierarchical
= 1;
802 debug(85, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
803 request
->flags
.nocache
? "SET" : "NOT SET");
805 debug(85, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
806 request
->flags
.cachable
? "SET" : "NOT SET");
808 debug(85, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
809 request
->flags
.hierarchical
? "SET" : "NOT SET");
813 clientRedirectDoneWrapper(void *data
, char *result
)
815 ClientRequestContext
*calloutContext
= (ClientRequestContext
*)data
;
817 if (!calloutContext
->httpStateIsValid())
820 calloutContext
->clientRedirectDone(result
);
824 ClientRequestContext::clientRedirectDone(char *result
)
826 HttpRequest
*new_request
= NULL
;
827 HttpRequest
*old_request
= http
->request
;
828 debug(85, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
829 result
? result
: "NULL");
830 assert(redirect_state
== REDIRECT_PENDING
);
831 redirect_state
= REDIRECT_DONE
;
834 http_status status
= (http_status
) atoi(result
);
836 if (status
== HTTP_MOVED_PERMANENTLY
837 || status
== HTTP_MOVED_TEMPORARILY
838 || status
== HTTP_SEE_OTHER
839 || status
== HTTP_TEMPORARY_REDIRECT
) {
842 if ((t
= strchr(result
, ':')) != NULL
) {
843 http
->redirect
.status
= status
;
844 http
->redirect
.location
= xstrdup(t
+ 1);
846 debug(85, 1) ("clientRedirectDone: bad input: %s\n", result
);
848 } else if (strcmp(result
, http
->uri
))
849 new_request
= urlParse(old_request
->method
, result
);
853 safe_free(http
->uri
);
854 http
->uri
= xstrdup(urlCanonical(new_request
));
855 new_request
->http_ver
= old_request
->http_ver
;
856 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
857 new_request
->client_addr
= old_request
->client_addr
;
858 new_request
->client_port
= old_request
->client_port
;
859 new_request
->my_addr
= old_request
->my_addr
;
860 new_request
->my_port
= old_request
->my_port
;
861 new_request
->flags
= old_request
->flags
;
862 new_request
->flags
.redirected
= 1;
864 if (old_request
->auth_user_request
) {
865 new_request
->auth_user_request
= old_request
->auth_user_request
;
867 new_request
->auth_user_request
->lock()
872 if (old_request
->body_connection
.getRaw() != NULL
) {
873 new_request
->body_connection
= old_request
->body_connection
;
874 old_request
->body_connection
= NULL
;
877 new_request
->content_length
= old_request
->content_length
;
878 new_request
->extacl_user
= old_request
->extacl_user
;
879 new_request
->extacl_passwd
= old_request
->extacl_passwd
;
880 new_request
->flags
.proxy_keepalive
= old_request
->flags
.proxy_keepalive
;
881 HTTPMSGUNLOCK(old_request
);
882 http
->request
= HTTPMSGLOCK(new_request
);
885 /* FIXME PIPELINE: This is innacurate during pipelining */
887 if (http
->getConn().getRaw() != NULL
)
888 fd_note(http
->getConn()->fd
, http
->uri
);
896 ClientRequestContext::checkNoCache()
898 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.noCache
, http
);
899 acl_checklist
->nonBlockingCheck(checkNoCacheDoneWrapper
, this);
903 checkNoCacheDoneWrapper(int answer
, void *data
)
905 ClientRequestContext
*calloutContext
= (ClientRequestContext
*) data
;
907 if (!calloutContext
->httpStateIsValid())
910 calloutContext
->checkNoCacheDone(answer
);
914 ClientRequestContext::checkNoCacheDone(int answer
)
916 acl_checklist
= NULL
;
917 http
->request
->flags
.cachable
= answer
;
922 * Identify requests that do not go through the store and client side stream
923 * and forward them to the appropriate location. All other requests, request
927 ClientHttpRequest::processRequest()
929 debug(85, 4) ("clientProcessRequest: %s '%s'\n",
930 RequestMethodStr
[request
->method
], uri
);
932 if (request
->method
== METHOD_CONNECT
&& !redirect
.status
) {
933 logType
= LOG_TCP_MISS
;
934 sslStart(this, &out
.size
, &al
.http
.code
);
942 ClientHttpRequest::httpStart()
944 logType
= LOG_TAG_NONE
;
945 debug(85, 4) ("ClientHttpRequest::httpStart: %s for '%s'\n",
946 log_tags
[logType
], uri
);
947 /* no one should have touched this */
948 assert(out
.offset
== 0);
949 /* Use the Stream Luke */
950 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->data
;
951 clientStreamRead(node
, this, node
->readBuffer
);
955 ClientHttpRequest::gotEnough() const
957 /** TODO: should be querying the stream. */
959 memObject()->getReply()->bodySize(request
->method
);
960 assert(contentLength
>= 0);
962 if (out
.offset
< contentLength
)
969 ClientHttpRequest::maxReplyBodySize(ssize_t clen
)
971 maxReplyBodySize_
= clen
;
975 ClientHttpRequest::maxReplyBodySize() const
977 return maxReplyBodySize_
;
981 ClientHttpRequest::isReplyBodyTooLarge(ssize_t clen
) const
983 if (0 == maxReplyBodySize())
984 return 0; /* disabled */
987 return 0; /* unknown */
989 return clen
> maxReplyBodySize();
993 ClientHttpRequest::storeEntry(StoreEntry
*newEntry
)
999 ClientHttpRequest::loggingEntry(StoreEntry
*newEntry
)
1002 storeUnlockObject(loggingEntry_
);
1004 loggingEntry_
= newEntry
;
1007 storeLockObject(loggingEntry_
);
1011 * doCallouts() - This function controls the order of "callout"
1012 * executions, including non-blocking access control checks, the
1013 * redirector, and ICAP. Previously, these callouts were chained
1014 * together such that "clientAccessCheckDone()" would call
1015 * "clientRedirectStart()" and so on.
1017 * The ClientRequestContext (aka calloutContext) class holds certain
1018 * state data for the callout/callback operations. Previously
1019 * ClientHttpRequest would sort of hand off control to ClientRequestContext
1020 * for a short time. ClientRequestContext would then delete itself
1021 * and pass control back to ClientHttpRequest when all callouts
1024 * This caused some problems for ICAP because we want to make the
1025 * ICAP callout after checking ACLs, but before checking the no_cache
1026 * list. We can't stuff the ICAP state into the ClientRequestContext
1027 * class because we still need the ICAP state after ClientRequestContext
1030 * Note that ClientRequestContext is created before the first call
1033 * If one of the callouts notices that ClientHttpRequest is no
1034 * longer valid, it should call cbdataReferenceDone() so that
1035 * ClientHttpRequest's reference count goes to zero and it will get
1036 * deleted. ClientHttpRequest will then delete ClientRequestContext.
1038 * Note that we set the _done flags here before actually starting
1039 * the callout. This is strictly for convenience.
1043 ClientHttpRequest::doCallouts()
1045 assert(calloutContext
);
1047 if (!calloutContext
->http_access_done
) {
1048 calloutContext
->http_access_done
= true;
1049 calloutContext
->clientAccessCheck();
1054 if (TheICAPConfig
.onoff
&& !calloutContext
->icap_acl_check_done
) {
1055 calloutContext
->icap_acl_check_done
= true;
1056 calloutContext
->icapAccessCheck();
1062 if (!calloutContext
->redirect_done
) {
1063 calloutContext
->redirect_done
= true;
1064 assert(calloutContext
->redirect_state
== REDIRECT_NONE
);
1066 if (Config
.Program
.redirect
) {
1067 calloutContext
->redirect_state
= REDIRECT_PENDING
;
1068 calloutContext
->clientRedirectStart();
1073 if (!calloutContext
->interpreted_req_hdrs
) {
1074 calloutContext
->interpreted_req_hdrs
= 1;
1075 clientInterpretRequestHeaders(this);
1078 if (!calloutContext
->no_cache_done
) {
1079 calloutContext
->no_cache_done
= true;
1081 if (Config
.accessList
.noCache
&& request
->flags
.cachable
) {
1082 calloutContext
->checkNoCache();
1087 cbdataReferenceDone(calloutContext
->http
);
1088 delete calloutContext
;
1089 calloutContext
= NULL
;
1092 headersLog(0, 1, request
->method
, request
);
1098 #ifndef _USE_INLINE_
1099 #include "client_side_request.cci"
1104 * Initiate an ICAP transaction. Return 0 if all is well, or -1 upon error.
1105 * Caller will handle error condition by generating a Squid error message
1106 * or take other action.
1109 ClientHttpRequest::doIcap(ICAPServiceRep::Pointer service
)
1111 debug(85,3)("ClientHttpRequest::doIcap() called\n");
1112 assert(NULL
== icap
);
1113 icap
= new ICAPClientReqmodPrecache(service
);
1114 (void) cbdataReference(icap
);
1115 icap
->startReqMod(this, request
);
1116 icap
->doneSending();
1121 * Called by ICAPAnchor when it has space available for us.
1124 ClientHttpRequest::icapSpaceAvailable()
1126 debug(85,3)("ClientHttpRequest::icapSpaceAvailable() called\n");
1130 ClientHttpRequest::takeAdaptedHeaders(HttpMsg
*msg
)
1132 debug(85,3)("ClientHttpRequest::takeAdaptedHeaders() called\n");
1133 assert(cbdataReferenceValid(this)); // indicates bug
1135 if (HttpRequest
*new_req
= dynamic_cast<HttpRequest
*>(msg
)) {
1137 * Replace the old request with the new request. First,
1138 * Move the "body_connection" over, then unlink old and
1139 * link new to the http state.
1141 new_req
->body_connection
= request
->body_connection
;
1142 request
->body_connection
= NULL
;
1143 HTTPMSGUNLOCK(request
);
1144 request
= HTTPMSGLOCK(new_req
);
1146 * Store the new URI for logging
1149 uri
= xstrdup(urlCanonical(request
));
1150 setLogUri(this, urlCanonicalClean(request
));
1151 assert(request
->method
);
1152 } else if (HttpReply
*new_rep
= dynamic_cast<HttpReply
*>(msg
)) {
1153 debugs(85,3,HERE
<< "REQMOD reply is HTTP reply");
1155 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->prev
->data
;
1156 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1157 repContext
->createStoreEntry(request
->method
, request
->flags
);
1159 EBIT_CLR(storeEntry()->flags
, ENTRY_FWD_HDR_WAIT
);
1160 request_satisfaction_mode
= true;
1161 request_satisfaction_offset
= 0;
1162 storeEntry()->replaceHttpReply(new_rep
);
1163 clientGetMoreData(node
, this);
1166 if (!request_satisfaction_mode
)
1169 debug(85,3)("ClientHttpRequest::takeAdaptedHeaders() finished\n");
1173 ClientHttpRequest::takeAdaptedBody(MemBuf
*buf
)
1175 debug(85,3)("ClientHttpRequest::takeAdaptedBody() called\n");
1177 if (request_satisfaction_mode
) {
1178 storeEntry()->write(StoreIOBuffer(buf
, request_satisfaction_offset
));
1179 request_satisfaction_offset
+= buf
->contentSize();
1180 buf
->consume(buf
->contentSize()); // consume everything written
1185 ClientHttpRequest::doneAdapting()
1187 debug(85,3)("ClientHttpRequest::doneAdapting() called\n");
1191 ClientHttpRequest::abortAdapting()
1193 debug(85,3)("ClientHttpRequest::abortAdapting() called\n");
1195 if ((NULL
== storeEntry()) || storeEntry()->isEmpty()) {
1196 debug(85,3)("WARNING: ICAP REQMOD callout failed, proceeding with original request\n");
1200 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->prev
->data
;
1201 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1202 assert (repContext
);
1203 // Note if this code is ever used, clientBuildError() should be modified to
1204 // accept an errno arg
1205 repContext
->setReplyToError(ERR_ICAP_FAILURE
, HTTP_INTERNAL_SERVER_ERROR
,
1206 request
->method
, NULL
,
1207 getConn().getRaw() != NULL
? &getConn()->peer
.sin_addr
: &no_addr
, request
,
1208 NULL
, getConn().getRaw() != NULL
1209 && getConn()->auth_user_request
? getConn()->
1210 auth_user_request
: request
->auth_user_request
, errno
);
1211 node
= (clientStreamNode
*)client_stream
.tail
->data
;
1212 clientStreamRead(node
, this, node
->readBuffer
);
1218 debug(0,0)("write me at %s:%d\n", __FILE__
,__LINE__
);