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