]>
Commit | Line | Data |
---|---|---|
da2b3a17 | 1 | |
30a4f2a8 | 2 | /* |
262a0e14 | 3 | * $Id$ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 11 Hypertext Transfer Protocol (HTTP) | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
2b6662ba | 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. | |
30a4f2a8 | 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. | |
9e008dda | 24 | * |
30a4f2a8 | 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. | |
9e008dda | 29 | * |
30a4f2a8 | 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 | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
30a4f2a8 | 34 | */ |
019dd986 | 35 | |
4a83b852 | 36 | /* |
37 | * Anonymizing patch by lutz@as-node.jena.thur.de | |
de3bdb4c | 38 | * have a look into http-anon.c to get more informations. |
4a83b852 | 39 | */ |
40 | ||
44a47c6e | 41 | #include "squid.h" |
9ca29d23 AJ |
42 | |
43 | #include "acl/FilledChecklist.h" | |
44 | #include "auth/UserRequest.h" | |
45 | #if DELAY_POOLS | |
46 | #include "DelayPools.h" | |
47 | #endif | |
aa839030 | 48 | #include "errorpage.h" |
9ca29d23 | 49 | #include "fde.h" |
e6ccf245 | 50 | #include "http.h" |
528b2c61 | 51 | #include "HttpHdrContRange.h" |
b19dd748 | 52 | #include "HttpHdrSc.h" |
53 | #include "HttpHdrScTarget.h" | |
9ca29d23 AJ |
54 | #include "HttpReply.h" |
55 | #include "HttpRequest.h" | |
56 | #include "MemBuf.h" | |
57 | #include "MemObject.h" | |
58 | #include "protos.h" | |
1fa9b1a7 | 59 | #include "rfc1738.h" |
985c86bc | 60 | #include "SquidTime.h" |
9ca29d23 | 61 | #include "Store.h" |
af0bb8e5 | 62 | #include "TextException.h" |
63 | ||
9ca29d23 | 64 | |
af0bb8e5 | 65 | #define SQUID_ENTER_THROWING_CODE() try { |
66 | #define SQUID_EXIT_THROWING_CODE(status) \ | |
67 | status = true; \ | |
68 | } \ | |
0a8bbeeb AR |
69 | catch (const std::exception &e) { \ |
70 | debugs (11, 1, "Exception error:" << e.what()); \ | |
af0bb8e5 | 71 | status = false; \ |
9e008dda | 72 | } |
e6ccf245 | 73 | |
2afaba07 | 74 | CBDATA_CLASS_INIT(HttpStateData); |
090089c4 | 75 | |
6bf8443a | 76 | static const char *const crlf = "\r\n"; |
4db43fab | 77 | |
f9cece6e | 78 | static void httpMaybeRemovePublic(StoreEntry *, http_status); |
9e498bfb AJ |
79 | static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, HttpRequest * request, const HttpRequest * orig_request, |
80 | HttpHeader * hdr_out, const int we_do_ranges, const http_state_flags); | |
528b2c61 | 81 | |
dc56a9b1 | 82 | HttpStateData::HttpStateData(FwdState *theFwdState) : AsyncJob("HttpStateData"), ServerStateData(theFwdState), |
e1381638 AJ |
83 | lastChunk(0), header_bytes_read(0), reply_bytes_read(0), |
84 | body_bytes_truncated(0), httpChunkDecoder(NULL) | |
2bb867b5 | 85 | { |
86 | debugs(11,5,HERE << "HttpStateData " << this << " created"); | |
a3d50c30 | 87 | ignoreCacheControl = false; |
88 | surrogateNoStore = false; | |
a3d50c30 | 89 | fd = fwd->server_fd; |
90 | readBuf = new MemBuf; | |
4b6af349 | 91 | readBuf->init(); |
6dd9f4bd | 92 | orig_request = HTTPMSGLOCK(fwd->request); |
a3d50c30 | 93 | |
3ff65596 AR |
94 | // reset peer response time stats for %<pt |
95 | orig_request->hier.peer_http_request_sent.tv_sec = 0; | |
96 | orig_request->hier.peer_http_request_sent.tv_usec = 0; | |
97 | ||
a3d50c30 | 98 | if (fwd->servers) |
99 | _peer = fwd->servers->_peer; /* might be NULL */ | |
100 | ||
101 | if (_peer) { | |
102 | const char *url; | |
103 | ||
104 | if (_peer->options.originserver) | |
d53b3f6d | 105 | url = orig_request->urlpath.termedBuf(); |
a3d50c30 | 106 | else |
3900307b | 107 | url = entry->url(); |
a3d50c30 | 108 | |
5cafad19 | 109 | HttpRequest * proxy_req = new HttpRequest(orig_request->method, |
9e008dda | 110 | orig_request->protocol, url); |
a3d50c30 | 111 | |
cc192b50 | 112 | proxy_req->SetHost(_peer->host); |
a3d50c30 | 113 | |
114 | proxy_req->port = _peer->http_port; | |
115 | ||
116 | proxy_req->flags = orig_request->flags; | |
117 | ||
118 | proxy_req->lastmod = orig_request->lastmod; | |
119 | ||
120 | proxy_req->flags.proxying = 1; | |
121 | ||
6dd9f4bd | 122 | HTTPMSGUNLOCK(request); |
253caccb | 123 | |
6dd9f4bd | 124 | request = HTTPMSGLOCK(proxy_req); |
a3d50c30 | 125 | |
126 | /* | |
127 | * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here. | |
128 | * We might end up getting the object from somewhere else if, | |
129 | * for example, the request to this neighbor fails. | |
130 | */ | |
131 | if (_peer->options.proxy_only) | |
d88e3c49 | 132 | entry->releaseRequest(); |
a3d50c30 | 133 | |
134 | #if DELAY_POOLS | |
135 | ||
136 | entry->setNoDelay(_peer->options.no_delay); | |
137 | ||
138 | #endif | |
a3d50c30 | 139 | } |
140 | ||
141 | /* | |
142 | * register the handler to free HTTP state data when the FD closes | |
143 | */ | |
dc56a9b1 | 144 | typedef CommCbMemFunT<HttpStateData, CommCloseCbParams> Dialer; |
145 | closeHandler = asyncCall(9, 5, "httpStateData::httpStateConnClosed", | |
9e008dda | 146 | Dialer(this,&HttpStateData::httpStateConnClosed)); |
dc56a9b1 | 147 | comm_add_close_handler(fd, closeHandler); |
2bb867b5 | 148 | } |
b8d8561b | 149 | |
2afaba07 | 150 | HttpStateData::~HttpStateData() |
f5558c95 | 151 | { |
253caccb | 152 | /* |
3b299123 | 153 | * don't forget that ~ServerStateData() gets called automatically |
253caccb | 154 | */ |
155 | ||
2afaba07 | 156 | if (!readBuf->isNull()) |
157 | readBuf->clean(); | |
62e76326 | 158 | |
2afaba07 | 159 | delete readBuf; |
160 | ||
9e008dda AJ |
161 | if (httpChunkDecoder) |
162 | delete httpChunkDecoder; | |
af0bb8e5 | 163 | |
6dd9f4bd | 164 | HTTPMSGUNLOCK(orig_request); |
2afaba07 | 165 | |
5f8252d2 | 166 | debugs(11,5, HERE << "HttpStateData " << this << " destroyed; FD " << fd); |
167 | } | |
168 | ||
169 | int | |
fc68f6b1 | 170 | HttpStateData::dataDescriptor() const |
171 | { | |
5f8252d2 | 172 | return fd; |
2afaba07 | 173 | } |
dc56a9b1 | 174 | /* |
2afaba07 | 175 | static void |
176 | httpStateFree(int fd, void *data) | |
177 | { | |
178 | HttpStateData *httpState = static_cast<HttpStateData *>(data); | |
bf8fe701 | 179 | debugs(11, 5, "httpStateFree: FD " << fd << ", httpState=" << data); |
5f8252d2 | 180 | delete httpState; |
dc56a9b1 | 181 | }*/ |
182 | ||
9e008dda | 183 | void |
dc56a9b1 | 184 | HttpStateData::httpStateConnClosed(const CommCloseCbParams ¶ms) |
185 | { | |
186 | debugs(11, 5, "httpStateFree: FD " << params.fd << ", httpState=" << params.data); | |
187 | deleteThis("HttpStateData::httpStateConnClosed"); | |
f5558c95 | 188 | } |
189 | ||
b8d8561b | 190 | int |
60745f24 | 191 | httpCachable(const HttpRequestMethod& method) |
090089c4 | 192 | { |
090089c4 | 193 | /* GET and HEAD are cachable. Others are not. */ |
62e76326 | 194 | |
9e008dda | 195 | // TODO: replase to HttpRequestMethod::isCachable() ? |
6eb42cae | 196 | if (method != METHOD_GET && method != METHOD_HEAD) |
62e76326 | 197 | return 0; |
198 | ||
090089c4 | 199 | /* else cachable */ |
200 | return 1; | |
201 | } | |
202 | ||
dc56a9b1 | 203 | void |
204 | HttpStateData::httpTimeout(const CommTimeoutCbParams ¶ms) | |
090089c4 | 205 | { |
bf8fe701 | 206 | debugs(11, 4, "httpTimeout: FD " << fd << ": '" << entry->url() << "'" ); |
62e76326 | 207 | |
12158bdc | 208 | if (entry->store_status == STORE_PENDING) { |
dc56a9b1 | 209 | fwd->fail(errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT, fwd->request)); |
9b312a19 | 210 | } |
62e76326 | 211 | |
0d4d4170 | 212 | comm_close(fd); |
090089c4 | 213 | } |
214 | ||
f9cece6e | 215 | static void |
216 | httpMaybeRemovePublic(StoreEntry * e, http_status status) | |
217 | { | |
914b89a2 | 218 | int remove = 0; |
7e3ce7b9 | 219 | int forbidden = 0; |
f9cece6e | 220 | StoreEntry *pe; |
62e76326 | 221 | |
d46a87a8 | 222 | if (!EBIT_TEST(e->flags, KEY_PRIVATE)) |
62e76326 | 223 | return; |
224 | ||
f9cece6e | 225 | switch (status) { |
62e76326 | 226 | |
f9cece6e | 227 | case HTTP_OK: |
62e76326 | 228 | |
f9cece6e | 229 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
62e76326 | 230 | |
f9cece6e | 231 | case HTTP_MULTIPLE_CHOICES: |
62e76326 | 232 | |
f9cece6e | 233 | case HTTP_MOVED_PERMANENTLY: |
62e76326 | 234 | |
f9cece6e | 235 | case HTTP_MOVED_TEMPORARILY: |
62e76326 | 236 | |
f9cece6e | 237 | case HTTP_GONE: |
62e76326 | 238 | |
7e3ce7b9 | 239 | case HTTP_NOT_FOUND: |
914b89a2 | 240 | remove = 1; |
62e76326 | 241 | |
242 | break; | |
243 | ||
7e3ce7b9 | 244 | case HTTP_FORBIDDEN: |
62e76326 | 245 | |
7e3ce7b9 | 246 | case HTTP_METHOD_NOT_ALLOWED: |
62e76326 | 247 | forbidden = 1; |
248 | ||
249 | break; | |
250 | ||
f9cece6e | 251 | #if WORK_IN_PROGRESS |
62e76326 | 252 | |
c8fd0193 | 253 | case HTTP_UNAUTHORIZED: |
62e76326 | 254 | forbidden = 1; |
255 | ||
256 | break; | |
257 | ||
f9cece6e | 258 | #endif |
62e76326 | 259 | |
f9cece6e | 260 | default: |
7e3ce7b9 | 261 | #if QUESTIONABLE |
62e76326 | 262 | /* |
263 | * Any 2xx response should eject previously cached entities... | |
264 | */ | |
abb929f0 | 265 | |
62e76326 | 266 | if (status >= 200 && status < 300) |
914b89a2 | 267 | remove = 1; |
62e76326 | 268 | |
7e3ce7b9 | 269 | #endif |
62e76326 | 270 | |
271 | break; | |
f9cece6e | 272 | } |
62e76326 | 273 | |
914b89a2 | 274 | if (!remove && !forbidden) |
62e76326 | 275 | return; |
276 | ||
f9cece6e | 277 | assert(e->mem_obj); |
62e76326 | 278 | |
f66a9ef4 | 279 | if (e->mem_obj->request) |
62e76326 | 280 | pe = storeGetPublicByRequest(e->mem_obj->request); |
f66a9ef4 | 281 | else |
62e76326 | 282 | pe = storeGetPublic(e->mem_obj->url, e->mem_obj->method); |
283 | ||
f66a9ef4 | 284 | if (pe != NULL) { |
62e76326 | 285 | assert(e != pe); |
d9129474 | 286 | #if USE_HTCP |
8dceeee3 | 287 | neighborsHtcpClear(e, NULL, e->mem_obj->request, e->mem_obj->method, HTCP_CLR_INVALIDATION); |
d9129474 | 288 | #endif |
5f33b71d | 289 | pe->release(); |
0856d155 | 290 | } |
62e76326 | 291 | |
914b89a2 | 292 | /** \par |
7e3ce7b9 | 293 | * Also remove any cached HEAD response in case the object has |
294 | * changed. | |
295 | */ | |
f66a9ef4 | 296 | if (e->mem_obj->request) |
62e76326 | 297 | pe = storeGetPublicByRequestMethod(e->mem_obj->request, METHOD_HEAD); |
f66a9ef4 | 298 | else |
62e76326 | 299 | pe = storeGetPublic(e->mem_obj->url, METHOD_HEAD); |
300 | ||
f66a9ef4 | 301 | if (pe != NULL) { |
62e76326 | 302 | assert(e != pe); |
d9129474 | 303 | #if USE_HTCP |
8dceeee3 | 304 | neighborsHtcpClear(e, NULL, e->mem_obj->request, HttpRequestMethod(METHOD_HEAD), HTCP_CLR_INVALIDATION); |
d9129474 | 305 | #endif |
5f33b71d | 306 | pe->release(); |
7e3ce7b9 | 307 | } |
f9cece6e | 308 | } |
309 | ||
43ae1d95 | 310 | void |
311 | HttpStateData::processSurrogateControl(HttpReply *reply) | |
312 | { | |
43ae1d95 | 313 | if (request->flags.accelerated && reply->surrogate_control) { |
45cca89d | 314 | HttpHdrScTarget *sctusable = httpHdrScGetMergedTarget(reply->surrogate_control, Config.Accel.surrogate_id); |
43ae1d95 | 315 | |
316 | if (sctusable) { | |
317 | if (EBIT_TEST(sctusable->mask, SC_NO_STORE) || | |
318 | (Config.onoff.surrogate_is_remote | |
319 | && EBIT_TEST(sctusable->mask, SC_NO_STORE_REMOTE))) { | |
320 | surrogateNoStore = true; | |
5ed72359 | 321 | entry->makePrivate(); |
43ae1d95 | 322 | } |
323 | ||
324 | /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an | |
325 | * accelerated request or not... | |
45cca89d | 326 | * Still, this is an abstraction breach. - RC |
43ae1d95 | 327 | */ |
328 | if (sctusable->max_age != -1) { | |
329 | if (sctusable->max_age < sctusable->max_stale) | |
330 | reply->expires = reply->date + sctusable->max_age; | |
331 | else | |
332 | reply->expires = reply->date + sctusable->max_stale; | |
333 | ||
334 | /* And update the timestamps */ | |
3900307b | 335 | entry->timestampsSet(); |
43ae1d95 | 336 | } |
337 | ||
338 | /* We ignore cache-control directives as per the Surrogate specification */ | |
339 | ignoreCacheControl = true; | |
340 | ||
341 | httpHdrScTargetDestroy(sctusable); | |
342 | } | |
343 | } | |
43ae1d95 | 344 | } |
345 | ||
924f73bc | 346 | int |
347 | HttpStateData::cacheableReply() | |
c54e9052 | 348 | { |
585ab260 | 349 | HttpReply const *rep = finalReply(); |
528b2c61 | 350 | HttpHeader const *hdr = &rep->header; |
d8b249ef | 351 | const int cc_mask = (rep->cache_control) ? rep->cache_control->mask : 0; |
c68e9c6b | 352 | const char *v; |
38f9c547 | 353 | #if HTTP_VIOLATIONS |
62e76326 | 354 | |
38f9c547 | 355 | const refresh_t *R = NULL; |
b6445726 | 356 | |
346be6ad | 357 | /* This strange looking define first looks up the refresh pattern |
b6445726 | 358 | * and then checks if the specified flag is set. The main purpose |
359 | * of this is to simplify the refresh pattern lookup and HTTP_VIOLATIONS | |
360 | * condition | |
361 | */ | |
362 | #define REFRESH_OVERRIDE(flag) \ | |
5f8252d2 | 363 | ((R = (R ? R : refreshLimits(entry->mem_obj->url))) , \ |
364 | (R && R->flags.flag)) | |
b445957e | 365 | #else |
366 | #define REFRESH_OVERRIDE(flag) 0 | |
38f9c547 | 367 | #endif |
43ae1d95 | 368 | |
38f9c547 | 369 | if (surrogateNoStore) |
62e76326 | 370 | return 0; |
371 | ||
924f73bc | 372 | if (!ignoreCacheControl) { |
38f9c547 | 373 | if (EBIT_TEST(cc_mask, CC_PRIVATE)) { |
b6445726 | 374 | if (!REFRESH_OVERRIDE(ignore_private)) |
38f9c547 | 375 | return 0; |
376 | } | |
377 | ||
378 | if (EBIT_TEST(cc_mask, CC_NO_CACHE)) { | |
b6445726 | 379 | if (!REFRESH_OVERRIDE(ignore_no_cache)) |
38f9c547 | 380 | return 0; |
381 | } | |
382 | ||
383 | if (EBIT_TEST(cc_mask, CC_NO_STORE)) { | |
b6445726 | 384 | if (!REFRESH_OVERRIDE(ignore_no_store)) |
38f9c547 | 385 | return 0; |
386 | } | |
43ae1d95 | 387 | } |
388 | ||
d67acb4e | 389 | if (request->flags.auth || request->flags.auth_sent) { |
62e76326 | 390 | /* |
391 | * Responses to requests with authorization may be cached | |
392 | * only if a Cache-Control: public reply header is present. | |
393 | * RFC 2068, sec 14.9.4 | |
394 | */ | |
395 | ||
38f9c547 | 396 | if (!EBIT_TEST(cc_mask, CC_PUBLIC)) { |
b6445726 | 397 | if (!REFRESH_OVERRIDE(ignore_auth)) |
38f9c547 | 398 | return 0; |
399 | } | |
a6dfe2d9 | 400 | } |
62e76326 | 401 | |
c68e9c6b | 402 | /* Pragma: no-cache in _replies_ is not documented in HTTP, |
403 | * but servers like "Active Imaging Webcast/2.0" sure do use it */ | |
a9925b40 | 404 | if (hdr->has(HDR_PRAGMA)) { |
30abd221 | 405 | String s = hdr->getList(HDR_PRAGMA); |
62e76326 | 406 | const int no_cache = strListIsMember(&s, "no-cache", ','); |
30abd221 | 407 | s.clean(); |
62e76326 | 408 | |
38f9c547 | 409 | if (no_cache) { |
b6445726 | 410 | if (!REFRESH_OVERRIDE(ignore_no_cache)) |
38f9c547 | 411 | return 0; |
412 | } | |
c68e9c6b | 413 | } |
62e76326 | 414 | |
c68e9c6b | 415 | /* |
416 | * The "multipart/x-mixed-replace" content type is used for | |
417 | * continuous push replies. These are generally dynamic and | |
418 | * probably should not be cachable | |
419 | */ | |
a9925b40 | 420 | if ((v = hdr->getStr(HDR_CONTENT_TYPE))) |
62e76326 | 421 | if (!strncasecmp(v, "multipart/x-mixed-replace", 25)) |
422 | return 0; | |
423 | ||
585ab260 | 424 | switch (rep->sline.status) { |
62e76326 | 425 | /* Responses that are cacheable */ |
426 | ||
19a04dac | 427 | case HTTP_OK: |
62e76326 | 428 | |
19a04dac | 429 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
62e76326 | 430 | |
19a04dac | 431 | case HTTP_MULTIPLE_CHOICES: |
62e76326 | 432 | |
19a04dac | 433 | case HTTP_MOVED_PERMANENTLY: |
62e76326 | 434 | |
19a04dac | 435 | case HTTP_GONE: |
62e76326 | 436 | /* |
437 | * Don't cache objects that need to be refreshed on next request, | |
438 | * unless we know how to refresh it. | |
439 | */ | |
440 | ||
1b5358c3 | 441 | if (!refreshIsCachable(entry)) { |
bf8fe701 | 442 | debugs(22, 3, "refreshIsCachable() returned non-cacheable.."); |
62e76326 | 443 | return 0; |
fc68f6b1 | 444 | } |
62e76326 | 445 | |
446 | /* don't cache objects from peers w/o LMT, Date, or Expires */ | |
447 | /* check that is it enough to check headers @?@ */ | |
448 | if (rep->date > -1) | |
449 | return 1; | |
450 | else if (rep->last_modified > -1) | |
451 | return 1; | |
924f73bc | 452 | else if (!_peer) |
62e76326 | 453 | return 1; |
454 | ||
455 | /* @?@ (here and 302): invalid expires header compiles to squid_curtime */ | |
456 | else if (rep->expires > -1) | |
457 | return 1; | |
458 | else | |
459 | return 0; | |
460 | ||
461 | /* NOTREACHED */ | |
462 | break; | |
463 | ||
464 | /* Responses that only are cacheable if the server says so */ | |
465 | ||
19a04dac | 466 | case HTTP_MOVED_TEMPORARILY: |
f18c86fd | 467 | case HTTP_TEMPORARY_REDIRECT: |
6a2bf8f4 | 468 | if (rep->expires > rep->date && rep->date > 0) |
62e76326 | 469 | return 1; |
470 | else | |
471 | return 0; | |
472 | ||
473 | /* NOTREACHED */ | |
474 | break; | |
475 | ||
476 | /* Errors can be negatively cached */ | |
477 | ||
19a04dac | 478 | case HTTP_NO_CONTENT: |
62e76326 | 479 | |
19a04dac | 480 | case HTTP_USE_PROXY: |
62e76326 | 481 | |
19a04dac | 482 | case HTTP_BAD_REQUEST: |
62e76326 | 483 | |
19a04dac | 484 | case HTTP_FORBIDDEN: |
62e76326 | 485 | |
19a04dac | 486 | case HTTP_NOT_FOUND: |
62e76326 | 487 | |
19a04dac | 488 | case HTTP_METHOD_NOT_ALLOWED: |
62e76326 | 489 | |
19a04dac | 490 | case HTTP_REQUEST_URI_TOO_LARGE: |
62e76326 | 491 | |
19a04dac | 492 | case HTTP_INTERNAL_SERVER_ERROR: |
62e76326 | 493 | |
19a04dac | 494 | case HTTP_NOT_IMPLEMENTED: |
62e76326 | 495 | |
19a04dac | 496 | case HTTP_BAD_GATEWAY: |
62e76326 | 497 | |
19a04dac | 498 | case HTTP_SERVICE_UNAVAILABLE: |
62e76326 | 499 | |
19a04dac | 500 | case HTTP_GATEWAY_TIMEOUT: |
62e76326 | 501 | return -1; |
502 | ||
503 | /* NOTREACHED */ | |
504 | break; | |
505 | ||
506 | /* Some responses can never be cached */ | |
507 | ||
0cdcddb9 | 508 | case HTTP_PARTIAL_CONTENT: /* Not yet supported */ |
62e76326 | 509 | |
19a04dac | 510 | case HTTP_SEE_OTHER: |
62e76326 | 511 | |
19a04dac | 512 | case HTTP_NOT_MODIFIED: |
62e76326 | 513 | |
19a04dac | 514 | case HTTP_UNAUTHORIZED: |
62e76326 | 515 | |
19a04dac | 516 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
62e76326 | 517 | |
0cdcddb9 | 518 | case HTTP_INVALID_HEADER: /* Squid header parsing error */ |
4eb368f9 | 519 | |
520 | case HTTP_HEADER_TOO_LARGE: | |
b004a7fc | 521 | |
522 | case HTTP_PAYMENT_REQUIRED: | |
523 | case HTTP_NOT_ACCEPTABLE: | |
524 | case HTTP_REQUEST_TIMEOUT: | |
525 | case HTTP_CONFLICT: | |
526 | case HTTP_LENGTH_REQUIRED: | |
527 | case HTTP_PRECONDITION_FAILED: | |
528 | case HTTP_REQUEST_ENTITY_TOO_LARGE: | |
529 | case HTTP_UNSUPPORTED_MEDIA_TYPE: | |
530 | case HTTP_UNPROCESSABLE_ENTITY: | |
531 | case HTTP_LOCKED: | |
532 | case HTTP_FAILED_DEPENDENCY: | |
533 | case HTTP_INSUFFICIENT_STORAGE: | |
cc7c3c84 | 534 | case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: |
535 | case HTTP_EXPECTATION_FAILED: | |
b004a7fc | 536 | |
62e76326 | 537 | return 0; |
538 | ||
41217979 AJ |
539 | default: |
540 | /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */ | |
541 | debugs (11, 3, HERE << "Unknown HTTP status code " << rep->sline.status << ". Not cacheable."); | |
62e76326 | 542 | |
543 | return 0; | |
544 | ||
545 | /* NOTREACHED */ | |
546 | break; | |
c54e9052 | 547 | } |
62e76326 | 548 | |
79d39a72 | 549 | /* NOTREACHED */ |
c54e9052 | 550 | } |
090089c4 | 551 | |
f66a9ef4 | 552 | /* |
9e008dda | 553 | * For Vary, store the relevant request headers as |
f66a9ef4 | 554 | * virtual headers in the reply |
555 | * Returns false if the variance cannot be stored | |
556 | */ | |
557 | const char * | |
190154cf | 558 | httpMakeVaryMark(HttpRequest * request, HttpReply const * reply) |
f66a9ef4 | 559 | { |
30abd221 | 560 | String vary, hdr; |
f66a9ef4 | 561 | const char *pos = NULL; |
562 | const char *item; | |
563 | const char *value; | |
564 | int ilen; | |
30abd221 | 565 | static String vstr; |
f66a9ef4 | 566 | |
30abd221 | 567 | vstr.clean(); |
a9925b40 | 568 | vary = reply->header.getList(HDR_VARY); |
62e76326 | 569 | |
f66a9ef4 | 570 | while (strListGetItem(&vary, ',', &item, &ilen, &pos)) { |
62e76326 | 571 | char *name = (char *)xmalloc(ilen + 1); |
572 | xstrncpy(name, item, ilen + 1); | |
573 | Tolower(name); | |
9776e3cc | 574 | |
575 | if (strcmp(name, "*") == 0) { | |
576 | /* Can not handle "Vary: *" withtout ETag support */ | |
577 | safe_free(name); | |
30abd221 | 578 | vstr.clean(); |
9776e3cc | 579 | break; |
580 | } | |
581 | ||
62e76326 | 582 | strListAdd(&vstr, name, ','); |
a9925b40 | 583 | hdr = request->header.getByName(name); |
62e76326 | 584 | safe_free(name); |
d53b3f6d | 585 | value = hdr.termedBuf(); |
62e76326 | 586 | |
587 | if (value) { | |
588 | value = rfc1738_escape_part(value); | |
589 | vstr.append("=\"", 2); | |
590 | vstr.append(value); | |
591 | vstr.append("\"", 1); | |
592 | } | |
593 | ||
30abd221 | 594 | hdr.clean(); |
f66a9ef4 | 595 | } |
62e76326 | 596 | |
30abd221 | 597 | vary.clean(); |
f66a9ef4 | 598 | #if X_ACCELERATOR_VARY |
62e76326 | 599 | |
aa38be4a | 600 | pos = NULL; |
a9925b40 | 601 | vary = reply->header.getList(HDR_X_ACCELERATOR_VARY); |
62e76326 | 602 | |
f66a9ef4 | 603 | while (strListGetItem(&vary, ',', &item, &ilen, &pos)) { |
62e76326 | 604 | char *name = (char *)xmalloc(ilen + 1); |
605 | xstrncpy(name, item, ilen + 1); | |
606 | Tolower(name); | |
607 | strListAdd(&vstr, name, ','); | |
a9925b40 | 608 | hdr = request->header.getByName(name); |
62e76326 | 609 | safe_free(name); |
d53b3f6d | 610 | value = hdr.termedBuf(); |
62e76326 | 611 | |
612 | if (value) { | |
613 | value = rfc1738_escape_part(value); | |
614 | vstr.append("=\"", 2); | |
615 | vstr.append(value); | |
616 | vstr.append("\"", 1); | |
617 | } | |
618 | ||
30abd221 | 619 | hdr.clean(); |
f66a9ef4 | 620 | } |
62e76326 | 621 | |
30abd221 | 622 | vary.clean(); |
f66a9ef4 | 623 | #endif |
62e76326 | 624 | |
d53b3f6d FC |
625 | debugs(11, 3, "httpMakeVaryMark: " << vstr); |
626 | return vstr.termedBuf(); | |
f66a9ef4 | 627 | } |
628 | ||
2afaba07 | 629 | void |
630 | HttpStateData::keepaliveAccounting(HttpReply *reply) | |
631 | { | |
632 | if (flags.keepalive) | |
633 | if (_peer) | |
634 | _peer->stats.n_keepalives_sent++; | |
635 | ||
636 | if (reply->keep_alive) { | |
637 | if (_peer) | |
638 | _peer->stats.n_keepalives_recv++; | |
639 | ||
af6a12ee AJ |
640 | if (Config.onoff.detect_broken_server_pconns |
641 | && reply->bodySize(request->method) == -1 && !flags.chunked) { | |
bf8fe701 | 642 | debugs(11, 1, "keepaliveAccounting: Impossible keep-alive header from '" << entry->url() << "'" ); |
643 | // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" ); | |
2afaba07 | 644 | flags.keepalive_broken = 1; |
645 | } | |
646 | } | |
647 | } | |
648 | ||
649 | void | |
650 | HttpStateData::checkDateSkew(HttpReply *reply) | |
651 | { | |
652 | if (reply->date > -1 && !_peer) { | |
653 | int skew = abs((int)(reply->date - squid_curtime)); | |
654 | ||
655 | if (skew > 86400) | |
cc192b50 | 656 | debugs(11, 3, "" << request->GetHost() << "'s clock is skewed by " << skew << " seconds!"); |
2afaba07 | 657 | } |
658 | } | |
659 | ||
073ba374 | 660 | /** |
4eb368f9 | 661 | * This creates the error page itself.. its likely |
662 | * that the forward ported reply header max size patch | |
663 | * generates non http conformant error pages - in which | |
664 | * case the errors where should be 'BAD_GATEWAY' etc | |
665 | */ | |
b8d8561b | 666 | void |
2afaba07 | 667 | HttpStateData::processReplyHeader() |
f5558c95 | 668 | { |
073ba374 | 669 | /** Creates a blank header. If this routine is made incremental, this will not do */ |
82384411 | 670 | Ctx ctx = ctx_enter(entry->mem_obj->url); |
bf8fe701 | 671 | debugs(11, 3, "processReplyHeader: key '" << entry->getMD5Text() << "'"); |
62e76326 | 672 | |
1a98175f | 673 | assert(!flags.headers_parsed); |
62e76326 | 674 | |
2afaba07 | 675 | http_status error = HTTP_STATUS_NONE; |
62e76326 | 676 | |
585ab260 | 677 | HttpReply *newrep = new HttpReply; |
4a56ee8d | 678 | const bool parsed = newrep->parse(readBuf, eof, &error); |
62e76326 | 679 | |
e77d7ef0 | 680 | if (!parsed && readBuf->contentSize() > 5 && strncmp(readBuf->content(), "HTTP/", 5) != 0 && strncmp(readBuf->content(), "ICY", 3) != 0) { |
9e008dda AJ |
681 | MemBuf *mb; |
682 | HttpReply *tmprep = new HttpReply; | |
11992b6f | 683 | tmprep->setHeaders(HTTP_OK, "Gatewaying", NULL, -1, -1, -1); |
9e008dda AJ |
684 | tmprep->header.putExt("X-Transformed-From", "HTTP/0.9"); |
685 | mb = tmprep->pack(); | |
686 | newrep->parse(mb, eof, &error); | |
687 | delete tmprep; | |
688 | } else { | |
689 | if (!parsed && error > 0) { // unrecoverable parsing error | |
690 | debugs(11, 3, "processReplyHeader: Non-HTTP-compliant header: '" << readBuf->content() << "'"); | |
691 | flags.headers_parsed = 1; | |
11992b6f | 692 | newrep->sline.version = HttpVersion(1,0); |
9e008dda AJ |
693 | newrep->sline.status = error; |
694 | HttpReply *vrep = setVirginReply(newrep); | |
695 | entry->replaceHttpReply(vrep); | |
696 | ctx_exit(ctx); | |
697 | return; | |
698 | } | |
699 | ||
700 | if (!parsed) { // need more data | |
701 | assert(!error); | |
702 | assert(!eof); | |
703 | delete newrep; | |
704 | ctx_exit(ctx); | |
705 | return; | |
706 | } | |
707 | ||
708 | debugs(11, 9, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------"); | |
709 | ||
710 | header_bytes_read = headersEnd(readBuf->content(), readBuf->contentSize()); | |
711 | readBuf->consume(header_bytes_read); | |
f5558c95 | 712 | } |
62e76326 | 713 | |
3d9e71e6 AJ |
714 | /* Skip 1xx messages for now. Advertised in Via as an internal 1.0 hop */ |
715 | if (newrep->sline.protocol == PROTO_HTTP && newrep->sline.status >= 100 && newrep->sline.status < 200) { | |
716 | ||
717 | #if WHEN_HTTP11 | |
718 | /* When HTTP/1.1 check if the client is expecting a 1xx reply and maybe pass it on */ | |
719 | if (orig_request->header.has(HDR_EXPECT)) { | |
720 | // TODO: pass to the client anyway? | |
721 | } | |
722 | #endif | |
723 | delete newrep; | |
724 | debugs(11, 2, HERE << "1xx headers consume " << header_bytes_read << " bytes header."); | |
725 | header_bytes_read = 0; | |
726 | if (reply_bytes_read > 0) | |
727 | debugs(11, 2, HERE << "1xx headers consume " << reply_bytes_read << " bytes reply."); | |
728 | reply_bytes_read = 0; | |
729 | ctx_exit(ctx); | |
730 | processReplyHeader(); | |
731 | return; | |
732 | } | |
733 | ||
af0bb8e5 | 734 | flags.chunked = 0; |
ca8ef94f | 735 | if (newrep->sline.protocol == PROTO_HTTP && newrep->header.hasListMember(HDR_TRANSFER_ENCODING, "chunked", ',')) { |
9e008dda AJ |
736 | flags.chunked = 1; |
737 | httpChunkDecoder = new ChunkedCodingParser; | |
af0bb8e5 | 738 | } |
739 | ||
9e008dda AJ |
740 | if (!peerSupportsConnectionPinning()) |
741 | orig_request->flags.connection_auth_disabled = 1; | |
d67acb4e | 742 | |
585ab260 | 743 | HttpReply *vrep = setVirginReply(newrep); |
6965ab28 | 744 | flags.headers_parsed = 1; |
745 | ||
585ab260 | 746 | keepaliveAccounting(vrep); |
47ac2ebe | 747 | |
585ab260 | 748 | checkDateSkew(vrep); |
47ac2ebe | 749 | |
585ab260 | 750 | processSurrogateControl (vrep); |
528b2c61 | 751 | |
073ba374 | 752 | /** \todo IF the reply is a 1.0 reply, AND it has a Connection: Header |
528b2c61 | 753 | * Parse the header and remove all referenced headers |
754 | */ | |
755 | ||
3ff65596 AR |
756 | orig_request->hier.peer_reply_status = newrep->sline.status; |
757 | ||
2afaba07 | 758 | ctx_exit(ctx); |
7dc79973 | 759 | |
2afaba07 | 760 | } |
761 | ||
d67acb4e AJ |
762 | /** |
763 | * returns true if the peer can support connection pinning | |
764 | */ | |
765 | bool HttpStateData::peerSupportsConnectionPinning() const | |
766 | { | |
767 | const HttpReply *rep = entry->mem_obj->getReply(); | |
768 | const HttpHeader *hdr = &rep->header; | |
769 | bool rc; | |
770 | String header; | |
771 | ||
772 | if (!_peer) | |
9e008dda AJ |
773 | return true; |
774 | ||
775 | /*If this peer does not support connection pinning (authenticated | |
d67acb4e AJ |
776 | connections) return false |
777 | */ | |
778 | if (!_peer->connection_auth) | |
9e008dda | 779 | return false; |
d67acb4e | 780 | |
9e008dda | 781 | /*The peer supports connection pinning and the http reply status |
d67acb4e AJ |
782 | is not unauthorized, so the related connection can be pinned |
783 | */ | |
784 | if (rep->sline.status != HTTP_UNAUTHORIZED) | |
9e008dda AJ |
785 | return true; |
786 | ||
787 | /*The server respond with HTTP_UNAUTHORIZED and the peer configured | |
788 | with "connection-auth=on" we know that the peer supports pinned | |
d67acb4e AJ |
789 | connections |
790 | */ | |
791 | if (_peer->connection_auth == 1) | |
9e008dda | 792 | return true; |
d67acb4e | 793 | |
9e008dda AJ |
794 | /*At this point peer has configured with "connection-auth=auto" |
795 | parameter so we need some extra checks to decide if we are going | |
d67acb4e AJ |
796 | to allow pinned connections or not |
797 | */ | |
798 | ||
9e008dda | 799 | /*if the peer configured with originserver just allow connection |
d67acb4e AJ |
800 | pinning (squid 2.6 behaviour) |
801 | */ | |
802 | if (_peer->options.originserver) | |
9e008dda | 803 | return true; |
d67acb4e AJ |
804 | |
805 | /*if the connections it is already pinned it is OK*/ | |
806 | if (request->flags.pinned) | |
9e008dda AJ |
807 | return true; |
808 | ||
809 | /*Allow pinned connections only if the Proxy-support header exists in | |
810 | reply and has in its list the "Session-Based-Authentication" | |
d67acb4e AJ |
811 | which means that the peer supports connection pinning. |
812 | */ | |
813 | if (!hdr->has(HDR_PROXY_SUPPORT)) | |
9e008dda | 814 | return false; |
d67acb4e AJ |
815 | |
816 | header = hdr->getStrOrList(HDR_PROXY_SUPPORT); | |
817 | /* XXX This ought to be done in a case-insensitive manner */ | |
d53b3f6d | 818 | rc = (strstr(header.termedBuf(), "Session-Based-Authentication") != NULL); |
d67acb4e AJ |
819 | |
820 | return rc; | |
821 | } | |
822 | ||
5f8252d2 | 823 | // Called when we parsed (and possibly adapted) the headers but |
824 | // had not starting storing (a.k.a., sending) the body yet. | |
2afaba07 | 825 | void |
826 | HttpStateData::haveParsedReplyHeaders() | |
827 | { | |
c1520b67 AJ |
828 | ServerStateData::haveParsedReplyHeaders(); |
829 | ||
2afaba07 | 830 | Ctx ctx = ctx_enter(entry->mem_obj->url); |
585ab260 | 831 | HttpReply *rep = finalReply(); |
2afaba07 | 832 | |
585ab260 | 833 | if (rep->sline.status == HTTP_PARTIAL_CONTENT && |
834 | rep->content_range) | |
835 | currentOffset = rep->content_range->spec.offset; | |
62e76326 | 836 | |
3900307b | 837 | entry->timestampsSet(); |
62e76326 | 838 | |
9bc73deb | 839 | /* Check if object is cacheable or not based on reply code */ |
585ab260 | 840 | debugs(11, 3, "haveParsedReplyHeaders: HTTP CODE: " << rep->sline.status); |
62e76326 | 841 | |
9bc73deb | 842 | if (neighbors_do_private_keys) |
585ab260 | 843 | httpMaybeRemovePublic(entry, rep->sline.status); |
e6ccf245 | 844 | |
585ab260 | 845 | if (rep->header.has(HDR_VARY) |
f66a9ef4 | 846 | #if X_ACCELERATOR_VARY |
585ab260 | 847 | || rep->header.has(HDR_X_ACCELERATOR_VARY) |
f66a9ef4 | 848 | #endif |
4b44c907 | 849 | ) { |
585ab260 | 850 | const char *vary = httpMakeVaryMark(orig_request, rep); |
4b44c907 | 851 | |
852 | if (!vary) { | |
5ed72359 | 853 | entry->makePrivate(); |
d7d3253b AJ |
854 | if (!fwd->reforwardableStatus(rep->sline.status)) |
855 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); | |
4b44c907 | 856 | goto no_cache; |
62e76326 | 857 | } |
858 | ||
4b44c907 | 859 | entry->mem_obj->vary_headers = xstrdup(vary); |
860 | } | |
861 | ||
2afaba07 | 862 | #if WIP_FWD_LOG |
863 | fwdStatus(fwd, s); | |
864 | ||
865 | #endif | |
866 | /* | |
867 | * If its not a reply that we will re-forward, then | |
868 | * allow the client to get it. | |
869 | */ | |
585ab260 | 870 | if (!fwd->reforwardableStatus(rep->sline.status)) |
2afaba07 | 871 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); |
872 | ||
4b44c907 | 873 | switch (cacheableReply()) { |
874 | ||
875 | case 1: | |
5ed72359 | 876 | entry->makePublic(); |
62e76326 | 877 | break; |
878 | ||
9bc73deb | 879 | case 0: |
5ed72359 | 880 | entry->makePrivate(); |
62e76326 | 881 | break; |
882 | ||
9bc73deb | 883 | case -1: |
4b44c907 | 884 | |
ac9cc053 | 885 | #if HTTP_VIOLATIONS |
4b44c907 | 886 | if (Config.negativeTtl > 0) |
5ed72359 | 887 | entry->cacheNegatively(); |
4b44c907 | 888 | else |
ac9cc053 | 889 | #endif |
5ed72359 | 890 | entry->makePrivate(); |
4b44c907 | 891 | |
62e76326 | 892 | break; |
893 | ||
9bc73deb | 894 | default: |
62e76326 | 895 | assert(0); |
4b44c907 | 896 | |
62e76326 | 897 | break; |
9bc73deb | 898 | } |
62e76326 | 899 | |
4b44c907 | 900 | no_cache: |
901 | ||
585ab260 | 902 | if (!ignoreCacheControl && rep->cache_control) { |
903 | if (EBIT_TEST(rep->cache_control->mask, CC_PROXY_REVALIDATE)) | |
62e76326 | 904 | EBIT_SET(entry->flags, ENTRY_REVALIDATE); |
585ab260 | 905 | else if (EBIT_TEST(rep->cache_control->mask, CC_MUST_REVALIDATE)) |
62e76326 | 906 | EBIT_SET(entry->flags, ENTRY_REVALIDATE); |
9bc73deb | 907 | } |
62e76326 | 908 | |
c3609322 | 909 | #if HEADERS_LOG |
585ab260 | 910 | headersLog(1, 0, request->method, rep); |
fc68f6b1 | 911 | |
c3609322 | 912 | #endif |
5f8252d2 | 913 | |
914 | ctx_exit(ctx); | |
f5558c95 | 915 | } |
916 | ||
528b2c61 | 917 | HttpStateData::ConnectionStatus |
918 | HttpStateData::statusIfComplete() const | |
603a02fd | 919 | { |
585ab260 | 920 | const HttpReply *rep = virginReply(); |
073ba374 AJ |
921 | /** \par |
922 | * If the reply wants to close the connection, it takes precedence */ | |
62e76326 | 923 | |
2afaba07 | 924 | if (httpHeaderHasConnDir(&rep->header, "close")) |
62e76326 | 925 | return COMPLETE_NONPERSISTENT_MSG; |
926 | ||
073ba374 AJ |
927 | /** \par |
928 | * If we didn't send a keep-alive request header, then this | |
978e455f | 929 | * can not be a persistent connection. |
930 | */ | |
528b2c61 | 931 | if (!flags.keepalive) |
62e76326 | 932 | return COMPLETE_NONPERSISTENT_MSG; |
933 | ||
073ba374 | 934 | /** \par |
72b63f06 | 935 | * If we haven't sent the whole request then this can not be a persistent |
936 | * connection. | |
937 | */ | |
938 | if (!flags.request_sent) { | |
60745f24 | 939 | debugs(11, 1, "statusIfComplete: Request not yet fully sent \"" << RequestMethodStr(orig_request->method) << " " << entry->url() << "\"" ); |
72b63f06 | 940 | return COMPLETE_NONPERSISTENT_MSG; |
941 | } | |
942 | ||
073ba374 | 943 | /** \par |
9f5a2895 | 944 | * What does the reply have to say about keep-alive? |
945 | */ | |
073ba374 AJ |
946 | /** |
947 | \bug XXX BUG? | |
b6a2f15e | 948 | * If the origin server (HTTP/1.0) does not send a keep-alive |
949 | * header, but keeps the connection open anyway, what happens? | |
950 | * We'll return here and http.c waits for an EOF before changing | |
951 | * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT | |
952 | * and an error status code, and we might have to wait until | |
953 | * the server times out the socket. | |
954 | */ | |
2afaba07 | 955 | if (!rep->keep_alive) |
528b2c61 | 956 | return COMPLETE_NONPERSISTENT_MSG; |
62e76326 | 957 | |
528b2c61 | 958 | return COMPLETE_PERSISTENT_MSG; |
959 | } | |
960 | ||
961 | HttpStateData::ConnectionStatus | |
962 | HttpStateData::persistentConnStatus() const | |
963 | { | |
ba82c452 | 964 | debugs(11, 3, "persistentConnStatus: FD " << fd << " eof=" << eof); |
585ab260 | 965 | const HttpReply *vrep = virginReply(); |
966 | debugs(11, 5, "persistentConnStatus: content_length=" << vrep->content_length); | |
62e76326 | 967 | |
5f8252d2 | 968 | /* If we haven't seen the end of reply headers, we are not done */ |
bf8fe701 | 969 | debugs(11, 5, "persistentConnStatus: flags.headers_parsed=" << flags.headers_parsed); |
fc68f6b1 | 970 | |
1a98175f | 971 | if (!flags.headers_parsed) |
62e76326 | 972 | return INCOMPLETE_MSG; |
973 | ||
839291ac AJ |
974 | if (eof) // already reached EOF |
975 | return COMPLETE_NONPERSISTENT_MSG; | |
976 | ||
9035d1d5 AJ |
977 | /** \par |
978 | * In chunked response we do not know the content length but we are absolutely | |
af0bb8e5 | 979 | * sure about the end of response, so we are calling the statusIfComplete to |
9e008dda | 980 | * decide if we can be persistant |
af0bb8e5 | 981 | */ |
839291ac | 982 | if (lastChunk && flags.chunked) |
9e008dda | 983 | return statusIfComplete(); |
af0bb8e5 | 984 | |
47f6e231 | 985 | const int64_t clen = vrep->bodySize(request->method); |
fc68f6b1 | 986 | |
bf8fe701 | 987 | debugs(11, 5, "persistentConnStatus: clen=" << clen); |
2afaba07 | 988 | |
35282fbf | 989 | /* If the body size is unknown we must wait for EOF */ |
990 | if (clen < 0) | |
62e76326 | 991 | return INCOMPLETE_MSG; |
992 | ||
9035d1d5 AJ |
993 | /** \par |
994 | * If the body size is known, we must wait until we've gotten all of it. */ | |
5f8252d2 | 995 | if (clen > 0) { |
996 | // old technique: | |
585ab260 | 997 | // if (entry->mem_obj->endOffset() < vrep->content_length + vrep->hdr_sz) |
47f6e231 | 998 | const int64_t body_bytes_read = reply_bytes_read - header_bytes_read; |
5f8252d2 | 999 | debugs(11,5, "persistentConnStatus: body_bytes_read=" << |
585ab260 | 1000 | body_bytes_read << " content_length=" << vrep->content_length); |
2afaba07 | 1001 | |
585ab260 | 1002 | if (body_bytes_read < vrep->content_length) |
5f8252d2 | 1003 | return INCOMPLETE_MSG; |
821beb5e AR |
1004 | |
1005 | if (body_bytes_truncated > 0) // already read more than needed | |
1006 | return COMPLETE_NONPERSISTENT_MSG; // disable pconns | |
5f8252d2 | 1007 | } |
62e76326 | 1008 | |
9035d1d5 AJ |
1009 | /** \par |
1010 | * If there is no message body or we got it all, we can be persistent */ | |
5f8252d2 | 1011 | return statusIfComplete(); |
603a02fd | 1012 | } |
090089c4 | 1013 | |
2afaba07 | 1014 | /* |
1015 | * This is the callback after some data has been read from the network | |
1016 | */ | |
dc56a9b1 | 1017 | /* |
e5ee81f0 | 1018 | void |
d53b3f6d | 1019 | HttpStateData::ReadReplyWrapper(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data) |
c4b7a5a9 | 1020 | { |
1021 | HttpStateData *httpState = static_cast<HttpStateData *>(data); | |
7194987f | 1022 | assert (fd == httpState->fd); |
d53b3f6d | 1023 | // assert(buf == readBuf->content()); |
1d5161bd | 1024 | PROF_start(HttpStateData_readReply); |
e6edd8df | 1025 | httpState->readReply(len, flag, xerrno); |
1d5161bd | 1026 | PROF_stop(HttpStateData_readReply); |
c4b7a5a9 | 1027 | } |
dc56a9b1 | 1028 | */ |
073ba374 | 1029 | |
2afdbf48 | 1030 | /* XXX this function is too long! */ |
c4b7a5a9 | 1031 | void |
e6edd8df | 1032 | HttpStateData::readReply(const CommIoCbParams &io) |
090089c4 | 1033 | { |
30a4f2a8 | 1034 | int bin; |
090089c4 | 1035 | int clen; |
dc56a9b1 | 1036 | int len = io.size; |
c4b7a5a9 | 1037 | |
dc56a9b1 | 1038 | assert(fd == io.fd); |
1039 | ||
1040 | flags.do_next_read = 0; | |
9e008dda | 1041 | |
7a7cc03f | 1042 | debugs(11, 5, "httpReadReply: FD " << fd << ": len " << len << "."); |
62e76326 | 1043 | |
7a7cc03f | 1044 | // Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us |
dc56a9b1 | 1045 | if (io.flag == COMM_ERR_CLOSING) { |
bf8fe701 | 1046 | debugs(11, 3, "http socket closing"); |
c4b7a5a9 | 1047 | return; |
1048 | } | |
1049 | ||
e92e4e44 | 1050 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
5f8252d2 | 1051 | maybeReadVirginBody(); |
62e76326 | 1052 | return; |
e92e4e44 | 1053 | } |
c4b7a5a9 | 1054 | |
fdf55365 | 1055 | // handle I/O errors |
dc56a9b1 | 1056 | if (io.flag != COMM_OK || len < 0) { |
7a7cc03f | 1057 | debugs(11, 2, "httpReadReply: FD " << fd << ": read failure: " << xstrerror() << "."); |
fdf55365 | 1058 | |
dc56a9b1 | 1059 | if (ignoreErrno(io.xerrno)) { |
fdf55365 | 1060 | flags.do_next_read = 1; |
1061 | } else { | |
1062 | ErrorState *err; | |
1063 | err = errorCon(ERR_READ_ERROR, HTTP_BAD_GATEWAY, fwd->request); | |
dc56a9b1 | 1064 | err->xerrno = io.xerrno; |
fdf55365 | 1065 | fwd->fail(err); |
1066 | flags.do_next_read = 0; | |
1067 | comm_close(fd); | |
1068 | } | |
1069 | ||
1070 | return; | |
1071 | } | |
1072 | ||
7a7cc03f | 1073 | // update I/O stats |
fdf55365 | 1074 | if (len > 0) { |
2afaba07 | 1075 | readBuf->appended(len); |
5f8252d2 | 1076 | reply_bytes_read += len; |
447e176b | 1077 | #if DELAY_POOLS |
2afaba07 | 1078 | |
1079 | DelayId delayId = entry->mem_obj->mostBytesAllowed(); | |
62e76326 | 1080 | delayId.bytesIn(len); |
447e176b | 1081 | #endif |
62e76326 | 1082 | |
1083 | kb_incr(&statCounter.server.all.kbytes_in, len); | |
1084 | kb_incr(&statCounter.server.http.kbytes_in, len); | |
62e76326 | 1085 | IOStats.Http.reads++; |
1086 | ||
1087 | for (clen = len - 1, bin = 0; clen; bin++) | |
1088 | clen >>= 1; | |
1089 | ||
1090 | IOStats.Http.read_hist[bin]++; | |
3ff65596 AR |
1091 | |
1092 | // update peer response time stats (%<pt) | |
1093 | const timeval &sent = orig_request->hier.peer_http_request_sent; | |
1094 | orig_request->hier.peer_response_time = | |
1095 | sent.tv_sec ? tvSubMsec(sent, current_time) : -1; | |
30a4f2a8 | 1096 | } |
62e76326 | 1097 | |
073ba374 AJ |
1098 | /** \par |
1099 | * Here the RFC says we should ignore whitespace between replies, but we can't as | |
5fa061b8 | 1100 | * doing so breaks HTTP/0.9 replies beginning with witespace, and in addition |
1101 | * the response splitting countermeasures is extremely likely to trigger on this, | |
1102 | * not allowing connection reuse in the first place. | |
1103 | */ | |
1104 | #if DONT_DO_THIS | |
fdf55365 | 1105 | if (!flags.headers_parsed && len > 0 && fd_table[fd].uses > 1) { |
5fa061b8 | 1106 | /* Skip whitespace between replies */ |
62e76326 | 1107 | |
d53b3f6d FC |
1108 | while (len > 0 && xisspace(*buf)) |
1109 | xmemmove(buf, buf + 1, len--); | |
62e76326 | 1110 | |
1111 | if (len == 0) { | |
1112 | /* Continue to read... */ | |
21b92762 | 1113 | /* Timeout NOT increased. This whitespace was from previous reply */ |
f61f0107 | 1114 | flags.do_next_read = 1; |
5f8252d2 | 1115 | maybeReadVirginBody(); |
62e76326 | 1116 | return; |
1117 | } | |
5ede6c8f | 1118 | } |
62e76326 | 1119 | |
5fa061b8 | 1120 | #endif |
1121 | ||
ba82c452 | 1122 | if (len == 0) { // reached EOF? |
62e76326 | 1123 | eof = 1; |
f61f0107 | 1124 | flags.do_next_read = 0; |
ba82c452 | 1125 | } |
62e76326 | 1126 | |
ba82c452 | 1127 | if (!flags.headers_parsed) { // have not parsed headers yet? |
1128 | PROF_start(HttpStateData_processReplyHeader); | |
1129 | processReplyHeader(); | |
1130 | PROF_stop(HttpStateData_processReplyHeader); | |
1131 | ||
1132 | if (!continueAfterParsingHeader()) // parsing error or need more data | |
1133 | return; // TODO: send errors to ICAP | |
1134 | ||
585ab260 | 1135 | adaptOrFinalizeReply(); |
ba82c452 | 1136 | } |
1137 | ||
1138 | // kick more reads if needed and/or process the response body, if any | |
1139 | PROF_start(HttpStateData_processReplyBody); | |
1140 | processReplyBody(); // may call serverComplete() | |
1141 | PROF_stop(HttpStateData_processReplyBody); | |
1142 | } | |
1143 | ||
073ba374 AJ |
1144 | /** |
1145 | \retval true if we can continue with processing the body or doing ICAP. | |
1146 | */ | |
ba82c452 | 1147 | bool |
1148 | HttpStateData::continueAfterParsingHeader() | |
1149 | { | |
073ba374 | 1150 | if (!flags.headers_parsed && !eof) { |
ba82c452 | 1151 | debugs(11, 9, HERE << "needs more at " << readBuf->contentSize()); |
1152 | flags.do_next_read = 1; | |
073ba374 AJ |
1153 | /** \retval false If we have not finished parsing the headers and may get more data. |
1154 | * Schedules more reads to retrieve the missing data. | |
1155 | */ | |
ba82c452 | 1156 | maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename |
073ba374 | 1157 | return false; |
ba82c452 | 1158 | } |
1159 | ||
073ba374 | 1160 | /** If we are done with parsing, check for errors */ |
ba82c452 | 1161 | |
1162 | err_type error = ERR_NONE; | |
1163 | ||
1164 | if (flags.headers_parsed) { // parsed headers, possibly with errors | |
1165 | // check for header parsing errors | |
585ab260 | 1166 | if (HttpReply *vrep = virginReply()) { |
1167 | const http_status s = vrep->sline.status; | |
1168 | const HttpVersion &v = vrep->sline.version; | |
ba82c452 | 1169 | if (s == HTTP_INVALID_HEADER && v != HttpVersion(0,9)) { |
9121eba6 | 1170 | debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() ); |
ba82c452 | 1171 | error = ERR_INVALID_RESP; |
e1381638 AJ |
1172 | } else if (s == HTTP_HEADER_TOO_LARGE) { |
1173 | fwd->dontRetry(true); | |
1174 | error = ERR_TOO_BIG; | |
1175 | } else { | |
1176 | return true; // done parsing, got reply, and no error | |
1177 | } | |
ba82c452 | 1178 | } else { |
1179 | // parsed headers but got no reply | |
9121eba6 | 1180 | debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No reply at all for " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() ); |
ba82c452 | 1181 | error = ERR_INVALID_RESP; |
62e76326 | 1182 | } |
090089c4 | 1183 | } else { |
ba82c452 | 1184 | assert(eof); |
9121eba6 AJ |
1185 | if (readBuf->hasContent()) { |
1186 | error = ERR_INVALID_RESP; | |
1187 | debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() ); | |
1188 | } else { | |
1189 | error = ERR_ZERO_SIZE_OBJECT; | |
1190 | debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No object data received for " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() ); | |
1191 | } | |
2afaba07 | 1192 | } |
ba82c452 | 1193 | |
1194 | assert(error != ERR_NONE); | |
1195 | entry->reset(); | |
1196 | fwd->fail(errorCon(error, HTTP_BAD_GATEWAY, fwd->request)); | |
1197 | flags.do_next_read = 0; | |
1198 | comm_close(fd); | |
1199 | return false; // quit on error | |
2afaba07 | 1200 | } |
1201 | ||
821beb5e AR |
1202 | /** truncate what we read if we read too much so that writeReplyBody() |
1203 | writes no more than what we should have read */ | |
1204 | void | |
1205 | HttpStateData::truncateVirginBody() | |
1206 | { | |
1207 | assert(flags.headers_parsed); | |
1208 | ||
1209 | HttpReply *vrep = virginReply(); | |
1210 | int64_t clen = -1; | |
1211 | if (!vrep->expectingBody(request->method, clen) || clen < 0) | |
1212 | return; // no body or a body of unknown size, including chunked | |
1213 | ||
1214 | const int64_t body_bytes_read = reply_bytes_read - header_bytes_read; | |
e1381638 | 1215 | if (body_bytes_read - body_bytes_truncated <= clen) |
821beb5e AR |
1216 | return; // we did not read too much or already took care of the extras |
1217 | ||
1218 | if (const int64_t extras = body_bytes_read - body_bytes_truncated - clen) { | |
1219 | // server sent more that the advertised content length | |
e1381638 AJ |
1220 | debugs(11,5, HERE << "body_bytes_read=" << body_bytes_read << |
1221 | " clen=" << clen << '/' << vrep->content_length << | |
1222 | " body_bytes_truncated=" << body_bytes_truncated << '+' << extras); | |
821beb5e AR |
1223 | |
1224 | readBuf->truncate(extras); | |
1225 | body_bytes_truncated += extras; | |
1226 | } | |
1227 | } | |
1228 | ||
073ba374 | 1229 | /** |
2afaba07 | 1230 | * Call this when there is data from the origin server |
1231 | * which should be sent to either StoreEntry, or to ICAP... | |
1232 | */ | |
1233 | void | |
5f8252d2 | 1234 | HttpStateData::writeReplyBody() |
2afaba07 | 1235 | { |
821beb5e | 1236 | truncateVirginBody(); // if needed |
5f8252d2 | 1237 | const char *data = readBuf->content(); |
1238 | int len = readBuf->contentSize(); | |
bc81cb2b | 1239 | addVirginReplyBody(data, len); |
5f8252d2 | 1240 | readBuf->consume(len); |
af0bb8e5 | 1241 | } |
fc68f6b1 | 1242 | |
af0bb8e5 | 1243 | bool |
1244 | HttpStateData::decodeAndWriteReplyBody() | |
1245 | { | |
1246 | const char *data = NULL; | |
1247 | int len; | |
e053c141 | 1248 | bool wasThereAnException = false; |
af0bb8e5 | 1249 | assert(flags.chunked); |
1250 | assert(httpChunkDecoder); | |
1251 | SQUID_ENTER_THROWING_CODE(); | |
1252 | MemBuf decodedData; | |
1253 | decodedData.init(); | |
e053c141 | 1254 | const bool doneParsing = httpChunkDecoder->parse(readBuf,&decodedData); |
af0bb8e5 | 1255 | len = decodedData.contentSize(); |
1256 | data=decodedData.content(); | |
1257 | addVirginReplyBody(data, len); | |
e053c141 | 1258 | if (doneParsing) { |
839291ac AJ |
1259 | lastChunk = 1; |
1260 | flags.do_next_read = 0; | |
af0bb8e5 | 1261 | } |
e053c141 FC |
1262 | SQUID_EXIT_THROWING_CODE(wasThereAnException); |
1263 | return wasThereAnException; | |
e6ccf245 | 1264 | } |
1265 | ||
073ba374 | 1266 | /** |
2afaba07 | 1267 | * processReplyBody has two purposes: |
1268 | * 1 - take the reply body data, if any, and put it into either | |
1269 | * the StoreEntry, or give it over to ICAP. | |
1270 | * 2 - see if we made it to the end of the response (persistent | |
1271 | * connections and such) | |
1272 | */ | |
e6ccf245 | 1273 | void |
2afaba07 | 1274 | HttpStateData::processReplyBody() |
e6ccf245 | 1275 | { |
dc56a9b1 | 1276 | AsyncCall::Pointer call; |
ad61a2b4 | 1277 | IpAddress client_addr; |
d67acb4e | 1278 | bool ispinned = false; |
fc68f6b1 | 1279 | |
1a98175f | 1280 | if (!flags.headers_parsed) { |
f61f0107 | 1281 | flags.do_next_read = 1; |
5f8252d2 | 1282 | maybeReadVirginBody(); |
62e76326 | 1283 | return; |
528b2c61 | 1284 | } |
62e76326 | 1285 | |
a83c6ed6 | 1286 | #if USE_ADAPTATION |
c30ac6ea | 1287 | debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending); |
a83c6ed6 | 1288 | if (adaptationAccessCheckPending) |
2afaba07 | 1289 | return; |
fc68f6b1 | 1290 | |
2afaba07 | 1291 | #endif |
62e76326 | 1292 | |
2afaba07 | 1293 | /* |
1294 | * At this point the reply headers have been parsed and consumed. | |
1295 | * That means header content has been removed from readBuf and | |
1296 | * it contains only body data. | |
1297 | */ | |
9e008dda AJ |
1298 | if (flags.chunked) { |
1299 | if (!decodeAndWriteReplyBody()) { | |
1300 | flags.do_next_read = 0; | |
1301 | serverComplete(); | |
1302 | return; | |
1303 | } | |
1304 | } else | |
1305 | writeReplyBody(); | |
528b2c61 | 1306 | |
e6ccf245 | 1307 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
62e76326 | 1308 | /* |
073ba374 | 1309 | * The above writeReplyBody() call could ABORT this entry, |
62e76326 | 1310 | * in that case, the server FD should already be closed. |
1311 | * there's nothing for us to do. | |
1312 | */ | |
1313 | (void) 0; | |
1314 | } else | |
1315 | switch (persistentConnStatus()) { | |
62e76326 | 1316 | case INCOMPLETE_MSG: |
bf8fe701 | 1317 | debugs(11, 5, "processReplyBody: INCOMPLETE_MSG"); |
21b92762 | 1318 | /* Wait for more data or EOF condition */ |
21b92762 | 1319 | if (flags.keepalive_broken) { |
9e008dda | 1320 | call = NULL; |
dc56a9b1 | 1321 | commSetTimeout(fd, 10, call); |
21b92762 | 1322 | } else { |
9e008dda | 1323 | call = NULL; |
dc56a9b1 | 1324 | commSetTimeout(fd, Config.Timeout.read, call); |
21b92762 | 1325 | } |
1326 | ||
f61f0107 | 1327 | flags.do_next_read = 1; |
62e76326 | 1328 | break; |
1329 | ||
1330 | case COMPLETE_PERSISTENT_MSG: | |
bf8fe701 | 1331 | debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG"); |
62e76326 | 1332 | /* yes we have to clear all these! */ |
9e008dda | 1333 | call = NULL; |
dc56a9b1 | 1334 | commSetTimeout(fd, -1, call); |
f61f0107 | 1335 | flags.do_next_read = 0; |
62e76326 | 1336 | |
9e008dda | 1337 | comm_remove_close_handler(fd, closeHandler); |
dc56a9b1 | 1338 | closeHandler = NULL; |
b6b6f466 | 1339 | fwd->unregister(fd); |
fc68f6b1 | 1340 | |
f165d2fb | 1341 | if (orig_request->flags.spoof_client_ip) |
cc192b50 | 1342 | client_addr = orig_request->client_addr; |
fc68f6b1 | 1343 | |
d67acb4e | 1344 | |
9e008dda AJ |
1345 | if (request->flags.pinned) { |
1346 | ispinned = true; | |
1347 | } else if (request->flags.connection_auth && request->flags.auth_sent) { | |
1348 | ispinned = true; | |
1349 | } | |
1350 | ||
1351 | if (orig_request->pinnedConnection() && ispinned) { | |
1352 | orig_request->pinnedConnection()->pinConnection(fd, orig_request, _peer, | |
1353 | (request->flags.connection_auth != 0)); | |
bd0723ad | 1354 | } else { |
06093389 | 1355 | fwd->pconnPush(fd, _peer, request, orig_request->GetHost(), client_addr); |
bd0723ad | 1356 | } |
1357 | ||
62e76326 | 1358 | fd = -1; |
2afaba07 | 1359 | |
5f8252d2 | 1360 | serverComplete(); |
62e76326 | 1361 | return; |
1362 | ||
1363 | case COMPLETE_NONPERSISTENT_MSG: | |
bf8fe701 | 1364 | debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG"); |
5f8252d2 | 1365 | serverComplete(); |
62e76326 | 1366 | return; |
1367 | } | |
1368 | ||
5f8252d2 | 1369 | maybeReadVirginBody(); |
c4b7a5a9 | 1370 | } |
1371 | ||
1372 | void | |
5f8252d2 | 1373 | HttpStateData::maybeReadVirginBody() |
c4b7a5a9 | 1374 | { |
52edecde | 1375 | // we may need to grow the buffer if headers do not fit |
1c9605c5 | 1376 | const int minRead = flags.headers_parsed ? 0 :1024; |
d5f8d05f | 1377 | const int read_size = replyBodySpace(*readBuf, minRead); |
2afaba07 | 1378 | |
5f8252d2 | 1379 | debugs(11,9, HERE << (flags.do_next_read ? "may" : "wont") << |
d5f8d05f | 1380 | " read up to " << read_size << " bytes from FD " << fd); |
2afaba07 | 1381 | |
1382 | /* | |
1383 | * why <2? Because delayAwareRead() won't actually read if | |
1384 | * you ask it to read 1 byte. The delayed read request | |
1385 | * just gets re-queued until the client side drains, then | |
1386 | * the I/O thread hangs. Better to not register any read | |
1387 | * handler until we get a notification from someone that | |
1388 | * its okay to read again. | |
1389 | */ | |
d5f8d05f | 1390 | if (read_size < 2) |
52edecde | 1391 | return; |
2afaba07 | 1392 | |
f61f0107 | 1393 | if (flags.do_next_read) { |
dc56a9b1 | 1394 | flags.do_next_read = 0; |
1395 | typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer; | |
d5f8d05f | 1396 | entry->delayAwareRead(fd, readBuf->space(read_size), read_size, |
9e008dda AJ |
1397 | asyncCall(11, 5, "HttpStateData::readReply", |
1398 | Dialer(this, &HttpStateData::readReply))); | |
528b2c61 | 1399 | } |
090089c4 | 1400 | } |
1401 | ||
2afaba07 | 1402 | /* |
1403 | * This will be called when request write is complete. | |
1404 | */ | |
d576a6a6 | 1405 | void |
dc56a9b1 | 1406 | HttpStateData::sendComplete(const CommIoCbParams &io) |
090089c4 | 1407 | { |
dc56a9b1 | 1408 | debugs(11, 5, "httpSendComplete: FD " << fd << ": size " << io.size << ": errflag " << io.flag << "."); |
bc87dc25 | 1409 | #if URL_CHECKSUM_DEBUG |
62e76326 | 1410 | |
528b2c61 | 1411 | entry->mem_obj->checkUrlChecksum(); |
bc87dc25 | 1412 | #endif |
62e76326 | 1413 | |
dc56a9b1 | 1414 | if (io.size > 0) { |
1415 | fd_bytes(fd, io.size, FD_WRITE); | |
1416 | kb_incr(&statCounter.server.all.kbytes_out, io.size); | |
1417 | kb_incr(&statCounter.server.http.kbytes_out, io.size); | |
ee1679df | 1418 | } |
62e76326 | 1419 | |
dc56a9b1 | 1420 | if (io.flag == COMM_ERR_CLOSING) |
62e76326 | 1421 | return; |
1422 | ||
dc56a9b1 | 1423 | if (io.flag) { |
6cae5db1 | 1424 | ErrorState *err; |
dc56a9b1 | 1425 | err = errorCon(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY, fwd->request); |
1426 | err->xerrno = io.xerrno; | |
1427 | fwd->fail(err); | |
62e76326 | 1428 | comm_close(fd); |
1429 | return; | |
090089c4 | 1430 | } |
72b63f06 | 1431 | |
2afaba07 | 1432 | /* |
1433 | * Set the read timeout here because it hasn't been set yet. | |
1434 | * We only set the read timeout after the request has been | |
1435 | * fully written to the server-side. If we start the timeout | |
1436 | * after connection establishment, then we are likely to hit | |
1437 | * the timeout for POST/PUT requests that have very large | |
1438 | * request bodies. | |
1439 | */ | |
dc56a9b1 | 1440 | typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer; |
1441 | AsyncCall::Pointer timeoutCall = asyncCall(11, 5, "HttpStateData::httpTimeout", | |
9e008dda | 1442 | TimeoutDialer(this,&HttpStateData::httpTimeout)); |
2afaba07 | 1443 | |
dc56a9b1 | 1444 | commSetTimeout(fd, Config.Timeout.read, timeoutCall); |
1445 | ||
1446 | flags.request_sent = 1; | |
e1381638 | 1447 | |
3ff65596 | 1448 | orig_request->hier.peer_http_request_sent = current_time; |
090089c4 | 1449 | } |
1450 | ||
5f8252d2 | 1451 | // Close the HTTP server connection. Used by serverComplete(). |
2afaba07 | 1452 | void |
5f8252d2 | 1453 | HttpStateData::closeServer() |
2afaba07 | 1454 | { |
5f8252d2 | 1455 | debugs(11,5, HERE << "closing HTTP server FD " << fd << " this " << this); |
fc68f6b1 | 1456 | |
2afaba07 | 1457 | if (fd >= 0) { |
b6b6f466 | 1458 | fwd->unregister(fd); |
9e008dda | 1459 | comm_remove_close_handler(fd, closeHandler); |
dc56a9b1 | 1460 | closeHandler = NULL; |
2afaba07 | 1461 | comm_close(fd); |
1462 | fd = -1; | |
1463 | } | |
5f8252d2 | 1464 | } |
2afaba07 | 1465 | |
5f8252d2 | 1466 | bool |
1467 | HttpStateData::doneWithServer() const | |
1468 | { | |
1469 | return fd < 0; | |
2afaba07 | 1470 | } |
1471 | ||
ee0b94f4 HN |
1472 | |
1473 | /* | |
1474 | * Fixup authentication request headers for special cases | |
1475 | */ | |
1476 | static void | |
1477 | httpFixupAuthentication(HttpRequest * request, HttpRequest * orig_request, const HttpHeader * hdr_in, HttpHeader * hdr_out, http_state_flags flags) | |
1478 | { | |
1479 | http_hdr_type header = flags.originpeer ? HDR_AUTHORIZATION : HDR_PROXY_AUTHORIZATION; | |
1480 | ||
1481 | /* Nothing to do unless we are forwarding to a peer */ | |
1482 | if (!request->flags.proxying) | |
f54f527e | 1483 | return; |
ee0b94f4 HN |
1484 | |
1485 | /* Needs to be explicitly enabled */ | |
dcde39a7 | 1486 | if (!orig_request->peer_login) |
f54f527e | 1487 | return; |
ee0b94f4 HN |
1488 | |
1489 | /* Maybe already dealt with? */ | |
1490 | if (hdr_out->has(header)) | |
f54f527e | 1491 | return; |
ee0b94f4 HN |
1492 | |
1493 | /* Nothing to do here for PASSTHRU */ | |
1494 | if (strcmp(orig_request->peer_login, "PASSTHRU") == 0) | |
f54f527e | 1495 | return; |
ee0b94f4 HN |
1496 | |
1497 | /* PROXYPASS is a special case, single-signon to servers with the proxy password (basic only) */ | |
1498 | if (flags.originpeer && strcmp(orig_request->peer_login, "PROXYPASS") == 0 && hdr_in->has(HDR_PROXY_AUTHORIZATION)) { | |
f54f527e | 1499 | const char *auth = hdr_in->getStr(HDR_PROXY_AUTHORIZATION); |
ee0b94f4 | 1500 | |
f54f527e AJ |
1501 | if (auth && strncasecmp(auth, "basic ", 6) == 0) { |
1502 | hdr_out->putStr(header, auth); | |
1503 | return; | |
1504 | } | |
ee0b94f4 HN |
1505 | } |
1506 | ||
1507 | /* Special mode to pass the username to the upstream cache */ | |
1508 | if (*orig_request->peer_login == '*') { | |
f54f527e AJ |
1509 | char loginbuf[256]; |
1510 | const char *username = "-"; | |
ee0b94f4 | 1511 | |
f54f527e AJ |
1512 | if (orig_request->extacl_user.size()) |
1513 | username = orig_request->extacl_user.termedBuf(); | |
1514 | else if (orig_request->auth_user_request) | |
1515 | username = orig_request->auth_user_request->username(); | |
ee0b94f4 | 1516 | |
f54f527e | 1517 | snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, orig_request->peer_login + 1); |
ee0b94f4 | 1518 | |
f54f527e AJ |
1519 | httpHeaderPutStrf(hdr_out, header, "Basic %s", |
1520 | base64_encode(loginbuf)); | |
1521 | return; | |
ee0b94f4 HN |
1522 | } |
1523 | ||
1524 | /* external_acl provided credentials */ | |
1525 | if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size() && | |
f54f527e AJ |
1526 | (strcmp(orig_request->peer_login, "PASS") == 0 || |
1527 | strcmp(orig_request->peer_login, "PROXYPASS") == 0)) { | |
1528 | char loginbuf[256]; | |
1529 | snprintf(loginbuf, sizeof(loginbuf), SQUIDSTRINGPH ":" SQUIDSTRINGPH, | |
1530 | SQUIDSTRINGPRINT(orig_request->extacl_user), | |
1531 | SQUIDSTRINGPRINT(orig_request->extacl_passwd)); | |
1532 | httpHeaderPutStrf(hdr_out, header, "Basic %s", | |
1533 | base64_encode(loginbuf)); | |
1534 | return; | |
ee0b94f4 HN |
1535 | } |
1536 | ||
9ca29d23 AJ |
1537 | /* Kerberos login to peer */ |
1538 | #if HAVE_KRB5 && HAVE_GSSAPI | |
1539 | if (strncmp(orig_request->peer_login, "NEGOTIATE",strlen("NEGOTIATE")) == 0) { | |
1540 | char *Token=NULL; | |
1541 | char *PrincipalName=NULL,*p; | |
1542 | if ((p=strchr(orig_request->peer_login,':')) != NULL ) { | |
1543 | PrincipalName=++p; | |
1544 | } | |
1545 | Token = peer_proxy_negotiate_auth(PrincipalName,request->peer_host); | |
1546 | if (Token) { | |
1547 | httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Negotiate %s",Token); | |
1548 | } | |
1549 | return; | |
1550 | } | |
1551 | #endif /* HAVE_KRB5 && HAVE_GSSAPI */ | |
1552 | ||
0606266f | 1553 | httpHeaderPutStrf(hdr_out, header, "Basic %s", |
f54f527e | 1554 | base64_encode(orig_request->peer_login)); |
ee0b94f4 HN |
1555 | return; |
1556 | } | |
1557 | ||
99edd1c3 | 1558 | /* |
9e008dda | 1559 | * build request headers and append them to a given MemBuf |
e5ee81f0 | 1560 | * used by buildRequestPrefix() |
818c6c9e | 1561 | * note: initialised the HttpHeader, the caller is responsible for Clean()-ing |
99edd1c3 | 1562 | */ |
e1e72f06 | 1563 | void |
e5ee81f0 | 1564 | HttpStateData::httpBuildRequestHeader(HttpRequest * request, |
1565 | HttpRequest * orig_request, | |
1566 | StoreEntry * entry, | |
1567 | HttpHeader * hdr_out, | |
1568 | http_state_flags flags) | |
6bf8443a | 1569 | { |
99edd1c3 | 1570 | /* building buffer for complex strings */ |
5999b776 | 1571 | #define BBUF_SZ (MAX_URL+32) |
99edd1c3 | 1572 | LOCAL_ARRAY(char, bbuf, BBUF_SZ); |
67c06f0d | 1573 | LOCAL_ARRAY(char, ntoabuf, MAX_IPSTRLEN); |
99edd1c3 | 1574 | const HttpHeader *hdr_in = &orig_request->header; |
67c06f0d | 1575 | const HttpHeaderEntry *e = NULL; |
99edd1c3 | 1576 | HttpHeaderPos pos = HttpHeaderInitPos; |
75faaa7a | 1577 | assert (hdr_out->owner == hoRequest); |
62e76326 | 1578 | |
262a0e14 | 1579 | /* append our IMS header */ |
fa3e249f | 1580 | if (request->lastmod > -1) |
a9925b40 | 1581 | hdr_out->putTime(HDR_IF_MODIFIED_SINCE, request->lastmod); |
99edd1c3 | 1582 | |
528b2c61 | 1583 | bool we_do_ranges = decideIfWeDoRanges (orig_request); |
1584 | ||
30abd221 | 1585 | String strConnection (hdr_in->getList(HDR_CONNECTION)); |
62e76326 | 1586 | |
a9925b40 | 1587 | while ((e = hdr_in->getEntry(&pos))) |
62e76326 | 1588 | copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, orig_request, hdr_out, we_do_ranges, flags); |
528b2c61 | 1589 | |
43ae1d95 | 1590 | /* Abstraction break: We should interpret multipart/byterange responses |
528b2c61 | 1591 | * into offset-length data, and this works around our inability to do so. |
1592 | */ | |
62e76326 | 1593 | if (!we_do_ranges && orig_request->multipartRangeRequest()) { |
1594 | /* don't cache the result */ | |
1595 | orig_request->flags.cachable = 0; | |
1596 | /* pretend it's not a range request */ | |
00d77d6b | 1597 | delete orig_request->range; |
62e76326 | 1598 | orig_request->range = NULL; |
1599 | orig_request->flags.range = 0; | |
1600 | } | |
528b2c61 | 1601 | |
99edd1c3 | 1602 | /* append Via */ |
736cb6aa | 1603 | if (Config.onoff.via) { |
30abd221 | 1604 | String strVia; |
a9925b40 | 1605 | strVia = hdr_in->getList(HDR_VIA); |
62e76326 | 1606 | snprintf(bbuf, BBUF_SZ, "%d.%d %s", |
1607 | orig_request->http_ver.major, | |
1608 | orig_request->http_ver.minor, ThisCache); | |
1609 | strListAdd(&strVia, bbuf, ','); | |
d53b3f6d | 1610 | hdr_out->putStr(HDR_VIA, strVia.termedBuf()); |
30abd221 | 1611 | strVia.clean(); |
736cb6aa | 1612 | } |
62e76326 | 1613 | |
432bc83c | 1614 | if (orig_request->flags.accelerated) { |
43ae1d95 | 1615 | /* Append Surrogate-Capabilities */ |
45cca89d AJ |
1616 | String strSurrogate(hdr_in->getList(HDR_SURROGATE_CAPABILITY)); |
1617 | #if USE_SQUID_ESI | |
1618 | snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", Config.Accel.surrogate_id); | |
1619 | #else | |
1620 | snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0\"", Config.Accel.surrogate_id); | |
1621 | #endif | |
43ae1d95 | 1622 | strListAdd(&strSurrogate, bbuf, ','); |
d53b3f6d | 1623 | hdr_out->putStr(HDR_SURROGATE_CAPABILITY, strSurrogate.termedBuf()); |
43ae1d95 | 1624 | } |
43ae1d95 | 1625 | |
67c06f0d | 1626 | /** \pre Handle X-Forwarded-For */ |
9e008dda | 1627 | if (strcmp(opt_forwarded_for, "delete") != 0) { |
c4f30223 AR |
1628 | |
1629 | String strFwd = hdr_in->getList(HDR_X_FORWARDED_FOR); | |
1630 | ||
1631 | if (strFwd.size() > 65536/2) { | |
1632 | // There is probably a forwarding loop with Via detection disabled. | |
1633 | // If we do nothing, String will assert on overflow soon. | |
1634 | // TODO: Terminate all transactions with huge XFF? | |
1635 | strFwd = "error"; | |
1636 | ||
1637 | static int warnedCount = 0; | |
1638 | if (warnedCount++ < 100) { | |
1639 | const char *url = entry ? entry->url() : urlCanonical(orig_request); | |
1640 | debugs(11, 1, "Warning: likely forwarding loop with " << url); | |
1641 | } | |
1642 | } | |
1643 | ||
9e008dda | 1644 | if (strcmp(opt_forwarded_for, "on") == 0) { |
67c06f0d | 1645 | /** If set to ON - append client IP or 'unknown'. */ |
9e008dda | 1646 | if ( orig_request->client_addr.IsNoAddr() ) |
67c06f0d AJ |
1647 | strListAdd(&strFwd, "unknown", ','); |
1648 | else | |
1649 | strListAdd(&strFwd, orig_request->client_addr.NtoA(ntoabuf, MAX_IPSTRLEN), ','); | |
9e008dda | 1650 | } else if (strcmp(opt_forwarded_for, "off") == 0) { |
67c06f0d | 1651 | /** If set to OFF - append 'unknown'. */ |
67c06f0d | 1652 | strListAdd(&strFwd, "unknown", ','); |
9e008dda | 1653 | } else if (strcmp(opt_forwarded_for, "transparent") == 0) { |
67c06f0d | 1654 | /** If set to TRANSPARENT - pass through unchanged. */ |
9e008dda | 1655 | } else if (strcmp(opt_forwarded_for, "truncate") == 0) { |
67c06f0d | 1656 | /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */ |
9e008dda | 1657 | if ( orig_request->client_addr.IsNoAddr() ) |
67c06f0d AJ |
1658 | strFwd = "unknown"; |
1659 | else | |
1660 | strFwd = orig_request->client_addr.NtoA(ntoabuf, MAX_IPSTRLEN); | |
1661 | } | |
9e008dda | 1662 | if (strFwd.size() > 0) |
d53b3f6d | 1663 | hdr_out->putStr(HDR_X_FORWARDED_FOR, strFwd.termedBuf()); |
cc192b50 | 1664 | } |
67c06f0d | 1665 | /** If set to DELETE - do not copy through. */ |
6bccf575 | 1666 | |
99edd1c3 | 1667 | /* append Host if not there already */ |
a9925b40 | 1668 | if (!hdr_out->has(HDR_HOST)) { |
62e76326 | 1669 | if (orig_request->peer_domain) { |
a9925b40 | 1670 | hdr_out->putStr(HDR_HOST, orig_request->peer_domain); |
62e76326 | 1671 | } else if (orig_request->port == urlDefaultPort(orig_request->protocol)) { |
1672 | /* use port# only if not default */ | |
cc192b50 | 1673 | hdr_out->putStr(HDR_HOST, orig_request->GetHost()); |
62e76326 | 1674 | } else { |
1675 | httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d", | |
cc192b50 | 1676 | orig_request->GetHost(), |
1677 | (int) orig_request->port); | |
62e76326 | 1678 | } |
6bf8443a | 1679 | } |
62e76326 | 1680 | |
c68e9c6b | 1681 | /* append Authorization if known in URL, not in header and going direct */ |
a9925b40 | 1682 | if (!hdr_out->has(HDR_AUTHORIZATION)) { |
62e76326 | 1683 | if (!request->flags.proxying && *request->login) { |
1684 | httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s", | |
1685 | base64_encode(request->login)); | |
1686 | } | |
c68e9c6b | 1687 | } |
62e76326 | 1688 | |
ee0b94f4 HN |
1689 | /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */ |
1690 | httpFixupAuthentication(request, orig_request, hdr_in, hdr_out, flags); | |
62e76326 | 1691 | |
ee0b94f4 HN |
1692 | /* append Cache-Control, add max-age if not there already */ |
1693 | { | |
a9925b40 | 1694 | HttpHdrCc *cc = hdr_in->getCc(); |
62e76326 | 1695 | |
1696 | if (!cc) | |
1697 | cc = httpHdrCcCreate(); | |
1698 | ||
7dc5c309 AJ |
1699 | #if 0 /* see bug 2330 */ |
1700 | /* Set no-cache if determined needed but not found */ | |
1701 | if (orig_request->flags.nocache) | |
1702 | EBIT_SET(cc->mask, CC_NO_CACHE); | |
1703 | #endif | |
1704 | ||
af6a12ee AJ |
1705 | /* Add max-age only without no-cache */ |
1706 | if (!EBIT_TEST(cc->mask, CC_MAX_AGE) && !EBIT_TEST(cc->mask, CC_NO_CACHE)) { | |
43ae1d95 | 1707 | const char *url = |
3900307b | 1708 | entry ? entry->url() : urlCanonical(orig_request); |
62e76326 | 1709 | httpHdrCcSetMaxAge(cc, getMaxAge(url)); |
1710 | ||
1711 | if (request->urlpath.size()) | |
e8466ea9 | 1712 | assert(strstr(url, request->urlpath.termedBuf())); |
62e76326 | 1713 | } |
1714 | ||
ce2d6441 | 1715 | /* Enforce sibling relations */ |
62e76326 | 1716 | if (flags.only_if_cached) |
1717 | EBIT_SET(cc->mask, CC_ONLY_IF_CACHED); | |
1718 | ||
a9925b40 | 1719 | hdr_out->putCc(cc); |
62e76326 | 1720 | |
1721 | httpHdrCcDestroy(cc); | |
6bf8443a | 1722 | } |
62e76326 | 1723 | |
99edd1c3 | 1724 | /* maybe append Connection: keep-alive */ |
b515fc11 | 1725 | if (flags.keepalive) { |
62e76326 | 1726 | if (flags.proxying) { |
a9925b40 | 1727 | hdr_out->putStr(HDR_PROXY_CONNECTION, "keep-alive"); |
62e76326 | 1728 | } else { |
a9925b40 | 1729 | hdr_out->putStr(HDR_CONNECTION, "keep-alive"); |
62e76326 | 1730 | } |
603a02fd | 1731 | } |
62e76326 | 1732 | |
a7ad6e4e | 1733 | /* append Front-End-Https */ |
1734 | if (flags.front_end_https) { | |
62e76326 | 1735 | if (flags.front_end_https == 1 || request->protocol == PROTO_HTTPS) |
a9925b40 | 1736 | hdr_out->putStr(HDR_FRONT_END_HTTPS, "On"); |
a7ad6e4e | 1737 | } |
1738 | ||
6bccf575 | 1739 | /* Now mangle the headers. */ |
4f56514c | 1740 | if (Config2.onoff.mangle_request_headers) |
5967c0bf | 1741 | httpHdrMangleList(hdr_out, request, ROR_REQUEST); |
62e76326 | 1742 | |
30abd221 | 1743 | strConnection.clean(); |
99edd1c3 | 1744 | } |
1745 | ||
9e498bfb AJ |
1746 | /** |
1747 | * Decides whether a particular header may be cloned from the received Clients request | |
1748 | * to our outgoing fetch request. | |
1749 | */ | |
528b2c61 | 1750 | void |
9e498bfb | 1751 | copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, HttpRequest * request, const HttpRequest * orig_request, HttpHeader * hdr_out, const int we_do_ranges, const http_state_flags flags) |
528b2c61 | 1752 | { |
e8466ea9 | 1753 | debugs(11, 5, "httpBuildRequestHeader: " << e->name << ": " << e->value ); |
62e76326 | 1754 | |
528b2c61 | 1755 | switch (e->id) { |
62e76326 | 1756 | |
af6a12ee | 1757 | /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */ |
9e498bfb | 1758 | |
be753325 | 1759 | case HDR_PROXY_AUTHORIZATION: |
9e498bfb AJ |
1760 | /** \par Proxy-Authorization: |
1761 | * Only pass on proxy authentication to peers for which | |
62e76326 | 1762 | * authentication forwarding is explicitly enabled |
1763 | */ | |
ee0b94f4 | 1764 | if (!flags.originpeer && flags.proxying && orig_request->peer_login && |
f54f527e AJ |
1765 | (strcmp(orig_request->peer_login, "PASS") == 0 || |
1766 | strcmp(orig_request->peer_login, "PROXYPASS") == 0 || | |
1767 | strcmp(orig_request->peer_login, "PASSTHRU") == 0)) { | |
eede25e7 | 1768 | hdr_out->addEntry(e->clone()); |
62e76326 | 1769 | } |
62e76326 | 1770 | break; |
1771 | ||
af6a12ee | 1772 | /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */ |
9e498bfb AJ |
1773 | |
1774 | case HDR_CONNECTION: /** \par Connection: */ | |
1775 | case HDR_TE: /** \par TE: */ | |
1776 | case HDR_KEEP_ALIVE: /** \par Keep-Alive: */ | |
1777 | case HDR_PROXY_AUTHENTICATE: /** \par Proxy-Authenticate: */ | |
1778 | case HDR_TRAILERS: /** \par Trailers: */ | |
1779 | case HDR_UPGRADE: /** \par Upgrade: */ | |
1780 | case HDR_TRANSFER_ENCODING: /** \par Transfer-Encoding: */ | |
1781 | break; | |
1782 | ||
1783 | ||
af6a12ee | 1784 | /** \par OTHER headers I haven't bothered to track down yet. */ |
9e498bfb | 1785 | |
be753325 | 1786 | case HDR_AUTHORIZATION: |
9e498bfb AJ |
1787 | /** \par WWW-Authorization: |
1788 | * Pass on WWW authentication */ | |
62e76326 | 1789 | |
1790 | if (!flags.originpeer) { | |
eede25e7 | 1791 | hdr_out->addEntry(e->clone()); |
62e76326 | 1792 | } else { |
9e498bfb | 1793 | /** \note In accelerators, only forward authentication if enabled |
ee0b94f4 | 1794 | * (see also httpFixupAuthentication for special cases) |
62e76326 | 1795 | */ |
abb929f0 | 1796 | if (orig_request->peer_login && |
1797 | (strcmp(orig_request->peer_login, "PASS") == 0 || | |
11e4c5e5 | 1798 | strcmp(orig_request->peer_login, "PASSTHRU") == 0 || |
abb929f0 | 1799 | strcmp(orig_request->peer_login, "PROXYPASS") == 0)) { |
eede25e7 | 1800 | hdr_out->addEntry(e->clone()); |
62e76326 | 1801 | } |
1802 | } | |
1803 | ||
1804 | break; | |
1805 | ||
be753325 | 1806 | case HDR_HOST: |
9e498bfb | 1807 | /** \par Host: |
b883b594 | 1808 | * Normally Squid rewrites the Host: header. |
1809 | * However, there is one case when we don't: If the URL | |
62e76326 | 1810 | * went through our redirector and the admin configured |
1811 | * 'redir_rewrites_host' to be off. | |
1812 | */ | |
9e008dda | 1813 | if (orig_request->peer_domain) |
488b27c5 HN |
1814 | hdr_out->putStr(HDR_HOST, orig_request->peer_domain); |
1815 | else if (request->flags.redirected && !Config.onoff.redir_rewrites_host) | |
eede25e7 | 1816 | hdr_out->addEntry(e->clone()); |
b883b594 | 1817 | else { |
1818 | /* use port# only if not default */ | |
1819 | ||
1820 | if (orig_request->port == urlDefaultPort(orig_request->protocol)) { | |
cc192b50 | 1821 | hdr_out->putStr(HDR_HOST, orig_request->GetHost()); |
b883b594 | 1822 | } else { |
1823 | httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d", | |
cc192b50 | 1824 | orig_request->GetHost(), |
1825 | (int) orig_request->port); | |
b883b594 | 1826 | } |
1827 | } | |
62e76326 | 1828 | |
1829 | break; | |
1830 | ||
be753325 | 1831 | case HDR_IF_MODIFIED_SINCE: |
9e498bfb | 1832 | /** \par If-Modified-Since: |
af6a12ee | 1833 | * append unless we added our own; |
9e498bfb | 1834 | * \note at most one client's ims header can pass through */ |
b883b594 | 1835 | |
a9925b40 | 1836 | if (!hdr_out->has(HDR_IF_MODIFIED_SINCE)) |
eede25e7 | 1837 | hdr_out->addEntry(e->clone()); |
62e76326 | 1838 | |
1839 | break; | |
1840 | ||
be753325 | 1841 | case HDR_MAX_FORWARDS: |
9e498bfb | 1842 | /** \par Max-Forwards: |
fc90edc3 AJ |
1843 | * pass only on TRACE or OPTIONS requests */ |
1844 | if (orig_request->method == METHOD_TRACE || orig_request->method == METHOD_OPTIONS) { | |
1845 | const int64_t hops = e->getInt64(); | |
62e76326 | 1846 | |
1847 | if (hops > 0) | |
fc90edc3 | 1848 | hdr_out->putInt64(HDR_MAX_FORWARDS, hops - 1); |
62e76326 | 1849 | } |
1850 | ||
1851 | break; | |
1852 | ||
be753325 | 1853 | case HDR_VIA: |
9e498bfb AJ |
1854 | /** \par Via: |
1855 | * If Via is disabled then forward any received header as-is. | |
1856 | * Otherwise leave for explicit updated addition later. */ | |
62e76326 | 1857 | |
1858 | if (!Config.onoff.via) | |
eede25e7 | 1859 | hdr_out->addEntry(e->clone()); |
62e76326 | 1860 | |
1861 | break; | |
1862 | ||
be753325 | 1863 | case HDR_RANGE: |
62e76326 | 1864 | |
be753325 | 1865 | case HDR_IF_RANGE: |
62e76326 | 1866 | |
be753325 | 1867 | case HDR_REQUEST_RANGE: |
9e498bfb AJ |
1868 | /** \par Range:, If-Range:, Request-Range: |
1869 | * Only pass if we accept ranges */ | |
62e76326 | 1870 | if (!we_do_ranges) |
eede25e7 | 1871 | hdr_out->addEntry(e->clone()); |
62e76326 | 1872 | |
1873 | break; | |
1874 | ||
be753325 | 1875 | case HDR_PROXY_CONNECTION: |
62e76326 | 1876 | |
be753325 | 1877 | case HDR_X_FORWARDED_FOR: |
62e76326 | 1878 | |
be753325 | 1879 | case HDR_CACHE_CONTROL: |
9e498bfb AJ |
1880 | /** \par Proxy-Connaction:, X-Forwarded-For:, Cache-Control: |
1881 | * handled specially by Squid, so leave off for now. | |
1882 | * append these after the loop if needed */ | |
62e76326 | 1883 | break; |
1884 | ||
be753325 | 1885 | case HDR_FRONT_END_HTTPS: |
9e498bfb AJ |
1886 | /** \par Front-End-Https: |
1887 | * Pass thru only if peer is configured with front-end-https */ | |
62e76326 | 1888 | if (!flags.front_end_https) |
eede25e7 | 1889 | hdr_out->addEntry(e->clone()); |
62e76326 | 1890 | |
1891 | break; | |
1892 | ||
be753325 | 1893 | default: |
9e498bfb AJ |
1894 | /** \par default. |
1895 | * pass on all other header fields | |
1896 | * which are NOT listed by the special Connection: header. */ | |
1897 | ||
a7a42b14 | 1898 | if (strConnection.size()>0 && strListIsMember(&strConnection, e->name.termedBuf(), ',')) { |
e1ea7456 | 1899 | debugs(11, 2, "'" << e->name << "' header cropped by Connection: definition"); |
9e498bfb AJ |
1900 | return; |
1901 | } | |
1902 | ||
eede25e7 | 1903 | hdr_out->addEntry(e->clone()); |
528b2c61 | 1904 | } |
1905 | } | |
1906 | ||
e5ee81f0 | 1907 | bool |
1908 | HttpStateData::decideIfWeDoRanges (HttpRequest * orig_request) | |
528b2c61 | 1909 | { |
e5ee81f0 | 1910 | bool result = true; |
62e76326 | 1911 | /* decide if we want to do Ranges ourselves |
1912 | * and fetch the whole object now) | |
1913 | * We want to handle Ranges ourselves iff | |
1914 | * - we can actually parse client Range specs | |
1915 | * - the specs are expected to be simple enough (e.g. no out-of-order ranges) | |
1916 | * - reply will be cachable | |
1917 | * (If the reply will be uncachable we have to throw it away after | |
1918 | * serving this request, so it is better to forward ranges to | |
1919 | * the server and fetch only the requested content) | |
1920 | */ | |
1921 | ||
1922 | if (NULL == orig_request->range || !orig_request->flags.cachable | |
d67acb4e | 1923 | || orig_request->range->offsetLimitExceeded() || orig_request->flags.connection_auth) |
e5ee81f0 | 1924 | result = false; |
62e76326 | 1925 | |
9e008dda AJ |
1926 | debugs(11, 8, "decideIfWeDoRanges: range specs: " << |
1927 | orig_request->range << ", cachable: " << | |
1928 | orig_request->flags.cachable << "; we_do_ranges: " << result); | |
62e76326 | 1929 | |
1930 | return result; | |
528b2c61 | 1931 | } |
1932 | ||
62e76326 | 1933 | /* build request prefix and append it to a given MemBuf; |
99edd1c3 | 1934 | * return the length of the prefix */ |
9bc73deb | 1935 | mb_size_t |
e053c141 FC |
1936 | HttpStateData::buildRequestPrefix(HttpRequest * aRequest, |
1937 | HttpRequest * original_request, | |
1938 | StoreEntry * sentry, | |
e5ee81f0 | 1939 | MemBuf * mb, |
e053c141 | 1940 | http_state_flags stateFlags) |
99edd1c3 | 1941 | { |
1942 | const int offset = mb->size; | |
11992b6f | 1943 | HttpVersion httpver(1,0); |
2fe7eff9 | 1944 | mb->Printf("%s %s HTTP/%d.%d\r\n", |
e053c141 FC |
1945 | RequestMethodStr(aRequest->method), |
1946 | aRequest->urlpath.size() ? aRequest->urlpath.termedBuf() : "/", | |
2fe7eff9 | 1947 | httpver.major,httpver.minor); |
99edd1c3 | 1948 | /* build and pack headers */ |
1949 | { | |
75faaa7a | 1950 | HttpHeader hdr(hoRequest); |
62e76326 | 1951 | Packer p; |
e053c141 | 1952 | httpBuildRequestHeader(aRequest, original_request, sentry, &hdr, stateFlags); |
9e008dda | 1953 | |
e053c141 FC |
1954 | if (aRequest->flags.pinned && aRequest->flags.connection_auth) |
1955 | aRequest->flags.auth_sent = 1; | |
d67acb4e | 1956 | else if (hdr.has(HDR_AUTHORIZATION)) |
e053c141 | 1957 | aRequest->flags.auth_sent = 1; |
d67acb4e | 1958 | |
62e76326 | 1959 | packerToMemInit(&p, mb); |
a9925b40 | 1960 | hdr.packInto(&p); |
519e0948 | 1961 | hdr.clean(); |
62e76326 | 1962 | packerClean(&p); |
9d9d144b | 1963 | } |
99edd1c3 | 1964 | /* append header terminator */ |
2fe7eff9 | 1965 | mb->append(crlf, 2); |
99edd1c3 | 1966 | return mb->size - offset; |
6bf8443a | 1967 | } |
62e76326 | 1968 | |
090089c4 | 1969 | /* This will be called when connect completes. Write request. */ |
5f8252d2 | 1970 | bool |
2bb867b5 | 1971 | HttpStateData::sendRequest() |
090089c4 | 1972 | { |
99edd1c3 | 1973 | MemBuf mb; |
090089c4 | 1974 | |
bf8fe701 | 1975 | debugs(11, 5, "httpSendRequest: FD " << fd << ", request " << request << ", this " << this << "."); |
dc56a9b1 | 1976 | typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer; |
1977 | AsyncCall::Pointer timeoutCall = asyncCall(11, 5, "HttpStateData::httpTimeout", | |
9e008dda | 1978 | TimeoutDialer(this,&HttpStateData::httpTimeout)); |
dc56a9b1 | 1979 | commSetTimeout(fd, Config.Timeout.lifetime, timeoutCall); |
2bb867b5 | 1980 | flags.do_next_read = 1; |
5f8252d2 | 1981 | maybeReadVirginBody(); |
1982 | ||
1983 | if (orig_request->body_pipe != NULL) { | |
123ec4de | 1984 | if (!startRequestBodyFlow()) // register to receive body data |
5f8252d2 | 1985 | return false; |
9e008dda | 1986 | typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer; |
dc56a9b1 | 1987 | Dialer dialer(this, &HttpStateData::sentRequestBody); |
9e008dda | 1988 | requestSender = asyncCall(11,5, "HttpStateData::sentRequestBody", dialer); |
5f8252d2 | 1989 | } else { |
1990 | assert(!requestBodySource); | |
9e008dda | 1991 | typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer; |
dc56a9b1 | 1992 | Dialer dialer(this, &HttpStateData::sendComplete); |
9e008dda | 1993 | requestSender = asyncCall(11,5, "HttpStateData::SendComplete", dialer); |
5f8252d2 | 1994 | } |
54220df8 | 1995 | |
2bb867b5 | 1996 | if (_peer != NULL) { |
1997 | if (_peer->options.originserver) { | |
1998 | flags.proxying = 0; | |
1999 | flags.originpeer = 1; | |
62e76326 | 2000 | } else { |
2bb867b5 | 2001 | flags.proxying = 1; |
2002 | flags.originpeer = 0; | |
62e76326 | 2003 | } |
be753325 | 2004 | } else { |
2bb867b5 | 2005 | flags.proxying = 0; |
2006 | flags.originpeer = 0; | |
be753325 | 2007 | } |
62e76326 | 2008 | |
efb9218c | 2009 | /* |
99edd1c3 | 2010 | * Is keep-alive okay for all request methods? |
efb9218c | 2011 | */ |
d67acb4e | 2012 | if (orig_request->flags.must_keepalive) |
9e008dda | 2013 | flags.keepalive = 1; |
d67acb4e | 2014 | else if (!Config.onoff.server_pconns) |
2bb867b5 | 2015 | flags.keepalive = 0; |
2016 | else if (_peer == NULL) | |
2017 | flags.keepalive = 1; | |
2018 | else if (_peer->stats.n_keepalives_sent < 10) | |
2019 | flags.keepalive = 1; | |
2020 | else if ((double) _peer->stats.n_keepalives_recv / | |
2021 | (double) _peer->stats.n_keepalives_sent > 0.50) | |
2022 | flags.keepalive = 1; | |
2023 | ||
2024 | if (_peer) { | |
2025 | if (neighborType(_peer, request) == PEER_SIBLING && | |
2026 | !_peer->options.allow_miss) | |
2027 | flags.only_if_cached = 1; | |
2028 | ||
2029 | flags.front_end_https = _peer->front_end_https; | |
a7ad6e4e | 2030 | } |
62e76326 | 2031 | |
2fe7eff9 | 2032 | mb.init(); |
9ca29d23 | 2033 | request->peer_host=_peer?_peer->host:NULL; |
e5ee81f0 | 2034 | buildRequestPrefix(request, orig_request, entry, &mb, flags); |
bf8fe701 | 2035 | debugs(11, 6, "httpSendRequest: FD " << fd << ":\n" << mb.buf); |
dc56a9b1 | 2036 | comm_write_mbuf(fd, &mb, requestSender); |
5f8252d2 | 2037 | |
2038 | return true; | |
090089c4 | 2039 | } |
b6a2f15e | 2040 | |
910169e5 | 2041 | void |
b6b6f466 | 2042 | httpStart(FwdState *fwd) |
603a02fd | 2043 | { |
60745f24 | 2044 | debugs(11, 3, "httpStart: \"" << RequestMethodStr(fwd->request->method) << " " << fwd->entry->url() << "\"" ); |
a3d50c30 | 2045 | HttpStateData *httpState = new HttpStateData(fwd); |
62e76326 | 2046 | |
5f8252d2 | 2047 | if (!httpState->sendRequest()) { |
bf8fe701 | 2048 | debugs(11, 3, "httpStart: aborted"); |
5f8252d2 | 2049 | delete httpState; |
2050 | return; | |
2051 | } | |
62e76326 | 2052 | |
5f8252d2 | 2053 | statCounter.server.all.requests++; |
83704487 | 2054 | statCounter.server.http.requests++; |
62e76326 | 2055 | |
b6a2f15e | 2056 | /* |
2057 | * We used to set the read timeout here, but not any more. | |
2058 | * Now its set in httpSendComplete() after the full request, | |
2059 | * including request body, has been written to the server. | |
2060 | */ | |
090089c4 | 2061 | } |
2062 | ||
2bb867b5 | 2063 | void |
5f8252d2 | 2064 | HttpStateData::doneSendingRequestBody() |
2bb867b5 | 2065 | { |
5f8252d2 | 2066 | debugs(11,5, HERE << "doneSendingRequestBody: FD " << fd); |
aa49962c AJ |
2067 | |
2068 | #if HTTP_VIOLATIONS | |
aa49962c | 2069 | if (Config.accessList.brokenPosts) { |
af6a12ee | 2070 | ACLFilledChecklist ch(Config.accessList.brokenPosts, request, NULL); |
aa49962c AJ |
2071 | if (!ch.fastCheck()) { |
2072 | debugs(11, 5, "doneSendingRequestBody: didn't match brokenPosts"); | |
2073 | CommIoCbParams io(NULL); | |
2074 | io.fd=fd; | |
2075 | io.flag=COMM_OK; | |
2076 | sendComplete(io); | |
2077 | } else { | |
2078 | debugs(11, 2, "doneSendingRequestBody: matched brokenPosts"); | |
2079 | typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer; | |
2080 | Dialer dialer(this, &HttpStateData::sendComplete); | |
2081 | AsyncCall::Pointer call= asyncCall(11,5, "HttpStateData::SendComplete", dialer); | |
2082 | comm_write(fd, "\r\n", 2, call); | |
2083 | } | |
2084 | return; | |
54220df8 | 2085 | } |
aa49962c AJ |
2086 | debugs(11, 5, "doneSendingRequestBody: No brokenPosts list"); |
2087 | #endif /* HTTP_VIOLATIONS */ | |
2088 | ||
2089 | CommIoCbParams io(NULL); | |
2090 | io.fd=fd; | |
2091 | io.flag=COMM_OK; | |
2092 | sendComplete(io); | |
94439e4e | 2093 | } |
2094 | ||
5f8252d2 | 2095 | // more origin request body data is available |
2bb867b5 | 2096 | void |
5f8252d2 | 2097 | HttpStateData::handleMoreRequestBodyAvailable() |
2bb867b5 | 2098 | { |
2bb867b5 | 2099 | if (eof || fd < 0) { |
5f8252d2 | 2100 | // XXX: we should check this condition in other callbacks then! |
2101 | // TODO: Check whether this can actually happen: We should unsubscribe | |
2102 | // as a body consumer when the above condition(s) are detected. | |
2bb867b5 | 2103 | debugs(11, 1, HERE << "Transaction aborted while reading HTTP body"); |
2bb867b5 | 2104 | return; |
2105 | } | |
62e76326 | 2106 | |
5f8252d2 | 2107 | assert(requestBodySource != NULL); |
fc68f6b1 | 2108 | |
5f8252d2 | 2109 | if (requestBodySource->buf().hasContent()) { |
2110 | // XXX: why does not this trigger a debug message on every request? | |
fc68f6b1 | 2111 | |
2bb867b5 | 2112 | if (flags.headers_parsed && !flags.abuse_detected) { |
2113 | flags.abuse_detected = 1; | |
cc192b50 | 2114 | debugs(11, 1, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << orig_request->client_addr << "' -> '" << entry->url() << "'" ); |
21b92762 | 2115 | |
585ab260 | 2116 | if (virginReply()->sline.status == HTTP_INVALID_HEADER) { |
2bb867b5 | 2117 | comm_close(fd); |
21b92762 | 2118 | return; |
2119 | } | |
2120 | } | |
b6a2f15e | 2121 | } |
5f8252d2 | 2122 | |
2123 | HttpStateData::handleMoreRequestBodyAvailable(); | |
376bb137 | 2124 | } |
2125 | ||
5f8252d2 | 2126 | // premature end of the request body |
2bb867b5 | 2127 | void |
5f8252d2 | 2128 | HttpStateData::handleRequestBodyProducerAborted() |
376bb137 | 2129 | { |
5f8252d2 | 2130 | ServerStateData::handleRequestBodyProducerAborted(); |
2131 | // XXX: SendComplete(COMM_ERR_CLOSING) does little. Is it enough? | |
dc56a9b1 | 2132 | CommIoCbParams io(NULL); |
2133 | io.fd=fd; | |
2134 | io.flag=COMM_ERR_CLOSING; | |
2135 | sendComplete(io); | |
2bb867b5 | 2136 | } |
2137 | ||
5f8252d2 | 2138 | // called when we wrote request headers(!) or a part of the body |
2bb867b5 | 2139 | void |
dc56a9b1 | 2140 | HttpStateData::sentRequestBody(const CommIoCbParams &io) |
2bb867b5 | 2141 | { |
dc56a9b1 | 2142 | if (io.size > 0) |
2143 | kb_incr(&statCounter.server.http.kbytes_out, io.size); | |
fc68f6b1 | 2144 | |
dc56a9b1 | 2145 | ServerStateData::sentRequestBody(io); |
5f8252d2 | 2146 | } |
3b299123 | 2147 | |
5f8252d2 | 2148 | // Quickly abort the transaction |
2149 | // TODO: destruction should be sufficient as the destructor should cleanup, | |
2150 | // including canceling close handlers | |
2151 | void | |
2152 | HttpStateData::abortTransaction(const char *reason) | |
2153 | { | |
2154 | debugs(11,5, HERE << "aborting transaction for " << reason << | |
2155 | "; FD " << fd << ", this " << this); | |
fc68f6b1 | 2156 | |
3e8c047e | 2157 | if (fd >= 0) { |
62e76326 | 2158 | comm_close(fd); |
3e8c047e | 2159 | return; |
c23f0c74 | 2160 | } |
3e8c047e | 2161 | |
2162 | fwd->handleUnregisteredServerEnd(); | |
dc56a9b1 | 2163 | deleteThis("HttpStateData::abortTransaction"); |
54220df8 | 2164 | } |
ccf44862 | 2165 | |
073ba374 | 2166 | #if DEAD_CODE |
ccf44862 | 2167 | void |
450e0c10 | 2168 | httpBuildVersion(HttpVersion * version, unsigned int major, unsigned int minor) |
110eb4e5 | 2169 | { |
2170 | version->major = major; | |
2171 | version->minor = minor; | |
ccf44862 | 2172 | } |
073ba374 | 2173 | #endif |
2afaba07 | 2174 | |
7c4e4e7f | 2175 | HttpRequest * |
2176 | HttpStateData::originalRequest() | |
2afaba07 | 2177 | { |
7c4e4e7f | 2178 | return orig_request; |
2afaba07 | 2179 | } |