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