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