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