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