]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpRequest.cc
5 * DEBUG: section 73 HTTP Request
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
37 #include "squid-old.h"
38 #include "DnsLookupDetails.h"
39 #include "HttpRequest.h"
40 #include "HttpHdrCc.h"
42 #include "auth/UserRequest.h"
44 #include "HttpHeaderRange.h"
45 #include "log/Config.h"
49 #include "adaptation/icap/icap_log.h"
51 #include "acl/FilledChecklist.h"
52 #include "err_detail_type.h"
54 HttpRequest::HttpRequest() : HttpMsg(hoRequest
)
59 HttpRequest::HttpRequest(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aUrlpath
) : HttpMsg(hoRequest
)
61 static unsigned int id
= 1;
62 debugs(93,7, HERE
<< "constructed, this=" << this << " id=" << ++id
);
64 initHTTP(aMethod
, aProtocol
, aUrlpath
);
67 HttpRequest::~HttpRequest()
70 debugs(93,7, HERE
<< "destructed, this=" << this);
74 HttpRequest::initHTTP(const HttpRequestMethod
& aMethod
, AnyP::ProtocolType aProtocol
, const char *aUrlpath
)
85 protocol
= AnyP::PROTO_NONE
;
91 auth_user_request
= NULL
;
95 memset(&flags
, '\0', sizeof(flags
));
100 client_addr
.SetEmpty();
106 errDetail
= ERR_DETAIL_NONE
;
107 peer_login
= NULL
; // not allocated/deallocated by this class
108 peer_domain
= NULL
; // not allocated/deallocated by this class
110 myportname
= null_string
;
113 extacl_user
= null_string
;
114 extacl_passwd
= null_string
;
116 extacl_log
= null_string
;
117 extacl_message
= null_string
;
118 pstate
= psReadyToParseStartLine
;
119 #if FOLLOW_X_FORWARDED_FOR
120 indirect_client_addr
.SetEmpty();
121 #endif /* FOLLOW_X_FORWARDED_FOR */
123 adaptHistory_
= NULL
;
128 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
134 // we used to assert that the pipe is NULL, but now the request only
135 // points to a pipe that is owned and initiated by another object.
138 auth_user_request
= NULL
;
140 safe_free(canonical
);
142 safe_free(vary_headers
);
149 delete cache_control
;
150 cache_control
= NULL
;
163 extacl_passwd
.clean();
167 extacl_message
.clean();
170 adaptHistory_
= NULL
;
185 HttpRequest::clone() const
187 HttpRequest
*copy
= new HttpRequest(method
, protocol
, urlpath
.termedBuf());
188 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
189 copy
->header
.append(&header
);
190 copy
->hdrCacheInit();
191 copy
->hdr_sz
= hdr_sz
;
192 copy
->http_ver
= http_ver
;
193 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
194 copy
->body_pipe
= body_pipe
;
196 strncpy(copy
->login
, login
, sizeof(login
)); // MAX_LOGIN_SZ
197 strncpy(copy
->host
, host
, sizeof(host
)); // SQUIDHOSTNAMELEN
198 copy
->host_addr
= host_addr
;
201 // urlPath handled in ctor
202 copy
->canonical
= canonical
? xstrdup(canonical
) : NULL
;
204 // range handled in hdrCacheInit()
206 copy
->imslen
= imslen
;
207 copy
->hier
= hier
; // Is it safe to copy? Should we?
209 copy
->errType
= errType
;
211 // XXX: what to do with copy->peer_login?
213 copy
->lastmod
= lastmod
;
214 copy
->vary_headers
= vary_headers
? xstrdup(vary_headers
) : NULL
;
215 // XXX: what to do with copy->peer_domain?
217 copy
->myportname
= myportname
;
220 copy
->extacl_user
= extacl_user
;
221 copy
->extacl_passwd
= extacl_passwd
;
223 copy
->extacl_log
= extacl_log
;
224 copy
->extacl_message
= extacl_message
;
226 assert(copy
->inheritProperties(this));
232 HttpRequest::inheritProperties(const HttpMsg
*aMsg
)
234 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
238 client_addr
= aReq
->client_addr
;
239 #if FOLLOW_X_FORWARDED_FOR
240 indirect_client_addr
= aReq
->indirect_client_addr
;
242 my_addr
= aReq
->my_addr
;
244 dnsWait
= aReq
->dnsWait
;
247 adaptHistory_
= aReq
->adaptHistory();
250 icapHistory_
= aReq
->icapHistory();
253 // This may be too conservative for the 204 No Content case
254 // may eventually need cloneNullAdaptationImmune() for that.
255 flags
= aReq
->flags
.cloneAdaptationImmune();
257 errType
= aReq
->errType
;
258 errDetail
= aReq
->errDetail
;
260 auth_user_request
= aReq
->auth_user_request
;
263 // main property is which connection the request was received on (if any)
264 clientConnectionManager
= aReq
->clientConnectionManager
;
269 * Checks the first line of an HTTP request is valid
270 * currently just checks the request method is present.
272 * NP: Other errors are left for detection later in the parse.
275 HttpRequest::sanityCheckStartLine(MemBuf
*buf
, const size_t hdr_len
, http_status
*error
)
277 // content is long enough to possibly hold a reply
278 // 2 being magic size of a 1-byte request method plus space delimiter
279 if ( buf
->contentSize() < 2 ) {
280 // this is ony a real error if the headers apparently complete.
282 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
283 *error
= HTTP_INVALID_HEADER
;
288 /* See if the request buffer starts with a known HTTP request method. */
289 if (HttpRequestMethod(buf
->content(),NULL
) == METHOD_NONE
) {
290 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
291 *error
= HTTP_INVALID_HEADER
;
299 HttpRequest::parseFirstLine(const char *start
, const char *end
)
301 const char *t
= start
+ strcspn(start
, w_space
);
302 method
= HttpRequestMethod(start
, t
);
304 if (method
== METHOD_NONE
)
307 start
= t
+ strspn(t
, w_space
);
309 const char *ver
= findTrailingHTTPVersion(start
, end
);
314 while (xisspace(*end
)) // find prev non-space
317 end
++; // back to space
319 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
320 debugs(73, 1, "parseRequestLine: Invalid HTTP identifier.");
328 if (end
< start
) // missing URI
333 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
335 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
337 * (char *) end
= save
;
346 HttpRequest::parseHeader(const char *parse_start
, int len
)
348 const char *blk_start
, *blk_end
;
350 if (!httpMsgIsolateHeaders(&parse_start
, len
, &blk_start
, &blk_end
))
353 int result
= header
.parse(blk_start
, blk_end
);
361 /* swaps out request using httpRequestPack */
363 HttpRequest::swapOut(StoreEntry
* e
)
367 packerToStoreInit(&p
, e
);
372 /* packs request-line and headers, appends <crlf> terminator */
374 HttpRequest::pack(Packer
* p
)
377 /* pack request-line */
378 packerPrintf(p
, "%s " SQUIDSTRINGPH
" HTTP/%d.%d\r\n",
379 RequestMethodStr(method
), SQUIDSTRINGPRINT(urlpath
),
380 http_ver
.major
, http_ver
.minor
);
384 packerAppend(p
, "\r\n", 2);
388 * A wrapper for debugObj()
391 httpRequestPack(void *obj
, Packer
*p
)
393 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
397 /* returns the length of request line + headers + crlf */
399 HttpRequest::prefixLen()
401 return strlen(RequestMethodStr(method
)) + 1 +
407 /* sync this routine when you update HttpRequest struct */
409 HttpRequest::hdrCacheInit()
411 HttpMsg::hdrCacheInit();
414 range
= header
.getRange();
419 request_flags::resetTCP() const
421 return reset_tcp
!= 0;
425 request_flags::setResetTCP()
427 debugs(73, 9, "request_flags::setResetTCP");
432 request_flags::clearResetTCP()
434 debugs(73, 9, "request_flags::clearResetTCP");
439 Adaptation::Icap::History::Pointer
440 HttpRequest::icapHistory() const
443 if (Log::TheConfig
.hasIcapToken
|| IcapLogfileStatus
== LOG_ENABLE
) {
444 icapHistory_
= new Adaptation::Icap::History();
445 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
454 Adaptation::History::Pointer
455 HttpRequest::adaptHistory(bool createIfNone
) const
457 if (!adaptHistory_
&& createIfNone
) {
458 adaptHistory_
= new Adaptation::History();
459 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
462 return adaptHistory_
;
465 Adaptation::History::Pointer
466 HttpRequest::adaptLogHistory() const
468 return HttpRequest::adaptHistory(Log::TheConfig
.hasAdaptToken
);
472 HttpRequest::adaptHistoryImport(const HttpRequest
&them
)
474 if (!adaptHistory_
) {
475 adaptHistory_
= them
.adaptHistory_
; // may be nil
477 // check that histories did not diverge
478 Must(!them
.adaptHistory_
|| them
.adaptHistory_
== adaptHistory_
);
485 HttpRequest::multipartRangeRequest() const
487 return (range
&& range
->specs
.count
> 1);
491 request_flags::destinationIPLookupCompleted()
493 destinationIPLookedUp_
= true;
497 request_flags::destinationIPLookedUp() const
499 return destinationIPLookedUp_
;
503 request_flags::cloneAdaptationImmune() const
505 // At the time of writing, all flags where either safe to copy after
506 // adaptation or were not set at the time of the adaptation. If there
507 // are flags that are different, they should be cleared in the clone.
512 HttpRequest::bodyNibbled() const
514 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
518 HttpRequest::detailError(err_type aType
, int aDetail
)
520 if (errType
|| errDetail
)
521 debugs(11, 5, HERE
<< "old error details: " << errType
<< '/' << errDetail
);
522 debugs(11, 5, HERE
<< "current error details: " << aType
<< '/' << aDetail
);
523 // checking type and detail separately may cause inconsistency, but
524 // may result in more details available if they only become available later
531 const char *HttpRequest::packableURI(bool full_uri
) const
534 return urlCanonical((HttpRequest
*)this);
537 return urlpath
.termedBuf();
542 void HttpRequest::packFirstLineInto(Packer
* p
, bool full_uri
) const
544 // form HTTP request-line
545 packerPrintf(p
, "%s %s HTTP/%d.%d\r\n",
546 RequestMethodStr(method
),
547 packableURI(full_uri
),
548 http_ver
.major
, http_ver
.minor
);
552 * Indicate whether or not we would expect an entity-body
553 * along with this request
556 HttpRequest::expectingBody(const HttpRequestMethod
& unused
, int64_t& theSize
) const
558 bool expectBody
= false;
561 * Note: Checks for message validity is in clientIsContentLengthValid().
562 * this just checks if a entity-body is expected based on HTTP message syntax
564 if (header
.chunked()) {
567 } else if (content_length
>= 0) {
569 theSize
= content_length
;
579 * Create a Request from a URL and METHOD.
581 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
582 * If the request cannot be created cleanly, NULL is returned
585 HttpRequest::CreateFromUrlAndMethod(char * url
, const HttpRequestMethod
& method
)
587 return urlParse(method
, url
, NULL
);
591 * Create a Request from a URL.
593 * If the request cannot be created cleanly, NULL is returned
596 HttpRequest::CreateFromUrl(char * url
)
598 return urlParse(METHOD_GET
, url
, NULL
);
602 * Are responses to this request possible cacheable ?
603 * If false then no matter what the response must not be cached.
606 HttpRequest::cacheable() const
608 if (protocol
== AnyP::PROTO_HTTP
)
609 return httpCachable(method
);
612 * The below looks questionable: what non HTTP protocols use connect,
613 * trace, put and post? RC
616 if (!method
.isCacheble())
620 * XXX POST may be cached sometimes.. ignored
623 if (protocol
== AnyP::PROTO_GOPHER
)
624 return gopherCachable(this);
626 if (protocol
== AnyP::PROTO_CACHE_OBJECT
)
633 HttpRequest::conditional() const
636 header
.has(HDR_IF_MATCH
) ||
637 header
.has(HDR_IF_NONE_MATCH
);
641 HttpRequest::recordLookup(const DnsLookupDetails
&dns
)
643 if (dns
.wait
>= 0) { // known delay
644 if (dnsWait
>= 0) // have recorded DNS wait before
652 HttpRequest::getRangeOffsetLimit()
654 /* -2 is the starting value of rangeOffsetLimit.
655 * If it is -2, that means we haven't checked it yet.
656 * Otherwise, return the current value */
657 if (rangeOffsetLimit
!= -2)
658 return rangeOffsetLimit
;
660 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
662 ACLFilledChecklist
ch(NULL
, this, NULL
);
663 ch
.src_addr
= client_addr
;
664 ch
.my_addr
= my_addr
;
666 for (acl_size_t
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
667 /* if there is no ACL list or if the ACLs listed match use this limit value */
668 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
669 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
670 rangeOffsetLimit
= l
->size
; // may be -1
675 return rangeOffsetLimit
;
679 HttpRequest::canHandle1xx() const
681 // old clients do not support 1xx unless they sent Expect: 100-continue
682 // (we reject all other HDR_EXPECT values so just check for HDR_EXPECT)
683 if (http_ver
<= HttpVersion(1,0) && !header
.has(HDR_EXPECT
))
686 // others must support 1xx control messages