]>
Commit | Line | Data |
---|---|---|
da2b3a17 | 1 | |
30a4f2a8 | 2 | /* |
72b63f06 | 3 | * $Id: http.cc,v 1.449 2005/04/03 18:53:24 serassio Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 11 Hypertext Transfer Protocol (HTTP) | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
2b6662ba | 11 | * Squid is the result of efforts by numerous individuals from |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
30a4f2a8 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
30a4f2a8 | 34 | */ |
019dd986 | 35 | |
4a83b852 | 36 | /* |
37 | * Anonymizing patch by lutz@as-node.jena.thur.de | |
de3bdb4c | 38 | * have a look into http-anon.c to get more informations. |
4a83b852 | 39 | */ |
40 | ||
44a47c6e | 41 | #include "squid.h" |
e6ccf245 | 42 | #include "http.h" |
f5691f9c | 43 | #include "AuthUserRequest.h" |
e6ccf245 | 44 | #include "Store.h" |
528b2c61 | 45 | #include "HttpReply.h" |
46 | #include "HttpRequest.h" | |
47 | #include "MemObject.h" | |
48 | #include "HttpHdrContRange.h" | |
4fb35c3c | 49 | #include "ACLChecklist.h" |
21b92762 | 50 | #include "fde.h" |
b67e2c8c | 51 | #if DELAY_POOLS |
52 | #include "DelayPools.h" | |
53 | #endif | |
e6ccf245 | 54 | |
55 | CBDATA_TYPE(HttpStateData); | |
56 | ||
090089c4 | 57 | |
6bf8443a | 58 | static const char *const crlf = "\r\n"; |
4db43fab | 59 | |
c3d63b2a | 60 | static CWCB httpSendRequestEntity; |
54220df8 | 61 | |
c4b7a5a9 | 62 | static IOCB httpReadReply; |
b6a2f15e | 63 | static void httpSendRequest(HttpStateData *); |
9e4ad609 | 64 | static PF httpStateFree; |
65 | static PF httpTimeout; | |
f5b8bbc4 | 66 | static void httpCacheNegatively(StoreEntry *); |
67 | static void httpMakePrivate(StoreEntry *); | |
68 | static void httpMakePublic(StoreEntry *); | |
f9cece6e | 69 | static void httpMaybeRemovePublic(StoreEntry *, http_status); |
190154cf | 70 | static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, String strConnection, HttpRequest * request, HttpRequest * orig_request, |
62e76326 | 71 | HttpHeader * hdr_out, int we_do_ranges, http_state_flags); |
190154cf | 72 | static int decideIfWeDoRanges (HttpRequest * orig_request); |
528b2c61 | 73 | |
b8d8561b | 74 | |
b177367b | 75 | static void |
59715b38 | 76 | httpStateFree(int fd, void *data) |
f5558c95 | 77 | { |
e6ccf245 | 78 | HttpStateData *httpState = static_cast<HttpStateData *>(data); |
62e76326 | 79 | |
0d4d4170 | 80 | if (httpState == NULL) |
62e76326 | 81 | return; |
82 | ||
21b92762 | 83 | if (httpState->body_buf) { |
9ea12fca | 84 | if (httpState->orig_request->body_connection.getRaw()) { |
85 | clientAbortBody(httpState->orig_request); | |
86 | } | |
21b92762 | 87 | |
88 | if (httpState->body_buf) { | |
89 | memFree(httpState->body_buf, MEM_8K_BUF); | |
90 | httpState->body_buf = NULL; | |
91 | } | |
92 | } | |
93 | ||
f88211e8 | 94 | storeUnlockObject(httpState->entry); |
62e76326 | 95 | |
4eb368f9 | 96 | if (!memBufIsNull(&httpState->reply_hdr)) { |
97 | memBufClean(&httpState->reply_hdr); | |
0d4d4170 | 98 | } |
62e76326 | 99 | |
30a4f2a8 | 100 | requestUnlink(httpState->request); |
20cc1450 | 101 | requestUnlink(httpState->orig_request); |
7dd44885 | 102 | httpState->request = NULL; |
103 | httpState->orig_request = NULL; | |
104 | cbdataFree(httpState); | |
f5558c95 | 105 | } |
106 | ||
b8d8561b | 107 | int |
75e88d56 | 108 | httpCachable(method_t method) |
090089c4 | 109 | { |
090089c4 | 110 | /* GET and HEAD are cachable. Others are not. */ |
62e76326 | 111 | |
6eb42cae | 112 | if (method != METHOD_GET && method != METHOD_HEAD) |
62e76326 | 113 | return 0; |
114 | ||
090089c4 | 115 | /* else cachable */ |
116 | return 1; | |
117 | } | |
118 | ||
b8d8561b | 119 | static void |
5c5783a2 | 120 | httpTimeout(int fd, void *data) |
090089c4 | 121 | { |
e6ccf245 | 122 | HttpStateData *httpState = static_cast<HttpStateData *>(data); |
593c9a75 | 123 | StoreEntry *entry = httpState->entry; |
9fb13bb6 | 124 | debug(11, 4) ("httpTimeout: FD %d: '%s'\n", fd, storeUrl(entry)); |
62e76326 | 125 | |
12158bdc | 126 | if (entry->store_status == STORE_PENDING) { |
62e76326 | 127 | if (entry->isEmpty()) { |
128 | fwdFail(httpState->fwd, | |
129 | errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT)); | |
130 | } | |
9b312a19 | 131 | } |
62e76326 | 132 | |
0d4d4170 | 133 | comm_close(fd); |
090089c4 | 134 | } |
135 | ||
30a4f2a8 | 136 | /* This object can be cached for a long time */ |
b8d8561b | 137 | static void |
138 | httpMakePublic(StoreEntry * entry) | |
30a4f2a8 | 139 | { |
d46a87a8 | 140 | if (EBIT_TEST(entry->flags, ENTRY_CACHABLE)) |
62e76326 | 141 | storeSetPublicKey(entry); |
30a4f2a8 | 142 | } |
143 | ||
144 | /* This object should never be cached at all */ | |
b8d8561b | 145 | static void |
146 | httpMakePrivate(StoreEntry * entry) | |
30a4f2a8 | 147 | { |
30a4f2a8 | 148 | storeExpireNow(entry); |
30a4f2a8 | 149 | storeReleaseRequest(entry); /* delete object when not used */ |
f3e570e9 | 150 | /* storeReleaseRequest clears ENTRY_CACHABLE flag */ |
30a4f2a8 | 151 | } |
152 | ||
153 | /* This object may be negatively cached */ | |
b8d8561b | 154 | static void |
155 | httpCacheNegatively(StoreEntry * entry) | |
30a4f2a8 | 156 | { |
79b5cc5f | 157 | storeNegativeCache(entry); |
62e76326 | 158 | |
d46a87a8 | 159 | if (EBIT_TEST(entry->flags, ENTRY_CACHABLE)) |
62e76326 | 160 | storeSetPublicKey(entry); |
30a4f2a8 | 161 | } |
162 | ||
f9cece6e | 163 | static void |
164 | httpMaybeRemovePublic(StoreEntry * e, http_status status) | |
165 | { | |
62e76326 | 166 | |
167 | int remove | |
168 | = 0; | |
169 | ||
7e3ce7b9 | 170 | int forbidden = 0; |
62e76326 | 171 | |
f9cece6e | 172 | StoreEntry *pe; |
62e76326 | 173 | |
d46a87a8 | 174 | if (!EBIT_TEST(e->flags, KEY_PRIVATE)) |
62e76326 | 175 | return; |
176 | ||
f9cece6e | 177 | switch (status) { |
62e76326 | 178 | |
f9cece6e | 179 | case HTTP_OK: |
62e76326 | 180 | |
f9cece6e | 181 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
62e76326 | 182 | |
f9cece6e | 183 | case HTTP_MULTIPLE_CHOICES: |
62e76326 | 184 | |
f9cece6e | 185 | case HTTP_MOVED_PERMANENTLY: |
62e76326 | 186 | |
f9cece6e | 187 | case HTTP_MOVED_TEMPORARILY: |
62e76326 | 188 | |
f9cece6e | 189 | case HTTP_GONE: |
62e76326 | 190 | |
7e3ce7b9 | 191 | case HTTP_NOT_FOUND: |
62e76326 | 192 | |
193 | remove | |
194 | = 1; | |
195 | ||
196 | break; | |
197 | ||
7e3ce7b9 | 198 | case HTTP_FORBIDDEN: |
62e76326 | 199 | |
7e3ce7b9 | 200 | case HTTP_METHOD_NOT_ALLOWED: |
62e76326 | 201 | forbidden = 1; |
202 | ||
203 | break; | |
204 | ||
f9cece6e | 205 | #if WORK_IN_PROGRESS |
62e76326 | 206 | |
c8fd0193 | 207 | case HTTP_UNAUTHORIZED: |
62e76326 | 208 | forbidden = 1; |
209 | ||
210 | break; | |
211 | ||
f9cece6e | 212 | #endif |
62e76326 | 213 | |
f9cece6e | 214 | default: |
7e3ce7b9 | 215 | #if QUESTIONABLE |
62e76326 | 216 | /* |
217 | * Any 2xx response should eject previously cached entities... | |
218 | */ | |
abb929f0 | 219 | |
62e76326 | 220 | if (status >= 200 && status < 300) |
221 | remove | |
222 | = 1; | |
223 | ||
7e3ce7b9 | 224 | #endif |
62e76326 | 225 | |
226 | break; | |
f9cece6e | 227 | } |
62e76326 | 228 | |
229 | if (!remove | |
230 | && !forbidden) | |
231 | return; | |
232 | ||
f9cece6e | 233 | assert(e->mem_obj); |
62e76326 | 234 | |
f66a9ef4 | 235 | if (e->mem_obj->request) |
62e76326 | 236 | pe = storeGetPublicByRequest(e->mem_obj->request); |
f66a9ef4 | 237 | else |
62e76326 | 238 | pe = storeGetPublic(e->mem_obj->url, e->mem_obj->method); |
239 | ||
f66a9ef4 | 240 | if (pe != NULL) { |
62e76326 | 241 | assert(e != pe); |
242 | storeRelease(pe); | |
0856d155 | 243 | } |
62e76326 | 244 | |
7e3ce7b9 | 245 | /* |
246 | * Also remove any cached HEAD response in case the object has | |
247 | * changed. | |
248 | */ | |
f66a9ef4 | 249 | if (e->mem_obj->request) |
62e76326 | 250 | pe = storeGetPublicByRequestMethod(e->mem_obj->request, METHOD_HEAD); |
f66a9ef4 | 251 | else |
62e76326 | 252 | pe = storeGetPublic(e->mem_obj->url, METHOD_HEAD); |
253 | ||
f66a9ef4 | 254 | if (pe != NULL) { |
62e76326 | 255 | assert(e != pe); |
256 | storeRelease(pe); | |
7e3ce7b9 | 257 | } |
62e76326 | 258 | |
7e3ce7b9 | 259 | if (forbidden) |
62e76326 | 260 | return; |
261 | ||
7e3ce7b9 | 262 | switch (e->mem_obj->method) { |
62e76326 | 263 | |
7e3ce7b9 | 264 | case METHOD_PUT: |
62e76326 | 265 | |
7e3ce7b9 | 266 | case METHOD_DELETE: |
62e76326 | 267 | |
7e3ce7b9 | 268 | case METHOD_PROPPATCH: |
62e76326 | 269 | |
7e3ce7b9 | 270 | case METHOD_MKCOL: |
62e76326 | 271 | |
7e3ce7b9 | 272 | case METHOD_MOVE: |
62e76326 | 273 | |
42b51993 | 274 | case METHOD_BMOVE: |
62e76326 | 275 | |
42b51993 | 276 | case METHOD_BDELETE: |
62e76326 | 277 | /* |
278 | * Remove any cached GET object if it is beleived that the | |
279 | * object may have changed as a result of other methods | |
280 | */ | |
281 | ||
282 | if (e->mem_obj->request) | |
283 | pe = storeGetPublicByRequestMethod(e->mem_obj->request, METHOD_GET); | |
284 | else | |
285 | pe = storeGetPublic(e->mem_obj->url, METHOD_GET); | |
286 | ||
287 | if (pe != NULL) { | |
288 | assert(e != pe); | |
289 | storeRelease(pe); | |
290 | } | |
291 | ||
292 | break; | |
293 | ||
c8be6d7b | 294 | default: |
62e76326 | 295 | /* Keep GCC happy. The methods above are all mutating HTTP methods |
296 | */ | |
297 | break; | |
0856d155 | 298 | } |
f9cece6e | 299 | } |
300 | ||
43ae1d95 | 301 | void |
302 | HttpStateData::processSurrogateControl(HttpReply *reply) | |
303 | { | |
304 | #if ESI | |
305 | ||
306 | if (request->flags.accelerated && reply->surrogate_control) { | |
307 | HttpHdrScTarget *sctusable = | |
308 | httpHdrScGetMergedTarget(reply->surrogate_control, | |
309 | Config.Accel.surrogate_id); | |
310 | ||
311 | if (sctusable) { | |
312 | if (EBIT_TEST(sctusable->mask, SC_NO_STORE) || | |
313 | (Config.onoff.surrogate_is_remote | |
314 | && EBIT_TEST(sctusable->mask, SC_NO_STORE_REMOTE))) { | |
315 | surrogateNoStore = true; | |
316 | httpMakePrivate(entry); | |
317 | } | |
318 | ||
319 | /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an | |
320 | * accelerated request or not... | |
321 | * Still, this is an abtraction breach. - RC | |
322 | */ | |
323 | if (sctusable->max_age != -1) { | |
324 | if (sctusable->max_age < sctusable->max_stale) | |
325 | reply->expires = reply->date + sctusable->max_age; | |
326 | else | |
327 | reply->expires = reply->date + sctusable->max_stale; | |
328 | ||
329 | /* And update the timestamps */ | |
330 | storeTimestampsSet(entry); | |
331 | } | |
332 | ||
333 | /* We ignore cache-control directives as per the Surrogate specification */ | |
334 | ignoreCacheControl = true; | |
335 | ||
336 | httpHdrScTargetDestroy(sctusable); | |
337 | } | |
338 | } | |
339 | ||
340 | #endif | |
341 | } | |
342 | ||
924f73bc | 343 | int |
344 | HttpStateData::cacheableReply() | |
c54e9052 | 345 | { |
924f73bc | 346 | HttpReply const *rep = entry->getReply(); |
528b2c61 | 347 | HttpHeader const *hdr = &rep->header; |
d8b249ef | 348 | const int cc_mask = (rep->cache_control) ? rep->cache_control->mask : 0; |
c68e9c6b | 349 | const char *v; |
38f9c547 | 350 | #if HTTP_VIOLATIONS |
62e76326 | 351 | |
38f9c547 | 352 | const refresh_t *R = NULL; |
353 | #endif | |
43ae1d95 | 354 | |
38f9c547 | 355 | if (surrogateNoStore) |
62e76326 | 356 | return 0; |
357 | ||
924f73bc | 358 | if (!ignoreCacheControl) { |
38f9c547 | 359 | if (EBIT_TEST(cc_mask, CC_PRIVATE)) { |
360 | #if HTTP_VIOLATIONS | |
43ae1d95 | 361 | |
38f9c547 | 362 | if (!R) |
363 | R = refreshLimits(entry->mem_obj->url); | |
43ae1d95 | 364 | |
38f9c547 | 365 | if (R && !R->flags.ignore_private) |
366 | #endif | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
371 | if (EBIT_TEST(cc_mask, CC_NO_CACHE)) { | |
372 | #if HTTP_VIOLATIONS | |
373 | ||
374 | if (!R) | |
375 | R = refreshLimits(entry->mem_obj->url); | |
376 | ||
377 | if (R && !R->flags.ignore_no_cache) | |
378 | #endif | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
383 | if (EBIT_TEST(cc_mask, CC_NO_STORE)) { | |
384 | #if HTTP_VIOLATIONS | |
385 | ||
386 | if (!R) | |
387 | R = refreshLimits(entry->mem_obj->url); | |
388 | ||
389 | if (R && !R->flags.ignore_no_store) | |
390 | #endif | |
391 | ||
392 | return 0; | |
393 | } | |
43ae1d95 | 394 | } |
395 | ||
924f73bc | 396 | if (request->flags.auth) { |
62e76326 | 397 | /* |
398 | * Responses to requests with authorization may be cached | |
399 | * only if a Cache-Control: public reply header is present. | |
400 | * RFC 2068, sec 14.9.4 | |
401 | */ | |
402 | ||
38f9c547 | 403 | if (!EBIT_TEST(cc_mask, CC_PUBLIC)) { |
404 | #if HTTP_VIOLATIONS | |
405 | ||
406 | if (!R) | |
407 | R = refreshLimits(entry->mem_obj->url); | |
408 | ||
409 | if (R && !R->flags.ignore_auth) | |
410 | #endif | |
411 | ||
412 | return 0; | |
413 | } | |
a6dfe2d9 | 414 | } |
62e76326 | 415 | |
c68e9c6b | 416 | /* Pragma: no-cache in _replies_ is not documented in HTTP, |
417 | * but servers like "Active Imaging Webcast/2.0" sure do use it */ | |
418 | if (httpHeaderHas(hdr, HDR_PRAGMA)) { | |
62e76326 | 419 | String s = httpHeaderGetList(hdr, HDR_PRAGMA); |
420 | const int no_cache = strListIsMember(&s, "no-cache", ','); | |
421 | s.clean(); | |
422 | ||
38f9c547 | 423 | if (no_cache) { |
424 | #if HTTP_VIOLATIONS | |
425 | ||
426 | if (!R) | |
427 | R = refreshLimits(entry->mem_obj->url); | |
428 | ||
429 | if (R && !R->flags.ignore_no_cache) | |
430 | #endif | |
431 | ||
432 | return 0; | |
433 | } | |
c68e9c6b | 434 | } |
62e76326 | 435 | |
c68e9c6b | 436 | /* |
437 | * The "multipart/x-mixed-replace" content type is used for | |
438 | * continuous push replies. These are generally dynamic and | |
439 | * probably should not be cachable | |
440 | */ | |
441 | if ((v = httpHeaderGetStr(hdr, HDR_CONTENT_TYPE))) | |
62e76326 | 442 | if (!strncasecmp(v, "multipart/x-mixed-replace", 25)) |
443 | return 0; | |
444 | ||
924f73bc | 445 | switch (entry->getReply()->sline.status) { |
62e76326 | 446 | /* Responses that are cacheable */ |
447 | ||
19a04dac | 448 | case HTTP_OK: |
62e76326 | 449 | |
19a04dac | 450 | case HTTP_NON_AUTHORITATIVE_INFORMATION: |
62e76326 | 451 | |
19a04dac | 452 | case HTTP_MULTIPLE_CHOICES: |
62e76326 | 453 | |
19a04dac | 454 | case HTTP_MOVED_PERMANENTLY: |
62e76326 | 455 | |
19a04dac | 456 | case HTTP_GONE: |
62e76326 | 457 | /* |
458 | * Don't cache objects that need to be refreshed on next request, | |
459 | * unless we know how to refresh it. | |
460 | */ | |
461 | ||
924f73bc | 462 | if (!refreshIsCachable(entry)) |
62e76326 | 463 | return 0; |
464 | ||
465 | /* don't cache objects from peers w/o LMT, Date, or Expires */ | |
466 | /* check that is it enough to check headers @?@ */ | |
467 | if (rep->date > -1) | |
468 | return 1; | |
469 | else if (rep->last_modified > -1) | |
470 | return 1; | |
924f73bc | 471 | else if (!_peer) |
62e76326 | 472 | return 1; |
473 | ||
474 | /* @?@ (here and 302): invalid expires header compiles to squid_curtime */ | |
475 | else if (rep->expires > -1) | |
476 | return 1; | |
477 | else | |
478 | return 0; | |
479 | ||
480 | /* NOTREACHED */ | |
481 | break; | |
482 | ||
483 | /* Responses that only are cacheable if the server says so */ | |
484 | ||
19a04dac | 485 | case HTTP_MOVED_TEMPORARILY: |
62e76326 | 486 | if (rep->expires > -1) |
487 | return 1; | |
488 | else | |
489 | return 0; | |
490 | ||
491 | /* NOTREACHED */ | |
492 | break; | |
493 | ||
494 | /* Errors can be negatively cached */ | |
495 | ||
19a04dac | 496 | case HTTP_NO_CONTENT: |
62e76326 | 497 | |
19a04dac | 498 | case HTTP_USE_PROXY: |
62e76326 | 499 | |
19a04dac | 500 | case HTTP_BAD_REQUEST: |
62e76326 | 501 | |
19a04dac | 502 | case HTTP_FORBIDDEN: |
62e76326 | 503 | |
19a04dac | 504 | case HTTP_NOT_FOUND: |
62e76326 | 505 | |
19a04dac | 506 | case HTTP_METHOD_NOT_ALLOWED: |
62e76326 | 507 | |
19a04dac | 508 | case HTTP_REQUEST_URI_TOO_LARGE: |
62e76326 | 509 | |
19a04dac | 510 | case HTTP_INTERNAL_SERVER_ERROR: |
62e76326 | 511 | |
19a04dac | 512 | case HTTP_NOT_IMPLEMENTED: |
62e76326 | 513 | |
19a04dac | 514 | case HTTP_BAD_GATEWAY: |
62e76326 | 515 | |
19a04dac | 516 | case HTTP_SERVICE_UNAVAILABLE: |
62e76326 | 517 | |
19a04dac | 518 | case HTTP_GATEWAY_TIMEOUT: |
62e76326 | 519 | return -1; |
520 | ||
521 | /* NOTREACHED */ | |
522 | break; | |
523 | ||
524 | /* Some responses can never be cached */ | |
525 | ||
0cdcddb9 | 526 | case HTTP_PARTIAL_CONTENT: /* Not yet supported */ |
62e76326 | 527 | |
19a04dac | 528 | case HTTP_SEE_OTHER: |
62e76326 | 529 | |
19a04dac | 530 | case HTTP_NOT_MODIFIED: |
62e76326 | 531 | |
19a04dac | 532 | case HTTP_UNAUTHORIZED: |
62e76326 | 533 | |
19a04dac | 534 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
62e76326 | 535 | |
0cdcddb9 | 536 | case HTTP_INVALID_HEADER: /* Squid header parsing error */ |
4eb368f9 | 537 | |
538 | case HTTP_HEADER_TOO_LARGE: | |
62e76326 | 539 | return 0; |
540 | ||
c54e9052 | 541 | default: /* Unknown status code */ |
924f73bc | 542 | debug (11,0)("HttpStateData::cacheableReply: unknown http status code in reply\n"); |
62e76326 | 543 | |
544 | return 0; | |
545 | ||
546 | /* NOTREACHED */ | |
547 | break; | |
c54e9052 | 548 | } |
62e76326 | 549 | |
79d39a72 | 550 | /* NOTREACHED */ |
c54e9052 | 551 | } |
090089c4 | 552 | |
f66a9ef4 | 553 | /* |
554 | * For Vary, store the relevant request headers as | |
555 | * virtual headers in the reply | |
556 | * Returns false if the variance cannot be stored | |
557 | */ | |
558 | const char * | |
190154cf | 559 | httpMakeVaryMark(HttpRequest * request, HttpReply const * reply) |
f66a9ef4 | 560 | { |
f66a9ef4 | 561 | String vary, hdr; |
562 | const char *pos = NULL; | |
563 | const char *item; | |
564 | const char *value; | |
565 | int ilen; | |
528b2c61 | 566 | static String vstr; |
f66a9ef4 | 567 | |
528b2c61 | 568 | vstr.clean(); |
f66a9ef4 | 569 | vary = httpHeaderGetList(&reply->header, HDR_VARY); |
62e76326 | 570 | |
f66a9ef4 | 571 | while (strListGetItem(&vary, ',', &item, &ilen, &pos)) { |
62e76326 | 572 | char *name = (char *)xmalloc(ilen + 1); |
573 | xstrncpy(name, item, ilen + 1); | |
574 | Tolower(name); | |
9776e3cc | 575 | |
576 | if (strcmp(name, "*") == 0) { | |
577 | /* Can not handle "Vary: *" withtout ETag support */ | |
578 | safe_free(name); | |
579 | vstr.clean(); | |
580 | break; | |
581 | } | |
582 | ||
62e76326 | 583 | strListAdd(&vstr, name, ','); |
584 | hdr = httpHeaderGetByName(&request->header, name); | |
585 | safe_free(name); | |
586 | value = hdr.buf(); | |
587 | ||
588 | if (value) { | |
589 | value = rfc1738_escape_part(value); | |
590 | vstr.append("=\"", 2); | |
591 | vstr.append(value); | |
592 | vstr.append("\"", 1); | |
593 | } | |
594 | ||
595 | hdr.clean(); | |
f66a9ef4 | 596 | } |
62e76326 | 597 | |
528b2c61 | 598 | vary.clean(); |
f66a9ef4 | 599 | #if X_ACCELERATOR_VARY |
62e76326 | 600 | |
aa38be4a | 601 | pos = NULL; |
f66a9ef4 | 602 | vary = httpHeaderGetList(&reply->header, HDR_X_ACCELERATOR_VARY); |
62e76326 | 603 | |
f66a9ef4 | 604 | while (strListGetItem(&vary, ',', &item, &ilen, &pos)) { |
62e76326 | 605 | char *name = (char *)xmalloc(ilen + 1); |
606 | xstrncpy(name, item, ilen + 1); | |
607 | Tolower(name); | |
608 | strListAdd(&vstr, name, ','); | |
609 | hdr = httpHeaderGetByName(&request->header, name); | |
610 | safe_free(name); | |
611 | value = hdr.buf(); | |
612 | ||
613 | if (value) { | |
614 | value = rfc1738_escape_part(value); | |
615 | vstr.append("=\"", 2); | |
616 | vstr.append(value); | |
617 | vstr.append("\"", 1); | |
618 | } | |
619 | ||
620 | hdr.clean(); | |
f66a9ef4 | 621 | } |
62e76326 | 622 | |
528b2c61 | 623 | vary.clean(); |
f66a9ef4 | 624 | #endif |
62e76326 | 625 | |
528b2c61 | 626 | debug(11, 3) ("httpMakeVaryMark: %s\n", vstr.buf()); |
627 | return vstr.buf(); | |
f66a9ef4 | 628 | } |
629 | ||
4eb368f9 | 630 | void |
631 | HttpStateData::failReply(HttpReply *reply, http_status const & status) | |
632 | { | |
633 | reply->sline.version = HttpVersion(1, 0); | |
634 | reply->sline.status = status; | |
635 | storeEntryReplaceObject (entry, reply); | |
636 | ||
637 | if (eof == 1) { | |
638 | fwdComplete(fwd); | |
639 | comm_close(fd); | |
640 | } | |
641 | } | |
642 | ||
643 | /* rewrite this later using new interfaces @?@ | |
644 | * This creates the error page itself.. its likely | |
645 | * that the forward ported reply header max size patch | |
646 | * generates non http conformant error pages - in which | |
647 | * case the errors where should be 'BAD_GATEWAY' etc | |
648 | */ | |
b8d8561b | 649 | void |
e6ccf245 | 650 | HttpStateData::processReplyHeader(const char *buf, int size) |
f5558c95 | 651 | { |
9bc73deb | 652 | size_t hdr_len; |
bdec8360 | 653 | size_t hdr_size; |
528b2c61 | 654 | /* Creates a blank header. If this routine is made incremental, this will |
655 | * not do | |
656 | */ | |
657 | HttpReply *reply = httpReplyCreate(); | |
82384411 | 658 | Ctx ctx = ctx_enter(entry->mem_obj->url); |
b6cfb65c | 659 | debug(11, 3) ("httpProcessReplyHeader: key '%s'\n", |
62e76326 | 660 | entry->getMD5Text()); |
661 | ||
4eb368f9 | 662 | if (memBufIsNull(&reply_hdr)) |
663 | memBufDefInit(&reply_hdr); | |
62e76326 | 664 | |
e6ccf245 | 665 | assert(reply_hdr_state == 0); |
62e76326 | 666 | |
bdec8360 | 667 | memBufAppend(&reply_hdr, buf, size); |
62e76326 | 668 | |
4eb368f9 | 669 | hdr_len = reply_hdr.size; |
62e76326 | 670 | |
4eb368f9 | 671 | if (hdr_len > 4 && strncmp(reply_hdr.buf, "HTTP/", 5)) { |
c8f4eac4 | 672 | debugs(11, 3, "httpProcessReplyHeader: Non-HTTP-compliant header: '" << reply_hdr.buf << "'"); |
4eb368f9 | 673 | reply_hdr_state += 2; |
674 | memBufClean(&reply_hdr); | |
675 | failReply (reply, HTTP_INVALID_HEADER); | |
82384411 | 676 | ctx_exit(ctx); |
4eb368f9 | 677 | return; |
678 | } | |
62e76326 | 679 | |
bdec8360 | 680 | hdr_size = headersEnd(reply_hdr.buf, hdr_len); |
62e76326 | 681 | |
4eb368f9 | 682 | if (hdr_size) |
683 | hdr_len = hdr_size; | |
62e76326 | 684 | |
4eb368f9 | 685 | if (hdr_len > Config.maxReplyHeaderSize) { |
c8f4eac4 | 686 | debugs(11, 1, "httpProcessReplyHeader: Too large reply header"); |
62e76326 | 687 | |
4eb368f9 | 688 | if (!memBufIsNull(&reply_hdr)) |
689 | memBufClean(&reply_hdr); | |
62e76326 | 690 | |
4eb368f9 | 691 | failReply (reply, HTTP_HEADER_TOO_LARGE); |
62e76326 | 692 | |
82384411 | 693 | reply_hdr_state += 2; |
694 | ||
695 | ctx_exit(ctx); | |
696 | ||
62e76326 | 697 | return; |
f5558c95 | 698 | } |
62e76326 | 699 | |
9bc73deb | 700 | /* headers can be incomplete only if object still arriving */ |
4eb368f9 | 701 | if (!hdr_size) { |
702 | if (eof) | |
703 | hdr_size = hdr_len; | |
82384411 | 704 | else { |
705 | ctx_exit(ctx); | |
62e76326 | 706 | return; /* headers not complete */ |
82384411 | 707 | } |
9bc73deb | 708 | } |
62e76326 | 709 | |
4eb368f9 | 710 | /* Cut away any excess body data (only needed for debug?) */ |
711 | memBufAppend(&reply_hdr, "\0", 1); | |
712 | ||
713 | reply_hdr.buf[hdr_size] = '\0'; | |
714 | ||
e6ccf245 | 715 | reply_hdr_state++; |
4eb368f9 | 716 | |
e6ccf245 | 717 | assert(reply_hdr_state == 1); |
4eb368f9 | 718 | |
e6ccf245 | 719 | reply_hdr_state++; |
4eb368f9 | 720 | |
9bc73deb | 721 | debug(11, 9) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n", |
4eb368f9 | 722 | reply_hdr.buf); |
723 | ||
9bc73deb | 724 | /* Parse headers into reply structure */ |
725 | /* what happens if we fail to parse here? */ | |
4eb368f9 | 726 | httpReplyParse(reply, reply_hdr.buf, hdr_size); |
47ac2ebe | 727 | |
728 | if (reply->sline.status >= HTTP_INVALID_HEADER) { | |
c8f4eac4 | 729 | debugs(11, 3, "httpProcessReplyHeader: Non-HTTP-compliant header: '" << reply_hdr.buf << "'"); |
4eb368f9 | 730 | failReply (reply, HTTP_INVALID_HEADER); |
82384411 | 731 | ctx_exit(ctx); |
47ac2ebe | 732 | return; |
733 | } | |
734 | ||
43ae1d95 | 735 | processSurrogateControl (reply); |
528b2c61 | 736 | /* TODO: we need our own reply * in the httpState, as we probably don't want to replace |
737 | * the storeEntry with interim headers | |
738 | */ | |
739 | ||
740 | /* TODO: IF the reply is a 1.0 reply, AND it has a Connection: Header | |
741 | * Parse the header and remove all referenced headers | |
742 | */ | |
743 | ||
744 | storeEntryReplaceObject(entry, reply); | |
745 | /* DO NOT USE reply now */ | |
746 | reply = NULL; | |
747 | ||
123516e5 | 748 | if (entry->getReply()->sline.status == HTTP_PARTIAL_CONTENT && |
749 | entry->getReply()->content_range) | |
62e76326 | 750 | currentOffset = entry->getReply()->content_range->spec.offset; |
751 | ||
9bc73deb | 752 | storeTimestampsSet(entry); |
62e76326 | 753 | |
9bc73deb | 754 | /* Check if object is cacheable or not based on reply code */ |
528b2c61 | 755 | debug(11, 3) ("httpProcessReplyHeader: HTTP CODE: %d\n", entry->getReply()->sline.status); |
62e76326 | 756 | |
9bc73deb | 757 | if (neighbors_do_private_keys) |
62e76326 | 758 | httpMaybeRemovePublic(entry, entry->getReply()->sline.status); |
e6ccf245 | 759 | |
4b44c907 | 760 | if (httpHeaderHas(&entry->getReply()->header, HDR_VARY) |
f66a9ef4 | 761 | #if X_ACCELERATOR_VARY |
4b44c907 | 762 | || httpHeaderHas(&entry->getReply()->header, HDR_X_ACCELERATOR_VARY) |
f66a9ef4 | 763 | #endif |
4b44c907 | 764 | ) { |
765 | const char *vary = httpMakeVaryMark(orig_request, entry->getReply()); | |
766 | ||
767 | if (!vary) { | |
768 | httpMakePrivate(entry); | |
769 | goto no_cache; | |
770 | ||
62e76326 | 771 | } |
772 | ||
4b44c907 | 773 | entry->mem_obj->vary_headers = xstrdup(vary); |
774 | } | |
775 | ||
776 | switch (cacheableReply()) { | |
777 | ||
778 | case 1: | |
779 | httpMakePublic(entry); | |
62e76326 | 780 | break; |
781 | ||
9bc73deb | 782 | case 0: |
62e76326 | 783 | httpMakePrivate(entry); |
784 | break; | |
785 | ||
9bc73deb | 786 | case -1: |
4b44c907 | 787 | |
788 | if (Config.negativeTtl > 0) | |
789 | httpCacheNegatively(entry); | |
790 | else | |
791 | httpMakePrivate(entry); | |
792 | ||
62e76326 | 793 | break; |
794 | ||
9bc73deb | 795 | default: |
62e76326 | 796 | assert(0); |
4b44c907 | 797 | |
62e76326 | 798 | break; |
9bc73deb | 799 | } |
62e76326 | 800 | |
4b44c907 | 801 | no_cache: |
802 | ||
43ae1d95 | 803 | if (!ignoreCacheControl && entry->getReply()->cache_control) { |
62e76326 | 804 | if (EBIT_TEST(entry->getReply()->cache_control->mask, CC_PROXY_REVALIDATE)) |
805 | EBIT_SET(entry->flags, ENTRY_REVALIDATE); | |
806 | else if (EBIT_TEST(entry->getReply()->cache_control->mask, CC_MUST_REVALIDATE)) | |
807 | EBIT_SET(entry->flags, ENTRY_REVALIDATE); | |
9bc73deb | 808 | } |
62e76326 | 809 | |
e6ccf245 | 810 | if (flags.keepalive) |
62e76326 | 811 | if (_peer) |
812 | _peer->stats.n_keepalives_sent++; | |
813 | ||
21b92762 | 814 | if (entry->getReply()->keep_alive) { |
62e76326 | 815 | if (_peer) |
816 | _peer->stats.n_keepalives_recv++; | |
817 | ||
21b92762 | 818 | if (Config.onoff.detect_broken_server_pconns && httpReplyBodySize(request->method, reply) == -1) { |
819 | debug(11, 1) ("httpProcessReplyHeader: Impossible keep-alive header from '%s'\n", storeUrl(entry)); | |
820 | debug(11, 2) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n", reply_hdr.buf); | |
821 | flags.keepalive_broken = 1; | |
822 | } | |
823 | } | |
824 | ||
528b2c61 | 825 | if (entry->getReply()->date > -1 && !_peer) { |
411c6ea3 | 826 | int skew = abs((int)(entry->getReply()->date - squid_curtime)); |
62e76326 | 827 | |
828 | if (skew > 86400) | |
829 | debug(11, 3) ("%s's clock is skewed by %d seconds!\n", | |
830 | request->host, skew); | |
f5558c95 | 831 | } |
62e76326 | 832 | |
9bc73deb | 833 | ctx_exit(ctx); |
c3609322 | 834 | #if HEADERS_LOG |
62e76326 | 835 | |
528b2c61 | 836 | headersLog(1, 0, request->method, entry->getReply()); |
c3609322 | 837 | #endif |
62e76326 | 838 | |
e6ccf245 | 839 | if (eof == 1) { |
62e76326 | 840 | fwdComplete(fwd); |
841 | comm_close(fd); | |
e6ccf245 | 842 | } |
f5558c95 | 843 | } |
844 | ||
528b2c61 | 845 | HttpStateData::ConnectionStatus |
846 | HttpStateData::statusIfComplete() const | |
603a02fd | 847 | { |
528b2c61 | 848 | HttpReply const *reply = entry->getReply(); |
849 | /* If the reply wants to close the connection, it takes precedence */ | |
62e76326 | 850 | |
528b2c61 | 851 | if (httpHeaderHasConnDir(&reply->header, "close")) |
62e76326 | 852 | return COMPLETE_NONPERSISTENT_MSG; |
853 | ||
528b2c61 | 854 | /* If we didn't send a keep-alive request header, then this |
978e455f | 855 | * can not be a persistent connection. |
856 | */ | |
528b2c61 | 857 | if (!flags.keepalive) |
62e76326 | 858 | return COMPLETE_NONPERSISTENT_MSG; |
859 | ||
72b63f06 | 860 | /* |
861 | * If we haven't sent the whole request then this can not be a persistent | |
862 | * connection. | |
863 | */ | |
864 | if (!flags.request_sent) { | |
865 | debug(11, 1) ("httpReadReply: Request not yet fully sent \"%s %s\"\n", | |
866 | RequestMethodStr[orig_request->method], | |
867 | storeUrl(entry)); | |
868 | return COMPLETE_NONPERSISTENT_MSG; | |
869 | } | |
870 | ||
9f5a2895 | 871 | /* |
872 | * What does the reply have to say about keep-alive? | |
873 | */ | |
b6a2f15e | 874 | /* |
875 | * XXX BUG? | |
876 | * If the origin server (HTTP/1.0) does not send a keep-alive | |
877 | * header, but keeps the connection open anyway, what happens? | |
878 | * We'll return here and http.c waits for an EOF before changing | |
879 | * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT | |
880 | * and an error status code, and we might have to wait until | |
881 | * the server times out the socket. | |
882 | */ | |
9f5a2895 | 883 | if (!reply->keep_alive) |
528b2c61 | 884 | return COMPLETE_NONPERSISTENT_MSG; |
62e76326 | 885 | |
528b2c61 | 886 | return COMPLETE_PERSISTENT_MSG; |
887 | } | |
888 | ||
889 | HttpStateData::ConnectionStatus | |
890 | HttpStateData::persistentConnStatus() const | |
891 | { | |
892 | HttpReply const *reply = entry->getReply(); | |
893 | int clen; | |
894 | debug(11, 3) ("httpPconnTransferDone: FD %d\n", fd); | |
895 | ConnectionStatus result = statusIfComplete(); | |
51fdcbd5 | 896 | debug(11, 5) ("httpPconnTransferDone: content_length=%d\n", |
62e76326 | 897 | reply->content_length); |
35282fbf | 898 | /* If we haven't seen the end of reply headers, we are not done */ |
62e76326 | 899 | |
528b2c61 | 900 | if (reply_hdr_state < 2) |
62e76326 | 901 | return INCOMPLETE_MSG; |
902 | ||
528b2c61 | 903 | clen = httpReplyBodySize(request->method, reply); |
62e76326 | 904 | |
35282fbf | 905 | /* If there is no message body, we can be persistent */ |
906 | if (0 == clen) | |
62e76326 | 907 | return result; |
908 | ||
35282fbf | 909 | /* If the body size is unknown we must wait for EOF */ |
910 | if (clen < 0) | |
62e76326 | 911 | return INCOMPLETE_MSG; |
912 | ||
35282fbf | 913 | /* If the body size is known, we must wait until we've gotten all of it. */ |
528b2c61 | 914 | if (entry->mem_obj->endOffset() < reply->content_length + reply->hdr_sz) |
62e76326 | 915 | return INCOMPLETE_MSG; |
916 | ||
35282fbf | 917 | /* We got it all */ |
528b2c61 | 918 | return result; |
603a02fd | 919 | } |
090089c4 | 920 | |
921 | /* This will be called when data is ready to be read from fd. Read until | |
922 | * error or connection closed. */ | |
f5558c95 | 923 | /* XXX this function is too long! */ |
b8d8561b | 924 | static void |
c4b7a5a9 | 925 | httpReadReply(int fd, char *buf, size_t len, comm_err_t flag, int xerrno,void *data) |
926 | { | |
927 | HttpStateData *httpState = static_cast<HttpStateData *>(data); | |
7194987f | 928 | assert (fd == httpState->fd); |
1d5161bd | 929 | PROF_start(HttpStateData_readReply); |
c4b7a5a9 | 930 | httpState->readReply (fd, buf, len, flag, xerrno, data); |
1d5161bd | 931 | PROF_stop(HttpStateData_readReply); |
c4b7a5a9 | 932 | } |
933 | ||
934 | void | |
528b2c61 | 935 | HttpStateData::readReply (int fd, char *readBuf, size_t len, comm_err_t flag, int xerrno,void *data) |
090089c4 | 936 | { |
30a4f2a8 | 937 | int bin; |
090089c4 | 938 | int clen; |
c4b7a5a9 | 939 | do_next_read = 0; |
c4b7a5a9 | 940 | |
941 | ||
528b2c61 | 942 | assert(buf == readBuf); |
c4b7a5a9 | 943 | |
944 | /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us | |
62e76326 | 945 | */ |
946 | ||
c4b7a5a9 | 947 | if (flag == COMM_ERR_CLOSING) { |
d09176e1 | 948 | debug (11,3)("http socket closing\n"); |
c4b7a5a9 | 949 | return; |
950 | } | |
951 | ||
e92e4e44 | 952 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
62e76326 | 953 | maybeReadData(); |
954 | return; | |
e92e4e44 | 955 | } |
c4b7a5a9 | 956 | |
1513873c | 957 | errno = 0; |
c4b7a5a9 | 958 | /* prepare the read size for the next read (if any) */ |
447e176b | 959 | #if DELAY_POOLS |
62e76326 | 960 | |
a46d2c0e | 961 | DelayId delayId = entry->mem_obj->mostBytesAllowed(); |
62e76326 | 962 | |
447e176b | 963 | #endif |
62e76326 | 964 | |
c4b7a5a9 | 965 | debug(11, 5) ("httpReadReply: FD %d: len %d.\n", fd, (int)len); |
62e76326 | 966 | |
c4b7a5a9 | 967 | if (flag == COMM_OK && len > 0) { |
447e176b | 968 | #if DELAY_POOLS |
62e76326 | 969 | delayId.bytesIn(len); |
447e176b | 970 | #endif |
62e76326 | 971 | |
972 | kb_incr(&statCounter.server.all.kbytes_in, len); | |
973 | kb_incr(&statCounter.server.http.kbytes_in, len); | |
62e76326 | 974 | IOStats.Http.reads++; |
975 | ||
976 | for (clen = len - 1, bin = 0; clen; bin++) | |
977 | clen >>= 1; | |
978 | ||
979 | IOStats.Http.read_hist[bin]++; | |
30a4f2a8 | 980 | } |
62e76326 | 981 | |
21b92762 | 982 | if (!memBufIsNull(&reply_hdr) && flag == COMM_OK && len > 0 && fd_table[fd].uses > 1) { |
62e76326 | 983 | /* Skip whitespace */ |
984 | ||
985 | while (len > 0 && xisspace(*buf)) | |
986 | xmemmove(buf, buf + 1, len--); | |
987 | ||
988 | if (len == 0) { | |
989 | /* Continue to read... */ | |
21b92762 | 990 | /* Timeout NOT increased. This whitespace was from previous reply */ |
62e76326 | 991 | do_next_read = 1; |
992 | maybeReadData(); | |
993 | return; | |
994 | } | |
5ede6c8f | 995 | } |
62e76326 | 996 | |
c4b7a5a9 | 997 | if (flag != COMM_OK || len < 0) { |
62e76326 | 998 | debug(50, 2) ("httpReadReply: FD %d: read failure: %s.\n", |
999 | fd, xstrerror()); | |
1000 | ||
1001 | if (ignoreErrno(errno)) { | |
1002 | do_next_read = 1; | |
1003 | } else if (entry->isEmpty()) { | |
1004 | ErrorState *err; | |
4eb368f9 | 1005 | err = errorCon(ERR_READ_ERROR, HTTP_BAD_GATEWAY); |
190154cf | 1006 | err->request = requestLink((HttpRequest *) request); |
62e76326 | 1007 | err->xerrno = errno; |
1008 | fwdFail(fwd, err); | |
1009 | do_next_read = 0; | |
1010 | comm_close(fd); | |
1011 | } else { | |
1012 | do_next_read = 0; | |
1013 | comm_close(fd); | |
1014 | } | |
528b2c61 | 1015 | } else if (flag == COMM_OK && len == 0 && entry->isEmpty()) { |
62e76326 | 1016 | ErrorState *err; |
4eb368f9 | 1017 | err = errorCon(ERR_ZERO_SIZE_OBJECT, HTTP_BAD_GATEWAY); |
62e76326 | 1018 | err->xerrno = errno; |
190154cf | 1019 | err->request = requestLink((HttpRequest *) request); |
62e76326 | 1020 | fwdFail(fwd, err); |
1021 | eof = 1; | |
1022 | do_next_read = 0; | |
1023 | comm_close(fd); | |
c4b7a5a9 | 1024 | } else if (flag == COMM_OK && len == 0) { |
62e76326 | 1025 | /* Connection closed; retrieval done. */ |
1026 | eof = 1; | |
1027 | ||
1028 | if (reply_hdr_state < 2) | |
1029 | /* | |
1030 | * Yes Henrik, there is a point to doing this. When we | |
1031 | * called httpProcessReplyHeader() before, we didn't find | |
1032 | * the end of headers, but now we are definately at EOF, so | |
1033 | * we want to process the reply headers. | |
1034 | */ | |
1035 | /* doesn't return */ | |
1036 | processReplyHeader(buf, len); | |
b735b90c | 1037 | else if (entry->getReply()->sline.status == HTTP_INVALID_HEADER && HttpVersion(0,9) != entry->getReply()->sline.version) { |
47ac2ebe | 1038 | ErrorState *err; |
52d3f198 | 1039 | err = errorCon(ERR_INVALID_RESP, HTTP_BAD_GATEWAY); |
47ac2ebe | 1040 | err->request = requestLink((HttpRequest *) request); |
1041 | fwdFail(fwd, err); | |
1042 | do_next_read = 0; | |
1043 | } else { | |
4eb368f9 | 1044 | if (entry->mem_obj->getReply()->sline.status == HTTP_HEADER_TOO_LARGE) { |
1045 | ErrorState *err; | |
1046 | storeEntryReset(entry); | |
1047 | err = errorCon(ERR_TOO_BIG, HTTP_BAD_GATEWAY); | |
1048 | err->request = requestLink((HttpRequest *) request); | |
1049 | fwdFail(fwd, err); | |
1050 | fwd->flags.dont_retry = 1; | |
1051 | } else { | |
1052 | fwdComplete(fwd); | |
1053 | } | |
1054 | ||
62e76326 | 1055 | do_next_read = 0; |
1056 | comm_close(fd); | |
1057 | } | |
090089c4 | 1058 | } else { |
62e76326 | 1059 | if (reply_hdr_state < 2) { |
1060 | processReplyHeader(buf, len); | |
1061 | ||
1062 | if (reply_hdr_state == 2) { | |
1063 | http_status s = entry->getReply()->sline.status; | |
47ac2ebe | 1064 | HttpVersion httpver = entry->getReply()->sline.version; |
1065 | ||
b735b90c | 1066 | if (s == HTTP_INVALID_HEADER && httpver != HttpVersion(0,9)) { |
47ac2ebe | 1067 | ErrorState *err; |
1068 | storeEntryReset(entry); | |
52d3f198 | 1069 | err = errorCon(ERR_INVALID_RESP, HTTP_BAD_GATEWAY); |
47ac2ebe | 1070 | err->request = requestLink((HttpRequest *) request); |
1071 | fwdFail(fwd, err); | |
1072 | comm_close(fd); | |
1073 | return; | |
1074 | } | |
1075 | ||
225644d7 | 1076 | #if WIP_FWD_LOG |
62e76326 | 1077 | |
1078 | fwdStatus(fwd, s); | |
47ac2ebe | 1079 | |
225644d7 | 1080 | #endif |
62e76326 | 1081 | /* |
1082 | * If its not a reply that we will re-forward, then | |
1083 | * allow the client to get it. | |
1084 | */ | |
1085 | ||
1086 | if (!fwdReforwardableStatus(s)) | |
1087 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); | |
1088 | } | |
1089 | } | |
1090 | ||
1d5161bd | 1091 | PROF_start(HttpStateData_processReplyData); |
62e76326 | 1092 | processReplyData(buf, len); |
1d5161bd | 1093 | PROF_stop(HttpStateData_processReplyData); |
e6ccf245 | 1094 | } |
1095 | } | |
1096 | ||
1097 | void | |
528b2c61 | 1098 | HttpStateData::processReplyData(const char *buf, size_t len) |
e6ccf245 | 1099 | { |
528b2c61 | 1100 | if (reply_hdr_state < 2) { |
62e76326 | 1101 | do_next_read = 1; |
1102 | maybeReadData(); | |
1103 | return; | |
528b2c61 | 1104 | } |
62e76326 | 1105 | |
528b2c61 | 1106 | StoreIOBuffer tempBuffer; |
62e76326 | 1107 | |
1108 | if (!flags.headers_pushed) { | |
1109 | /* The first block needs us to skip the headers */ | |
1110 | /* TODO: make this cleaner. WE should push the headers, NOT the parser */ | |
1111 | size_t end = headersEnd (buf, len); | |
1112 | /* IF len > end, we need to append data after the | |
1113 | * out of band update to the store | |
1114 | */ | |
1115 | ||
1116 | if (len > end) { | |
1117 | tempBuffer.data = (char *)buf+end; | |
1118 | tempBuffer.length = len - end; | |
1119 | tempBuffer.offset = currentOffset; | |
1120 | currentOffset += tempBuffer.length; | |
1121 | entry->write (tempBuffer); | |
1122 | } | |
1123 | ||
1124 | flags.headers_pushed = 1; | |
1125 | } else { | |
1126 | tempBuffer.data = (char *)buf; | |
1127 | tempBuffer.length = len; | |
1128 | tempBuffer.offset = currentOffset; | |
1129 | currentOffset += len; | |
1130 | entry->write(tempBuffer); | |
1131 | } | |
528b2c61 | 1132 | |
e6ccf245 | 1133 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
62e76326 | 1134 | /* |
1135 | * the above storeAppend() call could ABORT this entry, | |
1136 | * in that case, the server FD should already be closed. | |
1137 | * there's nothing for us to do. | |
1138 | */ | |
1139 | (void) 0; | |
1140 | } else | |
1141 | switch (persistentConnStatus()) { | |
1142 | ||
1143 | case INCOMPLETE_MSG: | |
21b92762 | 1144 | /* Wait for more data or EOF condition */ |
1145 | ||
1146 | if (flags.keepalive_broken) { | |
1147 | commSetTimeout(fd, 10, NULL, NULL); | |
1148 | } else { | |
1149 | commSetTimeout(fd, Config.Timeout.read, NULL, NULL); | |
1150 | } | |
1151 | ||
62e76326 | 1152 | do_next_read = 1; |
1153 | break; | |
1154 | ||
1155 | case COMPLETE_PERSISTENT_MSG: | |
1156 | /* yes we have to clear all these! */ | |
62e76326 | 1157 | commSetTimeout(fd, -1, NULL, NULL); |
1158 | do_next_read = 0; | |
62e76326 | 1159 | |
1160 | comm_remove_close_handler(fd, httpStateFree, this); | |
1161 | fwdUnregister(fd, fwd); | |
bd0723ad | 1162 | |
1163 | if (_peer) { | |
1164 | if (_peer->options.originserver) | |
1165 | pconnPush(fd, _peer->name, orig_request->port, orig_request->host); | |
1166 | else | |
1167 | pconnPush(fd, _peer->name, _peer->http_port, NULL); | |
1168 | } else { | |
1169 | pconnPush(fd, request->host, request->port, NULL); | |
1170 | } | |
1171 | ||
62e76326 | 1172 | fwdComplete(fwd); |
1173 | fd = -1; | |
1174 | httpStateFree(fd, this); | |
1175 | return; | |
1176 | ||
1177 | case COMPLETE_NONPERSISTENT_MSG: | |
1178 | /* close the connection ourselves */ | |
1179 | /* yes - same as for a complete persistent conn here */ | |
62e76326 | 1180 | commSetTimeout(fd, -1, NULL, NULL); |
1181 | commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); | |
62e76326 | 1182 | comm_remove_close_handler(fd, httpStateFree, this); |
1183 | fwdUnregister(fd, fwd); | |
1184 | fwdComplete(fwd); | |
1185 | /* TODO: check that fd is still open here */ | |
1186 | comm_close (fd); | |
1187 | fd = -1; | |
1188 | httpStateFree(fd, this); | |
1189 | return; | |
1190 | } | |
1191 | ||
c4b7a5a9 | 1192 | maybeReadData(); |
1193 | } | |
1194 | ||
1195 | void | |
1196 | HttpStateData::maybeReadData() | |
1197 | { | |
528b2c61 | 1198 | if (do_next_read) { |
62e76326 | 1199 | do_next_read = 0; |
a46d2c0e | 1200 | entry->delayAwareRead(fd, buf, SQUID_TCP_SO_RCVBUF, httpReadReply, this); |
528b2c61 | 1201 | } |
090089c4 | 1202 | } |
1203 | ||
1204 | /* This will be called when request write is complete. Schedule read of | |
1205 | * reply. */ | |
d576a6a6 | 1206 | void |
1207 | HttpStateData::SendComplete(int fd, char *bufnotused, size_t size, comm_err_t errflag, void *data) | |
090089c4 | 1208 | { |
e6ccf245 | 1209 | HttpStateData *httpState = static_cast<HttpStateData *>(data); |
9b312a19 | 1210 | StoreEntry *entry = httpState->entry; |
1211 | ErrorState *err; | |
a3d5953d | 1212 | debug(11, 5) ("httpSendComplete: FD %d: size %d: errflag %d.\n", |
62e76326 | 1213 | fd, (int) size, errflag); |
bc87dc25 | 1214 | #if URL_CHECKSUM_DEBUG |
62e76326 | 1215 | |
528b2c61 | 1216 | entry->mem_obj->checkUrlChecksum(); |
bc87dc25 | 1217 | #endif |
62e76326 | 1218 | |
ee1679df | 1219 | if (size > 0) { |
62e76326 | 1220 | fd_bytes(fd, size, FD_WRITE); |
1221 | kb_incr(&statCounter.server.all.kbytes_out, size); | |
1222 | kb_incr(&statCounter.server.http.kbytes_out, size); | |
ee1679df | 1223 | } |
62e76326 | 1224 | |
ea3a2a69 | 1225 | if (errflag == COMM_ERR_CLOSING) |
62e76326 | 1226 | return; |
1227 | ||
090089c4 | 1228 | if (errflag) { |
4eb368f9 | 1229 | err = errorCon(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY); |
62e76326 | 1230 | err->xerrno = errno; |
1231 | err->request = requestLink(httpState->orig_request); | |
1232 | errorAppendEntry(entry, err); | |
1233 | comm_close(fd); | |
1234 | return; | |
090089c4 | 1235 | } else { |
62e76326 | 1236 | /* |
1237 | * Set the read timeout here because it hasn't been set yet. | |
1238 | * We only set the read timeout after the request has been | |
1239 | * fully written to the server-side. If we start the timeout | |
1240 | * after connection establishment, then we are likely to hit | |
1241 | * the timeout for POST/PUT requests that have very large | |
1242 | * request bodies. | |
1243 | */ | |
1244 | commSetTimeout(fd, Config.Timeout.read, httpTimeout, httpState); | |
090089c4 | 1245 | } |
72b63f06 | 1246 | |
1247 | httpState->flags.request_sent = 1; | |
090089c4 | 1248 | } |
1249 | ||
99edd1c3 | 1250 | /* |
1251 | * build request headers and append them to a given MemBuf | |
1252 | * used by httpBuildRequestPrefix() | |
818c6c9e | 1253 | * note: initialised the HttpHeader, the caller is responsible for Clean()-ing |
99edd1c3 | 1254 | */ |
e1e72f06 | 1255 | void |
190154cf | 1256 | httpBuildRequestHeader(HttpRequest * request, |
1257 | HttpRequest * orig_request, | |
62e76326 | 1258 | StoreEntry * entry, |
1259 | HttpHeader * hdr_out, | |
1260 | http_state_flags flags) | |
6bf8443a | 1261 | { |
99edd1c3 | 1262 | /* building buffer for complex strings */ |
5999b776 | 1263 | #define BBUF_SZ (MAX_URL+32) |
99edd1c3 | 1264 | LOCAL_ARRAY(char, bbuf, BBUF_SZ); |
99edd1c3 | 1265 | const HttpHeader *hdr_in = &orig_request->header; |
1266 | const HttpHeaderEntry *e; | |
6bccf575 | 1267 | String strFwd; |
99edd1c3 | 1268 | HttpHeaderPos pos = HttpHeaderInitPos; |
75faaa7a | 1269 | assert (hdr_out->owner == hoRequest); |
99edd1c3 | 1270 | /* append our IMS header */ |
62e76326 | 1271 | |
9bc73deb | 1272 | if (request->lastmod > -1 && request->method == METHOD_GET) |
62e76326 | 1273 | httpHeaderPutTime(hdr_out, HDR_IF_MODIFIED_SINCE, request->lastmod); |
99edd1c3 | 1274 | |
528b2c61 | 1275 | bool we_do_ranges = decideIfWeDoRanges (orig_request); |
1276 | ||
650c4b88 | 1277 | String strConnection (httpHeaderGetList(hdr_in, HDR_CONNECTION)); |
62e76326 | 1278 | |
528b2c61 | 1279 | while ((e = httpHeaderGetEntry(hdr_in, &pos))) |
62e76326 | 1280 | copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, orig_request, hdr_out, we_do_ranges, flags); |
528b2c61 | 1281 | |
43ae1d95 | 1282 | /* Abstraction break: We should interpret multipart/byterange responses |
528b2c61 | 1283 | * into offset-length data, and this works around our inability to do so. |
1284 | */ | |
62e76326 | 1285 | if (!we_do_ranges && orig_request->multipartRangeRequest()) { |
1286 | /* don't cache the result */ | |
1287 | orig_request->flags.cachable = 0; | |
1288 | /* pretend it's not a range request */ | |
00d77d6b | 1289 | delete orig_request->range; |
62e76326 | 1290 | orig_request->range = NULL; |
1291 | orig_request->flags.range = 0; | |
1292 | } | |
528b2c61 | 1293 | |
99edd1c3 | 1294 | |
99edd1c3 | 1295 | /* append Via */ |
736cb6aa | 1296 | if (Config.onoff.via) { |
43ae1d95 | 1297 | String strVia; |
1298 | strVia = httpHeaderGetList(hdr_in, HDR_VIA); | |
62e76326 | 1299 | snprintf(bbuf, BBUF_SZ, "%d.%d %s", |
1300 | orig_request->http_ver.major, | |
1301 | orig_request->http_ver.minor, ThisCache); | |
1302 | strListAdd(&strVia, bbuf, ','); | |
1303 | httpHeaderPutStr(hdr_out, HDR_VIA, strVia.buf()); | |
1304 | strVia.clean(); | |
736cb6aa | 1305 | } |
62e76326 | 1306 | |
43ae1d95 | 1307 | #if ESI |
1308 | { | |
1309 | /* Append Surrogate-Capabilities */ | |
1310 | String strSurrogate (httpHeaderGetList(hdr_in, HDR_SURROGATE_CAPABILITY)); | |
ec43ae0e | 1311 | snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", |
43ae1d95 | 1312 | Config.Accel.surrogate_id); |
1313 | strListAdd(&strSurrogate, bbuf, ','); | |
1314 | httpHeaderPutStr(hdr_out, HDR_SURROGATE_CAPABILITY, strSurrogate.buf()); | |
1315 | } | |
1316 | #endif | |
1317 | ||
99edd1c3 | 1318 | /* append X-Forwarded-For */ |
6bccf575 | 1319 | strFwd = httpHeaderGetList(hdr_in, HDR_X_FORWARDED_FOR); |
62e76326 | 1320 | |
6056ae68 | 1321 | if (opt_forwarded_for && orig_request->client_addr.s_addr != no_addr.s_addr) |
62e76326 | 1322 | strListAdd(&strFwd, inet_ntoa(orig_request->client_addr), ','); |
6056ae68 | 1323 | else |
62e76326 | 1324 | strListAdd(&strFwd, "unknown", ','); |
1325 | ||
528b2c61 | 1326 | httpHeaderPutStr(hdr_out, HDR_X_FORWARDED_FOR, strFwd.buf()); |
62e76326 | 1327 | |
528b2c61 | 1328 | strFwd.clean(); |
6bccf575 | 1329 | |
99edd1c3 | 1330 | /* append Host if not there already */ |
1331 | if (!httpHeaderHas(hdr_out, HDR_HOST)) { | |
62e76326 | 1332 | if (orig_request->peer_domain) { |
1333 | httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->peer_domain); | |
1334 | } else if (orig_request->port == urlDefaultPort(orig_request->protocol)) { | |
1335 | /* use port# only if not default */ | |
1336 | httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->host); | |
1337 | } else { | |
1338 | httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d", | |
1339 | orig_request->host, (int) orig_request->port); | |
1340 | } | |
6bf8443a | 1341 | } |
62e76326 | 1342 | |
c68e9c6b | 1343 | /* append Authorization if known in URL, not in header and going direct */ |
1344 | if (!httpHeaderHas(hdr_out, HDR_AUTHORIZATION)) { | |
62e76326 | 1345 | if (!request->flags.proxying && *request->login) { |
1346 | httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s", | |
1347 | base64_encode(request->login)); | |
1348 | } | |
c68e9c6b | 1349 | } |
62e76326 | 1350 | |
c68e9c6b | 1351 | /* append Proxy-Authorization if configured for peer, and proxying */ |
c3b33cb7 | 1352 | if (request->flags.proxying && orig_request->peer_login && |
62e76326 | 1353 | !httpHeaderHas(hdr_out, HDR_PROXY_AUTHORIZATION)) { |
1354 | if (*orig_request->peer_login == '*') { | |
1355 | /* Special mode, to pass the username to the upstream cache */ | |
1356 | char loginbuf[256]; | |
1357 | const char *username = "-"; | |
1358 | ||
1359 | if (orig_request->auth_user_request) | |
1360 | username = orig_request->auth_user_request->username(); | |
abb929f0 | 1361 | else if (orig_request->extacl_user.size()) |
1362 | username = orig_request->extacl_user.buf(); | |
62e76326 | 1363 | |
1364 | snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, orig_request->peer_login + 1); | |
1365 | ||
1366 | httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s", | |
1367 | base64_encode(loginbuf)); | |
1368 | } else if (strcmp(orig_request->peer_login, "PASS") == 0) { | |
abb929f0 | 1369 | if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size()) { |
1370 | char loginbuf[256]; | |
1371 | snprintf(loginbuf, sizeof(loginbuf), "%s:%s", orig_request->extacl_user.buf(), orig_request->extacl_passwd.buf()); | |
1372 | httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s", | |
1373 | base64_encode(loginbuf)); | |
1374 | } | |
62e76326 | 1375 | } else if (strcmp(orig_request->peer_login, "PROXYPASS") == 0) { |
1376 | /* Nothing to do */ | |
1377 | } else { | |
1378 | httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s", | |
1379 | base64_encode(orig_request->peer_login)); | |
1380 | } | |
c68e9c6b | 1381 | } |
62e76326 | 1382 | |
be753325 | 1383 | /* append WWW-Authorization if configured for peer */ |
1384 | if (flags.originpeer && orig_request->peer_login && | |
62e76326 | 1385 | !httpHeaderHas(hdr_out, HDR_AUTHORIZATION)) { |
1386 | if (strcmp(orig_request->peer_login, "PASS") == 0) { | |
1387 | /* No credentials to forward.. (should have been done above if available) */ | |
1388 | } else if (strcmp(orig_request->peer_login, "PROXYPASS") == 0) { | |
1389 | /* Special mode, convert proxy authentication to WWW authentication | |
abb929f0 | 1390 | * (also applies to authentication provided by external acl) |
62e76326 | 1391 | */ |
1392 | const char *auth = httpHeaderGetStr(hdr_in, HDR_PROXY_AUTHORIZATION); | |
1393 | ||
1394 | if (auth && strncasecmp(auth, "basic ", 6) == 0) { | |
1395 | httpHeaderPutStr(hdr_out, HDR_AUTHORIZATION, auth); | |
abb929f0 | 1396 | } else if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size()) { |
1397 | char loginbuf[256]; | |
1398 | snprintf(loginbuf, sizeof(loginbuf), "%s:%s", orig_request->extacl_user.buf(), orig_request->extacl_passwd.buf()); | |
1399 | httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s", | |
1400 | base64_encode(loginbuf)); | |
62e76326 | 1401 | } |
1402 | } else if (*orig_request->peer_login == '*') { | |
1403 | /* Special mode, to pass the username to the upstream cache */ | |
1404 | char loginbuf[256]; | |
1405 | const char *username = "-"; | |
1406 | ||
1407 | if (orig_request->auth_user_request) | |
f5691f9c | 1408 | username = orig_request->auth_user_request->username(); |
abb929f0 | 1409 | else if (orig_request->extacl_user.size()) |
1410 | username = orig_request->extacl_user.buf(); | |
62e76326 | 1411 | |
1412 | snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, orig_request->peer_login + 1); | |
1413 | ||
1414 | httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s", | |
1415 | base64_encode(loginbuf)); | |
1416 | } else { | |
1417 | /* Fixed login string */ | |
1418 | httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s", | |
1419 | base64_encode(orig_request->peer_login)); | |
1420 | } | |
be753325 | 1421 | } |
62e76326 | 1422 | |
abb929f0 | 1423 | /* append Cache-Control, add max-age if not there already */ { |
62e76326 | 1424 | HttpHdrCc *cc = httpHeaderGetCc(hdr_in); |
1425 | ||
1426 | if (!cc) | |
1427 | cc = httpHdrCcCreate(); | |
1428 | ||
1429 | if (!EBIT_TEST(cc->mask, CC_MAX_AGE)) { | |
43ae1d95 | 1430 | const char *url = |
1431 | entry ? storeUrl(entry) : urlCanonical(orig_request); | |
62e76326 | 1432 | httpHdrCcSetMaxAge(cc, getMaxAge(url)); |
1433 | ||
1434 | if (request->urlpath.size()) | |
1435 | assert(strstr(url, request->urlpath.buf())); | |
1436 | } | |
1437 | ||
ce2d6441 | 1438 | /* Set no-cache if determined needed but not found */ |
1439 | if (orig_request->flags.nocache && !httpHeaderHas(hdr_in, HDR_PRAGMA)) | |
1440 | EBIT_SET(cc->mask, CC_NO_CACHE); | |
1441 | ||
1442 | /* Enforce sibling relations */ | |
62e76326 | 1443 | if (flags.only_if_cached) |
1444 | EBIT_SET(cc->mask, CC_ONLY_IF_CACHED); | |
1445 | ||
1446 | httpHeaderPutCc(hdr_out, cc); | |
1447 | ||
1448 | httpHdrCcDestroy(cc); | |
6bf8443a | 1449 | } |
62e76326 | 1450 | |
99edd1c3 | 1451 | /* maybe append Connection: keep-alive */ |
b515fc11 | 1452 | if (flags.keepalive) { |
62e76326 | 1453 | if (flags.proxying) { |
1454 | httpHeaderPutStr(hdr_out, HDR_PROXY_CONNECTION, "keep-alive"); | |
1455 | } else { | |
1456 | httpHeaderPutStr(hdr_out, HDR_CONNECTION, "keep-alive"); | |
1457 | } | |
603a02fd | 1458 | } |
62e76326 | 1459 | |
a7ad6e4e | 1460 | /* append Front-End-Https */ |
1461 | if (flags.front_end_https) { | |
62e76326 | 1462 | if (flags.front_end_https == 1 || request->protocol == PROTO_HTTPS) |
1463 | httpHeaderPutStr(hdr_out, HDR_FRONT_END_HTTPS, "On"); | |
a7ad6e4e | 1464 | } |
1465 | ||
6bccf575 | 1466 | /* Now mangle the headers. */ |
8c01ada0 | 1467 | httpHdrMangleList(hdr_out, request, ROR_REQUEST); |
62e76326 | 1468 | |
528b2c61 | 1469 | strConnection.clean(); |
99edd1c3 | 1470 | } |
1471 | ||
528b2c61 | 1472 | |
1473 | void | |
190154cf | 1474 | copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, String strConnection, HttpRequest * request, HttpRequest * orig_request, HttpHeader * hdr_out, int we_do_ranges, http_state_flags flags) |
528b2c61 | 1475 | { |
1476 | debug(11, 5) ("httpBuildRequestHeader: %s: %s\n", | |
62e76326 | 1477 | e->name.buf(), e->value.buf()); |
1478 | ||
528b2c61 | 1479 | if (!httpRequestHdrAllowed(e, &strConnection)) { |
62e76326 | 1480 | debug(11, 2) ("'%s' header denied by anonymize_headers configuration\n",+ e->name.buf()); |
1481 | return; | |
528b2c61 | 1482 | } |
62e76326 | 1483 | |
528b2c61 | 1484 | switch (e->id) { |
62e76326 | 1485 | |
be753325 | 1486 | case HDR_PROXY_AUTHORIZATION: |
62e76326 | 1487 | /* Only pass on proxy authentication to peers for which |
1488 | * authentication forwarding is explicitly enabled | |
1489 | */ | |
1490 | ||
1491 | if (flags.proxying && orig_request->peer_login && | |
abb929f0 | 1492 | (strcmp(orig_request->peer_login, "PASS") == 0 || |
1493 | strcmp(orig_request->peer_login, "PROXYPASS") == 0)) { | |
62e76326 | 1494 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); |
1495 | } | |
1496 | ||
1497 | break; | |
1498 | ||
be753325 | 1499 | case HDR_AUTHORIZATION: |
62e76326 | 1500 | /* Pass on WWW authentication */ |
1501 | ||
1502 | if (!flags.originpeer) { | |
1503 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
1504 | } else { | |
1505 | /* In accelerators, only forward authentication if enabled | |
1506 | * (see also below for proxy->server authentication) | |
1507 | */ | |
1508 | ||
abb929f0 | 1509 | if (orig_request->peer_login && |
1510 | (strcmp(orig_request->peer_login, "PASS") == 0 || | |
1511 | strcmp(orig_request->peer_login, "PROXYPASS") == 0)) { | |
62e76326 | 1512 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); |
1513 | } | |
1514 | } | |
1515 | ||
1516 | break; | |
1517 | ||
be753325 | 1518 | case HDR_HOST: |
62e76326 | 1519 | /* |
b883b594 | 1520 | * Normally Squid rewrites the Host: header. |
1521 | * However, there is one case when we don't: If the URL | |
62e76326 | 1522 | * went through our redirector and the admin configured |
1523 | * 'redir_rewrites_host' to be off. | |
1524 | */ | |
1525 | ||
b883b594 | 1526 | if (request->flags.redirected && !Config.onoff.redir_rewrites_host) |
1527 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
1528 | else { | |
1529 | /* use port# only if not default */ | |
1530 | ||
1531 | if (orig_request->port == urlDefaultPort(orig_request->protocol)) { | |
1532 | httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->host); | |
1533 | } else { | |
1534 | httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d", | |
1535 | orig_request->host, (int) orig_request->port); | |
1536 | } | |
1537 | } | |
62e76326 | 1538 | |
1539 | break; | |
1540 | ||
be753325 | 1541 | case HDR_IF_MODIFIED_SINCE: |
62e76326 | 1542 | /* append unless we added our own; |
1543 | * note: at most one client's ims header can pass through */ | |
b883b594 | 1544 | |
62e76326 | 1545 | if (!httpHeaderHas(hdr_out, HDR_IF_MODIFIED_SINCE)) |
1546 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
1547 | ||
1548 | break; | |
1549 | ||
be753325 | 1550 | case HDR_MAX_FORWARDS: |
62e76326 | 1551 | if (orig_request->method == METHOD_TRACE) { |
1552 | const int hops = httpHeaderEntryGetInt(e); | |
1553 | ||
1554 | if (hops > 0) | |
1555 | httpHeaderPutInt(hdr_out, HDR_MAX_FORWARDS, hops - 1); | |
1556 | } | |
1557 | ||
1558 | break; | |
1559 | ||
be753325 | 1560 | case HDR_VIA: |
62e76326 | 1561 | /* If Via is disabled then forward any received header as-is */ |
1562 | ||
1563 | if (!Config.onoff.via) | |
1564 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
1565 | ||
1566 | break; | |
1567 | ||
be753325 | 1568 | case HDR_RANGE: |
62e76326 | 1569 | |
be753325 | 1570 | case HDR_IF_RANGE: |
62e76326 | 1571 | |
be753325 | 1572 | case HDR_REQUEST_RANGE: |
62e76326 | 1573 | if (!we_do_ranges) |
1574 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
1575 | ||
1576 | break; | |
1577 | ||
be753325 | 1578 | case HDR_PROXY_CONNECTION: |
62e76326 | 1579 | |
be753325 | 1580 | case HDR_CONNECTION: |
62e76326 | 1581 | |
be753325 | 1582 | case HDR_X_FORWARDED_FOR: |
62e76326 | 1583 | |
be753325 | 1584 | case HDR_CACHE_CONTROL: |
62e76326 | 1585 | /* append these after the loop if needed */ |
1586 | break; | |
1587 | ||
be753325 | 1588 | case HDR_FRONT_END_HTTPS: |
62e76326 | 1589 | if (!flags.front_end_https) |
1590 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
1591 | ||
1592 | break; | |
1593 | ||
be753325 | 1594 | default: |
62e76326 | 1595 | /* pass on all other header fields */ |
1596 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
528b2c61 | 1597 | } |
1598 | } | |
1599 | ||
1600 | int | |
190154cf | 1601 | decideIfWeDoRanges (HttpRequest * orig_request) |
528b2c61 | 1602 | { |
62e76326 | 1603 | int result = 1; |
1604 | /* decide if we want to do Ranges ourselves | |
1605 | * and fetch the whole object now) | |
1606 | * We want to handle Ranges ourselves iff | |
1607 | * - we can actually parse client Range specs | |
1608 | * - the specs are expected to be simple enough (e.g. no out-of-order ranges) | |
1609 | * - reply will be cachable | |
1610 | * (If the reply will be uncachable we have to throw it away after | |
1611 | * serving this request, so it is better to forward ranges to | |
1612 | * the server and fetch only the requested content) | |
1613 | */ | |
1614 | ||
1615 | if (NULL == orig_request->range || !orig_request->flags.cachable | |
1616 | || orig_request->range->offsetLimitExceeded()) | |
1617 | result = 0; | |
1618 | ||
1619 | debug(11, 8) ("decideIfWeDoRanges: range specs: %p, cachable: %d; we_do_ranges: %d\n", | |
1620 | orig_request->range, orig_request->flags.cachable, result); | |
1621 | ||
1622 | return result; | |
528b2c61 | 1623 | } |
1624 | ||
1625 | ||
62e76326 | 1626 | /* build request prefix and append it to a given MemBuf; |
99edd1c3 | 1627 | * return the length of the prefix */ |
9bc73deb | 1628 | mb_size_t |
190154cf | 1629 | httpBuildRequestPrefix(HttpRequest * request, |
1630 | HttpRequest * orig_request, | |
62e76326 | 1631 | StoreEntry * entry, |
1632 | MemBuf * mb, | |
1633 | http_state_flags flags) | |
99edd1c3 | 1634 | { |
1635 | const int offset = mb->size; | |
450e0c10 | 1636 | HttpVersion httpver(1, 0); |
528b2c61 | 1637 | memBufPrintf(mb, "%s %s HTTP/%d.%d\r\n", |
62e76326 | 1638 | RequestMethodStr[request->method], |
1639 | request->urlpath.size() ? request->urlpath.buf() : "/", | |
1640 | httpver.major,httpver.minor); | |
99edd1c3 | 1641 | /* build and pack headers */ |
1642 | { | |
75faaa7a | 1643 | HttpHeader hdr(hoRequest); |
62e76326 | 1644 | Packer p; |
1645 | httpBuildRequestHeader(request, orig_request, entry, &hdr, flags); | |
1646 | packerToMemInit(&p, mb); | |
1647 | httpHeaderPackInto(&hdr, &p); | |
1648 | httpHeaderClean(&hdr); | |
1649 | packerClean(&p); | |
9d9d144b | 1650 | } |
99edd1c3 | 1651 | /* append header terminator */ |
b8890359 | 1652 | memBufAppend(mb, crlf, 2); |
99edd1c3 | 1653 | return mb->size - offset; |
6bf8443a | 1654 | } |
62e76326 | 1655 | |
090089c4 | 1656 | /* This will be called when connect completes. Write request. */ |
b8d8561b | 1657 | static void |
b6a2f15e | 1658 | httpSendRequest(HttpStateData * httpState) |
090089c4 | 1659 | { |
99edd1c3 | 1660 | MemBuf mb; |
190154cf | 1661 | HttpRequest *req = httpState->request; |
620da955 | 1662 | StoreEntry *entry = httpState->entry; |
29b8d8d6 | 1663 | peer *p = httpState->_peer; |
901e234d | 1664 | CWCB *sendHeaderDone; |
21b92762 | 1665 | int fd = httpState->fd; |
090089c4 | 1666 | |
21b92762 | 1667 | debug(11, 5) ("httpSendRequest: FD %d: httpState %p.\n", fd, |
43ae1d95 | 1668 | httpState); |
090089c4 | 1669 | |
21b92762 | 1670 | /* Schedule read reply. */ |
1671 | commSetTimeout(fd, Config.Timeout.lifetime, httpTimeout, httpState); | |
1672 | entry->delayAwareRead(fd, httpState->buf, SQUID_TCP_SO_RCVBUF, httpReadReply, httpState); | |
1673 | ||
a2ac85d9 | 1674 | if (httpState->orig_request->body_connection.getRaw() != NULL) |
62e76326 | 1675 | sendHeaderDone = httpSendRequestEntity; |
7db8b16d | 1676 | else |
62e76326 | 1677 | sendHeaderDone = HttpStateData::SendComplete; |
54220df8 | 1678 | |
be753325 | 1679 | if (p != NULL) { |
62e76326 | 1680 | if (p->options.originserver) { |
1681 | httpState->flags.proxying = 0; | |
1682 | httpState->flags.originpeer = 1; | |
1683 | } else { | |
1684 | httpState->flags.proxying = 1; | |
1685 | httpState->flags.originpeer = 0; | |
1686 | } | |
be753325 | 1687 | } else { |
62e76326 | 1688 | httpState->flags.proxying = 0; |
1689 | httpState->flags.originpeer = 0; | |
be753325 | 1690 | } |
62e76326 | 1691 | |
efb9218c | 1692 | /* |
99edd1c3 | 1693 | * Is keep-alive okay for all request methods? |
efb9218c | 1694 | */ |
efd900cb | 1695 | if (!Config.onoff.server_pconns) |
62e76326 | 1696 | httpState->flags.keepalive = 0; |
efd900cb | 1697 | else if (p == NULL) |
62e76326 | 1698 | httpState->flags.keepalive = 1; |
efb9218c | 1699 | else if (p->stats.n_keepalives_sent < 10) |
62e76326 | 1700 | httpState->flags.keepalive = 1; |
43ae1d95 | 1701 | else if ((double) p->stats.n_keepalives_recv / |
1702 | (double) p->stats.n_keepalives_sent > 0.50) | |
62e76326 | 1703 | httpState->flags.keepalive = 1; |
1704 | ||
a7ad6e4e | 1705 | if (httpState->_peer) { |
62e76326 | 1706 | if (neighborType(httpState->_peer, httpState->request) == PEER_SIBLING && |
1707 | !httpState->_peer->options.allow_miss) | |
1708 | httpState->flags.only_if_cached = 1; | |
1709 | ||
1710 | httpState->flags.front_end_https = httpState->_peer->front_end_https; | |
a7ad6e4e | 1711 | } |
62e76326 | 1712 | |
99edd1c3 | 1713 | memBufDefInit(&mb); |
1714 | httpBuildRequestPrefix(req, | |
62e76326 | 1715 | httpState->orig_request, |
1716 | entry, | |
1717 | &mb, | |
1718 | httpState->flags); | |
21b92762 | 1719 | debug(11, 6) ("httpSendRequest: FD %d:\n%s\n", fd, mb.buf); |
1720 | comm_old_write_mbuf(fd, mb, sendHeaderDone, httpState); | |
090089c4 | 1721 | } |
b6a2f15e | 1722 | |
910169e5 | 1723 | void |
db1cd23c | 1724 | httpStart(FwdState * fwd) |
603a02fd | 1725 | { |
db1cd23c | 1726 | int fd = fwd->server_fd; |
28c60158 | 1727 | HttpStateData *httpState; |
190154cf | 1728 | HttpRequest *proxy_req; |
1729 | HttpRequest *orig_req = fwd->request; | |
910169e5 | 1730 | debug(11, 3) ("httpStart: \"%s %s\"\n", |
62e76326 | 1731 | RequestMethodStr[orig_req->method], |
1732 | storeUrl(fwd->entry)); | |
e6ccf245 | 1733 | CBDATA_INIT_TYPE(HttpStateData); |
72711e31 | 1734 | httpState = cbdataAlloc(HttpStateData); |
43ae1d95 | 1735 | httpState->ignoreCacheControl = false; |
1736 | httpState->surrogateNoStore = false; | |
db1cd23c | 1737 | storeLockObject(fwd->entry); |
1738 | httpState->fwd = fwd; | |
1739 | httpState->entry = fwd->entry; | |
9e4ad609 | 1740 | httpState->fd = fd; |
62e76326 | 1741 | |
db1cd23c | 1742 | if (fwd->servers) |
62e76326 | 1743 | httpState->_peer = fwd->servers->_peer; /* might be NULL */ |
1744 | ||
29b8d8d6 | 1745 | if (httpState->_peer) { |
62e76326 | 1746 | const char *url; |
1747 | ||
1748 | if (httpState->_peer->options.originserver) | |
1749 | url = orig_req->urlpath.buf(); | |
1750 | else | |
1751 | url = storeUrl(httpState->entry); | |
1752 | ||
1753 | proxy_req = requestCreate(orig_req->method, | |
1754 | orig_req->protocol, url); | |
1755 | ||
1756 | xstrncpy(proxy_req->host, httpState->_peer->host, SQUIDHOSTNAMELEN); | |
1757 | ||
1758 | proxy_req->port = httpState->_peer->http_port; | |
1759 | ||
1760 | proxy_req->flags = orig_req->flags; | |
1761 | ||
1762 | proxy_req->lastmod = orig_req->lastmod; | |
1763 | ||
1764 | httpState->request = requestLink(proxy_req); | |
1765 | ||
1766 | httpState->orig_request = requestLink(orig_req); | |
1767 | ||
1768 | proxy_req->flags.proxying = 1; | |
1769 | ||
1770 | /* | |
1771 | * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here. | |
1772 | * We might end up getting the object from somewhere else if, | |
1773 | * for example, the request to this neighbor fails. | |
1774 | */ | |
1775 | if (httpState->_peer->options.proxy_only) | |
1776 | storeReleaseRequest(httpState->entry); | |
1777 | ||
95e36d02 | 1778 | #if DELAY_POOLS |
62e76326 | 1779 | |
a46d2c0e | 1780 | httpState->entry->setNoDelay(httpState->_peer->options.no_delay); |
62e76326 | 1781 | |
95e36d02 | 1782 | #endif |
62e76326 | 1783 | |
603a02fd | 1784 | } else { |
62e76326 | 1785 | httpState->request = requestLink(orig_req); |
1786 | httpState->orig_request = requestLink(orig_req); | |
603a02fd | 1787 | } |
62e76326 | 1788 | |
910169e5 | 1789 | /* |
1790 | * register the handler to free HTTP state data when the FD closes | |
1791 | */ | |
1792 | comm_add_close_handler(fd, httpStateFree, httpState); | |
62e76326 | 1793 | |
83704487 | 1794 | statCounter.server.all.requests++; |
62e76326 | 1795 | |
83704487 | 1796 | statCounter.server.http.requests++; |
62e76326 | 1797 | |
b6a2f15e | 1798 | httpSendRequest(httpState); |
62e76326 | 1799 | |
b6a2f15e | 1800 | /* |
1801 | * We used to set the read timeout here, but not any more. | |
1802 | * Now its set in httpSendComplete() after the full request, | |
1803 | * including request body, has been written to the server. | |
1804 | */ | |
090089c4 | 1805 | } |
1806 | ||
54220df8 | 1807 | static void |
c3d63b2a | 1808 | httpSendRequestEntityDone(int fd, void *data) |
54220df8 | 1809 | { |
e6ccf245 | 1810 | HttpStateData *httpState = static_cast<HttpStateData *>(data); |
4fb35c3c | 1811 | ACLChecklist ch; |
43ae1d95 | 1812 | debug(11, 5) ("httpSendRequestEntityDone: FD %d\n", fd); |
8000a965 | 1813 | ch.request = requestLink(httpState->request); |
b448c119 | 1814 | ch.accessList = Config.accessList.brokenPosts; |
62e76326 | 1815 | |
94439e4e | 1816 | if (!Config.accessList.brokenPosts) { |
62e76326 | 1817 | debug(11, 5) ("httpSendRequestEntityDone: No brokenPosts list\n"); |
1818 | HttpStateData::SendComplete(fd, NULL, 0, COMM_OK, data); | |
b448c119 | 1819 | } else if (!ch.fastCheck()) { |
62e76326 | 1820 | debug(11, 5) ("httpSendRequestEntityDone: didn't match brokenPosts\n"); |
1821 | HttpStateData::SendComplete(fd, NULL, 0, COMM_OK, data); | |
94439e4e | 1822 | } else { |
62e76326 | 1823 | debug(11, 2) ("httpSendRequestEntityDone: matched brokenPosts\n"); |
1824 | comm_old_write(fd, "\r\n", 2, HttpStateData::SendComplete, data, NULL); | |
54220df8 | 1825 | } |
b448c119 | 1826 | |
1827 | ch.accessList = NULL; | |
94439e4e | 1828 | } |
1829 | ||
1830 | static void | |
e6ccf245 | 1831 | httpRequestBodyHandler(char *buf, ssize_t size, void *data) |
94439e4e | 1832 | { |
1833 | HttpStateData *httpState = (HttpStateData *) data; | |
21b92762 | 1834 | httpState->body_buf = NULL; |
62e76326 | 1835 | |
94439e4e | 1836 | if (size > 0) { |
21b92762 | 1837 | if (httpState->reply_hdr_state >= 2 && !httpState->flags.abuse_detected) { |
1838 | httpState->flags.abuse_detected = 1; | |
1839 | debug(11, 1) ("httpSendRequestEntryDone: Likely proxy abuse detected '%s' -> '%s'\n", | |
1840 | inet_ntoa(httpState->orig_request->client_addr), | |
1841 | storeUrl(httpState->entry)); | |
1842 | ||
1843 | if (httpState->entry->getReply()->sline.status == HTTP_INVALID_HEADER) { | |
1844 | memFree8K(buf); | |
1845 | comm_close(httpState->fd); | |
1846 | return; | |
1847 | } | |
1848 | } | |
1849 | ||
62e76326 | 1850 | comm_old_write(httpState->fd, buf, size, httpSendRequestEntity, data, memFree8K); |
94439e4e | 1851 | } else if (size == 0) { |
62e76326 | 1852 | /* End of body */ |
1853 | memFree8K(buf); | |
1854 | httpSendRequestEntityDone(httpState->fd, data); | |
94439e4e | 1855 | } else { |
62e76326 | 1856 | /* Failed to get whole body, probably aborted */ |
1857 | memFree8K(buf); | |
1858 | HttpStateData::SendComplete(httpState->fd, NULL, 0, COMM_ERR_CLOSING, data); | |
b6a2f15e | 1859 | } |
376bb137 | 1860 | } |
1861 | ||
1862 | static void | |
3d7e9d7c | 1863 | httpSendRequestEntity(int fd, char *bufnotused, size_t size, comm_err_t errflag, void *data) |
376bb137 | 1864 | { |
e6ccf245 | 1865 | HttpStateData *httpState = static_cast<HttpStateData *>(data); |
376bb137 | 1866 | StoreEntry *entry = httpState->entry; |
1867 | ErrorState *err; | |
c3d63b2a | 1868 | debug(11, 5) ("httpSendRequestEntity: FD %d: size %d: errflag %d.\n", |
62e76326 | 1869 | fd, (int) size, errflag); |
1870 | ||
376bb137 | 1871 | if (size > 0) { |
62e76326 | 1872 | fd_bytes(fd, size, FD_WRITE); |
1873 | kb_incr(&statCounter.server.all.kbytes_out, size); | |
1874 | kb_incr(&statCounter.server.http.kbytes_out, size); | |
376bb137 | 1875 | } |
62e76326 | 1876 | |
376bb137 | 1877 | if (errflag == COMM_ERR_CLOSING) |
62e76326 | 1878 | return; |
1879 | ||
376bb137 | 1880 | if (errflag) { |
4eb368f9 | 1881 | err = errorCon(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY); |
62e76326 | 1882 | err->xerrno = errno; |
1883 | err->request = requestLink(httpState->orig_request); | |
1884 | errorAppendEntry(entry, err); | |
1885 | comm_close(fd); | |
1886 | return; | |
376bb137 | 1887 | } |
62e76326 | 1888 | |
94439e4e | 1889 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
62e76326 | 1890 | comm_close(fd); |
1891 | return; | |
376bb137 | 1892 | } |
62e76326 | 1893 | |
21b92762 | 1894 | httpState->body_buf = (char *)memAllocate(MEM_8K_BUF); |
1895 | clientReadBody(httpState->orig_request, httpState->body_buf, 8192, httpRequestBodyHandler, httpState); | |
54220df8 | 1896 | } |
ccf44862 | 1897 | |
1898 | void | |
450e0c10 | 1899 | httpBuildVersion(HttpVersion * version, unsigned int major, unsigned int minor) |
110eb4e5 | 1900 | { |
1901 | version->major = major; | |
1902 | version->minor = minor; | |
ccf44862 | 1903 | } |