]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpRequest.cc
Use RegisteredRunners for WCCP (de)activation (#2104)
[thirdparty/squid.git] / src / HttpRequest.cc
CommitLineData
99edd1c3 1/*
1f7b830e 2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
99edd1c3 3 *
bbc27441
AJ
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.
99edd1c3 7 */
8
bbc27441
AJ
9/* DEBUG: section 73 HTTP Request */
10
582c2af2 11#include "squid.h"
f4698e0b 12#include "AccessLogEntry.h"
1328cfb7 13#include "acl/AclSizeLimit.h"
582c2af2 14#include "acl/FilledChecklist.h"
f5e17947 15#include "CachePeer.h"
582c2af2 16#include "client_side.h"
cd4a5c60 17#include "client_side_request.h"
4a3b98d7 18#include "dns/LookupDetails.h"
cda7024f 19#include "Downloader.h"
83b053a0 20#include "error/Detail.h"
67679543 21#include "globals.h"
5c0c642e 22#include "http.h"
4f1c93a7 23#include "http/ContentLengthInterpreter.h"
c99510dd 24#include "http/one/RequestParser.h"
d3dddfb5 25#include "http/Stream.h"
7ebe76de 26#include "HttpHdrCc.h"
528b2c61 27#include "HttpHeaderRange.h"
582c2af2 28#include "HttpRequest.h"
38e16f92 29#include "log/Config.h"
0eb49b6d 30#include "MemBuf.h"
ac38abee 31#include "sbuf/StringConvert.h"
4d5904f7 32#include "SquidConfig.h"
25b6a907 33#include "Store.h"
582c2af2
FC
34
35#if USE_AUTH
36#include "auth/UserRequest.h"
37#endif
3ff65596
AR
38#if ICAP_CLIENT
39#include "adaptation/icap/icap_log.h"
40#endif
528b2c61 41
5ceaee75
CT
42HttpRequest::HttpRequest(const MasterXaction::Pointer &mx) :
43 Http::Message(hoRequest),
44 masterXaction(mx)
75faaa7a 45{
5ceaee75 46 assert(mx);
5cafad19 47 init();
48}
49
5ceaee75
CT
50HttpRequest::HttpRequest(const HttpRequestMethod& aMethod, AnyP::ProtocolType aProtocol, const char *aSchemeImg, const char *aUrlpath, const MasterXaction::Pointer &mx) :
51 Http::Message(hoRequest),
52 masterXaction(mx)
5cafad19 53{
5ceaee75 54 assert(mx);
5dfc3258 55 static unsigned int id = 1;
bf95c10a 56 debugs(93,7, "constructed, this=" << this << " id=" << ++id);
5cafad19 57 init();
d31d59d8 58 initHTTP(aMethod, aProtocol, aSchemeImg, aUrlpath);
5cafad19 59}
60
61HttpRequest::~HttpRequest()
62{
5cafad19 63 clean();
bf95c10a 64 debugs(93,7, "destructed, this=" << this);
5cafad19 65}
66
67void
d31d59d8 68HttpRequest::initHTTP(const HttpRequestMethod& aMethod, AnyP::ProtocolType aProtocol, const char *aSchemeImg, const char *aUrlpath)
5cafad19 69{
70 method = aMethod;
d31d59d8 71 url.setScheme(aProtocol, aSchemeImg);
51b5dcf5 72 url.path(aUrlpath);
5cafad19 73}
74
75void
76HttpRequest::init()
77{
c2a7cefd 78 method = Http::METHOD_NONE;
4e3f4dc7 79 url.clear();
2f1431ea 80#if USE_AUTH
aee3523a 81 auth_user_request = nullptr;
2f1431ea 82#endif
b56b37cf 83 flags = RequestFlags();
aee3523a 84 range = nullptr;
a3f28359 85 ims = -1;
86 imslen = 0;
5cafad19 87 lastmod = -1;
4dd643d5
AJ
88 client_addr.setEmpty();
89 my_addr.setEmpty();
aee3523a 90 body_pipe = nullptr;
a3f28359 91 // hier
3ff65596 92 dnsWait = -1;
83b053a0 93 error.clear();
aee3523a
AR
94 peer_login = nullptr; // not allocated/deallocated by this class
95 peer_domain = nullptr; // not allocated/deallocated by this class
90ab8f20 96 vary_headers = SBuf();
35fb56c9 97 myportname = null_string;
5cafad19 98 tag = null_string;
2f1431ea 99#if USE_AUTH
5cafad19 100 extacl_user = null_string;
101 extacl_passwd = null_string;
2f1431ea 102#endif
5cafad19 103 extacl_log = null_string;
8c93a598 104 extacl_message = null_string;
fb654382 105 pstate = Http::Message::psReadyToParseStartLine;
3d674977 106#if FOLLOW_X_FORWARDED_FOR
4dd643d5 107 indirect_client_addr.setEmpty();
3d674977 108#endif /* FOLLOW_X_FORWARDED_FOR */
3ff65596 109#if USE_ADAPTATION
aee3523a 110 adaptHistory_ = nullptr;
3ff65596
AR
111#endif
112#if ICAP_CLIENT
aee3523a 113 icapHistory_ = nullptr;
3ff65596 114#endif
11e3fa1c 115 rangeOffsetLimit = -2; //a value of -2 means not checked yet
ec69bdb2 116 forcedBodyContinuation = false;
8596962e 117}
118
99edd1c3 119void
5cafad19 120HttpRequest::clean()
99edd1c3 121{
9e008dda 122 // we used to assert that the pipe is NULL, but now the request only
5f8252d2 123 // points to a pipe that is owned and initiated by another object.
aee3523a 124 body_pipe = nullptr;
2f1431ea 125#if USE_AUTH
aee3523a 126 auth_user_request = nullptr;
2f1431ea 127#endif
90ab8f20 128 vary_headers.clear();
4e3f4dc7 129 url.clear();
62e76326 130
519e0948 131 header.clean();
62e76326 132
07947ad8 133 if (cache_control) {
3d7782c1 134 delete cache_control;
aee3523a 135 cache_control = nullptr;
07947ad8 136 }
62e76326 137
5cafad19 138 if (range) {
8596962e 139 delete range;
aee3523a 140 range = nullptr;
5cafad19 141 }
62e76326 142
35fb56c9
AJ
143 myportname.clean();
144
75d47340 145 theNotes = nullptr;
d06e17ea 146
30abd221 147 tag.clean();
2f1431ea 148#if USE_AUTH
30abd221 149 extacl_user.clean();
30abd221 150 extacl_passwd.clean();
2f1431ea 151#endif
30abd221 152 extacl_log.clean();
3ff65596 153
8c93a598
HN
154 extacl_message.clean();
155
46017fdd
CT
156 etag.clean();
157
3ff65596 158#if USE_ADAPTATION
aee3523a 159 adaptHistory_ = nullptr;
3ff65596
AR
160#endif
161#if ICAP_CLIENT
aee3523a 162 icapHistory_ = nullptr;
3ff65596 163#endif
8596962e 164}
4a972fa2 165
5cafad19 166void
167HttpRequest::reset()
168{
169 clean();
170 init();
171}
172
fa0e6114
AR
173HttpRequest *
174HttpRequest::clone() const
175{
5ceaee75 176 HttpRequest *copy = new HttpRequest(masterXaction);
51b5dcf5 177 copy->method = method;
fa0e6114
AR
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;
185
d31d59d8 186 copy->url = url;
fa0e6114 187
66363092 188 // range handled in hdrCacheInit()
9e008dda
AJ
189 copy->ims = ims;
190 copy->imslen = imslen;
fa0e6114
AR
191 copy->hier = hier; // Is it safe to copy? Should we?
192
83b053a0 193 copy->error = error;
fa0e6114
AR
194
195 // XXX: what to do with copy->peer_login?
196
9e008dda 197 copy->lastmod = lastmod;
46017fdd 198 copy->etag = etag;
90ab8f20 199 copy->vary_headers = vary_headers;
fa0e6114
AR
200 // XXX: what to do with copy->peer_domain?
201
202 copy->tag = tag;
fa0e6114 203 copy->extacl_log = extacl_log;
8c93a598 204 copy->extacl_message = extacl_message;
fa0e6114 205
fbc1c5a6
AJ
206 const bool inheritWorked = copy->inheritProperties(this);
207 assert(inheritWorked);
3ff65596 208
fa0e6114
AR
209 return copy;
210}
211
d5964067 212bool
63df1d28 213HttpRequest::inheritProperties(const Http::Message *aMsg)
d5964067
AJ
214{
215 const HttpRequest* aReq = dynamic_cast<const HttpRequest*>(aMsg);
216 if (!aReq)
217 return false;
218
219 client_addr = aReq->client_addr;
220#if FOLLOW_X_FORWARDED_FOR
221 indirect_client_addr = aReq->indirect_client_addr;
d5964067
AJ
222#endif
223 my_addr = aReq->my_addr;
224
225 dnsWait = aReq->dnsWait;
226
227#if USE_ADAPTATION
228 adaptHistory_ = aReq->adaptHistory();
229#endif
230#if ICAP_CLIENT
231 icapHistory_ = aReq->icapHistory();
232#endif
233
234 // This may be too conservative for the 204 No Content case
235 // may eventually need cloneNullAdaptationImmune() for that.
236 flags = aReq->flags.cloneAdaptationImmune();
237
83b053a0 238 error = aReq->error;
d5964067
AJ
239#if USE_AUTH
240 auth_user_request = aReq->auth_user_request;
f4f6ab06
AJ
241 extacl_user = aReq->extacl_user;
242 extacl_passwd = aReq->extacl_passwd;
d5964067 243#endif
983983ce 244
20fb3571
AJ
245 myportname = aReq->myportname;
246
ec69bdb2
CT
247 forcedBodyContinuation = aReq->forcedBodyContinuation;
248
983983ce 249 // main property is which connection the request was received on (if any)
d5964067 250 clientConnectionManager = aReq->clientConnectionManager;
f4698e0b 251
cda7024f
CT
252 downloader = aReq->downloader;
253
75d47340 254 theNotes = aReq->theNotes;
88df846b
CT
255
256 sources = aReq->sources;
d5964067
AJ
257 return true;
258}
259
96ee497f
AJ
260/**
261 * Checks the first line of an HTTP request is valid
262 * currently just checks the request method is present.
263 *
264 * NP: Other errors are left for detection later in the parse.
265 */
c21ad0f5 266bool
83b053a0 267HttpRequest::sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *scode)
8596962e 268{
96ee497f
AJ
269 // content is long enough to possibly hold a reply
270 // 2 being magic size of a 1-byte request method plus space delimiter
84ae6223 271 if (hdr_len < 2) {
2f8abb64 272 // this is only a real error if the headers apparently complete.
96ee497f 273 if (hdr_len > 0) {
bf95c10a 274 debugs(58, 3, "Too large request header (" << hdr_len << " bytes)");
83b053a0 275 *scode = Http::scInvalidHeader;
96ee497f
AJ
276 }
277 return false;
278 }
8596962e 279
f9688132
AJ
280 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
281 HttpRequestMethod m;
84ae6223 282 m.HttpRequestMethodXXX(buf);
f9688132 283 if (m == Http::METHOD_NONE) {
bf8fe701 284 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
83b053a0 285 *scode = Http::scInvalidHeader;
8596962e 286 return false;
287 }
288
289 return true;
290}
291
c21ad0f5 292bool
293HttpRequest::parseFirstLine(const char *start, const char *end)
8596962e 294{
f9688132 295 method.HttpRequestMethodXXX(start);
863d85ee 296
c2a7cefd 297 if (method == Http::METHOD_NONE)
863d85ee 298 return false;
299
f9688132
AJ
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);
303
304 start = t + strspn(t, w_space); // skip w_space after method
863d85ee 305
306 const char *ver = findTrailingHTTPVersion(start, end);
307
308 if (ver) {
309 end = ver - 1;
310
311 while (xisspace(*end)) // find prev non-space
5e263176 312 --end;
863d85ee 313
95dc7ff4 314 ++end; // back to space
863d85ee 315
316 if (2 != sscanf(ver + 5, "%d.%d", &http_ver.major, &http_ver.minor)) {
d816f28d 317 debugs(73, DBG_IMPORTANT, "ERROR: parseRequestLine: Invalid HTTP identifier.");
863d85ee 318 return false;
319 }
320 } else {
321 http_ver.major = 0;
322 http_ver.minor = 9;
323 }
324
325 if (end < start) // missing URI
326 return false;
327
6c880a16 328 return url.parse(method, SBuf(start, size_t(end-start)));
99edd1c3 329}
330
a00a7c85 331/* swaps out request using httpRequestPack */
99edd1c3 332void
5cafad19 333HttpRequest::swapOut(StoreEntry * e)
99edd1c3 334{
5cafad19 335 assert(e);
15179984
AJ
336 e->buffer();
337 pack(e);
691ad095 338 e->flush();
a00a7c85 339}
340
341/* packs request-line and headers, appends <crlf> terminator */
342void
1f28a150 343HttpRequest::pack(Packable * p) const
a00a7c85 344{
5cafad19 345 assert(p);
a00a7c85 346 /* pack request-line */
51b5dcf5
AJ
347 p->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " HTTP/%d.%d\r\n",
348 SQUIDSBUFPRINT(method.image()), SQUIDSBUFPRINT(url.path()),
4391cd15 349 http_ver.major, http_ver.minor);
a00a7c85 350 /* headers */
a9925b40 351 header.packInto(p);
2246b732 352 /* trailer */
785b508d 353 p->append("\r\n", 2);
2246b732 354}
355
5cafad19 356/*
357 * A wrapper for debugObj()
358 */
2246b732 359void
17802cf1 360httpRequestPack(void *obj, Packable *p)
2246b732 361{
5cafad19 362 HttpRequest *request = static_cast<HttpRequest*>(obj);
363 request->pack(p);
99edd1c3 364}
eeb423fb 365
2246b732 366/* returns the length of request line + headers + crlf */
367int
51b5dcf5 368HttpRequest::prefixLen() const
2246b732 369{
7f06a3d8 370 return method.image().length() + 1 +
51b5dcf5 371 url.path().length() + 1 +
62e76326 372 4 + 1 + 3 + 2 +
5cafad19 373 header.len + 2;
2246b732 374}
99edd1c3 375
190154cf 376/* sync this routine when you update HttpRequest struct */
8596962e 377void
07947ad8 378HttpRequest::hdrCacheInit()
528b2c61 379{
63df1d28 380 Http::Message::hdrCacheInit();
62e76326 381
66363092 382 assert(!range);
a9925b40 383 range = header.getRange();
528b2c61 384}
385
3ff65596 386#if ICAP_CLIENT
e1381638 387Adaptation::Icap::History::Pointer
3ff65596
AR
388HttpRequest::icapHistory() const
389{
390 if (!icapHistory_) {
38e16f92 391 if (Log::TheConfig.hasIcapToken || IcapLogfileStatus == LOG_ENABLE) {
3ff65596 392 icapHistory_ = new Adaptation::Icap::History();
bf95c10a 393 debugs(93,4, "made " << icapHistory_ << " for " << this);
3ff65596
AR
394 }
395 }
396
397 return icapHistory_;
398}
399#endif
400
401#if USE_ADAPTATION
e1381638 402Adaptation::History::Pointer
a22e6cd3 403HttpRequest::adaptHistory(bool createIfNone) const
3ff65596 404{
a22e6cd3
AR
405 if (!adaptHistory_ && createIfNone) {
406 adaptHistory_ = new Adaptation::History();
bf95c10a 407 debugs(93,4, "made " << adaptHistory_ << " for " << this);
3ff65596
AR
408 }
409
410 return adaptHistory_;
411}
a22e6cd3 412
e1381638 413Adaptation::History::Pointer
a22e6cd3
AR
414HttpRequest::adaptLogHistory() const
415{
38e16f92 416 return HttpRequest::adaptHistory(Log::TheConfig.hasAdaptToken);
a22e6cd3
AR
417}
418
aaf0559d
AR
419void
420HttpRequest::adaptHistoryImport(const HttpRequest &them)
421{
422 if (!adaptHistory_) {
423 adaptHistory_ = them.adaptHistory_; // may be nil
424 } else {
425 // check that histories did not diverge
426 Must(!them.adaptHistory_ || them.adaptHistory_ == adaptHistory_);
427 }
428}
429
3ff65596
AR
430#endif
431
528b2c61 432bool
190154cf 433HttpRequest::multipartRangeRequest() const
528b2c61 434{
9ad511b6 435 return (range && range->specs.size() > 1);
528b2c61 436}
8000a965 437
58217e94 438bool
9e008dda
AJ
439HttpRequest::bodyNibbled() const
440{
aee3523a 441 return body_pipe != nullptr && body_pipe->consumedSize() > 0;
58217e94 442}
5f8252d2 443
f5e17947
CT
444void
445HttpRequest::prepForPeering(const CachePeer &peer)
446{
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;
a555a85b 451 debugs(11, 4, this << " to " << peer);
f5e17947
CT
452}
453
454void
455HttpRequest::prepForDirect()
456{
457 peer_login = nullptr;
458 peer_domain = nullptr;
459 flags.auth_no_keytab = false;
460 debugs(11, 4, this);
461}
462
129fe2a1
CT
463void
464HttpRequest::clearError()
465{
83b053a0
CT
466 debugs(11, 7, "old: " << error);
467 error.clear();
129fe2a1
CT
468}
469
51b5dcf5
AJ
470void
471HttpRequest::packFirstLineInto(Packable * p, bool full_uri) const
8596962e 472{
851feda6 473 const SBuf tmp(full_uri ? effectiveRequestUri() : url.path());
8596962e 474
8596962e 475 // form HTTP request-line
51b5dcf5 476 p->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " HTTP/%d.%d\r\n",
4391cd15 477 SQUIDSBUFPRINT(method.image()),
51b5dcf5 478 SQUIDSBUFPRINT(tmp),
4391cd15 479 http_ver.major, http_ver.minor);
8596962e 480}
8ddea643 481
482/*
684e9c80 483 * Indicate whether or not we would expect an entity-body
8ddea643 484 * along with this request
485 */
486bool
ced8def3 487HttpRequest::expectingBody(const HttpRequestMethod &, int64_t &theSize) const
8ddea643 488{
489 bool expectBody = false;
490
491 /*
684e9c80
HN
492 * Note: Checks for message validity is in clientIsContentLengthValid().
493 * this just checks if a entity-body is expected based on HTTP message syntax
8ddea643 494 */
684e9c80 495 if (header.chunked()) {
8ddea643 496 expectBody = true;
684e9c80
HN
497 theSize = -1;
498 } else if (content_length >= 0) {
8ddea643 499 expectBody = true;
684e9c80
HN
500 theSize = content_length;
501 } else {
8ddea643 502 expectBody = false;
684e9c80 503 // theSize undefined
8ddea643 504 }
505
506 return expectBody;
507}
c21ad0f5 508
509/*
510 * Create a Request from a URL and METHOD.
511 *
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
514 */
515HttpRequest *
6c880a16 516HttpRequest::FromUrl(const SBuf &url, const MasterXaction::Pointer &mx, const HttpRequestMethod& method)
c21ad0f5 517{
5ceaee75 518 std::unique_ptr<HttpRequest> req(new HttpRequest(mx));
91489e45
AJ
519 if (req->url.parse(method, url)) {
520 req->method = method;
9157915c 521 return req.release();
91489e45 522 }
9157915c 523 return nullptr;
c21ad0f5 524}
525
6c880a16
AJ
526HttpRequest *
527HttpRequest::FromUrlXXX(const char * url, const MasterXaction::Pointer &mx, const HttpRequestMethod& method)
528{
529 return FromUrl(SBuf(url), mx, method);
530}
531
c2a7cefd 532/**
610ee341 533 * Are responses to this request possible cacheable ?
534 * If false then no matter what the response must not be cached.
535 */
536bool
c2a7cefd 537HttpRequest::maybeCacheable()
610ee341 538{
2962f8b8
AJ
539 // Intercepted request with Host: header which cannot be trusted.
540 // Because it failed verification, or someone bypassed the security tests
2f8abb64 541 // we cannot cache the response for sharing between clients.
2962f8b8 542 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
0d901ef4 543 if (!flags.hostVerified && (flags.intercepted || flags.interceptTproxy))
2962f8b8
AJ
544 return false;
545
4e3f4dc7 546 switch (url.getScheme()) {
c2a7cefd 547 case AnyP::PROTO_HTTP:
2c32c73e 548 case AnyP::PROTO_HTTPS:
c2a7cefd
AJ
549 if (!method.respMaybeCacheable())
550 return false;
9e008dda 551
0ac163be
AJ
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."
38041f53
GD
555 //
556 // NP: refresh_pattern ignore-no-store only applies to response messages
557 // this test is handling request message CC header.
810d879f 558 if (!flags.ignoreCc && cache_control && cache_control->hasNoStore())
38041f53 559 return false;
c2a7cefd 560 break;
610ee341 561
f53969cc 562 //case AnyP::PROTO_FTP:
c2a7cefd
AJ
563 default:
564 break;
565 }
566
60745f24 567 return true;
610ee341 568}
d67acb4e 569
79c8035e
AR
570bool
571HttpRequest::conditional() const
572{
45e5102d 573 return flags.ims ||
789217a2
FC
574 header.has(Http::HdrType::IF_MATCH) ||
575 header.has(Http::HdrType::IF_NONE_MATCH);
79c8035e
AR
576}
577
d5964067 578void
4a3b98d7 579HttpRequest::recordLookup(const Dns::LookupDetails &dns)
3ff65596
AR
580{
581 if (dns.wait >= 0) { // known delay
fd9c47d1
AR
582 if (dnsWait >= 0) { // have recorded DNS wait before
583 debugs(78, 7, this << " " << dnsWait << " += " << dns);
3ff65596 584 dnsWait += dns.wait;
fd9c47d1
AR
585 } else {
586 debugs(78, 7, this << " " << dns);
3ff65596 587 dnsWait = dns.wait;
fd9c47d1 588 }
3ff65596
AR
589 }
590}
11e3fa1c
AJ
591
592int64_t
593HttpRequest::getRangeOffsetLimit()
594{
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 */
5e5f247c 598 if (rangeOffsetLimit != -2)
11e3fa1c
AJ
599 return rangeOffsetLimit;
600
601 rangeOffsetLimit = 0; // default value for rangeOffsetLimit
602
e94ff527 603 ACLFilledChecklist ch(nullptr, this);
11e3fa1c
AJ
604 ch.src_addr = client_addr;
605 ch.my_addr = my_addr;
606
1328cfb7 607 for (AclSizeLimit *l = Config.rangeOffsetLimit; l; l = l -> next) {
11e3fa1c 608 /* if there is no ACL list or if the ACLs listed match use this limit value */
06bf5384 609 if (!l->aclList || ch.fastCheck(l->aclList).allowed()) {
11e3fa1c 610 rangeOffsetLimit = l->size; // may be -1
3755c7a5 611 debugs(58, 4, rangeOffsetLimit);
11e3fa1c
AJ
612 break;
613 }
614 }
615
616 return rangeOffsetLimit;
617}
655daa06 618
f0baf149
AR
619void
620HttpRequest::ignoreRange(const char *reason)
621{
622 if (range) {
623 debugs(73, 3, static_cast<void*>(range) << " for " << reason);
624 delete range;
aee3523a 625 range = nullptr;
f0baf149
AR
626 }
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?
631}
632
655daa06
AR
633bool
634HttpRequest::canHandle1xx() const
635{
636 // old clients do not support 1xx unless they sent Expect: 100-continue
789217a2
FC
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))
655daa06
AR
639 return false;
640
641 // others must support 1xx control messages
642 return true;
643}
582c2af2 644
4bd88eb4
AJ
645Http::StatusCode
646HttpRequest::checkEntityFraming() const
647{
648 // RFC 7230 section 3.3.1:
649 // "
650 // A server that receives a request message with a transfer coding it
651 // does not understand SHOULD respond with 501 (Not Implemented).
652 // "
653 if (header.unsupportedTe())
654 return Http::scNotImplemented;
655
656 // RFC 7230 section 3.3.3 #3 paragraph 3:
657 // Transfer-Encoding overrides Content-Length
658 if (header.chunked())
659 return Http::scNone;
660
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;
665
666 // HTTP/1.0 requirements differ from HTTP/1.1
667 if (http_ver <= Http::ProtocolVersion(1,0)) {
668 const auto m = method.id();
669
670 // RFC 1945 section 8.3:
671 // "
672 // A valid Content-Length is required on all HTTP/1.0 POST requests.
673 // "
674 // RFC 1945 Appendix D.1.1:
675 // "
676 // The fundamental difference between the POST and PUT requests is
677 // reflected in the different meaning of the Request-URI.
678 // "
679 if (m == Http::METHOD_POST || m == Http::METHOD_PUT)
680 return (content_length >= 0 ? Http::scNone : Http::scLengthRequired);
681
682 // RFC 1945 section 7.2:
683 // "
684 // An entity body is included with a request message only when the
685 // request method calls for one.
686 // "
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);
693
694 // other methods are not defined in RFC 1945
695 // assume they support an (optional) entity
696 return Http::scNone;
697 }
698
699 // RFC 7230 section 3.3
700 // "
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.
705 // "
706 return Http::scNone;
707}
708
4f1c93a7
EB
709bool
710HttpRequest::parseHeader(Http1::Parser &hp)
711{
712 Http::ContentLengthInterpreter clen;
713 return Message::parseHeader(hp, clen);
714}
715
716bool
717HttpRequest::parseHeader(const char *buffer, const size_t size)
718{
719 Http::ContentLengthInterpreter clen;
720 return header.parse(buffer, size, clen);
721}
722
582c2af2 723ConnStateData *
b0fb853f
A
724HttpRequest::pinnedConnection()
725{
582c2af2
FC
726 if (clientConnectionManager.valid() && clientConnectionManager->pinning.pinned)
727 return clientConnectionManager.get();
aee3523a 728 return nullptr;
582c2af2 729}
a8a0b1c2 730
851feda6 731const SBuf
a8a0b1c2
EC
732HttpRequest::storeId()
733{
734 if (store_id.size() != 0) {
851feda6 735 debugs(73, 3, "sent back store_id: " << store_id);
d7ef451f 736 return StringToSBuf(store_id);
a8a0b1c2 737 }
851feda6
AJ
738 debugs(73, 3, "sent back effectiveRequestUrl: " << effectiveRequestUri());
739 return effectiveRequestUri();
740}
a8a0b1c2 741
851feda6
AJ
742const SBuf &
743HttpRequest::effectiveRequestUri() const
744{
6b2b6cfe 745 if (method.id() == Http::METHOD_CONNECT || url.getScheme() == AnyP::PROTO_AUTHORITY_FORM)
851feda6
AJ
746 return url.authority(true); // host:port
747 return url.absolute();
a8a0b1c2 748}
f53969cc 749
75d47340
CT
750NotePairs::Pointer
751HttpRequest::notes()
752{
753 if (!theNotes)
754 theNotes = new NotePairs;
755 return theNotes;
756}
757
758void
759UpdateRequestNotes(ConnStateData *csd, HttpRequest &request, NotePairs const &helperNotes)
760{
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)) {
764 if (csd) {
765 csd->notes()->remove(cltTag);
766 csd->notes()->add(cltTag, connTag);
767 }
768 }
769 request.notes()->replaceOrAdd(&helperNotes);
770}
771
cd4a5c60
CT
772void
773HttpRequest::manager(const CbcPointer<ConnStateData> &aMgr, const AccessLogEntryPointer &al)
774{
775 clientConnectionManager = aMgr;
776
777 if (!clientConnectionManager.valid())
778 return;
779
780 AnyP::PortCfgPointer port = clientConnectionManager->port;
781 if (port) {
782 myportname = port->name;
783 flags.ignoreCc = port->ignore_cc;
784 }
785
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;
b6307671 794
cd4a5c60
CT
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) {
ebe87c09
EB
800 ACLFilledChecklist checklist(Config.accessList.spoof_client_ip, this);
801 checklist.al = al;
802 checklist.syncAle(this, nullptr);
803 flags.spoofClientIp = checklist.fastCheck().allowed();
cd4a5c60
CT
804 } else
805 flags.spoofClientIp = true;
806 } else
807 flags.spoofClientIp = false;
808 }
809}
b6307671 810
bec110e4
EB
811char *
812HttpRequest::canonicalCleanUrl() const
813{
814 return urlCanonicalCleanWithoutRequest(effectiveRequestUri(), method, url.getScheme());
815}
816
ea35939b 817/// a helper for handling PortCfg cases of FindListeningPortAddress()
b6c0f0dc 818template <typename Filter>
ea35939b 819static const Ip::Address *
b6c0f0dc 820FindGoodListeningPortAddressInPort(const AnyP::PortCfgPointer &port, const Filter isGood)
ea35939b 821{
b6c0f0dc 822 return (port && isGood(port->s)) ? &port->s : nullptr;
ea35939b
A
823}
824
825/// a helper for handling Connection cases of FindListeningPortAddress()
b6c0f0dc 826template <typename Filter>
ea35939b 827static const Ip::Address *
b6c0f0dc 828FindGoodListeningPortAddressInConn(const Comm::ConnectionPointer &conn, const Filter isGood)
ea35939b 829{
b6c0f0dc 830 return (conn && isGood(conn->local)) ? &conn->local : nullptr;
ea35939b
A
831}
832
b6c0f0dc 833template <typename Filter>
ea35939b 834const Ip::Address *
b6c0f0dc 835FindGoodListeningPortAddress(const HttpRequest *callerRequest, const AccessLogEntry *ale, const Filter filter)
ea35939b
A
836{
837 // Check all sources of usable listening port information, giving
838 // HttpRequest and masterXaction a preference over ALE.
839
840 const HttpRequest *request = callerRequest;
841 if (!request && ale)
842 request = ale->request;
843 if (!request)
844 return nullptr; // not enough information
845
b6c0f0dc 846 auto ip = FindGoodListeningPortAddressInPort(request->masterXaction->squidPort, filter);
ea35939b 847 if (!ip && ale)
b6c0f0dc 848 ip = FindGoodListeningPortAddressInPort(ale->cache.port, filter);
ea35939b
A
849
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)
852 return ip;
853
854 /* handle non-intercepted cases that were not handled above */
b6c0f0dc 855 ip = FindGoodListeningPortAddressInConn(request->masterXaction->tcpClient, filter);
ea35939b 856 if (!ip && ale)
b6c0f0dc 857 ip = FindGoodListeningPortAddressInConn(ale->tcpClient, filter);
ea35939b
A
858 return ip; // may still be nil
859}
860
b6c0f0dc
AR
861const Ip::Address *
862FindListeningPortAddress(const HttpRequest *callerRequest, const AccessLogEntry *ale)
863{
864 return FindGoodListeningPortAddress(callerRequest, ale, [](const Ip::Address &address) {
865 // FindListeningPortAddress() callers do not want INADDR_ANY addresses
866 return !address.isAnyAddr();
867 });
868}
869
380b09ae 870AnyP::Port
b6c0f0dc
AR
871FindListeningPortNumber(const HttpRequest *callerRequest, const AccessLogEntry *ale)
872{
873 const auto ip = FindGoodListeningPortAddress(callerRequest, ale, [](const Ip::Address &address) {
874 return address.port() > 0;
875 });
380b09ae
AR
876
877 if (!ip)
878 return std::nullopt;
879
880 Assure(ip->port() > 0);
881 return ip->port();
b6c0f0dc 882}