]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_side_request.cc
change automake --force to -f - different automakes use different long versions
[thirdparty/squid.git] / src / client_side_request.cc
CommitLineData
edce4d98 1
2/*
b9ae18aa 3 * $Id: client_side_request.cc,v 1.42 2004/12/20 16:30:35 robertc Exp $
69660be0 4 *
ae45c4de 5 * DEBUG: section 85 Client-side Request Routines
6 * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
69660be0 7 *
edce4d98 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
69660be0 10 *
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.
18 *
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)
22 * any later version.
23 *
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
27 * details.
28 *
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.
32 *
edce4d98 33 */
34
35
69660be0 36/*
37 * General logic of request processing:
38 *
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.
edce4d98 42 */
43
44#include "squid.h"
c8be6d7b 45#include "clientStream.h"
46#include "client_side_request.h"
f5691f9c 47#include "AuthUserRequest.h"
528b2c61 48#include "HttpRequest.h"
8000a965 49#include "ACLChecklist.h"
50#include "ACL.h"
a46d2c0e 51#include "client_side.h"
0655fa4d 52#include "client_side_reply.h"
53#include "Store.h"
54#include "HttpReply.h"
86a2f789 55#include "MemObject.h"
edce4d98 56
57#if LINGERING_CLOSE
58#define comm_close comm_lingering_close
59#endif
60
61static const char *const crlf = "\r\n";
62
0655fa4d 63class ClientRequestContext : public RefCountable
62e76326 64{
65
66public:
8e2745f4 67 void *operator new(size_t);
68 void operator delete(void *);
8e2745f4 69
70 ClientRequestContext();
71 ClientRequestContext(ClientHttpRequest *);
72 ~ClientRequestContext();
62e76326 73
8e2745f4 74 void checkNoCache();
75
4fb35c3c 76 ACLChecklist *acl_checklist; /* need ptr back so we can unreg if needed */
edce4d98 77 int redirect_state;
78 clientHttpRequest *http;
62e76326 79
80private:
8e2745f4 81 CBDATA_CLASS(ClientRequestContext);
82 static void CheckNoCacheDone(int answer, void *data);
83 void checkNoCacheDone(int answer);
84};
edce4d98 85
8e2745f4 86CBDATA_CLASS_INIT(ClientRequestContext);
87
88void *
89ClientRequestContext::operator new (size_t size)
90{
91 assert (size == sizeof(ClientRequestContext));
92 CBDATA_INIT_TYPE(ClientRequestContext);
93 ClientRequestContext *result = cbdataAlloc(ClientRequestContext);
94 /* Mark result as being owned - we want the refcounter to do the delete
95 * call */
7e6b941f 96 return cbdataReference(result);
8e2745f4 97}
62e76326 98
8e2745f4 99void
100ClientRequestContext::operator delete (void *address)
101{
102 ClientRequestContext *t = static_cast<ClientRequestContext *>(address);
103 cbdataFree(address);
104 /* And allow the memory to be freed */
105 cbdataReferenceDone (t);
106}
107
edce4d98 108/* Local functions */
edce4d98 109/* other */
edce4d98 110static void clientAccessCheckDone(int, void *);
edce4d98 111static int clientCachable(clientHttpRequest * http);
112static int clientHierarchical(clientHttpRequest * http);
113static void clientInterpretRequestHeaders(clientHttpRequest * http);
14cc8559 114static void clientRedirectStart(ClientRequestContext *context);
edce4d98 115static RH clientRedirectDone;
e6ccf245 116extern "C" CSR clientGetMoreData;
117extern "C" CSS clientReplyStatus;
118extern "C" CSD clientReplyDetach;
528b2c61 119static void checkFailureRatio(err_type, hier_code);
edce4d98 120
8e2745f4 121ClientRequestContext::~ClientRequestContext()
122{
123 if (http)
62e76326 124 cbdataReferenceDone(http);
125
8e2745f4 126 if (acl_checklist)
00d77d6b 127 delete acl_checklist;
8e2745f4 128}
129
130ClientRequestContext::ClientRequestContext() : acl_checklist (NULL), redirect_state (REDIRECT_NONE), http(NULL)
62e76326 131{}
edce4d98 132
8e2745f4 133ClientRequestContext::ClientRequestContext(ClientHttpRequest *newHttp) : acl_checklist (NULL), redirect_state (REDIRECT_NONE), http(cbdataReference(newHttp))
edce4d98 134{
8e2745f4 135 assert (newHttp != NULL);
edce4d98 136}
137
528b2c61 138CBDATA_CLASS_INIT(ClientHttpRequest);
8e2745f4 139
528b2c61 140void *
141ClientHttpRequest::operator new (size_t size)
142{
143 assert (size == sizeof (ClientHttpRequest));
144 CBDATA_INIT_TYPE(ClientHttpRequest);
145 ClientHttpRequest *result = cbdataAlloc(ClientHttpRequest);
146 /* Mark result as being owned - we want the refcounter to do the delete
147 * call */
7e6b941f 148 return cbdataReference(result);
528b2c61 149}
150
62e76326 151void
528b2c61 152ClientHttpRequest::operator delete (void *address)
153{
154 ClientHttpRequest *temp = static_cast<ClientHttpRequest *>(address);
155 cbdataFree(address);
156 /* And allow the memory to be freed */
157 cbdataReferenceDone (temp);
158}
159
0976f8db 160ClientHttpRequest::ClientHttpRequest() : loggingEntry_(NULL)
528b2c61 161{
162 /* reset range iterator */
163 start = current_time;
164}
165
0655fa4d 166/*
167 * returns true if client specified that the object must come from the cache
168 * without contacting origin server
169 */
170bool
171ClientHttpRequest::onlyIfCached()const
172{
173 assert(request);
174 return request->cache_control &&
175 EBIT_TEST(request->cache_control->mask, CC_ONLY_IF_CACHED);
176}
177
528b2c61 178/*
179 * This function is designed to serve a fairly specific purpose.
180 * Occasionally our vBNS-connected caches can talk to each other, but not
181 * the rest of the world. Here we try to detect frequent failures which
182 * make the cache unusable (e.g. DNS lookup and connect() failures). If
183 * the failure:success ratio goes above 1.0 then we go into "hit only"
184 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
185 * will only fetch HITs from us if they are using the ICP protocol. We
186 * stay in this mode for 5 minutes.
187 *
188 * Duane W., Sept 16, 1996
189 */
190
191#define FAILURE_MODE_TIME 300
192
193static void
194checkFailureRatio(err_type etype, hier_code hcode)
195{
196 static double magic_factor = 100.0;
197 double n_good;
198 double n_bad;
62e76326 199
528b2c61 200 if (hcode == HIER_NONE)
62e76326 201 return;
202
528b2c61 203 n_good = magic_factor / (1.0 + request_failure_ratio);
62e76326 204
528b2c61 205 n_bad = magic_factor - n_good;
62e76326 206
528b2c61 207 switch (etype) {
62e76326 208
528b2c61 209 case ERR_DNS_FAIL:
62e76326 210
528b2c61 211 case ERR_CONNECT_FAIL:
62e76326 212
528b2c61 213 case ERR_READ_ERROR:
62e76326 214 n_bad++;
215 break;
216
528b2c61 217 default:
62e76326 218 n_good++;
528b2c61 219 }
62e76326 220
528b2c61 221 request_failure_ratio = n_bad / n_good;
62e76326 222
528b2c61 223 if (hit_only_mode_until > squid_curtime)
62e76326 224 return;
225
528b2c61 226 if (request_failure_ratio < 1.0)
62e76326 227 return;
228
528b2c61 229 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio);
62e76326 230
528b2c61 231 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
62e76326 232 FAILURE_MODE_TIME / 60);
233
528b2c61 234 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
62e76326 235
528b2c61 236 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
237}
238
239ClientHttpRequest::~ClientHttpRequest()
240{
241 debug(33, 3) ("httpRequestFree: %s\n", uri);
242 /* if body_connection !NULL, then ProcessBody has not
243 * found the end of the body yet
244 */
62e76326 245
a2ac85d9 246 if (request && request->body_connection.getRaw() != NULL)
62e76326 247 clientAbortBody(request); /* abort body transter */
248
528b2c61 249 /* the ICP check here was erroneous
250 * - storeReleaseRequest was always called if entry was valid
251 */
252 assert(logType < LOG_TYPE_MAX);
62e76326 253
528b2c61 254 logRequest();
62e76326 255
0976f8db 256 loggingEntry(NULL);
257
528b2c61 258 if (request)
62e76326 259 checkFailureRatio(request->errType, al.hier.code);
260
528b2c61 261 freeResources();
62e76326 262
528b2c61 263 /* moving to the next connection is handled by the context free */
264 dlinkDelete(&active, &ClientActiveRequests);
265}
62e76326 266
edce4d98 267/* Create a request and kick it off */
69660be0 268/*
269 * TODO: Pass in the buffers to be used in the inital Read request, as they are
270 * determined by the user
edce4d98 271 */
272int /* returns nonzero on failure */
273clientBeginRequest(method_t method, char const *url, CSCB * streamcallback,
0655fa4d 274 CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header,
62e76326 275 char *tailbuf, size_t taillen)
edce4d98 276{
277 size_t url_sz;
450e0c10 278 HttpVersion http_ver (1, 0);
528b2c61 279 clientHttpRequest *http = new ClientHttpRequest;
190154cf 280 HttpRequest *request;
528b2c61 281 StoreIOBuffer tempBuffer;
edce4d98 282 http->http_ver = http_ver;
98242069 283 http->setConn(NULL);
edce4d98 284 http->start = current_time;
285 /* this is only used to adjust the connection offset in client_side.c */
286 http->req_sz = 0;
c8be6d7b 287 tempBuffer.length = taillen;
288 tempBuffer.data = tailbuf;
edce4d98 289 /* client stream setup */
290 clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach,
0655fa4d 291 clientReplyStatus, new clientReplyContext(http), streamcallback,
62e76326 292 streamdetach, streamdata, tempBuffer);
edce4d98 293 /* make it visible in the 'current acctive requests list' */
294 dlinkAdd(http, &http->active, &ClientActiveRequests);
295 /* Set flags */
a46d2c0e 296 /* internal requests only makes sense in an
297 * accelerator today. TODO: accept flags ? */
298 http->flags.accel = 1;
edce4d98 299 /* allow size for url rewriting */
300 url_sz = strlen(url) + Config.appendDomainLen + 5;
e6ccf245 301 http->uri = (char *)xcalloc(url_sz, 1);
edce4d98 302 strcpy(http->uri, url);
303
304 if ((request = urlParse(method, http->uri)) == NULL) {
62e76326 305 debug(85, 5) ("Invalid URL: %s\n", http->uri);
306 return -1;
edce4d98 307 }
62e76326 308
69660be0 309 /*
310 * now update the headers in request with our supplied headers. urLParse
311 * should return a blank header set, but we use Update to be sure of
312 * correctness.
edce4d98 313 */
314 if (header)
62e76326 315 httpHeaderUpdate(&request->header, header, NULL);
316
edce4d98 317 http->log_uri = xstrdup(urlCanonicalClean(request));
62e76326 318
edce4d98 319 /* http struct now ready */
320
69660be0 321 /*
322 * build new header list *? TODO
edce4d98 323 */
324 request->flags.accelerated = http->flags.accel;
62e76326 325
a46d2c0e 326 request->flags.internalclient = 1;
327
328 /* this is an internally created
329 * request, not subject to acceleration
330 * target overrides */
69660be0 331 /*
332 * FIXME? Do we want to detect and handle internal requests of internal
333 * objects ?
334 */
edce4d98 335
336 /* Internally created requests cannot have bodies today */
337 request->content_length = 0;
62e76326 338
edce4d98 339 request->client_addr = no_addr;
62e76326 340
edce4d98 341 request->my_addr = no_addr; /* undefined for internal requests */
62e76326 342
edce4d98 343 request->my_port = 0;
62e76326 344
edce4d98 345 request->http_ver = http_ver;
62e76326 346
edce4d98 347 http->request = requestLink(request);
348
349 /* optional - skip the access check ? */
350 clientAccessCheck(http);
62e76326 351
edce4d98 352 return 0;
353}
354
edce4d98 355/* This is the entry point for external users of the client_side routines */
356void
8e2745f4 357clientAccessCheck(ClientHttpRequest *http)
edce4d98 358{
8e2745f4 359 ClientRequestContext *context = new ClientRequestContext(http);
edce4d98 360 context->acl_checklist =
62e76326 361 clientAclChecklistCreate(Config.accessList.http, http);
225b7b10 362 context->acl_checklist->nonBlockingCheck(clientAccessCheckDone, context);
edce4d98 363}
364
365void
366clientAccessCheckDone(int answer, void *data)
367{
8e2745f4 368 ClientRequestContext *context = (ClientRequestContext *)data;
62e76326 369
7d31d5fa 370 context->acl_checklist = NULL;
fbade053 371 clientHttpRequest *http_ = context->http;
372
373 if (!cbdataReferenceValid (http_)) {
00d77d6b 374 delete context;
62e76326 375 return;
fbade053 376 }
62e76326 377
edce4d98 378 clientHttpRequest *http = context->http;
379 err_type page_id;
380 http_status status;
edce4d98 381 debug(85, 2) ("The request %s %s is %s, because it matched '%s'\n",
62e76326 382 RequestMethodStr[http->request->method], http->uri,
383 answer == ACCESS_ALLOWED ? "ALLOWED" : "DENIED",
384 AclMatchedName ? AclMatchedName : "NO ACL's");
f5691f9c 385 char const *proxy_auth_msg = "<null>";
386
387 if (http->getConn().getRaw() != NULL && http->getConn()->auth_user_request != NULL)
388 proxy_auth_msg = http->getConn()->auth_user_request->denyMessage("<null>");
389 else if (http->request->auth_user_request != NULL)
390 proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>");
62e76326 391
edce4d98 392 if (answer == ACCESS_ALLOWED) {
62e76326 393 safe_free(http->uri);
394 http->uri = xstrdup(urlCanonical(http->request));
395 assert(context->redirect_state == REDIRECT_NONE);
396 context->redirect_state = REDIRECT_PENDING;
14cc8559 397 clientRedirectStart(context);
edce4d98 398 } else {
62e76326 399 /* Send an error */
400 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->prev->data;
00d77d6b 401 delete context;
62e76326 402 debug(85, 5) ("Access Denied: %s\n", http->uri);
403 debug(85, 5) ("AclMatchedName = %s\n",
404 AclMatchedName ? AclMatchedName : "<null>");
405 debug(85, 5) ("Proxy Auth Message = %s\n",
406 proxy_auth_msg ? proxy_auth_msg : "<null>");
407 /*
408 * NOTE: get page_id here, based on AclMatchedName because if
409 * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in
410 * the clientCreateStoreEntry() call just below. Pedro Ribeiro
411 * <pribeiro@isel.pt>
412 */
413 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName);
414 http->logType = LOG_TCP_DENIED;
415
416 if (answer == ACCESS_REQ_PROXY_AUTH || aclIsProxyAuth(AclMatchedName)) {
417 if (!http->flags.accel) {
418 /* Proxy authorisation needed */
419 status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
420 } else {
421 /* WWW authorisation needed */
422 status = HTTP_UNAUTHORIZED;
423 }
424
425 if (page_id == ERR_NONE)
426 page_id = ERR_CACHE_ACCESS_DENIED;
427 } else {
428 status = HTTP_FORBIDDEN;
429
430 if (page_id == ERR_NONE)
431 page_id = ERR_ACCESS_DENIED;
432 }
433
0655fa4d 434 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
435 assert (repContext);
436 repContext->setReplyToError(page_id, status,
437 http->request->method, NULL,
a2ac85d9 438 http->getConn().getRaw() != NULL ? &http->getConn()->peer.sin_addr : &no_addr, http->request,
439 NULL, http->getConn().getRaw() != NULL
98242069 440 && http->getConn()->auth_user_request ? http->getConn()->
0655fa4d 441 auth_user_request : http->request->auth_user_request);
62e76326 442 node = (clientStreamNode *)http->client_stream.tail->data;
443 clientStreamRead(node, http, node->readBuffer);
edce4d98 444 }
445}
446
14cc8559 447static void
448clientRedirectAccessCheckDone(int answer, void *data)
449{
450 ClientRequestContext *context = (ClientRequestContext *)data;
451 clientHttpRequest *http = context->http;
452 context->acl_checklist = NULL;
453
454 if (answer == ACCESS_ALLOWED)
455 redirectStart(http, clientRedirectDone, context);
456 else
457 clientRedirectDone(context, NULL);
458}
459
460static void
461clientRedirectStart(ClientRequestContext *context)
462{
463 clientHttpRequest *http = context->http;
464 debug(33, 5) ("clientRedirectStart: '%s'\n", http->uri);
465
466 if (Config.Program.redirect == NULL) {
467 clientRedirectDone(context, NULL);
468 return;
469 }
470
471 if (Config.accessList.redirector) {
472 context->acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http);
473 context->acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, context);
474 } else
475 redirectStart(http, clientRedirectDone, context);
476}
477
edce4d98 478static int
479clientCachable(clientHttpRequest * http)
480{
190154cf 481 HttpRequest *req = http->request;
edce4d98 482 method_t method = req->method;
62e76326 483
edce4d98 484 if (req->protocol == PROTO_HTTP)
62e76326 485 return httpCachable(method);
486
edce4d98 487 /* FTP is always cachable */
488 if (req->protocol == PROTO_WAIS)
62e76326 489 return 0;
490
69660be0 491 /*
492 * The below looks questionable: what non HTTP protocols use connect,
493 * trace, put and post? RC
edce4d98 494 */
495 if (method == METHOD_CONNECT)
62e76326 496 return 0;
497
edce4d98 498 if (method == METHOD_TRACE)
62e76326 499 return 0;
500
edce4d98 501 if (method == METHOD_PUT)
62e76326 502 return 0;
503
edce4d98 504 if (method == METHOD_POST)
a46d2c0e 505 return 0;
506
507 /* XXX POST may be cached sometimes.. ignored
508
509 * for now */
edce4d98 510 if (req->protocol == PROTO_GOPHER)
62e76326 511 return gopherCachable(req);
512
edce4d98 513 if (req->protocol == PROTO_CACHEOBJ)
62e76326 514 return 0;
515
edce4d98 516 return 1;
517}
518
519static int
520clientHierarchical(clientHttpRequest * http)
521{
522 const char *url = http->uri;
190154cf 523 HttpRequest *request = http->request;
edce4d98 524 method_t method = request->method;
525 const wordlist *p = NULL;
526
69660be0 527 /*
528 * IMS needs a private key, so we can use the hierarchy for IMS only if our
529 * neighbors support private keys
530 */
62e76326 531
edce4d98 532 if (request->flags.ims && !neighbors_do_private_keys)
62e76326 533 return 0;
534
69660be0 535 /*
536 * This is incorrect: authenticating requests can be sent via a hierarchy
537 * (they can even be cached if the correct headers are set on the reply
edce4d98 538 */
539 if (request->flags.auth)
62e76326 540 return 0;
541
edce4d98 542 if (method == METHOD_TRACE)
62e76326 543 return 1;
544
edce4d98 545 if (method != METHOD_GET)
62e76326 546 return 0;
547
edce4d98 548 /* scan hierarchy_stoplist */
549 for (p = Config.hierarchy_stoplist; p; p = p->next)
62e76326 550 if (strstr(url, p->key))
551 return 0;
552
edce4d98 553 if (request->flags.loopdetect)
62e76326 554 return 0;
555
edce4d98 556 if (request->protocol == PROTO_HTTP)
62e76326 557 return httpCachable(method);
558
edce4d98 559 if (request->protocol == PROTO_GOPHER)
62e76326 560 return gopherCachable(request);
561
edce4d98 562 if (request->protocol == PROTO_WAIS)
62e76326 563 return 0;
564
edce4d98 565 if (request->protocol == PROTO_CACHEOBJ)
62e76326 566 return 0;
567
edce4d98 568 return 1;
569}
570
571
572static void
573clientInterpretRequestHeaders(clientHttpRequest * http)
574{
190154cf 575 HttpRequest *request = http->request;
edce4d98 576 const HttpHeader *req_hdr = &request->header;
577 int no_cache = 0;
a787b56a 578#if !(ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
62e76326 579
edce4d98 580 const char *str;
581#endif
62e76326 582
edce4d98 583 request->imslen = -1;
584 request->ims = httpHeaderGetTime(req_hdr, HDR_IF_MODIFIED_SINCE);
62e76326 585
edce4d98 586 if (request->ims > 0)
62e76326 587 request->flags.ims = 1;
588
edce4d98 589#if ESI
69660be0 590 /*
591 * We ignore Cache-Control as per the Edge Architecture Section 3. See
592 * www.esi.org for more information.
edce4d98 593 */
594#else
62e76326 595
edce4d98 596 if (httpHeaderHas(req_hdr, HDR_PRAGMA)) {
62e76326 597 String s = httpHeaderGetList(req_hdr, HDR_PRAGMA);
598
599 if (strListIsMember(&s, "no-cache", ','))
600 no_cache++;
601
602 s.clean();
edce4d98 603 }
62e76326 604
edce4d98 605 request->cache_control = httpHeaderGetCc(req_hdr);
62e76326 606
edce4d98 607 if (request->cache_control)
62e76326 608 if (EBIT_TEST(request->cache_control->mask, CC_NO_CACHE))
609 no_cache++;
610
69660be0 611 /*
62e76326 612 * Work around for supporting the Reload button in IE browsers when Squid
613 * is used as an accelerator or transparent proxy, by turning accelerated
614 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
615 * actually only fixed in SP1, but we can't tell whether we are talking to
616 * SP1 or not so all 5.5 versions are treated 'normally').
617 */
edce4d98 618 if (Config.onoff.ie_refresh) {
62e76326 619 if (http->flags.accel && request->flags.ims) {
620 if ((str = httpHeaderGetStr(req_hdr, HDR_USER_AGENT))) {
621 if (strstr(str, "MSIE 5.01") != NULL)
622 no_cache++;
623 else if (strstr(str, "MSIE 5.0") != NULL)
624 no_cache++;
625 else if (strstr(str, "MSIE 4.") != NULL)
626 no_cache++;
627 else if (strstr(str, "MSIE 3.") != NULL)
628 no_cache++;
629 }
630 }
edce4d98 631 }
62e76326 632
edce4d98 633#endif
634 if (no_cache) {
635#if HTTP_VIOLATIONS
62e76326 636
637 if (Config.onoff.reload_into_ims)
638 request->flags.nocache_hack = 1;
639 else if (refresh_nocache_hack)
640 request->flags.nocache_hack = 1;
641 else
edce4d98 642#endif
62e76326 643
644 request->flags.nocache = 1;
edce4d98 645 }
62e76326 646
edce4d98 647 /* ignore range header in non-GETs */
648 if (request->method == METHOD_GET) {
62e76326 649 request->range = httpHeaderGetRange(req_hdr);
650
651 if (request->range) {
652 request->flags.range = 1;
653 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data;
654 /* XXX: This is suboptimal. We should give the stream the range set,
655 * and thereby let the top of the stream set the offset when the
656 * size becomes known. As it is, we will end up requesting from 0
657 * for evey -X range specification.
658 * RBC - this may be somewhat wrong. We should probably set the range
659 * iter up at this point.
660 */
661 node->readBuffer.offset = request->range->lowestOffset(0);
662 http->range_iter.pos = request->range->begin();
663 http->range_iter.valid = true;
664 }
edce4d98 665 }
62e76326 666
edce4d98 667 if (httpHeaderHas(req_hdr, HDR_AUTHORIZATION))
62e76326 668 request->flags.auth = 1;
669
edce4d98 670 if (request->login[0] != '\0')
62e76326 671 request->flags.auth = 1;
672
edce4d98 673 if (httpHeaderHas(req_hdr, HDR_VIA)) {
62e76326 674 String s = httpHeaderGetList(req_hdr, HDR_VIA);
675 /*
676 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
677 * Note ThisCache2 has a space prepended to the hostname so we don't
678 * accidentally match super-domains.
679 */
680
681 if (strListIsSubstr(&s, ThisCache2, ',')) {
682 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
683 request, (ObjPackMethod) & httpRequestPack);
684 request->flags.loopdetect = 1;
685 }
686
edce4d98 687#if FORW_VIA_DB
62e76326 688 fvdbCountVia(s.buf());
689
edce4d98 690#endif
62e76326 691
692 s.clean();
edce4d98 693 }
62e76326 694
edce4d98 695#if USE_USERAGENT_LOG
696 if ((str = httpHeaderGetStr(req_hdr, HDR_USER_AGENT)))
7928e475 697 logUserAgent(fqdnFromAddr(http->getConn().getRaw() ? http->getConn()->log_addr : no_addr), str);
62e76326 698
edce4d98 699#endif
700#if USE_REFERER_LOG
62e76326 701
edce4d98 702 if ((str = httpHeaderGetStr(req_hdr, HDR_REFERER)))
7928e475 703 logReferer(fqdnFromAddr(http->getConn().getRaw() ? http->getConn()->log_addr : no_addr), str, http->log_uri);
62e76326 704
edce4d98 705#endif
706#if FORW_VIA_DB
62e76326 707
edce4d98 708 if (httpHeaderHas(req_hdr, HDR_X_FORWARDED_FOR)) {
62e76326 709 String s = httpHeaderGetList(req_hdr, HDR_X_FORWARDED_FOR);
710 fvdbCountForw(s.buf());
711 s.clean();
edce4d98 712 }
62e76326 713
edce4d98 714#endif
715 if (request->method == METHOD_TRACE) {
62e76326 716 request->max_forwards = httpHeaderGetInt(req_hdr, HDR_MAX_FORWARDS);
edce4d98 717 }
62e76326 718
edce4d98 719 if (clientCachable(http))
62e76326 720 request->flags.cachable = 1;
721
edce4d98 722 if (clientHierarchical(http))
62e76326 723 request->flags.hierarchical = 1;
724
edce4d98 725 debug(85, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
62e76326 726 request->flags.nocache ? "SET" : "NOT SET");
727
edce4d98 728 debug(85, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
62e76326 729 request->flags.cachable ? "SET" : "NOT SET");
730
edce4d98 731 debug(85, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
62e76326 732 request->flags.hierarchical ? "SET" : "NOT SET");
edce4d98 733}
734
735void
736clientRedirectDone(void *data, char *result)
737{
8e2745f4 738 ClientRequestContext *context = (ClientRequestContext *)data;
db02222f 739 clientHttpRequest *http_ = context->http;
740
741 if (!cbdataReferenceValid (http_)) {
00d77d6b 742 delete context;
62e76326 743 return;
db02222f 744 }
62e76326 745
edce4d98 746 clientHttpRequest *http = context->http;
190154cf 747 HttpRequest *new_request = NULL;
748 HttpRequest *old_request = http->request;
edce4d98 749 debug(85, 5) ("clientRedirectDone: '%s' result=%s\n", http->uri,
62e76326 750 result ? result : "NULL");
edce4d98 751 assert(context->redirect_state == REDIRECT_PENDING);
752 context->redirect_state = REDIRECT_DONE;
62e76326 753
edce4d98 754 if (result) {
62e76326 755 http_status status = (http_status) atoi(result);
756
757 if (status == HTTP_MOVED_PERMANENTLY
758 || status == HTTP_MOVED_TEMPORARILY
759 || status == HTTP_SEE_OTHER
760 || status == HTTP_TEMPORARY_REDIRECT) {
761 char *t = result;
762
763 if ((t = strchr(result, ':')) != NULL) {
764 http->redirect.status = status;
765 http->redirect.location = xstrdup(t + 1);
766 } else {
767 debug(85, 1) ("clientRedirectDone: bad input: %s\n", result);
768 }
769 }
770
771 if (strcmp(result, http->uri))
772 new_request = urlParse(old_request->method, result);
edce4d98 773 }
62e76326 774
edce4d98 775 if (new_request) {
62e76326 776 safe_free(http->uri);
777 http->uri = xstrdup(urlCanonical(new_request));
778 new_request->http_ver = old_request->http_ver;
779 httpHeaderAppend(&new_request->header, &old_request->header);
780 new_request->client_addr = old_request->client_addr;
47b0c1fa 781 new_request->client_port = old_request->client_port;
62e76326 782 new_request->my_addr = old_request->my_addr;
783 new_request->my_port = old_request->my_port;
784 new_request->flags = old_request->flags;
3c1f01bc 785 new_request->flags.redirected = 1;
62e76326 786
787 if (old_request->auth_user_request) {
788 new_request->auth_user_request = old_request->auth_user_request;
f5691f9c 789
790 new_request->auth_user_request->lock()
791
792 ;
62e76326 793 }
794
a2ac85d9 795 if (old_request->body_connection.getRaw() != NULL) {
62e76326 796 new_request->body_connection = old_request->body_connection;
797 old_request->body_connection = NULL;
798 }
799
800 new_request->content_length = old_request->content_length;
abb929f0 801 new_request->extacl_user = old_request->extacl_user;
802 new_request->extacl_passwd = old_request->extacl_passwd;
62e76326 803 new_request->flags.proxy_keepalive = old_request->flags.proxy_keepalive;
804 requestUnlink(old_request);
805 http->request = requestLink(new_request);
edce4d98 806 }
62e76326 807
edce4d98 808 clientInterpretRequestHeaders(http);
809#if HEADERS_LOG
62e76326 810
edce4d98 811 headersLog(0, 1, request->method, request);
812#endif
813 /* FIXME PIPELINE: This is innacurate during pipelining */
62e76326 814
a2ac85d9 815 if (http->getConn().getRaw() != NULL)
98242069 816 fd_note(http->getConn()->fd, http->uri);
62e76326 817
c8be6d7b 818 assert(http->uri);
62e76326 819
8e2745f4 820 context->checkNoCache();
edce4d98 821}
822
823void
8e2745f4 824ClientRequestContext::checkNoCache()
edce4d98 825{
edce4d98 826 if (Config.accessList.noCache && http->request->flags.cachable) {
62e76326 827 acl_checklist =
828 clientAclChecklistCreate(Config.accessList.noCache, http);
829 acl_checklist->nonBlockingCheck(CheckNoCacheDone, cbdataReference(this));
edce4d98 830 } else {
62e76326 831 CheckNoCacheDone(http->request->flags.cachable, cbdataReference(this));
edce4d98 832 }
833}
834
835void
8e2745f4 836ClientRequestContext::CheckNoCacheDone(int answer, void *data)
edce4d98 837{
4fb35c3c 838 void *temp;
e4a67a80 839#ifndef PURIFY
840
841 bool valid =
842#endif
843 cbdataReferenceValidDone(data, &temp);
8e2745f4 844 /* acl NB calls cannot invalidate cbdata in the normal course of things */
4fb35c3c 845 assert (valid);
8e2745f4 846 ClientRequestContext *context = (ClientRequestContext *)temp;
847 context->checkNoCacheDone(answer);
848}
4fb35c3c 849
8e2745f4 850void
851ClientRequestContext::checkNoCacheDone(int answer)
62e76326 852{
8e2745f4 853 acl_checklist = NULL;
854 clientHttpRequest *http_ = http;
8e2745f4 855
3b1b4c07 856 if (!cbdataReferenceValid (http_)) {
00d77d6b 857 delete this;
62e76326 858 return;
3b1b4c07 859 }
62e76326 860
00d77d6b 861 delete this;
8e2745f4 862 http_->request->flags.cachable = answer;
863 http_->processRequest();
edce4d98 864}
865
69660be0 866/*
867 * Identify requests that do not go through the store and client side stream
868 * and forward them to the appropriate location. All other requests, request
869 * them.
edce4d98 870 */
871void
8e2745f4 872ClientHttpRequest::processRequest()
edce4d98 873{
edce4d98 874 debug(85, 4) ("clientProcessRequest: %s '%s'\n",
62e76326 875 RequestMethodStr[request->method], uri);
876
8e2745f4 877 if (request->method == METHOD_CONNECT) {
62e76326 878 logType = LOG_TCP_MISS;
879 sslStart(this, &out.size, &al.http.code);
880 return;
edce4d98 881 }
62e76326 882
8e2745f4 883 httpStart();
884}
885
886void
887ClientHttpRequest::httpStart()
888{
889 logType = LOG_TAG_NONE;
890 debug(85, 4) ("ClientHttpRequest::httpStart: %s for '%s'\n",
62e76326 891 log_tags[logType], uri);
edce4d98 892 /* no one should have touched this */
8e2745f4 893 assert(out.offset == 0);
edce4d98 894 /* Use the Stream Luke */
8e2745f4 895 clientStreamNode *node = (clientStreamNode *)client_stream.tail->data;
896 clientStreamRead(node, this, node->readBuffer);
edce4d98 897}
0655fa4d 898
899bool
900ClientHttpRequest::gotEnough() const
901{
86a2f789 902 /** TODO: should be querying the stream. */
0655fa4d 903 int contentLength =
86a2f789 904 httpReplyBodySize(request->method, memObject()->getReply());
0655fa4d 905 assert(contentLength >= 0);
906
907 if (out.offset < contentLength)
908 return false;
909
910 return true;
911}
912
b51aec66 913void
914ClientHttpRequest::maxReplyBodySize(ssize_t clen)
915{
916 maxReplyBodySize_ = clen;
917}
918
919ssize_t
920ClientHttpRequest::maxReplyBodySize() const
921{
922 return maxReplyBodySize_;
923}
924
925bool
926ClientHttpRequest::isReplyBodyTooLarge(ssize_t clen) const
927{
928 if (0 == maxReplyBodySize())
929 return 0; /* disabled */
930
931 if (clen < 0)
932 return 0; /* unknown */
933
934 return clen > maxReplyBodySize();
935}
86a2f789 936
937void
938ClientHttpRequest::storeEntry(StoreEntry *newEntry)
939{
940 entry_ = newEntry;
941}
942
0976f8db 943void
944ClientHttpRequest::loggingEntry(StoreEntry *newEntry)
945{
946 if (loggingEntry_)
947 storeUnlockObject(loggingEntry_);
948
949 loggingEntry_ = newEntry;
950
951 if (loggingEntry_)
952 storeLockObject(loggingEntry_);
953}
86a2f789 954
955#ifndef _USE_INLINE_
956#include "client_side_request.cci"
957#endif