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