]>
Commit | Line | Data |
---|---|---|
da2b3a17 | 1 | |
30a4f2a8 | 2 | /* |
77b32a34 | 3 | * $Id: http.cc,v 1.335 1999/01/08 21:12:14 wessels Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 11 Hypertext Transfer Protocol (HTTP) | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
42c04c16 | 8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
e25c139f | 13 | * National Laboratory for Applied Network Research and funded by the |
14 | * National Science Foundation. Squid is Copyrighted (C) 1998 by | |
15 | * Duane Wessels and the University of California San Diego. Please | |
16 | * see the COPYRIGHT file for full details. Squid incorporates | |
17 | * software developed and/or copyrighted by other sources. Please see | |
18 | * 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" |
090089c4 | 42 | |
6bf8443a | 43 | static const char *const crlf = "\r\n"; |
4db43fab | 44 | |
9e4ad609 | 45 | static CNCB httpConnectDone; |
46 | static CWCB httpSendComplete; | |
54220df8 | 47 | static CWCB httpSendRequestEntry; |
48 | ||
9e4ad609 | 49 | static PF httpReadReply; |
50 | static PF httpSendRequest; | |
51 | static PF httpStateFree; | |
52 | static PF httpTimeout; | |
f5b8bbc4 | 53 | static void httpCacheNegatively(StoreEntry *); |
54 | static void httpMakePrivate(StoreEntry *); | |
55 | static void httpMakePublic(StoreEntry *); | |
f8309b15 | 56 | static int httpCachableReply(HttpStateData *); |
f9cece6e | 57 | static void httpMaybeRemovePublic(StoreEntry *, http_status); |
b8d8561b | 58 | |
b177367b | 59 | static void |
79d39a72 | 60 | httpStateFree(int fdnotused, void *data) |
f5558c95 | 61 | { |
b177367b | 62 | HttpStateData *httpState = data; |
0d4d4170 | 63 | if (httpState == NULL) |
b177367b | 64 | return; |
f88211e8 | 65 | storeUnlockObject(httpState->entry); |
0d4d4170 | 66 | if (httpState->reply_hdr) { |
db1cd23c | 67 | memFree(httpState->reply_hdr, MEM_8K_BUF); |
0d4d4170 | 68 | httpState->reply_hdr = NULL; |
69 | } | |
30a4f2a8 | 70 | requestUnlink(httpState->request); |
20cc1450 | 71 | requestUnlink(httpState->orig_request); |
7dd44885 | 72 | httpState->request = NULL; |
73 | httpState->orig_request = NULL; | |
74 | cbdataFree(httpState); | |
f5558c95 | 75 | } |
76 | ||
b8d8561b | 77 | int |
75e88d56 | 78 | httpCachable(method_t method) |
090089c4 | 79 | { |
090089c4 | 80 | /* GET and HEAD are cachable. Others are not. */ |
6eb42cae | 81 | if (method != METHOD_GET && method != METHOD_HEAD) |
090089c4 | 82 | return 0; |
090089c4 | 83 | /* else cachable */ |
84 | return 1; | |
85 | } | |
86 | ||
b8d8561b | 87 | static void |
5c5783a2 | 88 | httpTimeout(int fd, void *data) |
090089c4 | 89 | { |
b177367b | 90 | HttpStateData *httpState = data; |
593c9a75 | 91 | StoreEntry *entry = httpState->entry; |
9fb13bb6 | 92 | debug(11, 4) ("httpTimeout: FD %d: '%s'\n", fd, storeUrl(entry)); |
8796b9e9 | 93 | assert(entry->store_status == STORE_PENDING); |
73a3014d | 94 | if (entry->mem_obj->inmem_hi == 0) { |
db1cd23c | 95 | fwdFail(httpState->fwd, ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT, 0); |
9b312a19 | 96 | } |
0d4d4170 | 97 | comm_close(fd); |
090089c4 | 98 | } |
99 | ||
30a4f2a8 | 100 | /* This object can be cached for a long time */ |
b8d8561b | 101 | static void |
102 | httpMakePublic(StoreEntry * entry) | |
30a4f2a8 | 103 | { |
d46a87a8 | 104 | if (EBIT_TEST(entry->flags, ENTRY_CACHABLE)) |
30a4f2a8 | 105 | storeSetPublicKey(entry); |
106 | } | |
107 | ||
108 | /* This object should never be cached at all */ | |
b8d8561b | 109 | static void |
110 | httpMakePrivate(StoreEntry * entry) | |
30a4f2a8 | 111 | { |
30a4f2a8 | 112 | storeExpireNow(entry); |
30a4f2a8 | 113 | storeReleaseRequest(entry); /* delete object when not used */ |
f3e570e9 | 114 | /* storeReleaseRequest clears ENTRY_CACHABLE flag */ |
30a4f2a8 | 115 | } |
116 | ||
117 | /* This object may be negatively cached */ | |
b8d8561b | 118 | static void |
119 | httpCacheNegatively(StoreEntry * entry) | |
30a4f2a8 | 120 | { |
79b5cc5f | 121 | storeNegativeCache(entry); |
d46a87a8 | 122 | if (EBIT_TEST(entry->flags, ENTRY_CACHABLE)) |
30a4f2a8 | 123 | storeSetPublicKey(entry); |
30a4f2a8 | 124 | } |
125 | ||
f9cece6e | 126 | static void |
127 | httpMaybeRemovePublic(StoreEntry * e, http_status status) | |
128 | { | |
129 | int remove = 0; | |
f9cece6e | 130 | StoreEntry *pe; |
d46a87a8 | 131 | if (!EBIT_TEST(e->flags, KEY_PRIVATE)) |
9dc1202d | 132 | return; |
f9cece6e | 133 | switch (status) { |
134 | case HTTP_OK: | |
135 | case HTTP_NON_AUTHORITATIVE_INFORMATION: | |
136 | case HTTP_MULTIPLE_CHOICES: | |
137 | case HTTP_MOVED_PERMANENTLY: | |
138 | case HTTP_MOVED_TEMPORARILY: | |
139 | case HTTP_FORBIDDEN: | |
140 | case HTTP_NOT_FOUND: | |
141 | case HTTP_METHOD_NOT_ALLOWED: | |
142 | case HTTP_GONE: | |
143 | remove = 1; | |
144 | break; | |
145 | #if WORK_IN_PROGRESS | |
c8fd0193 | 146 | case HTTP_UNAUTHORIZED: |
147 | remove = 1; | |
f9cece6e | 148 | break; |
149 | #endif | |
150 | default: | |
151 | remove = 0; | |
152 | break; | |
153 | } | |
154 | if (!remove) | |
155 | return; | |
156 | assert(e->mem_obj); | |
08e5d64f | 157 | if ((pe = storeGetPublic(e->mem_obj->url, e->mem_obj->method)) != NULL) { |
0856d155 | 158 | assert(e != pe); |
159 | storeRelease(pe); | |
160 | } | |
161 | if (e->mem_obj->method == METHOD_GET) { | |
162 | /* A fresh GET should eject old HEAD objects */ | |
08e5d64f | 163 | if ((pe = storeGetPublic(e->mem_obj->url, METHOD_HEAD)) != NULL) { |
0856d155 | 164 | assert(e != pe); |
165 | storeRelease(pe); | |
166 | } | |
167 | } | |
f9cece6e | 168 | } |
169 | ||
f8309b15 | 170 | static int |
171 | httpCachableReply(HttpStateData * httpState) | |
c54e9052 | 172 | { |
d8b249ef | 173 | HttpReply *rep = httpState->entry->mem_obj->reply; |
174 | HttpHeader *hdr = &rep->header; | |
175 | const int cc_mask = (rep->cache_control) ? rep->cache_control->mask : 0; | |
c68e9c6b | 176 | const char *v; |
7faf2bdb | 177 | if (EBIT_TEST(cc_mask, CC_PRIVATE)) |
f8309b15 | 178 | return 0; |
7faf2bdb | 179 | if (EBIT_TEST(cc_mask, CC_NO_CACHE)) |
f8309b15 | 180 | return 0; |
ed2f05a1 | 181 | if (EBIT_TEST(cc_mask, CC_NO_STORE)) |
182 | return 0; | |
92695e5e | 183 | if (httpState->request->flags.auth) { |
a6dfe2d9 | 184 | /* |
185 | * Responses to requests with authorization may be cached | |
68aefb7d | 186 | * only if a Cache-Control: public reply header is present. |
a6dfe2d9 | 187 | * RFC 2068, sec 14.9.4 |
188 | */ | |
189 | if (!EBIT_TEST(cc_mask, CC_PUBLIC)) | |
fee0cebb | 190 | return 0; |
a6dfe2d9 | 191 | } |
f8309b15 | 192 | /* |
02fe0fbc | 193 | * We don't properly deal with Vary features yet, so we can't |
194 | * cache these | |
f8309b15 | 195 | */ |
783e4699 | 196 | if (httpHeaderHas(hdr, HDR_VARY)) |
197 | return 0; | |
c68e9c6b | 198 | /* Pragma: no-cache in _replies_ is not documented in HTTP, |
199 | * but servers like "Active Imaging Webcast/2.0" sure do use it */ | |
200 | if (httpHeaderHas(hdr, HDR_PRAGMA)) { | |
201 | String s = httpHeaderGetList(hdr, HDR_PRAGMA); | |
202 | const int no_cache = strListIsMember(&s, "no-cache", ','); | |
203 | stringClean(&s); | |
204 | if (no_cache) | |
205 | return 0; | |
206 | } | |
207 | /* | |
208 | * The "multipart/x-mixed-replace" content type is used for | |
209 | * continuous push replies. These are generally dynamic and | |
210 | * probably should not be cachable | |
211 | */ | |
212 | if ((v = httpHeaderGetStr(hdr, HDR_CONTENT_TYPE))) | |
213 | if (!strncasecmp(v, "multipart/x-mixed-replace", 25)) | |
214 | return 0; | |
cb69b4c7 | 215 | switch (httpState->entry->mem_obj->reply->sline.status) { |
c54e9052 | 216 | /* Responses that are cacheable */ |
19a04dac | 217 | case HTTP_OK: |
218 | case HTTP_NON_AUTHORITATIVE_INFORMATION: | |
219 | case HTTP_MULTIPLE_CHOICES: | |
220 | case HTTP_MOVED_PERMANENTLY: | |
221 | case HTTP_GONE: | |
1294c0fc | 222 | /* don't cache objects from peers w/o LMT, Date, or Expires */ |
cb69b4c7 | 223 | /* check that is it enough to check headers @?@ */ |
d8b249ef | 224 | if (rep->date > -1) |
c54e9052 | 225 | return 1; |
d8b249ef | 226 | else if (rep->last_modified > -1) |
c54e9052 | 227 | return 1; |
1294c0fc | 228 | else if (!httpState->peer) |
c54e9052 | 229 | return 1; |
d8b249ef | 230 | /* @?@ (here and 302): invalid expires header compiles to squid_curtime */ |
231 | else if (rep->expires > -1) | |
c54e9052 | 232 | return 1; |
c54e9052 | 233 | else |
234 | return 0; | |
79d39a72 | 235 | /* NOTREACHED */ |
c54e9052 | 236 | break; |
237 | /* Responses that only are cacheable if the server says so */ | |
19a04dac | 238 | case HTTP_MOVED_TEMPORARILY: |
d8b249ef | 239 | if (rep->expires > -1) |
c54e9052 | 240 | return 1; |
241 | else | |
242 | return 0; | |
79d39a72 | 243 | /* NOTREACHED */ |
c54e9052 | 244 | break; |
245 | /* Errors can be negatively cached */ | |
19a04dac | 246 | case HTTP_NO_CONTENT: |
247 | case HTTP_USE_PROXY: | |
248 | case HTTP_BAD_REQUEST: | |
249 | case HTTP_FORBIDDEN: | |
250 | case HTTP_NOT_FOUND: | |
251 | case HTTP_METHOD_NOT_ALLOWED: | |
252 | case HTTP_REQUEST_URI_TOO_LARGE: | |
253 | case HTTP_INTERNAL_SERVER_ERROR: | |
254 | case HTTP_NOT_IMPLEMENTED: | |
255 | case HTTP_BAD_GATEWAY: | |
256 | case HTTP_SERVICE_UNAVAILABLE: | |
257 | case HTTP_GATEWAY_TIMEOUT: | |
c54e9052 | 258 | return -1; |
79d39a72 | 259 | /* NOTREACHED */ |
c54e9052 | 260 | break; |
261 | /* Some responses can never be cached */ | |
0cdcddb9 | 262 | case HTTP_PARTIAL_CONTENT: /* Not yet supported */ |
19a04dac | 263 | case HTTP_SEE_OTHER: |
264 | case HTTP_NOT_MODIFIED: | |
265 | case HTTP_UNAUTHORIZED: | |
266 | case HTTP_PROXY_AUTHENTICATION_REQUIRED: | |
0cdcddb9 | 267 | case HTTP_INVALID_HEADER: /* Squid header parsing error */ |
c54e9052 | 268 | default: /* Unknown status code */ |
269 | return 0; | |
79d39a72 | 270 | /* NOTREACHED */ |
c54e9052 | 271 | break; |
272 | } | |
79d39a72 | 273 | /* NOTREACHED */ |
c54e9052 | 274 | } |
090089c4 | 275 | |
cb69b4c7 | 276 | /* rewrite this later using new interfaces @?@ */ |
b8d8561b | 277 | void |
0ee4272b | 278 | httpProcessReplyHeader(HttpStateData * httpState, const char *buf, int size) |
f5558c95 | 279 | { |
280 | char *t = NULL; | |
30a4f2a8 | 281 | StoreEntry *entry = httpState->entry; |
d3fb4dea | 282 | int room; |
283 | int hdr_len; | |
cb69b4c7 | 284 | HttpReply *reply = entry->mem_obj->reply; |
b6cfb65c | 285 | debug(11, 3) ("httpProcessReplyHeader: key '%s'\n", |
286 | storeKeyText(entry->key)); | |
e924600d | 287 | if (httpState->reply_hdr == NULL) |
7021844c | 288 | httpState->reply_hdr = memAllocate(MEM_8K_BUF); |
30a4f2a8 | 289 | if (httpState->reply_hdr_state == 0) { |
290 | hdr_len = strlen(httpState->reply_hdr); | |
ed85b771 | 291 | room = 8191 - hdr_len; |
30a4f2a8 | 292 | strncat(httpState->reply_hdr, buf, room < size ? room : size); |
d3fb4dea | 293 | hdr_len += room < size ? room : size; |
30a4f2a8 | 294 | if (hdr_len > 4 && strncmp(httpState->reply_hdr, "HTTP/", 5)) { |
84fa351c | 295 | debug(11, 3) ("httpProcessReplyHeader: Non-HTTP-compliant header: '%s'\n", httpState->reply_hdr); |
30a4f2a8 | 296 | httpState->reply_hdr_state += 2; |
728da2ee | 297 | reply->sline.status = HTTP_INVALID_HEADER; |
ed85b771 | 298 | return; |
d3fb4dea | 299 | } |
d1a43e28 | 300 | t = httpState->reply_hdr + hdr_len; |
301 | /* headers can be incomplete only if object still arriving */ | |
2334c194 | 302 | if (!httpState->eof) { |
303 | size_t k = headersEnd(httpState->reply_hdr, 8192); | |
304 | if (0 == k) | |
d1a43e28 | 305 | return; /* headers not complete */ |
2334c194 | 306 | t = httpState->reply_hdr + k; |
307 | } | |
2285407f | 308 | *t = '\0'; |
30a4f2a8 | 309 | httpState->reply_hdr_state++; |
f5558c95 | 310 | } |
30a4f2a8 | 311 | if (httpState->reply_hdr_state == 1) { |
123abbe1 | 312 | const Ctx ctx = ctx_enter(entry->mem_obj->url); |
30a4f2a8 | 313 | httpState->reply_hdr_state++; |
a3d5953d | 314 | debug(11, 9) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n", |
30a4f2a8 | 315 | httpState->reply_hdr); |
316 | /* Parse headers into reply structure */ | |
2246b732 | 317 | /* what happens if we fail to parse here? */ |
ee1679df | 318 | httpReplyParse(reply, httpState->reply_hdr); /* httpState->eof); */ |
ca98227c | 319 | storeTimestampsSet(entry); |
30a4f2a8 | 320 | /* Check if object is cacheable or not based on reply code */ |
cb69b4c7 | 321 | debug(11, 3) ("httpProcessReplyHeader: HTTP CODE: %d\n", reply->sline.status); |
9f239bed | 322 | if (neighbors_do_private_keys) |
323 | httpMaybeRemovePublic(entry, reply->sline.status); | |
f8309b15 | 324 | switch (httpCachableReply(httpState)) { |
c54e9052 | 325 | case 1: |
326 | httpMakePublic(entry); | |
30a4f2a8 | 327 | break; |
c54e9052 | 328 | case 0: |
329 | httpMakePrivate(entry); | |
f5558c95 | 330 | break; |
c54e9052 | 331 | case -1: |
851eeef7 | 332 | httpCacheNegatively(entry); |
30a4f2a8 | 333 | break; |
c54e9052 | 334 | default: |
335 | assert(0); | |
4e38e700 | 336 | break; |
f5558c95 | 337 | } |
0336304c | 338 | if (reply->cache_control) { |
339 | if (EBIT_TEST(reply->cache_control->mask, CC_PROXY_REVALIDATE)) | |
d46a87a8 | 340 | EBIT_SET(entry->flags, ENTRY_REVALIDATE); |
308e4a84 | 341 | else if (EBIT_TEST(reply->cache_control->mask, CC_MUST_REVALIDATE)) |
d46a87a8 | 342 | EBIT_SET(entry->flags, ENTRY_REVALIDATE); |
0336304c | 343 | } |
b515fc11 | 344 | if (httpState->flags.keepalive) |
9a47da71 | 345 | if (httpState->peer) |
346 | httpState->peer->stats.n_keepalives_sent++; | |
9f5a2895 | 347 | if (reply->keep_alive) |
1294c0fc | 348 | if (httpState->peer) |
349 | httpState->peer->stats.n_keepalives_recv++; | |
123abbe1 | 350 | ctx_exit(ctx); |
fc4d5b17 | 351 | if (reply->date > -1 && !httpState->peer) { |
352 | int skew = abs(reply->date - squid_curtime); | |
e1c811de | 353 | if (skew > 86400) |
354 | debug(11, 3) ("%s's clock is skewed by %d seconds!\n", | |
7d47d8e6 | 355 | httpState->request->host, skew); |
fc4d5b17 | 356 | } |
f5558c95 | 357 | } |
358 | } | |
359 | ||
603a02fd | 360 | static int |
361 | httpPconnTransferDone(HttpStateData * httpState) | |
362 | { | |
363 | /* return 1 if we got the last of the data on a persistent connection */ | |
364 | MemObject *mem = httpState->entry->mem_obj; | |
cb69b4c7 | 365 | HttpReply *reply = mem->reply; |
51fdcbd5 | 366 | debug(11, 3) ("httpPconnTransferDone: FD %d\n", httpState->fd); |
978e455f | 367 | /* |
99edd1c3 | 368 | * If we didn't send a keep-alive request header, then this |
978e455f | 369 | * can not be a persistent connection. |
370 | */ | |
b515fc11 | 371 | if (!httpState->flags.keepalive) |
603a02fd | 372 | return 0; |
9f5a2895 | 373 | /* |
374 | * What does the reply have to say about keep-alive? | |
375 | */ | |
376 | if (!reply->keep_alive) | |
377 | return 0; | |
51fdcbd5 | 378 | debug(11, 5) ("httpPconnTransferDone: content_length=%d\n", |
d8b249ef | 379 | reply->content_length); |
603a02fd | 380 | /* |
978e455f | 381 | * Deal with gross HTTP stuff |
382 | * - If we haven't seen the end of the reply headers, we can't | |
383 | * be persistent. | |
0856d155 | 384 | * - For HEAD requests we're done. |
978e455f | 385 | * - For "200 OK" check the content-length in the next block. |
978e455f | 386 | * - For "204 No Content" (even with content-length) we're done. |
387 | * - For "304 Not Modified" (even with content-length) we're done. | |
a3c60429 | 388 | * - 1XX replies never have a body; we're done. |
a3c60429 | 389 | * - For all other replies, check content length in next block. |
603a02fd | 390 | */ |
978e455f | 391 | if (httpState->reply_hdr_state < 2) |
392 | return 0; | |
0856d155 | 393 | else if (httpState->request->method == METHOD_HEAD) |
394 | return 1; | |
cb69b4c7 | 395 | else if (reply->sline.status == HTTP_OK) |
a3c60429 | 396 | (void) 0; /* common case, continue */ |
cb69b4c7 | 397 | else if (reply->sline.status == HTTP_NO_CONTENT) |
978e455f | 398 | return 1; |
cb69b4c7 | 399 | else if (reply->sline.status == HTTP_NOT_MODIFIED) |
978e455f | 400 | return 1; |
cb69b4c7 | 401 | else if (reply->sline.status < HTTP_OK) |
a3c60429 | 402 | return 1; |
603a02fd | 403 | /* |
a3c60429 | 404 | * If there is no content-length, then we can't be |
978e455f | 405 | * persistent. If there is a content length, then we must |
406 | * wait until we've seen the end of the body. | |
603a02fd | 407 | */ |
d8b249ef | 408 | if (reply->content_length < 0) |
603a02fd | 409 | return 0; |
d8b249ef | 410 | else if (mem->inmem_hi < reply->content_length + reply->hdr_sz) |
603a02fd | 411 | return 0; |
978e455f | 412 | else |
b34ed725 | 413 | return 1; |
603a02fd | 414 | } |
090089c4 | 415 | |
416 | /* This will be called when data is ready to be read from fd. Read until | |
417 | * error or connection closed. */ | |
f5558c95 | 418 | /* XXX this function is too long! */ |
b8d8561b | 419 | static void |
b177367b | 420 | httpReadReply(int fd, void *data) |
090089c4 | 421 | { |
b177367b | 422 | HttpStateData *httpState = data; |
95d659f0 | 423 | LOCAL_ARRAY(char, buf, SQUID_TCP_SO_RCVBUF); |
bfcaf585 | 424 | StoreEntry *entry = httpState->entry; |
603a02fd | 425 | const request_t *request = httpState->request; |
090089c4 | 426 | int len; |
30a4f2a8 | 427 | int bin; |
090089c4 | 428 | int clen; |
447e176b | 429 | size_t read_sz; |
430 | #if DELAY_POOLS | |
431 | delay_id delay_id = delayMostBytesAllowed(entry->mem_obj); | |
432 | #endif | |
234967c9 | 433 | /* check if we want to defer reading */ |
1513873c | 434 | errno = 0; |
447e176b | 435 | read_sz = SQUID_TCP_SO_RCVBUF; |
436 | #if DELAY_POOLS | |
56e64999 | 437 | read_sz = delayBytesWanted(delay_id, 1, read_sz); |
447e176b | 438 | #endif |
886f2785 | 439 | Counter.syscalls.sock.reads++; |
447e176b | 440 | len = read(fd, buf, read_sz); |
a3d5953d | 441 | debug(11, 5) ("httpReadReply: FD %d: len %d.\n", fd, len); |
30a4f2a8 | 442 | if (len > 0) { |
ee1679df | 443 | fd_bytes(fd, len, FD_READ); |
447e176b | 444 | #if DELAY_POOLS |
445 | delayBytesIn(delay_id, len); | |
446 | #endif | |
a0f32775 | 447 | kb_incr(&Counter.server.all.kbytes_in, len); |
448 | kb_incr(&Counter.server.http.kbytes_in, len); | |
4f92c80c | 449 | commSetTimeout(fd, Config.Timeout.read, NULL, NULL); |
4a63c85f | 450 | IOStats.Http.reads++; |
30a4f2a8 | 451 | for (clen = len - 1, bin = 0; clen; bin++) |
452 | clen >>= 1; | |
453 | IOStats.Http.read_hist[bin]++; | |
454 | } | |
5ede6c8f | 455 | if (!httpState->reply_hdr && len > 0) { |
456 | /* Skip whitespace */ | |
457 | while (len > 0 && isspace(*buf)) | |
458 | xmemmove(buf, buf + 1, len--); | |
459 | if (len == 0) { | |
460 | /* Continue to read... */ | |
461 | commSetSelect(fd, COMM_SELECT_READ, httpReadReply, httpState, 0); | |
462 | return; | |
463 | } | |
464 | } | |
ba718c8f | 465 | if (len < 0) { |
55cb44f1 | 466 | debug(50, 2) ("httpReadReply: FD %d: read failure: %s.\n", |
467 | fd, xstrerror()); | |
b224ea98 | 468 | if (ignoreErrno(errno)) { |
9b312a19 | 469 | commSetSelect(fd, COMM_SELECT_READ, httpReadReply, httpState, 0); |
910169e5 | 470 | } else if (entry->mem_obj->inmem_hi == 0) { |
db1cd23c | 471 | fwdFail(httpState->fwd, ERR_READ_ERROR, HTTP_INTERNAL_SERVER_ERROR, errno); |
1afe05c5 | 472 | comm_close(fd); |
090089c4 | 473 | } else { |
0d4d4170 | 474 | comm_close(fd); |
090089c4 | 475 | } |
8350fe9b | 476 | } else if (len == 0 && entry->mem_obj->inmem_hi == 0) { |
db1cd23c | 477 | fwdFail(httpState->fwd, ERR_ZERO_SIZE_OBJECT, HTTP_SERVICE_UNAVAILABLE, errno); |
910169e5 | 478 | httpState->eof = 1; |
479 | comm_close(fd); | |
090089c4 | 480 | } else if (len == 0) { |
481 | /* Connection closed; retrieval done. */ | |
f86a6a46 | 482 | httpState->eof = 1; |
d1a43e28 | 483 | if (httpState->reply_hdr_state < 2) |
b34ed725 | 484 | /* |
485 | * Yes Henrik, there is a point to doing this. When we | |
486 | * called httpProcessReplyHeader() before, we didn't find | |
487 | * the end of headers, but now we are definately at EOF, so | |
488 | * we want to process the reply headers. | |
489 | */ | |
d1a43e28 | 490 | httpProcessReplyHeader(httpState, buf, len); |
db1cd23c | 491 | fwdComplete(httpState->fwd); |
0d4d4170 | 492 | comm_close(fd); |
090089c4 | 493 | } else { |
7e3e1d01 | 494 | if (httpState->reply_hdr_state < 2) { |
30a4f2a8 | 495 | httpProcessReplyHeader(httpState, buf, len); |
db1cd23c | 496 | if (httpState->reply_hdr_state == 2) { |
497 | http_status s = entry->mem_obj->reply->sline.status; | |
498 | /* If its "successful" reply, allow the client | |
499 | * to get it | |
500 | */ | |
501 | if (s >= 200 && s < 300) | |
502 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); | |
503 | } | |
7e3e1d01 | 504 | } |
620da955 | 505 | storeAppend(entry, buf, len); |
9d66d521 | 506 | #ifdef OPTIMISTIC_IO |
507 | if (entry->store_status == STORE_ABORTED) { | |
508 | /* | |
509 | * the above storeAppend() call could ABORT this entry, | |
510 | * in that case, the server FD should already be closed. | |
511 | * there's nothing for us to do. | |
512 | */ | |
513 | (void) 0; | |
514 | } else | |
515 | #endif | |
603a02fd | 516 | if (httpPconnTransferDone(httpState)) { |
5b29969a | 517 | /* yes we have to clear all these! */ |
8796b9e9 | 518 | commSetDefer(fd, NULL, NULL); |
5b29969a | 519 | commSetTimeout(fd, -1, NULL, NULL); |
520 | commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); | |
603a02fd | 521 | comm_remove_close_handler(fd, httpStateFree, httpState); |
db1cd23c | 522 | fwdUnregister(fd, httpState->fwd); |
8796b9e9 | 523 | pconnPush(fd, request->host, request->port); |
8a28f65f | 524 | fwdComplete(httpState->fwd); |
603a02fd | 525 | httpState->fd = -1; |
526 | httpStateFree(-1, httpState); | |
527 | } else { | |
9f5a2895 | 528 | /* Wait for EOF condition */ |
603a02fd | 529 | commSetSelect(fd, COMM_SELECT_READ, httpReadReply, httpState, 0); |
530 | } | |
090089c4 | 531 | } |
532 | } | |
533 | ||
534 | /* This will be called when request write is complete. Schedule read of | |
535 | * reply. */ | |
b8d8561b | 536 | static void |
79a15e0a | 537 | httpSendComplete(int fd, char *bufnotused, size_t size, int errflag, void *data) |
090089c4 | 538 | { |
30a4f2a8 | 539 | HttpStateData *httpState = data; |
9b312a19 | 540 | StoreEntry *entry = httpState->entry; |
541 | ErrorState *err; | |
a3d5953d | 542 | debug(11, 5) ("httpSendComplete: FD %d: size %d: errflag %d.\n", |
090089c4 | 543 | fd, size, errflag); |
ee1679df | 544 | if (size > 0) { |
545 | fd_bytes(fd, size, FD_WRITE); | |
a0f32775 | 546 | kb_incr(&Counter.server.all.kbytes_out, size); |
399e85ea | 547 | kb_incr(&Counter.server.http.kbytes_out, size); |
ee1679df | 548 | } |
ea3a2a69 | 549 | if (errflag == COMM_ERR_CLOSING) |
550 | return; | |
090089c4 | 551 | if (errflag) { |
fe40a877 | 552 | err = errorCon(ERR_WRITE_ERROR, HTTP_INTERNAL_SERVER_ERROR); |
c45ed9ad | 553 | err->xerrno = errno; |
79a15e0a | 554 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 555 | errorAppendEntry(entry, err); |
0d4d4170 | 556 | comm_close(fd); |
090089c4 | 557 | return; |
558 | } else { | |
559 | /* Schedule read reply. */ | |
b177367b | 560 | commSetSelect(fd, |
019dd986 | 561 | COMM_SELECT_READ, |
b177367b | 562 | httpReadReply, |
cd1fb0eb | 563 | httpState, 0); |
41462d93 | 564 | commSetDefer(fd, fwdCheckDeferRead, entry); |
090089c4 | 565 | } |
566 | } | |
567 | ||
99edd1c3 | 568 | /* |
569 | * build request headers and append them to a given MemBuf | |
570 | * used by httpBuildRequestPrefix() | |
571 | * note: calls httpHeaderInit(), the caller is responsible for Clean()-ing | |
572 | */ | |
e1e72f06 | 573 | void |
6bf8443a | 574 | httpBuildRequestHeader(request_t * request, |
575 | request_t * orig_request, | |
576 | StoreEntry * entry, | |
5999b776 | 577 | HttpHeader * hdr_out, |
603a02fd | 578 | int cfd, |
b515fc11 | 579 | http_state_flags flags) |
6bf8443a | 580 | { |
99edd1c3 | 581 | /* building buffer for complex strings */ |
5999b776 | 582 | #define BBUF_SZ (MAX_URL+32) |
99edd1c3 | 583 | LOCAL_ARRAY(char, bbuf, BBUF_SZ); |
584 | String strConnection = StringNull; | |
585 | const HttpHeader *hdr_in = &orig_request->header; | |
5d679edb | 586 | int we_do_ranges; |
99edd1c3 | 587 | const HttpHeaderEntry *e; |
588 | HttpHeaderPos pos = HttpHeaderInitPos; | |
2246b732 | 589 | httpHeaderInit(hdr_out, hoRequest); |
99edd1c3 | 590 | /* append our IMS header */ |
e17dc75c | 591 | if (entry && entry->lastmod > -1 && request->method == METHOD_GET) |
99edd1c3 | 592 | httpHeaderPutTime(hdr_out, HDR_IF_MODIFIED_SINCE, entry->lastmod); |
593 | ||
5d679edb | 594 | /* decide if we want to do Ranges ourselves |
595 | * (and fetch the whole object now) | |
596 | * We want to handle Ranges ourselves iff | |
597 | * - we can actually parse client Range specs | |
598 | * - the specs are expected to be simple enough (e.g. no out-of-order ranges) | |
599 | * - reply will be cachable | |
db1cd23c | 600 | * (If the reply will be uncachable we have to throw it away after |
5d679edb | 601 | * serving this request, so it is better to forward ranges to |
602 | * the server and fetch only the requested content) | |
603 | */ | |
604 | we_do_ranges = | |
c68e9c6b | 605 | orig_request->range && orig_request->flags.cachable && !httpHdrRangeWillBeComplex(orig_request->range) && (Config.rangeOffsetLimit == -1 || httpHdrRangeFirstOffset(orig_request->range) <= Config.rangeOffsetLimit); |
5d679edb | 606 | debug(11, 8) ("httpBuildRequestHeader: range specs: %p, cachable: %d; we_do_ranges: %d\n", |
607 | orig_request->range, orig_request->flags.cachable, we_do_ranges); | |
137ee196 | 608 | |
99edd1c3 | 609 | strConnection = httpHeaderGetList(hdr_in, HDR_CONNECTION); |
610 | while ((e = httpHeaderGetEntry(hdr_in, &pos))) { | |
611 | debug(11, 5) ("httpBuildRequestHeader: %s: %s\n", | |
612 | strBuf(e->name), strBuf(e->value)); | |
613 | if (!httpRequestHdrAllowed(e, &strConnection)) | |
6bf8443a | 614 | continue; |
99edd1c3 | 615 | switch (e->id) { |
616 | case HDR_PROXY_AUTHORIZATION: | |
c68e9c6b | 617 | /* If we're not doing proxy auth, then it must be passed on */ |
92695e5e | 618 | if (!request->flags.used_proxy_auth) |
99edd1c3 | 619 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); |
620 | break; | |
c68e9c6b | 621 | case HDR_AUTHORIZATION: |
622 | /* If we're not doing www auth, then it must be passed on */ | |
623 | if (!request->flags.accelerated || !request->flags.used_proxy_auth) | |
624 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
625 | else | |
626 | request->flags.auth = 0; /* We have used the authentication */ | |
627 | break; | |
99edd1c3 | 628 | case HDR_HOST: |
77ed547a | 629 | /* Don't use client's Host: header for redirected requests */ |
c68e9c6b | 630 | if (!request->flags.redirected || !Config.onoff.redir_rewrites_host) |
99edd1c3 | 631 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); |
632 | break; | |
633 | case HDR_IF_MODIFIED_SINCE: | |
634 | /* append unless we added our own; | |
635 | * note: at most one client's ims header can pass through */ | |
636 | if (!httpHeaderHas(hdr_out, HDR_IF_MODIFIED_SINCE)) | |
637 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
638 | break; | |
639 | case HDR_MAX_FORWARDS: | |
b3b64e58 | 640 | if (orig_request->method == METHOD_TRACE) { |
99edd1c3 | 641 | /* sacrificing efficiency over clarity, etc. */ |
642 | const int hops = httpHeaderGetInt(hdr_in, HDR_MAX_FORWARDS); | |
643 | if (hops > 0) | |
5999b776 | 644 | httpHeaderPutInt(hdr_out, HDR_MAX_FORWARDS, hops - 1); |
b3b64e58 | 645 | } |
99edd1c3 | 646 | break; |
137ee196 | 647 | case HDR_RANGE: |
a9771e51 | 648 | case HDR_IF_RANGE: |
5d679edb | 649 | case HDR_REQUEST_RANGE: |
650 | if (!we_do_ranges) | |
137ee196 | 651 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); |
652 | break; | |
99edd1c3 | 653 | case HDR_PROXY_CONNECTION: |
654 | case HDR_CONNECTION: | |
655 | case HDR_VIA: | |
656 | case HDR_X_FORWARDED_FOR: | |
657 | case HDR_CACHE_CONTROL: | |
658 | /* append these after the loop if needed */ | |
659 | break; | |
660 | default: | |
661 | /* pass on all other header fields */ | |
662 | httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e)); | |
66f7337b | 663 | } |
88738790 | 664 | } |
99edd1c3 | 665 | |
666 | /* append fake user agent if configured and | |
667 | * the real one is not supplied by the client */ | |
668 | if (Config.fake_ua && !httpHeaderHas(hdr_out, HDR_USER_AGENT)) | |
669 | httpHeaderPutStr(hdr_out, HDR_USER_AGENT, Config.fake_ua); | |
670 | ||
671 | /* append Via */ | |
672 | { | |
673 | String strVia = httpHeaderGetList(hdr_in, HDR_VIA); | |
674 | snprintf(bbuf, BBUF_SZ, "%3.1f %s", orig_request->http_ver, ThisCache); | |
675 | strListAdd(&strVia, bbuf, ','); | |
676 | httpHeaderPutStr(hdr_out, HDR_VIA, strBuf(strVia)); | |
677 | stringClean(&strVia); | |
6bf8443a | 678 | } |
99edd1c3 | 679 | /* append X-Forwarded-For */ |
680 | { | |
681 | String strFwd = httpHeaderGetList(hdr_in, HDR_X_FORWARDED_FOR); | |
682 | strListAdd(&strFwd, (cfd < 0 ? "unknown" : fd_table[cfd].ipaddr), ','); | |
683 | httpHeaderPutStr(hdr_out, HDR_X_FORWARDED_FOR, strBuf(strFwd)); | |
684 | stringClean(&strFwd); | |
685 | } | |
686 | /* append Host if not there already */ | |
687 | if (!httpHeaderHas(hdr_out, HDR_HOST)) { | |
688 | /* use port# only if not default */ | |
689 | if (orig_request->port == urlDefaultPort(orig_request->protocol)) { | |
690 | httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->host); | |
691 | } else { | |
2246b732 | 692 | httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d", |
99edd1c3 | 693 | orig_request->host, (int) orig_request->port); |
99edd1c3 | 694 | } |
6bf8443a | 695 | } |
c68e9c6b | 696 | /* append Authorization if known in URL, not in header and going direct */ |
697 | if (!httpHeaderHas(hdr_out, HDR_AUTHORIZATION)) { | |
698 | if (!request->flags.proxying && *request->login) { | |
699 | httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s", | |
700 | base64_encode(request->login)); | |
701 | } | |
702 | } | |
703 | /* append Proxy-Authorization if configured for peer, and proxying */ | |
704 | if (!httpHeaderHas(hdr_out, HDR_PROXY_AUTHORIZATION)) { | |
705 | if (request->flags.proxying && request->peer_login) { | |
706 | httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s", | |
707 | base64_encode(request->peer_login)); | |
708 | } | |
709 | } | |
99edd1c3 | 710 | /* append Cache-Control, add max-age if not there already */ |
711 | { | |
712 | HttpHdrCc *cc = httpHeaderGetCc(hdr_in); | |
713 | if (!cc) | |
714 | cc = httpHdrCcCreate(); | |
715 | if (!EBIT_TEST(cc->mask, CC_MAX_AGE)) { | |
9b5d1d21 | 716 | const char *url = entry ? storeUrl(entry) : urlCanonical(orig_request); |
99edd1c3 | 717 | httpHdrCcSetMaxAge(cc, getMaxAge(url)); |
718 | if (strLen(request->urlpath)) | |
719 | assert(strstr(url, strBuf(request->urlpath))); | |
720 | } | |
db1cd23c | 721 | if (flags.only_if_cached) |
722 | EBIT_SET(cc->mask, CC_ONLY_IF_CACHED); | |
99edd1c3 | 723 | httpHeaderPutCc(hdr_out, cc); |
724 | httpHdrCcDestroy(cc); | |
6bf8443a | 725 | } |
99edd1c3 | 726 | /* maybe append Connection: keep-alive */ |
b515fc11 | 727 | if (flags.keepalive) { |
728 | if (flags.proxying) { | |
99edd1c3 | 729 | httpHeaderPutStr(hdr_out, HDR_PROXY_CONNECTION, "keep-alive"); |
603a02fd | 730 | } else { |
99edd1c3 | 731 | httpHeaderPutStr(hdr_out, HDR_CONNECTION, "keep-alive"); |
603a02fd | 732 | } |
603a02fd | 733 | } |
99edd1c3 | 734 | stringClean(&strConnection); |
735 | } | |
736 | ||
737 | /* build request prefix and append it to a given MemBuf; | |
738 | * return the length of the prefix */ | |
739 | size_t | |
740 | httpBuildRequestPrefix(request_t * request, | |
741 | request_t * orig_request, | |
742 | StoreEntry * entry, | |
5999b776 | 743 | MemBuf * mb, |
99edd1c3 | 744 | int cfd, |
b515fc11 | 745 | http_state_flags flags) |
99edd1c3 | 746 | { |
747 | const int offset = mb->size; | |
748 | memBufPrintf(mb, "%s %s HTTP/1.0\r\n", | |
749 | RequestMethodStr[request->method], | |
750 | strLen(request->urlpath) ? strBuf(request->urlpath) : "/"); | |
751 | /* build and pack headers */ | |
752 | { | |
753 | HttpHeader hdr; | |
754 | Packer p; | |
755 | httpBuildRequestHeader(request, orig_request, entry, &hdr, cfd, flags); | |
756 | packerToMemInit(&p, mb); | |
757 | httpHeaderPackInto(&hdr, &p); | |
758 | httpHeaderClean(&hdr); | |
759 | packerClean(&p); | |
9d9d144b | 760 | } |
99edd1c3 | 761 | /* append header terminator */ |
762 | memBufAppend(mb, "\r\n", 2); | |
763 | return mb->size - offset; | |
6bf8443a | 764 | } |
090089c4 | 765 | /* This will be called when connect completes. Write request. */ |
b8d8561b | 766 | static void |
b177367b | 767 | httpSendRequest(int fd, void *data) |
090089c4 | 768 | { |
b177367b | 769 | HttpStateData *httpState = data; |
99edd1c3 | 770 | MemBuf mb; |
30a4f2a8 | 771 | request_t *req = httpState->request; |
620da955 | 772 | StoreEntry *entry = httpState->entry; |
2a26c096 | 773 | int cfd; |
1294c0fc | 774 | peer *p = httpState->peer; |
901e234d | 775 | CWCB *sendHeaderDone; |
090089c4 | 776 | |
a3d5953d | 777 | debug(11, 5) ("httpSendRequest: FD %d: httpState %p.\n", fd, httpState); |
090089c4 | 778 | |
efb9218c | 779 | if (pumpMethod(req->method)) |
7db8b16d | 780 | sendHeaderDone = httpSendRequestEntry; |
781 | else | |
782 | sendHeaderDone = httpSendComplete; | |
54220df8 | 783 | |
2a26c096 | 784 | if (!opt_forwarded_for) |
6bf8443a | 785 | cfd = -1; |
2a26c096 | 786 | else if (entry->mem_obj == NULL) |
6bf8443a | 787 | cfd = -1; |
2a26c096 | 788 | else |
382d851a | 789 | cfd = entry->mem_obj->fd; |
b0a1e5bf | 790 | assert(-1 == cfd || FD_SOCKET == fd_table[cfd].type); |
1294c0fc | 791 | if (p != NULL) |
b515fc11 | 792 | httpState->flags.proxying = 1; |
efb9218c | 793 | /* |
99edd1c3 | 794 | * Is keep-alive okay for all request methods? |
efb9218c | 795 | */ |
796 | if (p == NULL) | |
b515fc11 | 797 | httpState->flags.keepalive = 1; |
efb9218c | 798 | else if (p->stats.n_keepalives_sent < 10) |
b515fc11 | 799 | httpState->flags.keepalive = 1; |
efb9218c | 800 | else if ((double) p->stats.n_keepalives_recv / (double) p->stats.n_keepalives_sent > 0.50) |
b515fc11 | 801 | httpState->flags.keepalive = 1; |
db1cd23c | 802 | if (httpState->peer) |
803 | if (neighborType(httpState->peer, httpState->request) == PEER_SIBLING) | |
804 | httpState->flags.only_if_cached = 1; | |
99edd1c3 | 805 | memBufDefInit(&mb); |
806 | httpBuildRequestPrefix(req, | |
79a15e0a | 807 | httpState->orig_request, |
6bf8443a | 808 | entry, |
99edd1c3 | 809 | &mb, |
603a02fd | 810 | cfd, |
811 | httpState->flags); | |
99edd1c3 | 812 | debug(11, 6) ("httpSendRequest: FD %d:\n%s\n", fd, mb.buf); |
813 | comm_write_mbuf(fd, mb, sendHeaderDone, httpState); | |
090089c4 | 814 | } |
910169e5 | 815 | void |
db1cd23c | 816 | httpStart(FwdState * fwd) |
603a02fd | 817 | { |
db1cd23c | 818 | int fd = fwd->server_fd; |
cb87dab6 | 819 | HttpStateData *httpState = memAllocate(MEM_HTTP_STATE_DATA); |
910169e5 | 820 | request_t *proxy_req; |
db1cd23c | 821 | request_t *orig_req = fwd->request; |
910169e5 | 822 | debug(11, 3) ("httpStart: \"%s %s\"\n", |
823 | RequestMethodStr[orig_req->method], | |
db1cd23c | 824 | storeUrl(fwd->entry)); |
825 | cbdataAdd(httpState, memFree, MEM_HTTP_STATE_DATA); | |
826 | storeLockObject(fwd->entry); | |
827 | httpState->fwd = fwd; | |
828 | httpState->entry = fwd->entry; | |
9e4ad609 | 829 | httpState->fd = fd; |
db1cd23c | 830 | EBIT_SET(httpState->entry->flags, ENTRY_FWD_HDR_WAIT); |
831 | if (fwd->servers) | |
832 | httpState->peer = fwd->servers->peer; /* might be NULL */ | |
910169e5 | 833 | if (httpState->peer) { |
834 | proxy_req = requestCreate(orig_req->method, | |
835 | PROTO_NONE, storeUrl(httpState->entry)); | |
836 | xstrncpy(proxy_req->host, httpState->peer->host, SQUIDHOSTNAMELEN); | |
837 | proxy_req->port = httpState->peer->http_port; | |
23e8446b | 838 | proxy_req->flags = orig_req->flags; |
c68e9c6b | 839 | proxy_req->peer_login = httpState->peer->login; |
910169e5 | 840 | httpState->request = requestLink(proxy_req); |
910169e5 | 841 | httpState->orig_request = requestLink(orig_req); |
92695e5e | 842 | proxy_req->flags.proxying = 1; |
910169e5 | 843 | /* |
844 | * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here. | |
845 | * We might end up getting the object from somewhere else if, | |
846 | * for example, the request to this neighbor fails. | |
847 | */ | |
cd196bc8 | 848 | if (httpState->peer->options.proxy_only) |
910169e5 | 849 | storeReleaseRequest(httpState->entry); |
95e36d02 | 850 | #if DELAY_POOLS |
cd196bc8 | 851 | if (httpState->peer->options.no_delay) { |
447e176b | 852 | proxy_req->delay_id = 0; |
95e36d02 | 853 | } else { |
447e176b | 854 | proxy_req->delay_id = orig_req->delay_id; |
95e36d02 | 855 | } |
856 | #endif | |
603a02fd | 857 | } else { |
910169e5 | 858 | httpState->request = requestLink(orig_req); |
859 | httpState->orig_request = requestLink(orig_req); | |
603a02fd | 860 | } |
910169e5 | 861 | /* |
862 | * register the handler to free HTTP state data when the FD closes | |
863 | */ | |
864 | comm_add_close_handler(fd, httpStateFree, httpState); | |
a0f32775 | 865 | Counter.server.all.requests++; |
866 | Counter.server.http.requests++; | |
41462d93 | 867 | httpConnectDone(fd, COMM_OK, httpState); |
e5f6c5c2 | 868 | } |
869 | ||
870 | static void | |
871 | httpConnectDone(int fd, int status, void *data) | |
872 | { | |
873 | HttpStateData *httpState = data; | |
874 | request_t *request = httpState->request; | |
875 | StoreEntry *entry = httpState->entry; | |
9b312a19 | 876 | ErrorState *err; |
edeb28fd | 877 | if (status == COMM_ERR_DNS) { |
a3d5953d | 878 | debug(11, 4) ("httpConnectDone: Unknown host: %s\n", request->host); |
fe40a877 | 879 | err = errorCon(ERR_DNS_FAIL, HTTP_SERVICE_UNAVAILABLE); |
9b312a19 | 880 | err->dnsserver_msg = xstrdup(dns_error_message); |
79a15e0a | 881 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 882 | errorAppendEntry(entry, err); |
edeb28fd | 883 | comm_close(fd); |
884 | } else if (status != COMM_OK) { | |
fe40a877 | 885 | err = errorCon(ERR_CONNECT_FAIL, HTTP_SERVICE_UNAVAILABLE); |
c45ed9ad | 886 | err->xerrno = errno; |
9b312a19 | 887 | err->host = xstrdup(request->host); |
888 | err->port = request->port; | |
79a15e0a | 889 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 890 | errorAppendEntry(entry, err); |
1294c0fc | 891 | if (httpState->peer) |
892 | peerCheckConnectStart(httpState->peer); | |
e5f6c5c2 | 893 | comm_close(fd); |
894 | } else { | |
bfcaf585 | 895 | commSetSelect(fd, COMM_SELECT_WRITE, httpSendRequest, httpState, 0); |
86cf9987 | 896 | commSetTimeout(fd, Config.Timeout.read, httpTimeout, httpState); |
090089c4 | 897 | } |
090089c4 | 898 | } |
899 | ||
54220df8 | 900 | static void |
7db8b16d | 901 | httpSendRequestEntry(int fd, char *bufnotused, size_t size, int errflag, void *data) |
54220df8 | 902 | { |
903 | HttpStateData *httpState = data; | |
904 | StoreEntry *entry = httpState->entry; | |
905 | ErrorState *err; | |
906 | debug(11, 5) ("httpSendRequestEntry: FD %d: size %d: errflag %d.\n", | |
7db8b16d | 907 | fd, size, errflag); |
54220df8 | 908 | if (size > 0) { |
7db8b16d | 909 | fd_bytes(fd, size, FD_WRITE); |
54220df8 | 910 | kb_incr(&Counter.server.all.kbytes_out, size); |
911 | kb_incr(&Counter.server.http.kbytes_out, size); | |
912 | } | |
913 | if (errflag == COMM_ERR_CLOSING) | |
7db8b16d | 914 | return; |
54220df8 | 915 | if (errflag) { |
7db8b16d | 916 | err = errorCon(ERR_WRITE_ERROR, HTTP_INTERNAL_SERVER_ERROR); |
917 | err->xerrno = errno; | |
918 | err->request = requestLink(httpState->orig_request); | |
919 | errorAppendEntry(entry, err); | |
920 | comm_close(fd); | |
921 | return; | |
54220df8 | 922 | } |
7db8b16d | 923 | pumpStart(fd, entry, httpState->orig_request, httpSendComplete, httpState); |
54220df8 | 924 | } |