3 * $Id: client_side.cc,v 1.441 1999/03/24 00:17:03 rousskov Exp $
5 * DEBUG: section 33 Client-side Routines
6 * AUTHOR: Duane Wessels
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
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 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
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.
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.
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.
40 #include <sys/ioctl.h>
42 #include <netinet/tcp.h>
44 #include <ip_compat.h>
52 #define comm_close comm_lingering_close
55 static const char *const crlf
= "\r\n";
57 #define REQUEST_BUF_SIZE 4096
58 #define FAILURE_MODE_TIME 300
62 static CWCB clientWriteComplete
;
63 static PF clientReadRequest
;
64 static PF connStateFree
;
65 static PF requestTimeout
;
66 static int clientCheckTransferDone(clientHttpRequest
*);
67 static int clientGotNotEnough(clientHttpRequest
*);
68 static void checkFailureRatio(err_type
, hier_code
);
69 static void clientProcessMiss(clientHttpRequest
*);
70 static void clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
);
71 static clientHttpRequest
*parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
);
72 static clientHttpRequest
*parseHttpRequest(ConnStateData
*, method_t
*, int *, char **, size_t *);
73 static RH clientRedirectDone
;
74 static STCB clientHandleIMSReply
;
75 static int clientGetsOldEntry(StoreEntry
* new, StoreEntry
* old
, request_t
* request
);
76 static int checkAccelOnly(clientHttpRequest
*);
78 static IDCB clientIdentDone
;
80 static int clientOnlyIfCached(clientHttpRequest
* http
);
81 static STCB clientSendMoreData
;
82 static STCB clientCacheHit
;
83 static void clientSetKeepaliveFlag(clientHttpRequest
*);
84 static void clientInterpretRequestHeaders(clientHttpRequest
*);
85 static void clientProcessRequest(clientHttpRequest
*);
86 static void clientProcessExpired(void *data
);
87 static void clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
);
88 static int clientCachable(clientHttpRequest
* http
);
89 static int clientHierarchical(clientHttpRequest
* http
);
90 static int clientCheckContentLength(request_t
* r
);
91 static int httpAcceptDefer(void);
92 static log_type
clientProcessRequest2(clientHttpRequest
* http
);
95 checkAccelOnly(clientHttpRequest
* http
)
97 /* return TRUE if someone makes a proxy request to us and
98 * we are in httpd-accel only mode */
99 if (!Config2
.Accel
.on
)
101 if (Config
.onoff
.accel_with_proxy
)
103 if (http
->request
->protocol
== PROTO_CACHEOBJ
)
105 if (http
->flags
.accel
)
112 clientIdentDone(const char *ident
, void *data
)
114 ConnStateData
*conn
= data
;
116 xstrncpy(conn
->ident
, ident
, sizeof(conn
->ident
));
118 xstrncpy(conn
->ident
, "-", sizeof(conn
->ident
));
123 clientAccessCheck(void *data
)
125 clientHttpRequest
*http
= data
;
126 ConnStateData
*conn
= http
->conn
;
128 if (checkAccelOnly(http
)) {
129 clientAccessCheckDone(0, http
);
132 browser
= httpHeaderGetStr(&http
->request
->header
, HDR_USER_AGENT
);
133 http
->acl_checklist
= aclChecklistCreate(Config
.accessList
.http
,
141 * hack for ident ACL. It needs to get full addresses, and a
142 * place to store the ident result on persistent connections...
144 http
->acl_checklist
->conn
= conn
;
145 cbdataLock(http
->acl_checklist
->conn
);
147 aclNBCheck(http
->acl_checklist
, clientAccessCheckDone
, http
);
151 * returns true if client specified that the object must come from the cache
152 * witout contacting origin server
155 clientOnlyIfCached(clientHttpRequest
* http
)
157 const request_t
*r
= http
->request
;
159 return r
->cache_control
&&
160 EBIT_TEST(r
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
164 clientCreateStoreEntry(clientHttpRequest
* h
, method_t m
, request_flags flags
)
168 * For erroneous requests, we might not have a h->request,
169 * so make a fake one.
171 if (h
->request
== NULL
)
172 h
->request
= requestLink(requestCreate(m
, PROTO_NONE
, NULL
));
173 e
= storeCreateEntry(h
->uri
, h
->log_uri
, flags
, m
);
174 storeClientListAdd(e
, h
);
176 delaySetStoreClient(e
, h
, delayClient(h
->request
));
178 storeClientCopy(e
, 0, 0, CLIENT_SOCK_SZ
,
179 memAllocate(MEM_CLIENT_SOCK_BUF
), clientSendMoreData
, h
);
184 clientAccessCheckDone(int answer
, void *data
)
186 clientHttpRequest
*http
= data
;
189 ErrorState
*err
= NULL
;
190 debug(33, 5) ("clientAccessCheckDone: '%s' answer=%d\n", http
->uri
, answer
);
191 http
->acl_checklist
= NULL
;
192 if (answer
== ACCESS_ALLOWED
) {
193 safe_free(http
->uri
);
194 http
->uri
= xstrdup(urlCanonical(http
->request
));
195 assert(http
->redirect_state
== REDIRECT_NONE
);
196 http
->redirect_state
= REDIRECT_PENDING
;
197 redirectStart(http
, clientRedirectDone
, http
);
199 debug(33, 5) ("Access Denied: %s\n", http
->uri
);
200 debug(33, 5) ("AclMatchedName = %s\n",
201 AclMatchedName
? AclMatchedName
: "<null>");
202 http
->log_type
= LOG_TCP_DENIED
;
203 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
205 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
206 if (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
)) {
207 if (!http
->flags
.accel
) {
208 /* Proxy authorisation needed */
209 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
211 /* WWW authorisation needed */
212 status
= HTTP_UNAUTHORIZED
;
215 page_id
= ERR_CACHE_ACCESS_DENIED
;
217 status
= HTTP_FORBIDDEN
;
219 page_id
= ERR_ACCESS_DENIED
;
221 err
= errorCon(page_id
, status
);
222 err
->request
= requestLink(http
->request
);
223 err
->src_addr
= http
->conn
->peer
.sin_addr
;
224 errorAppendEntry(http
->entry
, err
);
229 clientRedirectDone(void *data
, char *result
)
231 clientHttpRequest
*http
= data
;
232 request_t
*new_request
= NULL
;
233 request_t
*old_request
= http
->request
;
234 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
235 result
? result
: "NULL");
236 assert(http
->redirect_state
== REDIRECT_PENDING
);
237 http
->redirect_state
= REDIRECT_DONE
;
239 http_status status
= atoi(result
);
240 if (status
== 301 || status
== 302) {
242 if ((t
= strchr(result
, ':')) != NULL
) {
243 http
->redirect
.status
= status
;
244 http
->redirect
.location
= xstrdup(t
+ 1);
246 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result
);
249 if (strcmp(result
, http
->uri
))
250 new_request
= urlParse(old_request
->method
, result
);
253 safe_free(http
->uri
);
254 http
->uri
= xstrdup(urlCanonical(new_request
));
255 new_request
->http_ver
= old_request
->http_ver
;
256 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
257 new_request
->client_addr
= old_request
->client_addr
;
258 new_request
->my_addr
= old_request
->my_addr
;
259 new_request
->flags
.redirected
= 1;
260 if (old_request
->body
) {
261 new_request
->body
= xmalloc(old_request
->body_sz
);
262 xmemcpy(new_request
->body
, old_request
->body
, old_request
->body_sz
);
263 new_request
->body_sz
= old_request
->body_sz
;
265 requestUnlink(old_request
);
266 http
->request
= requestLink(new_request
);
268 clientInterpretRequestHeaders(http
);
269 fd_note(http
->conn
->fd
, http
->uri
);
270 clientProcessRequest(http
);
274 clientProcessExpired(void *data
)
276 clientHttpRequest
*http
= data
;
277 char *url
= http
->uri
;
278 StoreEntry
*entry
= NULL
;
279 debug(33, 3) ("clientProcessExpired: '%s'\n", http
->uri
);
280 assert(http
->entry
->lastmod
>= 0);
282 * check if we are allowed to contact other servers
283 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
284 * a stale entry *if* it matches client requirements
286 if (clientOnlyIfCached(http
)) {
287 clientProcessOnlyIfCachedMiss(http
);
290 http
->request
->flags
.refresh
= 1;
291 http
->old_entry
= http
->entry
;
292 entry
= storeCreateEntry(url
,
294 http
->request
->flags
,
295 http
->request
->method
);
296 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
297 storeClientListAdd(entry
, http
);
298 storeClientListAdd(http
->old_entry
, http
);
300 /* delay_id is already set on original store client */
301 delaySetStoreClient(entry
, http
, delayClient(http
->request
));
303 entry
->lastmod
= http
->old_entry
->lastmod
;
304 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry
->lastmod
);
305 entry
->refcount
++; /* EXPIRED CASE */
307 http
->out
.offset
= 0;
308 fwdStart(http
->conn
->fd
, http
->entry
, http
->request
,
309 http
->conn
->peer
.sin_addr
, http
->conn
->me
.sin_addr
);
310 /* Register with storage manager to receive updates when data comes in. */
311 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
312 debug(33, 0) ("clientProcessExpired: found ENTRY_ABORTED object\n");
313 storeClientCopy(entry
,
317 memAllocate(MEM_CLIENT_SOCK_BUF
),
318 clientHandleIMSReply
,
323 clientGetsOldEntry(StoreEntry
* new_entry
, StoreEntry
* old_entry
, request_t
* request
)
325 const http_status status
= new_entry
->mem_obj
->reply
->sline
.status
;
327 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
330 /* If the reply is a failure then send the old object as a last
332 if (status
>= 500 && status
< 600) {
333 debug(33, 2) ("clientGetsOldEntry: YES, failure reply=%d\n", status
);
336 /* If the reply is anything but "Not Modified" then
337 * we must forward it to the client */
338 if (HTTP_NOT_MODIFIED
!= status
) {
339 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status
);
342 /* If the client did not send IMS in the request, then it
343 * must get the old object, not this "Not Modified" reply */
344 if (!request
->flags
.ims
) {
345 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
348 /* If the client IMS time is prior to the entry LASTMOD time we
349 * need to send the old object */
350 if (modifiedSince(old_entry
, request
)) {
351 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
355 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
361 clientHandleIMSReply(void *data
, char *buf
, ssize_t size
)
363 clientHttpRequest
*http
= data
;
364 StoreEntry
*entry
= http
->entry
;
365 MemObject
*mem
= entry
->mem_obj
;
366 const char *url
= storeUrl(entry
);
367 int unlink_request
= 0;
368 StoreEntry
*oldentry
;
370 const http_status status
= mem
->reply
->sline
.status
;
371 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url
, (int) size
);
372 if (size
< 0 && !EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
373 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
376 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
377 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url
);
378 /* We have an existing entry, but failed to validate it */
379 /* Its okay to send the old one anyway */
380 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
381 storeUnregister(entry
, http
);
382 storeUnlockObject(entry
);
383 entry
= http
->entry
= http
->old_entry
;
385 } else if (STORE_PENDING
== entry
->store_status
&& 0 == status
) {
386 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url
);
387 if (size
>= CLIENT_SOCK_SZ
) {
388 /* will not get any bigger than that */
389 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url
);
390 /* use old entry, this repeats the code abovez */
391 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
392 storeUnregister(entry
, http
);
393 storeUnlockObject(entry
);
394 entry
= http
->entry
= http
->old_entry
;
398 storeClientCopy(entry
,
399 http
->out
.offset
+ size
,
403 clientHandleIMSReply
,
407 } else if (clientGetsOldEntry(entry
, http
->old_entry
, http
->request
)) {
408 /* We initiated the IMS request, the client is not expecting
409 * 304, so put the good one back. First, make sure the old entry
410 * headers have been loaded from disk. */
411 oldentry
= http
->old_entry
;
412 http
->log_type
= LOG_TCP_REFRESH_HIT
;
413 if (oldentry
->mem_obj
->request
== NULL
) {
414 oldentry
->mem_obj
->request
= requestLink(mem
->request
);
417 /* Don't memcpy() the whole reply structure here. For example,
418 * www.thegist.com (Netscape/1.13) returns a content-length for
419 * 304's which seems to be the length of the 304 HEADERS!!! and
420 * not the body they refer to. */
421 httpReplyUpdateOnNotModified(oldentry
->mem_obj
->reply
, mem
->reply
);
422 storeTimestampsSet(oldentry
);
423 storeUnregister(entry
, http
);
424 storeUnlockObject(entry
);
425 entry
= http
->entry
= oldentry
;
426 entry
->timestamp
= squid_curtime
;
427 if (unlink_request
) {
428 requestUnlink(entry
->mem_obj
->request
);
429 entry
->mem_obj
->request
= NULL
;
432 /* the client can handle this reply, whatever it is */
433 http
->log_type
= LOG_TCP_REFRESH_MISS
;
434 if (HTTP_NOT_MODIFIED
== mem
->reply
->sline
.status
) {
435 http
->old_entry
->timestamp
= squid_curtime
;
436 http
->old_entry
->refcount
++;
437 http
->log_type
= LOG_TCP_REFRESH_HIT
;
439 storeUnregister(http
->old_entry
, http
);
440 storeUnlockObject(http
->old_entry
);
443 http
->old_entry
= NULL
; /* done with old_entry */
444 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
446 storeClientCopy(entry
,
454 clientSendMoreData(data
, buf
, size
);
459 modifiedSince(StoreEntry
* entry
, request_t
* request
)
462 MemObject
*mem
= entry
->mem_obj
;
463 time_t mod_time
= entry
->lastmod
;
464 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry
));
466 mod_time
= entry
->timestamp
;
467 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time
);
470 /* Find size of the object */
471 object_length
= mem
->reply
->content_length
;
472 if (object_length
< 0)
473 object_length
= contentLen(entry
);
474 if (mod_time
> request
->ims
) {
475 debug(33, 3) ("--> YES: entry newer than client\n");
477 } else if (mod_time
< request
->ims
) {
478 debug(33, 3) ("--> NO: entry older than client\n");
480 } else if (request
->imslen
< 0) {
481 debug(33, 3) ("--> NO: same LMT, no client length\n");
483 } else if (request
->imslen
== object_length
) {
484 debug(33, 3) ("--> NO: same LMT, same length\n");
487 debug(33, 3) ("--> YES: same LMT, different length\n");
493 clientPurgeRequest(clientHttpRequest
* http
)
496 ErrorState
*err
= NULL
;
498 debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config
.onoff
.enable_purge
);
499 if (!Config
.onoff
.enable_purge
) {
500 http
->log_type
= LOG_TCP_DENIED
;
501 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
502 err
->request
= requestLink(http
->request
);
503 err
->src_addr
= http
->conn
->peer
.sin_addr
;
504 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
505 errorAppendEntry(http
->entry
, err
);
508 http
->log_type
= LOG_TCP_MISS
;
509 if ((entry
= storeGetPublic(http
->uri
, METHOD_GET
)) == NULL
) {
510 http
->http_code
= HTTP_NOT_FOUND
;
513 http
->http_code
= HTTP_OK
;
515 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
518 * Make a new entry to hold the reply to be written
521 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
522 httpReplyReset(r
= http
->entry
->mem_obj
->reply
);
523 httpReplySetHeaders(r
, 1.0, http
->http_code
, NULL
, NULL
, 0, 0, -1);
524 httpReplySwapOut(r
, http
->entry
);
525 storeComplete(http
->entry
);
529 checkNegativeHit(StoreEntry
* e
)
531 if (!EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
533 if (e
->expires
<= squid_curtime
)
535 if (e
->store_status
!= STORE_OK
)
541 clientUpdateCounters(clientHttpRequest
* http
)
543 int svc_time
= tvSubMsec(http
->start
, current_time
);
545 HierarchyLogEntry
*H
;
546 Counter
.client_http
.requests
++;
547 if (isTcpHit(http
->log_type
))
548 Counter
.client_http
.hits
++;
549 if (http
->request
->err_type
!= ERR_NONE
)
550 Counter
.client_http
.errors
++;
551 statHistCount(&Counter
.client_http
.all_svc_time
, svc_time
);
553 * The idea here is not to be complete, but to get service times
554 * for only well-defined types. For example, we don't include
555 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
556 * (we *tried* to validate it, but failed).
558 switch (http
->log_type
) {
559 case LOG_TCP_REFRESH_HIT
:
560 statHistCount(&Counter
.client_http
.nh_svc_time
, svc_time
);
562 case LOG_TCP_IMS_HIT
:
563 statHistCount(&Counter
.client_http
.nm_svc_time
, svc_time
);
566 case LOG_TCP_MEM_HIT
:
567 case LOG_TCP_OFFLINE_HIT
:
568 statHistCount(&Counter
.client_http
.hit_svc_time
, svc_time
);
571 case LOG_TCP_CLIENT_REFRESH_MISS
:
572 statHistCount(&Counter
.client_http
.miss_svc_time
, svc_time
);
575 /* make compiler warnings go away */
578 H
= &http
->request
->hier
;
581 Counter
.cd
.times_used
++;
584 Counter
.icp
.times_used
++;
586 if (0 != i
->stop
.tv_sec
&& 0 != i
->start
.tv_sec
)
587 statHistCount(&Counter
.icp
.query_svc_time
,
588 tvSubUsec(i
->start
, i
->stop
));
590 Counter
.icp
.query_timeouts
++;
593 Counter
.netdb
.times_used
++;
601 httpRequestFree(void *data
)
603 clientHttpRequest
*http
= data
;
604 clientHttpRequest
**H
;
605 ConnStateData
*conn
= http
->conn
;
606 StoreEntry
*entry
= http
->entry
;
607 request_t
*request
= http
->request
;
608 MemObject
*mem
= NULL
;
609 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(entry
));
610 if (!clientCheckTransferDone(http
)) {
612 storeUnregister(entry
, http
);
613 entry
= http
->entry
; /* reset, IMS might have changed it */
614 if (entry
&& entry
->ping_status
== PING_WAITING
)
615 storeReleaseRequest(entry
);
617 assert(http
->log_type
< LOG_TYPE_MAX
);
619 mem
= entry
->mem_obj
;
620 if (http
->out
.size
|| http
->log_type
) {
621 http
->al
.icp
.opcode
= ICP_INVALID
;
622 http
->al
.url
= http
->log_uri
;
623 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http
->al
.url
);
625 http
->al
.http
.code
= mem
->reply
->sline
.status
;
626 http
->al
.http
.content_type
= strBuf(mem
->reply
->content_type
);
628 http
->al
.cache
.caddr
= conn
->log_addr
;
629 http
->al
.cache
.size
= http
->out
.size
;
630 http
->al
.cache
.code
= http
->log_type
;
631 http
->al
.cache
.msec
= tvSubMsec(http
->start
, current_time
);
632 if (request
->user_ident
[0])
633 http
->al
.cache
.ident
= request
->user_ident
;
635 http
->al
.cache
.ident
= conn
->ident
;
640 packerToMemInit(&p
, &mb
);
641 httpHeaderPackInto(&request
->header
, &p
);
642 http
->al
.http
.method
= request
->method
;
643 http
->al
.http
.version
= request
->http_ver
;
644 http
->al
.headers
.request
= xstrdup(mb
.buf
);
645 http
->al
.hier
= request
->hier
;
649 accessLogLog(&http
->al
);
650 clientUpdateCounters(http
);
651 clientdbUpdate(conn
->peer
.sin_addr
, http
->log_type
, PROTO_HTTP
, http
->out
.size
);
653 if (http
->acl_checklist
)
654 aclChecklistFree(http
->acl_checklist
);
656 checkFailureRatio(request
->err_type
, http
->al
.hier
.code
);
657 safe_free(http
->uri
);
658 safe_free(http
->log_uri
);
659 safe_free(http
->al
.headers
.request
);
660 safe_free(http
->al
.headers
.reply
);
661 safe_free(http
->redirect
.location
);
662 stringClean(&http
->range_iter
.boundary
);
665 storeUnregister(entry
, http
);
666 storeUnlockObject(entry
);
668 /* old_entry might still be set if we didn't yet get the reply
669 * code in clientHandleIMSReply() */
670 if (http
->old_entry
) {
671 storeUnregister(http
->old_entry
, http
);
672 storeUnlockObject(http
->old_entry
);
673 http
->old_entry
= NULL
;
675 requestUnlink(http
->request
);
676 assert(http
!= http
->next
);
677 assert(http
->conn
->chr
!= NULL
);
678 H
= &http
->conn
->chr
;
687 dlinkDelete(&http
->active
, &ClientActiveRequests
);
691 /* This is a handler normally called by comm_close() */
693 connStateFree(int fd
, void *data
)
695 ConnStateData
*connState
= data
;
696 clientHttpRequest
*http
;
697 debug(33, 3) ("connStateFree: FD %d\n", fd
);
698 assert(connState
!= NULL
);
699 while ((http
= connState
->chr
) != NULL
) {
700 assert(http
->conn
== connState
);
701 assert(connState
->chr
!= connState
->chr
->next
);
702 httpRequestFree(http
);
704 safe_free(connState
->in
.buf
);
705 /* XXX account connState->in.buf */
706 pconnHistCount(0, connState
->nrequests
);
707 cbdataFree(connState
);
709 /* prevent those nasty RST packets */
711 char buf
[SQUID_TCP_SO_RCVBUF
];
712 while (read(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0);
718 clientInterpretRequestHeaders(clientHttpRequest
* http
)
720 request_t
*request
= http
->request
;
721 const HttpHeader
*req_hdr
= &request
->header
;
723 #if USE_USERAGENT_LOG
726 request
->imslen
= -1;
727 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
728 if (request
->ims
> 0)
729 request
->flags
.ims
= 1;
730 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
731 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
732 if (strListIsMember(&s
, "no-cache", ','))
736 if (request
->cache_control
)
737 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
741 if (Config
.onoff
.reload_into_ims
)
742 request
->flags
.nocache_hack
= 1;
743 else if (refresh_nocache_hack
)
744 request
->flags
.nocache_hack
= 1;
747 request
->flags
.nocache
= 1;
749 /* ignore range header in non-GETs */
750 if (request
->method
== METHOD_GET
) {
751 request
->range
= httpHeaderGetRange(req_hdr
);
753 request
->flags
.range
= 1;
755 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
756 request
->flags
.auth
= 1;
757 if (request
->login
[0] != '\0')
758 request
->flags
.auth
= 1;
759 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
760 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
762 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
763 * Note ThisCache2 has a space prepended to the hostname so we don't
764 * accidentally match super-domains.
766 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
767 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
768 request
, (ObjPackMethod
) & httpRequestPack
);
769 request
->flags
.loopdetect
= 1;
772 fvdbCountVia(strBuf(s
));
776 #if USE_USERAGENT_LOG
777 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
778 logUserAgent(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
);
781 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
782 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
783 fvdbCountForw(strBuf(s
));
787 request
->cache_control
= httpHeaderGetCc(req_hdr
);
788 if (request
->method
== METHOD_TRACE
) {
789 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
791 if (clientCachable(http
))
792 request
->flags
.cachable
= 1;
793 if (clientHierarchical(http
))
794 request
->flags
.hierarchical
= 1;
795 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
796 request
->flags
.nocache
? "SET" : "NOT SET");
797 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
798 request
->flags
.cachable
? "SET" : "NOT SET");
799 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
800 request
->flags
.hierarchical
? "SET" : "NOT SET");
804 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
805 * This is the client-side persistent connection flag. We need
806 * to set this relatively early in the request processing
807 * to handle hacks for broken servers and clients.
810 clientSetKeepaliveFlag(clientHttpRequest
* http
)
812 request_t
*request
= http
->request
;
813 const HttpHeader
*req_hdr
= &request
->header
;
814 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
816 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
817 RequestMethodStr
[request
->method
]);
818 if (httpMsgIsPersistent(request
->http_ver
, req_hdr
))
819 request
->flags
.proxy_keepalive
= 1;
823 clientCheckContentLength(request_t
* r
)
825 /* We only require a content-length for "upload" methods */
826 if (!pumpMethod(r
->method
))
828 if (httpHeaderGetInt(&r
->header
, HDR_CONTENT_LENGTH
) < 0)
834 clientCachable(clientHttpRequest
* http
)
836 const char *url
= http
->uri
;
837 request_t
*req
= http
->request
;
838 method_t method
= req
->method
;
840 memset(&ch
, '\0', sizeof(ch
));
842 * Hopefully, nobody really wants 'no_cache' by client's IP
843 * address, but if they do, this should work if they use IP
844 * addresses in their ACLs, or if the client's address is in
847 * This may not work yet for 'dst' and 'dst_domain' ACLs.
849 ch
.src_addr
= http
->conn
->peer
.sin_addr
;
850 ch
.my_addr
= http
->conn
->me
.sin_addr
;
851 ch
.request
= http
->request
;
853 * aclCheckFast returns 1 for ALLOW and 0 for DENY. The default
854 * is ALLOW, so we require 'no_cache DENY foo' in squid.conf
855 * to indicate uncachable objects.
857 if (!aclCheckFast(Config
.accessList
.noCache
, &ch
))
859 if (req
->protocol
== PROTO_HTTP
)
860 return httpCachable(method
);
861 /* FTP is always cachable */
862 if (req
->protocol
== PROTO_GOPHER
)
863 return gopherCachable(url
);
864 if (req
->protocol
== PROTO_WAIS
)
866 if (method
== METHOD_CONNECT
)
868 if (method
== METHOD_TRACE
)
870 if (req
->protocol
== PROTO_CACHEOBJ
)
875 /* Return true if we can query our neighbors for this object */
877 clientHierarchical(clientHttpRequest
* http
)
879 const char *url
= http
->uri
;
880 request_t
*request
= http
->request
;
881 method_t method
= request
->method
;
882 const wordlist
*p
= NULL
;
884 /* IMS needs a private key, so we can use the hierarchy for IMS only
885 * if our neighbors support private keys */
886 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
888 if (request
->flags
.auth
)
890 if (method
== METHOD_TRACE
)
892 if (method
!= METHOD_GET
)
894 /* scan hierarchy_stoplist */
895 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
896 if (strstr(url
, p
->key
))
898 if (request
->flags
.loopdetect
)
900 if (request
->protocol
== PROTO_HTTP
)
901 return httpCachable(method
);
902 if (request
->protocol
== PROTO_GOPHER
)
903 return gopherCachable(url
);
904 if (request
->protocol
== PROTO_WAIS
)
906 if (request
->protocol
== PROTO_CACHEOBJ
)
912 isTcpHit(log_type code
)
914 /* this should be a bitmap for better optimization */
915 if (code
== LOG_TCP_HIT
)
917 if (code
== LOG_TCP_IMS_HIT
)
919 if (code
== LOG_TCP_REFRESH_FAIL_HIT
)
921 if (code
== LOG_TCP_REFRESH_HIT
)
923 if (code
== LOG_TCP_NEGATIVE_HIT
)
925 if (code
== LOG_TCP_MEM_HIT
)
927 if (code
== LOG_TCP_OFFLINE_HIT
)
933 * returns true if If-Range specs match reply, false otherwise
936 clientIfRangeMatch(clientHttpRequest
* http
, HttpReply
* rep
)
938 const TimeOrTag spec
= httpHeaderGetTimeOrTag(&http
->request
->header
, HDR_IF_RANGE
);
939 /* check for parsing falure */
944 ETag rep_tag
= httpHeaderGetETag(&rep
->header
, HDR_ETAG
);
945 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
946 spec
.tag
.str
, rep_tag
.str
? rep_tag
.str
: "<none>");
948 return 0; /* entity has no etag to compare with! */
949 if (spec
.tag
.weak
|| rep_tag
.weak
) {
950 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
951 spec
.tag
.str
, rep_tag
.str
);
952 return 0; /* must use strong validator for sub-range requests */
954 return etagIsEqual(&rep_tag
, &spec
.tag
);
956 /* got modification time? */
957 if (spec
.time
>= 0) {
958 return http
->entry
->lastmod
<= spec
.time
;
960 assert(0); /* should not happen */
964 /* adds appropriate Range headers if needed */
966 clientBuildRangeHeader(clientHttpRequest
* http
, HttpReply
* rep
)
968 HttpHeader
*hdr
= rep
? &rep
->header
: 0;
969 const char *range_err
= NULL
;
970 assert(http
->request
->range
);
971 /* check if we still want to do ranges */
973 range_err
= "no [parse-able] reply";
974 else if (rep
->sline
.status
!= HTTP_OK
)
975 range_err
= "wrong status code";
976 else if (httpHeaderHas(hdr
, HDR_CONTENT_RANGE
))
977 range_err
= "origin server does ranges";
978 else if (rep
->content_length
< 0)
979 range_err
= "unknown length";
980 else if (rep
->content_length
!= http
->entry
->mem_obj
->reply
->content_length
)
981 range_err
= "INCONSISTENT length"; /* a bug? */
982 else if (httpHeaderHas(&http
->request
->header
, HDR_IF_RANGE
) && !clientIfRangeMatch(http
, rep
))
983 range_err
= "If-Range match failed";
984 else if (!httpHdrRangeCanonize(http
->request
->range
, rep
->content_length
))
985 range_err
= "canonization failed";
986 else if (httpHdrRangeIsComplex(http
->request
->range
))
987 range_err
= "too complex range header";
988 /* get rid of our range specs on error */
990 debug(33, 2) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err
);
991 httpHdrRangeDestroy(http
->request
->range
);
992 http
->request
->range
= NULL
;
994 const int spec_count
= http
->request
->range
->specs
.count
;
995 debug(33, 2) ("clientBuildRangeHeader: range spec count: %d clen: %d\n",
996 spec_count
, rep
->content_length
);
997 assert(spec_count
> 0);
998 /* ETags should not be returned with Partial Content replies? */
999 httpHeaderDelById(hdr
, HDR_ETAG
);
1000 /* append appropriate header(s) */
1001 if (spec_count
== 1) {
1002 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1003 const HttpHdrRangeSpec
*spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
);
1005 /* append Content-Range */
1006 httpHeaderAddContRange(hdr
, *spec
, rep
->content_length
);
1007 /* set new Content-Length to the actual number of OCTETs
1008 * transmitted in the message-body */
1009 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1010 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, spec
->length
);
1011 debug(33, 2) ("clientBuildRangeHeader: actual content length: %d\n", spec
->length
);
1014 /* generate boundary string */
1015 http
->range_iter
.boundary
= httpHdrRangeBoundaryStr(http
);
1016 /* delete old Content-Type, add ours */
1017 httpHeaderDelById(hdr
, HDR_CONTENT_TYPE
);
1018 httpHeaderPutStrf(hdr
, HDR_CONTENT_TYPE
,
1019 "multipart/byteranges; boundary=\"%s\"",
1020 strBuf(http
->range_iter
.boundary
));
1021 /* no need for Content-Length in multipart responses */
1022 /* but we must delete the original one if we cannot (yet)
1023 * calculate the actual length */
1024 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1029 /* filters out unwanted entries from original reply header
1030 * adds extra entries if we have more info than origin server
1031 * adds Squid specific entries */
1033 clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1035 HttpHeader
*hdr
= &rep
->header
;
1036 int is_hit
= isTcpHit(http
->log_type
);
1037 request_t
*request
= http
->request
;
1038 #if DONT_FILTER_THESE
1039 /* but you might want to if you run Squid as an HTTP accelerator */
1040 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1041 httpHeaderDelById(hdr
, HDR_ETAG
);
1043 httpHeaderDelById(hdr
, HDR_PROXY_CONNECTION
);
1044 /* here: Keep-Alive is a field-name, not a connection directive! */
1045 httpHeaderDelByName(hdr
, "Keep-Alive");
1046 /* remove Set-Cookie if a hit */
1048 httpHeaderDelById(hdr
, HDR_SET_COOKIE
);
1049 /* handle Connection header */
1050 if (httpHeaderHas(hdr
, HDR_CONNECTION
)) {
1051 /* anything that matches Connection list member will be deleted */
1052 String strConnection
= httpHeaderGetList(hdr
, HDR_CONNECTION
);
1053 const HttpHeaderEntry
*e
;
1054 HttpHeaderPos pos
= HttpHeaderInitPos
;
1056 * think: on-average-best nesting of the two loops (hdrEntry
1057 * and strListItem) @?@
1060 * maybe we should delete standard stuff ("keep-alive","close")
1061 * from strConnection first?
1063 while ((e
= httpHeaderGetEntry(hdr
, &pos
))) {
1064 if (strListIsMember(&strConnection
, strBuf(e
->name
), ','))
1065 httpHeaderDelAt(hdr
, pos
);
1067 httpHeaderDelById(hdr
, HDR_CONNECTION
);
1068 stringClean(&strConnection
);
1072 clientBuildRangeHeader(http
, rep
);
1074 * Add Age header, not that our header must replace Age headers
1075 * from other caches if any
1077 if (http
->entry
->timestamp
> 0) {
1078 httpHeaderDelById(hdr
, HDR_AGE
);
1080 * we do not follow HTTP/1.1 precisely here becuase we rely
1081 * on Date header when computing entry->timestamp; we should
1082 * be using _request_ time if Date header is not available
1083 * or if it is out of sync
1085 httpHeaderPutInt(hdr
, HDR_AGE
,
1086 http
->entry
->timestamp
<= squid_curtime
?
1087 squid_curtime
- http
->entry
->timestamp
: 0);
1089 /* Append X-Cache */
1090 httpHeaderPutStrf(hdr
, HDR_X_CACHE
, "%s from %s",
1091 is_hit
? "HIT" : "MISS", getMyHostname());
1092 #if USE_CACHE_DIGESTS
1093 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1094 httpHeaderPutStrf(hdr
, HDR_X_CACHE_LOOKUP
, "%s from %s:%d",
1095 http
->lookup_type
? http
->lookup_type
: "NONE",
1096 getMyHostname(), Config
.Port
.http
->i
);
1099 * Clear keepalive for NON-HEAD requests with invalid content length
1101 if (request
->method
!= METHOD_HEAD
)
1102 if (http
->entry
->mem_obj
->reply
->content_length
< 0)
1103 request
->flags
.proxy_keepalive
= 0;
1104 /* Signal keep-alive if needed */
1105 httpHeaderPutStr(hdr
,
1106 http
->flags
.accel
? HDR_CONNECTION
: HDR_PROXY_CONNECTION
,
1107 request
->flags
.proxy_keepalive
? "keep-alive" : "close");
1108 #if ADD_X_REQUEST_URI
1110 * Knowing the URI of the request is useful when debugging persistent
1111 * connections in a client; we cannot guarantee the order of http headers,
1112 * but X-Request-URI is likely to be the very last header to ease use from a
1113 * debugger [hdr->entries.count-1].
1115 httpHeaderPutStr(hdr
, HDR_X_REQUEST_URI
,
1116 http
->entry
->mem_obj
->url
? http
->entry
->mem_obj
->url
: http
->uri
);
1121 clientBuildReply(clientHttpRequest
* http
, const char *buf
, size_t size
)
1123 HttpReply
*rep
= httpReplyCreate();
1124 if (httpReplyParse(rep
, buf
)) {
1125 /* enforce 1.0 reply version */
1126 rep
->sline
.version
= 1.0;
1127 /* do header conversions */
1128 clientBuildReplyHeader(http
, rep
);
1129 /* if we do ranges, change status to "Partial Content" */
1130 if (http
->request
->range
)
1131 httpStatusLineSet(&rep
->sline
, rep
->sline
.version
, HTTP_PARTIAL_CONTENT
, NULL
);
1133 /* parsing failure, get rid of the invalid reply */
1134 httpReplyDestroy(rep
);
1136 /* if we were going to do ranges, backoff */
1137 if (http
->request
->range
)
1138 clientBuildRangeHeader(http
, rep
); /* will fail and destroy request->range */
1144 * clientCacheHit should only be called until the HTTP reply headers
1145 * have been parsed. Normally this should be a single call, but
1146 * it might take more than one. As soon as we have the headers,
1147 * we hand off to clientSendMoreData, clientProcessExpired, or
1148 * clientProcessMiss.
1151 clientCacheHit(void *data
, char *buf
, ssize_t size
)
1153 clientHttpRequest
*http
= data
;
1154 StoreEntry
*e
= http
->entry
;
1156 request_t
*r
= http
->request
;
1157 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http
->uri
, (int) size
);
1158 if (http
->entry
== NULL
) {
1159 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1160 debug(33, 3) ("clientCacheHit: request aborted\n");
1162 } else if (size
< 0) {
1163 /* swap in failure */
1164 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1165 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http
->uri
);
1166 http
->log_type
= LOG_TCP_SWAPFAIL_MISS
;
1167 if ((e
= http
->entry
)) {
1169 storeUnregister(e
, http
);
1170 storeUnlockObject(e
);
1172 clientProcessMiss(http
);
1177 assert(!EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
1178 if (mem
->reply
->sline
.status
== 0) {
1180 * we don't have full reply headers yet; either wait for more or
1181 * punt to clientProcessMiss.
1183 if (e
->mem_status
== IN_MEMORY
|| e
->store_status
== STORE_OK
) {
1184 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1185 clientProcessMiss(http
);
1186 } else if (size
== CLIENT_SOCK_SZ
&& http
->out
.offset
== 0) {
1187 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1188 clientProcessMiss(http
);
1190 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1192 http
->out
.offset
+ size
,
1202 * Got the headers, now grok them
1204 assert(http
->log_type
== LOG_TCP_HIT
);
1205 if (checkNegativeHit(e
)) {
1206 http
->log_type
= LOG_TCP_NEGATIVE_HIT
;
1207 clientSendMoreData(data
, buf
, size
);
1208 } else if (r
->method
== METHOD_HEAD
) {
1210 * RFC 2068 seems to indicate there is no "conditional HEAD"
1211 * request. We cannot validate a cached object for a HEAD
1212 * request, nor can we return 304.
1214 if (e
->mem_status
== IN_MEMORY
)
1215 http
->log_type
= LOG_TCP_MEM_HIT
;
1216 clientSendMoreData(data
, buf
, size
);
1217 } else if (refreshCheckHTTP(e
, r
) && !http
->flags
.internal
) {
1218 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1220 * We hold a stale copy; it needs to be validated
1223 * The 'need_validation' flag is used to prevent forwarding
1224 * loops between siblings. If our copy of the object is stale,
1225 * then we should probably only use parents for the validation
1226 * request. Otherwise two siblings could generate a loop if
1227 * both have a stale version of the object.
1229 r
->flags
.need_validation
= 1;
1230 if (e
->lastmod
< 0) {
1232 * Previous reply didn't have a Last-Modified header,
1233 * we cannot revalidate it.
1235 http
->log_type
= LOG_TCP_MISS
;
1236 clientProcessMiss(http
);
1237 } else if (r
->flags
.nocache
) {
1239 * This did not match a refresh pattern that overrides no-cache
1240 * we should honour the client no-cache header.
1242 http
->log_type
= LOG_TCP_CLIENT_REFRESH_MISS
;
1243 clientProcessMiss(http
);
1244 } else if (r
->protocol
== PROTO_HTTP
) {
1246 * Object needs to be revalidated
1247 * XXX This could apply to FTP as well, if Last-Modified is known.
1249 http
->log_type
= LOG_TCP_REFRESH_MISS
;
1250 clientProcessExpired(http
);
1253 * We don't know how to re-validate other protocols. Handle
1254 * them as if the object has expired.
1256 http
->log_type
= LOG_TCP_MISS
;
1257 clientProcessMiss(http
);
1259 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1260 } else if (r
->flags
.ims
) {
1262 * Handle If-Modified-Since requests from the client
1264 if (mem
->reply
->sline
.status
!= HTTP_OK
) {
1265 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1266 mem
->reply
->sline
.status
);
1267 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1268 clientProcessMiss(http
);
1269 } else if (modifiedSince(e
, http
->request
)) {
1270 http
->log_type
= LOG_TCP_IMS_HIT
;
1271 clientSendMoreData(data
, buf
, size
);
1273 MemBuf mb
= httpPacked304Reply(e
->mem_obj
->reply
);
1274 http
->log_type
= LOG_TCP_IMS_HIT
;
1275 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1276 storeUnregister(e
, http
);
1277 storeUnlockObject(e
);
1278 e
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
1280 httpReplyParse(e
->mem_obj
->reply
, mb
.buf
);
1281 storeAppend(e
, mb
.buf
, mb
.size
);
1287 * plain ol' cache hit
1289 if (e
->mem_status
== IN_MEMORY
)
1290 http
->log_type
= LOG_TCP_MEM_HIT
;
1291 else if (Config
.onoff
.offline
)
1292 http
->log_type
= LOG_TCP_OFFLINE_HIT
;
1293 clientSendMoreData(data
, buf
, size
);
1298 /* appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */
1300 clientPackRangeHdr(const HttpReply
*rep
, const HttpHdrRangeSpec
*spec
, String boundary
, MemBuf
*mb
)
1308 debug(33, 5) ("clientPackRangeHdr: appending boundary: %s\n", strBuf(boundary
));
1310 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1311 memBufPrintf(mb
, "\r\n--%s\r\n", strBuf(boundary
));
1313 /* stuff the header with required entries and pack it */
1314 httpHeaderInit(&hdr
, hoReply
);
1315 if (httpHeaderHas(&rep
->header
, HDR_CONTENT_TYPE
))
1316 httpHeaderPutStr(&hdr
, HDR_CONTENT_TYPE
, httpHeaderGetStr(&rep
->header
, HDR_CONTENT_TYPE
));
1317 httpHeaderAddContRange(&hdr
, *spec
, rep
->content_length
);
1318 packerToMemInit(&p
, mb
);
1319 httpHeaderPackInto(&hdr
, &p
);
1321 httpHeaderClean(&hdr
);
1323 /* append <crlf> (we packed a header, not a reply */
1324 memBufPrintf(mb
, crlf
);
1327 /* extracts a "range" from *buf and appends them to mb, updating all offsets and such */
1329 clientPackRange(clientHttpRequest
* http
, HttpHdrRangeIter
* i
, const char **buf
, ssize_t
* size
, MemBuf
* mb
)
1331 const size_t copy_sz
= i
->debt_size
<= *size
? i
->debt_size
: *size
;
1332 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1336 /* intersection of "have" and "need" ranges must not be empty */
1337 assert(body_off
< i
->spec
->offset
+ i
->spec
->length
);
1338 assert(body_off
+ *size
> i
->spec
->offset
);
1340 /* put boundary and headers at the beginning of a range in a multi-range */
1341 if (http
->request
->range
->specs
.count
> 1 && i
->debt_size
== i
->spec
->length
) {
1342 assert(http
->entry
->mem_obj
);
1344 http
->entry
->mem_obj
->reply
, /* original reply */
1345 i
->spec
, /* current range */
1346 i
->boundary
, /* boundary, the same for all */
1351 /* append content */
1352 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz
);
1353 memBufAppend(mb
, *buf
, copy_sz
);
1355 /* update offsets */
1357 i
->debt_size
-= copy_sz
;
1358 body_off
+= copy_sz
;
1360 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1362 /* paranoid check */
1363 assert(*size
>= 0 && i
->debt_size
>= 0);
1366 /* returns true if there is still data available to pack more ranges
1367 * increments iterator "i"
1368 * used by clientPackMoreRanges */
1370 clientCanPackMoreRanges(const clientHttpRequest
* http
, HttpHdrRangeIter
* i
, ssize_t size
)
1372 /* first update "i" if needed */
1373 if (!i
->debt_size
) {
1374 if ((i
->spec
= httpHdrRangeGetSpec(http
->request
->range
, &i
->pos
)))
1375 i
->debt_size
= i
->spec
->length
;
1377 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1378 /* continue condition: need_more_data && have_more_data */
1379 return i
->spec
&& size
> 0;
1382 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1383 /* returns true if we need more data */
1385 clientPackMoreRanges(clientHttpRequest
* http
, const char *buf
, ssize_t size
, MemBuf
* mb
)
1387 HttpHdrRangeIter
*i
= &http
->range_iter
;
1388 /* offset in range specs does not count the prefix of an http msg */
1389 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1391 /* check: reply was parsed and range iterator was initialized */
1392 assert(i
->prefix_size
> 0);
1393 /* filter out data according to range specs */
1394 while (clientCanPackMoreRanges(http
, i
, size
)) {
1395 off_t start
; /* offset of still missing data */
1397 start
= i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
;
1398 debug(33, 2) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1399 (int) body_off
, size
);
1400 debug(33, 2) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1401 (int) start
, (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
, i
->debt_size
);
1402 assert(body_off
<= start
); /* we did not miss it */
1403 /* skip up to start */
1404 if (body_off
+ size
> start
) {
1405 const size_t skip_size
= start
- body_off
;
1410 /* has not reached start yet */
1415 /* put next chunk if any */
1417 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1418 clientPackRange(http
, i
, &buf
, &size
, mb
);
1419 body_off
= http
->out
.offset
- i
->prefix_size
; /* sync */
1422 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1423 debug(33, 2) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1424 (int) body_off
, size
, i
->debt_size
);
1426 debug(33, 2) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1427 (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
);
1428 /* skip the data we do not need if possible */
1429 if (i
->debt_size
== i
->spec
->length
) /* at the start of the cur. spec */
1430 body_off
= i
->spec
->offset
;
1432 assert(body_off
== i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
);
1433 } else if (http
->request
->range
->specs
.count
> 1) {
1434 /* put terminating boundary for multiparts */
1435 memBufPrintf(mb
, "\r\n--%s--\r\n", strBuf(i
->boundary
));
1437 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1438 return i
->debt_size
> 0;
1442 * accepts chunk of a http message in buf, parses prefix, filters headers and
1443 * such, writes processed message to the client's socket
1446 clientSendMoreData(void *data
, char *buf
, ssize_t size
)
1448 clientHttpRequest
*http
= data
;
1449 StoreEntry
*entry
= http
->entry
;
1450 ConnStateData
*conn
= http
->conn
;
1452 HttpReply
*rep
= NULL
;
1453 const char *body_buf
= buf
;
1454 ssize_t body_size
= size
;
1456 ssize_t check_size
= 0;
1457 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http
->uri
, (int) size
);
1458 assert(size
<= CLIENT_SOCK_SZ
);
1459 assert(http
->request
!= NULL
);
1460 dlinkDelete(&http
->active
, &ClientActiveRequests
);
1461 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1462 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1463 fd
, storeUrl(entry
), (int) http
->out
.offset
);
1464 if (conn
->chr
!= http
) {
1465 /* there is another object in progress, defer this one */
1466 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry
));
1467 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1469 } else if (entry
&& EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1470 /* call clientWriteComplete so the client socket gets closed */
1471 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1472 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1474 } else if (size
< 0) {
1475 /* call clientWriteComplete so the client socket gets closed */
1476 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1477 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1479 } else if (size
== 0) {
1480 /* call clientWriteComplete so the client socket gets closed */
1481 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1482 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1485 if (http
->out
.offset
== 0) {
1486 if (Config
.onoff
.log_mime_hdrs
) {
1488 if ((k
= headersEnd(buf
, size
))) {
1489 safe_free(http
->al
.headers
.reply
);
1490 http
->al
.headers
.reply
= xcalloc(k
+ 1, 1);
1491 xstrncpy(http
->al
.headers
.reply
, buf
, k
);
1494 rep
= clientBuildReply(http
, buf
, size
);
1496 body_size
= size
- rep
->hdr_sz
;
1497 assert(body_size
>= 0);
1498 body_buf
= buf
+ rep
->hdr_sz
;
1499 http
->range_iter
.prefix_size
= rep
->hdr_sz
;
1500 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1501 body_size
, rep
->hdr_sz
);
1502 } else if (size
< CLIENT_SOCK_SZ
&& entry
->store_status
== STORE_PENDING
) {
1503 /* wait for more to arrive */
1504 storeClientCopy(entry
,
1505 http
->out
.offset
+ size
,
1513 /* reset range iterator */
1514 http
->range_iter
.pos
= HttpHdrRangeInitPos
;
1516 if (http
->request
->method
== METHOD_HEAD
) {
1518 /* do not forward body for HEAD replies */
1520 http
->flags
.done_copying
= 1;
1523 * If we are here, then store_status == STORE_OK and it
1524 * seems we have a HEAD repsponse which is missing the
1525 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1526 * does this). Because clientBuildReply() fails we just
1527 * call this reply a body, set the done_copying flag and
1530 http
->flags
.done_copying
= 1;
1533 /* write headers and/or body if any */
1534 assert(rep
|| (body_buf
&& body_size
));
1535 /* init mb; put status line and headers if any */
1537 mb
= httpReplyPack(rep
);
1538 http
->out
.offset
+= rep
->hdr_sz
;
1539 check_size
+= rep
->hdr_sz
;
1540 httpReplyDestroy(rep
);
1545 /* append body if any */
1546 if (http
->request
->range
) {
1547 /* Only GET requests should have ranges */
1548 assert(http
->request
->method
== METHOD_GET
);
1549 /* clientPackMoreRanges() updates http->out.offset */
1550 /* force the end of the transfer if we are done */
1551 if (!clientPackMoreRanges(http
, body_buf
, body_size
, &mb
))
1552 http
->flags
.done_copying
= 1;
1553 } else if (body_buf
&& body_size
) {
1554 http
->out
.offset
+= body_size
;
1555 check_size
+= body_size
;
1556 memBufAppend(&mb
, body_buf
, body_size
);
1558 if (!http
->request
->range
&& http
->request
->method
== METHOD_GET
)
1559 assert(check_size
== size
);
1561 comm_write_mbuf(fd
, mb
, clientWriteComplete
, http
);
1562 /* if we don't do it, who will? */
1563 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1567 clientKeepaliveNextRequest(clientHttpRequest
* http
)
1569 ConnStateData
*conn
= http
->conn
;
1571 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn
->fd
);
1572 conn
->defer
.until
= 0; /* Kick it to read a new request */
1573 httpRequestFree(http
);
1574 if ((http
= conn
->chr
) == NULL
) {
1575 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1577 fd_note(conn
->fd
, "Reading next request");
1579 * Set the timeout BEFORE calling clientReadRequest().
1581 commSetTimeout(conn
->fd
, 15, requestTimeout
, conn
);
1582 clientReadRequest(conn
->fd
, conn
); /* Read next request */
1584 * Note, the FD may be closed at this point.
1586 } else if ((entry
= http
->entry
) == NULL
) {
1588 * this request is in progress, maybe doing an ACL or a redirect,
1589 * execution will resume after the operation completes.
1592 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1595 if (0 == storeClientCopyPending(entry
, http
)) {
1596 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1597 debug(33, 0) ("clientKeepaliveNextRequest: ENTRY_ABORTED\n");
1598 storeClientCopy(entry
,
1602 memAllocate(MEM_CLIENT_SOCK_BUF
),
1610 clientWriteComplete(int fd
, char *bufnotused
, size_t size
, int errflag
, void *data
)
1612 clientHttpRequest
*http
= data
;
1613 StoreEntry
*entry
= http
->entry
;
1615 http
->out
.size
+= size
;
1616 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1617 fd
, size
, errflag
, (int) http
->out
.offset
, entry
? objectLen(entry
) : 0);
1619 kb_incr(&Counter
.client_http
.kbytes_out
, size
);
1620 if (isTcpHit(http
->log_type
))
1621 kb_incr(&Counter
.client_http
.hit_kbytes_out
, size
);
1625 * just close the socket, httpRequestFree will abort if needed
1628 } else if (NULL
== entry
) {
1629 comm_close(fd
); /* yuk */
1630 } else if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1632 } else if ((done
= clientCheckTransferDone(http
)) != 0 || size
== 0) {
1633 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd
);
1634 /* We're finished case */
1635 if (http
->entry
->mem_obj
->reply
->content_length
< 0) {
1636 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1639 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1641 } else if (clientGotNotEnough(http
)) {
1642 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1644 } else if (http
->request
->flags
.proxy_keepalive
) {
1645 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd
);
1646 clientKeepaliveNextRequest(http
);
1651 /* More data will be coming from primary server; register with
1652 * storage manager. */
1653 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1654 debug(33, 0) ("clientWriteComplete 2: ENTRY_ABORTED\n");
1655 storeClientCopy(entry
,
1659 memAllocate(MEM_CLIENT_SOCK_BUF
),
1666 * client issued a request with an only-if-cached cache-control directive;
1667 * we did not find a cached object that can be returned without
1668 * contacting other servers;
1669 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1672 clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
)
1674 char *url
= http
->uri
;
1675 request_t
*r
= http
->request
;
1676 ErrorState
*err
= NULL
;
1677 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1678 RequestMethodStr
[r
->method
], url
);
1679 http
->al
.http
.code
= HTTP_GATEWAY_TIMEOUT
;
1680 err
= errorCon(ERR_ONLY_IF_CACHED_MISS
, HTTP_GATEWAY_TIMEOUT
);
1681 err
->request
= requestLink(r
);
1682 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1684 storeUnregister(http
->entry
, http
);
1685 storeUnlockObject(http
->entry
);
1687 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1688 errorAppendEntry(http
->entry
, err
);
1692 clientProcessRequest2(clientHttpRequest
* http
)
1694 request_t
*r
= http
->request
;
1696 e
= http
->entry
= storeGetPublic(http
->uri
, r
->method
);
1697 if (r
->method
== METHOD_HEAD
&& e
== NULL
) {
1698 /* We can generate a HEAD reply from a cached GET object */
1699 e
= http
->entry
= storeGetPublic(http
->uri
, METHOD_GET
);
1701 #if USE_CACHE_DIGESTS
1702 http
->lookup_type
= e
? "HIT" : "MISS";
1705 /* this object isn't in the cache */
1706 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
1707 return LOG_TCP_MISS
;
1709 if (Config
.onoff
.offline
) {
1710 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
1714 if (!storeEntryValidToSend(e
)) {
1715 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
1717 return LOG_TCP_MISS
;
1719 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1720 /* Special entries are always hits, no matter what the client says */
1721 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
1726 if (r
->flags
.nocache_hack
) {
1727 /* if nocache_hack is set, nocache should always be clear, right? */
1728 assert(!r
->flags
.nocache
);
1729 ipcacheReleaseInvalid(r
->host
);
1732 if (e
->store_status
== STORE_PENDING
) {
1733 if (r
->flags
.nocache
|| r
->flags
.nocache_hack
) {
1734 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
1736 r
->flags
.nocache
= 0;
1737 r
->flags
.nocache_hack
= 0;
1741 if (r
->flags
.nocache
) {
1742 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
1744 ipcacheReleaseInvalid(r
->host
);
1745 return LOG_TCP_CLIENT_REFRESH_MISS
;
1747 if (r
->range
&& httpHdrRangeWillBeComplex(r
->range
)) {
1749 * Some clients break if we return "200 OK" for a Range
1750 * request. We would have to return "200 OK" for a _complex_
1751 * Range request that is also a HIT. Thus, let's prevent HITs
1752 * on complex Range requests
1754 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
1756 return LOG_TCP_MISS
;
1758 debug(33, 3) ("clientProcessRequest2: default HIT\n");
1764 clientProcessRequest(clientHttpRequest
* http
)
1766 char *url
= http
->uri
;
1767 request_t
*r
= http
->request
;
1768 int fd
= http
->conn
->fd
;
1770 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
1771 RequestMethodStr
[r
->method
],
1773 if (r
->method
== METHOD_CONNECT
) {
1774 http
->log_type
= LOG_TCP_MISS
;
1775 sslStart(fd
, url
, r
, &http
->out
.size
);
1777 } else if (r
->method
== METHOD_PURGE
) {
1778 clientPurgeRequest(http
);
1780 } else if (r
->method
== METHOD_TRACE
) {
1781 if (r
->max_forwards
== 0) {
1782 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1783 storeReleaseRequest(http
->entry
);
1784 storeBuffer(http
->entry
);
1785 rep
= httpReplyCreate();
1786 httpReplySetHeaders(rep
, 1.0, HTTP_OK
, NULL
, "text/plain",
1787 httpRequestPrefixLen(r
), 0, squid_curtime
);
1788 httpReplySwapOut(rep
, http
->entry
);
1789 httpReplyDestroy(rep
);
1790 httpRequestSwapOut(r
, http
->entry
);
1791 storeComplete(http
->entry
);
1795 http
->log_type
= LOG_TCP_MISS
;
1796 } else if (pumpMethod(r
->method
)) {
1797 http
->log_type
= LOG_TCP_MISS
;
1798 /* XXX oof, POST can be cached! */
1799 pumpInit(fd
, r
, http
->uri
);
1801 http
->log_type
= clientProcessRequest2(http
);
1803 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
1804 log_tags
[http
->log_type
],
1806 http
->out
.offset
= 0;
1807 if (NULL
!= http
->entry
) {
1808 storeLockObject(http
->entry
);
1809 storeCreateMemObject(http
->entry
, http
->uri
, http
->log_uri
);
1810 storeClientListAdd(http
->entry
, http
);
1812 delaySetStoreClient(http
->entry
, http
, delayClient(r
));
1814 http
->entry
->refcount
++;
1815 storeClientCopy(http
->entry
,
1819 memAllocate(MEM_CLIENT_SOCK_BUF
),
1824 http
->log_type
= LOG_TCP_MISS
;
1825 clientProcessMiss(http
);
1830 * Prepare to fetch the object as it's a cache miss of some kind.
1833 clientProcessMiss(clientHttpRequest
* http
)
1835 char *url
= http
->uri
;
1836 request_t
*r
= http
->request
;
1837 ErrorState
*err
= NULL
;
1838 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
1839 RequestMethodStr
[r
->method
], url
);
1841 * We might have a left-over StoreEntry from a failed cache hit
1845 if (EBIT_TEST(http
->entry
->flags
, ENTRY_SPECIAL
))
1846 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url
);
1847 storeUnregister(http
->entry
, http
);
1848 storeUnlockObject(http
->entry
);
1851 if (clientOnlyIfCached(http
)) {
1852 clientProcessOnlyIfCachedMiss(http
);
1856 * Deny loops when running in accelerator/transproxy mode.
1858 if (http
->flags
.accel
&& r
->flags
.loopdetect
) {
1859 http
->al
.http
.code
= HTTP_FORBIDDEN
;
1860 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
1861 err
->request
= requestLink(r
);
1862 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1863 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1864 errorAppendEntry(http
->entry
, err
);
1867 assert(http
->out
.offset
== 0);
1868 http
->entry
= clientCreateStoreEntry(http
, r
->method
, r
->flags
);
1869 http
->entry
->refcount
++;
1870 if (http
->redirect
.status
) {
1871 HttpReply
*rep
= httpReplyCreate();
1872 storeReleaseRequest(http
->entry
);
1873 httpRedirectReply(rep
, http
->redirect
.status
, http
->redirect
.location
);
1874 httpReplySwapOut(rep
, http
->entry
);
1875 httpReplyDestroy(rep
);
1876 storeComplete(http
->entry
);
1879 if (http
->flags
.internal
)
1880 r
->protocol
= PROTO_INTERNAL
;
1881 fwdStart(http
->conn
->fd
, http
->entry
, r
,
1882 http
->conn
->peer
.sin_addr
, http
->conn
->me
.sin_addr
);
1885 static clientHttpRequest
*
1886 parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
)
1888 clientHttpRequest
*http
= xcalloc(1, sizeof(clientHttpRequest
));
1889 cbdataAdd(http
, cbdataXfree
, 0);
1891 http
->start
= current_time
;
1892 http
->req_sz
= conn
->in
.offset
;
1893 http
->uri
= xstrdup(uri
);
1894 http
->log_uri
= xstrndup(uri
, MAX_URL
);
1895 http
->range_iter
.boundary
= StringNull
;
1896 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1901 * parseHttpRequest()
1904 * NULL on error or incomplete request
1905 * a clientHttpRequest structure on success
1907 static clientHttpRequest
*
1908 parseHttpRequest(ConnStateData
* conn
, method_t
* method_p
, int *status
,
1909 char **prefix_p
, size_t * req_line_sz_p
)
1914 char *req_hdr
= NULL
;
1919 int free_request
= 0;
1920 size_t header_sz
; /* size of headers, not including first line */
1921 size_t prefix_sz
; /* size of whole request (req-line + headers) */
1925 clientHttpRequest
*http
= NULL
;
1927 struct natlookup natLookup
;
1928 static int natfd
= -1;
1931 if ((req_sz
= headersEnd(conn
->in
.buf
, conn
->in
.offset
)) == 0) {
1932 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
1935 *method_p
= METHOD_NONE
;
1938 assert(req_sz
<= conn
->in
.offset
);
1939 /* Use memcpy, not strdup! */
1940 inbuf
= xmalloc(req_sz
+ 1);
1941 xmemcpy(inbuf
, conn
->in
.buf
, req_sz
);
1942 *(inbuf
+ req_sz
) = '\0';
1944 /* pre-set these values to make aborting simpler */
1946 *method_p
= METHOD_NONE
;
1949 /* Barf on NULL characters in the headers */
1950 if (strlen(inbuf
) != req_sz
) {
1951 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
1952 return parseHttpRequestAbort(conn
, "error:invalid-request");
1954 /* Look for request method */
1955 if ((mstr
= strtok(inbuf
, "\t ")) == NULL
) {
1956 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
1957 return parseHttpRequestAbort(conn
, "error:invalid-request-method");
1959 method
= urlParseMethod(mstr
);
1960 if (method
== METHOD_NONE
) {
1961 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr
);
1962 return parseHttpRequestAbort(conn
, "error:unsupported-request-method");
1964 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr
);
1967 /* look for URL+HTTP/x.x */
1968 if ((url
= strtok(NULL
, "\n")) == NULL
) {
1969 debug(33, 1) ("parseHttpRequest: Missing URL\n");
1970 return parseHttpRequestAbort(conn
, "error:missing-url");
1972 while (isspace(*url
))
1974 t
= url
+ strlen(url
);
1979 if (isspace(*t
) && !strncmp(t
+ 1, "HTTP/", 5)) {
1984 while (t
> url
&& isspace(*t
))
1986 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url
);
1987 if (token
== NULL
) {
1988 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
1989 #if RELAXED_HTTP_PARSER
1990 http_ver
= (float) 0.9; /* wild guess */
1992 return parseHttpRequestAbort(conn
, "error:missing-http-ident");
1995 http_ver
= (float) atof(token
+ 5);
1999 * Process headers after request line
2001 req_hdr
= strtok(NULL
, null_string
);
2002 header_sz
= req_sz
- (req_hdr
- inbuf
);
2003 if (0 == header_sz
) {
2004 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
2008 assert(header_sz
> 0);
2009 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr
);
2010 end
= req_hdr
+ header_sz
;
2011 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end
);
2013 prefix_sz
= end
- inbuf
;
2014 *req_line_sz_p
= req_hdr
- inbuf
;
2015 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
2016 (int) prefix_sz
, (int) *req_line_sz_p
);
2017 assert(prefix_sz
<= conn
->in
.offset
);
2019 /* Ok, all headers are received */
2020 http
= xcalloc(1, sizeof(clientHttpRequest
));
2021 cbdataAdd(http
, cbdataXfree
, 0);
2022 http
->http_ver
= http_ver
;
2024 http
->start
= current_time
;
2025 http
->req_sz
= prefix_sz
;
2026 http
->range_iter
.boundary
= StringNull
;
2027 *prefix_p
= xmalloc(prefix_sz
+ 1);
2028 xmemcpy(*prefix_p
, conn
->in
.buf
, prefix_sz
);
2029 *(*prefix_p
+ prefix_sz
) = '\0';
2030 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2032 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p
) + *req_line_sz_p
);
2033 if ((t
= strchr(url
, '#'))) /* remove HTML anchors */
2036 /* handle internal objects */
2037 if (internalCheck(url
)) {
2038 /* prepend our name & port */
2039 http
->uri
= xstrdup(internalLocalUri(NULL
, url
));
2040 http
->flags
.internal
= 1;
2041 http
->flags
.accel
= 1;
2043 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2044 else if (Config2
.Accel
.on
&& *url
== '/') {
2045 /* prepend the accel prefix */
2046 if (opt_accel_uses_host
&& (t
= mime_get_header(req_hdr
, "Host"))) {
2047 /* If a Host: header was specified, use it to build the URL
2048 * instead of the one in the Config file. */
2050 * XXX Use of the Host: header here opens a potential
2051 * security hole. There are no checks that the Host: value
2052 * corresponds to one of your servers. It might, for example,
2053 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2054 * types should be used to prevent httpd-accelerators
2055 * handling requests for non-local servers */
2057 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
+
2059 http
->uri
= xcalloc(url_sz
, 1);
2060 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2061 t
, (int) Config
.Accel
.port
, url
);
2062 } else if (vhost_mode
) {
2063 /* Put the local socket IP address as the hostname */
2064 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
;
2065 http
->uri
= xcalloc(url_sz
, 1);
2067 natLookup
.nl_inport
= http
->conn
->me
.sin_port
;
2068 natLookup
.nl_outport
= http
->conn
->peer
.sin_port
;
2069 natLookup
.nl_inip
= http
->conn
->me
.sin_addr
;
2070 natLookup
.nl_outip
= http
->conn
->peer
.sin_addr
;
2071 natLookup
.nl_flags
= IPN_TCP
;
2073 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
2075 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2077 return parseHttpRequestAbort(conn
, "error:nat-open-failed");
2079 if (ioctl(natfd
, SIOCGNATL
, &natLookup
) < 0) {
2080 if (errno
!= ESRCH
) {
2081 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2084 return parseHttpRequestAbort(conn
, "error:nat-lookup-failed");
2086 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2087 inet_ntoa(http
->conn
->me
.sin_addr
),
2088 (int) Config
.Accel
.port
,
2091 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2092 inet_ntoa(natLookup
.nl_realip
),
2093 (int) Config
.Accel
.port
,
2096 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2097 inet_ntoa(http
->conn
->me
.sin_addr
),
2098 (int) Config
.Accel
.port
,
2101 debug(33, 5) ("VHOST REWRITE: '%s'\n", http
->uri
);
2103 url_sz
= strlen(Config2
.Accel
.prefix
) + strlen(url
) +
2104 Config
.appendDomainLen
+ 1;
2105 http
->uri
= xcalloc(url_sz
, 1);
2106 snprintf(http
->uri
, url_sz
, "%s%s", Config2
.Accel
.prefix
, url
);
2108 http
->flags
.accel
= 1;
2110 /* URL may be rewritten later, so make extra room */
2111 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
2112 http
->uri
= xcalloc(url_sz
, 1);
2113 strcpy(http
->uri
, url
);
2114 http
->flags
.accel
= 0;
2116 if (!stringHasWhitespace(http
->uri
))
2117 http
->log_uri
= xstrndup(http
->uri
, MAX_URL
);
2119 http
->log_uri
= xstrndup(rfc1738_escape(http
->uri
), MAX_URL
);
2120 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2129 clientReadDefer(int fdnotused
, void *data
)
2131 ConnStateData
*conn
= data
;
2132 return conn
->defer
.until
> squid_curtime
;
2136 clientReadRequest(int fd
, void *data
)
2138 ConnStateData
*conn
= data
;
2139 int parser_return_code
= 0;
2141 request_t
*request
= NULL
;
2144 clientHttpRequest
*http
= NULL
;
2145 clientHttpRequest
**H
= NULL
;
2146 char *prefix
= NULL
;
2147 ErrorState
*err
= NULL
;
2148 fde
*F
= &fd_table
[fd
];
2149 int len
= conn
->in
.size
- conn
->in
.offset
- 1;
2150 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd
);
2151 Counter
.syscalls
.sock
.reads
++;
2152 size
= read(fd
, conn
->in
.buf
+ conn
->in
.offset
, len
);
2154 fd_bytes(fd
, size
, FD_READ
);
2155 kb_incr(&Counter
.client_http
.kbytes_in
, size
);
2158 * Don't reset the timeout value here. The timeout value will be
2159 * set to Config.Timeout.request by httpAccept() and
2160 * clientWriteComplete(), and should apply to the request as a
2161 * whole, not individual read() calls. Plus, it breaks our
2162 * lame half-close detection
2164 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, conn
, 0);
2166 if (conn
->chr
== NULL
) {
2167 /* no current or pending requests */
2170 } else if (!Config
.onoff
.half_closed_clients
) {
2171 /* admin doesn't want to support half-closed client sockets */
2175 /* It might be half-closed, we can't tell */
2176 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd
);
2177 F
->flags
.socket_eof
= 1;
2178 conn
->defer
.until
= squid_curtime
+ 1;
2180 fd_note(fd
, "half-closed");
2182 } else if (size
< 0) {
2183 if (!ignoreErrno(errno
)) {
2184 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd
, xstrerror());
2187 } else if (conn
->in
.offset
== 0) {
2188 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd
, xstrerror());
2191 /* Continue to process previously read data */
2194 conn
->in
.offset
+= size
;
2195 /* Skip leading (and trailing) whitespace */
2196 while (conn
->in
.offset
> 0) {
2199 while (conn
->in
.offset
> 0 && isspace(conn
->in
.buf
[0])) {
2200 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ 1, conn
->in
.offset
- 1);
2203 conn
->in
.buf
[conn
->in
.offset
] = '\0'; /* Terminate the string */
2204 if (conn
->in
.offset
== 0)
2206 /* Limit the number of concurrent requests to 2 */
2207 for (H
= &conn
->chr
, nrequests
= 0; *H
; H
= &(*H
)->next
, nrequests
++);
2208 if (nrequests
>= 2) {
2209 debug(33, 2) ("clientReadRequest: FD %d max concurrent requests reached\n", fd
);
2210 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd
);
2211 conn
->defer
.until
= squid_curtime
+ 100; /* Reset when a request is complete */
2214 /* Process request */
2215 http
= parseHttpRequest(conn
,
2217 &parser_return_code
,
2223 assert(http
->req_sz
> 0);
2224 conn
->in
.offset
-= http
->req_sz
;
2225 assert(conn
->in
.offset
>= 0);
2226 debug(33, 5) ("conn->in.offset = %d\n", (int) conn
->in
.offset
);
2228 * If we read past the end of this request, move the remaining
2229 * data to the beginning
2231 if (conn
->in
.offset
> 0)
2232 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ http
->req_sz
, conn
->in
.offset
);
2233 /* add to the client request queue */
2234 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2237 commSetTimeout(fd
, Config
.Timeout
.lifetime
, NULL
, NULL
);
2238 if (parser_return_code
< 0) {
2239 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd
);
2240 err
= errorCon(ERR_INVALID_REQ
, HTTP_BAD_REQUEST
);
2241 err
->request_hdrs
= xstrdup(conn
->in
.buf
);
2242 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2243 errorAppendEntry(http
->entry
, err
);
2247 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
2248 debug(33, 5) ("Invalid URL: %s\n", http
->uri
);
2249 err
= errorCon(ERR_INVALID_URL
, HTTP_BAD_REQUEST
);
2250 err
->src_addr
= conn
->peer
.sin_addr
;
2251 err
->url
= xstrdup(http
->uri
);
2252 http
->al
.http
.code
= err
->http_status
;
2253 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2254 errorAppendEntry(http
->entry
, err
);
2258 /* compile headers */
2259 /* we should skip request line! */
2260 if (!httpRequestParseHeader(request
, prefix
+ req_line_sz
))
2261 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2263 /* continue anyway? */
2265 request
->flags
.accelerated
= http
->flags
.accel
;
2266 if (!http
->flags
.internal
) {
2267 if (internalCheck(strBuf(request
->urlpath
))) {
2268 if (0 == strcasecmp(request
->host
, internalHostname())) {
2269 if (request
->port
== Config
.Port
.http
->i
)
2270 http
->flags
.internal
= 1;
2271 } else if (internalStaticCheck(strBuf(request
->urlpath
))) {
2272 xstrncpy(request
->host
, internalHostname(), SQUIDHOSTNAMELEN
);
2273 request
->port
= Config
.Port
.http
->i
;
2274 http
->flags
.internal
= 1;
2278 request
->flags
.internal
= http
->flags
.internal
;
2280 safe_free(http
->log_uri
);
2281 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
2282 request
->client_addr
= conn
->peer
.sin_addr
;
2283 request
->my_addr
= conn
->me
.sin_addr
;
2284 request
->http_ver
= http
->http_ver
;
2285 if (!urlCheckRequest(request
)) {
2286 err
= errorCon(ERR_UNSUP_REQ
, HTTP_NOT_IMPLEMENTED
);
2287 err
->src_addr
= conn
->peer
.sin_addr
;
2288 err
->request
= requestLink(request
);
2289 http
->al
.http
.code
= err
->http_status
;
2290 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2291 errorAppendEntry(http
->entry
, err
);
2294 if (0 == clientCheckContentLength(request
)) {
2295 err
= errorCon(ERR_INVALID_REQ
, HTTP_LENGTH_REQUIRED
);
2296 err
->src_addr
= conn
->peer
.sin_addr
;
2297 err
->request
= requestLink(request
);
2298 http
->al
.http
.code
= err
->http_status
;
2299 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2300 errorAppendEntry(http
->entry
, err
);
2303 http
->request
= requestLink(request
);
2305 * We need to set the keepalive flag before doing some
2306 * hacks for POST/PUT requests below. Maybe we could
2307 * set keepalive flag even earlier.
2309 clientSetKeepaliveFlag(http
);
2311 * break here for NON-GET because most likely there is a
2312 * reqeust body following and we don't want to parse it
2313 * as though it was new request
2315 if (request
->method
!= METHOD_GET
) {
2316 int cont_len
= httpHeaderGetInt(&request
->header
, HDR_CONTENT_LENGTH
);
2317 int copy_len
= XMIN(conn
->in
.offset
, cont_len
);
2319 assert(conn
->in
.offset
>= copy_len
);
2320 request
->body_sz
= copy_len
;
2321 request
->body
= xmalloc(request
->body_sz
);
2322 xmemcpy(request
->body
, conn
->in
.buf
, request
->body_sz
);
2323 conn
->in
.offset
-= copy_len
;
2324 if (conn
->in
.offset
)
2325 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ copy_len
, conn
->in
.offset
);
2328 * if we didn't get the full body now, then more will
2329 * be arriving on the client socket. Lets cancel
2330 * the read handler until this request gets forwarded.
2332 if (request
->body_sz
< cont_len
)
2333 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2335 clientAccessCheck(http
);
2336 continue; /* while offset > 0 */
2337 } else if (parser_return_code
== 0) {
2339 * Partial request received; reschedule until parseHttpRequest()
2340 * is happy with the input
2342 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2344 if (conn
->in
.offset
>= Config
.maxRequestSize
) {
2345 /* The request is too large to handle */
2346 debug(33, 0) ("Request won't fit in buffer.\n");
2347 debug(33, 0) ("Config 'request_size'= %d bytes.\n",
2348 Config
.maxRequestSize
);
2349 debug(33, 0) ("This request = %d bytes.\n",
2350 (int) conn
->in
.offset
);
2351 err
= errorCon(ERR_INVALID_REQ
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2352 http
= parseHttpRequestAbort(conn
, "error:request-too-large");
2353 /* add to the client request queue */
2354 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2356 http
->entry
= clientCreateStoreEntry(http
, METHOD_NONE
, null_request_flags
);
2357 errorAppendEntry(http
->entry
, err
);
2360 /* Grow the request memory area to accomodate for a large request */
2361 conn
->in
.size
+= REQUEST_BUF_SIZE
;
2362 conn
->in
.buf
= xrealloc(conn
->in
.buf
, conn
->in
.size
);
2363 /* XXX account conn->in.buf */
2364 debug(33, 2) ("Handling a large request, offset=%d inbufsize=%d\n",
2365 (int) conn
->in
.offset
, conn
->in
.size
);
2366 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2373 /* general lifetime handler for HTTP requests */
2375 requestTimeout(int fd
, void *data
)
2377 ConnStateData
*conn
= data
;
2379 debug(33, 2) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2380 if (fd_table
[fd
].rwstate
) {
2382 * Some data has been sent to the client, just close the FD
2385 } else if (conn
->nrequests
) {
2387 * assume its a persistent connection; just close it
2394 err
= errorCon(ERR_LIFETIME_EXP
, HTTP_REQUEST_TIMEOUT
);
2395 err
->url
= xstrdup("N/A");
2397 * Normally we shouldn't call errorSend() in client_side.c, but
2398 * it should be okay in this case. Presumably if we get here
2399 * this is the first request for the connection, and no data
2400 * has been written yet
2402 assert(conn
->chr
== NULL
);
2405 * if we don't close() here, we still need a timeout handler!
2407 commSetTimeout(fd
, 30, requestTimeout
, conn
);
2412 httpAcceptDefer(void)
2414 static time_t last_warn
= 0;
2415 if (fdNFree() >= RESERVED_FD
)
2417 if (last_warn
+ 15 < squid_curtime
) {
2418 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2419 last_warn
= squid_curtime
;
2424 /* Handle a new connection on HTTP socket. */
2426 httpAccept(int sock
, void *data
)
2430 ConnStateData
*connState
= NULL
;
2431 struct sockaddr_in peer
;
2432 struct sockaddr_in me
;
2433 int max
= INCOMING_HTTP_MAX
;
2435 static aclCheck_t identChecklist
;
2437 commSetSelect(sock
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2438 while (max
-- && !httpAcceptDefer()) {
2439 memset(&peer
, '\0', sizeof(struct sockaddr_in
));
2440 memset(&me
, '\0', sizeof(struct sockaddr_in
));
2441 if ((fd
= comm_accept(sock
, &peer
, &me
)) < 0) {
2442 if (!ignoreErrno(errno
))
2443 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2447 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd
);
2448 connState
= xcalloc(1, sizeof(ConnStateData
));
2449 connState
->peer
= peer
;
2450 connState
->log_addr
= peer
.sin_addr
;
2451 connState
->log_addr
.s_addr
&= Config
.Addrs
.client_netmask
.s_addr
;
2454 connState
->in
.size
= REQUEST_BUF_SIZE
;
2455 connState
->in
.buf
= xcalloc(connState
->in
.size
, 1);
2456 cbdataAdd(connState
, cbdataXfree
, 0);
2457 /* XXX account connState->in.buf */
2458 comm_add_close_handler(fd
, connStateFree
, connState
);
2459 if (Config
.onoff
.log_fqdn
)
2460 fqdncache_gethostbyaddr(peer
.sin_addr
, FQDN_LOOKUP_IF_MISS
);
2461 commSetTimeout(fd
, Config
.Timeout
.request
, requestTimeout
, connState
);
2463 identChecklist
.src_addr
= peer
.sin_addr
;
2464 if (aclCheckFast(Config
.accessList
.identLookup
, &identChecklist
))
2465 identStart(&me
, &peer
, clientIdentDone
, connState
);
2467 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, connState
, 0);
2468 commSetDefer(fd
, clientReadDefer
, connState
);
2473 #define SENDING_BODY 0
2474 #define SENDING_HDRSONLY 1
2476 clientCheckTransferDone(clientHttpRequest
* http
)
2478 int sending
= SENDING_BODY
;
2479 StoreEntry
*entry
= http
->entry
;
2486 * For now, 'done_copying' is used for special cases like
2487 * Range and HEAD requests.
2489 if (http
->flags
.done_copying
)
2492 * Handle STORE_OK objects.
2493 * objectLen(entry) will be set proprely.
2495 if (entry
->store_status
== STORE_OK
) {
2496 if (http
->out
.offset
>= objectLen(entry
))
2502 * Now, handle STORE_PENDING objects
2504 mem
= entry
->mem_obj
;
2505 assert(mem
!= NULL
);
2506 assert(http
->request
!= NULL
);
2508 if (reply
->hdr_sz
== 0)
2509 return 0; /* haven't found end of headers yet */
2510 else if (reply
->sline
.status
== HTTP_OK
)
2511 sending
= SENDING_BODY
;
2512 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
2513 sending
= SENDING_HDRSONLY
;
2514 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
2515 sending
= SENDING_HDRSONLY
;
2516 else if (reply
->sline
.status
< HTTP_OK
)
2517 sending
= SENDING_HDRSONLY
;
2518 else if (http
->request
->method
== METHOD_HEAD
)
2519 sending
= SENDING_HDRSONLY
;
2521 sending
= SENDING_BODY
;
2523 * Figure out how much data we are supposed to send.
2524 * If we are sending a body and we don't have a content-length,
2525 * then we must wait for the object to become STORE_OK.
2527 if (sending
== SENDING_HDRSONLY
)
2528 sendlen
= reply
->hdr_sz
;
2529 else if (reply
->content_length
< 0)
2532 sendlen
= reply
->content_length
+ reply
->hdr_sz
;
2534 * Now that we have the expected length, did we send it all?
2536 if (http
->out
.offset
< sendlen
)
2543 clientGotNotEnough(clientHttpRequest
* http
)
2545 int cl
= http
->entry
->mem_obj
->reply
->content_length
;
2546 int hs
= http
->entry
->mem_obj
->reply
->hdr_sz
;
2548 if (http
->out
.offset
< cl
+ hs
)
2554 * This function is designed to serve a fairly specific purpose.
2555 * Occasionally our vBNS-connected caches can talk to each other, but not
2556 * the rest of the world. Here we try to detect frequent failures which
2557 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2558 * the failure:success ratio goes above 1.0 then we go into "hit only"
2559 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2560 * will only fetch HITs from us if they are using the ICP protocol. We
2561 * stay in this mode for 5 minutes.
2563 * Duane W., Sept 16, 1996
2567 checkFailureRatio(err_type etype
, hier_code hcode
)
2569 static double magic_factor
= 100.0;
2572 if (hcode
== HIER_NONE
)
2574 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
2575 n_bad
= magic_factor
- n_good
;
2578 case ERR_CONNECT_FAIL
:
2579 case ERR_READ_ERROR
:
2585 request_failure_ratio
= n_bad
/ n_good
;
2586 if (hit_only_mode_until
> squid_curtime
)
2588 if (request_failure_ratio
< 1.0)
2590 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
2591 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2592 FAILURE_MODE_TIME
/ 60);
2593 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
2594 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
2598 clientHttpConnectionsOpen(void)
2602 for (u
= Config
.Port
.http
; u
; u
= u
->next
) {
2604 fd
= comm_open(SOCK_STREAM
,
2606 Config
.Addrs
.tcp_incoming
,
2614 commSetSelect(fd
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2615 /*commSetDefer(fd, httpAcceptDefer, NULL); */
2616 debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n",
2618 HttpSockets
[NHttpSockets
++] = fd
;
2620 if (NHttpSockets
< 1)
2621 fatal("Cannot open HTTP Port");
2625 clientHttpConnectionsClose(void)
2628 for (i
= 0; i
< NHttpSockets
; i
++) {
2629 if (HttpSockets
[i
] >= 0) {
2630 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets
[i
]);
2631 comm_close(HttpSockets
[i
]);
2632 HttpSockets
[i
] = -1;