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