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