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