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