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