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