]>
Commit | Line | Data |
---|---|---|
edce4d98 | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
0a9297f6 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
edce4d98 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 85 Client-side Request Routines */ |
10 | ||
69660be0 | 11 | /* |
12 | * General logic of request processing: | |
26ac0430 | 13 | * |
69660be0 | 14 | * We run a series of tests to determine if access will be permitted, and to do |
15 | * any redirection. Then we call into the result clientStream to retrieve data. | |
16 | * From that point on it's up to reply management. | |
edce4d98 | 17 | */ |
18 | ||
582c2af2 | 19 | #include "squid.h" |
c0941a6a AR |
20 | #include "acl/FilledChecklist.h" |
21 | #include "acl/Gadgets.h" | |
65d448bc | 22 | #include "anyp/PortCfg.h" |
9e104535 | 23 | #include "base/AsyncJobCalls.h" |
27bc2077 AJ |
24 | #include "client_side.h" |
25 | #include "client_side_reply.h" | |
26 | #include "client_side_request.h" | |
602d9612 | 27 | #include "ClientRequestContext.h" |
582c2af2 | 28 | #include "clientStream.h" |
5c336a3b | 29 | #include "comm/Connection.h" |
ec41b64c | 30 | #include "comm/Write.h" |
582c2af2 | 31 | #include "err_detail_type.h" |
2bd84e5f | 32 | #include "errorpage.h" |
c4ad1349 | 33 | #include "fd.h" |
27bc2077 | 34 | #include "fde.h" |
31971e6a | 35 | #include "format/Token.h" |
d05c079c | 36 | #include "gopher.h" |
e166785a | 37 | #include "helper.h" |
24438ec5 | 38 | #include "helper/Reply.h" |
5c0c642e | 39 | #include "http.h" |
d3dddfb5 | 40 | #include "http/Stream.h" |
ce394734 | 41 | #include "HttpHdrCc.h" |
27bc2077 AJ |
42 | #include "HttpReply.h" |
43 | #include "HttpRequest.h" | |
425de4c8 | 44 | #include "ip/QosConfig.h" |
602d9612 | 45 | #include "ipcache.h" |
1c7ae5ff | 46 | #include "log/access_log.h" |
27bc2077 | 47 | #include "MemObject.h" |
8a01b99e | 48 | #include "Parsing.h" |
582c2af2 | 49 | #include "profiler/Profiler.h" |
e166785a | 50 | #include "redirect.h" |
4d5904f7 | 51 | #include "SquidConfig.h" |
27bc2077 | 52 | #include "SquidTime.h" |
582c2af2 | 53 | #include "Store.h" |
28204b3b | 54 | #include "StrList.h" |
685c6ff5 | 55 | #include "tools.h" |
b1bd952a | 56 | #include "URL.h" |
27bc2077 | 57 | #include "wordlist.h" |
582c2af2 FC |
58 | #if USE_AUTH |
59 | #include "auth/UserRequest.h" | |
60 | #endif | |
61 | #if USE_ADAPTATION | |
62 | #include "adaptation/AccessCheck.h" | |
63 | #include "adaptation/Answer.h" | |
64 | #include "adaptation/Iterator.h" | |
65 | #include "adaptation/Service.h" | |
66 | #if ICAP_CLIENT | |
67 | #include "adaptation/icap/History.h" | |
68 | #endif | |
69 | #endif | |
cb4f4424 | 70 | #if USE_OPENSSL |
2bd84e5f | 71 | #include "ssl/ServerBump.h" |
602d9612 | 72 | #include "ssl/support.h" |
4db984be | 73 | #endif |
3ff65596 | 74 | |
edce4d98 | 75 | #if LINGERING_CLOSE |
76 | #define comm_close comm_lingering_close | |
77 | #endif | |
78 | ||
79 | static const char *const crlf = "\r\n"; | |
80 | ||
609c620a | 81 | #if FOLLOW_X_FORWARDED_FOR |
2efeb0b7 | 82 | static void clientFollowXForwardedForCheck(allow_t answer, void *data); |
609c620a | 83 | #endif /* FOLLOW_X_FORWARDED_FOR */ |
3d674977 | 84 | |
955394ce | 85 | ErrorState *clientBuildError(err_type, Http::StatusCode, char const *url, Ip::Address &, HttpRequest *); |
2bd84e5f | 86 | |
8e2745f4 | 87 | CBDATA_CLASS_INIT(ClientRequestContext); |
88 | ||
edce4d98 | 89 | /* Local functions */ |
edce4d98 | 90 | /* other */ |
2efeb0b7 | 91 | static void clientAccessCheckDoneWrapper(allow_t, void *); |
cb4f4424 | 92 | #if USE_OPENSSL |
2efeb0b7 | 93 | static void sslBumpAccessCheckDoneWrapper(allow_t, void *); |
e0c0d54c | 94 | #endif |
59a1efb2 | 95 | static int clientHierarchical(ClientHttpRequest * http); |
96 | static void clientInterpretRequestHeaders(ClientHttpRequest * http); | |
e166785a | 97 | static HLPCB clientRedirectDoneWrapper; |
a8a0b1c2 | 98 | static HLPCB clientStoreIdDoneWrapper; |
2efeb0b7 | 99 | static void checkNoCacheDoneWrapper(allow_t, void *); |
82afb125 FC |
100 | SQUIDCEXTERN CSR clientGetMoreData; |
101 | SQUIDCEXTERN CSS clientReplyStatus; | |
102 | SQUIDCEXTERN CSD clientReplyDetach; | |
528b2c61 | 103 | static void checkFailureRatio(err_type, hier_code); |
edce4d98 | 104 | |
8e2745f4 | 105 | ClientRequestContext::~ClientRequestContext() |
106 | { | |
de31d06f | 107 | /* |
a546b04b | 108 | * Release our "lock" on our parent, ClientHttpRequest, if we |
109 | * still have one | |
de31d06f | 110 | */ |
a546b04b | 111 | |
112 | if (http) | |
113 | cbdataReferenceDone(http); | |
62e76326 | 114 | |
2bd84e5f | 115 | delete error; |
cc8c4af2 | 116 | debugs(85,3, "ClientRequestContext destructed, this=" << this); |
8e2745f4 | 117 | } |
118 | ||
cc8c4af2 AJ |
119 | ClientRequestContext::ClientRequestContext(ClientHttpRequest *anHttp) : |
120 | http(cbdataReference(anHttp)), | |
121 | acl_checklist(NULL), | |
122 | redirect_state(REDIRECT_NONE), | |
123 | store_id_state(REDIRECT_NONE), | |
124 | host_header_verify_done(false), | |
125 | http_access_done(false), | |
126 | adapted_http_access_done(false), | |
127 | #if USE_ADAPTATION | |
128 | adaptation_acl_check_done(false), | |
129 | #endif | |
130 | redirect_done(false), | |
131 | store_id_done(false), | |
132 | no_cache_done(false), | |
133 | interpreted_req_hdrs(false), | |
134 | tosToClientDone(false), | |
135 | nfmarkToClientDone(false), | |
cb4f4424 | 136 | #if USE_OPENSSL |
cc8c4af2 | 137 | sslBumpCheckDone(false), |
e0c0d54c | 138 | #endif |
cc8c4af2 AJ |
139 | error(NULL), |
140 | readNextRequest(false) | |
141 | { | |
142 | debugs(85, 3, "ClientRequestContext constructed, this=" << this); | |
edce4d98 | 143 | } |
144 | ||
528b2c61 | 145 | CBDATA_CLASS_INIT(ClientHttpRequest); |
8e2745f4 | 146 | |
26ac0430 | 147 | ClientHttpRequest::ClientHttpRequest(ConnStateData * aConn) : |
a83c6ed6 | 148 | #if USE_ADAPTATION |
f53969cc | 149 | AsyncJob("ClientHttpRequest"), |
1cf238db | 150 | #endif |
cc8c4af2 AJ |
151 | request(NULL), |
152 | uri(NULL), | |
153 | log_uri(NULL), | |
154 | req_sz(0), | |
155 | logType(LOG_TAG_NONE), | |
156 | calloutContext(NULL), | |
157 | maxReplyBodySize_(0), | |
158 | entry_(NULL), | |
159 | loggingEntry_(NULL), | |
160 | conn_(NULL) | |
161 | #if USE_OPENSSL | |
162 | , sslBumpNeed_(Ssl::bumpEnd) | |
163 | #endif | |
164 | #if USE_ADAPTATION | |
165 | , request_satisfaction_mode(false) | |
166 | , request_satisfaction_offset(0) | |
167 | #endif | |
528b2c61 | 168 | { |
a0355e95 | 169 | setConn(aConn); |
41ebd397 | 170 | al = new AccessLogEntry; |
af0ded40 | 171 | al->cache.start_time = current_time; |
70b0f938 AJ |
172 | if (aConn) { |
173 | al->tcpClient = clientConnection = aConn->clientConnection; | |
174 | al->cache.port = aConn->port; | |
175 | al->cache.caddr = aConn->log_addr; | |
d4806c91 | 176 | |
cb4f4424 | 177 | #if USE_OPENSSL |
70b0f938 AJ |
178 | if (aConn->clientConnection != NULL && aConn->clientConnection->isOpen()) { |
179 | if (auto ssl = fd_table[aConn->clientConnection->fd].ssl.get()) | |
35b3559c | 180 | al->cache.sslClientCert.resetWithoutLocking(SSL_get_peer_certificate(ssl)); |
70b0f938 | 181 | } |
f4698e0b | 182 | #endif |
70b0f938 | 183 | } |
a0355e95 | 184 | dlinkAdd(this, &active, &ClientActiveRequests); |
528b2c61 | 185 | } |
186 | ||
0655fa4d | 187 | /* |
188 | * returns true if client specified that the object must come from the cache | |
189 | * without contacting origin server | |
190 | */ | |
191 | bool | |
192 | ClientHttpRequest::onlyIfCached()const | |
193 | { | |
194 | assert(request); | |
195 | return request->cache_control && | |
4ce6e3b5 | 196 | request->cache_control->onlyIfCached(); |
0655fa4d | 197 | } |
198 | ||
77aacca5 | 199 | /** |
528b2c61 | 200 | * This function is designed to serve a fairly specific purpose. |
201 | * Occasionally our vBNS-connected caches can talk to each other, but not | |
202 | * the rest of the world. Here we try to detect frequent failures which | |
203 | * make the cache unusable (e.g. DNS lookup and connect() failures). If | |
204 | * the failure:success ratio goes above 1.0 then we go into "hit only" | |
205 | * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors | |
206 | * will only fetch HITs from us if they are using the ICP protocol. We | |
207 | * stay in this mode for 5 minutes. | |
26ac0430 | 208 | * |
528b2c61 | 209 | * Duane W., Sept 16, 1996 |
210 | */ | |
528b2c61 | 211 | static void |
212 | checkFailureRatio(err_type etype, hier_code hcode) | |
213 | { | |
77aacca5 AJ |
214 | // Can be set at compile time with -D compiler flag |
215 | #ifndef FAILURE_MODE_TIME | |
216 | #define FAILURE_MODE_TIME 300 | |
217 | #endif | |
218 | ||
8d74a311 AJ |
219 | if (hcode == HIER_NONE) |
220 | return; | |
221 | ||
222 | // don't bother when ICP is disabled. | |
223 | if (Config.Port.icp <= 0) | |
224 | return; | |
225 | ||
528b2c61 | 226 | static double magic_factor = 100.0; |
227 | double n_good; | |
228 | double n_bad; | |
62e76326 | 229 | |
528b2c61 | 230 | n_good = magic_factor / (1.0 + request_failure_ratio); |
62e76326 | 231 | |
528b2c61 | 232 | n_bad = magic_factor - n_good; |
62e76326 | 233 | |
528b2c61 | 234 | switch (etype) { |
62e76326 | 235 | |
528b2c61 | 236 | case ERR_DNS_FAIL: |
62e76326 | 237 | |
528b2c61 | 238 | case ERR_CONNECT_FAIL: |
3712be3f | 239 | case ERR_SECURE_CONNECT_FAIL: |
62e76326 | 240 | |
528b2c61 | 241 | case ERR_READ_ERROR: |
5086523e | 242 | ++n_bad; |
62e76326 | 243 | break; |
244 | ||
528b2c61 | 245 | default: |
5086523e | 246 | ++n_good; |
528b2c61 | 247 | } |
62e76326 | 248 | |
528b2c61 | 249 | request_failure_ratio = n_bad / n_good; |
62e76326 | 250 | |
528b2c61 | 251 | if (hit_only_mode_until > squid_curtime) |
62e76326 | 252 | return; |
253 | ||
528b2c61 | 254 | if (request_failure_ratio < 1.0) |
62e76326 | 255 | return; |
256 | ||
77aacca5 | 257 | debugs(33, DBG_CRITICAL, "WARNING: Failure Ratio at "<< std::setw(4)<< |
bf8fe701 | 258 | std::setprecision(3) << request_failure_ratio); |
62e76326 | 259 | |
8d74a311 | 260 | debugs(33, DBG_CRITICAL, "WARNING: ICP going into HIT-only mode for " << |
bf8fe701 | 261 | FAILURE_MODE_TIME / 60 << " minutes..."); |
62e76326 | 262 | |
528b2c61 | 263 | hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME; |
62e76326 | 264 | |
f53969cc | 265 | request_failure_ratio = 0.8; /* reset to something less than 1.0 */ |
528b2c61 | 266 | } |
267 | ||
268 | ClientHttpRequest::~ClientHttpRequest() | |
269 | { | |
bf8fe701 | 270 | debugs(33, 3, "httpRequestFree: " << uri); |
72bdee4c | 271 | PROF_start(httpRequestFree); |
62e76326 | 272 | |
5f8252d2 | 273 | // Even though freeResources() below may destroy the request, |
274 | // we no longer set request->body_pipe to NULL here | |
275 | // because we did not initiate that pipe (ConnStateData did) | |
62e76326 | 276 | |
528b2c61 | 277 | /* the ICP check here was erroneous |
26ac0430 | 278 | * - StoreEntry::releaseRequest was always called if entry was valid |
528b2c61 | 279 | */ |
9ce7856a | 280 | |
528b2c61 | 281 | logRequest(); |
9ce7856a | 282 | |
0976f8db | 283 | loggingEntry(NULL); |
284 | ||
528b2c61 | 285 | if (request) |
41ebd397 | 286 | checkFailureRatio(request->errType, al->hier.code); |
62e76326 | 287 | |
528b2c61 | 288 | freeResources(); |
62e76326 | 289 | |
a83c6ed6 AR |
290 | #if USE_ADAPTATION |
291 | announceInitiatorAbort(virginHeadSource); | |
9d4d7c5e | 292 | |
a83c6ed6 AR |
293 | if (adaptedBodySource != NULL) |
294 | stopConsumingFrom(adaptedBodySource); | |
de31d06f | 295 | #endif |
9ce7856a | 296 | |
de31d06f | 297 | if (calloutContext) |
298 | delete calloutContext; | |
299 | ||
be364179 AJ |
300 | clientConnection = NULL; |
301 | ||
26ac0430 AJ |
302 | if (conn_) |
303 | cbdataReferenceDone(conn_); | |
1cf238db | 304 | |
528b2c61 | 305 | /* moving to the next connection is handled by the context free */ |
306 | dlinkDelete(&active, &ClientActiveRequests); | |
9ce7856a | 307 | |
72bdee4c | 308 | PROF_stop(httpRequestFree); |
528b2c61 | 309 | } |
62e76326 | 310 | |
11992b6f AJ |
311 | /** |
312 | * Create a request and kick it off | |
313 | * | |
314 | * \retval 0 success | |
315 | * \retval -1 failure | |
316 | * | |
69660be0 | 317 | * TODO: Pass in the buffers to be used in the inital Read request, as they are |
318 | * determined by the user | |
edce4d98 | 319 | */ |
11992b6f | 320 | int |
60745f24 | 321 | clientBeginRequest(const HttpRequestMethod& method, char const *url, CSCB * streamcallback, |
0655fa4d | 322 | CSD * streamdetach, ClientStreamData streamdata, HttpHeader const *header, |
62e76326 | 323 | char *tailbuf, size_t taillen) |
edce4d98 | 324 | { |
325 | size_t url_sz; | |
a0355e95 | 326 | ClientHttpRequest *http = new ClientHttpRequest(NULL); |
190154cf | 327 | HttpRequest *request; |
528b2c61 | 328 | StoreIOBuffer tempBuffer; |
af0ded40 CT |
329 | if (http->al != NULL) |
330 | http->al->cache.start_time = current_time; | |
edce4d98 | 331 | /* this is only used to adjust the connection offset in client_side.c */ |
332 | http->req_sz = 0; | |
c8be6d7b | 333 | tempBuffer.length = taillen; |
334 | tempBuffer.data = tailbuf; | |
edce4d98 | 335 | /* client stream setup */ |
336 | clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach, | |
0655fa4d | 337 | clientReplyStatus, new clientReplyContext(http), streamcallback, |
62e76326 | 338 | streamdetach, streamdata, tempBuffer); |
edce4d98 | 339 | /* make it visible in the 'current acctive requests list' */ |
edce4d98 | 340 | /* Set flags */ |
a46d2c0e | 341 | /* internal requests only makes sense in an |
342 | * accelerator today. TODO: accept flags ? */ | |
be4d35dc | 343 | http->flags.accel = true; |
edce4d98 | 344 | /* allow size for url rewriting */ |
345 | url_sz = strlen(url) + Config.appendDomainLen + 5; | |
e6ccf245 | 346 | http->uri = (char *)xcalloc(url_sz, 1); |
edce4d98 | 347 | strcpy(http->uri, url); |
348 | ||
d6067ac1 | 349 | if ((request = HttpRequest::CreateFromUrl(http->uri, method)) == NULL) { |
bf8fe701 | 350 | debugs(85, 5, "Invalid URL: " << http->uri); |
62e76326 | 351 | return -1; |
edce4d98 | 352 | } |
62e76326 | 353 | |
69660be0 | 354 | /* |
11992b6f | 355 | * now update the headers in request with our supplied headers. urlParse |
69660be0 | 356 | * should return a blank header set, but we use Update to be sure of |
357 | * correctness. | |
edce4d98 | 358 | */ |
359 | if (header) | |
2d4f252d | 360 | request->header.update(header); |
62e76326 | 361 | |
edce4d98 | 362 | http->log_uri = xstrdup(urlCanonicalClean(request)); |
62e76326 | 363 | |
edce4d98 | 364 | /* http struct now ready */ |
365 | ||
69660be0 | 366 | /* |
367 | * build new header list *? TODO | |
edce4d98 | 368 | */ |
45e5102d | 369 | request->flags.accelerated = http->flags.accel; |
62e76326 | 370 | |
e857372a | 371 | request->flags.internalClient = true; |
a46d2c0e | 372 | |
373 | /* this is an internally created | |
374 | * request, not subject to acceleration | |
375 | * target overrides */ | |
69660be0 | 376 | /* |
377 | * FIXME? Do we want to detect and handle internal requests of internal | |
378 | * objects ? | |
379 | */ | |
edce4d98 | 380 | |
381 | /* Internally created requests cannot have bodies today */ | |
382 | request->content_length = 0; | |
62e76326 | 383 | |
4dd643d5 | 384 | request->client_addr.setNoAddr(); |
62e76326 | 385 | |
3d674977 | 386 | #if FOLLOW_X_FORWARDED_FOR |
4dd643d5 | 387 | request->indirect_client_addr.setNoAddr(); |
3d674977 | 388 | #endif /* FOLLOW_X_FORWARDED_FOR */ |
26ac0430 | 389 | |
f53969cc | 390 | request->my_addr.setNoAddr(); /* undefined for internal requests */ |
62e76326 | 391 | |
4dd643d5 | 392 | request->my_addr.port(0); |
62e76326 | 393 | |
2592bc70 | 394 | request->http_ver = Http::ProtocolVersion(); |
62e76326 | 395 | |
b248c2a3 AJ |
396 | http->request = request; |
397 | HTTPMSGLOCK(http->request); | |
edce4d98 | 398 | |
399 | /* optional - skip the access check ? */ | |
de31d06f | 400 | http->calloutContext = new ClientRequestContext(http); |
401 | ||
57abaac0 | 402 | http->calloutContext->http_access_done = false; |
de31d06f | 403 | |
57abaac0 | 404 | http->calloutContext->redirect_done = true; |
de31d06f | 405 | |
57abaac0 | 406 | http->calloutContext->no_cache_done = true; |
de31d06f | 407 | |
408 | http->doCallouts(); | |
62e76326 | 409 | |
edce4d98 | 410 | return 0; |
411 | } | |
412 | ||
de31d06f | 413 | bool |
414 | ClientRequestContext::httpStateIsValid() | |
415 | { | |
416 | ClientHttpRequest *http_ = http; | |
417 | ||
418 | if (cbdataReferenceValid(http_)) | |
419 | return true; | |
420 | ||
421 | http = NULL; | |
422 | ||
423 | cbdataReferenceDone(http_); | |
424 | ||
425 | return false; | |
426 | } | |
427 | ||
3d674977 AJ |
428 | #if FOLLOW_X_FORWARDED_FOR |
429 | /** | |
a9044668 | 430 | * clientFollowXForwardedForCheck() checks the content of X-Forwarded-For: |
3d674977 AJ |
431 | * against the followXFF ACL, or cleans up and passes control to |
432 | * clientAccessCheck(). | |
d096ace1 AJ |
433 | * |
434 | * The trust model here is a little ambiguous. So to clarify the logic: | |
435 | * - we may always use the direct client address as the client IP. | |
a9044668 | 436 | * - these trust tests merey tell whether we trust given IP enough to believe the |
d096ace1 AJ |
437 | * IP string which it appended to the X-Forwarded-For: header. |
438 | * - if at any point we don't trust what an IP adds we stop looking. | |
439 | * - at that point the current contents of indirect_client_addr are the value set | |
440 | * by the last previously trusted IP. | |
441 | * ++ indirect_client_addr contains the remote direct client from the trusted peers viewpoint. | |
3d674977 | 442 | */ |
3d674977 | 443 | static void |
2efeb0b7 | 444 | clientFollowXForwardedForCheck(allow_t answer, void *data) |
3d674977 AJ |
445 | { |
446 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; | |
3d674977 AJ |
447 | |
448 | if (!calloutContext->httpStateIsValid()) | |
449 | return; | |
450 | ||
d096ace1 AJ |
451 | ClientHttpRequest *http = calloutContext->http; |
452 | HttpRequest *request = http->request; | |
453 | ||
3d674977 AJ |
454 | /* |
455 | * answer should be be ACCESS_ALLOWED or ACCESS_DENIED if we are | |
456 | * called as a result of ACL checks, or -1 if we are called when | |
457 | * there's nothing left to do. | |
458 | */ | |
459 | if (answer == ACCESS_ALLOWED && | |
26ac0430 | 460 | request->x_forwarded_for_iterator.size () != 0) { |
d096ace1 | 461 | |
3d674977 | 462 | /* |
d096ace1 AJ |
463 | * Remove the last comma-delimited element from the |
464 | * x_forwarded_for_iterator and use it to repeat the cycle. | |
465 | */ | |
3d674977 AJ |
466 | const char *p; |
467 | const char *asciiaddr; | |
468 | int l; | |
b7ac5457 | 469 | Ip::Address addr; |
bb790702 | 470 | p = request->x_forwarded_for_iterator.termedBuf(); |
3d674977 AJ |
471 | l = request->x_forwarded_for_iterator.size(); |
472 | ||
473 | /* | |
474 | * XXX x_forwarded_for_iterator should really be a list of | |
475 | * IP addresses, but it's a String instead. We have to | |
476 | * walk backwards through the String, biting off the last | |
477 | * comma-delimited part each time. As long as the data is in | |
478 | * a String, we should probably implement and use a variant of | |
479 | * strListGetItem() that walks backwards instead of forwards | |
480 | * through a comma-separated list. But we don't even do that; | |
481 | * we just do the work in-line here. | |
482 | */ | |
483 | /* skip trailing space and commas */ | |
484 | while (l > 0 && (p[l-1] == ',' || xisspace(p[l-1]))) | |
5e263176 | 485 | --l; |
3d674977 AJ |
486 | request->x_forwarded_for_iterator.cut(l); |
487 | /* look for start of last item in list */ | |
488 | while (l > 0 && ! (p[l-1] == ',' || xisspace(p[l-1]))) | |
5e263176 | 489 | --l; |
3d674977 | 490 | asciiaddr = p+l; |
fafd0efa | 491 | if ((addr = asciiaddr)) { |
3d674977 AJ |
492 | request->indirect_client_addr = addr; |
493 | request->x_forwarded_for_iterator.cut(l); | |
d096ace1 AJ |
494 | calloutContext->acl_checklist = clientAclChecklistCreate(Config.accessList.followXFF, http); |
495 | if (!Config.onoff.acl_uses_indirect_client) { | |
496 | /* override the default src_addr tested if we have to go deeper than one level into XFF */ | |
497 | Filled(calloutContext->acl_checklist)->src_addr = request->indirect_client_addr; | |
3d674977 | 498 | } |
d096ace1 | 499 | calloutContext->acl_checklist->nonBlockingCheck(clientFollowXForwardedForCheck, data); |
3d674977 AJ |
500 | return; |
501 | } | |
502 | } /*if (answer == ACCESS_ALLOWED && | |
503 | request->x_forwarded_for_iterator.size () != 0)*/ | |
504 | ||
505 | /* clean up, and pass control to clientAccessCheck */ | |
26ac0430 | 506 | if (Config.onoff.log_uses_indirect_client) { |
3d674977 AJ |
507 | /* |
508 | * Ensure that the access log shows the indirect client | |
509 | * instead of the direct client. | |
510 | */ | |
511 | ConnStateData *conn = http->getConn(); | |
512 | conn->log_addr = request->indirect_client_addr; | |
d4806c91 | 513 | http->al->cache.caddr = conn->log_addr; |
3d674977 AJ |
514 | } |
515 | request->x_forwarded_for_iterator.clean(); | |
e857372a | 516 | request->flags.done_follow_x_forwarded_for = true; |
3d674977 | 517 | |
d096ace1 AJ |
518 | if (answer != ACCESS_ALLOWED && answer != ACCESS_DENIED) { |
519 | debugs(28, DBG_CRITICAL, "ERROR: Processing X-Forwarded-For. Stopping at IP address: " << request->indirect_client_addr ); | |
493d3865 AJ |
520 | } |
521 | ||
522 | /* process actual access ACL as normal. */ | |
523 | calloutContext->clientAccessCheck(); | |
3d674977 AJ |
524 | } |
525 | #endif /* FOLLOW_X_FORWARDED_FOR */ | |
526 | ||
fe97983f | 527 | static void |
4a3b98d7 | 528 | hostHeaderIpVerifyWrapper(const ipcache_addrs* ia, const Dns::LookupDetails &dns, void *data) |
fe97983f AJ |
529 | { |
530 | ClientRequestContext *c = static_cast<ClientRequestContext*>(data); | |
531 | c->hostHeaderIpVerify(ia, dns); | |
532 | } | |
533 | ||
534 | void | |
4a3b98d7 | 535 | ClientRequestContext::hostHeaderIpVerify(const ipcache_addrs* ia, const Dns::LookupDetails &dns) |
fe97983f AJ |
536 | { |
537 | Comm::ConnectionPointer clientConn = http->getConn()->clientConnection; | |
538 | ||
539 | // note the DNS details for the transaction stats. | |
540 | http->request->recordLookup(dns); | |
541 | ||
542 | if (ia != NULL && ia->count > 0) { | |
543 | // Is the NAT destination IP in DNS? | |
5086523e | 544 | for (int i = 0; i < ia->count; ++i) { |
fe97983f AJ |
545 | if (clientConn->local.matchIPAddr(ia->in_addrs[i]) == 0) { |
546 | debugs(85, 3, HERE << "validate IP " << clientConn->local << " possible from Host:"); | |
e857372a | 547 | http->request->flags.hostVerified = true; |
fe97983f AJ |
548 | http->doCallouts(); |
549 | return; | |
550 | } | |
551 | debugs(85, 3, HERE << "validate IP " << clientConn->local << " non-match from Host: IP " << ia->in_addrs[i]); | |
552 | } | |
553 | } | |
554 | debugs(85, 3, HERE << "FAIL: validate IP " << clientConn->local << " possible from Host:"); | |
05b28f84 | 555 | hostHeaderVerifyFailed("local IP", "any domain IP"); |
fe97983f AJ |
556 | } |
557 | ||
558 | void | |
05b28f84 | 559 | ClientRequestContext::hostHeaderVerifyFailed(const char *A, const char *B) |
fe97983f | 560 | { |
2962f8b8 AJ |
561 | // IP address validation for Host: failed. Admin wants to ignore them. |
562 | // NP: we do not yet handle CONNECT tunnels well, so ignore for them | |
c2a7cefd | 563 | if (!Config.onoff.hostStrictVerify && http->request->method != Http::METHOD_CONNECT) { |
2962f8b8 | 564 | debugs(85, 3, "SECURITY ALERT: Host header forgery detected on " << http->getConn()->clientConnection << |
851feda6 | 565 | " (" << A << " does not match " << B << ") on URL: " << http->request->effectiveRequestUri()); |
2962f8b8 | 566 | |
450fe1cb | 567 | // NP: it is tempting to use 'flags.noCache' but that is all about READing cache data. |
2962f8b8 | 568 | // The problems here are about WRITE for new cache content, which means flags.cachable |
e857372a | 569 | http->request->flags.cachable = false; // MUST NOT cache (for now) |
2962f8b8 | 570 | // XXX: when we have updated the cache key to base on raw-IP + URI this cacheable limit can go. |
e857372a | 571 | http->request->flags.hierarchical = false; // MUST NOT pass to peers (for now) |
2962f8b8 | 572 | // XXX: when we have sorted out the best way to relay requests properly to peers this hierarchical limit can go. |
567fe088 | 573 | http->doCallouts(); |
2962f8b8 AJ |
574 | return; |
575 | } | |
576 | ||
8f489ad7 AJ |
577 | debugs(85, DBG_IMPORTANT, "SECURITY ALERT: Host header forgery detected on " << |
578 | http->getConn()->clientConnection << " (" << A << " does not match " << B << ")"); | |
d610d018 AR |
579 | if (const char *ua = http->request->header.getStr(Http::HdrType::USER_AGENT)) |
580 | debugs(85, DBG_IMPORTANT, "SECURITY ALERT: By user agent: " << ua); | |
851feda6 | 581 | debugs(85, DBG_IMPORTANT, "SECURITY ALERT: on URL: " << http->request->effectiveRequestUri()); |
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); | |
955394ce | 587 | repContext->setReplyToError(ERR_CONFLICT_HOST, Http::scConflict, |
fe97983f AJ |
588 | http->request->method, NULL, |
589 | http->getConn()->clientConnection->remote, | |
590 | http->request, | |
591 | NULL, | |
592 | #if USE_AUTH | |
cc1e110a AJ |
593 | http->getConn() != NULL && http->getConn()->getAuth() != NULL ? |
594 | http->getConn()->getAuth() : http->request->auth_user_request); | |
fe97983f AJ |
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. | |
789217a2 | 606 | const char *host = http->request->header.getStr(Http::HdrType::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 | ||
45e5102d | 616 | if (http->request->flags.internal) { |
8f489ad7 AJ |
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 ':' | |
126e1dc0 AJ |
642 | if (*(++portStr) != '\0') { |
643 | char *end = NULL; | |
644 | int64_t ret = strtoll(portStr, &end, 10); | |
645 | if (end == portStr || *end != '\0' || ret < 1 || ret > 0xFFFF) { | |
646 | // invalid port details. Replace the ':' | |
647 | *(--portStr) = ':'; | |
648 | portStr = NULL; | |
649 | } else | |
650 | port = (ret & 0xFFFF); | |
651 | } | |
fe97983f AJ |
652 | } |
653 | ||
5c51bffb | 654 | debugs(85, 3, "validate host=" << host << ", port=" << port << ", portStr=" << (portStr?portStr:"NULL")); |
0d901ef4 | 655 | if (http->request->flags.intercepted || http->request->flags.interceptTproxy) { |
ba4d9da0 | 656 | // verify the Host: port (if any) matches the apparent destination |
4dd643d5 | 657 | if (portStr && port != http->getConn()->clientConnection->local.port()) { |
5c51bffb | 658 | debugs(85, 3, "FAIL on validate port " << http->getConn()->clientConnection->local.port() << |
05b28f84 AJ |
659 | " matches Host: port " << port << " (" << portStr << ")"); |
660 | hostHeaderVerifyFailed("intercepted port", portStr); | |
ba4d9da0 AJ |
661 | } else { |
662 | // XXX: match the scheme default port against the apparent destination | |
fe97983f | 663 | |
ba4d9da0 AJ |
664 | // verify the destination DNS is one of the Host: headers IPs |
665 | ipcache_nbgethostbyname(host, hostHeaderIpVerifyWrapper, this); | |
666 | } | |
06059513 | 667 | } else if (!Config.onoff.hostStrictVerify) { |
5c51bffb | 668 | debugs(85, 3, "validate skipped."); |
90529125 | 669 | http->doCallouts(); |
5c51bffb | 670 | } else if (strlen(host) != strlen(http->request->url.host())) { |
8f489ad7 | 671 | // Verify forward-proxy requested URL domain matches the Host: header |
5c51bffb AJ |
672 | debugs(85, 3, "FAIL on validate URL domain length " << http->request->url.host() << " matches Host: " << host); |
673 | hostHeaderVerifyFailed(host, http->request->url.host()); | |
674 | } else if (matchDomainName(host, http->request->url.host()) != 0) { | |
ba4d9da0 | 675 | // Verify forward-proxy requested URL domain matches the Host: header |
5c51bffb AJ |
676 | debugs(85, 3, "FAIL on validate URL domain " << http->request->url.host() << " matches Host: " << host); |
677 | hostHeaderVerifyFailed(host, http->request->url.host()); | |
678 | } else if (portStr && port != http->request->url.port()) { | |
ba4d9da0 | 679 | // Verify forward-proxy requested URL domain matches the Host: header |
5c51bffb | 680 | debugs(85, 3, "FAIL on validate URL port " << http->request->url.port() << " matches Host: port " << portStr); |
ba4d9da0 | 681 | hostHeaderVerifyFailed("URL port", portStr); |
5c51bffb | 682 | } else if (!portStr && http->request->method != Http::METHOD_CONNECT && http->request->url.port() != http->request->url.getScheme().defaultPort()) { |
ba4d9da0 | 683 | // Verify forward-proxy requested URL domain matches the Host: header |
65d2fdbf | 684 | // Special case: we don't have a default-port to check for CONNECT. Assume URL is correct. |
5c51bffb | 685 | debugs(85, 3, "FAIL on validate URL port " << http->request->url.port() << " matches Host: default port " << http->request->url.getScheme().defaultPort()); |
ba4d9da0 AJ |
686 | hostHeaderVerifyFailed("URL port", "default port"); |
687 | } else { | |
688 | // Okay no problem. | |
5c51bffb | 689 | debugs(85, 3, "validate passed."); |
e857372a | 690 | http->request->flags.hostVerified = true; |
ba4d9da0 | 691 | http->doCallouts(); |
fe97983f | 692 | } |
ba4d9da0 | 693 | safe_free(hostB); |
fe97983f AJ |
694 | } |
695 | ||
edce4d98 | 696 | /* This is the entry point for external users of the client_side routines */ |
697 | void | |
de31d06f | 698 | ClientRequestContext::clientAccessCheck() |
edce4d98 | 699 | { |
fbe9e379 | 700 | #if FOLLOW_X_FORWARDED_FOR |
f1a1f20a | 701 | if (!http->request->flags.doneFollowXff() && |
26ac0430 | 702 | Config.accessList.followXFF && |
789217a2 | 703 | http->request->header.has(Http::HdrType::X_FORWARDED_FOR)) { |
d096ace1 AJ |
704 | |
705 | /* we always trust the direct client address for actual use */ | |
706 | http->request->indirect_client_addr = http->request->client_addr; | |
4dd643d5 | 707 | http->request->indirect_client_addr.port(0); |
d096ace1 AJ |
708 | |
709 | /* setup the XFF iterator for processing */ | |
789217a2 | 710 | http->request->x_forwarded_for_iterator = http->request->header.getList(Http::HdrType::X_FORWARDED_FOR); |
d096ace1 AJ |
711 | |
712 | /* begin by checking to see if we trust direct client enough to walk XFF */ | |
713 | acl_checklist = clientAclChecklistCreate(Config.accessList.followXFF, http); | |
714 | acl_checklist->nonBlockingCheck(clientFollowXForwardedForCheck, this); | |
3d674977 AJ |
715 | return; |
716 | } | |
fbe9e379 | 717 | #endif |
493d3865 | 718 | |
b50e327b AJ |
719 | if (Config.accessList.http) { |
720 | acl_checklist = clientAclChecklistCreate(Config.accessList.http, http); | |
721 | acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this); | |
722 | } else { | |
723 | debugs(0, DBG_CRITICAL, "No http_access configuration found. This will block ALL traffic"); | |
724 | clientAccessCheckDone(ACCESS_DENIED); | |
725 | } | |
edce4d98 | 726 | } |
727 | ||
533493da AJ |
728 | /** |
729 | * Identical in operation to clientAccessCheck() but performed later using different configured ACL list. | |
730 | * The default here is to allow all. Since the earlier http_access should do a default deny all. | |
731 | * This check is just for a last-minute denial based on adapted request headers. | |
732 | */ | |
733 | void | |
734 | ClientRequestContext::clientAccessCheck2() | |
735 | { | |
736 | if (Config.accessList.adapted_http) { | |
737 | acl_checklist = clientAclChecklistCreate(Config.accessList.adapted_http, http); | |
738 | acl_checklist->nonBlockingCheck(clientAccessCheckDoneWrapper, this); | |
739 | } else { | |
327e2131 | 740 | debugs(85, 2, HERE << "No adapted_http_access configuration. default: ALLOW"); |
533493da AJ |
741 | clientAccessCheckDone(ACCESS_ALLOWED); |
742 | } | |
743 | } | |
744 | ||
edce4d98 | 745 | void |
2efeb0b7 | 746 | clientAccessCheckDoneWrapper(allow_t answer, void *data) |
edce4d98 | 747 | { |
de31d06f | 748 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; |
fbade053 | 749 | |
de31d06f | 750 | if (!calloutContext->httpStateIsValid()) |
62e76326 | 751 | return; |
62e76326 | 752 | |
de31d06f | 753 | calloutContext->clientAccessCheckDone(answer); |
754 | } | |
755 | ||
9d5e7196 AJ |
756 | void |
757 | ClientRequestContext::clientAccessCheckDone(const allow_t &answer) | |
de31d06f | 758 | { |
759 | acl_checklist = NULL; | |
edce4d98 | 760 | err_type page_id; |
955394ce | 761 | Http::StatusCode status; |
7f06a3d8 | 762 | debugs(85, 2, "The request " << http->request->method << ' ' << |
9d5e7196 | 763 | http->uri << " is " << answer << |
6f58d7d7 | 764 | "; last ACL checked: " << (AclMatchedName ? AclMatchedName : "[none]")); |
f5691f9c | 765 | |
2f1431ea AJ |
766 | #if USE_AUTH |
767 | char const *proxy_auth_msg = "<null>"; | |
cc1e110a AJ |
768 | if (http->getConn() != NULL && http->getConn()->getAuth() != NULL) |
769 | proxy_auth_msg = http->getConn()->getAuth()->denyMessage("<null>"); | |
f5691f9c | 770 | else if (http->request->auth_user_request != NULL) |
771 | proxy_auth_msg = http->request->auth_user_request->denyMessage("<null>"); | |
2f1431ea | 772 | #endif |
62e76326 | 773 | |
dd332b92 | 774 | if (answer != ACCESS_ALLOWED) { |
9d5e7196 | 775 | // auth has a grace period where credentials can be expired but okay not to challenge. |
309347ef | 776 | |
9d5e7196 AJ |
777 | /* Send an auth challenge or error */ |
778 | // XXX: do we still need aclIsProxyAuth() ? | |
dd332b92 | 779 | bool auth_challenge = (answer == ACCESS_AUTH_REQUIRED || aclIsProxyAuth(AclMatchedName)); |
9d5e7196 AJ |
780 | debugs(85, 5, "Access Denied: " << http->uri); |
781 | debugs(85, 5, "AclMatchedName = " << (AclMatchedName ? AclMatchedName : "<null>")); | |
2f1431ea | 782 | #if USE_AUTH |
9d5e7196 AJ |
783 | if (auth_challenge) |
784 | debugs(33, 5, "Proxy Auth Message = " << (proxy_auth_msg ? proxy_auth_msg : "<null>")); | |
2f1431ea | 785 | #endif |
9ce7856a | 786 | |
9d5e7196 AJ |
787 | /* |
788 | * NOTE: get page_id here, based on AclMatchedName because if | |
789 | * USE_DELAY_POOLS is enabled, then AclMatchedName gets clobbered in | |
790 | * the clientCreateStoreEntry() call just below. Pedro Ribeiro | |
791 | * <pribeiro@isel.pt> | |
792 | */ | |
793 | page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, answer != ACCESS_AUTH_REQUIRED); | |
9ce7856a | 794 | |
9d5e7196 | 795 | http->logType = LOG_TCP_DENIED; |
62e76326 | 796 | |
9d5e7196 | 797 | if (auth_challenge) { |
2f1431ea | 798 | #if USE_AUTH |
450fe1cb | 799 | if (http->request->flags.sslBumped) { |
9d5e7196 | 800 | /*SSL Bumped request, authentication is not possible*/ |
955394ce | 801 | status = Http::scForbidden; |
9d5e7196 AJ |
802 | } else if (!http->flags.accel) { |
803 | /* Proxy authorisation needed */ | |
955394ce | 804 | status = Http::scProxyAuthenticationRequired; |
9d5e7196 AJ |
805 | } else { |
806 | /* WWW authorisation needed */ | |
955394ce | 807 | status = Http::scUnauthorized; |
9d5e7196 | 808 | } |
ed6163ef | 809 | #else |
9d5e7196 | 810 | // need auth, but not possible to do. |
955394ce | 811 | status = Http::scForbidden; |
ed6163ef | 812 | #endif |
9d5e7196 AJ |
813 | if (page_id == ERR_NONE) |
814 | page_id = ERR_CACHE_ACCESS_DENIED; | |
815 | } else { | |
955394ce | 816 | status = Http::scForbidden; |
ed6163ef | 817 | |
9d5e7196 AJ |
818 | if (page_id == ERR_NONE) |
819 | page_id = ERR_ACCESS_DENIED; | |
820 | } | |
62e76326 | 821 | |
9d5e7196 | 822 | Ip::Address tmpnoaddr; |
4dd643d5 | 823 | tmpnoaddr.setNoAddr(); |
87f237a9 | 824 | error = clientBuildError(page_id, status, |
68715527 CT |
825 | NULL, |
826 | http->getConn() != NULL ? http->getConn()->clientConnection->remote : tmpnoaddr, | |
827 | http->request | |
87f237a9 | 828 | ); |
68715527 | 829 | |
2f1431ea | 830 | #if USE_AUTH |
87f237a9 | 831 | error->auth_user_request = |
cc1e110a AJ |
832 | http->getConn() != NULL && http->getConn()->getAuth() != NULL ? |
833 | http->getConn()->getAuth() : http->request->auth_user_request; | |
2f1431ea | 834 | #endif |
68715527 CT |
835 | |
836 | readNextRequest = true; | |
9d5e7196 | 837 | } |
de31d06f | 838 | |
dd332b92 | 839 | /* ACCESS_ALLOWED continues here ... */ |
3f0e38d6 AJ |
840 | xfree(http->uri); |
841 | http->uri = SBufToCstring(http->request->effectiveRequestUri()); | |
de31d06f | 842 | http->doCallouts(); |
843 | } | |
844 | ||
a83c6ed6 | 845 | #if USE_ADAPTATION |
de31d06f | 846 | void |
79628299 | 847 | ClientHttpRequest::noteAdaptationAclCheckDone(Adaptation::ServiceGroupPointer g) |
de31d06f | 848 | { |
a83c6ed6 | 849 | debugs(93,3,HERE << this << " adaptationAclCheckDone called"); |
6ec67de9 | 850 | |
e1381638 | 851 | #if ICAP_CLIENT |
79628299 | 852 | Adaptation::Icap::History::Pointer ih = request->icapHistory(); |
e1381638 | 853 | if (ih != NULL) { |
6bc2a98d | 854 | if (getConn() != NULL && getConn()->clientConnection != NULL) { |
79628299 | 855 | ih->rfc931 = getConn()->clientConnection->rfc931; |
cb4f4424 | 856 | #if USE_OPENSSL |
6bc2a98d | 857 | if (getConn()->clientConnection->isOpen()) { |
33cc0629 | 858 | ih->ssluser = sslGetUserEmail(fd_table[getConn()->clientConnection->fd].ssl.get()); |
6bc2a98d | 859 | } |
e1381638 | 860 | #endif |
3ff65596 | 861 | } |
79628299 CT |
862 | ih->log_uri = log_uri; |
863 | ih->req_sz = req_sz; | |
3ff65596 AR |
864 | } |
865 | #endif | |
866 | ||
a22e6cd3 AR |
867 | if (!g) { |
868 | debugs(85,3, HERE << "no adaptation needed"); | |
79628299 | 869 | doCallouts(); |
5f8252d2 | 870 | return; |
871 | } | |
de31d06f | 872 | |
79628299 | 873 | startAdaptation(g); |
edce4d98 | 874 | } |
875 | ||
de31d06f | 876 | #endif |
877 | ||
14cc8559 | 878 | static void |
2efeb0b7 | 879 | clientRedirectAccessCheckDone(allow_t answer, void *data) |
14cc8559 | 880 | { |
881 | ClientRequestContext *context = (ClientRequestContext *)data; | |
9d5e7196 AJ |
882 | ClientHttpRequest *http = context->http; |
883 | context->acl_checklist = NULL; | |
14cc8559 | 884 | |
885 | if (answer == ACCESS_ALLOWED) | |
9d5e7196 | 886 | redirectStart(http, clientRedirectDoneWrapper, context); |
bc98bc4b | 887 | else { |
ddc77a2e | 888 | Helper::Reply const nilReply(Helper::Error); |
bc98bc4b AJ |
889 | context->clientRedirectDone(nilReply); |
890 | } | |
14cc8559 | 891 | } |
892 | ||
de31d06f | 893 | void |
894 | ClientRequestContext::clientRedirectStart() | |
14cc8559 | 895 | { |
48e7baac | 896 | debugs(33, 5, HERE << "'" << http->uri << "'"); |
b11724bb | 897 | (void)SyncNotes(*http->al, *http->request); |
14cc8559 | 898 | if (Config.accessList.redirector) { |
de31d06f | 899 | acl_checklist = clientAclChecklistCreate(Config.accessList.redirector, http); |
900 | acl_checklist->nonBlockingCheck(clientRedirectAccessCheckDone, this); | |
14cc8559 | 901 | } else |
de31d06f | 902 | redirectStart(http, clientRedirectDoneWrapper, this); |
14cc8559 | 903 | } |
904 | ||
a8a0b1c2 EC |
905 | /** |
906 | * This methods handles Access checks result of StoreId access list. | |
907 | * Will handle as "ERR" (no change) in a case Access is not allowed. | |
908 | */ | |
909 | static void | |
910 | clientStoreIdAccessCheckDone(allow_t answer, void *data) | |
911 | { | |
912 | ClientRequestContext *context = static_cast<ClientRequestContext *>(data); | |
913 | ClientHttpRequest *http = context->http; | |
914 | context->acl_checklist = NULL; | |
915 | ||
916 | if (answer == ACCESS_ALLOWED) | |
917 | storeIdStart(http, clientStoreIdDoneWrapper, context); | |
918 | else { | |
919 | debugs(85, 3, "access denied expected ERR reply handling: " << answer); | |
ddc77a2e | 920 | Helper::Reply const nilReply(Helper::Error); |
a8a0b1c2 EC |
921 | context->clientStoreIdDone(nilReply); |
922 | } | |
923 | } | |
924 | ||
925 | /** | |
926 | * Start locating an alternative storeage ID string (if any) from admin | |
927 | * configured helper program. This is an asynchronous operation terminating in | |
928 | * ClientRequestContext::clientStoreIdDone() when completed. | |
929 | */ | |
930 | void | |
931 | ClientRequestContext::clientStoreIdStart() | |
932 | { | |
933 | debugs(33, 5,"'" << http->uri << "'"); | |
934 | ||
935 | if (Config.accessList.store_id) { | |
936 | acl_checklist = clientAclChecklistCreate(Config.accessList.store_id, http); | |
937 | acl_checklist->nonBlockingCheck(clientStoreIdAccessCheckDone, this); | |
938 | } else | |
939 | storeIdStart(http, clientStoreIdDoneWrapper, this); | |
940 | } | |
941 | ||
edce4d98 | 942 | static int |
59a1efb2 | 943 | clientHierarchical(ClientHttpRequest * http) |
edce4d98 | 944 | { |
190154cf | 945 | HttpRequest *request = http->request; |
60745f24 | 946 | HttpRequestMethod method = request->method; |
edce4d98 | 947 | |
2962f8b8 | 948 | // intercepted requests MUST NOT (yet) be sent to peers unless verified |
0d901ef4 | 949 | if (!request->flags.hostVerified && (request->flags.intercepted || request->flags.interceptTproxy)) |
2962f8b8 AJ |
950 | return 0; |
951 | ||
69660be0 | 952 | /* |
953 | * IMS needs a private key, so we can use the hierarchy for IMS only if our | |
954 | * neighbors support private keys | |
955 | */ | |
62e76326 | 956 | |
45e5102d | 957 | if (request->flags.ims && !neighbors_do_private_keys) |
62e76326 | 958 | return 0; |
959 | ||
69660be0 | 960 | /* |
961 | * This is incorrect: authenticating requests can be sent via a hierarchy | |
06b97e72 | 962 | * (they can even be cached if the correct headers are set on the reply) |
edce4d98 | 963 | */ |
45e5102d | 964 | if (request->flags.auth) |
62e76326 | 965 | return 0; |
966 | ||
c2a7cefd | 967 | if (method == Http::METHOD_TRACE) |
62e76326 | 968 | return 1; |
969 | ||
c2a7cefd | 970 | if (method != Http::METHOD_GET) |
62e76326 | 971 | return 0; |
972 | ||
450fe1cb | 973 | if (request->flags.loopDetected) |
62e76326 | 974 | return 0; |
975 | ||
4e3f4dc7 | 976 | if (request->url.getScheme() == AnyP::PROTO_HTTP) |
c2a7cefd | 977 | return method.respMaybeCacheable(); |
62e76326 | 978 | |
4e3f4dc7 | 979 | if (request->url.getScheme() == AnyP::PROTO_GOPHER) |
62e76326 | 980 | return gopherCachable(request); |
981 | ||
4e3f4dc7 | 982 | if (request->url.getScheme() == AnyP::PROTO_CACHE_OBJECT) |
62e76326 | 983 | return 0; |
984 | ||
edce4d98 | 985 | return 1; |
986 | } | |
987 | ||
46a1f562 HN |
988 | static void |
989 | clientCheckPinning(ClientHttpRequest * http) | |
990 | { | |
991 | HttpRequest *request = http->request; | |
992 | HttpHeader *req_hdr = &request->header; | |
993 | ConnStateData *http_conn = http->getConn(); | |
994 | ||
995 | /* Internal requests such as those from ESI includes may be without | |
996 | * a client connection | |
997 | */ | |
998 | if (!http_conn) | |
f54f527e | 999 | return; |
46a1f562 | 1000 | |
450fe1cb FC |
1001 | request->flags.connectionAuthDisabled = http_conn->port->connection_auth_disabled; |
1002 | if (!request->flags.connectionAuthDisabled) { | |
73c36fd9 | 1003 | if (Comm::IsConnOpen(http_conn->pinning.serverConnection)) { |
46a1f562 | 1004 | if (http_conn->pinning.auth) { |
e857372a FC |
1005 | request->flags.connectionAuth = true; |
1006 | request->flags.auth = true; | |
46a1f562 | 1007 | } else { |
e857372a | 1008 | request->flags.connectionProxyAuth = true; |
46a1f562 | 1009 | } |
b1cf2350 AJ |
1010 | // These should already be linked correctly. |
1011 | assert(request->clientConnectionManager == http_conn); | |
46a1f562 HN |
1012 | } |
1013 | } | |
1014 | ||
1015 | /* check if connection auth is used, and flag as candidate for pinning | |
45e5102d | 1016 | * in such case. |
450fe1cb | 1017 | * Note: we may need to set flags.connectionAuth even if the connection |
46a1f562 HN |
1018 | * is already pinned if it was pinned earlier due to proxy auth |
1019 | */ | |
450fe1cb | 1020 | if (!request->flags.connectionAuth) { |
789217a2 | 1021 | if (req_hdr->has(Http::HdrType::AUTHORIZATION) || req_hdr->has(Http::HdrType::PROXY_AUTHORIZATION)) { |
46a1f562 HN |
1022 | HttpHeaderPos pos = HttpHeaderInitPos; |
1023 | HttpHeaderEntry *e; | |
1024 | int may_pin = 0; | |
1025 | while ((e = req_hdr->getEntry(&pos))) { | |
789217a2 | 1026 | if (e->id == Http::HdrType::AUTHORIZATION || e->id == Http::HdrType::PROXY_AUTHORIZATION) { |
46a1f562 HN |
1027 | const char *value = e->value.rawBuf(); |
1028 | if (strncasecmp(value, "NTLM ", 5) == 0 | |
1029 | || | |
1030 | strncasecmp(value, "Negotiate ", 10) == 0 | |
1031 | || | |
1032 | strncasecmp(value, "Kerberos ", 9) == 0) { | |
789217a2 | 1033 | if (e->id == Http::HdrType::AUTHORIZATION) { |
e857372a | 1034 | request->flags.connectionAuth = true; |
46a1f562 HN |
1035 | may_pin = 1; |
1036 | } else { | |
e857372a | 1037 | request->flags.connectionProxyAuth = true; |
46a1f562 HN |
1038 | may_pin = 1; |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | } | |
1043 | if (may_pin && !request->pinnedConnection()) { | |
b1cf2350 AJ |
1044 | // These should already be linked correctly. Just need the ServerConnection to pinn. |
1045 | assert(request->clientConnectionManager == http_conn); | |
46a1f562 HN |
1046 | } |
1047 | } | |
1048 | } | |
1049 | } | |
1050 | ||
edce4d98 | 1051 | static void |
59a1efb2 | 1052 | clientInterpretRequestHeaders(ClientHttpRequest * http) |
edce4d98 | 1053 | { |
190154cf | 1054 | HttpRequest *request = http->request; |
0ef77270 | 1055 | HttpHeader *req_hdr = &request->header; |
5086523e | 1056 | bool no_cache = false; |
62e76326 | 1057 | |
edce4d98 | 1058 | request->imslen = -1; |
789217a2 | 1059 | request->ims = req_hdr->getTime(Http::HdrType::IF_MODIFIED_SINCE); |
62e76326 | 1060 | |
edce4d98 | 1061 | if (request->ims > 0) |
e857372a | 1062 | request->flags.ims = true; |
62e76326 | 1063 | |
450fe1cb | 1064 | if (!request->flags.ignoreCc) { |
47fbd2a7 | 1065 | if (request->cache_control) { |
1259f9cf | 1066 | if (request->cache_control->hasNoCache()) |
5086523e | 1067 | no_cache=true; |
62e76326 | 1068 | |
adc2a453 | 1069 | // RFC 2616: treat Pragma:no-cache as if it was Cache-Control:no-cache when Cache-Control is missing |
789217a2 FC |
1070 | } else if (req_hdr->has(Http::HdrType::PRAGMA)) |
1071 | no_cache = req_hdr->hasListMember(Http::HdrType::PRAGMA,"no-cache",','); | |
edce4d98 | 1072 | } |
914b89a2 | 1073 | |
c2a7cefd | 1074 | if (request->method == Http::METHOD_OTHER) { |
5086523e | 1075 | no_cache=true; |
60745f24 | 1076 | } |
62e76326 | 1077 | |
edce4d98 | 1078 | if (no_cache) { |
626096be | 1079 | #if USE_HTTP_VIOLATIONS |
62e76326 | 1080 | |
1081 | if (Config.onoff.reload_into_ims) | |
e857372a | 1082 | request->flags.nocacheHack = true; |
62e76326 | 1083 | else if (refresh_nocache_hack) |
e857372a | 1084 | request->flags.nocacheHack = true; |
62e76326 | 1085 | else |
edce4d98 | 1086 | #endif |
62e76326 | 1087 | |
e857372a | 1088 | request->flags.noCache = true; |
edce4d98 | 1089 | } |
62e76326 | 1090 | |
0ef77270 | 1091 | /* ignore range header in non-GETs or non-HEADs */ |
c2a7cefd | 1092 | if (request->method == Http::METHOD_GET || request->method == Http::METHOD_HEAD) { |
56713d9a AR |
1093 | // XXX: initialize if we got here without HttpRequest::parseHeader() |
1094 | if (!request->range) | |
1095 | request->range = req_hdr->getRange(); | |
62e76326 | 1096 | |
1097 | if (request->range) { | |
e857372a | 1098 | request->flags.isRanged = true; |
62e76326 | 1099 | clientStreamNode *node = (clientStreamNode *)http->client_stream.tail->data; |
1100 | /* XXX: This is suboptimal. We should give the stream the range set, | |
1101 | * and thereby let the top of the stream set the offset when the | |
26ac0430 | 1102 | * size becomes known. As it is, we will end up requesting from 0 |
62e76326 | 1103 | * for evey -X range specification. |
1104 | * RBC - this may be somewhat wrong. We should probably set the range | |
1105 | * iter up at this point. | |
1106 | */ | |
1107 | node->readBuffer.offset = request->range->lowestOffset(0); | |
1108 | http->range_iter.pos = request->range->begin(); | |
24a8eaae | 1109 | http->range_iter.end = request->range->end(); |
62e76326 | 1110 | http->range_iter.valid = true; |
1111 | } | |
edce4d98 | 1112 | } |
62e76326 | 1113 | |
0ef77270 | 1114 | /* Only HEAD and GET requests permit a Range or Request-Range header. |
1115 | * If these headers appear on any other type of request, delete them now. | |
1116 | */ | |
1117 | else { | |
789217a2 FC |
1118 | req_hdr->delById(Http::HdrType::RANGE); |
1119 | req_hdr->delById(Http::HdrType::REQUEST_RANGE); | |
f0baf149 | 1120 | request->ignoreRange("neither HEAD nor GET"); |
0ef77270 | 1121 | } |
1122 | ||
789217a2 | 1123 | if (req_hdr->has(Http::HdrType::AUTHORIZATION)) |
e857372a | 1124 | request->flags.auth = true; |
62e76326 | 1125 | |
46a1f562 | 1126 | clientCheckPinning(http); |
d67acb4e | 1127 | |
92d6986d | 1128 | if (!request->url.userInfo().isEmpty()) |
e857372a | 1129 | request->flags.auth = true; |
62e76326 | 1130 | |
789217a2 FC |
1131 | if (req_hdr->has(Http::HdrType::VIA)) { |
1132 | String s = req_hdr->getList(Http::HdrType::VIA); | |
62e76326 | 1133 | /* |
3c4fcf0f | 1134 | * ThisCache cannot be a member of Via header, "1.1 ThisCache" can. |
62e76326 | 1135 | * Note ThisCache2 has a space prepended to the hostname so we don't |
1136 | * accidentally match super-domains. | |
1137 | */ | |
1138 | ||
1139 | if (strListIsSubstr(&s, ThisCache2, ',')) { | |
1140 | debugObj(33, 1, "WARNING: Forwarding loop detected for:\n", | |
1141 | request, (ObjPackMethod) & httpRequestPack); | |
e857372a | 1142 | request->flags.loopDetected = true; |
62e76326 | 1143 | } |
1144 | ||
21f6708d | 1145 | #if USE_FORW_VIA_DB |
bb790702 | 1146 | fvdbCountVia(s.termedBuf()); |
62e76326 | 1147 | |
edce4d98 | 1148 | #endif |
62e76326 | 1149 | |
30abd221 | 1150 | s.clean(); |
edce4d98 | 1151 | } |
62e76326 | 1152 | |
21f6708d | 1153 | #if USE_FORW_VIA_DB |
62e76326 | 1154 | |
789217a2 FC |
1155 | if (req_hdr->has(Http::HdrType::X_FORWARDED_FOR)) { |
1156 | String s = req_hdr->getList(Http::HdrType::X_FORWARDED_FOR); | |
bb790702 | 1157 | fvdbCountForw(s.termedBuf()); |
30abd221 | 1158 | s.clean(); |
edce4d98 | 1159 | } |
62e76326 | 1160 | |
edce4d98 | 1161 | #endif |
62e76326 | 1162 | |
c2a7cefd | 1163 | request->flags.cachable = http->request->maybeCacheable(); |
62e76326 | 1164 | |
edce4d98 | 1165 | if (clientHierarchical(http)) |
e857372a | 1166 | request->flags.hierarchical = true; |
62e76326 | 1167 | |
bf8fe701 | 1168 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_NOCACHE = " << |
450fe1cb | 1169 | (request->flags.noCache ? "SET" : "NOT SET")); |
bf8fe701 | 1170 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_CACHABLE = " << |
45e5102d | 1171 | (request->flags.cachable ? "SET" : "NOT SET")); |
bf8fe701 | 1172 | debugs(85, 5, "clientInterpretRequestHeaders: REQ_HIERARCHICAL = " << |
45e5102d | 1173 | (request->flags.hierarchical ? "SET" : "NOT SET")); |
62e76326 | 1174 | |
edce4d98 | 1175 | } |
1176 | ||
1177 | void | |
24438ec5 | 1178 | clientRedirectDoneWrapper(void *data, const Helper::Reply &result) |
edce4d98 | 1179 | { |
de31d06f | 1180 | ClientRequestContext *calloutContext = (ClientRequestContext *)data; |
db02222f | 1181 | |
de31d06f | 1182 | if (!calloutContext->httpStateIsValid()) |
62e76326 | 1183 | return; |
62e76326 | 1184 | |
de31d06f | 1185 | calloutContext->clientRedirectDone(result); |
1186 | } | |
1187 | ||
a8a0b1c2 | 1188 | void |
24438ec5 | 1189 | clientStoreIdDoneWrapper(void *data, const Helper::Reply &result) |
a8a0b1c2 EC |
1190 | { |
1191 | ClientRequestContext *calloutContext = (ClientRequestContext *)data; | |
1192 | ||
1193 | if (!calloutContext->httpStateIsValid()) | |
1194 | return; | |
1195 | ||
1196 | calloutContext->clientStoreIdDone(result); | |
1197 | } | |
1198 | ||
de31d06f | 1199 | void |
24438ec5 | 1200 | ClientRequestContext::clientRedirectDone(const Helper::Reply &reply) |
de31d06f | 1201 | { |
190154cf | 1202 | HttpRequest *old_request = http->request; |
e166785a | 1203 | debugs(85, 5, HERE << "'" << http->uri << "' result=" << reply); |
de31d06f | 1204 | assert(redirect_state == REDIRECT_PENDING); |
1205 | redirect_state = REDIRECT_DONE; | |
62e76326 | 1206 | |
cf9f0261 | 1207 | // Put helper response Notes into the transaction state record (ALE) eventually |
d06e17ea | 1208 | // do it early to ensure that no matter what the outcome the notes are present. |
457857fe CT |
1209 | if (http->al != NULL) |
1210 | (void)SyncNotes(*http->al, *old_request); | |
1211 | ||
1212 | UpdateRequestNotes(http->getConn(), *old_request, reply.notes); | |
d06e17ea | 1213 | |
63fc9fb5 | 1214 | switch (reply.result) { |
32fd6d8a CT |
1215 | case Helper::TimedOut: |
1216 | if (Config.onUrlRewriteTimeout.action != toutActBypass) { | |
1217 | http->calloutsError(ERR_GATEWAY_FAILURE, ERR_DETAIL_REDIRECTOR_TIMEDOUT); | |
1218 | debugs(85, DBG_IMPORTANT, "ERROR: URL rewrite helper: Timedout"); | |
1219 | } | |
1220 | break; | |
1221 | ||
2428ce02 AJ |
1222 | case Helper::Unknown: |
1223 | case Helper::TT: | |
d06e17ea AJ |
1224 | // Handler in redirect.cc should have already mapped Unknown |
1225 | // IF it contained valid entry for the old URL-rewrite helper protocol | |
1226 | debugs(85, DBG_IMPORTANT, "ERROR: URL rewrite helper returned invalid result code. Wrong helper? " << reply); | |
1227 | break; | |
1228 | ||
2428ce02 | 1229 | case Helper::BrokenHelper: |
32fd6d8a | 1230 | debugs(85, DBG_IMPORTANT, "ERROR: URL rewrite helper: " << reply); |
d06e17ea AJ |
1231 | break; |
1232 | ||
2428ce02 | 1233 | case Helper::Error: |
d06e17ea AJ |
1234 | // no change to be done. |
1235 | break; | |
62e76326 | 1236 | |
2428ce02 | 1237 | case Helper::Okay: { |
d06e17ea AJ |
1238 | // #1: redirect with a specific status code OK status=NNN url="..." |
1239 | // #2: redirect with a default status code OK url="..." | |
1240 | // #3: re-write the URL OK rewrite-url="..." | |
1241 | ||
cf9f0261 CT |
1242 | const char *statusNote = reply.notes.findFirst("status"); |
1243 | const char *urlNote = reply.notes.findFirst("url"); | |
d06e17ea AJ |
1244 | |
1245 | if (urlNote != NULL) { | |
1246 | // HTTP protocol redirect to be done. | |
1247 | ||
1248 | // TODO: change default redirect status for appropriate requests | |
1249 | // Squid defaults to 302 status for now for better compatibility with old clients. | |
f11c8e2f | 1250 | // HTTP/1.0 client should get 302 (Http::scFound) |
955394ce AJ |
1251 | // HTTP/1.1 client contacting reverse-proxy should get 307 (Http::scTemporaryRedirect) |
1252 | // HTTP/1.1 client being diverted by forward-proxy should get 303 (Http::scSeeOther) | |
f11c8e2f | 1253 | Http::StatusCode status = Http::scFound; |
d06e17ea | 1254 | if (statusNote != NULL) { |
cf9f0261 | 1255 | const char * result = statusNote; |
955394ce | 1256 | status = static_cast<Http::StatusCode>(atoi(result)); |
d06e17ea | 1257 | } |
62e76326 | 1258 | |
955394ce | 1259 | if (status == Http::scMovedPermanently |
f11c8e2f | 1260 | || status == Http::scFound |
955394ce AJ |
1261 | || status == Http::scSeeOther |
1262 | || status == Http::scPermanentRedirect | |
1263 | || status == Http::scTemporaryRedirect) { | |
62e76326 | 1264 | http->redirect.status = status; |
cf9f0261 | 1265 | http->redirect.location = xstrdup(urlNote); |
e5b677f0 | 1266 | // TODO: validate the URL produced here is RFC 2616 compliant absolute URI |
62e76326 | 1267 | } else { |
cf9f0261 | 1268 | debugs(85, DBG_CRITICAL, "ERROR: URL-rewrite produces invalid " << status << " redirect Location: " << urlNote); |
62e76326 | 1269 | } |
d06e17ea AJ |
1270 | } else { |
1271 | // URL-rewrite wanted. Ew. | |
cf9f0261 | 1272 | urlNote = reply.notes.findFirst("rewrite-url"); |
d06e17ea AJ |
1273 | |
1274 | // prevent broken helpers causing too much damage. If old URL == new URL skip the re-write. | |
cf9f0261 | 1275 | if (urlNote != NULL && strcmp(urlNote, http->uri)) { |
d06e17ea AJ |
1276 | // XXX: validate the URL properly *without* generating a whole new request object right here. |
1277 | // XXX: the clone() should be done only AFTER we know the new URL is valid. | |
1278 | HttpRequest *new_request = old_request->clone(); | |
cf9f0261 | 1279 | if (urlParse(old_request->method, const_cast<char*>(urlNote), new_request)) { |
851feda6 | 1280 | debugs(61, 2, "URL-rewriter diverts URL from " << old_request->effectiveRequestUri() << " to " << new_request->effectiveRequestUri()); |
d06e17ea AJ |
1281 | |
1282 | // update the new request to flag the re-writing was done on it | |
e857372a | 1283 | new_request->flags.redirected = true; |
d06e17ea AJ |
1284 | |
1285 | // unlink bodypipe from the old request. Not needed there any longer. | |
1286 | if (old_request->body_pipe != NULL) { | |
1287 | old_request->body_pipe = NULL; | |
1288 | debugs(61,2, HERE << "URL-rewriter diverts body_pipe " << new_request->body_pipe << | |
1289 | " from request " << old_request << " to " << new_request); | |
1290 | } | |
9be14530 | 1291 | |
d06e17ea | 1292 | // update the current working ClientHttpRequest fields |
3f0e38d6 AJ |
1293 | xfree(http->uri); |
1294 | http->uri = SBufToCstring(new_request->effectiveRequestUri()); | |
d06e17ea | 1295 | HTTPMSGUNLOCK(old_request); |
b248c2a3 AJ |
1296 | http->request = new_request; |
1297 | HTTPMSGLOCK(http->request); | |
d06e17ea AJ |
1298 | } else { |
1299 | debugs(85, DBG_CRITICAL, "ERROR: URL-rewrite produces invalid request: " << | |
cf9f0261 | 1300 | old_request->method << " " << urlNote << " " << old_request->http_ver); |
d06e17ea AJ |
1301 | delete new_request; |
1302 | } | |
9be14530 | 1303 | } |
74b48915 | 1304 | } |
edce4d98 | 1305 | } |
d06e17ea AJ |
1306 | break; |
1307 | } | |
62e76326 | 1308 | |
edce4d98 | 1309 | /* FIXME PIPELINE: This is innacurate during pipelining */ |
62e76326 | 1310 | |
73c36fd9 AJ |
1311 | if (http->getConn() != NULL && Comm::IsConnOpen(http->getConn()->clientConnection)) |
1312 | fd_note(http->getConn()->clientConnection->fd, http->uri); | |
62e76326 | 1313 | |
c8be6d7b | 1314 | assert(http->uri); |
62e76326 | 1315 | |
de31d06f | 1316 | http->doCallouts(); |
edce4d98 | 1317 | } |
1318 | ||
a8a0b1c2 EC |
1319 | /** |
1320 | * This method handles the different replies from StoreID helper. | |
1321 | */ | |
1322 | void | |
24438ec5 | 1323 | ClientRequestContext::clientStoreIdDone(const Helper::Reply &reply) |
a8a0b1c2 EC |
1324 | { |
1325 | HttpRequest *old_request = http->request; | |
1326 | debugs(85, 5, "'" << http->uri << "' result=" << reply); | |
1327 | assert(store_id_state == REDIRECT_PENDING); | |
1328 | store_id_state = REDIRECT_DONE; | |
1329 | ||
cf9f0261 | 1330 | // Put helper response Notes into the transaction state record (ALE) eventually |
a8a0b1c2 | 1331 | // do it early to ensure that no matter what the outcome the notes are present. |
457857fe CT |
1332 | if (http->al != NULL) |
1333 | (void)SyncNotes(*http->al, *old_request); | |
1334 | ||
1335 | UpdateRequestNotes(http->getConn(), *old_request, reply.notes); | |
a8a0b1c2 EC |
1336 | |
1337 | switch (reply.result) { | |
2428ce02 AJ |
1338 | case Helper::Unknown: |
1339 | case Helper::TT: | |
a8a0b1c2 EC |
1340 | // Handler in redirect.cc should have already mapped Unknown |
1341 | // IF it contained valid entry for the old helper protocol | |
1342 | debugs(85, DBG_IMPORTANT, "ERROR: storeID helper returned invalid result code. Wrong helper? " << reply); | |
1343 | break; | |
1344 | ||
32fd6d8a | 1345 | case Helper::TimedOut: |
f53969cc | 1346 | // Timeouts for storeID are not implemented |
2428ce02 | 1347 | case Helper::BrokenHelper: |
32fd6d8a | 1348 | debugs(85, DBG_IMPORTANT, "ERROR: storeID helper: " << reply); |
a8a0b1c2 EC |
1349 | break; |
1350 | ||
2428ce02 | 1351 | case Helper::Error: |
a8a0b1c2 EC |
1352 | // no change to be done. |
1353 | break; | |
1354 | ||
2428ce02 | 1355 | case Helper::Okay: { |
cf9f0261 | 1356 | const char *urlNote = reply.notes.findFirst("store-id"); |
a8a0b1c2 EC |
1357 | |
1358 | // prevent broken helpers causing too much damage. If old URL == new URL skip the re-write. | |
cf9f0261 | 1359 | if (urlNote != NULL && strcmp(urlNote, http->uri) ) { |
a8a0b1c2 | 1360 | // Debug section required for some very specific cases. |
cf9f0261 CT |
1361 | debugs(85, 9, "Setting storeID with: " << urlNote ); |
1362 | http->request->store_id = urlNote; | |
1363 | http->store_id = urlNote; | |
a8a0b1c2 EC |
1364 | } |
1365 | } | |
1366 | break; | |
1367 | } | |
1368 | ||
1369 | http->doCallouts(); | |
1370 | } | |
1371 | ||
b50e327b AJ |
1372 | /** Test cache allow/deny configuration |
1373 | * Sets flags.cachable=1 if caching is not denied. | |
1374 | */ | |
edce4d98 | 1375 | void |
8e2745f4 | 1376 | ClientRequestContext::checkNoCache() |
edce4d98 | 1377 | { |
b50e327b AJ |
1378 | if (Config.accessList.noCache) { |
1379 | acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http); | |
1380 | acl_checklist->nonBlockingCheck(checkNoCacheDoneWrapper, this); | |
1381 | } else { | |
1382 | /* unless otherwise specified, we try to cache. */ | |
2efeb0b7 | 1383 | checkNoCacheDone(ACCESS_ALLOWED); |
b50e327b | 1384 | } |
edce4d98 | 1385 | } |
1386 | ||
de31d06f | 1387 | static void |
2efeb0b7 | 1388 | checkNoCacheDoneWrapper(allow_t answer, void *data) |
edce4d98 | 1389 | { |
de31d06f | 1390 | ClientRequestContext *calloutContext = (ClientRequestContext *) data; |
e4a67a80 | 1391 | |
de31d06f | 1392 | if (!calloutContext->httpStateIsValid()) |
1393 | return; | |
1394 | ||
1395 | calloutContext->checkNoCacheDone(answer); | |
8e2745f4 | 1396 | } |
4fb35c3c | 1397 | |
8e2745f4 | 1398 | void |
2efeb0b7 | 1399 | ClientRequestContext::checkNoCacheDone(const allow_t &answer) |
62e76326 | 1400 | { |
8e2745f4 | 1401 | acl_checklist = NULL; |
58e1b950 AJ |
1402 | if (answer == ACCESS_DENIED) { |
1403 | http->request->flags.noCache = true; // dont read reply from cache | |
1404 | http->request->flags.cachable = false; // dont store reply into cache | |
1405 | } | |
de31d06f | 1406 | http->doCallouts(); |
edce4d98 | 1407 | } |
1408 | ||
cb4f4424 | 1409 | #if USE_OPENSSL |
e0c0d54c CT |
1410 | bool |
1411 | ClientRequestContext::sslBumpAccessCheck() | |
1412 | { | |
4b5ea8a6 CT |
1413 | if (!http->getConn()) { |
1414 | http->al->ssl.bumpMode = Ssl::bumpEnd; // SslBump does not apply; log - | |
1415 | return false; | |
1416 | } | |
1417 | ||
6b2b6cfe CT |
1418 | if (http->request->flags.forceTunnel) { |
1419 | debugs(85, 5, "not needed; already decided to tunnel " << http->getConn()); | |
1420 | return false; | |
1421 | } | |
1422 | ||
08097970 AR |
1423 | // If SSL connection tunneling or bumping decision has been made, obey it. |
1424 | const Ssl::BumpMode bumpMode = http->getConn()->sslBumpMode; | |
1425 | if (bumpMode != Ssl::bumpEnd) { | |
1426 | debugs(85, 5, HERE << "SslBump already decided (" << bumpMode << | |
1427 | "), " << "ignoring ssl_bump for " << http->getConn()); | |
b4049e38 CT |
1428 | if (!http->getConn()->serverBump()) |
1429 | http->sslBumpNeed(bumpMode); // for processRequest() to bump if needed and not already bumped | |
71cae389 | 1430 | http->al->ssl.bumpMode = bumpMode; // inherited from bumped connection |
08097970 AR |
1431 | return false; |
1432 | } | |
e0c0d54c | 1433 | |
08097970 AR |
1434 | // If we have not decided yet, decide whether to bump now. |
1435 | ||
1436 | // Bumping here can only start with a CONNECT request on a bumping port | |
1437 | // (bumping of intercepted SSL conns is decided before we get 1st request). | |
1438 | // We also do not bump redirected CONNECT requests. | |
c2a7cefd | 1439 | if (http->request->method != Http::METHOD_CONNECT || http->redirect.status || |
6a25a046 FC |
1440 | !Config.accessList.ssl_bump || |
1441 | !http->getConn()->port->flags.tunnelSslBumping) { | |
71cae389 | 1442 | http->al->ssl.bumpMode = Ssl::bumpEnd; // SslBump does not apply; log - |
08097970 | 1443 | debugs(85, 5, HERE << "cannot SslBump this request"); |
e0c0d54c CT |
1444 | return false; |
1445 | } | |
08097970 AR |
1446 | |
1447 | // Do not bump during authentication: clients would not proxy-authenticate | |
1448 | // if we delay a 407 response and respond with 200 OK to CONNECT. | |
955394ce | 1449 | if (error && error->httpStatus == Http::scProxyAuthenticationRequired) { |
71cae389 | 1450 | http->al->ssl.bumpMode = Ssl::bumpEnd; // SslBump does not apply; log - |
08097970 AR |
1451 | debugs(85, 5, HERE << "no SslBump during proxy authentication"); |
1452 | return false; | |
1453 | } | |
1454 | ||
1455 | debugs(85, 5, HERE << "SslBump possible, checking ACL"); | |
1456 | ||
eb898410 AJ |
1457 | ACLFilledChecklist *aclChecklist = clientAclChecklistCreate(Config.accessList.ssl_bump, http); |
1458 | aclChecklist->nonBlockingCheck(sslBumpAccessCheckDoneWrapper, this); | |
08097970 | 1459 | return true; |
e0c0d54c CT |
1460 | } |
1461 | ||
f8901ea9 | 1462 | /** |
e0c0d54c CT |
1463 | * A wrapper function to use the ClientRequestContext::sslBumpAccessCheckDone method |
1464 | * as ACLFilledChecklist callback | |
1465 | */ | |
1466 | static void | |
2efeb0b7 | 1467 | sslBumpAccessCheckDoneWrapper(allow_t answer, void *data) |
e0c0d54c CT |
1468 | { |
1469 | ClientRequestContext *calloutContext = static_cast<ClientRequestContext *>(data); | |
9d5e7196 AJ |
1470 | |
1471 | if (!calloutContext->httpStateIsValid()) | |
1472 | return; | |
ed6163ef | 1473 | calloutContext->sslBumpAccessCheckDone(answer); |
e0c0d54c CT |
1474 | } |
1475 | ||
1476 | void | |
ed6163ef | 1477 | ClientRequestContext::sslBumpAccessCheckDone(const allow_t &answer) |
e0c0d54c | 1478 | { |
ed6163ef AJ |
1479 | if (!httpStateIsValid()) |
1480 | return; | |
1481 | ||
08097970 | 1482 | const Ssl::BumpMode bumpMode = answer == ACCESS_ALLOWED ? |
87f237a9 | 1483 | static_cast<Ssl::BumpMode>(answer.kind) : Ssl::bumpNone; |
08097970 | 1484 | http->sslBumpNeed(bumpMode); // for processRequest() to bump if needed |
71cae389 | 1485 | http->al->ssl.bumpMode = bumpMode; // for logging |
caf3666d | 1486 | |
e0c0d54c CT |
1487 | http->doCallouts(); |
1488 | } | |
1489 | #endif | |
1490 | ||
69660be0 | 1491 | /* |
1492 | * Identify requests that do not go through the store and client side stream | |
1493 | * and forward them to the appropriate location. All other requests, request | |
1494 | * them. | |
edce4d98 | 1495 | */ |
1496 | void | |
8e2745f4 | 1497 | ClientHttpRequest::processRequest() |
edce4d98 | 1498 | { |
7f06a3d8 | 1499 | debugs(85, 4, request->method << ' ' << uri); |
62e76326 | 1500 | |
6b2b6cfe CT |
1501 | const bool untouchedConnect = request->method == Http::METHOD_CONNECT && !redirect.status; |
1502 | ||
cb4f4424 | 1503 | #if USE_OPENSSL |
6b2b6cfe CT |
1504 | if (untouchedConnect && sslBumpNeeded()) { |
1505 | assert(!request->flags.forceTunnel); | |
1506 | sslBumpStart(); | |
1507 | return; | |
1508 | } | |
3712be3f | 1509 | #endif |
6b2b6cfe CT |
1510 | |
1511 | if (untouchedConnect || request->flags.forceTunnel) { | |
f84dd7eb | 1512 | getConn()->stopReading(); // tunnels read for themselves |
ac9f46af | 1513 | tunnelStart(this); |
62e76326 | 1514 | return; |
edce4d98 | 1515 | } |
62e76326 | 1516 | |
8e2745f4 | 1517 | httpStart(); |
1518 | } | |
1519 | ||
1520 | void | |
1521 | ClientHttpRequest::httpStart() | |
1522 | { | |
559da936 | 1523 | PROF_start(httpStart); |
8e2745f4 | 1524 | logType = LOG_TAG_NONE; |
91369933 | 1525 | debugs(85, 4, logType.c_str() << " for '" << uri << "'"); |
bf8fe701 | 1526 | |
edce4d98 | 1527 | /* no one should have touched this */ |
8e2745f4 | 1528 | assert(out.offset == 0); |
edce4d98 | 1529 | /* Use the Stream Luke */ |
8e2745f4 | 1530 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->data; |
1531 | clientStreamRead(node, this, node->readBuffer); | |
559da936 | 1532 | PROF_stop(httpStart); |
edce4d98 | 1533 | } |
0655fa4d | 1534 | |
cb4f4424 | 1535 | #if USE_OPENSSL |
3712be3f | 1536 | |
e0c0d54c | 1537 | void |
08097970 | 1538 | ClientHttpRequest::sslBumpNeed(Ssl::BumpMode mode) |
e0c0d54c | 1539 | { |
caf3666d | 1540 | debugs(83, 3, HERE << "sslBump required: "<< Ssl::bumpMode(mode)); |
08097970 | 1541 | sslBumpNeed_ = mode; |
3712be3f | 1542 | } |
1543 | ||
1544 | // called when comm_write has completed | |
1545 | static void | |
c8407295 | 1546 | SslBumpEstablish(const Comm::ConnectionPointer &, char *, size_t, Comm::Flag errflag, int, void *data) |
3712be3f | 1547 | { |
1548 | ClientHttpRequest *r = static_cast<ClientHttpRequest*>(data); | |
1549 | debugs(85, 5, HERE << "responded to CONNECT: " << r << " ? " << errflag); | |
1550 | ||
1551 | assert(r && cbdataReferenceValid(r)); | |
1552 | r->sslBumpEstablish(errflag); | |
1553 | } | |
1554 | ||
1555 | void | |
c8407295 | 1556 | ClientHttpRequest::sslBumpEstablish(Comm::Flag errflag) |
3712be3f | 1557 | { |
c8407295 AJ |
1558 | // Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up |
1559 | if (errflag == Comm::ERR_CLOSING) | |
3712be3f | 1560 | return; |
1561 | ||
1562 | if (errflag) { | |
1fa35be8 | 1563 | debugs(85, 3, HERE << "CONNECT response failure in SslBump: " << errflag); |
bbc83914 | 1564 | getConn()->clientConnection->close(); |
3712be3f | 1565 | return; |
1566 | } | |
1567 | ||
40d8be97 AR |
1568 | // We lack HttpReply which logRequest() uses to log the status code. |
1569 | // TODO: Use HttpReply instead of the "200 Connection established" string. | |
71cae389 | 1570 | al->http.code = 200; |
40d8be97 | 1571 | |
21512911 CT |
1572 | #if USE_AUTH |
1573 | // Preserve authentication info for the ssl-bumped request | |
1574 | if (request->auth_user_request != NULL) | |
cc1e110a | 1575 | getConn()->setAuth(request->auth_user_request, "SSL-bumped CONNECT"); |
21512911 | 1576 | #endif |
03f00a11 | 1577 | |
08097970 AR |
1578 | assert(sslBumpNeeded()); |
1579 | getConn()->switchToHttps(request, sslBumpNeed_); | |
3712be3f | 1580 | } |
1581 | ||
1582 | void | |
1583 | ClientHttpRequest::sslBumpStart() | |
1584 | { | |
08097970 AR |
1585 | debugs(85, 5, HERE << "Confirming " << Ssl::bumpMode(sslBumpNeed_) << |
1586 | "-bumped CONNECT tunnel on FD " << getConn()->clientConnection); | |
1587 | getConn()->sslBumpMode = sslBumpNeed_; | |
3712be3f | 1588 | |
9e104535 | 1589 | AsyncCall::Pointer bumpCall = commCbCall(85, 5, "ClientSocketContext::sslBumpEstablish", |
f53969cc | 1590 | CommIoCbPtrFun(&SslBumpEstablish, this)); |
9e104535 CT |
1591 | |
1592 | if (request->flags.interceptTproxy || request->flags.intercepted) { | |
1593 | CommIoCbParams ¶ms = GetCommParams<CommIoCbParams>(bumpCall); | |
1594 | params.flag = Comm::OK; | |
1595 | params.conn = getConn()->clientConnection; | |
1596 | ScheduleCallHere(bumpCall); | |
1597 | return; | |
1598 | } | |
1599 | ||
08097970 | 1600 | // send an HTTP 200 response to kick client SSL negotiation |
3712be3f | 1601 | // TODO: Unify with tunnel.cc and add a Server(?) header |
b0388924 | 1602 | static const char *const conn_established = "HTTP/1.1 200 Connection established\r\n\r\n"; |
9e104535 | 1603 | Comm::Write(getConn()->clientConnection, conn_established, strlen(conn_established), bumpCall, NULL); |
3712be3f | 1604 | } |
1605 | ||
1606 | #endif | |
1607 | ||
0655fa4d | 1608 | bool |
1609 | ClientHttpRequest::gotEnough() const | |
1610 | { | |
86a2f789 | 1611 | /** TODO: should be querying the stream. */ |
7173d5b0 | 1612 | int64_t contentLength = |
06a5ae20 | 1613 | memObject()->getReply()->bodySize(request->method); |
0655fa4d | 1614 | assert(contentLength >= 0); |
1615 | ||
1616 | if (out.offset < contentLength) | |
1617 | return false; | |
1618 | ||
1619 | return true; | |
1620 | } | |
1621 | ||
86a2f789 | 1622 | void |
1623 | ClientHttpRequest::storeEntry(StoreEntry *newEntry) | |
1624 | { | |
1625 | entry_ = newEntry; | |
1626 | } | |
1627 | ||
0976f8db | 1628 | void |
1629 | ClientHttpRequest::loggingEntry(StoreEntry *newEntry) | |
1630 | { | |
1631 | if (loggingEntry_) | |
1bfe9ade | 1632 | loggingEntry_->unlock("ClientHttpRequest::loggingEntry"); |
0976f8db | 1633 | |
1634 | loggingEntry_ = newEntry; | |
1635 | ||
1636 | if (loggingEntry_) | |
1bfe9ade | 1637 | loggingEntry_->lock("ClientHttpRequest::loggingEntry"); |
0976f8db | 1638 | } |
86a2f789 | 1639 | |
de31d06f | 1640 | /* |
1641 | * doCallouts() - This function controls the order of "callout" | |
1642 | * executions, including non-blocking access control checks, the | |
1643 | * redirector, and ICAP. Previously, these callouts were chained | |
1644 | * together such that "clientAccessCheckDone()" would call | |
1645 | * "clientRedirectStart()" and so on. | |
1646 | * | |
1647 | * The ClientRequestContext (aka calloutContext) class holds certain | |
1648 | * state data for the callout/callback operations. Previously | |
1649 | * ClientHttpRequest would sort of hand off control to ClientRequestContext | |
1650 | * for a short time. ClientRequestContext would then delete itself | |
1651 | * and pass control back to ClientHttpRequest when all callouts | |
1652 | * were finished. | |
1653 | * | |
1654 | * This caused some problems for ICAP because we want to make the | |
1655 | * ICAP callout after checking ACLs, but before checking the no_cache | |
1656 | * list. We can't stuff the ICAP state into the ClientRequestContext | |
1657 | * class because we still need the ICAP state after ClientRequestContext | |
1658 | * goes away. | |
1659 | * | |
1660 | * Note that ClientRequestContext is created before the first call | |
1661 | * to doCallouts(). | |
1662 | * | |
1663 | * If one of the callouts notices that ClientHttpRequest is no | |
1664 | * longer valid, it should call cbdataReferenceDone() so that | |
1665 | * ClientHttpRequest's reference count goes to zero and it will get | |
1666 | * deleted. ClientHttpRequest will then delete ClientRequestContext. | |
1667 | * | |
1668 | * Note that we set the _done flags here before actually starting | |
1669 | * the callout. This is strictly for convenience. | |
1670 | */ | |
1671 | ||
82afb125 FC |
1672 | tos_t aclMapTOS (acl_tos * head, ACLChecklist * ch); |
1673 | nfmark_t aclMapNfmark (acl_nfmark * head, ACLChecklist * ch); | |
057f5854 | 1674 | |
de31d06f | 1675 | void |
1676 | ClientHttpRequest::doCallouts() | |
1677 | { | |
1678 | assert(calloutContext); | |
1679 | ||
6fca33e0 | 1680 | /*Save the original request for logging purposes*/ |
b248c2a3 AJ |
1681 | if (!calloutContext->http->al->request) { |
1682 | calloutContext->http->al->request = request; | |
1683 | HTTPMSGLOCK(calloutContext->http->al->request); | |
457857fe CT |
1684 | |
1685 | NotePairs ¬es = SyncNotes(*calloutContext->http->al, *calloutContext->http->request); | |
1686 | // Make the previously set client connection ID available as annotation. | |
1687 | if (ConnStateData *csd = calloutContext->http->getConn()) { | |
1688 | if (!csd->connectionTag().isEmpty()) | |
1689 | notes.add("clt_conn_tag", SBuf(csd->connectionTag()).c_str()); | |
1690 | } | |
b248c2a3 | 1691 | } |
6fca33e0 | 1692 | |
38450a50 | 1693 | if (!calloutContext->error) { |
87f237a9 | 1694 | // CVE-2009-0801: verify the Host: header is consistent with other known details. |
38450a50 CT |
1695 | if (!calloutContext->host_header_verify_done) { |
1696 | debugs(83, 3, HERE << "Doing calloutContext->hostHeaderVerify()"); | |
1697 | calloutContext->host_header_verify_done = true; | |
1698 | calloutContext->hostHeaderVerify(); | |
1699 | return; | |
1700 | } | |
fe97983f | 1701 | |
38450a50 CT |
1702 | if (!calloutContext->http_access_done) { |
1703 | debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck()"); | |
1704 | calloutContext->http_access_done = true; | |
1705 | calloutContext->clientAccessCheck(); | |
1706 | return; | |
1707 | } | |
de31d06f | 1708 | |
a83c6ed6 | 1709 | #if USE_ADAPTATION |
38450a50 CT |
1710 | if (!calloutContext->adaptation_acl_check_done) { |
1711 | calloutContext->adaptation_acl_check_done = true; | |
1712 | if (Adaptation::AccessCheck::Start( | |
87f237a9 | 1713 | Adaptation::methodReqmod, Adaptation::pointPreCache, |
af0ded40 | 1714 | request, NULL, calloutContext->http->al, this)) |
38450a50 CT |
1715 | return; // will call callback |
1716 | } | |
de31d06f | 1717 | #endif |
1718 | ||
38450a50 CT |
1719 | if (!calloutContext->redirect_done) { |
1720 | calloutContext->redirect_done = true; | |
de31d06f | 1721 | |
38450a50 CT |
1722 | if (Config.Program.redirect) { |
1723 | debugs(83, 3, HERE << "Doing calloutContext->clientRedirectStart()"); | |
1724 | calloutContext->redirect_state = REDIRECT_PENDING; | |
1725 | calloutContext->clientRedirectStart(); | |
1726 | return; | |
1727 | } | |
de31d06f | 1728 | } |
de31d06f | 1729 | |
38450a50 CT |
1730 | if (!calloutContext->adapted_http_access_done) { |
1731 | debugs(83, 3, HERE << "Doing calloutContext->clientAccessCheck2()"); | |
1732 | calloutContext->adapted_http_access_done = true; | |
1733 | calloutContext->clientAccessCheck2(); | |
1734 | return; | |
1735 | } | |
533493da | 1736 | |
a8a0b1c2 EC |
1737 | if (!calloutContext->store_id_done) { |
1738 | calloutContext->store_id_done = true; | |
a8a0b1c2 EC |
1739 | |
1740 | if (Config.Program.store_id) { | |
1741 | debugs(83, 3,"Doing calloutContext->clientStoreIdStart()"); | |
1742 | calloutContext->store_id_state = REDIRECT_PENDING; | |
1743 | calloutContext->clientStoreIdStart(); | |
1744 | return; | |
1745 | } | |
1746 | } | |
1747 | ||
38450a50 CT |
1748 | if (!calloutContext->interpreted_req_hdrs) { |
1749 | debugs(83, 3, HERE << "Doing clientInterpretRequestHeaders()"); | |
1750 | calloutContext->interpreted_req_hdrs = 1; | |
1751 | clientInterpretRequestHeaders(this); | |
1752 | } | |
57abaac0 | 1753 | |
38450a50 CT |
1754 | if (!calloutContext->no_cache_done) { |
1755 | calloutContext->no_cache_done = true; | |
de31d06f | 1756 | |
45e5102d | 1757 | if (Config.accessList.noCache && request->flags.cachable) { |
38450a50 CT |
1758 | debugs(83, 3, HERE << "Doing calloutContext->checkNoCache()"); |
1759 | calloutContext->checkNoCache(); | |
1760 | return; | |
1761 | } | |
de31d06f | 1762 | } |
38450a50 | 1763 | } // if !calloutContext->error |
de31d06f | 1764 | |
425de4c8 AJ |
1765 | if (!calloutContext->tosToClientDone) { |
1766 | calloutContext->tosToClientDone = true; | |
73c36fd9 | 1767 | if (getConn() != NULL && Comm::IsConnOpen(getConn()->clientConnection)) { |
c0941a6a | 1768 | ACLFilledChecklist ch(NULL, request, NULL); |
057f5854 | 1769 | ch.src_addr = request->client_addr; |
1770 | ch.my_addr = request->my_addr; | |
425de4c8 | 1771 | tos_t tos = aclMapTOS(Ip::Qos::TheConfig.tosToClient, &ch); |
3712be3f | 1772 | if (tos) |
73c36fd9 | 1773 | Ip::Qos::setSockTos(getConn()->clientConnection, tos); |
425de4c8 AJ |
1774 | } |
1775 | } | |
1776 | ||
1777 | if (!calloutContext->nfmarkToClientDone) { | |
1778 | calloutContext->nfmarkToClientDone = true; | |
73c36fd9 | 1779 | if (getConn() != NULL && Comm::IsConnOpen(getConn()->clientConnection)) { |
425de4c8 AJ |
1780 | ACLFilledChecklist ch(NULL, request, NULL); |
1781 | ch.src_addr = request->client_addr; | |
1782 | ch.my_addr = request->my_addr; | |
1783 | nfmark_t mark = aclMapNfmark(Ip::Qos::TheConfig.nfmarkToClient, &ch); | |
1784 | if (mark) | |
73c36fd9 | 1785 | Ip::Qos::setSockNfmark(getConn()->clientConnection, mark); |
3712be3f | 1786 | } |
057f5854 | 1787 | } |
1788 | ||
cb4f4424 | 1789 | #if USE_OPENSSL |
7a957a93 AR |
1790 | // We need to check for SslBump even if the calloutContext->error is set |
1791 | // because bumping may require delaying the error until after CONNECT. | |
e0c0d54c CT |
1792 | if (!calloutContext->sslBumpCheckDone) { |
1793 | calloutContext->sslBumpCheckDone = true; | |
1794 | if (calloutContext->sslBumpAccessCheck()) | |
1795 | return; | |
1796 | /* else no ssl bump required*/ | |
1797 | } | |
d2565320 | 1798 | #endif |
e0c0d54c | 1799 | |
2bd84e5f | 1800 | if (calloutContext->error) { |
851feda6 | 1801 | // XXX: prformance regression. c_str() reallocates |
81e019a0 AR |
1802 | SBuf storeUriBuf(request->storeId()); |
1803 | const char *storeUri = storeUriBuf.c_str(); | |
1804 | StoreEntry *e = storeCreateEntry(storeUri, storeUri, request->flags, request->method); | |
cb4f4424 | 1805 | #if USE_OPENSSL |
08097970 | 1806 | if (sslBumpNeeded()) { |
9e104535 CT |
1807 | // We have to serve an error, so bump the client first. |
1808 | sslBumpNeed(Ssl::bumpClientFirst); | |
2bd84e5f | 1809 | // set final error but delay sending until we bump |
6b2b6cfe | 1810 | Ssl::ServerBump *srvBump = new Ssl::ServerBump(request, e, Ssl::bumpClientFirst); |
2bd84e5f CT |
1811 | errorAppendEntry(e, calloutContext->error); |
1812 | calloutContext->error = NULL; | |
1813 | getConn()->setServerBump(srvBump); | |
1bfe9ade | 1814 | e->unlock("ClientHttpRequest::doCallouts+sslBumpNeeded"); |
87f237a9 | 1815 | } else |
2bd84e5f CT |
1816 | #endif |
1817 | { | |
7a957a93 | 1818 | // send the error to the client now |
2bd84e5f CT |
1819 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
1820 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
1821 | assert (repContext); | |
f0baf149 | 1822 | repContext->setReplyToStoreEntry(e, "immediate SslBump error"); |
2bd84e5f CT |
1823 | errorAppendEntry(e, calloutContext->error); |
1824 | calloutContext->error = NULL; | |
c3760363 | 1825 | if (calloutContext->readNextRequest && getConn()) |
2bd84e5f CT |
1826 | getConn()->flags.readMore = true; // resume any pipeline reads. |
1827 | node = (clientStreamNode *)client_stream.tail->data; | |
1828 | clientStreamRead(node, this, node->readBuffer); | |
1bfe9ade | 1829 | e->unlock("ClientHttpRequest::doCallouts-sslBumpNeeded"); |
2bd84e5f CT |
1830 | return; |
1831 | } | |
1832 | } | |
1833 | ||
de31d06f | 1834 | cbdataReferenceDone(calloutContext->http); |
1835 | delete calloutContext; | |
1836 | calloutContext = NULL; | |
de31d06f | 1837 | #if HEADERS_LOG |
1838 | ||
1839 | headersLog(0, 1, request->method, request); | |
1840 | #endif | |
1841 | ||
58d7150d | 1842 | debugs(83, 3, HERE << "calling processRequest()"); |
de31d06f | 1843 | processRequest(); |
3ff65596 AR |
1844 | |
1845 | #if ICAP_CLIENT | |
1846 | Adaptation::Icap::History::Pointer ih = request->icapHistory(); | |
1847 | if (ih != NULL) | |
1848 | ih->logType = logType; | |
1849 | #endif | |
de31d06f | 1850 | } |
1851 | ||
32d002cb | 1852 | #if !_USE_INLINE_ |
86a2f789 | 1853 | #include "client_side_request.cci" |
1854 | #endif | |
de31d06f | 1855 | |
a83c6ed6 | 1856 | #if USE_ADAPTATION |
a22e6cd3 AR |
1857 | /// Initiate an asynchronous adaptation transaction which will call us back. |
1858 | void | |
1859 | ClientHttpRequest::startAdaptation(const Adaptation::ServiceGroupPointer &g) | |
3b299123 | 1860 | { |
a22e6cd3 | 1861 | debugs(85, 3, HERE << "adaptation needed for " << this); |
a83c6ed6 AR |
1862 | assert(!virginHeadSource); |
1863 | assert(!adaptedBodySource); | |
a22e6cd3 | 1864 | virginHeadSource = initiateAdaptation( |
af0ded40 | 1865 | new Adaptation::Iterator(request, NULL, al, g)); |
a83c6ed6 | 1866 | |
e1381638 | 1867 | // we could try to guess whether we can bypass this adaptation |
a22e6cd3 | 1868 | // initiation failure, but it should not really happen |
4299f876 | 1869 | Must(initiated(virginHeadSource)); |
de31d06f | 1870 | } |
1871 | ||
1872 | void | |
3af10ac0 | 1873 | ClientHttpRequest::noteAdaptationAnswer(const Adaptation::Answer &answer) |
de31d06f | 1874 | { |
f53969cc | 1875 | assert(cbdataReferenceValid(this)); // indicates bug |
3af10ac0 AR |
1876 | clearAdaptation(virginHeadSource); |
1877 | assert(!adaptedBodySource); | |
1878 | ||
1879 | switch (answer.kind) { | |
1880 | case Adaptation::Answer::akForward: | |
b248c2a3 | 1881 | handleAdaptedHeader(const_cast<HttpMsg*>(answer.message.getRaw())); |
3af10ac0 AR |
1882 | break; |
1883 | ||
1884 | case Adaptation::Answer::akBlock: | |
1885 | handleAdaptationBlock(answer); | |
1886 | break; | |
1887 | ||
1888 | case Adaptation::Answer::akError: | |
1889 | handleAdaptationFailure(ERR_DETAIL_CLT_REQMOD_ABORT, !answer.final); | |
1890 | break; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | void | |
ec4d1a1d | 1895 | ClientHttpRequest::handleAdaptedHeader(HttpMsg *msg) |
3af10ac0 | 1896 | { |
5f8252d2 | 1897 | assert(msg); |
1898 | ||
b044675d | 1899 | if (HttpRequest *new_req = dynamic_cast<HttpRequest*>(msg)) { |
200ac359 | 1900 | /* |
5f8252d2 | 1901 | * Replace the old request with the new request. |
200ac359 | 1902 | */ |
6dd9f4bd | 1903 | HTTPMSGUNLOCK(request); |
b248c2a3 AJ |
1904 | request = new_req; |
1905 | HTTPMSGLOCK(request); | |
11f62bfb AJ |
1906 | |
1907 | // update the new message to flag whether URL re-writing was done on it | |
851feda6 | 1908 | if (request->effectiveRequestUri().cmp(uri) != 0) |
11f62bfb AJ |
1909 | request->flags.redirected = 1; |
1910 | ||
200ac359 | 1911 | /* |
1912 | * Store the new URI for logging | |
1913 | */ | |
1914 | xfree(uri); | |
3f0e38d6 | 1915 | uri = SBufToCstring(request->effectiveRequestUri()); |
200ac359 | 1916 | setLogUri(this, urlCanonicalClean(request)); |
914b89a2 | 1917 | assert(request->method.id()); |
b044675d | 1918 | } else if (HttpReply *new_rep = dynamic_cast<HttpReply*>(msg)) { |
1919 | debugs(85,3,HERE << "REQMOD reply is HTTP reply"); | |
1920 | ||
5f8252d2 | 1921 | // subscribe to receive reply body |
1922 | if (new_rep->body_pipe != NULL) { | |
a83c6ed6 | 1923 | adaptedBodySource = new_rep->body_pipe; |
d222a56c HN |
1924 | int consumer_ok = adaptedBodySource->setConsumerIfNotLate(this); |
1925 | assert(consumer_ok); | |
5f8252d2 | 1926 | } |
1927 | ||
b044675d | 1928 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
1929 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
ea25a575 | 1930 | assert(repContext); |
b044675d | 1931 | repContext->createStoreEntry(request->method, request->flags); |
1932 | ||
1933 | EBIT_CLR(storeEntry()->flags, ENTRY_FWD_HDR_WAIT); | |
1934 | request_satisfaction_mode = true; | |
1935 | request_satisfaction_offset = 0; | |
1936 | storeEntry()->replaceHttpReply(new_rep); | |
97ae5196 | 1937 | storeEntry()->timestampsSet(); |
cb4c4288 | 1938 | |
a83c6ed6 | 1939 | if (!adaptedBodySource) // no body |
cb4c4288 | 1940 | storeEntry()->complete(); |
b044675d | 1941 | clientGetMoreData(node, this); |
200ac359 | 1942 | } |
de31d06f | 1943 | |
5f8252d2 | 1944 | // we are done with getting headers (but may be receiving body) |
a83c6ed6 | 1945 | clearAdaptation(virginHeadSource); |
5f8252d2 | 1946 | |
b044675d | 1947 | if (!request_satisfaction_mode) |
1948 | doCallouts(); | |
de31d06f | 1949 | } |
1950 | ||
1951 | void | |
3af10ac0 | 1952 | ClientHttpRequest::handleAdaptationBlock(const Adaptation::Answer &answer) |
de31d06f | 1953 | { |
3af10ac0 AR |
1954 | request->detailError(ERR_ACCESS_DENIED, ERR_DETAIL_REQMOD_BLOCK); |
1955 | AclMatchedName = answer.ruleId.termedBuf(); | |
1956 | assert(calloutContext); | |
1957 | calloutContext->clientAccessCheckDone(ACCESS_DENIED); | |
1958 | AclMatchedName = NULL; | |
de31d06f | 1959 | } |
1960 | ||
0ad2b63b CT |
1961 | void |
1962 | ClientHttpRequest::resumeBodyStorage() | |
1963 | { | |
e83cdc25 | 1964 | if (!adaptedBodySource) |
0ad2b63b CT |
1965 | return; |
1966 | ||
1967 | noteMoreBodyDataAvailable(adaptedBodySource); | |
1968 | } | |
1969 | ||
de31d06f | 1970 | void |
1cf238db | 1971 | ClientHttpRequest::noteMoreBodyDataAvailable(BodyPipe::Pointer) |
de31d06f | 1972 | { |
5f8252d2 | 1973 | assert(request_satisfaction_mode); |
a83c6ed6 | 1974 | assert(adaptedBodySource != NULL); |
5f8252d2 | 1975 | |
0ad2b63b | 1976 | if (size_t contentSize = adaptedBodySource->buf().contentSize()) { |
4dc2b072 | 1977 | const size_t spaceAvailable = storeEntry()->bytesWanted(Range<size_t>(0,contentSize)); |
0ad2b63b CT |
1978 | |
1979 | if (spaceAvailable < contentSize ) { | |
1980 | // No or partial body data consuming | |
1981 | typedef NullaryMemFunT<ClientHttpRequest> Dialer; | |
1982 | AsyncCall::Pointer call = asyncCall(93, 5, "ClientHttpRequest::resumeBodyStorage", | |
1983 | Dialer(this, &ClientHttpRequest::resumeBodyStorage)); | |
1984 | storeEntry()->deferProducer(call); | |
1985 | } | |
1986 | ||
4dc2b072 | 1987 | if (!spaceAvailable) |
0ad2b63b CT |
1988 | return; |
1989 | ||
1990 | if (spaceAvailable < contentSize ) | |
1991 | contentSize = spaceAvailable; | |
1992 | ||
a83c6ed6 | 1993 | BodyPipeCheckout bpc(*adaptedBodySource); |
0ad2b63b | 1994 | const StoreIOBuffer ioBuf(&bpc.buf, request_satisfaction_offset, contentSize); |
5f8252d2 | 1995 | storeEntry()->write(ioBuf); |
0ad2b63b CT |
1996 | // assume StoreEntry::write() writes the entire ioBuf |
1997 | request_satisfaction_offset += ioBuf.length; | |
4ce0e99b | 1998 | bpc.buf.consume(contentSize); |
5f8252d2 | 1999 | bpc.checkIn(); |
2000 | } | |
2001 | ||
a83c6ed6 | 2002 | if (adaptedBodySource->exhausted()) |
5f8252d2 | 2003 | endRequestSatisfaction(); |
2004 | // else wait for more body data | |
de31d06f | 2005 | } |
2006 | ||
2007 | void | |
1cf238db | 2008 | ClientHttpRequest::noteBodyProductionEnded(BodyPipe::Pointer) |
de31d06f | 2009 | { |
a83c6ed6 | 2010 | assert(!virginHeadSource); |
0ad2b63b CT |
2011 | // should we end request satisfaction now? |
2012 | if (adaptedBodySource != NULL && adaptedBodySource->exhausted()) | |
5f8252d2 | 2013 | endRequestSatisfaction(); |
5f8252d2 | 2014 | } |
3b299123 | 2015 | |
5f8252d2 | 2016 | void |
26ac0430 AJ |
2017 | ClientHttpRequest::endRequestSatisfaction() |
2018 | { | |
5f8252d2 | 2019 | debugs(85,4, HERE << this << " ends request satisfaction"); |
2020 | assert(request_satisfaction_mode); | |
a83c6ed6 | 2021 | stopConsumingFrom(adaptedBodySource); |
3b299123 | 2022 | |
5f8252d2 | 2023 | // TODO: anything else needed to end store entry formation correctly? |
2024 | storeEntry()->complete(); | |
2025 | } | |
de31d06f | 2026 | |
5f8252d2 | 2027 | void |
1cf238db | 2028 | ClientHttpRequest::noteBodyProducerAborted(BodyPipe::Pointer) |
5f8252d2 | 2029 | { |
a83c6ed6 AR |
2030 | assert(!virginHeadSource); |
2031 | stopConsumingFrom(adaptedBodySource); | |
eae3a9a6 AR |
2032 | |
2033 | debugs(85,3, HERE << "REQMOD body production failed"); | |
2034 | if (request_satisfaction_mode) { // too late to recover or serve an error | |
2035 | request->detailError(ERR_ICAP_FAILURE, ERR_DETAIL_CLT_REQMOD_RESP_BODY); | |
73c36fd9 | 2036 | const Comm::ConnectionPointer c = getConn()->clientConnection; |
e7cea0ed AJ |
2037 | Must(Comm::IsConnOpen(c)); |
2038 | c->close(); // drastic, but we may be writing a response already | |
eae3a9a6 AR |
2039 | } else { |
2040 | handleAdaptationFailure(ERR_DETAIL_CLT_REQMOD_REQ_BODY); | |
2041 | } | |
5f8252d2 | 2042 | } |
3b299123 | 2043 | |
5f8252d2 | 2044 | void |
64b66b76 | 2045 | ClientHttpRequest::handleAdaptationFailure(int errDetail, bool bypassable) |
5f8252d2 | 2046 | { |
a83c6ed6 | 2047 | debugs(85,3, HERE << "handleAdaptationFailure(" << bypassable << ")"); |
3b299123 | 2048 | |
5f8252d2 | 2049 | const bool usedStore = storeEntry() && !storeEntry()->isEmpty(); |
2050 | const bool usedPipe = request->body_pipe != NULL && | |
26ac0430 | 2051 | request->body_pipe->consumedSize() > 0; |
3b299123 | 2052 | |
9d4d7c5e | 2053 | if (bypassable && !usedStore && !usedPipe) { |
2054 | debugs(85,3, HERE << "ICAP REQMOD callout failed, bypassing: " << calloutContext); | |
5f8252d2 | 2055 | if (calloutContext) |
2056 | doCallouts(); | |
2057 | return; | |
2058 | } | |
3b299123 | 2059 | |
5f8252d2 | 2060 | debugs(85,3, HERE << "ICAP REQMOD callout failed, responding with error"); |
3b299123 | 2061 | |
5f8252d2 | 2062 | clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; |
2063 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
2064 | assert(repContext); | |
de31d06f | 2065 | |
32fd6d8a CT |
2066 | calloutsError(ERR_ICAP_FAILURE, errDetail); |
2067 | ||
2068 | if (calloutContext) | |
2069 | doCallouts(); | |
2070 | } | |
2071 | ||
5f1d7e48 CT |
2072 | #endif |
2073 | ||
32fd6d8a CT |
2074 | // XXX: modify and use with ClientRequestContext::clientAccessCheckDone too. |
2075 | void | |
2076 | ClientHttpRequest::calloutsError(const err_type error, const int errDetail) | |
2077 | { | |
26ac0430 | 2078 | // The original author of the code also wanted to pass an errno to |
5f8252d2 | 2079 | // setReplyToError, but it seems unlikely that the errno reflects the |
2080 | // true cause of the error at this point, so I did not pass it. | |
2bd84e5f CT |
2081 | if (calloutContext) { |
2082 | Ip::Address noAddr; | |
4dd643d5 | 2083 | noAddr.setNoAddr(); |
2bd84e5f | 2084 | ConnStateData * c = getConn(); |
32fd6d8a | 2085 | calloutContext->error = clientBuildError(error, Http::scInternalServerError, |
87f237a9 A |
2086 | NULL, |
2087 | c != NULL ? c->clientConnection->remote : noAddr, | |
2088 | request | |
2089 | ); | |
79fc6915 | 2090 | #if USE_AUTH |
87f237a9 | 2091 | calloutContext->error->auth_user_request = |
cc1e110a | 2092 | c != NULL && c->getAuth() != NULL ? c->getAuth() : request->auth_user_request; |
79fc6915 | 2093 | #endif |
129fe2a1 | 2094 | calloutContext->error->detailError(errDetail); |
2bd84e5f | 2095 | calloutContext->readNextRequest = true; |
7830d88a FC |
2096 | if (c != NULL) |
2097 | c->expectNoForwarding(); | |
2bd84e5f CT |
2098 | } |
2099 | //else if(calloutContext == NULL) is it possible? | |
de31d06f | 2100 | } |
2101 |