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