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