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