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