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