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