]>
Commit | Line | Data |
---|---|---|
edce4d98 | 1 | |
2 | /* | |
262a0e14 | 3 | * $Id$ |
26ac0430 | 4 | * |
ae45c4de | 5 | * DEBUG: section 85 Client-side Request Routines |
6 | * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c) | |
26ac0430 | 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: | |
26ac0430 | 39 | * |
69660be0 | 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 | ||
f7f3304a | 45 | #include "squid-old.h" |
c0941a6a AR |
46 | #include "acl/FilledChecklist.h" |
47 | #include "acl/Gadgets.h" | |
a83c6ed6 | 48 | #if USE_ADAPTATION |
62c7f90e | 49 | #include "adaptation/AccessCheck.h" |
1adcebc3 | 50 | #include "adaptation/Answer.h" |
a22e6cd3 | 51 | #include "adaptation/Iterator.h" |
abd4b611 | 52 | #include "adaptation/Service.h" |
3ff65596 AR |
53 | #if ICAP_CLIENT |
54 | #include "adaptation/icap/History.h" | |
55 | #endif | |
de31d06f | 56 | #endif |
65d448bc | 57 | #include "anyp/PortCfg.h" |
2f1431ea | 58 | #if USE_AUTH |
27bc2077 | 59 | #include "auth/UserRequest.h" |
2f1431ea | 60 | #endif |
27bc2077 AJ |
61 | #include "clientStream.h" |
62 | #include "client_side.h" | |
63 | #include "client_side_reply.h" | |
64 | #include "client_side_request.h" | |
65 | #include "ClientRequestContext.h" | |
5c336a3b | 66 | #include "comm/Connection.h" |
ec41b64c | 67 | #include "comm/Write.h" |
27bc2077 | 68 | #include "compat/inet_pton.h" |
2bd84e5f | 69 | #include "errorpage.h" |
27bc2077 | 70 | #include "fde.h" |
31971e6a | 71 | #include "format/Token.h" |
ce394734 | 72 | #include "HttpHdrCc.h" |
27bc2077 AJ |
73 | #include "HttpReply.h" |
74 | #include "HttpRequest.h" | |
425de4c8 | 75 | #include "ip/QosConfig.h" |
27bc2077 | 76 | #include "MemObject.h" |
27bc2077 AJ |
77 | #include "Store.h" |
78 | #include "SquidTime.h" | |
79 | #include "wordlist.h" | |
64b66b76 | 80 | #include "err_detail_type.h" |
4db984be CT |
81 | #if USE_SSL |
82 | #include "ssl/support.h" | |
2bd84e5f | 83 | #include "ssl/ServerBump.h" |
4db984be | 84 | #endif |
3ff65596 AR |
85 | |
86 | ||
edce4d98 | 87 | #if LINGERING_CLOSE |
88 | #define comm_close comm_lingering_close | |
89 | #endif | |
90 | ||
91 | static const char *const crlf = "\r\n"; | |
92 | ||
609c620a | 93 | #if FOLLOW_X_FORWARDED_FOR |
2efeb0b7 | 94 | static void clientFollowXForwardedForCheck(allow_t answer, void *data); |
609c620a | 95 | #endif /* FOLLOW_X_FORWARDED_FOR */ |
3d674977 | 96 | |
2bd84e5f CT |
97 | extern ErrorState *clientBuildError(err_type, http_status, char const *url, Ip::Address &, HttpRequest *); |
98 | ||
8e2745f4 | 99 | CBDATA_CLASS_INIT(ClientRequestContext); |
100 | ||
101 | void * | |
102 | ClientRequestContext::operator new (size_t size) | |
103 | { | |
104 | assert (size == sizeof(ClientRequestContext)); | |
105 | CBDATA_INIT_TYPE(ClientRequestContext); | |
106 | ClientRequestContext *result = cbdataAlloc(ClientRequestContext); | |
aa625860 | 107 | return result; |
8e2745f4 | 108 | } |
62e76326 | 109 | |
8e2745f4 | 110 | void |
111 | ClientRequestContext::operator delete (void *address) | |
112 | { | |
113 | ClientRequestContext *t = static_cast<ClientRequestContext *>(address); | |
aa625860 | 114 | cbdataFree(t); |
8e2745f4 | 115 | } |
116 | ||
edce4d98 | 117 | /* Local functions */ |
edce4d98 | 118 | /* other */ |
2efeb0b7 | 119 | static void clientAccessCheckDoneWrapper(allow_t, void *); |
e0c0d54c | 120 | #if USE_SSL |
2efeb0b7 | 121 | static void sslBumpAccessCheckDoneWrapper(allow_t, void *); |
e0c0d54c | 122 | #endif |
59a1efb2 | 123 | static int clientHierarchical(ClientHttpRequest * http); |
124 | static void clientInterpretRequestHeaders(ClientHttpRequest * http); | |
de31d06f | 125 | static RH clientRedirectDoneWrapper; |
2efeb0b7 | 126 | static void checkNoCacheDoneWrapper(allow_t, void *); |
e6ccf245 | 127 | extern "C" CSR clientGetMoreData; |
128 | extern "C" CSS clientReplyStatus; | |
129 | extern "C" CSD clientReplyDetach; | |
528b2c61 | 130 | static void checkFailureRatio(err_type, hier_code); |
edce4d98 | 131 | |
8e2745f4 | 132 | ClientRequestContext::~ClientRequestContext() |
133 | { | |
de31d06f | 134 | /* |
a546b04b | 135 | * Release our "lock" on our parent, ClientHttpRequest, if we |
136 | * still have one | |
de31d06f | 137 | */ |
a546b04b | 138 | |
139 | if (http) | |
140 | cbdataReferenceDone(http); | |
62e76326 | 141 | |
2bd84e5f | 142 | delete error; |
5f8252d2 | 143 | debugs(85,3, HERE << this << " ClientRequestContext destructed"); |
8e2745f4 | 144 | } |
145 | ||
2bd84e5f | 146 | ClientRequestContext::ClientRequestContext(ClientHttpRequest *anHttp) : http(cbdataReference(anHttp)), acl_checklist (NULL), redirect_state (REDIRECT_NONE), error(NULL), readNextRequest(false) |
edce4d98 | 147 | { |
57abaac0 | 148 | http_access_done = false; |
149 | redirect_done = false; | |
150 | no_cache_done = false; | |
151 | interpreted_req_hdrs = false; | |
e0c0d54c CT |
152 | #if USE_SSL |
153 | sslBumpCheckDone = false; | |
154 | #endif | |
0b86805b | 155 | debugs(85,3, HERE << this << " ClientRequestContext constructed"); |
edce4d98 | 156 | } |
157 | ||
528b2c61 | 158 | CBDATA_CLASS_INIT(ClientHttpRequest); |
8e2745f4 | 159 | |
528b2c61 | 160 | void * |
161 | ClientHttpRequest::operator new (size_t size) | |
162 | { | |
163 | assert (size == sizeof (ClientHttpRequest)); | |
164 | CBDATA_INIT_TYPE(ClientHttpRequest); | |
165 | ClientHttpRequest *result = cbdataAlloc(ClientHttpRequest); | |
aa625860 | 166 | return result; |
528b2c61 | 167 | } |
168 | ||
62e76326 | 169 | void |
528b2c61 | 170 | ClientHttpRequest::operator delete (void *address) |
171 | { | |
aa625860 | 172 | ClientHttpRequest *t = static_cast<ClientHttpRequest *>(address); |
173 | cbdataFree(t); | |
528b2c61 | 174 | } |
175 | ||
26ac0430 | 176 | ClientHttpRequest::ClientHttpRequest(ConnStateData * aConn) : |
a83c6ed6 | 177 | #if USE_ADAPTATION |
26ac0430 | 178 | AsyncJob("ClientHttpRequest"), |
1cf238db | 179 | #endif |
26ac0430 | 180 | loggingEntry_(NULL) |
528b2c61 | 181 | { |
1cf238db | 182 | start_time = current_time; |
a0355e95 | 183 | setConn(aConn); |
a14f38d0 | 184 | al.tcpClient = clientConnection = aConn->clientConnection; |
a0355e95 | 185 | dlinkAdd(this, &active, &ClientActiveRequests); |
a83c6ed6 | 186 | #if USE_ADAPTATION |
b044675d | 187 | request_satisfaction_mode = false; |
188 | #endif | |
e0c0d54c | 189 | #if USE_SSL |
caf3666d | 190 | sslBumpNeed = Ssl::bumpEnd; |
e0c0d54c | 191 | #endif |
528b2c61 | 192 | } |
193 | ||
0655fa4d | 194 | /* |
195 | * returns true if client specified that the object must come from the cache | |
196 | * without contacting origin server | |
197 | */ | |
198 | bool | |
199 | ClientHttpRequest::onlyIfCached()const | |
200 | { | |
201 | assert(request); | |
202 | return request->cache_control && | |
4ce6e3b5 | 203 | request->cache_control->onlyIfCached(); |
0655fa4d | 204 | } |
205 | ||
528b2c61 | 206 | /* |
207 | * This function is designed to serve a fairly specific purpose. | |
208 | * Occasionally our vBNS-connected caches can talk to each other, but not | |
209 | * the rest of the world. Here we try to detect frequent failures which | |
210 | * make the cache unusable (e.g. DNS lookup and connect() failures). If | |
211 | * the failure:success ratio goes above 1.0 then we go into "hit only" | |
212 | * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors | |
213 | * will only fetch HITs from us if they are using the ICP protocol. We | |
214 | * stay in this mode for 5 minutes. | |
26ac0430 | 215 | * |
528b2c61 | 216 | * Duane W., Sept 16, 1996 |
217 | */ | |
218 | ||
219 | #define FAILURE_MODE_TIME 300 | |
220 | ||
221 | static void | |
222 | checkFailureRatio(err_type etype, hier_code hcode) | |
223 | { | |
224 | static double magic_factor = 100.0; | |
225 | double n_good; | |
226 | double n_bad; | |
62e76326 | 227 | |
528b2c61 | 228 | if (hcode == HIER_NONE) |
62e76326 | 229 | return; |
230 | ||
528b2c61 | 231 | n_good = magic_factor / (1.0 + request_failure_ratio); |
62e76326 | 232 | |
528b2c61 | 233 | n_bad = magic_factor - n_good; |
62e76326 | 234 | |
528b2c61 | 235 | switch (etype) { |
62e76326 | 236 | |
528b2c61 | 237 | case ERR_DNS_FAIL: |
62e76326 | 238 | |
528b2c61 | 239 | case ERR_CONNECT_FAIL: |
3712be3f | 240 | case ERR_SECURE_CONNECT_FAIL: |
62e76326 | 241 | |
528b2c61 | 242 | case ERR_READ_ERROR: |
62e76326 | 243 | n_bad++; |
244 | break; | |
245 | ||
528b2c61 | 246 | default: |
62e76326 | 247 | n_good++; |
528b2c61 | 248 | } |
62e76326 | 249 | |
528b2c61 | 250 | request_failure_ratio = n_bad / n_good; |
62e76326 | 251 | |
528b2c61 | 252 | if (hit_only_mode_until > squid_curtime) |
62e76326 | 253 | return; |
254 | ||
528b2c61 | 255 | if (request_failure_ratio < 1.0) |
62e76326 | 256 | return; |
257 | ||
bf8fe701 | 258 | debugs(33, 0, "Failure Ratio at "<< std::setw(4)<< |
259 | std::setprecision(3) << request_failure_ratio); | |
62e76326 | 260 | |
bf8fe701 | 261 | debugs(33, 0, "Going into hit-only-mode for " << |
262 | FAILURE_MODE_TIME / 60 << " minutes..."); | |
62e76326 | 263 | |
528b2c61 | 264 | hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME; |
62e76326 | 265 | |
528b2c61 | 266 | request_failure_ratio = 0.8; /* reset to something less than 1.0 */ |
267 | } | |
268 | ||
269 | ClientHttpRequest::~ClientHttpRequest() | |
270 | { | |
bf8fe701 | 271 | debugs(33, 3, "httpRequestFree: " << uri); |
72bdee4c | 272 | PROF_start(httpRequestFree); |
62e76326 | 273 | |
5f8252d2 | 274 | // Even though freeResources() below may destroy the request, |
275 | // we no longer set request->body_pipe to NULL here | |
276 | // because we did not initiate that pipe (ConnStateData did) | |
62e76326 | 277 | |
528b2c61 | 278 | /* the ICP check here was erroneous |
26ac0430 | 279 | * - StoreEntry::releaseRequest was always called if entry was valid |
528b2c61 | 280 | */ |
281 | assert(logType < LOG_TYPE_MAX); | |
9ce7856a | 282 | |
528b2c61 | 283 | logRequest(); |
9ce7856a | 284 | |
0976f8db | 285 | loggingEntry(NULL); |
286 | ||
528b2c61 | 287 | if (request) |
62e76326 | 288 | checkFailureRatio(request->errType, al.hier.code); |
289 | ||
528b2c61 | 290 | freeResources(); |
62e76326 | 291 | |
a83c6ed6 AR |
292 | #if USE_ADAPTATION |
293 | announceInitiatorAbort(virginHeadSource); | |
9d4d7c5e | 294 | |
a83c6ed6 AR |
295 | if (adaptedBodySource != NULL) |
296 | stopConsumingFrom(adaptedBodySource); | |
de31d06f | 297 | #endif |
9ce7856a | 298 | |
de31d06f | 299 | if (calloutContext) |
300 | delete calloutContext; | |
301 | ||
be364179 AJ |
302 | clientConnection = NULL; |
303 | ||
26ac0430 AJ |
304 | if (conn_) |
305 | cbdataReferenceDone(conn_); | |
1cf238db | 306 | |
528b2c61 | 307 | /* moving to the next connection is handled by the context free */ |
308 | dlinkDelete(&active, &ClientActiveRequests); | |
9ce7856a | 309 | |
72bdee4c | 310 | PROF_stop(httpRequestFree); |
528b2c61 | 311 | } |
62e76326 | 312 | |
11992b6f AJ |
313 | /** |
314 | * Create a request and kick it off | |
315 | * | |
316 | * \retval 0 success | |
317 | * \retval -1 failure | |
318 | * | |
69660be0 | 319 | * TODO: Pass in the buffers to be used in the inital Read request, as they are |
320 | * determined by the user | |
edce4d98 | 321 | */ |
11992b6f | 322 | int |
60745f24 | 323 | clientBeginRequest(const HttpRequestMethod& method, char const *url, CSCB * streamcallback, |
0655fa4d | 324 | CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header, |
62e76326 | 325 | char *tailbuf, size_t taillen) |
edce4d98 | 326 | { |
327 | size_t url_sz; | |
a0355e95 | 328 | ClientHttpRequest *http = new ClientHttpRequest(NULL); |
190154cf | 329 | HttpRequest *request; |
528b2c61 | 330 | StoreIOBuffer tempBuffer; |
1cf238db | 331 | http->start_time = current_time; |
edce4d98 | 332 | /* this is only used to adjust the connection offset in client_side.c */ |
333 | http->req_sz = 0; | |
c8be6d7b | 334 | tempBuffer.length = taillen; |
335 | tempBuffer.data = tailbuf; | |
edce4d98 | 336 | /* client stream setup */ |
337 | clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach, | |
0655fa4d | 338 | clientReplyStatus, new clientReplyContext(http), streamcallback, |
62e76326 | 339 | streamdetach, streamdata, tempBuffer); |
edce4d98 | 340 | /* make it visible in the 'current acctive requests list' */ |
edce4d98 | 341 | /* Set flags */ |
a46d2c0e | 342 | /* internal requests only makes sense in an |
343 | * accelerator today. TODO: accept flags ? */ | |
344 | http->flags.accel = 1; | |
edce4d98 | 345 | /* allow size for url rewriting */ |
346 | url_sz = strlen(url) + Config.appendDomainLen + 5; | |
e6ccf245 | 347 | http->uri = (char *)xcalloc(url_sz, 1); |
edce4d98 | 348 | strcpy(http->uri, url); |
349 | ||
c21ad0f5 | 350 | if ((request = HttpRequest::CreateFromUrlAndMethod(http->uri, method)) == NULL) { |
bf8fe701 | 351 | debugs(85, 5, "Invalid URL: " << http->uri); |
62e76326 | 352 | return -1; |
edce4d98 | 353 | } |
62e76326 | 354 | |
69660be0 | 355 | /* |
11992b6f | 356 | * now update the headers in request with our supplied headers. urlParse |
69660be0 | 357 | * should return a blank header set, but we use Update to be sure of |
358 | * correctness. | |
edce4d98 | 359 | */ |
360 | if (header) | |
a9925b40 | 361 | request->header.update(header, NULL); |
62e76326 | 362 | |
edce4d98 | 363 | http->log_uri = xstrdup(urlCanonicalClean(request)); |
62e76326 | 364 | |
edce4d98 | 365 | /* http struct now ready */ |
366 | ||
69660be0 | 367 | /* |
368 | * build new header list *? TODO | |
edce4d98 | 369 | */ |
370 | request->flags.accelerated = http->flags.accel; | |
62e76326 | 371 | |
a46d2c0e | 372 | request->flags.internalclient = 1; |
373 | ||
374 | /* this is an internally created | |
375 | * request, not subject to acceleration | |
376 | * target overrides */ | |
69660be0 | 377 | /* |
378 | * FIXME? Do we want to detect and handle internal requests of internal | |
379 | * objects ? | |
380 | */ | |
edce4d98 | 381 | |
382 | /* Internally created requests cannot have bodies today */ | |
383 | request->content_length = 0; | |
62e76326 | 384 | |
cc192b50 | 385 | request->client_addr.SetNoAddr(); |
62e76326 | 386 | |
3d674977 AJ |
387 | #if FOLLOW_X_FORWARDED_FOR |
388 | request->indirect_client_addr.SetNoAddr(); | |
389 | #endif /* FOLLOW_X_FORWARDED_FOR */ | |
26ac0430 | 390 | |
cc192b50 | 391 | request->my_addr.SetNoAddr(); /* undefined for internal requests */ |
62e76326 | 392 | |
cc192b50 | 393 | request->my_addr.SetPort(0); |
62e76326 | 394 | |
3872be7c AJ |
395 | /* Our version is HTTP/1.1 */ |
396 | HttpVersion http_ver(1,1); | |
edce4d98 | 397 | request->http_ver = http_ver; |
62e76326 | 398 | |
6dd9f4bd | 399 | http->request = HTTPMSGLOCK(request); |
edce4d98 | 400 | |
401 | /* optional - skip the access check ? */ | |
de31d06f | 402 | http->calloutContext = new ClientRequestContext(http); |
403 | ||
57abaac0 | 404 | http->calloutContext->http_access_done = false; |
de31d06f | 405 | |
57abaac0 | 406 | http->calloutContext->redirect_done = true; |
de31d06f | 407 | |
57abaac0 | 408 | http->calloutContext->no_cache_done = true; |
de31d06f | 409 | |
410 | http->doCallouts(); | |
62e76326 | 411 | |
edce4d98 | 412 | return 0; |
413 | } | |
414 | ||
de31d06f | 415 | bool |
416 | ClientRequestContext::httpStateIsValid() | |
417 | { | |
418 | ClientHttpRequest *http_ = http; | |
419 | ||
420 | if (cbdataReferenceValid(http_)) | |
421 | return true; | |
422 | ||
423 | http = NULL; | |
424 | ||
425 | cbdataReferenceDone(http_); | |
426 | ||
427 | return false; | |
428 | } | |
429 | ||
3d674977 AJ |
430 | #if FOLLOW_X_FORWARDED_FOR |
431 | /** | |
a9044668 | 432 | * clientFollowXForwardedForCheck() checks the content of X-Forwarded-For: |
3d674977 AJ |
433 | * against the followXFF ACL, or cleans up and passes control to |
434 | * clientAccessCheck(). | |
d096ace1 AJ |
435 | * |
436 | * The trust model here is a little ambiguous. So to clarify the logic: | |
437 | * - we may always use the direct client address as the client IP. | |
a9044668 | 438 | * - these trust tests merey tell whether we trust given IP enough to believe the |
d096ace1 AJ |
439 | * IP string which it appended to the X-Forwarded-For: header. |
440 | * - if at any point we don't trust what an IP adds we stop looking. | |
441 | * - at that point the current contents of indirect_client_addr are the value set | |
442 | * by the last previously trusted IP. | |
443 | * ++ indirect_client_addr contains the remote direct client from the trusted peers viewpoint. | |
3d674977 | 444 | */ |
3d674977 | 445 | static void |
2efeb0b7 | 446 | clientFollowXForwardedForCheck(allow_t answer, void *data) |
3d674977 AJ |
447 | { |
448 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; | |
3d674977 AJ |
449 | |
450 | if (!calloutContext->httpStateIsValid()) | |
451 | return; | |
452 | ||
d096ace1 AJ |
453 | ClientHttpRequest *http = calloutContext->http; |
454 | HttpRequest *request = http->request; | |
455 | ||
3d674977 AJ |
456 | /* |
457 | * answer should be be ACCESS_ALLOWED or ACCESS_DENIED if we are | |
458 | * called as a result of ACL checks, or -1 if we are called when | |
459 | * there's nothing left to do. | |
460 | */ | |
461 | if (answer == ACCESS_ALLOWED && | |
26ac0430 | 462 | request->x_forwarded_for_iterator.size () != 0) { |
d096ace1 | 463 | |
3d674977 | 464 | /* |
d096ace1 AJ |
465 | * Remove the last comma-delimited element from the |
466 | * x_forwarded_for_iterator and use it to repeat the cycle. | |
467 | */ | |
3d674977 AJ |
468 | const char *p; |
469 | const char *asciiaddr; | |
470 | int l; | |
b7ac5457 | 471 | Ip::Address addr; |
bb790702 | 472 | p = request->x_forwarded_for_iterator.termedBuf(); |
3d674977 AJ |
473 | l = request->x_forwarded_for_iterator.size(); |
474 | ||
475 | /* | |
476 | * XXX x_forwarded_for_iterator should really be a list of | |
477 | * IP addresses, but it's a String instead. We have to | |
478 | * walk backwards through the String, biting off the last | |
479 | * comma-delimited part each time. As long as the data is in | |
480 | * a String, we should probably implement and use a variant of | |
481 | * strListGetItem() that walks backwards instead of forwards | |
482 | * through a comma-separated list. But we don't even do that; | |
483 | * we just do the work in-line here. | |
484 | */ | |
485 | /* skip trailing space and commas */ | |
486 | while (l > 0 && (p[l-1] == ',' || xisspace(p[l-1]))) | |
487 | l--; | |
488 | request->x_forwarded_for_iterator.cut(l); | |
489 | /* look for start of last item in list */ | |
490 | while (l > 0 && ! (p[l-1] == ',' || xisspace(p[l-1]))) | |
491 | l--; | |
492 | asciiaddr = p+l; | |
fafd0efa | 493 | if ((addr = asciiaddr)) { |
3d674977 AJ |
494 | request->indirect_client_addr = addr; |
495 | request->x_forwarded_for_iterator.cut(l); | |
d096ace1 AJ |
496 | calloutContext->acl_checklist = clientAclChecklistCreate(Config.accessList.followXFF, http); |
497 | if (!Config.onoff.acl_uses_indirect_client) { | |
498 | /* override the default src_addr tested if we have to go deeper than one level into XFF */ | |
499 | Filled(calloutContext->acl_checklist)->src_addr = request->indirect_client_addr; | |
3d674977 | 500 | } |
d096ace1 | 501 | calloutContext->acl_checklist->nonBlockingCheck(clientFollowXForwardedForCheck, data); |
3d674977 AJ |
502 | return; |
503 | } | |
504 | } /*if (answer == ACCESS_ALLOWED && | |
505 | request->x_forwarded_for_iterator.size () != 0)*/ | |
506 | ||
507 | /* clean up, and pass control to clientAccessCheck */ | |
26ac0430 | 508 | if (Config.onoff.log_uses_indirect_client) { |
3d674977 AJ |
509 | /* |
510 | * Ensure that the access log shows the indirect client | |
511 | * instead of the direct client. | |
512 | */ | |
513 | ConnStateData *conn = http->getConn(); | |
514 | conn->log_addr = request->indirect_client_addr; | |
515 | } | |
516 | request->x_forwarded_for_iterator.clean(); | |
517 | request->flags.done_follow_x_forwarded_for = 1; | |
518 | ||
d096ace1 AJ |
519 | if (answer != ACCESS_ALLOWED && answer != ACCESS_DENIED) { |
520 | debugs(28, DBG_CRITICAL, "ERROR: Processing X-Forwarded-For. Stopping at IP address: " << request->indirect_client_addr ); | |
493d3865 AJ |
521 | } |
522 | ||
523 | /* process actual access ACL as normal. */ | |
524 | calloutContext->clientAccessCheck(); | |
3d674977 AJ |
525 | } |
526 | #endif /* FOLLOW_X_FORWARDED_FOR */ | |
527 | ||
fe97983f AJ |
528 | static void |
529 | hostHeaderIpVerifyWrapper(const ipcache_addrs* ia, const DnsLookupDetails &dns, void *data) | |
530 | { | |
531 | ClientRequestContext *c = static_cast<ClientRequestContext*>(data); | |
532 | c->hostHeaderIpVerify(ia, dns); | |
533 | } | |
534 | ||
535 | void | |
536 | ClientRequestContext::hostHeaderIpVerify(const ipcache_addrs* ia, const DnsLookupDetails &dns) | |
537 | { | |
538 | Comm::ConnectionPointer clientConn = http->getConn()->clientConnection; | |
539 | ||
540 | // note the DNS details for the transaction stats. | |
541 | http->request->recordLookup(dns); | |
542 | ||
543 | if (ia != NULL && ia->count > 0) { | |
544 | // Is the NAT destination IP in DNS? | |
545 | for (int i = 0; i < ia->count; i++) { | |
546 | if (clientConn->local.matchIPAddr(ia->in_addrs[i]) == 0) { | |
547 | debugs(85, 3, HERE << "validate IP " << clientConn->local << " possible from Host:"); | |
891dfa1b | 548 | http->request->flags.hostVerified = 1; |
fe97983f AJ |
549 | http->doCallouts(); |
550 | return; | |
551 | } | |
552 | debugs(85, 3, HERE << "validate IP " << clientConn->local << " non-match from Host: IP " << ia->in_addrs[i]); | |
553 | } | |
554 | } | |
555 | debugs(85, 3, HERE << "FAIL: validate IP " << clientConn->local << " possible from Host:"); | |
05b28f84 | 556 | hostHeaderVerifyFailed("local IP", "any domain IP"); |
fe97983f AJ |
557 | } |
558 | ||
559 | void | |
05b28f84 | 560 | ClientRequestContext::hostHeaderVerifyFailed(const char *A, const char *B) |
fe97983f | 561 | { |
2962f8b8 AJ |
562 | // IP address validation for Host: failed. Admin wants to ignore them. |
563 | // NP: we do not yet handle CONNECT tunnels well, so ignore for them | |
564 | if (!Config.onoff.hostStrictVerify && http->request->method != METHOD_CONNECT) { | |
565 | debugs(85, 3, "SECURITY ALERT: Host header forgery detected on " << http->getConn()->clientConnection << | |
566 | " (" << A << " does not match " << B << ") on URL: " << urlCanonical(http->request)); | |
567 | ||
568 | // NP: it is tempting to use 'flags.nocache' but that is all about READing cache data. | |
569 | // The problems here are about WRITE for new cache content, which means flags.cachable | |
570 | http->request->flags.cachable = 0; // MUST NOT cache (for now) | |
571 | // XXX: when we have updated the cache key to base on raw-IP + URI this cacheable limit can go. | |
572 | http->request->flags.hierarchical = 0; // MUST NOT pass to peers (for now) | |
573 | // XXX: when we have sorted out the best way to relay requests properly to peers this hierarchical limit can go. | |
567fe088 | 574 | http->doCallouts(); |
2962f8b8 AJ |
575 | return; |
576 | } | |
577 | ||
8f489ad7 AJ |
578 | debugs(85, DBG_IMPORTANT, "SECURITY ALERT: Host header forgery detected on " << |
579 | http->getConn()->clientConnection << " (" << A << " does not match " << B << ")"); | |
580 | debugs(85, DBG_IMPORTANT, "SECURITY ALERT: By user agent: " << http->request->header.getStr(HDR_USER_AGENT)); | |
581 | debugs(85, DBG_IMPORTANT, "SECURITY ALERT: on URL: " << urlCanonical(http->request)); | |
fe97983f AJ |
582 | |
583 | // IP address validation for Host: failed. reject the connection. | |
584 | clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->prev->data; | |
585 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
586 | assert (repContext); | |
2962f8b8 | 587 | repContext->setReplyToError(ERR_CONFLICT_HOST, HTTP_CONFLICT, |
fe97983f AJ |
588 | http->request->method, NULL, |
589 | http->getConn()->clientConnection->remote, | |
590 | http->request, | |
591 | NULL, | |
592 | #if USE_AUTH | |
593 | http->getConn() != NULL && http->getConn()->auth_user_request != NULL ? | |
594 | http->getConn()->auth_user_request : http->request->auth_user_request); | |
595 | #else | |
596 | NULL); | |
597 | #endif | |
598 | node = (clientStreamNode *)http->client_stream.tail->data; | |
599 | clientStreamRead(node, http, node->readBuffer); | |
600 | } | |
601 | ||
602 | void | |
603 | ClientRequestContext::hostHeaderVerify() | |
604 | { | |
605 | // Require a Host: header. | |
606 | const char *host = http->request->header.getStr(HDR_HOST); | |
fe97983f AJ |
607 | |
608 | if (!host) { | |
609 | // TODO: dump out the HTTP/1.1 error about missing host header. | |
610 | // otherwise this is fine, can't forge a header value when its not even set. | |
611 | debugs(85, 3, HERE << "validate skipped with no Host: header present."); | |
612 | http->doCallouts(); | |
613 | return; | |
614 | } | |
615 | ||
8f489ad7 AJ |
616 | if (http->request->flags.internal) { |
617 | // TODO: kill this when URL handling allows partial URLs out of accel mode | |
618 | // and we no longer screw with the URL just to add our internal host there | |
619 | debugs(85, 6, HERE << "validate skipped due to internal composite URL."); | |
620 | http->doCallouts(); | |
621 | return; | |
622 | } | |
623 | ||
fe97983f AJ |
624 | // Locate if there is a port attached, strip ready for IP lookup |
625 | char *portStr = NULL; | |
91663dce AJ |
626 | char *hostB = xstrdup(host); |
627 | host = hostB; | |
fe97983f AJ |
628 | if (host[0] == '[') { |
629 | // IPv6 literal. | |
fe97983f | 630 | portStr = strchr(hostB, ']'); |
91663dce AJ |
631 | if (portStr && *(++portStr) != ':') { |
632 | portStr = NULL; | |
fe97983f | 633 | } |
91663dce | 634 | } else { |
fe97983f | 635 | // Domain or IPv4 literal with port |
fe97983f | 636 | portStr = strrchr(hostB, ':'); |
91663dce AJ |
637 | } |
638 | ||
639 | uint16_t port = 0; | |
640 | if (portStr) { | |
641 | *portStr = '\0'; // strip the ':' | |
642 | if (*(++portStr) != '\0') | |
643 | port = xatoi(portStr); | |
fe97983f AJ |
644 | } |
645 | ||
646 | debugs(85, 3, HERE << "validate host=" << host << ", port=" << port << ", portStr=" << (portStr?portStr:"NULL")); | |
647 | if (http->request->flags.intercepted || http->request->flags.spoof_client_ip) { | |
ba4d9da0 | 648 | // verify the Host: port (if any) matches the apparent destination |
fe97983f | 649 | if (portStr && port != http->getConn()->clientConnection->local.GetPort()) { |
05b28f84 AJ |
650 | debugs(85, 3, HERE << "FAIL on validate port " << http->getConn()->clientConnection->local.GetPort() << |
651 | " matches Host: port " << port << " (" << portStr << ")"); | |
652 | hostHeaderVerifyFailed("intercepted port", portStr); | |
ba4d9da0 AJ |
653 | } else { |
654 | // XXX: match the scheme default port against the apparent destination | |
fe97983f | 655 | |
ba4d9da0 AJ |
656 | // verify the destination DNS is one of the Host: headers IPs |
657 | ipcache_nbgethostbyname(host, hostHeaderIpVerifyWrapper, this); | |
658 | } | |
06059513 | 659 | } else if (!Config.onoff.hostStrictVerify) { |
90529125 AJ |
660 | debugs(85, 3, HERE << "validate skipped."); |
661 | http->doCallouts(); | |
8f489ad7 AJ |
662 | } else if (strlen(host) != strlen(http->request->GetHost())) { |
663 | // Verify forward-proxy requested URL domain matches the Host: header | |
664 | debugs(85, 3, HERE << "FAIL on validate URL domain length " << http->request->GetHost() << " matches Host: " << host); | |
665 | hostHeaderVerifyFailed(host, http->request->GetHost()); | |
666 | } else if (matchDomainName(host, http->request->GetHost()) != 0) { | |
ba4d9da0 | 667 | // Verify forward-proxy requested URL domain matches the Host: header |
fe97983f | 668 | debugs(85, 3, HERE << "FAIL on validate URL domain " << http->request->GetHost() << " matches Host: " << host); |
05b28f84 | 669 | hostHeaderVerifyFailed(host, http->request->GetHost()); |
ba4d9da0 AJ |
670 | } else if (portStr && port != http->request->port) { |
671 | // Verify forward-proxy requested URL domain matches the Host: header | |
672 | debugs(85, 3, HERE << "FAIL on validate URL port " << http->request->port << " matches Host: port " << portStr); | |
673 | hostHeaderVerifyFailed("URL port", portStr); | |
65d2fdbf | 674 | } else if (!portStr && http->request->method != METHOD_CONNECT && http->request->port != urlDefaultPort(http->request->protocol)) { |
ba4d9da0 | 675 | // Verify forward-proxy requested URL domain matches the Host: header |
65d2fdbf | 676 | // Special case: we don't have a default-port to check for CONNECT. Assume URL is correct. |
ba4d9da0 AJ |
677 | debugs(85, 3, HERE << "FAIL on validate URL port " << http->request->port << " matches Host: default port " << urlDefaultPort(http->request->protocol)); |
678 | hostHeaderVerifyFailed("URL port", "default port"); | |
679 | } else { | |
680 | // Okay no problem. | |
681 | debugs(85, 3, HERE << "validate passed."); | |
2962f8b8 | 682 | http->request->flags.hostVerified = 1; |
ba4d9da0 | 683 | http->doCallouts(); |
fe97983f | 684 | } |
ba4d9da0 | 685 | safe_free(hostB); |
fe97983f AJ |
686 | } |
687 | ||
edce4d98 | 688 | /* This is the entry point for external users of the client_side routines */ |
689 | void | |
de31d06f | 690 | ClientRequestContext::clientAccessCheck() |
edce4d98 | 691 | { |
3d674977 AJ |
692 | #if FOLLOW_X_FORWARDED_FOR |
693 | if (!http->request->flags.done_follow_x_forwarded_for && | |
26ac0430 AJ |
694 | Config.accessList.followXFF && |
695 | http->request->header.has(HDR_X_FORWARDED_FOR)) { | |
d096ace1 AJ |
696 | |
697 | /* we always trust the direct client address for actual use */ | |
698 | http->request->indirect_client_addr = http->request->client_addr; | |
fafd0efa | 699 | http->request->indirect_client_addr.SetPort(0); |
d096ace1 AJ |
700 | |
701 | /* setup the XFF iterator for processing */ | |
702 | http->request->x_forwarded_for_iterator = http->request->header.getList(HDR_X_FORWARDED_FOR); | |
703 | ||
704 | /* begin by checking to see if we trust direct client enough to walk XFF */ | |
705 | acl_checklist = clientAclChecklistCreate(Config.accessList.followXFF, http); | |
706 | acl_checklist->nonBlockingCheck(clientFollowXForwardedForCheck, this); | |
3d674977 AJ |
707 | return; |
708 | } | |
709 | #endif /* FOLLOW_X_FORWARDED_FOR */ | |
493d3865 | 710 | |
b50e327b AJ |
711 | if (Config.accessList.http) { |
712 | acl_checklist = clientAclChecklistCreate(Config.accessList.http, http); | |
713 | acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this); | |
714 | } else { | |
715 | debugs(0, DBG_CRITICAL, "No http_access configuration found. This will block ALL traffic"); | |
716 | clientAccessCheckDone(ACCESS_DENIED); | |
717 | } | |
edce4d98 | 718 | } |
719 | ||
533493da AJ |
720 | /** |
721 | * Identical in operation to clientAccessCheck() but performed later using different configured ACL list. | |
722 | * The default here is to allow all. Since the earlier http_access should do a default deny all. | |
723 | * This check is just for a last-minute denial based on adapted request headers. | |
724 | */ | |
725 | void | |
726 | ClientRequestContext::clientAccessCheck2() | |
727 | { | |
728 | if (Config.accessList.adapted_http) { | |
729 | acl_checklist = clientAclChecklistCreate(Config.accessList.adapted_http, http); | |
730 | acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this); | |
731 | } else { | |
327e2131 | 732 | debugs(85, 2, HERE << "No adapted_http_access configuration. default: ALLOW"); |
533493da AJ |
733 | clientAccessCheckDone(ACCESS_ALLOWED); |
734 | } | |
735 | } | |
736 | ||
edce4d98 | 737 | void |
2efeb0b7 | 738 | clientAccessCheckDoneWrapper(allow_t answer, void *data) |
edce4d98 | 739 | { |
de31d06f | 740 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; |
fbade053 | 741 | |
de31d06f | 742 | if (!calloutContext->httpStateIsValid()) |
62e76326 | 743 | return; |
62e76326 | 744 | |
de31d06f | 745 | calloutContext->clientAccessCheckDone(answer); |
746 | } | |
747 | ||
9d5e7196 AJ |
748 | void |
749 | ClientRequestContext::clientAccessCheckDone(const allow_t &answer) | |
de31d06f | 750 | { |
751 | acl_checklist = NULL; | |
edce4d98 | 752 | err_type page_id; |
753 | http_status status; | |
9d5e7196 AJ |
754 | debugs(85, 2, "The request " << |
755 | RequestMethodStr(http->request->method) << " " << | |
756 | http->uri << " is " << answer << | |
757 | ", because it matched '" << | |
758 | (AclMatchedName ? AclMatchedName : "NO ACL's") << "'" ); | |
f5691f9c | 759 | |
2f1431ea AJ |
760 | #if USE_AUTH |
761 | char const *proxy_auth_msg = "<null>"; | |
94a396a3 | 762 | if (http->getConn() != NULL && http->getConn()->auth_user_request != NULL) |
f5691f9c | 763 | proxy_auth_msg = http->getConn()->auth_user_request->denyMessage("<null>"); |
764 | else if (http->request->auth_user_request != NULL) | |
765 | proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>"); | |
2f1431ea | 766 | #endif |
62e76326 | 767 | |
dd332b92 | 768 | if (answer != ACCESS_ALLOWED) { |
9d5e7196 | 769 | // auth has a grace period where credentials can be expired but okay not to challenge. |
309347ef | 770 | |
9d5e7196 AJ |
771 | /* Send an auth challenge or error */ |
772 | // XXX: do we still need aclIsProxyAuth() ? | |
dd332b92 | 773 | bool auth_challenge = (answer == ACCESS_AUTH_REQUIRED || aclIsProxyAuth(AclMatchedName)); |
9d5e7196 AJ |
774 | debugs(85, 5, "Access Denied: " << http->uri); |
775 | debugs(85, 5, "AclMatchedName = " << (AclMatchedName ? AclMatchedName : "<null>")); | |
2f1431ea | 776 | #if USE_AUTH |
9d5e7196 AJ |
777 | if (auth_challenge) |
778 | debugs(33, 5, "Proxy Auth Message = " << (proxy_auth_msg ? proxy_auth_msg : "<null>")); | |
2f1431ea | 779 | #endif |
9ce7856a | 780 | |
9d5e7196 AJ |
781 | /* |
782 | * NOTE: get page_id here, based on AclMatchedName because if | |
783 | * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in | |
784 | * the clientCreateStoreEntry() call just below. Pedro Ribeiro | |
785 | * <pribeiro@isel.pt> | |
786 | */ | |
787 | page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, answer != ACCESS_AUTH_REQUIRED); | |
9ce7856a | 788 | |
9d5e7196 | 789 | http->logType = LOG_TCP_DENIED; |
62e76326 | 790 | |
9d5e7196 | 791 | if (auth_challenge) { |
2f1431ea | 792 | #if USE_AUTH |
9d5e7196 AJ |
793 | if (http->request->flags.sslBumped) { |
794 | /*SSL Bumped request, authentication is not possible*/ | |
795 | status = HTTP_FORBIDDEN; | |
796 | } else if (!http->flags.accel) { | |
797 | /* Proxy authorisation needed */ | |
798 | status = HTTP_PROXY_AUTHENTICATION_REQUIRED; | |
799 | } else { | |
800 | /* WWW authorisation needed */ | |
801 | status = HTTP_UNAUTHORIZED; | |
802 | } | |
ed6163ef | 803 | #else |
9d5e7196 | 804 | // need auth, but not possible to do. |
2f1431ea | 805 | status = HTTP_FORBIDDEN; |
ed6163ef | 806 | #endif |
9d5e7196 AJ |
807 | if (page_id == ERR_NONE) |
808 | page_id = ERR_CACHE_ACCESS_DENIED; | |
62e76326 | 809 | } else { |
9d5e7196 | 810 | status = HTTP_FORBIDDEN; |
ed6163ef | 811 | |
9d5e7196 AJ |
812 | if (page_id == ERR_NONE) |
813 | page_id = ERR_ACCESS_DENIED; | |
62e76326 | 814 | } |
815 | ||
9d5e7196 AJ |
816 | Ip::Address tmpnoaddr; |
817 | tmpnoaddr.SetNoAddr(); | |
68715527 CT |
818 | error = clientBuildError(page_id, status, |
819 | NULL, | |
820 | http->getConn() != NULL ? http->getConn()->clientConnection->remote : tmpnoaddr, | |
821 | http->request | |
822 | ); | |
823 | ||
824 | error->auth_user_request = | |
825 | http->getConn() != NULL && http->getConn()->auth_user_request != NULL ? | |
826 | http->getConn()->auth_user_request : http->request->auth_user_request; | |
827 | ||
828 | readNextRequest = true; | |
ed6163ef | 829 | } |
62e76326 | 830 | |
dd332b92 | 831 | /* ACCESS_ALLOWED continues here ... */ |
de31d06f | 832 | safe_free(http->uri); |
833 | ||
834 | http->uri = xstrdup(urlCanonical(http->request)); | |
835 | ||
836 | http->doCallouts(); | |
837 | } | |
838 | ||
a83c6ed6 | 839 | #if USE_ADAPTATION |
de31d06f | 840 | void |
79628299 | 841 | ClientHttpRequest::noteAdaptationAclCheckDone(Adaptation::ServiceGroupPointer g) |
de31d06f | 842 | { |
a83c6ed6 | 843 | debugs(93,3,HERE << this << " adaptationAclCheckDone called"); |
6ec67de9 | 844 | |
e1381638 | 845 | #if ICAP_CLIENT |
79628299 | 846 | Adaptation::Icap::History::Pointer ih = request->icapHistory(); |
e1381638 | 847 | if (ih != NULL) { |
79628299 CT |
848 | if (getConn() != NULL) { |
849 | ih->rfc931 = getConn()->clientConnection->rfc931; | |
e1381638 | 850 | #if USE_SSL |
79628299 CT |
851 | assert(getConn()->clientConnection != NULL); |
852 | ih->ssluser = sslGetUserEmail(fd_table[getConn()->clientConnection->fd].ssl); | |
e1381638 | 853 | #endif |
3ff65596 | 854 | } |
79628299 CT |
855 | ih->log_uri = log_uri; |
856 | ih->req_sz = req_sz; | |
3ff65596 AR |
857 | } |
858 | #endif | |
859 | ||
a22e6cd3 AR |
860 | if (!g) { |
861 | debugs(85,3, HERE << "no adaptation needed"); | |
79628299 | 862 | doCallouts(); |
5f8252d2 | 863 | return; |
864 | } | |
de31d06f | 865 | |
79628299 | 866 | startAdaptation(g); |
edce4d98 | 867 | } |
868 | ||
de31d06f | 869 | #endif |
870 | ||
14cc8559 | 871 | static void |
2efeb0b7 | 872 | clientRedirectAccessCheckDone(allow_t answer, void *data) |
14cc8559 | 873 | { |
874 | ClientRequestContext *context = (ClientRequestContext *)data; | |
9d5e7196 AJ |
875 | ClientHttpRequest *http = context->http; |
876 | context->acl_checklist = NULL; | |
ed6163ef | 877 | |
14cc8559 | 878 | if (answer == ACCESS_ALLOWED) |
9d5e7196 | 879 | redirectStart(http, clientRedirectDoneWrapper, context); |
14cc8559 | 880 | else |
de31d06f | 881 | context->clientRedirectDone(NULL); |
14cc8559 | 882 | } |
883 | ||
de31d06f | 884 | void |
885 | ClientRequestContext::clientRedirectStart() | |
14cc8559 | 886 | { |
48e7baac | 887 | debugs(33, 5, HERE << "'" << http->uri << "'"); |
14cc8559 | 888 | |
14cc8559 | 889 | if (Config.accessList.redirector) { |
de31d06f | 890 | acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http); |
891 | acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, this); | |
14cc8559 | 892 | } else |
de31d06f | 893 | redirectStart(http, clientRedirectDoneWrapper, this); |
14cc8559 | 894 | } |
895 | ||
edce4d98 | 896 | static int |
59a1efb2 | 897 | clientHierarchical(ClientHttpRequest * http) |
edce4d98 | 898 | { |
899 | const char *url = http->uri; | |
190154cf | 900 | HttpRequest *request = http->request; |
60745f24 | 901 | HttpRequestMethod method = request->method; |
edce4d98 | 902 | const wordlist *p = NULL; |
903 | ||
2962f8b8 AJ |
904 | // intercepted requests MUST NOT (yet) be sent to peers unless verified |
905 | if (!request->flags.hostVerified && (request->flags.intercepted || request->flags.spoof_client_ip)) | |
906 | return 0; | |
907 | ||
69660be0 | 908 | /* |
909 | * IMS needs a private key, so we can use the hierarchy for IMS only if our | |
910 | * neighbors support private keys | |
911 | */ | |
62e76326 | 912 | |
edce4d98 | 913 | if (request->flags.ims && !neighbors_do_private_keys) |
62e76326 | 914 | return 0; |
915 | ||
69660be0 | 916 | /* |
917 | * This is incorrect: authenticating requests can be sent via a hierarchy | |
06b97e72 | 918 | * (they can even be cached if the correct headers are set on the reply) |
edce4d98 | 919 | */ |
920 | if (request->flags.auth) | |
62e76326 | 921 | return 0; |
922 | ||
edce4d98 | 923 | if (method == METHOD_TRACE) |
62e76326 | 924 | return 1; |
925 | ||
edce4d98 | 926 | if (method != METHOD_GET) |
62e76326 | 927 | return 0; |
928 | ||
edce4d98 | 929 | /* scan hierarchy_stoplist */ |
930 | for (p = Config.hierarchy_stoplist; p; p = p->next) | |
62e76326 | 931 | if (strstr(url, p->key)) |
932 | return 0; | |
933 | ||
edce4d98 | 934 | if (request->flags.loopdetect) |
62e76326 | 935 | return 0; |
936 | ||
0c3d3f65 | 937 | if (request->protocol == AnyP::PROTO_HTTP) |
62e76326 | 938 | return httpCachable(method); |
939 | ||
0c3d3f65 | 940 | if (request->protocol == AnyP::PROTO_GOPHER) |
62e76326 | 941 | return gopherCachable(request); |
942 | ||
39a19cb7 | 943 | if (request->protocol == AnyP::PROTO_CACHE_OBJECT) |
62e76326 | 944 | return 0; |
945 | ||
edce4d98 | 946 | return 1; |
947 | } | |
948 | ||
46a1f562 HN |
949 | static void |
950 | clientCheckPinning(ClientHttpRequest * http) | |
951 | { | |
952 | HttpRequest *request = http->request; | |
953 | HttpHeader *req_hdr = &request->header; | |
954 | ConnStateData *http_conn = http->getConn(); | |
955 | ||
956 | /* Internal requests such as those from ESI includes may be without | |
957 | * a client connection | |
958 | */ | |
959 | if (!http_conn) | |
f54f527e | 960 | return; |
46a1f562 HN |
961 | |
962 | request->flags.connection_auth_disabled = http_conn->port->connection_auth_disabled; | |
963 | if (!request->flags.connection_auth_disabled) { | |
73c36fd9 | 964 | if (Comm::IsConnOpen(http_conn->pinning.serverConnection)) { |
46a1f562 HN |
965 | if (http_conn->pinning.auth) { |
966 | request->flags.connection_auth = 1; | |
967 | request->flags.auth = 1; | |
968 | } else { | |
969 | request->flags.connection_proxy_auth = 1; | |
970 | } | |
b1cf2350 AJ |
971 | // These should already be linked correctly. |
972 | assert(request->clientConnectionManager == http_conn); | |
46a1f562 HN |
973 | } |
974 | } | |
975 | ||
976 | /* check if connection auth is used, and flag as candidate for pinning | |
977 | * in such case. | |
978 | * Note: we may need to set flags.connection_auth even if the connection | |
979 | * is already pinned if it was pinned earlier due to proxy auth | |
980 | */ | |
981 | if (!request->flags.connection_auth) { | |
982 | if (req_hdr->has(HDR_AUTHORIZATION) || req_hdr->has(HDR_PROXY_AUTHORIZATION)) { | |
983 | HttpHeaderPos pos = HttpHeaderInitPos; | |
984 | HttpHeaderEntry *e; | |
985 | int may_pin = 0; | |
986 | while ((e = req_hdr->getEntry(&pos))) { | |
987 | if (e->id == HDR_AUTHORIZATION || e->id == HDR_PROXY_AUTHORIZATION) { | |
988 | const char *value = e->value.rawBuf(); | |
989 | if (strncasecmp(value, "NTLM ", 5) == 0 | |
990 | || | |
991 | strncasecmp(value, "Negotiate ", 10) == 0 | |
992 | || | |
993 | strncasecmp(value, "Kerberos ", 9) == 0) { | |
994 | if (e->id == HDR_AUTHORIZATION) { | |
995 | request->flags.connection_auth = 1; | |
996 | may_pin = 1; | |
997 | } else { | |
998 | request->flags.connection_proxy_auth = 1; | |
999 | may_pin = 1; | |
1000 | } | |
1001 | } | |
1002 | } | |
1003 | } | |
1004 | if (may_pin && !request->pinnedConnection()) { | |
b1cf2350 AJ |
1005 | // These should already be linked correctly. Just need the ServerConnection to pinn. |
1006 | assert(request->clientConnectionManager == http_conn); | |
46a1f562 HN |
1007 | } |
1008 | } | |
1009 | } | |
1010 | } | |
1011 | ||
edce4d98 | 1012 | static void |
59a1efb2 | 1013 | clientInterpretRequestHeaders(ClientHttpRequest * http) |
edce4d98 | 1014 | { |
190154cf | 1015 | HttpRequest *request = http->request; |
0ef77270 | 1016 | HttpHeader *req_hdr = &request->header; |
edce4d98 | 1017 | int no_cache = 0; |
edce4d98 | 1018 | const char *str; |
62e76326 | 1019 | |
edce4d98 | 1020 | request->imslen = -1; |
a9925b40 | 1021 | request->ims = req_hdr->getTime(HDR_IF_MODIFIED_SINCE); |
62e76326 | 1022 | |
edce4d98 | 1023 | if (request->ims > 0) |
62e76326 | 1024 | request->flags.ims = 1; |
1025 | ||
432bc83c HN |
1026 | if (!request->flags.ignore_cc) { |
1027 | if (req_hdr->has(HDR_PRAGMA)) { | |
1028 | String s = req_hdr->getList(HDR_PRAGMA); | |
62e76326 | 1029 | |
432bc83c HN |
1030 | if (strListIsMember(&s, "no-cache", ',')) |
1031 | no_cache++; | |
30abd221 | 1032 | |
432bc83c HN |
1033 | s.clean(); |
1034 | } | |
62e76326 | 1035 | |
432bc83c | 1036 | if (request->cache_control) |
4ce6e3b5 | 1037 | if (request->cache_control->noCache()) |
432bc83c | 1038 | no_cache++; |
62e76326 | 1039 | |
432bc83c HN |
1040 | /* |
1041 | * Work around for supporting the Reload button in IE browsers when Squid | |
1042 | * is used as an accelerator or transparent proxy, by turning accelerated | |
1043 | * IMS request to no-cache requests. Now knows about IE 5.5 fix (is | |
1044 | * actually only fixed in SP1, but we can't tell whether we are talking to | |
1045 | * SP1 or not so all 5.5 versions are treated 'normally'). | |
1046 | */ | |
1047 | if (Config.onoff.ie_refresh) { | |
1048 | if (http->flags.accel && request->flags.ims) { | |
1049 | if ((str = req_hdr->getStr(HDR_USER_AGENT))) { | |
1050 | if (strstr(str, "MSIE 5.01") != NULL) | |
1051 | no_cache++; | |
1052 | else if (strstr(str, "MSIE 5.0") != NULL) | |
1053 | no_cache++; | |
1054 | else if (strstr(str, "MSIE 4.") != NULL) | |
1055 | no_cache++; | |
1056 | else if (strstr(str, "MSIE 3.") != NULL) | |
1057 | no_cache++; | |
1058 | } | |
62e76326 | 1059 | } |
1060 | } | |
edce4d98 | 1061 | } |
914b89a2 | 1062 | |
1063 | if (request->method == METHOD_OTHER) { | |
26ac0430 | 1064 | no_cache++; |
60745f24 | 1065 | } |
62e76326 | 1066 | |
edce4d98 | 1067 | if (no_cache) { |
626096be | 1068 | #if USE_HTTP_VIOLATIONS |
62e76326 | 1069 | |
1070 | if (Config.onoff.reload_into_ims) | |
1071 | request->flags.nocache_hack = 1; | |
1072 | else if (refresh_nocache_hack) | |
1073 | request->flags.nocache_hack = 1; | |
1074 | else | |
edce4d98 | 1075 | #endif |
62e76326 | 1076 | |
1077 | request->flags.nocache = 1; | |
edce4d98 | 1078 | } |
62e76326 | 1079 | |
0ef77270 | 1080 | /* ignore range header in non-GETs or non-HEADs */ |
1081 | if (request->method == METHOD_GET || request->method == METHOD_HEAD) { | |
56713d9a AR |
1082 | // XXX: initialize if we got here without HttpRequest::parseHeader() |
1083 | if (!request->range) | |
1084 | request->range = req_hdr->getRange(); | |
62e76326 | 1085 | |
1086 | if (request->range) { | |
1087 | request->flags.range = 1; | |
1088 | clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data; | |
1089 | /* XXX: This is suboptimal. We should give the stream the range set, | |
1090 | * and thereby let the top of the stream set the offset when the | |
26ac0430 | 1091 | * size becomes known. As it is, we will end up requesting from 0 |
62e76326 | 1092 | * for evey -X range specification. |
1093 | * RBC - this may be somewhat wrong. We should probably set the range | |
1094 | * iter up at this point. | |
1095 | */ | |
1096 | node->readBuffer.offset = request->range->lowestOffset(0); | |
1097 | http->range_iter.pos = request->range->begin(); | |
1098 | http->range_iter.valid = true; | |
1099 | } | |
edce4d98 | 1100 | } |
62e76326 | 1101 | |
0ef77270 | 1102 | /* Only HEAD and GET requests permit a Range or Request-Range header. |
1103 | * If these headers appear on any other type of request, delete them now. | |
1104 | */ | |
1105 | else { | |
1106 | req_hdr->delById(HDR_RANGE); | |
1107 | req_hdr->delById(HDR_REQUEST_RANGE); | |
56713d9a | 1108 | delete request->range; |
0ef77270 | 1109 | request->range = NULL; |
1110 | } | |
1111 | ||
a9925b40 | 1112 | if (req_hdr->has(HDR_AUTHORIZATION)) |
62e76326 | 1113 | request->flags.auth = 1; |
1114 | ||
46a1f562 | 1115 | clientCheckPinning(http); |
d67acb4e | 1116 | |
edce4d98 | 1117 | if (request->login[0] != '\0') |
62e76326 | 1118 | request->flags.auth = 1; |
1119 | ||
a9925b40 | 1120 | if (req_hdr->has(HDR_VIA)) { |
30abd221 | 1121 | String s = req_hdr->getList(HDR_VIA); |
62e76326 | 1122 | /* |
3c4fcf0f | 1123 | * ThisCache cannot be a member of Via header, "1.1 ThisCache" can. |
62e76326 | 1124 | * Note ThisCache2 has a space prepended to the hostname so we don't |
1125 | * accidentally match super-domains. | |
1126 | */ | |
1127 | ||
1128 | if (strListIsSubstr(&s, ThisCache2, ',')) { | |
1129 | debugObj(33, 1, "WARNING: Forwarding loop detected for:\n", | |
1130 | request, (ObjPackMethod) & httpRequestPack); | |
1131 | request->flags.loopdetect = 1; | |
1132 | } | |
1133 | ||
21f6708d | 1134 | #if USE_FORW_VIA_DB |
bb790702 | 1135 | fvdbCountVia(s.termedBuf()); |
62e76326 | 1136 | |
edce4d98 | 1137 | #endif |
62e76326 | 1138 | |
30abd221 | 1139 | s.clean(); |
edce4d98 | 1140 | } |
62e76326 | 1141 | |
21f6708d | 1142 | #if USE_FORW_VIA_DB |
62e76326 | 1143 | |
a9925b40 | 1144 | if (req_hdr->has(HDR_X_FORWARDED_FOR)) { |
30abd221 | 1145 | String s = req_hdr->getList(HDR_X_FORWARDED_FOR); |
bb790702 | 1146 | fvdbCountForw(s.termedBuf()); |
30abd221 | 1147 | s.clean(); |
edce4d98 | 1148 | } |
62e76326 | 1149 | |
edce4d98 | 1150 | #endif |
62e76326 | 1151 | |
610ee341 | 1152 | request->flags.cachable = http->request->cacheable(); |
62e76326 | 1153 | |
edce4d98 | 1154 | if (clientHierarchical(http)) |
62e76326 | 1155 | request->flags.hierarchical = 1; |
1156 | ||
bf8fe701 | 1157 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_NOCACHE = " << |
1158 | (request->flags.nocache ? "SET" : "NOT SET")); | |
1159 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_CACHABLE = " << | |
1160 | (request->flags.cachable ? "SET" : "NOT SET")); | |
1161 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_HIERARCHICAL = " << | |
1162 | (request->flags.hierarchical ? "SET" : "NOT SET")); | |
62e76326 | 1163 | |
edce4d98 | 1164 | } |
1165 | ||
1166 | void | |
de31d06f | 1167 | clientRedirectDoneWrapper(void *data, char *result) |
edce4d98 | 1168 | { |
de31d06f | 1169 | ClientRequestContext *calloutContext = (ClientRequestContext *)data; |
db02222f | 1170 | |
de31d06f | 1171 | if (!calloutContext->httpStateIsValid()) |
62e76326 | 1172 | return; |
62e76326 | 1173 | |
de31d06f | 1174 | calloutContext->clientRedirectDone(result); |
1175 | } | |
1176 | ||
1177 | void | |
1178 | ClientRequestContext::clientRedirectDone(char *result) | |
1179 | { | |
190154cf | 1180 | HttpRequest *old_request = http->request; |
bf8fe701 | 1181 | debugs(85, 5, "clientRedirectDone: '" << http->uri << "' result=" << (result ? result : "NULL")); |
de31d06f | 1182 | assert(redirect_state == REDIRECT_PENDING); |
1183 | redirect_state = REDIRECT_DONE; | |
62e76326 | 1184 | |
edce4d98 | 1185 | if (result) { |
62e76326 | 1186 | http_status status = (http_status) atoi(result); |
1187 | ||
1188 | if (status == HTTP_MOVED_PERMANENTLY | |
1189 | || status == HTTP_MOVED_TEMPORARILY | |
1190 | || status == HTTP_SEE_OTHER | |
6bc67152 | 1191 | || status == HTTP_PERMANENT_REDIRECT |
62e76326 | 1192 | || status == HTTP_TEMPORARY_REDIRECT) { |
1193 | char *t = result; | |
1194 | ||
1195 | if ((t = strchr(result, ':')) != NULL) { | |
1196 | http->redirect.status = status; | |
1197 | http->redirect.location = xstrdup(t + 1); | |
e5b677f0 | 1198 | // TODO: validate the URL produced here is RFC 2616 compliant absolute URI |
62e76326 | 1199 | } else { |
6bc67152 | 1200 | debugs(85, DBG_CRITICAL, "ERROR: URL-rewrite produces invalid " << status << " redirect Location: " << result); |
62e76326 | 1201 | } |
e5b677f0 | 1202 | } else if (strcmp(result, http->uri)) { |
9be14530 AJ |
1203 | // XXX: validate the URL properly *without* generating a whole new request object right here. |
1204 | // XXX: the clone() should be done only AFTER we know the new URL is valid. | |
1205 | HttpRequest *new_request = old_request->clone(); | |
1206 | if (urlParse(old_request->method, result, new_request)) { | |
1207 | debugs(61,2, HERE << "URL-rewriter diverts URL from " << urlCanonical(old_request) << " to " << urlCanonical(new_request)); | |
1208 | ||
1209 | // update the new request to flag the re-writing was done on it | |
1210 | new_request->flags.redirected = 1; | |
1211 | ||
1212 | // unlink bodypipe from the old request. Not needed there any longer. | |
1213 | if (old_request->body_pipe != NULL) { | |
1214 | old_request->body_pipe = NULL; | |
1215 | debugs(61,2, HERE << "URL-rewriter diverts body_pipe " << new_request->body_pipe << | |
1216 | " from request " << old_request << " to " << new_request); | |
1217 | } | |
1218 | ||
1219 | // update the current working ClientHttpRequest fields | |
1220 | safe_free(http->uri); | |
1221 | http->uri = xstrdup(urlCanonical(new_request)); | |
1222 | HTTPMSGUNLOCK(old_request); | |
1223 | http->request = HTTPMSGLOCK(new_request); | |
1224 | } else { | |
e5b677f0 | 1225 | debugs(85, DBG_CRITICAL, "ERROR: URL-rewrite produces invalid request: " << |
c9fd01b4 | 1226 | old_request->method << " " << result << " " << old_request->http_ver); |
9be14530 AJ |
1227 | delete new_request; |
1228 | } | |
74b48915 | 1229 | } |
edce4d98 | 1230 | } |
62e76326 | 1231 | |
edce4d98 | 1232 | /* FIXME PIPELINE: This is innacurate during pipelining */ |
62e76326 | 1233 | |
73c36fd9 AJ |
1234 | if (http->getConn() != NULL && Comm::IsConnOpen(http->getConn()->clientConnection)) |
1235 | fd_note(http->getConn()->clientConnection->fd, http->uri); | |
62e76326 | 1236 | |
c8be6d7b | 1237 | assert(http->uri); |
62e76326 | 1238 | |
de31d06f | 1239 | http->doCallouts(); |
edce4d98 | 1240 | } |
1241 | ||
b50e327b AJ |
1242 | /** Test cache allow/deny configuration |
1243 | * Sets flags.cachable=1 if caching is not denied. | |
1244 | */ | |
edce4d98 | 1245 | void |
8e2745f4 | 1246 | ClientRequestContext::checkNoCache() |
edce4d98 | 1247 | { |
b50e327b AJ |
1248 | if (Config.accessList.noCache) { |
1249 | acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http); | |
1250 | acl_checklist->nonBlockingCheck(checkNoCacheDoneWrapper, this); | |
1251 | } else { | |
1252 | /* unless otherwise specified, we try to cache. */ | |
2efeb0b7 | 1253 | checkNoCacheDone(ACCESS_ALLOWED); |
b50e327b | 1254 | } |
edce4d98 | 1255 | } |
1256 | ||
de31d06f | 1257 | static void |
2efeb0b7 | 1258 | checkNoCacheDoneWrapper(allow_t answer, void *data) |
edce4d98 | 1259 | { |
de31d06f | 1260 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; |
e4a67a80 | 1261 | |
de31d06f | 1262 | if (!calloutContext->httpStateIsValid()) |
1263 | return; | |
1264 | ||
1265 | calloutContext->checkNoCacheDone(answer); | |
8e2745f4 | 1266 | } |
4fb35c3c | 1267 | |
8e2745f4 | 1268 | void |
2efeb0b7 | 1269 | ClientRequestContext::checkNoCacheDone(const allow_t &answer) |
62e76326 | 1270 | { |
8e2745f4 | 1271 | acl_checklist = NULL; |
2efeb0b7 | 1272 | http->request->flags.cachable = (answer == ACCESS_ALLOWED); |
de31d06f | 1273 | http->doCallouts(); |
edce4d98 | 1274 | } |
1275 | ||
e0c0d54c CT |
1276 | #if USE_SSL |
1277 | bool | |
1278 | ClientRequestContext::sslBumpAccessCheck() | |
1279 | { | |
1280 | if (http->request->method == METHOD_CONNECT && | |
379e8c1c | 1281 | !http->request->flags.spoof_client_ip && // is not a fake ssl-bumped request from a https port |
f8901ea9 | 1282 | Config.accessList.ssl_bump && http->getConn()->port->sslBump) { |
e0c0d54c CT |
1283 | debugs(85, 5, HERE << "SslBump possible, checking ACL"); |
1284 | ||
1285 | ACLFilledChecklist *acl_checklist = clientAclChecklistCreate(Config.accessList.ssl_bump, http); | |
1286 | acl_checklist->nonBlockingCheck(sslBumpAccessCheckDoneWrapper, this); | |
1287 | return true; | |
f8901ea9 | 1288 | } else { |
caf3666d | 1289 | http->sslBumpNeeded(Ssl::bumpNone); |
e0c0d54c CT |
1290 | return false; |
1291 | } | |
1292 | } | |
1293 | ||
f8901ea9 | 1294 | /** |
e0c0d54c CT |
1295 | * A wrapper function to use the ClientRequestContext::sslBumpAccessCheckDone method |
1296 | * as ACLFilledChecklist callback | |
1297 | */ | |
1298 | static void | |
2efeb0b7 | 1299 | sslBumpAccessCheckDoneWrapper(allow_t answer, void *data) |
e0c0d54c CT |
1300 | { |
1301 | ClientRequestContext *calloutContext = static_cast<ClientRequestContext *>(data); | |
9d5e7196 AJ |
1302 | |
1303 | if (!calloutContext->httpStateIsValid()) | |
1304 | return; | |
ed6163ef | 1305 | calloutContext->sslBumpAccessCheckDone(answer); |
e0c0d54c CT |
1306 | } |
1307 | ||
1308 | void | |
ed6163ef | 1309 | ClientRequestContext::sslBumpAccessCheckDone(const allow_t &answer) |
e0c0d54c | 1310 | { |
ed6163ef AJ |
1311 | if (!httpStateIsValid()) |
1312 | return; | |
1313 | ||
caf3666d AR |
1314 | if (answer == ACCESS_ALLOWED) |
1315 | http->sslBumpNeeded(static_cast<Ssl::BumpMode>(answer.kind)); | |
1316 | else | |
1317 | http->sslBumpNeeded(Ssl::bumpNone); | |
1318 | ||
e0c0d54c CT |
1319 | http->doCallouts(); |
1320 | } | |
1321 | #endif | |
1322 | ||
69660be0 | 1323 | /* |
1324 | * Identify requests that do not go through the store and client side stream | |
1325 | * and forward them to the appropriate location. All other requests, request | |
1326 | * them. | |
edce4d98 | 1327 | */ |
1328 | void | |
8e2745f4 | 1329 | ClientHttpRequest::processRequest() |
edce4d98 | 1330 | { |
60745f24 | 1331 | debugs(85, 4, "clientProcessRequest: " << RequestMethodStr(request->method) << " '" << uri << "'"); |
62e76326 | 1332 | |
31281814 | 1333 | if (request->method == METHOD_CONNECT && !redirect.status) { |
3712be3f | 1334 | #if USE_SSL |
31281814 AJ |
1335 | if (sslBumpNeeded()) { |
1336 | sslBumpStart(); | |
1337 | return; | |
1338 | } | |
3712be3f | 1339 | #endif |
62e76326 | 1340 | logType = LOG_TCP_MISS; |
f84dd7eb | 1341 | getConn()->stopReading(); // tunnels read for themselves |
11007d4b | 1342 | tunnelStart(this, &out.size, &al.http.code); |
62e76326 | 1343 | return; |
edce4d98 | 1344 | } |
62e76326 | 1345 | |
8e2745f4 | 1346 | httpStart(); |
1347 | } | |
1348 | ||
1349 | void | |
1350 | ClientHttpRequest::httpStart() | |
1351 | { | |
559da936 | 1352 | PROF_start(httpStart); |
8e2745f4 | 1353 | logType = LOG_TAG_NONE; |
38e16f92 | 1354 | debugs(85, 4, "ClientHttpRequest::httpStart: " << Format::log_tags[logType] << " for '" << uri << "'"); |
bf8fe701 | 1355 | |
edce4d98 | 1356 | /* no one should have touched this */ |
8e2745f4 | 1357 | assert(out.offset == 0); |
edce4d98 | 1358 | /* Use the Stream Luke */ |
8e2745f4 | 1359 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->data; |
1360 | clientStreamRead(node, this, node->readBuffer); | |
559da936 | 1361 | PROF_stop(httpStart); |
edce4d98 | 1362 | } |
0655fa4d | 1363 | |
3712be3f | 1364 | #if USE_SSL |
1365 | ||
caf3666d | 1366 | Ssl::BumpMode |
26ac0430 AJ |
1367 | ClientHttpRequest::sslBumpNeeded() const |
1368 | { | |
caf3666d AR |
1369 | assert(sslBumpNeed != Ssl::bumpEnd); |
1370 | return sslBumpNeed; | |
e0c0d54c | 1371 | } |
3712be3f | 1372 | |
e0c0d54c | 1373 | void |
caf3666d | 1374 | ClientHttpRequest::sslBumpNeeded(Ssl::BumpMode mode) |
e0c0d54c | 1375 | { |
caf3666d AR |
1376 | debugs(83, 3, HERE << "sslBump required: "<< Ssl::bumpMode(mode)); |
1377 | sslBumpNeed = mode; | |
3712be3f | 1378 | } |
1379 | ||
1380 | // called when comm_write has completed | |
1381 | static void | |
e0d28505 | 1382 | SslBumpEstablish(const Comm::ConnectionPointer &, char *, size_t, comm_err_t errflag, int, void *data) |
3712be3f | 1383 | { |
1384 | ClientHttpRequest *r = static_cast<ClientHttpRequest*>(data); | |
1385 | debugs(85, 5, HERE << "responded to CONNECT: " << r << " ? " << errflag); | |
1386 | ||
1387 | assert(r && cbdataReferenceValid(r)); | |
1388 | r->sslBumpEstablish(errflag); | |
1389 | } | |
1390 | ||
1391 | void | |
1392 | ClientHttpRequest::sslBumpEstablish(comm_err_t errflag) | |
1393 | { | |
1394 | // Bail out quickly on COMM_ERR_CLOSING - close handlers will tidy up | |
1395 | if (errflag == COMM_ERR_CLOSING) | |
1396 | return; | |
1397 | ||
1398 | if (errflag) { | |
1fa35be8 | 1399 | debugs(85, 3, HERE << "CONNECT response failure in SslBump: " << errflag); |
bbc83914 | 1400 | getConn()->clientConnection->close(); |
3712be3f | 1401 | return; |
1402 | } | |
1403 | ||
40d8be97 AR |
1404 | // We lack HttpReply which logRequest() uses to log the status code. |
1405 | // TODO: Use HttpReply instead of the "200 Connection established" string. | |
1406 | al.http.code = 200; | |
1407 | ||
21512911 CT |
1408 | #if USE_AUTH |
1409 | // Preserve authentication info for the ssl-bumped request | |
1410 | if (request->auth_user_request != NULL) | |
1411 | getConn()->auth_user_request = request->auth_user_request; | |
1412 | #endif | |
03f00a11 | 1413 | |
caf3666d | 1414 | getConn()->switchToHttps(request, sslBumpNeeded()); |
3712be3f | 1415 | } |
1416 | ||
1417 | void | |
1418 | ClientHttpRequest::sslBumpStart() | |
1419 | { | |
73c36fd9 | 1420 | debugs(85, 5, HERE << "Confirming CONNECT tunnel on FD " << getConn()->clientConnection); |
3712be3f | 1421 | // send an HTTP 200 response to kick client SSL negotiation |
73c36fd9 | 1422 | debugs(33, 7, HERE << "Confirming CONNECT tunnel on FD " << getConn()->clientConnection); |
3712be3f | 1423 | |
1424 | // TODO: Unify with tunnel.cc and add a Server(?) header | |
b0388924 | 1425 | static const char *const conn_established = "HTTP/1.1 200 Connection established\r\n\r\n"; |
ec41b64c AJ |
1426 | AsyncCall::Pointer call = commCbCall(85, 5, "ClientSocketContext::sslBumpEstablish", |
1427 | CommIoCbPtrFun(&SslBumpEstablish, this)); | |
73c36fd9 | 1428 | Comm::Write(getConn()->clientConnection, conn_established, strlen(conn_established), call, NULL); |
3712be3f | 1429 | } |
1430 | ||
1431 | #endif | |
1432 | ||
0655fa4d | 1433 | bool |
1434 | ClientHttpRequest::gotEnough() const | |
1435 | { | |
86a2f789 | 1436 | /** TODO: should be querying the stream. */ |
7173d5b0 | 1437 | int64_t contentLength = |
06a5ae20 | 1438 | memObject()->getReply()->bodySize(request->method); |
0655fa4d | 1439 | assert(contentLength >= 0); |
1440 | ||
1441 | if (out.offset < contentLength) | |
1442 | return false; | |
1443 | ||
1444 | return true; | |
1445 | } | |
1446 | ||
86a2f789 | 1447 | void |
1448 | ClientHttpRequest::storeEntry(StoreEntry *newEntry) | |
1449 | { | |
1450 | entry_ = newEntry; | |
1451 | } | |
1452 | ||
0976f8db | 1453 | void |
1454 | ClientHttpRequest::loggingEntry(StoreEntry *newEntry) | |
1455 | { | |
1456 | if (loggingEntry_) | |
97b5e68f | 1457 | loggingEntry_->unlock(); |
0976f8db | 1458 | |
1459 | loggingEntry_ = newEntry; | |
1460 | ||
1461 | if (loggingEntry_) | |
3d0ac046 | 1462 | loggingEntry_->lock(); |
0976f8db | 1463 | } |
86a2f789 | 1464 | |
de31d06f | 1465 | /* |
1466 | * doCallouts() - This function controls the order of "callout" | |
1467 | * executions, including non-blocking access control checks, the | |
1468 | * redirector, and ICAP. Previously, these callouts were chained | |
1469 | * together such that "clientAccessCheckDone()" would call | |
1470 | * "clientRedirectStart()" and so on. | |
1471 | * | |
1472 | * The ClientRequestContext (aka calloutContext) class holds certain | |
1473 | * state data for the callout/callback operations. Previously | |
1474 | * ClientHttpRequest would sort of hand off control to ClientRequestContext | |
1475 | * for a short time. ClientRequestContext would then delete itself | |
1476 | * and pass control back to ClientHttpRequest when all callouts | |
1477 | * were finished. | |
1478 | * | |
1479 | * This caused some problems for ICAP because we want to make the | |
1480 | * ICAP callout after checking ACLs, but before checking the no_cache | |
1481 | * list. We can't stuff the ICAP state into the ClientRequestContext | |
1482 | * class because we still need the ICAP state after ClientRequestContext | |
1483 | * goes away. | |
1484 | * | |
1485 | * Note that ClientRequestContext is created before the first call | |
1486 | * to doCallouts(). | |
1487 | * | |
1488 | * If one of the callouts notices that ClientHttpRequest is no | |
1489 | * longer valid, it should call cbdataReferenceDone() so that | |
1490 | * ClientHttpRequest's reference count goes to zero and it will get | |
1491 | * deleted. ClientHttpRequest will then delete ClientRequestContext. | |
1492 | * | |
1493 | * Note that we set the _done flags here before actually starting | |
1494 | * the callout. This is strictly for convenience. | |
1495 | */ | |
1496 | ||
425de4c8 AJ |
1497 | extern tos_t aclMapTOS (acl_tos * head, ACLChecklist * ch); |
1498 | extern nfmark_t aclMapNfmark (acl_nfmark * head, ACLChecklist * ch); | |
057f5854 | 1499 | |
de31d06f | 1500 | void |
1501 | ClientHttpRequest::doCallouts() | |
1502 | { | |
1503 | assert(calloutContext); | |
1504 | ||
6fca33e0 CT |
1505 | /*Save the original request for logging purposes*/ |
1506 | if (!calloutContext->http->al.request) | |
105d1937 | 1507 | calloutContext->http->al.request = HTTPMSGLOCK(request); |
6fca33e0 | 1508 | |
fe97983f | 1509 | // CVE-2009-0801: verify the Host: header is consistent with other known details. |
2bd84e5f | 1510 | if (!calloutContext->error && !calloutContext->host_header_verify_done) { |
fe97983f AJ |
1511 | debugs(83, 3, HERE << "Doing calloutContext->hostHeaderVerify()"); |
1512 | calloutContext->host_header_verify_done = true; | |
1513 | calloutContext->hostHeaderVerify(); | |
1514 | return; | |
1515 | } | |
1516 | ||
2bd84e5f | 1517 | if (!calloutContext->error && !calloutContext->http_access_done) { |
3712be3f | 1518 | debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck()"); |
57abaac0 | 1519 | calloutContext->http_access_done = true; |
de31d06f | 1520 | calloutContext->clientAccessCheck(); |
1521 | return; | |
1522 | } | |
1523 | ||
a83c6ed6 | 1524 | #if USE_ADAPTATION |
2bd84e5f | 1525 | if (!calloutContext->error && !calloutContext->adaptation_acl_check_done) { |
a83c6ed6 | 1526 | calloutContext->adaptation_acl_check_done = true; |
abd4b611 | 1527 | if (Adaptation::AccessCheck::Start( |
26ac0430 | 1528 | Adaptation::methodReqmod, Adaptation::pointPreCache, |
79628299 | 1529 | request, NULL, this)) |
abd4b611 | 1530 | return; // will call callback |
de31d06f | 1531 | } |
de31d06f | 1532 | #endif |
1533 | ||
2bd84e5f | 1534 | if (!calloutContext->error && !calloutContext->redirect_done) { |
57abaac0 | 1535 | calloutContext->redirect_done = true; |
de31d06f | 1536 | assert(calloutContext->redirect_state == REDIRECT_NONE); |
1537 | ||
1538 | if (Config.Program.redirect) { | |
3712be3f | 1539 | debugs(83, 3, HERE << "Doing calloutContext->clientRedirectStart()"); |
de31d06f | 1540 | calloutContext->redirect_state = REDIRECT_PENDING; |
1541 | calloutContext->clientRedirectStart(); | |
1542 | return; | |
1543 | } | |
1544 | } | |
1545 | ||
2bd84e5f | 1546 | if (!calloutContext->error && !calloutContext->adapted_http_access_done) { |
533493da AJ |
1547 | debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck2()"); |
1548 | calloutContext->adapted_http_access_done = true; | |
1549 | calloutContext->clientAccessCheck2(); | |
1550 | return; | |
1551 | } | |
1552 | ||
2bd84e5f | 1553 | if (!calloutContext->error && !calloutContext->interpreted_req_hdrs) { |
3712be3f | 1554 | debugs(83, 3, HERE << "Doing clientInterpretRequestHeaders()"); |
57abaac0 | 1555 | calloutContext->interpreted_req_hdrs = 1; |
1556 | clientInterpretRequestHeaders(this); | |
1557 | } | |
1558 | ||
2bd84e5f | 1559 | if (!calloutContext->error && !calloutContext->no_cache_done) { |
57abaac0 | 1560 | calloutContext->no_cache_done = true; |
de31d06f | 1561 | |
1562 | if (Config.accessList.noCache && request->flags.cachable) { | |
3712be3f | 1563 | debugs(83, 3, HERE << "Doing calloutContext->checkNoCache()"); |
de31d06f | 1564 | calloutContext->checkNoCache(); |
1565 | return; | |
1566 | } | |
1567 | } | |
1568 | ||
425de4c8 AJ |
1569 | if (!calloutContext->tosToClientDone) { |
1570 | calloutContext->tosToClientDone = true; | |
73c36fd9 | 1571 | if (getConn() != NULL && Comm::IsConnOpen(getConn()->clientConnection)) { |
c0941a6a | 1572 | ACLFilledChecklist ch(NULL, request, NULL); |
057f5854 | 1573 | ch.src_addr = request->client_addr; |
1574 | ch.my_addr = request->my_addr; | |
425de4c8 | 1575 | tos_t tos = aclMapTOS(Ip::Qos::TheConfig.tosToClient, &ch); |
3712be3f | 1576 | if (tos) |
73c36fd9 | 1577 | Ip::Qos::setSockTos(getConn()->clientConnection, tos); |
425de4c8 AJ |
1578 | } |
1579 | } | |
1580 | ||
1581 | if (!calloutContext->nfmarkToClientDone) { | |
1582 | calloutContext->nfmarkToClientDone = true; | |
73c36fd9 | 1583 | if (getConn() != NULL && Comm::IsConnOpen(getConn()->clientConnection)) { |
425de4c8 AJ |
1584 | ACLFilledChecklist ch(NULL, request, NULL); |
1585 | ch.src_addr = request->client_addr; | |
1586 | ch.my_addr = request->my_addr; | |
1587 | nfmark_t mark = aclMapNfmark(Ip::Qos::TheConfig.nfmarkToClient, &ch); | |
1588 | if (mark) | |
73c36fd9 | 1589 | Ip::Qos::setSockNfmark(getConn()->clientConnection, mark); |
3712be3f | 1590 | } |
057f5854 | 1591 | } |
1592 | ||
d2565320 | 1593 | #if USE_SSL |
2bd84e5f CT |
1594 | // We need to check for SSL bump even if the calloutContext->error is set |
1595 | // because we are handling with different way the error inside SSL-bump | |
e0c0d54c CT |
1596 | if (!calloutContext->sslBumpCheckDone) { |
1597 | calloutContext->sslBumpCheckDone = true; | |
1598 | if (calloutContext->sslBumpAccessCheck()) | |
1599 | return; | |
1600 | /* else no ssl bump required*/ | |
1601 | } | |
d2565320 | 1602 | #endif |
e0c0d54c | 1603 | |
2bd84e5f CT |
1604 | if (calloutContext->error) { |
1605 | const char *uri = urlCanonical(request); | |
1606 | StoreEntry *e= storeCreateEntry(uri, uri, request->flags, request->method); | |
1607 | #if USE_SSL | |
1608 | if (sslBumpNeeded() && | |
1609 | calloutContext->error->httpStatus != HTTP_PROXY_AUTHENTICATION_REQUIRED) { | |
1610 | // set final error but delay sending until we bump | |
1611 | Ssl::ServerBump *srvBump = new Ssl::ServerBump(request, e); | |
1612 | errorAppendEntry(e, calloutContext->error); | |
1613 | calloutContext->error = NULL; | |
1614 | getConn()->setServerBump(srvBump); | |
1615 | e->unlock(); | |
1616 | } else | |
1617 | #endif | |
1618 | { | |
1619 | // send the error to the client | |
1620 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; | |
1621 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
1622 | assert (repContext); | |
1623 | repContext->setReplyToStoreEntry(e); | |
1624 | errorAppendEntry(e, calloutContext->error); | |
1625 | calloutContext->error = NULL; | |
1626 | if (calloutContext->readNextRequest) | |
1627 | getConn()->flags.readMore = true; // resume any pipeline reads. | |
1628 | node = (clientStreamNode *)client_stream.tail->data; | |
1629 | clientStreamRead(node, this, node->readBuffer); | |
1630 | e->unlock(); | |
1631 | // Stop here. | |
1632 | return; | |
1633 | } | |
1634 | } | |
1635 | ||
de31d06f | 1636 | cbdataReferenceDone(calloutContext->http); |
1637 | delete calloutContext; | |
1638 | calloutContext = NULL; | |
de31d06f | 1639 | #if HEADERS_LOG |
1640 | ||
1641 | headersLog(0, 1, request->method, request); | |
1642 | #endif | |
1643 | ||
58d7150d | 1644 | debugs(83, 3, HERE << "calling processRequest()"); |
de31d06f | 1645 | processRequest(); |
3ff65596 AR |
1646 | |
1647 | #if ICAP_CLIENT | |
1648 | Adaptation::Icap::History::Pointer ih = request->icapHistory(); | |
1649 | if (ih != NULL) | |
1650 | ih->logType = logType; | |
1651 | #endif | |
de31d06f | 1652 | } |
1653 | ||
32d002cb | 1654 | #if !_USE_INLINE_ |
86a2f789 | 1655 | #include "client_side_request.cci" |
1656 | #endif | |
de31d06f | 1657 | |
a83c6ed6 | 1658 | #if USE_ADAPTATION |
a22e6cd3 AR |
1659 | /// Initiate an asynchronous adaptation transaction which will call us back. |
1660 | void | |
1661 | ClientHttpRequest::startAdaptation(const Adaptation::ServiceGroupPointer &g) | |
3b299123 | 1662 | { |
a22e6cd3 | 1663 | debugs(85, 3, HERE << "adaptation needed for " << this); |
a83c6ed6 AR |
1664 | assert(!virginHeadSource); |
1665 | assert(!adaptedBodySource); | |
a22e6cd3 | 1666 | virginHeadSource = initiateAdaptation( |
4cb2536f | 1667 | new Adaptation::Iterator(request, NULL, g)); |
a83c6ed6 | 1668 | |
e1381638 | 1669 | // we could try to guess whether we can bypass this adaptation |
a22e6cd3 | 1670 | // initiation failure, but it should not really happen |
4299f876 | 1671 | Must(initiated(virginHeadSource)); |
de31d06f | 1672 | } |
1673 | ||
1674 | void | |
3af10ac0 | 1675 | ClientHttpRequest::noteAdaptationAnswer(const Adaptation::Answer &answer) |
de31d06f | 1676 | { |
de31d06f | 1677 | assert(cbdataReferenceValid(this)); // indicates bug |
3af10ac0 AR |
1678 | clearAdaptation(virginHeadSource); |
1679 | assert(!adaptedBodySource); | |
1680 | ||
1681 | switch (answer.kind) { | |
1682 | case Adaptation::Answer::akForward: | |
1683 | handleAdaptedHeader(answer.message); | |
1684 | break; | |
1685 | ||
1686 | case Adaptation::Answer::akBlock: | |
1687 | handleAdaptationBlock(answer); | |
1688 | break; | |
1689 | ||
1690 | case Adaptation::Answer::akError: | |
1691 | handleAdaptationFailure(ERR_DETAIL_CLT_REQMOD_ABORT, !answer.final); | |
1692 | break; | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | void | |
ec4d1a1d | 1697 | ClientHttpRequest::handleAdaptedHeader(HttpMsg *msg) |
3af10ac0 | 1698 | { |
5f8252d2 | 1699 | assert(msg); |
1700 | ||
b044675d | 1701 | if (HttpRequest *new_req = dynamic_cast<HttpRequest*>(msg)) { |
200ac359 | 1702 | /* |
5f8252d2 | 1703 | * Replace the old request with the new request. |
200ac359 | 1704 | */ |
6dd9f4bd | 1705 | HTTPMSGUNLOCK(request); |
1706 | request = HTTPMSGLOCK(new_req); | |
200ac359 | 1707 | /* |
1708 | * Store the new URI for logging | |
1709 | */ | |
1710 | xfree(uri); | |
1711 | uri = xstrdup(urlCanonical(request)); | |
1712 | setLogUri(this, urlCanonicalClean(request)); | |
914b89a2 | 1713 | assert(request->method.id()); |
b044675d | 1714 | } else if (HttpReply *new_rep = dynamic_cast<HttpReply*>(msg)) { |
1715 | debugs(85,3,HERE << "REQMOD reply is HTTP reply"); | |
1716 | ||
5f8252d2 | 1717 | // subscribe to receive reply body |
1718 | if (new_rep->body_pipe != NULL) { | |
a83c6ed6 | 1719 | adaptedBodySource = new_rep->body_pipe; |
d222a56c HN |
1720 | int consumer_ok = adaptedBodySource->setConsumerIfNotLate(this); |
1721 | assert(consumer_ok); | |
5f8252d2 | 1722 | } |
1723 | ||
b044675d | 1724 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
1725 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
1726 | repContext->createStoreEntry(request->method, request->flags); | |
1727 | ||
1728 | EBIT_CLR(storeEntry()->flags, ENTRY_FWD_HDR_WAIT); | |
1729 | request_satisfaction_mode = true; | |
1730 | request_satisfaction_offset = 0; | |
1731 | storeEntry()->replaceHttpReply(new_rep); | |
97ae5196 | 1732 | storeEntry()->timestampsSet(); |
cb4c4288 | 1733 | |
a83c6ed6 | 1734 | if (!adaptedBodySource) // no body |
cb4c4288 | 1735 | storeEntry()->complete(); |
b044675d | 1736 | clientGetMoreData(node, this); |
200ac359 | 1737 | } |
de31d06f | 1738 | |
5f8252d2 | 1739 | // we are done with getting headers (but may be receiving body) |
a83c6ed6 | 1740 | clearAdaptation(virginHeadSource); |
5f8252d2 | 1741 | |
b044675d | 1742 | if (!request_satisfaction_mode) |
1743 | doCallouts(); | |
de31d06f | 1744 | } |
1745 | ||
1746 | void | |
3af10ac0 | 1747 | ClientHttpRequest::handleAdaptationBlock(const Adaptation::Answer &answer) |
de31d06f | 1748 | { |
3af10ac0 AR |
1749 | request->detailError(ERR_ACCESS_DENIED, ERR_DETAIL_REQMOD_BLOCK); |
1750 | AclMatchedName = answer.ruleId.termedBuf(); | |
1751 | assert(calloutContext); | |
1752 | calloutContext->clientAccessCheckDone(ACCESS_DENIED); | |
1753 | AclMatchedName = NULL; | |
de31d06f | 1754 | } |
1755 | ||
0ad2b63b CT |
1756 | void |
1757 | ClientHttpRequest::resumeBodyStorage() | |
1758 | { | |
e83cdc25 | 1759 | if (!adaptedBodySource) |
0ad2b63b CT |
1760 | return; |
1761 | ||
1762 | noteMoreBodyDataAvailable(adaptedBodySource); | |
1763 | } | |
1764 | ||
de31d06f | 1765 | void |
1cf238db | 1766 | ClientHttpRequest::noteMoreBodyDataAvailable(BodyPipe::Pointer) |
de31d06f | 1767 | { |
5f8252d2 | 1768 | assert(request_satisfaction_mode); |
a83c6ed6 | 1769 | assert(adaptedBodySource != NULL); |
5f8252d2 | 1770 | |
0ad2b63b | 1771 | if (size_t contentSize = adaptedBodySource->buf().contentSize()) { |
4dc2b072 | 1772 | const size_t spaceAvailable = storeEntry()->bytesWanted(Range<size_t>(0,contentSize)); |
0ad2b63b CT |
1773 | |
1774 | if (spaceAvailable < contentSize ) { | |
1775 | // No or partial body data consuming | |
1776 | typedef NullaryMemFunT<ClientHttpRequest> Dialer; | |
1777 | AsyncCall::Pointer call = asyncCall(93, 5, "ClientHttpRequest::resumeBodyStorage", | |
1778 | Dialer(this, &ClientHttpRequest::resumeBodyStorage)); | |
1779 | storeEntry()->deferProducer(call); | |
1780 | } | |
1781 | ||
4dc2b072 | 1782 | if (!spaceAvailable) |
0ad2b63b CT |
1783 | return; |
1784 | ||
1785 | if (spaceAvailable < contentSize ) | |
1786 | contentSize = spaceAvailable; | |
1787 | ||
a83c6ed6 | 1788 | BodyPipeCheckout bpc(*adaptedBodySource); |
0ad2b63b | 1789 | const StoreIOBuffer ioBuf(&bpc.buf, request_satisfaction_offset, contentSize); |
5f8252d2 | 1790 | storeEntry()->write(ioBuf); |
0ad2b63b CT |
1791 | // assume StoreEntry::write() writes the entire ioBuf |
1792 | request_satisfaction_offset += ioBuf.length; | |
4ce0e99b | 1793 | bpc.buf.consume(contentSize); |
5f8252d2 | 1794 | bpc.checkIn(); |
1795 | } | |
1796 | ||
a83c6ed6 | 1797 | if (adaptedBodySource->exhausted()) |
5f8252d2 | 1798 | endRequestSatisfaction(); |
1799 | // else wait for more body data | |
de31d06f | 1800 | } |
1801 | ||
1802 | void | |
1cf238db | 1803 | ClientHttpRequest::noteBodyProductionEnded(BodyPipe::Pointer) |
de31d06f | 1804 | { |
a83c6ed6 | 1805 | assert(!virginHeadSource); |
0ad2b63b CT |
1806 | // should we end request satisfaction now? |
1807 | if (adaptedBodySource != NULL && adaptedBodySource->exhausted()) | |
5f8252d2 | 1808 | endRequestSatisfaction(); |
5f8252d2 | 1809 | } |
3b299123 | 1810 | |
5f8252d2 | 1811 | void |
26ac0430 AJ |
1812 | ClientHttpRequest::endRequestSatisfaction() |
1813 | { | |
5f8252d2 | 1814 | debugs(85,4, HERE << this << " ends request satisfaction"); |
1815 | assert(request_satisfaction_mode); | |
a83c6ed6 | 1816 | stopConsumingFrom(adaptedBodySource); |
3b299123 | 1817 | |
5f8252d2 | 1818 | // TODO: anything else needed to end store entry formation correctly? |
1819 | storeEntry()->complete(); | |
1820 | } | |
de31d06f | 1821 | |
5f8252d2 | 1822 | void |
1cf238db | 1823 | ClientHttpRequest::noteBodyProducerAborted(BodyPipe::Pointer) |
5f8252d2 | 1824 | { |
a83c6ed6 AR |
1825 | assert(!virginHeadSource); |
1826 | stopConsumingFrom(adaptedBodySource); | |
eae3a9a6 AR |
1827 | |
1828 | debugs(85,3, HERE << "REQMOD body production failed"); | |
1829 | if (request_satisfaction_mode) { // too late to recover or serve an error | |
1830 | request->detailError(ERR_ICAP_FAILURE, ERR_DETAIL_CLT_REQMOD_RESP_BODY); | |
73c36fd9 | 1831 | const Comm::ConnectionPointer c = getConn()->clientConnection; |
e7cea0ed AJ |
1832 | Must(Comm::IsConnOpen(c)); |
1833 | c->close(); // drastic, but we may be writing a response already | |
eae3a9a6 AR |
1834 | } else { |
1835 | handleAdaptationFailure(ERR_DETAIL_CLT_REQMOD_REQ_BODY); | |
1836 | } | |
5f8252d2 | 1837 | } |
3b299123 | 1838 | |
5f8252d2 | 1839 | void |
64b66b76 | 1840 | ClientHttpRequest::handleAdaptationFailure(int errDetail, bool bypassable) |
5f8252d2 | 1841 | { |
a83c6ed6 | 1842 | debugs(85,3, HERE << "handleAdaptationFailure(" << bypassable << ")"); |
3b299123 | 1843 | |
5f8252d2 | 1844 | const bool usedStore = storeEntry() && !storeEntry()->isEmpty(); |
1845 | const bool usedPipe = request->body_pipe != NULL && | |
26ac0430 | 1846 | request->body_pipe->consumedSize() > 0; |
3b299123 | 1847 | |
9d4d7c5e | 1848 | if (bypassable && !usedStore && !usedPipe) { |
1849 | debugs(85,3, HERE << "ICAP REQMOD callout failed, bypassing: " << calloutContext); | |
5f8252d2 | 1850 | if (calloutContext) |
1851 | doCallouts(); | |
1852 | return; | |
1853 | } | |
3b299123 | 1854 | |
5f8252d2 | 1855 | debugs(85,3, HERE << "ICAP REQMOD callout failed, responding with error"); |
3b299123 | 1856 | |
5f8252d2 | 1857 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
1858 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
1859 | assert(repContext); | |
de31d06f | 1860 | |
26ac0430 | 1861 | // The original author of the code also wanted to pass an errno to |
5f8252d2 | 1862 | // setReplyToError, but it seems unlikely that the errno reflects the |
1863 | // true cause of the error at this point, so I did not pass it. | |
2bd84e5f CT |
1864 | if (calloutContext) { |
1865 | Ip::Address noAddr; | |
1866 | noAddr.SetNoAddr(); | |
1867 | ConnStateData * c = getConn(); | |
1868 | calloutContext->error = clientBuildError(ERR_ICAP_FAILURE, HTTP_INTERNAL_SERVER_ERROR, | |
1869 | NULL, | |
1870 | c != NULL ? c->clientConnection->remote : noAddr, | |
1871 | request | |
1872 | ); | |
daa1b1a7 | 1873 | #if USE_AUTH |
2bd84e5f CT |
1874 | calloutContext->error->auth_user_request = |
1875 | c != NULL && c->auth_user_request != NULL ? c->auth_user_request : request->auth_user_request; | |
daa1b1a7 | 1876 | #endif |
129fe2a1 | 1877 | calloutContext->error->detailError(errDetail); |
2bd84e5f CT |
1878 | calloutContext->readNextRequest = true; |
1879 | c->expectNoForwarding(); | |
1880 | doCallouts(); | |
1881 | } | |
1882 | //else if(calloutContext == NULL) is it possible? | |
de31d06f | 1883 | } |
1884 | ||
1885 | #endif |