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