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