]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpRequest.cc
2 * Copyright (C) 1996-2017 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 "client_side.h"
16 #include "dns/LookupDetails.h"
17 #include "Downloader.h"
18 #include "err_detail_type.h"
22 #include "http/one/RequestParser.h"
23 #include "http/Stream.h"
24 #include "HttpHdrCc.h"
25 #include "HttpHeaderRange.h"
26 #include "HttpRequest.h"
27 #include "log/Config.h"
29 #include "sbuf/StringConvert.h"
30 #include "SquidConfig.h"
35 #include "auth/UserRequest.h"
38 #include "adaptation/icap/icap_log.h"
41 HttpRequest::HttpRequest() :
42 Http::Message(hoRequest
)
47 HttpRequest::HttpRequest(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aSchemeImg
, const char *aUrlpath
) :
48 Http::Message(hoRequest
)
50 static unsigned int id
= 1;
51 debugs(93,7, HERE
<< "constructed, this=" << this << " id=" << ++id
);
53 initHTTP(aMethod
, aProtocol
, aSchemeImg
, aUrlpath
);
56 HttpRequest::~HttpRequest()
59 debugs(93,7, HERE
<< "destructed, this=" << this);
63 HttpRequest::initHTTP(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aSchemeImg
, const char *aUrlpath
)
66 url
.setScheme(aProtocol
, aSchemeImg
);
73 method
= Http::METHOD_NONE
;
76 auth_user_request
= NULL
;
78 memset(&flags
, '\0', sizeof(flags
));
83 client_addr
.setEmpty();
89 errDetail
= ERR_DETAIL_NONE
;
90 peer_login
= NULL
; // not allocated/deallocated by this class
91 peer_domain
= NULL
; // not allocated/deallocated by this class
93 vary_headers
= SBuf();
94 myportname
= null_string
;
97 extacl_user
= null_string
;
98 extacl_passwd
= null_string
;
100 extacl_log
= null_string
;
101 extacl_message
= null_string
;
102 pstate
= psReadyToParseStartLine
;
103 #if FOLLOW_X_FORWARDED_FOR
104 indirect_client_addr
.setEmpty();
105 #endif /* FOLLOW_X_FORWARDED_FOR */
107 adaptHistory_
= NULL
;
112 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
113 forcedBodyContinuation
= false;
119 // we used to assert that the pipe is NULL, but now the request only
120 // points to a pipe that is owned and initiated by another object.
123 auth_user_request
= NULL
;
125 vary_headers
.clear();
131 delete cache_control
;
132 cache_control
= NULL
;
147 extacl_passwd
.clean();
151 extacl_message
.clean();
156 adaptHistory_
= NULL
;
171 HttpRequest::clone() const
173 HttpRequest
*copy
= new HttpRequest();
174 copy
->method
= method
;
175 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
176 copy
->header
.append(&header
);
177 copy
->hdrCacheInit();
178 copy
->hdr_sz
= hdr_sz
;
179 copy
->http_ver
= http_ver
;
180 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
181 copy
->body_pipe
= body_pipe
;
185 // range handled in hdrCacheInit()
187 copy
->imslen
= imslen
;
188 copy
->hier
= hier
; // Is it safe to copy? Should we?
190 copy
->errType
= errType
;
192 // XXX: what to do with copy->peer_login?
194 copy
->lastmod
= lastmod
;
196 copy
->vary_headers
= vary_headers
;
197 // XXX: what to do with copy->peer_domain?
200 copy
->extacl_log
= extacl_log
;
201 copy
->extacl_message
= extacl_message
;
203 const bool inheritWorked
= copy
->inheritProperties(this);
204 assert(inheritWorked
);
210 HttpRequest::inheritProperties(const Http::Message
*aMsg
)
212 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
216 client_addr
= aReq
->client_addr
;
217 #if FOLLOW_X_FORWARDED_FOR
218 indirect_client_addr
= aReq
->indirect_client_addr
;
220 my_addr
= aReq
->my_addr
;
222 dnsWait
= aReq
->dnsWait
;
225 adaptHistory_
= aReq
->adaptHistory();
228 icapHistory_
= aReq
->icapHistory();
231 // This may be too conservative for the 204 No Content case
232 // may eventually need cloneNullAdaptationImmune() for that.
233 flags
= aReq
->flags
.cloneAdaptationImmune();
235 errType
= aReq
->errType
;
236 errDetail
= aReq
->errDetail
;
238 auth_user_request
= aReq
->auth_user_request
;
239 extacl_user
= aReq
->extacl_user
;
240 extacl_passwd
= aReq
->extacl_passwd
;
243 myportname
= aReq
->myportname
;
245 forcedBodyContinuation
= aReq
->forcedBodyContinuation
;
247 // main property is which connection the request was received on (if any)
248 clientConnectionManager
= aReq
->clientConnectionManager
;
250 downloader
= aReq
->downloader
;
252 theNotes
= aReq
->theNotes
;
254 sources
= aReq
->sources
;
259 * Checks the first line of an HTTP request is valid
260 * currently just checks the request method is present.
262 * NP: Other errors are left for detection later in the parse.
265 HttpRequest::sanityCheckStartLine(const char *buf
, const size_t hdr_len
, Http::StatusCode
*error
)
267 // content is long enough to possibly hold a reply
268 // 2 being magic size of a 1-byte request method plus space delimiter
270 // this is ony a real error if the headers apparently complete.
272 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
273 *error
= Http::scInvalidHeader
;
278 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
280 m
.HttpRequestMethodXXX(buf
);
281 if (m
== Http::METHOD_NONE
) {
282 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
283 *error
= Http::scInvalidHeader
;
291 HttpRequest::parseFirstLine(const char *start
, const char *end
)
293 method
.HttpRequestMethodXXX(start
);
295 if (method
== Http::METHOD_NONE
)
298 // XXX: performance regression, strcspn() over the method bytes a second time.
299 // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start.
300 const char *t
= start
+ strcspn(start
, w_space
);
302 start
= t
+ strspn(t
, w_space
); // skip w_space after method
304 const char *ver
= findTrailingHTTPVersion(start
, end
);
309 while (xisspace(*end
)) // find prev non-space
312 ++end
; // back to space
314 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
315 debugs(73, DBG_IMPORTANT
, "parseRequestLine: Invalid HTTP identifier.");
323 if (end
< start
) // missing URI
328 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
330 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
332 * (char *) end
= save
;
340 /* swaps out request using httpRequestPack */
342 HttpRequest::swapOut(StoreEntry
* e
)
350 /* packs request-line and headers, appends <crlf> terminator */
352 HttpRequest::pack(Packable
* p
) const
355 /* pack request-line */
356 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
357 SQUIDSBUFPRINT(method
.image()), SQUIDSBUFPRINT(url
.path()),
358 http_ver
.major
, http_ver
.minor
);
362 p
->append("\r\n", 2);
366 * A wrapper for debugObj()
369 httpRequestPack(void *obj
, Packable
*p
)
371 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
375 /* returns the length of request line + headers + crlf */
377 HttpRequest::prefixLen() const
379 return method
.image().length() + 1 +
380 url
.path().length() + 1 +
385 /* sync this routine when you update HttpRequest struct */
387 HttpRequest::hdrCacheInit()
389 Http::Message::hdrCacheInit();
392 range
= header
.getRange();
396 Adaptation::Icap::History::Pointer
397 HttpRequest::icapHistory() const
400 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
401 icapHistory_
= new Adaptation::Icap::History();
402 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
411 Adaptation::History::Pointer
412 HttpRequest::adaptHistory(bool createIfNone
) const
414 if (!adaptHistory_
&& createIfNone
) {
415 adaptHistory_
= new Adaptation::History();
416 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
419 return adaptHistory_
;
422 Adaptation::History::Pointer
423 HttpRequest::adaptLogHistory() const
425 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
429 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
431 if (!adaptHistory_
) {
432 adaptHistory_
= them
.adaptHistory_
; // may be nil
434 // check that histories did not diverge
435 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
442 HttpRequest::multipartRangeRequest() const
444 return (range
&& range
->specs
.size() > 1);
448 HttpRequest::bodyNibbled() const
450 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
454 HttpRequest::detailError(err_type aType
, int aDetail
)
456 if (errType
|| errDetail
)
457 debugs(11, 5, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
458 debugs(11, 5, HERE
<< "current error details: " << aType
<< '/' << aDetail
);
459 // checking type and detail separately may cause inconsistency, but
460 // may result in more details available if they only become available later
468 HttpRequest::clearError()
470 debugs(11, 7, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
472 errDetail
= ERR_DETAIL_NONE
;
476 HttpRequest::packFirstLineInto(Packable
* p
, bool full_uri
) const
478 const SBuf
tmp(full_uri
? effectiveRequestUri() : url
.path());
480 // form HTTP request-line
481 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
482 SQUIDSBUFPRINT(method
.image()),
484 http_ver
.major
, http_ver
.minor
);
488 * Indicate whether or not we would expect an entity-body
489 * along with this request
492 HttpRequest::expectingBody(const HttpRequestMethod
&, int64_t &theSize
) const
494 bool expectBody
= false;
497 * Note: Checks for message validity is in clientIsContentLengthValid().
498 * this just checks if a entity-body is expected based on HTTP message syntax
500 if (header
.chunked()) {
503 } else if (content_length
>= 0) {
505 theSize
= content_length
;
515 * Create a Request from a URL and METHOD.
517 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
518 * If the request cannot be created cleanly, NULL is returned
521 HttpRequest::CreateFromUrl(char * url
, const HttpRequestMethod
& method
)
523 return urlParse(method
, url
, NULL
);
527 * Are responses to this request possible cacheable ?
528 * If false then no matter what the response must not be cached.
531 HttpRequest::maybeCacheable()
533 // Intercepted request with Host: header which cannot be trusted.
534 // Because it failed verification, or someone bypassed the security tests
535 // we cannot cache the reponse for sharing between clients.
536 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
537 if (!flags
.hostVerified
&& (flags
.intercepted
|| flags
.interceptTproxy
))
540 switch (url
.getScheme()) {
541 case AnyP::PROTO_HTTP
:
542 case AnyP::PROTO_HTTPS
:
543 if (!method
.respMaybeCacheable())
546 // RFC 7234 section 5.2.1.5:
547 // "cache MUST NOT store any part of either this request or any response to it"
549 // NP: refresh_pattern ignore-no-store only applies to response messages
550 // this test is handling request message CC header.
551 if (!flags
.ignoreCc
&& cache_control
&& cache_control
->noStore())
555 case AnyP::PROTO_GOPHER
:
556 if (!gopherCachable(this))
560 case AnyP::PROTO_CACHE_OBJECT
:
563 //case AnyP::PROTO_FTP:
572 HttpRequest::conditional() const
575 header
.has(Http::HdrType::IF_MATCH
) ||
576 header
.has(Http::HdrType::IF_NONE_MATCH
);
580 HttpRequest::recordLookup(const Dns::LookupDetails
&dns
)
582 if (dns
.wait
>= 0) { // known delay
583 if (dnsWait
>= 0) // have recorded DNS wait before
591 HttpRequest::getRangeOffsetLimit()
593 /* -2 is the starting value of rangeOffsetLimit.
594 * If it is -2, that means we haven't checked it yet.
595 * Otherwise, return the current value */
596 if (rangeOffsetLimit
!= -2)
597 return rangeOffsetLimit
;
599 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
601 ACLFilledChecklist
ch(NULL
, this, NULL
);
602 ch
.src_addr
= client_addr
;
603 ch
.my_addr
= my_addr
;
605 for (AclSizeLimit
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
606 /* if there is no ACL list or if the ACLs listed match use this limit value */
607 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
608 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
609 rangeOffsetLimit
= l
->size
; // may be -1
614 return rangeOffsetLimit
;
618 HttpRequest::ignoreRange(const char *reason
)
621 debugs(73, 3, static_cast<void*>(range
) << " for " << reason
);
625 // Some callers also reset isRanged but it may not be safe for all callers:
626 // isRanged is used to determine whether a weak ETag comparison is allowed,
627 // and that check should not ignore the Range header if it was present.
628 // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we?
632 HttpRequest::canHandle1xx() const
634 // old clients do not support 1xx unless they sent Expect: 100-continue
635 // (we reject all other Http::HdrType::EXPECT values so just check for Http::HdrType::EXPECT)
636 if (http_ver
<= Http::ProtocolVersion(1,0) && !header
.has(Http::HdrType::EXPECT
))
639 // others must support 1xx control messages
644 HttpRequest::pinnedConnection()
646 if (clientConnectionManager
.valid() && clientConnectionManager
->pinning
.pinned
)
647 return clientConnectionManager
.get();
652 HttpRequest::storeId()
654 if (store_id
.size() != 0) {
655 debugs(73, 3, "sent back store_id: " << store_id
);
656 return StringToSBuf(store_id
);
658 debugs(73, 3, "sent back effectiveRequestUrl: " << effectiveRequestUri());
659 return effectiveRequestUri();
663 HttpRequest::effectiveRequestUri() const
665 if (method
.id() == Http::METHOD_CONNECT
|| url
.getScheme() == AnyP::PROTO_AUTHORITY_FORM
)
666 return url
.authority(true); // host:port
667 return url
.absolute();
674 theNotes
= new NotePairs
;
679 UpdateRequestNotes(ConnStateData
*csd
, HttpRequest
&request
, NotePairs
const &helperNotes
)
681 // Tag client connection if the helper responded with clt_conn_tag=tag.
682 const char *cltTag
= "clt_conn_tag";
683 if (const char *connTag
= helperNotes
.findFirst(cltTag
)) {
685 csd
->notes()->remove(cltTag
);
686 csd
->notes()->add(cltTag
, connTag
);
689 request
.notes()->replaceOrAdd(&helperNotes
);