]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpRequest.cc
2 * Copyright (C) 1996-2015 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 "HttpHdrCc.h"
23 #include "HttpHeaderRange.h"
24 #include "HttpRequest.h"
25 #include "log/Config.h"
27 #include "SquidConfig.h"
32 #include "auth/UserRequest.h"
35 #include "adaptation/icap/icap_log.h"
38 HttpRequest::HttpRequest() :
44 HttpRequest::HttpRequest(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aUrlpath
) :
47 static unsigned int id
= 1;
48 debugs(93,7, HERE
<< "constructed, this=" << this << " id=" << ++id
);
50 initHTTP(aMethod
, aProtocol
, aUrlpath
);
53 HttpRequest::~HttpRequest()
56 debugs(93,7, HERE
<< "destructed, this=" << this);
60 HttpRequest::initHTTP(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aUrlpath
)
63 url
.setScheme(aProtocol
);
70 method
= Http::METHOD_NONE
;
73 auth_user_request
= NULL
;
76 memset(&flags
, '\0', sizeof(flags
));
81 client_addr
.setEmpty();
87 errDetail
= ERR_DETAIL_NONE
;
88 peer_login
= NULL
; // not allocated/deallocated by this class
89 peer_domain
= NULL
; // not allocated/deallocated by this class
92 myportname
= null_string
;
95 extacl_user
= null_string
;
96 extacl_passwd
= null_string
;
98 extacl_log
= null_string
;
99 extacl_message
= null_string
;
100 pstate
= psReadyToParseStartLine
;
101 #if FOLLOW_X_FORWARDED_FOR
102 indirect_client_addr
.setEmpty();
103 #endif /* FOLLOW_X_FORWARDED_FOR */
105 adaptHistory_
= NULL
;
110 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
111 forcedBodyContinuation
= false;
117 // we used to assert that the pipe is NULL, but now the request only
118 // points to a pipe that is owned and initiated by another object.
121 auth_user_request
= NULL
;
123 safe_free(canonical
);
125 safe_free(vary_headers
);
132 delete cache_control
;
133 cache_control
= NULL
;
148 extacl_passwd
.clean();
152 extacl_message
.clean();
157 adaptHistory_
= NULL
;
172 HttpRequest::clone() const
174 HttpRequest
*copy
= new HttpRequest();
175 copy
->method
= method
;
176 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
177 copy
->header
.append(&header
);
178 copy
->hdrCacheInit();
179 copy
->hdr_sz
= hdr_sz
;
180 copy
->http_ver
= http_ver
;
181 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
182 copy
->body_pipe
= body_pipe
;
184 copy
->url
.setScheme(url
.getScheme());
185 copy
->url
.userInfo(url
.userInfo());
186 copy
->url
.host(url
.host());
187 copy
->url
.port(url
.port());
188 copy
->url
.path(url
.path());
190 // urlPath handled in ctor
191 copy
->canonical
= canonical
? xstrdup(canonical
) : NULL
;
193 // range handled in hdrCacheInit()
195 copy
->imslen
= imslen
;
196 copy
->hier
= hier
; // Is it safe to copy? Should we?
198 copy
->errType
= errType
;
200 // XXX: what to do with copy->peer_login?
202 copy
->lastmod
= lastmod
;
204 copy
->vary_headers
= vary_headers
? xstrdup(vary_headers
) : NULL
;
205 // XXX: what to do with copy->peer_domain?
208 copy
->extacl_log
= extacl_log
;
209 copy
->extacl_message
= extacl_message
;
211 const bool inheritWorked
= copy
->inheritProperties(this);
212 assert(inheritWorked
);
218 HttpRequest::inheritProperties(const HttpMsg
*aMsg
)
220 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
224 client_addr
= aReq
->client_addr
;
225 #if FOLLOW_X_FORWARDED_FOR
226 indirect_client_addr
= aReq
->indirect_client_addr
;
228 my_addr
= aReq
->my_addr
;
230 dnsWait
= aReq
->dnsWait
;
233 adaptHistory_
= aReq
->adaptHistory();
236 icapHistory_
= aReq
->icapHistory();
239 // This may be too conservative for the 204 No Content case
240 // may eventually need cloneNullAdaptationImmune() for that.
241 flags
= aReq
->flags
.cloneAdaptationImmune();
243 errType
= aReq
->errType
;
244 errDetail
= aReq
->errDetail
;
246 auth_user_request
= aReq
->auth_user_request
;
247 extacl_user
= aReq
->extacl_user
;
248 extacl_passwd
= aReq
->extacl_passwd
;
251 myportname
= aReq
->myportname
;
253 forcedBodyContinuation
= aReq
->forcedBodyContinuation
;
255 // main property is which connection the request was received on (if any)
256 clientConnectionManager
= aReq
->clientConnectionManager
;
263 * Checks the first line of an HTTP request is valid
264 * currently just checks the request method is present.
266 * NP: Other errors are left for detection later in the parse.
269 HttpRequest::sanityCheckStartLine(const char *buf
, const size_t hdr_len
, Http::StatusCode
*error
)
271 // content is long enough to possibly hold a reply
272 // 2 being magic size of a 1-byte request method plus space delimiter
274 // this is ony a real error if the headers apparently complete.
276 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
277 *error
= Http::scInvalidHeader
;
282 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
284 m
.HttpRequestMethodXXX(buf
);
285 if (m
== Http::METHOD_NONE
) {
286 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
287 *error
= Http::scInvalidHeader
;
295 HttpRequest::parseFirstLine(const char *start
, const char *end
)
297 method
.HttpRequestMethodXXX(start
);
299 if (method
== Http::METHOD_NONE
)
302 // XXX: performance regression, strcspn() over the method bytes a second time.
303 // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start.
304 const char *t
= start
+ strcspn(start
, w_space
);
306 start
= t
+ strspn(t
, w_space
); // skip w_space after method
308 const char *ver
= findTrailingHTTPVersion(start
, end
);
313 while (xisspace(*end
)) // find prev non-space
316 ++end
; // back to space
318 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
319 debugs(73, DBG_IMPORTANT
, "parseRequestLine: Invalid HTTP identifier.");
327 if (end
< start
) // missing URI
332 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
334 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
336 * (char *) end
= save
;
345 HttpRequest::parseHeader(Http1::RequestParser
&hp
)
347 // HTTP/1 message contains "zero or more header fields"
348 // zero does not need parsing
349 if (!hp
.headerBlockSize())
352 // XXX: c_str() reallocates. performance regression.
353 const bool result
= header
.parse(hp
.mimeHeader().c_str(), hp
.headerBlockSize());
361 /* swaps out request using httpRequestPack */
363 HttpRequest::swapOut(StoreEntry
* e
)
370 /* packs request-line and headers, appends <crlf> terminator */
372 HttpRequest::pack(Packable
* p
)
375 /* pack request-line */
376 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
377 SQUIDSBUFPRINT(method
.image()), SQUIDSBUFPRINT(url
.path()),
378 http_ver
.major
, http_ver
.minor
);
382 p
->append("\r\n", 2);
386 * A wrapper for debugObj()
389 httpRequestPack(void *obj
, Packable
*p
)
391 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
395 /* returns the length of request line + headers + crlf */
397 HttpRequest::prefixLen() const
399 return method
.image().length() + 1 +
400 url
.path().length() + 1 +
405 /* sync this routine when you update HttpRequest struct */
407 HttpRequest::hdrCacheInit()
409 HttpMsg::hdrCacheInit();
412 range
= header
.getRange();
416 Adaptation::Icap::History::Pointer
417 HttpRequest::icapHistory() const
420 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
421 icapHistory_
= new Adaptation::Icap::History();
422 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
431 Adaptation::History::Pointer
432 HttpRequest::adaptHistory(bool createIfNone
) const
434 if (!adaptHistory_
&& createIfNone
) {
435 adaptHistory_
= new Adaptation::History();
436 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
439 return adaptHistory_
;
442 Adaptation::History::Pointer
443 HttpRequest::adaptLogHistory() const
445 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
449 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
451 if (!adaptHistory_
) {
452 adaptHistory_
= them
.adaptHistory_
; // may be nil
454 // check that histories did not diverge
455 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
462 HttpRequest::multipartRangeRequest() const
464 return (range
&& range
->specs
.size() > 1);
468 HttpRequest::bodyNibbled() const
470 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
474 HttpRequest::detailError(err_type aType
, int aDetail
)
476 if (errType
|| errDetail
)
477 debugs(11, 5, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
478 debugs(11, 5, HERE
<< "current error details: " << aType
<< '/' << aDetail
);
479 // checking type and detail separately may cause inconsistency, but
480 // may result in more details available if they only become available later
488 HttpRequest::clearError()
490 debugs(11, 7, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
492 errDetail
= ERR_DETAIL_NONE
;
496 HttpRequest::packFirstLineInto(Packable
* p
, bool full_uri
) const
500 tmp
= urlCanonical((HttpRequest
*)this);
504 // form HTTP request-line
505 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
506 SQUIDSBUFPRINT(method
.image()),
508 http_ver
.major
, http_ver
.minor
);
512 * Indicate whether or not we would expect an entity-body
513 * along with this request
516 HttpRequest::expectingBody(const HttpRequestMethod
&, int64_t &theSize
) const
518 bool expectBody
= false;
521 * Note: Checks for message validity is in clientIsContentLengthValid().
522 * this just checks if a entity-body is expected based on HTTP message syntax
524 if (header
.chunked()) {
527 } else if (content_length
>= 0) {
529 theSize
= content_length
;
539 * Create a Request from a URL and METHOD.
541 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
542 * If the request cannot be created cleanly, NULL is returned
545 HttpRequest::CreateFromUrlAndMethod(char * url
, const HttpRequestMethod
& method
)
547 return urlParse(method
, url
, NULL
);
551 * Create a Request from a URL.
553 * If the request cannot be created cleanly, NULL is returned
556 HttpRequest::CreateFromUrl(char * url
)
558 return urlParse(Http::METHOD_GET
, url
, NULL
);
562 * Are responses to this request possible cacheable ?
563 * If false then no matter what the response must not be cached.
566 HttpRequest::maybeCacheable()
568 // Intercepted request with Host: header which cannot be trusted.
569 // Because it failed verification, or someone bypassed the security tests
570 // we cannot cache the reponse for sharing between clients.
571 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
572 if (!flags
.hostVerified
&& (flags
.intercepted
|| flags
.interceptTproxy
))
575 switch (url
.getScheme()) {
576 case AnyP::PROTO_HTTP
:
577 case AnyP::PROTO_HTTPS
:
578 if (!method
.respMaybeCacheable())
581 // XXX: this would seem the correct place to detect request cache-controls
582 // no-store, private and related which block cacheability
585 case AnyP::PROTO_GOPHER
:
586 if (!gopherCachable(this))
590 case AnyP::PROTO_CACHE_OBJECT
:
593 //case AnyP::PROTO_FTP:
602 HttpRequest::conditional() const
605 header
.has(HDR_IF_MATCH
) ||
606 header
.has(HDR_IF_NONE_MATCH
);
610 HttpRequest::recordLookup(const Dns::LookupDetails
&dns
)
612 if (dns
.wait
>= 0) { // known delay
613 if (dnsWait
>= 0) // have recorded DNS wait before
621 HttpRequest::getRangeOffsetLimit()
623 /* -2 is the starting value of rangeOffsetLimit.
624 * If it is -2, that means we haven't checked it yet.
625 * Otherwise, return the current value */
626 if (rangeOffsetLimit
!= -2)
627 return rangeOffsetLimit
;
629 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
631 ACLFilledChecklist
ch(NULL
, this, NULL
);
632 ch
.src_addr
= client_addr
;
633 ch
.my_addr
= my_addr
;
635 for (AclSizeLimit
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
636 /* if there is no ACL list or if the ACLs listed match use this limit value */
637 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
638 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
639 rangeOffsetLimit
= l
->size
; // may be -1
644 return rangeOffsetLimit
;
648 HttpRequest::ignoreRange(const char *reason
)
651 debugs(73, 3, static_cast<void*>(range
) << " for " << reason
);
655 // Some callers also reset isRanged but it may not be safe for all callers:
656 // isRanged is used to determine whether a weak ETag comparison is allowed,
657 // and that check should not ignore the Range header if it was present.
658 // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we?
662 HttpRequest::canHandle1xx() const
664 // old clients do not support 1xx unless they sent Expect: 100-continue
665 // (we reject all other HDR_EXPECT values so just check for HDR_EXPECT)
666 if (http_ver
<= Http::ProtocolVersion(1,0) && !header
.has(HDR_EXPECT
))
669 // others must support 1xx control messages
674 HttpRequest::pinnedConnection()
676 if (clientConnectionManager
.valid() && clientConnectionManager
->pinning
.pinned
)
677 return clientConnectionManager
.get();
682 HttpRequest::storeId()
684 if (store_id
.size() != 0) {
685 debugs(73, 3, "sent back store_id:" << store_id
);
687 return store_id
.termedBuf();
689 debugs(73, 3, "sent back canonicalUrl:" << urlCanonical(this) );
691 return urlCanonical(this);