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