]>
Commit | Line | Data |
---|---|---|
da2b3a17 | 1 | |
30a4f2a8 | 2 | /* |
d8b249ef | 3 | * $Id: http.cc,v 1.257 1998/03/20 18:06:44 rousskov 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/ |
30a4f2a8 | 9 | * -------------------------------------------------------- |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
13 | * National Laboratory for Applied Network Research and funded by | |
14 | * the National Science Foundation. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
29 | * | |
30 | */ | |
019dd986 | 31 | |
32 | /* | |
30a4f2a8 | 33 | * Copyright (c) 1994, 1995. All rights reserved. |
34 | * | |
35 | * The Harvest software was developed by the Internet Research Task | |
36 | * Force Research Group on Resource Discovery (IRTF-RD): | |
37 | * | |
38 | * Mic Bowman of Transarc Corporation. | |
39 | * Peter Danzig of the University of Southern California. | |
40 | * Darren R. Hardy of the University of Colorado at Boulder. | |
41 | * Udi Manber of the University of Arizona. | |
42 | * Michael F. Schwartz of the University of Colorado at Boulder. | |
43 | * Duane Wessels of the University of Colorado at Boulder. | |
44 | * | |
45 | * This copyright notice applies to software in the Harvest | |
46 | * ``src/'' directory only. Users should consult the individual | |
47 | * copyright notices in the ``components/'' subdirectories for | |
48 | * copyright information about other software bundled with the | |
49 | * Harvest source code distribution. | |
50 | * | |
51 | * TERMS OF USE | |
52 | * | |
53 | * The Harvest software may be used and re-distributed without | |
54 | * charge, provided that the software origin and research team are | |
55 | * cited in any use of the system. Most commonly this is | |
56 | * accomplished by including a link to the Harvest Home Page | |
57 | * (http://harvest.cs.colorado.edu/) from the query page of any | |
58 | * Broker you deploy, as well as in the query result pages. These | |
59 | * links are generated automatically by the standard Broker | |
60 | * software distribution. | |
61 | * | |
62 | * The Harvest software is provided ``as is'', without express or | |
63 | * implied warranty, and with no support nor obligation to assist | |
64 | * in its use, correction, modification or enhancement. We assume | |
65 | * no liability with respect to the infringement of copyrights, | |
66 | * trade secrets, or any patents, and are not responsible for | |
67 | * consequential damages. Proper use of the Harvest software is | |
68 | * entirely the responsibility of the user. | |
69 | * | |
70 | * DERIVATIVE WORKS | |
71 | * | |
72 | * Users may make derivative works from the Harvest software, subject | |
73 | * to the following constraints: | |
74 | * | |
75 | * - You must include the above copyright notice and these | |
76 | * accompanying paragraphs in all forms of derivative works, | |
77 | * and any documentation and other materials related to such | |
78 | * distribution and use acknowledge that the software was | |
79 | * developed at the above institutions. | |
80 | * | |
81 | * - You must notify IRTF-RD regarding your distribution of | |
82 | * the derivative work. | |
83 | * | |
84 | * - You must clearly notify users that your are distributing | |
85 | * a modified version and not the original Harvest software. | |
86 | * | |
87 | * - Any derivative product is also subject to these copyright | |
88 | * and use restrictions. | |
89 | * | |
90 | * Note that the Harvest software is NOT in the public domain. We | |
91 | * retain copyright, as specified above. | |
92 | * | |
93 | * HISTORY OF FREE SOFTWARE STATUS | |
94 | * | |
95 | * Originally we required sites to license the software in cases | |
96 | * where they were going to build commercial products/services | |
97 | * around Harvest. In June 1995 we changed this policy. We now | |
98 | * allow people to use the core Harvest software (the code found in | |
99 | * the Harvest ``src/'' directory) for free. We made this change | |
100 | * in the interest of encouraging the widest possible deployment of | |
101 | * the technology. The Harvest software is really a reference | |
102 | * implementation of a set of protocols and formats, some of which | |
103 | * we intend to standardize. We encourage commercial | |
104 | * re-implementations of code complying to this set of standards. | |
019dd986 | 105 | */ |
44a47c6e | 106 | |
4a83b852 | 107 | /* |
108 | * Anonymizing patch by lutz@as-node.jena.thur.de | |
de3bdb4c | 109 | * have a look into http-anon.c to get more informations. |
4a83b852 | 110 | */ |
111 | ||
44a47c6e | 112 | #include "squid.h" |
090089c4 | 113 | |
6bf8443a | 114 | static const char *const crlf = "\r\n"; |
4db43fab | 115 | |
6bf8443a | 116 | enum { |
6fb52f6c | 117 | CCC_NOCACHE, |
118 | CCC_NOSTORE, | |
119 | CCC_MAXAGE, | |
120 | CCC_MAXSTALE, | |
121 | CCC_MINFRESH, | |
122 | CCC_ONLYIFCACHED, | |
123 | CCC_ENUM_END | |
6bf8443a | 124 | }; |
125 | ||
9e4ad609 | 126 | static CNCB httpConnectDone; |
127 | static CWCB httpSendComplete; | |
54220df8 | 128 | static CWCB httpSendRequestEntry; |
129 | ||
9e4ad609 | 130 | static PF httpReadReply; |
131 | static PF httpSendRequest; | |
132 | static PF httpStateFree; | |
133 | static PF httpTimeout; | |
f5b8bbc4 | 134 | static void httpAppendRequestHeader(char *hdr, const char *line, size_t * sz, size_t max, int); |
135 | static void httpCacheNegatively(StoreEntry *); | |
136 | static void httpMakePrivate(StoreEntry *); | |
137 | static void httpMakePublic(StoreEntry *); | |
bfcaf585 | 138 | static STABH httpAbort; |
f5b8bbc4 | 139 | static HttpStateData *httpBuildState(int, StoreEntry *, request_t *, peer *); |
140 | static int httpSocketOpen(StoreEntry *, request_t *); | |
141 | static void httpRestart(HttpStateData *); | |
efb9218c | 142 | static int httpTryRestart(HttpStateData *); |
f8309b15 | 143 | static int httpCachableReply(HttpStateData *); |
b8d8561b | 144 | |
b177367b | 145 | static void |
79d39a72 | 146 | httpStateFree(int fdnotused, void *data) |
f5558c95 | 147 | { |
b177367b | 148 | HttpStateData *httpState = data; |
0d4d4170 | 149 | if (httpState == NULL) |
b177367b | 150 | return; |
bfcaf585 | 151 | storeUnregisterAbort(httpState->entry); |
ddb6142d | 152 | assert(httpState->entry->store_status != STORE_PENDING); |
f88211e8 | 153 | storeUnlockObject(httpState->entry); |
0d4d4170 | 154 | if (httpState->reply_hdr) { |
3f6c0fb2 | 155 | memFree(MEM_8K_BUF, httpState->reply_hdr); |
0d4d4170 | 156 | httpState->reply_hdr = NULL; |
157 | } | |
30a4f2a8 | 158 | requestUnlink(httpState->request); |
20cc1450 | 159 | requestUnlink(httpState->orig_request); |
7dd44885 | 160 | httpState->request = NULL; |
161 | httpState->orig_request = NULL; | |
162 | cbdataFree(httpState); | |
f5558c95 | 163 | } |
164 | ||
b8d8561b | 165 | int |
75e88d56 | 166 | httpCachable(method_t method) |
090089c4 | 167 | { |
090089c4 | 168 | /* GET and HEAD are cachable. Others are not. */ |
6eb42cae | 169 | if (method != METHOD_GET && method != METHOD_HEAD) |
090089c4 | 170 | return 0; |
090089c4 | 171 | /* else cachable */ |
172 | return 1; | |
173 | } | |
174 | ||
b8d8561b | 175 | static void |
5c5783a2 | 176 | httpTimeout(int fd, void *data) |
090089c4 | 177 | { |
b177367b | 178 | HttpStateData *httpState = data; |
593c9a75 | 179 | StoreEntry *entry = httpState->entry; |
9b312a19 | 180 | ErrorState *err; |
9fb13bb6 | 181 | debug(11, 4) ("httpTimeout: FD %d: '%s'\n", fd, storeUrl(entry)); |
8796b9e9 | 182 | assert(entry->store_status == STORE_PENDING); |
73a3014d | 183 | if (entry->mem_obj->inmem_hi == 0) { |
fe40a877 | 184 | err = errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT); |
79a15e0a | 185 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 186 | errorAppendEntry(entry, err); |
b50179a6 | 187 | } else { |
b34ed725 | 188 | storeAbort(entry, 0); |
9b312a19 | 189 | } |
0d4d4170 | 190 | comm_close(fd); |
090089c4 | 191 | } |
192 | ||
30a4f2a8 | 193 | /* This object can be cached for a long time */ |
b8d8561b | 194 | static void |
195 | httpMakePublic(StoreEntry * entry) | |
30a4f2a8 | 196 | { |
79a15e0a | 197 | if (EBIT_TEST(entry->flag, ENTRY_CACHABLE)) |
30a4f2a8 | 198 | storeSetPublicKey(entry); |
199 | } | |
200 | ||
201 | /* This object should never be cached at all */ | |
b8d8561b | 202 | static void |
203 | httpMakePrivate(StoreEntry * entry) | |
30a4f2a8 | 204 | { |
30a4f2a8 | 205 | storeExpireNow(entry); |
79a15e0a | 206 | EBIT_CLR(entry->flag, ENTRY_CACHABLE); |
30a4f2a8 | 207 | storeReleaseRequest(entry); /* delete object when not used */ |
208 | } | |
209 | ||
210 | /* This object may be negatively cached */ | |
b8d8561b | 211 | static void |
212 | httpCacheNegatively(StoreEntry * entry) | |
30a4f2a8 | 213 | { |
79b5cc5f | 214 | storeNegativeCache(entry); |
79a15e0a | 215 | if (EBIT_TEST(entry->flag, ENTRY_CACHABLE)) |
30a4f2a8 | 216 | storeSetPublicKey(entry); |
30a4f2a8 | 217 | } |
218 | ||
f8309b15 | 219 | static int |
220 | httpCachableReply(HttpStateData * httpState) | |
c54e9052 | 221 | { |
d8b249ef | 222 | HttpReply *rep = httpState->entry->mem_obj->reply; |
223 | HttpHeader *hdr = &rep->header; | |
224 | const int cc_mask = (rep->cache_control) ? rep->cache_control->mask : 0; | |
7faf2bdb | 225 | if (EBIT_TEST(cc_mask, CC_PRIVATE)) |
f8309b15 | 226 | return 0; |
7faf2bdb | 227 | if (EBIT_TEST(cc_mask, CC_NO_CACHE)) |
f8309b15 | 228 | return 0; |
79a15e0a | 229 | if (EBIT_TEST(httpState->request->flags, REQ_AUTH)) |
7faf2bdb | 230 | if (!EBIT_TEST(cc_mask, CC_PROXY_REVALIDATE)) |
fee0cebb | 231 | return 0; |
f8309b15 | 232 | /* |
233 | * Dealing with cookies is quite a bit more complicated | |
234 | * than this. Ideally we should strip the cookie | |
235 | * header from the reply but still cache the reply body. | |
236 | * More confusion at draft-ietf-http-state-mgmt-05.txt. | |
237 | */ | |
cb69b4c7 | 238 | /* With new headers the above stripping should be easy to do? @?@ */ |
239 | if (httpHeaderHas(hdr, HDR_SET_COOKIE)) | |
f8309b15 | 240 | return 0; |
cb69b4c7 | 241 | switch (httpState->entry->mem_obj->reply->sline.status) { |
c54e9052 | 242 | /* Responses that are cacheable */ |
243 | case 200: /* OK */ | |
244 | case 203: /* Non-Authoritative Information */ | |
245 | case 300: /* Multiple Choices */ | |
246 | case 301: /* Moved Permanently */ | |
247 | case 410: /* Gone */ | |
1294c0fc | 248 | /* don't cache objects from peers w/o LMT, Date, or Expires */ |
cb69b4c7 | 249 | /* check that is it enough to check headers @?@ */ |
d8b249ef | 250 | if (rep->date > -1) |
c54e9052 | 251 | return 1; |
d8b249ef | 252 | else if (rep->last_modified > -1) |
c54e9052 | 253 | return 1; |
1294c0fc | 254 | else if (!httpState->peer) |
c54e9052 | 255 | return 1; |
d8b249ef | 256 | /* @?@ (here and 302): invalid expires header compiles to squid_curtime */ |
257 | else if (rep->expires > -1) | |
c54e9052 | 258 | return 1; |
c54e9052 | 259 | else |
260 | return 0; | |
79d39a72 | 261 | /* NOTREACHED */ |
c54e9052 | 262 | break; |
263 | /* Responses that only are cacheable if the server says so */ | |
264 | case 302: /* Moved temporarily */ | |
d8b249ef | 265 | if (rep->expires > -1) |
c54e9052 | 266 | return 1; |
267 | else | |
268 | return 0; | |
79d39a72 | 269 | /* NOTREACHED */ |
c54e9052 | 270 | break; |
cb69b4c7 | 271 | /* @?@ should we replace these magic numbers with http_status enums? */ |
c54e9052 | 272 | /* Errors can be negatively cached */ |
273 | case 204: /* No Content */ | |
274 | case 305: /* Use Proxy (proxy redirect) */ | |
275 | case 400: /* Bad Request */ | |
276 | case 403: /* Forbidden */ | |
277 | case 404: /* Not Found */ | |
278 | case 405: /* Method Now Allowed */ | |
279 | case 414: /* Request-URI Too Long */ | |
280 | case 500: /* Internal Server Error */ | |
281 | case 501: /* Not Implemented */ | |
282 | case 502: /* Bad Gateway */ | |
283 | case 503: /* Service Unavailable */ | |
284 | case 504: /* Gateway Timeout */ | |
285 | return -1; | |
79d39a72 | 286 | /* NOTREACHED */ |
c54e9052 | 287 | break; |
288 | /* Some responses can never be cached */ | |
88738790 | 289 | case 206: /* Partial Content -- Not yet supported */ |
c54e9052 | 290 | case 303: /* See Other */ |
291 | case 304: /* Not Modified */ | |
292 | case 401: /* Unauthorized */ | |
293 | case 407: /* Proxy Authentication Required */ | |
294 | case 600: /* Squid header parsing error */ | |
295 | default: /* Unknown status code */ | |
296 | return 0; | |
79d39a72 | 297 | /* NOTREACHED */ |
c54e9052 | 298 | break; |
299 | } | |
79d39a72 | 300 | /* NOTREACHED */ |
c54e9052 | 301 | } |
090089c4 | 302 | |
cb69b4c7 | 303 | /* rewrite this later using new interfaces @?@ */ |
b8d8561b | 304 | void |
0ee4272b | 305 | httpProcessReplyHeader(HttpStateData * httpState, const char *buf, int size) |
f5558c95 | 306 | { |
307 | char *t = NULL; | |
30a4f2a8 | 308 | StoreEntry *entry = httpState->entry; |
d3fb4dea | 309 | int room; |
310 | int hdr_len; | |
cb69b4c7 | 311 | HttpReply *reply = entry->mem_obj->reply; |
b6cfb65c | 312 | debug(11, 3) ("httpProcessReplyHeader: key '%s'\n", |
313 | storeKeyText(entry->key)); | |
e924600d | 314 | if (httpState->reply_hdr == NULL) |
7021844c | 315 | httpState->reply_hdr = memAllocate(MEM_8K_BUF); |
30a4f2a8 | 316 | if (httpState->reply_hdr_state == 0) { |
317 | hdr_len = strlen(httpState->reply_hdr); | |
ed85b771 | 318 | room = 8191 - hdr_len; |
30a4f2a8 | 319 | strncat(httpState->reply_hdr, buf, room < size ? room : size); |
d3fb4dea | 320 | hdr_len += room < size ? room : size; |
30a4f2a8 | 321 | if (hdr_len > 4 && strncmp(httpState->reply_hdr, "HTTP/", 5)) { |
84fa351c | 322 | debug(11, 3) ("httpProcessReplyHeader: Non-HTTP-compliant header: '%s'\n", httpState->reply_hdr); |
30a4f2a8 | 323 | httpState->reply_hdr_state += 2; |
cb69b4c7 | 324 | reply->sline.status = 555; |
ed85b771 | 325 | return; |
d3fb4dea | 326 | } |
d1a43e28 | 327 | t = httpState->reply_hdr + hdr_len; |
328 | /* headers can be incomplete only if object still arriving */ | |
f86a6a46 | 329 | if (!httpState->eof) |
d1a43e28 | 330 | if ((t = mime_headers_end(httpState->reply_hdr)) == NULL) |
331 | return; /* headers not complete */ | |
2285407f | 332 | *t = '\0'; |
30a4f2a8 | 333 | httpState->reply_hdr_state++; |
f5558c95 | 334 | } |
30a4f2a8 | 335 | if (httpState->reply_hdr_state == 1) { |
123abbe1 | 336 | const Ctx ctx = ctx_enter(entry->mem_obj->url); |
30a4f2a8 | 337 | httpState->reply_hdr_state++; |
a3d5953d | 338 | debug(11, 9) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n", |
30a4f2a8 | 339 | httpState->reply_hdr); |
340 | /* Parse headers into reply structure */ | |
cb69b4c7 | 341 | /* Old code never parsed headers if mime_headers_end failed, was it intentional ? @?@ @?@ */ |
342 | /* what happens if we fail to parse here? @?@ @?@ */ | |
ee1679df | 343 | httpReplyParse(reply, httpState->reply_hdr); /* httpState->eof); */ |
ca98227c | 344 | storeTimestampsSet(entry); |
30a4f2a8 | 345 | /* Check if object is cacheable or not based on reply code */ |
cb69b4c7 | 346 | debug(11, 3) ("httpProcessReplyHeader: HTTP CODE: %d\n", reply->sline.status); |
f8309b15 | 347 | switch (httpCachableReply(httpState)) { |
c54e9052 | 348 | case 1: |
349 | httpMakePublic(entry); | |
30a4f2a8 | 350 | break; |
c54e9052 | 351 | case 0: |
352 | httpMakePrivate(entry); | |
f5558c95 | 353 | break; |
c54e9052 | 354 | case -1: |
851eeef7 | 355 | httpCacheNegatively(entry); |
30a4f2a8 | 356 | break; |
c54e9052 | 357 | default: |
358 | assert(0); | |
4e38e700 | 359 | break; |
f5558c95 | 360 | } |
d8b249ef | 361 | if (reply->cache_control && EBIT_TEST(reply->cache_control->mask, CC_PROXY_REVALIDATE)) |
79a15e0a | 362 | EBIT_SET(entry->flag, ENTRY_REVALIDATE); |
9a47da71 | 363 | if (EBIT_TEST(httpState->flags, HTTP_KEEPALIVE)) |
364 | if (httpState->peer) | |
365 | httpState->peer->stats.n_keepalives_sent++; | |
d8b249ef | 366 | if (reply->pconn_keep_alive) |
1294c0fc | 367 | if (httpState->peer) |
368 | httpState->peer->stats.n_keepalives_recv++; | |
123abbe1 | 369 | ctx_exit(ctx); |
f5558c95 | 370 | } |
371 | } | |
372 | ||
603a02fd | 373 | static int |
374 | httpPconnTransferDone(HttpStateData * httpState) | |
375 | { | |
376 | /* return 1 if we got the last of the data on a persistent connection */ | |
377 | MemObject *mem = httpState->entry->mem_obj; | |
cb69b4c7 | 378 | HttpReply *reply = mem->reply; |
51fdcbd5 | 379 | debug(11, 3) ("httpPconnTransferDone: FD %d\n", httpState->fd); |
978e455f | 380 | /* |
381 | * If we didn't send a Keepalive request header, then this | |
382 | * can not be a persistent connection. | |
383 | */ | |
79a15e0a | 384 | if (!EBIT_TEST(httpState->flags, HTTP_KEEPALIVE)) |
603a02fd | 385 | return 0; |
51fdcbd5 | 386 | debug(11, 5) ("httpPconnTransferDone: content_length=%d\n", |
d8b249ef | 387 | reply->content_length); |
603a02fd | 388 | /* |
978e455f | 389 | * Deal with gross HTTP stuff |
390 | * - If we haven't seen the end of the reply headers, we can't | |
391 | * be persistent. | |
392 | * - For "200 OK" check the content-length in the next block. | |
978e455f | 393 | * - For "204 No Content" (even with content-length) we're done. |
394 | * - For "304 Not Modified" (even with content-length) we're done. | |
a3c60429 | 395 | * - 1XX replies never have a body; we're done. |
978e455f | 396 | * - For HEAD requests with content-length we're done. |
a3c60429 | 397 | * - For all other replies, check content length in next block. |
603a02fd | 398 | */ |
978e455f | 399 | if (httpState->reply_hdr_state < 2) |
400 | return 0; | |
cb69b4c7 | 401 | else if (reply->sline.status == HTTP_OK) |
a3c60429 | 402 | (void) 0; /* common case, continue */ |
cb69b4c7 | 403 | else if (reply->sline.status == HTTP_NO_CONTENT) |
978e455f | 404 | return 1; |
cb69b4c7 | 405 | else if (reply->sline.status == HTTP_NOT_MODIFIED) |
978e455f | 406 | return 1; |
cb69b4c7 | 407 | else if (reply->sline.status < HTTP_OK) |
a3c60429 | 408 | return 1; |
978e455f | 409 | else if (httpState->request->method == METHOD_HEAD) |
410 | return 1; | |
603a02fd | 411 | /* |
a3c60429 | 412 | * If there is no content-length, then we can't be |
978e455f | 413 | * persistent. If there is a content length, then we must |
414 | * wait until we've seen the end of the body. | |
603a02fd | 415 | */ |
d8b249ef | 416 | if (reply->content_length < 0) |
603a02fd | 417 | return 0; |
d8b249ef | 418 | else if (mem->inmem_hi < reply->content_length + reply->hdr_sz) |
603a02fd | 419 | return 0; |
978e455f | 420 | else |
b34ed725 | 421 | return 1; |
603a02fd | 422 | } |
090089c4 | 423 | |
424 | /* This will be called when data is ready to be read from fd. Read until | |
425 | * error or connection closed. */ | |
f5558c95 | 426 | /* XXX this function is too long! */ |
b8d8561b | 427 | static void |
b177367b | 428 | httpReadReply(int fd, void *data) |
090089c4 | 429 | { |
b177367b | 430 | HttpStateData *httpState = data; |
95d659f0 | 431 | LOCAL_ARRAY(char, buf, SQUID_TCP_SO_RCVBUF); |
bfcaf585 | 432 | StoreEntry *entry = httpState->entry; |
603a02fd | 433 | const request_t *request = httpState->request; |
090089c4 | 434 | int len; |
30a4f2a8 | 435 | int bin; |
090089c4 | 436 | int clen; |
9b312a19 | 437 | ErrorState *err; |
d89d1fb6 | 438 | if (protoAbortFetch(entry)) { |
9b312a19 | 439 | storeAbort(entry, 0); |
a3d5953d | 440 | comm_close(fd); |
441 | return; | |
234967c9 | 442 | } |
443 | /* check if we want to defer reading */ | |
8350fe9b | 444 | clen = entry->mem_obj->inmem_hi; |
1513873c | 445 | errno = 0; |
30a4f2a8 | 446 | len = read(fd, buf, SQUID_TCP_SO_RCVBUF); |
a3d5953d | 447 | debug(11, 5) ("httpReadReply: FD %d: len %d.\n", fd, len); |
30a4f2a8 | 448 | if (len > 0) { |
ee1679df | 449 | fd_bytes(fd, len, FD_READ); |
a0f32775 | 450 | kb_incr(&Counter.server.all.kbytes_in, len); |
451 | kb_incr(&Counter.server.http.kbytes_in, len); | |
4f92c80c | 452 | commSetTimeout(fd, Config.Timeout.read, NULL, NULL); |
4a63c85f | 453 | IOStats.Http.reads++; |
30a4f2a8 | 454 | for (clen = len - 1, bin = 0; clen; bin++) |
455 | clen >>= 1; | |
456 | IOStats.Http.read_hist[bin]++; | |
457 | } | |
ba718c8f | 458 | if (len < 0) { |
b224ea98 | 459 | if (ignoreErrno(errno)) { |
9b312a19 | 460 | commSetSelect(fd, COMM_SELECT_READ, httpReadReply, httpState, 0); |
090089c4 | 461 | } else { |
73a3014d | 462 | if (clen == 0) { |
fe40a877 | 463 | err = errorCon(ERR_READ_ERROR, HTTP_INTERNAL_SERVER_ERROR); |
c45ed9ad | 464 | err->xerrno = errno; |
79a15e0a | 465 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 466 | errorAppendEntry(entry, err); |
b50179a6 | 467 | } else { |
b34ed725 | 468 | storeAbort(entry, 0); |
9b312a19 | 469 | } |
0d4d4170 | 470 | comm_close(fd); |
090089c4 | 471 | } |
a3d5953d | 472 | debug(50, 2) ("httpReadReply: FD %d: read failure: %s.\n", |
0a0bf5db | 473 | fd, xstrerror()); |
8350fe9b | 474 | } else if (len == 0 && entry->mem_obj->inmem_hi == 0) { |
efb9218c | 475 | if (httpTryRestart(httpState)) { |
b716a8ad | 476 | httpRestart(httpState); |
477 | } else { | |
478 | httpState->eof = 1; | |
fe40a877 | 479 | err = errorCon(ERR_ZERO_SIZE_OBJECT, HTTP_SERVICE_UNAVAILABLE); |
c45ed9ad | 480 | err->xerrno = errno; |
79a15e0a | 481 | err->request = requestLink(httpState->orig_request); |
b716a8ad | 482 | errorAppendEntry(entry, err); |
b716a8ad | 483 | comm_close(fd); |
484 | } | |
090089c4 | 485 | } else if (len == 0) { |
486 | /* Connection closed; retrieval done. */ | |
f86a6a46 | 487 | httpState->eof = 1; |
d1a43e28 | 488 | if (httpState->reply_hdr_state < 2) |
b34ed725 | 489 | /* |
490 | * Yes Henrik, there is a point to doing this. When we | |
491 | * called httpProcessReplyHeader() before, we didn't find | |
492 | * the end of headers, but now we are definately at EOF, so | |
493 | * we want to process the reply headers. | |
494 | */ | |
d1a43e28 | 495 | httpProcessReplyHeader(httpState, buf, len); |
d1a43e28 | 496 | storeComplete(entry); /* deallocates mem_obj->request */ |
0d4d4170 | 497 | comm_close(fd); |
090089c4 | 498 | } else { |
d1a43e28 | 499 | if (httpState->reply_hdr_state < 2) |
30a4f2a8 | 500 | httpProcessReplyHeader(httpState, buf, len); |
620da955 | 501 | storeAppend(entry, buf, len); |
603a02fd | 502 | if (httpPconnTransferDone(httpState)) { |
5b29969a | 503 | /* yes we have to clear all these! */ |
8796b9e9 | 504 | commSetDefer(fd, NULL, NULL); |
5b29969a | 505 | commSetTimeout(fd, -1, NULL, NULL); |
506 | commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); | |
603a02fd | 507 | comm_remove_close_handler(fd, httpStateFree, httpState); |
508 | storeComplete(entry); /* deallocates mem_obj->request */ | |
8796b9e9 | 509 | pconnPush(fd, request->host, request->port); |
603a02fd | 510 | httpState->fd = -1; |
511 | httpStateFree(-1, httpState); | |
512 | } else { | |
513 | commSetSelect(fd, COMM_SELECT_READ, httpReadReply, httpState, 0); | |
514 | } | |
090089c4 | 515 | } |
516 | } | |
517 | ||
518 | /* This will be called when request write is complete. Schedule read of | |
519 | * reply. */ | |
b8d8561b | 520 | static void |
79a15e0a | 521 | httpSendComplete(int fd, char *bufnotused, size_t size, int errflag, void *data) |
090089c4 | 522 | { |
30a4f2a8 | 523 | HttpStateData *httpState = data; |
9b312a19 | 524 | StoreEntry *entry = httpState->entry; |
525 | ErrorState *err; | |
a3d5953d | 526 | debug(11, 5) ("httpSendComplete: FD %d: size %d: errflag %d.\n", |
090089c4 | 527 | fd, size, errflag); |
ee1679df | 528 | if (size > 0) { |
529 | fd_bytes(fd, size, FD_WRITE); | |
a0f32775 | 530 | kb_incr(&Counter.server.all.kbytes_out, size); |
399e85ea | 531 | kb_incr(&Counter.server.http.kbytes_out, size); |
ee1679df | 532 | } |
ea3a2a69 | 533 | if (errflag == COMM_ERR_CLOSING) |
534 | return; | |
090089c4 | 535 | if (errflag) { |
fe40a877 | 536 | err = errorCon(ERR_WRITE_ERROR, HTTP_INTERNAL_SERVER_ERROR); |
c45ed9ad | 537 | err->xerrno = errno; |
79a15e0a | 538 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 539 | errorAppendEntry(entry, err); |
0d4d4170 | 540 | comm_close(fd); |
090089c4 | 541 | return; |
542 | } else { | |
543 | /* Schedule read reply. */ | |
b177367b | 544 | commSetSelect(fd, |
019dd986 | 545 | COMM_SELECT_READ, |
b177367b | 546 | httpReadReply, |
cd1fb0eb | 547 | httpState, 0); |
70a9dab4 | 548 | commSetDefer(fd, protoCheckDeferRead, entry); |
090089c4 | 549 | } |
550 | } | |
551 | ||
6bf8443a | 552 | static void |
88738790 | 553 | httpAppendRequestHeader(char *hdr, const char *line, size_t * sz, size_t max, int check) |
6bf8443a | 554 | { |
555 | size_t n = *sz + strlen(line) + 2; | |
556 | if (n >= max) | |
557 | return; | |
88738790 | 558 | if (check) { |
17a0a4ee | 559 | if (Config.onoff.anonymizer == ANONYMIZER_PARANOID) { |
88738790 | 560 | if (!httpAnonAllowed(line)) |
561 | return; | |
17a0a4ee | 562 | } else if (Config.onoff.anonymizer == ANONYMIZER_STANDARD) { |
88738790 | 563 | if (httpAnonDenied(line)) |
564 | return; | |
565 | } | |
4a83b852 | 566 | } |
4a83b852 | 567 | /* allowed header, explicitly known to be not dangerous */ |
a3d5953d | 568 | debug(11, 5) ("httpAppendRequestHeader: %s\n", line); |
929545fe | 569 | strcpy(hdr + (*sz), line); |
6bf8443a | 570 | strcat(hdr + (*sz), crlf); |
571 | *sz = n; | |
572 | } | |
573 | ||
1294c0fc | 574 | #define YBUF_SZ (MAX_URL+32) |
6bf8443a | 575 | size_t |
576 | httpBuildRequestHeader(request_t * request, | |
577 | request_t * orig_request, | |
578 | StoreEntry * entry, | |
6bf8443a | 579 | size_t * in_len, |
580 | char *hdr_out, | |
581 | size_t out_sz, | |
603a02fd | 582 | int cfd, |
583 | int flags) | |
6bf8443a | 584 | { |
1294c0fc | 585 | LOCAL_ARRAY(char, ybuf, YBUF_SZ); |
ab013258 | 586 | LOCAL_ARRAY(char, no_forward, 1024); |
7021844c | 587 | char *xbuf = memAllocate(MEM_4K_BUF); |
588 | char *viabuf = memAllocate(MEM_4K_BUF); | |
589 | char *fwdbuf = memAllocate(MEM_4K_BUF); | |
6bf8443a | 590 | char *t = NULL; |
591 | char *s = NULL; | |
592 | char *end = NULL; | |
593 | size_t len = 0; | |
594 | size_t hdr_len = 0; | |
6bf8443a | 595 | size_t l; |
596 | int hdr_flags = 0; | |
151a0b6d | 597 | int cc_flags = 0; |
b3b64e58 | 598 | int n; |
6bf8443a | 599 | const char *url = NULL; |
2357f74a | 600 | char *hdr_in = orig_request->headers; |
6bf8443a | 601 | |
2357f74a | 602 | assert(hdr_in != NULL); |
a3d5953d | 603 | debug(11, 3) ("httpBuildRequestHeader: INPUT:\n%s\n", hdr_in); |
6bf8443a | 604 | xstrncpy(fwdbuf, "X-Forwarded-For: ", 4096); |
605 | xstrncpy(viabuf, "Via: ", 4096); | |
1294c0fc | 606 | snprintf(ybuf, YBUF_SZ, "%s %s HTTP/1.0", |
6bf8443a | 607 | RequestMethodStr[request->method], |
02922e76 | 608 | strLen(request->urlpath) ? strBuf(request->urlpath) : "/"); |
88738790 | 609 | httpAppendRequestHeader(hdr_out, ybuf, &len, out_sz, 1); |
6bf8443a | 610 | /* Add IMS header */ |
611 | if (entry && entry->lastmod && request->method == METHOD_GET) { | |
1294c0fc | 612 | snprintf(ybuf, YBUF_SZ, "If-Modified-Since: %s", mkrfc1123(entry->lastmod)); |
88738790 | 613 | httpAppendRequestHeader(hdr_out, ybuf, &len, out_sz, 1); |
6bf8443a | 614 | EBIT_SET(hdr_flags, HDR_IMS); |
615 | } | |
00c59270 | 616 | end = mime_headers_end(hdr_in); |
6bf8443a | 617 | for (t = hdr_in; t < end; t += strcspn(t, crlf), t += strspn(t, crlf)) { |
618 | hdr_len = t - hdr_in; | |
6bf8443a | 619 | l = strcspn(t, crlf) + 1; |
620 | if (l > 4096) | |
621 | l = 4096; | |
622 | xstrncpy(xbuf, t, l); | |
a3d5953d | 623 | debug(11, 5) ("httpBuildRequestHeader: %s\n", xbuf); |
6bf8443a | 624 | if (strncasecmp(xbuf, "Proxy-Connection:", 17) == 0) |
625 | continue; | |
88738790 | 626 | if (strncasecmp(xbuf, "Proxy-authorization:", 20) == 0) |
afe95a7e | 627 | /* If we're not going to do proxy auth, then it must be passed on */ |
79a15e0a | 628 | if (EBIT_TEST(request->flags, REQ_USED_PROXY_AUTH)) |
88738790 | 629 | continue; |
ab013258 | 630 | if (strncasecmp(xbuf, "Connection:", 11) == 0) { |
067bea91 | 631 | handleConnectionHeader(0, no_forward, &xbuf[11]); |
6bf8443a | 632 | continue; |
067bea91 | 633 | } |
66f7337b | 634 | if (strncasecmp(xbuf, "Host:", 5) == 0) { |
6bf8443a | 635 | EBIT_SET(hdr_flags, HDR_HOST); |
66f7337b | 636 | } else if (strncasecmp(xbuf, "Cache-Control:", 14) == 0) { |
6bf8443a | 637 | for (s = xbuf + 14; *s && isspace(*s); s++); |
638 | if (strncasecmp(s, "Max-age=", 8) == 0) | |
151a0b6d | 639 | EBIT_SET(cc_flags, CCC_MAXAGE); |
66f7337b | 640 | } else if (strncasecmp(xbuf, "Via:", 4) == 0) { |
6bf8443a | 641 | for (s = xbuf + 4; *s && isspace(*s); s++); |
69e81830 | 642 | if ((int) strlen(viabuf) + (int) strlen(s) < 4000) |
6bf8443a | 643 | strcat(viabuf, s); |
644 | strcat(viabuf, ", "); | |
645 | continue; | |
66f7337b | 646 | } else if (strncasecmp(xbuf, "X-Forwarded-For:", 16) == 0) { |
6bf8443a | 647 | for (s = xbuf + 16; *s && isspace(*s); s++); |
69e81830 | 648 | if ((int) strlen(fwdbuf) + (int) strlen(s) < 4000) |
6bf8443a | 649 | strcat(fwdbuf, s); |
650 | strcat(fwdbuf, ", "); | |
651 | continue; | |
66f7337b | 652 | } else if (strncasecmp(xbuf, "If-Modified-Since:", 18) == 0) { |
6bf8443a | 653 | if (EBIT_TEST(hdr_flags, HDR_IMS)) |
654 | continue; | |
b3b64e58 | 655 | } else if (strncasecmp(xbuf, "Max-Forwards:", 13) == 0) { |
656 | if (orig_request->method == METHOD_TRACE) { | |
657 | for (s = xbuf + 13; *s && isspace(*s); s++); | |
658 | n = atoi(s); | |
56878878 | 659 | snprintf(xbuf, 4096, "Max-Forwards: %d", n - 1); |
b3b64e58 | 660 | } |
66f7337b | 661 | } |
067bea91 | 662 | if (!handleConnectionHeader(1, no_forward, xbuf)) |
ab013258 | 663 | httpAppendRequestHeader(hdr_out, xbuf, &len, out_sz - 512, 1); |
88738790 | 664 | } |
665 | hdr_len = t - hdr_in; | |
666 | if (Config.fake_ua && strstr(hdr_out, "User-Agent") == NULL) { | |
1294c0fc | 667 | snprintf(ybuf, YBUF_SZ, "User-Agent: %s", Config.fake_ua); |
88738790 | 668 | httpAppendRequestHeader(hdr_out, ybuf, &len, out_sz, 0); |
6bf8443a | 669 | } |
56878878 | 670 | /* Append Via: */ |
671 | /* snprintf would fail here too */ | |
1294c0fc | 672 | snprintf(ybuf, YBUF_SZ, "%3.1f %s", orig_request->http_ver, ThisCache); |
6bf8443a | 673 | strcat(viabuf, ybuf); |
88738790 | 674 | httpAppendRequestHeader(hdr_out, viabuf, &len, out_sz, 1); |
6bf8443a | 675 | /* Append to X-Forwarded-For: */ |
a08307eb | 676 | strcat(fwdbuf, cfd < 0 ? "unknown" : fd_table[cfd].ipaddr); |
88738790 | 677 | httpAppendRequestHeader(hdr_out, fwdbuf, &len, out_sz, 1); |
6bf8443a | 678 | if (!EBIT_TEST(hdr_flags, HDR_HOST)) { |
1294c0fc | 679 | snprintf(ybuf, YBUF_SZ, "Host: %s", orig_request->host); |
88738790 | 680 | httpAppendRequestHeader(hdr_out, ybuf, &len, out_sz, 1); |
6bf8443a | 681 | } |
151a0b6d | 682 | if (!EBIT_TEST(cc_flags, CCC_MAXAGE)) { |
9fb13bb6 | 683 | url = entry ? storeUrl(entry) : urlCanonical(orig_request, NULL); |
1294c0fc | 684 | snprintf(ybuf, YBUF_SZ, "Cache-control: Max-age=%d", (int) getMaxAge(url)); |
88738790 | 685 | httpAppendRequestHeader(hdr_out, ybuf, &len, out_sz, 1); |
02922e76 | 686 | if (strLen(request->urlpath)) |
687 | assert(strstr(url, strBuf(request->urlpath))); | |
6bf8443a | 688 | } |
603a02fd | 689 | /* maybe append Connection: Keep-Alive */ |
79a15e0a | 690 | if (EBIT_TEST(flags, HTTP_KEEPALIVE)) { |
691 | if (EBIT_TEST(flags, HTTP_PROXYING)) { | |
1294c0fc | 692 | snprintf(ybuf, YBUF_SZ, "Proxy-Connection: Keep-Alive"); |
603a02fd | 693 | } else { |
1294c0fc | 694 | snprintf(ybuf, YBUF_SZ, "Connection: Keep-Alive"); |
603a02fd | 695 | } |
696 | httpAppendRequestHeader(hdr_out, ybuf, &len, out_sz, 1); | |
697 | } | |
88738790 | 698 | httpAppendRequestHeader(hdr_out, null_string, &len, out_sz, 1); |
3f6c0fb2 | 699 | memFree(MEM_4K_BUF, xbuf); |
700 | memFree(MEM_4K_BUF, viabuf); | |
701 | memFree(MEM_4K_BUF, fwdbuf); | |
6bf8443a | 702 | if (in_len) |
703 | *in_len = hdr_len; | |
9d9d144b | 704 | if ((l = strlen(hdr_out)) != len) { |
705 | debug_trap("httpBuildRequestHeader: size mismatch"); | |
706 | len = l; | |
707 | } | |
a3d5953d | 708 | debug(11, 3) ("httpBuildRequestHeader: OUTPUT:\n%s\n", hdr_out); |
6bf8443a | 709 | return len; |
710 | } | |
711 | ||
090089c4 | 712 | /* This will be called when connect completes. Write request. */ |
b8d8561b | 713 | static void |
b177367b | 714 | httpSendRequest(int fd, void *data) |
090089c4 | 715 | { |
b177367b | 716 | HttpStateData *httpState = data; |
090089c4 | 717 | char *buf = NULL; |
090089c4 | 718 | int len = 0; |
719 | int buflen; | |
30a4f2a8 | 720 | request_t *req = httpState->request; |
9864ee44 | 721 | int buftype = 0; |
620da955 | 722 | StoreEntry *entry = httpState->entry; |
2a26c096 | 723 | int cfd; |
1294c0fc | 724 | peer *p = httpState->peer; |
901e234d | 725 | CWCB *sendHeaderDone; |
090089c4 | 726 | |
a3d5953d | 727 | debug(11, 5) ("httpSendRequest: FD %d: httpState %p.\n", fd, httpState); |
02922e76 | 728 | buflen = strLen(req->urlpath); |
62dec5a7 | 729 | if (req->headers) |
730 | buflen += req->headers_sz + 1; | |
090089c4 | 731 | buflen += 512; /* lots of extra */ |
732 | ||
efb9218c | 733 | if (pumpMethod(req->method)) |
7db8b16d | 734 | sendHeaderDone = httpSendRequestEntry; |
735 | else | |
736 | sendHeaderDone = httpSendComplete; | |
54220df8 | 737 | |
30a4f2a8 | 738 | if (buflen < DISK_PAGE_SIZE) { |
7021844c | 739 | buf = memAllocate(MEM_8K_BUF); |
9864ee44 | 740 | buftype = BUF_TYPE_8K; |
6bf8443a | 741 | buflen = DISK_PAGE_SIZE; |
30a4f2a8 | 742 | } else { |
9864ee44 | 743 | buf = xcalloc(buflen, 1); |
744 | buftype = BUF_TYPE_MALLOC; | |
090089c4 | 745 | } |
2a26c096 | 746 | if (!opt_forwarded_for) |
6bf8443a | 747 | cfd = -1; |
2a26c096 | 748 | else if (entry->mem_obj == NULL) |
6bf8443a | 749 | cfd = -1; |
2a26c096 | 750 | else |
382d851a | 751 | cfd = entry->mem_obj->fd; |
1294c0fc | 752 | if (p != NULL) |
79a15e0a | 753 | EBIT_SET(httpState->flags, HTTP_PROXYING); |
efb9218c | 754 | /* |
755 | * Is Keepalive okay for all request methods? | |
756 | */ | |
757 | if (p == NULL) | |
758 | EBIT_SET(httpState->flags, HTTP_KEEPALIVE); | |
759 | else if (p->stats.n_keepalives_sent < 10) | |
760 | EBIT_SET(httpState->flags, HTTP_KEEPALIVE); | |
761 | else if ((double) p->stats.n_keepalives_recv / (double) p->stats.n_keepalives_sent > 0.50) | |
762 | EBIT_SET(httpState->flags, HTTP_KEEPALIVE); | |
6bf8443a | 763 | len = httpBuildRequestHeader(req, |
79a15e0a | 764 | httpState->orig_request, |
6bf8443a | 765 | entry, |
6bf8443a | 766 | NULL, |
767 | buf, | |
768 | buflen, | |
603a02fd | 769 | cfd, |
770 | httpState->flags); | |
a3d5953d | 771 | debug(11, 6) ("httpSendRequest: FD %d:\n%s\n", fd, buf); |
30a4f2a8 | 772 | comm_write(fd, |
14e59844 | 773 | buf, |
774 | len, | |
54220df8 | 775 | sendHeaderDone, |
9864ee44 | 776 | httpState, |
3f6c0fb2 | 777 | buftype == BUF_TYPE_8K ? memFree8K : xfree); |
090089c4 | 778 | } |
779 | ||
603a02fd | 780 | static int |
b716a8ad | 781 | httpSocketOpen(StoreEntry * entry, request_t * request) |
090089c4 | 782 | { |
9e4ad609 | 783 | int fd; |
9b312a19 | 784 | ErrorState *err; |
9e4ad609 | 785 | fd = comm_open(SOCK_STREAM, |
16b204c4 | 786 | 0, |
787 | Config.Addrs.tcp_outgoing, | |
788 | 0, | |
789 | COMM_NONBLOCKING, | |
9fb13bb6 | 790 | storeUrl(entry)); |
603a02fd | 791 | if (fd < 0) { |
79a15e0a | 792 | debug(50, 4) ("httpSocketOpen: %s\n", xstrerror()); |
fe40a877 | 793 | err = errorCon(ERR_SOCKET_FAILURE, HTTP_INTERNAL_SERVER_ERROR); |
c45ed9ad | 794 | err->xerrno = errno; |
79a15e0a | 795 | err->request = requestLink(request); |
9b312a19 | 796 | errorAppendEntry(entry, err); |
090089c4 | 797 | } |
603a02fd | 798 | return fd; |
799 | } | |
800 | ||
801 | static HttpStateData * | |
802 | httpBuildState(int fd, StoreEntry * entry, request_t * orig_request, peer * e) | |
803 | { | |
cb87dab6 | 804 | HttpStateData *httpState = memAllocate(MEM_HTTP_STATE_DATA); |
603a02fd | 805 | request_t *request; |
770f051d | 806 | storeLockObject(entry); |
cb87dab6 | 807 | cbdataAdd(httpState, MEM_HTTP_STATE_DATA); |
0a0bf5db | 808 | httpState->entry = entry; |
9e4ad609 | 809 | httpState->fd = fd; |
603a02fd | 810 | if (e) { |
7021844c | 811 | request = memAllocate(MEM_REQUEST_T); |
603a02fd | 812 | request->method = orig_request->method; |
813 | xstrncpy(request->host, e->host, SQUIDHOSTNAMELEN); | |
814 | request->port = e->http_port; | |
02922e76 | 815 | stringReset(&request->urlpath, storeUrl(entry)); |
603a02fd | 816 | httpState->request = requestLink(request); |
1294c0fc | 817 | httpState->peer = e; |
603a02fd | 818 | httpState->orig_request = requestLink(orig_request); |
79a15e0a | 819 | EBIT_SET(request->flags, REQ_PROXYING); |
603a02fd | 820 | } else { |
821 | httpState->request = requestLink(orig_request); | |
79a15e0a | 822 | httpState->orig_request = requestLink(orig_request); |
603a02fd | 823 | } |
0d4d4170 | 824 | /* register the handler to free HTTP state data when the FD closes */ |
603a02fd | 825 | comm_add_close_handler(httpState->fd, httpStateFree, httpState); |
e102ebda | 826 | storeRegisterAbort(entry, httpAbort, httpState); |
603a02fd | 827 | return httpState; |
828 | } | |
829 | ||
830 | void | |
831 | httpStart(request_t * request, StoreEntry * entry, peer * e) | |
832 | { | |
833 | HttpStateData *httpState; | |
834 | int fd; | |
835 | debug(11, 3) ("httpStart: \"%s %s\"\n", | |
9fb13bb6 | 836 | RequestMethodStr[request->method], storeUrl(entry)); |
a0f32775 | 837 | Counter.server.all.requests++; |
838 | Counter.server.http.requests++; | |
603a02fd | 839 | if (e) { |
a369131d | 840 | if (EBIT_TEST(e->options, NEIGHBOR_PROXY_ONLY)) |
603a02fd | 841 | storeReleaseRequest(entry); |
603a02fd | 842 | if ((fd = pconnPop(e->host, e->http_port)) >= 0) { |
51fdcbd5 | 843 | debug(11, 3) ("httpStart: reusing pconn FD %d\n", fd); |
603a02fd | 844 | httpState = httpBuildState(fd, entry, request, e); |
b716a8ad | 845 | commSetTimeout(httpState->fd, |
846 | Config.Timeout.connect, | |
847 | httpTimeout, | |
848 | httpState); | |
603a02fd | 849 | httpConnectDone(fd, COMM_OK, httpState); |
850 | return; | |
851 | } | |
852 | } else { | |
853 | if ((fd = pconnPop(request->host, request->port)) >= 0) { | |
51fdcbd5 | 854 | debug(11, 3) ("httpStart: reusing pconn FD %d\n", fd); |
603a02fd | 855 | httpState = httpBuildState(fd, entry, request, e); |
b716a8ad | 856 | commSetTimeout(httpState->fd, |
857 | Config.Timeout.connect, | |
858 | httpTimeout, | |
859 | httpState); | |
603a02fd | 860 | httpConnectDone(fd, COMM_OK, httpState); |
861 | return; | |
862 | } | |
863 | } | |
79a15e0a | 864 | if ((fd = httpSocketOpen(entry, request)) < 0) |
603a02fd | 865 | return; |
866 | httpState = httpBuildState(fd, entry, request, e); | |
603a02fd | 867 | commSetTimeout(httpState->fd, |
868 | Config.Timeout.connect, | |
869 | httpTimeout, | |
cd1fb0eb | 870 | httpState); |
edeb28fd | 871 | commConnectStart(httpState->fd, |
603a02fd | 872 | httpState->request->host, |
873 | httpState->request->port, | |
e924600d | 874 | httpConnectDone, |
875 | httpState); | |
e5f6c5c2 | 876 | } |
877 | ||
efb9218c | 878 | static int |
879 | httpTryRestart(HttpStateData * httpState) | |
880 | { | |
881 | /* | |
882 | * We only retry the request if it looks like it was | |
883 | * on a persistent/pipelined connection | |
884 | */ | |
885 | if (fd_table[httpState->fd].uses < 2) | |
886 | return 0; | |
887 | if (pumpMethod(httpState->orig_request->method)) | |
888 | if (0 == pumpRestart(httpState->orig_request)) | |
889 | return 0; | |
890 | return 1; | |
891 | } | |
892 | ||
b716a8ad | 893 | static void |
894 | httpRestart(HttpStateData * httpState) | |
895 | { | |
896 | /* restart a botched request from a persistent connection */ | |
9fb13bb6 | 897 | debug(11, 2) ("Retrying HTTP request for %s\n", storeUrl(httpState->entry)); |
901e234d | 898 | if (pumpMethod(httpState->orig_request->method)) { |
efb9218c | 899 | debug(11, 1) ("Potential Coredump: httpRestart %s %s\n", |
900 | RequestMethodStr[httpState->orig_request->method], | |
901 | storeUrl(httpState->entry)); | |
e371ed5c | 902 | } |
b716a8ad | 903 | if (httpState->fd >= 0) { |
904 | comm_remove_close_handler(httpState->fd, httpStateFree, httpState); | |
905 | comm_close(httpState->fd); | |
906 | httpState->fd = -1; | |
907 | } | |
79a15e0a | 908 | httpState->fd = httpSocketOpen(httpState->entry, httpState->orig_request); |
b716a8ad | 909 | if (httpState->fd < 0) |
910 | return; | |
911 | comm_add_close_handler(httpState->fd, httpStateFree, httpState); | |
912 | commSetTimeout(httpState->fd, | |
913 | Config.Timeout.connect, | |
914 | httpTimeout, | |
915 | httpState); | |
916 | commConnectStart(httpState->fd, | |
917 | httpState->request->host, | |
918 | httpState->request->port, | |
919 | httpConnectDone, | |
920 | httpState); | |
921 | } | |
922 | ||
e5f6c5c2 | 923 | static void |
924 | httpConnectDone(int fd, int status, void *data) | |
925 | { | |
926 | HttpStateData *httpState = data; | |
927 | request_t *request = httpState->request; | |
928 | StoreEntry *entry = httpState->entry; | |
9b312a19 | 929 | ErrorState *err; |
edeb28fd | 930 | if (status == COMM_ERR_DNS) { |
a3d5953d | 931 | debug(11, 4) ("httpConnectDone: Unknown host: %s\n", request->host); |
fe40a877 | 932 | err = errorCon(ERR_DNS_FAIL, HTTP_SERVICE_UNAVAILABLE); |
9b312a19 | 933 | err->dnsserver_msg = xstrdup(dns_error_message); |
79a15e0a | 934 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 935 | errorAppendEntry(entry, err); |
edeb28fd | 936 | comm_close(fd); |
937 | } else if (status != COMM_OK) { | |
fe40a877 | 938 | err = errorCon(ERR_CONNECT_FAIL, HTTP_SERVICE_UNAVAILABLE); |
c45ed9ad | 939 | err->xerrno = errno; |
9b312a19 | 940 | err->host = xstrdup(request->host); |
941 | err->port = request->port; | |
79a15e0a | 942 | err->request = requestLink(httpState->orig_request); |
9b312a19 | 943 | errorAppendEntry(entry, err); |
1294c0fc | 944 | if (httpState->peer) |
945 | peerCheckConnectStart(httpState->peer); | |
e5f6c5c2 | 946 | comm_close(fd); |
947 | } else { | |
9fb13bb6 | 948 | fd_note(fd, storeUrl(entry)); |
b716a8ad | 949 | fd_table[fd].uses++; |
bfcaf585 | 950 | commSetSelect(fd, COMM_SELECT_WRITE, httpSendRequest, httpState, 0); |
090089c4 | 951 | } |
090089c4 | 952 | } |
953 | ||
bfcaf585 | 954 | static void |
955 | httpAbort(void *data) | |
956 | { | |
957 | HttpStateData *httpState = data; | |
9fb13bb6 | 958 | debug(11, 2) ("httpAbort: %s\n", storeUrl(httpState->entry)); |
bfcaf585 | 959 | comm_close(httpState->fd); |
960 | } | |
9b312a19 | 961 | |
54220df8 | 962 | static void |
7db8b16d | 963 | httpSendRequestEntry(int fd, char *bufnotused, size_t size, int errflag, void *data) |
54220df8 | 964 | { |
965 | HttpStateData *httpState = data; | |
966 | StoreEntry *entry = httpState->entry; | |
967 | ErrorState *err; | |
968 | debug(11, 5) ("httpSendRequestEntry: FD %d: size %d: errflag %d.\n", | |
7db8b16d | 969 | fd, size, errflag); |
54220df8 | 970 | if (size > 0) { |
7db8b16d | 971 | fd_bytes(fd, size, FD_WRITE); |
54220df8 | 972 | kb_incr(&Counter.server.all.kbytes_out, size); |
973 | kb_incr(&Counter.server.http.kbytes_out, size); | |
974 | } | |
975 | if (errflag == COMM_ERR_CLOSING) | |
7db8b16d | 976 | return; |
54220df8 | 977 | if (errflag) { |
7db8b16d | 978 | err = errorCon(ERR_WRITE_ERROR, HTTP_INTERNAL_SERVER_ERROR); |
979 | err->xerrno = errno; | |
980 | err->request = requestLink(httpState->orig_request); | |
981 | errorAppendEntry(entry, err); | |
982 | comm_close(fd); | |
983 | return; | |
54220df8 | 984 | } |
7db8b16d | 985 | pumpStart(fd, entry, httpState->orig_request, httpSendComplete, httpState); |
54220df8 | 986 | } |