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