]>
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
;
75 memset(&flags
, '\0', sizeof(flags
));
80 client_addr
.setEmpty();
86 errDetail
= ERR_DETAIL_NONE
;
87 peer_login
= NULL
; // not allocated/deallocated by this class
88 peer_domain
= NULL
; // not allocated/deallocated by this class
91 myportname
= null_string
;
94 extacl_user
= null_string
;
95 extacl_passwd
= null_string
;
97 extacl_log
= null_string
;
98 extacl_message
= null_string
;
99 pstate
= psReadyToParseStartLine
;
100 #if FOLLOW_X_FORWARDED_FOR
101 indirect_client_addr
.setEmpty();
102 #endif /* FOLLOW_X_FORWARDED_FOR */
104 adaptHistory_
= NULL
;
109 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
110 forcedBodyContinuation
= false;
116 // we used to assert that the pipe is NULL, but now the request only
117 // points to a pipe that is owned and initiated by another object.
120 auth_user_request
= NULL
;
122 safe_free(vary_headers
);
129 delete cache_control
;
130 cache_control
= NULL
;
145 extacl_passwd
.clean();
149 extacl_message
.clean();
154 adaptHistory_
= NULL
;
169 HttpRequest::clone() const
171 HttpRequest
*copy
= new HttpRequest();
172 copy
->method
= method
;
173 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
174 copy
->header
.append(&header
);
175 copy
->hdrCacheInit();
176 copy
->hdr_sz
= hdr_sz
;
177 copy
->http_ver
= http_ver
;
178 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
179 copy
->body_pipe
= body_pipe
;
181 copy
->url
.setScheme(url
.getScheme());
182 copy
->url
.userInfo(url
.userInfo());
183 copy
->url
.host(url
.host());
184 copy
->url
.port(url
.port());
185 copy
->url
.path(url
.path());
187 // range handled in hdrCacheInit()
189 copy
->imslen
= imslen
;
190 copy
->hier
= hier
; // Is it safe to copy? Should we?
192 copy
->errType
= errType
;
194 // XXX: what to do with copy->peer_login?
196 copy
->lastmod
= lastmod
;
198 copy
->vary_headers
= vary_headers
? xstrdup(vary_headers
) : NULL
;
199 // XXX: what to do with copy->peer_domain?
202 copy
->extacl_log
= extacl_log
;
203 copy
->extacl_message
= extacl_message
;
205 const bool inheritWorked
= copy
->inheritProperties(this);
206 assert(inheritWorked
);
212 HttpRequest::inheritProperties(const HttpMsg
*aMsg
)
214 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
218 client_addr
= aReq
->client_addr
;
219 #if FOLLOW_X_FORWARDED_FOR
220 indirect_client_addr
= aReq
->indirect_client_addr
;
222 my_addr
= aReq
->my_addr
;
224 dnsWait
= aReq
->dnsWait
;
227 adaptHistory_
= aReq
->adaptHistory();
230 icapHistory_
= aReq
->icapHistory();
233 // This may be too conservative for the 204 No Content case
234 // may eventually need cloneNullAdaptationImmune() for that.
235 flags
= aReq
->flags
.cloneAdaptationImmune();
237 errType
= aReq
->errType
;
238 errDetail
= aReq
->errDetail
;
240 auth_user_request
= aReq
->auth_user_request
;
241 extacl_user
= aReq
->extacl_user
;
242 extacl_passwd
= aReq
->extacl_passwd
;
245 myportname
= aReq
->myportname
;
247 forcedBodyContinuation
= aReq
->forcedBodyContinuation
;
249 // main property is which connection the request was received on (if any)
250 clientConnectionManager
= aReq
->clientConnectionManager
;
257 * Checks the first line of an HTTP request is valid
258 * currently just checks the request method is present.
260 * NP: Other errors are left for detection later in the parse.
263 HttpRequest::sanityCheckStartLine(const char *buf
, const size_t hdr_len
, Http::StatusCode
*error
)
265 // content is long enough to possibly hold a reply
266 // 2 being magic size of a 1-byte request method plus space delimiter
268 // this is ony a real error if the headers apparently complete.
270 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
271 *error
= Http::scInvalidHeader
;
276 /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */
278 m
.HttpRequestMethodXXX(buf
);
279 if (m
== Http::METHOD_NONE
) {
280 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
281 *error
= Http::scInvalidHeader
;
289 HttpRequest::parseFirstLine(const char *start
, const char *end
)
291 method
.HttpRequestMethodXXX(start
);
293 if (method
== Http::METHOD_NONE
)
296 // XXX: performance regression, strcspn() over the method bytes a second time.
297 // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start.
298 const char *t
= start
+ strcspn(start
, w_space
);
300 start
= t
+ strspn(t
, w_space
); // skip w_space after method
302 const char *ver
= findTrailingHTTPVersion(start
, end
);
307 while (xisspace(*end
)) // find prev non-space
310 ++end
; // back to space
312 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
313 debugs(73, DBG_IMPORTANT
, "parseRequestLine: Invalid HTTP identifier.");
321 if (end
< start
) // missing URI
326 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
328 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
330 * (char *) end
= save
;
338 /* swaps out request using httpRequestPack */
340 HttpRequest::swapOut(StoreEntry
* e
)
348 /* packs request-line and headers, appends <crlf> terminator */
350 HttpRequest::pack(Packable
* p
)
353 /* pack request-line */
354 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
355 SQUIDSBUFPRINT(method
.image()), SQUIDSBUFPRINT(url
.path()),
356 http_ver
.major
, http_ver
.minor
);
360 p
->append("\r\n", 2);
364 * A wrapper for debugObj()
367 httpRequestPack(void *obj
, Packable
*p
)
369 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
373 /* returns the length of request line + headers + crlf */
375 HttpRequest::prefixLen() const
377 return method
.image().length() + 1 +
378 url
.path().length() + 1 +
383 /* sync this routine when you update HttpRequest struct */
385 HttpRequest::hdrCacheInit()
387 HttpMsg::hdrCacheInit();
390 range
= header
.getRange();
394 Adaptation::Icap::History::Pointer
395 HttpRequest::icapHistory() const
398 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
399 icapHistory_
= new Adaptation::Icap::History();
400 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
409 Adaptation::History::Pointer
410 HttpRequest::adaptHistory(bool createIfNone
) const
412 if (!adaptHistory_
&& createIfNone
) {
413 adaptHistory_
= new Adaptation::History();
414 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
417 return adaptHistory_
;
420 Adaptation::History::Pointer
421 HttpRequest::adaptLogHistory() const
423 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
427 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
429 if (!adaptHistory_
) {
430 adaptHistory_
= them
.adaptHistory_
; // may be nil
432 // check that histories did not diverge
433 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
440 HttpRequest::multipartRangeRequest() const
442 return (range
&& range
->specs
.size() > 1);
446 HttpRequest::bodyNibbled() const
448 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
452 HttpRequest::detailError(err_type aType
, int aDetail
)
454 if (errType
|| errDetail
)
455 debugs(11, 5, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
456 debugs(11, 5, HERE
<< "current error details: " << aType
<< '/' << aDetail
);
457 // checking type and detail separately may cause inconsistency, but
458 // may result in more details available if they only become available later
466 HttpRequest::clearError()
468 debugs(11, 7, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
470 errDetail
= ERR_DETAIL_NONE
;
474 HttpRequest::packFirstLineInto(Packable
* p
, bool full_uri
) const
476 const SBuf
tmp(full_uri
? effectiveRequestUri() : url
.path());
478 // form HTTP request-line
479 p
->appendf(SQUIDSBUFPH
" " SQUIDSBUFPH
" HTTP/%d.%d\r\n",
480 SQUIDSBUFPRINT(method
.image()),
482 http_ver
.major
, http_ver
.minor
);
486 * Indicate whether or not we would expect an entity-body
487 * along with this request
490 HttpRequest::expectingBody(const HttpRequestMethod
&, int64_t &theSize
) const
492 bool expectBody
= false;
495 * Note: Checks for message validity is in clientIsContentLengthValid().
496 * this just checks if a entity-body is expected based on HTTP message syntax
498 if (header
.chunked()) {
501 } else if (content_length
>= 0) {
503 theSize
= content_length
;
513 * Create a Request from a URL and METHOD.
515 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
516 * If the request cannot be created cleanly, NULL is returned
519 HttpRequest::CreateFromUrlAndMethod(char * url
, const HttpRequestMethod
& method
)
521 return urlParse(method
, url
, NULL
);
525 * Create a Request from a URL.
527 * If the request cannot be created cleanly, NULL is returned
530 HttpRequest::CreateFromUrl(char * url
)
532 return urlParse(Http::METHOD_GET
, url
, NULL
);
536 * Are responses to this request possible cacheable ?
537 * If false then no matter what the response must not be cached.
540 HttpRequest::maybeCacheable()
542 // Intercepted request with Host: header which cannot be trusted.
543 // Because it failed verification, or someone bypassed the security tests
544 // we cannot cache the reponse for sharing between clients.
545 // TODO: update cache to store for particular clients only (going to same Host: and destination IP)
546 if (!flags
.hostVerified
&& (flags
.intercepted
|| flags
.interceptTproxy
))
549 switch (url
.getScheme()) {
550 case AnyP::PROTO_HTTP
:
551 case AnyP::PROTO_HTTPS
:
552 if (!method
.respMaybeCacheable())
555 // XXX: this would seem the correct place to detect request cache-controls
556 // no-store, private and related which block cacheability
559 case AnyP::PROTO_GOPHER
:
560 if (!gopherCachable(this))
564 case AnyP::PROTO_CACHE_OBJECT
:
567 //case AnyP::PROTO_FTP:
576 HttpRequest::conditional() const
579 header
.has(Http::HdrType::IF_MATCH
) ||
580 header
.has(Http::HdrType::IF_NONE_MATCH
);
584 HttpRequest::recordLookup(const Dns::LookupDetails
&dns
)
586 if (dns
.wait
>= 0) { // known delay
587 if (dnsWait
>= 0) // have recorded DNS wait before
595 HttpRequest::getRangeOffsetLimit()
597 /* -2 is the starting value of rangeOffsetLimit.
598 * If it is -2, that means we haven't checked it yet.
599 * Otherwise, return the current value */
600 if (rangeOffsetLimit
!= -2)
601 return rangeOffsetLimit
;
603 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
605 ACLFilledChecklist
ch(NULL
, this, NULL
);
606 ch
.src_addr
= client_addr
;
607 ch
.my_addr
= my_addr
;
609 for (AclSizeLimit
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
610 /* if there is no ACL list or if the ACLs listed match use this limit value */
611 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
612 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
613 rangeOffsetLimit
= l
->size
; // may be -1
618 return rangeOffsetLimit
;
622 HttpRequest::ignoreRange(const char *reason
)
625 debugs(73, 3, static_cast<void*>(range
) << " for " << reason
);
629 // Some callers also reset isRanged but it may not be safe for all callers:
630 // isRanged is used to determine whether a weak ETag comparison is allowed,
631 // and that check should not ignore the Range header if it was present.
632 // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we?
636 HttpRequest::canHandle1xx() const
638 // old clients do not support 1xx unless they sent Expect: 100-continue
639 // (we reject all other Http::HdrType::EXPECT values so just check for Http::HdrType::EXPECT)
640 if (http_ver
<= Http::ProtocolVersion(1,0) && !header
.has(Http::HdrType::EXPECT
))
643 // others must support 1xx control messages
648 HttpRequest::pinnedConnection()
650 if (clientConnectionManager
.valid() && clientConnectionManager
->pinning
.pinned
)
651 return clientConnectionManager
.get();
656 HttpRequest::storeId()
658 if (store_id
.size() != 0) {
659 debugs(73, 3, "sent back store_id: " << store_id
);
660 return SBuf(store_id
);
662 debugs(73, 3, "sent back effectiveRequestUrl: " << effectiveRequestUri());
663 return effectiveRequestUri();
667 HttpRequest::effectiveRequestUri() const
669 if (method
.id() == Http::METHOD_CONNECT
)
670 return url
.authority(true); // host:port
671 return url
.absolute();