3 * $Id: client_side_request.cc,v 1.77 2006/10/31 23:30:57 wessels 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
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 "AuthUserRequest.h"
49 #include "HttpRequest.h"
50 #include "ACLChecklist.h"
52 #include "client_side.h"
53 #include "client_side_reply.h"
55 #include "HttpReply.h"
56 #include "MemObject.h"
57 #include "ClientRequestContext.h"
58 #include "SquidTime.h"
62 #include "ICAP/ICAPClientReqmodPrecache.h"
63 #include "ICAP/ICAPElements.h"
64 #include "ICAP/ICAPConfig.h"
65 static void icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service
, void *data
);
66 extern ICAPConfig TheICAPConfig
;
70 #define comm_close comm_lingering_close
73 static const char *const crlf
= "\r\n";
75 CBDATA_CLASS_INIT(ClientRequestContext
);
78 ClientRequestContext::operator new (size_t size
)
80 assert (size
== sizeof(ClientRequestContext
));
81 CBDATA_INIT_TYPE(ClientRequestContext
);
82 ClientRequestContext
*result
= cbdataAlloc(ClientRequestContext
);
87 ClientRequestContext::operator delete (void *address
)
89 ClientRequestContext
*t
= static_cast<ClientRequestContext
*>(address
);
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
);
105 ClientRequestContext::~ClientRequestContext()
108 * Release our "lock" on our parent, ClientHttpRequest, if we
113 cbdataReferenceDone(http
);
116 if (acl_checklist
->asyncInProgress()) {
117 acl_checklist
->markDeleteWhenDone();
119 delete acl_checklist
;
123 debugs(85,3, HERE
<< this << " ClientHttpRequest destructed");
126 ClientRequestContext::ClientRequestContext(ClientHttpRequest
*anHttp
) : http(cbdataReference(anHttp
)), acl_checklist (NULL
), redirect_state (REDIRECT_NONE
)
128 http_access_done
= false;
129 redirect_done
= false;
130 no_cache_done
= false;
131 interpreted_req_hdrs
= false;
132 debugs(85,3, HERE
<< this << " ClientRequestContext constructed");
135 CBDATA_CLASS_INIT(ClientHttpRequest
);
138 ClientHttpRequest::operator new (size_t size
)
140 assert (size
== sizeof (ClientHttpRequest
));
141 CBDATA_INIT_TYPE(ClientHttpRequest
);
142 ClientHttpRequest
*result
= cbdataAlloc(ClientHttpRequest
);
147 ClientHttpRequest::operator delete (void *address
)
149 ClientHttpRequest
*t
= static_cast<ClientHttpRequest
*>(address
);
153 ClientHttpRequest::ClientHttpRequest(ConnStateData::Pointer aConn
) : loggingEntry_(NULL
)
155 start
= current_time
;
157 dlinkAdd(this, &active
, &ClientActiveRequests
);
160 request_satisfaction_mode
= false;
165 * returns true if client specified that the object must come from the cache
166 * without contacting origin server
169 ClientHttpRequest::onlyIfCached()const
172 return request
->cache_control
&&
173 EBIT_TEST(request
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
177 * This function is designed to serve a fairly specific purpose.
178 * Occasionally our vBNS-connected caches can talk to each other, but not
179 * the rest of the world. Here we try to detect frequent failures which
180 * make the cache unusable (e.g. DNS lookup and connect() failures). If
181 * the failure:success ratio goes above 1.0 then we go into "hit only"
182 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
183 * will only fetch HITs from us if they are using the ICP protocol. We
184 * stay in this mode for 5 minutes.
186 * Duane W., Sept 16, 1996
189 #define FAILURE_MODE_TIME 300
192 checkFailureRatio(err_type etype
, hier_code hcode
)
194 static double magic_factor
= 100.0;
198 if (hcode
== HIER_NONE
)
201 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
203 n_bad
= magic_factor
- n_good
;
209 case ERR_CONNECT_FAIL
:
219 request_failure_ratio
= n_bad
/ n_good
;
221 if (hit_only_mode_until
> squid_curtime
)
224 if (request_failure_ratio
< 1.0)
227 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
229 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
230 FAILURE_MODE_TIME
/ 60);
232 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
234 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
237 ClientHttpRequest::~ClientHttpRequest()
239 debug(33, 3) ("httpRequestFree: %s\n", uri
);
240 PROF_start(httpRequestFree
);
241 /* if body_connection !NULL, then ProcessBody has not
242 * found the end of the body yet
245 if (request
&& request
->body_reader
!= NULL
) {
246 request
->body_reader
= NULL
; // refcounted, triggers abort if needed.
247 debugs(32, 3, HERE
<< "setting body_reader = NULL for request " << request
);
250 /* the ICP check here was erroneous
251 * - storeReleaseRequest was always called if entry was valid
253 assert(logType
< LOG_TYPE_MAX
);
258 checkFailureRatio(request
->errType
, al
.hier
.code
);
267 delete calloutContext
;
269 /* moving to the next connection is handled by the context free */
270 dlinkDelete(&active
, &ClientActiveRequests
);
271 PROF_stop(httpRequestFree
);
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
= HttpRequest::CreateFromUrlAndMethod(http
->uri
, method
)) == 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 request
->header
.update(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
)
505 debugs(93,3,HERE
<< this << " icapAclCheckDone called");
507 * No matching ICAP service in the config file
510 if (service
== NULL
) {
516 * Setup ICAP state and such. If successful, just return.
517 * We'll get back to doCallouts() after REQMOD is done.
521 if (0 == http
->doIcap(service
))
525 * If doIcap() fails, then we have to either return an error
526 * to the user, or keep going without ICAP.
528 fatal("Fix this case in ClientRequestContext::icapAclCheckDone()");
529 // And when fixed, check whether the service is down in doIcap and
530 // if it is, abort early, without creating ICAPClientReqmodPrecache.
531 // See Server::startIcap() and its use.
539 clientRedirectAccessCheckDone(int answer
, void *data
)
541 ClientRequestContext
*context
= (ClientRequestContext
*)data
;
542 ClientHttpRequest
*http
= context
->http
;
543 context
->acl_checklist
= NULL
;
545 if (answer
== ACCESS_ALLOWED
)
546 redirectStart(http
, clientRedirectDoneWrapper
, context
);
548 context
->clientRedirectDone(NULL
);
552 ClientRequestContext::clientRedirectStart()
554 debug(33, 5) ("clientRedirectStart: '%s'\n", http
->uri
);
556 if (Config
.accessList
.redirector
) {
557 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.redirector
, http
);
558 acl_checklist
->nonBlockingCheck(clientRedirectAccessCheckDone
, this);
560 redirectStart(http
, clientRedirectDoneWrapper
, this);
564 clientHierarchical(ClientHttpRequest
* http
)
566 const char *url
= http
->uri
;
567 HttpRequest
*request
= http
->request
;
568 method_t method
= request
->method
;
569 const wordlist
*p
= NULL
;
572 * IMS needs a private key, so we can use the hierarchy for IMS only if our
573 * neighbors support private keys
576 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
580 * This is incorrect: authenticating requests can be sent via a hierarchy
581 * (they can even be cached if the correct headers are set on the reply)
583 if (request
->flags
.auth
)
586 if (method
== METHOD_TRACE
)
589 if (method
!= METHOD_GET
)
592 /* scan hierarchy_stoplist */
593 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
594 if (strstr(url
, p
->key
))
597 if (request
->flags
.loopdetect
)
600 if (request
->protocol
== PROTO_HTTP
)
601 return httpCachable(method
);
603 if (request
->protocol
== PROTO_GOPHER
)
604 return gopherCachable(request
);
606 if (request
->protocol
== PROTO_WAIS
)
609 if (request
->protocol
== PROTO_CACHEOBJ
)
617 clientInterpretRequestHeaders(ClientHttpRequest
* http
)
619 HttpRequest
*request
= http
->request
;
620 HttpHeader
*req_hdr
= &request
->header
;
622 #if !(ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
627 request
->imslen
= -1;
628 request
->ims
= req_hdr
->getTime(HDR_IF_MODIFIED_SINCE
);
630 if (request
->ims
> 0)
631 request
->flags
.ims
= 1;
635 * We ignore Cache-Control as per the Edge Architecture Section 3. See
636 * www.esi.org for more information.
640 if (req_hdr
->has(HDR_PRAGMA
)) {
641 String s
= req_hdr
->getList(HDR_PRAGMA
);
643 if (strListIsMember(&s
, "no-cache", ','))
649 if (request
->cache_control
)
650 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
654 * Work around for supporting the Reload button in IE browsers when Squid
655 * is used as an accelerator or transparent proxy, by turning accelerated
656 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
657 * actually only fixed in SP1, but we can't tell whether we are talking to
658 * SP1 or not so all 5.5 versions are treated 'normally').
660 if (Config
.onoff
.ie_refresh
) {
661 if (http
->flags
.accel
&& request
->flags
.ims
) {
662 if ((str
= req_hdr
->getStr(HDR_USER_AGENT
))) {
663 if (strstr(str
, "MSIE 5.01") != NULL
)
665 else if (strstr(str
, "MSIE 5.0") != NULL
)
667 else if (strstr(str
, "MSIE 4.") != NULL
)
669 else if (strstr(str
, "MSIE 3.") != NULL
)
679 if (Config
.onoff
.reload_into_ims
)
680 request
->flags
.nocache_hack
= 1;
681 else if (refresh_nocache_hack
)
682 request
->flags
.nocache_hack
= 1;
686 request
->flags
.nocache
= 1;
689 /* ignore range header in non-GETs or non-HEADs */
690 if (request
->method
== METHOD_GET
|| request
->method
== METHOD_HEAD
) {
691 request
->range
= req_hdr
->getRange();
693 if (request
->range
) {
694 request
->flags
.range
= 1;
695 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
696 /* XXX: This is suboptimal. We should give the stream the range set,
697 * and thereby let the top of the stream set the offset when the
698 * size becomes known. As it is, we will end up requesting from 0
699 * for evey -X range specification.
700 * RBC - this may be somewhat wrong. We should probably set the range
701 * iter up at this point.
703 node
->readBuffer
.offset
= request
->range
->lowestOffset(0);
704 http
->range_iter
.pos
= request
->range
->begin();
705 http
->range_iter
.valid
= true;
709 /* Only HEAD and GET requests permit a Range or Request-Range header.
710 * If these headers appear on any other type of request, delete them now.
713 req_hdr
->delById(HDR_RANGE
);
714 req_hdr
->delById(HDR_REQUEST_RANGE
);
715 request
->range
= NULL
;
718 if (req_hdr
->has(HDR_AUTHORIZATION
))
719 request
->flags
.auth
= 1;
721 if (request
->login
[0] != '\0')
722 request
->flags
.auth
= 1;
724 if (req_hdr
->has(HDR_VIA
)) {
725 String s
= req_hdr
->getList(HDR_VIA
);
727 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
728 * Note ThisCache2 has a space prepended to the hostname so we don't
729 * accidentally match super-domains.
732 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
733 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
734 request
, (ObjPackMethod
) & httpRequestPack
);
735 request
->flags
.loopdetect
= 1;
739 fvdbCountVia(s
.buf());
746 #if USE_USERAGENT_LOG
747 if ((str
= req_hdr
->getStr(HDR_USER_AGENT
)))
748 logUserAgent(fqdnFromAddr(http
->getConn().getRaw() ? http
->getConn()->log_addr
: no_addr
), str
);
753 if ((str
= req_hdr
->getStr(HDR_REFERER
)))
754 logReferer(fqdnFromAddr(http
->getConn().getRaw() ? http
->getConn()->log_addr
: no_addr
), str
, http
->log_uri
);
759 if (req_hdr
->has(HDR_X_FORWARDED_FOR
)) {
760 String s
= req_hdr
->getList(HDR_X_FORWARDED_FOR
);
761 fvdbCountForw(s
.buf());
766 if (request
->method
== METHOD_TRACE
) {
767 request
->max_forwards
= req_hdr
->getInt(HDR_MAX_FORWARDS
);
770 request
->flags
.cachable
= http
->request
->cacheable();
772 if (clientHierarchical(http
))
773 request
->flags
.hierarchical
= 1;
775 debug(85, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
776 request
->flags
.nocache
? "SET" : "NOT SET");
778 debug(85, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
779 request
->flags
.cachable
? "SET" : "NOT SET");
781 debug(85, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
782 request
->flags
.hierarchical
? "SET" : "NOT SET");
786 clientRedirectDoneWrapper(void *data
, char *result
)
788 ClientRequestContext
*calloutContext
= (ClientRequestContext
*)data
;
790 if (!calloutContext
->httpStateIsValid())
793 calloutContext
->clientRedirectDone(result
);
797 ClientRequestContext::clientRedirectDone(char *result
)
799 HttpRequest
*new_request
= NULL
;
800 HttpRequest
*old_request
= http
->request
;
801 debug(85, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
802 result
? result
: "NULL");
803 assert(redirect_state
== REDIRECT_PENDING
);
804 redirect_state
= REDIRECT_DONE
;
807 http_status status
= (http_status
) atoi(result
);
809 if (status
== HTTP_MOVED_PERMANENTLY
810 || status
== HTTP_MOVED_TEMPORARILY
811 || status
== HTTP_SEE_OTHER
812 || status
== HTTP_TEMPORARY_REDIRECT
) {
815 if ((t
= strchr(result
, ':')) != NULL
) {
816 http
->redirect
.status
= status
;
817 http
->redirect
.location
= xstrdup(t
+ 1);
819 debug(85, 1) ("clientRedirectDone: bad input: %s\n", result
);
821 } else if (strcmp(result
, http
->uri
))
822 new_request
= HttpRequest::CreateFromUrlAndMethod(result
, old_request
->method
);
826 safe_free(http
->uri
);
827 http
->uri
= xstrdup(urlCanonical(new_request
));
828 new_request
->http_ver
= old_request
->http_ver
;
829 new_request
->header
.append(&old_request
->header
);
830 new_request
->client_addr
= old_request
->client_addr
;
831 new_request
->client_port
= old_request
->client_port
;
832 new_request
->my_addr
= old_request
->my_addr
;
833 new_request
->my_port
= old_request
->my_port
;
834 new_request
->flags
= old_request
->flags
;
835 new_request
->flags
.redirected
= 1;
837 if (old_request
->auth_user_request
) {
838 new_request
->auth_user_request
= old_request
->auth_user_request
;
840 new_request
->auth_user_request
->lock()
845 if (old_request
->body_reader
!= NULL
) {
846 new_request
->body_reader
= old_request
->body_reader
;
847 old_request
->body_reader
= NULL
;
848 debugs(0,0,HERE
<< "setting body_reader = NULL for request " << old_request
);
851 new_request
->content_length
= old_request
->content_length
;
852 new_request
->extacl_user
= old_request
->extacl_user
;
853 new_request
->extacl_passwd
= old_request
->extacl_passwd
;
854 new_request
->flags
.proxy_keepalive
= old_request
->flags
.proxy_keepalive
;
855 HTTPMSGUNLOCK(old_request
);
856 http
->request
= HTTPMSGLOCK(new_request
);
859 /* FIXME PIPELINE: This is innacurate during pipelining */
861 if (http
->getConn().getRaw() != NULL
)
862 fd_note(http
->getConn()->fd
, http
->uri
);
870 ClientRequestContext::checkNoCache()
872 acl_checklist
= clientAclChecklistCreate(Config
.accessList
.noCache
, http
);
873 acl_checklist
->nonBlockingCheck(checkNoCacheDoneWrapper
, this);
877 checkNoCacheDoneWrapper(int answer
, void *data
)
879 ClientRequestContext
*calloutContext
= (ClientRequestContext
*) data
;
881 if (!calloutContext
->httpStateIsValid())
884 calloutContext
->checkNoCacheDone(answer
);
888 ClientRequestContext::checkNoCacheDone(int answer
)
890 acl_checklist
= NULL
;
891 http
->request
->flags
.cachable
= answer
;
896 * Identify requests that do not go through the store and client side stream
897 * and forward them to the appropriate location. All other requests, request
901 ClientHttpRequest::processRequest()
903 debug(85, 4) ("clientProcessRequest: %s '%s'\n",
904 RequestMethodStr
[request
->method
], uri
);
906 if (request
->method
== METHOD_CONNECT
&& !redirect
.status
) {
907 logType
= LOG_TCP_MISS
;
908 sslStart(this, &out
.size
, &al
.http
.code
);
916 ClientHttpRequest::httpStart()
918 PROF_start(httpStart
);
919 logType
= LOG_TAG_NONE
;
920 debug(85, 4) ("ClientHttpRequest::httpStart: %s for '%s'\n",
921 log_tags
[logType
], uri
);
922 /* no one should have touched this */
923 assert(out
.offset
== 0);
924 /* Use the Stream Luke */
925 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->data
;
926 clientStreamRead(node
, this, node
->readBuffer
);
927 PROF_stop(httpStart
);
931 ClientHttpRequest::gotEnough() const
933 /** TODO: should be querying the stream. */
935 memObject()->getReply()->bodySize(request
->method
);
936 assert(contentLength
>= 0);
938 if (out
.offset
< contentLength
)
945 ClientHttpRequest::maxReplyBodySize(ssize_t clen
)
947 maxReplyBodySize_
= clen
;
951 ClientHttpRequest::maxReplyBodySize() const
953 return maxReplyBodySize_
;
957 ClientHttpRequest::isReplyBodyTooLarge(ssize_t clen
) const
959 if (0 == maxReplyBodySize())
960 return 0; /* disabled */
963 return 0; /* unknown */
965 return clen
> maxReplyBodySize();
969 ClientHttpRequest::storeEntry(StoreEntry
*newEntry
)
975 ClientHttpRequest::loggingEntry(StoreEntry
*newEntry
)
978 loggingEntry_
->unlock();
980 loggingEntry_
= newEntry
;
983 loggingEntry_
->lock()
989 * doCallouts() - This function controls the order of "callout"
990 * executions, including non-blocking access control checks, the
991 * redirector, and ICAP. Previously, these callouts were chained
992 * together such that "clientAccessCheckDone()" would call
993 * "clientRedirectStart()" and so on.
995 * The ClientRequestContext (aka calloutContext) class holds certain
996 * state data for the callout/callback operations. Previously
997 * ClientHttpRequest would sort of hand off control to ClientRequestContext
998 * for a short time. ClientRequestContext would then delete itself
999 * and pass control back to ClientHttpRequest when all callouts
1002 * This caused some problems for ICAP because we want to make the
1003 * ICAP callout after checking ACLs, but before checking the no_cache
1004 * list. We can't stuff the ICAP state into the ClientRequestContext
1005 * class because we still need the ICAP state after ClientRequestContext
1008 * Note that ClientRequestContext is created before the first call
1011 * If one of the callouts notices that ClientHttpRequest is no
1012 * longer valid, it should call cbdataReferenceDone() so that
1013 * ClientHttpRequest's reference count goes to zero and it will get
1014 * deleted. ClientHttpRequest will then delete ClientRequestContext.
1016 * Note that we set the _done flags here before actually starting
1017 * the callout. This is strictly for convenience.
1021 ClientHttpRequest::doCallouts()
1023 assert(calloutContext
);
1025 if (!calloutContext
->http_access_done
) {
1026 calloutContext
->http_access_done
= true;
1027 calloutContext
->clientAccessCheck();
1032 if (TheICAPConfig
.onoff
&& !calloutContext
->icap_acl_check_done
) {
1033 calloutContext
->icap_acl_check_done
= true;
1034 calloutContext
->icapAccessCheck();
1040 if (!calloutContext
->redirect_done
) {
1041 calloutContext
->redirect_done
= true;
1042 assert(calloutContext
->redirect_state
== REDIRECT_NONE
);
1044 if (Config
.Program
.redirect
) {
1045 calloutContext
->redirect_state
= REDIRECT_PENDING
;
1046 calloutContext
->clientRedirectStart();
1051 if (!calloutContext
->interpreted_req_hdrs
) {
1052 calloutContext
->interpreted_req_hdrs
= 1;
1053 clientInterpretRequestHeaders(this);
1056 if (!calloutContext
->no_cache_done
) {
1057 calloutContext
->no_cache_done
= true;
1059 if (Config
.accessList
.noCache
&& request
->flags
.cachable
) {
1060 calloutContext
->checkNoCache();
1065 cbdataReferenceDone(calloutContext
->http
);
1066 delete calloutContext
;
1067 calloutContext
= NULL
;
1070 headersLog(0, 1, request
->method
, request
);
1076 #ifndef _USE_INLINE_
1077 #include "client_side_request.cci"
1082 * Initiate an ICAP transaction. Return 0 if all is well, or -1 upon error.
1083 * Caller will handle error condition by generating a Squid error message
1084 * or take other action.
1087 ClientHttpRequest::doIcap(ICAPServiceRep::Pointer service
)
1089 debugs(85, 3, HERE
<< this << " ClientHttpRequest::doIcap() called");
1090 assert(NULL
== icap
);
1091 icap
= new ICAPClientReqmodPrecache(service
);
1092 icap
->startReqMod(this, request
);
1094 if (request
->body_reader
== NULL
) {
1095 debugs(32, 3, HERE
<< "client request hasnt body...");
1096 icap
->doneSending();
1104 * icapSendRequestBodyWrapper
1106 * A callback wrapper for ::icapSendRequestBody()
1108 * icapSendRequestBodyWrapper is of type CBCB
1111 ClientHttpRequest::icapSendRequestBodyWrapper(MemBuf
&mb
, void *data
)
1113 ClientHttpRequest
*chr
= static_cast<ClientHttpRequest
*>(data
);
1114 chr
->icapSendRequestBody(mb
);
1119 * icapSendRequestBody
1121 * Sends some chunk of a request body to the ICAP side. Must make sure
1122 * that the ICAP-side can accept the data we have. If there is more
1123 * body data to read, then schedule another BodyReader callback.
1126 ClientHttpRequest::icapSendRequestBody(MemBuf
&mb
)
1128 ssize_t size_to_send
= mb
.contentSize();
1129 debugs(32,3,HERE
<< "have " << mb
.contentSize() << " bytes in mb");
1131 if (size_to_send
== 0) {
1133 * An error occurred during this transaction. Tell ICAP that we're done.
1137 icap
->doneSending();
1142 debugs(32,3,HERE
<< "icap->potentialSpaceSize() = " << icap
->potentialSpaceSize());
1144 if (size_to_send
> icap
->potentialSpaceSize())
1145 size_to_send
= icap
->potentialSpaceSize();
1148 debugs(32,3,HERE
<< "sending " << size_to_send
<< " body bytes to ICAP");
1149 StoreIOBuffer
sbuf(size_to_send
, 0, mb
.content());
1150 icap
->sendMoreData(sbuf
);
1151 icap
->body_reader
->consume(size_to_send
);
1152 icap
->body_reader
->bytes_read
+= size_to_send
;
1153 debugs(32,3," HTTP client body bytes_read=" << icap
->body_reader
->bytes_read
);
1155 debugs(32,2,HERE
<< "cannot send body data to ICAP");
1156 debugs(32,2,HERE
<< "\tBodyReader MemBuf has " << mb
.contentSize());
1157 debugs(32,2,HERE
<< "\tbut icap->potentialSpaceSize() is " << icap
->potentialSpaceSize());
1162 * If we sent some data this time, and there is more data to
1163 * read, then schedule another read request via BodyReader.
1165 if (size_to_send
&& icap
->body_reader
->remaining()) {
1166 debugs(32,3,HERE
<< "calling body_reader->read()");
1167 icap
->body_reader
->read(icapSendRequestBodyWrapper
, this);
1169 debugs(32,3,HERE
<< "No more request body bytes to send");
1170 icap
->doneSending();
1175 * Called by ICAPAnchor when it has space available for us.
1178 ClientHttpRequest::icapSpaceAvailable()
1180 debugs(85,3,HERE
<< this << " ClientHttpRequest::icapSpaceAvailable() called\n");
1182 if (request
->body_reader
!= NULL
&& icap
->body_reader
== NULL
) {
1183 debugs(32,3,HERE
<< "reassigning HttpRequest->body_reader to ICAP");
1185 * ICAP hooks on to the BodyReader that gets data from
1186 * ConnStateData. We'll make a new BodyReader that
1187 * HttpStateData can use if the adapted response has a
1188 * request body. See ICAPClientReqmodPrecache::noteSourceStart()
1190 icap
->body_reader
= request
->body_reader
;
1191 request
->body_reader
= NULL
;
1194 if (icap
->body_reader
== NULL
)
1197 if (icap
->body_reader
->callbackPending())
1200 debugs(32,3,HERE
<< "Calling read() for body data");
1202 icap
->body_reader
->read(icapSendRequestBodyWrapper
, this);
1206 ClientHttpRequest::takeAdaptedHeaders(HttpMsg
*msg
)
1208 debug(85,3)("ClientHttpRequest::takeAdaptedHeaders() called\n");
1209 assert(cbdataReferenceValid(this)); // indicates bug
1211 if (HttpRequest
*new_req
= dynamic_cast<HttpRequest
*>(msg
)) {
1213 * Replace the old request with the new request. First,
1214 * Move the "body_connection" over, then unlink old and
1215 * link new to the http state.
1217 HTTPMSGUNLOCK(request
);
1218 request
= HTTPMSGLOCK(new_req
);
1220 * Store the new URI for logging
1223 uri
= xstrdup(urlCanonical(request
));
1224 setLogUri(this, urlCanonicalClean(request
));
1225 assert(request
->method
);
1226 } else if (HttpReply
*new_rep
= dynamic_cast<HttpReply
*>(msg
)) {
1227 debugs(85,3,HERE
<< "REQMOD reply is HTTP reply");
1229 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->prev
->data
;
1230 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1231 repContext
->createStoreEntry(request
->method
, request
->flags
);
1233 EBIT_CLR(storeEntry()->flags
, ENTRY_FWD_HDR_WAIT
);
1234 request_satisfaction_mode
= true;
1235 request_satisfaction_offset
= 0;
1236 storeEntry()->replaceHttpReply(new_rep
);
1237 clientGetMoreData(node
, this);
1240 if (!request_satisfaction_mode
)
1243 debug(85,3)("ClientHttpRequest::takeAdaptedHeaders() finished\n");
1247 ClientHttpRequest::takeAdaptedBody(MemBuf
*buf
)
1249 debug(85,3)("ClientHttpRequest::takeAdaptedBody() called\n");
1251 if (request_satisfaction_mode
) {
1252 storeEntry()->write(StoreIOBuffer(buf
, request_satisfaction_offset
));
1253 request_satisfaction_offset
+= buf
->contentSize();
1254 buf
->consume(buf
->contentSize()); // consume everything written
1256 debug(85,0)("Unexpected call to takeAdaptedBody when "
1257 "not in request_satisfaction_mode");
1262 ClientHttpRequest::doneAdapting()
1264 debug(85,3)("ClientHttpRequest::doneAdapting() called\n");
1268 ClientHttpRequest::abortAdapting()
1270 debug(85,3)("ClientHttpRequest::abortAdapting() called\n");
1272 if ((NULL
== storeEntry()) || storeEntry()->isEmpty()) {
1273 debug(85,3)("WARNING: ICAP REQMOD callout failed, proceeding with original request\n");
1280 clientStreamNode
*node
= (clientStreamNode
*)client_stream
.tail
->prev
->data
;
1282 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
1284 assert (repContext
);
1286 // Note if this code is ever used, clientBuildError() should be modified to
1287 // accept an errno arg
1288 repContext
->setReplyToError(ERR_ICAP_FAILURE
, HTTP_INTERNAL_SERVER_ERROR
,
1289 request
->method
, NULL
,
1290 getConn().getRaw() != NULL
? &getConn()->peer
.sin_addr
: &no_addr
, request
,
1291 NULL
, getConn().getRaw() != NULL
1292 && getConn()->auth_user_request
? getConn()->
1293 auth_user_request
: request
->auth_user_request
, errno
);
1295 node
= (clientStreamNode
*)client_stream
.tail
->data
;
1297 clientStreamRead(node
, this, node
->readBuffer
);
1304 debug(0,0)("write me at %s:%d\n", __FILE__
,__LINE__
);