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