]>
Commit | Line | Data |
---|---|---|
edce4d98 | 1 | |
2 | /* | |
1cf238db | 3 | * $Id: client_side_request.cc,v 1.105 2008/02/12 23:07:52 rousskov 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 | * ---------------------------------------------------------- | |
0a9297f6 | 10 | * |
11 | * Squid is the result of efforts by numerous individuals from | |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
19 | * | |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
33 | * | |
edce4d98 | 34 | */ |
35 | ||
36 | ||
69660be0 | 37 | /* |
38 | * General logic of request processing: | |
39 | * | |
40 | * We run a series of tests to determine if access will be permitted, and to do | |
41 | * any redirection. Then we call into the result clientStream to retrieve data. | |
42 | * From that point on it's up to reply management. | |
edce4d98 | 43 | */ |
44 | ||
45 | #include "squid.h" | |
c8be6d7b | 46 | #include "clientStream.h" |
47 | #include "client_side_request.h" | |
f5691f9c | 48 | #include "AuthUserRequest.h" |
528b2c61 | 49 | #include "HttpRequest.h" |
3712be3f | 50 | #include "ProtoPort.h" |
8000a965 | 51 | #include "ACLChecklist.h" |
52 | #include "ACL.h" | |
a46d2c0e | 53 | #include "client_side.h" |
0655fa4d | 54 | #include "client_side_reply.h" |
55 | #include "Store.h" | |
56 | #include "HttpReply.h" | |
86a2f789 | 57 | #include "MemObject.h" |
de31d06f | 58 | #include "ClientRequestContext.h" |
985c86bc | 59 | #include "SquidTime.h" |
d295d770 | 60 | #include "wordlist.h" |
de31d06f | 61 | |
a83c6ed6 AR |
62 | #if USE_ADAPTATION |
63 | #include "ICAP/ICAPConfig.h" /* XXX: replace with generic adaptation config */ | |
64 | static void adaptationAclCheckDoneWrapper(Adaptation::ServicePointer service, void *data); | |
de31d06f | 65 | #endif |
edce4d98 | 66 | |
67 | #if LINGERING_CLOSE | |
68 | #define comm_close comm_lingering_close | |
69 | #endif | |
70 | ||
71 | static const char *const crlf = "\r\n"; | |
72 | ||
8e2745f4 | 73 | CBDATA_CLASS_INIT(ClientRequestContext); |
74 | ||
75 | void * | |
76 | ClientRequestContext::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 | 84 | void |
85 | ClientRequestContext::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 | 93 | static void clientAccessCheckDoneWrapper(int, void *); |
59a1efb2 | 94 | static int clientHierarchical(ClientHttpRequest * http); |
95 | static void clientInterpretRequestHeaders(ClientHttpRequest * http); | |
de31d06f | 96 | static RH clientRedirectDoneWrapper; |
97 | static PF checkNoCacheDoneWrapper; | |
e6ccf245 | 98 | extern "C" CSR clientGetMoreData; |
99 | extern "C" CSS clientReplyStatus; | |
100 | extern "C" CSD clientReplyDetach; | |
528b2c61 | 101 | static void checkFailureRatio(err_type, hier_code); |
edce4d98 | 102 | |
8e2745f4 | 103 | ClientRequestContext::~ClientRequestContext() |
104 | { | |
de31d06f | 105 | /* |
a546b04b | 106 | * Release our "lock" on our parent, ClientHttpRequest, if we |
107 | * still have one | |
de31d06f | 108 | */ |
a546b04b | 109 | |
110 | if (http) | |
111 | cbdataReferenceDone(http); | |
62e76326 | 112 | |
5f8252d2 | 113 | debugs(85,3, HERE << this << " ClientRequestContext destructed"); |
8e2745f4 | 114 | } |
115 | ||
4e2eb5c3 | 116 | ClientRequestContext::ClientRequestContext(ClientHttpRequest *anHttp) : http(cbdataReference(anHttp)), acl_checklist (NULL), redirect_state (REDIRECT_NONE) |
edce4d98 | 117 | { |
57abaac0 | 118 | http_access_done = false; |
119 | redirect_done = false; | |
120 | no_cache_done = false; | |
121 | interpreted_req_hdrs = false; | |
0b86805b | 122 | debugs(85,3, HERE << this << " ClientRequestContext constructed"); |
edce4d98 | 123 | } |
124 | ||
528b2c61 | 125 | CBDATA_CLASS_INIT(ClientHttpRequest); |
8e2745f4 | 126 | |
528b2c61 | 127 | void * |
128 | ClientHttpRequest::operator new (size_t size) | |
129 | { | |
130 | assert (size == sizeof (ClientHttpRequest)); | |
131 | CBDATA_INIT_TYPE(ClientHttpRequest); | |
132 | ClientHttpRequest *result = cbdataAlloc(ClientHttpRequest); | |
aa625860 | 133 | return result; |
528b2c61 | 134 | } |
135 | ||
62e76326 | 136 | void |
528b2c61 | 137 | ClientHttpRequest::operator delete (void *address) |
138 | { | |
aa625860 | 139 | ClientHttpRequest *t = static_cast<ClientHttpRequest *>(address); |
140 | cbdataFree(t); | |
528b2c61 | 141 | } |
142 | ||
1cf238db | 143 | ClientHttpRequest::ClientHttpRequest(ConnStateData * aConn) : |
a83c6ed6 | 144 | #if USE_ADAPTATION |
1cf238db | 145 | AsyncJob("ClientHttpRequest"), |
146 | #endif | |
147 | loggingEntry_(NULL) | |
528b2c61 | 148 | { |
1cf238db | 149 | start_time = current_time; |
a0355e95 | 150 | setConn(aConn); |
151 | dlinkAdd(this, &active, &ClientActiveRequests); | |
a83c6ed6 | 152 | #if USE_ADAPTATION |
b044675d | 153 | request_satisfaction_mode = false; |
154 | #endif | |
528b2c61 | 155 | } |
156 | ||
0655fa4d | 157 | /* |
158 | * returns true if client specified that the object must come from the cache | |
159 | * without contacting origin server | |
160 | */ | |
161 | bool | |
162 | ClientHttpRequest::onlyIfCached()const | |
163 | { | |
164 | assert(request); | |
165 | return request->cache_control && | |
166 | EBIT_TEST(request->cache_control->mask, CC_ONLY_IF_CACHED); | |
167 | } | |
168 | ||
528b2c61 | 169 | /* |
170 | * This function is designed to serve a fairly specific purpose. | |
171 | * Occasionally our vBNS-connected caches can talk to each other, but not | |
172 | * the rest of the world. Here we try to detect frequent failures which | |
173 | * make the cache unusable (e.g. DNS lookup and connect() failures). If | |
174 | * the failure:success ratio goes above 1.0 then we go into "hit only" | |
175 | * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors | |
176 | * will only fetch HITs from us if they are using the ICP protocol. We | |
177 | * stay in this mode for 5 minutes. | |
178 | * | |
179 | * Duane W., Sept 16, 1996 | |
180 | */ | |
181 | ||
182 | #define FAILURE_MODE_TIME 300 | |
183 | ||
184 | static void | |
185 | checkFailureRatio(err_type etype, hier_code hcode) | |
186 | { | |
187 | static double magic_factor = 100.0; | |
188 | double n_good; | |
189 | double n_bad; | |
62e76326 | 190 | |
528b2c61 | 191 | if (hcode == HIER_NONE) |
62e76326 | 192 | return; |
193 | ||
528b2c61 | 194 | n_good = magic_factor / (1.0 + request_failure_ratio); |
62e76326 | 195 | |
528b2c61 | 196 | n_bad = magic_factor - n_good; |
62e76326 | 197 | |
528b2c61 | 198 | switch (etype) { |
62e76326 | 199 | |
528b2c61 | 200 | case ERR_DNS_FAIL: |
62e76326 | 201 | |
528b2c61 | 202 | case ERR_CONNECT_FAIL: |
3712be3f | 203 | case ERR_SECURE_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 | ||
bf8fe701 | 221 | debugs(33, 0, "Failure Ratio at "<< std::setw(4)<< |
222 | std::setprecision(3) << request_failure_ratio); | |
62e76326 | 223 | |
bf8fe701 | 224 | debugs(33, 0, "Going into hit-only-mode for " << |
225 | FAILURE_MODE_TIME / 60 << " minutes..."); | |
62e76326 | 226 | |
528b2c61 | 227 | hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME; |
62e76326 | 228 | |
528b2c61 | 229 | request_failure_ratio = 0.8; /* reset to something less than 1.0 */ |
230 | } | |
231 | ||
232 | ClientHttpRequest::~ClientHttpRequest() | |
233 | { | |
bf8fe701 | 234 | debugs(33, 3, "httpRequestFree: " << uri); |
72bdee4c | 235 | PROF_start(httpRequestFree); |
62e76326 | 236 | |
5f8252d2 | 237 | // Even though freeResources() below may destroy the request, |
238 | // we no longer set request->body_pipe to NULL here | |
239 | // because we did not initiate that pipe (ConnStateData did) | |
62e76326 | 240 | |
528b2c61 | 241 | /* the ICP check here was erroneous |
d88e3c49 | 242 | * - StoreEntry::releaseRequest was always called if entry was valid |
528b2c61 | 243 | */ |
244 | assert(logType < LOG_TYPE_MAX); | |
9ce7856a | 245 | |
528b2c61 | 246 | logRequest(); |
9ce7856a | 247 | |
0976f8db | 248 | loggingEntry(NULL); |
249 | ||
528b2c61 | 250 | if (request) |
62e76326 | 251 | checkFailureRatio(request->errType, al.hier.code); |
252 | ||
528b2c61 | 253 | freeResources(); |
62e76326 | 254 | |
a83c6ed6 AR |
255 | #if USE_ADAPTATION |
256 | announceInitiatorAbort(virginHeadSource); | |
9d4d7c5e | 257 | |
a83c6ed6 AR |
258 | if (adaptedBodySource != NULL) |
259 | stopConsumingFrom(adaptedBodySource); | |
de31d06f | 260 | #endif |
9ce7856a | 261 | |
de31d06f | 262 | if (calloutContext) |
263 | delete calloutContext; | |
264 | ||
1cf238db | 265 | if(conn_) |
266 | cbdataReferenceDone(conn_); | |
267 | ||
528b2c61 | 268 | /* moving to the next connection is handled by the context free */ |
269 | dlinkDelete(&active, &ClientActiveRequests); | |
9ce7856a | 270 | |
72bdee4c | 271 | PROF_stop(httpRequestFree); |
528b2c61 | 272 | } |
62e76326 | 273 | |
edce4d98 | 274 | /* Create a request and kick it off */ |
69660be0 | 275 | /* |
276 | * TODO: Pass in the buffers to be used in the inital Read request, as they are | |
277 | * determined by the user | |
edce4d98 | 278 | */ |
279 | int /* returns nonzero on failure */ | |
60745f24 | 280 | clientBeginRequest(const HttpRequestMethod& method, char const *url, CSCB * streamcallback, |
0655fa4d | 281 | CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header, |
62e76326 | 282 | char *tailbuf, size_t taillen) |
edce4d98 | 283 | { |
284 | size_t url_sz; | |
450e0c10 | 285 | HttpVersion http_ver (1, 0); |
a0355e95 | 286 | ClientHttpRequest *http = new ClientHttpRequest(NULL); |
190154cf | 287 | HttpRequest *request; |
528b2c61 | 288 | StoreIOBuffer tempBuffer; |
1cf238db | 289 | http->start_time = current_time; |
edce4d98 | 290 | /* this is only used to adjust the connection offset in client_side.c */ |
291 | http->req_sz = 0; | |
c8be6d7b | 292 | tempBuffer.length = taillen; |
293 | tempBuffer.data = tailbuf; | |
edce4d98 | 294 | /* client stream setup */ |
295 | clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach, | |
0655fa4d | 296 | clientReplyStatus, new clientReplyContext(http), streamcallback, |
62e76326 | 297 | streamdetach, streamdata, tempBuffer); |
edce4d98 | 298 | /* make it visible in the 'current acctive requests list' */ |
edce4d98 | 299 | /* Set flags */ |
a46d2c0e | 300 | /* internal requests only makes sense in an |
301 | * accelerator today. TODO: accept flags ? */ | |
302 | http->flags.accel = 1; | |
edce4d98 | 303 | /* allow size for url rewriting */ |
304 | url_sz = strlen(url) + Config.appendDomainLen + 5; | |
e6ccf245 | 305 | http->uri = (char *)xcalloc(url_sz, 1); |
edce4d98 | 306 | strcpy(http->uri, url); |
307 | ||
c21ad0f5 | 308 | if ((request = HttpRequest::CreateFromUrlAndMethod(http->uri, method)) == NULL) { |
bf8fe701 | 309 | debugs(85, 5, "Invalid URL: " << http->uri); |
62e76326 | 310 | return -1; |
edce4d98 | 311 | } |
62e76326 | 312 | |
69660be0 | 313 | /* |
314 | * now update the headers in request with our supplied headers. urLParse | |
315 | * should return a blank header set, but we use Update to be sure of | |
316 | * correctness. | |
edce4d98 | 317 | */ |
318 | if (header) | |
a9925b40 | 319 | request->header.update(header, NULL); |
62e76326 | 320 | |
edce4d98 | 321 | http->log_uri = xstrdup(urlCanonicalClean(request)); |
62e76326 | 322 | |
edce4d98 | 323 | /* http struct now ready */ |
324 | ||
69660be0 | 325 | /* |
326 | * build new header list *? TODO | |
edce4d98 | 327 | */ |
328 | request->flags.accelerated = http->flags.accel; | |
62e76326 | 329 | |
a46d2c0e | 330 | request->flags.internalclient = 1; |
331 | ||
332 | /* this is an internally created | |
333 | * request, not subject to acceleration | |
334 | * target overrides */ | |
69660be0 | 335 | /* |
336 | * FIXME? Do we want to detect and handle internal requests of internal | |
337 | * objects ? | |
338 | */ | |
edce4d98 | 339 | |
340 | /* Internally created requests cannot have bodies today */ | |
341 | request->content_length = 0; | |
62e76326 | 342 | |
cc192b50 | 343 | request->client_addr.SetNoAddr(); |
62e76326 | 344 | |
cc192b50 | 345 | request->my_addr.SetNoAddr(); /* undefined for internal requests */ |
62e76326 | 346 | |
cc192b50 | 347 | request->my_addr.SetPort(0); |
62e76326 | 348 | |
edce4d98 | 349 | request->http_ver = http_ver; |
62e76326 | 350 | |
6dd9f4bd | 351 | http->request = HTTPMSGLOCK(request); |
edce4d98 | 352 | |
353 | /* optional - skip the access check ? */ | |
de31d06f | 354 | http->calloutContext = new ClientRequestContext(http); |
355 | ||
57abaac0 | 356 | http->calloutContext->http_access_done = false; |
de31d06f | 357 | |
57abaac0 | 358 | http->calloutContext->redirect_done = true; |
de31d06f | 359 | |
57abaac0 | 360 | http->calloutContext->no_cache_done = true; |
de31d06f | 361 | |
362 | http->doCallouts(); | |
62e76326 | 363 | |
edce4d98 | 364 | return 0; |
365 | } | |
366 | ||
de31d06f | 367 | bool |
368 | ClientRequestContext::httpStateIsValid() | |
369 | { | |
370 | ClientHttpRequest *http_ = http; | |
371 | ||
372 | if (cbdataReferenceValid(http_)) | |
373 | return true; | |
374 | ||
375 | http = NULL; | |
376 | ||
377 | cbdataReferenceDone(http_); | |
378 | ||
379 | return false; | |
380 | } | |
381 | ||
edce4d98 | 382 | /* This is the entry point for external users of the client_side routines */ |
383 | void | |
de31d06f | 384 | ClientRequestContext::clientAccessCheck() |
edce4d98 | 385 | { |
de31d06f | 386 | acl_checklist = |
62e76326 | 387 | clientAclChecklistCreate(Config.accessList.http, http); |
de31d06f | 388 | acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this); |
edce4d98 | 389 | } |
390 | ||
391 | void | |
de31d06f | 392 | clientAccessCheckDoneWrapper(int answer, void *data) |
edce4d98 | 393 | { |
de31d06f | 394 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; |
fbade053 | 395 | |
de31d06f | 396 | if (!calloutContext->httpStateIsValid()) |
62e76326 | 397 | return; |
62e76326 | 398 | |
de31d06f | 399 | calloutContext->clientAccessCheckDone(answer); |
400 | } | |
401 | ||
402 | void | |
403 | ClientRequestContext::clientAccessCheckDone(int answer) | |
404 | { | |
405 | acl_checklist = NULL; | |
edce4d98 | 406 | err_type page_id; |
407 | http_status status; | |
bf8fe701 | 408 | debugs(85, 2, "The request " << |
60745f24 | 409 | RequestMethodStr(http->request->method) << " " << |
bf8fe701 | 410 | http->uri << " is " << |
411 | (answer == ACCESS_ALLOWED ? "ALLOWED" : "DENIED") << | |
412 | ", because it matched '" << | |
413 | (AclMatchedName ? AclMatchedName : "NO ACL's") << "'" ); | |
f5691f9c | 414 | char const *proxy_auth_msg = "<null>"; |
415 | ||
94a396a3 | 416 | if (http->getConn() != NULL && http->getConn()->auth_user_request != NULL) |
f5691f9c | 417 | proxy_auth_msg = http->getConn()->auth_user_request->denyMessage("<null>"); |
418 | else if (http->request->auth_user_request != NULL) | |
419 | proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>"); | |
62e76326 | 420 | |
de31d06f | 421 | if (answer != ACCESS_ALLOWED) { |
62e76326 | 422 | /* Send an error */ |
9ce7856a | 423 | int require_auth = (answer == ACCESS_REQ_PROXY_AUTH || aclIsProxyAuth(AclMatchedName)); |
bf8fe701 | 424 | debugs(85, 5, "Access Denied: " << http->uri); |
425 | debugs(85, 5, "AclMatchedName = " << (AclMatchedName ? AclMatchedName : "<null>")); | |
9ce7856a | 426 | |
427 | if (require_auth) | |
bf8fe701 | 428 | debugs(33, 5, "Proxy Auth Message = " << (proxy_auth_msg ? proxy_auth_msg : "<null>")); |
9ce7856a | 429 | |
62e76326 | 430 | /* |
431 | * NOTE: get page_id here, based on AclMatchedName because if | |
432 | * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in | |
433 | * the clientCreateStoreEntry() call just below. Pedro Ribeiro | |
434 | * <pribeiro@isel.pt> | |
435 | */ | |
9ce7856a | 436 | page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, answer != ACCESS_REQ_PROXY_AUTH); |
437 | ||
62e76326 | 438 | http->logType = LOG_TCP_DENIED; |
439 | ||
9ce7856a | 440 | if (require_auth) { |
62e76326 | 441 | if (!http->flags.accel) { |
442 | /* Proxy authorisation needed */ | |
443 | status = HTTP_PROXY_AUTHENTICATION_REQUIRED; | |
444 | } else { | |
445 | /* WWW authorisation needed */ | |
446 | status = HTTP_UNAUTHORIZED; | |
447 | } | |
448 | ||
449 | if (page_id == ERR_NONE) | |
450 | page_id = ERR_CACHE_ACCESS_DENIED; | |
451 | } else { | |
452 | status = HTTP_FORBIDDEN; | |
453 | ||
454 | if (page_id == ERR_NONE) | |
455 | page_id = ERR_ACCESS_DENIED; | |
456 | } | |
457 | ||
de31d06f | 458 | clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->prev->data; |
0655fa4d | 459 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
460 | assert (repContext); | |
cc192b50 | 461 | IPAddress tmpnoaddr; tmpnoaddr.SetNoAddr(); |
0655fa4d | 462 | repContext->setReplyToError(page_id, status, |
463 | http->request->method, NULL, | |
cc192b50 | 464 | http->getConn() != NULL ? http->getConn()->peer : tmpnoaddr, |
465 | http->request, | |
466 | NULL, | |
467 | http->getConn() != NULL && http->getConn()->auth_user_request ? | |
468 | http->getConn()->auth_user_request : http->request->auth_user_request); | |
469 | ||
62e76326 | 470 | node = (clientStreamNode *)http->client_stream.tail->data; |
471 | clientStreamRead(node, http, node->readBuffer); | |
a546b04b | 472 | return; |
edce4d98 | 473 | } |
de31d06f | 474 | |
475 | /* ACCESS_ALLOWED continues here ... */ | |
476 | safe_free(http->uri); | |
477 | ||
478 | http->uri = xstrdup(urlCanonical(http->request)); | |
479 | ||
480 | http->doCallouts(); | |
481 | } | |
482 | ||
a83c6ed6 | 483 | #if USE_ADAPTATION |
de31d06f | 484 | void |
a83c6ed6 | 485 | ClientRequestContext::adaptationAccessCheck() |
de31d06f | 486 | { |
a83c6ed6 AR |
487 | Adaptation::AccessCheck *check = new Adaptation::AccessCheck( |
488 | Adaptation::methodReqmod, Adaptation::pointPreCache, | |
489 | http->request, NULL, adaptationAclCheckDoneWrapper, this); | |
de31d06f | 490 | |
a83c6ed6 | 491 | check->check(); // will eventually delete self |
de31d06f | 492 | } |
493 | ||
494 | static void | |
a83c6ed6 | 495 | adaptationAclCheckDoneWrapper(Adaptation::ServicePointer service, void *data) |
de31d06f | 496 | { |
497 | ClientRequestContext *calloutContext = (ClientRequestContext *)data; | |
498 | ||
499 | if (!calloutContext->httpStateIsValid()) | |
500 | return; | |
501 | ||
a83c6ed6 | 502 | calloutContext->adaptationAclCheckDone(service); |
de31d06f | 503 | } |
504 | ||
505 | void | |
a83c6ed6 | 506 | ClientRequestContext::adaptationAclCheckDone(Adaptation::ServicePointer service) |
de31d06f | 507 | { |
a83c6ed6 | 508 | debugs(93,3,HERE << this << " adaptationAclCheckDone called"); |
6ec67de9 | 509 | assert(http); |
510 | ||
a83c6ed6 | 511 | if (http->startAdaptation(service)) |
de31d06f | 512 | return; |
513 | ||
a83c6ed6 | 514 | if (!service || service->cfg().bypass) { |
5f8252d2 | 515 | // handle ICAP start failure when no service was selected |
516 | // or where the selected service was optional | |
517 | http->doCallouts(); | |
518 | return; | |
519 | } | |
de31d06f | 520 | |
5f8252d2 | 521 | // handle start failure for an essential ICAP service |
a83c6ed6 | 522 | http->handleAdaptationFailure(); |
edce4d98 | 523 | } |
524 | ||
de31d06f | 525 | #endif |
526 | ||
14cc8559 | 527 | static void |
528 | clientRedirectAccessCheckDone(int answer, void *data) | |
529 | { | |
530 | ClientRequestContext *context = (ClientRequestContext *)data; | |
59a1efb2 | 531 | ClientHttpRequest *http = context->http; |
14cc8559 | 532 | context->acl_checklist = NULL; |
533 | ||
534 | if (answer == ACCESS_ALLOWED) | |
de31d06f | 535 | redirectStart(http, clientRedirectDoneWrapper, context); |
14cc8559 | 536 | else |
de31d06f | 537 | context->clientRedirectDone(NULL); |
14cc8559 | 538 | } |
539 | ||
de31d06f | 540 | void |
541 | ClientRequestContext::clientRedirectStart() | |
14cc8559 | 542 | { |
bf8fe701 | 543 | debugs(33, 5, "clientRedirectStart: '" << http->uri << "'"); |
14cc8559 | 544 | |
14cc8559 | 545 | if (Config.accessList.redirector) { |
de31d06f | 546 | acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http); |
547 | acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, this); | |
14cc8559 | 548 | } else |
de31d06f | 549 | redirectStart(http, clientRedirectDoneWrapper, this); |
14cc8559 | 550 | } |
551 | ||
edce4d98 | 552 | static int |
59a1efb2 | 553 | clientHierarchical(ClientHttpRequest * http) |
edce4d98 | 554 | { |
555 | const char *url = http->uri; | |
190154cf | 556 | HttpRequest *request = http->request; |
60745f24 | 557 | HttpRequestMethod method = request->method; |
edce4d98 | 558 | const wordlist *p = NULL; |
559 | ||
69660be0 | 560 | /* |
561 | * IMS needs a private key, so we can use the hierarchy for IMS only if our | |
562 | * neighbors support private keys | |
563 | */ | |
62e76326 | 564 | |
edce4d98 | 565 | if (request->flags.ims && !neighbors_do_private_keys) |
62e76326 | 566 | return 0; |
567 | ||
69660be0 | 568 | /* |
569 | * This is incorrect: authenticating requests can be sent via a hierarchy | |
06b97e72 | 570 | * (they can even be cached if the correct headers are set on the reply) |
edce4d98 | 571 | */ |
572 | if (request->flags.auth) | |
62e76326 | 573 | return 0; |
574 | ||
edce4d98 | 575 | if (method == METHOD_TRACE) |
62e76326 | 576 | return 1; |
577 | ||
edce4d98 | 578 | if (method != METHOD_GET) |
62e76326 | 579 | return 0; |
580 | ||
edce4d98 | 581 | /* scan hierarchy_stoplist */ |
582 | for (p = Config.hierarchy_stoplist; p; p = p->next) | |
62e76326 | 583 | if (strstr(url, p->key)) |
584 | return 0; | |
585 | ||
edce4d98 | 586 | if (request->flags.loopdetect) |
62e76326 | 587 | return 0; |
588 | ||
edce4d98 | 589 | if (request->protocol == PROTO_HTTP) |
62e76326 | 590 | return httpCachable(method); |
591 | ||
edce4d98 | 592 | if (request->protocol == PROTO_GOPHER) |
62e76326 | 593 | return gopherCachable(request); |
594 | ||
edce4d98 | 595 | if (request->protocol == PROTO_CACHEOBJ) |
62e76326 | 596 | return 0; |
597 | ||
edce4d98 | 598 | return 1; |
599 | } | |
600 | ||
601 | ||
602 | static void | |
59a1efb2 | 603 | clientInterpretRequestHeaders(ClientHttpRequest * http) |
edce4d98 | 604 | { |
190154cf | 605 | HttpRequest *request = http->request; |
0ef77270 | 606 | HttpHeader *req_hdr = &request->header; |
edce4d98 | 607 | int no_cache = 0; |
f41735ea | 608 | #if !(USE_SQUID_ESI) || defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG) |
62e76326 | 609 | |
edce4d98 | 610 | const char *str; |
611 | #endif | |
62e76326 | 612 | |
edce4d98 | 613 | request->imslen = -1; |
a9925b40 | 614 | request->ims = req_hdr->getTime(HDR_IF_MODIFIED_SINCE); |
62e76326 | 615 | |
edce4d98 | 616 | if (request->ims > 0) |
62e76326 | 617 | request->flags.ims = 1; |
618 | ||
f41735ea | 619 | #if USE_SQUID_ESI |
69660be0 | 620 | /* |
621 | * We ignore Cache-Control as per the Edge Architecture Section 3. See | |
622 | * www.esi.org for more information. | |
edce4d98 | 623 | */ |
624 | #else | |
62e76326 | 625 | |
a9925b40 | 626 | if (req_hdr->has(HDR_PRAGMA)) { |
30abd221 | 627 | String s = req_hdr->getList(HDR_PRAGMA); |
62e76326 | 628 | |
629 | if (strListIsMember(&s, "no-cache", ',')) | |
630 | no_cache++; | |
30abd221 | 631 | |
632 | s.clean(); | |
edce4d98 | 633 | } |
62e76326 | 634 | |
edce4d98 | 635 | if (request->cache_control) |
62e76326 | 636 | if (EBIT_TEST(request->cache_control->mask, CC_NO_CACHE)) |
637 | no_cache++; | |
638 | ||
69660be0 | 639 | /* |
62e76326 | 640 | * Work around for supporting the Reload button in IE browsers when Squid |
641 | * is used as an accelerator or transparent proxy, by turning accelerated | |
642 | * IMS request to no-cache requests. Now knows about IE 5.5 fix (is | |
643 | * actually only fixed in SP1, but we can't tell whether we are talking to | |
644 | * SP1 or not so all 5.5 versions are treated 'normally'). | |
645 | */ | |
edce4d98 | 646 | if (Config.onoff.ie_refresh) { |
62e76326 | 647 | if (http->flags.accel && request->flags.ims) { |
a9925b40 | 648 | if ((str = req_hdr->getStr(HDR_USER_AGENT))) { |
62e76326 | 649 | if (strstr(str, "MSIE 5.01") != NULL) |
650 | no_cache++; | |
651 | else if (strstr(str, "MSIE 5.0") != NULL) | |
652 | no_cache++; | |
653 | else if (strstr(str, "MSIE 4.") != NULL) | |
654 | no_cache++; | |
655 | else if (strstr(str, "MSIE 3.") != NULL) | |
656 | no_cache++; | |
657 | } | |
658 | } | |
edce4d98 | 659 | } |
914b89a2 | 660 | |
661 | if (request->method == METHOD_OTHER) { | |
60745f24 | 662 | no_cache++; |
663 | } | |
62e76326 | 664 | |
edce4d98 | 665 | #endif |
666 | if (no_cache) { | |
667 | #if HTTP_VIOLATIONS | |
62e76326 | 668 | |
669 | if (Config.onoff.reload_into_ims) | |
670 | request->flags.nocache_hack = 1; | |
671 | else if (refresh_nocache_hack) | |
672 | request->flags.nocache_hack = 1; | |
673 | else | |
edce4d98 | 674 | #endif |
62e76326 | 675 | |
676 | request->flags.nocache = 1; | |
edce4d98 | 677 | } |
62e76326 | 678 | |
0ef77270 | 679 | /* ignore range header in non-GETs or non-HEADs */ |
680 | if (request->method == METHOD_GET || request->method == METHOD_HEAD) { | |
a9925b40 | 681 | request->range = req_hdr->getRange(); |
62e76326 | 682 | |
683 | if (request->range) { | |
684 | request->flags.range = 1; | |
685 | clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data; | |
686 | /* XXX: This is suboptimal. We should give the stream the range set, | |
687 | * and thereby let the top of the stream set the offset when the | |
688 | * size becomes known. As it is, we will end up requesting from 0 | |
689 | * for evey -X range specification. | |
690 | * RBC - this may be somewhat wrong. We should probably set the range | |
691 | * iter up at this point. | |
692 | */ | |
693 | node->readBuffer.offset = request->range->lowestOffset(0); | |
694 | http->range_iter.pos = request->range->begin(); | |
695 | http->range_iter.valid = true; | |
696 | } | |
edce4d98 | 697 | } |
62e76326 | 698 | |
0ef77270 | 699 | /* Only HEAD and GET requests permit a Range or Request-Range header. |
700 | * If these headers appear on any other type of request, delete them now. | |
701 | */ | |
702 | else { | |
703 | req_hdr->delById(HDR_RANGE); | |
704 | req_hdr->delById(HDR_REQUEST_RANGE); | |
705 | request->range = NULL; | |
706 | } | |
707 | ||
a9925b40 | 708 | if (req_hdr->has(HDR_AUTHORIZATION)) |
62e76326 | 709 | request->flags.auth = 1; |
710 | ||
edce4d98 | 711 | if (request->login[0] != '\0') |
62e76326 | 712 | request->flags.auth = 1; |
713 | ||
a9925b40 | 714 | if (req_hdr->has(HDR_VIA)) { |
30abd221 | 715 | String s = req_hdr->getList(HDR_VIA); |
62e76326 | 716 | /* |
717 | * ThisCache cannot be a member of Via header, "1.0 ThisCache" can. | |
718 | * Note ThisCache2 has a space prepended to the hostname so we don't | |
719 | * accidentally match super-domains. | |
720 | */ | |
721 | ||
722 | if (strListIsSubstr(&s, ThisCache2, ',')) { | |
723 | debugObj(33, 1, "WARNING: Forwarding loop detected for:\n", | |
724 | request, (ObjPackMethod) & httpRequestPack); | |
725 | request->flags.loopdetect = 1; | |
726 | } | |
727 | ||
edce4d98 | 728 | #if FORW_VIA_DB |
30abd221 | 729 | fvdbCountVia(s.buf()); |
62e76326 | 730 | |
edce4d98 | 731 | #endif |
62e76326 | 732 | |
30abd221 | 733 | s.clean(); |
edce4d98 | 734 | } |
62e76326 | 735 | |
5ad35449 | 736 | /** |
737 | \todo --enable-useragent-log and --enable-referer-log. We should | |
738 | probably drop those two as the custom log formats accomplish pretty much the same thing.. | |
739 | */ | |
edce4d98 | 740 | #if USE_USERAGENT_LOG |
a9925b40 | 741 | if ((str = req_hdr->getStr(HDR_USER_AGENT))) |
b4306cba | 742 | logUserAgent(fqdnFromAddr(http->getConn()->log_addr), str); |
62e76326 | 743 | |
edce4d98 | 744 | #endif |
745 | #if USE_REFERER_LOG | |
62e76326 | 746 | |
a9925b40 | 747 | if ((str = req_hdr->getStr(HDR_REFERER))) |
b4306cba | 748 | logReferer(fqdnFromAddr(http->getConn()->log_addr), str, http->log_uri); |
62e76326 | 749 | |
edce4d98 | 750 | #endif |
751 | #if FORW_VIA_DB | |
62e76326 | 752 | |
a9925b40 | 753 | if (req_hdr->has(HDR_X_FORWARDED_FOR)) { |
30abd221 | 754 | String s = req_hdr->getList(HDR_X_FORWARDED_FOR); |
755 | fvdbCountForw(s.buf()); | |
756 | s.clean(); | |
edce4d98 | 757 | } |
62e76326 | 758 | |
edce4d98 | 759 | #endif |
760 | if (request->method == METHOD_TRACE) { | |
a9925b40 | 761 | request->max_forwards = req_hdr->getInt(HDR_MAX_FORWARDS); |
edce4d98 | 762 | } |
62e76326 | 763 | |
610ee341 | 764 | request->flags.cachable = http->request->cacheable(); |
62e76326 | 765 | |
edce4d98 | 766 | if (clientHierarchical(http)) |
62e76326 | 767 | request->flags.hierarchical = 1; |
768 | ||
bf8fe701 | 769 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_NOCACHE = " << |
770 | (request->flags.nocache ? "SET" : "NOT SET")); | |
771 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_CACHABLE = " << | |
772 | (request->flags.cachable ? "SET" : "NOT SET")); | |
773 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_HIERARCHICAL = " << | |
774 | (request->flags.hierarchical ? "SET" : "NOT SET")); | |
62e76326 | 775 | |
edce4d98 | 776 | } |
777 | ||
778 | void | |
de31d06f | 779 | clientRedirectDoneWrapper(void *data, char *result) |
edce4d98 | 780 | { |
de31d06f | 781 | ClientRequestContext *calloutContext = (ClientRequestContext *)data; |
db02222f | 782 | |
de31d06f | 783 | if (!calloutContext->httpStateIsValid()) |
62e76326 | 784 | return; |
62e76326 | 785 | |
de31d06f | 786 | calloutContext->clientRedirectDone(result); |
787 | } | |
788 | ||
789 | void | |
790 | ClientRequestContext::clientRedirectDone(char *result) | |
791 | { | |
190154cf | 792 | HttpRequest *new_request = NULL; |
793 | HttpRequest *old_request = http->request; | |
bf8fe701 | 794 | debugs(85, 5, "clientRedirectDone: '" << http->uri << "' result=" << (result ? result : "NULL")); |
de31d06f | 795 | assert(redirect_state == REDIRECT_PENDING); |
796 | redirect_state = REDIRECT_DONE; | |
62e76326 | 797 | |
edce4d98 | 798 | if (result) { |
62e76326 | 799 | http_status status = (http_status) atoi(result); |
800 | ||
801 | if (status == HTTP_MOVED_PERMANENTLY | |
802 | || status == HTTP_MOVED_TEMPORARILY | |
803 | || status == HTTP_SEE_OTHER | |
804 | || status == HTTP_TEMPORARY_REDIRECT) { | |
805 | char *t = result; | |
806 | ||
807 | if ((t = strchr(result, ':')) != NULL) { | |
808 | http->redirect.status = status; | |
809 | http->redirect.location = xstrdup(t + 1); | |
810 | } else { | |
bf8fe701 | 811 | debugs(85, 1, "clientRedirectDone: bad input: " << result); |
62e76326 | 812 | } |
2baf58c3 | 813 | } else if (strcmp(result, http->uri)) |
c21ad0f5 | 814 | new_request = HttpRequest::CreateFromUrlAndMethod(result, old_request->method); |
edce4d98 | 815 | } |
62e76326 | 816 | |
edce4d98 | 817 | if (new_request) { |
62e76326 | 818 | safe_free(http->uri); |
819 | http->uri = xstrdup(urlCanonical(new_request)); | |
820 | new_request->http_ver = old_request->http_ver; | |
a9925b40 | 821 | new_request->header.append(&old_request->header); |
62e76326 | 822 | new_request->client_addr = old_request->client_addr; |
823 | new_request->my_addr = old_request->my_addr; | |
62e76326 | 824 | new_request->flags = old_request->flags; |
3c1f01bc | 825 | new_request->flags.redirected = 1; |
62e76326 | 826 | |
827 | if (old_request->auth_user_request) { | |
828 | new_request->auth_user_request = old_request->auth_user_request; | |
4f0ef8e8 | 829 | AUTHUSERREQUESTLOCK(new_request->auth_user_request, "new request"); |
62e76326 | 830 | } |
831 | ||
5f8252d2 | 832 | if (old_request->body_pipe != NULL) { |
833 | new_request->body_pipe = old_request->body_pipe; | |
834 | old_request->body_pipe = NULL; | |
835 | debugs(0,0,HERE << "redirecting body_pipe " << new_request->body_pipe << " from request " << old_request << " to " << new_request); | |
62e76326 | 836 | } |
837 | ||
838 | new_request->content_length = old_request->content_length; | |
abb929f0 | 839 | new_request->extacl_user = old_request->extacl_user; |
840 | new_request->extacl_passwd = old_request->extacl_passwd; | |
62e76326 | 841 | new_request->flags.proxy_keepalive = old_request->flags.proxy_keepalive; |
6dd9f4bd | 842 | HTTPMSGUNLOCK(old_request); |
843 | http->request = HTTPMSGLOCK(new_request); | |
edce4d98 | 844 | } |
62e76326 | 845 | |
edce4d98 | 846 | /* FIXME PIPELINE: This is innacurate during pipelining */ |
62e76326 | 847 | |
5f8252d2 | 848 | if (http->getConn() != NULL) |
98242069 | 849 | fd_note(http->getConn()->fd, http->uri); |
62e76326 | 850 | |
c8be6d7b | 851 | assert(http->uri); |
62e76326 | 852 | |
de31d06f | 853 | http->doCallouts(); |
edce4d98 | 854 | } |
855 | ||
856 | void | |
8e2745f4 | 857 | ClientRequestContext::checkNoCache() |
edce4d98 | 858 | { |
de31d06f | 859 | acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http); |
57abaac0 | 860 | acl_checklist->nonBlockingCheck(checkNoCacheDoneWrapper, this); |
edce4d98 | 861 | } |
862 | ||
de31d06f | 863 | static void |
864 | checkNoCacheDoneWrapper(int answer, void *data) | |
edce4d98 | 865 | { |
de31d06f | 866 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; |
e4a67a80 | 867 | |
de31d06f | 868 | if (!calloutContext->httpStateIsValid()) |
869 | return; | |
870 | ||
871 | calloutContext->checkNoCacheDone(answer); | |
8e2745f4 | 872 | } |
4fb35c3c | 873 | |
8e2745f4 | 874 | void |
875 | ClientRequestContext::checkNoCacheDone(int answer) | |
62e76326 | 876 | { |
8e2745f4 | 877 | acl_checklist = NULL; |
de31d06f | 878 | http->request->flags.cachable = answer; |
879 | http->doCallouts(); | |
edce4d98 | 880 | } |
881 | ||
69660be0 | 882 | /* |
883 | * Identify requests that do not go through the store and client side stream | |
884 | * and forward them to the appropriate location. All other requests, request | |
885 | * them. | |
edce4d98 | 886 | */ |
887 | void | |
8e2745f4 | 888 | ClientHttpRequest::processRequest() |
edce4d98 | 889 | { |
60745f24 | 890 | debugs(85, 4, "clientProcessRequest: " << RequestMethodStr(request->method) << " '" << uri << "'"); |
62e76326 | 891 | |
3712be3f | 892 | #if USE_SSL |
893 | if (request->method == METHOD_CONNECT && sslBumpNeeded()) { | |
894 | sslBumpStart(); | |
895 | return; | |
896 | } | |
897 | #endif | |
898 | ||
2baf58c3 | 899 | if (request->method == METHOD_CONNECT && !redirect.status) { |
62e76326 | 900 | logType = LOG_TCP_MISS; |
11007d4b | 901 | tunnelStart(this, &out.size, &al.http.code); |
62e76326 | 902 | return; |
edce4d98 | 903 | } |
62e76326 | 904 | |
8e2745f4 | 905 | httpStart(); |
906 | } | |
907 | ||
908 | void | |
909 | ClientHttpRequest::httpStart() | |
910 | { | |
559da936 | 911 | PROF_start(httpStart); |
8e2745f4 | 912 | logType = LOG_TAG_NONE; |
bf8fe701 | 913 | debugs(85, 4, "ClientHttpRequest::httpStart: " << log_tags[logType] << " for '" << uri << "'"); |
914 | ||
edce4d98 | 915 | /* no one should have touched this */ |
8e2745f4 | 916 | assert(out.offset == 0); |
edce4d98 | 917 | /* Use the Stream Luke */ |
8e2745f4 | 918 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->data; |
919 | clientStreamRead(node, this, node->readBuffer); | |
559da936 | 920 | PROF_stop(httpStart); |
edce4d98 | 921 | } |
0655fa4d | 922 | |
3712be3f | 923 | #if USE_SSL |
924 | ||
925 | // determines whether we should bump the CONNECT request | |
926 | bool | |
927 | ClientHttpRequest::sslBumpNeeded() const { | |
928 | if (!getConn()->port->sslBump || !Config.accessList.ssl_bump) | |
929 | return false; | |
930 | ||
931 | debugs(85, 5, HERE << "SslBump possible, checking ACL"); | |
932 | ||
933 | ACLChecklist check; | |
934 | check.src_addr = request->client_addr; | |
935 | check.my_addr = request->my_addr; | |
936 | check.request = HTTPMSGLOCK(request); | |
937 | check.accessList = cbdataReference(Config.accessList.ssl_bump); | |
938 | /* cbdataReferenceDone() happens in either fastCheck() or ~ACLCheckList */ | |
939 | return check.fastCheck() == 1; | |
940 | } | |
941 | ||
942 | // called when comm_write has completed | |
943 | static void | |
944 | SslBumpEstablish(int, char *, size_t, comm_err_t errflag, int, void *data) | |
945 | { | |
946 | ClientHttpRequest *r = static_cast<ClientHttpRequest*>(data); | |
947 | debugs(85, 5, HERE << "responded to CONNECT: " << r << " ? " << errflag); | |
948 | ||
949 | assert(r && cbdataReferenceValid(r)); | |
950 | r->sslBumpEstablish(errflag); | |
951 | } | |
952 | ||
953 | void | |
954 | ClientHttpRequest::sslBumpEstablish(comm_err_t errflag) | |
955 | { | |
956 | // Bail out quickly on COMM_ERR_CLOSING - close handlers will tidy up | |
957 | if (errflag == COMM_ERR_CLOSING) | |
958 | return; | |
959 | ||
960 | if (errflag) { | |
961 | getConn()->startClosing("CONNECT response failure in SslBump"); | |
962 | return; | |
963 | } | |
964 | ||
965 | getConn()->switchToHttps(); | |
966 | } | |
967 | ||
968 | void | |
969 | ClientHttpRequest::sslBumpStart() | |
970 | { | |
971 | debugs(85, 5, HERE << "ClientHttpRequest::sslBumpStart"); | |
972 | ||
973 | // send an HTTP 200 response to kick client SSL negotiation | |
974 | const int fd = getConn()->fd; | |
975 | debugs(33, 7, HERE << "Confirming CONNECT tunnel on FD " << fd); | |
976 | ||
977 | // TODO: Unify with tunnel.cc and add a Server(?) header | |
978 | static const char *const conn_established = | |
979 | "HTTP/1.0 200 Connection established\r\n\r\n"; | |
980 | comm_write(fd, conn_established, strlen(conn_established), | |
981 | &SslBumpEstablish, this, NULL); | |
982 | } | |
983 | ||
984 | #endif | |
985 | ||
0655fa4d | 986 | bool |
987 | ClientHttpRequest::gotEnough() const | |
988 | { | |
86a2f789 | 989 | /** TODO: should be querying the stream. */ |
7173d5b0 | 990 | int64_t contentLength = |
06a5ae20 | 991 | memObject()->getReply()->bodySize(request->method); |
0655fa4d | 992 | assert(contentLength >= 0); |
993 | ||
994 | if (out.offset < contentLength) | |
995 | return false; | |
996 | ||
997 | return true; | |
998 | } | |
999 | ||
86a2f789 | 1000 | void |
1001 | ClientHttpRequest::storeEntry(StoreEntry *newEntry) | |
1002 | { | |
1003 | entry_ = newEntry; | |
1004 | } | |
1005 | ||
0976f8db | 1006 | void |
1007 | ClientHttpRequest::loggingEntry(StoreEntry *newEntry) | |
1008 | { | |
1009 | if (loggingEntry_) | |
97b5e68f | 1010 | loggingEntry_->unlock(); |
0976f8db | 1011 | |
1012 | loggingEntry_ = newEntry; | |
1013 | ||
1014 | if (loggingEntry_) | |
3d0ac046 | 1015 | loggingEntry_->lock(); |
0976f8db | 1016 | } |
86a2f789 | 1017 | |
de31d06f | 1018 | /* |
1019 | * doCallouts() - This function controls the order of "callout" | |
1020 | * executions, including non-blocking access control checks, the | |
1021 | * redirector, and ICAP. Previously, these callouts were chained | |
1022 | * together such that "clientAccessCheckDone()" would call | |
1023 | * "clientRedirectStart()" and so on. | |
1024 | * | |
1025 | * The ClientRequestContext (aka calloutContext) class holds certain | |
1026 | * state data for the callout/callback operations. Previously | |
1027 | * ClientHttpRequest would sort of hand off control to ClientRequestContext | |
1028 | * for a short time. ClientRequestContext would then delete itself | |
1029 | * and pass control back to ClientHttpRequest when all callouts | |
1030 | * were finished. | |
1031 | * | |
1032 | * This caused some problems for ICAP because we want to make the | |
1033 | * ICAP callout after checking ACLs, but before checking the no_cache | |
1034 | * list. We can't stuff the ICAP state into the ClientRequestContext | |
1035 | * class because we still need the ICAP state after ClientRequestContext | |
1036 | * goes away. | |
1037 | * | |
1038 | * Note that ClientRequestContext is created before the first call | |
1039 | * to doCallouts(). | |
1040 | * | |
1041 | * If one of the callouts notices that ClientHttpRequest is no | |
1042 | * longer valid, it should call cbdataReferenceDone() so that | |
1043 | * ClientHttpRequest's reference count goes to zero and it will get | |
1044 | * deleted. ClientHttpRequest will then delete ClientRequestContext. | |
1045 | * | |
1046 | * Note that we set the _done flags here before actually starting | |
1047 | * the callout. This is strictly for convenience. | |
1048 | */ | |
1049 | ||
057f5854 | 1050 | extern int aclMapTOS (acl_tos * head, ACLChecklist * ch); |
1051 | ||
de31d06f | 1052 | void |
1053 | ClientHttpRequest::doCallouts() | |
1054 | { | |
1055 | assert(calloutContext); | |
1056 | ||
1057 | if (!calloutContext->http_access_done) { | |
3712be3f | 1058 | debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck()"); |
57abaac0 | 1059 | calloutContext->http_access_done = true; |
de31d06f | 1060 | calloutContext->clientAccessCheck(); |
1061 | return; | |
1062 | } | |
1063 | ||
a83c6ed6 AR |
1064 | #if USE_ADAPTATION |
1065 | if (TheICAPConfig.onoff && !calloutContext->adaptation_acl_check_done) { | |
1066 | debugs(83, 3, HERE << "Doing calloutContext->adaptationAccessCheck()"); | |
1067 | calloutContext->adaptation_acl_check_done = true; | |
1068 | calloutContext->adaptationAccessCheck(); | |
de31d06f | 1069 | return; |
1070 | } | |
1071 | ||
1072 | #endif | |
1073 | ||
1074 | if (!calloutContext->redirect_done) { | |
57abaac0 | 1075 | calloutContext->redirect_done = true; |
de31d06f | 1076 | assert(calloutContext->redirect_state == REDIRECT_NONE); |
1077 | ||
1078 | if (Config.Program.redirect) { | |
3712be3f | 1079 | debugs(83, 3, HERE << "Doing calloutContext->clientRedirectStart()"); |
de31d06f | 1080 | calloutContext->redirect_state = REDIRECT_PENDING; |
1081 | calloutContext->clientRedirectStart(); | |
1082 | return; | |
1083 | } | |
1084 | } | |
1085 | ||
57abaac0 | 1086 | if (!calloutContext->interpreted_req_hdrs) { |
3712be3f | 1087 | debugs(83, 3, HERE << "Doing clientInterpretRequestHeaders()"); |
57abaac0 | 1088 | calloutContext->interpreted_req_hdrs = 1; |
1089 | clientInterpretRequestHeaders(this); | |
1090 | } | |
1091 | ||
de31d06f | 1092 | if (!calloutContext->no_cache_done) { |
57abaac0 | 1093 | calloutContext->no_cache_done = true; |
de31d06f | 1094 | |
1095 | if (Config.accessList.noCache && request->flags.cachable) { | |
3712be3f | 1096 | debugs(83, 3, HERE << "Doing calloutContext->checkNoCache()"); |
de31d06f | 1097 | calloutContext->checkNoCache(); |
1098 | return; | |
1099 | } | |
1100 | } | |
1101 | ||
057f5854 | 1102 | if (!calloutContext->clientside_tos_done) { |
1103 | calloutContext->clientside_tos_done = true; | |
3712be3f | 1104 | if (getConn() != NULL) { |
1105 | ACLChecklist ch; | |
057f5854 | 1106 | ch.src_addr = request->client_addr; |
1107 | ch.my_addr = request->my_addr; | |
057f5854 | 1108 | ch.request = HTTPMSGLOCK(request); |
3712be3f | 1109 | int tos = aclMapTOS(Config.accessList.clientside_tos, &ch); |
1110 | if (tos) | |
1111 | comm_set_tos(getConn()->fd, tos); | |
1112 | } | |
057f5854 | 1113 | } |
1114 | ||
de31d06f | 1115 | cbdataReferenceDone(calloutContext->http); |
1116 | delete calloutContext; | |
1117 | calloutContext = NULL; | |
de31d06f | 1118 | #if HEADERS_LOG |
1119 | ||
1120 | headersLog(0, 1, request->method, request); | |
1121 | #endif | |
1122 | ||
58d7150d | 1123 | debugs(83, 3, HERE << "calling processRequest()"); |
de31d06f | 1124 | processRequest(); |
1125 | } | |
1126 | ||
86a2f789 | 1127 | #ifndef _USE_INLINE_ |
1128 | #include "client_side_request.cci" | |
1129 | #endif | |
de31d06f | 1130 | |
a83c6ed6 | 1131 | #if USE_ADAPTATION |
de31d06f | 1132 | /* |
5f8252d2 | 1133 | * Initiate an ICAP transaction. Return false on errors. |
1134 | * The caller must handle errors. | |
de31d06f | 1135 | */ |
5f8252d2 | 1136 | bool |
a83c6ed6 | 1137 | ClientHttpRequest::startAdaptation(Adaptation::ServicePointer service) |
3b299123 | 1138 | { |
a83c6ed6 | 1139 | debugs(85, 3, HERE << this << " ClientHttpRequest::startAdaptation() called"); |
5f8252d2 | 1140 | if (!service) { |
a83c6ed6 | 1141 | debugs(85, 3, "ClientHttpRequest::startAdaptation fails: lack of service"); |
5f8252d2 | 1142 | return false; |
3b299123 | 1143 | } |
5f8252d2 | 1144 | if (service->broken()) { |
a83c6ed6 | 1145 | debugs(85, 3, "ClientHttpRequest::startAdaptation fails: broken service"); |
5f8252d2 | 1146 | return false; |
3b299123 | 1147 | } |
1148 | ||
a83c6ed6 AR |
1149 | assert(!virginHeadSource); |
1150 | assert(!adaptedBodySource); | |
1151 | virginHeadSource = initiateAdaptation(service->makeXactLauncher( | |
1152 | this, request, NULL)); | |
1153 | ||
1154 | return virginHeadSource != NULL; | |
de31d06f | 1155 | } |
1156 | ||
1157 | void | |
a83c6ed6 | 1158 | ClientHttpRequest::noteAdaptationAnswer(HttpMsg *msg) |
de31d06f | 1159 | { |
de31d06f | 1160 | assert(cbdataReferenceValid(this)); // indicates bug |
5f8252d2 | 1161 | assert(msg); |
1162 | ||
b044675d | 1163 | if (HttpRequest *new_req = dynamic_cast<HttpRequest*>(msg)) { |
200ac359 | 1164 | /* |
5f8252d2 | 1165 | * Replace the old request with the new request. |
200ac359 | 1166 | */ |
6dd9f4bd | 1167 | HTTPMSGUNLOCK(request); |
1168 | request = HTTPMSGLOCK(new_req); | |
200ac359 | 1169 | /* |
1170 | * Store the new URI for logging | |
1171 | */ | |
1172 | xfree(uri); | |
1173 | uri = xstrdup(urlCanonical(request)); | |
1174 | setLogUri(this, urlCanonicalClean(request)); | |
914b89a2 | 1175 | assert(request->method.id()); |
b044675d | 1176 | } else if (HttpReply *new_rep = dynamic_cast<HttpReply*>(msg)) { |
1177 | debugs(85,3,HERE << "REQMOD reply is HTTP reply"); | |
1178 | ||
5f8252d2 | 1179 | // subscribe to receive reply body |
1180 | if (new_rep->body_pipe != NULL) { | |
a83c6ed6 AR |
1181 | adaptedBodySource = new_rep->body_pipe; |
1182 | assert(adaptedBodySource->setConsumerIfNotLate(this)); | |
5f8252d2 | 1183 | } |
1184 | ||
b044675d | 1185 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
1186 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
1187 | repContext->createStoreEntry(request->method, request->flags); | |
1188 | ||
1189 | EBIT_CLR(storeEntry()->flags, ENTRY_FWD_HDR_WAIT); | |
1190 | request_satisfaction_mode = true; | |
1191 | request_satisfaction_offset = 0; | |
1192 | storeEntry()->replaceHttpReply(new_rep); | |
cb4c4288 | 1193 | |
a83c6ed6 | 1194 | if (!adaptedBodySource) // no body |
cb4c4288 | 1195 | storeEntry()->complete(); |
b044675d | 1196 | clientGetMoreData(node, this); |
200ac359 | 1197 | } |
de31d06f | 1198 | |
5f8252d2 | 1199 | // we are done with getting headers (but may be receiving body) |
a83c6ed6 | 1200 | clearAdaptation(virginHeadSource); |
5f8252d2 | 1201 | |
b044675d | 1202 | if (!request_satisfaction_mode) |
1203 | doCallouts(); | |
de31d06f | 1204 | } |
1205 | ||
1206 | void | |
a83c6ed6 | 1207 | ClientHttpRequest::noteAdaptationQueryAbort(bool final) |
de31d06f | 1208 | { |
a83c6ed6 AR |
1209 | clearAdaptation(virginHeadSource); |
1210 | assert(!adaptedBodySource); | |
1211 | handleAdaptationFailure(!final); | |
de31d06f | 1212 | } |
1213 | ||
1214 | void | |
1cf238db | 1215 | ClientHttpRequest::noteMoreBodyDataAvailable(BodyPipe::Pointer) |
de31d06f | 1216 | { |
5f8252d2 | 1217 | assert(request_satisfaction_mode); |
a83c6ed6 | 1218 | assert(adaptedBodySource != NULL); |
5f8252d2 | 1219 | |
a83c6ed6 AR |
1220 | if (const size_t contentSize = adaptedBodySource->buf().contentSize()) { |
1221 | BodyPipeCheckout bpc(*adaptedBodySource); | |
5f8252d2 | 1222 | const StoreIOBuffer ioBuf(&bpc.buf, request_satisfaction_offset); |
1223 | storeEntry()->write(ioBuf); | |
1224 | // assume can write everything | |
1225 | request_satisfaction_offset += contentSize; | |
1226 | bpc.buf.consume(contentSize); | |
1227 | bpc.checkIn(); | |
1228 | } | |
1229 | ||
a83c6ed6 | 1230 | if (adaptedBodySource->exhausted()) |
5f8252d2 | 1231 | endRequestSatisfaction(); |
1232 | // else wait for more body data | |
de31d06f | 1233 | } |
1234 | ||
1235 | void | |
1cf238db | 1236 | ClientHttpRequest::noteBodyProductionEnded(BodyPipe::Pointer) |
de31d06f | 1237 | { |
a83c6ed6 AR |
1238 | assert(!virginHeadSource); |
1239 | if (adaptedBodySource != NULL) { // did not end request satisfaction yet | |
5f8252d2 | 1240 | // We do not expect more because noteMoreBodyDataAvailable always |
1241 | // consumes everything. We do not even have a mechanism to consume | |
1242 | // leftovers after noteMoreBodyDataAvailable notifications seize. | |
a83c6ed6 | 1243 | assert(adaptedBodySource->exhausted()); |
5f8252d2 | 1244 | endRequestSatisfaction(); |
1245 | } | |
1246 | } | |
3b299123 | 1247 | |
5f8252d2 | 1248 | void |
1249 | ClientHttpRequest::endRequestSatisfaction() { | |
1250 | debugs(85,4, HERE << this << " ends request satisfaction"); | |
1251 | assert(request_satisfaction_mode); | |
a83c6ed6 | 1252 | stopConsumingFrom(adaptedBodySource); |
3b299123 | 1253 | |
5f8252d2 | 1254 | // TODO: anything else needed to end store entry formation correctly? |
1255 | storeEntry()->complete(); | |
1256 | } | |
de31d06f | 1257 | |
5f8252d2 | 1258 | void |
1cf238db | 1259 | ClientHttpRequest::noteBodyProducerAborted(BodyPipe::Pointer) |
5f8252d2 | 1260 | { |
a83c6ed6 AR |
1261 | assert(!virginHeadSource); |
1262 | stopConsumingFrom(adaptedBodySource); | |
1263 | handleAdaptationFailure(); | |
5f8252d2 | 1264 | } |
3b299123 | 1265 | |
5f8252d2 | 1266 | void |
a83c6ed6 | 1267 | ClientHttpRequest::handleAdaptationFailure(bool bypassable) |
5f8252d2 | 1268 | { |
a83c6ed6 | 1269 | debugs(85,3, HERE << "handleAdaptationFailure(" << bypassable << ")"); |
3b299123 | 1270 | |
5f8252d2 | 1271 | const bool usedStore = storeEntry() && !storeEntry()->isEmpty(); |
1272 | const bool usedPipe = request->body_pipe != NULL && | |
1273 | request->body_pipe->consumedSize() > 0; | |
3b299123 | 1274 | |
9d4d7c5e | 1275 | if (bypassable && !usedStore && !usedPipe) { |
1276 | debugs(85,3, HERE << "ICAP REQMOD callout failed, bypassing: " << calloutContext); | |
5f8252d2 | 1277 | if (calloutContext) |
1278 | doCallouts(); | |
1279 | return; | |
1280 | } | |
3b299123 | 1281 | |
5f8252d2 | 1282 | debugs(85,3, HERE << "ICAP REQMOD callout failed, responding with error"); |
3b299123 | 1283 | |
5f8252d2 | 1284 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
1285 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
1286 | assert(repContext); | |
de31d06f | 1287 | |
5f8252d2 | 1288 | // The original author of the code also wanted to pass an errno to |
1289 | // setReplyToError, but it seems unlikely that the errno reflects the | |
1290 | // true cause of the error at this point, so I did not pass it. | |
b70ba605 | 1291 | IPAddress noAddr; |
1292 | noAddr.SetNoAddr(); | |
1cf238db | 1293 | ConnStateData * c = getConn(); |
5f8252d2 | 1294 | repContext->setReplyToError(ERR_ICAP_FAILURE, HTTP_INTERNAL_SERVER_ERROR, |
1295 | request->method, NULL, | |
b70ba605 | 1296 | (c != NULL ? c->peer : noAddr), request, NULL, |
5f8252d2 | 1297 | (c != NULL && c->auth_user_request ? |
1298 | c->auth_user_request : request->auth_user_request)); | |
de31d06f | 1299 | |
5f8252d2 | 1300 | node = (clientStreamNode *)client_stream.tail->data; |
1301 | clientStreamRead(node, this, node->readBuffer); | |
de31d06f | 1302 | } |
1303 | ||
1304 | #endif |