2 * Copyright (C) 1996-2018 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 informations.
17 #include "acl/FilledChecklist.h"
18 #include "base/AsyncJobCalls.h"
19 #include "base/TextException.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"
27 #include "err_detail_type.h"
28 #include "errorpage.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 "log/access_log.h"
46 #include "MemObject.h"
47 #include "neighbors.h"
48 #include "peer_proxy_negotiate_auth.h"
49 #include "profiler/Profiler.h"
51 #include "RefreshPattern.h"
53 #include "SquidConfig.h"
54 #include "SquidTime.h"
55 #include "StatCounters.h"
62 #include "auth/UserRequest.h"
65 #include "DelayPools.h"
68 #define SQUID_ENTER_THROWING_CODE() try {
69 #define SQUID_EXIT_THROWING_CODE(status) \
72 catch (const std::exception &e) { \
73 debugs (11, 1, "Exception error:" << e.what()); \
77 CBDATA_CLASS_INIT(HttpStateData
);
79 static const char *const crlf
= "\r\n";
81 static void httpMaybeRemovePublic(StoreEntry
*, Http::StatusCode
);
82 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, const String strConnection
, const HttpRequest
* request
,
83 HttpHeader
* hdr_out
, const int we_do_ranges
, const Http::StateFlags
&);
85 HttpStateData::HttpStateData(FwdState
*theFwdState
) :
86 AsyncJob("HttpStateData"),
89 httpChunkDecoder(NULL
),
94 debugs(11,5,HERE
<< "HttpStateData " << this << " created");
95 ignoreCacheControl
= false;
96 surrogateNoStore
= false;
97 serverConnection
= fwd
->serverConnection();
99 if (fwd
->serverConnection() != NULL
)
100 _peer
= cbdataReference(fwd
->serverConnection()->getPeer()); /* might be NULL */
103 request
->flags
.proxying
= true;
105 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
106 * We might end up getting the object from somewhere else if,
107 * for example, the request to this neighbor fails.
109 if (_peer
->options
.proxy_only
)
110 entry
->releaseRequest(true);
113 entry
->setNoDelay(_peer
->options
.no_delay
);
118 * register the handler to free HTTP state data when the FD closes
120 typedef CommCbMemFunT
<HttpStateData
, CommCloseCbParams
> Dialer
;
121 closeHandler
= JobCallback(9, 5, Dialer
, this, HttpStateData::httpStateConnClosed
);
122 comm_add_close_handler(serverConnection
->fd
, closeHandler
);
125 HttpStateData::~HttpStateData()
128 * don't forget that ~Client() gets called automatically
131 if (httpChunkDecoder
)
132 delete httpChunkDecoder
;
134 cbdataReferenceDone(_peer
);
136 debugs(11,5, HERE
<< "HttpStateData " << this << " destroyed; " << serverConnection
);
139 const Comm::ConnectionPointer
&
140 HttpStateData::dataConnection() const
142 return serverConnection
;
146 HttpStateData::httpStateConnClosed(const CommCloseCbParams
¶ms
)
148 debugs(11, 5, "httpStateFree: FD " << params
.fd
<< ", httpState=" << params
.data
);
149 doneWithFwd
= "httpStateConnClosed()"; // assume FwdState is monitoring too
150 mustStop("HttpStateData::httpStateConnClosed");
154 HttpStateData::httpTimeout(const CommTimeoutCbParams
&)
156 debugs(11, 4, serverConnection
<< ": '" << entry
->url() << "'");
158 if (entry
->store_status
== STORE_PENDING
) {
159 fwd
->fail(new ErrorState(ERR_READ_TIMEOUT
, Http::scGatewayTimeout
, fwd
->request
));
163 mustStop("HttpStateData::httpTimeout");
167 findPreviouslyCachedEntry(StoreEntry
*newEntry
) {
168 assert(newEntry
->mem_obj
);
169 return newEntry
->mem_obj
->request
?
170 storeGetPublicByRequest(newEntry
->mem_obj
->request
.getRaw()) :
171 storeGetPublic(newEntry
->mem_obj
->storeId(), newEntry
->mem_obj
->method
);
174 /// Remove an existing public store entry if the incoming response (to be
175 /// stored in a currently private entry) is going to invalidate it.
177 httpMaybeRemovePublic(StoreEntry
* e
, Http::StatusCode status
)
182 // If the incoming response already goes into a public entry, then there is
183 // nothing to remove. This protects ready-for-collapsing entries as well.
184 if (!EBIT_TEST(e
->flags
, KEY_PRIVATE
))
187 // If the new/incoming response cannot be stored, then it does not
188 // compete with the old stored response for the public key, and the
189 // old stored response should be left as is.
190 if (e
->mem_obj
->request
&& !e
->mem_obj
->request
->flags
.cachable
)
197 case Http::scNonAuthoritativeInformation
:
199 case Http::scMultipleChoices
:
201 case Http::scMovedPermanently
:
205 case Http::scSeeOther
:
209 case Http::scNotFound
:
214 case Http::scForbidden
:
216 case Http::scMethodNotAllowed
:
223 case Http::scUnauthorized
:
233 * Any 2xx response should eject previously cached entities...
236 if (status
>= 200 && status
< 300)
244 if (!remove
&& !forbidden
)
247 StoreEntry
*pe
= findPreviouslyCachedEntry(e
);
252 neighborsHtcpClear(e
, nullptr, e
->mem_obj
->request
.getRaw(), e
->mem_obj
->method
, HTCP_CLR_INVALIDATION
);
258 * Also remove any cached HEAD response in case the object has
261 if (e
->mem_obj
->request
)
262 pe
= storeGetPublicByRequestMethod(e
->mem_obj
->request
.getRaw(), Http::METHOD_HEAD
);
264 pe
= storeGetPublic(e
->mem_obj
->storeId(), Http::METHOD_HEAD
);
269 neighborsHtcpClear(e
, nullptr, e
->mem_obj
->request
.getRaw(), HttpRequestMethod(Http::METHOD_HEAD
), HTCP_CLR_INVALIDATION
);
276 HttpStateData::processSurrogateControl(HttpReply
*reply
)
278 if (request
->flags
.accelerated
&& reply
->surrogate_control
) {
279 HttpHdrScTarget
*sctusable
= reply
->surrogate_control
->getMergedTarget(Config
.Accel
.surrogate_id
);
282 if (sctusable
->hasNoStore() ||
283 (Config
.onoff
.surrogate_is_remote
284 && sctusable
->noStoreRemote())) {
285 surrogateNoStore
= true;
286 // Be conservative for now and make it non-shareable because
287 // there is no enough information here to make the decision.
288 entry
->makePrivate(false);
291 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
292 * accelerated request or not...
293 * Still, this is an abstraction breach. - RC
295 if (sctusable
->hasMaxAge()) {
296 if (sctusable
->maxAge() < sctusable
->maxStale())
297 reply
->expires
= reply
->date
+ sctusable
->maxAge();
299 reply
->expires
= reply
->date
+ sctusable
->maxStale();
301 /* And update the timestamps */
302 entry
->timestampsSet();
305 /* We ignore cache-control directives as per the Surrogate specification */
306 ignoreCacheControl
= true;
313 HttpStateData::ReuseDecision::Answers
314 HttpStateData::reusableReply(HttpStateData::ReuseDecision
&decision
)
316 HttpReply
const *rep
= finalReply();
317 HttpHeader
const *hdr
= &rep
->header
;
319 #if USE_HTTP_VIOLATIONS
321 const RefreshPattern
*R
= NULL
;
323 /* This strange looking define first looks up the refresh pattern
324 * and then checks if the specified flag is set. The main purpose
325 * of this is to simplify the refresh pattern lookup and USE_HTTP_VIOLATIONS
328 #define REFRESH_OVERRIDE(flag) \
329 ((R = (R ? R : refreshLimits(entry->mem_obj->storeId()))) , \
330 (R && R->flags.flag))
332 #define REFRESH_OVERRIDE(flag) 0
335 if (EBIT_TEST(entry
->flags
, RELEASE_REQUEST
))
336 return decision
.make(ReuseDecision::doNotCacheButShare
, "the entry has been released");
338 // RFC 7234 section 4: a cache MUST use the most recent response
339 // (as determined by the Date header field)
340 // TODO: whether such responses could be shareable?
342 return decision
.make(ReuseDecision::reuseNot
, "the response has an older date header");
344 // Check for Surrogate/1.0 protocol conditions
345 // NP: reverse-proxy traffic our parent server has instructed us never to cache
346 if (surrogateNoStore
)
347 return decision
.make(ReuseDecision::reuseNot
, "Surrogate-Control:no-store");
349 // RFC 2616: HTTP/1.1 Cache-Control conditions
350 if (!ignoreCacheControl
) {
351 // XXX: check to see if the request headers alone were enough to prevent caching earlier
352 // (ie no-store request header) no need to check those all again here if so.
353 // for now we are not reliably doing that so we waste CPU re-checking request CC
355 // RFC 2616 section 14.9.2 - MUST NOT cache any response with request CC:no-store
356 if (request
&& request
->cache_control
&& request
->cache_control
->hasNoStore() &&
357 !REFRESH_OVERRIDE(ignore_no_store
))
358 return decision
.make(ReuseDecision::reuseNot
,
359 "client request Cache-Control:no-store");
361 // NP: request CC:no-cache only means cache READ is forbidden. STORE is permitted.
362 if (rep
->cache_control
&& rep
->cache_control
->hasNoCacheWithParameters()) {
363 /* TODO: we are allowed to cache when no-cache= has parameters.
364 * Provided we strip away any of the listed headers unless they are revalidated
365 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
366 * That is a bit tricky for squid right now so we avoid caching entirely.
368 return decision
.make(ReuseDecision::reuseNot
,
369 "server reply Cache-Control:no-cache has parameters");
372 // NP: request CC:private is undefined. We ignore.
373 // NP: other request CC flags are limiters on HIT/MISS. We don't care about here.
375 // RFC 2616 section 14.9.2 - MUST NOT cache any response with CC:no-store
376 if (rep
->cache_control
&& rep
->cache_control
->hasNoStore() &&
377 !REFRESH_OVERRIDE(ignore_no_store
))
378 return decision
.make(ReuseDecision::reuseNot
,
379 "server reply Cache-Control:no-store");
381 // RFC 2616 section 14.9.1 - MUST NOT cache any response with CC:private in a shared cache like Squid.
382 // CC:private overrides CC:public when both are present in a response.
383 // TODO: add a shared/private cache configuration possibility.
384 if (rep
->cache_control
&&
385 rep
->cache_control
->hasPrivate() &&
386 !REFRESH_OVERRIDE(ignore_private
)) {
387 /* TODO: we are allowed to cache when private= has parameters.
388 * Provided we strip away any of the listed headers unless they are revalidated
389 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
390 * That is a bit tricky for squid right now so we avoid caching entirely.
392 return decision
.make(ReuseDecision::reuseNot
,
393 "server reply Cache-Control:private");
397 // RFC 2068, sec 14.9.4 - MUST NOT cache any response with Authentication UNLESS certain CC controls are present
398 // allow HTTP violations to IGNORE those controls (ie re-block caching Auth)
399 if (request
&& (request
->flags
.auth
|| request
->flags
.authSent
)) {
400 if (!rep
->cache_control
)
401 return decision
.make(ReuseDecision::reuseNot
,
402 "authenticated and server reply missing Cache-Control");
404 if (ignoreCacheControl
)
405 return decision
.make(ReuseDecision::reuseNot
,
406 "authenticated and ignoring Cache-Control");
408 bool mayStore
= false;
409 // HTTPbis pt6 section 3.2: a response CC:public is present
410 if (rep
->cache_control
->hasPublic()) {
411 debugs(22, 3, HERE
<< "Authenticated but server reply Cache-Control:public");
414 // HTTPbis pt6 section 3.2: a response CC:must-revalidate is present
415 } else if (rep
->cache_control
->hasMustRevalidate()) {
416 debugs(22, 3, HERE
<< "Authenticated but server reply Cache-Control:must-revalidate");
419 #if USE_HTTP_VIOLATIONS
420 // NP: given the must-revalidate exception we should also be able to exempt no-cache.
421 // HTTPbis WG verdict on this is that it is omitted from the spec due to being 'unexpected' by
422 // some. The caching+revalidate is not exactly unsafe though with Squids interpretation of no-cache
423 // (without parameters) as equivalent to must-revalidate in the reply.
424 } else if (rep
->cache_control
->hasNoCacheWithoutParameters()) {
425 debugs(22, 3, HERE
<< "Authenticated but server reply Cache-Control:no-cache (equivalent to must-revalidate)");
429 // HTTPbis pt6 section 3.2: a response CC:s-maxage is present
430 } else if (rep
->cache_control
->hasSMaxAge()) {
431 debugs(22, 3, HERE
<< "Authenticated but server reply Cache-Control:s-maxage");
436 return decision
.make(ReuseDecision::reuseNot
, "authenticated transaction");
438 // NP: response CC:no-cache is equivalent to CC:must-revalidate,max-age=0. We MAY cache, and do so.
439 // NP: other request CC flags are limiters on HIT/MISS/REFRESH. We don't care about here.
442 /* HACK: The "multipart/x-mixed-replace" content type is used for
443 * continuous push replies. These are generally dynamic and
444 * probably should not be cachable
446 if ((v
= hdr
->getStr(Http::HdrType::CONTENT_TYPE
)))
447 if (!strncasecmp(v
, "multipart/x-mixed-replace", 25))
448 return decision
.make(ReuseDecision::reuseNot
, "Content-Type:multipart/x-mixed-replace");
450 // TODO: if possible, provide more specific message for each status code
451 static const char *shareableError
= "shareable error status code";
452 static const char *nonShareableError
= "non-shareable error status code";
453 ReuseDecision::Answers statusAnswer
= ReuseDecision::reuseNot
;
454 const char *statusReason
= nonShareableError
;
456 switch (rep
->sline
.status()) {
458 /* There are several situations when a non-cacheable response may be
459 * still shareable (e.g., among collapsed clients). We assume that these
460 * are 3xx and 5xx responses, indicating server problems and some of
461 * 4xx responses, common for all clients with a given cache key (e.g.,
462 * 404 Not Found or 414 URI Too Long). On the other hand, we should not
463 * share non-cacheable client-specific errors, such as 400 Bad Request
464 * or 406 Not Acceptable.
467 /* Responses that are cacheable */
471 case Http::scNonAuthoritativeInformation
:
473 case Http::scMultipleChoices
:
475 case Http::scMovedPermanently
:
476 case Http::scPermanentRedirect
:
480 * Don't cache objects that need to be refreshed on next request,
481 * unless we know how to refresh it.
484 if (refreshIsCachable(entry
) || REFRESH_OVERRIDE(store_stale
))
485 decision
.make(ReuseDecision::cachePositively
, "refresh check returned cacheable");
487 decision
.make(ReuseDecision::doNotCacheButShare
, "refresh check returned non-cacheable");
490 /* Responses that only are cacheable if the server says so */
493 case Http::scTemporaryRedirect
:
495 decision
.make(ReuseDecision::doNotCacheButShare
, "Date is missing/invalid");
496 else if (rep
->expires
> rep
->date
)
497 decision
.make(ReuseDecision::cachePositively
, "Expires > Date");
499 decision
.make(ReuseDecision::doNotCacheButShare
, "Expires <= Date");
502 /* These responses can be negatively cached. Most can also be shared. */
503 case Http::scNoContent
:
504 case Http::scUseProxy
:
505 case Http::scForbidden
:
506 case Http::scNotFound
:
507 case Http::scMethodNotAllowed
:
508 case Http::scUriTooLong
:
509 case Http::scInternalServerError
:
510 case Http::scNotImplemented
:
511 case Http::scBadGateway
:
512 case Http::scServiceUnavailable
:
513 case Http::scGatewayTimeout
:
514 case Http::scMisdirectedRequest
:
515 statusAnswer
= ReuseDecision::doNotCacheButShare
;
516 statusReason
= shareableError
;
517 // fall through to the actual decision making below
519 case Http::scBadRequest
: // no sharing; perhaps the server did not like something specific to this request
520 #if USE_HTTP_VIOLATIONS
521 if (Config
.negativeTtl
> 0)
522 decision
.make(ReuseDecision::cacheNegatively
, "Config.negativeTtl > 0");
525 decision
.make(statusAnswer
, statusReason
);
528 /* these responses can never be cached, some
529 of them can be shared though */
530 case Http::scSeeOther
:
531 case Http::scNotModified
:
532 case Http::scUnauthorized
:
533 case Http::scProxyAuthenticationRequired
:
534 case Http::scPaymentRequired
:
535 case Http::scInsufficientStorage
:
536 // TODO: use more specific reason for non-error status codes
537 decision
.make(ReuseDecision::doNotCacheButShare
, shareableError
);
540 case Http::scPartialContent
: /* Not yet supported. TODO: make shareable for suitable ranges */
541 case Http::scNotAcceptable
:
542 case Http::scRequestTimeout
: // TODO: is this shareable?
543 case Http::scConflict
: // TODO: is this shareable?
544 case Http::scLengthRequired
:
545 case Http::scPreconditionFailed
:
546 case Http::scPayloadTooLarge
:
547 case Http::scUnsupportedMediaType
:
548 case Http::scUnprocessableEntity
:
549 case Http::scLocked
: // TODO: is this shareable?
550 case Http::scFailedDependency
:
551 case Http::scRequestedRangeNotSatisfied
:
552 case Http::scExpectationFailed
:
553 case Http::scInvalidHeader
: /* Squid header parsing error */
554 case Http::scHeaderTooLarge
:
555 decision
.make(ReuseDecision::reuseNot
, nonShareableError
);
559 /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */
560 decision
.make(ReuseDecision::reuseNot
, "unknown status code");
564 return decision
.answer
;
567 /// assemble a variant key (vary-mark) from the given Vary header and HTTP request
569 assembleVaryKey(String
&vary
, SBuf
&vstr
, const HttpRequest
&request
)
571 static const SBuf
asterisk("*");
572 const char *pos
= nullptr;
573 const char *item
= nullptr;
576 while (strListGetItem(&vary
, ',', &item
, &ilen
, &pos
)) {
577 SBuf
name(item
, ilen
);
578 if (name
== asterisk
) {
584 vstr
.append(", ", 2);
586 String
hdr(request
.header
.getByName(name
));
587 const char *value
= hdr
.termedBuf();
589 value
= rfc1738_escape_part(value
);
590 vstr
.append("=\"", 2);
592 vstr
.append("\"", 1);
600 * For Vary, store the relevant request headers as
601 * virtual headers in the reply
602 * Returns an empty SBuf if the variance cannot be stored
605 httpMakeVaryMark(HttpRequest
* request
, HttpReply
const * reply
)
610 vary
= reply
->header
.getList(Http::HdrType::VARY
);
611 assembleVaryKey(vary
, vstr
, *request
);
613 #if X_ACCELERATOR_VARY
615 vary
= reply
->header
.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY
);
616 assembleVaryKey(vary
, vstr
, *request
);
624 HttpStateData::keepaliveAccounting(HttpReply
*reply
)
628 ++ _peer
->stats
.n_keepalives_sent
;
630 if (reply
->keep_alive
) {
632 ++ _peer
->stats
.n_keepalives_recv
;
634 if (Config
.onoff
.detect_broken_server_pconns
635 && reply
->bodySize(request
->method
) == -1 && !flags
.chunked
) {
636 debugs(11, DBG_IMPORTANT
, "keepaliveAccounting: Impossible keep-alive header from '" << entry
->url() << "'" );
637 // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
638 flags
.keepalive_broken
= true;
644 HttpStateData::checkDateSkew(HttpReply
*reply
)
646 if (reply
->date
> -1 && !_peer
) {
647 int skew
= abs((int)(reply
->date
- squid_curtime
));
650 debugs(11, 3, "" << request
->url
.host() << "'s clock is skewed by " << skew
<< " seconds!");
655 * This creates the error page itself.. its likely
656 * that the forward ported reply header max size patch
657 * generates non http conformant error pages - in which
658 * case the errors where should be 'BAD_GATEWAY' etc
661 HttpStateData::processReplyHeader()
663 /** Creates a blank header. If this routine is made incremental, this will not do */
665 /* NP: all exit points to this function MUST call ctx_exit(ctx) */
666 Ctx ctx
= ctx_enter(entry
->mem_obj
->urlXXX());
668 debugs(11, 3, "processReplyHeader: key '" << entry
->getMD5Text() << "'");
670 assert(!flags
.headers_parsed
);
672 if (!inBuf
.length()) {
677 /* Attempt to parse the first line; this will define where the protocol, status, reason-phrase and header begin */
680 hp
= new Http1::ResponseParser
;
682 bool parsedOk
= hp
->parse(inBuf
);
684 // sync the buffers after parsing.
685 inBuf
= hp
->remaining();
687 if (hp
->needsMoreData()) {
688 if (eof
) { // no more data coming
689 /* Bug 2879: Replies may terminate with \r\n then EOF instead of \r\n\r\n.
690 * We also may receive truncated responses.
691 * Ensure here that we have at minimum two \r\n when EOF is seen.
693 inBuf
.append("\r\n\r\n", 4);
695 parsedOk
= hp
->parse(inBuf
);
696 // sync the buffers after parsing.
697 inBuf
= hp
->remaining();
699 debugs(33, 5, "Incomplete response, waiting for end of response headers");
706 // unrecoverable parsing error
707 // TODO: Use Raw! XXX: inBuf no longer has the [beginning of the] malformed header.
708 debugs(11, 3, "Non-HTTP-compliant header:\n---------\n" << inBuf
<< "\n----------");
709 flags
.headers_parsed
= true;
710 HttpReply
*newrep
= new HttpReply
;
711 newrep
->sline
.set(Http::ProtocolVersion(), hp
->parseStatusCode
);
712 setVirginReply(newrep
);
718 /* We know the whole response is in parser now */
719 debugs(11, 2, "HTTP Server " << serverConnection
);
720 debugs(11, 2, "HTTP Server RESPONSE:\n---------\n" <<
721 hp
->messageProtocol() << " " << hp
->messageStatus() << " " << hp
->reasonPhrase() << "\n" <<
725 // reset payload tracking to begin after message headers
726 payloadSeen
= inBuf
.length();
728 HttpReply
*newrep
= new HttpReply
;
729 // XXX: RFC 7230 indicates we MAY ignore the reason phrase,
730 // and use an empty string on unknown status.
731 // We do that now to avoid performance regression from using SBuf::c_str()
732 newrep
->sline
.set(Http::ProtocolVersion(1,1), hp
->messageStatus() /* , hp->reasonPhrase() */);
733 newrep
->sline
.protocol
= newrep
->sline
.version
.protocol
= hp
->messageProtocol().protocol
;
734 newrep
->sline
.version
.major
= hp
->messageProtocol().major
;
735 newrep
->sline
.version
.minor
= hp
->messageProtocol().minor
;
738 if (!newrep
->parseHeader(*hp
)) {
739 // XXX: when Http::ProtocolVersion is a function, remove this hack. just set with messageProtocol()
740 newrep
->sline
.set(Http::ProtocolVersion(), Http::scInvalidHeader
);
741 newrep
->sline
.version
.protocol
= hp
->messageProtocol().protocol
;
742 newrep
->sline
.version
.major
= hp
->messageProtocol().major
;
743 newrep
->sline
.version
.minor
= hp
->messageProtocol().minor
;
744 debugs(11, 2, "error parsing response headers mime block");
747 // done with Parser, now process using the HttpReply
750 newrep
->sources
|= request
->url
.getScheme() == AnyP::PROTO_HTTPS
? Http::Message::srcHttps
: Http::Message::srcHttp
;
752 newrep
->removeStaleWarnings();
754 if (newrep
->sline
.protocol
== AnyP::PROTO_HTTP
&& newrep
->sline
.status() >= 100 && newrep
->sline
.status() < 200) {
760 flags
.chunked
= false;
761 if (newrep
->sline
.protocol
== AnyP::PROTO_HTTP
&& newrep
->header
.chunked()) {
762 flags
.chunked
= true;
763 httpChunkDecoder
= new Http1::TeChunkedParser
;
766 if (!peerSupportsConnectionPinning())
767 request
->flags
.connectionAuthDisabled
= true;
769 HttpReply
*vrep
= setVirginReply(newrep
);
770 flags
.headers_parsed
= true;
772 keepaliveAccounting(vrep
);
776 processSurrogateControl (vrep
);
778 request
->hier
.peer_reply_status
= newrep
->sline
.status();
783 /// ignore or start forwarding the 1xx response (a.k.a., control message)
785 HttpStateData::handle1xx(HttpReply
*reply
)
787 HttpReply::Pointer
msg(reply
); // will destroy reply if unused
789 // one 1xx at a time: we must not be called while waiting for previous 1xx
790 Must(!flags
.handling1xx
);
791 flags
.handling1xx
= true;
793 if (!request
->canHandle1xx() || request
->forcedBodyContinuation
) {
794 debugs(11, 2, "ignoring 1xx because it is " << (request
->forcedBodyContinuation
? "already sent" : "not supported by client"));
799 #if USE_HTTP_VIOLATIONS
800 // check whether the 1xx response forwarding is allowed by squid.conf
801 if (Config
.accessList
.reply
) {
802 ACLFilledChecklist
ch(Config
.accessList
.reply
, originalRequest().getRaw());
805 ch
.syncAle(originalRequest().getRaw(), nullptr);
806 HTTPMSGLOCK(ch
.reply
);
807 if (!ch
.fastCheck().allowed()) { // TODO: support slow lookups?
808 debugs(11, 3, HERE
<< "ignoring denied 1xx");
813 #endif // USE_HTTP_VIOLATIONS
815 debugs(11, 2, HERE
<< "forwarding 1xx to client");
817 // the Sink will use this to call us back after writing 1xx to the client
818 typedef NullaryMemFunT
<HttpStateData
> CbDialer
;
819 const AsyncCall::Pointer cb
= JobCallback(11, 3, CbDialer
, this,
820 HttpStateData::proceedAfter1xx
);
821 CallJobHere1(11, 4, request
->clientConnectionManager
, ConnStateData
,
822 ConnStateData::sendControlMsg
, HttpControlMsg(msg
, cb
));
823 // If the call is not fired, then the Sink is gone, and HttpStateData
824 // will terminate due to an aborted store entry or another similar error.
825 // If we get stuck, it is not handle1xx fault if we could get stuck
826 // for similar reasons without a 1xx response.
829 /// restores state and resumes processing after 1xx is ignored or forwarded
831 HttpStateData::proceedAfter1xx()
833 Must(flags
.handling1xx
);
834 debugs(11, 2, "continuing with " << payloadSeen
<< " bytes in buffer after 1xx");
835 CallJobHere(11, 3, this, HttpStateData
, HttpStateData::processReply
);
839 * returns true if the peer can support connection pinning
842 HttpStateData::peerSupportsConnectionPinning() const
847 /*If this peer does not support connection pinning (authenticated
848 connections) return false
850 if (!_peer
->connection_auth
)
853 const HttpReplyPointer
rep(entry
->mem_obj
->getReply());
855 /*The peer supports connection pinning and the http reply status
856 is not unauthorized, so the related connection can be pinned
858 if (rep
->sline
.status() != Http::scUnauthorized
)
861 /*The server respond with Http::scUnauthorized and the peer configured
862 with "connection-auth=on" we know that the peer supports pinned
865 if (_peer
->connection_auth
== 1)
868 /*At this point peer has configured with "connection-auth=auto"
869 parameter so we need some extra checks to decide if we are going
870 to allow pinned connections or not
873 /*if the peer configured with originserver just allow connection
874 pinning (squid 2.6 behaviour)
876 if (_peer
->options
.originserver
)
879 /*if the connections it is already pinned it is OK*/
880 if (request
->flags
.pinned
)
883 /*Allow pinned connections only if the Proxy-support header exists in
884 reply and has in its list the "Session-Based-Authentication"
885 which means that the peer supports connection pinning.
887 if (rep
->header
.hasListMember(Http::HdrType::PROXY_SUPPORT
, "Session-Based-Authentication", ','))
893 // Called when we parsed (and possibly adapted) the headers but
894 // had not starting storing (a.k.a., sending) the body yet.
896 HttpStateData::haveParsedReplyHeaders()
898 Client::haveParsedReplyHeaders();
900 Ctx ctx
= ctx_enter(entry
->mem_obj
->urlXXX());
901 HttpReply
*rep
= finalReply();
902 const Http::StatusCode statusCode
= rep
->sline
.status();
904 entry
->timestampsSet();
906 /* Check if object is cacheable or not based on reply code */
907 debugs(11, 3, "HTTP CODE: " << statusCode
);
909 if (StoreEntry
*oldEntry
= findPreviouslyCachedEntry(entry
)) {
910 oldEntry
->lock("HttpStateData::haveParsedReplyHeaders");
911 sawDateGoBack
= rep
->olderThan(oldEntry
->getReply());
912 oldEntry
->unlock("HttpStateData::haveParsedReplyHeaders");
915 if (neighbors_do_private_keys
&& !sawDateGoBack
)
916 httpMaybeRemovePublic(entry
, rep
->sline
.status());
918 bool varyFailure
= false;
919 if (rep
->header
.has(Http::HdrType::VARY
)
920 #if X_ACCELERATOR_VARY
921 || rep
->header
.has(Http::HdrType::HDR_X_ACCELERATOR_VARY
)
924 const SBuf
vary(httpMakeVaryMark(request
.getRaw(), rep
));
926 if (vary
.isEmpty()) {
927 // TODO: check whether such responses are shareable.
928 // Do not share for now.
929 entry
->makePrivate(false);
930 if (fwd
->reforwardableStatus(rep
->sline
.status()))
931 EBIT_SET(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
934 entry
->mem_obj
->vary_headers
= vary
;
936 // RFC 7231 section 7.1.4
937 // Vary:* can be cached, but has mandatory revalidation
938 static const SBuf
asterisk("*");
939 if (vary
== asterisk
)
940 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_ALWAYS
);
946 * If its not a reply that we will re-forward, then
947 * allow the client to get it.
949 if (fwd
->reforwardableStatus(rep
->sline
.status()))
950 EBIT_SET(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
952 ReuseDecision
decision(entry
, statusCode
);
954 switch (reusableReply(decision
)) {
956 case ReuseDecision::reuseNot
:
957 entry
->makePrivate(false);
960 case ReuseDecision::cachePositively
:
961 if (!entry
->makePublic()) {
962 decision
.make(ReuseDecision::doNotCacheButShare
, "public key creation error");
963 entry
->makePrivate(true);
967 case ReuseDecision::cacheNegatively
:
968 if (!entry
->cacheNegatively()) {
969 decision
.make(ReuseDecision::doNotCacheButShare
, "public key creation error");
970 entry
->makePrivate(true);
974 case ReuseDecision::doNotCacheButShare
:
975 entry
->makePrivate(true);
982 debugs(11, 3, "decided: " << decision
);
985 if (!ignoreCacheControl
) {
986 if (rep
->cache_control
) {
987 // We are required to revalidate on many conditions.
988 // For security reasons we do so even if storage was caused by refresh_pattern ignore-* option
990 // CC:must-revalidate or CC:proxy-revalidate
991 const bool ccMustRevalidate
= (rep
->cache_control
->hasProxyRevalidate() || rep
->cache_control
->hasMustRevalidate());
993 // CC:no-cache (only if there are no parameters)
994 const bool ccNoCacheNoParams
= rep
->cache_control
->hasNoCacheWithoutParameters();
997 const bool ccSMaxAge
= rep
->cache_control
->hasSMaxAge();
999 // CC:private (yes, these can sometimes be stored)
1000 const bool ccPrivate
= rep
->cache_control
->hasPrivate();
1002 if (ccNoCacheNoParams
|| ccPrivate
)
1003 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_ALWAYS
);
1004 else if (ccMustRevalidate
|| ccSMaxAge
)
1005 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_STALE
);
1007 #if USE_HTTP_VIOLATIONS // response header Pragma::no-cache is undefined in HTTP
1009 // Expensive calculation. So only do it IF the CC: header is not present.
1011 /* HACK: Pragma: no-cache in _replies_ is not documented in HTTP,
1012 * but servers like "Active Imaging Webcast/2.0" sure do use it */
1013 if (rep
->header
.has(Http::HdrType::PRAGMA
) &&
1014 rep
->header
.hasListMember(Http::HdrType::PRAGMA
,"no-cache",','))
1015 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE_ALWAYS
);
1021 headersLog(1, 0, request
->method
, rep
);
1028 HttpStateData::ConnectionStatus
1029 HttpStateData::statusIfComplete() const
1031 const HttpReply
*rep
= virginReply();
1033 * If the reply wants to close the connection, it takes precedence */
1035 static SBuf
close("close", 5);
1036 if (httpHeaderHasConnDir(&rep
->header
, close
))
1037 return COMPLETE_NONPERSISTENT_MSG
;
1040 * If we didn't send a keep-alive request header, then this
1041 * can not be a persistent connection.
1043 if (!flags
.keepalive
)
1044 return COMPLETE_NONPERSISTENT_MSG
;
1047 * If we haven't sent the whole request then this can not be a persistent
1050 if (!flags
.request_sent
) {
1051 debugs(11, 2, "Request not yet fully sent " << request
->method
<< ' ' << entry
->url());
1052 return COMPLETE_NONPERSISTENT_MSG
;
1056 * What does the reply have to say about keep-alive?
1060 * If the origin server (HTTP/1.0) does not send a keep-alive
1061 * header, but keeps the connection open anyway, what happens?
1062 * We'll return here and http.c waits for an EOF before changing
1063 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
1064 * and an error status code, and we might have to wait until
1065 * the server times out the socket.
1067 if (!rep
->keep_alive
)
1068 return COMPLETE_NONPERSISTENT_MSG
;
1070 return COMPLETE_PERSISTENT_MSG
;
1073 HttpStateData::ConnectionStatus
1074 HttpStateData::persistentConnStatus() const
1076 debugs(11, 3, HERE
<< serverConnection
<< " eof=" << eof
);
1077 if (eof
) // already reached EOF
1078 return COMPLETE_NONPERSISTENT_MSG
;
1080 /* If server fd is closing (but we have not been notified yet), stop Comm
1081 I/O to avoid assertions. TODO: Change Comm API to handle callers that
1082 want more I/O after async closing (usually initiated by others). */
1083 // XXX: add canReceive or s/canSend/canTalkToServer/
1084 if (!Comm::IsConnOpen(serverConnection
))
1085 return COMPLETE_NONPERSISTENT_MSG
;
1088 * In chunked response we do not know the content length but we are absolutely
1089 * sure about the end of response, so we are calling the statusIfComplete to
1090 * decide if we can be persistant
1092 if (lastChunk
&& flags
.chunked
)
1093 return statusIfComplete();
1095 const HttpReply
*vrep
= virginReply();
1096 debugs(11, 5, "persistentConnStatus: content_length=" << vrep
->content_length
);
1098 const int64_t clen
= vrep
->bodySize(request
->method
);
1100 debugs(11, 5, "persistentConnStatus: clen=" << clen
);
1102 /* If the body size is unknown we must wait for EOF */
1104 return INCOMPLETE_MSG
;
1107 * If the body size is known, we must wait until we've gotten all of it. */
1109 debugs(11,5, "payloadSeen=" << payloadSeen
<< " content_length=" << vrep
->content_length
);
1111 if (payloadSeen
< vrep
->content_length
)
1112 return INCOMPLETE_MSG
;
1114 if (payloadTruncated
> 0) // already read more than needed
1115 return COMPLETE_NONPERSISTENT_MSG
; // disable pconns
1119 * If there is no message body or we got it all, we can be persistent */
1120 return statusIfComplete();
1124 readDelayed(void *context
, CommRead
const &)
1126 HttpStateData
*state
= static_cast<HttpStateData
*>(context
);
1127 state
->flags
.do_next_read
= true;
1128 state
->maybeReadVirginBody();
1132 HttpStateData::readReply(const CommIoCbParams
&io
)
1134 Must(!flags
.do_next_read
); // XXX: should have been set false by mayReadVirginBody()
1135 flags
.do_next_read
= false;
1137 debugs(11, 5, io
.conn
);
1139 // Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us
1140 if (io
.flag
== Comm::ERR_CLOSING
) {
1141 debugs(11, 3, "http socket closing");
1145 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1146 abortTransaction("store entry aborted while reading reply");
1150 Must(Comm::IsConnOpen(serverConnection
));
1151 Must(io
.conn
->fd
== serverConnection
->fd
);
1154 * Don't reset the timeout value here. The value should be
1155 * counting Config.Timeout.request and applies to the request
1156 * as a whole, not individual read() calls.
1157 * Plus, it breaks our lame *HalfClosed() detection
1160 Must(maybeMakeSpaceAvailable(true));
1161 CommIoCbParams
rd(this); // will be expanded with ReadNow results
1163 rd
.size
= entry
->bytesWanted(Range
<size_t>(0, inBuf
.spaceSize()));
1166 assert(entry
->mem_obj
);
1167 AsyncCall::Pointer nilCall
;
1168 entry
->mem_obj
->delayRead(DeferredRead(readDelayed
, this, CommRead(io
.conn
, NULL
, 0, nilCall
)));
1172 switch (Comm::ReadNow(rd
, inBuf
)) {
1173 case Comm::INPROGRESS
:
1174 if (inBuf
.isEmpty())
1175 debugs(33, 2, io
.conn
<< ": no data to process, " << xstrerr(rd
.xerrno
));
1176 flags
.do_next_read
= true;
1177 maybeReadVirginBody();
1182 payloadSeen
+= rd
.size
;
1184 DelayId delayId
= entry
->mem_obj
->mostBytesAllowed();
1185 delayId
.bytesIn(rd
.size
);
1188 statCounter
.server
.all
.kbytes_in
+= rd
.size
;
1189 statCounter
.server
.http
.kbytes_in
+= rd
.size
;
1190 ++ IOStats
.Http
.reads
;
1193 for (int clen
= rd
.size
- 1; clen
; ++bin
)
1196 ++ IOStats
.Http
.read_hist
[bin
];
1198 request
->hier
.notePeerRead();
1201 /* Continue to process previously read data */
1204 case Comm::ENDFILE
: // close detected by 0-byte read
1206 flags
.do_next_read
= false;
1208 /* Continue to process previously read data */
1211 // case Comm::COMM_ERROR:
1212 default: // no other flags should ever occur
1213 debugs(11, 2, io
.conn
<< ": read failure: " << xstrerr(rd
.xerrno
));
1214 ErrorState
*err
= new ErrorState(ERR_READ_ERROR
, Http::scBadGateway
, fwd
->request
);
1215 err
->xerrno
= rd
.xerrno
;
1217 flags
.do_next_read
= false;
1219 mustStop("HttpStateData::readReply");
1223 /* Process next response from buffer */
1227 /// processes the already read and buffered response data, possibly after
1228 /// waiting for asynchronous 1xx control message processing
1230 HttpStateData::processReply()
1233 if (flags
.handling1xx
) { // we came back after handling a 1xx response
1234 debugs(11, 5, HERE
<< "done with 1xx handling");
1235 flags
.handling1xx
= false;
1236 Must(!flags
.headers_parsed
);
1239 if (!flags
.headers_parsed
) { // have not parsed headers yet?
1240 PROF_start(HttpStateData_processReplyHeader
);
1241 processReplyHeader();
1242 PROF_stop(HttpStateData_processReplyHeader
);
1244 if (!continueAfterParsingHeader()) // parsing error or need more data
1245 return; // TODO: send errors to ICAP
1247 adaptOrFinalizeReply(); // may write to, abort, or "close" the entry
1250 // kick more reads if needed and/or process the response body, if any
1251 PROF_start(HttpStateData_processReplyBody
);
1252 processReplyBody(); // may call serverComplete()
1253 PROF_stop(HttpStateData_processReplyBody
);
1257 \retval true if we can continue with processing the body or doing ICAP.
1260 HttpStateData::continueAfterParsingHeader()
1262 if (flags
.handling1xx
) {
1263 debugs(11, 5, HERE
<< "wait for 1xx handling");
1264 Must(!flags
.headers_parsed
);
1268 if (!flags
.headers_parsed
&& !eof
) {
1269 debugs(11, 9, "needs more at " << inBuf
.length());
1270 flags
.do_next_read
= true;
1271 /** \retval false If we have not finished parsing the headers and may get more data.
1272 * Schedules more reads to retrieve the missing data.
1274 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1278 /** If we are done with parsing, check for errors */
1280 err_type error
= ERR_NONE
;
1282 if (flags
.headers_parsed
) { // parsed headers, possibly with errors
1283 // check for header parsing errors
1284 if (HttpReply
*vrep
= virginReply()) {
1285 const Http::StatusCode s
= vrep
->sline
.status();
1286 const AnyP::ProtocolVersion
&v
= vrep
->sline
.version
;
1287 if (s
== Http::scInvalidHeader
&& v
!= Http::ProtocolVersion(0,9)) {
1288 debugs(11, DBG_IMPORTANT
, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry
->url() << " AKA " << request
->url
);
1289 error
= ERR_INVALID_RESP
;
1290 } else if (s
== Http::scHeaderTooLarge
) {
1291 fwd
->dontRetry(true);
1292 error
= ERR_TOO_BIG
;
1293 } else if (vrep
->header
.conflictingContentLength()) {
1294 fwd
->dontRetry(true);
1295 error
= ERR_INVALID_RESP
;
1297 return true; // done parsing, got reply, and no error
1300 // parsed headers but got no reply
1301 debugs(11, DBG_IMPORTANT
, "WARNING: HTTP: Invalid Response: No reply at all for " << entry
->url() << " AKA " << request
->url
);
1302 error
= ERR_INVALID_RESP
;
1306 if (inBuf
.length()) {
1307 error
= ERR_INVALID_RESP
;
1308 debugs(11, DBG_IMPORTANT
, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry
->url() << " AKA " << request
->url
);
1310 error
= ERR_ZERO_SIZE_OBJECT
;
1311 debugs(11, (request
->flags
.accelerated
?DBG_IMPORTANT
:2), "WARNING: HTTP: Invalid Response: No object data received for " << entry
->url() << " AKA " << request
->url
);
1315 assert(error
!= ERR_NONE
);
1317 fwd
->fail(new ErrorState(error
, Http::scBadGateway
, fwd
->request
));
1318 flags
.do_next_read
= false;
1320 mustStop("HttpStateData::continueAfterParsingHeader");
1321 return false; // quit on error
1324 /** truncate what we read if we read too much so that writeReplyBody()
1325 writes no more than what we should have read */
1327 HttpStateData::truncateVirginBody()
1329 assert(flags
.headers_parsed
);
1331 HttpReply
*vrep
= virginReply();
1333 if (!vrep
->expectingBody(request
->method
, clen
) || clen
< 0)
1334 return; // no body or a body of unknown size, including chunked
1336 if (payloadSeen
- payloadTruncated
<= clen
)
1337 return; // we did not read too much or already took care of the extras
1339 if (const int64_t extras
= payloadSeen
- payloadTruncated
- clen
) {
1340 // server sent more that the advertised content length
1341 debugs(11, 5, "payloadSeen=" << payloadSeen
<<
1342 " clen=" << clen
<< '/' << vrep
->content_length
<<
1343 " trucated=" << payloadTruncated
<< '+' << extras
);
1345 inBuf
.chop(0, inBuf
.length() - extras
);
1346 payloadTruncated
+= extras
;
1351 * Call this when there is data from the origin server
1352 * which should be sent to either StoreEntry, or to ICAP...
1355 HttpStateData::writeReplyBody()
1357 truncateVirginBody(); // if needed
1358 const char *data
= inBuf
.rawContent();
1359 int len
= inBuf
.length();
1360 addVirginReplyBody(data
, len
);
1365 HttpStateData::decodeAndWriteReplyBody()
1367 const char *data
= NULL
;
1369 bool wasThereAnException
= false;
1370 assert(flags
.chunked
);
1371 assert(httpChunkDecoder
);
1372 SQUID_ENTER_THROWING_CODE();
1375 httpChunkDecoder
->setPayloadBuffer(&decodedData
);
1376 const bool doneParsing
= httpChunkDecoder
->parse(inBuf
);
1377 inBuf
= httpChunkDecoder
->remaining(); // sync buffers after parse
1378 len
= decodedData
.contentSize();
1379 data
=decodedData
.content();
1380 addVirginReplyBody(data
, len
);
1383 flags
.do_next_read
= false;
1385 SQUID_EXIT_THROWING_CODE(wasThereAnException
);
1386 return wasThereAnException
;
1390 * processReplyBody has two purposes:
1391 * 1 - take the reply body data, if any, and put it into either
1392 * the StoreEntry, or give it over to ICAP.
1393 * 2 - see if we made it to the end of the response (persistent
1394 * connections and such)
1397 HttpStateData::processReplyBody()
1399 if (!flags
.headers_parsed
) {
1400 flags
.do_next_read
= true;
1401 maybeReadVirginBody();
1406 debugs(11,5, HERE
<< "adaptationAccessCheckPending=" << adaptationAccessCheckPending
);
1407 if (adaptationAccessCheckPending
)
1413 * At this point the reply headers have been parsed and consumed.
1414 * That means header content has been removed from readBuf and
1415 * it contains only body data.
1417 if (entry
->isAccepting()) {
1418 if (flags
.chunked
) {
1419 if (!decodeAndWriteReplyBody()) {
1420 flags
.do_next_read
= false;
1428 // storing/sending methods like earlier adaptOrFinalizeReply() or
1429 // above writeReplyBody() may release/abort the store entry.
1430 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1431 // TODO: In some cases (e.g., 304), we should keep persistent conn open.
1432 // Detect end-of-reply (and, hence, pool our idle pconn) earlier (ASAP).
1433 abortTransaction("store entry aborted while storing reply");
1436 switch (persistentConnStatus()) {
1437 case INCOMPLETE_MSG
: {
1438 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG from " << serverConnection
);
1439 /* Wait for more data or EOF condition */
1440 AsyncCall::Pointer nil
;
1441 if (flags
.keepalive_broken
) {
1442 commSetConnTimeout(serverConnection
, 10, nil
);
1444 commSetConnTimeout(serverConnection
, Config
.Timeout
.read
, nil
);
1447 flags
.do_next_read
= true;
1451 case COMPLETE_PERSISTENT_MSG
: {
1452 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG from " << serverConnection
);
1454 // TODO: Remove serverConnectionSaved but preserve exception safety.
1456 commUnsetConnTimeout(serverConnection
);
1457 flags
.do_next_read
= false;
1459 comm_remove_close_handler(serverConnection
->fd
, closeHandler
);
1460 closeHandler
= NULL
;
1462 Ip::Address client_addr
; // XXX: Remove as unused. Why was it added?
1463 if (request
->flags
.spoofClientIp
)
1464 client_addr
= request
->client_addr
;
1466 auto serverConnectionSaved
= serverConnection
;
1467 fwd
->unregister(serverConnection
);
1468 serverConnection
= nullptr;
1470 bool ispinned
= false; // TODO: Rename to isOrShouldBePinned
1471 if (request
->flags
.pinned
) {
1473 } else if (request
->flags
.connectionAuth
&& request
->flags
.authSent
) {
1478 if (request
->clientConnectionManager
.valid()) {
1479 CallJobHere1(11, 4, request
->clientConnectionManager
,
1481 notePinnedConnectionBecameIdle
,
1482 ConnStateData::PinnedIdleContext(serverConnectionSaved
, request
));
1484 // must not pool/share ispinned connections, even orphaned ones
1485 serverConnectionSaved
->close();
1488 fwd
->pconnPush(serverConnectionSaved
, request
->url
.host());
1495 case COMPLETE_NONPERSISTENT_MSG
:
1496 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection
);
1501 maybeReadVirginBody();
1505 HttpStateData::mayReadVirginReplyBody() const
1507 // TODO: Be more precise here. For example, if/when reading trailer, we may
1508 // not be doneWithServer() yet, but we should return false. Similarly, we
1509 // could still be writing the request body after receiving the whole reply.
1510 return !doneWithServer();
1514 HttpStateData::maybeReadVirginBody()
1517 if (!Comm::IsConnOpen(serverConnection
) || fd_table
[serverConnection
->fd
].closing())
1520 if (!maybeMakeSpaceAvailable(false))
1523 // XXX: get rid of the do_next_read flag
1524 // check for the proper reasons preventing read(2)
1525 if (!flags
.do_next_read
)
1528 flags
.do_next_read
= false;
1530 // must not already be waiting for read(2) ...
1531 assert(!Comm::MonitorsRead(serverConnection
->fd
));
1533 // wait for read(2) to be possible.
1534 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
1535 AsyncCall::Pointer call
= JobCallback(11, 5, Dialer
, this, HttpStateData::readReply
);
1536 Comm::Read(serverConnection
, call
);
1540 HttpStateData::maybeMakeSpaceAvailable(bool doGrow
)
1542 // how much we are allowed to buffer
1543 const int limitBuffer
= (flags
.headers_parsed
? Config
.readAheadGap
: Config
.maxReplyHeaderSize
);
1545 if (limitBuffer
< 0 || inBuf
.length() >= (SBuf::size_type
)limitBuffer
) {
1546 // when buffer is at or over limit already
1547 debugs(11, 7, "will not read up to " << limitBuffer
<< ". buffer has (" << inBuf
.length() << "/" << inBuf
.spaceSize() << ") from " << serverConnection
);
1548 debugs(11, DBG_DATA
, "buffer has {" << inBuf
<< "}");
1549 // Process next response from buffer
1554 // how much we want to read
1555 const size_t read_size
= calcBufferSpaceToReserve(inBuf
.spaceSize(), (limitBuffer
- inBuf
.length()));
1558 debugs(11, 7, "will not read up to " << read_size
<< " into buffer (" << inBuf
.length() << "/" << inBuf
.spaceSize() << ") from " << serverConnection
);
1562 // just report whether we could grow or not, do not actually do it
1564 return (read_size
>= 2);
1566 // we may need to grow the buffer
1567 inBuf
.reserveSpace(read_size
);
1568 debugs(11, 8, (!flags
.do_next_read
? "will not" : "may") <<
1569 " read up to " << read_size
<< " bytes info buf(" << inBuf
.length() << "/" << inBuf
.spaceSize() <<
1570 ") from " << serverConnection
);
1572 return (inBuf
.spaceSize() >= 2); // only read if there is 1+ bytes of space available
1575 /// called after writing the very last request byte (body, last-chunk, etc)
1577 HttpStateData::wroteLast(const CommIoCbParams
&io
)
1579 debugs(11, 5, HERE
<< serverConnection
<< ": size " << io
.size
<< ": errflag " << io
.flag
<< ".");
1580 #if URL_CHECKSUM_DEBUG
1582 entry
->mem_obj
->checkUrlChecksum();
1585 // XXX: Keep in sync with Client::sentRequestBody().
1586 // TODO: Extract common parts.
1589 fd_bytes(io
.fd
, io
.size
, FD_WRITE
);
1590 statCounter
.server
.all
.kbytes_out
+= io
.size
;
1591 statCounter
.server
.http
.kbytes_out
+= io
.size
;
1594 if (io
.flag
== Comm::ERR_CLOSING
)
1597 // both successful and failed writes affect response times
1598 request
->hier
.notePeerWrite();
1601 ErrorState
*err
= new ErrorState(ERR_WRITE_ERROR
, Http::scBadGateway
, fwd
->request
);
1602 err
->xerrno
= io
.xerrno
;
1605 mustStop("HttpStateData::wroteLast");
1612 /// successfully wrote the entire request (including body, last-chunk, etc.)
1614 HttpStateData::sendComplete()
1617 * Set the read timeout here because it hasn't been set yet.
1618 * We only set the read timeout after the request has been
1619 * fully written to the peer. If we start the timeout
1620 * after connection establishment, then we are likely to hit
1621 * the timeout for POST/PUT requests that have very large
1624 typedef CommCbMemFunT
<HttpStateData
, CommTimeoutCbParams
> TimeoutDialer
;
1625 AsyncCall::Pointer timeoutCall
= JobCallback(11, 5,
1626 TimeoutDialer
, this, HttpStateData::httpTimeout
);
1628 commSetConnTimeout(serverConnection
, Config
.Timeout
.read
, timeoutCall
);
1629 flags
.request_sent
= true;
1633 HttpStateData::closeServer()
1635 debugs(11,5, HERE
<< "closing HTTP server " << serverConnection
<< " this " << this);
1637 if (Comm::IsConnOpen(serverConnection
)) {
1638 fwd
->unregister(serverConnection
);
1639 comm_remove_close_handler(serverConnection
->fd
, closeHandler
);
1640 closeHandler
= NULL
;
1641 serverConnection
->close();
1646 HttpStateData::doneWithServer() const
1648 return !Comm::IsConnOpen(serverConnection
);
1652 * Fixup authentication request headers for special cases
1655 httpFixupAuthentication(HttpRequest
* request
, const HttpHeader
* hdr_in
, HttpHeader
* hdr_out
, const Http::StateFlags
&flags
)
1657 Http::HdrType header
= flags
.originpeer
? Http::HdrType::AUTHORIZATION
: Http::HdrType::PROXY_AUTHORIZATION
;
1659 /* Nothing to do unless we are forwarding to a peer */
1660 if (!request
->flags
.proxying
)
1663 /* Needs to be explicitly enabled */
1664 if (!request
->peer_login
)
1667 /* Maybe already dealt with? */
1668 if (hdr_out
->has(header
))
1671 /* Nothing to do here for PASSTHRU */
1672 if (strcmp(request
->peer_login
, "PASSTHRU") == 0)
1675 /* PROXYPASS is a special case, single-signon to servers with the proxy password (basic only) */
1676 if (flags
.originpeer
&& strcmp(request
->peer_login
, "PROXYPASS") == 0 && hdr_in
->has(Http::HdrType::PROXY_AUTHORIZATION
)) {
1677 const char *auth
= hdr_in
->getStr(Http::HdrType::PROXY_AUTHORIZATION
);
1679 if (auth
&& strncasecmp(auth
, "basic ", 6) == 0) {
1680 hdr_out
->putStr(header
, auth
);
1685 char loginbuf
[base64_encode_len(MAX_LOGIN_SZ
)];
1687 struct base64_encode_ctx ctx
;
1688 base64_encode_init(&ctx
);
1690 /* Special mode to pass the username to the upstream cache */
1691 if (*request
->peer_login
== '*') {
1692 const char *username
= "-";
1694 if (request
->extacl_user
.size())
1695 username
= request
->extacl_user
.termedBuf();
1697 else if (request
->auth_user_request
!= NULL
)
1698 username
= request
->auth_user_request
->username();
1701 blen
= base64_encode_update(&ctx
, loginbuf
, strlen(username
), reinterpret_cast<const uint8_t*>(username
));
1702 blen
+= base64_encode_update(&ctx
, loginbuf
+blen
, strlen(request
->peer_login
+1), reinterpret_cast<const uint8_t*>(request
->peer_login
+1));
1703 blen
+= base64_encode_final(&ctx
, loginbuf
+blen
);
1704 httpHeaderPutStrf(hdr_out
, header
, "Basic %.*s", (int)blen
, loginbuf
);
1708 /* external_acl provided credentials */
1709 if (request
->extacl_user
.size() && request
->extacl_passwd
.size() &&
1710 (strcmp(request
->peer_login
, "PASS") == 0 ||
1711 strcmp(request
->peer_login
, "PROXYPASS") == 0)) {
1713 blen
= base64_encode_update(&ctx
, loginbuf
, request
->extacl_user
.size(), reinterpret_cast<const uint8_t*>(request
->extacl_user
.rawBuf()));
1714 blen
+= base64_encode_update(&ctx
, loginbuf
+blen
, 1, reinterpret_cast<const uint8_t*>(":"));
1715 blen
+= base64_encode_update(&ctx
, loginbuf
+blen
, request
->extacl_passwd
.size(), reinterpret_cast<const uint8_t*>(request
->extacl_passwd
.rawBuf()));
1716 blen
+= base64_encode_final(&ctx
, loginbuf
+blen
);
1717 httpHeaderPutStrf(hdr_out
, header
, "Basic %.*s", (int)blen
, loginbuf
);
1720 // if no external user credentials are available to fake authentication with PASS acts like PASSTHRU
1721 if (strcmp(request
->peer_login
, "PASS") == 0)
1724 /* Kerberos login to peer */
1725 #if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
1726 if (strncmp(request
->peer_login
, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
1728 char *PrincipalName
=NULL
,*p
;
1729 int negotiate_flags
= 0;
1731 if ((p
=strchr(request
->peer_login
,':')) != NULL
) {
1734 if (request
->flags
.auth_no_keytab
) {
1735 negotiate_flags
|= PEER_PROXY_NEGOTIATE_NOKEYTAB
;
1737 Token
= peer_proxy_negotiate_auth(PrincipalName
, request
->peer_host
, negotiate_flags
);
1739 httpHeaderPutStrf(hdr_out
, header
, "Negotiate %s",Token
);
1743 #endif /* HAVE_KRB5 && HAVE_GSSAPI */
1745 blen
= base64_encode_update(&ctx
, loginbuf
, strlen(request
->peer_login
), reinterpret_cast<const uint8_t*>(request
->peer_login
));
1746 blen
+= base64_encode_final(&ctx
, loginbuf
+blen
);
1747 httpHeaderPutStrf(hdr_out
, header
, "Basic %.*s", (int)blen
, loginbuf
);
1752 * build request headers and append them to a given MemBuf
1753 * used by buildRequestPrefix()
1754 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1757 HttpStateData::httpBuildRequestHeader(HttpRequest
* request
,
1759 const AccessLogEntryPointer
&al
,
1760 HttpHeader
* hdr_out
,
1761 const Http::StateFlags
&flags
)
1763 /* building buffer for complex strings */
1764 #define BBUF_SZ (MAX_URL+32)
1765 LOCAL_ARRAY(char, bbuf
, BBUF_SZ
);
1766 LOCAL_ARRAY(char, ntoabuf
, MAX_IPSTRLEN
);
1767 const HttpHeader
*hdr_in
= &request
->header
;
1768 const HttpHeaderEntry
*e
= NULL
;
1769 HttpHeaderPos pos
= HttpHeaderInitPos
;
1770 assert (hdr_out
->owner
== hoRequest
);
1772 /* use our IMS header if the cached entry has Last-Modified time */
1773 if (request
->lastmod
> -1)
1774 hdr_out
->putTime(Http::HdrType::IF_MODIFIED_SINCE
, request
->lastmod
);
1776 // Add our own If-None-Match field if the cached entry has a strong ETag.
1777 // copyOneHeaderFromClientsideRequestToUpstreamRequest() adds client ones.
1778 if (request
->etag
.size() > 0) {
1779 hdr_out
->addEntry(new HttpHeaderEntry(Http::HdrType::IF_NONE_MATCH
, SBuf(),
1780 request
->etag
.termedBuf()));
1783 bool we_do_ranges
= decideIfWeDoRanges (request
);
1785 String
strConnection (hdr_in
->getList(Http::HdrType::CONNECTION
));
1787 while ((e
= hdr_in
->getEntry(&pos
)))
1788 copyOneHeaderFromClientsideRequestToUpstreamRequest(e
, strConnection
, request
, hdr_out
, we_do_ranges
, flags
);
1790 /* Abstraction break: We should interpret multipart/byterange responses
1791 * into offset-length data, and this works around our inability to do so.
1793 if (!we_do_ranges
&& request
->multipartRangeRequest()) {
1794 /* don't cache the result */
1795 request
->flags
.cachable
= false;
1796 /* pretend it's not a range request */
1797 request
->ignoreRange("want to request the whole object");
1798 request
->flags
.isRanged
= false;
1801 hdr_out
->addVia(request
->http_ver
, hdr_in
);
1803 if (request
->flags
.accelerated
) {
1804 /* Append Surrogate-Capabilities */
1805 String
strSurrogate(hdr_in
->getList(Http::HdrType::SURROGATE_CAPABILITY
));
1807 snprintf(bbuf
, BBUF_SZ
, "%s=\"Surrogate/1.0 ESI/1.0\"", Config
.Accel
.surrogate_id
);
1809 snprintf(bbuf
, BBUF_SZ
, "%s=\"Surrogate/1.0\"", Config
.Accel
.surrogate_id
);
1811 strListAdd(&strSurrogate
, bbuf
, ',');
1812 hdr_out
->putStr(Http::HdrType::SURROGATE_CAPABILITY
, strSurrogate
.termedBuf());
1815 /** \pre Handle X-Forwarded-For */
1816 if (strcmp(opt_forwarded_for
, "delete") != 0) {
1818 String strFwd
= hdr_in
->getList(Http::HdrType::X_FORWARDED_FOR
);
1820 // if we cannot double strFwd size, then it grew past 50% of the limit
1821 if (!strFwd
.canGrowBy(strFwd
.size())) {
1822 // There is probably a forwarding loop with Via detection disabled.
1823 // If we do nothing, String will assert on overflow soon.
1824 // TODO: Terminate all transactions with huge XFF?
1827 static int warnedCount
= 0;
1828 if (warnedCount
++ < 100) {
1829 const SBuf
url(entry
? SBuf(entry
->url()) : request
->effectiveRequestUri());
1830 debugs(11, DBG_IMPORTANT
, "Warning: likely forwarding loop with " << url
);
1834 if (strcmp(opt_forwarded_for
, "on") == 0) {
1835 /** If set to ON - append client IP or 'unknown'. */
1836 if ( request
->client_addr
.isNoAddr() )
1837 strListAdd(&strFwd
, "unknown", ',');
1839 strListAdd(&strFwd
, request
->client_addr
.toStr(ntoabuf
, MAX_IPSTRLEN
), ',');
1840 } else if (strcmp(opt_forwarded_for
, "off") == 0) {
1841 /** If set to OFF - append 'unknown'. */
1842 strListAdd(&strFwd
, "unknown", ',');
1843 } else if (strcmp(opt_forwarded_for
, "transparent") == 0) {
1844 /** If set to TRANSPARENT - pass through unchanged. */
1845 } else if (strcmp(opt_forwarded_for
, "truncate") == 0) {
1846 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
1847 if ( request
->client_addr
.isNoAddr() )
1850 strFwd
= request
->client_addr
.toStr(ntoabuf
, MAX_IPSTRLEN
);
1852 if (strFwd
.size() > 0)
1853 hdr_out
->putStr(Http::HdrType::X_FORWARDED_FOR
, strFwd
.termedBuf());
1855 /** If set to DELETE - do not copy through. */
1857 /* append Host if not there already */
1858 if (!hdr_out
->has(Http::HdrType::HOST
)) {
1859 if (request
->peer_domain
) {
1860 hdr_out
->putStr(Http::HdrType::HOST
, request
->peer_domain
);
1862 SBuf authority
= request
->url
.authority();
1863 hdr_out
->putStr(Http::HdrType::HOST
, authority
.c_str());
1867 /* append Authorization if known in URL, not in header and going direct */
1868 if (!hdr_out
->has(Http::HdrType::AUTHORIZATION
)) {
1869 if (!request
->flags
.proxying
&& !request
->url
.userInfo().isEmpty()) {
1870 static char result
[base64_encode_len(MAX_URL
*2)]; // should be big enough for a single URI segment
1871 struct base64_encode_ctx ctx
;
1872 base64_encode_init(&ctx
);
1873 size_t blen
= base64_encode_update(&ctx
, result
, request
->url
.userInfo().length(), reinterpret_cast<const uint8_t*>(request
->url
.userInfo().rawContent()));
1874 blen
+= base64_encode_final(&ctx
, result
+blen
);
1875 result
[blen
] = '\0';
1877 httpHeaderPutStrf(hdr_out
, Http::HdrType::AUTHORIZATION
, "Basic %.*s", (int)blen
, result
);
1881 /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
1882 httpFixupAuthentication(request
, hdr_in
, hdr_out
, flags
);
1884 /* append Cache-Control, add max-age if not there already */
1886 HttpHdrCc
*cc
= hdr_in
->getCc();
1889 cc
= new HttpHdrCc();
1891 #if 0 /* see bug 2330 */
1892 /* Set no-cache if determined needed but not found */
1893 if (request
->flags
.nocache
)
1894 EBIT_SET(cc
->mask
, HttpHdrCcType::CC_NO_CACHE
);
1897 /* Add max-age only without no-cache */
1898 if (!cc
->hasMaxAge() && !cc
->hasNoCache()) {
1899 // XXX: performance regression. c_str() reallocates
1900 SBuf
tmp(request
->effectiveRequestUri());
1901 cc
->maxAge(getMaxAge(entry
? entry
->url() : tmp
.c_str()));
1904 /* Enforce sibling relations */
1905 if (flags
.only_if_cached
)
1906 cc
->onlyIfCached(true);
1913 /* maybe append Connection: keep-alive */
1914 if (flags
.keepalive
) {
1915 hdr_out
->putStr(Http::HdrType::CONNECTION
, "keep-alive");
1918 /* append Front-End-Https */
1919 if (flags
.front_end_https
) {
1920 if (flags
.front_end_https
== 1 || request
->url
.getScheme() == AnyP::PROTO_HTTPS
)
1921 hdr_out
->putStr(Http::HdrType::FRONT_END_HTTPS
, "On");
1924 if (flags
.chunked_request
) {
1925 // Do not just copy the original value so that if the client-side
1926 // starts decode other encodings, this code may remain valid.
1927 hdr_out
->putStr(Http::HdrType::TRANSFER_ENCODING
, "chunked");
1930 /* Now mangle the headers. */
1931 httpHdrMangleList(hdr_out
, request
, al
, ROR_REQUEST
);
1933 strConnection
.clean();
1937 * Decides whether a particular header may be cloned from the received Clients request
1938 * to our outgoing fetch request.
1941 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, const String strConnection
, const HttpRequest
* request
, HttpHeader
* hdr_out
, const int we_do_ranges
, const Http::StateFlags
&flags
)
1943 debugs(11, 5, "httpBuildRequestHeader: " << e
->name
<< ": " << e
->value
);
1947 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
1949 case Http::HdrType::PROXY_AUTHORIZATION
:
1950 /** \par Proxy-Authorization:
1951 * Only pass on proxy authentication to peers for which
1952 * authentication forwarding is explicitly enabled
1954 if (!flags
.originpeer
&& flags
.proxying
&& request
->peer_login
&&
1955 (strcmp(request
->peer_login
, "PASS") == 0 ||
1956 strcmp(request
->peer_login
, "PROXYPASS") == 0 ||
1957 strcmp(request
->peer_login
, "PASSTHRU") == 0)) {
1958 hdr_out
->addEntry(e
->clone());
1962 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
1964 case Http::HdrType::CONNECTION
: /** \par Connection: */
1965 case Http::HdrType::TE
: /** \par TE: */
1966 case Http::HdrType::KEEP_ALIVE
: /** \par Keep-Alive: */
1967 case Http::HdrType::PROXY_AUTHENTICATE
: /** \par Proxy-Authenticate: */
1968 case Http::HdrType::TRAILER
: /** \par Trailer: */
1969 case Http::HdrType::UPGRADE
: /** \par Upgrade: */
1970 case Http::HdrType::TRANSFER_ENCODING
: /** \par Transfer-Encoding: */
1973 /** \par OTHER headers I haven't bothered to track down yet. */
1975 case Http::HdrType::AUTHORIZATION
:
1976 /** \par WWW-Authorization:
1977 * Pass on WWW authentication */
1979 if (!flags
.originpeer
) {
1980 hdr_out
->addEntry(e
->clone());
1982 /** \note In accelerators, only forward authentication if enabled
1983 * (see also httpFixupAuthentication for special cases)
1985 if (request
->peer_login
&&
1986 (strcmp(request
->peer_login
, "PASS") == 0 ||
1987 strcmp(request
->peer_login
, "PASSTHRU") == 0 ||
1988 strcmp(request
->peer_login
, "PROXYPASS") == 0)) {
1989 hdr_out
->addEntry(e
->clone());
1995 case Http::HdrType::HOST
:
1997 * Normally Squid rewrites the Host: header.
1998 * However, there is one case when we don't: If the URL
1999 * went through our redirector and the admin configured
2000 * 'redir_rewrites_host' to be off.
2002 if (request
->peer_domain
)
2003 hdr_out
->putStr(Http::HdrType::HOST
, request
->peer_domain
);
2004 else if (request
->flags
.redirected
&& !Config
.onoff
.redir_rewrites_host
)
2005 hdr_out
->addEntry(e
->clone());
2007 SBuf authority
= request
->url
.authority();
2008 hdr_out
->putStr(Http::HdrType::HOST
, authority
.c_str());
2013 case Http::HdrType::IF_MODIFIED_SINCE
:
2014 /** \par If-Modified-Since:
2015 * append unless we added our own,
2016 * but only if cache_miss_revalidate is enabled, or
2017 * the request is not cacheable, or
2018 * the request contains authentication credentials.
2019 * \note at most one client's If-Modified-Since header can pass through
2021 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2022 if (hdr_out
->has(Http::HdrType::IF_MODIFIED_SINCE
))
2024 else if (Config
.onoff
.cache_miss_revalidate
|| !request
->flags
.cachable
|| request
->flags
.auth
)
2025 hdr_out
->addEntry(e
->clone());
2028 case Http::HdrType::IF_NONE_MATCH
:
2029 /** \par If-None-Match:
2030 * append if the wildcard '*' special case value is present, or
2031 * cache_miss_revalidate is disabled, or
2032 * the request is not cacheable in this proxy, or
2033 * the request contains authentication credentials.
2034 * \note this header lists a set of responses for the server to elide sending. Squid added values are extending that set.
2036 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2037 if (hdr_out
->hasListMember(Http::HdrType::IF_MATCH
, "*", ',') || Config
.onoff
.cache_miss_revalidate
|| !request
->flags
.cachable
|| request
->flags
.auth
)
2038 hdr_out
->addEntry(e
->clone());
2041 case Http::HdrType::MAX_FORWARDS
:
2042 /** \par Max-Forwards:
2043 * pass only on TRACE or OPTIONS requests */
2044 if (request
->method
== Http::METHOD_TRACE
|| request
->method
== Http::METHOD_OPTIONS
) {
2045 const int64_t hops
= e
->getInt64();
2048 hdr_out
->putInt64(Http::HdrType::MAX_FORWARDS
, hops
- 1);
2053 case Http::HdrType::VIA
:
2055 * If Via is disabled then forward any received header as-is.
2056 * Otherwise leave for explicit updated addition later. */
2058 if (!Config
.onoff
.via
)
2059 hdr_out
->addEntry(e
->clone());
2063 case Http::HdrType::RANGE
:
2065 case Http::HdrType::IF_RANGE
:
2067 case Http::HdrType::REQUEST_RANGE
:
2068 /** \par Range:, If-Range:, Request-Range:
2069 * Only pass if we accept ranges */
2071 hdr_out
->addEntry(e
->clone());
2075 case Http::HdrType::PROXY_CONNECTION
: // SHOULD ignore. But doing so breaks things.
2078 case Http::HdrType::CONTENT_LENGTH
:
2079 // pass through unless we chunk; also, keeping this away from default
2080 // prevents request smuggling via Connection: Content-Length tricks
2081 if (!flags
.chunked_request
)
2082 hdr_out
->addEntry(e
->clone());
2085 case Http::HdrType::X_FORWARDED_FOR
:
2087 case Http::HdrType::CACHE_CONTROL
:
2088 /** \par X-Forwarded-For:, Cache-Control:
2089 * handled specially by Squid, so leave off for now.
2090 * append these after the loop if needed */
2093 case Http::HdrType::FRONT_END_HTTPS
:
2094 /** \par Front-End-Https:
2095 * Pass thru only if peer is configured with front-end-https */
2096 if (!flags
.front_end_https
)
2097 hdr_out
->addEntry(e
->clone());
2103 * pass on all other header fields
2104 * which are NOT listed by the special Connection: header. */
2105 if (strConnection
.size()>0 && strListIsMember(&strConnection
, e
->name
, ',')) {
2106 debugs(11, 2, "'" << e
->name
<< "' header cropped by Connection: definition");
2110 hdr_out
->addEntry(e
->clone());
2115 HttpStateData::decideIfWeDoRanges (HttpRequest
* request
)
2118 /* decide if we want to do Ranges ourselves
2119 * and fetch the whole object now)
2120 * We want to handle Ranges ourselves iff
2121 * - we can actually parse client Range specs
2122 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
2123 * - reply will be cachable
2124 * (If the reply will be uncachable we have to throw it away after
2125 * serving this request, so it is better to forward ranges to
2126 * the server and fetch only the requested content)
2129 int64_t roffLimit
= request
->getRangeOffsetLimit();
2131 if (NULL
== request
->range
|| !request
->flags
.cachable
2132 || request
->range
->offsetLimitExceeded(roffLimit
) || request
->flags
.connectionAuth
)
2135 debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
2136 request
->range
<< ", cachable: " <<
2137 request
->flags
.cachable
<< "; we_do_ranges: " << result
);
2142 /* build request prefix and append it to a given MemBuf;
2143 * return the length of the prefix */
2145 HttpStateData::buildRequestPrefix(MemBuf
* mb
)
2147 const int offset
= mb
->size
;
2148 /* Uses a local httpver variable to print the HTTP label
2149 * since the HttpRequest may have an older version label.
2150 * XXX: This could create protocol bugs as the headers sent and
2151 * flow control should all be based on the HttpRequest version
2152 * not the one we are sending. Needs checking.
2154 const AnyP::ProtocolVersion httpver
= Http::ProtocolVersion();
2155 const SBuf
url(_peer
&& !_peer
->options
.originserver
? request
->effectiveRequestUri() : request
->url
.path());
2156 mb
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" %s/%d.%d\r\n",
2157 SQUIDSBUFPRINT(request
->method
.image()),
2158 SQUIDSBUFPRINT(url
),
2159 AnyP::ProtocolType_str
[httpver
.protocol
],
2160 httpver
.major
,httpver
.minor
);
2161 /* build and pack headers */
2163 HttpHeader
hdr(hoRequest
);
2164 httpBuildRequestHeader(request
.getRaw(), entry
, fwd
->al
, &hdr
, flags
);
2166 if (request
->flags
.pinned
&& request
->flags
.connectionAuth
)
2167 request
->flags
.authSent
= true;
2168 else if (hdr
.has(Http::HdrType::AUTHORIZATION
))
2169 request
->flags
.authSent
= true;
2174 /* append header terminator */
2175 mb
->append(crlf
, 2);
2176 return mb
->size
- offset
;
2179 /* This will be called when connect completes. Write request. */
2181 HttpStateData::sendRequest()
2185 debugs(11, 5, HERE
<< serverConnection
<< ", request " << request
<< ", this " << this << ".");
2187 if (!Comm::IsConnOpen(serverConnection
)) {
2188 debugs(11,3, HERE
<< "cannot send request to closing " << serverConnection
);
2189 assert(closeHandler
!= NULL
);
2193 typedef CommCbMemFunT
<HttpStateData
, CommTimeoutCbParams
> TimeoutDialer
;
2194 AsyncCall::Pointer timeoutCall
= JobCallback(11, 5,
2195 TimeoutDialer
, this, HttpStateData::httpTimeout
);
2196 commSetConnTimeout(serverConnection
, Config
.Timeout
.lifetime
, timeoutCall
);
2197 flags
.do_next_read
= true;
2198 maybeReadVirginBody();
2200 if (request
->body_pipe
!= NULL
) {
2201 if (!startRequestBodyFlow()) // register to receive body data
2203 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2204 requestSender
= JobCallback(11,5,
2205 Dialer
, this, HttpStateData::sentRequestBody
);
2207 Must(!flags
.chunked_request
);
2208 // use chunked encoding if we do not know the length
2209 if (request
->content_length
< 0)
2210 flags
.chunked_request
= true;
2212 assert(!requestBodySource
);
2213 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2214 requestSender
= JobCallback(11,5,
2215 Dialer
, this, HttpStateData::wroteLast
);
2218 flags
.originpeer
= (_peer
!= NULL
&& _peer
->options
.originserver
);
2219 flags
.proxying
= (_peer
!= NULL
&& !flags
.originpeer
);
2222 * Is keep-alive okay for all request methods?
2224 if (request
->flags
.mustKeepalive
)
2225 flags
.keepalive
= true;
2226 else if (request
->flags
.pinned
)
2227 flags
.keepalive
= request
->persistent();
2228 else if (!Config
.onoff
.server_pconns
)
2229 flags
.keepalive
= false;
2230 else if (_peer
== NULL
)
2231 flags
.keepalive
= true;
2232 else if (_peer
->stats
.n_keepalives_sent
< 10)
2233 flags
.keepalive
= true;
2234 else if ((double) _peer
->stats
.n_keepalives_recv
/
2235 (double) _peer
->stats
.n_keepalives_sent
> 0.50)
2236 flags
.keepalive
= true;
2239 /*The old code here was
2240 if (neighborType(_peer, request->url) == PEER_SIBLING && ...
2241 which is equivalent to:
2242 if (neighborType(_peer, URL()) == PEER_SIBLING && ...
2244 if (((_peer->type == PEER_MULTICAST && p->options.mcast_siblings) ||
2245 _peer->type == PEER_SIBLINGS ) && _peer->options.allow_miss)
2246 flags.only_if_cached = 1;
2248 But I suppose it was a bug
2250 if (neighborType(_peer
, request
->url
) == PEER_SIBLING
&& !_peer
->options
.allow_miss
)
2251 flags
.only_if_cached
= true;
2253 flags
.front_end_https
= _peer
->front_end_https
;
2257 request
->peer_host
=_peer
?_peer
->host
:NULL
;
2258 buildRequestPrefix(&mb
);
2260 debugs(11, 2, "HTTP Server " << serverConnection
);
2261 debugs(11, 2, "HTTP Server REQUEST:\n---------\n" << mb
.buf
<< "\n----------");
2263 Comm::Write(serverConnection
, &mb
, requestSender
);
2268 HttpStateData::getMoreRequestBody(MemBuf
&buf
)
2270 // parent's implementation can handle the no-encoding case
2271 if (!flags
.chunked_request
)
2272 return Client::getMoreRequestBody(buf
);
2276 Must(requestBodySource
!= NULL
);
2277 if (!requestBodySource
->getMoreData(raw
))
2278 return false; // no request body bytes to chunk yet
2280 // optimization: pre-allocate buffer size that should be enough
2281 const mb_size_t rawDataSize
= raw
.contentSize();
2282 // we may need to send: hex-chunk-size CRLF raw-data CRLF last-chunk
2283 buf
.init(16 + 2 + rawDataSize
+ 2 + 5, raw
.max_capacity
);
2285 buf
.appendf("%x\r\n", static_cast<unsigned int>(rawDataSize
));
2286 buf
.append(raw
.content(), rawDataSize
);
2287 buf
.append("\r\n", 2);
2289 Must(rawDataSize
> 0); // we did not accidently created last-chunk above
2291 // Do not send last-chunk unless we successfully received everything
2292 if (receivedWholeRequestBody
) {
2293 Must(!flags
.sentLastChunk
);
2294 flags
.sentLastChunk
= true;
2295 buf
.append("0\r\n\r\n", 5);
2302 httpStart(FwdState
*fwd
)
2304 debugs(11, 3, fwd
->request
->method
<< ' ' << fwd
->entry
->url());
2305 AsyncJob::Start(new HttpStateData(fwd
));
2309 HttpStateData::start()
2311 if (!sendRequest()) {
2312 debugs(11, 3, "httpStart: aborted");
2313 mustStop("HttpStateData::start failed");
2317 ++ statCounter
.server
.all
.requests
;
2318 ++ statCounter
.server
.http
.requests
;
2321 * We used to set the read timeout here, but not any more.
2322 * Now its set in httpSendComplete() after the full request,
2323 * including request body, has been written to the server.
2327 /// if broken posts are enabled for the request, try to fix and return true
2329 HttpStateData::finishingBrokenPost()
2331 #if USE_HTTP_VIOLATIONS
2332 if (!Config
.accessList
.brokenPosts
) {
2333 debugs(11, 5, HERE
<< "No brokenPosts list");
2337 ACLFilledChecklist
ch(Config
.accessList
.brokenPosts
, originalRequest().getRaw());
2339 ch
.syncAle(originalRequest().getRaw(), nullptr);
2340 if (!ch
.fastCheck().allowed()) {
2341 debugs(11, 5, HERE
<< "didn't match brokenPosts");
2345 if (!Comm::IsConnOpen(serverConnection
)) {
2346 debugs(11, 3, HERE
<< "ignoring broken POST for closed " << serverConnection
);
2347 assert(closeHandler
!= NULL
);
2348 return true; // prevent caller from proceeding as if nothing happened
2351 debugs(11, 3, "finishingBrokenPost: fixing broken POST");
2352 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2353 requestSender
= JobCallback(11,5,
2354 Dialer
, this, HttpStateData::wroteLast
);
2355 Comm::Write(serverConnection
, "\r\n", 2, requestSender
, NULL
);
2359 #endif /* USE_HTTP_VIOLATIONS */
2362 /// if needed, write last-chunk to end the request body and return true
2364 HttpStateData::finishingChunkedRequest()
2366 if (flags
.sentLastChunk
) {
2367 debugs(11, 5, HERE
<< "already sent last-chunk");
2371 Must(receivedWholeRequestBody
); // or we should not be sending last-chunk
2372 flags
.sentLastChunk
= true;
2374 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
2375 requestSender
= JobCallback(11,5, Dialer
, this, HttpStateData::wroteLast
);
2376 Comm::Write(serverConnection
, "0\r\n\r\n", 5, requestSender
, NULL
);
2381 HttpStateData::doneSendingRequestBody()
2383 Client::doneSendingRequestBody();
2384 debugs(11,5, HERE
<< serverConnection
);
2386 // do we need to write something after the last body byte?
2387 if (flags
.chunked_request
&& finishingChunkedRequest())
2389 if (!flags
.chunked_request
&& finishingBrokenPost())
2395 // more origin request body data is available
2397 HttpStateData::handleMoreRequestBodyAvailable()
2399 if (eof
|| !Comm::IsConnOpen(serverConnection
)) {
2400 // XXX: we should check this condition in other callbacks then!
2401 // TODO: Check whether this can actually happen: We should unsubscribe
2402 // as a body consumer when the above condition(s) are detected.
2403 debugs(11, DBG_IMPORTANT
, HERE
<< "Transaction aborted while reading HTTP body");
2407 assert(requestBodySource
!= NULL
);
2409 if (requestBodySource
->buf().hasContent()) {
2410 // XXX: why does not this trigger a debug message on every request?
2412 if (flags
.headers_parsed
&& !flags
.abuse_detected
) {
2413 flags
.abuse_detected
= true;
2414 debugs(11, DBG_IMPORTANT
, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << request
->client_addr
<< "' -> '" << entry
->url() << "'" );
2416 if (virginReply()->sline
.status() == Http::scInvalidHeader
) {
2418 mustStop("HttpStateData::handleMoreRequestBodyAvailable");
2424 HttpStateData::handleMoreRequestBodyAvailable();
2427 // premature end of the request body
2429 HttpStateData::handleRequestBodyProducerAborted()
2431 Client::handleRequestBodyProducerAborted();
2432 if (entry
->isEmpty()) {
2433 debugs(11, 3, "request body aborted: " << serverConnection
);
2434 // We usually get here when ICAP REQMOD aborts during body processing.
2435 // We might also get here if client-side aborts, but then our response
2436 // should not matter because either client-side will provide its own or
2437 // there will be no response at all (e.g., if the the client has left).
2438 ErrorState
*err
= new ErrorState(ERR_ICAP_FAILURE
, Http::scInternalServerError
, fwd
->request
);
2439 err
->detailError(ERR_DETAIL_SRV_REQMOD_REQ_BODY
);
2443 abortTransaction("request body producer aborted");
2446 // called when we wrote request headers(!) or a part of the body
2448 HttpStateData::sentRequestBody(const CommIoCbParams
&io
)
2451 statCounter
.server
.http
.kbytes_out
+= io
.size
;
2453 Client::sentRequestBody(io
);
2457 HttpStateData::abortAll(const char *reason
)
2459 debugs(11,5, HERE
<< "aborting transaction for " << reason
<<
2460 "; " << serverConnection
<< ", this " << this);
2464 HttpStateData::ReuseDecision::ReuseDecision(const StoreEntry
*e
, const Http::StatusCode code
)
2465 : answer(HttpStateData::ReuseDecision::reuseNot
), reason(nullptr), entry(e
), statusCode(code
) {}
2467 HttpStateData::ReuseDecision::Answers
2468 HttpStateData::ReuseDecision::make(const HttpStateData::ReuseDecision::Answers ans
, const char *why
)
2475 std::ostream
&operator <<(std::ostream
&os
, const HttpStateData::ReuseDecision
&d
)
2477 static const char *ReuseMessages
[] = {
2478 "do not cache and do not share", // reuseNot
2479 "cache positively and share", // cachePositively
2480 "cache negatively and share", // cacheNegatively
2481 "do not cache but share" // doNotCacheButShare
2484 assert(d
.answer
>= HttpStateData::ReuseDecision::reuseNot
&&
2485 d
.answer
<= HttpStateData::ReuseDecision::doNotCacheButShare
);
2486 return os
<< ReuseMessages
[d
.answer
] << " because " << d
.reason
<<
2487 "; HTTP status " << d
.statusCode
<< " " << *(d
.entry
);