]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http.cc
Merged from trunk rev.14096
[thirdparty/squid.git] / src / http.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 11 Hypertext Transfer Protocol (HTTP) */
10
11 /*
12 * Anonymizing patch by lutz@as-node.jena.thur.de
13 * have a look into http-anon.c to get more informations.
14 */
15
16 #include "squid.h"
17 #include "acl/FilledChecklist.h"
18 #include "base/AsyncJobCalls.h"
19 #include "base/TextException.h"
20 #include "base64.h"
21 #include "CachePeer.h"
22 #include "client_side.h"
23 #include "comm/Connection.h"
24 #include "comm/Read.h"
25 #include "comm/Write.h"
26 #include "CommRead.h"
27 #include "err_detail_type.h"
28 #include "errorpage.h"
29 #include "fd.h"
30 #include "fde.h"
31 #include "globals.h"
32 #include "http.h"
33 #include "http/one/ChunkedCodingParser.h"
34 #include "http/one/ResponseParser.h"
35 #include "HttpControlMsg.h"
36 #include "HttpHdrCc.h"
37 #include "HttpHdrContRange.h"
38 #include "HttpHdrSc.h"
39 #include "HttpHdrScTarget.h"
40 #include "HttpHeaderTools.h"
41 #include "HttpReply.h"
42 #include "HttpRequest.h"
43 #include "HttpStateFlags.h"
44 #include "log/access_log.h"
45 #include "MemBuf.h"
46 #include "MemObject.h"
47 #include "neighbors.h"
48 #include "peer_proxy_negotiate_auth.h"
49 #include "profiler/Profiler.h"
50 #include "refresh.h"
51 #include "RefreshPattern.h"
52 #include "rfc1738.h"
53 #include "SquidConfig.h"
54 #include "SquidTime.h"
55 #include "StatCounters.h"
56 #include "Store.h"
57 #include "StrList.h"
58 #include "tools.h"
59 #include "URL.h"
60 #include "util.h"
61
62 #if USE_AUTH
63 #include "auth/UserRequest.h"
64 #endif
65 #if USE_DELAY_POOLS
66 #include "DelayPools.h"
67 #endif
68
69 #define SQUID_ENTER_THROWING_CODE() try {
70 #define SQUID_EXIT_THROWING_CODE(status) \
71 status = true; \
72 } \
73 catch (const std::exception &e) { \
74 debugs (11, 1, "Exception error:" << e.what()); \
75 status = false; \
76 }
77
78 CBDATA_CLASS_INIT(HttpStateData);
79
80 static const char *const crlf = "\r\n";
81
82 static void httpMaybeRemovePublic(StoreEntry *, Http::StatusCode);
83 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request,
84 HttpHeader * hdr_out, const int we_do_ranges, const HttpStateFlags &);
85 //Declared in HttpHeaderTools.cc
86 void httpHdrAdd(HttpHeader *heads, HttpRequest *request, const AccessLogEntryPointer &al, HeaderWithAclList &headers_add);
87
88 HttpStateData::HttpStateData(FwdState *theFwdState) :
89 AsyncJob("HttpStateData"),
90 Client(theFwdState),
91 lastChunk(0),
92 httpChunkDecoder(NULL),
93 payloadSeen(0),
94 payloadTruncated(0)
95 {
96 debugs(11,5,HERE << "HttpStateData " << this << " created");
97 ignoreCacheControl = false;
98 surrogateNoStore = false;
99 serverConnection = fwd->serverConnection();
100
101 // reset peer response time stats for %<pt
102 request->hier.peer_http_request_sent.tv_sec = 0;
103 request->hier.peer_http_request_sent.tv_usec = 0;
104
105 if (fwd->serverConnection() != NULL)
106 _peer = cbdataReference(fwd->serverConnection()->getPeer()); /* might be NULL */
107
108 if (_peer) {
109 request->flags.proxying = true;
110 /*
111 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
112 * We might end up getting the object from somewhere else if,
113 * for example, the request to this neighbor fails.
114 */
115 if (_peer->options.proxy_only)
116 entry->releaseRequest();
117
118 #if USE_DELAY_POOLS
119 entry->setNoDelay(_peer->options.no_delay);
120 #endif
121 }
122
123 /*
124 * register the handler to free HTTP state data when the FD closes
125 */
126 typedef CommCbMemFunT<HttpStateData, CommCloseCbParams> Dialer;
127 closeHandler = JobCallback(9, 5, Dialer, this, HttpStateData::httpStateConnClosed);
128 comm_add_close_handler(serverConnection->fd, closeHandler);
129 }
130
131 HttpStateData::~HttpStateData()
132 {
133 /*
134 * don't forget that ~Client() gets called automatically
135 */
136
137 if (httpChunkDecoder)
138 delete httpChunkDecoder;
139
140 cbdataReferenceDone(_peer);
141
142 debugs(11,5, HERE << "HttpStateData " << this << " destroyed; " << serverConnection);
143 }
144
145 const Comm::ConnectionPointer &
146 HttpStateData::dataConnection() const
147 {
148 return serverConnection;
149 }
150
151 void
152 HttpStateData::httpStateConnClosed(const CommCloseCbParams &params)
153 {
154 debugs(11, 5, "httpStateFree: FD " << params.fd << ", httpState=" << params.data);
155 mustStop("HttpStateData::httpStateConnClosed");
156 }
157
158 void
159 HttpStateData::httpTimeout(const CommTimeoutCbParams &)
160 {
161 debugs(11, 4, serverConnection << ": '" << entry->url() << "'");
162
163 if (entry->store_status == STORE_PENDING) {
164 fwd->fail(new ErrorState(ERR_READ_TIMEOUT, Http::scGatewayTimeout, fwd->request));
165 }
166
167 serverConnection->close();
168 }
169
170 /// Remove an existing public store entry if the incoming response (to be
171 /// stored in a currently private entry) is going to invalidate it.
172 static void
173 httpMaybeRemovePublic(StoreEntry * e, Http::StatusCode status)
174 {
175 int remove = 0;
176 int forbidden = 0;
177 StoreEntry *pe;
178
179 // If the incoming response already goes into a public entry, then there is
180 // nothing to remove. This protects ready-for-collapsing entries as well.
181 if (!EBIT_TEST(e->flags, KEY_PRIVATE))
182 return;
183
184 switch (status) {
185
186 case Http::scOkay:
187
188 case Http::scNonAuthoritativeInformation:
189
190 case Http::scMultipleChoices:
191
192 case Http::scMovedPermanently:
193
194 case Http::scFound:
195
196 case Http::scGone:
197
198 case Http::scNotFound:
199 remove = 1;
200
201 break;
202
203 case Http::scForbidden:
204
205 case Http::scMethodNotAllowed:
206 forbidden = 1;
207
208 break;
209
210 #if WORK_IN_PROGRESS
211
212 case Http::scUnauthorized:
213 forbidden = 1;
214
215 break;
216
217 #endif
218
219 default:
220 #if QUESTIONABLE
221 /*
222 * Any 2xx response should eject previously cached entities...
223 */
224
225 if (status >= 200 && status < 300)
226 remove = 1;
227
228 #endif
229
230 break;
231 }
232
233 if (!remove && !forbidden)
234 return;
235
236 assert(e->mem_obj);
237
238 if (e->mem_obj->request)
239 pe = storeGetPublicByRequest(e->mem_obj->request);
240 else
241 pe = storeGetPublic(e->mem_obj->storeId(), e->mem_obj->method);
242
243 if (pe != NULL) {
244 assert(e != pe);
245 #if USE_HTCP
246 neighborsHtcpClear(e, NULL, e->mem_obj->request, e->mem_obj->method, HTCP_CLR_INVALIDATION);
247 #endif
248 pe->release();
249 }
250
251 /** \par
252 * Also remove any cached HEAD response in case the object has
253 * changed.
254 */
255 if (e->mem_obj->request)
256 pe = storeGetPublicByRequestMethod(e->mem_obj->request, Http::METHOD_HEAD);
257 else
258 pe = storeGetPublic(e->mem_obj->storeId(), Http::METHOD_HEAD);
259
260 if (pe != NULL) {
261 assert(e != pe);
262 #if USE_HTCP
263 neighborsHtcpClear(e, NULL, e->mem_obj->request, HttpRequestMethod(Http::METHOD_HEAD), HTCP_CLR_INVALIDATION);
264 #endif
265 pe->release();
266 }
267 }
268
269 void
270 HttpStateData::processSurrogateControl(HttpReply *reply)
271 {
272 if (request->flags.accelerated && reply->surrogate_control) {
273 HttpHdrScTarget *sctusable = reply->surrogate_control->getMergedTarget(Config.Accel.surrogate_id);
274
275 if (sctusable) {
276 if (sctusable->noStore() ||
277 (Config.onoff.surrogate_is_remote
278 && sctusable->noStoreRemote())) {
279 surrogateNoStore = true;
280 entry->makePrivate();
281 }
282
283 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
284 * accelerated request or not...
285 * Still, this is an abstraction breach. - RC
286 */
287 if (sctusable->hasMaxAge()) {
288 if (sctusable->maxAge() < sctusable->maxStale())
289 reply->expires = reply->date + sctusable->maxAge();
290 else
291 reply->expires = reply->date + sctusable->maxStale();
292
293 /* And update the timestamps */
294 entry->timestampsSet();
295 }
296
297 /* We ignore cache-control directives as per the Surrogate specification */
298 ignoreCacheControl = true;
299
300 delete sctusable;
301 }
302 }
303 }
304
305 int
306 HttpStateData::cacheableReply()
307 {
308 HttpReply const *rep = finalReply();
309 HttpHeader const *hdr = &rep->header;
310 const char *v;
311 #if USE_HTTP_VIOLATIONS
312
313 const RefreshPattern *R = NULL;
314
315 /* This strange looking define first looks up the refresh pattern
316 * and then checks if the specified flag is set. The main purpose
317 * of this is to simplify the refresh pattern lookup and USE_HTTP_VIOLATIONS
318 * condition
319 */
320 #define REFRESH_OVERRIDE(flag) \
321 ((R = (R ? R : refreshLimits(entry->mem_obj->storeId()))) , \
322 (R && R->flags.flag))
323 #else
324 #define REFRESH_OVERRIDE(flag) 0
325 #endif
326
327 if (EBIT_TEST(entry->flags, RELEASE_REQUEST)) {
328 debugs(22, 3, "NO because " << *entry << " has been released.");
329 return 0;
330 }
331
332 // Check for Surrogate/1.0 protocol conditions
333 // NP: reverse-proxy traffic our parent server has instructed us never to cache
334 if (surrogateNoStore) {
335 debugs(22, 3, HERE << "NO because Surrogate-Control:no-store");
336 return 0;
337 }
338
339 // RFC 2616: HTTP/1.1 Cache-Control conditions
340 if (!ignoreCacheControl) {
341 // XXX: check to see if the request headers alone were enough to prevent caching earlier
342 // (ie no-store request header) no need to check those all again here if so.
343 // for now we are not reliably doing that so we waste CPU re-checking request CC
344
345 // RFC 2616 section 14.9.2 - MUST NOT cache any response with request CC:no-store
346 if (request && request->cache_control && request->cache_control->noStore() &&
347 !REFRESH_OVERRIDE(ignore_no_store)) {
348 debugs(22, 3, HERE << "NO because client request Cache-Control:no-store");
349 return 0;
350 }
351
352 // NP: request CC:no-cache only means cache READ is forbidden. STORE is permitted.
353 if (rep->cache_control && rep->cache_control->hasNoCache() && rep->cache_control->noCache().size() > 0) {
354 /* TODO: we are allowed to cache when no-cache= has parameters.
355 * Provided we strip away any of the listed headers unless they are revalidated
356 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
357 * That is a bit tricky for squid right now so we avoid caching entirely.
358 */
359 debugs(22, 3, HERE << "NO because server reply Cache-Control:no-cache has parameters");
360 return 0;
361 }
362
363 // NP: request CC:private is undefined. We ignore.
364 // NP: other request CC flags are limiters on HIT/MISS. We don't care about here.
365
366 // RFC 2616 section 14.9.2 - MUST NOT cache any response with CC:no-store
367 if (rep->cache_control && rep->cache_control->noStore() &&
368 !REFRESH_OVERRIDE(ignore_no_store)) {
369 debugs(22, 3, HERE << "NO because server reply Cache-Control:no-store");
370 return 0;
371 }
372
373 // RFC 2616 section 14.9.1 - MUST NOT cache any response with CC:private in a shared cache like Squid.
374 // CC:private overrides CC:public when both are present in a response.
375 // TODO: add a shared/private cache configuration possibility.
376 if (rep->cache_control &&
377 rep->cache_control->hasPrivate() &&
378 !REFRESH_OVERRIDE(ignore_private)) {
379 /* TODO: we are allowed to cache when private= has parameters.
380 * Provided we strip away any of the listed headers unless they are revalidated
381 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
382 * That is a bit tricky for squid right now so we avoid caching entirely.
383 */
384 debugs(22, 3, HERE << "NO because server reply Cache-Control:private");
385 return 0;
386 }
387 }
388
389 // RFC 2068, sec 14.9.4 - MUST NOT cache any response with Authentication UNLESS certain CC controls are present
390 // allow HTTP violations to IGNORE those controls (ie re-block caching Auth)
391 if (request && (request->flags.auth || request->flags.authSent)) {
392 if (!rep->cache_control) {
393 debugs(22, 3, HERE << "NO because Authenticated and server reply missing Cache-Control");
394 return 0;
395 }
396
397 if (ignoreCacheControl) {
398 debugs(22, 3, HERE << "NO because Authenticated and ignoring Cache-Control");
399 return 0;
400 }
401
402 bool mayStore = false;
403 // HTTPbis pt6 section 3.2: a response CC:public is present
404 if (rep->cache_control->Public()) {
405 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:public");
406 mayStore = true;
407
408 // HTTPbis pt6 section 3.2: a response CC:must-revalidate is present
409 } else if (rep->cache_control->mustRevalidate() && !REFRESH_OVERRIDE(ignore_must_revalidate)) {
410 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:must-revalidate");
411 mayStore = true;
412
413 #if USE_HTTP_VIOLATIONS
414 // NP: given the must-revalidate exception we should also be able to exempt no-cache.
415 // HTTPbis WG verdict on this is that it is omitted from the spec due to being 'unexpected' by
416 // some. The caching+revalidate is not exactly unsafe though with Squids interpretation of no-cache
417 // (without parameters) as equivalent to must-revalidate in the reply.
418 } else if (rep->cache_control->hasNoCache() && rep->cache_control->noCache().size() == 0 && !REFRESH_OVERRIDE(ignore_must_revalidate)) {
419 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:no-cache (equivalent to must-revalidate)");
420 mayStore = true;
421 #endif
422
423 // HTTPbis pt6 section 3.2: a response CC:s-maxage is present
424 } else if (rep->cache_control->sMaxAge()) {
425 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:s-maxage");
426 mayStore = true;
427 }
428
429 if (!mayStore) {
430 debugs(22, 3, HERE << "NO because Authenticated transaction");
431 return 0;
432 }
433
434 // NP: response CC:no-cache is equivalent to CC:must-revalidate,max-age=0. We MAY cache, and do so.
435 // NP: other request CC flags are limiters on HIT/MISS/REFRESH. We don't care about here.
436 }
437
438 /* HACK: The "multipart/x-mixed-replace" content type is used for
439 * continuous push replies. These are generally dynamic and
440 * probably should not be cachable
441 */
442 if ((v = hdr->getStr(HDR_CONTENT_TYPE)))
443 if (!strncasecmp(v, "multipart/x-mixed-replace", 25)) {
444 debugs(22, 3, HERE << "NO because Content-Type:multipart/x-mixed-replace");
445 return 0;
446 }
447
448 switch (rep->sline.status()) {
449 /* Responses that are cacheable */
450
451 case Http::scOkay:
452
453 case Http::scNonAuthoritativeInformation:
454
455 case Http::scMultipleChoices:
456
457 case Http::scMovedPermanently:
458 case Http::scPermanentRedirect:
459
460 case Http::scGone:
461 /*
462 * Don't cache objects that need to be refreshed on next request,
463 * unless we know how to refresh it.
464 */
465
466 if (!refreshIsCachable(entry) && !REFRESH_OVERRIDE(store_stale)) {
467 debugs(22, 3, "NO because refreshIsCachable() returned non-cacheable..");
468 return 0;
469 } else {
470 debugs(22, 3, HERE << "YES because HTTP status " << rep->sline.status());
471 return 1;
472 }
473 /* NOTREACHED */
474 break;
475
476 /* Responses that only are cacheable if the server says so */
477
478 case Http::scFound:
479 case Http::scTemporaryRedirect:
480 if (rep->date <= 0) {
481 debugs(22, 3, HERE << "NO because HTTP status " << rep->sline.status() << " and Date missing/invalid");
482 return 0;
483 }
484 if (rep->expires > rep->date) {
485 debugs(22, 3, HERE << "YES because HTTP status " << rep->sline.status() << " and Expires > Date");
486 return 1;
487 } else {
488 debugs(22, 3, HERE << "NO because HTTP status " << rep->sline.status() << " and Expires <= Date");
489 return 0;
490 }
491 /* NOTREACHED */
492 break;
493
494 /* Errors can be negatively cached */
495
496 case Http::scNoContent:
497
498 case Http::scUseProxy:
499
500 case Http::scBadRequest:
501
502 case Http::scForbidden:
503
504 case Http::scNotFound:
505
506 case Http::scMethodNotAllowed:
507
508 case Http::scUriTooLong:
509
510 case Http::scInternalServerError:
511
512 case Http::scNotImplemented:
513
514 case Http::scBadGateway:
515
516 case Http::scServiceUnavailable:
517
518 case Http::scGatewayTimeout:
519 case Http::scMisdirectedRequest:
520
521 debugs(22, 3, "MAYBE because HTTP status " << rep->sline.status());
522 return -1;
523
524 /* NOTREACHED */
525 break;
526
527 /* Some responses can never be cached */
528
529 case Http::scPartialContent: /* Not yet supported */
530
531 case Http::scSeeOther:
532
533 case Http::scNotModified:
534
535 case Http::scUnauthorized:
536
537 case Http::scProxyAuthenticationRequired:
538
539 case Http::scInvalidHeader: /* Squid header parsing error */
540
541 case Http::scHeaderTooLarge:
542
543 case Http::scPaymentRequired:
544 case Http::scNotAcceptable:
545 case Http::scRequestTimeout:
546 case Http::scConflict:
547 case Http::scLengthRequired:
548 case Http::scPreconditionFailed:
549 case Http::scPayloadTooLarge:
550 case Http::scUnsupportedMediaType:
551 case Http::scUnprocessableEntity:
552 case Http::scLocked:
553 case Http::scFailedDependency:
554 case Http::scInsufficientStorage:
555 case Http::scRequestedRangeNotSatisfied:
556 case Http::scExpectationFailed:
557
558 debugs(22, 3, HERE << "NO because HTTP status " << rep->sline.status());
559 return 0;
560
561 default:
562 /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */
563 debugs (11, 3, HERE << "NO because unknown HTTP status code " << rep->sline.status());
564 return 0;
565
566 /* NOTREACHED */
567 break;
568 }
569
570 /* NOTREACHED */
571 }
572
573 /*
574 * For Vary, store the relevant request headers as
575 * virtual headers in the reply
576 * Returns false if the variance cannot be stored
577 */
578 const char *
579 httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
580 {
581 String vary, hdr;
582 const char *pos = NULL;
583 const char *item;
584 const char *value;
585 int ilen;
586 static String vstr;
587
588 vstr.clean();
589 vary = reply->header.getList(HDR_VARY);
590
591 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
592 char *name = (char *)xmalloc(ilen + 1);
593 xstrncpy(name, item, ilen + 1);
594 Tolower(name);
595
596 if (strcmp(name, "*") == 0) {
597 /* Can not handle "Vary: *" withtout ETag support */
598 safe_free(name);
599 vstr.clean();
600 break;
601 }
602
603 strListAdd(&vstr, name, ',');
604 hdr = request->header.getByName(name);
605 safe_free(name);
606 value = hdr.termedBuf();
607
608 if (value) {
609 value = rfc1738_escape_part(value);
610 vstr.append("=\"", 2);
611 vstr.append(value);
612 vstr.append("\"", 1);
613 }
614
615 hdr.clean();
616 }
617
618 vary.clean();
619 #if X_ACCELERATOR_VARY
620
621 pos = NULL;
622 vary = reply->header.getList(HDR_X_ACCELERATOR_VARY);
623
624 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
625 char *name = (char *)xmalloc(ilen + 1);
626 xstrncpy(name, item, ilen + 1);
627 Tolower(name);
628 strListAdd(&vstr, name, ',');
629 hdr = request->header.getByName(name);
630 safe_free(name);
631 value = hdr.termedBuf();
632
633 if (value) {
634 value = rfc1738_escape_part(value);
635 vstr.append("=\"", 2);
636 vstr.append(value);
637 vstr.append("\"", 1);
638 }
639
640 hdr.clean();
641 }
642
643 vary.clean();
644 #endif
645
646 debugs(11, 3, "httpMakeVaryMark: " << vstr);
647 return vstr.termedBuf();
648 }
649
650 void
651 HttpStateData::keepaliveAccounting(HttpReply *reply)
652 {
653 if (flags.keepalive)
654 if (_peer)
655 ++ _peer->stats.n_keepalives_sent;
656
657 if (reply->keep_alive) {
658 if (_peer)
659 ++ _peer->stats.n_keepalives_recv;
660
661 if (Config.onoff.detect_broken_server_pconns
662 && reply->bodySize(request->method) == -1 && !flags.chunked) {
663 debugs(11, DBG_IMPORTANT, "keepaliveAccounting: Impossible keep-alive header from '" << entry->url() << "'" );
664 // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
665 flags.keepalive_broken = true;
666 }
667 }
668 }
669
670 void
671 HttpStateData::checkDateSkew(HttpReply *reply)
672 {
673 if (reply->date > -1 && !_peer) {
674 int skew = abs((int)(reply->date - squid_curtime));
675
676 if (skew > 86400)
677 debugs(11, 3, "" << request->GetHost() << "'s clock is skewed by " << skew << " seconds!");
678 }
679 }
680
681 /**
682 * This creates the error page itself.. its likely
683 * that the forward ported reply header max size patch
684 * generates non http conformant error pages - in which
685 * case the errors where should be 'BAD_GATEWAY' etc
686 */
687 void
688 HttpStateData::processReplyHeader()
689 {
690 /** Creates a blank header. If this routine is made incremental, this will not do */
691
692 /* NP: all exit points to this function MUST call ctx_exit(ctx) */
693 Ctx ctx = ctx_enter(entry->mem_obj->urlXXX());
694
695 debugs(11, 3, "processReplyHeader: key '" << entry->getMD5Text() << "'");
696
697 assert(!flags.headers_parsed);
698
699 if (!inBuf.length()) {
700 ctx_exit(ctx);
701 return;
702 }
703
704 /* Attempt to parse the first line; this will define where the protocol, status, reason-phrase and header begin */
705 {
706 if (hp == NULL)
707 hp = new Http1::ResponseParser;
708
709 bool parsedOk = hp->parse(inBuf);
710
711 // sync the buffers after parsing.
712 inBuf = hp->remaining();
713
714 if (hp->needsMoreData()) {
715 if (eof) { // no more data coming
716 /* Bug 2879: Replies may terminate with \r\n then EOF instead of \r\n\r\n.
717 * We also may receive truncated responses.
718 * Ensure here that we have at minimum two \r\n when EOF is seen.
719 */
720 inBuf.append("\r\n\r\n", 4);
721 // retry the parse
722 parsedOk = hp->parse(inBuf);
723 // sync the buffers after parsing.
724 inBuf = hp->remaining();
725 } else {
726 debugs(33, 5, "Incomplete response, waiting for end of response headers");
727 ctx_exit(ctx);
728 return;
729 }
730 }
731
732 flags.headers_parsed = true;
733
734 if (!parsedOk) {
735 // unrecoverable parsing error
736 debugs(11, 3, "Non-HTTP-compliant header:\n---------\n" << inBuf << "\n----------");
737 HttpReply *newrep = new HttpReply;
738 newrep->sline.set(Http::ProtocolVersion(), hp->messageStatus());
739 HttpReply *vrep = setVirginReply(newrep);
740 entry->replaceHttpReply(vrep);
741 // XXX: close the server connection ?
742 ctx_exit(ctx);
743 return;
744 }
745 }
746
747 /* We know the whole response is in parser now */
748 debugs(11, 2, "HTTP Server " << serverConnection);
749 debugs(11, 2, "HTTP Server RESPONSE:\n---------\n" <<
750 hp->messageProtocol() << " " << hp->messageStatus() << " " << hp->reasonPhrase() << "\n" <<
751 hp->mimeHeader() <<
752 "----------");
753
754 // reset payload tracking to begin after message headers
755 payloadSeen = inBuf.length();
756
757 HttpReply *newrep = new HttpReply;
758 // XXX: RFC 7230 indicates we MAY ignore the reason phrase,
759 // and use an empty string on unknown status.
760 // We do that now to avoid performance regression from using SBuf::c_str()
761 newrep->sline.set(Http::ProtocolVersion(1,1), hp->messageStatus() /* , hp->reasonPhrase() */);
762 newrep->sline.protocol = newrep->sline.version.protocol = hp->messageProtocol().protocol;
763 newrep->sline.version.major = hp->messageProtocol().major;
764 newrep->sline.version.minor = hp->messageProtocol().minor;
765
766 // parse headers
767 newrep->pstate = psReadyToParseHeaders;
768 if (newrep->httpMsgParseStep(hp->mimeHeader().rawContent(), hp->mimeHeader().length(), true) < 0) {
769 // XXX: when Http::ProtocolVersion is a function, remove this hack. just set with messageProtocol()
770 newrep->sline.set(Http::ProtocolVersion(), Http::scInvalidHeader);
771 newrep->sline.version.protocol = hp->messageProtocol().protocol;
772 newrep->sline.version.major = hp->messageProtocol().major;
773 newrep->sline.version.minor = hp->messageProtocol().minor;
774 debugs(11, 2, "error parsing response headers mime block");
775 }
776
777 // done with Parser, now process using the HttpReply
778 hp = NULL;
779
780 newrep->removeStaleWarnings();
781
782 if (newrep->sline.protocol == AnyP::PROTO_HTTP && newrep->sline.status() >= 100 && newrep->sline.status() < 200) {
783 handle1xx(newrep);
784 ctx_exit(ctx);
785 return;
786 }
787
788 flags.chunked = false;
789 if (newrep->sline.protocol == AnyP::PROTO_HTTP && newrep->header.chunked()) {
790 flags.chunked = true;
791 httpChunkDecoder = new Http1::ChunkedCodingParser;
792 }
793
794 if (!peerSupportsConnectionPinning())
795 request->flags.connectionAuthDisabled = true;
796
797 HttpReply *vrep = setVirginReply(newrep);
798 flags.headers_parsed = true;
799
800 keepaliveAccounting(vrep);
801
802 checkDateSkew(vrep);
803
804 processSurrogateControl (vrep);
805
806 request->hier.peer_reply_status = newrep->sline.status();
807
808 ctx_exit(ctx);
809 }
810
811 /// ignore or start forwarding the 1xx response (a.k.a., control message)
812 void
813 HttpStateData::handle1xx(HttpReply *reply)
814 {
815 HttpReply::Pointer msg(reply); // will destroy reply if unused
816
817 // one 1xx at a time: we must not be called while waiting for previous 1xx
818 Must(!flags.handling1xx);
819 flags.handling1xx = true;
820
821 if (!request->canHandle1xx() || request->forcedBodyContinuation) {
822 debugs(11, 2, "ignoring 1xx because it is " << (request->forcedBodyContinuation ? "already sent" : "not supported by client"));
823 proceedAfter1xx();
824 return;
825 }
826
827 #if USE_HTTP_VIOLATIONS
828 // check whether the 1xx response forwarding is allowed by squid.conf
829 if (Config.accessList.reply) {
830 ACLFilledChecklist ch(Config.accessList.reply, originalRequest(), NULL);
831 ch.reply = reply;
832 HTTPMSGLOCK(ch.reply);
833 if (ch.fastCheck() != ACCESS_ALLOWED) { // TODO: support slow lookups?
834 debugs(11, 3, HERE << "ignoring denied 1xx");
835 proceedAfter1xx();
836 return;
837 }
838 }
839 #endif // USE_HTTP_VIOLATIONS
840
841 debugs(11, 2, HERE << "forwarding 1xx to client");
842
843 // the Sink will use this to call us back after writing 1xx to the client
844 typedef NullaryMemFunT<HttpStateData> CbDialer;
845 const AsyncCall::Pointer cb = JobCallback(11, 3, CbDialer, this,
846 HttpStateData::proceedAfter1xx);
847 CallJobHere1(11, 4, request->clientConnectionManager, ConnStateData,
848 ConnStateData::sendControlMsg, HttpControlMsg(msg, cb));
849 // If the call is not fired, then the Sink is gone, and HttpStateData
850 // will terminate due to an aborted store entry or another similar error.
851 // If we get stuck, it is not handle1xx fault if we could get stuck
852 // for similar reasons without a 1xx response.
853 }
854
855 /// restores state and resumes processing after 1xx is ignored or forwarded
856 void
857 HttpStateData::proceedAfter1xx()
858 {
859 Must(flags.handling1xx);
860 debugs(11, 2, "continuing with " << payloadSeen << " bytes in buffer after 1xx");
861 CallJobHere(11, 3, this, HttpStateData, HttpStateData::processReply);
862 }
863
864 /**
865 * returns true if the peer can support connection pinning
866 */
867 bool HttpStateData::peerSupportsConnectionPinning() const
868 {
869 const HttpReply *rep = entry->mem_obj->getReply();
870 const HttpHeader *hdr = &rep->header;
871 bool rc;
872 String header;
873
874 if (!_peer)
875 return true;
876
877 /*If this peer does not support connection pinning (authenticated
878 connections) return false
879 */
880 if (!_peer->connection_auth)
881 return false;
882
883 /*The peer supports connection pinning and the http reply status
884 is not unauthorized, so the related connection can be pinned
885 */
886 if (rep->sline.status() != Http::scUnauthorized)
887 return true;
888
889 /*The server respond with Http::scUnauthorized and the peer configured
890 with "connection-auth=on" we know that the peer supports pinned
891 connections
892 */
893 if (_peer->connection_auth == 1)
894 return true;
895
896 /*At this point peer has configured with "connection-auth=auto"
897 parameter so we need some extra checks to decide if we are going
898 to allow pinned connections or not
899 */
900
901 /*if the peer configured with originserver just allow connection
902 pinning (squid 2.6 behaviour)
903 */
904 if (_peer->options.originserver)
905 return true;
906
907 /*if the connections it is already pinned it is OK*/
908 if (request->flags.pinned)
909 return true;
910
911 /*Allow pinned connections only if the Proxy-support header exists in
912 reply and has in its list the "Session-Based-Authentication"
913 which means that the peer supports connection pinning.
914 */
915 if (!hdr->has(HDR_PROXY_SUPPORT))
916 return false;
917
918 header = hdr->getStrOrList(HDR_PROXY_SUPPORT);
919 /* XXX This ought to be done in a case-insensitive manner */
920 rc = (strstr(header.termedBuf(), "Session-Based-Authentication") != NULL);
921
922 return rc;
923 }
924
925 // Called when we parsed (and possibly adapted) the headers but
926 // had not starting storing (a.k.a., sending) the body yet.
927 void
928 HttpStateData::haveParsedReplyHeaders()
929 {
930 Client::haveParsedReplyHeaders();
931
932 Ctx ctx = ctx_enter(entry->mem_obj->urlXXX());
933 HttpReply *rep = finalReply();
934
935 entry->timestampsSet();
936
937 /* Check if object is cacheable or not based on reply code */
938 debugs(11, 3, "HTTP CODE: " << rep->sline.status());
939
940 if (neighbors_do_private_keys)
941 httpMaybeRemovePublic(entry, rep->sline.status());
942
943 bool varyFailure = false;
944 if (rep->header.has(HDR_VARY)
945 #if X_ACCELERATOR_VARY
946 || rep->header.has(HDR_X_ACCELERATOR_VARY)
947 #endif
948 ) {
949 const char *vary = httpMakeVaryMark(request, rep);
950
951 if (!vary) {
952 entry->makePrivate();
953 if (!fwd->reforwardableStatus(rep->sline.status()))
954 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
955 varyFailure = true;
956 } else {
957 entry->mem_obj->vary_headers = xstrdup(vary);
958 }
959 }
960
961 if (!varyFailure) {
962 /*
963 * If its not a reply that we will re-forward, then
964 * allow the client to get it.
965 */
966 if (!fwd->reforwardableStatus(rep->sline.status()))
967 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
968
969 switch (cacheableReply()) {
970
971 case 1:
972 entry->makePublic();
973 break;
974
975 case 0:
976 entry->makePrivate();
977 break;
978
979 case -1:
980
981 #if USE_HTTP_VIOLATIONS
982 if (Config.negativeTtl > 0)
983 entry->cacheNegatively();
984 else
985 #endif
986 entry->makePrivate();
987 break;
988
989 default:
990 assert(0);
991 break;
992 }
993 }
994
995 if (!ignoreCacheControl) {
996 if (rep->cache_control) {
997 // We are required to revalidate on many conditions.
998 // For security reasons we do so even if storage was caused by refresh_pattern ignore-* option
999
1000 // CC:must-revalidate or CC:proxy-revalidate
1001 const bool ccMustRevalidate = (rep->cache_control->proxyRevalidate() || rep->cache_control->mustRevalidate());
1002
1003 // CC:no-cache (only if there are no parameters)
1004 const bool ccNoCacheNoParams = (rep->cache_control->hasNoCache() && rep->cache_control->noCache().size()==0);
1005
1006 // CC:s-maxage=N
1007 const bool ccSMaxAge = rep->cache_control->hasSMaxAge();
1008
1009 // CC:private (yes, these can sometimes be stored)
1010 const bool ccPrivate = rep->cache_control->hasPrivate();
1011
1012 if (ccMustRevalidate || ccNoCacheNoParams || ccSMaxAge || ccPrivate)
1013 EBIT_SET(entry->flags, ENTRY_REVALIDATE);
1014 }
1015 #if USE_HTTP_VIOLATIONS // response header Pragma::no-cache is undefined in HTTP
1016 else {
1017 // Expensive calculation. So only do it IF the CC: header is not present.
1018
1019 /* HACK: Pragma: no-cache in _replies_ is not documented in HTTP,
1020 * but servers like "Active Imaging Webcast/2.0" sure do use it */
1021 if (rep->header.has(HDR_PRAGMA) &&
1022 rep->header.hasListMember(HDR_PRAGMA,"no-cache",','))
1023 EBIT_SET(entry->flags, ENTRY_REVALIDATE);
1024 }
1025 #endif
1026 }
1027
1028 #if HEADERS_LOG
1029 headersLog(1, 0, request->method, rep);
1030
1031 #endif
1032
1033 ctx_exit(ctx);
1034 }
1035
1036 HttpStateData::ConnectionStatus
1037 HttpStateData::statusIfComplete() const
1038 {
1039 const HttpReply *rep = virginReply();
1040 /** \par
1041 * If the reply wants to close the connection, it takes precedence */
1042
1043 if (httpHeaderHasConnDir(&rep->header, "close"))
1044 return COMPLETE_NONPERSISTENT_MSG;
1045
1046 /** \par
1047 * If we didn't send a keep-alive request header, then this
1048 * can not be a persistent connection.
1049 */
1050 if (!flags.keepalive)
1051 return COMPLETE_NONPERSISTENT_MSG;
1052
1053 /** \par
1054 * If we haven't sent the whole request then this can not be a persistent
1055 * connection.
1056 */
1057 if (!flags.request_sent) {
1058 debugs(11, 2, "Request not yet fully sent " << request->method << ' ' << entry->url());
1059 return COMPLETE_NONPERSISTENT_MSG;
1060 }
1061
1062 /** \par
1063 * What does the reply have to say about keep-alive?
1064 */
1065 /**
1066 \bug XXX BUG?
1067 * If the origin server (HTTP/1.0) does not send a keep-alive
1068 * header, but keeps the connection open anyway, what happens?
1069 * We'll return here and http.c waits for an EOF before changing
1070 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
1071 * and an error status code, and we might have to wait until
1072 * the server times out the socket.
1073 */
1074 if (!rep->keep_alive)
1075 return COMPLETE_NONPERSISTENT_MSG;
1076
1077 return COMPLETE_PERSISTENT_MSG;
1078 }
1079
1080 HttpStateData::ConnectionStatus
1081 HttpStateData::persistentConnStatus() const
1082 {
1083 debugs(11, 3, HERE << serverConnection << " eof=" << eof);
1084 if (eof) // already reached EOF
1085 return COMPLETE_NONPERSISTENT_MSG;
1086
1087 /* If server fd is closing (but we have not been notified yet), stop Comm
1088 I/O to avoid assertions. TODO: Change Comm API to handle callers that
1089 want more I/O after async closing (usually initiated by others). */
1090 // XXX: add canReceive or s/canSend/canTalkToServer/
1091 if (!Comm::IsConnOpen(serverConnection))
1092 return COMPLETE_NONPERSISTENT_MSG;
1093
1094 /** \par
1095 * In chunked response we do not know the content length but we are absolutely
1096 * sure about the end of response, so we are calling the statusIfComplete to
1097 * decide if we can be persistant
1098 */
1099 if (lastChunk && flags.chunked)
1100 return statusIfComplete();
1101
1102 const HttpReply *vrep = virginReply();
1103 debugs(11, 5, "persistentConnStatus: content_length=" << vrep->content_length);
1104
1105 const int64_t clen = vrep->bodySize(request->method);
1106
1107 debugs(11, 5, "persistentConnStatus: clen=" << clen);
1108
1109 /* If the body size is unknown we must wait for EOF */
1110 if (clen < 0)
1111 return INCOMPLETE_MSG;
1112
1113 /** \par
1114 * If the body size is known, we must wait until we've gotten all of it. */
1115 if (clen > 0) {
1116 debugs(11,5, "payloadSeen=" << payloadSeen << " content_length=" << vrep->content_length);
1117
1118 if (payloadSeen < vrep->content_length)
1119 return INCOMPLETE_MSG;
1120
1121 if (payloadTruncated > 0) // already read more than needed
1122 return COMPLETE_NONPERSISTENT_MSG; // disable pconns
1123 }
1124
1125 /** \par
1126 * If there is no message body or we got it all, we can be persistent */
1127 return statusIfComplete();
1128 }
1129
1130 #if USE_DELAY_POOLS
1131 static void
1132 readDelayed(void *context, CommRead const &)
1133 {
1134 HttpStateData *state = static_cast<HttpStateData*>(context);
1135 state->flags.do_next_read = true;
1136 state->maybeReadVirginBody();
1137 }
1138 #endif
1139
1140 void
1141 HttpStateData::readReply(const CommIoCbParams &io)
1142 {
1143 Must(!flags.do_next_read); // XXX: should have been set false by mayReadVirginBody()
1144 flags.do_next_read = false;
1145
1146 debugs(11, 5, io.conn);
1147
1148 // Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us
1149 if (io.flag == Comm::ERR_CLOSING) {
1150 debugs(11, 3, "http socket closing");
1151 return;
1152 }
1153
1154 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1155 abortTransaction("store entry aborted while reading reply");
1156 return;
1157 }
1158
1159 Must(Comm::IsConnOpen(serverConnection));
1160 Must(io.conn->fd == serverConnection->fd);
1161
1162 /*
1163 * Don't reset the timeout value here. The value should be
1164 * counting Config.Timeout.request and applies to the request
1165 * as a whole, not individual read() calls.
1166 * Plus, it breaks our lame *HalfClosed() detection
1167 */
1168
1169 Must(maybeMakeSpaceAvailable(true));
1170 CommIoCbParams rd(this); // will be expanded with ReadNow results
1171 rd.conn = io.conn;
1172 rd.size = entry->bytesWanted(Range<size_t>(0, inBuf.spaceSize()));
1173 #if USE_DELAY_POOLS
1174 if (rd.size < 1) {
1175 assert(entry->mem_obj);
1176
1177 /* read ahead limit */
1178 /* Perhaps these two calls should both live in MemObject */
1179 AsyncCall::Pointer nilCall;
1180 if (!entry->mem_obj->readAheadPolicyCanRead()) {
1181 entry->mem_obj->delayRead(DeferredRead(readDelayed, this, CommRead(io.conn, NULL, 0, nilCall)));
1182 return;
1183 }
1184
1185 /* delay id limit */
1186 entry->mem_obj->mostBytesAllowed().delayRead(DeferredRead(readDelayed, this, CommRead(io.conn, NULL, 0, nilCall)));
1187 return;
1188 }
1189 #endif
1190
1191 switch (Comm::ReadNow(rd, inBuf)) {
1192 case Comm::INPROGRESS:
1193 if (inBuf.isEmpty())
1194 debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
1195 flags.do_next_read = true;
1196 maybeReadVirginBody();
1197 return;
1198
1199 case Comm::OK:
1200 {
1201 payloadSeen += rd.size;
1202 #if USE_DELAY_POOLS
1203 DelayId delayId = entry->mem_obj->mostBytesAllowed();
1204 delayId.bytesIn(rd.size);
1205 #endif
1206
1207 kb_incr(&(statCounter.server.all.kbytes_in), rd.size);
1208 kb_incr(&(statCounter.server.http.kbytes_in), rd.size);
1209 ++ IOStats.Http.reads;
1210
1211 int bin = 0;
1212 for (int clen = rd.size - 1; clen; ++bin)
1213 clen >>= 1;
1214
1215 ++ IOStats.Http.read_hist[bin];
1216
1217 // update peer response time stats (%<pt)
1218 const timeval &sent = request->hier.peer_http_request_sent;
1219 if (sent.tv_sec)
1220 tvSub(request->hier.peer_response_time, sent, current_time);
1221 else
1222 request->hier.peer_response_time.tv_sec = -1;
1223 }
1224
1225 /* Continue to process previously read data */
1226 break;
1227
1228 case Comm::ENDFILE: // close detected by 0-byte read
1229 eof = 1;
1230 flags.do_next_read = false;
1231
1232 /* Continue to process previously read data */
1233 break;
1234
1235 // case Comm::COMM_ERROR:
1236 default: // no other flags should ever occur
1237 debugs(11, 2, io.conn << ": read failure: " << xstrerr(rd.xerrno));
1238 ErrorState *err = new ErrorState(ERR_READ_ERROR, Http::scBadGateway, fwd->request);
1239 err->xerrno = rd.xerrno;
1240 fwd->fail(err);
1241 flags.do_next_read = false;
1242 io.conn->close();
1243
1244 return;
1245 }
1246
1247 /* Process next response from buffer */
1248 processReply();
1249 }
1250
1251 /// processes the already read and buffered response data, possibly after
1252 /// waiting for asynchronous 1xx control message processing
1253 void
1254 HttpStateData::processReply()
1255 {
1256
1257 if (flags.handling1xx) { // we came back after handling a 1xx response
1258 debugs(11, 5, HERE << "done with 1xx handling");
1259 flags.handling1xx = false;
1260 Must(!flags.headers_parsed);
1261 }
1262
1263 if (!flags.headers_parsed) { // have not parsed headers yet?
1264 PROF_start(HttpStateData_processReplyHeader);
1265 processReplyHeader();
1266 PROF_stop(HttpStateData_processReplyHeader);
1267
1268 if (!continueAfterParsingHeader()) // parsing error or need more data
1269 return; // TODO: send errors to ICAP
1270
1271 adaptOrFinalizeReply(); // may write to, abort, or "close" the entry
1272 }
1273
1274 // kick more reads if needed and/or process the response body, if any
1275 PROF_start(HttpStateData_processReplyBody);
1276 processReplyBody(); // may call serverComplete()
1277 PROF_stop(HttpStateData_processReplyBody);
1278 }
1279
1280 /**
1281 \retval true if we can continue with processing the body or doing ICAP.
1282 */
1283 bool
1284 HttpStateData::continueAfterParsingHeader()
1285 {
1286 if (flags.handling1xx) {
1287 debugs(11, 5, HERE << "wait for 1xx handling");
1288 Must(!flags.headers_parsed);
1289 return false;
1290 }
1291
1292 if (!flags.headers_parsed && !eof) {
1293 debugs(11, 9, "needs more at " << inBuf.length());
1294 flags.do_next_read = true;
1295 /** \retval false If we have not finished parsing the headers and may get more data.
1296 * Schedules more reads to retrieve the missing data.
1297 */
1298 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1299 return false;
1300 }
1301
1302 /** If we are done with parsing, check for errors */
1303
1304 err_type error = ERR_NONE;
1305
1306 if (flags.headers_parsed) { // parsed headers, possibly with errors
1307 // check for header parsing errors
1308 if (HttpReply *vrep = virginReply()) {
1309 const Http::StatusCode s = vrep->sline.status();
1310 const AnyP::ProtocolVersion &v = vrep->sline.version;
1311 if (s == Http::scInvalidHeader && v != Http::ProtocolVersion(0,9)) {
1312 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
1313 error = ERR_INVALID_RESP;
1314 } else if (s == Http::scHeaderTooLarge) {
1315 fwd->dontRetry(true);
1316 error = ERR_TOO_BIG;
1317 } else {
1318 return true; // done parsing, got reply, and no error
1319 }
1320 } else {
1321 // parsed headers but got no reply
1322 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No reply at all for " << entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
1323 error = ERR_INVALID_RESP;
1324 }
1325 } else {
1326 assert(eof);
1327 if (inBuf.length()) {
1328 error = ERR_INVALID_RESP;
1329 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
1330 } else {
1331 error = ERR_ZERO_SIZE_OBJECT;
1332 debugs(11, (request->flags.accelerated?DBG_IMPORTANT:2), "WARNING: HTTP: Invalid Response: No object data received for " <<
1333 entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
1334 }
1335 }
1336
1337 assert(error != ERR_NONE);
1338 entry->reset();
1339 fwd->fail(new ErrorState(error, Http::scBadGateway, fwd->request));
1340 flags.do_next_read = false;
1341 serverConnection->close();
1342 return false; // quit on error
1343 }
1344
1345 /** truncate what we read if we read too much so that writeReplyBody()
1346 writes no more than what we should have read */
1347 void
1348 HttpStateData::truncateVirginBody()
1349 {
1350 assert(flags.headers_parsed);
1351
1352 HttpReply *vrep = virginReply();
1353 int64_t clen = -1;
1354 if (!vrep->expectingBody(request->method, clen) || clen < 0)
1355 return; // no body or a body of unknown size, including chunked
1356
1357 if (payloadSeen - payloadTruncated <= clen)
1358 return; // we did not read too much or already took care of the extras
1359
1360 if (const int64_t extras = payloadSeen - payloadTruncated - clen) {
1361 // server sent more that the advertised content length
1362 debugs(11, 5, "payloadSeen=" << payloadSeen <<
1363 " clen=" << clen << '/' << vrep->content_length <<
1364 " trucated=" << payloadTruncated << '+' << extras);
1365
1366 inBuf.chop(0, inBuf.length() - extras);
1367 payloadTruncated += extras;
1368 }
1369 }
1370
1371 /**
1372 * Call this when there is data from the origin server
1373 * which should be sent to either StoreEntry, or to ICAP...
1374 */
1375 void
1376 HttpStateData::writeReplyBody()
1377 {
1378 truncateVirginBody(); // if needed
1379 const char *data = inBuf.rawContent();
1380 int len = inBuf.length();
1381 addVirginReplyBody(data, len);
1382 inBuf.consume(len);
1383 }
1384
1385 bool
1386 HttpStateData::decodeAndWriteReplyBody()
1387 {
1388 const char *data = NULL;
1389 int len;
1390 bool wasThereAnException = false;
1391 assert(flags.chunked);
1392 assert(httpChunkDecoder);
1393 SQUID_ENTER_THROWING_CODE();
1394 MemBuf decodedData;
1395 decodedData.init();
1396 httpChunkDecoder->setPayloadBuffer(&decodedData);
1397 const bool doneParsing = httpChunkDecoder->parse(inBuf);
1398 inBuf = httpChunkDecoder->remaining(); // sync buffers after parse
1399 len = decodedData.contentSize();
1400 data=decodedData.content();
1401 addVirginReplyBody(data, len);
1402 if (doneParsing) {
1403 lastChunk = 1;
1404 flags.do_next_read = false;
1405 }
1406 SQUID_EXIT_THROWING_CODE(wasThereAnException);
1407 return wasThereAnException;
1408 }
1409
1410 /**
1411 * processReplyBody has two purposes:
1412 * 1 - take the reply body data, if any, and put it into either
1413 * the StoreEntry, or give it over to ICAP.
1414 * 2 - see if we made it to the end of the response (persistent
1415 * connections and such)
1416 */
1417 void
1418 HttpStateData::processReplyBody()
1419 {
1420 Ip::Address client_addr;
1421 bool ispinned = false;
1422
1423 if (!flags.headers_parsed) {
1424 flags.do_next_read = true;
1425 maybeReadVirginBody();
1426 return;
1427 }
1428
1429 #if USE_ADAPTATION
1430 debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending);
1431 if (adaptationAccessCheckPending)
1432 return;
1433
1434 #endif
1435
1436 /*
1437 * At this point the reply headers have been parsed and consumed.
1438 * That means header content has been removed from readBuf and
1439 * it contains only body data.
1440 */
1441 if (entry->isAccepting()) {
1442 if (flags.chunked) {
1443 if (!decodeAndWriteReplyBody()) {
1444 flags.do_next_read = false;
1445 serverComplete();
1446 return;
1447 }
1448 } else
1449 writeReplyBody();
1450 }
1451
1452 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1453 // The above writeReplyBody() call may have aborted the store entry.
1454 abortTransaction("store entry aborted while storing reply");
1455 return;
1456 } else
1457 switch (persistentConnStatus()) {
1458 case INCOMPLETE_MSG: {
1459 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG from " << serverConnection);
1460 /* Wait for more data or EOF condition */
1461 AsyncCall::Pointer nil;
1462 if (flags.keepalive_broken) {
1463 commSetConnTimeout(serverConnection, 10, nil);
1464 } else {
1465 commSetConnTimeout(serverConnection, Config.Timeout.read, nil);
1466 }
1467
1468 flags.do_next_read = true;
1469 }
1470 break;
1471
1472 case COMPLETE_PERSISTENT_MSG:
1473 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG from " << serverConnection);
1474 /* yes we have to clear all these! */
1475 commUnsetConnTimeout(serverConnection);
1476 flags.do_next_read = false;
1477
1478 comm_remove_close_handler(serverConnection->fd, closeHandler);
1479 closeHandler = NULL;
1480 fwd->unregister(serverConnection);
1481
1482 if (request->flags.spoofClientIp)
1483 client_addr = request->client_addr;
1484
1485 if (request->flags.pinned) {
1486 ispinned = true;
1487 } else if (request->flags.connectionAuth && request->flags.authSent) {
1488 ispinned = true;
1489 }
1490
1491 if (ispinned && request->clientConnectionManager.valid()) {
1492 request->clientConnectionManager->pinConnection(serverConnection, request, _peer,
1493 (request->flags.connectionAuth));
1494 } else {
1495 fwd->pconnPush(serverConnection, request->GetHost());
1496 }
1497
1498 serverConnection = NULL;
1499 serverComplete();
1500 return;
1501
1502 case COMPLETE_NONPERSISTENT_MSG:
1503 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection);
1504 serverComplete();
1505 return;
1506 }
1507
1508 maybeReadVirginBody();
1509 }
1510
1511 bool
1512 HttpStateData::mayReadVirginReplyBody() const
1513 {
1514 // TODO: Be more precise here. For example, if/when reading trailer, we may
1515 // not be doneWithServer() yet, but we should return false. Similarly, we
1516 // could still be writing the request body after receiving the whole reply.
1517 return !doneWithServer();
1518 }
1519
1520 void
1521 HttpStateData::maybeReadVirginBody()
1522 {
1523 // too late to read
1524 if (!Comm::IsConnOpen(serverConnection) || fd_table[serverConnection->fd].closing())
1525 return;
1526
1527 if (!maybeMakeSpaceAvailable(false))
1528 return;
1529
1530 // XXX: get rid of the do_next_read flag
1531 // check for the proper reasons preventing read(2)
1532 if (!flags.do_next_read)
1533 return;
1534
1535 flags.do_next_read = false;
1536
1537 // must not already be waiting for read(2) ...
1538 assert(!Comm::MonitorsRead(serverConnection->fd));
1539
1540 // wait for read(2) to be possible.
1541 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
1542 AsyncCall::Pointer call = JobCallback(11, 5, Dialer, this, HttpStateData::readReply);
1543 Comm::Read(serverConnection, call);
1544 }
1545
1546 bool
1547 HttpStateData::maybeMakeSpaceAvailable(bool doGrow)
1548 {
1549 // how much we are allowed to buffer
1550 const int limitBuffer = (flags.headers_parsed ? Config.readAheadGap : Config.maxReplyHeaderSize);
1551
1552 if (limitBuffer < 0 || inBuf.length() >= (SBuf::size_type)limitBuffer) {
1553 // when buffer is at or over limit already
1554 debugs(11, 7, "wont read up to " << limitBuffer << ". buffer has (" << inBuf.length() << "/" << inBuf.spaceSize() << ") from " << serverConnection);
1555 debugs(11, DBG_DATA, "buffer has {" << inBuf << "}");
1556 // Process next response from buffer
1557 processReply();
1558 return false;
1559 }
1560
1561 // how much we want to read
1562 const size_t read_size = calcBufferSpaceToReserve(inBuf.spaceSize(), (limitBuffer - inBuf.length()));
1563
1564 if (!read_size) {
1565 debugs(11, 7, "wont read up to " << read_size << " into buffer (" << inBuf.length() << "/" << inBuf.spaceSize() << ") from " << serverConnection);
1566 return false;
1567 }
1568
1569 // just report whether we could grow or not, dont actually do it
1570 if (doGrow)
1571 return (read_size >= 2);
1572
1573 // we may need to grow the buffer
1574 inBuf.reserveSpace(read_size);
1575 debugs(11, 8, (!flags.do_next_read ? "wont" : "may") <<
1576 " read up to " << read_size << " bytes info buf(" << inBuf.length() << "/" << inBuf.spaceSize() <<
1577 ") from " << serverConnection);
1578
1579 return (inBuf.spaceSize() >= 2); // only read if there is 1+ bytes of space available
1580 }
1581
1582 /// called after writing the very last request byte (body, last-chunk, etc)
1583 void
1584 HttpStateData::wroteLast(const CommIoCbParams &io)
1585 {
1586 debugs(11, 5, HERE << serverConnection << ": size " << io.size << ": errflag " << io.flag << ".");
1587 #if URL_CHECKSUM_DEBUG
1588
1589 entry->mem_obj->checkUrlChecksum();
1590 #endif
1591
1592 if (io.size > 0) {
1593 fd_bytes(io.fd, io.size, FD_WRITE);
1594 kb_incr(&(statCounter.server.all.kbytes_out), io.size);
1595 kb_incr(&(statCounter.server.http.kbytes_out), io.size);
1596 }
1597
1598 if (io.flag == Comm::ERR_CLOSING)
1599 return;
1600
1601 if (io.flag) {
1602 ErrorState *err = new ErrorState(ERR_WRITE_ERROR, Http::scBadGateway, fwd->request);
1603 err->xerrno = io.xerrno;
1604 fwd->fail(err);
1605 serverConnection->close();
1606 return;
1607 }
1608
1609 sendComplete();
1610 }
1611
1612 /// successfully wrote the entire request (including body, last-chunk, etc.)
1613 void
1614 HttpStateData::sendComplete()
1615 {
1616 /*
1617 * Set the read timeout here because it hasn't been set yet.
1618 * We only set the read timeout after the request has been
1619 * fully written to the peer. If we start the timeout
1620 * after connection establishment, then we are likely to hit
1621 * the timeout for POST/PUT requests that have very large
1622 * request bodies.
1623 */
1624 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
1625 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
1626 TimeoutDialer, this, HttpStateData::httpTimeout);
1627
1628 commSetConnTimeout(serverConnection, Config.Timeout.read, timeoutCall);
1629 flags.request_sent = true;
1630 request->hier.peer_http_request_sent = current_time;
1631 }
1632
1633 // Close the HTTP server connection. Used by serverComplete().
1634 void
1635 HttpStateData::closeServer()
1636 {
1637 debugs(11,5, HERE << "closing HTTP server " << serverConnection << " this " << this);
1638
1639 if (Comm::IsConnOpen(serverConnection)) {
1640 fwd->unregister(serverConnection);
1641 comm_remove_close_handler(serverConnection->fd, closeHandler);
1642 closeHandler = NULL;
1643 serverConnection->close();
1644 }
1645 }
1646
1647 bool
1648 HttpStateData::doneWithServer() const
1649 {
1650 return !Comm::IsConnOpen(serverConnection);
1651 }
1652
1653 /*
1654 * Fixup authentication request headers for special cases
1655 */
1656 static void
1657 httpFixupAuthentication(HttpRequest * request, const HttpHeader * hdr_in, HttpHeader * hdr_out, const HttpStateFlags &flags)
1658 {
1659 http_hdr_type header = flags.originpeer ? HDR_AUTHORIZATION : HDR_PROXY_AUTHORIZATION;
1660
1661 /* Nothing to do unless we are forwarding to a peer */
1662 if (!request->flags.proxying)
1663 return;
1664
1665 /* Needs to be explicitly enabled */
1666 if (!request->peer_login)
1667 return;
1668
1669 /* Maybe already dealt with? */
1670 if (hdr_out->has(header))
1671 return;
1672
1673 /* Nothing to do here for PASSTHRU */
1674 if (strcmp(request->peer_login, "PASSTHRU") == 0)
1675 return;
1676
1677 /* PROXYPASS is a special case, single-signon to servers with the proxy password (basic only) */
1678 if (flags.originpeer && strcmp(request->peer_login, "PROXYPASS") == 0 && hdr_in->has(HDR_PROXY_AUTHORIZATION)) {
1679 const char *auth = hdr_in->getStr(HDR_PROXY_AUTHORIZATION);
1680
1681 if (auth && strncasecmp(auth, "basic ", 6) == 0) {
1682 hdr_out->putStr(header, auth);
1683 return;
1684 }
1685 }
1686
1687 uint8_t loginbuf[base64_encode_len(MAX_LOGIN_SZ)];
1688 size_t blen;
1689 struct base64_encode_ctx ctx;
1690 base64_encode_init(&ctx);
1691
1692 /* Special mode to pass the username to the upstream cache */
1693 if (*request->peer_login == '*') {
1694 const char *username = "-";
1695
1696 if (request->extacl_user.size())
1697 username = request->extacl_user.termedBuf();
1698 #if USE_AUTH
1699 else if (request->auth_user_request != NULL)
1700 username = request->auth_user_request->username();
1701 #endif
1702
1703 blen = base64_encode_update(&ctx, loginbuf, strlen(username), reinterpret_cast<const uint8_t*>(username));
1704 blen += base64_encode_update(&ctx, loginbuf+blen, strlen(request->peer_login +1), reinterpret_cast<const uint8_t*>(request->peer_login +1));
1705 blen += base64_encode_final(&ctx, loginbuf+blen);
1706 httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1707 return;
1708 }
1709
1710 /* external_acl provided credentials */
1711 if (request->extacl_user.size() && request->extacl_passwd.size() &&
1712 (strcmp(request->peer_login, "PASS") == 0 ||
1713 strcmp(request->peer_login, "PROXYPASS") == 0)) {
1714
1715 blen = base64_encode_update(&ctx, loginbuf, request->extacl_user.size(), reinterpret_cast<const uint8_t*>(request->extacl_user.rawBuf()));
1716 blen += base64_encode_update(&ctx, loginbuf+blen, 1, reinterpret_cast<const uint8_t*>(":"));
1717 blen += base64_encode_update(&ctx, loginbuf+blen, request->extacl_passwd.size(), reinterpret_cast<const uint8_t*>(request->extacl_passwd.rawBuf()));
1718 blen += base64_encode_final(&ctx, loginbuf+blen);
1719 httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1720 return;
1721 }
1722 // if no external user credentials are available to fake authentication with PASS acts like PASSTHRU
1723 if (strcmp(request->peer_login, "PASS") == 0)
1724 return;
1725
1726 /* Kerberos login to peer */
1727 #if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
1728 if (strncmp(request->peer_login, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
1729 char *Token=NULL;
1730 char *PrincipalName=NULL,*p;
1731 if ((p=strchr(request->peer_login,':')) != NULL ) {
1732 PrincipalName=++p;
1733 }
1734 Token = peer_proxy_negotiate_auth(PrincipalName, request->peer_host);
1735 if (Token) {
1736 httpHeaderPutStrf(hdr_out, header, "Negotiate %s",Token);
1737 }
1738 return;
1739 }
1740 #endif /* HAVE_KRB5 && HAVE_GSSAPI */
1741
1742 blen = base64_encode_update(&ctx, loginbuf, strlen(request->peer_login), reinterpret_cast<const uint8_t*>(request->peer_login));
1743 blen += base64_encode_final(&ctx, loginbuf+blen);
1744 httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1745 return;
1746 }
1747
1748 /*
1749 * build request headers and append them to a given MemBuf
1750 * used by buildRequestPrefix()
1751 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1752 */
1753 void
1754 HttpStateData::httpBuildRequestHeader(HttpRequest * request,
1755 StoreEntry * entry,
1756 const AccessLogEntryPointer &al,
1757 HttpHeader * hdr_out,
1758 const HttpStateFlags &flags)
1759 {
1760 /* building buffer for complex strings */
1761 #define BBUF_SZ (MAX_URL+32)
1762 LOCAL_ARRAY(char, bbuf, BBUF_SZ);
1763 LOCAL_ARRAY(char, ntoabuf, MAX_IPSTRLEN);
1764 const HttpHeader *hdr_in = &request->header;
1765 const HttpHeaderEntry *e = NULL;
1766 HttpHeaderPos pos = HttpHeaderInitPos;
1767 assert (hdr_out->owner == hoRequest);
1768
1769 /* use our IMS header if the cached entry has Last-Modified time */
1770 if (request->lastmod > -1)
1771 hdr_out->putTime(HDR_IF_MODIFIED_SINCE, request->lastmod);
1772
1773 // Add our own If-None-Match field if the cached entry has a strong ETag.
1774 // copyOneHeaderFromClientsideRequestToUpstreamRequest() adds client ones.
1775 if (request->etag.size() > 0) {
1776 hdr_out->addEntry(new HttpHeaderEntry(HDR_IF_NONE_MATCH, NULL,
1777 request->etag.termedBuf()));
1778 }
1779
1780 bool we_do_ranges = decideIfWeDoRanges (request);
1781
1782 String strConnection (hdr_in->getList(HDR_CONNECTION));
1783
1784 while ((e = hdr_in->getEntry(&pos)))
1785 copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, hdr_out, we_do_ranges, flags);
1786
1787 /* Abstraction break: We should interpret multipart/byterange responses
1788 * into offset-length data, and this works around our inability to do so.
1789 */
1790 if (!we_do_ranges && request->multipartRangeRequest()) {
1791 /* don't cache the result */
1792 request->flags.cachable = false;
1793 /* pretend it's not a range request */
1794 request->ignoreRange("want to request the whole object");
1795 request->flags.isRanged = false;
1796 }
1797
1798 /* append Via */
1799 if (Config.onoff.via) {
1800 String strVia;
1801 strVia = hdr_in->getList(HDR_VIA);
1802 snprintf(bbuf, BBUF_SZ, "%d.%d %s",
1803 request->http_ver.major,
1804 request->http_ver.minor, ThisCache);
1805 strListAdd(&strVia, bbuf, ',');
1806 hdr_out->putStr(HDR_VIA, strVia.termedBuf());
1807 strVia.clean();
1808 }
1809
1810 if (request->flags.accelerated) {
1811 /* Append Surrogate-Capabilities */
1812 String strSurrogate(hdr_in->getList(HDR_SURROGATE_CAPABILITY));
1813 #if USE_SQUID_ESI
1814 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", Config.Accel.surrogate_id);
1815 #else
1816 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0\"", Config.Accel.surrogate_id);
1817 #endif
1818 strListAdd(&strSurrogate, bbuf, ',');
1819 hdr_out->putStr(HDR_SURROGATE_CAPABILITY, strSurrogate.termedBuf());
1820 }
1821
1822 /** \pre Handle X-Forwarded-For */
1823 if (strcmp(opt_forwarded_for, "delete") != 0) {
1824
1825 String strFwd = hdr_in->getList(HDR_X_FORWARDED_FOR);
1826
1827 if (strFwd.size() > 65536/2) {
1828 // There is probably a forwarding loop with Via detection disabled.
1829 // If we do nothing, String will assert on overflow soon.
1830 // TODO: Terminate all transactions with huge XFF?
1831 strFwd = "error";
1832
1833 static int warnedCount = 0;
1834 if (warnedCount++ < 100) {
1835 const char *url = entry ? entry->url() : urlCanonical(request);
1836 debugs(11, DBG_IMPORTANT, "Warning: likely forwarding loop with " << url);
1837 }
1838 }
1839
1840 if (strcmp(opt_forwarded_for, "on") == 0) {
1841 /** If set to ON - append client IP or 'unknown'. */
1842 if ( request->client_addr.isNoAddr() )
1843 strListAdd(&strFwd, "unknown", ',');
1844 else
1845 strListAdd(&strFwd, request->client_addr.toStr(ntoabuf, MAX_IPSTRLEN), ',');
1846 } else if (strcmp(opt_forwarded_for, "off") == 0) {
1847 /** If set to OFF - append 'unknown'. */
1848 strListAdd(&strFwd, "unknown", ',');
1849 } else if (strcmp(opt_forwarded_for, "transparent") == 0) {
1850 /** If set to TRANSPARENT - pass through unchanged. */
1851 } else if (strcmp(opt_forwarded_for, "truncate") == 0) {
1852 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
1853 if ( request->client_addr.isNoAddr() )
1854 strFwd = "unknown";
1855 else
1856 strFwd = request->client_addr.toStr(ntoabuf, MAX_IPSTRLEN);
1857 }
1858 if (strFwd.size() > 0)
1859 hdr_out->putStr(HDR_X_FORWARDED_FOR, strFwd.termedBuf());
1860 }
1861 /** If set to DELETE - do not copy through. */
1862
1863 /* append Host if not there already */
1864 if (!hdr_out->has(HDR_HOST)) {
1865 if (request->peer_domain) {
1866 hdr_out->putStr(HDR_HOST, request->peer_domain);
1867 } else if (request->port == urlDefaultPort(request->url.getScheme())) {
1868 /* use port# only if not default */
1869 hdr_out->putStr(HDR_HOST, request->GetHost());
1870 } else {
1871 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
1872 request->GetHost(),
1873 (int) request->port);
1874 }
1875 }
1876
1877 /* append Authorization if known in URL, not in header and going direct */
1878 if (!hdr_out->has(HDR_AUTHORIZATION)) {
1879 if (!request->flags.proxying && !request->url.userInfo().isEmpty()) {
1880 static uint8_t result[base64_encode_len(MAX_URL*2)]; // should be big enough for a single URI segment
1881 struct base64_encode_ctx ctx;
1882 base64_encode_init(&ctx);
1883 size_t blen = base64_encode_update(&ctx, result, request->url.userInfo().length(), reinterpret_cast<const uint8_t*>(request->url.userInfo().rawContent()));
1884 blen += base64_encode_final(&ctx, result+blen);
1885 result[blen] = '\0';
1886 if (blen)
1887 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %.*s", (int)blen, result);
1888 }
1889 }
1890
1891 /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
1892 httpFixupAuthentication(request, hdr_in, hdr_out, flags);
1893
1894 /* append Cache-Control, add max-age if not there already */
1895 {
1896 HttpHdrCc *cc = hdr_in->getCc();
1897
1898 if (!cc)
1899 cc = new HttpHdrCc();
1900
1901 #if 0 /* see bug 2330 */
1902 /* Set no-cache if determined needed but not found */
1903 if (request->flags.nocache)
1904 EBIT_SET(cc->mask, CC_NO_CACHE);
1905 #endif
1906
1907 /* Add max-age only without no-cache */
1908 if (!cc->hasMaxAge() && !cc->hasNoCache()) {
1909 const char *url =
1910 entry ? entry->url() : urlCanonical(request);
1911 cc->maxAge(getMaxAge(url));
1912
1913 }
1914
1915 /* Enforce sibling relations */
1916 if (flags.only_if_cached)
1917 cc->onlyIfCached(true);
1918
1919 hdr_out->putCc(cc);
1920
1921 delete cc;
1922 }
1923
1924 /* maybe append Connection: keep-alive */
1925 if (flags.keepalive) {
1926 hdr_out->putStr(HDR_CONNECTION, "keep-alive");
1927 }
1928
1929 /* append Front-End-Https */
1930 if (flags.front_end_https) {
1931 if (flags.front_end_https == 1 || request->url.getScheme() == AnyP::PROTO_HTTPS)
1932 hdr_out->putStr(HDR_FRONT_END_HTTPS, "On");
1933 }
1934
1935 if (flags.chunked_request) {
1936 // Do not just copy the original value so that if the client-side
1937 // starts decode other encodings, this code may remain valid.
1938 hdr_out->putStr(HDR_TRANSFER_ENCODING, "chunked");
1939 }
1940
1941 /* Now mangle the headers. */
1942 if (Config2.onoff.mangle_request_headers)
1943 httpHdrMangleList(hdr_out, request, ROR_REQUEST);
1944
1945 if (Config.request_header_add && !Config.request_header_add->empty())
1946 httpHdrAdd(hdr_out, request, al, *Config.request_header_add);
1947
1948 strConnection.clean();
1949 }
1950
1951 /**
1952 * Decides whether a particular header may be cloned from the received Clients request
1953 * to our outgoing fetch request.
1954 */
1955 void
1956 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request, HttpHeader * hdr_out, const int we_do_ranges, const HttpStateFlags &flags)
1957 {
1958 debugs(11, 5, "httpBuildRequestHeader: " << e->name << ": " << e->value );
1959
1960 switch (e->id) {
1961
1962 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
1963
1964 case HDR_PROXY_AUTHORIZATION:
1965 /** \par Proxy-Authorization:
1966 * Only pass on proxy authentication to peers for which
1967 * authentication forwarding is explicitly enabled
1968 */
1969 if (!flags.originpeer && flags.proxying && request->peer_login &&
1970 (strcmp(request->peer_login, "PASS") == 0 ||
1971 strcmp(request->peer_login, "PROXYPASS") == 0 ||
1972 strcmp(request->peer_login, "PASSTHRU") == 0)) {
1973 hdr_out->addEntry(e->clone());
1974 }
1975 break;
1976
1977 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
1978
1979 case HDR_CONNECTION: /** \par Connection: */
1980 case HDR_TE: /** \par TE: */
1981 case HDR_KEEP_ALIVE: /** \par Keep-Alive: */
1982 case HDR_PROXY_AUTHENTICATE: /** \par Proxy-Authenticate: */
1983 case HDR_TRAILER: /** \par Trailer: */
1984 case HDR_UPGRADE: /** \par Upgrade: */
1985 case HDR_TRANSFER_ENCODING: /** \par Transfer-Encoding: */
1986 break;
1987
1988 /** \par OTHER headers I haven't bothered to track down yet. */
1989
1990 case HDR_AUTHORIZATION:
1991 /** \par WWW-Authorization:
1992 * Pass on WWW authentication */
1993
1994 if (!flags.originpeer) {
1995 hdr_out->addEntry(e->clone());
1996 } else {
1997 /** \note In accelerators, only forward authentication if enabled
1998 * (see also httpFixupAuthentication for special cases)
1999 */
2000 if (request->peer_login &&
2001 (strcmp(request->peer_login, "PASS") == 0 ||
2002 strcmp(request->peer_login, "PASSTHRU") == 0 ||
2003 strcmp(request->peer_login, "PROXYPASS") == 0)) {
2004 hdr_out->addEntry(e->clone());
2005 }
2006 }
2007
2008 break;
2009
2010 case HDR_HOST:
2011 /** \par Host:
2012 * Normally Squid rewrites the Host: header.
2013 * However, there is one case when we don't: If the URL
2014 * went through our redirector and the admin configured
2015 * 'redir_rewrites_host' to be off.
2016 */
2017 if (request->peer_domain)
2018 hdr_out->putStr(HDR_HOST, request->peer_domain);
2019 else if (request->flags.redirected && !Config.onoff.redir_rewrites_host)
2020 hdr_out->addEntry(e->clone());
2021 else {
2022 /* use port# only if not default */
2023
2024 if (request->port == urlDefaultPort(request->url.getScheme())) {
2025 hdr_out->putStr(HDR_HOST, request->GetHost());
2026 } else {
2027 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
2028 request->GetHost(),
2029 (int) request->port);
2030 }
2031 }
2032
2033 break;
2034
2035 case HDR_IF_MODIFIED_SINCE:
2036 /** \par If-Modified-Since:
2037 * append unless we added our own,
2038 * but only if cache_miss_revalidate is enabled, or
2039 * the request is not cacheable, or
2040 * the request contains authentication credentials.
2041 * \note at most one client's If-Modified-Since header can pass through
2042 */
2043 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2044 if (hdr_out->has(HDR_IF_MODIFIED_SINCE))
2045 break;
2046 else if (Config.onoff.cache_miss_revalidate || !request->flags.cachable || request->flags.auth)
2047 hdr_out->addEntry(e->clone());
2048 break;
2049
2050 case HDR_IF_NONE_MATCH:
2051 /** \par If-None-Match:
2052 * append if the wildcard '*' special case value is present, or
2053 * cache_miss_revalidate is disabled, or
2054 * the request is not cacheable in this proxy, or
2055 * the request contains authentication credentials.
2056 * \note this header lists a set of responses for the server to elide sending. Squid added values are extending that set.
2057 */
2058 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2059 if (hdr_out->hasListMember(HDR_IF_MATCH, "*", ',') || Config.onoff.cache_miss_revalidate || !request->flags.cachable || request->flags.auth)
2060 hdr_out->addEntry(e->clone());
2061 break;
2062
2063 case HDR_MAX_FORWARDS:
2064 /** \par Max-Forwards:
2065 * pass only on TRACE or OPTIONS requests */
2066 if (request->method == Http::METHOD_TRACE || request->method == Http::METHOD_OPTIONS) {
2067 const int64_t hops = e->getInt64();
2068
2069 if (hops > 0)
2070 hdr_out->putInt64(HDR_MAX_FORWARDS, hops - 1);
2071 }
2072
2073 break;
2074
2075 case HDR_VIA:
2076 /** \par Via:
2077 * If Via is disabled then forward any received header as-is.
2078 * Otherwise leave for explicit updated addition later. */
2079
2080 if (!Config.onoff.via)
2081 hdr_out->addEntry(e->clone());
2082
2083 break;
2084
2085 case HDR_RANGE:
2086
2087 case HDR_IF_RANGE:
2088
2089 case HDR_REQUEST_RANGE:
2090 /** \par Range:, If-Range:, Request-Range:
2091 * Only pass if we accept ranges */
2092 if (!we_do_ranges)
2093 hdr_out->addEntry(e->clone());
2094
2095 break;
2096
2097 case HDR_PROXY_CONNECTION: // SHOULD ignore. But doing so breaks things.
2098 break;
2099
2100 case HDR_CONTENT_LENGTH:
2101 // pass through unless we chunk; also, keeping this away from default
2102 // prevents request smuggling via Connection: Content-Length tricks
2103 if (!flags.chunked_request)
2104 hdr_out->addEntry(e->clone());
2105 break;
2106
2107 case HDR_X_FORWARDED_FOR:
2108
2109 case HDR_CACHE_CONTROL:
2110 /** \par X-Forwarded-For:, Cache-Control:
2111 * handled specially by Squid, so leave off for now.
2112 * append these after the loop if needed */
2113 break;
2114
2115 case HDR_FRONT_END_HTTPS:
2116 /** \par Front-End-Https:
2117 * Pass thru only if peer is configured with front-end-https */
2118 if (!flags.front_end_https)
2119 hdr_out->addEntry(e->clone());
2120
2121 break;
2122
2123 default:
2124 /** \par default.
2125 * pass on all other header fields
2126 * which are NOT listed by the special Connection: header. */
2127
2128 if (strConnection.size()>0 && strListIsMember(&strConnection, e->name.termedBuf(), ',')) {
2129 debugs(11, 2, "'" << e->name << "' header cropped by Connection: definition");
2130 return;
2131 }
2132
2133 hdr_out->addEntry(e->clone());
2134 }
2135 }
2136
2137 bool
2138 HttpStateData::decideIfWeDoRanges (HttpRequest * request)
2139 {
2140 bool result = true;
2141 /* decide if we want to do Ranges ourselves
2142 * and fetch the whole object now)
2143 * We want to handle Ranges ourselves iff
2144 * - we can actually parse client Range specs
2145 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
2146 * - reply will be cachable
2147 * (If the reply will be uncachable we have to throw it away after
2148 * serving this request, so it is better to forward ranges to
2149 * the server and fetch only the requested content)
2150 */
2151
2152 int64_t roffLimit = request->getRangeOffsetLimit();
2153
2154 if (NULL == request->range || !request->flags.cachable
2155 || request->range->offsetLimitExceeded(roffLimit) || request->flags.connectionAuth)
2156 result = false;
2157
2158 debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
2159 request->range << ", cachable: " <<
2160 request->flags.cachable << "; we_do_ranges: " << result);
2161
2162 return result;
2163 }
2164
2165 /* build request prefix and append it to a given MemBuf;
2166 * return the length of the prefix */
2167 mb_size_t
2168 HttpStateData::buildRequestPrefix(MemBuf * mb)
2169 {
2170 const int offset = mb->size;
2171 /* Uses a local httpver variable to print the HTTP label
2172 * since the HttpRequest may have an older version label.
2173 * XXX: This could create protocol bugs as the headers sent and
2174 * flow control should all be based on the HttpRequest version
2175 * not the one we are sending. Needs checking.
2176 */
2177 const AnyP::ProtocolVersion httpver = Http::ProtocolVersion();
2178 const char * url;
2179 if (_peer && !_peer->options.originserver)
2180 url = urlCanonical(request);
2181 else
2182 url = request->urlpath.termedBuf();
2183 mb->appendf(SQUIDSBUFPH " %s %s/%d.%d\r\n",
2184 SQUIDSBUFPRINT(request->method.image()),
2185 url && *url ? url : "/",
2186 AnyP::ProtocolType_str[httpver.protocol],
2187 httpver.major,httpver.minor);
2188 /* build and pack headers */
2189 {
2190 HttpHeader hdr(hoRequest);
2191 httpBuildRequestHeader(request, entry, fwd->al, &hdr, flags);
2192
2193 if (request->flags.pinned && request->flags.connectionAuth)
2194 request->flags.authSent = true;
2195 else if (hdr.has(HDR_AUTHORIZATION))
2196 request->flags.authSent = true;
2197
2198 hdr.packInto(mb);
2199 hdr.clean();
2200 }
2201 /* append header terminator */
2202 mb->append(crlf, 2);
2203 return mb->size - offset;
2204 }
2205
2206 /* This will be called when connect completes. Write request. */
2207 bool
2208 HttpStateData::sendRequest()
2209 {
2210 MemBuf mb;
2211
2212 debugs(11, 5, HERE << serverConnection << ", request " << request << ", this " << this << ".");
2213
2214 if (!Comm::IsConnOpen(serverConnection)) {
2215 debugs(11,3, HERE << "cannot send request to closing " << serverConnection);
2216 assert(closeHandler != NULL);
2217 return false;
2218 }
2219
2220 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
2221 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
2222 TimeoutDialer, this, HttpStateData::httpTimeout);
2223 commSetConnTimeout(serverConnection, Config.Timeout.lifetime, timeoutCall);
2224 flags.do_next_read = true;
2225 maybeReadVirginBody();
2226
2227 if (request->body_pipe != NULL) {
2228 if (!startRequestBodyFlow()) // register to receive body data
2229 return false;
2230 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2231 requestSender = JobCallback(11,5,
2232 Dialer, this, HttpStateData::sentRequestBody);
2233
2234 Must(!flags.chunked_request);
2235 // use chunked encoding if we do not know the length
2236 if (request->content_length < 0)
2237 flags.chunked_request = true;
2238 } else {
2239 assert(!requestBodySource);
2240 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2241 requestSender = JobCallback(11,5,
2242 Dialer, this, HttpStateData::wroteLast);
2243 }
2244
2245 flags.originpeer = (_peer != NULL && _peer->options.originserver);
2246 flags.proxying = (_peer != NULL && !flags.originpeer);
2247
2248 /*
2249 * Is keep-alive okay for all request methods?
2250 */
2251 if (request->flags.mustKeepalive)
2252 flags.keepalive = true;
2253 else if (request->flags.pinned)
2254 flags.keepalive = request->persistent();
2255 else if (!Config.onoff.server_pconns)
2256 flags.keepalive = false;
2257 else if (_peer == NULL)
2258 flags.keepalive = true;
2259 else if (_peer->stats.n_keepalives_sent < 10)
2260 flags.keepalive = true;
2261 else if ((double) _peer->stats.n_keepalives_recv /
2262 (double) _peer->stats.n_keepalives_sent > 0.50)
2263 flags.keepalive = true;
2264
2265 if (_peer) {
2266 /*The old code here was
2267 if (neighborType(_peer, request) == PEER_SIBLING && ...
2268 which is equivalent to:
2269 if (neighborType(_peer, NULL) == PEER_SIBLING && ...
2270 or better:
2271 if (((_peer->type == PEER_MULTICAST && p->options.mcast_siblings) ||
2272 _peer->type == PEER_SIBLINGS ) && _peer->options.allow_miss)
2273 flags.only_if_cached = 1;
2274
2275 But I suppose it was a bug
2276 */
2277 if (neighborType(_peer, request) == PEER_SIBLING &&
2278 !_peer->options.allow_miss)
2279 flags.only_if_cached = true;
2280
2281 flags.front_end_https = _peer->front_end_https;
2282 }
2283
2284 mb.init();
2285 request->peer_host=_peer?_peer->host:NULL;
2286 buildRequestPrefix(&mb);
2287
2288 debugs(11, 2, "HTTP Server " << serverConnection);
2289 debugs(11, 2, "HTTP Server REQUEST:\n---------\n" << mb.buf << "\n----------");
2290
2291 Comm::Write(serverConnection, &mb, requestSender);
2292 return true;
2293 }
2294
2295 bool
2296 HttpStateData::getMoreRequestBody(MemBuf &buf)
2297 {
2298 // parent's implementation can handle the no-encoding case
2299 if (!flags.chunked_request)
2300 return Client::getMoreRequestBody(buf);
2301
2302 MemBuf raw;
2303
2304 Must(requestBodySource != NULL);
2305 if (!requestBodySource->getMoreData(raw))
2306 return false; // no request body bytes to chunk yet
2307
2308 // optimization: pre-allocate buffer size that should be enough
2309 const mb_size_t rawDataSize = raw.contentSize();
2310 // we may need to send: hex-chunk-size CRLF raw-data CRLF last-chunk
2311 buf.init(16 + 2 + rawDataSize + 2 + 5, raw.max_capacity);
2312
2313 buf.appendf("%x\r\n", static_cast<unsigned int>(rawDataSize));
2314 buf.append(raw.content(), rawDataSize);
2315 buf.append("\r\n", 2);
2316
2317 Must(rawDataSize > 0); // we did not accidently created last-chunk above
2318
2319 // Do not send last-chunk unless we successfully received everything
2320 if (receivedWholeRequestBody) {
2321 Must(!flags.sentLastChunk);
2322 flags.sentLastChunk = true;
2323 buf.append("0\r\n\r\n", 5);
2324 }
2325
2326 return true;
2327 }
2328
2329 void
2330 httpStart(FwdState *fwd)
2331 {
2332 debugs(11, 3, fwd->request->method << ' ' << fwd->entry->url());
2333 AsyncJob::Start(new HttpStateData(fwd));
2334 }
2335
2336 void
2337 HttpStateData::start()
2338 {
2339 if (!sendRequest()) {
2340 debugs(11, 3, "httpStart: aborted");
2341 mustStop("HttpStateData::start failed");
2342 return;
2343 }
2344
2345 ++ statCounter.server.all.requests;
2346 ++ statCounter.server.http.requests;
2347
2348 /*
2349 * We used to set the read timeout here, but not any more.
2350 * Now its set in httpSendComplete() after the full request,
2351 * including request body, has been written to the server.
2352 */
2353 }
2354
2355 /// if broken posts are enabled for the request, try to fix and return true
2356 bool
2357 HttpStateData::finishingBrokenPost()
2358 {
2359 #if USE_HTTP_VIOLATIONS
2360 if (!Config.accessList.brokenPosts) {
2361 debugs(11, 5, HERE << "No brokenPosts list");
2362 return false;
2363 }
2364
2365 ACLFilledChecklist ch(Config.accessList.brokenPosts, originalRequest(), NULL);
2366 if (ch.fastCheck() != ACCESS_ALLOWED) {
2367 debugs(11, 5, HERE << "didn't match brokenPosts");
2368 return false;
2369 }
2370
2371 if (!Comm::IsConnOpen(serverConnection)) {
2372 debugs(11, 3, HERE << "ignoring broken POST for closed " << serverConnection);
2373 assert(closeHandler != NULL);
2374 return true; // prevent caller from proceeding as if nothing happened
2375 }
2376
2377 debugs(11, 3, "finishingBrokenPost: fixing broken POST");
2378 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2379 requestSender = JobCallback(11,5,
2380 Dialer, this, HttpStateData::wroteLast);
2381 Comm::Write(serverConnection, "\r\n", 2, requestSender, NULL);
2382 return true;
2383 #else
2384 return false;
2385 #endif /* USE_HTTP_VIOLATIONS */
2386 }
2387
2388 /// if needed, write last-chunk to end the request body and return true
2389 bool
2390 HttpStateData::finishingChunkedRequest()
2391 {
2392 if (flags.sentLastChunk) {
2393 debugs(11, 5, HERE << "already sent last-chunk");
2394 return false;
2395 }
2396
2397 Must(receivedWholeRequestBody); // or we should not be sending last-chunk
2398 flags.sentLastChunk = true;
2399
2400 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2401 requestSender = JobCallback(11,5, Dialer, this, HttpStateData::wroteLast);
2402 Comm::Write(serverConnection, "0\r\n\r\n", 5, requestSender, NULL);
2403 return true;
2404 }
2405
2406 void
2407 HttpStateData::doneSendingRequestBody()
2408 {
2409 Client::doneSendingRequestBody();
2410 debugs(11,5, HERE << serverConnection);
2411
2412 // do we need to write something after the last body byte?
2413 if (flags.chunked_request && finishingChunkedRequest())
2414 return;
2415 if (!flags.chunked_request && finishingBrokenPost())
2416 return;
2417
2418 sendComplete();
2419 }
2420
2421 // more origin request body data is available
2422 void
2423 HttpStateData::handleMoreRequestBodyAvailable()
2424 {
2425 if (eof || !Comm::IsConnOpen(serverConnection)) {
2426 // XXX: we should check this condition in other callbacks then!
2427 // TODO: Check whether this can actually happen: We should unsubscribe
2428 // as a body consumer when the above condition(s) are detected.
2429 debugs(11, DBG_IMPORTANT, HERE << "Transaction aborted while reading HTTP body");
2430 return;
2431 }
2432
2433 assert(requestBodySource != NULL);
2434
2435 if (requestBodySource->buf().hasContent()) {
2436 // XXX: why does not this trigger a debug message on every request?
2437
2438 if (flags.headers_parsed && !flags.abuse_detected) {
2439 flags.abuse_detected = true;
2440 debugs(11, DBG_IMPORTANT, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << request->client_addr << "' -> '" << entry->url() << "'" );
2441
2442 if (virginReply()->sline.status() == Http::scInvalidHeader) {
2443 serverConnection->close();
2444 return;
2445 }
2446 }
2447 }
2448
2449 HttpStateData::handleMoreRequestBodyAvailable();
2450 }
2451
2452 // premature end of the request body
2453 void
2454 HttpStateData::handleRequestBodyProducerAborted()
2455 {
2456 Client::handleRequestBodyProducerAborted();
2457 if (entry->isEmpty()) {
2458 debugs(11, 3, "request body aborted: " << serverConnection);
2459 // We usually get here when ICAP REQMOD aborts during body processing.
2460 // We might also get here if client-side aborts, but then our response
2461 // should not matter because either client-side will provide its own or
2462 // there will be no response at all (e.g., if the the client has left).
2463 ErrorState *err = new ErrorState(ERR_ICAP_FAILURE, Http::scInternalServerError, fwd->request);
2464 err->detailError(ERR_DETAIL_SRV_REQMOD_REQ_BODY);
2465 fwd->fail(err);
2466 }
2467
2468 abortTransaction("request body producer aborted");
2469 }
2470
2471 // called when we wrote request headers(!) or a part of the body
2472 void
2473 HttpStateData::sentRequestBody(const CommIoCbParams &io)
2474 {
2475 if (io.size > 0)
2476 kb_incr(&statCounter.server.http.kbytes_out, io.size);
2477
2478 Client::sentRequestBody(io);
2479 }
2480
2481 // Quickly abort the transaction
2482 // TODO: destruction should be sufficient as the destructor should cleanup,
2483 // including canceling close handlers
2484 void
2485 HttpStateData::abortTransaction(const char *reason)
2486 {
2487 debugs(11,5, HERE << "aborting transaction for " << reason <<
2488 "; " << serverConnection << ", this " << this);
2489
2490 if (Comm::IsConnOpen(serverConnection)) {
2491 serverConnection->close();
2492 return;
2493 }
2494
2495 fwd->handleUnregisteredServerEnd();
2496 mustStop("HttpStateData::abortTransaction");
2497 }
2498