]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpRequest.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / HttpRequest.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 73 HTTP Request
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
19 *
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.
24 *
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.
29 *
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.
33 *
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 */
36
37 #include "squid-old.h"
38 #include "DnsLookupDetails.h"
39 #include "HttpRequest.h"
40 #include "HttpHdrCc.h"
41 #if USE_AUTH
42 #include "auth/UserRequest.h"
43 #endif
44 #include "HttpHeaderRange.h"
45 #include "log/Config.h"
46 #include "MemBuf.h"
47 #include "Store.h"
48 #if ICAP_CLIENT
49 #include "adaptation/icap/icap_log.h"
50 #endif
51 #include "acl/FilledChecklist.h"
52 #include "err_detail_type.h"
53
54 HttpRequest::HttpRequest() : HttpMsg(hoRequest)
55 {
56 init();
57 }
58
59 HttpRequest::HttpRequest(const HttpRequestMethod& aMethod, AnyP::ProtocolType aProtocol, const char *aUrlpath) : HttpMsg(hoRequest)
60 {
61 static unsigned int id = 1;
62 debugs(93,7, HERE << "constructed, this=" << this << " id=" << ++id);
63 init();
64 initHTTP(aMethod, aProtocol, aUrlpath);
65 }
66
67 HttpRequest::~HttpRequest()
68 {
69 clean();
70 debugs(93,7, HERE << "destructed, this=" << this);
71 }
72
73 void
74 HttpRequest::initHTTP(const HttpRequestMethod& aMethod, AnyP::ProtocolType aProtocol, const char *aUrlpath)
75 {
76 method = aMethod;
77 protocol = aProtocol;
78 urlpath = aUrlpath;
79 }
80
81 void
82 HttpRequest::init()
83 {
84 method = METHOD_NONE;
85 protocol = AnyP::PROTO_NONE;
86 urlpath = NULL;
87 login[0] = '\0';
88 host[0] = '\0';
89 host_is_numeric = -1;
90 #if USE_AUTH
91 auth_user_request = NULL;
92 #endif
93 port = 0;
94 canonical = NULL;
95 memset(&flags, '\0', sizeof(flags));
96 range = NULL;
97 ims = -1;
98 imslen = 0;
99 lastmod = -1;
100 client_addr.SetEmpty();
101 my_addr.SetEmpty();
102 body_pipe = NULL;
103 // hier
104 dnsWait = -1;
105 errType = ERR_NONE;
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
109 vary_headers = NULL;
110 myportname = null_string;
111 tag = null_string;
112 #if USE_AUTH
113 extacl_user = null_string;
114 extacl_passwd = null_string;
115 #endif
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 */
122 #if USE_ADAPTATION
123 adaptHistory_ = NULL;
124 #endif
125 #if ICAP_CLIENT
126 icapHistory_ = NULL;
127 #endif
128 rangeOffsetLimit = -2; //a value of -2 means not checked yet
129 }
130
131 void
132 HttpRequest::clean()
133 {
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.
136 body_pipe = NULL;
137 #if USE_AUTH
138 auth_user_request = NULL;
139 #endif
140 safe_free(canonical);
141
142 safe_free(vary_headers);
143
144 urlpath.clean();
145
146 header.clean();
147
148 if (cache_control) {
149 delete cache_control;
150 cache_control = NULL;
151 }
152
153 if (range) {
154 delete range;
155 range = NULL;
156 }
157
158 myportname.clean();
159
160 tag.clean();
161 #if USE_AUTH
162 extacl_user.clean();
163 extacl_passwd.clean();
164 #endif
165 extacl_log.clean();
166
167 extacl_message.clean();
168
169 #if USE_ADAPTATION
170 adaptHistory_ = NULL;
171 #endif
172 #if ICAP_CLIENT
173 icapHistory_ = NULL;
174 #endif
175 }
176
177 void
178 HttpRequest::reset()
179 {
180 clean();
181 init();
182 }
183
184 HttpRequest *
185 HttpRequest::clone() const
186 {
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;
195
196 strncpy(copy->login, login, sizeof(login)); // MAX_LOGIN_SZ
197 strncpy(copy->host, host, sizeof(host)); // SQUIDHOSTNAMELEN
198 copy->host_addr = host_addr;
199
200 copy->port = port;
201 // urlPath handled in ctor
202 copy->canonical = canonical ? xstrdup(canonical) : NULL;
203
204 // range handled in hdrCacheInit()
205 copy->ims = ims;
206 copy->imslen = imslen;
207 copy->hier = hier; // Is it safe to copy? Should we?
208
209 copy->errType = errType;
210
211 // XXX: what to do with copy->peer_login?
212
213 copy->lastmod = lastmod;
214 copy->vary_headers = vary_headers ? xstrdup(vary_headers) : NULL;
215 // XXX: what to do with copy->peer_domain?
216
217 copy->myportname = myportname;
218 copy->tag = tag;
219 #if USE_AUTH
220 copy->extacl_user = extacl_user;
221 copy->extacl_passwd = extacl_passwd;
222 #endif
223 copy->extacl_log = extacl_log;
224 copy->extacl_message = extacl_message;
225
226 assert(copy->inheritProperties(this));
227
228 return copy;
229 }
230
231 bool
232 HttpRequest::inheritProperties(const HttpMsg *aMsg)
233 {
234 const HttpRequest* aReq = dynamic_cast<const HttpRequest*>(aMsg);
235 if (!aReq)
236 return false;
237
238 client_addr = aReq->client_addr;
239 #if FOLLOW_X_FORWARDED_FOR
240 indirect_client_addr = aReq->indirect_client_addr;
241 #endif
242 my_addr = aReq->my_addr;
243
244 dnsWait = aReq->dnsWait;
245
246 #if USE_ADAPTATION
247 adaptHistory_ = aReq->adaptHistory();
248 #endif
249 #if ICAP_CLIENT
250 icapHistory_ = aReq->icapHistory();
251 #endif
252
253 // This may be too conservative for the 204 No Content case
254 // may eventually need cloneNullAdaptationImmune() for that.
255 flags = aReq->flags.cloneAdaptationImmune();
256
257 errType = aReq->errType;
258 errDetail = aReq->errDetail;
259 #if USE_AUTH
260 auth_user_request = aReq->auth_user_request;
261 #endif
262
263 // main property is which connection the request was received on (if any)
264 clientConnectionManager = aReq->clientConnectionManager;
265 return true;
266 }
267
268 /**
269 * Checks the first line of an HTTP request is valid
270 * currently just checks the request method is present.
271 *
272 * NP: Other errors are left for detection later in the parse.
273 */
274 bool
275 HttpRequest::sanityCheckStartLine(MemBuf *buf, const size_t hdr_len, http_status *error)
276 {
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.
281 if (hdr_len > 0) {
282 debugs(58, 3, HERE << "Too large request header (" << hdr_len << " bytes)");
283 *error = HTTP_INVALID_HEADER;
284 }
285 return false;
286 }
287
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;
292 return false;
293 }
294
295 return true;
296 }
297
298 bool
299 HttpRequest::parseFirstLine(const char *start, const char *end)
300 {
301 const char *t = start + strcspn(start, w_space);
302 method = HttpRequestMethod(start, t);
303
304 if (method == METHOD_NONE)
305 return false;
306
307 start = t + strspn(t, w_space);
308
309 const char *ver = findTrailingHTTPVersion(start, end);
310
311 if (ver) {
312 end = ver - 1;
313
314 while (xisspace(*end)) // find prev non-space
315 end--;
316
317 end++; // back to space
318
319 if (2 != sscanf(ver + 5, "%d.%d", &http_ver.major, &http_ver.minor)) {
320 debugs(73, 1, "parseRequestLine: Invalid HTTP identifier.");
321 return false;
322 }
323 } else {
324 http_ver.major = 0;
325 http_ver.minor = 9;
326 }
327
328 if (end < start) // missing URI
329 return false;
330
331 char save = *end;
332
333 * (char *) end = '\0'; // temp terminate URI, XXX dangerous?
334
335 HttpRequest *tmp = urlParse(method, (char *) start, this);
336
337 * (char *) end = save;
338
339 if (NULL == tmp)
340 return false;
341
342 return true;
343 }
344
345 int
346 HttpRequest::parseHeader(const char *parse_start, int len)
347 {
348 const char *blk_start, *blk_end;
349
350 if (!httpMsgIsolateHeaders(&parse_start, len, &blk_start, &blk_end))
351 return 0;
352
353 int result = header.parse(blk_start, blk_end);
354
355 if (result)
356 hdrCacheInit();
357
358 return result;
359 }
360
361 /* swaps out request using httpRequestPack */
362 void
363 HttpRequest::swapOut(StoreEntry * e)
364 {
365 Packer p;
366 assert(e);
367 packerToStoreInit(&p, e);
368 pack(&p);
369 packerClean(&p);
370 }
371
372 /* packs request-line and headers, appends <crlf> terminator */
373 void
374 HttpRequest::pack(Packer * p)
375 {
376 assert(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);
381 /* headers */
382 header.packInto(p);
383 /* trailer */
384 packerAppend(p, "\r\n", 2);
385 }
386
387 /*
388 * A wrapper for debugObj()
389 */
390 void
391 httpRequestPack(void *obj, Packer *p)
392 {
393 HttpRequest *request = static_cast<HttpRequest*>(obj);
394 request->pack(p);
395 }
396
397 /* returns the length of request line + headers + crlf */
398 int
399 HttpRequest::prefixLen()
400 {
401 return strlen(RequestMethodStr(method)) + 1 +
402 urlpath.size() + 1 +
403 4 + 1 + 3 + 2 +
404 header.len + 2;
405 }
406
407 /* sync this routine when you update HttpRequest struct */
408 void
409 HttpRequest::hdrCacheInit()
410 {
411 HttpMsg::hdrCacheInit();
412
413 assert(!range);
414 range = header.getRange();
415 }
416
417 /* request_flags */
418 bool
419 request_flags::resetTCP() const
420 {
421 return reset_tcp != 0;
422 }
423
424 void
425 request_flags::setResetTCP()
426 {
427 debugs(73, 9, "request_flags::setResetTCP");
428 reset_tcp = 1;
429 }
430
431 void
432 request_flags::clearResetTCP()
433 {
434 debugs(73, 9, "request_flags::clearResetTCP");
435 reset_tcp = 0;
436 }
437
438 #if ICAP_CLIENT
439 Adaptation::Icap::History::Pointer
440 HttpRequest::icapHistory() const
441 {
442 if (!icapHistory_) {
443 if (Log::TheConfig.hasIcapToken || IcapLogfileStatus == LOG_ENABLE) {
444 icapHistory_ = new Adaptation::Icap::History();
445 debugs(93,4, HERE << "made " << icapHistory_ << " for " << this);
446 }
447 }
448
449 return icapHistory_;
450 }
451 #endif
452
453 #if USE_ADAPTATION
454 Adaptation::History::Pointer
455 HttpRequest::adaptHistory(bool createIfNone) const
456 {
457 if (!adaptHistory_ && createIfNone) {
458 adaptHistory_ = new Adaptation::History();
459 debugs(93,4, HERE << "made " << adaptHistory_ << " for " << this);
460 }
461
462 return adaptHistory_;
463 }
464
465 Adaptation::History::Pointer
466 HttpRequest::adaptLogHistory() const
467 {
468 return HttpRequest::adaptHistory(Log::TheConfig.hasAdaptToken);
469 }
470
471 void
472 HttpRequest::adaptHistoryImport(const HttpRequest &them)
473 {
474 if (!adaptHistory_) {
475 adaptHistory_ = them.adaptHistory_; // may be nil
476 } else {
477 // check that histories did not diverge
478 Must(!them.adaptHistory_ || them.adaptHistory_ == adaptHistory_);
479 }
480 }
481
482 #endif
483
484 bool
485 HttpRequest::multipartRangeRequest() const
486 {
487 return (range && range->specs.count > 1);
488 }
489
490 void
491 request_flags::destinationIPLookupCompleted()
492 {
493 destinationIPLookedUp_ = true;
494 }
495
496 bool
497 request_flags::destinationIPLookedUp() const
498 {
499 return destinationIPLookedUp_;
500 }
501
502 request_flags
503 request_flags::cloneAdaptationImmune() const
504 {
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.
508 return *this;
509 }
510
511 bool
512 HttpRequest::bodyNibbled() const
513 {
514 return body_pipe != NULL && body_pipe->consumedSize() > 0;
515 }
516
517 void
518 HttpRequest::detailError(err_type aType, int aDetail)
519 {
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
525 if (!errType)
526 errType = aType;
527 if (!errDetail)
528 errDetail = aDetail;
529 }
530
531 const char *HttpRequest::packableURI(bool full_uri) const
532 {
533 if (full_uri)
534 return urlCanonical((HttpRequest*)this);
535
536 if (urlpath.size())
537 return urlpath.termedBuf();
538
539 return "/";
540 }
541
542 void HttpRequest::packFirstLineInto(Packer * p, bool full_uri) const
543 {
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);
549 }
550
551 /*
552 * Indicate whether or not we would expect an entity-body
553 * along with this request
554 */
555 bool
556 HttpRequest::expectingBody(const HttpRequestMethod& unused, int64_t& theSize) const
557 {
558 bool expectBody = false;
559
560 /*
561 * Note: Checks for message validity is in clientIsContentLengthValid().
562 * this just checks if a entity-body is expected based on HTTP message syntax
563 */
564 if (header.chunked()) {
565 expectBody = true;
566 theSize = -1;
567 } else if (content_length >= 0) {
568 expectBody = true;
569 theSize = content_length;
570 } else {
571 expectBody = false;
572 // theSize undefined
573 }
574
575 return expectBody;
576 }
577
578 /*
579 * Create a Request from a URL and METHOD.
580 *
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
583 */
584 HttpRequest *
585 HttpRequest::CreateFromUrlAndMethod(char * url, const HttpRequestMethod& method)
586 {
587 return urlParse(method, url, NULL);
588 }
589
590 /*
591 * Create a Request from a URL.
592 *
593 * If the request cannot be created cleanly, NULL is returned
594 */
595 HttpRequest *
596 HttpRequest::CreateFromUrl(char * url)
597 {
598 return urlParse(METHOD_GET, url, NULL);
599 }
600
601 /*
602 * Are responses to this request possible cacheable ?
603 * If false then no matter what the response must not be cached.
604 */
605 bool
606 HttpRequest::cacheable() const
607 {
608 if (protocol == AnyP::PROTO_HTTP)
609 return httpCachable(method);
610
611 /*
612 * The below looks questionable: what non HTTP protocols use connect,
613 * trace, put and post? RC
614 */
615
616 if (!method.isCacheble())
617 return false;
618
619 /*
620 * XXX POST may be cached sometimes.. ignored
621 * for now
622 */
623 if (protocol == AnyP::PROTO_GOPHER)
624 return gopherCachable(this);
625
626 if (protocol == AnyP::PROTO_CACHE_OBJECT)
627 return false;
628
629 return true;
630 }
631
632 bool
633 HttpRequest::conditional() const
634 {
635 return flags.ims ||
636 header.has(HDR_IF_MATCH) ||
637 header.has(HDR_IF_NONE_MATCH);
638 }
639
640 void
641 HttpRequest::recordLookup(const DnsLookupDetails &dns)
642 {
643 if (dns.wait >= 0) { // known delay
644 if (dnsWait >= 0) // have recorded DNS wait before
645 dnsWait += dns.wait;
646 else
647 dnsWait = dns.wait;
648 }
649 }
650
651 int64_t
652 HttpRequest::getRangeOffsetLimit()
653 {
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;
659
660 rangeOffsetLimit = 0; // default value for rangeOffsetLimit
661
662 ACLFilledChecklist ch(NULL, this, NULL);
663 ch.src_addr = client_addr;
664 ch.my_addr = my_addr;
665
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
671 break;
672 }
673 }
674
675 return rangeOffsetLimit;
676 }
677
678 bool
679 HttpRequest::canHandle1xx() const
680 {
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))
684 return false;
685
686 // others must support 1xx control messages
687 return true;
688 }