]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_side_request.cc
bugfix: ICAPConfig calls aclParseAccessLine(), which now takes a
[thirdparty/squid.git] / src / client_side_request.cc
CommitLineData
edce4d98 1
2/*
d295d770 3 * $Id: client_side_request.cc,v 1.61 2006/04/23 11:10:31 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"
de31d06f 56#include "ClientRequestContext.h"
d295d770 57#include "wordlist.h"
de31d06f 58
59#if ICAP_CLIENT
60#include "ICAP/ICAPClientReqmodPrecache.h"
61#include "ICAP/ICAPElements.h"
62#include "ICAP/ICAPConfig.h"
63static void icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service, void *data);
ab7ac359 64extern ICAPConfig TheICAPConfig;
de31d06f 65#endif
edce4d98 66
67#if LINGERING_CLOSE
68#define comm_close comm_lingering_close
69#endif
70
71static const char *const crlf = "\r\n";
72
8e2745f4 73CBDATA_CLASS_INIT(ClientRequestContext);
74
75void *
76ClientRequestContext::operator new (size_t size)
77{
78 assert (size == sizeof(ClientRequestContext));
79 CBDATA_INIT_TYPE(ClientRequestContext);
80 ClientRequestContext *result = cbdataAlloc(ClientRequestContext);
aa625860 81 return result;
8e2745f4 82}
62e76326 83
8e2745f4 84void
85ClientRequestContext::operator delete (void *address)
86{
87 ClientRequestContext *t = static_cast<ClientRequestContext *>(address);
aa625860 88 cbdataFree(t);
8e2745f4 89}
90
edce4d98 91/* Local functions */
edce4d98 92/* other */
de31d06f 93static void clientAccessCheckDoneWrapper(int, void *);
59a1efb2 94static int clientCachable(ClientHttpRequest * http);
95static int clientHierarchical(ClientHttpRequest * http);
96static void clientInterpretRequestHeaders(ClientHttpRequest * http);
de31d06f 97static RH clientRedirectDoneWrapper;
98static PF checkNoCacheDoneWrapper;
e6ccf245 99extern "C" CSR clientGetMoreData;
100extern "C" CSS clientReplyStatus;
101extern "C" CSD clientReplyDetach;
528b2c61 102static void checkFailureRatio(err_type, hier_code);
edce4d98 103
8e2745f4 104ClientRequestContext::~ClientRequestContext()
105{
de31d06f 106 /*
a546b04b 107 * Release our "lock" on our parent, ClientHttpRequest, if we
108 * still have one
de31d06f 109 */
a546b04b 110
111 if (http)
112 cbdataReferenceDone(http);
62e76326 113
501b58f9 114 if (acl_checklist) {
115 if (acl_checklist->asyncInProgress()) {
116 acl_checklist->markDeleteWhenDone();
117 } else {
118 delete acl_checklist;
119 }
120 }
8e2745f4 121}
122
de31d06f 123ClientRequestContext::ClientRequestContext(ClientHttpRequest *anHttp) : http(anHttp), acl_checklist (NULL), redirect_state (REDIRECT_NONE)
edce4d98 124{
de31d06f 125 (void) cbdataReference(http);
57abaac0 126 http_access_done = false;
127 redirect_done = false;
128 no_cache_done = false;
129 interpreted_req_hdrs = false;
edce4d98 130}
131
528b2c61 132CBDATA_CLASS_INIT(ClientHttpRequest);
8e2745f4 133
528b2c61 134void *
135ClientHttpRequest::operator new (size_t size)
136{
137 assert (size == sizeof (ClientHttpRequest));
138 CBDATA_INIT_TYPE(ClientHttpRequest);
139 ClientHttpRequest *result = cbdataAlloc(ClientHttpRequest);
aa625860 140 return result;
528b2c61 141}
142
62e76326 143void
528b2c61 144ClientHttpRequest::operator delete (void *address)
145{
aa625860 146 ClientHttpRequest *t = static_cast<ClientHttpRequest *>(address);
147 cbdataFree(t);
528b2c61 148}
149
a0355e95 150ClientHttpRequest::ClientHttpRequest(ConnStateData::Pointer aConn) : loggingEntry_(NULL)
528b2c61 151{
528b2c61 152 start = current_time;
a0355e95 153 setConn(aConn);
154 dlinkAdd(this, &active, &ClientActiveRequests);
b044675d 155#if ICAP_CLIENT
156
157 request_satisfaction_mode = false;
158#endif
528b2c61 159}
160
0655fa4d 161/*
162 * returns true if client specified that the object must come from the cache
163 * without contacting origin server
164 */
165bool
166ClientHttpRequest::onlyIfCached()const
167{
168 assert(request);
169 return request->cache_control &&
170 EBIT_TEST(request->cache_control->mask, CC_ONLY_IF_CACHED);
171}
172
528b2c61 173/*
174 * This function is designed to serve a fairly specific purpose.
175 * Occasionally our vBNS-connected caches can talk to each other, but not
176 * the rest of the world. Here we try to detect frequent failures which
177 * make the cache unusable (e.g. DNS lookup and connect() failures). If
178 * the failure:success ratio goes above 1.0 then we go into "hit only"
179 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
180 * will only fetch HITs from us if they are using the ICP protocol. We
181 * stay in this mode for 5 minutes.
182 *
183 * Duane W., Sept 16, 1996
184 */
185
186#define FAILURE_MODE_TIME 300
187
188static void
189checkFailureRatio(err_type etype, hier_code hcode)
190{
191 static double magic_factor = 100.0;
192 double n_good;
193 double n_bad;
62e76326 194
528b2c61 195 if (hcode == HIER_NONE)
62e76326 196 return;
197
528b2c61 198 n_good = magic_factor / (1.0 + request_failure_ratio);
62e76326 199
528b2c61 200 n_bad = magic_factor - n_good;
62e76326 201
528b2c61 202 switch (etype) {
62e76326 203
528b2c61 204 case ERR_DNS_FAIL:
62e76326 205
528b2c61 206 case ERR_CONNECT_FAIL:
62e76326 207
528b2c61 208 case ERR_READ_ERROR:
62e76326 209 n_bad++;
210 break;
211
528b2c61 212 default:
62e76326 213 n_good++;
528b2c61 214 }
62e76326 215
528b2c61 216 request_failure_ratio = n_bad / n_good;
62e76326 217
528b2c61 218 if (hit_only_mode_until > squid_curtime)
62e76326 219 return;
220
528b2c61 221 if (request_failure_ratio < 1.0)
62e76326 222 return;
223
528b2c61 224 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio);
62e76326 225
528b2c61 226 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
62e76326 227 FAILURE_MODE_TIME / 60);
228
528b2c61 229 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
62e76326 230
528b2c61 231 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
232}
233
234ClientHttpRequest::~ClientHttpRequest()
235{
236 debug(33, 3) ("httpRequestFree: %s\n", uri);
237 /* if body_connection !NULL, then ProcessBody has not
238 * found the end of the body yet
239 */
62e76326 240
21b92762 241 if (request && request->body_connection.getRaw() != NULL) {
62e76326 242 clientAbortBody(request); /* abort body transter */
21b92762 243 request->body_connection = NULL;
244 }
62e76326 245
528b2c61 246 /* the ICP check here was erroneous
247 * - storeReleaseRequest was always called if entry was valid
248 */
249 assert(logType < LOG_TYPE_MAX);
62e76326 250
528b2c61 251 logRequest();
62e76326 252
0976f8db 253 loggingEntry(NULL);
254
528b2c61 255 if (request)
62e76326 256 checkFailureRatio(request->errType, al.hier.code);
257
528b2c61 258 freeResources();
62e76326 259
de31d06f 260#if ICAP_CLIENT
261
262 if (icap) {
263 delete icap;
264 cbdataReferenceDone(icap);
265 }
266
267#endif
268 if (calloutContext)
269 delete calloutContext;
270
528b2c61 271 /* moving to the next connection is handled by the context free */
272 dlinkDelete(&active, &ClientActiveRequests);
273}
62e76326 274
edce4d98 275/* Create a request and kick it off */
69660be0 276/*
277 * TODO: Pass in the buffers to be used in the inital Read request, as they are
278 * determined by the user
edce4d98 279 */
280int /* returns nonzero on failure */
281clientBeginRequest(method_t method, char const *url, CSCB * streamcallback,
0655fa4d 282 CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header,
62e76326 283 char *tailbuf, size_t taillen)
edce4d98 284{
285 size_t url_sz;
450e0c10 286 HttpVersion http_ver (1, 0);
a0355e95 287 ClientHttpRequest *http = new ClientHttpRequest(NULL);
190154cf 288 HttpRequest *request;
528b2c61 289 StoreIOBuffer tempBuffer;
edce4d98 290 http->start = current_time;
291 /* this is only used to adjust the connection offset in client_side.c */
292 http->req_sz = 0;
c8be6d7b 293 tempBuffer.length = taillen;
294 tempBuffer.data = tailbuf;
edce4d98 295 /* client stream setup */
296 clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach,
0655fa4d 297 clientReplyStatus, new clientReplyContext(http), streamcallback,
62e76326 298 streamdetach, streamdata, tempBuffer);
edce4d98 299 /* make it visible in the 'current acctive requests list' */
edce4d98 300 /* Set flags */
a46d2c0e 301 /* internal requests only makes sense in an
302 * accelerator today. TODO: accept flags ? */
303 http->flags.accel = 1;
edce4d98 304 /* allow size for url rewriting */
305 url_sz = strlen(url) + Config.appendDomainLen + 5;
e6ccf245 306 http->uri = (char *)xcalloc(url_sz, 1);
edce4d98 307 strcpy(http->uri, url);
308
309 if ((request = urlParse(method, http->uri)) == NULL) {
62e76326 310 debug(85, 5) ("Invalid URL: %s\n", http->uri);
311 return -1;
edce4d98 312 }
62e76326 313
69660be0 314 /*
315 * now update the headers in request with our supplied headers. urLParse
316 * should return a blank header set, but we use Update to be sure of
317 * correctness.
edce4d98 318 */
319 if (header)
62e76326 320 httpHeaderUpdate(&request->header, header, NULL);
321
edce4d98 322 http->log_uri = xstrdup(urlCanonicalClean(request));
62e76326 323
edce4d98 324 /* http struct now ready */
325
69660be0 326 /*
327 * build new header list *? TODO
edce4d98 328 */
329 request->flags.accelerated = http->flags.accel;
62e76326 330
a46d2c0e 331 request->flags.internalclient = 1;
332
333 /* this is an internally created
334 * request, not subject to acceleration
335 * target overrides */
69660be0 336 /*
337 * FIXME? Do we want to detect and handle internal requests of internal
338 * objects ?
339 */
edce4d98 340
341 /* Internally created requests cannot have bodies today */
342 request->content_length = 0;
62e76326 343
edce4d98 344 request->client_addr = no_addr;
62e76326 345
edce4d98 346 request->my_addr = no_addr; /* undefined for internal requests */
62e76326 347
edce4d98 348 request->my_port = 0;
62e76326 349
edce4d98 350 request->http_ver = http_ver;
62e76326 351
6dd9f4bd 352 http->request = HTTPMSGLOCK(request);
edce4d98 353
354 /* optional - skip the access check ? */
de31d06f 355 http->calloutContext = new ClientRequestContext(http);
356
57abaac0 357 http->calloutContext->http_access_done = false;
de31d06f 358
57abaac0 359 http->calloutContext->redirect_done = true;
de31d06f 360
57abaac0 361 http->calloutContext->no_cache_done = true;
de31d06f 362
363 http->doCallouts();
62e76326 364
edce4d98 365 return 0;
366}
367
de31d06f 368bool
369ClientRequestContext::httpStateIsValid()
370{
371 ClientHttpRequest *http_ = http;
372
373 if (cbdataReferenceValid(http_))
374 return true;
375
376 http = NULL;
377
378 cbdataReferenceDone(http_);
379
380 return false;
381}
382
edce4d98 383/* This is the entry point for external users of the client_side routines */
384void
de31d06f 385ClientRequestContext::clientAccessCheck()
edce4d98 386{
de31d06f 387 acl_checklist =
62e76326 388 clientAclChecklistCreate(Config.accessList.http, http);
de31d06f 389 acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this);
edce4d98 390}
391
392void
de31d06f 393clientAccessCheckDoneWrapper(int answer, void *data)
edce4d98 394{
de31d06f 395 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
fbade053 396
de31d06f 397 if (!calloutContext->httpStateIsValid())
62e76326 398 return;
62e76326 399
de31d06f 400 calloutContext->clientAccessCheckDone(answer);
401}
402
403void
404ClientRequestContext::clientAccessCheckDone(int answer)
405{
406 acl_checklist = NULL;
edce4d98 407 err_type page_id;
408 http_status status;
edce4d98 409 debug(85, 2) ("The request %s %s is %s, because it matched '%s'\n",
62e76326 410 RequestMethodStr[http->request->method], http->uri,
411 answer == ACCESS_ALLOWED ? "ALLOWED" : "DENIED",
412 AclMatchedName ? AclMatchedName : "NO ACL's");
f5691f9c 413 char const *proxy_auth_msg = "<null>";
414
415 if (http->getConn().getRaw() != NULL && http->getConn()->auth_user_request != NULL)
416 proxy_auth_msg = http->getConn()->auth_user_request->denyMessage("<null>");
417 else if (http->request->auth_user_request != NULL)
418 proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>");
62e76326 419
de31d06f 420 if (answer != ACCESS_ALLOWED) {
62e76326 421 /* Send an error */
62e76326 422 debug(85, 5) ("Access Denied: %s\n", http->uri);
423 debug(85, 5) ("AclMatchedName = %s\n",
424 AclMatchedName ? AclMatchedName : "<null>");
425 debug(85, 5) ("Proxy Auth Message = %s\n",
426 proxy_auth_msg ? proxy_auth_msg : "<null>");
427 /*
428 * NOTE: get page_id here, based on AclMatchedName because if
429 * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in
430 * the clientCreateStoreEntry() call just below. Pedro Ribeiro
431 * <pribeiro@isel.pt>
432 */
433 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName);
434 http->logType = LOG_TCP_DENIED;
435
436 if (answer == ACCESS_REQ_PROXY_AUTH || aclIsProxyAuth(AclMatchedName)) {
437 if (!http->flags.accel) {
438 /* Proxy authorisation needed */
439 status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
440 } else {
441 /* WWW authorisation needed */
442 status = HTTP_UNAUTHORIZED;
443 }
444
445 if (page_id == ERR_NONE)
446 page_id = ERR_CACHE_ACCESS_DENIED;
447 } else {
448 status = HTTP_FORBIDDEN;
449
450 if (page_id == ERR_NONE)
451 page_id = ERR_ACCESS_DENIED;
452 }
453
de31d06f 454 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->prev->data;
0655fa4d 455 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
456 assert (repContext);
457 repContext->setReplyToError(page_id, status,
458 http->request->method, NULL,
a2ac85d9 459 http->getConn().getRaw() != NULL ? &http->getConn()->peer.sin_addr : &no_addr, http->request,
460 NULL, http->getConn().getRaw() != NULL
98242069 461 && http->getConn()->auth_user_request ? http->getConn()->
0655fa4d 462 auth_user_request : http->request->auth_user_request);
62e76326 463 node = (clientStreamNode *)http->client_stream.tail->data;
464 clientStreamRead(node, http, node->readBuffer);
a546b04b 465 return;
edce4d98 466 }
de31d06f 467
468 /* ACCESS_ALLOWED continues here ... */
469 safe_free(http->uri);
470
471 http->uri = xstrdup(urlCanonical(http->request));
472
473 http->doCallouts();
474}
475
476#if ICAP_CLIENT
477void
478ClientRequestContext::icapAccessCheck()
479{
480 ICAPAccessCheck *icap_access_check;
481
e8fe1384 482 icap_access_check = new ICAPAccessCheck(ICAP::methodReqmod, ICAP::pointPreCache, http->request, NULL, icapAclCheckDoneWrapper, this);
483
484 if (icap_access_check != NULL) {
de31d06f 485 icap_access_check->check();
486 return;
487 }
488
489 http->doCallouts();
490}
491
492static void
493icapAclCheckDoneWrapper(ICAPServiceRep::Pointer service, void *data)
494{
495 ClientRequestContext *calloutContext = (ClientRequestContext *)data;
496
497 if (!calloutContext->httpStateIsValid())
498 return;
499
500 calloutContext->icapAclCheckDone(service);
501}
502
503void
504ClientRequestContext::icapAclCheckDone(ICAPServiceRep::Pointer service)
505{
506 /*
507 * No matching ICAP service in the config file
508 */
509
510 if (service == NULL) {
511 http->doCallouts();
512 return;
513 }
514
515 /*
516 * Setup ICAP state and such. If successful, just return.
517 * We'll get back to doCallouts() after REQMOD is done.
518 */
519 if (0 == http->doIcap(service))
520 return;
521
522 /*
523 * If doIcap() fails, then we have to either return an error
524 * to the user, or keep going without ICAP.
525 */
526 fatal("Fix this case in ClientRequestContext::icapAclCheckDone()");
527
528 http->doCallouts();
edce4d98 529}
530
de31d06f 531#endif
532
14cc8559 533static void
534clientRedirectAccessCheckDone(int answer, void *data)
535{
536 ClientRequestContext *context = (ClientRequestContext *)data;
59a1efb2 537 ClientHttpRequest *http = context->http;
14cc8559 538 context->acl_checklist = NULL;
539
540 if (answer == ACCESS_ALLOWED)
de31d06f 541 redirectStart(http, clientRedirectDoneWrapper, context);
14cc8559 542 else
de31d06f 543 context->clientRedirectDone(NULL);
14cc8559 544}
545
de31d06f 546void
547ClientRequestContext::clientRedirectStart()
14cc8559 548{
14cc8559 549 debug(33, 5) ("clientRedirectStart: '%s'\n", http->uri);
550
14cc8559 551 if (Config.accessList.redirector) {
de31d06f 552 acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http);
553 acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, this);
14cc8559 554 } else
de31d06f 555 redirectStart(http, clientRedirectDoneWrapper, this);
14cc8559 556}
557
edce4d98 558static int
59a1efb2 559clientCachable(ClientHttpRequest * http)
edce4d98 560{
190154cf 561 HttpRequest *req = http->request;
edce4d98 562 method_t method = req->method;
62e76326 563
edce4d98 564 if (req->protocol == PROTO_HTTP)
62e76326 565 return httpCachable(method);
566
edce4d98 567 /* FTP is always cachable */
568 if (req->protocol == PROTO_WAIS)
62e76326 569 return 0;
570
69660be0 571 /*
572 * The below looks questionable: what non HTTP protocols use connect,
573 * trace, put and post? RC
edce4d98 574 */
575 if (method == METHOD_CONNECT)
62e76326 576 return 0;
577
edce4d98 578 if (method == METHOD_TRACE)
62e76326 579 return 0;
580
edce4d98 581 if (method == METHOD_PUT)
62e76326 582 return 0;
583
edce4d98 584 if (method == METHOD_POST)
a46d2c0e 585 return 0;
586
587 /* XXX POST may be cached sometimes.. ignored
588
589 * for now */
edce4d98 590 if (req->protocol == PROTO_GOPHER)
62e76326 591 return gopherCachable(req);
592
edce4d98 593 if (req->protocol == PROTO_CACHEOBJ)
62e76326 594 return 0;
595
edce4d98 596 return 1;
597}
598
599static int
59a1efb2 600clientHierarchical(ClientHttpRequest * http)
edce4d98 601{
602 const char *url = http->uri;
190154cf 603 HttpRequest *request = http->request;
edce4d98 604 method_t method = request->method;
605 const wordlist *p = NULL;
606
69660be0 607 /*
608 * IMS needs a private key, so we can use the hierarchy for IMS only if our
609 * neighbors support private keys
610 */
62e76326 611
edce4d98 612 if (request->flags.ims && !neighbors_do_private_keys)
62e76326 613 return 0;
614
69660be0 615 /*
616 * This is incorrect: authenticating requests can be sent via a hierarchy
06b97e72 617 * (they can even be cached if the correct headers are set on the reply)
edce4d98 618 */
619 if (request->flags.auth)
62e76326 620 return 0;
621
edce4d98 622 if (method == METHOD_TRACE)
62e76326 623 return 1;
624
edce4d98 625 if (method != METHOD_GET)
62e76326 626 return 0;
627
edce4d98 628 /* scan hierarchy_stoplist */
629 for (p = Config.hierarchy_stoplist; p; p = p->next)
62e76326 630 if (strstr(url, p->key))
631 return 0;
632
edce4d98 633 if (request->flags.loopdetect)
62e76326 634 return 0;
635
edce4d98 636 if (request->protocol == PROTO_HTTP)
62e76326 637 return httpCachable(method);
638
edce4d98 639 if (request->protocol == PROTO_GOPHER)
62e76326 640 return gopherCachable(request);
641
edce4d98 642 if (request->protocol == PROTO_WAIS)
62e76326 643 return 0;
644
edce4d98 645 if (request->protocol == PROTO_CACHEOBJ)
62e76326 646 return 0;
647
edce4d98 648 return 1;
649}
650
651
652static void
59a1efb2 653clientInterpretRequestHeaders(ClientHttpRequest * http)
edce4d98 654{
190154cf 655 HttpRequest *request = http->request;
edce4d98 656 const HttpHeader *req_hdr = &request->header;
657 int no_cache = 0;
a787b56a 658#if !(ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
62e76326 659
edce4d98 660 const char *str;
661#endif
62e76326 662
edce4d98 663 request->imslen = -1;
664 request->ims = httpHeaderGetTime(req_hdr, HDR_IF_MODIFIED_SINCE);
62e76326 665
edce4d98 666 if (request->ims > 0)
62e76326 667 request->flags.ims = 1;
668
edce4d98 669#if ESI
69660be0 670 /*
671 * We ignore Cache-Control as per the Edge Architecture Section 3. See
672 * www.esi.org for more information.
edce4d98 673 */
674#else
62e76326 675
edce4d98 676 if (httpHeaderHas(req_hdr, HDR_PRAGMA)) {
62e76326 677 String s = httpHeaderGetList(req_hdr, HDR_PRAGMA);
678
679 if (strListIsMember(&s, "no-cache", ','))
680 no_cache++;
681
682 s.clean();
edce4d98 683 }
62e76326 684
edce4d98 685 if (request->cache_control)
62e76326 686 if (EBIT_TEST(request->cache_control->mask, CC_NO_CACHE))
687 no_cache++;
688
69660be0 689 /*
62e76326 690 * Work around for supporting the Reload button in IE browsers when Squid
691 * is used as an accelerator or transparent proxy, by turning accelerated
692 * IMS request to no-cache requests. Now knows about IE 5.5 fix (is
693 * actually only fixed in SP1, but we can't tell whether we are talking to
694 * SP1 or not so all 5.5 versions are treated 'normally').
695 */
edce4d98 696 if (Config.onoff.ie_refresh) {
62e76326 697 if (http->flags.accel && request->flags.ims) {
698 if ((str = httpHeaderGetStr(req_hdr, HDR_USER_AGENT))) {
699 if (strstr(str, "MSIE 5.01") != NULL)
700 no_cache++;
701 else if (strstr(str, "MSIE 5.0") != NULL)
702 no_cache++;
703 else if (strstr(str, "MSIE 4.") != NULL)
704 no_cache++;
705 else if (strstr(str, "MSIE 3.") != NULL)
706 no_cache++;
707 }
708 }
edce4d98 709 }
62e76326 710
edce4d98 711#endif
712 if (no_cache) {
713#if HTTP_VIOLATIONS
62e76326 714
715 if (Config.onoff.reload_into_ims)
716 request->flags.nocache_hack = 1;
717 else if (refresh_nocache_hack)
718 request->flags.nocache_hack = 1;
719 else
edce4d98 720#endif
62e76326 721
722 request->flags.nocache = 1;
edce4d98 723 }
62e76326 724
edce4d98 725 /* ignore range header in non-GETs */
726 if (request->method == METHOD_GET) {
62e76326 727 request->range = httpHeaderGetRange(req_hdr);
728
729 if (request->range) {
730 request->flags.range = 1;
731 clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data;
732 /* XXX: This is suboptimal. We should give the stream the range set,
733 * and thereby let the top of the stream set the offset when the
734 * size becomes known. As it is, we will end up requesting from 0
735 * for evey -X range specification.
736 * RBC - this may be somewhat wrong. We should probably set the range
737 * iter up at this point.
738 */
739 node->readBuffer.offset = request->range->lowestOffset(0);
740 http->range_iter.pos = request->range->begin();
741 http->range_iter.valid = true;
742 }
edce4d98 743 }
62e76326 744
edce4d98 745 if (httpHeaderHas(req_hdr, HDR_AUTHORIZATION))
62e76326 746 request->flags.auth = 1;
747
edce4d98 748 if (request->login[0] != '\0')
62e76326 749 request->flags.auth = 1;
750
edce4d98 751 if (httpHeaderHas(req_hdr, HDR_VIA)) {
62e76326 752 String s = httpHeaderGetList(req_hdr, HDR_VIA);
753 /*
754 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
755 * Note ThisCache2 has a space prepended to the hostname so we don't
756 * accidentally match super-domains.
757 */
758
759 if (strListIsSubstr(&s, ThisCache2, ',')) {
760 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
761 request, (ObjPackMethod) & httpRequestPack);
762 request->flags.loopdetect = 1;
763 }
764
edce4d98 765#if FORW_VIA_DB
62e76326 766 fvdbCountVia(s.buf());
767
edce4d98 768#endif
62e76326 769
770 s.clean();
edce4d98 771 }
62e76326 772
edce4d98 773#if USE_USERAGENT_LOG
774 if ((str = httpHeaderGetStr(req_hdr, HDR_USER_AGENT)))
7928e475 775 logUserAgent(fqdnFromAddr(http->getConn().getRaw() ? http->getConn()->log_addr : no_addr), str);
62e76326 776
edce4d98 777#endif
778#if USE_REFERER_LOG
62e76326 779
edce4d98 780 if ((str = httpHeaderGetStr(req_hdr, HDR_REFERER)))
7928e475 781 logReferer(fqdnFromAddr(http->getConn().getRaw() ? http->getConn()->log_addr : no_addr), str, http->log_uri);
62e76326 782
edce4d98 783#endif
784#if FORW_VIA_DB
62e76326 785
edce4d98 786 if (httpHeaderHas(req_hdr, HDR_X_FORWARDED_FOR)) {
62e76326 787 String s = httpHeaderGetList(req_hdr, HDR_X_FORWARDED_FOR);
788 fvdbCountForw(s.buf());
789 s.clean();
edce4d98 790 }
62e76326 791
edce4d98 792#endif
793 if (request->method == METHOD_TRACE) {
62e76326 794 request->max_forwards = httpHeaderGetInt(req_hdr, HDR_MAX_FORWARDS);
edce4d98 795 }
62e76326 796
edce4d98 797 if (clientCachable(http))
62e76326 798 request->flags.cachable = 1;
799
edce4d98 800 if (clientHierarchical(http))
62e76326 801 request->flags.hierarchical = 1;
802
edce4d98 803 debug(85, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
62e76326 804 request->flags.nocache ? "SET" : "NOT SET");
805
edce4d98 806 debug(85, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
62e76326 807 request->flags.cachable ? "SET" : "NOT SET");
808
edce4d98 809 debug(85, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
62e76326 810 request->flags.hierarchical ? "SET" : "NOT SET");
edce4d98 811}
812
813void
de31d06f 814clientRedirectDoneWrapper(void *data, char *result)
edce4d98 815{
de31d06f 816 ClientRequestContext *calloutContext = (ClientRequestContext *)data;
db02222f 817
de31d06f 818 if (!calloutContext->httpStateIsValid())
62e76326 819 return;
62e76326 820
de31d06f 821 calloutContext->clientRedirectDone(result);
822}
823
824void
825ClientRequestContext::clientRedirectDone(char *result)
826{
190154cf 827 HttpRequest *new_request = NULL;
828 HttpRequest *old_request = http->request;
edce4d98 829 debug(85, 5) ("clientRedirectDone: '%s' result=%s\n", http->uri,
62e76326 830 result ? result : "NULL");
de31d06f 831 assert(redirect_state == REDIRECT_PENDING);
832 redirect_state = REDIRECT_DONE;
62e76326 833
edce4d98 834 if (result) {
62e76326 835 http_status status = (http_status) atoi(result);
836
837 if (status == HTTP_MOVED_PERMANENTLY
838 || status == HTTP_MOVED_TEMPORARILY
839 || status == HTTP_SEE_OTHER
840 || status == HTTP_TEMPORARY_REDIRECT) {
841 char *t = result;
842
843 if ((t = strchr(result, ':')) != NULL) {
844 http->redirect.status = status;
845 http->redirect.location = xstrdup(t + 1);
846 } else {
847 debug(85, 1) ("clientRedirectDone: bad input: %s\n", result);
848 }
2baf58c3 849 } else if (strcmp(result, http->uri))
62e76326 850 new_request = urlParse(old_request->method, result);
edce4d98 851 }
62e76326 852
edce4d98 853 if (new_request) {
62e76326 854 safe_free(http->uri);
855 http->uri = xstrdup(urlCanonical(new_request));
856 new_request->http_ver = old_request->http_ver;
857 httpHeaderAppend(&new_request->header, &old_request->header);
858 new_request->client_addr = old_request->client_addr;
47b0c1fa 859 new_request->client_port = old_request->client_port;
62e76326 860 new_request->my_addr = old_request->my_addr;
861 new_request->my_port = old_request->my_port;
862 new_request->flags = old_request->flags;
3c1f01bc 863 new_request->flags.redirected = 1;
62e76326 864
865 if (old_request->auth_user_request) {
866 new_request->auth_user_request = old_request->auth_user_request;
f5691f9c 867
868 new_request->auth_user_request->lock()
869
870 ;
62e76326 871 }
872
a2ac85d9 873 if (old_request->body_connection.getRaw() != NULL) {
62e76326 874 new_request->body_connection = old_request->body_connection;
875 old_request->body_connection = NULL;
876 }
877
878 new_request->content_length = old_request->content_length;
abb929f0 879 new_request->extacl_user = old_request->extacl_user;
880 new_request->extacl_passwd = old_request->extacl_passwd;
62e76326 881 new_request->flags.proxy_keepalive = old_request->flags.proxy_keepalive;
6dd9f4bd 882 HTTPMSGUNLOCK(old_request);
883 http->request = HTTPMSGLOCK(new_request);
edce4d98 884 }
62e76326 885
edce4d98 886 /* FIXME PIPELINE: This is innacurate during pipelining */
62e76326 887
a2ac85d9 888 if (http->getConn().getRaw() != NULL)
98242069 889 fd_note(http->getConn()->fd, http->uri);
62e76326 890
c8be6d7b 891 assert(http->uri);
62e76326 892
de31d06f 893 http->doCallouts();
edce4d98 894}
895
896void
8e2745f4 897ClientRequestContext::checkNoCache()
edce4d98 898{
de31d06f 899 acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http);
57abaac0 900 acl_checklist->nonBlockingCheck(checkNoCacheDoneWrapper, this);
edce4d98 901}
902
de31d06f 903static void
904checkNoCacheDoneWrapper(int answer, void *data)
edce4d98 905{
de31d06f 906 ClientRequestContext *calloutContext = (ClientRequestContext *) data;
e4a67a80 907
de31d06f 908 if (!calloutContext->httpStateIsValid())
909 return;
910
911 calloutContext->checkNoCacheDone(answer);
8e2745f4 912}
4fb35c3c 913
8e2745f4 914void
915ClientRequestContext::checkNoCacheDone(int answer)
62e76326 916{
8e2745f4 917 acl_checklist = NULL;
de31d06f 918 http->request->flags.cachable = answer;
919 http->doCallouts();
edce4d98 920}
921
69660be0 922/*
923 * Identify requests that do not go through the store and client side stream
924 * and forward them to the appropriate location. All other requests, request
925 * them.
edce4d98 926 */
927void
8e2745f4 928ClientHttpRequest::processRequest()
edce4d98 929{
edce4d98 930 debug(85, 4) ("clientProcessRequest: %s '%s'\n",
62e76326 931 RequestMethodStr[request->method], uri);
932
2baf58c3 933 if (request->method == METHOD_CONNECT && !redirect.status) {
62e76326 934 logType = LOG_TCP_MISS;
935 sslStart(this, &out.size, &al.http.code);
936 return;
edce4d98 937 }
62e76326 938
8e2745f4 939 httpStart();
940}
941
942void
943ClientHttpRequest::httpStart()
944{
945 logType = LOG_TAG_NONE;
946 debug(85, 4) ("ClientHttpRequest::httpStart: %s for '%s'\n",
62e76326 947 log_tags[logType], uri);
edce4d98 948 /* no one should have touched this */
8e2745f4 949 assert(out.offset == 0);
edce4d98 950 /* Use the Stream Luke */
8e2745f4 951 clientStreamNode *node = (clientStreamNode *)client_stream.tail->data;
952 clientStreamRead(node, this, node->readBuffer);
edce4d98 953}
0655fa4d 954
955bool
956ClientHttpRequest::gotEnough() const
957{
86a2f789 958 /** TODO: should be querying the stream. */
0655fa4d 959 int contentLength =
06a5ae20 960 memObject()->getReply()->bodySize(request->method);
0655fa4d 961 assert(contentLength >= 0);
962
963 if (out.offset < contentLength)
964 return false;
965
966 return true;
967}
968
b51aec66 969void
970ClientHttpRequest::maxReplyBodySize(ssize_t clen)
971{
972 maxReplyBodySize_ = clen;
973}
974
975ssize_t
976ClientHttpRequest::maxReplyBodySize() const
977{
978 return maxReplyBodySize_;
979}
980
981bool
982ClientHttpRequest::isReplyBodyTooLarge(ssize_t clen) const
983{
984 if (0 == maxReplyBodySize())
985 return 0; /* disabled */
986
987 if (clen < 0)
988 return 0; /* unknown */
989
990 return clen > maxReplyBodySize();
991}
86a2f789 992
993void
994ClientHttpRequest::storeEntry(StoreEntry *newEntry)
995{
996 entry_ = newEntry;
997}
998
0976f8db 999void
1000ClientHttpRequest::loggingEntry(StoreEntry *newEntry)
1001{
1002 if (loggingEntry_)
1003 storeUnlockObject(loggingEntry_);
1004
1005 loggingEntry_ = newEntry;
1006
1007 if (loggingEntry_)
1008 storeLockObject(loggingEntry_);
1009}
86a2f789 1010
de31d06f 1011/*
1012 * doCallouts() - This function controls the order of "callout"
1013 * executions, including non-blocking access control checks, the
1014 * redirector, and ICAP. Previously, these callouts were chained
1015 * together such that "clientAccessCheckDone()" would call
1016 * "clientRedirectStart()" and so on.
1017 *
1018 * The ClientRequestContext (aka calloutContext) class holds certain
1019 * state data for the callout/callback operations. Previously
1020 * ClientHttpRequest would sort of hand off control to ClientRequestContext
1021 * for a short time. ClientRequestContext would then delete itself
1022 * and pass control back to ClientHttpRequest when all callouts
1023 * were finished.
1024 *
1025 * This caused some problems for ICAP because we want to make the
1026 * ICAP callout after checking ACLs, but before checking the no_cache
1027 * list. We can't stuff the ICAP state into the ClientRequestContext
1028 * class because we still need the ICAP state after ClientRequestContext
1029 * goes away.
1030 *
1031 * Note that ClientRequestContext is created before the first call
1032 * to doCallouts().
1033 *
1034 * If one of the callouts notices that ClientHttpRequest is no
1035 * longer valid, it should call cbdataReferenceDone() so that
1036 * ClientHttpRequest's reference count goes to zero and it will get
1037 * deleted. ClientHttpRequest will then delete ClientRequestContext.
1038 *
1039 * Note that we set the _done flags here before actually starting
1040 * the callout. This is strictly for convenience.
1041 */
1042
1043void
1044ClientHttpRequest::doCallouts()
1045{
1046 assert(calloutContext);
1047
1048 if (!calloutContext->http_access_done) {
57abaac0 1049 calloutContext->http_access_done = true;
de31d06f 1050 calloutContext->clientAccessCheck();
1051 return;
1052 }
1053
1054#if ICAP_CLIENT
ab7ac359 1055 if (TheICAPConfig.onoff && !calloutContext->icap_acl_check_done) {
57abaac0 1056 calloutContext->icap_acl_check_done = true;
de31d06f 1057 calloutContext->icapAccessCheck();
1058 return;
1059 }
1060
1061#endif
1062
1063 if (!calloutContext->redirect_done) {
57abaac0 1064 calloutContext->redirect_done = true;
de31d06f 1065 assert(calloutContext->redirect_state == REDIRECT_NONE);
1066
1067 if (Config.Program.redirect) {
1068 calloutContext->redirect_state = REDIRECT_PENDING;
1069 calloutContext->clientRedirectStart();
1070 return;
1071 }
1072 }
1073
57abaac0 1074 if (!calloutContext->interpreted_req_hdrs) {
1075 calloutContext->interpreted_req_hdrs = 1;
1076 clientInterpretRequestHeaders(this);
1077 }
1078
de31d06f 1079 if (!calloutContext->no_cache_done) {
57abaac0 1080 calloutContext->no_cache_done = true;
de31d06f 1081
1082 if (Config.accessList.noCache && request->flags.cachable) {
1083 calloutContext->checkNoCache();
1084 return;
1085 }
1086 }
1087
1088 cbdataReferenceDone(calloutContext->http);
1089 delete calloutContext;
1090 calloutContext = NULL;
de31d06f 1091#if HEADERS_LOG
1092
1093 headersLog(0, 1, request->method, request);
1094#endif
1095
1096 processRequest();
1097}
1098
86a2f789 1099#ifndef _USE_INLINE_
1100#include "client_side_request.cci"
1101#endif
de31d06f 1102
1103#if ICAP_CLIENT
1104/*
1105 * Initiate an ICAP transaction. Return 0 if all is well, or -1 upon error.
1106 * Caller will handle error condition by generating a Squid error message
1107 * or take other action.
1108 */
1109int
1110ClientHttpRequest::doIcap(ICAPServiceRep::Pointer service)
1111{
1112 debug(85,3)("ClientHttpRequest::doIcap() called\n");
1113 assert(NULL == icap);
1114 icap = new ICAPClientReqmodPrecache(service);
1115 (void) cbdataReference(icap);
1116 icap->startReqMod(this, request);
1117 icap->doneSending();
1118 return 0;
1119}
1120
1121/*
1122 * Called by ICAPAnchor when it has space available for us.
1123 */
1124void
1125ClientHttpRequest::icapSpaceAvailable()
1126{
1127 debug(85,3)("ClientHttpRequest::icapSpaceAvailable() called\n");
1128}
1129
1130void
1131ClientHttpRequest::takeAdaptedHeaders(HttpMsg *msg)
1132{
1133 debug(85,3)("ClientHttpRequest::takeAdaptedHeaders() called\n");
1134 assert(cbdataReferenceValid(this)); // indicates bug
1135
b044675d 1136 if (HttpRequest *new_req = dynamic_cast<HttpRequest*>(msg)) {
200ac359 1137 /*
1138 * Replace the old request with the new request. First,
1139 * Move the "body_connection" over, then unlink old and
1140 * link new to the http state.
1141 */
1142 new_req->body_connection = request->body_connection;
1143 request->body_connection = NULL;
6dd9f4bd 1144 HTTPMSGUNLOCK(request);
1145 request = HTTPMSGLOCK(new_req);
200ac359 1146 /*
1147 * Store the new URI for logging
1148 */
1149 xfree(uri);
1150 uri = xstrdup(urlCanonical(request));
1151 setLogUri(this, urlCanonicalClean(request));
1152 assert(request->method);
b044675d 1153 } else if (HttpReply *new_rep = dynamic_cast<HttpReply*>(msg)) {
1154 debugs(85,3,HERE << "REQMOD reply is HTTP reply");
1155
1156 clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data;
1157 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
1158 repContext->createStoreEntry(request->method, request->flags);
1159
1160 EBIT_CLR(storeEntry()->flags, ENTRY_FWD_HDR_WAIT);
1161 request_satisfaction_mode = true;
1162 request_satisfaction_offset = 0;
1163 storeEntry()->replaceHttpReply(new_rep);
1164 clientGetMoreData(node, this);
200ac359 1165 }
de31d06f 1166
b044675d 1167 if (!request_satisfaction_mode)
1168 doCallouts();
de31d06f 1169
1170 debug(85,3)("ClientHttpRequest::takeAdaptedHeaders() finished\n");
1171}
1172
1173void
1174ClientHttpRequest::takeAdaptedBody(MemBuf *buf)
1175{
1176 debug(85,3)("ClientHttpRequest::takeAdaptedBody() called\n");
b044675d 1177
1178 if (request_satisfaction_mode) {
1179 storeEntry()->write(StoreIOBuffer(buf, request_satisfaction_offset));
1180 request_satisfaction_offset += buf->contentSize();
1181 buf->consume(buf->contentSize()); // consume everything written
1182 }
de31d06f 1183}
1184
1185void
1186ClientHttpRequest::doneAdapting()
1187{
1188 debug(85,3)("ClientHttpRequest::doneAdapting() called\n");
1189}
1190
1191void
1192ClientHttpRequest::abortAdapting()
1193{
1194 debug(85,3)("ClientHttpRequest::abortAdapting() called\n");
1195
1196 if ((NULL == storeEntry()) || storeEntry()->isEmpty()) {
1197 debug(85,3)("WARNING: ICAP REQMOD callout failed, proceeding with original request\n");
1198 doCallouts();
1199#if ICAP_HARD_ERROR
1200
1201 clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data;
1202 clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
1203 assert (repContext);
1204 // Note if this code is ever used, clientBuildError() should be modified to
1205 // accept an errno arg
1206 repContext->setReplyToError(ERR_ICAP_FAILURE, HTTP_INTERNAL_SERVER_ERROR,
1207 request->method, NULL,
1208 getConn().getRaw() != NULL ? &getConn()->peer.sin_addr : &no_addr, request,
1209 NULL, getConn().getRaw() != NULL
1210 && getConn()->auth_user_request ? getConn()->
1211 auth_user_request : request->auth_user_request, errno);
1212 node = (clientStreamNode *)client_stream.tail->data;
1213 clientStreamRead(node, this, node->readBuffer);
1214#endif
1215
1216 return;
1217 }
1218
1219 debug(0,0)("write me at %s:%d\n", __FILE__,__LINE__);
1220}
1221
1222#endif