]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpRequest.cc
2 * Copyright (C) 1996-2016 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 "err_detail_type.h"
21 #include "http/one/RequestParser.h"
22 #include "http/Stream.h"
23 #include "HttpHdrCc.h"
24 #include "HttpHeaderRange.h"
25 #include "HttpRequest.h"
26 #include "log/Config.h"
28 #include "sbuf/StringConvert.h"
29 #include "SquidConfig.h"
34 #include "auth/UserRequest.h"
37 #include "adaptation/icap/icap_log.h"
40 HttpRequest::HttpRequest() :
46 HttpRequest::HttpRequest(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aUrlpath
) :
49 static unsigned int id
= 1;
50 debugs(93,7, HERE
<< "constructed, this=" << this << " id=" << ++id
);
52 initHTTP(aMethod
, aProtocol
, aUrlpath
);
55 HttpRequest::~HttpRequest()
58 debugs(93,7, HERE
<< "destructed, this=" << this);
62 HttpRequest::initHTTP(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aUrlpath
)
65 url
.setScheme(aProtocol
);
72 method
= Http::METHOD_NONE
;
75 auth_user_request
= NULL
;
77 memset(&flags
, '\0', sizeof(flags
));
82 client_addr
.setEmpty();
88 errDetail
= ERR_DETAIL_NONE
;
89 peer_login
= NULL
; // not allocated/deallocated by this class
90 peer_domain
= NULL
; // not allocated/deallocated by this class
92 vary_headers
= SBuf();
93 myportname
= null_string
;
96 extacl_user
= null_string
;
97 extacl_passwd
= null_string
;
99 extacl_log
= null_string
;
100 extacl_message
= null_string
;
101 pstate
= psReadyToParseStartLine
;
102 #if FOLLOW_X_FORWARDED_FOR
103 indirect_client_addr
.setEmpty();
104 #endif /* FOLLOW_X_FORWARDED_FOR */
106 adaptHistory_
= NULL
;
111 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
112 forcedBodyContinuation
= false;
118 // we used to assert that the pipe is NULL, but now the request only
119 // points to a pipe that is owned and initiated by another object.
122 auth_user_request
= NULL
;
124 vary_headers
.clear();
130 delete cache_control
;
131 cache_control
= NULL
;
146 extacl_passwd
.clean();
150 extacl_message
.clean();
155 adaptHistory_
= NULL
;
170 HttpRequest::clone() const
172 HttpRequest
*copy
= new HttpRequest();
173 copy
->method
= method
;
174 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
175 copy
->header
.append(&header
);
176 copy
->hdrCacheInit();
177 copy
->hdr_sz
= hdr_sz
;
178 copy
->http_ver
= http_ver
;
179 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
180 copy
->body_pipe
= body_pipe
;
182 copy
->url
.setScheme(url
.getScheme());
183 copy
->url
.userInfo(url
.userInfo());
184 copy
->url
.host(url
.host());
185 copy
->url
.port(url
.port());
186 copy
->url
.path(url
.path());
188 // range handled in hdrCacheInit()
190 copy
->imslen
= imslen
;
191 copy
->hier
= hier
; // Is it safe to copy? Should we?
193 copy
->errType
= errType
;
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 HttpMsg
*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();
238 errType
= aReq
->errType
;
239 errDetail
= aReq
->errDetail
;
241 auth_user_request
= aReq
->auth_user_request
;
242 extacl_user
= aReq
->extacl_user
;
243 extacl_passwd
= aReq
->extacl_passwd
;
246 myportname
= aReq
->myportname
;
248 forcedBodyContinuation
= aReq
->forcedBodyContinuation
;
250 // main property is which connection the request was received on (if any)
251 clientConnectionManager
= aReq
->clientConnectionManager
;
255 sources
= aReq
->sources
;
260 * Checks the first line of an HTTP request is valid
261 * currently just checks the request method is present.
263 * NP: Other errors are left for detection later in the parse.
266 HttpRequest::sanityCheckStartLine(const char *buf
, const size_t hdr_len
, Http::StatusCode
*error
)
268 // content is long enough to possibly hold a reply
269 // 2 being magic size of a 1-byte request method plus space delimiter
271 // this is ony a real error if the headers apparently complete.
273 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
274 *error
= Http::scInvalidHeader
;
279 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
281 m
.HttpRequestMethodXXX(buf
);
282 if (m
== Http::METHOD_NONE
) {
283 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
284 *error
= Http::scInvalidHeader
;
292 HttpRequest::parseFirstLine(const char *start
, const char *end
)
294 method
.HttpRequestMethodXXX(start
);
296 if (method
== Http::METHOD_NONE
)
299 // XXX: performance regression, strcspn() over the method bytes a second time.
300 // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start.
301 const char *t
= start
+ strcspn(start
, w_space
);
303 start
= t
+ strspn(t
, w_space
); // skip w_space after method
305 const char *ver
= findTrailingHTTPVersion(start
, end
);
310 while (xisspace(*end
)) // find prev non-space
313 ++end
; // back to space
315 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
316 debugs(73, DBG_IMPORTANT
, "parseRequestLine: Invalid HTTP identifier.");
324 if (end
< start
) // missing URI
329 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
331 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
333 * (char *) end
= save
;
341 /* swaps out request using httpRequestPack */
343 HttpRequest::swapOut(StoreEntry
* e
)
351 /* packs request-line and headers, appends <crlf> terminator */
353 HttpRequest::pack(Packable
* p
) const
356 /* pack request-line */
357 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
358 SQUIDSBUFPRINT(method
.image()), SQUIDSBUFPRINT(url
.path()),
359 http_ver
.major
, http_ver
.minor
);
363 p
->append("\r\n", 2);
367 * A wrapper for debugObj()
370 httpRequestPack(void *obj
, Packable
*p
)
372 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
376 /* returns the length of request line + headers + crlf */
378 HttpRequest::prefixLen() const
380 return method
.image().length() + 1 +
381 url
.path().length() + 1 +
386 /* sync this routine when you update HttpRequest struct */
388 HttpRequest::hdrCacheInit()
390 HttpMsg::hdrCacheInit();
393 range
= header
.getRange();
397 Adaptation::Icap::History::Pointer
398 HttpRequest::icapHistory() const
401 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
402 icapHistory_
= new Adaptation::Icap::History();
403 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
412 Adaptation::History::Pointer
413 HttpRequest::adaptHistory(bool createIfNone
) const
415 if (!adaptHistory_
&& createIfNone
) {
416 adaptHistory_
= new Adaptation::History();
417 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
420 return adaptHistory_
;
423 Adaptation::History::Pointer
424 HttpRequest::adaptLogHistory() const
426 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
430 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
432 if (!adaptHistory_
) {
433 adaptHistory_
= them
.adaptHistory_
; // may be nil
435 // check that histories did not diverge
436 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
443 HttpRequest::multipartRangeRequest() const
445 return (range
&& range
->specs
.size() > 1);
449 HttpRequest::bodyNibbled() const
451 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
455 HttpRequest::detailError(err_type aType
, int aDetail
)
457 if (errType
|| errDetail
)
458 debugs(11, 5, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
459 debugs(11, 5, HERE
<< "current error details: " << aType
<< '/' << aDetail
);
460 // checking type and detail separately may cause inconsistency, but
461 // may result in more details available if they only become available later
469 HttpRequest::clearError()
471 debugs(11, 7, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
473 errDetail
= ERR_DETAIL_NONE
;
477 HttpRequest::packFirstLineInto(Packable
* p
, bool full_uri
) const
479 const SBuf
tmp(full_uri
? effectiveRequestUri() : url
.path());
481 // form HTTP request-line
482 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
483 SQUIDSBUFPRINT(method
.image()),
485 http_ver
.major
, http_ver
.minor
);
489 * Indicate whether or not we would expect an entity-body
490 * along with this request
493 HttpRequest::expectingBody(const HttpRequestMethod
&, int64_t &theSize
) const
495 bool expectBody
= false;
498 * Note: Checks for message validity is in clientIsContentLengthValid().
499 * this just checks if a entity-body is expected based on HTTP message syntax
501 if (header
.chunked()) {
504 } else if (content_length
>= 0) {
506 theSize
= content_length
;
516 * Create a Request from a URL and METHOD.
518 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
519 * If the request cannot be created cleanly, NULL is returned
522 HttpRequest::CreateFromUrl(char * url
, const HttpRequestMethod
& method
)
524 return urlParse(method
, url
, NULL
);
528 * Are responses to this request possible cacheable ?
529 * If false then no matter what the response must not be cached.
532 HttpRequest::maybeCacheable()
534 // Intercepted request with Host: header which cannot be trusted.
535 // Because it failed verification, or someone bypassed the security tests
536 // we cannot cache the reponse for sharing between clients.
537 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
538 if (!flags
.hostVerified
&& (flags
.intercepted
|| flags
.interceptTproxy
))
541 switch (url
.getScheme()) {
542 case AnyP::PROTO_HTTP
:
543 case AnyP::PROTO_HTTPS
:
544 if (!method
.respMaybeCacheable())
547 // XXX: this would seem the correct place to detect request cache-controls
548 // no-store, private and related which block cacheability
551 case AnyP::PROTO_GOPHER
:
552 if (!gopherCachable(this))
556 case AnyP::PROTO_CACHE_OBJECT
:
559 //case AnyP::PROTO_FTP:
568 HttpRequest::conditional() const
571 header
.has(Http::HdrType::IF_MATCH
) ||
572 header
.has(Http::HdrType::IF_NONE_MATCH
);
576 HttpRequest::recordLookup(const Dns::LookupDetails
&dns
)
578 if (dns
.wait
>= 0) { // known delay
579 if (dnsWait
>= 0) // have recorded DNS wait before
587 HttpRequest::getRangeOffsetLimit()
589 /* -2 is the starting value of rangeOffsetLimit.
590 * If it is -2, that means we haven't checked it yet.
591 * Otherwise, return the current value */
592 if (rangeOffsetLimit
!= -2)
593 return rangeOffsetLimit
;
595 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
597 ACLFilledChecklist
ch(NULL
, this, NULL
);
598 ch
.src_addr
= client_addr
;
599 ch
.my_addr
= my_addr
;
601 for (AclSizeLimit
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
602 /* if there is no ACL list or if the ACLs listed match use this limit value */
603 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
604 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
605 rangeOffsetLimit
= l
->size
; // may be -1
610 return rangeOffsetLimit
;
614 HttpRequest::ignoreRange(const char *reason
)
617 debugs(73, 3, static_cast<void*>(range
) << " for " << reason
);
621 // Some callers also reset isRanged but it may not be safe for all callers:
622 // isRanged is used to determine whether a weak ETag comparison is allowed,
623 // and that check should not ignore the Range header if it was present.
624 // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we?
628 HttpRequest::canHandle1xx() const
630 // old clients do not support 1xx unless they sent Expect: 100-continue
631 // (we reject all other Http::HdrType::EXPECT values so just check for Http::HdrType::EXPECT)
632 if (http_ver
<= Http::ProtocolVersion(1,0) && !header
.has(Http::HdrType::EXPECT
))
635 // others must support 1xx control messages
640 HttpRequest::pinnedConnection()
642 if (clientConnectionManager
.valid() && clientConnectionManager
->pinning
.pinned
)
643 return clientConnectionManager
.get();
648 HttpRequest::storeId()
650 if (store_id
.size() != 0) {
651 debugs(73, 3, "sent back store_id: " << store_id
);
652 return StringToSBuf(store_id
);
654 debugs(73, 3, "sent back effectiveRequestUrl: " << effectiveRequestUri());
655 return effectiveRequestUri();
659 HttpRequest::effectiveRequestUri() const
661 if (method
.id() == Http::METHOD_CONNECT
)
662 return url
.authority(true); // host:port
663 return url
.absolute();