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