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