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