]>
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
;
76 auth_user_request
= NULL
;
80 memset(&flags
, '\0', sizeof(flags
));
85 client_addr
.setEmpty();
91 errDetail
= ERR_DETAIL_NONE
;
92 peer_login
= NULL
; // not allocated/deallocated by this class
93 peer_domain
= NULL
; // not allocated/deallocated by this class
96 myportname
= null_string
;
99 extacl_user
= null_string
;
100 extacl_passwd
= null_string
;
102 extacl_log
= null_string
;
103 extacl_message
= null_string
;
104 pstate
= psReadyToParseStartLine
;
105 #if FOLLOW_X_FORWARDED_FOR
106 indirect_client_addr
.setEmpty();
107 #endif /* FOLLOW_X_FORWARDED_FOR */
109 adaptHistory_
= NULL
;
114 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
115 forcedBodyContinuation
= false;
121 // we used to assert that the pipe is NULL, but now the request only
122 // points to a pipe that is owned and initiated by another object.
125 auth_user_request
= NULL
;
127 safe_free(canonical
);
129 safe_free(vary_headers
);
137 delete cache_control
;
138 cache_control
= NULL
;
153 extacl_passwd
.clean();
157 extacl_message
.clean();
162 adaptHistory_
= NULL
;
177 HttpRequest::clone() const
179 HttpRequest
*copy
= new HttpRequest(method
, url
.getScheme(), urlpath
.termedBuf());
180 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
181 copy
->header
.append(&header
);
182 copy
->hdrCacheInit();
183 copy
->hdr_sz
= hdr_sz
;
184 copy
->http_ver
= http_ver
;
185 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
186 copy
->body_pipe
= body_pipe
;
188 copy
->url
.userInfo(url
.userInfo());
189 strncpy(copy
->host
, host
, sizeof(host
)); // SQUIDHOSTNAMELEN
190 copy
->host_addr
= host_addr
;
193 // urlPath handled in ctor
194 copy
->canonical
= canonical
? xstrdup(canonical
) : NULL
;
196 // range handled in hdrCacheInit()
198 copy
->imslen
= imslen
;
199 copy
->hier
= hier
; // Is it safe to copy? Should we?
201 copy
->errType
= errType
;
203 // XXX: what to do with copy->peer_login?
205 copy
->lastmod
= lastmod
;
207 copy
->vary_headers
= vary_headers
? xstrdup(vary_headers
) : NULL
;
208 // XXX: what to do with copy->peer_domain?
211 copy
->extacl_log
= extacl_log
;
212 copy
->extacl_message
= extacl_message
;
214 const bool inheritWorked
= copy
->inheritProperties(this);
215 assert(inheritWorked
);
221 HttpRequest::inheritProperties(const HttpMsg
*aMsg
)
223 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
227 client_addr
= aReq
->client_addr
;
228 #if FOLLOW_X_FORWARDED_FOR
229 indirect_client_addr
= aReq
->indirect_client_addr
;
231 my_addr
= aReq
->my_addr
;
233 dnsWait
= aReq
->dnsWait
;
236 adaptHistory_
= aReq
->adaptHistory();
239 icapHistory_
= aReq
->icapHistory();
242 // This may be too conservative for the 204 No Content case
243 // may eventually need cloneNullAdaptationImmune() for that.
244 flags
= aReq
->flags
.cloneAdaptationImmune();
246 errType
= aReq
->errType
;
247 errDetail
= aReq
->errDetail
;
249 auth_user_request
= aReq
->auth_user_request
;
250 extacl_user
= aReq
->extacl_user
;
251 extacl_passwd
= aReq
->extacl_passwd
;
254 myportname
= aReq
->myportname
;
256 forcedBodyContinuation
= aReq
->forcedBodyContinuation
;
258 // main property is which connection the request was received on (if any)
259 clientConnectionManager
= aReq
->clientConnectionManager
;
266 * Checks the first line of an HTTP request is valid
267 * currently just checks the request method is present.
269 * NP: Other errors are left for detection later in the parse.
272 HttpRequest::sanityCheckStartLine(MemBuf
*buf
, const size_t hdr_len
, Http::StatusCode
*error
)
274 // content is long enough to possibly hold a reply
275 // 2 being magic size of a 1-byte request method plus space delimiter
276 if ( buf
->contentSize() < 2 ) {
277 // this is ony a real error if the headers apparently complete.
279 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
280 *error
= Http::scInvalidHeader
;
285 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
287 m
.HttpRequestMethodXXX(buf
->content());
288 if (m
== Http::METHOD_NONE
) {
289 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
290 *error
= Http::scInvalidHeader
;
298 HttpRequest::parseFirstLine(const char *start
, const char *end
)
300 method
.HttpRequestMethodXXX(start
);
302 if (method
== Http::METHOD_NONE
)
305 // XXX: performance regression, strcspn() over the method bytes a second time.
306 // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start.
307 const char *t
= start
+ strcspn(start
, w_space
);
309 start
= t
+ strspn(t
, w_space
); // skip w_space after method
311 const char *ver
= findTrailingHTTPVersion(start
, end
);
316 while (xisspace(*end
)) // find prev non-space
319 ++end
; // back to space
321 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
322 debugs(73, DBG_IMPORTANT
, "parseRequestLine: Invalid HTTP identifier.");
330 if (end
< start
) // missing URI
335 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
337 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
339 * (char *) end
= save
;
348 HttpRequest::parseHeader(Http1::RequestParser
&hp
)
350 // HTTP/1 message contains "zero or more header fields"
351 // zero does not need parsing
352 if (!hp
.headerBlockSize())
355 // XXX: c_str() reallocates. performance regression.
356 const bool result
= header
.parse(hp
.mimeHeader().c_str(), hp
.headerBlockSize());
364 /* swaps out request using httpRequestPack */
366 HttpRequest::swapOut(StoreEntry
* e
)
370 packerToStoreInit(&p
, e
);
375 /* packs request-line and headers, appends <crlf> terminator */
377 HttpRequest::pack(Packer
* p
)
380 /* pack request-line */
381 packerPrintf(p
, SQUIDSBUFPH
" " SQUIDSTRINGPH
" HTTP/%d.%d\r\n",
382 SQUIDSBUFPRINT(method
.image()), SQUIDSTRINGPRINT(urlpath
),
383 http_ver
.major
, http_ver
.minor
);
387 packerAppend(p
, "\r\n", 2);
391 * A wrapper for debugObj()
394 httpRequestPack(void *obj
, Packer
*p
)
396 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
400 /* returns the length of request line + headers + crlf */
402 HttpRequest::prefixLen()
404 return method
.image().length() + 1 +
410 /* sync this routine when you update HttpRequest struct */
412 HttpRequest::hdrCacheInit()
414 HttpMsg::hdrCacheInit();
417 range
= header
.getRange();
421 Adaptation::Icap::History::Pointer
422 HttpRequest::icapHistory() const
425 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
426 icapHistory_
= new Adaptation::Icap::History();
427 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
436 Adaptation::History::Pointer
437 HttpRequest::adaptHistory(bool createIfNone
) const
439 if (!adaptHistory_
&& createIfNone
) {
440 adaptHistory_
= new Adaptation::History();
441 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
444 return adaptHistory_
;
447 Adaptation::History::Pointer
448 HttpRequest::adaptLogHistory() const
450 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
454 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
456 if (!adaptHistory_
) {
457 adaptHistory_
= them
.adaptHistory_
; // may be nil
459 // check that histories did not diverge
460 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
467 HttpRequest::multipartRangeRequest() const
469 return (range
&& range
->specs
.size() > 1);
473 HttpRequest::bodyNibbled() const
475 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
479 HttpRequest::detailError(err_type aType
, int aDetail
)
481 if (errType
|| errDetail
)
482 debugs(11, 5, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
483 debugs(11, 5, HERE
<< "current error details: " << aType
<< '/' << aDetail
);
484 // checking type and detail separately may cause inconsistency, but
485 // may result in more details available if they only become available later
493 HttpRequest::clearError()
495 debugs(11, 7, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
497 errDetail
= ERR_DETAIL_NONE
;
500 const char *HttpRequest::packableURI(bool full_uri
) const
503 return urlCanonical((HttpRequest
*)this);
506 return urlpath
.termedBuf();
511 void HttpRequest::packFirstLineInto(Packer
* p
, bool full_uri
) const
513 // form HTTP request-line
514 packerPrintf(p
, SQUIDSBUFPH
" %s HTTP/%d.%d\r\n",
515 SQUIDSBUFPRINT(method
.image()),
516 packableURI(full_uri
),
517 http_ver
.major
, http_ver
.minor
);
521 * Indicate whether or not we would expect an entity-body
522 * along with this request
525 HttpRequest::expectingBody(const HttpRequestMethod
&, int64_t &theSize
) const
527 bool expectBody
= false;
530 * Note: Checks for message validity is in clientIsContentLengthValid().
531 * this just checks if a entity-body is expected based on HTTP message syntax
533 if (header
.chunked()) {
536 } else if (content_length
>= 0) {
538 theSize
= content_length
;
548 * Create a Request from a URL and METHOD.
550 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
551 * If the request cannot be created cleanly, NULL is returned
554 HttpRequest::CreateFromUrlAndMethod(char * url
, const HttpRequestMethod
& method
)
556 return urlParse(method
, url
, NULL
);
560 * Create a Request from a URL.
562 * If the request cannot be created cleanly, NULL is returned
565 HttpRequest::CreateFromUrl(char * url
)
567 return urlParse(Http::METHOD_GET
, url
, NULL
);
571 * Are responses to this request possible cacheable ?
572 * If false then no matter what the response must not be cached.
575 HttpRequest::maybeCacheable()
577 // Intercepted request with Host: header which cannot be trusted.
578 // Because it failed verification, or someone bypassed the security tests
579 // we cannot cache the reponse for sharing between clients.
580 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
581 if (!flags
.hostVerified
&& (flags
.intercepted
|| flags
.interceptTproxy
))
584 switch (url
.getScheme()) {
585 case AnyP::PROTO_HTTP
:
586 case AnyP::PROTO_HTTPS
:
587 if (!method
.respMaybeCacheable())
590 // XXX: this would seem the correct place to detect request cache-controls
591 // no-store, private and related which block cacheability
594 case AnyP::PROTO_GOPHER
:
595 if (!gopherCachable(this))
599 case AnyP::PROTO_CACHE_OBJECT
:
602 //case AnyP::PROTO_FTP:
611 HttpRequest::conditional() const
614 header
.has(HDR_IF_MATCH
) ||
615 header
.has(HDR_IF_NONE_MATCH
);
619 HttpRequest::recordLookup(const Dns::LookupDetails
&dns
)
621 if (dns
.wait
>= 0) { // known delay
622 if (dnsWait
>= 0) // have recorded DNS wait before
630 HttpRequest::getRangeOffsetLimit()
632 /* -2 is the starting value of rangeOffsetLimit.
633 * If it is -2, that means we haven't checked it yet.
634 * Otherwise, return the current value */
635 if (rangeOffsetLimit
!= -2)
636 return rangeOffsetLimit
;
638 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
640 ACLFilledChecklist
ch(NULL
, this, NULL
);
641 ch
.src_addr
= client_addr
;
642 ch
.my_addr
= my_addr
;
644 for (AclSizeLimit
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
645 /* if there is no ACL list or if the ACLs listed match use this limit value */
646 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
647 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
648 rangeOffsetLimit
= l
->size
; // may be -1
653 return rangeOffsetLimit
;
657 HttpRequest::ignoreRange(const char *reason
)
660 debugs(73, 3, static_cast<void*>(range
) << " for " << reason
);
664 // Some callers also reset isRanged but it may not be safe for all callers:
665 // isRanged is used to determine whether a weak ETag comparison is allowed,
666 // and that check should not ignore the Range header if it was present.
667 // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we?
671 HttpRequest::canHandle1xx() const
673 // old clients do not support 1xx unless they sent Expect: 100-continue
674 // (we reject all other HDR_EXPECT values so just check for HDR_EXPECT)
675 if (http_ver
<= Http::ProtocolVersion(1,0) && !header
.has(HDR_EXPECT
))
678 // others must support 1xx control messages
683 HttpRequest::pinnedConnection()
685 if (clientConnectionManager
.valid() && clientConnectionManager
->pinning
.pinned
)
686 return clientConnectionManager
.get();
691 HttpRequest::storeId()
693 if (store_id
.size() != 0) {
694 debugs(73, 3, "sent back store_id:" << store_id
);
696 return store_id
.termedBuf();
698 debugs(73, 3, "sent back canonicalUrl:" << urlCanonical(this) );
700 return urlCanonical(this);