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