]>
Commit | Line | Data |
---|---|---|
99edd1c3 | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
99edd1c3 | 3 | * |
bbc27441 AJ |
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. | |
99edd1c3 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 73 HTTP Request */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
f4698e0b | 12 | #include "AccessLogEntry.h" |
1328cfb7 | 13 | #include "acl/AclSizeLimit.h" |
582c2af2 | 14 | #include "acl/FilledChecklist.h" |
f5e17947 | 15 | #include "CachePeer.h" |
582c2af2 | 16 | #include "client_side.h" |
cd4a5c60 | 17 | #include "client_side_request.h" |
4a3b98d7 | 18 | #include "dns/LookupDetails.h" |
cda7024f | 19 | #include "Downloader.h" |
83b053a0 | 20 | #include "error/Detail.h" |
67679543 | 21 | #include "globals.h" |
d05c079c | 22 | #include "gopher.h" |
5c0c642e | 23 | #include "http.h" |
4f1c93a7 | 24 | #include "http/ContentLengthInterpreter.h" |
c99510dd | 25 | #include "http/one/RequestParser.h" |
d3dddfb5 | 26 | #include "http/Stream.h" |
7ebe76de | 27 | #include "HttpHdrCc.h" |
528b2c61 | 28 | #include "HttpHeaderRange.h" |
582c2af2 | 29 | #include "HttpRequest.h" |
38e16f92 | 30 | #include "log/Config.h" |
0eb49b6d | 31 | #include "MemBuf.h" |
ac38abee | 32 | #include "sbuf/StringConvert.h" |
4d5904f7 | 33 | #include "SquidConfig.h" |
25b6a907 | 34 | #include "Store.h" |
582c2af2 FC |
35 | |
36 | #if USE_AUTH | |
37 | #include "auth/UserRequest.h" | |
38 | #endif | |
3ff65596 AR |
39 | #if ICAP_CLIENT |
40 | #include "adaptation/icap/icap_log.h" | |
41 | #endif | |
528b2c61 | 42 | |
5ceaee75 CT |
43 | HttpRequest::HttpRequest(const MasterXaction::Pointer &mx) : |
44 | Http::Message(hoRequest), | |
45 | masterXaction(mx) | |
75faaa7a | 46 | { |
5ceaee75 | 47 | assert(mx); |
5cafad19 | 48 | init(); |
49 | } | |
50 | ||
5ceaee75 CT |
51 | HttpRequest::HttpRequest(const HttpRequestMethod& aMethod, AnyP::ProtocolType aProtocol, const char *aSchemeImg, const char *aUrlpath, const MasterXaction::Pointer &mx) : |
52 | Http::Message(hoRequest), | |
53 | masterXaction(mx) | |
5cafad19 | 54 | { |
5ceaee75 | 55 | assert(mx); |
5dfc3258 | 56 | static unsigned int id = 1; |
bf95c10a | 57 | debugs(93,7, "constructed, this=" << this << " id=" << ++id); |
5cafad19 | 58 | init(); |
d31d59d8 | 59 | initHTTP(aMethod, aProtocol, aSchemeImg, aUrlpath); |
5cafad19 | 60 | } |
61 | ||
62 | HttpRequest::~HttpRequest() | |
63 | { | |
5cafad19 | 64 | clean(); |
bf95c10a | 65 | debugs(93,7, "destructed, this=" << this); |
5cafad19 | 66 | } |
67 | ||
68 | void | |
d31d59d8 | 69 | HttpRequest::initHTTP(const HttpRequestMethod& aMethod, AnyP::ProtocolType aProtocol, const char *aSchemeImg, const char *aUrlpath) |
5cafad19 | 70 | { |
71 | method = aMethod; | |
d31d59d8 | 72 | url.setScheme(aProtocol, aSchemeImg); |
51b5dcf5 | 73 | url.path(aUrlpath); |
5cafad19 | 74 | } |
75 | ||
76 | void | |
77 | HttpRequest::init() | |
78 | { | |
c2a7cefd | 79 | method = Http::METHOD_NONE; |
4e3f4dc7 | 80 | url.clear(); |
2f1431ea | 81 | #if USE_AUTH |
aee3523a | 82 | auth_user_request = nullptr; |
2f1431ea | 83 | #endif |
b56b37cf | 84 | flags = RequestFlags(); |
aee3523a | 85 | range = nullptr; |
a3f28359 | 86 | ims = -1; |
87 | imslen = 0; | |
5cafad19 | 88 | lastmod = -1; |
4dd643d5 AJ |
89 | client_addr.setEmpty(); |
90 | my_addr.setEmpty(); | |
aee3523a | 91 | body_pipe = nullptr; |
a3f28359 | 92 | // hier |
3ff65596 | 93 | dnsWait = -1; |
83b053a0 | 94 | error.clear(); |
aee3523a AR |
95 | peer_login = nullptr; // not allocated/deallocated by this class |
96 | peer_domain = nullptr; // not allocated/deallocated by this class | |
97 | peer_host = nullptr; | |
90ab8f20 | 98 | vary_headers = SBuf(); |
35fb56c9 | 99 | myportname = null_string; |
5cafad19 | 100 | tag = null_string; |
2f1431ea | 101 | #if USE_AUTH |
5cafad19 | 102 | extacl_user = null_string; |
103 | extacl_passwd = null_string; | |
2f1431ea | 104 | #endif |
5cafad19 | 105 | extacl_log = null_string; |
8c93a598 | 106 | extacl_message = null_string; |
fb654382 | 107 | pstate = Http::Message::psReadyToParseStartLine; |
3d674977 | 108 | #if FOLLOW_X_FORWARDED_FOR |
4dd643d5 | 109 | indirect_client_addr.setEmpty(); |
3d674977 | 110 | #endif /* FOLLOW_X_FORWARDED_FOR */ |
3ff65596 | 111 | #if USE_ADAPTATION |
aee3523a | 112 | adaptHistory_ = nullptr; |
3ff65596 AR |
113 | #endif |
114 | #if ICAP_CLIENT | |
aee3523a | 115 | icapHistory_ = nullptr; |
3ff65596 | 116 | #endif |
11e3fa1c | 117 | rangeOffsetLimit = -2; //a value of -2 means not checked yet |
ec69bdb2 | 118 | forcedBodyContinuation = false; |
8596962e | 119 | } |
120 | ||
99edd1c3 | 121 | void |
5cafad19 | 122 | HttpRequest::clean() |
99edd1c3 | 123 | { |
9e008dda | 124 | // we used to assert that the pipe is NULL, but now the request only |
5f8252d2 | 125 | // points to a pipe that is owned and initiated by another object. |
aee3523a | 126 | body_pipe = nullptr; |
2f1431ea | 127 | #if USE_AUTH |
aee3523a | 128 | auth_user_request = nullptr; |
2f1431ea | 129 | #endif |
90ab8f20 | 130 | vary_headers.clear(); |
4e3f4dc7 | 131 | url.clear(); |
62e76326 | 132 | |
519e0948 | 133 | header.clean(); |
62e76326 | 134 | |
07947ad8 | 135 | if (cache_control) { |
3d7782c1 | 136 | delete cache_control; |
aee3523a | 137 | cache_control = nullptr; |
07947ad8 | 138 | } |
62e76326 | 139 | |
5cafad19 | 140 | if (range) { |
8596962e | 141 | delete range; |
aee3523a | 142 | range = nullptr; |
5cafad19 | 143 | } |
62e76326 | 144 | |
35fb56c9 AJ |
145 | myportname.clean(); |
146 | ||
75d47340 | 147 | theNotes = nullptr; |
d06e17ea | 148 | |
30abd221 | 149 | tag.clean(); |
2f1431ea | 150 | #if USE_AUTH |
30abd221 | 151 | extacl_user.clean(); |
30abd221 | 152 | extacl_passwd.clean(); |
2f1431ea | 153 | #endif |
30abd221 | 154 | extacl_log.clean(); |
3ff65596 | 155 | |
8c93a598 HN |
156 | extacl_message.clean(); |
157 | ||
46017fdd CT |
158 | etag.clean(); |
159 | ||
3ff65596 | 160 | #if USE_ADAPTATION |
aee3523a | 161 | adaptHistory_ = nullptr; |
3ff65596 AR |
162 | #endif |
163 | #if ICAP_CLIENT | |
aee3523a | 164 | icapHistory_ = nullptr; |
3ff65596 | 165 | #endif |
8596962e | 166 | } |
4a972fa2 | 167 | |
5cafad19 | 168 | void |
169 | HttpRequest::reset() | |
170 | { | |
171 | clean(); | |
172 | init(); | |
173 | } | |
174 | ||
fa0e6114 AR |
175 | HttpRequest * |
176 | HttpRequest::clone() const | |
177 | { | |
5ceaee75 | 178 | HttpRequest *copy = new HttpRequest(masterXaction); |
51b5dcf5 | 179 | copy->method = method; |
fa0e6114 AR |
180 | // TODO: move common cloning clone to Msg::copyTo() or copy ctor |
181 | copy->header.append(&header); | |
182 | copy->hdrCacheInit(); | |
183 | copy->hdr_sz = hdr_sz; | |
184 | copy->http_ver = http_ver; | |
185 | copy->pstate = pstate; // TODO: should we assert a specific state here? | |
186 | copy->body_pipe = body_pipe; | |
187 | ||
d31d59d8 | 188 | copy->url = url; |
fa0e6114 | 189 | |
66363092 | 190 | // range handled in hdrCacheInit() |
9e008dda AJ |
191 | copy->ims = ims; |
192 | copy->imslen = imslen; | |
fa0e6114 AR |
193 | copy->hier = hier; // Is it safe to copy? Should we? |
194 | ||
83b053a0 | 195 | copy->error = error; |
fa0e6114 AR |
196 | |
197 | // XXX: what to do with copy->peer_login? | |
198 | ||
9e008dda | 199 | copy->lastmod = lastmod; |
46017fdd | 200 | copy->etag = etag; |
90ab8f20 | 201 | copy->vary_headers = vary_headers; |
fa0e6114 AR |
202 | // XXX: what to do with copy->peer_domain? |
203 | ||
204 | copy->tag = tag; | |
fa0e6114 | 205 | copy->extacl_log = extacl_log; |
8c93a598 | 206 | copy->extacl_message = extacl_message; |
fa0e6114 | 207 | |
fbc1c5a6 AJ |
208 | const bool inheritWorked = copy->inheritProperties(this); |
209 | assert(inheritWorked); | |
3ff65596 | 210 | |
fa0e6114 AR |
211 | return copy; |
212 | } | |
213 | ||
d5964067 | 214 | bool |
63df1d28 | 215 | HttpRequest::inheritProperties(const Http::Message *aMsg) |
d5964067 AJ |
216 | { |
217 | const HttpRequest* aReq = dynamic_cast<const HttpRequest*>(aMsg); | |
218 | if (!aReq) | |
219 | return false; | |
220 | ||
221 | client_addr = aReq->client_addr; | |
222 | #if FOLLOW_X_FORWARDED_FOR | |
223 | indirect_client_addr = aReq->indirect_client_addr; | |
d5964067 AJ |
224 | #endif |
225 | my_addr = aReq->my_addr; | |
226 | ||
227 | dnsWait = aReq->dnsWait; | |
228 | ||
229 | #if USE_ADAPTATION | |
230 | adaptHistory_ = aReq->adaptHistory(); | |
231 | #endif | |
232 | #if ICAP_CLIENT | |
233 | icapHistory_ = aReq->icapHistory(); | |
234 | #endif | |
235 | ||
236 | // This may be too conservative for the 204 No Content case | |
237 | // may eventually need cloneNullAdaptationImmune() for that. | |
238 | flags = aReq->flags.cloneAdaptationImmune(); | |
239 | ||
83b053a0 | 240 | error = aReq->error; |
d5964067 AJ |
241 | #if USE_AUTH |
242 | auth_user_request = aReq->auth_user_request; | |
f4f6ab06 AJ |
243 | extacl_user = aReq->extacl_user; |
244 | extacl_passwd = aReq->extacl_passwd; | |
d5964067 | 245 | #endif |
983983ce | 246 | |
20fb3571 AJ |
247 | myportname = aReq->myportname; |
248 | ||
ec69bdb2 CT |
249 | forcedBodyContinuation = aReq->forcedBodyContinuation; |
250 | ||
983983ce | 251 | // main property is which connection the request was received on (if any) |
d5964067 | 252 | clientConnectionManager = aReq->clientConnectionManager; |
f4698e0b | 253 | |
cda7024f CT |
254 | downloader = aReq->downloader; |
255 | ||
75d47340 | 256 | theNotes = aReq->theNotes; |
88df846b CT |
257 | |
258 | sources = aReq->sources; | |
d5964067 AJ |
259 | return true; |
260 | } | |
261 | ||
96ee497f AJ |
262 | /** |
263 | * Checks the first line of an HTTP request is valid | |
264 | * currently just checks the request method is present. | |
265 | * | |
266 | * NP: Other errors are left for detection later in the parse. | |
267 | */ | |
c21ad0f5 | 268 | bool |
83b053a0 | 269 | HttpRequest::sanityCheckStartLine(const char *buf, const size_t hdr_len, Http::StatusCode *scode) |
8596962e | 270 | { |
96ee497f AJ |
271 | // content is long enough to possibly hold a reply |
272 | // 2 being magic size of a 1-byte request method plus space delimiter | |
84ae6223 | 273 | if (hdr_len < 2) { |
2f8abb64 | 274 | // this is only a real error if the headers apparently complete. |
96ee497f | 275 | if (hdr_len > 0) { |
bf95c10a | 276 | debugs(58, 3, "Too large request header (" << hdr_len << " bytes)"); |
83b053a0 | 277 | *scode = Http::scInvalidHeader; |
96ee497f AJ |
278 | } |
279 | return false; | |
280 | } | |
8596962e | 281 | |
f9688132 AJ |
282 | /* See if the request buffer starts with a non-whitespace HTTP request 'method'. */ |
283 | HttpRequestMethod m; | |
84ae6223 | 284 | m.HttpRequestMethodXXX(buf); |
f9688132 | 285 | if (m == Http::METHOD_NONE) { |
bf8fe701 | 286 | debugs(73, 3, "HttpRequest::sanityCheckStartLine: did not find HTTP request method"); |
83b053a0 | 287 | *scode = Http::scInvalidHeader; |
8596962e | 288 | return false; |
289 | } | |
290 | ||
291 | return true; | |
292 | } | |
293 | ||
c21ad0f5 | 294 | bool |
295 | HttpRequest::parseFirstLine(const char *start, const char *end) | |
8596962e | 296 | { |
f9688132 | 297 | method.HttpRequestMethodXXX(start); |
863d85ee | 298 | |
c2a7cefd | 299 | if (method == Http::METHOD_NONE) |
863d85ee | 300 | return false; |
301 | ||
f9688132 AJ |
302 | // XXX: performance regression, strcspn() over the method bytes a second time. |
303 | // cheaper than allocate+copy+deallocate cycle to SBuf convert a piece of start. | |
304 | const char *t = start + strcspn(start, w_space); | |
305 | ||
306 | start = t + strspn(t, w_space); // skip w_space after method | |
863d85ee | 307 | |
308 | const char *ver = findTrailingHTTPVersion(start, end); | |
309 | ||
310 | if (ver) { | |
311 | end = ver - 1; | |
312 | ||
313 | while (xisspace(*end)) // find prev non-space | |
5e263176 | 314 | --end; |
863d85ee | 315 | |
95dc7ff4 | 316 | ++end; // back to space |
863d85ee | 317 | |
318 | if (2 != sscanf(ver + 5, "%d.%d", &http_ver.major, &http_ver.minor)) { | |
d816f28d | 319 | debugs(73, DBG_IMPORTANT, "ERROR: parseRequestLine: Invalid HTTP identifier."); |
863d85ee | 320 | return false; |
321 | } | |
322 | } else { | |
323 | http_ver.major = 0; | |
324 | http_ver.minor = 9; | |
325 | } | |
326 | ||
327 | if (end < start) // missing URI | |
328 | return false; | |
329 | ||
6c880a16 | 330 | return url.parse(method, SBuf(start, size_t(end-start))); |
99edd1c3 | 331 | } |
332 | ||
a00a7c85 | 333 | /* swaps out request using httpRequestPack */ |
99edd1c3 | 334 | void |
5cafad19 | 335 | HttpRequest::swapOut(StoreEntry * e) |
99edd1c3 | 336 | { |
5cafad19 | 337 | assert(e); |
15179984 AJ |
338 | e->buffer(); |
339 | pack(e); | |
691ad095 | 340 | e->flush(); |
a00a7c85 | 341 | } |
342 | ||
343 | /* packs request-line and headers, appends <crlf> terminator */ | |
344 | void | |
1f28a150 | 345 | HttpRequest::pack(Packable * p) const |
a00a7c85 | 346 | { |
5cafad19 | 347 | assert(p); |
a00a7c85 | 348 | /* pack request-line */ |
51b5dcf5 AJ |
349 | p->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " HTTP/%d.%d\r\n", |
350 | SQUIDSBUFPRINT(method.image()), SQUIDSBUFPRINT(url.path()), | |
4391cd15 | 351 | http_ver.major, http_ver.minor); |
a00a7c85 | 352 | /* headers */ |
a9925b40 | 353 | header.packInto(p); |
2246b732 | 354 | /* trailer */ |
785b508d | 355 | p->append("\r\n", 2); |
2246b732 | 356 | } |
357 | ||
5cafad19 | 358 | /* |
359 | * A wrapper for debugObj() | |
360 | */ | |
2246b732 | 361 | void |
17802cf1 | 362 | httpRequestPack(void *obj, Packable *p) |
2246b732 | 363 | { |
5cafad19 | 364 | HttpRequest *request = static_cast<HttpRequest*>(obj); |
365 | request->pack(p); | |
99edd1c3 | 366 | } |
eeb423fb | 367 | |
2246b732 | 368 | /* returns the length of request line + headers + crlf */ |
369 | int | |
51b5dcf5 | 370 | HttpRequest::prefixLen() const |
2246b732 | 371 | { |
7f06a3d8 | 372 | return method.image().length() + 1 + |
51b5dcf5 | 373 | url.path().length() + 1 + |
62e76326 | 374 | 4 + 1 + 3 + 2 + |
5cafad19 | 375 | header.len + 2; |
2246b732 | 376 | } |
99edd1c3 | 377 | |
190154cf | 378 | /* sync this routine when you update HttpRequest struct */ |
8596962e | 379 | void |
07947ad8 | 380 | HttpRequest::hdrCacheInit() |
528b2c61 | 381 | { |
63df1d28 | 382 | Http::Message::hdrCacheInit(); |
62e76326 | 383 | |
66363092 | 384 | assert(!range); |
a9925b40 | 385 | range = header.getRange(); |
528b2c61 | 386 | } |
387 | ||
3ff65596 | 388 | #if ICAP_CLIENT |
e1381638 | 389 | Adaptation::Icap::History::Pointer |
3ff65596 AR |
390 | HttpRequest::icapHistory() const |
391 | { | |
392 | if (!icapHistory_) { | |
38e16f92 | 393 | if (Log::TheConfig.hasIcapToken || IcapLogfileStatus == LOG_ENABLE) { |
3ff65596 | 394 | icapHistory_ = new Adaptation::Icap::History(); |
bf95c10a | 395 | debugs(93,4, "made " << icapHistory_ << " for " << this); |
3ff65596 AR |
396 | } |
397 | } | |
398 | ||
399 | return icapHistory_; | |
400 | } | |
401 | #endif | |
402 | ||
403 | #if USE_ADAPTATION | |
e1381638 | 404 | Adaptation::History::Pointer |
a22e6cd3 | 405 | HttpRequest::adaptHistory(bool createIfNone) const |
3ff65596 | 406 | { |
a22e6cd3 AR |
407 | if (!adaptHistory_ && createIfNone) { |
408 | adaptHistory_ = new Adaptation::History(); | |
bf95c10a | 409 | debugs(93,4, "made " << adaptHistory_ << " for " << this); |
3ff65596 AR |
410 | } |
411 | ||
412 | return adaptHistory_; | |
413 | } | |
a22e6cd3 | 414 | |
e1381638 | 415 | Adaptation::History::Pointer |
a22e6cd3 AR |
416 | HttpRequest::adaptLogHistory() const |
417 | { | |
38e16f92 | 418 | return HttpRequest::adaptHistory(Log::TheConfig.hasAdaptToken); |
a22e6cd3 AR |
419 | } |
420 | ||
aaf0559d AR |
421 | void |
422 | HttpRequest::adaptHistoryImport(const HttpRequest &them) | |
423 | { | |
424 | if (!adaptHistory_) { | |
425 | adaptHistory_ = them.adaptHistory_; // may be nil | |
426 | } else { | |
427 | // check that histories did not diverge | |
428 | Must(!them.adaptHistory_ || them.adaptHistory_ == adaptHistory_); | |
429 | } | |
430 | } | |
431 | ||
3ff65596 AR |
432 | #endif |
433 | ||
528b2c61 | 434 | bool |
190154cf | 435 | HttpRequest::multipartRangeRequest() const |
528b2c61 | 436 | { |
9ad511b6 | 437 | return (range && range->specs.size() > 1); |
528b2c61 | 438 | } |
8000a965 | 439 | |
58217e94 | 440 | bool |
9e008dda AJ |
441 | HttpRequest::bodyNibbled() const |
442 | { | |
aee3523a | 443 | return body_pipe != nullptr && body_pipe->consumedSize() > 0; |
58217e94 | 444 | } |
5f8252d2 | 445 | |
f5e17947 CT |
446 | void |
447 | HttpRequest::prepForPeering(const CachePeer &peer) | |
448 | { | |
449 | // XXX: Saving two pointers to memory controlled by an independent object. | |
450 | peer_login = peer.login; | |
451 | peer_domain = peer.domain; | |
452 | flags.auth_no_keytab = peer.options.auth_no_keytab; | |
453 | debugs(11, 4, this << " to " << peer.host << (!peer.options.originserver ? " proxy" : " origin")); | |
454 | } | |
455 | ||
456 | void | |
457 | HttpRequest::prepForDirect() | |
458 | { | |
459 | peer_login = nullptr; | |
460 | peer_domain = nullptr; | |
461 | flags.auth_no_keytab = false; | |
462 | debugs(11, 4, this); | |
463 | } | |
464 | ||
129fe2a1 CT |
465 | void |
466 | HttpRequest::clearError() | |
467 | { | |
83b053a0 CT |
468 | debugs(11, 7, "old: " << error); |
469 | error.clear(); | |
129fe2a1 CT |
470 | } |
471 | ||
51b5dcf5 AJ |
472 | void |
473 | HttpRequest::packFirstLineInto(Packable * p, bool full_uri) const | |
8596962e | 474 | { |
851feda6 | 475 | const SBuf tmp(full_uri ? effectiveRequestUri() : url.path()); |
8596962e | 476 | |
8596962e | 477 | // form HTTP request-line |
51b5dcf5 | 478 | p->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " HTTP/%d.%d\r\n", |
4391cd15 | 479 | SQUIDSBUFPRINT(method.image()), |
51b5dcf5 | 480 | SQUIDSBUFPRINT(tmp), |
4391cd15 | 481 | http_ver.major, http_ver.minor); |
8596962e | 482 | } |
8ddea643 | 483 | |
484 | /* | |
684e9c80 | 485 | * Indicate whether or not we would expect an entity-body |
8ddea643 | 486 | * along with this request |
487 | */ | |
488 | bool | |
ced8def3 | 489 | HttpRequest::expectingBody(const HttpRequestMethod &, int64_t &theSize) const |
8ddea643 | 490 | { |
491 | bool expectBody = false; | |
492 | ||
493 | /* | |
684e9c80 HN |
494 | * Note: Checks for message validity is in clientIsContentLengthValid(). |
495 | * this just checks if a entity-body is expected based on HTTP message syntax | |
8ddea643 | 496 | */ |
684e9c80 | 497 | if (header.chunked()) { |
8ddea643 | 498 | expectBody = true; |
684e9c80 HN |
499 | theSize = -1; |
500 | } else if (content_length >= 0) { | |
8ddea643 | 501 | expectBody = true; |
684e9c80 HN |
502 | theSize = content_length; |
503 | } else { | |
8ddea643 | 504 | expectBody = false; |
684e9c80 | 505 | // theSize undefined |
8ddea643 | 506 | } |
507 | ||
508 | return expectBody; | |
509 | } | |
c21ad0f5 | 510 | |
511 | /* | |
512 | * Create a Request from a URL and METHOD. | |
513 | * | |
514 | * If the METHOD is CONNECT, then a host:port pair is looked for instead of a URL. | |
515 | * If the request cannot be created cleanly, NULL is returned | |
516 | */ | |
517 | HttpRequest * | |
6c880a16 | 518 | HttpRequest::FromUrl(const SBuf &url, const MasterXaction::Pointer &mx, const HttpRequestMethod& method) |
c21ad0f5 | 519 | { |
5ceaee75 | 520 | std::unique_ptr<HttpRequest> req(new HttpRequest(mx)); |
91489e45 AJ |
521 | if (req->url.parse(method, url)) { |
522 | req->method = method; | |
9157915c | 523 | return req.release(); |
91489e45 | 524 | } |
9157915c | 525 | return nullptr; |
c21ad0f5 | 526 | } |
527 | ||
6c880a16 AJ |
528 | HttpRequest * |
529 | HttpRequest::FromUrlXXX(const char * url, const MasterXaction::Pointer &mx, const HttpRequestMethod& method) | |
530 | { | |
531 | return FromUrl(SBuf(url), mx, method); | |
532 | } | |
533 | ||
c2a7cefd | 534 | /** |
610ee341 | 535 | * Are responses to this request possible cacheable ? |
536 | * If false then no matter what the response must not be cached. | |
537 | */ | |
538 | bool | |
c2a7cefd | 539 | HttpRequest::maybeCacheable() |
610ee341 | 540 | { |
2962f8b8 AJ |
541 | // Intercepted request with Host: header which cannot be trusted. |
542 | // Because it failed verification, or someone bypassed the security tests | |
2f8abb64 | 543 | // we cannot cache the response for sharing between clients. |
2962f8b8 | 544 | // TODO: update cache to store for particular clients only (going to same Host: and destination IP) |
0d901ef4 | 545 | if (!flags.hostVerified && (flags.intercepted || flags.interceptTproxy)) |
2962f8b8 AJ |
546 | return false; |
547 | ||
4e3f4dc7 | 548 | switch (url.getScheme()) { |
c2a7cefd | 549 | case AnyP::PROTO_HTTP: |
2c32c73e | 550 | case AnyP::PROTO_HTTPS: |
c2a7cefd AJ |
551 | if (!method.respMaybeCacheable()) |
552 | return false; | |
9e008dda | 553 | |
0ac163be AJ |
554 | // RFC 9111 section 5.2.1.5: |
555 | // "The no-store request directive indicates that a cache MUST NOT | |
556 | // store any part of either this request or any response to it." | |
38041f53 GD |
557 | // |
558 | // NP: refresh_pattern ignore-no-store only applies to response messages | |
559 | // this test is handling request message CC header. | |
810d879f | 560 | if (!flags.ignoreCc && cache_control && cache_control->hasNoStore()) |
38041f53 | 561 | return false; |
c2a7cefd | 562 | break; |
610ee341 | 563 | |
c2a7cefd AJ |
564 | case AnyP::PROTO_GOPHER: |
565 | if (!gopherCachable(this)) | |
566 | return false; | |
567 | break; | |
610ee341 | 568 | |
c2a7cefd | 569 | case AnyP::PROTO_CACHE_OBJECT: |
60745f24 | 570 | return false; |
610ee341 | 571 | |
f53969cc | 572 | //case AnyP::PROTO_FTP: |
c2a7cefd AJ |
573 | default: |
574 | break; | |
575 | } | |
576 | ||
60745f24 | 577 | return true; |
610ee341 | 578 | } |
d67acb4e | 579 | |
79c8035e AR |
580 | bool |
581 | HttpRequest::conditional() const | |
582 | { | |
45e5102d | 583 | return flags.ims || |
789217a2 FC |
584 | header.has(Http::HdrType::IF_MATCH) || |
585 | header.has(Http::HdrType::IF_NONE_MATCH); | |
79c8035e AR |
586 | } |
587 | ||
d5964067 | 588 | void |
4a3b98d7 | 589 | HttpRequest::recordLookup(const Dns::LookupDetails &dns) |
3ff65596 AR |
590 | { |
591 | if (dns.wait >= 0) { // known delay | |
fd9c47d1 AR |
592 | if (dnsWait >= 0) { // have recorded DNS wait before |
593 | debugs(78, 7, this << " " << dnsWait << " += " << dns); | |
3ff65596 | 594 | dnsWait += dns.wait; |
fd9c47d1 AR |
595 | } else { |
596 | debugs(78, 7, this << " " << dns); | |
3ff65596 | 597 | dnsWait = dns.wait; |
fd9c47d1 | 598 | } |
3ff65596 AR |
599 | } |
600 | } | |
11e3fa1c AJ |
601 | |
602 | int64_t | |
603 | HttpRequest::getRangeOffsetLimit() | |
604 | { | |
605 | /* -2 is the starting value of rangeOffsetLimit. | |
606 | * If it is -2, that means we haven't checked it yet. | |
607 | * Otherwise, return the current value */ | |
5e5f247c | 608 | if (rangeOffsetLimit != -2) |
11e3fa1c AJ |
609 | return rangeOffsetLimit; |
610 | ||
611 | rangeOffsetLimit = 0; // default value for rangeOffsetLimit | |
612 | ||
aee3523a | 613 | ACLFilledChecklist ch(nullptr, this, nullptr); |
11e3fa1c AJ |
614 | ch.src_addr = client_addr; |
615 | ch.my_addr = my_addr; | |
616 | ||
1328cfb7 | 617 | for (AclSizeLimit *l = Config.rangeOffsetLimit; l; l = l -> next) { |
11e3fa1c | 618 | /* if there is no ACL list or if the ACLs listed match use this limit value */ |
06bf5384 | 619 | if (!l->aclList || ch.fastCheck(l->aclList).allowed()) { |
11e3fa1c | 620 | rangeOffsetLimit = l->size; // may be -1 |
3755c7a5 | 621 | debugs(58, 4, rangeOffsetLimit); |
11e3fa1c AJ |
622 | break; |
623 | } | |
624 | } | |
625 | ||
626 | return rangeOffsetLimit; | |
627 | } | |
655daa06 | 628 | |
f0baf149 AR |
629 | void |
630 | HttpRequest::ignoreRange(const char *reason) | |
631 | { | |
632 | if (range) { | |
633 | debugs(73, 3, static_cast<void*>(range) << " for " << reason); | |
634 | delete range; | |
aee3523a | 635 | range = nullptr; |
f0baf149 AR |
636 | } |
637 | // Some callers also reset isRanged but it may not be safe for all callers: | |
638 | // isRanged is used to determine whether a weak ETag comparison is allowed, | |
639 | // and that check should not ignore the Range header if it was present. | |
640 | // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we? | |
641 | } | |
642 | ||
655daa06 AR |
643 | bool |
644 | HttpRequest::canHandle1xx() const | |
645 | { | |
646 | // old clients do not support 1xx unless they sent Expect: 100-continue | |
789217a2 FC |
647 | // (we reject all other Http::HdrType::EXPECT values so just check for Http::HdrType::EXPECT) |
648 | if (http_ver <= Http::ProtocolVersion(1,0) && !header.has(Http::HdrType::EXPECT)) | |
655daa06 AR |
649 | return false; |
650 | ||
651 | // others must support 1xx control messages | |
652 | return true; | |
653 | } | |
582c2af2 | 654 | |
4bd88eb4 AJ |
655 | Http::StatusCode |
656 | HttpRequest::checkEntityFraming() const | |
657 | { | |
658 | // RFC 7230 section 3.3.1: | |
659 | // " | |
660 | // A server that receives a request message with a transfer coding it | |
661 | // does not understand SHOULD respond with 501 (Not Implemented). | |
662 | // " | |
663 | if (header.unsupportedTe()) | |
664 | return Http::scNotImplemented; | |
665 | ||
666 | // RFC 7230 section 3.3.3 #3 paragraph 3: | |
667 | // Transfer-Encoding overrides Content-Length | |
668 | if (header.chunked()) | |
669 | return Http::scNone; | |
670 | ||
671 | // RFC 7230 Section 3.3.3 #4: | |
672 | // conflicting Content-Length(s) mean a message framing error | |
673 | if (header.conflictingContentLength()) | |
674 | return Http::scBadRequest; | |
675 | ||
676 | // HTTP/1.0 requirements differ from HTTP/1.1 | |
677 | if (http_ver <= Http::ProtocolVersion(1,0)) { | |
678 | const auto m = method.id(); | |
679 | ||
680 | // RFC 1945 section 8.3: | |
681 | // " | |
682 | // A valid Content-Length is required on all HTTP/1.0 POST requests. | |
683 | // " | |
684 | // RFC 1945 Appendix D.1.1: | |
685 | // " | |
686 | // The fundamental difference between the POST and PUT requests is | |
687 | // reflected in the different meaning of the Request-URI. | |
688 | // " | |
689 | if (m == Http::METHOD_POST || m == Http::METHOD_PUT) | |
690 | return (content_length >= 0 ? Http::scNone : Http::scLengthRequired); | |
691 | ||
692 | // RFC 1945 section 7.2: | |
693 | // " | |
694 | // An entity body is included with a request message only when the | |
695 | // request method calls for one. | |
696 | // " | |
697 | // section 8.1-2: GET and HEAD do not define ('call for') an entity | |
698 | if (m == Http::METHOD_GET || m == Http::METHOD_HEAD) | |
699 | return (content_length < 0 ? Http::scNone : Http::scBadRequest); | |
700 | // appendix D1.1.2-4: DELETE, LINK, UNLINK do not define ('call for') an entity | |
701 | if (m == Http::METHOD_DELETE || m == Http::METHOD_LINK || m == Http::METHOD_UNLINK) | |
702 | return (content_length < 0 ? Http::scNone : Http::scBadRequest); | |
703 | ||
704 | // other methods are not defined in RFC 1945 | |
705 | // assume they support an (optional) entity | |
706 | return Http::scNone; | |
707 | } | |
708 | ||
709 | // RFC 7230 section 3.3 | |
710 | // " | |
711 | // The presence of a message body in a request is signaled by a | |
712 | // Content-Length or Transfer-Encoding header field. Request message | |
713 | // framing is independent of method semantics, even if the method does | |
714 | // not define any use for a message body. | |
715 | // " | |
716 | return Http::scNone; | |
717 | } | |
718 | ||
4f1c93a7 EB |
719 | bool |
720 | HttpRequest::parseHeader(Http1::Parser &hp) | |
721 | { | |
722 | Http::ContentLengthInterpreter clen; | |
723 | return Message::parseHeader(hp, clen); | |
724 | } | |
725 | ||
726 | bool | |
727 | HttpRequest::parseHeader(const char *buffer, const size_t size) | |
728 | { | |
729 | Http::ContentLengthInterpreter clen; | |
730 | return header.parse(buffer, size, clen); | |
731 | } | |
732 | ||
582c2af2 | 733 | ConnStateData * |
b0fb853f A |
734 | HttpRequest::pinnedConnection() |
735 | { | |
582c2af2 FC |
736 | if (clientConnectionManager.valid() && clientConnectionManager->pinning.pinned) |
737 | return clientConnectionManager.get(); | |
aee3523a | 738 | return nullptr; |
582c2af2 | 739 | } |
a8a0b1c2 | 740 | |
851feda6 | 741 | const SBuf |
a8a0b1c2 EC |
742 | HttpRequest::storeId() |
743 | { | |
744 | if (store_id.size() != 0) { | |
851feda6 | 745 | debugs(73, 3, "sent back store_id: " << store_id); |
d7ef451f | 746 | return StringToSBuf(store_id); |
a8a0b1c2 | 747 | } |
851feda6 AJ |
748 | debugs(73, 3, "sent back effectiveRequestUrl: " << effectiveRequestUri()); |
749 | return effectiveRequestUri(); | |
750 | } | |
a8a0b1c2 | 751 | |
851feda6 AJ |
752 | const SBuf & |
753 | HttpRequest::effectiveRequestUri() const | |
754 | { | |
6b2b6cfe | 755 | if (method.id() == Http::METHOD_CONNECT || url.getScheme() == AnyP::PROTO_AUTHORITY_FORM) |
851feda6 AJ |
756 | return url.authority(true); // host:port |
757 | return url.absolute(); | |
a8a0b1c2 | 758 | } |
f53969cc | 759 | |
75d47340 CT |
760 | NotePairs::Pointer |
761 | HttpRequest::notes() | |
762 | { | |
763 | if (!theNotes) | |
764 | theNotes = new NotePairs; | |
765 | return theNotes; | |
766 | } | |
767 | ||
768 | void | |
769 | UpdateRequestNotes(ConnStateData *csd, HttpRequest &request, NotePairs const &helperNotes) | |
770 | { | |
771 | // Tag client connection if the helper responded with clt_conn_tag=tag. | |
772 | const char *cltTag = "clt_conn_tag"; | |
773 | if (const char *connTag = helperNotes.findFirst(cltTag)) { | |
774 | if (csd) { | |
775 | csd->notes()->remove(cltTag); | |
776 | csd->notes()->add(cltTag, connTag); | |
777 | } | |
778 | } | |
779 | request.notes()->replaceOrAdd(&helperNotes); | |
780 | } | |
781 | ||
cd4a5c60 CT |
782 | void |
783 | HttpRequest::manager(const CbcPointer<ConnStateData> &aMgr, const AccessLogEntryPointer &al) | |
784 | { | |
785 | clientConnectionManager = aMgr; | |
786 | ||
787 | if (!clientConnectionManager.valid()) | |
788 | return; | |
789 | ||
790 | AnyP::PortCfgPointer port = clientConnectionManager->port; | |
791 | if (port) { | |
792 | myportname = port->name; | |
793 | flags.ignoreCc = port->ignore_cc; | |
794 | } | |
795 | ||
796 | if (auto clientConnection = clientConnectionManager->clientConnection) { | |
797 | client_addr = clientConnection->remote; // XXX: remove request->client_addr member. | |
798 | #if FOLLOW_X_FORWARDED_FOR | |
799 | // indirect client gets stored here because it is an HTTP header result (from X-Forwarded-For:) | |
800 | // not details about the TCP connection itself | |
801 | indirect_client_addr = clientConnection->remote; | |
802 | #endif /* FOLLOW_X_FORWARDED_FOR */ | |
803 | my_addr = clientConnection->local; | |
b6307671 | 804 | |
cd4a5c60 CT |
805 | flags.intercepted = ((clientConnection->flags & COMM_INTERCEPTION) != 0); |
806 | flags.interceptTproxy = ((clientConnection->flags & COMM_TRANSPARENT) != 0 ) ; | |
807 | const bool proxyProtocolPort = port ? port->flags.proxySurrogate : false; | |
808 | if (flags.interceptTproxy && !proxyProtocolPort) { | |
809 | if (Config.accessList.spoof_client_ip) { | |
810 | ACLFilledChecklist *checklist = new ACLFilledChecklist(Config.accessList.spoof_client_ip, this, clientConnection->rfc931); | |
811 | checklist->al = al; | |
cb365059 | 812 | checklist->syncAle(this, nullptr); |
06bf5384 | 813 | flags.spoofClientIp = checklist->fastCheck().allowed(); |
cd4a5c60 CT |
814 | delete checklist; |
815 | } else | |
816 | flags.spoofClientIp = true; | |
817 | } else | |
818 | flags.spoofClientIp = false; | |
819 | } | |
820 | } | |
b6307671 | 821 | |
bec110e4 EB |
822 | char * |
823 | HttpRequest::canonicalCleanUrl() const | |
824 | { | |
825 | return urlCanonicalCleanWithoutRequest(effectiveRequestUri(), method, url.getScheme()); | |
826 | } | |
827 | ||
ea35939b | 828 | /// a helper for handling PortCfg cases of FindListeningPortAddress() |
b6c0f0dc | 829 | template <typename Filter> |
ea35939b | 830 | static const Ip::Address * |
b6c0f0dc | 831 | FindGoodListeningPortAddressInPort(const AnyP::PortCfgPointer &port, const Filter isGood) |
ea35939b | 832 | { |
b6c0f0dc | 833 | return (port && isGood(port->s)) ? &port->s : nullptr; |
ea35939b A |
834 | } |
835 | ||
836 | /// a helper for handling Connection cases of FindListeningPortAddress() | |
b6c0f0dc | 837 | template <typename Filter> |
ea35939b | 838 | static const Ip::Address * |
b6c0f0dc | 839 | FindGoodListeningPortAddressInConn(const Comm::ConnectionPointer &conn, const Filter isGood) |
ea35939b | 840 | { |
b6c0f0dc | 841 | return (conn && isGood(conn->local)) ? &conn->local : nullptr; |
ea35939b A |
842 | } |
843 | ||
b6c0f0dc | 844 | template <typename Filter> |
ea35939b | 845 | const Ip::Address * |
b6c0f0dc | 846 | FindGoodListeningPortAddress(const HttpRequest *callerRequest, const AccessLogEntry *ale, const Filter filter) |
ea35939b A |
847 | { |
848 | // Check all sources of usable listening port information, giving | |
849 | // HttpRequest and masterXaction a preference over ALE. | |
850 | ||
851 | const HttpRequest *request = callerRequest; | |
852 | if (!request && ale) | |
853 | request = ale->request; | |
854 | if (!request) | |
855 | return nullptr; // not enough information | |
856 | ||
b6c0f0dc | 857 | auto ip = FindGoodListeningPortAddressInPort(request->masterXaction->squidPort, filter); |
ea35939b | 858 | if (!ip && ale) |
b6c0f0dc | 859 | ip = FindGoodListeningPortAddressInPort(ale->cache.port, filter); |
ea35939b A |
860 | |
861 | // XXX: also handle PROXY protocol here when we have a flag to identify such request | |
862 | if (ip || request->flags.interceptTproxy || request->flags.intercepted) | |
863 | return ip; | |
864 | ||
865 | /* handle non-intercepted cases that were not handled above */ | |
b6c0f0dc | 866 | ip = FindGoodListeningPortAddressInConn(request->masterXaction->tcpClient, filter); |
ea35939b | 867 | if (!ip && ale) |
b6c0f0dc | 868 | ip = FindGoodListeningPortAddressInConn(ale->tcpClient, filter); |
ea35939b A |
869 | return ip; // may still be nil |
870 | } | |
871 | ||
b6c0f0dc AR |
872 | const Ip::Address * |
873 | FindListeningPortAddress(const HttpRequest *callerRequest, const AccessLogEntry *ale) | |
874 | { | |
875 | return FindGoodListeningPortAddress(callerRequest, ale, [](const Ip::Address &address) { | |
876 | // FindListeningPortAddress() callers do not want INADDR_ANY addresses | |
877 | return !address.isAnyAddr(); | |
878 | }); | |
879 | } | |
880 | ||
881 | unsigned short | |
882 | FindListeningPortNumber(const HttpRequest *callerRequest, const AccessLogEntry *ale) | |
883 | { | |
884 | const auto ip = FindGoodListeningPortAddress(callerRequest, ale, [](const Ip::Address &address) { | |
885 | return address.port() > 0; | |
886 | }); | |
887 | return ip ? ip->port() : 0; | |
888 | } |