2 * Copyright (C) 1996-2025 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 73 HTTP Request */
12 #include "AccessLogEntry.h"
13 #include "acl/AclSizeLimit.h"
14 #include "acl/FilledChecklist.h"
15 #include "CachePeer.h"
16 #include "client_side.h"
17 #include "client_side_request.h"
18 #include "dns/LookupDetails.h"
19 #include "Downloader.h"
20 #include "error/Detail.h"
23 #include "http/ContentLengthInterpreter.h"
24 #include "http/one/RequestParser.h"
25 #include "http/Stream.h"
26 #include "HttpHdrCc.h"
27 #include "HttpHeaderRange.h"
28 #include "HttpRequest.h"
29 #include "log/Config.h"
31 #include "sbuf/StringConvert.h"
32 #include "SquidConfig.h"
36 #include "auth/UserRequest.h"
39 #include "adaptation/icap/icap_log.h"
42 HttpRequest::HttpRequest(const MasterXaction::Pointer
&mx
) :
43 Http::Message(hoRequest
),
50 HttpRequest::HttpRequest(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aSchemeImg
, const char *aUrlpath
, const MasterXaction::Pointer
&mx
) :
51 Http::Message(hoRequest
),
55 static unsigned int id
= 1;
56 debugs(93,7, "constructed, this=" << this << " id=" << ++id
);
58 initHTTP(aMethod
, aProtocol
, aSchemeImg
, aUrlpath
);
61 HttpRequest::~HttpRequest()
64 debugs(93,7, "destructed, this=" << this);
68 HttpRequest::initHTTP(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aSchemeImg
, const char *aUrlpath
)
71 url
.setScheme(aProtocol
, aSchemeImg
);
78 method
= Http::METHOD_NONE
;
81 auth_user_request
= nullptr;
83 flags
= RequestFlags();
88 client_addr
.setEmpty();
94 peer_login
= nullptr; // not allocated/deallocated by this class
95 peer_domain
= nullptr; // not allocated/deallocated by this class
96 vary_headers
= SBuf();
97 myportname
= null_string
;
100 extacl_user
= null_string
;
101 extacl_passwd
= null_string
;
103 extacl_log
= null_string
;
104 extacl_message
= null_string
;
105 pstate
= Http::Message::psReadyToParseStartLine
;
106 #if FOLLOW_X_FORWARDED_FOR
107 indirect_client_addr
.setEmpty();
108 #endif /* FOLLOW_X_FORWARDED_FOR */
110 adaptHistory_
= nullptr;
113 icapHistory_
= nullptr;
115 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
116 forcedBodyContinuation
= false;
122 // we used to assert that the pipe is NULL, but now the request only
123 // points to a pipe that is owned and initiated by another object.
126 auth_user_request
= nullptr;
128 vary_headers
.clear();
134 delete cache_control
;
135 cache_control
= nullptr;
150 extacl_passwd
.clean();
154 extacl_message
.clean();
159 adaptHistory_
= nullptr;
162 icapHistory_
= nullptr;
174 HttpRequest::clone() const
176 HttpRequest
*copy
= new HttpRequest(masterXaction
);
177 copy
->method
= method
;
178 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
179 copy
->header
.append(&header
);
180 copy
->hdrCacheInit();
181 copy
->hdr_sz
= hdr_sz
;
182 copy
->http_ver
= http_ver
;
183 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
184 copy
->body_pipe
= body_pipe
;
188 // range handled in hdrCacheInit()
190 copy
->imslen
= imslen
;
191 copy
->hier
= hier
; // Is it safe to copy? Should we?
195 // XXX: what to do with copy->peer_login?
197 copy
->lastmod
= lastmod
;
199 copy
->vary_headers
= vary_headers
;
200 // XXX: what to do with copy->peer_domain?
203 copy
->extacl_log
= extacl_log
;
204 copy
->extacl_message
= extacl_message
;
206 const bool inheritWorked
= copy
->inheritProperties(this);
207 assert(inheritWorked
);
213 HttpRequest::inheritProperties(const Http::Message
*aMsg
)
215 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
219 client_addr
= aReq
->client_addr
;
220 #if FOLLOW_X_FORWARDED_FOR
221 indirect_client_addr
= aReq
->indirect_client_addr
;
223 my_addr
= aReq
->my_addr
;
225 dnsWait
= aReq
->dnsWait
;
228 adaptHistory_
= aReq
->adaptHistory();
231 icapHistory_
= aReq
->icapHistory();
234 // This may be too conservative for the 204 No Content case
235 // may eventually need cloneNullAdaptationImmune() for that.
236 flags
= aReq
->flags
.cloneAdaptationImmune();
240 auth_user_request
= aReq
->auth_user_request
;
241 extacl_user
= aReq
->extacl_user
;
242 extacl_passwd
= aReq
->extacl_passwd
;
245 myportname
= aReq
->myportname
;
247 forcedBodyContinuation
= aReq
->forcedBodyContinuation
;
249 // main property is which connection the request was received on (if any)
250 clientConnectionManager
= aReq
->clientConnectionManager
;
252 downloader
= aReq
->downloader
;
254 theNotes
= aReq
->theNotes
;
256 sources
= aReq
->sources
;
261 * Checks the first line of an HTTP request is valid
262 * currently just checks the request method is present.
264 * NP: Other errors are left for detection later in the parse.
267 HttpRequest::sanityCheckStartLine(const char *buf
, const size_t hdr_len
, Http::StatusCode
*scode
)
269 // content is long enough to possibly hold a reply
270 // 2 being magic size of a 1-byte request method plus space delimiter
272 // this is only a real error if the headers apparently complete.
274 debugs(58, 3, "Too large request header (" << hdr_len
<< " bytes)");
275 *scode
= Http::scInvalidHeader
;
280 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
282 m
.HttpRequestMethodXXX(buf
);
283 if (m
== Http::METHOD_NONE
) {
284 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
285 *scode
= Http::scInvalidHeader
;
293 HttpRequest::parseFirstLine(const char *start
, const char *end
)
295 method
.HttpRequestMethodXXX(start
);
297 if (method
== Http::METHOD_NONE
)
300 // XXX: performance regression, strcspn() over the method bytes a second time.
301 // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start.
302 const char *t
= start
+ strcspn(start
, w_space
);
304 start
= t
+ strspn(t
, w_space
); // skip w_space after method
306 const char *ver
= findTrailingHTTPVersion(start
, end
);
311 while (xisspace(*end
)) // find prev non-space
314 ++end
; // back to space
316 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
317 debugs(73, DBG_IMPORTANT
, "ERROR: parseRequestLine: Invalid HTTP identifier.");
325 if (end
< start
) // missing URI
328 return url
.parse(method
, SBuf(start
, size_t(end
-start
)));
331 /* swaps out request using httpRequestPack */
333 HttpRequest::swapOut(StoreEntry
* e
)
341 /* packs request-line and headers, appends <crlf> terminator */
343 HttpRequest::pack(Packable
* p
) const
346 /* pack request-line */
347 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
348 SQUIDSBUFPRINT(method
.image()), SQUIDSBUFPRINT(url
.path()),
349 http_ver
.major
, http_ver
.minor
);
353 p
->append("\r\n", 2);
357 * A wrapper for debugObj()
360 httpRequestPack(void *obj
, Packable
*p
)
362 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
366 /* returns the length of request line + headers + crlf */
368 HttpRequest::prefixLen() const
370 return method
.image().length() + 1 +
371 url
.path().length() + 1 +
376 /* sync this routine when you update HttpRequest struct */
378 HttpRequest::hdrCacheInit()
380 Http::Message::hdrCacheInit();
383 range
= header
.getRange();
387 Adaptation::Icap::History::Pointer
388 HttpRequest::icapHistory() const
391 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
392 icapHistory_
= new Adaptation::Icap::History();
393 debugs(93,4, "made " << icapHistory_
<< " for " << this);
402 Adaptation::History::Pointer
403 HttpRequest::adaptHistory(bool createIfNone
) const
405 if (!adaptHistory_
&& createIfNone
) {
406 adaptHistory_
= new Adaptation::History();
407 debugs(93,4, "made " << adaptHistory_
<< " for " << this);
410 return adaptHistory_
;
413 Adaptation::History::Pointer
414 HttpRequest::adaptLogHistory() const
416 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
420 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
422 if (!adaptHistory_
) {
423 adaptHistory_
= them
.adaptHistory_
; // may be nil
425 // check that histories did not diverge
426 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
433 HttpRequest::multipartRangeRequest() const
435 return (range
&& range
->specs
.size() > 1);
439 HttpRequest::bodyNibbled() const
441 return body_pipe
!= nullptr && body_pipe
->consumedSize() > 0;
445 HttpRequest::prepForPeering(const CachePeer
&peer
)
447 // XXX: Saving two pointers to memory controlled by an independent object.
448 peer_login
= peer
.login
;
449 peer_domain
= peer
.domain
;
450 flags
.auth_no_keytab
= peer
.options
.auth_no_keytab
;
451 debugs(11, 4, this << " to " << peer
);
455 HttpRequest::prepForDirect()
457 peer_login
= nullptr;
458 peer_domain
= nullptr;
459 flags
.auth_no_keytab
= false;
464 HttpRequest::clearError()
466 debugs(11, 7, "old: " << error
);
471 HttpRequest::packFirstLineInto(Packable
* p
, bool full_uri
) const
473 const SBuf
tmp(full_uri
? effectiveRequestUri() : url
.path());
475 // form HTTP request-line
476 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
477 SQUIDSBUFPRINT(method
.image()),
479 http_ver
.major
, http_ver
.minor
);
483 * Indicate whether or not we would expect an entity-body
484 * along with this request
487 HttpRequest::expectingBody(const HttpRequestMethod
&, int64_t &theSize
) const
489 bool expectBody
= false;
492 * Note: Checks for message validity is in clientIsContentLengthValid().
493 * this just checks if a entity-body is expected based on HTTP message syntax
495 if (header
.chunked()) {
498 } else if (content_length
>= 0) {
500 theSize
= content_length
;
510 * Create a Request from a URL and METHOD.
512 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
513 * If the request cannot be created cleanly, NULL is returned
516 HttpRequest::FromUrl(const SBuf
&url
, const MasterXaction::Pointer
&mx
, const HttpRequestMethod
& method
)
518 std::unique_ptr
<HttpRequest
> req(new HttpRequest(mx
));
519 if (req
->url
.parse(method
, url
)) {
520 req
->method
= method
;
521 return req
.release();
527 HttpRequest::FromUrlXXX(const char * url
, const MasterXaction::Pointer
&mx
, const HttpRequestMethod
& method
)
529 return FromUrl(SBuf(url
), mx
, method
);
533 * Are responses to this request possible cacheable ?
534 * If false then no matter what the response must not be cached.
537 HttpRequest::maybeCacheable()
539 // Intercepted request with Host: header which cannot be trusted.
540 // Because it failed verification, or someone bypassed the security tests
541 // we cannot cache the response for sharing between clients.
542 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
543 if (!flags
.hostVerified
&& (flags
.intercepted
|| flags
.interceptTproxy
))
546 switch (url
.getScheme()) {
547 case AnyP::PROTO_HTTP
:
548 case AnyP::PROTO_HTTPS
:
549 if (!method
.respMaybeCacheable())
552 // RFC 9111 section 5.2.1.5:
553 // "The no-store request directive indicates that a cache MUST NOT
554 // store any part of either this request or any response to it."
556 // NP: refresh_pattern ignore-no-store only applies to response messages
557 // this test is handling request message CC header.
558 if (!flags
.ignoreCc
&& cache_control
&& cache_control
->hasNoStore())
562 //case AnyP::PROTO_FTP:
571 HttpRequest::conditional() const
574 header
.has(Http::HdrType::IF_MATCH
) ||
575 header
.has(Http::HdrType::IF_NONE_MATCH
);
579 HttpRequest::recordLookup(const Dns::LookupDetails
&dns
)
581 if (dns
.wait
>= 0) { // known delay
582 if (dnsWait
>= 0) { // have recorded DNS wait before
583 debugs(78, 7, this << " " << dnsWait
<< " += " << dns
);
586 debugs(78, 7, this << " " << dns
);
593 HttpRequest::getRangeOffsetLimit()
595 /* -2 is the starting value of rangeOffsetLimit.
596 * If it is -2, that means we haven't checked it yet.
597 * Otherwise, return the current value */
598 if (rangeOffsetLimit
!= -2)
599 return rangeOffsetLimit
;
601 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
603 ACLFilledChecklist
ch(nullptr, this);
604 ch
.src_addr
= client_addr
;
605 ch
.my_addr
= my_addr
;
607 for (AclSizeLimit
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
608 /* if there is no ACL list or if the ACLs listed match use this limit value */
609 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
).allowed()) {
610 rangeOffsetLimit
= l
->size
; // may be -1
611 debugs(58, 4, rangeOffsetLimit
);
616 return rangeOffsetLimit
;
620 HttpRequest::ignoreRange(const char *reason
)
623 debugs(73, 3, static_cast<void*>(range
) << " for " << reason
);
627 // Some callers also reset isRanged but it may not be safe for all callers:
628 // isRanged is used to determine whether a weak ETag comparison is allowed,
629 // and that check should not ignore the Range header if it was present.
630 // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we?
634 HttpRequest::canHandle1xx() const
636 // old clients do not support 1xx unless they sent Expect: 100-continue
637 // (we reject all other Http::HdrType::EXPECT values so just check for Http::HdrType::EXPECT)
638 if (http_ver
<= Http::ProtocolVersion(1,0) && !header
.has(Http::HdrType::EXPECT
))
641 // others must support 1xx control messages
646 HttpRequest::checkEntityFraming() const
648 // RFC 7230 section 3.3.1:
650 // A server that receives a request message with a transfer coding it
651 // does not understand SHOULD respond with 501 (Not Implemented).
653 if (header
.unsupportedTe())
654 return Http::scNotImplemented
;
656 // RFC 7230 section 3.3.3 #3 paragraph 3:
657 // Transfer-Encoding overrides Content-Length
658 if (header
.chunked())
661 // RFC 7230 Section 3.3.3 #4:
662 // conflicting Content-Length(s) mean a message framing error
663 if (header
.conflictingContentLength())
664 return Http::scBadRequest
;
666 // HTTP/1.0 requirements differ from HTTP/1.1
667 if (http_ver
<= Http::ProtocolVersion(1,0)) {
668 const auto m
= method
.id();
670 // RFC 1945 section 8.3:
672 // A valid Content-Length is required on all HTTP/1.0 POST requests.
674 // RFC 1945 Appendix D.1.1:
676 // The fundamental difference between the POST and PUT requests is
677 // reflected in the different meaning of the Request-URI.
679 if (m
== Http::METHOD_POST
|| m
== Http::METHOD_PUT
)
680 return (content_length
>= 0 ? Http::scNone
: Http::scLengthRequired
);
682 // RFC 1945 section 7.2:
684 // An entity body is included with a request message only when the
685 // request method calls for one.
687 // section 8.1-2: GET and HEAD do not define ('call for') an entity
688 if (m
== Http::METHOD_GET
|| m
== Http::METHOD_HEAD
)
689 return (content_length
< 0 ? Http::scNone
: Http::scBadRequest
);
690 // appendix D1.1.2-4: DELETE, LINK, UNLINK do not define ('call for') an entity
691 if (m
== Http::METHOD_DELETE
|| m
== Http::METHOD_LINK
|| m
== Http::METHOD_UNLINK
)
692 return (content_length
< 0 ? Http::scNone
: Http::scBadRequest
);
694 // other methods are not defined in RFC 1945
695 // assume they support an (optional) entity
699 // RFC 7230 section 3.3
701 // The presence of a message body in a request is signaled by a
702 // Content-Length or Transfer-Encoding header field. Request message
703 // framing is independent of method semantics, even if the method does
704 // not define any use for a message body.
710 HttpRequest::parseHeader(Http1::Parser
&hp
)
712 Http::ContentLengthInterpreter clen
;
713 return Message::parseHeader(hp
, clen
);
717 HttpRequest::parseHeader(const char *buffer
, const size_t size
)
719 Http::ContentLengthInterpreter clen
;
720 return header
.parse(buffer
, size
, clen
);
724 HttpRequest::pinnedConnection()
726 if (clientConnectionManager
.valid() && clientConnectionManager
->pinning
.pinned
)
727 return clientConnectionManager
.get();
732 HttpRequest::storeId()
734 if (store_id
.size() != 0) {
735 debugs(73, 3, "sent back store_id: " << store_id
);
736 return StringToSBuf(store_id
);
738 debugs(73, 3, "sent back effectiveRequestUrl: " << effectiveRequestUri());
739 return effectiveRequestUri();
743 HttpRequest::effectiveRequestUri() const
745 if (method
.id() == Http::METHOD_CONNECT
|| url
.getScheme() == AnyP::PROTO_AUTHORITY_FORM
)
746 return url
.authority(true); // host:port
747 return url
.absolute();
754 theNotes
= new NotePairs
;
759 UpdateRequestNotes(ConnStateData
*csd
, HttpRequest
&request
, NotePairs
const &helperNotes
)
761 // Tag client connection if the helper responded with clt_conn_tag=tag.
762 const char *cltTag
= "clt_conn_tag";
763 if (const char *connTag
= helperNotes
.findFirst(cltTag
)) {
765 csd
->notes()->remove(cltTag
);
766 csd
->notes()->add(cltTag
, connTag
);
769 request
.notes()->replaceOrAdd(&helperNotes
);
773 HttpRequest::manager(const CbcPointer
<ConnStateData
> &aMgr
, const AccessLogEntryPointer
&al
)
775 clientConnectionManager
= aMgr
;
777 if (!clientConnectionManager
.valid())
780 AnyP::PortCfgPointer port
= clientConnectionManager
->port
;
782 myportname
= port
->name
;
783 flags
.ignoreCc
= port
->ignore_cc
;
786 if (auto clientConnection
= clientConnectionManager
->clientConnection
) {
787 client_addr
= clientConnection
->remote
; // XXX: remove request->client_addr member.
788 #if FOLLOW_X_FORWARDED_FOR
789 // indirect client gets stored here because it is an HTTP header result (from X-Forwarded-For:)
790 // not details about the TCP connection itself
791 indirect_client_addr
= clientConnection
->remote
;
792 #endif /* FOLLOW_X_FORWARDED_FOR */
793 my_addr
= clientConnection
->local
;
795 flags
.intercepted
= ((clientConnection
->flags
& COMM_INTERCEPTION
) != 0);
796 flags
.interceptTproxy
= ((clientConnection
->flags
& COMM_TRANSPARENT
) != 0 ) ;
797 const bool proxyProtocolPort
= port
? port
->flags
.proxySurrogate
: false;
798 if (flags
.interceptTproxy
&& !proxyProtocolPort
) {
799 if (Config
.accessList
.spoof_client_ip
) {
800 ACLFilledChecklist
checklist(Config
.accessList
.spoof_client_ip
, this);
802 checklist
.syncAle(this, nullptr);
803 flags
.spoofClientIp
= checklist
.fastCheck().allowed();
805 flags
.spoofClientIp
= true;
807 flags
.spoofClientIp
= false;
812 HttpRequest::canonicalCleanUrl() const
814 return urlCanonicalCleanWithoutRequest(effectiveRequestUri(), method
, url
.getScheme());
817 /// a helper for handling PortCfg cases of FindListeningPortAddress()
818 template <typename Filter
>
819 static const Ip::Address
*
820 FindGoodListeningPortAddressInPort(const AnyP::PortCfgPointer
&port
, const Filter isGood
)
822 return (port
&& isGood(port
->s
)) ? &port
->s
: nullptr;
825 /// a helper for handling Connection cases of FindListeningPortAddress()
826 template <typename Filter
>
827 static const Ip::Address
*
828 FindGoodListeningPortAddressInConn(const Comm::ConnectionPointer
&conn
, const Filter isGood
)
830 return (conn
&& isGood(conn
->local
)) ? &conn
->local
: nullptr;
833 template <typename Filter
>
835 FindGoodListeningPortAddress(const HttpRequest
*callerRequest
, const AccessLogEntry
*ale
, const Filter filter
)
837 // Check all sources of usable listening port information, giving
838 // HttpRequest and masterXaction a preference over ALE.
840 const HttpRequest
*request
= callerRequest
;
842 request
= ale
->request
;
844 return nullptr; // not enough information
846 auto ip
= FindGoodListeningPortAddressInPort(request
->masterXaction
->squidPort
, filter
);
848 ip
= FindGoodListeningPortAddressInPort(ale
->cache
.port
, filter
);
850 // XXX: also handle PROXY protocol here when we have a flag to identify such request
851 if (ip
|| request
->flags
.interceptTproxy
|| request
->flags
.intercepted
)
854 /* handle non-intercepted cases that were not handled above */
855 ip
= FindGoodListeningPortAddressInConn(request
->masterXaction
->tcpClient
, filter
);
857 ip
= FindGoodListeningPortAddressInConn(ale
->tcpClient
, filter
);
858 return ip
; // may still be nil
862 FindListeningPortAddress(const HttpRequest
*callerRequest
, const AccessLogEntry
*ale
)
864 return FindGoodListeningPortAddress(callerRequest
, ale
, [](const Ip::Address
&address
) {
865 // FindListeningPortAddress() callers do not want INADDR_ANY addresses
866 return !address
.isAnyAddr();
871 FindListeningPortNumber(const HttpRequest
*callerRequest
, const AccessLogEntry
*ale
)
873 const auto ip
= FindGoodListeningPortAddress(callerRequest
, ale
, [](const Ip::Address
&address
) {
874 return address
.port() > 0;
880 Assure(ip
->port() > 0);