3 * $Id: client_side.cc,v 1.435 1999/01/24 02:22:56 wessels 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
*);
77 static IDCB clientIdentDone
;
78 static int clientOnlyIfCached(clientHttpRequest
* http
);
79 static STCB clientSendMoreData
;
80 static STCB clientCacheHit
;
81 static void clientSetKeepaliveFlag(clientHttpRequest
*);
82 static void clientInterpretRequestHeaders(clientHttpRequest
*);
83 static void clientProcessRequest(clientHttpRequest
*);
84 static void clientProcessExpired(void *data
);
85 static void clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
);
86 static int clientCachable(clientHttpRequest
* http
);
87 static int clientHierarchical(clientHttpRequest
* http
);
88 static int clientCheckContentLength(request_t
* r
);
89 static int httpAcceptDefer(void);
90 static log_type
clientProcessRequest2(clientHttpRequest
* http
);
93 checkAccelOnly(clientHttpRequest
* http
)
95 /* return TRUE if someone makes a proxy request to us and
96 * we are in httpd-accel only mode */
97 if (!Config2
.Accel
.on
)
99 if (Config
.onoff
.accel_with_proxy
)
101 if (http
->request
->protocol
== PROTO_CACHEOBJ
)
103 if (http
->flags
.accel
)
109 clientIdentDone(const char *ident
, void *data
)
111 ConnStateData
*conn
= data
;
113 xstrncpy(conn
->ident
, ident
, sizeof(conn
->ident
));
115 xstrncpy(conn
->ident
, "-", sizeof(conn
->ident
));
119 clientAccessCheck(void *data
)
121 clientHttpRequest
*http
= data
;
122 ConnStateData
*conn
= http
->conn
;
124 if (checkAccelOnly(http
)) {
125 clientAccessCheckDone(0, http
);
128 browser
= httpHeaderGetStr(&http
->request
->header
, HDR_USER_AGENT
);
129 http
->acl_checklist
= aclChecklistCreate(Config
.accessList
.http
,
134 /* hack for ident ACL. It needs to get full addresses, and a
135 * place to store the ident result on persistent connections...
137 http
->acl_checklist
->conn
= conn
;
138 cbdataLock(http
->acl_checklist
->conn
);
139 aclNBCheck(http
->acl_checklist
, clientAccessCheckDone
, http
);
143 * returns true if client specified that the object must come from the cache
144 * witout contacting origin server
147 clientOnlyIfCached(clientHttpRequest
* http
)
149 const request_t
*r
= http
->request
;
151 return r
->cache_control
&&
152 EBIT_TEST(r
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
156 clientCreateStoreEntry(clientHttpRequest
* h
, method_t m
, request_flags flags
)
160 * For erroneous requests, we might not have a h->request,
161 * so make a fake one.
163 if (h
->request
== NULL
)
164 h
->request
= requestLink(requestCreate(m
, PROTO_NONE
, NULL
));
165 e
= storeCreateEntry(h
->uri
, h
->log_uri
, flags
, m
);
166 storeClientListAdd(e
, h
);
168 delaySetStoreClient(e
, h
, h
->request
->delay_id
);
170 storeClientCopy(e
, 0, 0, CLIENT_SOCK_SZ
,
171 memAllocate(MEM_CLIENT_SOCK_BUF
), clientSendMoreData
, h
);
176 clientAccessCheckDone(int answer
, void *data
)
178 clientHttpRequest
*http
= data
;
181 ErrorState
*err
= NULL
;
182 debug(33, 5) ("clientAccessCheckDone: '%s' answer=%d\n", http
->uri
, answer
);
183 http
->acl_checklist
= NULL
;
184 if (answer
== ACCESS_ALLOWED
) {
185 safe_free(http
->uri
);
186 http
->uri
= xstrdup(urlCanonical(http
->request
));
187 assert(http
->redirect_state
== REDIRECT_NONE
);
188 http
->redirect_state
= REDIRECT_PENDING
;
189 redirectStart(http
, clientRedirectDone
, http
);
191 debug(33, 5) ("Access Denied: %s\n", http
->uri
);
192 debug(33, 5) ("AclMatchedName = %s\n",
193 AclMatchedName
? AclMatchedName
: "<null>");
194 http
->log_type
= LOG_TCP_DENIED
;
195 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
197 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
198 if (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
)) {
199 if (!http
->flags
.accel
) {
200 /* Proxy authorisation needed */
201 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
203 /* WWW authorisation needed */
204 status
= HTTP_UNAUTHORIZED
;
207 page_id
= ERR_CACHE_ACCESS_DENIED
;
209 status
= HTTP_FORBIDDEN
;
211 page_id
= ERR_ACCESS_DENIED
;
213 err
= errorCon(page_id
, status
);
214 err
->request
= requestLink(http
->request
);
215 err
->src_addr
= http
->conn
->peer
.sin_addr
;
216 errorAppendEntry(http
->entry
, err
);
221 clientRedirectDone(void *data
, char *result
)
223 clientHttpRequest
*http
= data
;
224 request_t
*new_request
= NULL
;
225 request_t
*old_request
= http
->request
;
226 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
227 result
? result
: "NULL");
228 assert(http
->redirect_state
== REDIRECT_PENDING
);
229 http
->redirect_state
= REDIRECT_DONE
;
231 http_status status
= atoi(result
);
232 if (status
== 301 || status
== 302) {
234 if ((t
= strchr(result
, ':')) != NULL
) {
235 http
->redirect
.status
= status
;
236 http
->redirect
.location
= xstrdup(t
+ 1);
238 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result
);
241 if (strcmp(result
, http
->uri
))
242 new_request
= urlParse(old_request
->method
, result
);
245 safe_free(http
->uri
);
246 http
->uri
= xstrdup(urlCanonical(new_request
));
247 new_request
->http_ver
= old_request
->http_ver
;
248 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
249 new_request
->client_addr
= old_request
->client_addr
;
250 new_request
->flags
.redirected
= 1;
251 if (old_request
->body
) {
252 new_request
->body
= xmalloc(old_request
->body_sz
);
253 xmemcpy(new_request
->body
, old_request
->body
, old_request
->body_sz
);
254 new_request
->body_sz
= old_request
->body_sz
;
256 requestUnlink(old_request
);
257 http
->request
= requestLink(new_request
);
259 clientInterpretRequestHeaders(http
);
260 fd_note(http
->conn
->fd
, http
->uri
);
261 clientProcessRequest(http
);
265 clientProcessExpired(void *data
)
267 clientHttpRequest
*http
= data
;
268 char *url
= http
->uri
;
269 StoreEntry
*entry
= NULL
;
270 debug(33, 3) ("clientProcessExpired: '%s'\n", http
->uri
);
271 assert(http
->entry
->lastmod
>= 0);
273 * check if we are allowed to contact other servers
274 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
275 * a stale entry *if* it matches client requirements
277 if (clientOnlyIfCached(http
)) {
278 clientProcessOnlyIfCachedMiss(http
);
281 http
->request
->flags
.refresh
= 1;
282 http
->old_entry
= http
->entry
;
283 entry
= storeCreateEntry(url
,
285 http
->request
->flags
,
286 http
->request
->method
);
287 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
288 storeClientListAdd(entry
, http
);
289 storeClientListAdd(http
->old_entry
, http
);
291 delaySetStoreClient(entry
, http
, http
->request
->delay_id
);
292 delaySetStoreClient(http
->old_entry
, http
, http
->request
->delay_id
);
294 entry
->lastmod
= http
->old_entry
->lastmod
;
295 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry
->lastmod
);
296 entry
->refcount
++; /* EXPIRED CASE */
298 http
->out
.offset
= 0;
299 fwdStart(http
->conn
->fd
, http
->entry
, http
->request
,
300 http
->conn
->peer
.sin_addr
);
301 /* Register with storage manager to receive updates when data comes in. */
302 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
303 debug(33, 0) ("clientProcessExpired: found ENTRY_ABORTED object\n");
304 storeClientCopy(entry
,
308 memAllocate(MEM_CLIENT_SOCK_BUF
),
309 clientHandleIMSReply
,
314 clientGetsOldEntry(StoreEntry
* new_entry
, StoreEntry
* old_entry
, request_t
* request
)
316 const http_status status
= new_entry
->mem_obj
->reply
->sline
.status
;
318 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
321 /* If the reply is a failure then send the old object as a last
323 if (status
>= 500 && status
< 600) {
324 debug(33, 2) ("clientGetsOldEntry: YES, failure reply=%d\n", status
);
327 /* If the reply is anything but "Not Modified" then
328 * we must forward it to the client */
329 if (HTTP_NOT_MODIFIED
!= status
) {
330 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status
);
333 /* If the client did not send IMS in the request, then it
334 * must get the old object, not this "Not Modified" reply */
335 if (!request
->flags
.ims
) {
336 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
339 /* If the client IMS time is prior to the entry LASTMOD time we
340 * need to send the old object */
341 if (modifiedSince(old_entry
, request
)) {
342 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
346 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
352 clientHandleIMSReply(void *data
, char *buf
, ssize_t size
)
354 clientHttpRequest
*http
= data
;
355 StoreEntry
*entry
= http
->entry
;
356 MemObject
*mem
= entry
->mem_obj
;
357 const char *url
= storeUrl(entry
);
358 int unlink_request
= 0;
359 StoreEntry
*oldentry
;
361 const http_status status
= mem
->reply
->sline
.status
;
362 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url
, (int) size
);
363 if (size
< 0 && !EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
364 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
367 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
368 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url
);
369 /* We have an existing entry, but failed to validate it */
370 /* Its okay to send the old one anyway */
371 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
372 storeUnregister(entry
, http
);
373 storeUnlockObject(entry
);
374 entry
= http
->entry
= http
->old_entry
;
376 } else if (STORE_PENDING
== entry
->store_status
&& 0 == status
) {
377 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url
);
378 if (size
>= CLIENT_SOCK_SZ
) {
379 /* will not get any bigger than that */
380 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url
);
381 /* use old entry, this repeats the code abovez */
382 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
383 storeUnregister(entry
, http
);
384 storeUnlockObject(entry
);
385 entry
= http
->entry
= http
->old_entry
;
389 storeClientCopy(entry
,
390 http
->out
.offset
+ size
,
394 clientHandleIMSReply
,
398 } else if (clientGetsOldEntry(entry
, http
->old_entry
, http
->request
)) {
399 /* We initiated the IMS request, the client is not expecting
400 * 304, so put the good one back. First, make sure the old entry
401 * headers have been loaded from disk. */
402 oldentry
= http
->old_entry
;
403 http
->log_type
= LOG_TCP_REFRESH_HIT
;
404 if (oldentry
->mem_obj
->request
== NULL
) {
405 oldentry
->mem_obj
->request
= requestLink(mem
->request
);
408 /* Don't memcpy() the whole reply structure here. For example,
409 * www.thegist.com (Netscape/1.13) returns a content-length for
410 * 304's which seems to be the length of the 304 HEADERS!!! and
411 * not the body they refer to. */
412 httpReplyUpdateOnNotModified(oldentry
->mem_obj
->reply
, mem
->reply
);
413 storeTimestampsSet(oldentry
);
414 storeUnregister(entry
, http
);
415 storeUnlockObject(entry
);
416 entry
= http
->entry
= oldentry
;
417 entry
->timestamp
= squid_curtime
;
418 if (unlink_request
) {
419 requestUnlink(entry
->mem_obj
->request
);
420 entry
->mem_obj
->request
= NULL
;
423 /* the client can handle this reply, whatever it is */
424 http
->log_type
= LOG_TCP_REFRESH_MISS
;
425 if (HTTP_NOT_MODIFIED
== mem
->reply
->sline
.status
) {
426 http
->old_entry
->timestamp
= squid_curtime
;
427 http
->old_entry
->refcount
++;
428 http
->log_type
= LOG_TCP_REFRESH_HIT
;
430 storeUnregister(http
->old_entry
, http
);
431 storeUnlockObject(http
->old_entry
);
434 http
->old_entry
= NULL
; /* done with old_entry */
435 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
437 storeClientCopy(entry
,
445 clientSendMoreData(data
, buf
, size
);
450 modifiedSince(StoreEntry
* entry
, request_t
* request
)
453 MemObject
*mem
= entry
->mem_obj
;
454 time_t mod_time
= entry
->lastmod
;
455 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry
));
457 mod_time
= entry
->timestamp
;
458 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time
);
461 /* Find size of the object */
462 object_length
= mem
->reply
->content_length
;
463 if (object_length
< 0)
464 object_length
= contentLen(entry
);
465 if (mod_time
> request
->ims
) {
466 debug(33, 3) ("--> YES: entry newer than client\n");
468 } else if (mod_time
< request
->ims
) {
469 debug(33, 3) ("--> NO: entry older than client\n");
471 } else if (request
->imslen
< 0) {
472 debug(33, 3) ("--> NO: same LMT, no client length\n");
474 } else if (request
->imslen
== object_length
) {
475 debug(33, 3) ("--> NO: same LMT, same length\n");
478 debug(33, 3) ("--> YES: same LMT, different length\n");
484 clientPurgeRequest(clientHttpRequest
* http
)
487 ErrorState
*err
= NULL
;
489 debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config
.onoff
.enable_purge
);
490 if (!Config
.onoff
.enable_purge
) {
491 http
->log_type
= LOG_TCP_DENIED
;
492 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
493 err
->request
= requestLink(http
->request
);
494 err
->src_addr
= http
->conn
->peer
.sin_addr
;
495 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
496 errorAppendEntry(http
->entry
, err
);
499 http
->log_type
= LOG_TCP_MISS
;
500 if ((entry
= storeGetPublic(http
->uri
, METHOD_GET
)) == NULL
) {
501 http
->http_code
= HTTP_NOT_FOUND
;
504 http
->http_code
= HTTP_OK
;
506 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
509 * Make a new entry to hold the reply to be written
512 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
513 httpReplyReset(r
= http
->entry
->mem_obj
->reply
);
514 httpReplySetHeaders(r
, 1.0, http
->http_code
, NULL
, NULL
, 0, 0, -1);
515 httpReplySwapOut(r
, http
->entry
);
516 storeComplete(http
->entry
);
520 checkNegativeHit(StoreEntry
* e
)
522 if (!EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
524 if (e
->expires
<= squid_curtime
)
526 if (e
->store_status
!= STORE_OK
)
532 clientUpdateCounters(clientHttpRequest
* http
)
534 int svc_time
= tvSubMsec(http
->start
, current_time
);
536 HierarchyLogEntry
*H
;
537 Counter
.client_http
.requests
++;
538 if (isTcpHit(http
->log_type
))
539 Counter
.client_http
.hits
++;
540 if (http
->request
->err_type
!= ERR_NONE
)
541 Counter
.client_http
.errors
++;
542 statHistCount(&Counter
.client_http
.all_svc_time
, svc_time
);
544 * The idea here is not to be complete, but to get service times
545 * for only well-defined types. For example, we don't include
546 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
547 * (we *tried* to validate it, but failed).
549 switch (http
->log_type
) {
550 case LOG_TCP_REFRESH_HIT
:
551 statHistCount(&Counter
.client_http
.nh_svc_time
, svc_time
);
553 case LOG_TCP_IMS_HIT
:
554 statHistCount(&Counter
.client_http
.nm_svc_time
, svc_time
);
557 case LOG_TCP_MEM_HIT
:
558 case LOG_TCP_OFFLINE_HIT
:
559 statHistCount(&Counter
.client_http
.hit_svc_time
, svc_time
);
562 case LOG_TCP_CLIENT_REFRESH_MISS
:
563 statHistCount(&Counter
.client_http
.miss_svc_time
, svc_time
);
566 /* make compiler warnings go away */
569 H
= &http
->request
->hier
;
572 Counter
.cd
.times_used
++;
575 Counter
.icp
.times_used
++;
577 if (0 != i
->stop
.tv_sec
&& 0 != i
->start
.tv_sec
)
578 statHistCount(&Counter
.icp
.query_svc_time
,
579 tvSubUsec(i
->start
, i
->stop
));
581 Counter
.icp
.query_timeouts
++;
584 Counter
.netdb
.times_used
++;
592 httpRequestFree(void *data
)
594 clientHttpRequest
*http
= data
;
595 clientHttpRequest
**H
;
596 ConnStateData
*conn
= http
->conn
;
597 StoreEntry
*entry
= http
->entry
;
598 request_t
*request
= http
->request
;
599 MemObject
*mem
= NULL
;
600 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(entry
));
601 if (!clientCheckTransferDone(http
)) {
603 storeUnregister(entry
, http
);
604 entry
= http
->entry
; /* reset, IMS might have changed it */
605 if (entry
&& entry
->ping_status
== PING_WAITING
)
606 storeReleaseRequest(entry
);
608 assert(http
->log_type
< LOG_TYPE_MAX
);
610 mem
= entry
->mem_obj
;
611 if (http
->out
.size
|| http
->log_type
) {
612 http
->al
.icp
.opcode
= ICP_INVALID
;
613 http
->al
.url
= http
->log_uri
;
614 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http
->al
.url
);
616 http
->al
.http
.code
= mem
->reply
->sline
.status
;
617 http
->al
.http
.content_type
= strBuf(mem
->reply
->content_type
);
619 http
->al
.cache
.caddr
= conn
->log_addr
;
620 http
->al
.cache
.size
= http
->out
.size
;
621 http
->al
.cache
.code
= http
->log_type
;
622 http
->al
.cache
.msec
= tvSubMsec(http
->start
, current_time
);
623 if (request
->user_ident
[0])
624 http
->al
.cache
.ident
= request
->user_ident
;
626 http
->al
.cache
.ident
= conn
->ident
;
631 packerToMemInit(&p
, &mb
);
632 httpHeaderPackInto(&request
->header
, &p
);
633 http
->al
.http
.method
= request
->method
;
634 http
->al
.http
.version
= request
->http_ver
;
635 http
->al
.headers
.request
= xstrdup(mb
.buf
);
636 http
->al
.hier
= request
->hier
;
640 accessLogLog(&http
->al
);
641 clientUpdateCounters(http
);
642 clientdbUpdate(conn
->peer
.sin_addr
, http
->log_type
, PROTO_HTTP
, http
->out
.size
);
644 if (http
->acl_checklist
)
645 aclChecklistFree(http
->acl_checklist
);
647 checkFailureRatio(request
->err_type
, http
->al
.hier
.code
);
648 safe_free(http
->uri
);
649 safe_free(http
->log_uri
);
650 safe_free(http
->al
.headers
.request
);
651 safe_free(http
->al
.headers
.reply
);
652 safe_free(http
->redirect
.location
);
653 stringClean(&http
->range_iter
.boundary
);
656 storeUnregister(entry
, http
);
657 storeUnlockObject(entry
);
659 /* old_entry might still be set if we didn't yet get the reply
660 * code in clientHandleIMSReply() */
661 if (http
->old_entry
) {
662 storeUnregister(http
->old_entry
, http
);
663 storeUnlockObject(http
->old_entry
);
664 http
->old_entry
= NULL
;
666 requestUnlink(http
->request
);
667 assert(http
!= http
->next
);
668 assert(http
->conn
->chr
!= NULL
);
669 H
= &http
->conn
->chr
;
678 dlinkDelete(&http
->active
, &ClientActiveRequests
);
682 /* This is a handler normally called by comm_close() */
684 connStateFree(int fd
, void *data
)
686 ConnStateData
*connState
= data
;
687 clientHttpRequest
*http
;
688 debug(33, 3) ("connStateFree: FD %d\n", fd
);
689 assert(connState
!= NULL
);
690 while ((http
= connState
->chr
) != NULL
) {
691 assert(http
->conn
== connState
);
692 assert(connState
->chr
!= connState
->chr
->next
);
693 httpRequestFree(http
);
695 safe_free(connState
->in
.buf
);
696 /* XXX account connState->in.buf */
697 pconnHistCount(0, connState
->nrequests
);
698 cbdataFree(connState
);
700 /* prevent those nasty RST packets */
702 char buf
[SQUID_TCP_SO_RCVBUF
];
703 while (read(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0);
709 clientInterpretRequestHeaders(clientHttpRequest
* http
)
711 request_t
*request
= http
->request
;
712 const HttpHeader
*req_hdr
= &request
->header
;
714 #if USE_USERAGENT_LOG
717 request
->imslen
= -1;
718 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
719 if (request
->ims
> 0)
720 request
->flags
.ims
= 1;
721 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
722 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
723 if (strListIsMember(&s
, "no-cache", ','))
727 if (request
->cache_control
)
728 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
732 if (Config
.onoff
.reload_into_ims
)
733 request
->flags
.nocache_hack
= 1;
734 else if (refresh_nocache_hack
)
735 request
->flags
.nocache_hack
= 1;
738 request
->flags
.nocache
= 1;
740 /* ignore range header in non-GETs */
741 if (request
->method
== METHOD_GET
) {
742 request
->range
= httpHeaderGetRange(req_hdr
);
744 request
->flags
.range
= 1;
746 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
747 request
->flags
.auth
= 1;
748 if (request
->login
[0] != '\0')
749 request
->flags
.auth
= 1;
750 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
751 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
753 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
754 * Note ThisCache2 has a space prepended to the hostname so we don't
755 * accidentally match super-domains.
757 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
758 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
759 request
, (ObjPackMethod
) & httpRequestPack
);
760 request
->flags
.loopdetect
= 1;
763 fvdbCountVia(strBuf(s
));
767 #if USE_USERAGENT_LOG
768 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
769 logUserAgent(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
);
772 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
773 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
774 fvdbCountForw(strBuf(s
));
778 request
->cache_control
= httpHeaderGetCc(req_hdr
);
779 if (request
->method
== METHOD_TRACE
) {
780 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
782 if (clientCachable(http
))
783 request
->flags
.cachable
= 1;
784 if (clientHierarchical(http
))
785 request
->flags
.hierarchical
= 1;
787 if (delayClient(http
)) {
788 debug(33, 5) ("clientInterpretRequestHeaders: delay request class %d position %d\n",
789 request
->delay_id
>> 16,
790 request
->delay_id
& 0xFFFF);
793 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
794 request
->flags
.nocache
? "SET" : "NOT SET");
795 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
796 request
->flags
.cachable
? "SET" : "NOT SET");
797 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
798 request
->flags
.hierarchical
? "SET" : "NOT SET");
802 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
803 * This is the client-side persistent connection flag. We need
804 * to set this relatively early in the request processing
805 * to handle hacks for broken servers and clients.
808 clientSetKeepaliveFlag(clientHttpRequest
* http
)
810 request_t
*request
= http
->request
;
811 const HttpHeader
*req_hdr
= &request
->header
;
812 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
814 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
815 RequestMethodStr
[request
->method
]);
816 if (httpMsgIsPersistent(request
->http_ver
, req_hdr
))
817 request
->flags
.proxy_keepalive
= 1;
821 clientCheckContentLength(request_t
* r
)
823 /* We only require a content-length for "upload" methods */
824 if (!pumpMethod(r
->method
))
826 if (httpHeaderGetInt(&r
->header
, HDR_CONTENT_LENGTH
) < 0)
832 clientCachable(clientHttpRequest
* http
)
834 const char *url
= http
->uri
;
835 request_t
*req
= http
->request
;
836 method_t method
= req
->method
;
838 memset(&ch
, '\0', sizeof(ch
));
840 * Hopefully, nobody really wants 'no_cache' by client's IP
841 * address, but if they do, this should work if they use IP
842 * addresses in their ACLs, or if the client's address is in
845 * This may not work yet for 'dst' and 'dst_domain' ACLs.
847 ch
.src_addr
= http
->conn
->peer
.sin_addr
;
848 ch
.request
= http
->request
;
850 * aclCheckFast returns 1 for ALLOW and 0 for DENY. The default
851 * is ALLOW, so we require 'no_cache DENY foo' in squid.conf
852 * to indicate uncachable objects.
854 if (!aclCheckFast(Config
.accessList
.noCache
, &ch
))
856 if (req
->protocol
== PROTO_HTTP
)
857 return httpCachable(method
);
858 /* FTP is always cachable */
859 if (req
->protocol
== PROTO_GOPHER
)
860 return gopherCachable(url
);
861 if (req
->protocol
== PROTO_WAIS
)
863 if (method
== METHOD_CONNECT
)
865 if (method
== METHOD_TRACE
)
867 if (req
->protocol
== PROTO_CACHEOBJ
)
872 /* Return true if we can query our neighbors for this object */
874 clientHierarchical(clientHttpRequest
* http
)
876 const char *url
= http
->uri
;
877 request_t
*request
= http
->request
;
878 method_t method
= request
->method
;
879 const wordlist
*p
= NULL
;
881 /* IMS needs a private key, so we can use the hierarchy for IMS only
882 * if our neighbors support private keys */
883 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
885 if (request
->flags
.auth
)
887 if (method
== METHOD_TRACE
)
889 if (method
!= METHOD_GET
)
891 /* scan hierarchy_stoplist */
892 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
893 if (strstr(url
, p
->key
))
895 if (request
->flags
.loopdetect
)
897 if (request
->protocol
== PROTO_HTTP
)
898 return httpCachable(method
);
899 if (request
->protocol
== PROTO_GOPHER
)
900 return gopherCachable(url
);
901 if (request
->protocol
== PROTO_WAIS
)
903 if (request
->protocol
== PROTO_CACHEOBJ
)
909 isTcpHit(log_type code
)
911 /* this should be a bitmap for better optimization */
912 if (code
== LOG_TCP_HIT
)
914 if (code
== LOG_TCP_IMS_HIT
)
916 if (code
== LOG_TCP_REFRESH_FAIL_HIT
)
918 if (code
== LOG_TCP_REFRESH_HIT
)
920 if (code
== LOG_TCP_NEGATIVE_HIT
)
922 if (code
== LOG_TCP_MEM_HIT
)
924 if (code
== LOG_TCP_OFFLINE_HIT
)
930 * returns true if If-Range specs match reply, false otherwise
933 clientIfRangeMatch(clientHttpRequest
* http
, HttpReply
* rep
)
935 const TimeOrTag spec
= httpHeaderGetTimeOrTag(&http
->request
->header
, HDR_IF_RANGE
);
936 /* check for parsing falure */
941 ETag rep_tag
= httpHeaderGetETag(&rep
->header
, HDR_ETAG
);
942 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
943 spec
.tag
.str
, rep_tag
.str
? rep_tag
.str
: "<none>");
945 return 0; /* entity has no etag to compare with! */
946 if (spec
.tag
.weak
|| rep_tag
.weak
) {
947 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
948 spec
.tag
.str
, rep_tag
.str
);
949 return 0; /* must use strong validator for sub-range requests */
951 return etagIsEqual(&rep_tag
, &spec
.tag
);
953 /* got modification time? */
954 if (spec
.time
>= 0) {
955 return http
->entry
->lastmod
<= spec
.time
;
957 assert(0); /* should not happen */
961 /* adds appropriate Range headers if needed */
963 clientBuildRangeHeader(clientHttpRequest
* http
, HttpReply
* rep
)
965 HttpHeader
*hdr
= rep
? &rep
->header
: 0;
966 const char *range_err
= NULL
;
967 assert(http
->request
->range
);
968 /* check if we still want to do ranges */
970 range_err
= "no [parse-able] reply";
971 else if (rep
->sline
.status
!= HTTP_OK
)
972 range_err
= "wrong status code";
973 else if (httpHeaderHas(hdr
, HDR_CONTENT_RANGE
))
974 range_err
= "origin server does ranges";
975 else if (rep
->content_length
< 0)
976 range_err
= "unknown length";
977 else if (rep
->content_length
!= http
->entry
->mem_obj
->reply
->content_length
)
978 range_err
= "INCONSISTENT length"; /* a bug? */
979 else if (httpHeaderHas(&http
->request
->header
, HDR_IF_RANGE
) && !clientIfRangeMatch(http
, rep
))
980 range_err
= "If-Range match failed";
981 else if (!httpHdrRangeCanonize(http
->request
->range
, rep
->content_length
))
982 range_err
= "canonization failed";
983 else if (httpHdrRangeIsComplex(http
->request
->range
))
984 range_err
= "too complex range header";
985 /* get rid of our range specs on error */
987 debug(33, 2) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err
);
988 httpHdrRangeDestroy(http
->request
->range
);
989 http
->request
->range
= NULL
;
991 const int spec_count
= http
->request
->range
->specs
.count
;
992 debug(33, 2) ("clientBuildRangeHeader: range spec count: %d clen: %d\n",
993 spec_count
, rep
->content_length
);
994 assert(spec_count
> 0);
995 /* ETags should not be returned with Partial Content replies? */
996 httpHeaderDelById(hdr
, HDR_ETAG
);
997 /* append appropriate header(s) */
998 if (spec_count
== 1) {
999 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1000 const HttpHdrRangeSpec
*spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
);
1002 /* append Content-Range */
1003 httpHeaderAddContRange(hdr
, *spec
, rep
->content_length
);
1004 /* set new Content-Length to the actual number of OCTETs
1005 * transmitted in the message-body */
1006 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1007 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, spec
->length
);
1008 debug(33, 2) ("clientBuildRangeHeader: actual content length: %d\n", spec
->length
);
1011 /* generate boundary string */
1012 http
->range_iter
.boundary
= httpHdrRangeBoundaryStr(http
);
1013 /* delete old Content-Type, add ours */
1014 httpHeaderDelById(hdr
, HDR_CONTENT_TYPE
);
1015 httpHeaderPutStrf(hdr
, HDR_CONTENT_TYPE
,
1016 "multipart/byteranges; boundary=\"%s\"",
1017 strBuf(http
->range_iter
.boundary
));
1018 /* no need for Content-Length in multipart responses */
1019 /* but we must delete the original one if we cannot (yet)
1020 * calculate the actual length */
1021 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1026 /* filters out unwanted entries from original reply header
1027 * adds extra entries if we have more info than origin server
1028 * adds Squid specific entries */
1030 clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1032 HttpHeader
*hdr
= &rep
->header
;
1033 int is_hit
= isTcpHit(http
->log_type
);
1034 request_t
*request
= http
->request
;
1035 #if DONT_FILTER_THESE
1036 /* but you might want to if you run Squid as an HTTP accelerator */
1037 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1038 httpHeaderDelById(hdr
, HDR_ETAG
);
1040 httpHeaderDelById(hdr
, HDR_PROXY_CONNECTION
);
1041 /* here: Keep-Alive is a field-name, not a connection directive! */
1042 httpHeaderDelByName(hdr
, "Keep-Alive");
1043 /* remove Set-Cookie if a hit */
1045 httpHeaderDelById(hdr
, HDR_SET_COOKIE
);
1046 /* handle Connection header */
1047 if (httpHeaderHas(hdr
, HDR_CONNECTION
)) {
1048 /* anything that matches Connection list member will be deleted */
1049 String strConnection
= httpHeaderGetList(hdr
, HDR_CONNECTION
);
1050 const HttpHeaderEntry
*e
;
1051 HttpHeaderPos pos
= HttpHeaderInitPos
;
1053 * think: on-average-best nesting of the two loops (hdrEntry
1054 * and strListItem) @?@
1057 * maybe we should delete standard stuff ("keep-alive","close")
1058 * from strConnection first?
1060 while ((e
= httpHeaderGetEntry(hdr
, &pos
))) {
1061 if (strListIsMember(&strConnection
, strBuf(e
->name
), ','))
1062 httpHeaderDelAt(hdr
, pos
);
1064 httpHeaderDelById(hdr
, HDR_CONNECTION
);
1065 stringClean(&strConnection
);
1069 clientBuildRangeHeader(http
, rep
);
1071 * Add Age header, not that our header must replace Age headers
1072 * from other caches if any
1074 if (http
->entry
->timestamp
> 0) {
1075 httpHeaderDelById(hdr
, HDR_AGE
);
1077 * we do not follow HTTP/1.1 precisely here becuase we rely
1078 * on Date header when computing entry->timestamp; we should
1079 * be using _request_ time if Date header is not available
1080 * or if it is out of sync
1082 httpHeaderPutInt(hdr
, HDR_AGE
,
1083 http
->entry
->timestamp
<= squid_curtime
?
1084 squid_curtime
- http
->entry
->timestamp
: 0);
1086 /* Append X-Cache */
1087 httpHeaderPutStrf(hdr
, HDR_X_CACHE
, "%s from %s",
1088 is_hit
? "HIT" : "MISS", getMyHostname());
1089 #if USE_CACHE_DIGESTS
1090 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1091 httpHeaderPutStrf(hdr
, HDR_X_CACHE_LOOKUP
, "%s from %s:%d",
1092 http
->lookup_type
? http
->lookup_type
: "NONE",
1093 getMyHostname(), Config
.Port
.http
->i
);
1096 * Clear keepalive for NON-HEAD requests with invalid content length
1098 if (request
->method
!= METHOD_HEAD
)
1099 if (http
->entry
->mem_obj
->reply
->content_length
< 0)
1100 request
->flags
.proxy_keepalive
= 0;
1101 /* Signal keep-alive if needed */
1102 httpHeaderPutStr(hdr
,
1103 http
->flags
.accel
? HDR_CONNECTION
: HDR_PROXY_CONNECTION
,
1104 request
->flags
.proxy_keepalive
? "keep-alive" : "close");
1105 #if ADD_X_REQUEST_URI
1107 * Knowing the URI of the request is useful when debugging persistent
1108 * connections in a client; we cannot guarantee the order of http headers,
1109 * but X-Request-URI is likely to be the very last header to ease use from a
1110 * debugger [hdr->entries.count-1].
1112 httpHeaderPutStr(hdr
, HDR_X_REQUEST_URI
,
1113 http
->entry
->mem_obj
->url
? http
->entry
->mem_obj
->url
: http
->uri
);
1118 clientBuildReply(clientHttpRequest
* http
, const char *buf
, size_t size
)
1120 HttpReply
*rep
= httpReplyCreate();
1121 if (httpReplyParse(rep
, buf
)) {
1122 /* enforce 1.0 reply version */
1123 rep
->sline
.version
= 1.0;
1124 /* do header conversions */
1125 clientBuildReplyHeader(http
, rep
);
1126 /* if we do ranges, change status to "Partial Content" */
1127 if (http
->request
->range
)
1128 httpStatusLineSet(&rep
->sline
, rep
->sline
.version
, HTTP_PARTIAL_CONTENT
, NULL
);
1130 /* parsing failure, get rid of the invalid reply */
1131 httpReplyDestroy(rep
);
1133 /* if we were going to do ranges, backoff */
1134 if (http
->request
->range
)
1135 clientBuildRangeHeader(http
, rep
); /* will fail and destroy request->range */
1141 * clientCacheHit should only be called until the HTTP reply headers
1142 * have been parsed. Normally this should be a single call, but
1143 * it might take more than one. As soon as we have the headers,
1144 * we hand off to clientSendMoreData, clientProcessExpired, or
1145 * clientProcessMiss.
1148 clientCacheHit(void *data
, char *buf
, ssize_t size
)
1150 clientHttpRequest
*http
= data
;
1151 StoreEntry
*e
= http
->entry
;
1153 request_t
*r
= http
->request
;
1154 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http
->uri
, (int) size
);
1155 if (http
->entry
== NULL
) {
1156 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1157 debug(33, 3) ("clientCacheHit: request aborted\n");
1159 } else if (size
< 0) {
1160 /* swap in failure */
1161 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1162 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http
->uri
);
1163 http
->log_type
= LOG_TCP_SWAPFAIL_MISS
;
1164 if ((e
= http
->entry
)) {
1166 storeUnregister(e
, http
);
1167 storeUnlockObject(e
);
1169 clientProcessMiss(http
);
1174 assert(!EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
1175 if (mem
->reply
->sline
.status
== 0) {
1177 * we don't have full reply headers yet; either wait for more or
1178 * punt to clientProcessMiss.
1180 if (e
->mem_status
== IN_MEMORY
|| e
->store_status
== STORE_OK
) {
1181 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1182 clientProcessMiss(http
);
1183 } else if (size
== CLIENT_SOCK_SZ
&& http
->out
.offset
== 0) {
1184 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1185 clientProcessMiss(http
);
1187 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1189 http
->out
.offset
+ size
,
1199 * Got the headers, now grok them
1201 assert(http
->log_type
== LOG_TCP_HIT
);
1202 if (checkNegativeHit(e
)) {
1203 http
->log_type
= LOG_TCP_NEGATIVE_HIT
;
1204 clientSendMoreData(data
, buf
, size
);
1205 } else if (r
->method
== METHOD_HEAD
) {
1207 * RFC 2068 seems to indicate there is no "conditional HEAD"
1208 * request. We cannot validate a cached object for a HEAD
1209 * request, nor can we return 304.
1211 if (e
->mem_status
== IN_MEMORY
)
1212 http
->log_type
= LOG_TCP_MEM_HIT
;
1213 clientSendMoreData(data
, buf
, size
);
1214 } else if (refreshCheckHTTP(e
, r
) && !http
->flags
.internal
) {
1215 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1217 * We hold a stale copy; it needs to be validated
1220 * The 'need_validation' flag is used to prevent forwarding
1221 * loops between siblings. If our copy of the object is stale,
1222 * then we should probably only use parents for the validation
1223 * request. Otherwise two siblings could generate a loop if
1224 * both have a stale version of the object.
1226 r
->flags
.need_validation
= 1;
1227 if (e
->lastmod
< 0) {
1229 * Previous reply didn't have a Last-Modified header,
1230 * we cannot revalidate it.
1232 http
->log_type
= LOG_TCP_MISS
;
1233 clientProcessMiss(http
);
1234 } else if (r
->flags
.nocache
) {
1236 * This did not match a refresh pattern that overrides no-cache
1237 * we should honour the client no-cache header.
1239 http
->log_type
= LOG_TCP_CLIENT_REFRESH_MISS
;
1240 clientProcessMiss(http
);
1241 } else if (r
->protocol
== PROTO_HTTP
) {
1243 * Object needs to be revalidated
1244 * XXX This could apply to FTP as well, if Last-Modified is known.
1246 http
->log_type
= LOG_TCP_REFRESH_MISS
;
1247 clientProcessExpired(http
);
1250 * We don't know how to re-validate other protocols. Handle
1251 * them as if the object has expired.
1253 http
->log_type
= LOG_TCP_MISS
;
1254 clientProcessMiss(http
);
1256 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1257 } else if (r
->flags
.ims
) {
1259 * Handle If-Modified-Since requests from the client
1261 if (mem
->reply
->sline
.status
!= HTTP_OK
) {
1262 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1263 mem
->reply
->sline
.status
);
1264 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1265 clientProcessMiss(http
);
1266 } else if (modifiedSince(e
, http
->request
)) {
1267 http
->log_type
= LOG_TCP_IMS_HIT
;
1268 clientSendMoreData(data
, buf
, size
);
1270 MemBuf mb
= httpPacked304Reply(e
->mem_obj
->reply
);
1271 http
->log_type
= LOG_TCP_IMS_HIT
;
1272 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1273 storeUnregister(e
, http
);
1274 storeUnlockObject(e
);
1275 e
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
1277 httpReplyParse(e
->mem_obj
->reply
, mb
.buf
);
1278 storeAppend(e
, mb
.buf
, mb
.size
);
1284 * plain ol' cache hit
1286 if (e
->mem_status
== IN_MEMORY
)
1287 http
->log_type
= LOG_TCP_MEM_HIT
;
1288 else if (Config
.onoff
.offline
)
1289 http
->log_type
= LOG_TCP_OFFLINE_HIT
;
1290 clientSendMoreData(data
, buf
, size
);
1295 /* extracts a "range" from *buf and appends them to mb, updating all offsets and such */
1297 clientPackRange(clientHttpRequest
* http
, HttpHdrRangeIter
* i
, const char **buf
, ssize_t
* size
, MemBuf
* mb
)
1299 const size_t copy_sz
= i
->debt_size
<= *size
? i
->debt_size
: *size
;
1300 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1303 /* intersection of "have" and "need" ranges must not be empty */
1304 assert(body_off
< i
->spec
->offset
+ i
->spec
->length
);
1305 assert(body_off
+ *size
> i
->spec
->offset
);
1306 /* put boundary and headers at the beginning of range in a multi-range */
1307 if (http
->request
->range
->specs
.count
> 1 && i
->debt_size
== i
->spec
->length
) {
1308 HttpReply
*rep
= http
->entry
->mem_obj
? /* original reply */
1309 http
->entry
->mem_obj
->reply
: NULL
;
1314 debug(33, 5) ("clientPackRange: appending boundary: %s\n", strBuf(i
->boundary
));
1315 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1316 memBufPrintf(mb
, "\r\n--%s\r\n", strBuf(i
->boundary
));
1317 httpHeaderInit(&hdr
, hoReply
);
1318 if (httpHeaderHas(&rep
->header
, HDR_CONTENT_TYPE
))
1319 httpHeaderPutStr(&hdr
, HDR_CONTENT_TYPE
, httpHeaderGetStr(&rep
->header
, HDR_CONTENT_TYPE
));
1320 httpHeaderAddContRange(&hdr
, *i
->spec
, rep
->content_length
);
1321 packerToMemInit(&p
, mb
);
1322 httpHeaderPackInto(&hdr
, &p
);
1324 httpHeaderClean(&hdr
);
1325 /* append <crlf> (we packed a header, not a reply */
1326 memBufPrintf(mb
, crlf
);
1329 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz
);
1330 memBufAppend(mb
, *buf
, copy_sz
);
1331 /* update offsets */
1333 i
->debt_size
-= copy_sz
;
1334 body_off
+= copy_sz
;
1336 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1337 /* paranoid check */
1338 assert(*size
>= 0 && i
->debt_size
>= 0);
1341 /* returns true if there is still data available to pack more ranges
1342 * increments iterator "i"
1343 * used by clientPackMoreRanges */
1345 clientCanPackMoreRanges(const clientHttpRequest
* http
, HttpHdrRangeIter
* i
, ssize_t size
)
1347 /* first update "i" if needed */
1348 if (!i
->debt_size
) {
1349 if ((i
->spec
= httpHdrRangeGetSpec(http
->request
->range
, &i
->pos
)))
1350 i
->debt_size
= i
->spec
->length
;
1352 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1353 /* continue condition: need_more_data && have_more_data */
1354 return i
->spec
&& size
> 0;
1357 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1358 /* returns true if we need more data */
1360 clientPackMoreRanges(clientHttpRequest
* http
, const char *buf
, ssize_t size
, MemBuf
* mb
)
1362 HttpHdrRangeIter
*i
= &http
->range_iter
;
1363 /* offset in range specs does not count the prefix of an http msg */
1364 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1366 /* check: reply was parsed and range iterator was initialized */
1367 assert(i
->prefix_size
> 0);
1368 /* filter out data according to range specs */
1369 while (clientCanPackMoreRanges(http
, i
, size
)) {
1370 off_t start
; /* offset of still missing data */
1372 start
= i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
;
1373 debug(33, 2) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1374 (int) body_off
, size
);
1375 debug(33, 2) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1376 (int) start
, (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
, i
->debt_size
);
1377 assert(body_off
<= start
); /* we did not miss it */
1378 /* skip up to start */
1379 if (body_off
+ size
> start
) {
1380 const size_t skip_size
= start
- body_off
;
1385 /* has not reached start yet */
1390 /* put next chunk if any */
1392 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1393 clientPackRange(http
, i
, &buf
, &size
, mb
);
1394 body_off
= http
->out
.offset
- i
->prefix_size
; /* sync */
1397 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1398 debug(33, 2) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1399 (int) body_off
, size
, i
->debt_size
);
1401 debug(33, 2) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1402 (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
);
1403 /* skip the data we do not need if possible */
1404 if (i
->debt_size
== i
->spec
->length
) /* at the start of the cur. spec */
1405 body_off
= i
->spec
->offset
;
1407 assert(body_off
== i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
);
1408 } else if (http
->request
->range
->specs
.count
> 1) {
1409 /* put terminating boundary for multiparts */
1410 memBufPrintf(mb
, "\r\n--%s--\r\n", strBuf(i
->boundary
));
1412 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1413 return i
->debt_size
> 0;
1417 * accepts chunk of a http message in buf, parses prefix, filters headers and
1418 * such, writes processed message to the client's socket
1421 clientSendMoreData(void *data
, char *buf
, ssize_t size
)
1423 clientHttpRequest
*http
= data
;
1424 StoreEntry
*entry
= http
->entry
;
1425 ConnStateData
*conn
= http
->conn
;
1427 HttpReply
*rep
= NULL
;
1428 const char *body_buf
= buf
;
1429 ssize_t body_size
= size
;
1431 ssize_t check_size
= 0;
1432 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http
->uri
, (int) size
);
1433 assert(size
<= CLIENT_SOCK_SZ
);
1434 assert(http
->request
!= NULL
);
1435 dlinkDelete(&http
->active
, &ClientActiveRequests
);
1436 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1437 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1438 fd
, storeUrl(entry
), (int) http
->out
.offset
);
1439 if (conn
->chr
!= http
) {
1440 /* there is another object in progress, defer this one */
1441 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry
));
1442 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1444 } else if (entry
&& EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1445 /* call clientWriteComplete so the client socket gets closed */
1446 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1447 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1449 } else if (size
< 0) {
1450 /* call clientWriteComplete so the client socket gets closed */
1451 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1452 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1454 } else if (size
== 0) {
1455 /* call clientWriteComplete so the client socket gets closed */
1456 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1457 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1460 if (http
->out
.offset
== 0) {
1461 if (Config
.onoff
.log_mime_hdrs
) {
1463 if ((k
= headersEnd(buf
, size
))) {
1464 safe_free(http
->al
.headers
.reply
);
1465 http
->al
.headers
.reply
= xcalloc(k
+ 1, 1);
1466 xstrncpy(http
->al
.headers
.reply
, buf
, k
);
1469 rep
= clientBuildReply(http
, buf
, size
);
1471 body_size
= size
- rep
->hdr_sz
;
1472 assert(body_size
>= 0);
1473 body_buf
= buf
+ rep
->hdr_sz
;
1474 http
->range_iter
.prefix_size
= rep
->hdr_sz
;
1475 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1476 body_size
, rep
->hdr_sz
);
1477 } else if (size
< CLIENT_SOCK_SZ
&& entry
->store_status
== STORE_PENDING
) {
1478 /* wait for more to arrive */
1479 storeClientCopy(entry
,
1480 http
->out
.offset
+ size
,
1488 /* reset range iterator */
1489 http
->range_iter
.pos
= HttpHdrRangeInitPos
;
1491 if (http
->request
->method
== METHOD_HEAD
) {
1493 /* do not forward body for HEAD replies */
1495 http
->flags
.done_copying
= 1;
1498 * If we are here, then store_status == STORE_OK and it
1499 * seems we have a HEAD repsponse which is missing the
1500 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1501 * does this). Because clientBuildReply() fails we just
1502 * call this reply a body, set the done_copying flag and
1505 http
->flags
.done_copying
= 1;
1508 /* write headers and/or body if any */
1509 assert(rep
|| (body_buf
&& body_size
));
1510 /* init mb; put status line and headers if any */
1512 mb
= httpReplyPack(rep
);
1513 http
->out
.offset
+= rep
->hdr_sz
;
1514 check_size
+= rep
->hdr_sz
;
1515 httpReplyDestroy(rep
);
1520 /* append body if any */
1521 if (http
->request
->range
) {
1522 /* Only GET requests should have ranges */
1523 assert(http
->request
->method
== METHOD_GET
);
1524 /* clientPackMoreRanges() updates http->out.offset */
1525 /* force the end of the transfer if we are done */
1526 if (!clientPackMoreRanges(http
, body_buf
, body_size
, &mb
))
1527 http
->flags
.done_copying
= 1;
1528 } else if (body_buf
&& body_size
) {
1529 http
->out
.offset
+= body_size
;
1530 check_size
+= body_size
;
1531 memBufAppend(&mb
, body_buf
, body_size
);
1533 if (!http
->request
->range
&& http
->request
->method
== METHOD_GET
)
1534 assert(check_size
== size
);
1536 comm_write_mbuf(fd
, mb
, clientWriteComplete
, http
);
1537 /* if we don't do it, who will? */
1538 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1542 clientKeepaliveNextRequest(clientHttpRequest
* http
)
1544 ConnStateData
*conn
= http
->conn
;
1546 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn
->fd
);
1547 conn
->defer
.until
= 0; /* Kick it to read a new request */
1548 httpRequestFree(http
);
1549 if ((http
= conn
->chr
) == NULL
) {
1550 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1552 fd_note(conn
->fd
, "Reading next request");
1554 * Set the timeout BEFORE calling clientReadRequest().
1556 commSetTimeout(conn
->fd
, 15, requestTimeout
, conn
);
1557 clientReadRequest(conn
->fd
, conn
); /* Read next request */
1559 * Note, the FD may be closed at this point.
1561 } else if ((entry
= http
->entry
) == NULL
) {
1563 * this request is in progress, maybe doing an ACL or a redirect,
1564 * execution will resume after the operation completes.
1567 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1570 if (0 == storeClientCopyPending(entry
, http
)) {
1571 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1572 debug(33, 0) ("clientKeepaliveNextRequest: ENTRY_ABORTED\n");
1573 storeClientCopy(entry
,
1577 memAllocate(MEM_CLIENT_SOCK_BUF
),
1585 clientWriteComplete(int fd
, char *bufnotused
, size_t size
, int errflag
, void *data
)
1587 clientHttpRequest
*http
= data
;
1588 StoreEntry
*entry
= http
->entry
;
1590 http
->out
.size
+= size
;
1591 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1592 fd
, size
, errflag
, (int) http
->out
.offset
, entry
? objectLen(entry
) : 0);
1594 kb_incr(&Counter
.client_http
.kbytes_out
, size
);
1595 if (isTcpHit(http
->log_type
))
1596 kb_incr(&Counter
.client_http
.hit_kbytes_out
, size
);
1600 * just close the socket, httpRequestFree will abort if needed
1603 } else if (NULL
== entry
) {
1604 comm_close(fd
); /* yuk */
1605 } else if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1607 } else if ((done
= clientCheckTransferDone(http
)) != 0 || size
== 0) {
1608 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd
);
1609 /* We're finished case */
1610 if (http
->entry
->mem_obj
->reply
->content_length
< 0) {
1611 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1614 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1616 } else if (clientGotNotEnough(http
)) {
1617 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1619 } else if (http
->request
->flags
.proxy_keepalive
) {
1620 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd
);
1621 clientKeepaliveNextRequest(http
);
1626 /* More data will be coming from primary server; register with
1627 * storage manager. */
1628 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1629 debug(33, 0) ("clientWriteComplete 2: ENTRY_ABORTED\n");
1630 storeClientCopy(entry
,
1634 memAllocate(MEM_CLIENT_SOCK_BUF
),
1641 * client issued a request with an only-if-cached cache-control directive;
1642 * we did not find a cached object that can be returned without
1643 * contacting other servers;
1644 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1647 clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
)
1649 char *url
= http
->uri
;
1650 request_t
*r
= http
->request
;
1651 ErrorState
*err
= NULL
;
1652 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1653 RequestMethodStr
[r
->method
], url
);
1654 http
->al
.http
.code
= HTTP_GATEWAY_TIMEOUT
;
1655 err
= errorCon(ERR_ONLY_IF_CACHED_MISS
, HTTP_GATEWAY_TIMEOUT
);
1656 err
->request
= requestLink(r
);
1657 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1659 storeUnregister(http
->entry
, http
);
1660 storeUnlockObject(http
->entry
);
1662 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1663 errorAppendEntry(http
->entry
, err
);
1667 clientProcessRequest2(clientHttpRequest
* http
)
1669 request_t
*r
= http
->request
;
1671 e
= http
->entry
= storeGetPublic(http
->uri
, r
->method
);
1672 if (r
->method
== METHOD_HEAD
&& e
== NULL
) {
1673 /* We can generate a HEAD reply from a cached GET object */
1674 e
= http
->entry
= storeGetPublic(http
->uri
, METHOD_GET
);
1676 #if USE_CACHE_DIGESTS
1677 http
->lookup_type
= e
? "HIT" : "MISS";
1680 /* this object isn't in the cache */
1681 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
1682 return LOG_TCP_MISS
;
1684 if (Config
.onoff
.offline
) {
1685 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
1689 if (!storeEntryValidToSend(e
)) {
1690 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
1692 return LOG_TCP_MISS
;
1694 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1695 /* Special entries are always hits, no matter what the client says */
1696 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
1701 if (r
->flags
.nocache_hack
) {
1702 /* if nocache_hack is set, nocache should always be clear, right? */
1703 assert(!r
->flags
.nocache
);
1704 ipcacheReleaseInvalid(r
->host
);
1707 if (e
->store_status
== STORE_PENDING
) {
1708 if (r
->flags
.nocache
|| r
->flags
.nocache_hack
) {
1709 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
1711 r
->flags
.nocache
= 0;
1712 r
->flags
.nocache_hack
= 0;
1716 if (r
->flags
.nocache
) {
1717 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
1719 ipcacheReleaseInvalid(r
->host
);
1720 return LOG_TCP_CLIENT_REFRESH_MISS
;
1722 if (r
->range
&& httpHdrRangeWillBeComplex(r
->range
)) {
1724 * Some clients break if we return "200 OK" for a Range
1725 * request. We would have to return "200 OK" for a _complex_
1726 * Range request that is also a HIT. Thus, let's prevent HITs
1727 * on complex Range requests
1729 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
1731 return LOG_TCP_MISS
;
1733 debug(33, 3) ("clientProcessRequest2: default HIT\n");
1739 clientProcessRequest(clientHttpRequest
* http
)
1741 char *url
= http
->uri
;
1742 request_t
*r
= http
->request
;
1743 int fd
= http
->conn
->fd
;
1745 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
1746 RequestMethodStr
[r
->method
],
1748 if (r
->method
== METHOD_CONNECT
) {
1749 http
->log_type
= LOG_TCP_MISS
;
1750 sslStart(fd
, url
, r
, &http
->out
.size
);
1752 } else if (r
->method
== METHOD_PURGE
) {
1753 clientPurgeRequest(http
);
1755 } else if (r
->method
== METHOD_TRACE
) {
1756 if (r
->max_forwards
== 0) {
1757 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1758 storeReleaseRequest(http
->entry
);
1759 storeBuffer(http
->entry
);
1760 rep
= httpReplyCreate();
1761 httpReplySetHeaders(rep
, 1.0, HTTP_OK
, NULL
, "text/plain",
1762 httpRequestPrefixLen(r
), 0, squid_curtime
);
1763 httpReplySwapOut(rep
, http
->entry
);
1764 httpReplyDestroy(rep
);
1765 httpRequestSwapOut(r
, http
->entry
);
1766 storeComplete(http
->entry
);
1770 http
->log_type
= LOG_TCP_MISS
;
1771 } else if (pumpMethod(r
->method
)) {
1772 http
->log_type
= LOG_TCP_MISS
;
1773 /* XXX oof, POST can be cached! */
1774 pumpInit(fd
, r
, http
->uri
);
1776 http
->log_type
= clientProcessRequest2(http
);
1778 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
1779 log_tags
[http
->log_type
],
1781 http
->out
.offset
= 0;
1782 if (NULL
!= http
->entry
) {
1783 storeLockObject(http
->entry
);
1784 storeCreateMemObject(http
->entry
, http
->uri
, http
->log_uri
);
1785 storeClientListAdd(http
->entry
, http
);
1787 delaySetStoreClient(http
->entry
, http
, http
->request
->delay_id
);
1789 http
->entry
->refcount
++;
1790 storeClientCopy(http
->entry
,
1794 memAllocate(MEM_CLIENT_SOCK_BUF
),
1799 http
->log_type
= LOG_TCP_MISS
;
1800 clientProcessMiss(http
);
1805 * Prepare to fetch the object as it's a cache miss of some kind.
1808 clientProcessMiss(clientHttpRequest
* http
)
1810 char *url
= http
->uri
;
1811 request_t
*r
= http
->request
;
1812 ErrorState
*err
= NULL
;
1813 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
1814 RequestMethodStr
[r
->method
], url
);
1816 * We might have a left-over StoreEntry from a failed cache hit
1820 if (EBIT_TEST(http
->entry
->flags
, ENTRY_SPECIAL
))
1821 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url
);
1822 storeUnregister(http
->entry
, http
);
1823 storeUnlockObject(http
->entry
);
1826 if (clientOnlyIfCached(http
)) {
1827 clientProcessOnlyIfCachedMiss(http
);
1831 * Deny loops when running in accelerator/transproxy mode.
1833 if (http
->flags
.accel
&& r
->flags
.loopdetect
) {
1834 http
->al
.http
.code
= HTTP_FORBIDDEN
;
1835 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
1836 err
->request
= requestLink(r
);
1837 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1838 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1839 errorAppendEntry(http
->entry
, err
);
1842 assert(http
->out
.offset
== 0);
1843 http
->entry
= clientCreateStoreEntry(http
, r
->method
, r
->flags
);
1844 http
->entry
->refcount
++;
1845 if (http
->redirect
.status
) {
1846 HttpReply
*rep
= httpReplyCreate();
1847 storeReleaseRequest(http
->entry
);
1848 httpRedirectReply(rep
, http
->redirect
.status
, http
->redirect
.location
);
1849 httpReplySwapOut(rep
, http
->entry
);
1850 httpReplyDestroy(rep
);
1851 storeComplete(http
->entry
);
1854 if (http
->flags
.internal
)
1855 r
->protocol
= PROTO_INTERNAL
;
1856 fwdStart(http
->conn
->fd
, http
->entry
, r
, http
->conn
->peer
.sin_addr
);
1859 static clientHttpRequest
*
1860 parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
)
1862 clientHttpRequest
*http
= xcalloc(1, sizeof(clientHttpRequest
));
1863 cbdataAdd(http
, cbdataXfree
, 0);
1865 http
->start
= current_time
;
1866 http
->req_sz
= conn
->in
.offset
;
1867 http
->uri
= xstrdup(uri
);
1868 http
->log_uri
= xstrndup(uri
, MAX_URL
);
1869 http
->range_iter
.boundary
= StringNull
;
1870 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1875 * parseHttpRequest()
1878 * NULL on error or incomplete request
1879 * a clientHttpRequest structure on success
1881 static clientHttpRequest
*
1882 parseHttpRequest(ConnStateData
* conn
, method_t
* method_p
, int *status
,
1883 char **prefix_p
, size_t * req_line_sz_p
)
1888 char *req_hdr
= NULL
;
1893 int free_request
= 0;
1894 size_t header_sz
; /* size of headers, not including first line */
1895 size_t prefix_sz
; /* size of whole request (req-line + headers) */
1899 clientHttpRequest
*http
= NULL
;
1901 struct natlookup natLookup
;
1902 static int natfd
= -1;
1905 if ((req_sz
= headersEnd(conn
->in
.buf
, conn
->in
.offset
)) == 0) {
1906 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
1909 *method_p
= METHOD_NONE
;
1912 assert(req_sz
<= conn
->in
.offset
);
1913 /* Use memcpy, not strdup! */
1914 inbuf
= xmalloc(req_sz
+ 1);
1915 xmemcpy(inbuf
, conn
->in
.buf
, req_sz
);
1916 *(inbuf
+ req_sz
) = '\0';
1918 /* pre-set these values to make aborting simpler */
1920 *method_p
= METHOD_NONE
;
1923 /* Barf on NULL characters in the headers */
1924 if (strlen(inbuf
) != req_sz
) {
1925 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
1926 return parseHttpRequestAbort(conn
, "error:invalid-request");
1928 /* Look for request method */
1929 if ((mstr
= strtok(inbuf
, "\t ")) == NULL
) {
1930 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
1931 return parseHttpRequestAbort(conn
, "error:invalid-request-method");
1933 method
= urlParseMethod(mstr
);
1934 if (method
== METHOD_NONE
) {
1935 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr
);
1936 return parseHttpRequestAbort(conn
, "error:unsupported-request-method");
1938 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr
);
1941 /* look for URL+HTTP/x.x */
1942 if ((url
= strtok(NULL
, "\n")) == NULL
) {
1943 debug(33, 1) ("parseHttpRequest: Missing URL\n");
1944 return parseHttpRequestAbort(conn
, "error:missing-url");
1946 while (isspace(*url
))
1948 t
= url
+ strlen(url
);
1953 if (isspace(*t
) && !strncmp(t
+ 1, "HTTP/", 5)) {
1958 while (t
> url
&& isspace(*t
))
1960 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url
);
1961 if (token
== NULL
) {
1962 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
1963 #if RELAXED_HTTP_PARSER
1964 http_ver
= (float) 0.9; /* wild guess */
1966 return parseHttpRequestAbort(conn
, "error:missing-http-ident");
1969 http_ver
= (float) atof(token
+ 5);
1973 * Process headers after request line
1975 req_hdr
= strtok(NULL
, null_string
);
1976 header_sz
= req_sz
- (req_hdr
- inbuf
);
1977 if (0 == header_sz
) {
1978 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
1982 assert(header_sz
> 0);
1983 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr
);
1984 end
= req_hdr
+ header_sz
;
1985 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end
);
1987 prefix_sz
= end
- inbuf
;
1988 *req_line_sz_p
= req_hdr
- inbuf
;
1989 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
1990 (int) prefix_sz
, (int) *req_line_sz_p
);
1991 assert(prefix_sz
<= conn
->in
.offset
);
1993 /* Ok, all headers are received */
1994 http
= xcalloc(1, sizeof(clientHttpRequest
));
1995 cbdataAdd(http
, cbdataXfree
, 0);
1996 http
->http_ver
= http_ver
;
1998 http
->start
= current_time
;
1999 http
->req_sz
= prefix_sz
;
2000 http
->range_iter
.boundary
= StringNull
;
2001 *prefix_p
= xmalloc(prefix_sz
+ 1);
2002 xmemcpy(*prefix_p
, conn
->in
.buf
, prefix_sz
);
2003 *(*prefix_p
+ prefix_sz
) = '\0';
2004 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2006 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p
) + *req_line_sz_p
);
2007 if ((t
= strchr(url
, '#'))) /* remove HTML anchors */
2010 /* handle internal objects */
2011 if (internalCheck(url
)) {
2012 /* prepend our name & port */
2013 http
->uri
= xstrdup(internalLocalUri(NULL
, url
));
2014 http
->flags
.internal
= 1;
2015 http
->flags
.accel
= 1;
2017 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2018 else if (Config2
.Accel
.on
&& *url
== '/') {
2019 /* prepend the accel prefix */
2020 if (opt_accel_uses_host
&& (t
= mime_get_header(req_hdr
, "Host"))) {
2021 /* If a Host: header was specified, use it to build the URL
2022 * instead of the one in the Config file. */
2024 * XXX Use of the Host: header here opens a potential
2025 * security hole. There are no checks that the Host: value
2026 * corresponds to one of your servers. It might, for example,
2027 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2028 * types should be used to prevent httpd-accelerators
2029 * handling requests for non-local servers */
2031 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
+
2033 http
->uri
= xcalloc(url_sz
, 1);
2034 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2035 t
, (int) Config
.Accel
.port
, url
);
2036 } else if (vhost_mode
) {
2037 /* Put the local socket IP address as the hostname */
2038 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
;
2039 http
->uri
= xcalloc(url_sz
, 1);
2041 natLookup
.nl_inport
= http
->conn
->me
.sin_port
;
2042 natLookup
.nl_outport
= http
->conn
->peer
.sin_port
;
2043 natLookup
.nl_inip
= http
->conn
->me
.sin_addr
;
2044 natLookup
.nl_outip
= http
->conn
->peer
.sin_addr
;
2045 natLookup
.nl_flags
= IPN_TCP
;
2047 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
2049 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2051 return parseHttpRequestAbort(conn
, "error:nat-open-failed");
2053 if (ioctl(natfd
, SIOCGNATL
, &natLookup
) < 0) {
2054 if (errno
!= ESRCH
) {
2055 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2058 return parseHttpRequestAbort(conn
, "error:nat-lookup-failed");
2060 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2061 inet_ntoa(http
->conn
->me
.sin_addr
),
2062 (int) Config
.Accel
.port
,
2065 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2066 inet_ntoa(natLookup
.nl_realip
),
2067 (int) Config
.Accel
.port
,
2070 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2071 inet_ntoa(http
->conn
->me
.sin_addr
),
2072 (int) Config
.Accel
.port
,
2075 debug(33, 5) ("VHOST REWRITE: '%s'\n", http
->uri
);
2077 url_sz
= strlen(Config2
.Accel
.prefix
) + strlen(url
) +
2078 Config
.appendDomainLen
+ 1;
2079 http
->uri
= xcalloc(url_sz
, 1);
2080 snprintf(http
->uri
, url_sz
, "%s%s", Config2
.Accel
.prefix
, url
);
2082 http
->flags
.accel
= 1;
2084 /* URL may be rewritten later, so make extra room */
2085 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
2086 http
->uri
= xcalloc(url_sz
, 1);
2087 strcpy(http
->uri
, url
);
2088 http
->flags
.accel
= 0;
2090 if (!stringHasWhitespace(http
->uri
))
2091 http
->log_uri
= xstrndup(http
->uri
, MAX_URL
);
2093 http
->log_uri
= xstrndup(rfc1738_escape(http
->uri
), MAX_URL
);
2094 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2103 clientReadDefer(int fdnotused
, void *data
)
2105 ConnStateData
*conn
= data
;
2106 return conn
->defer
.until
> squid_curtime
;
2110 clientReadRequest(int fd
, void *data
)
2112 ConnStateData
*conn
= data
;
2113 int parser_return_code
= 0;
2115 request_t
*request
= NULL
;
2118 clientHttpRequest
*http
= NULL
;
2119 clientHttpRequest
**H
= NULL
;
2120 char *prefix
= NULL
;
2121 ErrorState
*err
= NULL
;
2122 fde
*F
= &fd_table
[fd
];
2123 int len
= conn
->in
.size
- conn
->in
.offset
- 1;
2124 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd
);
2125 Counter
.syscalls
.sock
.reads
++;
2126 size
= read(fd
, conn
->in
.buf
+ conn
->in
.offset
, len
);
2128 fd_bytes(fd
, size
, FD_READ
);
2129 kb_incr(&Counter
.client_http
.kbytes_in
, size
);
2132 * Don't reset the timeout value here. The timeout value will be
2133 * set to Config.Timeout.request by httpAccept() and
2134 * clientWriteComplete(), and should apply to the request as a
2135 * whole, not individual read() calls. Plus, it breaks our
2136 * lame half-close detection
2138 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, conn
, 0);
2140 if (conn
->chr
== NULL
) {
2141 /* no current or pending requests */
2144 } else if (!Config
.onoff
.half_closed_clients
) {
2145 /* admin doesn't want to support half-closed client sockets */
2149 /* It might be half-closed, we can't tell */
2150 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd
);
2151 F
->flags
.socket_eof
= 1;
2152 conn
->defer
.until
= squid_curtime
+ 1;
2154 fd_note(fd
, "half-closed");
2156 } else if (size
< 0) {
2157 if (!ignoreErrno(errno
)) {
2158 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd
, xstrerror());
2161 } else if (conn
->in
.offset
== 0) {
2162 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd
, xstrerror());
2165 /* Continue to process previously read data */
2168 conn
->in
.offset
+= size
;
2169 /* Skip leading (and trailing) whitespace */
2170 while (conn
->in
.offset
> 0) {
2173 while (conn
->in
.offset
> 0 && isspace(conn
->in
.buf
[0])) {
2174 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ 1, conn
->in
.offset
- 1);
2177 conn
->in
.buf
[conn
->in
.offset
] = '\0'; /* Terminate the string */
2178 if (conn
->in
.offset
== 0)
2180 /* Limit the number of concurrent requests to 2 */
2181 for (H
= &conn
->chr
, nrequests
= 0; *H
; H
= &(*H
)->next
, nrequests
++);
2182 if (nrequests
>= 2) {
2183 debug(33, 2) ("clientReadRequest: FD %d max concurrent requests reached\n", fd
);
2184 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd
);
2185 conn
->defer
.until
= squid_curtime
+ 100; /* Reset when a request is complete */
2188 /* Process request */
2189 http
= parseHttpRequest(conn
,
2191 &parser_return_code
,
2197 assert(http
->req_sz
> 0);
2198 conn
->in
.offset
-= http
->req_sz
;
2199 assert(conn
->in
.offset
>= 0);
2200 debug(33, 5) ("conn->in.offset = %d\n", (int) conn
->in
.offset
);
2202 * If we read past the end of this request, move the remaining
2203 * data to the beginning
2205 if (conn
->in
.offset
> 0)
2206 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ http
->req_sz
, conn
->in
.offset
);
2207 /* add to the client request queue */
2208 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2211 commSetTimeout(fd
, Config
.Timeout
.lifetime
, NULL
, NULL
);
2212 if (parser_return_code
< 0) {
2213 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd
);
2214 err
= errorCon(ERR_INVALID_REQ
, HTTP_BAD_REQUEST
);
2215 err
->request_hdrs
= xstrdup(conn
->in
.buf
);
2216 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2217 errorAppendEntry(http
->entry
, err
);
2221 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
2222 debug(33, 5) ("Invalid URL: %s\n", http
->uri
);
2223 err
= errorCon(ERR_INVALID_URL
, HTTP_BAD_REQUEST
);
2224 err
->src_addr
= conn
->peer
.sin_addr
;
2225 err
->url
= xstrdup(http
->uri
);
2226 http
->al
.http
.code
= err
->http_status
;
2227 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2228 errorAppendEntry(http
->entry
, err
);
2232 /* compile headers */
2233 /* we should skip request line! */
2234 if (!httpRequestParseHeader(request
, prefix
+ req_line_sz
))
2235 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2237 /* continue anyway? */
2239 request
->flags
.accelerated
= http
->flags
.accel
;
2240 if (!http
->flags
.internal
) {
2241 if (internalCheck(strBuf(request
->urlpath
))) {
2242 if (0 == strcasecmp(request
->host
, internalHostname())) {
2243 if (request
->port
== Config
.Port
.http
->i
)
2244 http
->flags
.internal
= 1;
2245 } else if (internalStaticCheck(strBuf(request
->urlpath
))) {
2246 xstrncpy(request
->host
, internalHostname(), SQUIDHOSTNAMELEN
);
2247 request
->port
= Config
.Port
.http
->i
;
2248 http
->flags
.internal
= 1;
2252 request
->flags
.internal
= http
->flags
.internal
;
2254 safe_free(http
->log_uri
);
2255 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
2256 request
->client_addr
= conn
->peer
.sin_addr
;
2257 request
->http_ver
= http
->http_ver
;
2258 if (!urlCheckRequest(request
)) {
2259 err
= errorCon(ERR_UNSUP_REQ
, HTTP_NOT_IMPLEMENTED
);
2260 err
->src_addr
= conn
->peer
.sin_addr
;
2261 err
->request
= requestLink(request
);
2262 http
->al
.http
.code
= err
->http_status
;
2263 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2264 errorAppendEntry(http
->entry
, err
);
2267 if (0 == clientCheckContentLength(request
)) {
2268 err
= errorCon(ERR_INVALID_REQ
, HTTP_LENGTH_REQUIRED
);
2269 err
->src_addr
= conn
->peer
.sin_addr
;
2270 err
->request
= requestLink(request
);
2271 http
->al
.http
.code
= err
->http_status
;
2272 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2273 errorAppendEntry(http
->entry
, err
);
2276 http
->request
= requestLink(request
);
2278 * We need to set the keepalive flag before doing some
2279 * hacks for POST/PUT requests below. Maybe we could
2280 * set keepalive flag even earlier.
2282 clientSetKeepaliveFlag(http
);
2284 * break here for NON-GET because most likely there is a
2285 * reqeust body following and we don't want to parse it
2286 * as though it was new request
2288 if (request
->method
!= METHOD_GET
) {
2289 int cont_len
= httpHeaderGetInt(&request
->header
, HDR_CONTENT_LENGTH
);
2290 int copy_len
= XMIN(conn
->in
.offset
, cont_len
);
2292 assert(conn
->in
.offset
>= copy_len
);
2293 request
->body_sz
= copy_len
;
2294 request
->body
= xmalloc(request
->body_sz
);
2295 xmemcpy(request
->body
, conn
->in
.buf
, request
->body_sz
);
2296 conn
->in
.offset
-= copy_len
;
2297 if (conn
->in
.offset
)
2298 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ copy_len
, conn
->in
.offset
);
2301 * if we didn't get the full body now, then more will
2302 * be arriving on the client socket. Lets cancel
2303 * the read handler until this request gets forwarded.
2305 if (request
->body_sz
< cont_len
)
2306 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2308 clientAccessCheck(http
);
2309 continue; /* while offset > 0 */
2310 } else if (parser_return_code
== 0) {
2312 * Partial request received; reschedule until parseHttpRequest()
2313 * is happy with the input
2315 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2317 if (conn
->in
.offset
>= Config
.maxRequestSize
) {
2318 /* The request is too large to handle */
2319 debug(33, 0) ("Request won't fit in buffer.\n");
2320 debug(33, 0) ("Config 'request_size'= %d bytes.\n",
2321 Config
.maxRequestSize
);
2322 debug(33, 0) ("This request = %d bytes.\n",
2323 (int) conn
->in
.offset
);
2324 err
= errorCon(ERR_INVALID_REQ
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2325 http
= parseHttpRequestAbort(conn
, "error:request-too-large");
2326 /* add to the client request queue */
2327 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2329 http
->entry
= clientCreateStoreEntry(http
, METHOD_NONE
, null_request_flags
);
2330 errorAppendEntry(http
->entry
, err
);
2333 /* Grow the request memory area to accomodate for a large request */
2334 conn
->in
.size
+= REQUEST_BUF_SIZE
;
2335 conn
->in
.buf
= xrealloc(conn
->in
.buf
, conn
->in
.size
);
2336 /* XXX account conn->in.buf */
2337 debug(33, 2) ("Handling a large request, offset=%d inbufsize=%d\n",
2338 (int) conn
->in
.offset
, conn
->in
.size
);
2339 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2346 /* general lifetime handler for HTTP requests */
2348 requestTimeout(int fd
, void *data
)
2350 ConnStateData
*conn
= data
;
2352 debug(33, 2) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2353 if (fd_table
[fd
].rwstate
) {
2355 * Some data has been sent to the client, just close the FD
2358 } else if (conn
->nrequests
) {
2360 * assume its a persistent connection; just close it
2367 err
= errorCon(ERR_LIFETIME_EXP
, HTTP_REQUEST_TIMEOUT
);
2368 err
->url
= xstrdup("N/A");
2370 * Normally we shouldn't call errorSend() in client_side.c, but
2371 * it should be okay in this case. Presumably if we get here
2372 * this is the first request for the connection, and no data
2373 * has been written yet
2375 assert(conn
->chr
== NULL
);
2378 * if we don't close() here, we still need a timeout handler!
2380 commSetTimeout(fd
, 30, requestTimeout
, conn
);
2385 httpAcceptDefer(void)
2387 static time_t last_warn
= 0;
2388 if (fdNFree() >= RESERVED_FD
)
2390 if (last_warn
+ 15 < squid_curtime
) {
2391 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2392 last_warn
= squid_curtime
;
2397 /* Handle a new connection on HTTP socket. */
2399 httpAccept(int sock
, void *data
)
2403 ConnStateData
*connState
= NULL
;
2404 struct sockaddr_in peer
;
2405 struct sockaddr_in me
;
2406 int max
= INCOMING_HTTP_MAX
;
2407 commSetSelect(sock
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2408 while (max
-- && !httpAcceptDefer()) {
2409 memset(&peer
, '\0', sizeof(struct sockaddr_in
));
2410 memset(&me
, '\0', sizeof(struct sockaddr_in
));
2411 if ((fd
= comm_accept(sock
, &peer
, &me
)) < 0) {
2412 if (!ignoreErrno(errno
))
2413 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2417 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd
);
2418 connState
= xcalloc(1, sizeof(ConnStateData
));
2419 connState
->peer
= peer
;
2420 connState
->log_addr
= peer
.sin_addr
;
2421 connState
->log_addr
.s_addr
&= Config
.Addrs
.client_netmask
.s_addr
;
2424 connState
->in
.size
= REQUEST_BUF_SIZE
;
2425 connState
->in
.buf
= xcalloc(connState
->in
.size
, 1);
2426 cbdataAdd(connState
, cbdataXfree
, 0);
2427 /* XXX account connState->in.buf */
2428 comm_add_close_handler(fd
, connStateFree
, connState
);
2429 if (Config
.onoff
.log_fqdn
)
2430 fqdncache_gethostbyaddr(peer
.sin_addr
, FQDN_LOOKUP_IF_MISS
);
2431 commSetTimeout(fd
, Config
.Timeout
.request
, requestTimeout
, connState
);
2432 if (Config
.onoff
.ident_lookup
)
2433 identStart(&me
, &peer
, clientIdentDone
, connState
);
2434 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, connState
, 0);
2435 commSetDefer(fd
, clientReadDefer
, connState
);
2440 #define SENDING_BODY 0
2441 #define SENDING_HDRSONLY 1
2443 clientCheckTransferDone(clientHttpRequest
* http
)
2445 int sending
= SENDING_BODY
;
2446 StoreEntry
*entry
= http
->entry
;
2453 * For now, 'done_copying' is used for special cases like
2454 * Range and HEAD requests.
2456 if (http
->flags
.done_copying
)
2459 * Handle STORE_OK objects.
2460 * objectLen(entry) will be set proprely.
2462 if (entry
->store_status
== STORE_OK
) {
2463 if (http
->out
.offset
>= objectLen(entry
))
2469 * Now, handle STORE_PENDING objects
2471 mem
= entry
->mem_obj
;
2472 assert(mem
!= NULL
);
2473 assert(http
->request
!= NULL
);
2475 if (reply
->hdr_sz
== 0)
2476 return 0; /* haven't found end of headers yet */
2477 else if (reply
->sline
.status
== HTTP_OK
)
2478 sending
= SENDING_BODY
;
2479 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
2480 sending
= SENDING_HDRSONLY
;
2481 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
2482 sending
= SENDING_HDRSONLY
;
2483 else if (reply
->sline
.status
< HTTP_OK
)
2484 sending
= SENDING_HDRSONLY
;
2485 else if (http
->request
->method
== METHOD_HEAD
)
2486 sending
= SENDING_HDRSONLY
;
2488 sending
= SENDING_BODY
;
2490 * Figure out how much data we are supposed to send.
2491 * If we are sending a body and we don't have a content-length,
2492 * then we must wait for the object to become STORE_OK.
2494 if (sending
== SENDING_HDRSONLY
)
2495 sendlen
= reply
->hdr_sz
;
2496 else if (reply
->content_length
< 0)
2499 sendlen
= reply
->content_length
+ reply
->hdr_sz
;
2501 * Now that we have the expected length, did we send it all?
2503 if (http
->out
.offset
< sendlen
)
2510 clientGotNotEnough(clientHttpRequest
* http
)
2512 int cl
= http
->entry
->mem_obj
->reply
->content_length
;
2513 int hs
= http
->entry
->mem_obj
->reply
->hdr_sz
;
2515 if (http
->out
.offset
< cl
+ hs
)
2521 * This function is designed to serve a fairly specific purpose.
2522 * Occasionally our vBNS-connected caches can talk to each other, but not
2523 * the rest of the world. Here we try to detect frequent failures which
2524 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2525 * the failure:success ratio goes above 1.0 then we go into "hit only"
2526 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2527 * will only fetch HITs from us if they are using the ICP protocol. We
2528 * stay in this mode for 5 minutes.
2530 * Duane W., Sept 16, 1996
2534 checkFailureRatio(err_type etype
, hier_code hcode
)
2536 static double magic_factor
= 100.0;
2539 if (hcode
== HIER_NONE
)
2541 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
2542 n_bad
= magic_factor
- n_good
;
2545 case ERR_CONNECT_FAIL
:
2546 case ERR_READ_ERROR
:
2552 request_failure_ratio
= n_bad
/ n_good
;
2553 if (hit_only_mode_until
> squid_curtime
)
2555 if (request_failure_ratio
< 1.0)
2557 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
2558 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2559 FAILURE_MODE_TIME
/ 60);
2560 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
2561 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
2565 clientHttpConnectionsOpen(void)
2569 for (u
= Config
.Port
.http
; u
; u
= u
->next
) {
2571 fd
= comm_open(SOCK_STREAM
,
2573 Config
.Addrs
.tcp_incoming
,
2581 commSetSelect(fd
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2582 /*commSetDefer(fd, httpAcceptDefer, NULL); */
2583 debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n",
2585 HttpSockets
[NHttpSockets
++] = fd
;
2587 if (NHttpSockets
< 1)
2588 fatal("Cannot open HTTP Port");
2592 clientHttpConnectionsClose(void)
2595 for (i
= 0; i
< NHttpSockets
; i
++) {
2596 if (HttpSockets
[i
] >= 0) {
2597 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets
[i
]);
2598 comm_close(HttpSockets
[i
]);
2599 HttpSockets
[i
] = -1;