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