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