]>
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 copy
->range
= range
? new HttpHdrRange(*range
) : NULL
;
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();
367 range
= header
.getRange();
372 request_flags::resetTCP() const
374 return reset_tcp
!= 0;
378 request_flags::setResetTCP()
380 debugs(73, 9, "request_flags::setResetTCP");
385 request_flags::clearResetTCP()
387 debugs(73, 9, "request_flags::clearResetTCP");
392 Adaptation::Icap::History::Pointer
393 HttpRequest::icapHistory() const
396 if ((LogfileStatus
== LOG_ENABLE
&& alLogformatHasIcapToken
) ||
397 IcapLogfileStatus
== LOG_ENABLE
) {
398 icapHistory_
= new Adaptation::Icap::History();
399 debugs(93,4, HERE
<< "made " << icapHistory_
<< " for " << this);
408 Adaptation::History::Pointer
409 HttpRequest::adaptHistory(bool createIfNone
) const
411 if (!adaptHistory_
&& createIfNone
) {
412 adaptHistory_
= new Adaptation::History();
413 debugs(93,4, HERE
<< "made " << adaptHistory_
<< " for " << this);
416 return adaptHistory_
;
419 Adaptation::History::Pointer
420 HttpRequest::adaptLogHistory() const
422 const bool loggingNeedsHistory
= (LogfileStatus
== LOG_ENABLE
) &&
423 alLogformatHasAdaptToken
; // TODO: make global to remove this method?
424 return HttpRequest::adaptHistory(loggingNeedsHistory
);
430 HttpRequest::multipartRangeRequest() const
432 return (range
&& range
->specs
.count
> 1);
436 request_flags::destinationIPLookupCompleted()
438 destinationIPLookedUp_
= true;
442 request_flags::destinationIPLookedUp() const
444 return destinationIPLookedUp_
;
448 request_flags::cloneAdaptationImmune() const
450 // At the time of writing, all flags where either safe to copy after
451 // adaptation or were not set at the time of the adaptation. If there
452 // are flags that are different, they should be cleared in the clone.
457 HttpRequest::bodyNibbled() const
459 return body_pipe
!= NULL
&& body_pipe
->consumedSize() > 0;
462 const char *HttpRequest::packableURI(bool full_uri
) const
465 return urlCanonical((HttpRequest
*)this);
468 return urlpath
.termedBuf();
473 void HttpRequest::packFirstLineInto(Packer
* p
, bool full_uri
) const
475 // form HTTP request-line
476 packerPrintf(p
, "%s %s HTTP/%d.%d\r\n",
477 RequestMethodStr(method
),
478 packableURI(full_uri
),
479 http_ver
.major
, http_ver
.minor
);
483 * Indicate whether or not we would usually expect an entity-body
484 * along with this request
487 HttpRequest::expectingBody(const HttpRequestMethod
& unused
, int64_t& theSize
) const
489 bool expectBody
= false;
492 * GET and HEAD don't usually have bodies, but we should be prepared
493 * to accept one if the request_entities directive is set
496 if (method
== METHOD_GET
|| method
== METHOD_HEAD
)
497 expectBody
= Config
.onoff
.request_entities
? true : false;
498 else if (method
== METHOD_PUT
|| method
== METHOD_POST
)
500 else if (header
.hasListMember(HDR_TRANSFER_ENCODING
, "chunked", ','))
502 else if (content_length
>= 0)
508 if (header
.hasListMember(HDR_TRANSFER_ENCODING
, "chunked", ','))
510 else if (content_length
>= 0)
511 theSize
= content_length
;
520 * Create a Request from a URL and METHOD.
522 * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL.
523 * If the request cannot be created cleanly, NULL is returned
526 HttpRequest::CreateFromUrlAndMethod(char * url
, const HttpRequestMethod
& method
)
528 return urlParse(method
, url
, NULL
);
532 * Create a Request from a URL.
534 * If the request cannot be created cleanly, NULL is returned
537 HttpRequest::CreateFromUrl(char * url
)
539 return urlParse(METHOD_GET
, url
, NULL
);
543 * Are responses to this request possible cacheable ?
544 * If false then no matter what the response must not be cached.
547 HttpRequest::cacheable() const
549 if (protocol
== PROTO_HTTP
)
550 return httpCachable(method
);
553 * The below looks questionable: what non HTTP protocols use connect,
554 * trace, put and post? RC
557 if (!method
.isCacheble())
561 * XXX POST may be cached sometimes.. ignored
564 if (protocol
== PROTO_GOPHER
)
565 return gopherCachable(this);
567 if (protocol
== PROTO_CACHEOBJ
)
573 bool HttpRequest::inheritProperties(const HttpMsg
*aMsg
)
575 const HttpRequest
* aReq
= dynamic_cast<const HttpRequest
*>(aMsg
);
579 client_addr
= aReq
->client_addr
;
581 client_eui48
= aReq
->client_eui48
;
582 client_eui64
= aReq
->client_eui64
;
584 my_addr
= aReq
->my_addr
;
586 dnsWait
= aReq
->dnsWait
;
589 adaptHistory_
= aReq
->adaptHistory();
592 icapHistory_
= aReq
->icapHistory();
595 // This may be too conservative for the 204 No Content case
596 // may eventually need cloneNullAdaptationImmune() for that.
597 flags
= aReq
->flags
.cloneAdaptationImmune();
599 auth_user_request
= aReq
->auth_user_request
;
601 if (aReq
->pinned_connection
) {
602 pinned_connection
= cbdataReference(aReq
->pinned_connection
);
607 void HttpRequest::recordLookup(const DnsLookupDetails
&dns
)
609 if (dns
.wait
>= 0) { // known delay
610 if (dnsWait
>= 0) // have recorded DNS wait before
618 HttpRequest::getRangeOffsetLimit()
620 /* -2 is the starting value of rangeOffsetLimit.
621 * If it is -2, that means we haven't checked it yet.
622 * Otherwise, return the current value */
623 if (rangeOffsetLimit
!= -2)
624 return rangeOffsetLimit
;
626 rangeOffsetLimit
= 0; // default value for rangeOffsetLimit
628 ACLFilledChecklist
ch(NULL
, this, NULL
);
629 ch
.src_addr
= client_addr
;
630 ch
.my_addr
= my_addr
;
632 for (acl_size_t
*l
= Config
.rangeOffsetLimit
; l
; l
= l
-> next
) {
633 /* if there is no ACL list or if the ACLs listed match use this limit value */
634 if (!l
->aclList
|| ch
.matchAclListFast(l
->aclList
)) {
635 debugs(58, 4, HERE
<< "rangeOffsetLimit=" << rangeOffsetLimit
);
636 rangeOffsetLimit
= l
->size
; // may be -1
641 return rangeOffsetLimit
;