3 * $Id: client_side.cc,v 1.450 1999/05/04 19:22:23 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
*);
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 clientPackRangeHdr(const HttpReply
* rep
, const HttpHdrRangeSpec
* spec
, String boundary
, MemBuf
* mb
);
85 static void clientPackTermBound(String boundary
, MemBuf
* mb
);
86 static void clientInterpretRequestHeaders(clientHttpRequest
*);
87 static void clientProcessRequest(clientHttpRequest
*);
88 static void clientProcessExpired(void *data
);
89 static void clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
);
90 static int clientCachable(clientHttpRequest
* http
);
91 static int clientHierarchical(clientHttpRequest
* http
);
92 static int clientCheckContentLength(request_t
* r
);
93 static int httpAcceptDefer(void);
94 static log_type
clientProcessRequest2(clientHttpRequest
* http
);
97 checkAccelOnly(clientHttpRequest
* http
)
99 /* return TRUE if someone makes a proxy request to us and
100 * we are in httpd-accel only mode */
101 if (!Config2
.Accel
.on
)
103 if (Config
.onoff
.accel_with_proxy
)
105 if (http
->request
->protocol
== PROTO_CACHEOBJ
)
107 if (http
->flags
.accel
)
114 clientIdentDone(const char *ident
, void *data
)
116 ConnStateData
*conn
= data
;
118 xstrncpy(conn
->ident
, ident
, sizeof(conn
->ident
));
120 xstrncpy(conn
->ident
, "-", sizeof(conn
->ident
));
125 clientAccessCheck(void *data
)
127 clientHttpRequest
*http
= data
;
128 ConnStateData
*conn
= http
->conn
;
130 if (checkAccelOnly(http
)) {
131 clientAccessCheckDone(0, http
);
134 browser
= httpHeaderGetStr(&http
->request
->header
, HDR_USER_AGENT
);
135 http
->acl_checklist
= aclChecklistCreate(Config
.accessList
.http
,
143 * hack for ident ACL. It needs to get full addresses, and a
144 * place to store the ident result on persistent connections...
146 http
->acl_checklist
->conn
= conn
;
147 cbdataLock(http
->acl_checklist
->conn
);
149 aclNBCheck(http
->acl_checklist
, clientAccessCheckDone
, http
);
153 * returns true if client specified that the object must come from the cache
154 * witout contacting origin server
157 clientOnlyIfCached(clientHttpRequest
* http
)
159 const request_t
*r
= http
->request
;
161 return r
->cache_control
&&
162 EBIT_TEST(r
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
166 clientCreateStoreEntry(clientHttpRequest
* h
, method_t m
, request_flags flags
)
170 * For erroneous requests, we might not have a h->request,
171 * so make a fake one.
173 if (h
->request
== NULL
)
174 h
->request
= requestLink(requestCreate(m
, PROTO_NONE
, NULL
));
175 e
= storeCreateEntry(h
->uri
, h
->log_uri
, flags
, m
);
176 storeClientListAdd(e
, h
);
178 delaySetStoreClient(e
, h
, delayClient(h
->request
));
180 storeClientCopy(e
, 0, 0, CLIENT_SOCK_SZ
,
181 memAllocate(MEM_CLIENT_SOCK_BUF
), clientSendMoreData
, h
);
186 clientAccessCheckDone(int answer
, void *data
)
188 clientHttpRequest
*http
= data
;
191 ErrorState
*err
= NULL
;
192 debug(33, 2) ("The request %s %s is %s, because it matched '%s'\n",
193 RequestMethodStr
[http
->request
->method
], http
->uri
,
194 answer
? "ALLOWED" : "DENIED",
195 AclMatchedName
? AclMatchedName
: "NO ACL's");
196 http
->acl_checklist
= NULL
;
197 if (answer
== ACCESS_ALLOWED
) {
198 safe_free(http
->uri
);
199 http
->uri
= xstrdup(urlCanonical(http
->request
));
200 assert(http
->redirect_state
== REDIRECT_NONE
);
201 http
->redirect_state
= REDIRECT_PENDING
;
202 redirectStart(http
, clientRedirectDone
, http
);
204 debug(33, 5) ("Access Denied: %s\n", http
->uri
);
205 debug(33, 5) ("AclMatchedName = %s\n",
206 AclMatchedName
? AclMatchedName
: "<null>");
208 * NOTE: get page_id here, based on AclMatchedName because
209 * if USE_DELAY_POOLS is enabled, then AclMatchedName gets
210 * clobbered in the clientCreateStoreEntry() call
211 * just below. Pedro Ribeiro <pribeiro@isel.pt>
213 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
214 http
->log_type
= LOG_TCP_DENIED
;
215 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
217 if (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
)) {
218 if (!http
->flags
.accel
) {
219 /* Proxy authorisation needed */
220 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
222 /* WWW authorisation needed */
223 status
= HTTP_UNAUTHORIZED
;
226 page_id
= ERR_CACHE_ACCESS_DENIED
;
228 status
= HTTP_FORBIDDEN
;
230 page_id
= ERR_ACCESS_DENIED
;
232 err
= errorCon(page_id
, status
);
233 err
->request
= requestLink(http
->request
);
234 err
->src_addr
= http
->conn
->peer
.sin_addr
;
235 errorAppendEntry(http
->entry
, err
);
240 clientRedirectDone(void *data
, char *result
)
242 clientHttpRequest
*http
= data
;
243 request_t
*new_request
= NULL
;
244 request_t
*old_request
= http
->request
;
245 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
246 result
? result
: "NULL");
247 assert(http
->redirect_state
== REDIRECT_PENDING
);
248 http
->redirect_state
= REDIRECT_DONE
;
250 http_status status
= atoi(result
);
251 if (status
== 301 || status
== 302) {
253 if ((t
= strchr(result
, ':')) != NULL
) {
254 http
->redirect
.status
= status
;
255 http
->redirect
.location
= xstrdup(t
+ 1);
257 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result
);
260 if (strcmp(result
, http
->uri
))
261 new_request
= urlParse(old_request
->method
, result
);
264 safe_free(http
->uri
);
265 http
->uri
= xstrdup(urlCanonical(new_request
));
266 new_request
->http_ver
= old_request
->http_ver
;
267 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
268 new_request
->client_addr
= old_request
->client_addr
;
269 new_request
->my_addr
= old_request
->my_addr
;
270 new_request
->flags
.redirected
= 1;
271 if (old_request
->body
) {
272 new_request
->body
= xmalloc(old_request
->body_sz
);
273 xmemcpy(new_request
->body
, old_request
->body
, old_request
->body_sz
);
274 new_request
->body_sz
= old_request
->body_sz
;
276 requestUnlink(old_request
);
277 http
->request
= requestLink(new_request
);
279 clientInterpretRequestHeaders(http
);
280 fd_note(http
->conn
->fd
, http
->uri
);
281 clientProcessRequest(http
);
285 clientProcessExpired(void *data
)
287 clientHttpRequest
*http
= data
;
288 char *url
= http
->uri
;
289 StoreEntry
*entry
= NULL
;
290 debug(33, 3) ("clientProcessExpired: '%s'\n", http
->uri
);
291 assert(http
->entry
->lastmod
>= 0);
293 * check if we are allowed to contact other servers
294 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
295 * a stale entry *if* it matches client requirements
297 if (clientOnlyIfCached(http
)) {
298 clientProcessOnlyIfCachedMiss(http
);
301 http
->request
->flags
.refresh
= 1;
302 http
->old_entry
= http
->entry
;
304 * Assert that 'http' is already a client of old_entry. If
305 * it is not, then the beginning of the object data might get
306 * freed from memory before we need to access it.
308 assert(storeClientListSearch(http
->old_entry
->mem_obj
, http
));
309 entry
= storeCreateEntry(url
,
311 http
->request
->flags
,
312 http
->request
->method
);
313 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
314 storeClientListAdd(entry
, http
);
316 /* delay_id is already set on original store client */
317 delaySetStoreClient(entry
, http
, delayClient(http
->request
));
319 entry
->lastmod
= http
->old_entry
->lastmod
;
320 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry
->lastmod
);
321 entry
->refcount
++; /* EXPIRED CASE */
323 http
->out
.offset
= 0;
324 fwdStart(http
->conn
->fd
, http
->entry
, http
->request
,
325 http
->conn
->peer
.sin_addr
, http
->conn
->me
.sin_addr
);
326 /* Register with storage manager to receive updates when data comes in. */
327 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
328 debug(33, 0) ("clientProcessExpired: found ENTRY_ABORTED object\n");
329 storeClientCopy(entry
,
333 memAllocate(MEM_CLIENT_SOCK_BUF
),
334 clientHandleIMSReply
,
339 clientGetsOldEntry(StoreEntry
* new_entry
, StoreEntry
* old_entry
, request_t
* request
)
341 const http_status status
= new_entry
->mem_obj
->reply
->sline
.status
;
343 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
346 /* If the reply is a failure then send the old object as a last
348 if (status
>= 500 && status
< 600) {
349 debug(33, 3) ("clientGetsOldEntry: YES, failure reply=%d\n", status
);
352 /* If the reply is anything but "Not Modified" then
353 * we must forward it to the client */
354 if (HTTP_NOT_MODIFIED
!= status
) {
355 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status
);
358 /* If the client did not send IMS in the request, then it
359 * must get the old object, not this "Not Modified" reply */
360 if (!request
->flags
.ims
) {
361 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
364 /* If the client IMS time is prior to the entry LASTMOD time we
365 * need to send the old object */
366 if (modifiedSince(old_entry
, request
)) {
367 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
371 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
377 clientHandleIMSReply(void *data
, char *buf
, ssize_t size
)
379 clientHttpRequest
*http
= data
;
380 StoreEntry
*entry
= http
->entry
;
382 const char *url
= storeUrl(entry
);
383 int unlink_request
= 0;
384 StoreEntry
*oldentry
;
387 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url
, (int) size
);
389 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
392 if (size
< 0 && !EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
393 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
396 mem
= entry
->mem_obj
;
397 status
= mem
->reply
->sline
.status
;
398 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
399 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url
);
400 /* We have an existing entry, but failed to validate it */
401 /* Its okay to send the old one anyway */
402 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
403 storeUnregister(entry
, http
);
404 storeUnlockObject(entry
);
405 entry
= http
->entry
= http
->old_entry
;
407 } else if (STORE_PENDING
== entry
->store_status
&& 0 == status
) {
408 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url
);
409 if (size
>= CLIENT_SOCK_SZ
) {
410 /* will not get any bigger than that */
411 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url
);
412 /* use old entry, this repeats the code abovez */
413 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
414 storeUnregister(entry
, http
);
415 storeUnlockObject(entry
);
416 entry
= http
->entry
= http
->old_entry
;
420 storeClientCopy(entry
,
421 http
->out
.offset
+ size
,
425 clientHandleIMSReply
,
429 } else if (clientGetsOldEntry(entry
, http
->old_entry
, http
->request
)) {
430 /* We initiated the IMS request, the client is not expecting
431 * 304, so put the good one back. First, make sure the old entry
432 * headers have been loaded from disk. */
433 oldentry
= http
->old_entry
;
434 http
->log_type
= LOG_TCP_REFRESH_HIT
;
435 if (oldentry
->mem_obj
->request
== NULL
) {
436 oldentry
->mem_obj
->request
= requestLink(mem
->request
);
439 /* Don't memcpy() the whole reply structure here. For example,
440 * www.thegist.com (Netscape/1.13) returns a content-length for
441 * 304's which seems to be the length of the 304 HEADERS!!! and
442 * not the body they refer to. */
443 httpReplyUpdateOnNotModified(oldentry
->mem_obj
->reply
, mem
->reply
);
444 storeTimestampsSet(oldentry
);
445 storeUnregister(entry
, http
);
446 storeUnlockObject(entry
);
447 entry
= http
->entry
= oldentry
;
448 entry
->timestamp
= squid_curtime
;
449 if (unlink_request
) {
450 requestUnlink(entry
->mem_obj
->request
);
451 entry
->mem_obj
->request
= NULL
;
454 /* the client can handle this reply, whatever it is */
455 http
->log_type
= LOG_TCP_REFRESH_MISS
;
456 if (HTTP_NOT_MODIFIED
== mem
->reply
->sline
.status
) {
457 httpReplyUpdateOnNotModified(http
->old_entry
->mem_obj
->reply
,
459 storeTimestampsSet(http
->old_entry
);
460 http
->old_entry
->refcount
++;
461 http
->log_type
= LOG_TCP_REFRESH_HIT
;
463 storeUnregister(http
->old_entry
, http
);
464 storeUnlockObject(http
->old_entry
);
467 http
->old_entry
= NULL
; /* done with old_entry */
468 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
470 storeClientCopy(entry
,
478 clientSendMoreData(data
, buf
, size
);
483 modifiedSince(StoreEntry
* entry
, request_t
* request
)
486 MemObject
*mem
= entry
->mem_obj
;
487 time_t mod_time
= entry
->lastmod
;
488 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry
));
490 mod_time
= entry
->timestamp
;
491 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time
);
494 /* Find size of the object */
495 object_length
= mem
->reply
->content_length
;
496 if (object_length
< 0)
497 object_length
= contentLen(entry
);
498 if (mod_time
> request
->ims
) {
499 debug(33, 3) ("--> YES: entry newer than client\n");
501 } else if (mod_time
< request
->ims
) {
502 debug(33, 3) ("--> NO: entry older than client\n");
504 } else if (request
->imslen
< 0) {
505 debug(33, 3) ("--> NO: same LMT, no client length\n");
507 } else if (request
->imslen
== object_length
) {
508 debug(33, 3) ("--> NO: same LMT, same length\n");
511 debug(33, 3) ("--> YES: same LMT, different length\n");
517 clientPurgeRequest(clientHttpRequest
* http
)
520 ErrorState
*err
= NULL
;
522 debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config
.onoff
.enable_purge
);
523 if (!Config
.onoff
.enable_purge
) {
524 http
->log_type
= LOG_TCP_DENIED
;
525 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
526 err
->request
= requestLink(http
->request
);
527 err
->src_addr
= http
->conn
->peer
.sin_addr
;
528 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
529 errorAppendEntry(http
->entry
, err
);
532 http
->log_type
= LOG_TCP_MISS
;
533 if ((entry
= storeGetPublic(http
->uri
, METHOD_GET
)) == NULL
) {
534 http
->http_code
= HTTP_NOT_FOUND
;
537 http
->http_code
= HTTP_OK
;
539 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
542 * Make a new entry to hold the reply to be written
545 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
546 httpReplyReset(r
= http
->entry
->mem_obj
->reply
);
547 httpReplySetHeaders(r
, 1.0, http
->http_code
, NULL
, NULL
, 0, 0, -1);
548 httpReplySwapOut(r
, http
->entry
);
549 storeComplete(http
->entry
);
553 checkNegativeHit(StoreEntry
* e
)
555 if (!EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
557 if (e
->expires
<= squid_curtime
)
559 if (e
->store_status
!= STORE_OK
)
565 clientUpdateCounters(clientHttpRequest
* http
)
567 int svc_time
= tvSubMsec(http
->start
, current_time
);
569 HierarchyLogEntry
*H
;
570 Counter
.client_http
.requests
++;
571 if (isTcpHit(http
->log_type
))
572 Counter
.client_http
.hits
++;
573 if (http
->request
->err_type
!= ERR_NONE
)
574 Counter
.client_http
.errors
++;
575 statHistCount(&Counter
.client_http
.all_svc_time
, svc_time
);
577 * The idea here is not to be complete, but to get service times
578 * for only well-defined types. For example, we don't include
579 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
580 * (we *tried* to validate it, but failed).
582 switch (http
->log_type
) {
583 case LOG_TCP_REFRESH_HIT
:
584 statHistCount(&Counter
.client_http
.nh_svc_time
, svc_time
);
586 case LOG_TCP_IMS_HIT
:
587 statHistCount(&Counter
.client_http
.nm_svc_time
, svc_time
);
590 case LOG_TCP_MEM_HIT
:
591 case LOG_TCP_OFFLINE_HIT
:
592 statHistCount(&Counter
.client_http
.hit_svc_time
, svc_time
);
595 case LOG_TCP_CLIENT_REFRESH_MISS
:
596 statHistCount(&Counter
.client_http
.miss_svc_time
, svc_time
);
599 /* make compiler warnings go away */
602 H
= &http
->request
->hier
;
605 Counter
.cd
.times_used
++;
608 Counter
.icp
.times_used
++;
610 if (0 != i
->stop
.tv_sec
&& 0 != i
->start
.tv_sec
)
611 statHistCount(&Counter
.icp
.query_svc_time
,
612 tvSubUsec(i
->start
, i
->stop
));
614 Counter
.icp
.query_timeouts
++;
617 Counter
.netdb
.times_used
++;
625 httpRequestFree(void *data
)
627 clientHttpRequest
*http
= data
;
628 clientHttpRequest
**H
;
629 ConnStateData
*conn
= http
->conn
;
631 request_t
*request
= http
->request
;
632 MemObject
*mem
= NULL
;
633 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(http
->entry
));
634 if (!clientCheckTransferDone(http
)) {
635 if ((e
= http
->entry
)) {
637 storeUnregister(e
, http
);
638 storeUnlockObject(e
);
640 if (http
->entry
&& http
->entry
->ping_status
== PING_WAITING
)
641 storeReleaseRequest(http
->entry
);
643 assert(http
->log_type
< LOG_TYPE_MAX
);
645 mem
= http
->entry
->mem_obj
;
646 if (http
->out
.size
|| http
->log_type
) {
647 http
->al
.icp
.opcode
= ICP_INVALID
;
648 http
->al
.url
= http
->log_uri
;
649 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http
->al
.url
);
651 http
->al
.http
.code
= mem
->reply
->sline
.status
;
652 http
->al
.http
.content_type
= strBuf(mem
->reply
->content_type
);
654 http
->al
.cache
.caddr
= conn
->log_addr
;
655 http
->al
.cache
.size
= http
->out
.size
;
656 http
->al
.cache
.code
= http
->log_type
;
657 http
->al
.cache
.msec
= tvSubMsec(http
->start
, current_time
);
658 if (request
->user_ident
[0])
659 http
->al
.cache
.ident
= request
->user_ident
;
661 http
->al
.cache
.ident
= conn
->ident
;
666 packerToMemInit(&p
, &mb
);
667 httpHeaderPackInto(&request
->header
, &p
);
668 http
->al
.http
.method
= request
->method
;
669 http
->al
.http
.version
= request
->http_ver
;
670 http
->al
.headers
.request
= xstrdup(mb
.buf
);
671 http
->al
.hier
= request
->hier
;
675 accessLogLog(&http
->al
);
676 clientUpdateCounters(http
);
677 clientdbUpdate(conn
->peer
.sin_addr
, http
->log_type
, PROTO_HTTP
, http
->out
.size
);
679 if (http
->acl_checklist
)
680 aclChecklistFree(http
->acl_checklist
);
682 checkFailureRatio(request
->err_type
, http
->al
.hier
.code
);
683 safe_free(http
->uri
);
684 safe_free(http
->log_uri
);
685 safe_free(http
->al
.headers
.request
);
686 safe_free(http
->al
.headers
.reply
);
687 safe_free(http
->redirect
.location
);
688 stringClean(&http
->range_iter
.boundary
);
689 if ((e
= http
->entry
)) {
691 storeUnregister(e
, http
);
692 storeUnlockObject(e
);
694 /* old_entry might still be set if we didn't yet get the reply
695 * code in clientHandleIMSReply() */
696 if ((e
= http
->old_entry
)) {
697 http
->old_entry
= NULL
;
698 storeUnregister(e
, http
);
699 storeUnlockObject(e
);
701 requestUnlink(http
->request
);
702 assert(http
!= http
->next
);
703 assert(http
->conn
->chr
!= NULL
);
704 H
= &http
->conn
->chr
;
713 dlinkDelete(&http
->active
, &ClientActiveRequests
);
717 /* This is a handler normally called by comm_close() */
719 connStateFree(int fd
, void *data
)
721 ConnStateData
*connState
= data
;
722 clientHttpRequest
*http
;
723 debug(33, 3) ("connStateFree: FD %d\n", fd
);
724 assert(connState
!= NULL
);
725 while ((http
= connState
->chr
) != NULL
) {
726 assert(http
->conn
== connState
);
727 assert(connState
->chr
!= connState
->chr
->next
);
728 httpRequestFree(http
);
730 safe_free(connState
->in
.buf
);
731 /* XXX account connState->in.buf */
732 pconnHistCount(0, connState
->nrequests
);
733 cbdataFree(connState
);
735 /* prevent those nasty RST packets */
737 char buf
[SQUID_TCP_SO_RCVBUF
];
738 while (read(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0);
744 clientInterpretRequestHeaders(clientHttpRequest
* http
)
746 request_t
*request
= http
->request
;
747 const HttpHeader
*req_hdr
= &request
->header
;
749 #if USE_USERAGENT_LOG
752 request
->imslen
= -1;
753 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
754 if (request
->ims
> 0)
755 request
->flags
.ims
= 1;
756 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
757 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
758 if (strListIsMember(&s
, "no-cache", ','))
762 if (request
->cache_control
)
763 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
767 if (Config
.onoff
.reload_into_ims
)
768 request
->flags
.nocache_hack
= 1;
769 else if (refresh_nocache_hack
)
770 request
->flags
.nocache_hack
= 1;
773 request
->flags
.nocache
= 1;
775 /* ignore range header in non-GETs */
776 if (request
->method
== METHOD_GET
) {
777 request
->range
= httpHeaderGetRange(req_hdr
);
779 request
->flags
.range
= 1;
781 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
782 request
->flags
.auth
= 1;
783 if (request
->login
[0] != '\0')
784 request
->flags
.auth
= 1;
785 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
786 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
788 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
789 * Note ThisCache2 has a space prepended to the hostname so we don't
790 * accidentally match super-domains.
792 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
793 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
794 request
, (ObjPackMethod
) & httpRequestPack
);
795 request
->flags
.loopdetect
= 1;
798 fvdbCountVia(strBuf(s
));
802 #if USE_USERAGENT_LOG
803 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
804 logUserAgent(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
);
807 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
808 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
809 fvdbCountForw(strBuf(s
));
813 request
->cache_control
= httpHeaderGetCc(req_hdr
);
814 if (request
->method
== METHOD_TRACE
) {
815 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
817 if (clientCachable(http
))
818 request
->flags
.cachable
= 1;
819 if (clientHierarchical(http
))
820 request
->flags
.hierarchical
= 1;
821 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
822 request
->flags
.nocache
? "SET" : "NOT SET");
823 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
824 request
->flags
.cachable
? "SET" : "NOT SET");
825 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
826 request
->flags
.hierarchical
? "SET" : "NOT SET");
830 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
831 * This is the client-side persistent connection flag. We need
832 * to set this relatively early in the request processing
833 * to handle hacks for broken servers and clients.
836 clientSetKeepaliveFlag(clientHttpRequest
* http
)
838 request_t
*request
= http
->request
;
839 const HttpHeader
*req_hdr
= &request
->header
;
840 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
842 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
843 RequestMethodStr
[request
->method
]);
844 if (httpMsgIsPersistent(request
->http_ver
, req_hdr
))
845 request
->flags
.proxy_keepalive
= 1;
849 clientCheckContentLength(request_t
* r
)
851 /* We only require a content-length for "upload" methods */
852 if (!pumpMethod(r
->method
))
854 if (httpHeaderGetInt(&r
->header
, HDR_CONTENT_LENGTH
) < 0)
860 clientCachable(clientHttpRequest
* http
)
862 const char *url
= http
->uri
;
863 request_t
*req
= http
->request
;
864 method_t method
= req
->method
;
866 memset(&ch
, '\0', sizeof(ch
));
868 * Hopefully, nobody really wants 'no_cache' by client's IP
869 * address, but if they do, this should work if they use IP
870 * addresses in their ACLs, or if the client's address is in
873 * This may not work yet for 'dst' and 'dst_domain' ACLs.
875 ch
.src_addr
= http
->conn
->peer
.sin_addr
;
876 ch
.my_addr
= http
->conn
->me
.sin_addr
;
877 ch
.request
= http
->request
;
879 * aclCheckFast returns 1 for ALLOW and 0 for DENY. The default
880 * is ALLOW, so we require 'no_cache DENY foo' in squid.conf
881 * to indicate uncachable objects.
883 if (!aclCheckFast(Config
.accessList
.noCache
, &ch
))
885 if (req
->protocol
== PROTO_HTTP
)
886 return httpCachable(method
);
887 /* FTP is always cachable */
888 if (req
->protocol
== PROTO_GOPHER
)
889 return gopherCachable(url
);
890 if (req
->protocol
== PROTO_WAIS
)
892 if (method
== METHOD_CONNECT
)
894 if (method
== METHOD_TRACE
)
896 if (req
->protocol
== PROTO_CACHEOBJ
)
901 /* Return true if we can query our neighbors for this object */
903 clientHierarchical(clientHttpRequest
* http
)
905 const char *url
= http
->uri
;
906 request_t
*request
= http
->request
;
907 method_t method
= request
->method
;
908 const wordlist
*p
= NULL
;
910 /* IMS needs a private key, so we can use the hierarchy for IMS only
911 * if our neighbors support private keys */
912 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
914 if (request
->flags
.auth
)
916 if (method
== METHOD_TRACE
)
918 if (method
!= METHOD_GET
)
920 /* scan hierarchy_stoplist */
921 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
922 if (strstr(url
, p
->key
))
924 if (request
->flags
.loopdetect
)
926 if (request
->protocol
== PROTO_HTTP
)
927 return httpCachable(method
);
928 if (request
->protocol
== PROTO_GOPHER
)
929 return gopherCachable(url
);
930 if (request
->protocol
== PROTO_WAIS
)
932 if (request
->protocol
== PROTO_CACHEOBJ
)
938 isTcpHit(log_type code
)
940 /* this should be a bitmap for better optimization */
941 if (code
== LOG_TCP_HIT
)
943 if (code
== LOG_TCP_IMS_HIT
)
945 if (code
== LOG_TCP_REFRESH_FAIL_HIT
)
947 if (code
== LOG_TCP_REFRESH_HIT
)
949 if (code
== LOG_TCP_NEGATIVE_HIT
)
951 if (code
== LOG_TCP_MEM_HIT
)
953 if (code
== LOG_TCP_OFFLINE_HIT
)
959 * returns true if If-Range specs match reply, false otherwise
962 clientIfRangeMatch(clientHttpRequest
* http
, HttpReply
* rep
)
964 const TimeOrTag spec
= httpHeaderGetTimeOrTag(&http
->request
->header
, HDR_IF_RANGE
);
965 /* check for parsing falure */
970 ETag rep_tag
= httpHeaderGetETag(&rep
->header
, HDR_ETAG
);
971 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
972 spec
.tag
.str
, rep_tag
.str
? rep_tag
.str
: "<none>");
974 return 0; /* entity has no etag to compare with! */
975 if (spec
.tag
.weak
|| rep_tag
.weak
) {
976 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
977 spec
.tag
.str
, rep_tag
.str
);
978 return 0; /* must use strong validator for sub-range requests */
980 return etagIsEqual(&rep_tag
, &spec
.tag
);
982 /* got modification time? */
983 if (spec
.time
>= 0) {
984 return http
->entry
->lastmod
<= spec
.time
;
986 assert(0); /* should not happen */
990 /* returns expected content length for multi-range replies
991 * note: assumes that httpHdrRangeCanonize has already been called
992 * warning: assumes that HTTP headers for individual ranges at the
993 * time of the actuall assembly will be exactly the same as
994 * the headers when clientMRangeCLen() is called */
996 clientMRangeCLen(clientHttpRequest
* http
)
999 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1000 const HttpHdrRangeSpec
*spec
;
1003 assert(http
->entry
->mem_obj
);
1006 while ((spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
))) {
1008 /* account for headers for this range */
1010 clientPackRangeHdr(http
->entry
->mem_obj
->reply
,
1011 spec
, http
->range_iter
.boundary
, &mb
);
1014 /* account for range content */
1015 clen
+= spec
->length
;
1017 debug(33, 6) ("clientMRangeCLen: (clen += %d + %d) == %d\n",
1018 mb
.size
, spec
->length
, clen
);
1020 /* account for the terminating boundary */
1022 clientPackTermBound(http
->range_iter
.boundary
, &mb
);
1029 /* adds appropriate Range headers if needed */
1031 clientBuildRangeHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1033 HttpHeader
*hdr
= rep
? &rep
->header
: 0;
1034 const char *range_err
= NULL
;
1035 assert(http
->request
->range
);
1036 /* check if we still want to do ranges */
1038 range_err
= "no [parse-able] reply";
1039 else if (rep
->sline
.status
!= HTTP_OK
)
1040 range_err
= "wrong status code";
1041 else if (httpHeaderHas(hdr
, HDR_CONTENT_RANGE
))
1042 range_err
= "origin server does ranges";
1043 else if (rep
->content_length
< 0)
1044 range_err
= "unknown length";
1045 else if (rep
->content_length
!= http
->entry
->mem_obj
->reply
->content_length
)
1046 range_err
= "INCONSISTENT length"; /* a bug? */
1047 else if (httpHeaderHas(&http
->request
->header
, HDR_IF_RANGE
) && !clientIfRangeMatch(http
, rep
))
1048 range_err
= "If-Range match failed";
1049 else if (!httpHdrRangeCanonize(http
->request
->range
, rep
->content_length
))
1050 range_err
= "canonization failed";
1051 else if (httpHdrRangeIsComplex(http
->request
->range
))
1052 range_err
= "too complex range header";
1053 /* get rid of our range specs on error */
1055 debug(33, 3) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err
);
1056 httpHdrRangeDestroy(http
->request
->range
);
1057 http
->request
->range
= NULL
;
1059 const int spec_count
= http
->request
->range
->specs
.count
;
1060 int actual_clen
= -1;
1062 debug(33, 3) ("clientBuildRangeHeader: range spec count: %d virgin clen: %d\n",
1063 spec_count
, rep
->content_length
);
1064 assert(spec_count
> 0);
1065 /* ETags should not be returned with Partial Content replies? */
1066 httpHeaderDelById(hdr
, HDR_ETAG
);
1067 /* append appropriate header(s) */
1068 if (spec_count
== 1) {
1069 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1070 const HttpHdrRangeSpec
*spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
);
1072 /* append Content-Range */
1073 httpHeaderAddContRange(hdr
, *spec
, rep
->content_length
);
1074 /* set new Content-Length to the actual number of bytes
1075 * transmitted in the message-body */
1076 actual_clen
= spec
->length
;
1079 /* generate boundary string */
1080 http
->range_iter
.boundary
= httpHdrRangeBoundaryStr(http
);
1081 /* delete old Content-Type, add ours */
1082 httpHeaderDelById(hdr
, HDR_CONTENT_TYPE
);
1083 httpHeaderPutStrf(hdr
, HDR_CONTENT_TYPE
,
1084 "multipart/byteranges; boundary=\"%s\"",
1085 strBuf(http
->range_iter
.boundary
));
1086 /* Content-Length is not required in multipart responses
1087 * but it is always nice to have one */
1088 actual_clen
= clientMRangeCLen(http
);
1091 /* replace Content-Length header */
1092 assert(actual_clen
>= 0);
1093 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1094 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, actual_clen
);
1095 debug(33, 3) ("clientBuildRangeHeader: actual content length: %d\n", actual_clen
);
1100 * filters out unwanted entries from original reply header
1101 * adds extra entries if we have more info than origin server
1102 * adds Squid specific entries
1105 clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1107 HttpHeader
*hdr
= &rep
->header
;
1108 int is_hit
= isTcpHit(http
->log_type
);
1109 request_t
*request
= http
->request
;
1110 #if DONT_FILTER_THESE
1111 /* but you might want to if you run Squid as an HTTP accelerator */
1112 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1113 httpHeaderDelById(hdr
, HDR_ETAG
);
1115 httpHeaderDelById(hdr
, HDR_PROXY_CONNECTION
);
1116 /* here: Keep-Alive is a field-name, not a connection directive! */
1117 httpHeaderDelByName(hdr
, "Keep-Alive");
1118 /* remove Set-Cookie if a hit */
1120 httpHeaderDelById(hdr
, HDR_SET_COOKIE
);
1121 /* handle Connection header */
1122 if (httpHeaderHas(hdr
, HDR_CONNECTION
)) {
1123 /* anything that matches Connection list member will be deleted */
1124 String strConnection
= httpHeaderGetList(hdr
, HDR_CONNECTION
);
1125 const HttpHeaderEntry
*e
;
1126 HttpHeaderPos pos
= HttpHeaderInitPos
;
1128 * think: on-average-best nesting of the two loops (hdrEntry
1129 * and strListItem) @?@
1132 * maybe we should delete standard stuff ("keep-alive","close")
1133 * from strConnection first?
1135 while ((e
= httpHeaderGetEntry(hdr
, &pos
))) {
1136 if (strListIsMember(&strConnection
, strBuf(e
->name
), ','))
1137 httpHeaderDelAt(hdr
, pos
);
1139 httpHeaderDelById(hdr
, HDR_CONNECTION
);
1140 stringClean(&strConnection
);
1144 clientBuildRangeHeader(http
, rep
);
1146 * Add Age header, not that our header must replace Age headers
1147 * from other caches if any
1149 if (http
->entry
->timestamp
> 0) {
1150 httpHeaderDelById(hdr
, HDR_AGE
);
1152 * we do not follow HTTP/1.1 precisely here becuase we rely
1153 * on Date header when computing entry->timestamp; we should
1154 * be using _request_ time if Date header is not available
1155 * or if it is out of sync
1157 httpHeaderPutInt(hdr
, HDR_AGE
,
1158 http
->entry
->timestamp
<= squid_curtime
?
1159 squid_curtime
- http
->entry
->timestamp
: 0);
1161 /* Append X-Cache */
1162 httpHeaderPutStrf(hdr
, HDR_X_CACHE
, "%s from %s",
1163 is_hit
? "HIT" : "MISS", getMyHostname());
1164 #if USE_CACHE_DIGESTS
1165 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1166 httpHeaderPutStrf(hdr
, HDR_X_CACHE_LOOKUP
, "%s from %s:%d",
1167 http
->lookup_type
? http
->lookup_type
: "NONE",
1168 getMyHostname(), Config
.Port
.http
->i
);
1170 if (httpReplyBodySize(request
->method
, http
->entry
->mem_obj
->reply
) < 0) {
1171 debug(33, 3) ("clientBuildReplyHeader: can't keep-alive, unknown body size\n");
1172 request
->flags
.proxy_keepalive
= 0;
1174 /* Signal keep-alive if needed */
1175 httpHeaderPutStr(hdr
,
1176 http
->flags
.accel
? HDR_CONNECTION
: HDR_PROXY_CONNECTION
,
1177 request
->flags
.proxy_keepalive
? "keep-alive" : "close");
1178 #if ADD_X_REQUEST_URI
1180 * Knowing the URI of the request is useful when debugging persistent
1181 * connections in a client; we cannot guarantee the order of http headers,
1182 * but X-Request-URI is likely to be the very last header to ease use from a
1183 * debugger [hdr->entries.count-1].
1185 httpHeaderPutStr(hdr
, HDR_X_REQUEST_URI
,
1186 http
->entry
->mem_obj
->url
? http
->entry
->mem_obj
->url
: http
->uri
);
1191 clientBuildReply(clientHttpRequest
* http
, const char *buf
, size_t size
)
1193 HttpReply
*rep
= httpReplyCreate();
1194 if (httpReplyParse(rep
, buf
)) {
1195 /* enforce 1.0 reply version */
1196 rep
->sline
.version
= 1.0;
1197 /* do header conversions */
1198 clientBuildReplyHeader(http
, rep
);
1199 /* if we do ranges, change status to "Partial Content" */
1200 if (http
->request
->range
)
1201 httpStatusLineSet(&rep
->sline
, rep
->sline
.version
, HTTP_PARTIAL_CONTENT
, NULL
);
1203 /* parsing failure, get rid of the invalid reply */
1204 httpReplyDestroy(rep
);
1206 /* if we were going to do ranges, backoff */
1207 if (http
->request
->range
)
1208 clientBuildRangeHeader(http
, rep
); /* will fail and destroy request->range */
1214 * clientCacheHit should only be called until the HTTP reply headers
1215 * have been parsed. Normally this should be a single call, but
1216 * it might take more than one. As soon as we have the headers,
1217 * we hand off to clientSendMoreData, clientProcessExpired, or
1218 * clientProcessMiss.
1221 clientCacheHit(void *data
, char *buf
, ssize_t size
)
1223 clientHttpRequest
*http
= data
;
1224 StoreEntry
*e
= http
->entry
;
1226 request_t
*r
= http
->request
;
1227 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http
->uri
, (int) size
);
1228 if (http
->entry
== NULL
) {
1229 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1230 debug(33, 3) ("clientCacheHit: request aborted\n");
1232 } else if (size
< 0) {
1233 /* swap in failure */
1234 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1235 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http
->uri
);
1236 http
->log_type
= LOG_TCP_SWAPFAIL_MISS
;
1237 if ((e
= http
->entry
)) {
1239 storeUnregister(e
, http
);
1240 storeUnlockObject(e
);
1242 clientProcessMiss(http
);
1247 assert(!EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
1248 if (mem
->reply
->sline
.status
== 0) {
1250 * we don't have full reply headers yet; either wait for more or
1251 * punt to clientProcessMiss.
1253 if (e
->mem_status
== IN_MEMORY
|| e
->store_status
== STORE_OK
) {
1254 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1255 clientProcessMiss(http
);
1256 } else if (size
== CLIENT_SOCK_SZ
&& http
->out
.offset
== 0) {
1257 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1258 clientProcessMiss(http
);
1260 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1262 http
->out
.offset
+ size
,
1272 * Got the headers, now grok them
1274 assert(http
->log_type
== LOG_TCP_HIT
);
1275 if (checkNegativeHit(e
)) {
1276 http
->log_type
= LOG_TCP_NEGATIVE_HIT
;
1277 clientSendMoreData(data
, buf
, size
);
1278 } else if (r
->method
== METHOD_HEAD
) {
1280 * RFC 2068 seems to indicate there is no "conditional HEAD"
1281 * request. We cannot validate a cached object for a HEAD
1282 * request, nor can we return 304.
1284 if (e
->mem_status
== IN_MEMORY
)
1285 http
->log_type
= LOG_TCP_MEM_HIT
;
1286 clientSendMoreData(data
, buf
, size
);
1287 } else if (refreshCheckHTTP(e
, r
) && !http
->flags
.internal
) {
1288 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1290 * We hold a stale copy; it needs to be validated
1293 * The 'need_validation' flag is used to prevent forwarding
1294 * loops between siblings. If our copy of the object is stale,
1295 * then we should probably only use parents for the validation
1296 * request. Otherwise two siblings could generate a loop if
1297 * both have a stale version of the object.
1299 r
->flags
.need_validation
= 1;
1300 if (e
->lastmod
< 0) {
1302 * Previous reply didn't have a Last-Modified header,
1303 * we cannot revalidate it.
1305 http
->log_type
= LOG_TCP_MISS
;
1306 clientProcessMiss(http
);
1307 } else if (r
->flags
.nocache
) {
1309 * This did not match a refresh pattern that overrides no-cache
1310 * we should honour the client no-cache header.
1312 http
->log_type
= LOG_TCP_CLIENT_REFRESH_MISS
;
1313 clientProcessMiss(http
);
1314 } else if (r
->protocol
== PROTO_HTTP
) {
1316 * Object needs to be revalidated
1317 * XXX This could apply to FTP as well, if Last-Modified is known.
1319 http
->log_type
= LOG_TCP_REFRESH_MISS
;
1320 clientProcessExpired(http
);
1323 * We don't know how to re-validate other protocols. Handle
1324 * them as if the object has expired.
1326 http
->log_type
= LOG_TCP_MISS
;
1327 clientProcessMiss(http
);
1329 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1330 } else if (r
->flags
.ims
) {
1332 * Handle If-Modified-Since requests from the client
1334 if (mem
->reply
->sline
.status
!= HTTP_OK
) {
1335 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1336 mem
->reply
->sline
.status
);
1337 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1338 clientProcessMiss(http
);
1339 } else if (modifiedSince(e
, http
->request
)) {
1340 http
->log_type
= LOG_TCP_IMS_HIT
;
1341 clientSendMoreData(data
, buf
, size
);
1343 MemBuf mb
= httpPacked304Reply(e
->mem_obj
->reply
);
1344 http
->log_type
= LOG_TCP_IMS_HIT
;
1345 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1346 storeUnregister(e
, http
);
1347 storeUnlockObject(e
);
1348 e
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
1350 httpReplyParse(e
->mem_obj
->reply
, mb
.buf
);
1351 storeAppend(e
, mb
.buf
, mb
.size
);
1357 * plain ol' cache hit
1359 if (e
->mem_status
== IN_MEMORY
)
1360 http
->log_type
= LOG_TCP_MEM_HIT
;
1361 else if (Config
.onoff
.offline
)
1362 http
->log_type
= LOG_TCP_OFFLINE_HIT
;
1363 clientSendMoreData(data
, buf
, size
);
1367 /* put terminating boundary for multiparts */
1369 clientPackTermBound(String boundary
, MemBuf
* mb
)
1371 memBufPrintf(mb
, "\r\n--%s--\r\n", strBuf(boundary
));
1372 debug(33, 6) ("clientPackTermBound: buf offset: %d\n", mb
->size
);
1375 /* appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */
1377 clientPackRangeHdr(const HttpReply
* rep
, const HttpHdrRangeSpec
* spec
, String boundary
, MemBuf
* mb
)
1385 debug(33, 5) ("clientPackRangeHdr: appending boundary: %s\n", strBuf(boundary
));
1386 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1387 memBufPrintf(mb
, "\r\n--%s\r\n", strBuf(boundary
));
1389 /* stuff the header with required entries and pack it */
1390 httpHeaderInit(&hdr
, hoReply
);
1391 if (httpHeaderHas(&rep
->header
, HDR_CONTENT_TYPE
))
1392 httpHeaderPutStr(&hdr
, HDR_CONTENT_TYPE
, httpHeaderGetStr(&rep
->header
, HDR_CONTENT_TYPE
));
1393 httpHeaderAddContRange(&hdr
, *spec
, rep
->content_length
);
1394 packerToMemInit(&p
, mb
);
1395 httpHeaderPackInto(&hdr
, &p
);
1397 httpHeaderClean(&hdr
);
1399 /* append <crlf> (we packed a header, not a reply) */
1400 memBufPrintf(mb
, crlf
);
1403 /* extracts a "range" from *buf and appends them to mb, updating all offsets and such */
1405 clientPackRange(clientHttpRequest
* http
, HttpHdrRangeIter
* i
, const char **buf
, ssize_t
* size
, MemBuf
* mb
)
1407 const size_t copy_sz
= i
->debt_size
<= *size
? i
->debt_size
: *size
;
1408 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1412 /* intersection of "have" and "need" ranges must not be empty */
1413 assert(body_off
< i
->spec
->offset
+ i
->spec
->length
);
1414 assert(body_off
+ *size
> i
->spec
->offset
);
1416 /* put boundary and headers at the beginning of a range in a multi-range */
1417 if (http
->request
->range
->specs
.count
> 1 && i
->debt_size
== i
->spec
->length
) {
1418 assert(http
->entry
->mem_obj
);
1420 http
->entry
->mem_obj
->reply
, /* original reply */
1421 i
->spec
, /* current range */
1422 i
->boundary
, /* boundary, the same for all */
1426 /* append content */
1427 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz
);
1428 memBufAppend(mb
, *buf
, copy_sz
);
1430 /* update offsets */
1432 i
->debt_size
-= copy_sz
;
1433 body_off
+= copy_sz
;
1435 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1437 /* paranoid check */
1438 assert(*size
>= 0 && i
->debt_size
>= 0);
1441 /* returns true if there is still data available to pack more ranges
1442 * increments iterator "i"
1443 * used by clientPackMoreRanges */
1445 clientCanPackMoreRanges(const clientHttpRequest
* http
, HttpHdrRangeIter
* i
, ssize_t size
)
1447 /* first update "i" if needed */
1448 if (!i
->debt_size
) {
1449 if ((i
->spec
= httpHdrRangeGetSpec(http
->request
->range
, &i
->pos
)))
1450 i
->debt_size
= i
->spec
->length
;
1452 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1453 /* continue condition: need_more_data && have_more_data */
1454 return i
->spec
&& size
> 0;
1457 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1458 /* returns true if we need more data */
1460 clientPackMoreRanges(clientHttpRequest
* http
, const char *buf
, ssize_t size
, MemBuf
* mb
)
1462 HttpHdrRangeIter
*i
= &http
->range_iter
;
1463 /* offset in range specs does not count the prefix of an http msg */
1464 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1466 /* check: reply was parsed and range iterator was initialized */
1467 assert(i
->prefix_size
> 0);
1468 /* filter out data according to range specs */
1469 while (clientCanPackMoreRanges(http
, i
, size
)) {
1470 off_t start
; /* offset of still missing data */
1472 start
= i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
;
1473 debug(33, 3) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1474 (int) body_off
, size
);
1475 debug(33, 3) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1476 (int) start
, (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
, i
->debt_size
);
1477 assert(body_off
<= start
); /* we did not miss it */
1478 /* skip up to start */
1479 if (body_off
+ size
> start
) {
1480 const size_t skip_size
= start
- body_off
;
1485 /* has not reached start yet */
1490 /* put next chunk if any */
1492 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1493 clientPackRange(http
, i
, &buf
, &size
, mb
);
1494 body_off
= http
->out
.offset
- i
->prefix_size
; /* sync */
1497 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1498 debug(33, 3) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1499 (int) body_off
, size
, i
->debt_size
);
1501 debug(33, 3) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1502 (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
);
1503 /* skip the data we do not need if possible */
1504 if (i
->debt_size
== i
->spec
->length
) /* at the start of the cur. spec */
1505 body_off
= i
->spec
->offset
;
1507 assert(body_off
== i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
);
1508 } else if (http
->request
->range
->specs
.count
> 1) {
1509 /* put terminating boundary for multiparts */
1510 clientPackTermBound(i
->boundary
, mb
);
1512 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1513 return i
->debt_size
> 0;
1517 * accepts chunk of a http message in buf, parses prefix, filters headers and
1518 * such, writes processed message to the client's socket
1521 clientSendMoreData(void *data
, char *buf
, ssize_t size
)
1523 clientHttpRequest
*http
= data
;
1524 StoreEntry
*entry
= http
->entry
;
1525 ConnStateData
*conn
= http
->conn
;
1527 HttpReply
*rep
= NULL
;
1528 const char *body_buf
= buf
;
1529 ssize_t body_size
= size
;
1531 ssize_t check_size
= 0;
1532 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http
->uri
, (int) size
);
1533 assert(size
<= CLIENT_SOCK_SZ
);
1534 assert(http
->request
!= NULL
);
1535 dlinkDelete(&http
->active
, &ClientActiveRequests
);
1536 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1537 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1538 fd
, storeUrl(entry
), (int) http
->out
.offset
);
1539 if (conn
->chr
!= http
) {
1540 /* there is another object in progress, defer this one */
1541 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry
));
1542 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1544 } else if (entry
&& EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1545 /* call clientWriteComplete so the client socket gets closed */
1546 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1547 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1549 } else if (size
< 0) {
1550 /* call clientWriteComplete so the client socket gets closed */
1551 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1552 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1554 } else if (size
== 0) {
1555 /* call clientWriteComplete so the client socket gets closed */
1556 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1557 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1560 if (http
->out
.offset
== 0) {
1561 if (Config
.onoff
.log_mime_hdrs
) {
1563 if ((k
= headersEnd(buf
, size
))) {
1564 safe_free(http
->al
.headers
.reply
);
1565 http
->al
.headers
.reply
= xcalloc(k
+ 1, 1);
1566 xstrncpy(http
->al
.headers
.reply
, buf
, k
);
1569 rep
= clientBuildReply(http
, buf
, size
);
1571 body_size
= size
- rep
->hdr_sz
;
1572 assert(body_size
>= 0);
1573 body_buf
= buf
+ rep
->hdr_sz
;
1574 http
->range_iter
.prefix_size
= rep
->hdr_sz
;
1575 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1576 body_size
, rep
->hdr_sz
);
1577 } else if (size
< CLIENT_SOCK_SZ
&& entry
->store_status
== STORE_PENDING
) {
1578 /* wait for more to arrive */
1579 storeClientCopy(entry
,
1580 http
->out
.offset
+ size
,
1588 /* reset range iterator */
1589 http
->range_iter
.pos
= HttpHdrRangeInitPos
;
1591 if (http
->request
->method
== METHOD_HEAD
) {
1593 /* do not forward body for HEAD replies */
1595 http
->flags
.done_copying
= 1;
1598 * If we are here, then store_status == STORE_OK and it
1599 * seems we have a HEAD repsponse which is missing the
1600 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1601 * does this). Because clientBuildReply() fails we just
1602 * call this reply a body, set the done_copying flag and
1605 http
->flags
.done_copying
= 1;
1608 /* write headers and/or body if any */
1609 assert(rep
|| (body_buf
&& body_size
));
1610 /* init mb; put status line and headers if any */
1612 mb
= httpReplyPack(rep
);
1613 http
->out
.offset
+= rep
->hdr_sz
;
1614 check_size
+= rep
->hdr_sz
;
1615 httpReplyDestroy(rep
);
1620 /* append body if any */
1621 if (http
->request
->range
) {
1622 /* Only GET requests should have ranges */
1623 assert(http
->request
->method
== METHOD_GET
);
1624 /* clientPackMoreRanges() updates http->out.offset */
1625 /* force the end of the transfer if we are done */
1626 if (!clientPackMoreRanges(http
, body_buf
, body_size
, &mb
))
1627 http
->flags
.done_copying
= 1;
1628 } else if (body_buf
&& body_size
) {
1629 http
->out
.offset
+= body_size
;
1630 check_size
+= body_size
;
1631 memBufAppend(&mb
, body_buf
, body_size
);
1633 if (!http
->request
->range
&& http
->request
->method
== METHOD_GET
)
1634 assert(check_size
== size
);
1636 comm_write_mbuf(fd
, mb
, clientWriteComplete
, http
);
1637 /* if we don't do it, who will? */
1638 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1642 clientKeepaliveNextRequest(clientHttpRequest
* http
)
1644 ConnStateData
*conn
= http
->conn
;
1646 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn
->fd
);
1647 conn
->defer
.until
= 0; /* Kick it to read a new request */
1648 httpRequestFree(http
);
1649 if ((http
= conn
->chr
) == NULL
) {
1650 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1652 fd_note(conn
->fd
, "Reading next request");
1654 * Set the timeout BEFORE calling clientReadRequest().
1656 commSetTimeout(conn
->fd
, 15, requestTimeout
, conn
);
1657 clientReadRequest(conn
->fd
, conn
); /* Read next request */
1659 * Note, the FD may be closed at this point.
1661 } else if ((entry
= http
->entry
) == NULL
) {
1663 * this request is in progress, maybe doing an ACL or a redirect,
1664 * execution will resume after the operation completes.
1667 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1670 if (0 == storeClientCopyPending(entry
, http
)) {
1671 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1672 debug(33, 0) ("clientKeepaliveNextRequest: ENTRY_ABORTED\n");
1673 storeClientCopy(entry
,
1677 memAllocate(MEM_CLIENT_SOCK_BUF
),
1685 clientWriteComplete(int fd
, char *bufnotused
, size_t size
, int errflag
, void *data
)
1687 clientHttpRequest
*http
= data
;
1688 StoreEntry
*entry
= http
->entry
;
1690 http
->out
.size
+= size
;
1691 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1692 fd
, size
, errflag
, (int) http
->out
.offset
, entry
? objectLen(entry
) : 0);
1694 kb_incr(&Counter
.client_http
.kbytes_out
, size
);
1695 if (isTcpHit(http
->log_type
))
1696 kb_incr(&Counter
.client_http
.hit_kbytes_out
, size
);
1700 * just close the socket, httpRequestFree will abort if needed
1703 } else if (NULL
== entry
) {
1704 comm_close(fd
); /* yuk */
1705 } else if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1707 } else if ((done
= clientCheckTransferDone(http
)) != 0 || size
== 0) {
1708 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd
);
1709 /* We're finished case */
1710 if (http
->entry
->mem_obj
->reply
->content_length
< 0) {
1711 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1714 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1716 } else if (clientGotNotEnough(http
)) {
1717 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1719 } else if (http
->request
->flags
.proxy_keepalive
) {
1720 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd
);
1721 clientKeepaliveNextRequest(http
);
1726 /* More data will be coming from primary server; register with
1727 * storage manager. */
1728 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1729 debug(33, 0) ("clientWriteComplete 2: ENTRY_ABORTED\n");
1730 storeClientCopy(entry
,
1734 memAllocate(MEM_CLIENT_SOCK_BUF
),
1741 * client issued a request with an only-if-cached cache-control directive;
1742 * we did not find a cached object that can be returned without
1743 * contacting other servers;
1744 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1747 clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
)
1749 char *url
= http
->uri
;
1750 request_t
*r
= http
->request
;
1751 ErrorState
*err
= NULL
;
1752 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1753 RequestMethodStr
[r
->method
], url
);
1754 http
->al
.http
.code
= HTTP_GATEWAY_TIMEOUT
;
1755 err
= errorCon(ERR_ONLY_IF_CACHED_MISS
, HTTP_GATEWAY_TIMEOUT
);
1756 err
->request
= requestLink(r
);
1757 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1759 storeUnregister(http
->entry
, http
);
1760 storeUnlockObject(http
->entry
);
1762 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1763 errorAppendEntry(http
->entry
, err
);
1767 clientProcessRequest2(clientHttpRequest
* http
)
1769 request_t
*r
= http
->request
;
1771 e
= http
->entry
= storeGetPublic(http
->uri
, r
->method
);
1772 if (r
->method
== METHOD_HEAD
&& e
== NULL
) {
1773 /* We can generate a HEAD reply from a cached GET object */
1774 e
= http
->entry
= storeGetPublic(http
->uri
, METHOD_GET
);
1776 #if USE_CACHE_DIGESTS
1777 http
->lookup_type
= e
? "HIT" : "MISS";
1780 /* this object isn't in the cache */
1781 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
1782 return LOG_TCP_MISS
;
1784 if (Config
.onoff
.offline
) {
1785 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
1789 if (!storeEntryValidToSend(e
)) {
1790 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
1792 return LOG_TCP_MISS
;
1794 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1795 /* Special entries are always hits, no matter what the client says */
1796 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
1801 if (r
->flags
.nocache_hack
) {
1802 /* if nocache_hack is set, nocache should always be clear, right? */
1803 assert(!r
->flags
.nocache
);
1804 ipcacheReleaseInvalid(r
->host
);
1807 if (e
->store_status
== STORE_PENDING
) {
1808 if (r
->flags
.nocache
|| r
->flags
.nocache_hack
) {
1809 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
1811 r
->flags
.nocache
= 0;
1812 r
->flags
.nocache_hack
= 0;
1816 if (r
->flags
.nocache
) {
1817 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
1819 ipcacheReleaseInvalid(r
->host
);
1820 return LOG_TCP_CLIENT_REFRESH_MISS
;
1822 if (r
->range
&& httpHdrRangeWillBeComplex(r
->range
)) {
1824 * Some clients break if we return "200 OK" for a Range
1825 * request. We would have to return "200 OK" for a _complex_
1826 * Range request that is also a HIT. Thus, let's prevent HITs
1827 * on complex Range requests
1829 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
1831 return LOG_TCP_MISS
;
1833 debug(33, 3) ("clientProcessRequest2: default HIT\n");
1839 clientProcessRequest(clientHttpRequest
* http
)
1841 char *url
= http
->uri
;
1842 request_t
*r
= http
->request
;
1843 int fd
= http
->conn
->fd
;
1845 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
1846 RequestMethodStr
[r
->method
],
1848 if (r
->method
== METHOD_CONNECT
) {
1849 http
->log_type
= LOG_TCP_MISS
;
1850 sslStart(fd
, url
, r
, &http
->out
.size
);
1852 } else if (r
->method
== METHOD_PURGE
) {
1853 clientPurgeRequest(http
);
1855 } else if (r
->method
== METHOD_TRACE
) {
1856 if (r
->max_forwards
== 0) {
1857 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1858 storeReleaseRequest(http
->entry
);
1859 storeBuffer(http
->entry
);
1860 rep
= httpReplyCreate();
1861 httpReplySetHeaders(rep
, 1.0, HTTP_OK
, NULL
, "text/plain",
1862 httpRequestPrefixLen(r
), 0, squid_curtime
);
1863 httpReplySwapOut(rep
, http
->entry
);
1864 httpReplyDestroy(rep
);
1865 httpRequestSwapOut(r
, http
->entry
);
1866 storeComplete(http
->entry
);
1870 http
->log_type
= LOG_TCP_MISS
;
1871 } else if (pumpMethod(r
->method
)) {
1872 http
->log_type
= LOG_TCP_MISS
;
1873 /* XXX oof, POST can be cached! */
1874 pumpInit(fd
, r
, http
->uri
);
1876 http
->log_type
= clientProcessRequest2(http
);
1878 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
1879 log_tags
[http
->log_type
],
1881 http
->out
.offset
= 0;
1882 if (NULL
!= http
->entry
) {
1883 storeLockObject(http
->entry
);
1884 storeCreateMemObject(http
->entry
, http
->uri
, http
->log_uri
);
1885 http
->entry
->mem_obj
->method
= r
->method
;
1886 storeClientListAdd(http
->entry
, http
);
1888 delaySetStoreClient(http
->entry
, http
, delayClient(r
));
1890 http
->entry
->refcount
++;
1891 storeClientCopy(http
->entry
,
1895 memAllocate(MEM_CLIENT_SOCK_BUF
),
1900 http
->log_type
= LOG_TCP_MISS
;
1901 clientProcessMiss(http
);
1906 * Prepare to fetch the object as it's a cache miss of some kind.
1909 clientProcessMiss(clientHttpRequest
* http
)
1911 char *url
= http
->uri
;
1912 request_t
*r
= http
->request
;
1913 ErrorState
*err
= NULL
;
1914 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
1915 RequestMethodStr
[r
->method
], url
);
1917 * We might have a left-over StoreEntry from a failed cache hit
1921 if (EBIT_TEST(http
->entry
->flags
, ENTRY_SPECIAL
))
1922 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url
);
1923 storeUnregister(http
->entry
, http
);
1924 storeUnlockObject(http
->entry
);
1927 if (clientOnlyIfCached(http
)) {
1928 clientProcessOnlyIfCachedMiss(http
);
1932 * Deny loops when running in accelerator/transproxy mode.
1934 if (http
->flags
.accel
&& r
->flags
.loopdetect
) {
1935 http
->al
.http
.code
= HTTP_FORBIDDEN
;
1936 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
1937 err
->request
= requestLink(r
);
1938 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1939 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1940 errorAppendEntry(http
->entry
, err
);
1943 assert(http
->out
.offset
== 0);
1944 http
->entry
= clientCreateStoreEntry(http
, r
->method
, r
->flags
);
1945 http
->entry
->refcount
++;
1946 if (http
->redirect
.status
) {
1947 HttpReply
*rep
= httpReplyCreate();
1948 storeReleaseRequest(http
->entry
);
1949 httpRedirectReply(rep
, http
->redirect
.status
, http
->redirect
.location
);
1950 httpReplySwapOut(rep
, http
->entry
);
1951 httpReplyDestroy(rep
);
1952 storeComplete(http
->entry
);
1955 if (http
->flags
.internal
)
1956 r
->protocol
= PROTO_INTERNAL
;
1957 fwdStart(http
->conn
->fd
, http
->entry
, r
,
1958 http
->conn
->peer
.sin_addr
, http
->conn
->me
.sin_addr
);
1961 static clientHttpRequest
*
1962 parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
)
1964 clientHttpRequest
*http
= xcalloc(1, sizeof(clientHttpRequest
));
1965 cbdataAdd(http
, cbdataXfree
, 0);
1967 http
->start
= current_time
;
1968 http
->req_sz
= conn
->in
.offset
;
1969 http
->uri
= xstrdup(uri
);
1970 http
->log_uri
= xstrndup(uri
, MAX_URL
);
1971 http
->range_iter
.boundary
= StringNull
;
1972 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1977 * parseHttpRequest()
1980 * NULL on error or incomplete request
1981 * a clientHttpRequest structure on success
1983 static clientHttpRequest
*
1984 parseHttpRequest(ConnStateData
* conn
, method_t
* method_p
, int *status
,
1985 char **prefix_p
, size_t * req_line_sz_p
)
1990 char *req_hdr
= NULL
;
1995 int free_request
= 0;
1996 size_t header_sz
; /* size of headers, not including first line */
1997 size_t prefix_sz
; /* size of whole request (req-line + headers) */
2001 clientHttpRequest
*http
= NULL
;
2003 struct natlookup natLookup
;
2004 static int natfd
= -1;
2007 if ((req_sz
= headersEnd(conn
->in
.buf
, conn
->in
.offset
)) == 0) {
2008 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
2011 *method_p
= METHOD_NONE
;
2014 assert(req_sz
<= conn
->in
.offset
);
2015 /* Use memcpy, not strdup! */
2016 inbuf
= xmalloc(req_sz
+ 1);
2017 xmemcpy(inbuf
, conn
->in
.buf
, req_sz
);
2018 *(inbuf
+ req_sz
) = '\0';
2020 /* pre-set these values to make aborting simpler */
2022 *method_p
= METHOD_NONE
;
2025 /* Barf on NULL characters in the headers */
2026 if (strlen(inbuf
) != req_sz
) {
2027 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
2028 return parseHttpRequestAbort(conn
, "error:invalid-request");
2030 /* Look for request method */
2031 if ((mstr
= strtok(inbuf
, "\t ")) == NULL
) {
2032 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
2033 return parseHttpRequestAbort(conn
, "error:invalid-request-method");
2035 method
= urlParseMethod(mstr
);
2036 if (method
== METHOD_NONE
) {
2037 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr
);
2038 return parseHttpRequestAbort(conn
, "error:unsupported-request-method");
2040 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr
);
2043 /* look for URL+HTTP/x.x */
2044 if ((url
= strtok(NULL
, "\n")) == NULL
) {
2045 debug(33, 1) ("parseHttpRequest: Missing URL\n");
2046 return parseHttpRequestAbort(conn
, "error:missing-url");
2048 while (xisspace(*url
))
2050 t
= url
+ strlen(url
);
2055 if (xisspace(*t
) && !strncmp(t
+ 1, "HTTP/", 5)) {
2060 while (t
> url
&& xisspace(*t
))
2062 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url
);
2063 if (token
== NULL
) {
2064 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
2065 #if RELAXED_HTTP_PARSER
2066 http_ver
= (float) 0.9; /* wild guess */
2068 return parseHttpRequestAbort(conn
, "error:missing-http-ident");
2071 http_ver
= (float) atof(token
+ 5);
2075 * Process headers after request line
2077 req_hdr
= strtok(NULL
, null_string
);
2078 header_sz
= req_sz
- (req_hdr
- inbuf
);
2079 if (0 == header_sz
) {
2080 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
2084 assert(header_sz
> 0);
2085 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr
);
2086 end
= req_hdr
+ header_sz
;
2087 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end
);
2089 prefix_sz
= end
- inbuf
;
2090 *req_line_sz_p
= req_hdr
- inbuf
;
2091 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
2092 (int) prefix_sz
, (int) *req_line_sz_p
);
2093 assert(prefix_sz
<= conn
->in
.offset
);
2095 /* Ok, all headers are received */
2096 http
= xcalloc(1, sizeof(clientHttpRequest
));
2097 cbdataAdd(http
, cbdataXfree
, 0);
2098 http
->http_ver
= http_ver
;
2100 http
->start
= current_time
;
2101 http
->req_sz
= prefix_sz
;
2102 http
->range_iter
.boundary
= StringNull
;
2103 *prefix_p
= xmalloc(prefix_sz
+ 1);
2104 xmemcpy(*prefix_p
, conn
->in
.buf
, prefix_sz
);
2105 *(*prefix_p
+ prefix_sz
) = '\0';
2106 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2108 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p
) + *req_line_sz_p
);
2109 if ((t
= strchr(url
, '#'))) /* remove HTML anchors */
2112 /* handle internal objects */
2113 if (internalCheck(url
)) {
2114 /* prepend our name & port */
2115 http
->uri
= xstrdup(internalLocalUri(NULL
, url
));
2116 http
->flags
.internal
= 1;
2117 http
->flags
.accel
= 1;
2119 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2120 else if (Config2
.Accel
.on
&& *url
== '/') {
2121 /* prepend the accel prefix */
2122 if (opt_accel_uses_host
&& (t
= mime_get_header(req_hdr
, "Host"))) {
2123 /* If a Host: header was specified, use it to build the URL
2124 * instead of the one in the Config file. */
2126 * XXX Use of the Host: header here opens a potential
2127 * security hole. There are no checks that the Host: value
2128 * corresponds to one of your servers. It might, for example,
2129 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2130 * types should be used to prevent httpd-accelerators
2131 * handling requests for non-local servers */
2133 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
+
2135 http
->uri
= xcalloc(url_sz
, 1);
2136 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2137 t
, (int) Config
.Accel
.port
, url
);
2138 } else if (vhost_mode
) {
2139 /* Put the local socket IP address as the hostname */
2140 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
;
2141 http
->uri
= xcalloc(url_sz
, 1);
2143 natLookup
.nl_inport
= http
->conn
->me
.sin_port
;
2144 natLookup
.nl_outport
= http
->conn
->peer
.sin_port
;
2145 natLookup
.nl_inip
= http
->conn
->me
.sin_addr
;
2146 natLookup
.nl_outip
= http
->conn
->peer
.sin_addr
;
2147 natLookup
.nl_flags
= IPN_TCP
;
2149 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
2151 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2153 return parseHttpRequestAbort(conn
, "error:nat-open-failed");
2155 if (ioctl(natfd
, SIOCGNATL
, &natLookup
) < 0) {
2156 if (errno
!= ESRCH
) {
2157 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2160 return parseHttpRequestAbort(conn
, "error:nat-lookup-failed");
2162 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2163 inet_ntoa(http
->conn
->me
.sin_addr
),
2164 (int) Config
.Accel
.port
,
2167 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2168 inet_ntoa(natLookup
.nl_realip
),
2169 (int) Config
.Accel
.port
,
2172 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2173 inet_ntoa(http
->conn
->me
.sin_addr
),
2174 (int) Config
.Accel
.port
,
2177 debug(33, 5) ("VHOST REWRITE: '%s'\n", http
->uri
);
2179 url_sz
= strlen(Config2
.Accel
.prefix
) + strlen(url
) +
2180 Config
.appendDomainLen
+ 1;
2181 http
->uri
= xcalloc(url_sz
, 1);
2182 snprintf(http
->uri
, url_sz
, "%s%s", Config2
.Accel
.prefix
, url
);
2184 http
->flags
.accel
= 1;
2186 /* URL may be rewritten later, so make extra room */
2187 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
2188 http
->uri
= xcalloc(url_sz
, 1);
2189 strcpy(http
->uri
, url
);
2190 http
->flags
.accel
= 0;
2192 if (!stringHasWhitespace(http
->uri
))
2193 http
->log_uri
= xstrndup(http
->uri
, MAX_URL
);
2195 http
->log_uri
= xstrndup(rfc1738_escape(http
->uri
), MAX_URL
);
2196 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2205 clientReadDefer(int fdnotused
, void *data
)
2207 ConnStateData
*conn
= data
;
2208 return conn
->defer
.until
> squid_curtime
;
2212 clientReadRequest(int fd
, void *data
)
2214 ConnStateData
*conn
= data
;
2215 int parser_return_code
= 0;
2217 request_t
*request
= NULL
;
2220 clientHttpRequest
*http
= NULL
;
2221 clientHttpRequest
**H
= NULL
;
2222 char *prefix
= NULL
;
2223 ErrorState
*err
= NULL
;
2224 fde
*F
= &fd_table
[fd
];
2225 int len
= conn
->in
.size
- conn
->in
.offset
- 1;
2226 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd
);
2227 Counter
.syscalls
.sock
.reads
++;
2228 size
= read(fd
, conn
->in
.buf
+ conn
->in
.offset
, len
);
2230 fd_bytes(fd
, size
, FD_READ
);
2231 kb_incr(&Counter
.client_http
.kbytes_in
, size
);
2234 * Don't reset the timeout value here. The timeout value will be
2235 * set to Config.Timeout.request by httpAccept() and
2236 * clientWriteComplete(), and should apply to the request as a
2237 * whole, not individual read() calls. Plus, it breaks our
2238 * lame half-close detection
2240 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, conn
, 0);
2242 if (conn
->chr
== NULL
) {
2243 /* no current or pending requests */
2246 } else if (!Config
.onoff
.half_closed_clients
) {
2247 /* admin doesn't want to support half-closed client sockets */
2251 /* It might be half-closed, we can't tell */
2252 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd
);
2253 F
->flags
.socket_eof
= 1;
2254 conn
->defer
.until
= squid_curtime
+ 1;
2256 fd_note(fd
, "half-closed");
2258 } else if (size
< 0) {
2259 if (!ignoreErrno(errno
)) {
2260 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd
, xstrerror());
2263 } else if (conn
->in
.offset
== 0) {
2264 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd
, xstrerror());
2267 /* Continue to process previously read data */
2270 conn
->in
.offset
+= size
;
2271 /* Skip leading (and trailing) whitespace */
2272 while (conn
->in
.offset
> 0) {
2275 while (conn
->in
.offset
> 0 && xisspace(conn
->in
.buf
[0])) {
2276 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ 1, conn
->in
.offset
- 1);
2279 conn
->in
.buf
[conn
->in
.offset
] = '\0'; /* Terminate the string */
2280 if (conn
->in
.offset
== 0)
2282 /* Limit the number of concurrent requests to 2 */
2283 for (H
= &conn
->chr
, nrequests
= 0; *H
; H
= &(*H
)->next
, nrequests
++);
2284 if (nrequests
>= 2) {
2285 debug(33, 3) ("clientReadRequest: FD %d max concurrent requests reached\n", fd
);
2286 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd
);
2287 conn
->defer
.until
= squid_curtime
+ 100; /* Reset when a request is complete */
2290 /* Process request */
2291 http
= parseHttpRequest(conn
,
2293 &parser_return_code
,
2299 assert(http
->req_sz
> 0);
2300 conn
->in
.offset
-= http
->req_sz
;
2301 assert(conn
->in
.offset
>= 0);
2302 debug(33, 5) ("conn->in.offset = %d\n", (int) conn
->in
.offset
);
2304 * If we read past the end of this request, move the remaining
2305 * data to the beginning
2307 if (conn
->in
.offset
> 0)
2308 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ http
->req_sz
, conn
->in
.offset
);
2309 /* add to the client request queue */
2310 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2313 commSetTimeout(fd
, Config
.Timeout
.lifetime
, NULL
, NULL
);
2314 if (parser_return_code
< 0) {
2315 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd
);
2316 err
= errorCon(ERR_INVALID_REQ
, HTTP_BAD_REQUEST
);
2317 err
->request_hdrs
= xstrdup(conn
->in
.buf
);
2318 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2319 errorAppendEntry(http
->entry
, err
);
2323 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
2324 debug(33, 5) ("Invalid URL: %s\n", http
->uri
);
2325 err
= errorCon(ERR_INVALID_URL
, HTTP_BAD_REQUEST
);
2326 err
->src_addr
= conn
->peer
.sin_addr
;
2327 err
->url
= xstrdup(http
->uri
);
2328 http
->al
.http
.code
= err
->http_status
;
2329 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2330 errorAppendEntry(http
->entry
, err
);
2334 /* compile headers */
2335 /* we should skip request line! */
2336 if (!httpRequestParseHeader(request
, prefix
+ req_line_sz
))
2337 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2339 /* continue anyway? */
2341 request
->flags
.accelerated
= http
->flags
.accel
;
2342 if (!http
->flags
.internal
) {
2343 if (internalCheck(strBuf(request
->urlpath
))) {
2344 if (0 == strcasecmp(request
->host
, internalHostname()) &&
2345 request
->port
== Config
.Port
.http
->i
) {
2346 http
->flags
.internal
= 1;
2347 } else if (internalStaticCheck(strBuf(request
->urlpath
))) {
2348 xstrncpy(request
->host
, internalHostname(), SQUIDHOSTNAMELEN
);
2349 request
->port
= Config
.Port
.http
->i
;
2350 http
->flags
.internal
= 1;
2354 request
->flags
.internal
= http
->flags
.internal
;
2356 safe_free(http
->log_uri
);
2357 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
2358 request
->client_addr
= conn
->peer
.sin_addr
;
2359 request
->my_addr
= conn
->me
.sin_addr
;
2360 request
->http_ver
= http
->http_ver
;
2361 if (!urlCheckRequest(request
)) {
2362 err
= errorCon(ERR_UNSUP_REQ
, HTTP_NOT_IMPLEMENTED
);
2363 err
->src_addr
= conn
->peer
.sin_addr
;
2364 err
->request
= requestLink(request
);
2365 http
->al
.http
.code
= err
->http_status
;
2366 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2367 errorAppendEntry(http
->entry
, err
);
2370 if (0 == clientCheckContentLength(request
)) {
2371 err
= errorCon(ERR_INVALID_REQ
, HTTP_LENGTH_REQUIRED
);
2372 err
->src_addr
= conn
->peer
.sin_addr
;
2373 err
->request
= requestLink(request
);
2374 http
->al
.http
.code
= err
->http_status
;
2375 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2376 errorAppendEntry(http
->entry
, err
);
2379 http
->request
= requestLink(request
);
2381 * We need to set the keepalive flag before doing some
2382 * hacks for POST/PUT requests below. Maybe we could
2383 * set keepalive flag even earlier.
2385 clientSetKeepaliveFlag(http
);
2387 * break here for NON-GET because most likely there is a
2388 * reqeust body following and we don't want to parse it
2389 * as though it was new request
2391 if (request
->method
!= METHOD_GET
) {
2392 int cont_len
= httpHeaderGetInt(&request
->header
, HDR_CONTENT_LENGTH
);
2393 int copy_len
= XMIN(conn
->in
.offset
, cont_len
);
2395 assert(conn
->in
.offset
>= copy_len
);
2396 request
->body_sz
= copy_len
;
2397 request
->body
= xmalloc(request
->body_sz
);
2398 xmemcpy(request
->body
, conn
->in
.buf
, request
->body_sz
);
2399 conn
->in
.offset
-= copy_len
;
2400 if (conn
->in
.offset
)
2401 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ copy_len
, conn
->in
.offset
);
2404 * if we didn't get the full body now, then more will
2405 * be arriving on the client socket. Lets cancel
2406 * the read handler until this request gets forwarded.
2408 if (request
->body_sz
< cont_len
)
2409 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2411 clientAccessCheck(http
);
2412 continue; /* while offset > 0 */
2413 } else if (parser_return_code
== 0) {
2415 * Partial request received; reschedule until parseHttpRequest()
2416 * is happy with the input
2418 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2420 if (conn
->in
.offset
>= Config
.maxRequestSize
) {
2421 /* The request is too large to handle */
2422 debug(33, 0) ("Request won't fit in buffer.\n");
2423 debug(33, 0) ("Config 'request_size'= %d bytes.\n",
2424 Config
.maxRequestSize
);
2425 debug(33, 0) ("This request = %d bytes.\n",
2426 (int) conn
->in
.offset
);
2427 err
= errorCon(ERR_INVALID_REQ
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2428 http
= parseHttpRequestAbort(conn
, "error:request-too-large");
2429 /* add to the client request queue */
2430 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2432 http
->entry
= clientCreateStoreEntry(http
, METHOD_NONE
, null_request_flags
);
2433 errorAppendEntry(http
->entry
, err
);
2436 /* Grow the request memory area to accomodate for a large request */
2437 conn
->in
.size
+= REQUEST_BUF_SIZE
;
2438 conn
->in
.buf
= xrealloc(conn
->in
.buf
, conn
->in
.size
);
2439 /* XXX account conn->in.buf */
2440 debug(33, 3) ("Handling a large request, offset=%d inbufsize=%d\n",
2441 (int) conn
->in
.offset
, conn
->in
.size
);
2442 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2449 /* general lifetime handler for HTTP requests */
2451 requestTimeout(int fd
, void *data
)
2453 ConnStateData
*conn
= data
;
2455 debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2456 if (fd_table
[fd
].rwstate
) {
2458 * Some data has been sent to the client, just close the FD
2461 } else if (conn
->nrequests
) {
2463 * assume its a persistent connection; just close it
2470 err
= errorCon(ERR_LIFETIME_EXP
, HTTP_REQUEST_TIMEOUT
);
2471 err
->url
= xstrdup("N/A");
2473 * Normally we shouldn't call errorSend() in client_side.c, but
2474 * it should be okay in this case. Presumably if we get here
2475 * this is the first request for the connection, and no data
2476 * has been written yet
2478 assert(conn
->chr
== NULL
);
2481 * if we don't close() here, we still need a timeout handler!
2483 commSetTimeout(fd
, 30, requestTimeout
, conn
);
2488 httpAcceptDefer(void)
2490 static time_t last_warn
= 0;
2491 if (fdNFree() >= RESERVED_FD
)
2493 if (last_warn
+ 15 < squid_curtime
) {
2494 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2495 last_warn
= squid_curtime
;
2500 /* Handle a new connection on HTTP socket. */
2502 httpAccept(int sock
, void *data
)
2506 ConnStateData
*connState
= NULL
;
2507 struct sockaddr_in peer
;
2508 struct sockaddr_in me
;
2509 int max
= INCOMING_HTTP_MAX
;
2511 static aclCheck_t identChecklist
;
2513 commSetSelect(sock
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2514 while (max
-- && !httpAcceptDefer()) {
2515 memset(&peer
, '\0', sizeof(struct sockaddr_in
));
2516 memset(&me
, '\0', sizeof(struct sockaddr_in
));
2517 if ((fd
= comm_accept(sock
, &peer
, &me
)) < 0) {
2518 if (!ignoreErrno(errno
))
2519 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2523 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd
);
2524 connState
= xcalloc(1, sizeof(ConnStateData
));
2525 connState
->peer
= peer
;
2526 connState
->log_addr
= peer
.sin_addr
;
2527 connState
->log_addr
.s_addr
&= Config
.Addrs
.client_netmask
.s_addr
;
2530 connState
->in
.size
= REQUEST_BUF_SIZE
;
2531 connState
->in
.buf
= xcalloc(connState
->in
.size
, 1);
2532 cbdataAdd(connState
, cbdataXfree
, 0);
2533 /* XXX account connState->in.buf */
2534 comm_add_close_handler(fd
, connStateFree
, connState
);
2535 if (Config
.onoff
.log_fqdn
)
2536 fqdncache_gethostbyaddr(peer
.sin_addr
, FQDN_LOOKUP_IF_MISS
);
2537 commSetTimeout(fd
, Config
.Timeout
.request
, requestTimeout
, connState
);
2539 identChecklist
.src_addr
= peer
.sin_addr
;
2540 identChecklist
.my_addr
= me
.sin_addr
;
2541 if (aclCheckFast(Config
.accessList
.identLookup
, &identChecklist
))
2542 identStart(&me
, &peer
, clientIdentDone
, connState
);
2544 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, connState
, 0);
2545 commSetDefer(fd
, clientReadDefer
, connState
);
2550 #define SENDING_BODY 0
2551 #define SENDING_HDRSONLY 1
2553 clientCheckTransferDone(clientHttpRequest
* http
)
2555 int sending
= SENDING_BODY
;
2556 StoreEntry
*entry
= http
->entry
;
2563 * For now, 'done_copying' is used for special cases like
2564 * Range and HEAD requests.
2566 if (http
->flags
.done_copying
)
2569 * Handle STORE_OK objects.
2570 * objectLen(entry) will be set proprely.
2572 if (entry
->store_status
== STORE_OK
) {
2573 if (http
->out
.offset
>= objectLen(entry
))
2579 * Now, handle STORE_PENDING objects
2581 mem
= entry
->mem_obj
;
2582 assert(mem
!= NULL
);
2583 assert(http
->request
!= NULL
);
2585 if (reply
->hdr_sz
== 0)
2586 return 0; /* haven't found end of headers yet */
2587 else if (reply
->sline
.status
== HTTP_OK
)
2588 sending
= SENDING_BODY
;
2589 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
2590 sending
= SENDING_HDRSONLY
;
2591 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
2592 sending
= SENDING_HDRSONLY
;
2593 else if (reply
->sline
.status
< HTTP_OK
)
2594 sending
= SENDING_HDRSONLY
;
2595 else if (http
->request
->method
== METHOD_HEAD
)
2596 sending
= SENDING_HDRSONLY
;
2598 sending
= SENDING_BODY
;
2600 * Figure out how much data we are supposed to send.
2601 * If we are sending a body and we don't have a content-length,
2602 * then we must wait for the object to become STORE_OK.
2604 if (sending
== SENDING_HDRSONLY
)
2605 sendlen
= reply
->hdr_sz
;
2606 else if (reply
->content_length
< 0)
2609 sendlen
= reply
->content_length
+ reply
->hdr_sz
;
2611 * Now that we have the expected length, did we send it all?
2613 if (http
->out
.offset
< sendlen
)
2620 clientGotNotEnough(clientHttpRequest
* http
)
2622 int cl
= http
->entry
->mem_obj
->reply
->content_length
;
2623 int hs
= http
->entry
->mem_obj
->reply
->hdr_sz
;
2625 if (http
->out
.offset
< cl
+ hs
)
2631 * This function is designed to serve a fairly specific purpose.
2632 * Occasionally our vBNS-connected caches can talk to each other, but not
2633 * the rest of the world. Here we try to detect frequent failures which
2634 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2635 * the failure:success ratio goes above 1.0 then we go into "hit only"
2636 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2637 * will only fetch HITs from us if they are using the ICP protocol. We
2638 * stay in this mode for 5 minutes.
2640 * Duane W., Sept 16, 1996
2644 checkFailureRatio(err_type etype
, hier_code hcode
)
2646 static double magic_factor
= 100.0;
2649 if (hcode
== HIER_NONE
)
2651 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
2652 n_bad
= magic_factor
- n_good
;
2655 case ERR_CONNECT_FAIL
:
2656 case ERR_READ_ERROR
:
2662 request_failure_ratio
= n_bad
/ n_good
;
2663 if (hit_only_mode_until
> squid_curtime
)
2665 if (request_failure_ratio
< 1.0)
2667 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
2668 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2669 FAILURE_MODE_TIME
/ 60);
2670 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
2671 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
2675 clientHttpConnectionsOpen(void)
2679 for (u
= Config
.Port
.http
; u
; u
= u
->next
) {
2681 fd
= comm_open(SOCK_STREAM
,
2683 Config
.Addrs
.tcp_incoming
,
2691 commSetSelect(fd
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2692 /*commSetDefer(fd, httpAcceptDefer, NULL); */
2693 debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n",
2695 HttpSockets
[NHttpSockets
++] = fd
;
2697 if (NHttpSockets
< 1)
2698 fatal("Cannot open HTTP Port");
2702 clientHttpConnectionsClose(void)
2705 for (i
= 0; i
< NHttpSockets
; i
++) {
2706 if (HttpSockets
[i
] >= 0) {
2707 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets
[i
]);
2708 comm_close(HttpSockets
[i
]);
2709 HttpSockets
[i
] = -1;