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