]> git.ipfire.org Git - thirdparty/squid.git/blob - src/client_side.cc
Persistent connection bugfix from Chris DeCenzo <decenzo at liberate.com>
[thirdparty/squid.git] / src / client_side.cc
1
2 /*
3 * $Id: client_side.cc,v 1.496 2000/08/03 21:27:54 hno Exp $
4 *
5 * DEBUG: section 33 Client-side Routines
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
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 the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37
38 #if IPF_TRANSPARENT
39 #if HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 #include <netinet/tcp.h>
43 #include <net/if.h>
44 #if HAVE_IP_FIL_COMPAT_H
45 #include <ip_fil_compat.h>
46 #elif HAVE_NETINET_IP_FIL_COMPAT_H
47 #include <netinet/ip_fil_compat.h>
48 #elif HAVE_IP_COMPAT_H
49 #include <ip_compat.h>
50 #elif HAVE_NETINET_IP_COMPAT_H
51 #include <netinet/ip_compat.h>
52 #endif
53 #if HAVE_IP_FIL_H
54 #include <ip_fil.h>
55 #elif HAVE_NETINET_IP_FIL_H
56 #include <netinet/ip_fil.h>
57 #endif
58 #if HAVE_IP_NAT_H
59 #include <ip_nat.h>
60 #elif HAVE_NETINET_IP_NAT_H
61 #include <netinet/ip_nat.h>
62 #endif
63 #endif
64
65
66
67 #if LINGERING_CLOSE
68 #define comm_close comm_lingering_close
69 #endif
70
71 static const char *const crlf = "\r\n";
72
73 #define REQUEST_BUF_SIZE 4096
74 #define FAILURE_MODE_TIME 300
75
76 /* Local functions */
77
78 static CWCB clientWriteComplete;
79 static CWCB clientWriteBodyComplete;
80 static PF clientReadRequest;
81 static PF connStateFree;
82 static PF requestTimeout;
83 static int clientCheckTransferDone(clientHttpRequest *);
84 static int clientGotNotEnough(clientHttpRequest *);
85 static void checkFailureRatio(err_type, hier_code);
86 static void clientProcessMiss(clientHttpRequest *);
87 static void clientBuildReplyHeader(clientHttpRequest * http, HttpReply * rep);
88 static clientHttpRequest *parseHttpRequestAbort(ConnStateData * conn, const char *uri);
89 static clientHttpRequest *parseHttpRequest(ConnStateData *, method_t *, int *, char **, size_t *);
90 static RH clientRedirectDone;
91 static void clientCheckNoCache(clientHttpRequest *);
92 static void clientCheckNoCacheDone(int answer, void *data);
93 static STCB clientHandleIMSReply;
94 static int clientGetsOldEntry(StoreEntry * new, StoreEntry * old, request_t * request);
95 static int checkAccelOnly(clientHttpRequest *);
96 #if USE_IDENT
97 static IDCB clientIdentDone;
98 #endif
99 static int clientOnlyIfCached(clientHttpRequest * http);
100 static STCB clientSendMoreData;
101 static STCB clientCacheHit;
102 static void clientSetKeepaliveFlag(clientHttpRequest *);
103 static void clientPackRangeHdr(const HttpReply * rep, const HttpHdrRangeSpec * spec, String boundary, MemBuf * mb);
104 static void clientPackTermBound(String boundary, MemBuf * mb);
105 static void clientInterpretRequestHeaders(clientHttpRequest *);
106 static void clientProcessRequest(clientHttpRequest *);
107 static void clientProcessExpired(void *data);
108 static void clientProcessOnlyIfCachedMiss(clientHttpRequest * http);
109 static int clientCachable(clientHttpRequest * http);
110 static int clientHierarchical(clientHttpRequest * http);
111 static int clientCheckContentLength(request_t * r);
112 static DEFER httpAcceptDefer;
113 static log_type clientProcessRequest2(clientHttpRequest * http);
114 static int clientReplyBodyTooLarge(int clen);
115 static int clientRequestBodyTooLarge(int clen);
116
117 static int
118 checkAccelOnly(clientHttpRequest * http)
119 {
120 /* return TRUE if someone makes a proxy request to us and
121 * we are in httpd-accel only mode */
122 if (!Config2.Accel.on)
123 return 0;
124 if (Config.onoff.accel_with_proxy)
125 return 0;
126 if (http->request->protocol == PROTO_CACHEOBJ)
127 return 0;
128 if (http->flags.accel)
129 return 0;
130 return 1;
131 }
132
133 #if USE_IDENT
134 static void
135 clientIdentDone(const char *ident, void *data)
136 {
137 ConnStateData *conn = data;
138 if (ident)
139 xstrncpy(conn->ident, ident, sizeof(conn->ident));
140 else
141 xstrncpy(conn->ident, "-", sizeof(conn->ident));
142 }
143 #endif
144
145 static aclCheck_t *
146 clientAclChecklistCreate(const acl_access * acl, const clientHttpRequest * http)
147 {
148 aclCheck_t *ch;
149 ConnStateData *conn = http->conn;
150 ch = aclChecklistCreate(acl,
151 http->request,
152 conn->ident);
153 #if USE_IDENT
154 /*
155 * hack for ident ACL. It needs to get full addresses, and a
156 * place to store the ident result on persistent connections...
157 */
158 ch->conn = conn;
159 cbdataLock(ch->conn);
160 #endif
161 return ch;
162 }
163
164 void
165 clientAccessCheck(void *data)
166 {
167 clientHttpRequest *http = data;
168 if (checkAccelOnly(http)) {
169 clientAccessCheckDone(ACCESS_ALLOWED, http);
170 return;
171 }
172 http->acl_checklist = clientAclChecklistCreate(Config.accessList.http, http);
173 aclNBCheck(http->acl_checklist, clientAccessCheckDone, http);
174 }
175
176 /*
177 * returns true if client specified that the object must come from the cache
178 * witout contacting origin server
179 */
180 static int
181 clientOnlyIfCached(clientHttpRequest * http)
182 {
183 const request_t *r = http->request;
184 assert(r);
185 return r->cache_control &&
186 EBIT_TEST(r->cache_control->mask, CC_ONLY_IF_CACHED);
187 }
188
189 StoreEntry *
190 clientCreateStoreEntry(clientHttpRequest * h, method_t m, request_flags flags)
191 {
192 StoreEntry *e;
193 /*
194 * For erroneous requests, we might not have a h->request,
195 * so make a fake one.
196 */
197 if (h->request == NULL)
198 h->request = requestLink(requestCreate(m, PROTO_NONE, null_string));
199 e = storeCreateEntry(h->uri, h->log_uri, flags, m);
200 h->sc = storeClientListAdd(e, h);
201 #if DELAY_POOLS
202 delaySetStoreClient(h->sc, delayClient(h->request));
203 #endif
204 storeClientCopy(h->sc, e, 0, 0, CLIENT_SOCK_SZ,
205 memAllocate(MEM_CLIENT_SOCK_BUF), clientSendMoreData, h);
206 return e;
207 }
208
209 void
210 clientAccessCheckDone(int answer, void *data)
211 {
212 clientHttpRequest *http = data;
213 int page_id = -1;
214 http_status status;
215 ErrorState *err = NULL;
216 debug(33, 2) ("The request %s %s is %s, because it matched '%s'\n",
217 RequestMethodStr[http->request->method], http->uri,
218 answer == ACCESS_ALLOWED ? "ALLOWED" : "DENIED",
219 AclMatchedName ? AclMatchedName : "NO ACL's");
220 http->acl_checklist = NULL;
221 if (answer == ACCESS_ALLOWED) {
222 safe_free(http->uri);
223 http->uri = xstrdup(urlCanonical(http->request));
224 assert(http->redirect_state == REDIRECT_NONE);
225 http->redirect_state = REDIRECT_PENDING;
226 redirectStart(http, clientRedirectDone, http);
227 } else {
228 debug(33, 5) ("Access Denied: %s\n", http->uri);
229 debug(33, 5) ("AclMatchedName = %s\n",
230 AclMatchedName ? AclMatchedName : "<null>");
231 /*
232 * NOTE: get page_id here, based on AclMatchedName because
233 * if USE_DELAY_POOLS is enabled, then AclMatchedName gets
234 * clobbered in the clientCreateStoreEntry() call
235 * just below. Pedro Ribeiro <pribeiro@isel.pt>
236 */
237 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName);
238 http->log_type = LOG_TCP_DENIED;
239 http->entry = clientCreateStoreEntry(http, http->request->method,
240 null_request_flags);
241 if (answer == ACCESS_REQ_PROXY_AUTH || aclIsProxyAuth(AclMatchedName)) {
242 if (!http->flags.accel) {
243 /* Proxy authorisation needed */
244 status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
245 } else {
246 /* WWW authorisation needed */
247 status = HTTP_UNAUTHORIZED;
248 }
249 if (page_id <= 0)
250 page_id = ERR_CACHE_ACCESS_DENIED;
251 } else {
252 status = HTTP_FORBIDDEN;
253 if (page_id <= 0)
254 page_id = ERR_ACCESS_DENIED;
255 }
256 err = errorCon(page_id, status);
257 err->request = requestLink(http->request);
258 err->src_addr = http->conn->peer.sin_addr;
259 errorAppendEntry(http->entry, err);
260 }
261 }
262
263 static void
264 clientRedirectDone(void *data, char *result)
265 {
266 clientHttpRequest *http = data;
267 request_t *new_request = NULL;
268 request_t *old_request = http->request;
269 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http->uri,
270 result ? result : "NULL");
271 assert(http->redirect_state == REDIRECT_PENDING);
272 http->redirect_state = REDIRECT_DONE;
273 if (result) {
274 http_status status = atoi(result);
275 if (status == 301 || status == 302) {
276 char *t = result;
277 if ((t = strchr(result, ':')) != NULL) {
278 http->redirect.status = status;
279 http->redirect.location = xstrdup(t + 1);
280 } else {
281 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result);
282 }
283 }
284 if (strcmp(result, http->uri))
285 new_request = urlParse(old_request->method, result);
286 }
287 if (new_request) {
288 safe_free(http->uri);
289 http->uri = xstrdup(urlCanonical(new_request));
290 new_request->http_ver = old_request->http_ver;
291 httpHeaderAppend(&new_request->header, &old_request->header);
292 new_request->client_addr = old_request->client_addr;
293 new_request->my_addr = old_request->my_addr;
294 new_request->my_port = old_request->my_port;
295 new_request->flags.redirected = 1;
296 if (old_request->user_ident[0])
297 xstrncpy(new_request->user_ident, old_request->user_ident,
298 USER_IDENT_SZ);
299 if (old_request->body) {
300 new_request->body = xmalloc(old_request->body_sz);
301 xmemcpy(new_request->body, old_request->body, old_request->body_sz);
302 new_request->body_sz = old_request->body_sz;
303 }
304 new_request->content_length = old_request->content_length;
305 new_request->flags.proxy_keepalive = old_request->flags.proxy_keepalive;
306 requestUnlink(old_request);
307 http->request = requestLink(new_request);
308 }
309 clientInterpretRequestHeaders(http);
310 #if HEADERS_LOG
311 headersLog(0, 1, request->method, request);
312 #endif
313 fd_note(http->conn->fd, http->uri);
314 clientCheckNoCache(http);
315 }
316
317 static void
318 clientCheckNoCache(clientHttpRequest * http)
319 {
320 if (Config.accessList.noCache && http->request->flags.cachable) {
321 http->acl_checklist = clientAclChecklistCreate(Config.accessList.noCache, http);
322 aclNBCheck(http->acl_checklist, clientCheckNoCacheDone, http);
323 } else {
324 clientCheckNoCacheDone(http->request->flags.cachable, http);
325 }
326 }
327
328 void
329 clientCheckNoCacheDone(int answer, void *data)
330 {
331 clientHttpRequest *http = data;
332 http->request->flags.cachable = answer;
333 http->acl_checklist = NULL;
334 clientProcessRequest(http);
335 }
336
337 static void
338 clientProcessExpired(void *data)
339 {
340 clientHttpRequest *http = data;
341 char *url = http->uri;
342 StoreEntry *entry = NULL;
343 debug(33, 3) ("clientProcessExpired: '%s'\n", http->uri);
344 assert(http->entry->lastmod >= 0);
345 /*
346 * check if we are allowed to contact other servers
347 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
348 * a stale entry *if* it matches client requirements
349 */
350 if (clientOnlyIfCached(http)) {
351 clientProcessOnlyIfCachedMiss(http);
352 return;
353 }
354 http->request->flags.refresh = 1;
355 http->old_entry = http->entry;
356 http->old_sc = http->sc;
357 /*
358 * Assert that 'http' is already a client of old_entry. If
359 * it is not, then the beginning of the object data might get
360 * freed from memory before we need to access it.
361 */
362 assert(http->sc->callback_data == http);
363 entry = storeCreateEntry(url,
364 http->log_uri,
365 http->request->flags,
366 http->request->method);
367 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
368 http->sc = storeClientListAdd(entry, http);
369 #if DELAY_POOLS
370 /* delay_id is already set on original store client */
371 delaySetStoreClient(http->sc, delayClient(http->request));
372 #endif
373 http->request->lastmod = http->old_entry->lastmod;
374 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry->lastmod);
375 http->entry = entry;
376 http->out.offset = 0;
377 fwdStart(http->conn->fd, http->entry, http->request);
378 /* Register with storage manager to receive updates when data comes in. */
379 if (EBIT_TEST(entry->flags, ENTRY_ABORTED))
380 debug(33, 0) ("clientProcessExpired: found ENTRY_ABORTED object\n");
381 storeClientCopy(http->sc, entry,
382 http->out.offset,
383 http->out.offset,
384 CLIENT_SOCK_SZ,
385 memAllocate(MEM_CLIENT_SOCK_BUF),
386 clientHandleIMSReply,
387 http);
388 }
389
390 static int
391 clientGetsOldEntry(StoreEntry * new_entry, StoreEntry * old_entry, request_t * request)
392 {
393 const http_status status = new_entry->mem_obj->reply->sline.status;
394 if (0 == status) {
395 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
396 return 1;
397 }
398 /* If the reply is a failure then send the old object as a last
399 * resort */
400 if (status >= 500 && status < 600) {
401 debug(33, 3) ("clientGetsOldEntry: YES, failure reply=%d\n", status);
402 return 1;
403 }
404 /* If the reply is anything but "Not Modified" then
405 * we must forward it to the client */
406 if (HTTP_NOT_MODIFIED != status) {
407 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status);
408 return 0;
409 }
410 /* If the client did not send IMS in the request, then it
411 * must get the old object, not this "Not Modified" reply */
412 if (!request->flags.ims) {
413 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
414 return 1;
415 }
416 /* If the client IMS time is prior to the entry LASTMOD time we
417 * need to send the old object */
418 if (modifiedSince(old_entry, request)) {
419 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
420 (int) request->ims);
421 return 1;
422 }
423 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
424 return 0;
425 }
426
427
428 static void
429 clientHandleIMSReply(void *data, char *buf, ssize_t size)
430 {
431 clientHttpRequest *http = data;
432 StoreEntry *entry = http->entry;
433 MemObject *mem;
434 const char *url = storeUrl(entry);
435 int unlink_request = 0;
436 StoreEntry *oldentry;
437 int recopy = 1;
438 http_status status;
439 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url, (int) size);
440 if (entry == NULL) {
441 memFree(buf, MEM_CLIENT_SOCK_BUF);
442 return;
443 }
444 if (size < 0 && !EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
445 memFree(buf, MEM_CLIENT_SOCK_BUF);
446 return;
447 }
448 mem = entry->mem_obj;
449 status = mem->reply->sline.status;
450 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
451 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url);
452 /* We have an existing entry, but failed to validate it */
453 /* Its okay to send the old one anyway */
454 http->log_type = LOG_TCP_REFRESH_FAIL_HIT;
455 storeUnregister(http->sc, entry, http);
456 storeUnlockObject(entry);
457 entry = http->entry = http->old_entry;
458 http->sc = http->old_sc;
459 } else if (STORE_PENDING == entry->store_status && 0 == status) {
460 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url);
461 if (size >= CLIENT_SOCK_SZ) {
462 /* will not get any bigger than that */
463 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url);
464 /* use old entry, this repeats the code abovez */
465 http->log_type = LOG_TCP_REFRESH_FAIL_HIT;
466 storeUnregister(http->sc, entry, http);
467 storeUnlockObject(entry);
468 entry = http->entry = http->old_entry;
469 http->sc = http->old_sc;
470 /* continue */
471 } else {
472 storeClientCopy(http->sc, entry,
473 http->out.offset + size,
474 http->out.offset,
475 CLIENT_SOCK_SZ,
476 buf,
477 clientHandleIMSReply,
478 http);
479 return;
480 }
481 } else if (clientGetsOldEntry(entry, http->old_entry, http->request)) {
482 /* We initiated the IMS request, the client is not expecting
483 * 304, so put the good one back. First, make sure the old entry
484 * headers have been loaded from disk. */
485 oldentry = http->old_entry;
486 http->log_type = LOG_TCP_REFRESH_HIT;
487 if (oldentry->mem_obj->request == NULL) {
488 oldentry->mem_obj->request = requestLink(mem->request);
489 unlink_request = 1;
490 }
491 /* Don't memcpy() the whole reply structure here. For example,
492 * www.thegist.com (Netscape/1.13) returns a content-length for
493 * 304's which seems to be the length of the 304 HEADERS!!! and
494 * not the body they refer to. */
495 httpReplyUpdateOnNotModified(oldentry->mem_obj->reply, mem->reply);
496 storeTimestampsSet(oldentry);
497 storeUnregister(http->sc, entry, http);
498 http->sc = http->old_sc;
499 storeUnlockObject(entry);
500 entry = http->entry = oldentry;
501 entry->timestamp = squid_curtime;
502 if (unlink_request) {
503 requestUnlink(entry->mem_obj->request);
504 entry->mem_obj->request = NULL;
505 }
506 } else {
507 /* the client can handle this reply, whatever it is */
508 http->log_type = LOG_TCP_REFRESH_MISS;
509 if (HTTP_NOT_MODIFIED == mem->reply->sline.status) {
510 httpReplyUpdateOnNotModified(http->old_entry->mem_obj->reply,
511 mem->reply);
512 storeTimestampsSet(http->old_entry);
513 http->log_type = LOG_TCP_REFRESH_HIT;
514 }
515 storeUnregister(http->old_sc, http->old_entry, http);
516 storeUnlockObject(http->old_entry);
517 recopy = 0;
518 }
519 http->old_entry = NULL; /* done with old_entry */
520 http->old_sc = NULL;
521 assert(!EBIT_TEST(entry->flags, ENTRY_ABORTED));
522 if (recopy) {
523 storeClientCopy(http->sc, entry,
524 http->out.offset,
525 http->out.offset,
526 CLIENT_SOCK_SZ,
527 buf,
528 clientSendMoreData,
529 http);
530 } else {
531 clientSendMoreData(data, buf, size);
532 }
533 }
534
535 int
536 modifiedSince(StoreEntry * entry, request_t * request)
537 {
538 int object_length;
539 MemObject *mem = entry->mem_obj;
540 time_t mod_time = entry->lastmod;
541 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry));
542 if (mod_time < 0)
543 mod_time = entry->timestamp;
544 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time);
545 if (mod_time < 0)
546 return 1;
547 /* Find size of the object */
548 object_length = mem->reply->content_length;
549 if (object_length < 0)
550 object_length = contentLen(entry);
551 if (mod_time > request->ims) {
552 debug(33, 3) ("--> YES: entry newer than client\n");
553 return 1;
554 } else if (mod_time < request->ims) {
555 debug(33, 3) ("--> NO: entry older than client\n");
556 return 0;
557 } else if (request->imslen < 0) {
558 debug(33, 3) ("--> NO: same LMT, no client length\n");
559 return 0;
560 } else if (request->imslen == object_length) {
561 debug(33, 3) ("--> NO: same LMT, same length\n");
562 return 0;
563 } else {
564 debug(33, 3) ("--> YES: same LMT, different length\n");
565 return 1;
566 }
567 }
568
569 void
570 clientPurgeRequest(clientHttpRequest * http)
571 {
572 StoreEntry *entry;
573 ErrorState *err = NULL;
574 HttpReply *r;
575 debug(33, 3) ("Config2.onoff.enable_purge = %d\n", Config2.onoff.enable_purge);
576 if (!Config2.onoff.enable_purge) {
577 http->log_type = LOG_TCP_DENIED;
578 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
579 err->request = requestLink(http->request);
580 err->src_addr = http->conn->peer.sin_addr;
581 http->entry = clientCreateStoreEntry(http, http->request->method, null_request_flags);
582 errorAppendEntry(http->entry, err);
583 return;
584 }
585 http->log_type = LOG_TCP_MISS;
586 /* Release both IP and object cache entries */
587 ipcacheInvalidate(http->request->host);
588 if ((entry = storeGetPublic(http->uri, METHOD_GET)) == NULL) {
589 http->http_code = HTTP_NOT_FOUND;
590 } else {
591 storeRelease(entry);
592 http->http_code = HTTP_OK;
593 }
594 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
595 storeUrl(entry));
596 /*
597 * Make a new entry to hold the reply to be written
598 * to the client.
599 */
600 http->entry = clientCreateStoreEntry(http, http->request->method, null_request_flags);
601 httpReplyReset(r = http->entry->mem_obj->reply);
602 httpReplySetHeaders(r, 1.0, http->http_code, NULL, NULL, 0, 0, -1);
603 httpReplySwapOut(r, http->entry);
604 storeComplete(http->entry);
605 }
606
607 int
608 checkNegativeHit(StoreEntry * e)
609 {
610 if (!EBIT_TEST(e->flags, ENTRY_NEGCACHED))
611 return 0;
612 if (e->expires <= squid_curtime)
613 return 0;
614 if (e->store_status != STORE_OK)
615 return 0;
616 return 1;
617 }
618
619 void
620 clientUpdateCounters(clientHttpRequest * http)
621 {
622 int svc_time = tvSubMsec(http->start, current_time);
623 ping_data *i;
624 HierarchyLogEntry *H;
625 statCounter.client_http.requests++;
626 if (isTcpHit(http->log_type))
627 statCounter.client_http.hits++;
628 if (http->log_type == LOG_TCP_HIT)
629 statCounter.client_http.disk_hits++;
630 else if (http->log_type == LOG_TCP_MEM_HIT)
631 statCounter.client_http.mem_hits++;
632 if (http->request->err_type != ERR_NONE)
633 statCounter.client_http.errors++;
634 statHistCount(&statCounter.client_http.all_svc_time, svc_time);
635 /*
636 * The idea here is not to be complete, but to get service times
637 * for only well-defined types. For example, we don't include
638 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
639 * (we *tried* to validate it, but failed).
640 */
641 switch (http->log_type) {
642 case LOG_TCP_REFRESH_HIT:
643 statHistCount(&statCounter.client_http.nh_svc_time, svc_time);
644 break;
645 case LOG_TCP_IMS_HIT:
646 statHistCount(&statCounter.client_http.nm_svc_time, svc_time);
647 break;
648 case LOG_TCP_HIT:
649 case LOG_TCP_MEM_HIT:
650 case LOG_TCP_OFFLINE_HIT:
651 statHistCount(&statCounter.client_http.hit_svc_time, svc_time);
652 break;
653 case LOG_TCP_MISS:
654 case LOG_TCP_CLIENT_REFRESH_MISS:
655 statHistCount(&statCounter.client_http.miss_svc_time, svc_time);
656 break;
657 default:
658 /* make compiler warnings go away */
659 break;
660 }
661 H = &http->request->hier;
662 switch (H->alg) {
663 case PEER_SA_DIGEST:
664 statCounter.cd.times_used++;
665 break;
666 case PEER_SA_ICP:
667 statCounter.icp.times_used++;
668 i = &H->ping;
669 if (0 != i->stop.tv_sec && 0 != i->start.tv_sec)
670 statHistCount(&statCounter.icp.query_svc_time,
671 tvSubUsec(i->start, i->stop));
672 if (i->timeout)
673 statCounter.icp.query_timeouts++;
674 break;
675 case PEER_SA_NETDB:
676 statCounter.netdb.times_used++;
677 break;
678 default:
679 break;
680 }
681 }
682
683 static void
684 httpRequestFree(void *data)
685 {
686 clientHttpRequest *http = data;
687 clientHttpRequest **H;
688 ConnStateData *conn = http->conn;
689 StoreEntry *e;
690 request_t *request = http->request;
691 MemObject *mem = NULL;
692 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(http->entry));
693 if (!clientCheckTransferDone(http)) {
694 #if MYSTERIOUS_CODE
695 /*
696 * DW: this seems odd here, is it really needed? It causes
697 * incomplete transfers to get logged with "000" status
698 * code because http->entry becomes NULL.
699 */
700 if ((e = http->entry)) {
701 http->entry = NULL;
702 storeUnregister(http->sc, e, http);
703 storeUnlockObject(e);
704 }
705 #endif
706 if (http->entry && http->entry->ping_status == PING_WAITING)
707 storeReleaseRequest(http->entry);
708 }
709 assert(http->log_type < LOG_TYPE_MAX);
710 if (http->entry)
711 mem = http->entry->mem_obj;
712 if (http->out.size || http->log_type) {
713 http->al.icp.opcode = ICP_INVALID;
714 http->al.url = http->log_uri;
715 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http->al.url);
716 if (mem) {
717 http->al.http.code = mem->reply->sline.status;
718 http->al.http.content_type = strBuf(mem->reply->content_type);
719 }
720 http->al.cache.caddr = conn->log_addr;
721 http->al.cache.size = http->out.size;
722 http->al.cache.code = http->log_type;
723 http->al.cache.msec = tvSubMsec(http->start, current_time);
724 if (request) {
725 Packer p;
726 MemBuf mb;
727 memBufDefInit(&mb);
728 packerToMemInit(&p, &mb);
729 httpHeaderPackInto(&request->header, &p);
730 http->al.http.method = request->method;
731 http->al.http.version = request->http_ver;
732 http->al.headers.request = xstrdup(mb.buf);
733 http->al.hier = request->hier;
734 if (request->user_ident[0])
735 http->al.cache.ident = request->user_ident;
736 else
737 http->al.cache.ident = conn->ident;
738 packerClean(&p);
739 memBufClean(&mb);
740 }
741 accessLogLog(&http->al);
742 clientUpdateCounters(http);
743 clientdbUpdate(conn->peer.sin_addr, http->log_type, PROTO_HTTP, http->out.size);
744 }
745 if (http->acl_checklist)
746 aclChecklistFree(http->acl_checklist);
747 if (request)
748 checkFailureRatio(request->err_type, http->al.hier.code);
749 safe_free(http->uri);
750 safe_free(http->log_uri);
751 safe_free(http->al.headers.request);
752 safe_free(http->al.headers.reply);
753 safe_free(http->redirect.location);
754 stringClean(&http->range_iter.boundary);
755 if ((e = http->entry)) {
756 http->entry = NULL;
757 storeUnregister(http->sc, e, http);
758 http->sc = NULL;
759 storeUnlockObject(e);
760 }
761 /* old_entry might still be set if we didn't yet get the reply
762 * code in clientHandleIMSReply() */
763 if ((e = http->old_entry)) {
764 http->old_entry = NULL;
765 storeUnregister(http->old_sc, e, http);
766 http->old_sc = NULL;
767 storeUnlockObject(e);
768 }
769 requestUnlink(http->request);
770 assert(http != http->next);
771 assert(http->conn->chr != NULL);
772 H = &http->conn->chr;
773 while (*H) {
774 if (*H == http)
775 break;
776 H = &(*H)->next;
777 }
778 assert(*H != NULL);
779 *H = http->next;
780 http->next = NULL;
781 dlinkDelete(&http->active, &ClientActiveRequests);
782 cbdataFree(http);
783 }
784
785 /* This is a handler normally called by comm_close() */
786 static void
787 connStateFree(int fd, void *data)
788 {
789 ConnStateData *connState = data;
790 clientHttpRequest *http;
791 debug(33, 3) ("connStateFree: FD %d\n", fd);
792 assert(connState != NULL);
793 clientdbEstablished(connState->peer.sin_addr, -1); /* decrement */
794 while ((http = connState->chr) != NULL) {
795 assert(http->conn == connState);
796 assert(connState->chr != connState->chr->next);
797 httpRequestFree(http);
798 }
799 safe_free(connState->in.buf);
800 /* XXX account connState->in.buf */
801 pconnHistCount(0, connState->nrequests);
802 cbdataFree(connState);
803 #ifdef _SQUID_LINUX_
804 /* prevent those nasty RST packets */
805 {
806 char buf[SQUID_TCP_SO_RCVBUF];
807 while (read(fd, buf, SQUID_TCP_SO_RCVBUF) > 0);
808 }
809 #endif
810 }
811
812 static void
813 clientInterpretRequestHeaders(clientHttpRequest * http)
814 {
815 request_t *request = http->request;
816 const HttpHeader *req_hdr = &request->header;
817 int no_cache = 0;
818 #if defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
819 const char *str;
820 #endif
821 request->imslen = -1;
822 request->ims = httpHeaderGetTime(req_hdr, HDR_IF_MODIFIED_SINCE);
823 if (request->ims > 0)
824 request->flags.ims = 1;
825 if (httpHeaderHas(req_hdr, HDR_PRAGMA)) {
826 String s = httpHeaderGetList(req_hdr, HDR_PRAGMA);
827 if (strListIsMember(&s, "no-cache", ','))
828 no_cache++;
829 stringClean(&s);
830 }
831 request->cache_control = httpHeaderGetCc(req_hdr);
832 if (request->cache_control)
833 if (EBIT_TEST(request->cache_control->mask, CC_NO_CACHE))
834 no_cache++;
835 if (no_cache) {
836 #if HTTP_VIOLATIONS
837 if (Config.onoff.reload_into_ims)
838 request->flags.nocache_hack = 1;
839 else if (refresh_nocache_hack)
840 request->flags.nocache_hack = 1;
841 else
842 #endif
843 request->flags.nocache = 1;
844 }
845 /* ignore range header in non-GETs */
846 if (request->method == METHOD_GET) {
847 request->range = httpHeaderGetRange(req_hdr);
848 if (request->range)
849 request->flags.range = 1;
850 }
851 if (httpHeaderHas(req_hdr, HDR_AUTHORIZATION))
852 request->flags.auth = 1;
853 if (request->login[0] != '\0')
854 request->flags.auth = 1;
855 if (httpHeaderHas(req_hdr, HDR_VIA)) {
856 String s = httpHeaderGetList(req_hdr, HDR_VIA);
857 /*
858 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
859 * Note ThisCache2 has a space prepended to the hostname so we don't
860 * accidentally match super-domains.
861 */
862 if (strListIsSubstr(&s, ThisCache2, ',')) {
863 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
864 request, (ObjPackMethod) & httpRequestPack);
865 request->flags.loopdetect = 1;
866 }
867 #if FORW_VIA_DB
868 fvdbCountVia(strBuf(s));
869 #endif
870 stringClean(&s);
871 }
872 #if USE_USERAGENT_LOG
873 if ((str = httpHeaderGetStr(req_hdr, HDR_USER_AGENT)))
874 logUserAgent(fqdnFromAddr(http->conn->peer.sin_addr), str);
875 #endif
876 #if USE_REFERER_LOG
877 if ((str = httpHeaderGetStr(req_hdr, HDR_REFERER)))
878 logReferer(fqdnFromAddr(http->conn->peer.sin_addr), str,
879 http->log_uri);
880 #endif
881 #if FORW_VIA_DB
882 if (httpHeaderHas(req_hdr, HDR_X_FORWARDED_FOR)) {
883 String s = httpHeaderGetList(req_hdr, HDR_X_FORWARDED_FOR);
884 fvdbCountForw(strBuf(s));
885 stringClean(&s);
886 }
887 #endif
888 if (request->method == METHOD_TRACE) {
889 request->max_forwards = httpHeaderGetInt(req_hdr, HDR_MAX_FORWARDS);
890 }
891 if (clientCachable(http))
892 request->flags.cachable = 1;
893 if (clientHierarchical(http))
894 request->flags.hierarchical = 1;
895 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
896 request->flags.nocache ? "SET" : "NOT SET");
897 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
898 request->flags.cachable ? "SET" : "NOT SET");
899 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
900 request->flags.hierarchical ? "SET" : "NOT SET");
901 }
902
903 /*
904 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
905 * This is the client-side persistent connection flag. We need
906 * to set this relatively early in the request processing
907 * to handle hacks for broken servers and clients.
908 */
909 static void
910 clientSetKeepaliveFlag(clientHttpRequest * http)
911 {
912 request_t *request = http->request;
913 const HttpHeader *req_hdr = &request->header;
914 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
915 request->http_ver);
916 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
917 RequestMethodStr[request->method]);
918 if (!Config.onoff.client_pconns)
919 request->flags.proxy_keepalive = 0;
920 else if (httpMsgIsPersistent(request->http_ver, req_hdr))
921 request->flags.proxy_keepalive = 1;
922 }
923
924 static int
925 clientCheckContentLength(request_t * r)
926 {
927 switch (r->method) {
928 case METHOD_PUT:
929 case METHOD_POST:
930 /* PUT/POST requires a request entity */
931 return (r->content_length >= 0);
932 case METHOD_GET:
933 case METHOD_HEAD:
934 /* We do not want to see a request entity on GET/HEAD requests */
935 return (r->content_length <= 0);
936 default:
937 /* For other types of requests we don't care */
938 return 1;
939 }
940 /* NOT REACHED */
941 }
942
943 static int
944 clientCachable(clientHttpRequest * http)
945 {
946 const char *url = http->uri;
947 request_t *req = http->request;
948 method_t method = req->method;
949 if (req->protocol == PROTO_HTTP)
950 return httpCachable(method);
951 /* FTP is always cachable */
952 if (req->protocol == PROTO_GOPHER)
953 return gopherCachable(url);
954 if (req->protocol == PROTO_WAIS)
955 return 0;
956 if (method == METHOD_CONNECT)
957 return 0;
958 if (method == METHOD_TRACE)
959 return 0;
960 if (req->protocol == PROTO_CACHEOBJ)
961 return 0;
962 return 1;
963 }
964
965 /* Return true if we can query our neighbors for this object */
966 static int
967 clientHierarchical(clientHttpRequest * http)
968 {
969 const char *url = http->uri;
970 request_t *request = http->request;
971 method_t method = request->method;
972 const wordlist *p = NULL;
973
974 /* IMS needs a private key, so we can use the hierarchy for IMS only
975 * if our neighbors support private keys */
976 if (request->flags.ims && !neighbors_do_private_keys)
977 return 0;
978 if (request->flags.auth)
979 return 0;
980 if (method == METHOD_TRACE)
981 return 1;
982 if (method != METHOD_GET)
983 return 0;
984 /* scan hierarchy_stoplist */
985 for (p = Config.hierarchy_stoplist; p; p = p->next)
986 if (strstr(url, p->key))
987 return 0;
988 if (request->flags.loopdetect)
989 return 0;
990 if (request->protocol == PROTO_HTTP)
991 return httpCachable(method);
992 if (request->protocol == PROTO_GOPHER)
993 return gopherCachable(url);
994 if (request->protocol == PROTO_WAIS)
995 return 0;
996 if (request->protocol == PROTO_CACHEOBJ)
997 return 0;
998 return 1;
999 }
1000
1001 int
1002 isTcpHit(log_type code)
1003 {
1004 /* this should be a bitmap for better optimization */
1005 if (code == LOG_TCP_HIT)
1006 return 1;
1007 if (code == LOG_TCP_IMS_HIT)
1008 return 1;
1009 if (code == LOG_TCP_REFRESH_FAIL_HIT)
1010 return 1;
1011 if (code == LOG_TCP_REFRESH_HIT)
1012 return 1;
1013 if (code == LOG_TCP_NEGATIVE_HIT)
1014 return 1;
1015 if (code == LOG_TCP_MEM_HIT)
1016 return 1;
1017 if (code == LOG_TCP_OFFLINE_HIT)
1018 return 1;
1019 return 0;
1020 }
1021
1022 /*
1023 * returns true if If-Range specs match reply, false otherwise
1024 */
1025 static int
1026 clientIfRangeMatch(clientHttpRequest * http, HttpReply * rep)
1027 {
1028 const TimeOrTag spec = httpHeaderGetTimeOrTag(&http->request->header, HDR_IF_RANGE);
1029 /* check for parsing falure */
1030 if (!spec.valid)
1031 return 0;
1032 /* got an ETag? */
1033 if (spec.tag.str) {
1034 ETag rep_tag = httpHeaderGetETag(&rep->header, HDR_ETAG);
1035 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
1036 spec.tag.str, rep_tag.str ? rep_tag.str : "<none>");
1037 if (!rep_tag.str)
1038 return 0; /* entity has no etag to compare with! */
1039 if (spec.tag.weak || rep_tag.weak) {
1040 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
1041 spec.tag.str, rep_tag.str);
1042 return 0; /* must use strong validator for sub-range requests */
1043 }
1044 return etagIsEqual(&rep_tag, &spec.tag);
1045 }
1046 /* got modification time? */
1047 if (spec.time >= 0) {
1048 return http->entry->lastmod <= spec.time;
1049 }
1050 assert(0); /* should not happen */
1051 return 0;
1052 }
1053
1054 /* returns expected content length for multi-range replies
1055 * note: assumes that httpHdrRangeCanonize has already been called
1056 * warning: assumes that HTTP headers for individual ranges at the
1057 * time of the actuall assembly will be exactly the same as
1058 * the headers when clientMRangeCLen() is called */
1059 static int
1060 clientMRangeCLen(clientHttpRequest * http)
1061 {
1062 int clen = 0;
1063 HttpHdrRangePos pos = HttpHdrRangeInitPos;
1064 const HttpHdrRangeSpec *spec;
1065 MemBuf mb;
1066
1067 assert(http->entry->mem_obj);
1068
1069 memBufDefInit(&mb);
1070 while ((spec = httpHdrRangeGetSpec(http->request->range, &pos))) {
1071
1072 /* account for headers for this range */
1073 memBufReset(&mb);
1074 clientPackRangeHdr(http->entry->mem_obj->reply,
1075 spec, http->range_iter.boundary, &mb);
1076 clen += mb.size;
1077
1078 /* account for range content */
1079 clen += spec->length;
1080
1081 debug(33, 6) ("clientMRangeCLen: (clen += %d + %d) == %d\n",
1082 mb.size, spec->length, clen);
1083 }
1084 /* account for the terminating boundary */
1085 memBufReset(&mb);
1086 clientPackTermBound(http->range_iter.boundary, &mb);
1087 clen += mb.size;
1088
1089 memBufClean(&mb);
1090 return clen;
1091 }
1092
1093 /* adds appropriate Range headers if needed */
1094 static void
1095 clientBuildRangeHeader(clientHttpRequest * http, HttpReply * rep)
1096 {
1097 HttpHeader *hdr = rep ? &rep->header : 0;
1098 const char *range_err = NULL;
1099 request_t *request = http->request;
1100 assert(request->range);
1101 /* check if we still want to do ranges */
1102 if (!rep)
1103 range_err = "no [parse-able] reply";
1104 else if (rep->sline.status != HTTP_OK)
1105 range_err = "wrong status code";
1106 else if (httpHeaderHas(hdr, HDR_CONTENT_RANGE))
1107 range_err = "origin server does ranges";
1108 else if (rep->content_length < 0)
1109 range_err = "unknown length";
1110 else if (rep->content_length != http->entry->mem_obj->reply->content_length)
1111 range_err = "INCONSISTENT length"; /* a bug? */
1112 else if (httpHeaderHas(&http->request->header, HDR_IF_RANGE) && !clientIfRangeMatch(http, rep))
1113 range_err = "If-Range match failed";
1114 else if (!httpHdrRangeCanonize(http->request->range, rep->content_length))
1115 range_err = "canonization failed";
1116 else if (httpHdrRangeIsComplex(http->request->range))
1117 range_err = "too complex range header";
1118 else if (!request->flags.cachable) /* from we_do_ranges in http.c */
1119 range_err = "non-cachable request";
1120 /* get rid of our range specs on error */
1121 if (range_err) {
1122 debug(33, 3) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err);
1123 httpHdrRangeDestroy(http->request->range);
1124 http->request->range = NULL;
1125 } else {
1126 const int spec_count = http->request->range->specs.count;
1127 int actual_clen = -1;
1128
1129 debug(33, 3) ("clientBuildRangeHeader: range spec count: %d virgin clen: %d\n",
1130 spec_count, rep->content_length);
1131 assert(spec_count > 0);
1132 /* ETags should not be returned with Partial Content replies? */
1133 httpHeaderDelById(hdr, HDR_ETAG);
1134 /* append appropriate header(s) */
1135 if (spec_count == 1) {
1136 HttpHdrRangePos pos = HttpHdrRangeInitPos;
1137 const HttpHdrRangeSpec *spec = httpHdrRangeGetSpec(http->request->range, &pos);
1138 assert(spec);
1139 /* append Content-Range */
1140 httpHeaderAddContRange(hdr, *spec, rep->content_length);
1141 /* set new Content-Length to the actual number of bytes
1142 * transmitted in the message-body */
1143 actual_clen = spec->length;
1144 } else {
1145 /* multipart! */
1146 /* generate boundary string */
1147 http->range_iter.boundary = httpHdrRangeBoundaryStr(http);
1148 /* delete old Content-Type, add ours */
1149 httpHeaderDelById(hdr, HDR_CONTENT_TYPE);
1150 httpHeaderPutStrf(hdr, HDR_CONTENT_TYPE,
1151 "multipart/byteranges; boundary=\"%s\"",
1152 strBuf(http->range_iter.boundary));
1153 /* Content-Length is not required in multipart responses
1154 * but it is always nice to have one */
1155 actual_clen = clientMRangeCLen(http);
1156 }
1157
1158 /* replace Content-Length header */
1159 assert(actual_clen >= 0);
1160 httpHeaderDelById(hdr, HDR_CONTENT_LENGTH);
1161 httpHeaderPutInt(hdr, HDR_CONTENT_LENGTH, actual_clen);
1162 debug(33, 3) ("clientBuildRangeHeader: actual content length: %d\n", actual_clen);
1163 }
1164 }
1165
1166 /*
1167 * filters out unwanted entries from original reply header
1168 * adds extra entries if we have more info than origin server
1169 * adds Squid specific entries
1170 */
1171 static void
1172 clientBuildReplyHeader(clientHttpRequest * http, HttpReply * rep)
1173 {
1174 HttpHeader *hdr = &rep->header;
1175 int is_hit = isTcpHit(http->log_type);
1176 request_t *request = http->request;
1177 #if DONT_FILTER_THESE
1178 /* but you might want to if you run Squid as an HTTP accelerator */
1179 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1180 httpHeaderDelById(hdr, HDR_ETAG);
1181 #endif
1182 httpHeaderDelById(hdr, HDR_PROXY_CONNECTION);
1183 /* here: Keep-Alive is a field-name, not a connection directive! */
1184 httpHeaderDelByName(hdr, "Keep-Alive");
1185 /* remove Set-Cookie if a hit */
1186 if (is_hit)
1187 httpHeaderDelById(hdr, HDR_SET_COOKIE);
1188 /* handle Connection header */
1189 if (httpHeaderHas(hdr, HDR_CONNECTION)) {
1190 /* anything that matches Connection list member will be deleted */
1191 String strConnection = httpHeaderGetList(hdr, HDR_CONNECTION);
1192 const HttpHeaderEntry *e;
1193 HttpHeaderPos pos = HttpHeaderInitPos;
1194 /*
1195 * think: on-average-best nesting of the two loops (hdrEntry
1196 * and strListItem) @?@
1197 */
1198 /*
1199 * maybe we should delete standard stuff ("keep-alive","close")
1200 * from strConnection first?
1201 */
1202 while ((e = httpHeaderGetEntry(hdr, &pos))) {
1203 if (strListIsMember(&strConnection, strBuf(e->name), ','))
1204 httpHeaderDelAt(hdr, pos);
1205 }
1206 httpHeaderDelById(hdr, HDR_CONNECTION);
1207 stringClean(&strConnection);
1208 }
1209 /* Handle Ranges */
1210 if (request->range)
1211 clientBuildRangeHeader(http, rep);
1212 /*
1213 * Add a estimated Age header on cache hits.
1214 */
1215 if (is_hit) {
1216 /*
1217 * Remove any existing Age header sent by upstream caches
1218 * (note that the existing header is passed along unmodified
1219 * on cache misses)
1220 */
1221 httpHeaderDelById(hdr, HDR_AGE);
1222 /*
1223 * This adds the calculated object age. Note that the details of the
1224 * age calculation is performed by adjusting the timestamp in
1225 * storeTimestampsSet(), not here.
1226 *
1227 * BROWSER WORKAROUND: IE sometimes hangs when receiving a 0 Age
1228 * header, so don't use it unless there is a age to report. Please
1229 * note that Age is only used to make a conservative estimation of
1230 * the objects age, so a Age: 0 header does not add any useful
1231 * information to the reply in any case.
1232 */
1233 if (http->entry->timestamp > -1)
1234 if (http->entry->timestamp < squid_curtime)
1235 httpHeaderPutInt(hdr, HDR_AGE,
1236 squid_curtime - http->entry->timestamp);
1237 }
1238 /* Append X-Cache */
1239 httpHeaderPutStrf(hdr, HDR_X_CACHE, "%s from %s",
1240 is_hit ? "HIT" : "MISS", getMyHostname());
1241 #if USE_CACHE_DIGESTS
1242 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1243 httpHeaderPutStrf(hdr, HDR_X_CACHE_LOOKUP, "%s from %s:%d",
1244 http->lookup_type ? http->lookup_type : "NONE",
1245 getMyHostname(), ntohs(Config.Sockaddr.http->s.sin_port));
1246 #endif
1247 if (httpReplyBodySize(request->method, rep) < 0) {
1248 debug(33, 3) ("clientBuildReplyHeader: can't keep-alive, unknown body size\n");
1249 request->flags.proxy_keepalive = 0;
1250 }
1251 /* Signal keep-alive if needed */
1252 httpHeaderPutStr(hdr,
1253 http->flags.accel ? HDR_CONNECTION : HDR_PROXY_CONNECTION,
1254 request->flags.proxy_keepalive ? "keep-alive" : "close");
1255 #if ADD_X_REQUEST_URI
1256 /*
1257 * Knowing the URI of the request is useful when debugging persistent
1258 * connections in a client; we cannot guarantee the order of http headers,
1259 * but X-Request-URI is likely to be the very last header to ease use from a
1260 * debugger [hdr->entries.count-1].
1261 */
1262 httpHeaderPutStr(hdr, HDR_X_REQUEST_URI,
1263 http->entry->mem_obj->url ? http->entry->mem_obj->url : http->uri);
1264 #endif
1265 }
1266
1267 static HttpReply *
1268 clientBuildReply(clientHttpRequest * http, const char *buf, size_t size)
1269 {
1270 HttpReply *rep = httpReplyCreate();
1271 size_t k = headersEnd(buf, size);
1272 if (k && httpReplyParse(rep, buf, k)) {
1273 /* enforce 1.0 reply version */
1274 rep->sline.version = 1.0;
1275 /* do header conversions */
1276 clientBuildReplyHeader(http, rep);
1277 /* if we do ranges, change status to "Partial Content" */
1278 if (http->request->range)
1279 httpStatusLineSet(&rep->sline, rep->sline.version,
1280 HTTP_PARTIAL_CONTENT, NULL);
1281 } else {
1282 /* parsing failure, get rid of the invalid reply */
1283 httpReplyDestroy(rep);
1284 rep = NULL;
1285 /* if we were going to do ranges, backoff */
1286 if (http->request->range) {
1287 /* this will fail and destroy request->range */
1288 clientBuildRangeHeader(http, rep);
1289 }
1290 }
1291 return rep;
1292 }
1293
1294 /*
1295 * clientCacheHit should only be called until the HTTP reply headers
1296 * have been parsed. Normally this should be a single call, but
1297 * it might take more than one. As soon as we have the headers,
1298 * we hand off to clientSendMoreData, clientProcessExpired, or
1299 * clientProcessMiss.
1300 */
1301 static void
1302 clientCacheHit(void *data, char *buf, ssize_t size)
1303 {
1304 clientHttpRequest *http = data;
1305 StoreEntry *e = http->entry;
1306 MemObject *mem;
1307 request_t *r = http->request;
1308 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http->uri, (int) size);
1309 if (http->entry == NULL) {
1310 memFree(buf, MEM_CLIENT_SOCK_BUF);
1311 debug(33, 3) ("clientCacheHit: request aborted\n");
1312 return;
1313 } else if (size < 0) {
1314 /* swap in failure */
1315 memFree(buf, MEM_CLIENT_SOCK_BUF);
1316 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http->uri);
1317 http->log_type = LOG_TCP_SWAPFAIL_MISS;
1318 if ((e = http->entry)) {
1319 http->entry = NULL;
1320 storeUnregister(http->sc, e, http);
1321 http->sc = NULL;
1322 storeUnlockObject(e);
1323 }
1324 clientProcessMiss(http);
1325 return;
1326 }
1327 assert(size > 0);
1328 mem = e->mem_obj;
1329 assert(!EBIT_TEST(e->flags, ENTRY_ABORTED));
1330 if (mem->reply->sline.status == 0) {
1331 /*
1332 * we don't have full reply headers yet; either wait for more or
1333 * punt to clientProcessMiss.
1334 */
1335 if (e->mem_status == IN_MEMORY || e->store_status == STORE_OK) {
1336 memFree(buf, MEM_CLIENT_SOCK_BUF);
1337 clientProcessMiss(http);
1338 } else if (size == CLIENT_SOCK_SZ && http->out.offset == 0) {
1339 memFree(buf, MEM_CLIENT_SOCK_BUF);
1340 clientProcessMiss(http);
1341 } else {
1342 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1343 storeClientCopy(http->sc, e,
1344 http->out.offset + size,
1345 http->out.offset,
1346 CLIENT_SOCK_SZ,
1347 buf,
1348 clientCacheHit,
1349 http);
1350 }
1351 return;
1352 }
1353 /*
1354 * Got the headers, now grok them
1355 */
1356 assert(http->log_type == LOG_TCP_HIT);
1357 if (checkNegativeHit(e)) {
1358 http->log_type = LOG_TCP_NEGATIVE_HIT;
1359 clientSendMoreData(data, buf, size);
1360 } else if (r->method == METHOD_HEAD) {
1361 /*
1362 * RFC 2068 seems to indicate there is no "conditional HEAD"
1363 * request. We cannot validate a cached object for a HEAD
1364 * request, nor can we return 304.
1365 */
1366 if (e->mem_status == IN_MEMORY)
1367 http->log_type = LOG_TCP_MEM_HIT;
1368 clientSendMoreData(data, buf, size);
1369 } else if (refreshCheckHTTP(e, r) && !http->flags.internal) {
1370 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1371 /*
1372 * We hold a stale copy; it needs to be validated
1373 */
1374 /*
1375 * The 'need_validation' flag is used to prevent forwarding
1376 * loops between siblings. If our copy of the object is stale,
1377 * then we should probably only use parents for the validation
1378 * request. Otherwise two siblings could generate a loop if
1379 * both have a stale version of the object.
1380 */
1381 r->flags.need_validation = 1;
1382 if (e->lastmod < 0) {
1383 /*
1384 * Previous reply didn't have a Last-Modified header,
1385 * we cannot revalidate it.
1386 */
1387 http->log_type = LOG_TCP_MISS;
1388 clientProcessMiss(http);
1389 } else if (r->flags.nocache) {
1390 /*
1391 * This did not match a refresh pattern that overrides no-cache
1392 * we should honour the client no-cache header.
1393 */
1394 http->log_type = LOG_TCP_CLIENT_REFRESH_MISS;
1395 clientProcessMiss(http);
1396 } else if (r->protocol == PROTO_HTTP) {
1397 /*
1398 * Object needs to be revalidated
1399 * XXX This could apply to FTP as well, if Last-Modified is known.
1400 */
1401 http->log_type = LOG_TCP_REFRESH_MISS;
1402 clientProcessExpired(http);
1403 } else {
1404 /*
1405 * We don't know how to re-validate other protocols. Handle
1406 * them as if the object has expired.
1407 */
1408 http->log_type = LOG_TCP_MISS;
1409 clientProcessMiss(http);
1410 }
1411 memFree(buf, MEM_CLIENT_SOCK_BUF);
1412 } else if (r->flags.ims) {
1413 /*
1414 * Handle If-Modified-Since requests from the client
1415 */
1416 if (mem->reply->sline.status != HTTP_OK) {
1417 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1418 mem->reply->sline.status);
1419 memFree(buf, MEM_CLIENT_SOCK_BUF);
1420 http->log_type = LOG_TCP_MISS;
1421 clientProcessMiss(http);
1422 } else if (modifiedSince(e, http->request)) {
1423 http->log_type = LOG_TCP_IMS_HIT;
1424 clientSendMoreData(data, buf, size);
1425 } else {
1426 time_t timestamp = e->timestamp;
1427 MemBuf mb = httpPacked304Reply(e->mem_obj->reply);
1428 http->log_type = LOG_TCP_IMS_HIT;
1429 memFree(buf, MEM_CLIENT_SOCK_BUF);
1430 storeUnregister(http->sc, e, http);
1431 http->sc = NULL;
1432 storeUnlockObject(e);
1433 e = clientCreateStoreEntry(http, http->request->method, null_request_flags);
1434 /*
1435 * Copy timestamp from the original entry so the 304
1436 * reply has a meaningful Age: header.
1437 */
1438 e->timestamp = timestamp;
1439 http->entry = e;
1440 httpReplyParse(e->mem_obj->reply, mb.buf, mb.size);
1441 storeAppend(e, mb.buf, mb.size);
1442 memBufClean(&mb);
1443 storeComplete(e);
1444 }
1445 } else {
1446 /*
1447 * plain ol' cache hit
1448 */
1449 if (e->mem_status == IN_MEMORY)
1450 http->log_type = LOG_TCP_MEM_HIT;
1451 else if (Config.onoff.offline)
1452 http->log_type = LOG_TCP_OFFLINE_HIT;
1453 clientSendMoreData(data, buf, size);
1454 }
1455 }
1456
1457 /* put terminating boundary for multiparts */
1458 static void
1459 clientPackTermBound(String boundary, MemBuf * mb)
1460 {
1461 memBufPrintf(mb, "\r\n--%s--\r\n", strBuf(boundary));
1462 debug(33, 6) ("clientPackTermBound: buf offset: %d\n", mb->size);
1463 }
1464
1465 /* appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */
1466 static void
1467 clientPackRangeHdr(const HttpReply * rep, const HttpHdrRangeSpec * spec, String boundary, MemBuf * mb)
1468 {
1469 HttpHeader hdr;
1470 Packer p;
1471 assert(rep);
1472 assert(spec);
1473
1474 /* put boundary */
1475 debug(33, 5) ("clientPackRangeHdr: appending boundary: %s\n", strBuf(boundary));
1476 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1477 memBufPrintf(mb, "\r\n--%s\r\n", strBuf(boundary));
1478
1479 /* stuff the header with required entries and pack it */
1480 httpHeaderInit(&hdr, hoReply);
1481 if (httpHeaderHas(&rep->header, HDR_CONTENT_TYPE))
1482 httpHeaderPutStr(&hdr, HDR_CONTENT_TYPE, httpHeaderGetStr(&rep->header, HDR_CONTENT_TYPE));
1483 httpHeaderAddContRange(&hdr, *spec, rep->content_length);
1484 packerToMemInit(&p, mb);
1485 httpHeaderPackInto(&hdr, &p);
1486 packerClean(&p);
1487 httpHeaderClean(&hdr);
1488
1489 /* append <crlf> (we packed a header, not a reply) */
1490 memBufPrintf(mb, crlf);
1491 }
1492
1493 /*
1494 * extracts a "range" from *buf and appends them to mb, updating
1495 * all offsets and such.
1496 */
1497 static void
1498 clientPackRange(clientHttpRequest * http,
1499 HttpHdrRangeIter * i,
1500 const char **buf,
1501 ssize_t * size,
1502 MemBuf * mb)
1503 {
1504 const ssize_t copy_sz = i->debt_size <= *size ? i->debt_size : *size;
1505 off_t body_off = http->out.offset - i->prefix_size;
1506 assert(*size > 0);
1507 assert(i->spec);
1508 /*
1509 * intersection of "have" and "need" ranges must not be empty
1510 */
1511 assert(body_off < i->spec->offset + i->spec->length);
1512 assert(body_off + *size > i->spec->offset);
1513 /*
1514 * put boundary and headers at the beginning of a range in a
1515 * multi-range
1516 */
1517 if (http->request->range->specs.count > 1 && i->debt_size == i->spec->length) {
1518 assert(http->entry->mem_obj);
1519 clientPackRangeHdr(
1520 http->entry->mem_obj->reply, /* original reply */
1521 i->spec, /* current range */
1522 i->boundary, /* boundary, the same for all */
1523 mb
1524 );
1525 }
1526 /*
1527 * append content
1528 */
1529 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz);
1530 memBufAppend(mb, *buf, copy_sz);
1531 /*
1532 * update offsets
1533 */
1534 *size -= copy_sz;
1535 i->debt_size -= copy_sz;
1536 body_off += copy_sz;
1537 *buf += copy_sz;
1538 http->out.offset = body_off + i->prefix_size; /* sync */
1539 /*
1540 * paranoid check
1541 */
1542 assert(*size >= 0 && i->debt_size >= 0);
1543 }
1544
1545 /* returns true if there is still data available to pack more ranges
1546 * increments iterator "i"
1547 * used by clientPackMoreRanges */
1548 static int
1549 clientCanPackMoreRanges(const clientHttpRequest * http, HttpHdrRangeIter * i, ssize_t size)
1550 {
1551 /* first update "i" if needed */
1552 if (!i->debt_size) {
1553 if ((i->spec = httpHdrRangeGetSpec(http->request->range, &i->pos)))
1554 i->debt_size = i->spec->length;
1555 }
1556 assert(!i->debt_size == !i->spec); /* paranoid sync condition */
1557 /* continue condition: need_more_data && have_more_data */
1558 return i->spec && size > 0;
1559 }
1560
1561 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1562 /* returns true if we need more data */
1563 static int
1564 clientPackMoreRanges(clientHttpRequest * http, const char *buf, ssize_t size, MemBuf * mb)
1565 {
1566 HttpHdrRangeIter *i = &http->range_iter;
1567 /* offset in range specs does not count the prefix of an http msg */
1568 off_t body_off = http->out.offset - i->prefix_size;
1569 assert(size >= 0);
1570 /* check: reply was parsed and range iterator was initialized */
1571 assert(i->prefix_size > 0);
1572 /* filter out data according to range specs */
1573 while (clientCanPackMoreRanges(http, i, size)) {
1574 off_t start; /* offset of still missing data */
1575 assert(i->spec);
1576 start = i->spec->offset + i->spec->length - i->debt_size;
1577 debug(33, 3) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1578 (int) body_off, size);
1579 debug(33, 3) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1580 (int) start, (int) i->pos, i->spec->offset, (int) (i->spec->offset + i->spec->length), i->spec->length, i->debt_size);
1581 assert(body_off <= start); /* we did not miss it */
1582 /* skip up to start */
1583 if (body_off + size > start) {
1584 const size_t skip_size = start - body_off;
1585 body_off = start;
1586 size -= skip_size;
1587 buf += skip_size;
1588 } else {
1589 /* has not reached start yet */
1590 body_off += size;
1591 size = 0;
1592 buf = NULL;
1593 }
1594 /* put next chunk if any */
1595 if (size) {
1596 http->out.offset = body_off + i->prefix_size; /* sync */
1597 clientPackRange(http, i, &buf, &size, mb);
1598 body_off = http->out.offset - i->prefix_size; /* sync */
1599 }
1600 }
1601 assert(!i->debt_size == !i->spec); /* paranoid sync condition */
1602 debug(33, 3) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1603 (int) body_off, size, i->debt_size);
1604 if (i->debt_size) {
1605 debug(33, 3) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1606 (int) i->pos, i->spec->offset, (int) (i->spec->offset + i->spec->length), i->spec->length);
1607 /* skip the data we do not need if possible */
1608 if (i->debt_size == i->spec->length) /* at the start of the cur. spec */
1609 body_off = i->spec->offset;
1610 else
1611 assert(body_off == i->spec->offset + i->spec->length - i->debt_size);
1612 } else if (http->request->range->specs.count > 1) {
1613 /* put terminating boundary for multiparts */
1614 clientPackTermBound(i->boundary, mb);
1615 }
1616 http->out.offset = body_off + i->prefix_size; /* sync */
1617 return i->debt_size > 0;
1618 }
1619
1620 static int
1621 clientReplyBodyTooLarge(int clen)
1622 {
1623 if (0 == Config.maxReplyBodySize)
1624 return 0; /* disabled */
1625 if (clen < 0)
1626 return 0; /* unknown */
1627 if (clen > Config.maxReplyBodySize)
1628 return 1; /* too large */
1629 return 0;
1630 }
1631
1632 static int
1633 clientRequestBodyTooLarge(int clen)
1634 {
1635 if (0 == Config.maxRequestBodySize)
1636 return 0; /* disabled */
1637 if (clen < 0)
1638 return 0; /* unknown, bug? */
1639 if (clen > Config.maxRequestBodySize)
1640 return 1; /* too large */
1641 return 0;
1642 }
1643
1644 /*
1645 * accepts chunk of a http message in buf, parses prefix, filters headers and
1646 * such, writes processed message to the client's socket
1647 */
1648 static void
1649 clientSendMoreData(void *data, char *buf, ssize_t size)
1650 {
1651 clientHttpRequest *http = data;
1652 StoreEntry *entry = http->entry;
1653 ConnStateData *conn = http->conn;
1654 int fd = conn->fd;
1655 HttpReply *rep = NULL;
1656 const char *body_buf = buf;
1657 ssize_t body_size = size;
1658 MemBuf mb;
1659 ssize_t check_size = 0;
1660 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http->uri, (int) size);
1661 assert(size <= CLIENT_SOCK_SZ);
1662 assert(http->request != NULL);
1663 dlinkDelete(&http->active, &ClientActiveRequests);
1664 dlinkAdd(http, &http->active, &ClientActiveRequests);
1665 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1666 fd, storeUrl(entry), (int) http->out.offset);
1667 if (conn->chr != http) {
1668 /* there is another object in progress, defer this one */
1669 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry));
1670 memFree(buf, MEM_CLIENT_SOCK_BUF);
1671 return;
1672 } else if (entry && EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1673 /* call clientWriteComplete so the client socket gets closed */
1674 clientWriteComplete(fd, NULL, 0, COMM_OK, http);
1675 memFree(buf, MEM_CLIENT_SOCK_BUF);
1676 return;
1677 } else if (size < 0) {
1678 /* call clientWriteComplete so the client socket gets closed */
1679 clientWriteComplete(fd, NULL, 0, COMM_OK, http);
1680 memFree(buf, MEM_CLIENT_SOCK_BUF);
1681 return;
1682 } else if (size == 0) {
1683 /* call clientWriteComplete so the client socket gets closed */
1684 clientWriteComplete(fd, NULL, 0, COMM_OK, http);
1685 memFree(buf, MEM_CLIENT_SOCK_BUF);
1686 return;
1687 }
1688 if (http->out.offset == 0) {
1689 if (Config.onoff.log_mime_hdrs) {
1690 size_t k;
1691 if ((k = headersEnd(buf, size))) {
1692 safe_free(http->al.headers.reply);
1693 http->al.headers.reply = xcalloc(k + 1, 1);
1694 xstrncpy(http->al.headers.reply, buf, k);
1695 }
1696 }
1697 rep = clientBuildReply(http, buf, size);
1698 if (rep && clientReplyBodyTooLarge(rep->content_length)) {
1699 ErrorState *err = errorCon(ERR_TOO_BIG, HTTP_FORBIDDEN);
1700 err->request = requestLink(http->request);
1701 storeUnregister(http->sc, http->entry, http);
1702 http->sc = NULL;
1703 storeUnlockObject(http->entry);
1704 http->entry = clientCreateStoreEntry(http, http->request->method,
1705 null_request_flags);
1706 errorAppendEntry(http->entry, err);
1707 httpReplyDestroy(rep);
1708 return;
1709 } else if (rep) {
1710 body_size = size - rep->hdr_sz;
1711 assert(body_size >= 0);
1712 body_buf = buf + rep->hdr_sz;
1713 http->range_iter.prefix_size = rep->hdr_sz;
1714 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1715 body_size, rep->hdr_sz);
1716 } else if (size < CLIENT_SOCK_SZ && entry->store_status == STORE_PENDING) {
1717 /* wait for more to arrive */
1718 storeClientCopy(http->sc, entry,
1719 http->out.offset + size,
1720 http->out.offset,
1721 CLIENT_SOCK_SZ,
1722 buf,
1723 clientSendMoreData,
1724 http);
1725 return;
1726 }
1727 /* reset range iterator */
1728 http->range_iter.pos = HttpHdrRangeInitPos;
1729 } else if (!http->request->range) {
1730 /* Avoid copying to MemBuf for non-range requests */
1731 /* Note, if we're here, then 'rep' is known to be NULL */
1732 http->out.offset += body_size;
1733 comm_write(fd, buf, size, clientWriteBodyComplete, http, NULL);
1734 /* NULL because clientWriteBodyComplete frees it */
1735 return;
1736 }
1737 if (http->request->method == METHOD_HEAD) {
1738 if (rep) {
1739 /* do not forward body for HEAD replies */
1740 body_size = 0;
1741 http->flags.done_copying = 1;
1742 } else {
1743 /*
1744 * If we are here, then store_status == STORE_OK and it
1745 * seems we have a HEAD repsponse which is missing the
1746 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1747 * does this). Because clientBuildReply() fails we just
1748 * call this reply a body, set the done_copying flag and
1749 * continue...
1750 */
1751 http->flags.done_copying = 1;
1752 }
1753 }
1754 /* write headers and/or body if any */
1755 assert(rep || (body_buf && body_size));
1756 /* init mb; put status line and headers if any */
1757 if (rep) {
1758 mb = httpReplyPack(rep);
1759 http->out.offset += rep->hdr_sz;
1760 check_size += rep->hdr_sz;
1761 #if HEADERS_LOG
1762 headersLog(0, 0, http->request->method, rep);
1763 #endif
1764 httpReplyDestroy(rep);
1765 rep = NULL;
1766 } else {
1767 memBufDefInit(&mb);
1768 }
1769 /* append body if any */
1770 if (http->request->range) {
1771 /* Only GET requests should have ranges */
1772 assert(http->request->method == METHOD_GET);
1773 /* clientPackMoreRanges() updates http->out.offset */
1774 /* force the end of the transfer if we are done */
1775 if (!clientPackMoreRanges(http, body_buf, body_size, &mb))
1776 http->flags.done_copying = 1;
1777 } else if (body_buf && body_size) {
1778 http->out.offset += body_size;
1779 check_size += body_size;
1780 memBufAppend(&mb, body_buf, body_size);
1781 }
1782 if (!http->request->range && http->request->method == METHOD_GET)
1783 assert(check_size == size);
1784 /* write */
1785 comm_write_mbuf(fd, mb, clientWriteComplete, http);
1786 /* if we don't do it, who will? */
1787 memFree(buf, MEM_CLIENT_SOCK_BUF);
1788 }
1789
1790 /*
1791 * clientWriteBodyComplete is called for MEM_CLIENT_SOCK_BUF's
1792 * written directly to the client socket, versus copying to a MemBuf
1793 * and going through comm_write_mbuf. Most non-range responses after
1794 * the headers probably go through here.
1795 */
1796 static void
1797 clientWriteBodyComplete(int fd, char *buf, size_t size, int errflag, void *data)
1798 {
1799 /*
1800 * NOTE: clientWriteComplete doesn't currently use its "buf"
1801 * (second) argument, so we pass in NULL.
1802 */
1803 clientWriteComplete(fd, NULL, size, errflag, data);
1804 memFree(buf, MEM_CLIENT_SOCK_BUF);
1805 }
1806
1807 static void
1808 clientKeepaliveNextRequest(clientHttpRequest * http)
1809 {
1810 ConnStateData *conn = http->conn;
1811 StoreEntry *entry;
1812 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn->fd);
1813 conn->defer.until = 0; /* Kick it to read a new request */
1814 httpRequestFree(http);
1815 if ((http = conn->chr) == NULL) {
1816 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1817 conn->fd);
1818 fd_note(conn->fd, "Reading next request");
1819 /*
1820 * Set the timeout BEFORE calling clientReadRequest().
1821 */
1822 commSetTimeout(conn->fd, Config.Timeout.pconn, requestTimeout, conn);
1823 clientReadRequest(conn->fd, conn); /* Read next request */
1824 /*
1825 * Note, the FD may be closed at this point.
1826 */
1827 } else if ((entry = http->entry) == NULL) {
1828 /*
1829 * this request is in progress, maybe doing an ACL or a redirect,
1830 * execution will resume after the operation completes.
1831 */
1832 } else {
1833 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1834 conn->fd);
1835 assert(entry);
1836 if (0 == storeClientCopyPending(http->sc, entry, http)) {
1837 if (EBIT_TEST(entry->flags, ENTRY_ABORTED))
1838 debug(33, 0) ("clientKeepaliveNextRequest: ENTRY_ABORTED\n");
1839 storeClientCopy(http->sc, entry,
1840 http->out.offset,
1841 http->out.offset,
1842 CLIENT_SOCK_SZ,
1843 memAllocate(MEM_CLIENT_SOCK_BUF),
1844 clientSendMoreData,
1845 http);
1846 }
1847 }
1848 }
1849
1850 static void
1851 clientWriteComplete(int fd, char *bufnotused, size_t size, int errflag, void *data)
1852 {
1853 clientHttpRequest *http = data;
1854 StoreEntry *entry = http->entry;
1855 int done;
1856 http->out.size += size;
1857 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1858 fd, size, errflag, (int) http->out.offset, entry ? objectLen(entry) : 0);
1859 if (size > 0) {
1860 kb_incr(&statCounter.client_http.kbytes_out, size);
1861 if (isTcpHit(http->log_type))
1862 kb_incr(&statCounter.client_http.hit_kbytes_out, size);
1863 }
1864 if (errflag) {
1865 /*
1866 * just close the socket, httpRequestFree will abort if needed
1867 */
1868 comm_close(fd);
1869 } else if (NULL == entry) {
1870 comm_close(fd); /* yuk */
1871 } else if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1872 comm_close(fd);
1873 } else if ((done = clientCheckTransferDone(http)) != 0 || size == 0) {
1874 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd);
1875 /* We're finished case */
1876 if (httpReplyBodySize(http->request->method, entry->mem_obj->reply) < 0) {
1877 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1878 comm_close(fd);
1879 } else if (!done) {
1880 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1881 comm_close(fd);
1882 } else if (clientGotNotEnough(http)) {
1883 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1884 comm_close(fd);
1885 } else if (http->request->flags.proxy_keepalive) {
1886 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd);
1887 clientKeepaliveNextRequest(http);
1888 } else {
1889 comm_close(fd);
1890 }
1891 } else if (clientReplyBodyTooLarge((int) http->out.offset)) {
1892 comm_close(fd);
1893 } else {
1894 /* More data will be coming from primary server; register with
1895 * storage manager. */
1896 if (EBIT_TEST(entry->flags, ENTRY_ABORTED))
1897 debug(33, 0) ("clientWriteComplete 2: ENTRY_ABORTED\n");
1898 storeClientCopy(http->sc, entry,
1899 http->out.offset,
1900 http->out.offset,
1901 CLIENT_SOCK_SZ,
1902 memAllocate(MEM_CLIENT_SOCK_BUF),
1903 clientSendMoreData,
1904 http);
1905 }
1906 }
1907
1908 /*
1909 * client issued a request with an only-if-cached cache-control directive;
1910 * we did not find a cached object that can be returned without
1911 * contacting other servers;
1912 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1913 */
1914 static void
1915 clientProcessOnlyIfCachedMiss(clientHttpRequest * http)
1916 {
1917 char *url = http->uri;
1918 request_t *r = http->request;
1919 ErrorState *err = NULL;
1920 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1921 RequestMethodStr[r->method], url);
1922 http->al.http.code = HTTP_GATEWAY_TIMEOUT;
1923 err = errorCon(ERR_ONLY_IF_CACHED_MISS, HTTP_GATEWAY_TIMEOUT);
1924 err->request = requestLink(r);
1925 err->src_addr = http->conn->peer.sin_addr;
1926 if (http->entry) {
1927 storeUnregister(http->sc, http->entry, http);
1928 http->sc = NULL;
1929 storeUnlockObject(http->entry);
1930 }
1931 http->entry = clientCreateStoreEntry(http, r->method, null_request_flags);
1932 errorAppendEntry(http->entry, err);
1933 }
1934
1935 /*
1936 * Return true if the first range offset is larger than the configured
1937 * limit, UNLESS the request is a hit AND the bits we want are in
1938 * the cache.
1939 */
1940 static int
1941 clientCheckRangeOffsetLimit(StoreEntry * entry, HttpHdrRange * range)
1942 {
1943 ssize_t range_start;
1944 /*
1945 * If the range offset limit is disabled, don't force a miss.
1946 */
1947 if (-1 == Config.rangeOffsetLimit)
1948 return 0;
1949 /*
1950 * If the first range offset is less than the configured limit
1951 * we won't force a cache miss.
1952 */
1953 range_start = httpHdrRangeFirstOffset(range);
1954 if (Config.rangeOffsetLimit >= range_start)
1955 return 0;
1956 /*
1957 * Now we know it's possibly a hit. If we already have the
1958 * whole object cached, we won't force a miss.
1959 */
1960 if (STORE_OK == entry->store_status)
1961 return 0; /* we have the whole object */
1962 /*
1963 * Now we have a hit on a PENDING object. We need to see
1964 * if the part we want is already cached. If so, we don't
1965 * force a miss.
1966 */
1967 assert(NULL != entry->mem_obj);
1968 if (range_start <= entry->mem_obj->inmem_hi)
1969 return 0;
1970 /*
1971 * Even though we have a PENDING copy of the object, we
1972 * don't want to wait to reach the first range offset,
1973 * so we force a miss for a new range request to the
1974 * origin.
1975 */
1976 return 1;
1977 }
1978
1979 static log_type
1980 clientProcessRequest2(clientHttpRequest * http)
1981 {
1982 request_t *r = http->request;
1983 StoreEntry *e;
1984 e = http->entry = storeGetPublic(http->uri, r->method);
1985 if (r->method == METHOD_HEAD && e == NULL) {
1986 /* We can generate a HEAD reply from a cached GET object */
1987 e = http->entry = storeGetPublic(http->uri, METHOD_GET);
1988 }
1989 /* Release negatively cached IP-cache entries on reload */
1990 if (r->flags.nocache)
1991 ipcacheReleaseInvalid(r->host);
1992 #if HTTP_VIOLATIONS
1993 else if (r->flags.nocache_hack)
1994 ipcacheReleaseInvalid(r->host);
1995 #endif
1996 #if USE_CACHE_DIGESTS
1997 http->lookup_type = e ? "HIT" : "MISS";
1998 #endif
1999 if (NULL == e) {
2000 /* this object isn't in the cache */
2001 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
2002 return LOG_TCP_MISS;
2003 }
2004 if (Config.onoff.offline) {
2005 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
2006 http->entry = e;
2007 return LOG_TCP_HIT;
2008 }
2009 if (!storeEntryValidToSend(e)) {
2010 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
2011 http->entry = NULL;
2012 return LOG_TCP_MISS;
2013 }
2014 if (EBIT_TEST(e->flags, ENTRY_SPECIAL)) {
2015 /* Special entries are always hits, no matter what the client says */
2016 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
2017 http->entry = e;
2018 return LOG_TCP_HIT;
2019 }
2020 #if HTTP_VIOLATIONS
2021 if (e->store_status == STORE_PENDING) {
2022 if (r->flags.nocache || r->flags.nocache_hack) {
2023 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
2024 storeUrl(e));
2025 r->flags.nocache = 0;
2026 r->flags.nocache_hack = 0;
2027 }
2028 }
2029 #endif
2030 if (r->flags.nocache) {
2031 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
2032 http->entry = NULL;
2033 ipcacheReleaseInvalid(r->host);
2034 return LOG_TCP_CLIENT_REFRESH_MISS;
2035 }
2036 if (NULL == r->range) {
2037 (void) 0;
2038 } else if (httpHdrRangeWillBeComplex(r->range)) {
2039 /*
2040 * Some clients break if we return "200 OK" for a Range
2041 * request. We would have to return "200 OK" for a _complex_
2042 * Range request that is also a HIT. Thus, let's prevent HITs
2043 * on complex Range requests
2044 */
2045 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
2046 http->entry = NULL;
2047 r->flags.we_dont_do_ranges = 1;
2048 return LOG_TCP_MISS;
2049 } else if (clientCheckRangeOffsetLimit(e, r->range)) {
2050 debug(33, 1) ("clientProcessRequest2: forcing miss due to range_offset_limit\n");
2051 http->entry = NULL;
2052 r->flags.we_dont_do_ranges = 1;
2053 return LOG_TCP_MISS;
2054 }
2055 debug(33, 3) ("clientProcessRequest2: default HIT\n");
2056 http->entry = e;
2057 return LOG_TCP_HIT;
2058 }
2059
2060 static void
2061 clientProcessRequest(clientHttpRequest * http)
2062 {
2063 char *url = http->uri;
2064 request_t *r = http->request;
2065 int fd = http->conn->fd;
2066 HttpReply *rep;
2067 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
2068 RequestMethodStr[r->method],
2069 url);
2070 if (r->method == METHOD_CONNECT) {
2071 http->log_type = LOG_TCP_MISS;
2072 sslStart(fd, url, r, &http->out.size);
2073 return;
2074 } else if (r->method == METHOD_PURGE) {
2075 clientPurgeRequest(http);
2076 return;
2077 } else if (r->method == METHOD_TRACE) {
2078 if (r->max_forwards == 0) {
2079 http->entry = clientCreateStoreEntry(http, r->method, null_request_flags);
2080 storeReleaseRequest(http->entry);
2081 storeBuffer(http->entry);
2082 rep = httpReplyCreate();
2083 httpReplySetHeaders(rep, 1.0, HTTP_OK, NULL, "text/plain",
2084 httpRequestPrefixLen(r), 0, squid_curtime);
2085 httpReplySwapOut(rep, http->entry);
2086 httpReplyDestroy(rep);
2087 httpRequestSwapOut(r, http->entry);
2088 storeComplete(http->entry);
2089 return;
2090 }
2091 /* yes, continue */
2092 http->log_type = LOG_TCP_MISS;
2093 } else if (r->content_length > 0) {
2094 http->log_type = LOG_TCP_MISS;
2095 /* XXX oof, POST can be cached! */
2096 pumpInit(fd, r, http->uri);
2097 } else {
2098 http->log_type = clientProcessRequest2(http);
2099 }
2100 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
2101 log_tags[http->log_type],
2102 http->uri);
2103 http->out.offset = 0;
2104 if (NULL != http->entry) {
2105 storeLockObject(http->entry);
2106 storeCreateMemObject(http->entry, http->uri, http->log_uri);
2107 http->entry->mem_obj->method = r->method;
2108 http->sc = storeClientListAdd(http->entry, http);
2109 #if DELAY_POOLS
2110 delaySetStoreClient(http->sc, delayClient(r));
2111 #endif
2112 storeClientCopy(http->sc, http->entry,
2113 http->out.offset,
2114 http->out.offset,
2115 CLIENT_SOCK_SZ,
2116 memAllocate(MEM_CLIENT_SOCK_BUF),
2117 clientCacheHit,
2118 http);
2119 } else {
2120 /* MISS CASE, http->log_type is already set! */
2121 clientProcessMiss(http);
2122 }
2123 }
2124
2125 /*
2126 * Prepare to fetch the object as it's a cache miss of some kind.
2127 */
2128 static void
2129 clientProcessMiss(clientHttpRequest * http)
2130 {
2131 char *url = http->uri;
2132 request_t *r = http->request;
2133 ErrorState *err = NULL;
2134 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
2135 RequestMethodStr[r->method], url);
2136 /*
2137 * We might have a left-over StoreEntry from a failed cache hit
2138 * or IMS request.
2139 */
2140 if (http->entry) {
2141 if (EBIT_TEST(http->entry->flags, ENTRY_SPECIAL)) {
2142 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url);
2143 debug(33, 0) ("\tlog_type = %s\n", log_tags[http->log_type]);
2144 storeEntryDump(http->entry, 1);
2145 }
2146 storeUnregister(http->sc, http->entry, http);
2147 http->sc = NULL;
2148 storeUnlockObject(http->entry);
2149 http->entry = NULL;
2150 }
2151 if (clientOnlyIfCached(http)) {
2152 clientProcessOnlyIfCachedMiss(http);
2153 return;
2154 }
2155 /*
2156 * Deny loops when running in accelerator/transproxy mode.
2157 */
2158 if (http->flags.accel && r->flags.loopdetect) {
2159 http->al.http.code = HTTP_FORBIDDEN;
2160 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
2161 err->request = requestLink(r);
2162 err->src_addr = http->conn->peer.sin_addr;
2163 http->entry = clientCreateStoreEntry(http, r->method, null_request_flags);
2164 errorAppendEntry(http->entry, err);
2165 return;
2166 }
2167 assert(http->out.offset == 0);
2168 http->entry = clientCreateStoreEntry(http, r->method, r->flags);
2169 if (http->redirect.status) {
2170 HttpReply *rep = httpReplyCreate();
2171 #if LOG_TCP_REDIRECTS
2172 http->log_type = LOG_TCP_REDIRECT;
2173 #endif
2174 storeReleaseRequest(http->entry);
2175 httpRedirectReply(rep, http->redirect.status, http->redirect.location);
2176 httpReplySwapOut(rep, http->entry);
2177 httpReplyDestroy(rep);
2178 storeComplete(http->entry);
2179 return;
2180 }
2181 if (http->flags.internal)
2182 r->protocol = PROTO_INTERNAL;
2183 fwdStart(http->conn->fd, http->entry, r);
2184 }
2185
2186 static clientHttpRequest *
2187 parseHttpRequestAbort(ConnStateData * conn, const char *uri)
2188 {
2189 clientHttpRequest *http = xcalloc(1, sizeof(clientHttpRequest));
2190 cbdataAdd(http, cbdataXfree, 0);
2191 http->conn = conn;
2192 http->start = current_time;
2193 http->req_sz = conn->in.offset;
2194 http->uri = xstrdup(uri);
2195 http->log_uri = xstrndup(uri, MAX_URL);
2196 http->range_iter.boundary = StringNull;
2197 dlinkAdd(http, &http->active, &ClientActiveRequests);
2198 return http;
2199 }
2200
2201 /*
2202 * parseHttpRequest()
2203 *
2204 * Returns
2205 * NULL on error or incomplete request
2206 * a clientHttpRequest structure on success
2207 */
2208 static clientHttpRequest *
2209 parseHttpRequest(ConnStateData * conn, method_t * method_p, int *status,
2210 char **prefix_p, size_t * req_line_sz_p)
2211 {
2212 char *inbuf = NULL;
2213 char *mstr = NULL;
2214 char *url = NULL;
2215 char *req_hdr = NULL;
2216 float http_ver;
2217 char *token = NULL;
2218 char *t = NULL;
2219 char *end;
2220 int free_request = 0;
2221 size_t header_sz; /* size of headers, not including first line */
2222 size_t prefix_sz; /* size of whole request (req-line + headers) */
2223 size_t url_sz;
2224 size_t req_sz;
2225 method_t method;
2226 clientHttpRequest *http = NULL;
2227 #if IPF_TRANSPARENT
2228 struct natlookup natLookup;
2229 static int natfd = -1;
2230 #endif
2231
2232 if ((req_sz = headersEnd(conn->in.buf, conn->in.offset)) == 0) {
2233 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
2234 *status = 0;
2235 *prefix_p = NULL;
2236 *method_p = METHOD_NONE;
2237 return NULL;
2238 }
2239 assert(req_sz <= conn->in.offset);
2240 /* Use memcpy, not strdup! */
2241 inbuf = xmalloc(req_sz + 1);
2242 xmemcpy(inbuf, conn->in.buf, req_sz);
2243 *(inbuf + req_sz) = '\0';
2244
2245 /* pre-set these values to make aborting simpler */
2246 *prefix_p = inbuf;
2247 *method_p = METHOD_NONE;
2248 *status = -1;
2249
2250 /* Barf on NULL characters in the headers */
2251 if (strlen(inbuf) != req_sz) {
2252 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
2253 return parseHttpRequestAbort(conn, "error:invalid-request");
2254 }
2255 /* Look for request method */
2256 if ((mstr = strtok(inbuf, "\t ")) == NULL) {
2257 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
2258 return parseHttpRequestAbort(conn, "error:invalid-request-method");
2259 }
2260 method = urlParseMethod(mstr);
2261 if (method == METHOD_NONE) {
2262 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr);
2263 return parseHttpRequestAbort(conn, "error:unsupported-request-method");
2264 }
2265 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr);
2266 *method_p = method;
2267
2268 /* look for URL+HTTP/x.x */
2269 if ((url = strtok(NULL, "\n")) == NULL) {
2270 debug(33, 1) ("parseHttpRequest: Missing URL\n");
2271 return parseHttpRequestAbort(conn, "error:missing-url");
2272 }
2273 while (xisspace(*url))
2274 url++;
2275 t = url + strlen(url);
2276 assert(*t == '\0');
2277 token = NULL;
2278 while (t > url) {
2279 t--;
2280 if (xisspace(*t) && !strncmp(t + 1, "HTTP/", 5)) {
2281 token = t + 1;
2282 break;
2283 }
2284 }
2285 while (t > url && xisspace(*t))
2286 *(t--) = '\0';
2287 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url);
2288 if (token == NULL) {
2289 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
2290 #if RELAXED_HTTP_PARSER
2291 http_ver = (float) 0.9; /* wild guess */
2292 #else
2293 return parseHttpRequestAbort(conn, "error:missing-http-ident");
2294 #endif
2295 } else {
2296 http_ver = (float) atof(token + 5);
2297 }
2298
2299 /*
2300 * Process headers after request line
2301 */
2302 req_hdr = strtok(NULL, null_string);
2303 header_sz = req_sz - (req_hdr - inbuf);
2304 if (0 == header_sz) {
2305 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
2306 *status = 0;
2307 return NULL;
2308 }
2309 assert(header_sz > 0);
2310 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr);
2311 end = req_hdr + header_sz;
2312 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end);
2313
2314 prefix_sz = end - inbuf;
2315 *req_line_sz_p = req_hdr - inbuf;
2316 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
2317 (int) prefix_sz, (int) *req_line_sz_p);
2318 assert(prefix_sz <= conn->in.offset);
2319
2320 /* Ok, all headers are received */
2321 http = xcalloc(1, sizeof(clientHttpRequest));
2322 cbdataAdd(http, cbdataXfree, 0);
2323 http->http_ver = http_ver;
2324 http->conn = conn;
2325 http->start = current_time;
2326 http->req_sz = prefix_sz;
2327 http->range_iter.boundary = StringNull;
2328 *prefix_p = xmalloc(prefix_sz + 1);
2329 xmemcpy(*prefix_p, conn->in.buf, prefix_sz);
2330 *(*prefix_p + prefix_sz) = '\0';
2331 dlinkAdd(http, &http->active, &ClientActiveRequests);
2332
2333 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p) + *req_line_sz_p);
2334 if ((t = strchr(url, '#'))) /* remove HTML anchors */
2335 *t = '\0';
2336
2337 /* handle internal objects */
2338 if (internalCheck(url)) {
2339 /* prepend our name & port */
2340 http->uri = xstrdup(internalLocalUri(NULL, url));
2341 http->flags.internal = 1;
2342 http->flags.accel = 1;
2343 }
2344 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2345 else if (Config2.Accel.on && *url == '/') {
2346 /* prepend the accel prefix */
2347 if (opt_accel_uses_host && (t = mime_get_header(req_hdr, "Host"))) {
2348 int vport = (int) Config.Accel.port;
2349 char *q;
2350 /* If a Host: header was specified, use it to build the URL
2351 * instead of the one in the Config file. */
2352 /*
2353 * XXX Use of the Host: header here opens a potential
2354 * security hole. There are no checks that the Host: value
2355 * corresponds to one of your servers. It might, for example,
2356 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2357 * types should be used to prevent httpd-accelerators
2358 * handling requests for non-local servers */
2359 strtok(t, " /;@");
2360 if ((q = strchr(t, ':'))) {
2361 *q++ = '\0';
2362 vport = atoi(q);
2363 }
2364 url_sz = strlen(url) + 32 + Config.appendDomainLen +
2365 strlen(t);
2366 http->uri = xcalloc(url_sz, 1);
2367 snprintf(http->uri, url_sz, "http://%s:%d%s",
2368 t, vport, url);
2369 } else if (vhost_mode) {
2370 int vport;
2371 /* Put the local socket IP address as the hostname */
2372 url_sz = strlen(url) + 32 + Config.appendDomainLen;
2373 http->uri = xcalloc(url_sz, 1);
2374 if (vport_mode)
2375 vport = (int) ntohs(http->conn->me.sin_port);
2376 else
2377 vport = (int) Config.Accel.port;
2378 #if IPF_TRANSPARENT
2379 natLookup.nl_inport = http->conn->me.sin_port;
2380 natLookup.nl_outport = http->conn->peer.sin_port;
2381 natLookup.nl_inip = http->conn->me.sin_addr;
2382 natLookup.nl_outip = http->conn->peer.sin_addr;
2383 natLookup.nl_flags = IPN_TCP;
2384 if (natfd < 0)
2385 natfd = open(IPL_NAT, O_RDONLY, 0);
2386 if (natfd < 0) {
2387 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2388 xstrerror());
2389 return parseHttpRequestAbort(conn, "error:nat-open-failed");
2390 }
2391 if (ioctl(natfd, SIOCGNATL, &natLookup) < 0) {
2392 if (errno != ESRCH) {
2393 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2394 close(natfd);
2395 natfd = -1;
2396 return parseHttpRequestAbort(conn, "error:nat-lookup-failed");
2397 } else
2398 snprintf(http->uri, url_sz, "http://%s:%d%s",
2399 inet_ntoa(http->conn->me.sin_addr),
2400 vport, url);
2401 } else
2402 snprintf(http->uri, url_sz, "http://%s:%d%s",
2403 inet_ntoa(natLookup.nl_realip),
2404 vport, url);
2405 #else
2406 snprintf(http->uri, url_sz, "http://%s:%d%s",
2407 inet_ntoa(http->conn->me.sin_addr),
2408 vport, url);
2409 #endif
2410 debug(33, 5) ("VHOST REWRITE: '%s'\n", http->uri);
2411 } else {
2412 url_sz = strlen(Config2.Accel.prefix) + strlen(url) +
2413 Config.appendDomainLen + 1;
2414 http->uri = xcalloc(url_sz, 1);
2415 snprintf(http->uri, url_sz, "%s%s", Config2.Accel.prefix, url);
2416 }
2417 http->flags.accel = 1;
2418 } else {
2419 /* URL may be rewritten later, so make extra room */
2420 url_sz = strlen(url) + Config.appendDomainLen + 5;
2421 http->uri = xcalloc(url_sz, 1);
2422 strcpy(http->uri, url);
2423 http->flags.accel = 0;
2424 }
2425 if (!stringHasCntl(http->uri))
2426 http->log_uri = xstrndup(http->uri, MAX_URL);
2427 else
2428 http->log_uri = xstrndup(rfc1738_escape_unescaped(http->uri), MAX_URL);
2429 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2430 if (free_request)
2431 safe_free(url);
2432 xfree(inbuf);
2433 *status = 1;
2434 return http;
2435 }
2436
2437 static int
2438 clientReadDefer(int fdnotused, void *data)
2439 {
2440 ConnStateData *conn = data;
2441 return conn->defer.until > squid_curtime;
2442 }
2443
2444 static void
2445 clientReadRequest(int fd, void *data)
2446 {
2447 ConnStateData *conn = data;
2448 int parser_return_code = 0;
2449 int k;
2450 request_t *request = NULL;
2451 int size;
2452 method_t method;
2453 clientHttpRequest *http = NULL;
2454 clientHttpRequest **H = NULL;
2455 char *prefix = NULL;
2456 ErrorState *err = NULL;
2457 fde *F = &fd_table[fd];
2458 int len = conn->in.size - conn->in.offset - 1;
2459 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd);
2460 statCounter.syscalls.sock.reads++;
2461 size = read(fd, conn->in.buf + conn->in.offset, len);
2462 if (size > 0) {
2463 fd_bytes(fd, size, FD_READ);
2464 kb_incr(&statCounter.client_http.kbytes_in, size);
2465 }
2466 /*
2467 * Don't reset the timeout value here. The timeout value will be
2468 * set to Config.Timeout.request by httpAccept() and
2469 * clientWriteComplete(), and should apply to the request as a
2470 * whole, not individual read() calls. Plus, it breaks our
2471 * lame half-close detection
2472 */
2473 commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, conn, 0);
2474 if (size == 0) {
2475 if (conn->chr == NULL) {
2476 /* no current or pending requests */
2477 comm_close(fd);
2478 return;
2479 } else if (!Config.onoff.half_closed_clients) {
2480 /* admin doesn't want to support half-closed client sockets */
2481 comm_close(fd);
2482 return;
2483 }
2484 /* It might be half-closed, we can't tell */
2485 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd);
2486 F->flags.socket_eof = 1;
2487 conn->defer.until = squid_curtime + 1;
2488 conn->defer.n++;
2489 fd_note(fd, "half-closed");
2490 return;
2491 } else if (size < 0) {
2492 if (!ignoreErrno(errno)) {
2493 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd, xstrerror());
2494 comm_close(fd);
2495 return;
2496 } else if (conn->in.offset == 0) {
2497 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd, xstrerror());
2498 return;
2499 }
2500 /* Continue to process previously read data */
2501 size = 0;
2502 }
2503 conn->in.offset += size;
2504 /* Skip leading (and trailing) whitespace */
2505 while (conn->in.offset > 0) {
2506 int nrequests;
2507 size_t req_line_sz;
2508 while (conn->in.offset > 0 && xisspace(conn->in.buf[0])) {
2509 xmemmove(conn->in.buf, conn->in.buf + 1, conn->in.offset - 1);
2510 conn->in.offset--;
2511 }
2512 conn->in.buf[conn->in.offset] = '\0'; /* Terminate the string */
2513 if (conn->in.offset == 0)
2514 break;
2515 /* Limit the number of concurrent requests to 2 */
2516 for (H = &conn->chr, nrequests = 0; *H; H = &(*H)->next, nrequests++);
2517 if (nrequests >= 2) {
2518 debug(33, 3) ("clientReadRequest: FD %d max concurrent requests reached\n", fd);
2519 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd);
2520 conn->defer.until = squid_curtime + 100; /* Reset when a request is complete */
2521 break;
2522 }
2523 /* Process request */
2524 http = parseHttpRequest(conn,
2525 &method,
2526 &parser_return_code,
2527 &prefix,
2528 &req_line_sz);
2529 if (!http)
2530 safe_free(prefix);
2531 if (http) {
2532 assert(http->req_sz > 0);
2533 conn->in.offset -= http->req_sz;
2534 assert(conn->in.offset >= 0);
2535 debug(33, 5) ("conn->in.offset = %d\n", (int) conn->in.offset);
2536 /*
2537 * If we read past the end of this request, move the remaining
2538 * data to the beginning
2539 */
2540 if (conn->in.offset > 0)
2541 xmemmove(conn->in.buf, conn->in.buf + http->req_sz, conn->in.offset);
2542 /* add to the client request queue */
2543 for (H = &conn->chr; *H; H = &(*H)->next);
2544 *H = http;
2545 conn->nrequests++;
2546 commSetTimeout(fd, Config.Timeout.lifetime, NULL, NULL);
2547 if (parser_return_code < 0) {
2548 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd);
2549 err = errorCon(ERR_INVALID_REQ, HTTP_BAD_REQUEST);
2550 err->request_hdrs = xstrdup(conn->in.buf);
2551 http->entry = clientCreateStoreEntry(http, method, null_request_flags);
2552 errorAppendEntry(http->entry, err);
2553 safe_free(prefix);
2554 break;
2555 }
2556 if ((request = urlParse(method, http->uri)) == NULL) {
2557 debug(33, 5) ("Invalid URL: %s\n", http->uri);
2558 err = errorCon(ERR_INVALID_URL, HTTP_BAD_REQUEST);
2559 err->src_addr = conn->peer.sin_addr;
2560 err->url = xstrdup(http->uri);
2561 http->al.http.code = err->http_status;
2562 http->entry = clientCreateStoreEntry(http, method, null_request_flags);
2563 errorAppendEntry(http->entry, err);
2564 safe_free(prefix);
2565 break;
2566 } else {
2567 /* compile headers */
2568 /* we should skip request line! */
2569 if (!httpRequestParseHeader(request, prefix + req_line_sz))
2570 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2571 http->uri, prefix);
2572 /* continue anyway? */
2573 }
2574 request->flags.accelerated = http->flags.accel;
2575 if (!http->flags.internal) {
2576 if (internalCheck(strBuf(request->urlpath))) {
2577 if (internalHostnameIs(request->host) &&
2578 request->port == ntohs(Config.Sockaddr.http->s.sin_port)) {
2579 http->flags.internal = 1;
2580 } else if (internalStaticCheck(strBuf(request->urlpath))) {
2581 xstrncpy(request->host, internalHostname(), SQUIDHOSTNAMELEN);
2582 request->port = ntohs(Config.Sockaddr.http->s.sin_port);
2583 http->flags.internal = 1;
2584 }
2585 }
2586 }
2587 /*
2588 * cache the Content-length value in request_t.
2589 */
2590 request->content_length = httpHeaderGetInt(&request->header,
2591 HDR_CONTENT_LENGTH);
2592 request->flags.internal = http->flags.internal;
2593 safe_free(prefix);
2594 safe_free(http->log_uri);
2595 http->log_uri = xstrdup(urlCanonicalClean(request));
2596 request->client_addr = conn->peer.sin_addr;
2597 request->my_addr = conn->me.sin_addr;
2598 request->my_port = ntohs(conn->me.sin_port);
2599 request->http_ver = http->http_ver;
2600 if (!urlCheckRequest(request)) {
2601 err = errorCon(ERR_UNSUP_REQ, HTTP_NOT_IMPLEMENTED);
2602 err->src_addr = conn->peer.sin_addr;
2603 err->request = requestLink(request);
2604 request->flags.proxy_keepalive = 0;
2605 http->al.http.code = err->http_status;
2606 http->entry = clientCreateStoreEntry(http, request->method, null_request_flags);
2607 errorAppendEntry(http->entry, err);
2608 break;
2609 }
2610 if (0 == clientCheckContentLength(request)) {
2611 err = errorCon(ERR_INVALID_REQ, HTTP_LENGTH_REQUIRED);
2612 err->src_addr = conn->peer.sin_addr;
2613 err->request = requestLink(request);
2614 http->al.http.code = err->http_status;
2615 http->entry = clientCreateStoreEntry(http, request->method, null_request_flags);
2616 errorAppendEntry(http->entry, err);
2617 break;
2618 }
2619 http->request = requestLink(request);
2620 /*
2621 * We need to set the keepalive flag before doing some
2622 * hacks for POST/PUT requests below. Maybe we could
2623 * set keepalive flag even earlier.
2624 */
2625 clientSetKeepaliveFlag(http);
2626 /*
2627 * break here if the request has a content-length
2628 * because there is a reqeust body following and we
2629 * don't want to parse it as though it was new request.
2630 */
2631 if (request->content_length >= 0) {
2632 int copy_len = XMIN(conn->in.offset, request->content_length);
2633 if (copy_len > 0) {
2634 assert(conn->in.offset >= copy_len);
2635 request->body_sz = copy_len;
2636 request->body = xmalloc(request->body_sz);
2637 xmemcpy(request->body, conn->in.buf, request->body_sz);
2638 conn->in.offset -= copy_len;
2639 if (conn->in.offset)
2640 xmemmove(conn->in.buf, conn->in.buf + copy_len, conn->in.offset);
2641 }
2642 /*
2643 * if we didn't get the full body now, then more will
2644 * be arriving on the client socket. Lets cancel
2645 * the read handler until this request gets forwarded.
2646 */
2647 if (request->body_sz < request->content_length)
2648 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
2649 if (request->content_length < 0)
2650 (void) 0;
2651 else if (clientRequestBodyTooLarge(request->content_length)) {
2652 err = errorCon(ERR_TOO_BIG, HTTP_REQUEST_ENTITY_TOO_LARGE);
2653 err->request = requestLink(request);
2654 http->entry = clientCreateStoreEntry(http,
2655 METHOD_NONE, null_request_flags);
2656 errorAppendEntry(http->entry, err);
2657 break;
2658 }
2659 }
2660 clientAccessCheck(http);
2661 continue; /* while offset > 0 */
2662 } else if (parser_return_code == 0) {
2663 /*
2664 * Partial request received; reschedule until parseHttpRequest()
2665 * is happy with the input
2666 */
2667 k = conn->in.size - 1 - conn->in.offset;
2668 if (k == 0) {
2669 if (conn->in.offset >= Config.maxRequestHeaderSize) {
2670 /* The request is too large to handle */
2671 debug(33, 1) ("Request header is too large (%d bytes)\n",
2672 (int) conn->in.offset);
2673 debug(33, 1) ("Config 'request_header_max_size'= %d bytes.\n",
2674 Config.maxRequestHeaderSize);
2675 err = errorCon(ERR_TOO_BIG, HTTP_REQUEST_ENTITY_TOO_LARGE);
2676 http = parseHttpRequestAbort(conn, "error:request-too-large");
2677 /* add to the client request queue */
2678 for (H = &conn->chr; *H; H = &(*H)->next);
2679 *H = http;
2680 http->entry = clientCreateStoreEntry(http, METHOD_NONE, null_request_flags);
2681 errorAppendEntry(http->entry, err);
2682 return;
2683 }
2684 /* Grow the request memory area to accomodate for a large request */
2685 conn->in.size += REQUEST_BUF_SIZE;
2686 conn->in.buf = xrealloc(conn->in.buf, conn->in.size);
2687 /* XXX account conn->in.buf */
2688 debug(33, 3) ("Handling a large request, offset=%d inbufsize=%d\n",
2689 (int) conn->in.offset, conn->in.size);
2690 k = conn->in.size - 1 - conn->in.offset;
2691 }
2692 break;
2693 }
2694 }
2695 }
2696
2697 /* general lifetime handler for HTTP requests */
2698 static void
2699 requestTimeout(int fd, void *data)
2700 {
2701 #if THIS_CONFUSES_PERSISTENT_CONNECTION_AWARE_BROWSERS_AND_USERS
2702 ConnStateData *conn = data;
2703 ErrorState *err;
2704 debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd);
2705 if (fd_table[fd].rwstate) {
2706 /*
2707 * Some data has been sent to the client, just close the FD
2708 */
2709 comm_close(fd);
2710 } else if (conn->nrequests) {
2711 /*
2712 * assume its a persistent connection; just close it
2713 */
2714 comm_close(fd);
2715 } else {
2716 /*
2717 * Generate an error
2718 */
2719 err = errorCon(ERR_LIFETIME_EXP, HTTP_REQUEST_TIMEOUT);
2720 err->url = xstrdup("N/A");
2721 /*
2722 * Normally we shouldn't call errorSend() in client_side.c, but
2723 * it should be okay in this case. Presumably if we get here
2724 * this is the first request for the connection, and no data
2725 * has been written yet
2726 */
2727 assert(conn->chr == NULL);
2728 errorSend(fd, err);
2729 /*
2730 * if we don't close() here, we still need a timeout handler!
2731 */
2732 commSetTimeout(fd, 30, requestTimeout, conn);
2733 /*
2734 * Aha, but we don't want a read handler!
2735 */
2736 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
2737 }
2738 #else
2739 /*
2740 * Just close the connection to not confuse browsers
2741 * using persistent connections. Some browsers opens
2742 * an connection and then does not use it until much
2743 * later (presumeably because the request triggering
2744 * the open has already been completed on another
2745 * connection)
2746 */
2747 debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd);
2748 comm_close(fd);
2749 #endif
2750 }
2751
2752 static int
2753 httpAcceptDefer(int fdunused, void *dataunused)
2754 {
2755 static time_t last_warn = 0;
2756 if (fdNFree() >= RESERVED_FD)
2757 return 0;
2758 if (last_warn + 15 < squid_curtime) {
2759 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2760 last_warn = squid_curtime;
2761 }
2762 return 1;
2763 }
2764
2765 /* Handle a new connection on HTTP socket. */
2766 void
2767 httpAccept(int sock, void *data)
2768 {
2769 int *N = data;
2770 int fd = -1;
2771 ConnStateData *connState = NULL;
2772 struct sockaddr_in peer;
2773 struct sockaddr_in me;
2774 int max = INCOMING_HTTP_MAX;
2775 #if USE_IDENT
2776 static aclCheck_t identChecklist;
2777 #endif
2778 commSetSelect(sock, COMM_SELECT_READ, httpAccept, NULL, 0);
2779 while (max-- && !httpAcceptDefer(sock, NULL)) {
2780 memset(&peer, '\0', sizeof(struct sockaddr_in));
2781 memset(&me, '\0', sizeof(struct sockaddr_in));
2782 if ((fd = comm_accept(sock, &peer, &me)) < 0) {
2783 if (!ignoreErrno(errno))
2784 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2785 sock, xstrerror());
2786 break;
2787 }
2788 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd);
2789 connState = memAllocate(MEM_CONNSTATEDATA);
2790 connState->peer = peer;
2791 connState->log_addr = peer.sin_addr;
2792 connState->log_addr.s_addr &= Config.Addrs.client_netmask.s_addr;
2793 connState->me = me;
2794 connState->fd = fd;
2795 connState->in.size = REQUEST_BUF_SIZE;
2796 connState->in.buf = xcalloc(connState->in.size, 1);
2797 cbdataAdd(connState, memFree, MEM_CONNSTATEDATA);
2798 /* XXX account connState->in.buf */
2799 comm_add_close_handler(fd, connStateFree, connState);
2800 if (Config.onoff.log_fqdn)
2801 fqdncache_gethostbyaddr(peer.sin_addr, FQDN_LOOKUP_IF_MISS);
2802 commSetTimeout(fd, Config.Timeout.request, requestTimeout, connState);
2803 #if USE_IDENT
2804 identChecklist.src_addr = peer.sin_addr;
2805 identChecklist.my_addr = me.sin_addr;
2806 identChecklist.my_port = ntohs(me.sin_port);
2807 if (aclCheckFast(Config.accessList.identLookup, &identChecklist))
2808 identStart(&me, &peer, clientIdentDone, connState);
2809 #endif
2810 commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, connState, 0);
2811 commSetDefer(fd, clientReadDefer, connState);
2812 clientdbEstablished(peer.sin_addr, 1);
2813 assert(N);
2814 (*N)++;
2815 }
2816 }
2817
2818 #define SENDING_BODY 0
2819 #define SENDING_HDRSONLY 1
2820 static int
2821 clientCheckTransferDone(clientHttpRequest * http)
2822 {
2823 int sending = SENDING_BODY;
2824 StoreEntry *entry = http->entry;
2825 MemObject *mem;
2826 http_reply *reply;
2827 int sendlen;
2828 if (entry == NULL)
2829 return 0;
2830 /*
2831 * For now, 'done_copying' is used for special cases like
2832 * Range and HEAD requests.
2833 */
2834 if (http->flags.done_copying)
2835 return 1;
2836 /*
2837 * Handle STORE_OK objects.
2838 * objectLen(entry) will be set proprely.
2839 */
2840 if (entry->store_status == STORE_OK) {
2841 if (http->out.offset >= objectLen(entry))
2842 return 1;
2843 else
2844 return 0;
2845 }
2846 /*
2847 * Now, handle STORE_PENDING objects
2848 */
2849 mem = entry->mem_obj;
2850 assert(mem != NULL);
2851 assert(http->request != NULL);
2852 reply = mem->reply;
2853 if (reply->hdr_sz == 0)
2854 return 0; /* haven't found end of headers yet */
2855 else if (reply->sline.status == HTTP_OK)
2856 sending = SENDING_BODY;
2857 else if (reply->sline.status == HTTP_NO_CONTENT)
2858 sending = SENDING_HDRSONLY;
2859 else if (reply->sline.status == HTTP_NOT_MODIFIED)
2860 sending = SENDING_HDRSONLY;
2861 else if (reply->sline.status < HTTP_OK)
2862 sending = SENDING_HDRSONLY;
2863 else if (http->request->method == METHOD_HEAD)
2864 sending = SENDING_HDRSONLY;
2865 else
2866 sending = SENDING_BODY;
2867 /*
2868 * Figure out how much data we are supposed to send.
2869 * If we are sending a body and we don't have a content-length,
2870 * then we must wait for the object to become STORE_OK.
2871 */
2872 if (sending == SENDING_HDRSONLY)
2873 sendlen = reply->hdr_sz;
2874 else if (reply->content_length < 0)
2875 return 0;
2876 else
2877 sendlen = reply->content_length + reply->hdr_sz;
2878 /*
2879 * Now that we have the expected length, did we send it all?
2880 */
2881 if (http->out.offset < sendlen)
2882 return 0;
2883 else
2884 return 1;
2885 }
2886
2887 static int
2888 clientGotNotEnough(clientHttpRequest * http)
2889 {
2890 int cl = httpReplyBodySize(http->request->method, http->entry->mem_obj->reply);
2891 int hs = http->entry->mem_obj->reply->hdr_sz;
2892 assert(cl >= 0);
2893 if (http->out.offset < cl + hs)
2894 return 1;
2895 return 0;
2896 }
2897
2898 /*
2899 * This function is designed to serve a fairly specific purpose.
2900 * Occasionally our vBNS-connected caches can talk to each other, but not
2901 * the rest of the world. Here we try to detect frequent failures which
2902 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2903 * the failure:success ratio goes above 1.0 then we go into "hit only"
2904 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2905 * will only fetch HITs from us if they are using the ICP protocol. We
2906 * stay in this mode for 5 minutes.
2907 *
2908 * Duane W., Sept 16, 1996
2909 */
2910
2911 static void
2912 checkFailureRatio(err_type etype, hier_code hcode)
2913 {
2914 static double magic_factor = 100.0;
2915 double n_good;
2916 double n_bad;
2917 if (hcode == HIER_NONE)
2918 return;
2919 n_good = magic_factor / (1.0 + request_failure_ratio);
2920 n_bad = magic_factor - n_good;
2921 switch (etype) {
2922 case ERR_DNS_FAIL:
2923 case ERR_CONNECT_FAIL:
2924 case ERR_READ_ERROR:
2925 n_bad++;
2926 break;
2927 default:
2928 n_good++;
2929 }
2930 request_failure_ratio = n_bad / n_good;
2931 if (hit_only_mode_until > squid_curtime)
2932 return;
2933 if (request_failure_ratio < 1.0)
2934 return;
2935 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio);
2936 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2937 FAILURE_MODE_TIME / 60);
2938 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
2939 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
2940 }
2941
2942 void
2943 clientHttpConnectionsOpen(void)
2944 {
2945 sockaddr_in_list *s;
2946 int fd;
2947 for (s = Config.Sockaddr.http; s; s = s->next) {
2948 if (MAXHTTPPORTS == NHttpSockets) {
2949 debug(1, 1) ("WARNING: You have too many 'http_port' lines.\n");
2950 debug(1, 1) (" The limit is %d\n", MAXHTTPPORTS);
2951 continue;
2952 }
2953 enter_suid();
2954 fd = comm_open(SOCK_STREAM,
2955 0,
2956 s->s.sin_addr,
2957 ntohs(s->s.sin_port),
2958 COMM_NONBLOCKING,
2959 "HTTP Socket");
2960 leave_suid();
2961 if (fd < 0)
2962 continue;
2963 comm_listen(fd);
2964 commSetSelect(fd, COMM_SELECT_READ, httpAccept, NULL, 0);
2965 /*
2966 * We need to set a defer handler here so that we don't
2967 * peg the CPU with select() when we hit the FD limit.
2968 */
2969 commSetDefer(fd, httpAcceptDefer, NULL);
2970 debug(1, 1) ("Accepting HTTP connections at %s, port %d, FD %d.\n",
2971 inet_ntoa(s->s.sin_addr),
2972 (int) ntohs(s->s.sin_port),
2973 fd);
2974 HttpSockets[NHttpSockets++] = fd;
2975 }
2976 if (NHttpSockets < 1)
2977 fatal("Cannot open HTTP Port");
2978 }
2979
2980 void
2981 clientHttpConnectionsClose(void)
2982 {
2983 int i;
2984 for (i = 0; i < NHttpSockets; i++) {
2985 if (HttpSockets[i] >= 0) {
2986 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets[i]);
2987 comm_close(HttpSockets[i]);
2988 HttpSockets[i] = -1;
2989 }
2990 }
2991 NHttpSockets = 0;
2992 }