]>
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>
38 #include "HttpRequest.h"
39 #include "auth/UserRequest.h"
40 #include "HttpHeaderRange.h"
44 #include "adaptation/icap/icap_log.h"
46 #include "acl/FilledChecklist.h"
48 HttpRequest::HttpRequest() : HttpMsg(hoRequest
)
53 HttpRequest::HttpRequest(const HttpRequestMethod
& aMethod
, protocol_t aProtocol
, const char *aUrlpath
) : HttpMsg(hoRequest
)
56 initHTTP(aMethod
, aProtocol
, aUrlpath
);
59 HttpRequest::~HttpRequest()
65 HttpRequest::initHTTP(const HttpRequestMethod
& aMethod
, protocol_t aProtocol
, const char *aUrlpath
)
76 protocol
= PROTO_NONE
;
81 auth_user_request
= NULL
;
82 pinned_connection
= NULL
;
85 memset(&flags
, '\0', sizeof(flags
));
91 client_addr
.SetEmpty();
101 peer_login
= NULL
; // not allocated/deallocated by this class
102 peer_domain
= NULL
; // not allocated/deallocated by this class
105 extacl_user
= null_string
;
106 extacl_passwd
= null_string
;
107 extacl_log
= null_string
;
108 extacl_message
= null_string
;
109 pstate
= psReadyToParseStartLine
;
110 #if FOLLOW_X_FORWARDED_FOR
111 indirect_client_addr
.SetEmpty();
112 #endif /* FOLLOW_X_FORWARDED_FOR */
114 adaptHistory_
= NULL
;
119 rangeOffsetLimit
= -2; //a value of -2 means not checked yet
125 // we used to assert that the pipe is NULL, but now the request only
126 // points to a pipe that is owned and initiated by another object.
129 auth_user_request
= NULL
;
131 safe_free(canonical
);
133 safe_free(vary_headers
);
140 httpHdrCcDestroy(cache_control
);
141 cache_control
= NULL
;
149 if (pinned_connection
)
150 cbdataReferenceDone(pinned_connection
);
156 extacl_passwd
.clean();
160 extacl_message
.clean();
163 adaptHistory_
= NULL
;
178 HttpRequest::clone() const
180 HttpRequest
*copy
= new HttpRequest(method
, protocol
, urlpath
.termedBuf());
181 // TODO: move common cloning clone to Msg::copyTo() or copy ctor
182 copy
->header
.append(&header
);
183 copy
->hdrCacheInit();
184 copy
->hdr_sz
= hdr_sz
;
185 copy
->http_ver
= http_ver
;
186 copy
->pstate
= pstate
; // TODO: should we assert a specific state here?
187 copy
->body_pipe
= body_pipe
;
189 strncpy(copy
->login
, login
, sizeof(login
)); // MAX_LOGIN_SZ
190 strncpy(copy
->host
, host
, sizeof(host
)); // SQUIDHOSTNAMELEN
191 copy
->host_addr
= host_addr
;
194 // urlPath handled in ctor
195 copy
->canonical
= canonical
? xstrdup(canonical
) : NULL
;
197 // range handled in hdrCacheInit()
199 copy
->imslen
= imslen
;
200 copy
->max_forwards
= max_forwards
;
201 copy
->hier
= hier
; // Is it safe to copy? Should we?
203 copy
->errType
= errType
;
205 // XXX: what to do with copy->peer_login?
207 copy
->lastmod
= lastmod
;
208 copy
->vary_headers
= vary_headers
? xstrdup(vary_headers
) : NULL
;
209 // XXX: what to do with copy->peer_domain?
212 copy
->extacl_user
= extacl_user
;
213 copy
->extacl_passwd
= extacl_passwd
;
214 copy
->extacl_log
= extacl_log
;
215 copy
->extacl_message
= extacl_message
;
217 assert(copy
->inheritProperties(this));
223 * Checks the first line of an HTTP request is valid
224 * currently just checks the request method is present.
226 * NP: Other errors are left for detection later in the parse.
229 HttpRequest::sanityCheckStartLine(MemBuf
*buf
, const size_t hdr_len
, http_status
*error
)
231 // content is long enough to possibly hold a reply
232 // 2 being magic size of a 1-byte request method plus space delimiter
233 if ( buf
->contentSize() < 2 ) {
234 // this is ony a real error if the headers apparently complete.
236 debugs(58, 3, HERE
<< "Too large request header (" << hdr_len
<< " bytes)");
237 *error
= HTTP_INVALID_HEADER
;
242 /* See if the request buffer starts with a known HTTP request method. */
243 if (HttpRequestMethod(buf
->content(),NULL
) == METHOD_NONE
) {
244 debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method");
245 *error
= HTTP_INVALID_HEADER
;
253 HttpRequest::parseFirstLine(const char *start
, const char *end
)
255 const char *t
= start
+ strcspn(start
, w_space
);
256 method
= HttpRequestMethod(start
, t
);
258 if (method
== METHOD_NONE
)
261 start
= t
+ strspn(t
, w_space
);
263 const char *ver
= findTrailingHTTPVersion(start
, end
);
268 while (xisspace(*end
)) // find prev non-space
271 end
++; // back to space
273 if (2 != sscanf(ver
+ 5, "%d.%d", &http_ver
.major
, &http_ver
.minor
)) {
274 debugs(73, 1, "parseRequestLine: Invalid HTTP identifier.");
282 if (end
< start
) // missing URI
287 * (char *) end
= '\0'; // temp terminate URI, XXX dangerous?
289 HttpRequest
*tmp
= urlParse(method
, (char *) start
, this);
291 * (char *) end
= save
;
300 HttpRequest::parseHeader(const char *parse_start
, int len
)
302 const char *blk_start
, *blk_end
;
304 if (!httpMsgIsolateHeaders(&parse_start
, len
, &blk_start
, &blk_end
))
307 int result
= header
.parse(blk_start
, blk_end
);
315 /* swaps out request using httpRequestPack */
317 HttpRequest::swapOut(StoreEntry
* e
)
321 packerToStoreInit(&p
, e
);
326 /* packs request-line and headers, appends <crlf> terminator */
328 HttpRequest::pack(Packer
* p
)
331 /* pack request-line */
332 packerPrintf(p
, "%s " SQUIDSTRINGPH
" HTTP/%d.%d\r\n",
333 RequestMethodStr(method
), SQUIDSTRINGPRINT(urlpath
),
334 http_ver
.major
, http_ver
.minor
);
338 packerAppend(p
, "\r\n", 2);
342 * A wrapper for debugObj()
345 httpRequestPack(void *obj
, Packer
*p
)
347 HttpRequest
*request
= static_cast<HttpRequest
*>(obj
);
351 /* returns the length of request line + headers + crlf */
353 HttpRequest::prefixLen()
355 return strlen(RequestMethodStr(method
)) + 1 +
361 /* sync this routine when you update HttpRequest struct */
363 HttpRequest::hdrCacheInit()
365 HttpMsg::hdrCacheInit();
368 range
= header
.getRange();
373 request_flags::resetTCP() const
375 return reset_tcp
!= 0;
379 request_flags::setResetTCP()
381 debugs(73, 9, "request_flags::setResetTCP");
386 request_flags::clearResetTCP()
388 debugs(73, 9, "request_flags::clearResetTCP");
393 Adaptation::Icap::History::Pointer
394 HttpRequest::icapHistory() const
397 if ((LogfileStatus
== LOG_ENABLE
&& alLogformatHasIcapToken
) ||
398 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 const bool loggingNeedsHistory
= (LogfileStatus
== LOG_ENABLE
) &&
424 alLogformatHasAdaptToken
; // TODO: make global to remove this method?
425 return HttpRequest::adaptHistory(loggingNeedsHistory
);
431 HttpRequest::multipartRangeRequest() const
433 return (range
&& range
->specs
.count
> 1);
437 request_flags::destinationIPLookupCompleted()
439 destinationIPLookedUp_
= true;
443 request_flags::destinationIPLookedUp() const
445 return destinationIPLookedUp_
;
449 request_flags::cloneAdaptationImmune() const
451 // At the time of writing, all flags where either safe to copy after
452 // adaptation or were not set at the time of the adaptation. If there
453 // are flags that are different, they should be cleared in the clone.
458 HttpRequest::bodyNibbled() const
460 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
463 const char *HttpRequest::packableURI(bool full_uri
) const
466 return urlCanonical((HttpRequest
*)this);
469 return urlpath
.termedBuf();
474 void HttpRequest::packFirstLineInto(Packer
* p
, bool full_uri
) const
476 // form HTTP request-line
477 packerPrintf(p
, "%s %s HTTP/%d.%d\r\n",
478 RequestMethodStr(method
),
479 packableURI(full_uri
),
480 http_ver
.major
, http_ver
.minor
);
484 * Indicate whether or not we would usually expect an entity-body
485 * along with this request
488 HttpRequest::expectingBody(const HttpRequestMethod
& unused
, int64_t& theSize
) const
490 bool expectBody
= false;
493 * GET and HEAD don't usually have bodies, but we should be prepared
494 * to accept one if the request_entities directive is set
497 if (method
== METHOD_GET
|| method
== METHOD_HEAD
)
498 expectBody
= Config
.onoff
.request_entities
? true : false;
499 else if (method
== METHOD_PUT
|| method
== METHOD_POST
)
501 else if (header
.chunked())
503 else if (content_length
>= 0)
509 if (header
.chunked())
511 else if (content_length
>= 0)
512 theSize
= content_length
;
521 * Create a Request from a URL and METHOD.
523 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
524 * If the request cannot be created cleanly, NULL is returned
527 HttpRequest::CreateFromUrlAndMethod(char * url
, const HttpRequestMethod
& method
)
529 return urlParse(method
, url
, NULL
);
533 * Create a Request from a URL.
535 * If the request cannot be created cleanly, NULL is returned
538 HttpRequest::CreateFromUrl(char * url
)
540 return urlParse(METHOD_GET
, url
, NULL
);
544 * Are responses to this request possible cacheable ?
545 * If false then no matter what the response must not be cached.
548 HttpRequest::cacheable() const
550 if (protocol
== PROTO_HTTP
)
551 return httpCachable(method
);
554 * The below looks questionable: what non HTTP protocols use connect,
555 * trace, put and post? RC
558 if (!method
.isCacheble())
562 * XXX POST may be cached sometimes.. ignored
565 if (protocol
== PROTO_GOPHER
)
566 return gopherCachable(this);
568 if (protocol
== PROTO_CACHEOBJ
)
574 bool HttpRequest::inheritProperties(const HttpMsg
*aMsg
)
576 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
580 client_addr
= aReq
->client_addr
;
581 #if FOLLOW_X_FORWARDED_FOR
582 indirect_client_addr
= aReq
->indirect_client_addr
;
585 client_eui48
= aReq
->client_eui48
;
586 client_eui64
= aReq
->client_eui64
;
588 my_addr
= aReq
->my_addr
;
590 dnsWait
= aReq
->dnsWait
;
593 adaptHistory_
= aReq
->adaptHistory();
596 icapHistory_
= aReq
->icapHistory();
599 // This may be too conservative for the 204 No Content case
600 // may eventually need cloneNullAdaptationImmune() for that.
601 flags
= aReq
->flags
.cloneAdaptationImmune();
603 auth_user_request
= aReq
->auth_user_request
;
605 if (aReq
->pinned_connection
) {
606 pinned_connection
= cbdataReference(aReq
->pinned_connection
);
611 void HttpRequest::recordLookup(const DnsLookupDetails
&dns
)
613 if (dns
.wait
>= 0) { // known delay
614 if (dnsWait
>= 0) // have recorded DNS wait before
622 HttpRequest::getRangeOffsetLimit()
624 /* -2 is the starting value of rangeOffsetLimit.
625 * If it is -2, that means we haven't checked it yet.
626 * Otherwise, return the current value */
627 if (rangeOffsetLimit
!= -2)
628 return rangeOffsetLimit
;
630 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
632 ACLFilledChecklist
ch(NULL
, this, NULL
);
633 ch
.src_addr
= client_addr
;
634 ch
.my_addr
= my_addr
;
636 for (acl_size_t
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
637 /* if there is no ACL list or if the ACLs listed match use this limit value */
638 if (!l
->aclList
|| ch
.matchAclListFast(l
->aclList
)) {
639 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
640 rangeOffsetLimit
= l
->size
; // may be -1
645 return rangeOffsetLimit
;
649 HttpRequest::canHandle1xx() const
651 // old clients do not support 1xx unless they sent Expect: 100-continue
652 // (we reject all other HDR_EXPECT values so just check for HDR_EXPECT)
653 if (http_ver
<= HttpVersion(1,0) && !header
.has(HDR_EXPECT
))
656 // others must support 1xx control messages