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