2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 11 Hypertext Transfer Protocol (HTTP) */
12 * Anonymizing patch by lutz@as-node.jena.thur.de
13 * have a look into http-anon.c to get more information.
17 #include "acl/FilledChecklist.h"
18 #include "base/AsyncJobCalls.h"
20 #include "base/TextException.h"
22 #include "CachePeer.h"
23 #include "client_side.h"
24 #include "comm/Connection.h"
25 #include "comm/Read.h"
26 #include "comm/Write.h"
28 #include "error/Detail.h"
29 #include "errorpage.h"
34 #include "http/one/ResponseParser.h"
35 #include "http/one/TeChunkedParser.h"
36 #include "http/Stream.h"
37 #include "HttpControlMsg.h"
38 #include "HttpHdrCc.h"
39 #include "HttpHdrContRange.h"
40 #include "HttpHdrSc.h"
41 #include "HttpHdrScTarget.h"
42 #include "HttpHeaderTools.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
45 #include "HttpUpgradeProtocolAccess.h"
46 #include "log/access_log.h"
48 #include "MemObject.h"
49 #include "neighbors.h"
51 #include "peer_proxy_negotiate_auth.h"
53 #include "RefreshPattern.h"
55 #include "SquidConfig.h"
56 #include "StatCounters.h"
63 #include "auth/UserRequest.h"
66 #include "DelayPools.h"
69 CBDATA_CLASS_INIT(HttpStateData
);
71 static const char *const crlf
= "\r\n";
73 static void httpMaybeRemovePublic(StoreEntry
*, Http::StatusCode
);
74 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, const String strConnection
, const HttpRequest
* request
,
75 HttpHeader
* hdr_out
, const int we_do_ranges
, const Http::StateFlags
&);
77 HttpStateData::HttpStateData(FwdState
*theFwdState
) :
78 AsyncJob("HttpStateData"),
81 httpChunkDecoder(NULL
),
86 debugs(11,5, "HttpStateData " << this << " created");
87 ignoreCacheControl
= false;
88 surrogateNoStore
= false;
89 serverConnection
= fwd
->serverConnection();
91 if (fwd
->serverConnection() != NULL
)
92 _peer
= cbdataReference(fwd
->serverConnection()->getPeer()); /* might be NULL */
94 flags
.peering
= _peer
;
95 flags
.tunneling
= (_peer
&& request
->flags
.sslBumped
);
96 flags
.toOrigin
= (!_peer
|| _peer
->options
.originserver
|| request
->flags
.sslBumped
);
100 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
101 * We might end up getting the object from somewhere else if,
102 * for example, the request to this neighbor fails.
104 if (!flags
.tunneling
&& _peer
->options
.proxy_only
)
105 entry
->releaseRequest(true);
108 entry
->setNoDelay(_peer
->options
.no_delay
);
113 * register the handler to free HTTP state data when the FD closes
115 typedef CommCbMemFunT
<HttpStateData
, CommCloseCbParams
> Dialer
;
116 closeHandler
= JobCallback(9, 5, Dialer
, this, HttpStateData::httpStateConnClosed
);
117 comm_add_close_handler(serverConnection
->fd
, closeHandler
);
120 HttpStateData::~HttpStateData()
123 * don't forget that ~Client() gets called automatically
126 if (httpChunkDecoder
)
127 delete httpChunkDecoder
;
129 cbdataReferenceDone(_peer
);
131 delete upgradeHeaderOut
;
133 debugs(11,5, "HttpStateData " << this << " destroyed; " << serverConnection
);
136 const Comm::ConnectionPointer
&
137 HttpStateData::dataConnection() const
139 return serverConnection
;
143 HttpStateData::httpStateConnClosed(const CommCloseCbParams
¶ms
)
145 debugs(11, 5, "httpStateFree: FD " << params
.fd
<< ", httpState=" << params
.data
);
146 doneWithFwd
= "httpStateConnClosed()"; // assume FwdState is monitoring too
147 mustStop("HttpStateData::httpStateConnClosed");
151 HttpStateData::httpTimeout(const CommTimeoutCbParams
&)
153 debugs(11, 4, serverConnection
<< ": '" << entry
->url() << "'");
155 if (entry
->store_status
== STORE_PENDING
) {
156 fwd
->fail(new ErrorState(ERR_READ_TIMEOUT
, Http::scGatewayTimeout
, fwd
->request
, fwd
->al
));
160 mustStop("HttpStateData::httpTimeout");
164 findPreviouslyCachedEntry(StoreEntry
*newEntry
) {
165 assert(newEntry
->mem_obj
);
166 return newEntry
->mem_obj
->request
?
167 storeGetPublicByRequest(newEntry
->mem_obj
->request
.getRaw()) :
168 storeGetPublic(newEntry
->mem_obj
->storeId(), newEntry
->mem_obj
->method
);
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.
174 httpMaybeRemovePublic(StoreEntry
* e
, Http::StatusCode status
)
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
))
184 // If the new/incoming response cannot be stored, then it does not
185 // compete with the old stored response for the public key, and the
186 // old stored response should be left as is.
187 if (e
->mem_obj
->request
&& !e
->mem_obj
->request
->flags
.cachable
)
194 case Http::scNonAuthoritativeInformation
:
196 case Http::scMultipleChoices
:
198 case Http::scMovedPermanently
:
202 case Http::scSeeOther
:
206 case Http::scNotFound
:
211 case Http::scForbidden
:
213 case Http::scMethodNotAllowed
:
220 case Http::scUnauthorized
:
231 if (!remove
&& !forbidden
)
234 StoreEntry
*pe
= findPreviouslyCachedEntry(e
);
239 neighborsHtcpClear(e
, e
->mem_obj
->request
.getRaw(), e
->mem_obj
->method
, HTCP_CLR_INVALIDATION
);
245 * Also remove any cached HEAD response in case the object has
248 if (e
->mem_obj
->request
)
249 pe
= storeGetPublicByRequestMethod(e
->mem_obj
->request
.getRaw(), Http::METHOD_HEAD
);
251 pe
= storeGetPublic(e
->mem_obj
->storeId(), Http::METHOD_HEAD
);
256 neighborsHtcpClear(e
, e
->mem_obj
->request
.getRaw(), HttpRequestMethod(Http::METHOD_HEAD
), HTCP_CLR_INVALIDATION
);
263 HttpStateData::processSurrogateControl(HttpReply
*reply
)
265 if (request
->flags
.accelerated
&& reply
->surrogate_control
) {
266 HttpHdrScTarget
*sctusable
= reply
->surrogate_control
->getMergedTarget(Config
.Accel
.surrogate_id
);
269 if (sctusable
->hasNoStore() ||
270 (Config
.onoff
.surrogate_is_remote
271 && sctusable
->noStoreRemote())) {
272 surrogateNoStore
= true;
273 // Be conservative for now and make it non-shareable because
274 // there is no enough information here to make the decision.
275 entry
->makePrivate(false);
278 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
279 * accelerated request or not...
280 * Still, this is an abstraction breach. - RC
282 if (sctusable
->hasMaxAge()) {
283 if (sctusable
->maxAge() < sctusable
->maxStale())
284 reply
->expires
= reply
->date
+ sctusable
->maxAge();
286 reply
->expires
= reply
->date
+ sctusable
->maxStale();
288 /* And update the timestamps */
289 entry
->timestampsSet();
292 /* We ignore cache-control directives as per the Surrogate specification */
293 ignoreCacheControl
= true;
300 HttpStateData::ReuseDecision::Answers
301 HttpStateData::reusableReply(HttpStateData::ReuseDecision
&decision
)
303 HttpReply
const *rep
= finalReply();
304 HttpHeader
const *hdr
= &rep
->header
;
306 #if USE_HTTP_VIOLATIONS
308 const RefreshPattern
*R
= NULL
;
310 /* This strange looking define first looks up the refresh pattern
311 * and then checks if the specified flag is set. The main purpose
312 * of this is to simplify the refresh pattern lookup and USE_HTTP_VIOLATIONS
315 #define REFRESH_OVERRIDE(flag) \
316 ((R = (R ? R : refreshLimits(entry->mem_obj->storeId()))) , \
317 (R && R->flags.flag))
319 #define REFRESH_OVERRIDE(flag) 0
322 if (EBIT_TEST(entry
->flags
, RELEASE_REQUEST
))
323 return decision
.make(ReuseDecision::doNotCacheButShare
, "the entry has been released");
325 // RFC 7234 section 4: a cache MUST use the most recent response
326 // (as determined by the Date header field)
327 // TODO: whether such responses could be shareable?
329 return decision
.make(ReuseDecision::reuseNot
, "the response has an older date header");
331 // Check for Surrogate/1.0 protocol conditions
332 // NP: reverse-proxy traffic our parent server has instructed us never to cache
333 if (surrogateNoStore
)
334 return decision
.make(ReuseDecision::reuseNot
, "Surrogate-Control:no-store");
336 // RFC 2616: HTTP/1.1 Cache-Control conditions
337 if (!ignoreCacheControl
) {
338 // XXX: check to see if the request headers alone were enough to prevent caching earlier
339 // (ie no-store request header) no need to check those all again here if so.
340 // for now we are not reliably doing that so we waste CPU re-checking request CC
342 // RFC 2616 section 14.9.2 - MUST NOT cache any response with request CC:no-store
343 if (request
&& request
->cache_control
&& request
->cache_control
->hasNoStore() &&
344 !REFRESH_OVERRIDE(ignore_no_store
))
345 return decision
.make(ReuseDecision::reuseNot
,
346 "client request Cache-Control:no-store");
348 // NP: request CC:no-cache only means cache READ is forbidden. STORE is permitted.
349 if (rep
->cache_control
&& rep
->cache_control
->hasNoCacheWithParameters()) {
350 /* TODO: we are allowed to cache when no-cache= has parameters.
351 * Provided we strip away any of the listed headers unless they are revalidated
352 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
353 * That is a bit tricky for squid right now so we avoid caching entirely.
355 return decision
.make(ReuseDecision::reuseNot
,
356 "server reply Cache-Control:no-cache has parameters");
359 // NP: request CC:private is undefined. We ignore.
360 // NP: other request CC flags are limiters on HIT/MISS. We don't care about here.
362 // RFC 2616 section 14.9.2 - MUST NOT cache any response with CC:no-store
363 if (rep
->cache_control
&& rep
->cache_control
->hasNoStore() &&
364 !REFRESH_OVERRIDE(ignore_no_store
))
365 return decision
.make(ReuseDecision::reuseNot
,
366 "server reply Cache-Control:no-store");
368 // RFC 2616 section 14.9.1 - MUST NOT cache any response with CC:private in a shared cache like Squid.
369 // CC:private overrides CC:public when both are present in a response.
370 // TODO: add a shared/private cache configuration possibility.
371 if (rep
->cache_control
&&
372 rep
->cache_control
->hasPrivate() &&
373 !REFRESH_OVERRIDE(ignore_private
)) {
374 /* TODO: we are allowed to cache when private= has parameters.
375 * Provided we strip away any of the listed headers unless they are revalidated
376 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
377 * That is a bit tricky for squid right now so we avoid caching entirely.
379 return decision
.make(ReuseDecision::reuseNot
,
380 "server reply Cache-Control:private");
384 // RFC 2068, sec 14.9.4 - MUST NOT cache any response with Authentication UNLESS certain CC controls are present
385 // allow HTTP violations to IGNORE those controls (ie re-block caching Auth)
386 if (request
&& (request
->flags
.auth
|| request
->flags
.authSent
)) {
387 if (!rep
->cache_control
)
388 return decision
.make(ReuseDecision::reuseNot
,
389 "authenticated and server reply missing Cache-Control");
391 if (ignoreCacheControl
)
392 return decision
.make(ReuseDecision::reuseNot
,
393 "authenticated and ignoring Cache-Control");
395 bool mayStore
= false;
396 // HTTPbis pt6 section 3.2: a response CC:public is present
397 if (rep
->cache_control
->hasPublic()) {
398 debugs(22, 3, "Authenticated but server reply Cache-Control:public");
401 // HTTPbis pt6 section 3.2: a response CC:must-revalidate is present
402 } else if (rep
->cache_control
->hasMustRevalidate()) {
403 debugs(22, 3, "Authenticated but server reply Cache-Control:must-revalidate");
406 #if USE_HTTP_VIOLATIONS
407 // NP: given the must-revalidate exception we should also be able to exempt no-cache.
408 // HTTPbis WG verdict on this is that it is omitted from the spec due to being 'unexpected' by
409 // some. The caching+revalidate is not exactly unsafe though with Squids interpretation of no-cache
410 // (without parameters) as equivalent to must-revalidate in the reply.
411 } else if (rep
->cache_control
->hasNoCacheWithoutParameters()) {
412 debugs(22, 3, "Authenticated but server reply Cache-Control:no-cache (equivalent to must-revalidate)");
416 // HTTPbis pt6 section 3.2: a response CC:s-maxage is present
417 } else if (rep
->cache_control
->hasSMaxAge()) {
418 debugs(22, 3, "Authenticated but server reply Cache-Control:s-maxage");
423 return decision
.make(ReuseDecision::reuseNot
, "authenticated transaction");
425 // NP: response CC:no-cache is equivalent to CC:must-revalidate,max-age=0. We MAY cache, and do so.
426 // NP: other request CC flags are limiters on HIT/MISS/REFRESH. We don't care about here.
429 /* HACK: The "multipart/x-mixed-replace" content type is used for
430 * continuous push replies. These are generally dynamic and
431 * probably should not be cachable
433 if ((v
= hdr
->getStr(Http::HdrType::CONTENT_TYPE
)))
434 if (!strncasecmp(v
, "multipart/x-mixed-replace", 25))
435 return decision
.make(ReuseDecision::reuseNot
, "Content-Type:multipart/x-mixed-replace");
437 // TODO: if possible, provide more specific message for each status code
438 static const char *shareableError
= "shareable error status code";
439 static const char *nonShareableError
= "non-shareable error status code";
440 ReuseDecision::Answers statusAnswer
= ReuseDecision::reuseNot
;
441 const char *statusReason
= nonShareableError
;
443 switch (rep
->sline
.status()) {
445 /* There are several situations when a non-cacheable response may be
446 * still shareable (e.g., among collapsed clients). We assume that these
447 * are 3xx and 5xx responses, indicating server problems and some of
448 * 4xx responses, common for all clients with a given cache key (e.g.,
449 * 404 Not Found or 414 URI Too Long). On the other hand, we should not
450 * share non-cacheable client-specific errors, such as 400 Bad Request
451 * or 406 Not Acceptable.
454 /* Responses that are cacheable */
458 case Http::scNonAuthoritativeInformation
:
460 case Http::scMultipleChoices
:
462 case Http::scMovedPermanently
:
463 case Http::scPermanentRedirect
:
467 * Don't cache objects that need to be refreshed on next request,
468 * unless we know how to refresh it.
471 if (refreshIsCachable(entry
) || REFRESH_OVERRIDE(store_stale
))
472 decision
.make(ReuseDecision::cachePositively
, "refresh check returned cacheable");
474 decision
.make(ReuseDecision::doNotCacheButShare
, "refresh check returned non-cacheable");
477 /* Responses that only are cacheable if the server says so */
480 case Http::scTemporaryRedirect
:
482 decision
.make(ReuseDecision::doNotCacheButShare
, "Date is missing/invalid");
483 else if (rep
->expires
> rep
->date
)
484 decision
.make(ReuseDecision::cachePositively
, "Expires > Date");
486 decision
.make(ReuseDecision::doNotCacheButShare
, "Expires <= Date");
489 /* These responses can be negatively cached. Most can also be shared. */
490 case Http::scNoContent
:
491 case Http::scUseProxy
:
492 case Http::scForbidden
:
493 case Http::scNotFound
:
494 case Http::scMethodNotAllowed
:
495 case Http::scUriTooLong
:
496 case Http::scInternalServerError
:
497 case Http::scNotImplemented
:
498 case Http::scBadGateway
:
499 case Http::scServiceUnavailable
:
500 case Http::scGatewayTimeout
:
501 case Http::scMisdirectedRequest
:
502 statusAnswer
= ReuseDecision::doNotCacheButShare
;
503 statusReason
= shareableError
;
504 /* [[fallthrough]] to the actual decision making below */
506 case Http::scBadRequest
: // no sharing; perhaps the server did not like something specific to this request
507 #if USE_HTTP_VIOLATIONS
508 if (Config
.negativeTtl
> 0)
509 decision
.make(ReuseDecision::cacheNegatively
, "Config.negativeTtl > 0");
512 decision
.make(statusAnswer
, statusReason
);
515 /* these responses can never be cached, some
516 of them can be shared though */
517 case Http::scSeeOther
:
518 case Http::scNotModified
:
519 case Http::scUnauthorized
:
520 case Http::scProxyAuthenticationRequired
:
521 case Http::scPaymentRequired
:
522 case Http::scInsufficientStorage
:
523 // TODO: use more specific reason for non-error status codes
524 decision
.make(ReuseDecision::doNotCacheButShare
, shareableError
);
527 case Http::scPartialContent
: /* Not yet supported. TODO: make shareable for suitable ranges */
528 case Http::scNotAcceptable
:
529 case Http::scRequestTimeout
: // TODO: is this shareable?
530 case Http::scConflict
: // TODO: is this shareable?
531 case Http::scLengthRequired
:
532 case Http::scPreconditionFailed
:
533 case Http::scPayloadTooLarge
:
534 case Http::scUnsupportedMediaType
:
535 case Http::scUnprocessableEntity
:
536 case Http::scLocked
: // TODO: is this shareable?
537 case Http::scFailedDependency
:
538 case Http::scRequestedRangeNotSatisfied
:
539 case Http::scExpectationFailed
:
540 case Http::scInvalidHeader
: /* Squid header parsing error */
541 case Http::scHeaderTooLarge
:
542 decision
.make(ReuseDecision::reuseNot
, nonShareableError
);
546 /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */
547 decision
.make(ReuseDecision::reuseNot
, "unknown status code");
551 return decision
.answer
;
554 /// assemble a variant key (vary-mark) from the given Vary header and HTTP request
556 assembleVaryKey(String
&vary
, SBuf
&vstr
, const HttpRequest
&request
)
558 static const SBuf
asterisk("*");
559 const char *pos
= nullptr;
560 const char *item
= nullptr;
563 while (strListGetItem(&vary
, ',', &item
, &ilen
, &pos
)) {
564 SBuf
name(item
, ilen
);
565 if (name
== asterisk
) {
571 vstr
.append(", ", 2);
573 String
hdr(request
.header
.getByName(name
));
574 const char *value
= hdr
.termedBuf();
576 value
= rfc1738_escape_part(value
);
577 vstr
.append("=\"", 2);
579 vstr
.append("\"", 1);
587 * For Vary, store the relevant request headers as
588 * virtual headers in the reply
589 * Returns an empty SBuf if the variance cannot be stored
592 httpMakeVaryMark(HttpRequest
* request
, HttpReply
const * reply
)
597 vary
= reply
->header
.getList(Http::HdrType::VARY
);
598 assembleVaryKey(vary
, vstr
, *request
);
600 #if X_ACCELERATOR_VARY
602 vary
= reply
->header
.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY
);
603 assembleVaryKey(vary
, vstr
, *request
);
611 HttpStateData::keepaliveAccounting(HttpReply
*reply
)
614 if (flags
.peering
&& !flags
.tunneling
)
615 ++ _peer
->stats
.n_keepalives_sent
;
617 if (reply
->keep_alive
) {
618 if (flags
.peering
&& !flags
.tunneling
)
619 ++ _peer
->stats
.n_keepalives_recv
;
621 if (Config
.onoff
.detect_broken_server_pconns
622 && reply
->bodySize(request
->method
) == -1 && !flags
.chunked
) {
623 debugs(11, DBG_IMPORTANT
, "keepaliveAccounting: Impossible keep-alive header from '" << entry
->url() << "'" );
624 // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
625 flags
.keepalive_broken
= true;
631 HttpStateData::checkDateSkew(HttpReply
*reply
)
633 if (reply
->date
> -1 && flags
.toOrigin
) {
634 int skew
= abs((int)(reply
->date
- squid_curtime
));
637 debugs(11, 3, "" << request
->url
.host() << "'s clock is skewed by " << skew
<< " seconds!");
642 * This creates the error page itself.. its likely
643 * that the forward ported reply header max size patch
644 * generates non http conformant error pages - in which
645 * case the errors where should be 'BAD_GATEWAY' etc
648 HttpStateData::processReplyHeader()
650 /** Creates a blank header. If this routine is made incremental, this will not do */
652 debugs(11, 3, "processReplyHeader: key '" << entry
->getMD5Text() << "'");
654 assert(!flags
.headers_parsed
);
659 /* Attempt to parse the first line; this will define where the protocol, status, reason-phrase and header begin */
662 hp
= new Http1::ResponseParser
;
664 bool parsedOk
= hp
->parse(inBuf
);
665 // remember the actual received status-code before returning on errors,
666 // overwriting any previously stored value from earlier forwarding attempts
667 request
->hier
.peer_reply_status
= hp
->messageStatus(); // may still be scNone
669 // sync the buffers after parsing.
670 inBuf
= hp
->remaining();
672 if (hp
->needsMoreData()) {
673 if (eof
) { // no more data coming
675 // fall through to handle this premature EOF as an error
677 debugs(33, 5, "Incomplete response, waiting for end of response headers");
683 // unrecoverable parsing error
684 // TODO: Use Raw! XXX: inBuf no longer has the [beginning of the] malformed header.
685 debugs(11, 3, "Non-HTTP-compliant header:\n---------\n" << inBuf
<< "\n----------");
686 flags
.headers_parsed
= true;
687 HttpReply
*newrep
= new HttpReply
;
688 // hp->needsMoreData() means hp->parseStatusCode is unusable, but, here,
689 // it also means that the reply header got truncated by a premature EOF
690 assert(!hp
->needsMoreData() || eof
);
691 const auto scode
= hp
->needsMoreData() ? Http::scInvalidHeader
: hp
->parseStatusCode
;
692 newrep
->sline
.set(Http::ProtocolVersion(), scode
);
693 setVirginReply(newrep
);
698 /* We know the whole response is in parser now */
699 debugs(11, 2, "HTTP Server " << serverConnection
);
700 debugs(11, 2, "HTTP Server RESPONSE:\n---------\n" <<
701 hp
->messageProtocol() << " " << hp
->messageStatus() << " " << hp
->reasonPhrase() << "\n" <<
705 // reset payload tracking to begin after message headers
706 payloadSeen
= inBuf
.length();
708 HttpReply
*newrep
= new HttpReply
;
709 // XXX: RFC 7230 indicates we MAY ignore the reason phrase,
710 // and use an empty string on unknown status.
711 // We do that now to avoid performance regression from using SBuf::c_str()
712 newrep
->sline
.set(hp
->messageProtocol(), hp
->messageStatus() /* , hp->reasonPhrase() */);
715 if (!newrep
->parseHeader(*hp
)) {
716 newrep
->sline
.set(hp
->messageProtocol(), Http::scInvalidHeader
);
717 debugs(11, 2, "error parsing response headers mime block");
720 // done with Parser, now process using the HttpReply
723 newrep
->sources
|= request
->url
.getScheme() == AnyP::PROTO_HTTPS
? Http::Message::srcHttps
: Http::Message::srcHttp
;
725 newrep
->removeStaleWarnings();
727 if (newrep
->sline
.version
.protocol
== AnyP::PROTO_HTTP
&& Http::Is1xx(newrep
->sline
.status())) {
732 flags
.chunked
= false;
733 if (newrep
->sline
.version
.protocol
== AnyP::PROTO_HTTP
&& newrep
->header
.chunked()) {
734 flags
.chunked
= true;
735 httpChunkDecoder
= new Http1::TeChunkedParser
;
738 if (!peerSupportsConnectionPinning())
739 request
->flags
.connectionAuthDisabled
= true;
741 HttpReply
*vrep
= setVirginReply(newrep
);
742 flags
.headers_parsed
= true;
744 keepaliveAccounting(vrep
);
748 processSurrogateControl (vrep
);
751 /// ignore or start forwarding the 1xx response (a.k.a., control message)
753 HttpStateData::handle1xx(HttpReply
*reply
)
756 fwd
->al
->reply
= reply
;
758 HttpReply::Pointer
msg(reply
); // will destroy reply if unused
760 // one 1xx at a time: we must not be called while waiting for previous 1xx
761 Must(!flags
.handling1xx
);
762 flags
.handling1xx
= true;
764 const auto statusCode
= reply
->sline
.status();
766 // drop1xx() needs to handle HTTP 101 (Switching Protocols) responses
767 // specially because they indicate that the server has stopped speaking HTTP
768 Must(!flags
.serverSwitchedProtocols
);
769 flags
.serverSwitchedProtocols
= (statusCode
== Http::scSwitchingProtocols
);
771 if (statusCode
== Http::scContinue
&& request
->forcedBodyContinuation
)
772 return drop1xx("we have sent it already");
774 if (!request
->canHandle1xx())
775 return drop1xx("the client does not support it");
777 #if USE_HTTP_VIOLATIONS
778 // check whether the 1xx response forwarding is allowed by squid.conf
779 if (Config
.accessList
.reply
) {
780 ACLFilledChecklist
ch(Config
.accessList
.reply
, originalRequest().getRaw());
783 ch
.syncAle(originalRequest().getRaw(), nullptr);
784 HTTPMSGLOCK(ch
.reply
);
785 if (!ch
.fastCheck().allowed()) // TODO: support slow lookups?
786 return drop1xx("http_reply_access blocked it");
788 #endif // USE_HTTP_VIOLATIONS
790 if (flags
.serverSwitchedProtocols
) {
791 if (const auto reason
= blockSwitchingProtocols(*reply
))
792 return drop1xx(reason
);
795 debugs(11, 2, "forwarding 1xx to client");
797 // the Sink will use this to call us back after writing 1xx to the client
798 typedef NullaryMemFunT
<HttpStateData
> CbDialer
;
799 const AsyncCall::Pointer cb
= JobCallback(11, 3, CbDialer
, this,
800 HttpStateData::proceedAfter1xx
);
801 CallJobHere1(11, 4, request
->clientConnectionManager
, ConnStateData
,
802 ConnStateData::sendControlMsg
, HttpControlMsg(msg
, cb
));
803 // If the call is not fired, then the Sink is gone, and HttpStateData
804 // will terminate due to an aborted store entry or another similar error.
805 // If we get stuck, it is not handle1xx fault if we could get stuck
806 // for similar reasons without a 1xx response.
809 /// if possible, safely ignores the received 1xx control message
810 /// otherwise, terminates the server connection
812 HttpStateData::drop1xx(const char *reason
)
814 if (flags
.serverSwitchedProtocols
) {
815 debugs(11, 2, "bad 101 because " << reason
);
816 const auto err
= new ErrorState(ERR_INVALID_RESP
, Http::scBadGateway
, request
.getRaw(), fwd
->al
);
819 mustStop("prohibited HTTP/101 response");
823 debugs(11, 2, "ignoring 1xx because " << reason
);
827 /// \retval nil if the HTTP/101 (Switching Protocols) reply should be forwarded
828 /// \retval reason why an attempt to switch protocols should be stopped
830 HttpStateData::blockSwitchingProtocols(const HttpReply
&reply
) const
832 if (!upgradeHeaderOut
)
833 return "Squid offered no Upgrade at all, but server switched to a tunnel";
835 // See RFC 7230 section 6.7 for the corresponding MUSTs
837 if (!reply
.header
.has(Http::HdrType::UPGRADE
))
838 return "server did not send an Upgrade header field";
840 if (!reply
.header
.hasListMember(Http::HdrType::CONNECTION
, "upgrade", ','))
841 return "server did not send 'Connection: upgrade'";
843 const auto acceptedProtos
= reply
.header
.getList(Http::HdrType::UPGRADE
);
844 const char *pos
= nullptr;
845 const char *accepted
= nullptr;
847 while (strListGetItem(&acceptedProtos
, ',', &accepted
, &acceptedLen
, &pos
)) {
848 debugs(11, 5, "server accepted at least" << Raw(nullptr, accepted
, acceptedLen
));
849 return nullptr; // OK: let the client validate server's selection
852 return "server sent an essentially empty Upgrade header field";
855 /// restores state and resumes processing after 1xx is ignored or forwarded
857 HttpStateData::proceedAfter1xx()
859 Must(flags
.handling1xx
);
861 if (flags
.serverSwitchedProtocols
) {
862 // pass server connection ownership to request->clientConnectionManager
863 ConnStateData::ServerConnectionContext
scc(serverConnection
, inBuf
);
864 typedef UnaryMemFunT
<ConnStateData
, ConnStateData::ServerConnectionContext
> MyDialer
;
865 AsyncCall::Pointer call
= asyncCall(11, 3, "ConnStateData::noteTakeServerConnectionControl",
866 MyDialer(request
->clientConnectionManager
,
867 &ConnStateData::noteTakeServerConnectionControl
, scc
));
868 ScheduleCallHere(call
);
869 fwd
->unregister(serverConnection
);
870 comm_remove_close_handler(serverConnection
->fd
, closeHandler
);
871 closeHandler
= nullptr;
872 serverConnection
= nullptr;
873 doneWithFwd
= "switched protocols";
874 mustStop(doneWithFwd
);
878 debugs(11, 2, "continuing with " << payloadSeen
<< " bytes in buffer after 1xx");
879 CallJobHere(11, 3, this, HttpStateData
, HttpStateData::processReply
);
883 * returns true if the peer can support connection pinning
886 HttpStateData::peerSupportsConnectionPinning() const
891 // we are talking "through" rather than "to" our _peer
895 /*If this peer does not support connection pinning (authenticated
896 connections) return false
898 if (!_peer
->connection_auth
)
901 const auto &rep
= entry
->mem().freshestReply();
903 /*The peer supports connection pinning and the http reply status
904 is not unauthorized, so the related connection can be pinned
906 if (rep
.sline
.status() != Http::scUnauthorized
)
909 /*The server respond with Http::scUnauthorized and the peer configured
910 with "connection-auth=on" we know that the peer supports pinned
913 if (_peer
->connection_auth
== 1)
916 /*At this point peer has configured with "connection-auth=auto"
917 parameter so we need some extra checks to decide if we are going
918 to allow pinned connections or not
921 /*if the peer configured with originserver just allow connection
922 pinning (squid 2.6 behaviour)
924 if (_peer
->options
.originserver
)
927 /*if the connections it is already pinned it is OK*/
928 if (request
->flags
.pinned
)
931 /*Allow pinned connections only if the Proxy-support header exists in
932 reply and has in its list the "Session-Based-Authentication"
933 which means that the peer supports connection pinning.
935 if (rep
.header
.hasListMember(Http::HdrType::PROXY_SUPPORT
, "Session-Based-Authentication", ','))
941 // Called when we parsed (and possibly adapted) the headers but
942 // had not starting storing (a.k.a., sending) the body yet.
944 HttpStateData::haveParsedReplyHeaders()
946 Client::haveParsedReplyHeaders();
948 HttpReply
*rep
= finalReply();
949 const Http::StatusCode statusCode
= rep
->sline
.status();
951 entry
->timestampsSet();
953 /* Check if object is cacheable or not based on reply code */
954 debugs(11, 3, "HTTP CODE: " << statusCode
);
956 if (StoreEntry
*oldEntry
= findPreviouslyCachedEntry(entry
)) {
957 oldEntry
->lock("HttpStateData::haveParsedReplyHeaders");
958 sawDateGoBack
= rep
->olderThan(oldEntry
->hasFreshestReply());
959 oldEntry
->unlock("HttpStateData::haveParsedReplyHeaders");
962 if (neighbors_do_private_keys
&& !sawDateGoBack
)
963 httpMaybeRemovePublic(entry
, rep
->sline
.status());
965 bool varyFailure
= false;
966 if (rep
->header
.has(Http::HdrType::VARY
)
967 #if X_ACCELERATOR_VARY
968 || rep
->header
.has(Http::HdrType::HDR_X_ACCELERATOR_VARY
)
971 const SBuf
vary(httpMakeVaryMark(request
.getRaw(), rep
));
973 if (vary
.isEmpty()) {
974 // TODO: check whether such responses are shareable.
975 // Do not share for now.
976 entry
->makePrivate(false);
977 if (fwd
->reforwardableStatus(rep
->sline
.status()))
978 EBIT_SET(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
981 entry
->mem_obj
->vary_headers
= vary
;
983 // RFC 7231 section 7.1.4
984 // Vary:* can be cached, but has mandatory revalidation
985 static const SBuf
asterisk("*");
986 if (vary
== asterisk
)
987 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_ALWAYS
);
993 * If its not a reply that we will re-forward, then
994 * allow the client to get it.
996 if (fwd
->reforwardableStatus(rep
->sline
.status()))
997 EBIT_SET(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
999 ReuseDecision
decision(entry
, statusCode
);
1001 switch (reusableReply(decision
)) {
1003 case ReuseDecision::reuseNot
:
1004 entry
->makePrivate(false);
1007 case ReuseDecision::cachePositively
:
1008 if (!entry
->makePublic()) {
1009 decision
.make(ReuseDecision::doNotCacheButShare
, "public key creation error");
1010 entry
->makePrivate(true);
1014 case ReuseDecision::cacheNegatively
:
1015 if (!entry
->cacheNegatively()) {
1016 decision
.make(ReuseDecision::doNotCacheButShare
, "public key creation error");
1017 entry
->makePrivate(true);
1021 case ReuseDecision::doNotCacheButShare
:
1022 entry
->makePrivate(true);
1029 debugs(11, 3, "decided: " << decision
);
1032 if (!ignoreCacheControl
) {
1033 if (rep
->cache_control
) {
1034 // We are required to revalidate on many conditions.
1035 // For security reasons we do so even if storage was caused by refresh_pattern ignore-* option
1037 // CC:must-revalidate or CC:proxy-revalidate
1038 const bool ccMustRevalidate
= (rep
->cache_control
->hasProxyRevalidate() || rep
->cache_control
->hasMustRevalidate());
1040 // CC:no-cache (only if there are no parameters)
1041 const bool ccNoCacheNoParams
= rep
->cache_control
->hasNoCacheWithoutParameters();
1044 const bool ccSMaxAge
= rep
->cache_control
->hasSMaxAge();
1046 // CC:private (yes, these can sometimes be stored)
1047 const bool ccPrivate
= rep
->cache_control
->hasPrivate();
1049 if (ccNoCacheNoParams
|| ccPrivate
)
1050 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_ALWAYS
);
1051 else if (ccMustRevalidate
|| ccSMaxAge
)
1052 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_STALE
);
1054 #if USE_HTTP_VIOLATIONS // response header Pragma::no-cache is undefined in HTTP
1056 // Expensive calculation. So only do it IF the CC: header is not present.
1058 /* HACK: Pragma: no-cache in _replies_ is not documented in HTTP,
1059 * but servers like "Active Imaging Webcast/2.0" sure do use it */
1060 if (rep
->header
.has(Http::HdrType::PRAGMA
) &&
1061 rep
->header
.hasListMember(Http::HdrType::PRAGMA
,"no-cache",','))
1062 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_ALWAYS
);
1068 headersLog(1, 0, request
->method
, rep
);
1073 HttpStateData::ConnectionStatus
1074 HttpStateData::statusIfComplete() const
1076 const HttpReply
*rep
= virginReply();
1078 * If the reply wants to close the connection, it takes precedence */
1080 static SBuf
close("close", 5);
1081 if (httpHeaderHasConnDir(&rep
->header
, close
))
1082 return COMPLETE_NONPERSISTENT_MSG
;
1085 * If we sent a Connection:close request header, then this
1086 * can not be a persistent connection.
1088 if (!flags
.keepalive
)
1089 return COMPLETE_NONPERSISTENT_MSG
;
1092 * If we banned reuse, then this cannot be a persistent connection.
1094 if (flags
.forceClose
)
1095 return COMPLETE_NONPERSISTENT_MSG
;
1098 * If we haven't sent the whole request then this can not be a persistent
1101 if (!flags
.request_sent
) {
1102 debugs(11, 2, "Request not yet fully sent " << request
->method
<< ' ' << entry
->url());
1103 return COMPLETE_NONPERSISTENT_MSG
;
1107 * What does the reply have to say about keep-alive?
1111 * If the origin server (HTTP/1.0) does not send a keep-alive
1112 * header, but keeps the connection open anyway, what happens?
1113 * We'll return here and http.c waits for an EOF before changing
1114 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
1115 * and an error status code, and we might have to wait until
1116 * the server times out the socket.
1118 if (!rep
->keep_alive
)
1119 return COMPLETE_NONPERSISTENT_MSG
;
1121 return COMPLETE_PERSISTENT_MSG
;
1124 HttpStateData::ConnectionStatus
1125 HttpStateData::persistentConnStatus() const
1127 debugs(11, 3, serverConnection
<< " eof=" << eof
);
1128 if (eof
) // already reached EOF
1129 return COMPLETE_NONPERSISTENT_MSG
;
1131 /* If server fd is closing (but we have not been notified yet), stop Comm
1132 I/O to avoid assertions. TODO: Change Comm API to handle callers that
1133 want more I/O after async closing (usually initiated by others). */
1134 // XXX: add canReceive or s/canSend/canTalkToServer/
1135 if (!Comm::IsConnOpen(serverConnection
))
1136 return COMPLETE_NONPERSISTENT_MSG
;
1139 * In chunked response we do not know the content length but we are absolutely
1140 * sure about the end of response, so we are calling the statusIfComplete to
1141 * decide if we can be persistent
1143 if (lastChunk
&& flags
.chunked
)
1144 return statusIfComplete();
1146 const HttpReply
*vrep
= virginReply();
1147 debugs(11, 5, "persistentConnStatus: content_length=" << vrep
->content_length
);
1149 const int64_t clen
= vrep
->bodySize(request
->method
);
1151 debugs(11, 5, "persistentConnStatus: clen=" << clen
);
1153 /* If the body size is unknown we must wait for EOF */
1155 return INCOMPLETE_MSG
;
1158 * If the body size is known, we must wait until we've gotten all of it. */
1160 debugs(11,5, "payloadSeen=" << payloadSeen
<< " content_length=" << vrep
->content_length
);
1162 if (payloadSeen
< vrep
->content_length
)
1163 return INCOMPLETE_MSG
;
1165 if (payloadTruncated
> 0) // already read more than needed
1166 return COMPLETE_NONPERSISTENT_MSG
; // disable pconns
1170 * If there is no message body or we got it all, we can be persistent */
1171 return statusIfComplete();
1175 readDelayed(void *context
, CommRead
const &)
1177 HttpStateData
*state
= static_cast<HttpStateData
*>(context
);
1178 state
->flags
.do_next_read
= true;
1179 state
->maybeReadVirginBody();
1183 HttpStateData::readReply(const CommIoCbParams
&io
)
1185 Must(!flags
.do_next_read
); // XXX: should have been set false by mayReadVirginBody()
1186 flags
.do_next_read
= false;
1188 debugs(11, 5, io
.conn
);
1190 // Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us
1191 if (io
.flag
== Comm::ERR_CLOSING
) {
1192 debugs(11, 3, "http socket closing");
1196 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1197 abortTransaction("store entry aborted while reading reply");
1201 Must(Comm::IsConnOpen(serverConnection
));
1202 Must(io
.conn
->fd
== serverConnection
->fd
);
1205 * Don't reset the timeout value here. The value should be
1206 * counting Config.Timeout.request and applies to the request
1207 * as a whole, not individual read() calls.
1208 * Plus, it breaks our lame *HalfClosed() detection
1211 Must(maybeMakeSpaceAvailable(true));
1212 CommIoCbParams
rd(this); // will be expanded with ReadNow results
1214 rd
.size
= entry
->bytesWanted(Range
<size_t>(0, inBuf
.spaceSize()));
1217 assert(entry
->mem_obj
);
1218 AsyncCall::Pointer nilCall
;
1219 entry
->mem_obj
->delayRead(DeferredRead(readDelayed
, this, CommRead(io
.conn
, NULL
, 0, nilCall
)));
1223 switch (Comm::ReadNow(rd
, inBuf
)) {
1224 case Comm::INPROGRESS
:
1225 if (inBuf
.isEmpty())
1226 debugs(33, 2, io
.conn
<< ": no data to process, " << xstrerr(rd
.xerrno
));
1227 flags
.do_next_read
= true;
1228 maybeReadVirginBody();
1233 payloadSeen
+= rd
.size
;
1235 DelayId delayId
= entry
->mem_obj
->mostBytesAllowed();
1236 delayId
.bytesIn(rd
.size
);
1239 statCounter
.server
.all
.kbytes_in
+= rd
.size
;
1240 statCounter
.server
.http
.kbytes_in
+= rd
.size
;
1241 ++ IOStats
.Http
.reads
;
1244 for (int clen
= rd
.size
- 1; clen
; ++bin
)
1247 ++ IOStats
.Http
.read_hist
[bin
];
1249 request
->hier
.notePeerRead();
1252 /* Continue to process previously read data */
1255 case Comm::ENDFILE
: // close detected by 0-byte read
1257 flags
.do_next_read
= false;
1259 /* Continue to process previously read data */
1262 // case Comm::COMM_ERROR:
1263 default: // no other flags should ever occur
1264 debugs(11, 2, io
.conn
<< ": read failure: " << xstrerr(rd
.xerrno
));
1265 const auto err
= new ErrorState(ERR_READ_ERROR
, Http::scBadGateway
, fwd
->request
, fwd
->al
);
1266 err
->xerrno
= rd
.xerrno
;
1268 flags
.do_next_read
= false;
1270 mustStop("HttpStateData::readReply");
1274 /* Process next response from buffer */
1278 /// processes the already read and buffered response data, possibly after
1279 /// waiting for asynchronous 1xx control message processing
1281 HttpStateData::processReply()
1284 if (flags
.handling1xx
) { // we came back after handling a 1xx response
1285 debugs(11, 5, "done with 1xx handling");
1286 flags
.handling1xx
= false;
1287 Must(!flags
.headers_parsed
);
1290 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1291 abortTransaction("store entry aborted while we were waiting for processReply()");
1295 if (!flags
.headers_parsed
) { // have not parsed headers yet?
1296 processReplyHeader();
1298 if (!continueAfterParsingHeader()) // parsing error or need more data
1299 return; // TODO: send errors to ICAP
1301 adaptOrFinalizeReply(); // may write to, abort, or "close" the entry
1304 // kick more reads if needed and/or process the response body, if any
1305 processReplyBody(); // may call serverComplete()
1309 \retval true if we can continue with processing the body or doing ICAP.
1312 HttpStateData::continueAfterParsingHeader()
1314 if (flags
.handling1xx
) {
1315 debugs(11, 5, "wait for 1xx handling");
1316 Must(!flags
.headers_parsed
);
1320 if (!flags
.headers_parsed
&& !eof
) {
1321 debugs(11, 9, "needs more at " << inBuf
.length());
1322 flags
.do_next_read
= true;
1323 /** \retval false If we have not finished parsing the headers and may get more data.
1324 * Schedules more reads to retrieve the missing data.
1326 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1330 /** If we are done with parsing, check for errors */
1332 err_type error
= ERR_NONE
;
1334 if (flags
.headers_parsed
) { // parsed headers, possibly with errors
1335 // check for header parsing errors
1336 if (HttpReply
*vrep
= virginReply()) {
1337 const Http::StatusCode s
= vrep
->sline
.status();
1338 const AnyP::ProtocolVersion
&v
= vrep
->sline
.version
;
1339 if (s
== Http::scInvalidHeader
&& v
!= Http::ProtocolVersion(0,9)) {
1340 debugs(11, DBG_IMPORTANT
, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry
->url() << " AKA " << request
->url
);
1341 error
= ERR_INVALID_RESP
;
1342 } else if (s
== Http::scHeaderTooLarge
) {
1343 fwd
->dontRetry(true);
1344 error
= ERR_TOO_BIG
;
1345 } else if (vrep
->header
.conflictingContentLength()) {
1346 fwd
->dontRetry(true);
1347 error
= ERR_INVALID_RESP
;
1348 } else if (vrep
->header
.unsupportedTe()) {
1349 fwd
->dontRetry(true);
1350 error
= ERR_INVALID_RESP
;
1352 return true; // done parsing, got reply, and no error
1355 // parsed headers but got no reply
1356 debugs(11, DBG_IMPORTANT
, "WARNING: HTTP: Invalid Response: No reply at all for " << entry
->url() << " AKA " << request
->url
);
1357 error
= ERR_INVALID_RESP
;
1361 if (inBuf
.length()) {
1362 error
= ERR_INVALID_RESP
;
1363 debugs(11, DBG_IMPORTANT
, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry
->url() << " AKA " << request
->url
);
1365 error
= ERR_ZERO_SIZE_OBJECT
;
1366 debugs(11, (request
->flags
.accelerated
?DBG_IMPORTANT
:2), "WARNING: HTTP: Invalid Response: No object data received for " << entry
->url() << " AKA " << request
->url
);
1370 assert(error
!= ERR_NONE
);
1372 fwd
->fail(new ErrorState(error
, Http::scBadGateway
, fwd
->request
, fwd
->al
));
1373 flags
.do_next_read
= false;
1375 mustStop("HttpStateData::continueAfterParsingHeader");
1376 return false; // quit on error
1379 /** truncate what we read if we read too much so that writeReplyBody()
1380 writes no more than what we should have read */
1382 HttpStateData::truncateVirginBody()
1384 assert(flags
.headers_parsed
);
1386 HttpReply
*vrep
= virginReply();
1388 if (!vrep
->expectingBody(request
->method
, clen
) || clen
< 0)
1389 return; // no body or a body of unknown size, including chunked
1391 if (payloadSeen
- payloadTruncated
<= clen
)
1392 return; // we did not read too much or already took care of the extras
1394 if (const int64_t extras
= payloadSeen
- payloadTruncated
- clen
) {
1395 // server sent more that the advertised content length
1396 debugs(11, 5, "payloadSeen=" << payloadSeen
<<
1397 " clen=" << clen
<< '/' << vrep
->content_length
<<
1398 " truncated=" << payloadTruncated
<< '+' << extras
);
1400 inBuf
.chop(0, inBuf
.length() - extras
);
1401 payloadTruncated
+= extras
;
1406 * Call this when there is data from the origin server
1407 * which should be sent to either StoreEntry, or to ICAP...
1410 HttpStateData::writeReplyBody()
1412 truncateVirginBody(); // if needed
1413 const char *data
= inBuf
.rawContent();
1414 int len
= inBuf
.length();
1415 addVirginReplyBody(data
, len
);
1418 // after addVirginReplyBody() wrote (when not adapting) everything we have
1419 // received to Store, check whether we have received/parsed the entire reply
1421 const char *parsedWhole
= nullptr;
1422 if (!virginReply()->expectingBody(request
->method
, clen
))
1423 parsedWhole
= "http parsed header-only reply";
1424 else if (clen
>= 0 && clen
== payloadSeen
- payloadTruncated
)
1425 parsedWhole
= "http parsed Content-Length body bytes";
1426 else if (clen
< 0 && eof
)
1427 parsedWhole
= "http parsed body ending with expected/required EOF";
1429 markParsedVirginReplyAsWhole(parsedWhole
);
1433 HttpStateData::decodeAndWriteReplyBody()
1435 assert(flags
.chunked
);
1436 assert(httpChunkDecoder
);
1440 httpChunkDecoder
->setPayloadBuffer(&decodedData
);
1441 const bool doneParsing
= httpChunkDecoder
->parse(inBuf
);
1442 inBuf
= httpChunkDecoder
->remaining(); // sync buffers after parse
1443 addVirginReplyBody(decodedData
.content(), decodedData
.contentSize());
1446 flags
.do_next_read
= false;
1447 markParsedVirginReplyAsWhole("http parsed last-chunk");
1452 debugs (11, 2, "de-chunking failure: " << CurrentException
);
1458 * processReplyBody has two purposes:
1459 * 1 - take the reply body data, if any, and put it into either
1460 * the StoreEntry, or give it over to ICAP.
1461 * 2 - see if we made it to the end of the response (persistent
1462 * connections and such)
1465 HttpStateData::processReplyBody()
1467 if (!flags
.headers_parsed
) {
1468 flags
.do_next_read
= true;
1469 maybeReadVirginBody();
1474 debugs(11,5, "adaptationAccessCheckPending=" << adaptationAccessCheckPending
);
1475 if (adaptationAccessCheckPending
)
1481 * At this point the reply headers have been parsed and consumed.
1482 * That means header content has been removed from readBuf and
1483 * it contains only body data.
1485 if (entry
->isAccepting()) {
1486 if (flags
.chunked
) {
1487 if (!decodeAndWriteReplyBody()) {
1488 flags
.do_next_read
= false;
1496 // storing/sending methods like earlier adaptOrFinalizeReply() or
1497 // above writeReplyBody() may release/abort the store entry.
1498 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1499 // TODO: In some cases (e.g., 304), we should keep persistent conn open.
1500 // Detect end-of-reply (and, hence, pool our idle pconn) earlier (ASAP).
1501 abortTransaction("store entry aborted while storing reply");
1504 switch (persistentConnStatus()) {
1505 case INCOMPLETE_MSG
: {
1506 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG from " << serverConnection
);
1507 /* Wait for more data or EOF condition */
1508 AsyncCall::Pointer nil
;
1509 if (flags
.keepalive_broken
) {
1510 commSetConnTimeout(serverConnection
, 10, nil
);
1512 commSetConnTimeout(serverConnection
, Config
.Timeout
.read
, nil
);
1515 flags
.do_next_read
= true;
1519 case COMPLETE_PERSISTENT_MSG
: {
1520 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG from " << serverConnection
);
1522 // TODO: Remove serverConnectionSaved but preserve exception safety.
1524 commUnsetConnTimeout(serverConnection
);
1525 flags
.do_next_read
= false;
1527 comm_remove_close_handler(serverConnection
->fd
, closeHandler
);
1528 closeHandler
= NULL
;
1530 Ip::Address client_addr
; // XXX: Remove as unused. Why was it added?
1531 if (request
->flags
.spoofClientIp
)
1532 client_addr
= request
->client_addr
;
1534 auto serverConnectionSaved
= serverConnection
;
1535 fwd
->unregister(serverConnection
);
1536 serverConnection
= nullptr;
1538 bool ispinned
= false; // TODO: Rename to isOrShouldBePinned
1539 if (request
->flags
.pinned
) {
1541 } else if (request
->flags
.connectionAuth
&& request
->flags
.authSent
) {
1546 if (request
->clientConnectionManager
.valid()) {
1547 CallJobHere1(11, 4, request
->clientConnectionManager
,
1549 notePinnedConnectionBecameIdle
,
1550 ConnStateData::PinnedIdleContext(serverConnectionSaved
, request
));
1552 // must not pool/share ispinned connections, even orphaned ones
1553 serverConnectionSaved
->close();
1556 fwdPconnPool
->push(serverConnectionSaved
, request
->url
.host());
1563 case COMPLETE_NONPERSISTENT_MSG
:
1564 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection
);
1570 maybeReadVirginBody();
1574 HttpStateData::mayReadVirginReplyBody() const
1576 // TODO: Be more precise here. For example, if/when reading trailer, we may
1577 // not be doneWithServer() yet, but we should return false. Similarly, we
1578 // could still be writing the request body after receiving the whole reply.
1579 return !doneWithServer();
1583 HttpStateData::maybeReadVirginBody()
1586 if (!Comm::IsConnOpen(serverConnection
) || fd_table
[serverConnection
->fd
].closing())
1589 if (!maybeMakeSpaceAvailable(false))
1592 // XXX: get rid of the do_next_read flag
1593 // check for the proper reasons preventing read(2)
1594 if (!flags
.do_next_read
)
1597 flags
.do_next_read
= false;
1599 // must not already be waiting for read(2) ...
1600 assert(!Comm::MonitorsRead(serverConnection
->fd
));
1602 // wait for read(2) to be possible.
1603 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
1604 AsyncCall::Pointer call
= JobCallback(11, 5, Dialer
, this, HttpStateData::readReply
);
1605 Comm::Read(serverConnection
, call
);
1609 HttpStateData::maybeMakeSpaceAvailable(bool doGrow
)
1611 // how much we are allowed to buffer
1612 const int limitBuffer
= (flags
.headers_parsed
? Config
.readAheadGap
: Config
.maxReplyHeaderSize
);
1614 if (limitBuffer
< 0 || inBuf
.length() >= (SBuf::size_type
)limitBuffer
) {
1615 // when buffer is at or over limit already
1616 debugs(11, 7, "will not read up to " << limitBuffer
<< ". buffer has (" << inBuf
.length() << "/" << inBuf
.spaceSize() << ") from " << serverConnection
);
1617 debugs(11, DBG_DATA
, "buffer has {" << inBuf
<< "}");
1618 // Process next response from buffer
1623 // how much we want to read
1624 const size_t read_size
= calcBufferSpaceToReserve(inBuf
.spaceSize(), (limitBuffer
- inBuf
.length()));
1627 debugs(11, 7, "will not read up to " << read_size
<< " into buffer (" << inBuf
.length() << "/" << inBuf
.spaceSize() << ") from " << serverConnection
);
1631 // just report whether we could grow or not, do not actually do it
1633 return (read_size
>= 2);
1635 // we may need to grow the buffer
1636 inBuf
.reserveSpace(read_size
);
1637 debugs(11, 8, (!flags
.do_next_read
? "will not" : "may") <<
1638 " read up to " << read_size
<< " bytes info buf(" << inBuf
.length() << "/" << inBuf
.spaceSize() <<
1639 ") from " << serverConnection
);
1641 return (inBuf
.spaceSize() >= 2); // only read if there is 1+ bytes of space available
1644 /// called after writing the very last request byte (body, last-chunk, etc)
1646 HttpStateData::wroteLast(const CommIoCbParams
&io
)
1648 debugs(11, 5, serverConnection
<< ": size " << io
.size
<< ": errflag " << io
.flag
<< ".");
1649 #if URL_CHECKSUM_DEBUG
1651 entry
->mem_obj
->checkUrlChecksum();
1654 // XXX: Keep in sync with Client::sentRequestBody().
1655 // TODO: Extract common parts.
1658 fd_bytes(io
.fd
, io
.size
, FD_WRITE
);
1659 statCounter
.server
.all
.kbytes_out
+= io
.size
;
1660 statCounter
.server
.http
.kbytes_out
+= io
.size
;
1663 if (io
.flag
== Comm::ERR_CLOSING
)
1666 // both successful and failed writes affect response times
1667 request
->hier
.notePeerWrite();
1670 const auto err
= new ErrorState(ERR_WRITE_ERROR
, Http::scBadGateway
, fwd
->request
, fwd
->al
);
1671 err
->xerrno
= io
.xerrno
;
1674 mustStop("HttpStateData::wroteLast");
1681 /// successfully wrote the entire request (including body, last-chunk, etc.)
1683 HttpStateData::sendComplete()
1686 * Set the read timeout here because it hasn't been set yet.
1687 * We only set the read timeout after the request has been
1688 * fully written to the peer. If we start the timeout
1689 * after connection establishment, then we are likely to hit
1690 * the timeout for POST/PUT requests that have very large
1693 typedef CommCbMemFunT
<HttpStateData
, CommTimeoutCbParams
> TimeoutDialer
;
1694 AsyncCall::Pointer timeoutCall
= JobCallback(11, 5,
1695 TimeoutDialer
, this, HttpStateData::httpTimeout
);
1697 commSetConnTimeout(serverConnection
, Config
.Timeout
.read
, timeoutCall
);
1698 flags
.request_sent
= true;
1702 HttpStateData::closeServer()
1704 debugs(11,5, "closing HTTP server " << serverConnection
<< " this " << this);
1706 if (Comm::IsConnOpen(serverConnection
)) {
1707 fwd
->unregister(serverConnection
);
1708 comm_remove_close_handler(serverConnection
->fd
, closeHandler
);
1709 closeHandler
= NULL
;
1710 serverConnection
->close();
1715 HttpStateData::doneWithServer() const
1717 return !Comm::IsConnOpen(serverConnection
);
1721 * Fixup authentication request headers for special cases
1724 httpFixupAuthentication(HttpRequest
* request
, const HttpHeader
* hdr_in
, HttpHeader
* hdr_out
, const Http::StateFlags
&flags
)
1726 /* Nothing to do unless we are forwarding to a peer */
1730 // This request is going "through" rather than "to" our _peer.
1731 if (flags
.tunneling
)
1734 /* Needs to be explicitly enabled */
1735 if (!request
->peer_login
)
1738 const auto header
= flags
.toOrigin
? Http::HdrType::AUTHORIZATION
: Http::HdrType::PROXY_AUTHORIZATION
;
1739 /* Maybe already dealt with? */
1740 if (hdr_out
->has(header
))
1743 /* Nothing to do here for PASSTHRU */
1744 if (strcmp(request
->peer_login
, "PASSTHRU") == 0)
1747 // Dangerous and undocumented PROXYPASS is a single-signon to servers with
1748 // the proxy password. Only Basic Authentication can work this way. This
1749 // statement forwards a "basic" Proxy-Authorization value from our client
1750 // to an originserver peer. Other PROXYPASS cases are handled lower.
1751 if (flags
.toOrigin
&&
1752 strcmp(request
->peer_login
, "PROXYPASS") == 0 &&
1753 hdr_in
->has(Http::HdrType::PROXY_AUTHORIZATION
)) {
1755 const char *auth
= hdr_in
->getStr(Http::HdrType::PROXY_AUTHORIZATION
);
1757 if (auth
&& strncasecmp(auth
, "basic ", 6) == 0) {
1758 hdr_out
->putStr(header
, auth
);
1763 char loginbuf
[base64_encode_len(MAX_LOGIN_SZ
)];
1765 struct base64_encode_ctx ctx
;
1766 base64_encode_init(&ctx
);
1768 /* Special mode to pass the username to the upstream cache */
1769 if (*request
->peer_login
== '*') {
1770 const char *username
= "-";
1772 if (request
->extacl_user
.size())
1773 username
= request
->extacl_user
.termedBuf();
1775 else if (request
->auth_user_request
!= NULL
)
1776 username
= request
->auth_user_request
->username();
1779 blen
= base64_encode_update(&ctx
, loginbuf
, strlen(username
), reinterpret_cast<const uint8_t*>(username
));
1780 blen
+= base64_encode_update(&ctx
, loginbuf
+blen
, strlen(request
->peer_login
+1), reinterpret_cast<const uint8_t*>(request
->peer_login
+1));
1781 blen
+= base64_encode_final(&ctx
, loginbuf
+blen
);
1782 httpHeaderPutStrf(hdr_out
, header
, "Basic %.*s", (int)blen
, loginbuf
);
1786 /* external_acl provided credentials */
1787 if (request
->extacl_user
.size() && request
->extacl_passwd
.size() &&
1788 (strcmp(request
->peer_login
, "PASS") == 0 ||
1789 strcmp(request
->peer_login
, "PROXYPASS") == 0)) {
1791 blen
= base64_encode_update(&ctx
, loginbuf
, request
->extacl_user
.size(), reinterpret_cast<const uint8_t*>(request
->extacl_user
.rawBuf()));
1792 blen
+= base64_encode_update(&ctx
, loginbuf
+blen
, 1, reinterpret_cast<const uint8_t*>(":"));
1793 blen
+= base64_encode_update(&ctx
, loginbuf
+blen
, request
->extacl_passwd
.size(), reinterpret_cast<const uint8_t*>(request
->extacl_passwd
.rawBuf()));
1794 blen
+= base64_encode_final(&ctx
, loginbuf
+blen
);
1795 httpHeaderPutStrf(hdr_out
, header
, "Basic %.*s", (int)blen
, loginbuf
);
1798 // if no external user credentials are available to fake authentication with PASS acts like PASSTHRU
1799 if (strcmp(request
->peer_login
, "PASS") == 0)
1802 /* Kerberos login to peer */
1803 #if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
1804 if (strncmp(request
->peer_login
, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
1806 char *PrincipalName
=NULL
,*p
;
1807 int negotiate_flags
= 0;
1809 if ((p
=strchr(request
->peer_login
,':')) != NULL
) {
1812 if (request
->flags
.auth_no_keytab
) {
1813 negotiate_flags
|= PEER_PROXY_NEGOTIATE_NOKEYTAB
;
1815 Token
= peer_proxy_negotiate_auth(PrincipalName
, request
->peer_host
, negotiate_flags
);
1817 httpHeaderPutStrf(hdr_out
, header
, "Negotiate %s",Token
);
1821 #endif /* HAVE_KRB5 && HAVE_GSSAPI */
1823 blen
= base64_encode_update(&ctx
, loginbuf
, strlen(request
->peer_login
), reinterpret_cast<const uint8_t*>(request
->peer_login
));
1824 blen
+= base64_encode_final(&ctx
, loginbuf
+blen
);
1825 httpHeaderPutStrf(hdr_out
, header
, "Basic %.*s", (int)blen
, loginbuf
);
1830 * build request headers and append them to a given MemBuf
1831 * used by buildRequestPrefix()
1832 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1835 HttpStateData::httpBuildRequestHeader(HttpRequest
* request
,
1837 const AccessLogEntryPointer
&al
,
1838 HttpHeader
* hdr_out
,
1839 const Http::StateFlags
&flags
)
1841 /* building buffer for complex strings */
1842 #define BBUF_SZ (MAX_URL+32)
1843 LOCAL_ARRAY(char, bbuf
, BBUF_SZ
);
1844 LOCAL_ARRAY(char, ntoabuf
, MAX_IPSTRLEN
);
1845 const HttpHeader
*hdr_in
= &request
->header
;
1846 const HttpHeaderEntry
*e
= NULL
;
1847 HttpHeaderPos pos
= HttpHeaderInitPos
;
1848 assert (hdr_out
->owner
== hoRequest
);
1850 /* use our IMS header if the cached entry has Last-Modified time */
1851 if (request
->lastmod
> -1)
1852 hdr_out
->putTime(Http::HdrType::IF_MODIFIED_SINCE
, request
->lastmod
);
1854 // Add our own If-None-Match field if the cached entry has a strong ETag.
1855 // copyOneHeaderFromClientsideRequestToUpstreamRequest() adds client ones.
1856 if (request
->etag
.size() > 0) {
1857 hdr_out
->addEntry(new HttpHeaderEntry(Http::HdrType::IF_NONE_MATCH
, SBuf(),
1858 request
->etag
.termedBuf()));
1861 bool we_do_ranges
= decideIfWeDoRanges (request
);
1863 String
strConnection (hdr_in
->getList(Http::HdrType::CONNECTION
));
1865 while ((e
= hdr_in
->getEntry(&pos
)))
1866 copyOneHeaderFromClientsideRequestToUpstreamRequest(e
, strConnection
, request
, hdr_out
, we_do_ranges
, flags
);
1868 /* Abstraction break: We should interpret multipart/byterange responses
1869 * into offset-length data, and this works around our inability to do so.
1871 if (!we_do_ranges
&& request
->multipartRangeRequest()) {
1872 /* don't cache the result */
1873 request
->flags
.cachable
= false;
1874 /* pretend it's not a range request */
1875 request
->ignoreRange("want to request the whole object");
1876 request
->flags
.isRanged
= false;
1879 hdr_out
->addVia(request
->http_ver
, hdr_in
);
1881 if (request
->flags
.accelerated
) {
1882 /* Append Surrogate-Capabilities */
1883 String
strSurrogate(hdr_in
->getList(Http::HdrType::SURROGATE_CAPABILITY
));
1885 snprintf(bbuf
, BBUF_SZ
, "%s=\"Surrogate/1.0 ESI/1.0\"", Config
.Accel
.surrogate_id
);
1887 snprintf(bbuf
, BBUF_SZ
, "%s=\"Surrogate/1.0\"", Config
.Accel
.surrogate_id
);
1889 strListAdd(&strSurrogate
, bbuf
, ',');
1890 hdr_out
->putStr(Http::HdrType::SURROGATE_CAPABILITY
, strSurrogate
.termedBuf());
1893 /** \pre Handle X-Forwarded-For */
1894 if (strcmp(opt_forwarded_for
, "delete") != 0) {
1896 String strFwd
= hdr_in
->getList(Http::HdrType::X_FORWARDED_FOR
);
1898 // if we cannot double strFwd size, then it grew past 50% of the limit
1899 if (!strFwd
.canGrowBy(strFwd
.size())) {
1900 // There is probably a forwarding loop with Via detection disabled.
1901 // If we do nothing, String will assert on overflow soon.
1902 // TODO: Terminate all transactions with huge XFF?
1905 static int warnedCount
= 0;
1906 if (warnedCount
++ < 100) {
1907 const SBuf
url(entry
? SBuf(entry
->url()) : request
->effectiveRequestUri());
1908 debugs(11, DBG_IMPORTANT
, "WARNING: likely forwarding loop with " << url
);
1912 if (strcmp(opt_forwarded_for
, "on") == 0) {
1913 /** If set to ON - append client IP or 'unknown'. */
1914 if ( request
->client_addr
.isNoAddr() )
1915 strListAdd(&strFwd
, "unknown", ',');
1917 strListAdd(&strFwd
, request
->client_addr
.toStr(ntoabuf
, MAX_IPSTRLEN
), ',');
1918 } else if (strcmp(opt_forwarded_for
, "off") == 0) {
1919 /** If set to OFF - append 'unknown'. */
1920 strListAdd(&strFwd
, "unknown", ',');
1921 } else if (strcmp(opt_forwarded_for
, "transparent") == 0) {
1922 /** If set to TRANSPARENT - pass through unchanged. */
1923 } else if (strcmp(opt_forwarded_for
, "truncate") == 0) {
1924 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
1925 if ( request
->client_addr
.isNoAddr() )
1928 strFwd
= request
->client_addr
.toStr(ntoabuf
, MAX_IPSTRLEN
);
1930 if (strFwd
.size() > 0)
1931 hdr_out
->putStr(Http::HdrType::X_FORWARDED_FOR
, strFwd
.termedBuf());
1933 /** If set to DELETE - do not copy through. */
1935 /* append Host if not there already */
1936 if (!hdr_out
->has(Http::HdrType::HOST
)) {
1937 if (request
->peer_domain
) {
1938 hdr_out
->putStr(Http::HdrType::HOST
, request
->peer_domain
);
1940 SBuf authority
= request
->url
.authority();
1941 hdr_out
->putStr(Http::HdrType::HOST
, authority
.c_str());
1945 /* append Authorization if known in URL, not in header and going direct */
1946 if (!hdr_out
->has(Http::HdrType::AUTHORIZATION
)) {
1947 if (flags
.toOrigin
&& !request
->url
.userInfo().isEmpty()) {
1948 static char result
[base64_encode_len(MAX_URL
*2)]; // should be big enough for a single URI segment
1949 struct base64_encode_ctx ctx
;
1950 base64_encode_init(&ctx
);
1951 size_t blen
= base64_encode_update(&ctx
, result
, request
->url
.userInfo().length(), reinterpret_cast<const uint8_t*>(request
->url
.userInfo().rawContent()));
1952 blen
+= base64_encode_final(&ctx
, result
+blen
);
1953 result
[blen
] = '\0';
1955 httpHeaderPutStrf(hdr_out
, Http::HdrType::AUTHORIZATION
, "Basic %.*s", (int)blen
, result
);
1959 /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
1960 httpFixupAuthentication(request
, hdr_in
, hdr_out
, flags
);
1962 /* append Cache-Control, add max-age if not there already */
1964 HttpHdrCc
*cc
= hdr_in
->getCc();
1967 cc
= new HttpHdrCc();
1969 /* Add max-age only without no-cache */
1970 if (!cc
->hasMaxAge() && !cc
->hasNoCache()) {
1971 // XXX: performance regression. c_str() reallocates
1972 SBuf
tmp(request
->effectiveRequestUri());
1973 cc
->maxAge(getMaxAge(entry
? entry
->url() : tmp
.c_str()));
1976 /* Enforce sibling relations */
1977 if (flags
.only_if_cached
)
1978 cc
->onlyIfCached(true);
1985 // Always send Connection because HTTP/1.0 servers need explicit
1986 // "keep-alive", HTTP/1.1 servers need explicit "close", Upgrade recipients
1987 // need bare "upgrade", and we do not always know the server expectations.
1988 if (!hdr_out
->has(Http::HdrType::CONNECTION
)) // forwardUpgrade() may add it
1989 hdr_out
->putStr(Http::HdrType::CONNECTION
, flags
.keepalive
? "keep-alive" : "close");
1991 /* append Front-End-Https */
1992 if (flags
.front_end_https
) {
1993 if (flags
.front_end_https
== 1 || request
->url
.getScheme() == AnyP::PROTO_HTTPS
)
1994 hdr_out
->putStr(Http::HdrType::FRONT_END_HTTPS
, "On");
1997 if (flags
.chunked_request
) {
1998 // Do not just copy the original value so that if the client-side
1999 // starts decode other encodings, this code may remain valid.
2000 hdr_out
->putStr(Http::HdrType::TRANSFER_ENCODING
, "chunked");
2003 /* Now mangle the headers. */
2004 httpHdrMangleList(hdr_out
, request
, al
, ROR_REQUEST
);
2006 strConnection
.clean();
2009 /// copies from-client Upgrade info into the given to-server header while
2010 /// honoring configuration filters and following HTTP requirements
2012 HttpStateData::forwardUpgrade(HttpHeader
&hdrOut
)
2014 if (!Config
.http_upgrade_request_protocols
)
2015 return; // forward nothing by default
2017 /* RFC 7230 section 6.7 paragraph 10:
2018 * A server MUST ignore an Upgrade header field that is received in
2019 * an HTTP/1.0 request.
2021 if (request
->http_ver
== Http::ProtocolVersion(1,0))
2024 const auto &hdrIn
= request
->header
;
2025 if (!hdrIn
.has(Http::HdrType::UPGRADE
))
2027 const auto upgradeIn
= hdrIn
.getList(Http::HdrType::UPGRADE
);
2031 ACLFilledChecklist
ch(nullptr, request
.getRaw());
2033 const char *pos
= nullptr;
2034 const char *offeredStr
= nullptr;
2035 int offeredStrLen
= 0;
2036 while (strListGetItem(&upgradeIn
, ',', &offeredStr
, &offeredStrLen
, &pos
)) {
2037 const ProtocolView
offeredProto(offeredStr
, offeredStrLen
);
2038 debugs(11, 5, "checks all rules applicable to " << offeredProto
);
2039 Config
.http_upgrade_request_protocols
->forApplicable(offeredProto
, [&ch
, offeredStr
, offeredStrLen
, &upgradeOut
] (const SBuf
&cfgProto
, const acl_access
*guard
) {
2040 debugs(11, 5, "checks " << cfgProto
<< " rule(s)");
2041 ch
.changeAcl(guard
);
2042 const auto answer
= ch
.fastCheck();
2043 if (answer
.implicit
)
2044 return false; // keep looking for an explicit rule match
2045 if (answer
.allowed())
2046 strListAdd(upgradeOut
, offeredStr
, offeredStrLen
);
2047 // else drop the offer (explicitly denied cases and ACL errors)
2048 return true; // stop after an explicit rule match or an error
2052 if (upgradeOut
.size()) {
2053 hdrOut
.putStr(Http::HdrType::UPGRADE
, upgradeOut
.termedBuf());
2055 /* RFC 7230 section 6.7 paragraph 10:
2056 * When Upgrade is sent, the sender MUST also send a Connection header
2057 * field that contains an "upgrade" connection option, in
2058 * order to prevent Upgrade from being accidentally forwarded by
2059 * intermediaries that might not implement the listed protocols.
2061 * NP: Squid does not truly implement the protocol(s) in this Upgrade.
2062 * For now we are treating an explicit blind tunnel as "implemented"
2063 * regardless of the security implications.
2065 hdrOut
.putStr(Http::HdrType::CONNECTION
, "upgrade");
2067 // Connection:close and Connection:keepalive confuse some Upgrade
2068 // recipients, so we do not send those headers. Our Upgrade request
2069 // implicitly offers connection persistency per HTTP/1.1 defaults.
2070 // Update the keepalive flag to reflect that offer.
2071 // * If the server upgrades, then we would not be talking HTTP past the
2072 // HTTP 101 control message, and HTTP persistence would be irrelevant.
2073 // * Otherwise, our request will contradict onoff.server_pconns=off or
2074 // other no-keepalive conditions (if any). We compensate by copying
2075 // the original no-keepalive decision now and honoring it later.
2076 flags
.forceClose
= !flags
.keepalive
;
2077 flags
.keepalive
= true; // should already be true in most cases
2082 * Decides whether a particular header may be cloned from the received Clients request
2083 * to our outgoing fetch request.
2086 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, const String strConnection
, const HttpRequest
* request
, HttpHeader
* hdr_out
, const int we_do_ranges
, const Http::StateFlags
&flags
)
2088 debugs(11, 5, "httpBuildRequestHeader: " << e
->name
<< ": " << e
->value
);
2092 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
2094 case Http::HdrType::PROXY_AUTHORIZATION
:
2095 /** \par Proxy-Authorization:
2096 * Only pass on proxy authentication to peers for which
2097 * authentication forwarding is explicitly enabled
2099 if (!flags
.toOrigin
&& request
->peer_login
&&
2100 (strcmp(request
->peer_login
, "PASS") == 0 ||
2101 strcmp(request
->peer_login
, "PROXYPASS") == 0 ||
2102 strcmp(request
->peer_login
, "PASSTHRU") == 0)) {
2103 hdr_out
->addEntry(e
->clone());
2107 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
2109 case Http::HdrType::CONNECTION
: /** \par Connection: */
2110 case Http::HdrType::TE
: /** \par TE: */
2111 case Http::HdrType::KEEP_ALIVE
: /** \par Keep-Alive: */
2112 case Http::HdrType::PROXY_AUTHENTICATE
: /** \par Proxy-Authenticate: */
2113 case Http::HdrType::TRAILER
: /** \par Trailer: */
2114 case Http::HdrType::TRANSFER_ENCODING
: /** \par Transfer-Encoding: */
2117 /// \par Upgrade is hop-by-hop but forwardUpgrade() may send a filtered one
2118 case Http::HdrType::UPGRADE
:
2121 /** \par OTHER headers I haven't bothered to track down yet. */
2123 case Http::HdrType::AUTHORIZATION
:
2124 /** \par WWW-Authorization:
2125 * Pass on WWW authentication */
2127 if (!flags
.toOriginPeer()) {
2128 hdr_out
->addEntry(e
->clone());
2130 /** \note Assume that talking to a cache_peer originserver makes
2131 * us a reverse proxy and only forward authentication if enabled
2132 * (see also httpFixupAuthentication for special cases)
2134 if (request
->peer_login
&&
2135 (strcmp(request
->peer_login
, "PASS") == 0 ||
2136 strcmp(request
->peer_login
, "PASSTHRU") == 0 ||
2137 strcmp(request
->peer_login
, "PROXYPASS") == 0)) {
2138 hdr_out
->addEntry(e
->clone());
2144 case Http::HdrType::HOST
:
2146 * Normally Squid rewrites the Host: header.
2147 * However, there is one case when we don't: If the URL
2148 * went through our redirector and the admin configured
2149 * 'redir_rewrites_host' to be off.
2151 if (request
->peer_domain
)
2152 hdr_out
->putStr(Http::HdrType::HOST
, request
->peer_domain
);
2153 else if (request
->flags
.redirected
&& !Config
.onoff
.redir_rewrites_host
)
2154 hdr_out
->addEntry(e
->clone());
2156 SBuf authority
= request
->url
.authority();
2157 hdr_out
->putStr(Http::HdrType::HOST
, authority
.c_str());
2162 case Http::HdrType::IF_MODIFIED_SINCE
:
2163 /** \par If-Modified-Since:
2164 * append unless we added our own,
2165 * but only if cache_miss_revalidate is enabled, or
2166 * the request is not cacheable, or
2167 * the request contains authentication credentials.
2168 * \note at most one client's If-Modified-Since header can pass through
2170 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2171 if (hdr_out
->has(Http::HdrType::IF_MODIFIED_SINCE
))
2173 else if (Config
.onoff
.cache_miss_revalidate
|| !request
->flags
.cachable
|| request
->flags
.auth
)
2174 hdr_out
->addEntry(e
->clone());
2177 case Http::HdrType::IF_NONE_MATCH
:
2178 /** \par If-None-Match:
2179 * append if the wildcard '*' special case value is present, or
2180 * cache_miss_revalidate is disabled, or
2181 * the request is not cacheable in this proxy, or
2182 * the request contains authentication credentials.
2183 * \note this header lists a set of responses for the server to elide sending. Squid added values are extending that set.
2185 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2186 if (hdr_out
->hasListMember(Http::HdrType::IF_MATCH
, "*", ',') || Config
.onoff
.cache_miss_revalidate
|| !request
->flags
.cachable
|| request
->flags
.auth
)
2187 hdr_out
->addEntry(e
->clone());
2190 case Http::HdrType::MAX_FORWARDS
:
2191 /** \par Max-Forwards:
2192 * pass only on TRACE or OPTIONS requests */
2193 if (request
->method
== Http::METHOD_TRACE
|| request
->method
== Http::METHOD_OPTIONS
) {
2194 const int64_t hops
= e
->getInt64();
2197 hdr_out
->putInt64(Http::HdrType::MAX_FORWARDS
, hops
- 1);
2202 case Http::HdrType::VIA
:
2204 * If Via is disabled then forward any received header as-is.
2205 * Otherwise leave for explicit updated addition later. */
2207 if (!Config
.onoff
.via
)
2208 hdr_out
->addEntry(e
->clone());
2212 case Http::HdrType::RANGE
:
2214 case Http::HdrType::IF_RANGE
:
2216 case Http::HdrType::REQUEST_RANGE
:
2217 /** \par Range:, If-Range:, Request-Range:
2218 * Only pass if we accept ranges */
2220 hdr_out
->addEntry(e
->clone());
2224 case Http::HdrType::PROXY_CONNECTION
: // SHOULD ignore. But doing so breaks things.
2227 case Http::HdrType::CONTENT_LENGTH
:
2228 // pass through unless we chunk; also, keeping this away from default
2229 // prevents request smuggling via Connection: Content-Length tricks
2230 if (!flags
.chunked_request
)
2231 hdr_out
->addEntry(e
->clone());
2234 case Http::HdrType::X_FORWARDED_FOR
:
2236 case Http::HdrType::CACHE_CONTROL
:
2237 /** \par X-Forwarded-For:, Cache-Control:
2238 * handled specially by Squid, so leave off for now.
2239 * append these after the loop if needed */
2242 case Http::HdrType::FRONT_END_HTTPS
:
2243 /** \par Front-End-Https:
2244 * Pass thru only if peer is configured with front-end-https */
2245 if (!flags
.front_end_https
)
2246 hdr_out
->addEntry(e
->clone());
2252 * pass on all other header fields
2253 * which are NOT listed by the special Connection: header. */
2254 if (strConnection
.size()>0 && strListIsMember(&strConnection
, e
->name
, ',')) {
2255 debugs(11, 2, "'" << e
->name
<< "' header cropped by Connection: definition");
2259 hdr_out
->addEntry(e
->clone());
2264 HttpStateData::decideIfWeDoRanges (HttpRequest
* request
)
2267 /* decide if we want to do Ranges ourselves
2268 * and fetch the whole object now)
2269 * We want to handle Ranges ourselves iff
2270 * - we can actually parse client Range specs
2271 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
2272 * - reply will be cachable
2273 * (If the reply will be uncachable we have to throw it away after
2274 * serving this request, so it is better to forward ranges to
2275 * the server and fetch only the requested content)
2278 int64_t roffLimit
= request
->getRangeOffsetLimit();
2280 if (NULL
== request
->range
|| !request
->flags
.cachable
2281 || request
->range
->offsetLimitExceeded(roffLimit
) || request
->flags
.connectionAuth
)
2284 debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
2285 request
->range
<< ", cachable: " <<
2286 request
->flags
.cachable
<< "; we_do_ranges: " << result
);
2291 /* build request prefix and append it to a given MemBuf;
2292 * return the length of the prefix */
2294 HttpStateData::buildRequestPrefix(MemBuf
* mb
)
2296 const int offset
= mb
->size
;
2297 /* Uses a local httpver variable to print the HTTP label
2298 * since the HttpRequest may have an older version label.
2299 * XXX: This could create protocol bugs as the headers sent and
2300 * flow control should all be based on the HttpRequest version
2301 * not the one we are sending. Needs checking.
2303 const AnyP::ProtocolVersion httpver
= Http::ProtocolVersion();
2304 const SBuf
url(flags
.toOrigin
? request
->url
.path() : request
->effectiveRequestUri());
2305 mb
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" %s/%d.%d\r\n",
2306 SQUIDSBUFPRINT(request
->method
.image()),
2307 SQUIDSBUFPRINT(url
),
2308 AnyP::ProtocolType_str
[httpver
.protocol
],
2309 httpver
.major
,httpver
.minor
);
2310 /* build and pack headers */
2312 HttpHeader
hdr(hoRequest
);
2313 forwardUpgrade(hdr
); // before httpBuildRequestHeader() for CONNECTION
2314 httpBuildRequestHeader(request
.getRaw(), entry
, fwd
->al
, &hdr
, flags
);
2316 if (request
->flags
.pinned
&& request
->flags
.connectionAuth
)
2317 request
->flags
.authSent
= true;
2318 else if (hdr
.has(Http::HdrType::AUTHORIZATION
))
2319 request
->flags
.authSent
= true;
2321 // The late placement of this check supports reply_header_add mangling,
2322 // but also complicates optimizing upgradeHeaderOut-like lookups.
2323 if (hdr
.has(Http::HdrType::UPGRADE
)) {
2324 assert(!upgradeHeaderOut
);
2325 upgradeHeaderOut
= new String(hdr
.getList(Http::HdrType::UPGRADE
));
2331 /* append header terminator */
2332 mb
->append(crlf
, 2);
2333 return mb
->size
- offset
;
2336 /* This will be called when connect completes. Write request. */
2338 HttpStateData::sendRequest()
2342 debugs(11, 5, serverConnection
<< ", request " << request
<< ", this " << this << ".");
2344 if (!Comm::IsConnOpen(serverConnection
)) {
2345 debugs(11,3, "cannot send request to closing " << serverConnection
);
2346 assert(closeHandler
!= NULL
);
2350 typedef CommCbMemFunT
<HttpStateData
, CommTimeoutCbParams
> TimeoutDialer
;
2351 AsyncCall::Pointer timeoutCall
= JobCallback(11, 5,
2352 TimeoutDialer
, this, HttpStateData::httpTimeout
);
2353 commSetConnTimeout(serverConnection
, Config
.Timeout
.lifetime
, timeoutCall
);
2354 flags
.do_next_read
= true;
2355 maybeReadVirginBody();
2357 if (request
->body_pipe
!= NULL
) {
2358 if (!startRequestBodyFlow()) // register to receive body data
2360 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2361 requestSender
= JobCallback(11,5,
2362 Dialer
, this, HttpStateData::sentRequestBody
);
2364 Must(!flags
.chunked_request
);
2365 // use chunked encoding if we do not know the length
2366 if (request
->content_length
< 0)
2367 flags
.chunked_request
= true;
2369 assert(!requestBodySource
);
2370 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2371 requestSender
= JobCallback(11,5,
2372 Dialer
, this, HttpStateData::wroteLast
);
2376 * Is keep-alive okay for all request methods?
2378 if (request
->flags
.mustKeepalive
)
2379 flags
.keepalive
= true;
2380 else if (request
->flags
.pinned
)
2381 flags
.keepalive
= request
->persistent();
2382 else if (!Config
.onoff
.server_pconns
)
2383 flags
.keepalive
= false;
2384 else if (flags
.tunneling
)
2385 // tunneled non pinned bumped requests must not keepalive
2386 flags
.keepalive
= !request
->flags
.sslBumped
;
2387 else if (_peer
== NULL
)
2388 flags
.keepalive
= true;
2389 else if (_peer
->stats
.n_keepalives_sent
< 10)
2390 flags
.keepalive
= true;
2391 else if ((double) _peer
->stats
.n_keepalives_recv
/
2392 (double) _peer
->stats
.n_keepalives_sent
> 0.50)
2393 flags
.keepalive
= true;
2395 if (_peer
&& !flags
.tunneling
) {
2396 /*The old code here was
2397 if (neighborType(_peer, request->url) == PEER_SIBLING && ...
2398 which is equivalent to:
2399 if (neighborType(_peer, URL()) == PEER_SIBLING && ...
2401 if (((_peer->type == PEER_MULTICAST && p->options.mcast_siblings) ||
2402 _peer->type == PEER_SIBLINGS ) && _peer->options.allow_miss)
2403 flags.only_if_cached = 1;
2405 But I suppose it was a bug
2407 if (neighborType(_peer
, request
->url
) == PEER_SIBLING
&& !_peer
->options
.allow_miss
)
2408 flags
.only_if_cached
= true;
2410 flags
.front_end_https
= _peer
->front_end_https
;
2414 request
->peer_host
=_peer
?_peer
->host
:NULL
;
2415 buildRequestPrefix(&mb
);
2417 debugs(11, 2, "HTTP Server " << serverConnection
);
2418 debugs(11, 2, "HTTP Server REQUEST:\n---------\n" << mb
.buf
<< "\n----------");
2420 Comm::Write(serverConnection
, &mb
, requestSender
);
2425 HttpStateData::getMoreRequestBody(MemBuf
&buf
)
2427 // parent's implementation can handle the no-encoding case
2428 if (!flags
.chunked_request
)
2429 return Client::getMoreRequestBody(buf
);
2433 Must(requestBodySource
!= NULL
);
2434 if (!requestBodySource
->getMoreData(raw
))
2435 return false; // no request body bytes to chunk yet
2437 // optimization: pre-allocate buffer size that should be enough
2438 const mb_size_t rawDataSize
= raw
.contentSize();
2439 // we may need to send: hex-chunk-size CRLF raw-data CRLF last-chunk
2440 buf
.init(16 + 2 + rawDataSize
+ 2 + 5, raw
.max_capacity
);
2442 buf
.appendf("%x\r\n", static_cast<unsigned int>(rawDataSize
));
2443 buf
.append(raw
.content(), rawDataSize
);
2444 buf
.append("\r\n", 2);
2446 Must(rawDataSize
> 0); // we did not accidentally created last-chunk above
2448 // Do not send last-chunk unless we successfully received everything
2449 if (receivedWholeRequestBody
) {
2450 Must(!flags
.sentLastChunk
);
2451 flags
.sentLastChunk
= true;
2452 buf
.append("0\r\n\r\n", 5);
2459 httpStart(FwdState
*fwd
)
2461 debugs(11, 3, fwd
->request
->method
<< ' ' << fwd
->entry
->url());
2462 AsyncJob::Start(new HttpStateData(fwd
));
2466 HttpStateData::start()
2468 if (!sendRequest()) {
2469 debugs(11, 3, "httpStart: aborted");
2470 mustStop("HttpStateData::start failed");
2474 ++ statCounter
.server
.all
.requests
;
2475 ++ statCounter
.server
.http
.requests
;
2478 * We used to set the read timeout here, but not any more.
2479 * Now its set in httpSendComplete() after the full request,
2480 * including request body, has been written to the server.
2484 /// if broken posts are enabled for the request, try to fix and return true
2486 HttpStateData::finishingBrokenPost()
2488 #if USE_HTTP_VIOLATIONS
2489 if (!Config
.accessList
.brokenPosts
) {
2490 debugs(11, 5, "No brokenPosts list");
2494 ACLFilledChecklist
ch(Config
.accessList
.brokenPosts
, originalRequest().getRaw());
2496 ch
.syncAle(originalRequest().getRaw(), nullptr);
2497 if (!ch
.fastCheck().allowed()) {
2498 debugs(11, 5, "didn't match brokenPosts");
2502 if (!Comm::IsConnOpen(serverConnection
)) {
2503 debugs(11, 3, "ignoring broken POST for closed " << serverConnection
);
2504 assert(closeHandler
!= NULL
);
2505 return true; // prevent caller from proceeding as if nothing happened
2508 debugs(11, 3, "finishingBrokenPost: fixing broken POST");
2509 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2510 requestSender
= JobCallback(11,5,
2511 Dialer
, this, HttpStateData::wroteLast
);
2512 Comm::Write(serverConnection
, "\r\n", 2, requestSender
, NULL
);
2516 #endif /* USE_HTTP_VIOLATIONS */
2519 /// if needed, write last-chunk to end the request body and return true
2521 HttpStateData::finishingChunkedRequest()
2523 if (flags
.sentLastChunk
) {
2524 debugs(11, 5, "already sent last-chunk");
2528 Must(receivedWholeRequestBody
); // or we should not be sending last-chunk
2529 flags
.sentLastChunk
= true;
2531 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2532 requestSender
= JobCallback(11,5, Dialer
, this, HttpStateData::wroteLast
);
2533 Comm::Write(serverConnection
, "0\r\n\r\n", 5, requestSender
, NULL
);
2538 HttpStateData::doneSendingRequestBody()
2540 Client::doneSendingRequestBody();
2541 debugs(11,5, serverConnection
);
2543 // do we need to write something after the last body byte?
2544 if (flags
.chunked_request
&& finishingChunkedRequest())
2546 if (!flags
.chunked_request
&& finishingBrokenPost())
2552 // more origin request body data is available
2554 HttpStateData::handleMoreRequestBodyAvailable()
2556 if (eof
|| !Comm::IsConnOpen(serverConnection
)) {
2557 // XXX: we should check this condition in other callbacks then!
2558 // TODO: Check whether this can actually happen: We should unsubscribe
2559 // as a body consumer when the above condition(s) are detected.
2560 debugs(11, DBG_IMPORTANT
, "Transaction aborted while reading HTTP body");
2564 assert(requestBodySource
!= NULL
);
2566 if (requestBodySource
->buf().hasContent()) {
2567 // XXX: why does not this trigger a debug message on every request?
2569 if (flags
.headers_parsed
&& !flags
.abuse_detected
) {
2570 flags
.abuse_detected
= true;
2571 debugs(11, DBG_IMPORTANT
, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << request
->client_addr
<< "' -> '" << entry
->url() << "'" );
2573 if (virginReply()->sline
.status() == Http::scInvalidHeader
) {
2575 mustStop("HttpStateData::handleMoreRequestBodyAvailable");
2581 HttpStateData::handleMoreRequestBodyAvailable();
2584 // premature end of the request body
2586 HttpStateData::handleRequestBodyProducerAborted()
2588 Client::handleRequestBodyProducerAborted();
2589 if (entry
->isEmpty()) {
2590 debugs(11, 3, "request body aborted: " << serverConnection
);
2591 // We usually get here when ICAP REQMOD aborts during body processing.
2592 // We might also get here if client-side aborts, but then our response
2593 // should not matter because either client-side will provide its own or
2594 // there will be no response at all (e.g., if the the client has left).
2595 const auto err
= new ErrorState(ERR_ICAP_FAILURE
, Http::scInternalServerError
, fwd
->request
, fwd
->al
);
2596 static const auto d
= MakeNamedErrorDetail("SRV_REQMOD_REQ_BODY");
2597 err
->detailError(d
);
2601 abortTransaction("request body producer aborted");
2604 // called when we wrote request headers(!) or a part of the body
2606 HttpStateData::sentRequestBody(const CommIoCbParams
&io
)
2609 statCounter
.server
.http
.kbytes_out
+= io
.size
;
2611 Client::sentRequestBody(io
);
2615 HttpStateData::abortAll(const char *reason
)
2617 debugs(11,5, "aborting transaction for " << reason
<<
2618 "; " << serverConnection
<< ", this " << this);
2622 HttpStateData::ReuseDecision::ReuseDecision(const StoreEntry
*e
, const Http::StatusCode code
)
2623 : answer(HttpStateData::ReuseDecision::reuseNot
), reason(nullptr), entry(e
), statusCode(code
) {}
2625 HttpStateData::ReuseDecision::Answers
2626 HttpStateData::ReuseDecision::make(const HttpStateData::ReuseDecision::Answers ans
, const char *why
)
2633 std::ostream
&operator <<(std::ostream
&os
, const HttpStateData::ReuseDecision
&d
)
2635 static const char *ReuseMessages
[] = {
2636 "do not cache and do not share", // reuseNot
2637 "cache positively and share", // cachePositively
2638 "cache negatively and share", // cacheNegatively
2639 "do not cache but share" // doNotCacheButShare
2642 assert(d
.answer
>= HttpStateData::ReuseDecision::reuseNot
&&
2643 d
.answer
<= HttpStateData::ReuseDecision::doNotCacheButShare
);
2644 return os
<< ReuseMessages
[d
.answer
] << " because " << d
.reason
<<
2645 "; HTTP status " << d
.statusCode
<< " " << *(d
.entry
);