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