3 * $Id: client_side.cc,v 1.474 2000/03/28 17:41:39 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 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
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>
45 #include <ip_compat.h>
46 #elif HAVE_NETINET_IP_COMPAT_H
47 #include <netinet/ip_compat.h>
51 #elif HAVE_NETINET_IP_FIL_H
52 #include <netinet/ip_fil.h>
56 #elif HAVE_NETINET_IP_NAT_H
57 #include <netinet/ip_nat.h>
64 #define comm_close comm_lingering_close
67 static const char *const crlf
= "\r\n";
69 #define REQUEST_BUF_SIZE 4096
70 #define FAILURE_MODE_TIME 300
74 static CWCB clientWriteComplete
;
75 static PF clientReadRequest
;
76 static PF connStateFree
;
77 static PF requestTimeout
;
78 static int clientCheckTransferDone(clientHttpRequest
*);
79 static int clientGotNotEnough(clientHttpRequest
*);
80 static void checkFailureRatio(err_type
, hier_code
);
81 static void clientProcessMiss(clientHttpRequest
*);
82 static void clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
);
83 static clientHttpRequest
*parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
);
84 static clientHttpRequest
*parseHttpRequest(ConnStateData
*, method_t
*, int *, char **, size_t *);
85 static RH clientRedirectDone
;
86 static STCB clientHandleIMSReply
;
87 static int clientGetsOldEntry(StoreEntry
* new, StoreEntry
* old
, request_t
* request
);
88 static int checkAccelOnly(clientHttpRequest
*);
90 static IDCB clientIdentDone
;
92 static int clientOnlyIfCached(clientHttpRequest
* http
);
93 static STCB clientSendMoreData
;
94 static STCB clientCacheHit
;
95 static void clientSetKeepaliveFlag(clientHttpRequest
*);
96 static void clientPackRangeHdr(const HttpReply
* rep
, const HttpHdrRangeSpec
* spec
, String boundary
, MemBuf
* mb
);
97 static void clientPackTermBound(String boundary
, MemBuf
* mb
);
98 static void clientInterpretRequestHeaders(clientHttpRequest
*);
99 static void clientProcessRequest(clientHttpRequest
*);
100 static void clientProcessExpired(void *data
);
101 static void clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
);
102 static int clientCachable(clientHttpRequest
* http
);
103 static int clientHierarchical(clientHttpRequest
* http
);
104 static int clientCheckContentLength(request_t
* r
);
105 static int httpAcceptDefer(void);
106 static log_type
clientProcessRequest2(clientHttpRequest
* http
);
107 static int clientReplyBodyTooLarge(int clen
);
108 static int clientRequestBodyTooLarge(int clen
);
111 checkAccelOnly(clientHttpRequest
* http
)
113 /* return TRUE if someone makes a proxy request to us and
114 * we are in httpd-accel only mode */
115 if (!Config2
.Accel
.on
)
117 if (Config
.onoff
.accel_with_proxy
)
119 if (http
->request
->protocol
== PROTO_CACHEOBJ
)
121 if (http
->flags
.accel
)
128 clientIdentDone(const char *ident
, void *data
)
130 ConnStateData
*conn
= data
;
132 xstrncpy(conn
->ident
, ident
, sizeof(conn
->ident
));
134 xstrncpy(conn
->ident
, "-", sizeof(conn
->ident
));
139 clientAccessCheck(void *data
)
141 clientHttpRequest
*http
= data
;
142 ConnStateData
*conn
= http
->conn
;
143 if (checkAccelOnly(http
)) {
144 clientAccessCheckDone(ACCESS_ALLOWED
, http
);
147 http
->acl_checklist
= aclChecklistCreate(Config
.accessList
.http
,
152 * hack for ident ACL. It needs to get full addresses, and a
153 * place to store the ident result on persistent connections...
155 http
->acl_checklist
->conn
= conn
;
156 cbdataLock(http
->acl_checklist
->conn
);
158 aclNBCheck(http
->acl_checklist
, clientAccessCheckDone
, http
);
162 * returns true if client specified that the object must come from the cache
163 * witout contacting origin server
166 clientOnlyIfCached(clientHttpRequest
* http
)
168 const request_t
*r
= http
->request
;
170 return r
->cache_control
&&
171 EBIT_TEST(r
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
175 clientCreateStoreEntry(clientHttpRequest
* h
, method_t m
, request_flags flags
)
179 * For erroneous requests, we might not have a h->request,
180 * so make a fake one.
182 if (h
->request
== NULL
)
183 h
->request
= requestLink(requestCreate(m
, PROTO_NONE
, null_string
));
184 e
= storeCreateEntry(h
->uri
, h
->log_uri
, flags
, m
);
185 storeClientListAdd(e
, h
);
187 delaySetStoreClient(e
, h
, delayClient(h
->request
));
189 storeClientCopy(e
, 0, 0, CLIENT_SOCK_SZ
,
190 memAllocate(MEM_CLIENT_SOCK_BUF
), clientSendMoreData
, h
);
195 clientAccessCheckDone(int answer
, void *data
)
197 clientHttpRequest
*http
= data
;
200 ErrorState
*err
= NULL
;
201 debug(33, 2) ("The request %s %s is %s, because it matched '%s'\n",
202 RequestMethodStr
[http
->request
->method
], http
->uri
,
203 answer
== ACCESS_ALLOWED
? "ALLOWED" : "DENIED",
204 AclMatchedName
? AclMatchedName
: "NO ACL's");
205 http
->acl_checklist
= NULL
;
206 if (answer
== ACCESS_ALLOWED
) {
207 safe_free(http
->uri
);
208 http
->uri
= xstrdup(urlCanonical(http
->request
));
209 assert(http
->redirect_state
== REDIRECT_NONE
);
210 http
->redirect_state
= REDIRECT_PENDING
;
211 redirectStart(http
, clientRedirectDone
, http
);
213 debug(33, 5) ("Access Denied: %s\n", http
->uri
);
214 debug(33, 5) ("AclMatchedName = %s\n",
215 AclMatchedName
? AclMatchedName
: "<null>");
217 * NOTE: get page_id here, based on AclMatchedName because
218 * if USE_DELAY_POOLS is enabled, then AclMatchedName gets
219 * clobbered in the clientCreateStoreEntry() call
220 * just below. Pedro Ribeiro <pribeiro@isel.pt>
222 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
223 http
->log_type
= LOG_TCP_DENIED
;
224 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
226 if (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
)) {
227 if (!http
->flags
.accel
) {
228 /* Proxy authorisation needed */
229 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
231 /* WWW authorisation needed */
232 status
= HTTP_UNAUTHORIZED
;
235 page_id
= ERR_CACHE_ACCESS_DENIED
;
237 status
= HTTP_FORBIDDEN
;
239 page_id
= ERR_ACCESS_DENIED
;
241 err
= errorCon(page_id
, status
);
242 err
->request
= requestLink(http
->request
);
243 err
->src_addr
= http
->conn
->peer
.sin_addr
;
244 errorAppendEntry(http
->entry
, err
);
249 clientRedirectDone(void *data
, char *result
)
251 clientHttpRequest
*http
= data
;
252 request_t
*new_request
= NULL
;
253 request_t
*old_request
= http
->request
;
254 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
255 result
? result
: "NULL");
256 assert(http
->redirect_state
== REDIRECT_PENDING
);
257 http
->redirect_state
= REDIRECT_DONE
;
259 http_status status
= atoi(result
);
260 if (status
== 301 || status
== 302) {
262 if ((t
= strchr(result
, ':')) != NULL
) {
263 http
->redirect
.status
= status
;
264 http
->redirect
.location
= xstrdup(t
+ 1);
266 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result
);
269 if (strcmp(result
, http
->uri
))
270 new_request
= urlParse(old_request
->method
, result
);
273 safe_free(http
->uri
);
274 http
->uri
= xstrdup(urlCanonical(new_request
));
275 new_request
->http_ver
= old_request
->http_ver
;
276 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
277 new_request
->client_addr
= old_request
->client_addr
;
278 new_request
->my_addr
= old_request
->my_addr
;
279 new_request
->my_port
= old_request
->my_port
;
280 new_request
->flags
.redirected
= 1;
281 if (old_request
->body
) {
282 new_request
->body
= xmalloc(old_request
->body_sz
);
283 xmemcpy(new_request
->body
, old_request
->body
, old_request
->body_sz
);
284 new_request
->body_sz
= old_request
->body_sz
;
286 new_request
->content_length
= old_request
->content_length
;
287 requestUnlink(old_request
);
288 http
->request
= requestLink(new_request
);
290 clientInterpretRequestHeaders(http
);
292 headersLog(0, 1, request
->method
, request
);
294 fd_note(http
->conn
->fd
, http
->uri
);
295 clientProcessRequest(http
);
299 clientProcessExpired(void *data
)
301 clientHttpRequest
*http
= data
;
302 char *url
= http
->uri
;
303 StoreEntry
*entry
= NULL
;
304 debug(33, 3) ("clientProcessExpired: '%s'\n", http
->uri
);
305 assert(http
->entry
->lastmod
>= 0);
307 * check if we are allowed to contact other servers
308 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
309 * a stale entry *if* it matches client requirements
311 if (clientOnlyIfCached(http
)) {
312 clientProcessOnlyIfCachedMiss(http
);
315 http
->request
->flags
.refresh
= 1;
316 http
->old_entry
= http
->entry
;
318 * Assert that 'http' is already a client of old_entry. If
319 * it is not, then the beginning of the object data might get
320 * freed from memory before we need to access it.
322 assert(storeClientListSearch(http
->old_entry
->mem_obj
, http
));
323 entry
= storeCreateEntry(url
,
325 http
->request
->flags
,
326 http
->request
->method
);
327 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
328 storeClientListAdd(entry
, http
);
330 /* delay_id is already set on original store client */
331 delaySetStoreClient(entry
, http
, delayClient(http
->request
));
333 http
->request
->lastmod
= http
->old_entry
->lastmod
;
334 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry
->lastmod
);
336 http
->out
.offset
= 0;
337 fwdStart(http
->conn
->fd
, http
->entry
, http
->request
);
338 /* Register with storage manager to receive updates when data comes in. */
339 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
340 debug(33, 0) ("clientProcessExpired: found ENTRY_ABORTED object\n");
341 storeClientCopy(entry
,
345 memAllocate(MEM_CLIENT_SOCK_BUF
),
346 clientHandleIMSReply
,
351 clientGetsOldEntry(StoreEntry
* new_entry
, StoreEntry
* old_entry
, request_t
* request
)
353 const http_status status
= new_entry
->mem_obj
->reply
->sline
.status
;
355 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
358 /* If the reply is a failure then send the old object as a last
360 if (status
>= 500 && status
< 600) {
361 debug(33, 3) ("clientGetsOldEntry: YES, failure reply=%d\n", status
);
364 /* If the reply is anything but "Not Modified" then
365 * we must forward it to the client */
366 if (HTTP_NOT_MODIFIED
!= status
) {
367 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status
);
370 /* If the client did not send IMS in the request, then it
371 * must get the old object, not this "Not Modified" reply */
372 if (!request
->flags
.ims
) {
373 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
376 /* If the client IMS time is prior to the entry LASTMOD time we
377 * need to send the old object */
378 if (modifiedSince(old_entry
, request
)) {
379 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
383 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
389 clientHandleIMSReply(void *data
, char *buf
, ssize_t size
)
391 clientHttpRequest
*http
= data
;
392 StoreEntry
*entry
= http
->entry
;
394 const char *url
= storeUrl(entry
);
395 int unlink_request
= 0;
396 StoreEntry
*oldentry
;
399 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url
, (int) size
);
401 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
404 if (size
< 0 && !EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
405 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
408 mem
= entry
->mem_obj
;
409 status
= mem
->reply
->sline
.status
;
410 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
411 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url
);
412 /* We have an existing entry, but failed to validate it */
413 /* Its okay to send the old one anyway */
414 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
415 storeUnregister(entry
, http
);
416 storeUnlockObject(entry
);
417 entry
= http
->entry
= http
->old_entry
;
418 } else if (STORE_PENDING
== entry
->store_status
&& 0 == status
) {
419 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url
);
420 if (size
>= CLIENT_SOCK_SZ
) {
421 /* will not get any bigger than that */
422 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url
);
423 /* use old entry, this repeats the code abovez */
424 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
425 storeUnregister(entry
, http
);
426 storeUnlockObject(entry
);
427 entry
= http
->entry
= http
->old_entry
;
430 storeClientCopy(entry
,
431 http
->out
.offset
+ size
,
435 clientHandleIMSReply
,
439 } else if (clientGetsOldEntry(entry
, http
->old_entry
, http
->request
)) {
440 /* We initiated the IMS request, the client is not expecting
441 * 304, so put the good one back. First, make sure the old entry
442 * headers have been loaded from disk. */
443 oldentry
= http
->old_entry
;
444 http
->log_type
= LOG_TCP_REFRESH_HIT
;
445 if (oldentry
->mem_obj
->request
== NULL
) {
446 oldentry
->mem_obj
->request
= requestLink(mem
->request
);
449 /* Don't memcpy() the whole reply structure here. For example,
450 * www.thegist.com (Netscape/1.13) returns a content-length for
451 * 304's which seems to be the length of the 304 HEADERS!!! and
452 * not the body they refer to. */
453 httpReplyUpdateOnNotModified(oldentry
->mem_obj
->reply
, mem
->reply
);
454 storeTimestampsSet(oldentry
);
455 storeUnregister(entry
, http
);
456 storeUnlockObject(entry
);
457 entry
= http
->entry
= oldentry
;
458 entry
->timestamp
= squid_curtime
;
459 if (unlink_request
) {
460 requestUnlink(entry
->mem_obj
->request
);
461 entry
->mem_obj
->request
= NULL
;
464 /* the client can handle this reply, whatever it is */
465 http
->log_type
= LOG_TCP_REFRESH_MISS
;
466 if (HTTP_NOT_MODIFIED
== mem
->reply
->sline
.status
) {
467 httpReplyUpdateOnNotModified(http
->old_entry
->mem_obj
->reply
,
469 storeTimestampsSet(http
->old_entry
);
470 http
->log_type
= LOG_TCP_REFRESH_HIT
;
472 storeUnregister(http
->old_entry
, http
);
473 storeUnlockObject(http
->old_entry
);
476 http
->old_entry
= NULL
; /* done with old_entry */
477 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
479 storeClientCopy(entry
,
487 clientSendMoreData(data
, buf
, size
);
492 modifiedSince(StoreEntry
* entry
, request_t
* request
)
495 MemObject
*mem
= entry
->mem_obj
;
496 time_t mod_time
= entry
->lastmod
;
497 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry
));
499 mod_time
= entry
->timestamp
;
500 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time
);
503 /* Find size of the object */
504 object_length
= mem
->reply
->content_length
;
505 if (object_length
< 0)
506 object_length
= contentLen(entry
);
507 if (mod_time
> request
->ims
) {
508 debug(33, 3) ("--> YES: entry newer than client\n");
510 } else if (mod_time
< request
->ims
) {
511 debug(33, 3) ("--> NO: entry older than client\n");
513 } else if (request
->imslen
< 0) {
514 debug(33, 3) ("--> NO: same LMT, no client length\n");
516 } else if (request
->imslen
== object_length
) {
517 debug(33, 3) ("--> NO: same LMT, same length\n");
520 debug(33, 3) ("--> YES: same LMT, different length\n");
526 clientPurgeRequest(clientHttpRequest
* http
)
529 ErrorState
*err
= NULL
;
531 debug(33, 1) ("Config2.onoff.enable_purge = %d\n", Config2
.onoff
.enable_purge
);
532 if (!Config2
.onoff
.enable_purge
) {
533 http
->log_type
= LOG_TCP_DENIED
;
534 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
535 err
->request
= requestLink(http
->request
);
536 err
->src_addr
= http
->conn
->peer
.sin_addr
;
537 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
538 errorAppendEntry(http
->entry
, err
);
541 http
->log_type
= LOG_TCP_MISS
;
542 if ((entry
= storeGetPublic(http
->uri
, METHOD_GET
)) == NULL
) {
543 http
->http_code
= HTTP_NOT_FOUND
;
546 http
->http_code
= HTTP_OK
;
548 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
551 * Make a new entry to hold the reply to be written
554 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
555 httpReplyReset(r
= http
->entry
->mem_obj
->reply
);
556 httpReplySetHeaders(r
, 1.0, http
->http_code
, NULL
, NULL
, 0, 0, -1);
557 httpReplySwapOut(r
, http
->entry
);
558 storeComplete(http
->entry
);
562 checkNegativeHit(StoreEntry
* e
)
564 if (!EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
566 if (e
->expires
<= squid_curtime
)
568 if (e
->store_status
!= STORE_OK
)
574 clientUpdateCounters(clientHttpRequest
* http
)
576 int svc_time
= tvSubMsec(http
->start
, current_time
);
578 HierarchyLogEntry
*H
;
579 Counter
.client_http
.requests
++;
580 if (isTcpHit(http
->log_type
))
581 Counter
.client_http
.hits
++;
582 if (http
->log_type
== LOG_TCP_HIT
)
583 Counter
.client_http
.disk_hits
++;
584 else if (http
->log_type
== LOG_TCP_MEM_HIT
)
585 Counter
.client_http
.mem_hits
++;
586 if (http
->request
->err_type
!= ERR_NONE
)
587 Counter
.client_http
.errors
++;
588 statHistCount(&Counter
.client_http
.all_svc_time
, svc_time
);
590 * The idea here is not to be complete, but to get service times
591 * for only well-defined types. For example, we don't include
592 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
593 * (we *tried* to validate it, but failed).
595 switch (http
->log_type
) {
596 case LOG_TCP_REFRESH_HIT
:
597 statHistCount(&Counter
.client_http
.nh_svc_time
, svc_time
);
599 case LOG_TCP_IMS_HIT
:
600 statHistCount(&Counter
.client_http
.nm_svc_time
, svc_time
);
603 case LOG_TCP_MEM_HIT
:
604 case LOG_TCP_OFFLINE_HIT
:
605 statHistCount(&Counter
.client_http
.hit_svc_time
, svc_time
);
608 case LOG_TCP_CLIENT_REFRESH_MISS
:
609 statHistCount(&Counter
.client_http
.miss_svc_time
, svc_time
);
612 /* make compiler warnings go away */
615 H
= &http
->request
->hier
;
618 Counter
.cd
.times_used
++;
621 Counter
.icp
.times_used
++;
623 if (0 != i
->stop
.tv_sec
&& 0 != i
->start
.tv_sec
)
624 statHistCount(&Counter
.icp
.query_svc_time
,
625 tvSubUsec(i
->start
, i
->stop
));
627 Counter
.icp
.query_timeouts
++;
630 Counter
.netdb
.times_used
++;
638 httpRequestFree(void *data
)
640 clientHttpRequest
*http
= data
;
641 clientHttpRequest
**H
;
642 ConnStateData
*conn
= http
->conn
;
644 request_t
*request
= http
->request
;
645 MemObject
*mem
= NULL
;
646 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(http
->entry
));
647 if (!clientCheckTransferDone(http
)) {
648 if ((e
= http
->entry
)) {
650 storeUnregister(e
, http
);
651 storeUnlockObject(e
);
653 if (http
->entry
&& http
->entry
->ping_status
== PING_WAITING
)
654 storeReleaseRequest(http
->entry
);
656 assert(http
->log_type
< LOG_TYPE_MAX
);
658 mem
= http
->entry
->mem_obj
;
659 if (http
->out
.size
|| http
->log_type
) {
660 http
->al
.icp
.opcode
= ICP_INVALID
;
661 http
->al
.url
= http
->log_uri
;
662 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http
->al
.url
);
664 http
->al
.http
.code
= mem
->reply
->sline
.status
;
665 http
->al
.http
.content_type
= strBuf(mem
->reply
->content_type
);
667 http
->al
.cache
.caddr
= conn
->log_addr
;
668 http
->al
.cache
.size
= http
->out
.size
;
669 http
->al
.cache
.code
= http
->log_type
;
670 http
->al
.cache
.msec
= tvSubMsec(http
->start
, current_time
);
675 packerToMemInit(&p
, &mb
);
676 httpHeaderPackInto(&request
->header
, &p
);
677 http
->al
.http
.method
= request
->method
;
678 http
->al
.http
.version
= request
->http_ver
;
679 http
->al
.headers
.request
= xstrdup(mb
.buf
);
680 http
->al
.hier
= request
->hier
;
681 if (request
->user_ident
[0])
682 http
->al
.cache
.ident
= request
->user_ident
;
684 http
->al
.cache
.ident
= conn
->ident
;
688 accessLogLog(&http
->al
);
689 clientUpdateCounters(http
);
690 clientdbUpdate(conn
->peer
.sin_addr
, http
->log_type
, PROTO_HTTP
, http
->out
.size
);
692 if (http
->acl_checklist
)
693 aclChecklistFree(http
->acl_checklist
);
695 checkFailureRatio(request
->err_type
, http
->al
.hier
.code
);
696 safe_free(http
->uri
);
697 safe_free(http
->log_uri
);
698 safe_free(http
->al
.headers
.request
);
699 safe_free(http
->al
.headers
.reply
);
700 safe_free(http
->redirect
.location
);
701 stringClean(&http
->range_iter
.boundary
);
702 if ((e
= http
->entry
)) {
704 storeUnregister(e
, http
);
705 storeUnlockObject(e
);
707 /* old_entry might still be set if we didn't yet get the reply
708 * code in clientHandleIMSReply() */
709 if ((e
= http
->old_entry
)) {
710 http
->old_entry
= NULL
;
711 storeUnregister(e
, http
);
712 storeUnlockObject(e
);
714 requestUnlink(http
->request
);
715 assert(http
!= http
->next
);
716 assert(http
->conn
->chr
!= NULL
);
717 H
= &http
->conn
->chr
;
726 dlinkDelete(&http
->active
, &ClientActiveRequests
);
730 /* This is a handler normally called by comm_close() */
732 connStateFree(int fd
, void *data
)
734 ConnStateData
*connState
= data
;
735 clientHttpRequest
*http
;
736 debug(33, 3) ("connStateFree: FD %d\n", fd
);
737 assert(connState
!= NULL
);
738 clientdbEstablished(connState
->peer
.sin_addr
, -1); /* decrement */
739 while ((http
= connState
->chr
) != NULL
) {
740 assert(http
->conn
== connState
);
741 assert(connState
->chr
!= connState
->chr
->next
);
742 httpRequestFree(http
);
744 safe_free(connState
->in
.buf
);
745 /* XXX account connState->in.buf */
746 pconnHistCount(0, connState
->nrequests
);
747 cbdataFree(connState
);
749 /* prevent those nasty RST packets */
751 char buf
[SQUID_TCP_SO_RCVBUF
];
752 while (read(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0);
758 clientInterpretRequestHeaders(clientHttpRequest
* http
)
760 request_t
*request
= http
->request
;
761 const HttpHeader
*req_hdr
= &request
->header
;
763 #if USE_USERAGENT_LOG
766 request
->imslen
= -1;
767 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
768 if (request
->ims
> 0)
769 request
->flags
.ims
= 1;
770 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
771 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
772 if (strListIsMember(&s
, "no-cache", ','))
776 request
->cache_control
= httpHeaderGetCc(req_hdr
);
777 if (request
->cache_control
)
778 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
782 if (Config
.onoff
.reload_into_ims
)
783 request
->flags
.nocache_hack
= 1;
784 else if (refresh_nocache_hack
)
785 request
->flags
.nocache_hack
= 1;
788 request
->flags
.nocache
= 1;
790 /* ignore range header in non-GETs */
791 if (request
->method
== METHOD_GET
) {
792 request
->range
= httpHeaderGetRange(req_hdr
);
794 request
->flags
.range
= 1;
796 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
797 request
->flags
.auth
= 1;
798 if (request
->login
[0] != '\0')
799 request
->flags
.auth
= 1;
800 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
801 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
803 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
804 * Note ThisCache2 has a space prepended to the hostname so we don't
805 * accidentally match super-domains.
807 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
808 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
809 request
, (ObjPackMethod
) & httpRequestPack
);
810 request
->flags
.loopdetect
= 1;
813 fvdbCountVia(strBuf(s
));
817 #if USE_USERAGENT_LOG
818 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
819 logUserAgent(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
);
822 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
823 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
824 fvdbCountForw(strBuf(s
));
828 if (request
->method
== METHOD_TRACE
) {
829 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
831 if (clientCachable(http
))
832 request
->flags
.cachable
= 1;
833 if (clientHierarchical(http
))
834 request
->flags
.hierarchical
= 1;
835 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
836 request
->flags
.nocache
? "SET" : "NOT SET");
837 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
838 request
->flags
.cachable
? "SET" : "NOT SET");
839 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
840 request
->flags
.hierarchical
? "SET" : "NOT SET");
844 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
845 * This is the client-side persistent connection flag. We need
846 * to set this relatively early in the request processing
847 * to handle hacks for broken servers and clients.
850 clientSetKeepaliveFlag(clientHttpRequest
* http
)
852 request_t
*request
= http
->request
;
853 const HttpHeader
*req_hdr
= &request
->header
;
854 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
856 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
857 RequestMethodStr
[request
->method
]);
858 if (!Config
.onoff
.client_pconns
)
859 request
->flags
.proxy_keepalive
= 0;
860 else if (httpMsgIsPersistent(request
->http_ver
, req_hdr
))
861 request
->flags
.proxy_keepalive
= 1;
865 clientCheckContentLength(request_t
* r
)
867 int has_cont_len
= (r
->content_length
>= 0);
871 /* PUT/POST requires a request entity */
875 /* We do not want to see a request entity on GET/HEAD requests */
876 return !has_cont_len
;
878 /* For other types of requests we don't care */
885 clientCachable(clientHttpRequest
* http
)
887 const char *url
= http
->uri
;
888 request_t
*req
= http
->request
;
889 method_t method
= req
->method
;
891 memset(&ch
, '\0', sizeof(ch
));
893 * Hopefully, nobody really wants 'no_cache' by client's IP
894 * address, but if they do, this should work if they use IP
895 * addresses in their ACLs, or if the client's address is in
898 * This may not work yet for 'dst' and 'dst_domain' ACLs.
900 ch
.src_addr
= http
->conn
->peer
.sin_addr
;
901 ch
.my_addr
= http
->conn
->me
.sin_addr
;
902 ch
.my_port
= ntohs(http
->conn
->me
.sin_port
);
903 ch
.request
= http
->request
;
905 * aclCheckFast returns 1 for ALLOW and 0 for DENY. The default
906 * is ALLOW, so we require 'no_cache DENY foo' in squid.conf
907 * to indicate uncachable objects.
909 if (Config
.accessList
.noCache
)
910 if (!aclCheckFast(Config
.accessList
.noCache
, &ch
))
912 if (req
->protocol
== PROTO_HTTP
)
913 return httpCachable(method
);
914 /* FTP is always cachable */
915 if (req
->protocol
== PROTO_GOPHER
)
916 return gopherCachable(url
);
917 if (req
->protocol
== PROTO_WAIS
)
919 if (method
== METHOD_CONNECT
)
921 if (method
== METHOD_TRACE
)
923 if (req
->protocol
== PROTO_CACHEOBJ
)
928 /* Return true if we can query our neighbors for this object */
930 clientHierarchical(clientHttpRequest
* http
)
932 const char *url
= http
->uri
;
933 request_t
*request
= http
->request
;
934 method_t method
= request
->method
;
935 const wordlist
*p
= NULL
;
937 /* IMS needs a private key, so we can use the hierarchy for IMS only
938 * if our neighbors support private keys */
939 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
941 if (request
->flags
.auth
)
943 if (method
== METHOD_TRACE
)
945 if (method
!= METHOD_GET
)
947 /* scan hierarchy_stoplist */
948 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
949 if (strstr(url
, p
->key
))
951 if (request
->flags
.loopdetect
)
953 if (request
->protocol
== PROTO_HTTP
)
954 return httpCachable(method
);
955 if (request
->protocol
== PROTO_GOPHER
)
956 return gopherCachable(url
);
957 if (request
->protocol
== PROTO_WAIS
)
959 if (request
->protocol
== PROTO_CACHEOBJ
)
965 isTcpHit(log_type code
)
967 /* this should be a bitmap for better optimization */
968 if (code
== LOG_TCP_HIT
)
970 if (code
== LOG_TCP_IMS_HIT
)
972 if (code
== LOG_TCP_REFRESH_FAIL_HIT
)
974 if (code
== LOG_TCP_REFRESH_HIT
)
976 if (code
== LOG_TCP_NEGATIVE_HIT
)
978 if (code
== LOG_TCP_MEM_HIT
)
980 if (code
== LOG_TCP_OFFLINE_HIT
)
986 * returns true if If-Range specs match reply, false otherwise
989 clientIfRangeMatch(clientHttpRequest
* http
, HttpReply
* rep
)
991 const TimeOrTag spec
= httpHeaderGetTimeOrTag(&http
->request
->header
, HDR_IF_RANGE
);
992 /* check for parsing falure */
997 ETag rep_tag
= httpHeaderGetETag(&rep
->header
, HDR_ETAG
);
998 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
999 spec
.tag
.str
, rep_tag
.str
? rep_tag
.str
: "<none>");
1001 return 0; /* entity has no etag to compare with! */
1002 if (spec
.tag
.weak
|| rep_tag
.weak
) {
1003 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
1004 spec
.tag
.str
, rep_tag
.str
);
1005 return 0; /* must use strong validator for sub-range requests */
1007 return etagIsEqual(&rep_tag
, &spec
.tag
);
1009 /* got modification time? */
1010 if (spec
.time
>= 0) {
1011 return http
->entry
->lastmod
<= spec
.time
;
1013 assert(0); /* should not happen */
1017 /* returns expected content length for multi-range replies
1018 * note: assumes that httpHdrRangeCanonize has already been called
1019 * warning: assumes that HTTP headers for individual ranges at the
1020 * time of the actuall assembly will be exactly the same as
1021 * the headers when clientMRangeCLen() is called */
1023 clientMRangeCLen(clientHttpRequest
* http
)
1026 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1027 const HttpHdrRangeSpec
*spec
;
1030 assert(http
->entry
->mem_obj
);
1033 while ((spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
))) {
1035 /* account for headers for this range */
1037 clientPackRangeHdr(http
->entry
->mem_obj
->reply
,
1038 spec
, http
->range_iter
.boundary
, &mb
);
1041 /* account for range content */
1042 clen
+= spec
->length
;
1044 debug(33, 6) ("clientMRangeCLen: (clen += %d + %d) == %d\n",
1045 mb
.size
, spec
->length
, clen
);
1047 /* account for the terminating boundary */
1049 clientPackTermBound(http
->range_iter
.boundary
, &mb
);
1056 /* adds appropriate Range headers if needed */
1058 clientBuildRangeHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1060 HttpHeader
*hdr
= rep
? &rep
->header
: 0;
1061 const char *range_err
= NULL
;
1062 assert(http
->request
->range
);
1063 /* check if we still want to do ranges */
1065 range_err
= "no [parse-able] reply";
1066 else if (rep
->sline
.status
!= HTTP_OK
)
1067 range_err
= "wrong status code";
1068 else if (httpHeaderHas(hdr
, HDR_CONTENT_RANGE
))
1069 range_err
= "origin server does ranges";
1070 else if (rep
->content_length
< 0)
1071 range_err
= "unknown length";
1072 else if (rep
->content_length
!= http
->entry
->mem_obj
->reply
->content_length
)
1073 range_err
= "INCONSISTENT length"; /* a bug? */
1074 else if (httpHeaderHas(&http
->request
->header
, HDR_IF_RANGE
) && !clientIfRangeMatch(http
, rep
))
1075 range_err
= "If-Range match failed";
1076 else if (!httpHdrRangeCanonize(http
->request
->range
, rep
->content_length
))
1077 range_err
= "canonization failed";
1078 else if (httpHdrRangeIsComplex(http
->request
->range
))
1079 range_err
= "too complex range header";
1080 /* get rid of our range specs on error */
1082 debug(33, 3) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err
);
1083 httpHdrRangeDestroy(http
->request
->range
);
1084 http
->request
->range
= NULL
;
1086 const int spec_count
= http
->request
->range
->specs
.count
;
1087 int actual_clen
= -1;
1089 debug(33, 3) ("clientBuildRangeHeader: range spec count: %d virgin clen: %d\n",
1090 spec_count
, rep
->content_length
);
1091 assert(spec_count
> 0);
1092 /* ETags should not be returned with Partial Content replies? */
1093 httpHeaderDelById(hdr
, HDR_ETAG
);
1094 /* append appropriate header(s) */
1095 if (spec_count
== 1) {
1096 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1097 const HttpHdrRangeSpec
*spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
);
1099 /* append Content-Range */
1100 httpHeaderAddContRange(hdr
, *spec
, rep
->content_length
);
1101 /* set new Content-Length to the actual number of bytes
1102 * transmitted in the message-body */
1103 actual_clen
= spec
->length
;
1106 /* generate boundary string */
1107 http
->range_iter
.boundary
= httpHdrRangeBoundaryStr(http
);
1108 /* delete old Content-Type, add ours */
1109 httpHeaderDelById(hdr
, HDR_CONTENT_TYPE
);
1110 httpHeaderPutStrf(hdr
, HDR_CONTENT_TYPE
,
1111 "multipart/byteranges; boundary=\"%s\"",
1112 strBuf(http
->range_iter
.boundary
));
1113 /* Content-Length is not required in multipart responses
1114 * but it is always nice to have one */
1115 actual_clen
= clientMRangeCLen(http
);
1118 /* replace Content-Length header */
1119 assert(actual_clen
>= 0);
1120 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1121 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, actual_clen
);
1122 debug(33, 3) ("clientBuildRangeHeader: actual content length: %d\n", actual_clen
);
1127 * filters out unwanted entries from original reply header
1128 * adds extra entries if we have more info than origin server
1129 * adds Squid specific entries
1132 clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1134 HttpHeader
*hdr
= &rep
->header
;
1135 int is_hit
= isTcpHit(http
->log_type
);
1136 request_t
*request
= http
->request
;
1137 #if DONT_FILTER_THESE
1138 /* but you might want to if you run Squid as an HTTP accelerator */
1139 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1140 httpHeaderDelById(hdr
, HDR_ETAG
);
1142 httpHeaderDelById(hdr
, HDR_PROXY_CONNECTION
);
1143 /* here: Keep-Alive is a field-name, not a connection directive! */
1144 httpHeaderDelByName(hdr
, "Keep-Alive");
1145 /* remove Set-Cookie if a hit */
1147 httpHeaderDelById(hdr
, HDR_SET_COOKIE
);
1148 /* handle Connection header */
1149 if (httpHeaderHas(hdr
, HDR_CONNECTION
)) {
1150 /* anything that matches Connection list member will be deleted */
1151 String strConnection
= httpHeaderGetList(hdr
, HDR_CONNECTION
);
1152 const HttpHeaderEntry
*e
;
1153 HttpHeaderPos pos
= HttpHeaderInitPos
;
1155 * think: on-average-best nesting of the two loops (hdrEntry
1156 * and strListItem) @?@
1159 * maybe we should delete standard stuff ("keep-alive","close")
1160 * from strConnection first?
1162 while ((e
= httpHeaderGetEntry(hdr
, &pos
))) {
1163 if (strListIsMember(&strConnection
, strBuf(e
->name
), ','))
1164 httpHeaderDelAt(hdr
, pos
);
1166 httpHeaderDelById(hdr
, HDR_CONNECTION
);
1167 stringClean(&strConnection
);
1171 clientBuildRangeHeader(http
, rep
);
1173 * Add a estimated Age header on cache hits.
1177 * Remove any existing Age header sent by upstream caches
1178 * (note that the existing header is passed along unmodified
1181 httpHeaderDelById(hdr
, HDR_AGE
);
1183 * This adds the calculated object age. Note that the details of the
1184 * age calculation is performed by adjusting the timestamp in
1185 * storeTimestampsSet(), not here.
1187 * BROWSER WORKAROUND: IE sometimes hangs when receiving a 0 Age
1188 * header, so don't use it unless there is a age to report. Please
1189 * note that Age is only used to make a conservative estimation of
1190 * the objects age, so a Age: 0 header does not add any useful
1191 * information to the reply in any case.
1193 if (http
->entry
->timestamp
< squid_curtime
)
1194 httpHeaderPutInt(hdr
, HDR_AGE
,
1195 squid_curtime
- http
->entry
->timestamp
);
1197 /* Append X-Cache */
1198 httpHeaderPutStrf(hdr
, HDR_X_CACHE
, "%s from %s",
1199 is_hit
? "HIT" : "MISS", getMyHostname());
1200 #if USE_CACHE_DIGESTS
1201 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1202 httpHeaderPutStrf(hdr
, HDR_X_CACHE_LOOKUP
, "%s from %s:%d",
1203 http
->lookup_type
? http
->lookup_type
: "NONE",
1204 getMyHostname(), ntohs(Config
.Sockaddr
.http
->s
.sin_port
));
1206 if (httpReplyBodySize(request
->method
, rep
) < 0) {
1207 debug(33, 3) ("clientBuildReplyHeader: can't keep-alive, unknown body size\n");
1208 request
->flags
.proxy_keepalive
= 0;
1210 /* Signal keep-alive if needed */
1211 httpHeaderPutStr(hdr
,
1212 http
->flags
.accel
? HDR_CONNECTION
: HDR_PROXY_CONNECTION
,
1213 request
->flags
.proxy_keepalive
? "keep-alive" : "close");
1214 #if ADD_X_REQUEST_URI
1216 * Knowing the URI of the request is useful when debugging persistent
1217 * connections in a client; we cannot guarantee the order of http headers,
1218 * but X-Request-URI is likely to be the very last header to ease use from a
1219 * debugger [hdr->entries.count-1].
1221 httpHeaderPutStr(hdr
, HDR_X_REQUEST_URI
,
1222 http
->entry
->mem_obj
->url
? http
->entry
->mem_obj
->url
: http
->uri
);
1227 clientBuildReply(clientHttpRequest
* http
, const char *buf
, size_t size
)
1229 HttpReply
*rep
= httpReplyCreate();
1230 size_t k
= headersEnd(buf
, size
);
1231 if (k
&& httpReplyParse(rep
, buf
, k
)) {
1232 /* enforce 1.0 reply version */
1233 rep
->sline
.version
= 1.0;
1234 /* do header conversions */
1235 clientBuildReplyHeader(http
, rep
);
1236 /* if we do ranges, change status to "Partial Content" */
1237 if (http
->request
->range
)
1238 httpStatusLineSet(&rep
->sline
, rep
->sline
.version
,
1239 HTTP_PARTIAL_CONTENT
, NULL
);
1241 /* parsing failure, get rid of the invalid reply */
1242 httpReplyDestroy(rep
);
1244 /* if we were going to do ranges, backoff */
1245 if (http
->request
->range
) {
1246 /* this will fail and destroy request->range */
1247 clientBuildRangeHeader(http
, rep
);
1254 * clientCacheHit should only be called until the HTTP reply headers
1255 * have been parsed. Normally this should be a single call, but
1256 * it might take more than one. As soon as we have the headers,
1257 * we hand off to clientSendMoreData, clientProcessExpired, or
1258 * clientProcessMiss.
1261 clientCacheHit(void *data
, char *buf
, ssize_t size
)
1263 clientHttpRequest
*http
= data
;
1264 StoreEntry
*e
= http
->entry
;
1266 request_t
*r
= http
->request
;
1267 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http
->uri
, (int) size
);
1268 if (http
->entry
== NULL
) {
1269 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1270 debug(33, 3) ("clientCacheHit: request aborted\n");
1272 } else if (size
< 0) {
1273 /* swap in failure */
1274 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1275 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http
->uri
);
1276 http
->log_type
= LOG_TCP_SWAPFAIL_MISS
;
1277 if ((e
= http
->entry
)) {
1279 storeUnregister(e
, http
);
1280 storeUnlockObject(e
);
1282 clientProcessMiss(http
);
1287 assert(!EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
1288 if (mem
->reply
->sline
.status
== 0) {
1290 * we don't have full reply headers yet; either wait for more or
1291 * punt to clientProcessMiss.
1293 if (e
->mem_status
== IN_MEMORY
|| e
->store_status
== STORE_OK
) {
1294 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1295 clientProcessMiss(http
);
1296 } else if (size
== CLIENT_SOCK_SZ
&& http
->out
.offset
== 0) {
1297 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1298 clientProcessMiss(http
);
1300 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1302 http
->out
.offset
+ size
,
1312 * Got the headers, now grok them
1314 assert(http
->log_type
== LOG_TCP_HIT
);
1315 if (checkNegativeHit(e
)) {
1316 http
->log_type
= LOG_TCP_NEGATIVE_HIT
;
1317 clientSendMoreData(data
, buf
, size
);
1318 } else if (r
->method
== METHOD_HEAD
) {
1320 * RFC 2068 seems to indicate there is no "conditional HEAD"
1321 * request. We cannot validate a cached object for a HEAD
1322 * request, nor can we return 304.
1324 if (e
->mem_status
== IN_MEMORY
)
1325 http
->log_type
= LOG_TCP_MEM_HIT
;
1326 clientSendMoreData(data
, buf
, size
);
1327 } else if (refreshCheckHTTP(e
, r
) && !http
->flags
.internal
) {
1328 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1330 * We hold a stale copy; it needs to be validated
1333 * The 'need_validation' flag is used to prevent forwarding
1334 * loops between siblings. If our copy of the object is stale,
1335 * then we should probably only use parents for the validation
1336 * request. Otherwise two siblings could generate a loop if
1337 * both have a stale version of the object.
1339 r
->flags
.need_validation
= 1;
1340 if (e
->lastmod
< 0) {
1342 * Previous reply didn't have a Last-Modified header,
1343 * we cannot revalidate it.
1345 http
->log_type
= LOG_TCP_MISS
;
1346 clientProcessMiss(http
);
1347 } else if (r
->flags
.nocache
) {
1349 * This did not match a refresh pattern that overrides no-cache
1350 * we should honour the client no-cache header.
1352 http
->log_type
= LOG_TCP_CLIENT_REFRESH_MISS
;
1353 clientProcessMiss(http
);
1354 } else if (r
->protocol
== PROTO_HTTP
) {
1356 * Object needs to be revalidated
1357 * XXX This could apply to FTP as well, if Last-Modified is known.
1359 http
->log_type
= LOG_TCP_REFRESH_MISS
;
1360 clientProcessExpired(http
);
1363 * We don't know how to re-validate other protocols. Handle
1364 * them as if the object has expired.
1366 http
->log_type
= LOG_TCP_MISS
;
1367 clientProcessMiss(http
);
1369 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1370 } else if (r
->flags
.ims
) {
1372 * Handle If-Modified-Since requests from the client
1374 if (mem
->reply
->sline
.status
!= HTTP_OK
) {
1375 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1376 mem
->reply
->sline
.status
);
1377 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1378 http
->log_type
= LOG_TCP_MISS
;
1379 clientProcessMiss(http
);
1380 } else if (modifiedSince(e
, http
->request
)) {
1381 http
->log_type
= LOG_TCP_IMS_HIT
;
1382 clientSendMoreData(data
, buf
, size
);
1384 MemBuf mb
= httpPacked304Reply(e
->mem_obj
->reply
);
1385 http
->log_type
= LOG_TCP_IMS_HIT
;
1386 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1387 storeUnregister(e
, http
);
1388 storeUnlockObject(e
);
1389 e
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
1391 httpReplyParse(e
->mem_obj
->reply
, mb
.buf
, mb
.size
);
1392 storeAppend(e
, mb
.buf
, mb
.size
);
1398 * plain ol' cache hit
1400 if (e
->mem_status
== IN_MEMORY
)
1401 http
->log_type
= LOG_TCP_MEM_HIT
;
1402 else if (Config
.onoff
.offline
)
1403 http
->log_type
= LOG_TCP_OFFLINE_HIT
;
1404 clientSendMoreData(data
, buf
, size
);
1408 /* put terminating boundary for multiparts */
1410 clientPackTermBound(String boundary
, MemBuf
* mb
)
1412 memBufPrintf(mb
, "\r\n--%s--\r\n", strBuf(boundary
));
1413 debug(33, 6) ("clientPackTermBound: buf offset: %d\n", mb
->size
);
1416 /* appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */
1418 clientPackRangeHdr(const HttpReply
* rep
, const HttpHdrRangeSpec
* spec
, String boundary
, MemBuf
* mb
)
1426 debug(33, 5) ("clientPackRangeHdr: appending boundary: %s\n", strBuf(boundary
));
1427 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1428 memBufPrintf(mb
, "\r\n--%s\r\n", strBuf(boundary
));
1430 /* stuff the header with required entries and pack it */
1431 httpHeaderInit(&hdr
, hoReply
);
1432 if (httpHeaderHas(&rep
->header
, HDR_CONTENT_TYPE
))
1433 httpHeaderPutStr(&hdr
, HDR_CONTENT_TYPE
, httpHeaderGetStr(&rep
->header
, HDR_CONTENT_TYPE
));
1434 httpHeaderAddContRange(&hdr
, *spec
, rep
->content_length
);
1435 packerToMemInit(&p
, mb
);
1436 httpHeaderPackInto(&hdr
, &p
);
1438 httpHeaderClean(&hdr
);
1440 /* append <crlf> (we packed a header, not a reply) */
1441 memBufPrintf(mb
, crlf
);
1445 * extracts a "range" from *buf and appends them to mb, updating
1446 * all offsets and such.
1449 clientPackRange(clientHttpRequest
* http
,
1450 HttpHdrRangeIter
* i
,
1455 const ssize_t copy_sz
= i
->debt_size
<= *size
? i
->debt_size
: *size
;
1456 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1460 * intersection of "have" and "need" ranges must not be empty
1462 assert(body_off
< i
->spec
->offset
+ i
->spec
->length
);
1463 assert(body_off
+ *size
> i
->spec
->offset
);
1465 * put boundary and headers at the beginning of a range in a
1468 if (http
->request
->range
->specs
.count
> 1 && i
->debt_size
== i
->spec
->length
) {
1469 assert(http
->entry
->mem_obj
);
1471 http
->entry
->mem_obj
->reply
, /* original reply */
1472 i
->spec
, /* current range */
1473 i
->boundary
, /* boundary, the same for all */
1480 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz
);
1481 memBufAppend(mb
, *buf
, copy_sz
);
1486 i
->debt_size
-= copy_sz
;
1487 body_off
+= copy_sz
;
1489 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1493 assert(*size
>= 0 && i
->debt_size
>= 0);
1496 /* returns true if there is still data available to pack more ranges
1497 * increments iterator "i"
1498 * used by clientPackMoreRanges */
1500 clientCanPackMoreRanges(const clientHttpRequest
* http
, HttpHdrRangeIter
* i
, ssize_t size
)
1502 /* first update "i" if needed */
1503 if (!i
->debt_size
) {
1504 if ((i
->spec
= httpHdrRangeGetSpec(http
->request
->range
, &i
->pos
)))
1505 i
->debt_size
= i
->spec
->length
;
1507 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1508 /* continue condition: need_more_data && have_more_data */
1509 return i
->spec
&& size
> 0;
1512 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1513 /* returns true if we need more data */
1515 clientPackMoreRanges(clientHttpRequest
* http
, const char *buf
, ssize_t size
, MemBuf
* mb
)
1517 HttpHdrRangeIter
*i
= &http
->range_iter
;
1518 /* offset in range specs does not count the prefix of an http msg */
1519 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1521 /* check: reply was parsed and range iterator was initialized */
1522 assert(i
->prefix_size
> 0);
1523 /* filter out data according to range specs */
1524 while (clientCanPackMoreRanges(http
, i
, size
)) {
1525 off_t start
; /* offset of still missing data */
1527 start
= i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
;
1528 debug(33, 3) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1529 (int) body_off
, size
);
1530 debug(33, 3) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1531 (int) start
, (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
, i
->debt_size
);
1532 assert(body_off
<= start
); /* we did not miss it */
1533 /* skip up to start */
1534 if (body_off
+ size
> start
) {
1535 const size_t skip_size
= start
- body_off
;
1540 /* has not reached start yet */
1545 /* put next chunk if any */
1547 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1548 clientPackRange(http
, i
, &buf
, &size
, mb
);
1549 body_off
= http
->out
.offset
- i
->prefix_size
; /* sync */
1552 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1553 debug(33, 3) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1554 (int) body_off
, size
, i
->debt_size
);
1556 debug(33, 3) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1557 (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
);
1558 /* skip the data we do not need if possible */
1559 if (i
->debt_size
== i
->spec
->length
) /* at the start of the cur. spec */
1560 body_off
= i
->spec
->offset
;
1562 assert(body_off
== i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
);
1563 } else if (http
->request
->range
->specs
.count
> 1) {
1564 /* put terminating boundary for multiparts */
1565 clientPackTermBound(i
->boundary
, mb
);
1567 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1568 return i
->debt_size
> 0;
1572 clientReplyBodyTooLarge(int clen
)
1574 if (0 == Config
.maxReplyBodySize
)
1575 return 0; /* disabled */
1577 return 0; /* unknown */
1578 if (clen
> Config
.maxReplyBodySize
)
1579 return 1; /* too large */
1584 clientRequestBodyTooLarge(int clen
)
1586 if (0 == Config
.maxRequestBodySize
)
1587 return 0; /* disabled */
1589 return 0; /* unknown, bug? */
1590 if (clen
> Config
.maxRequestBodySize
)
1591 return 1; /* too large */
1596 * accepts chunk of a http message in buf, parses prefix, filters headers and
1597 * such, writes processed message to the client's socket
1600 clientSendMoreData(void *data
, char *buf
, ssize_t size
)
1602 clientHttpRequest
*http
= data
;
1603 StoreEntry
*entry
= http
->entry
;
1604 ConnStateData
*conn
= http
->conn
;
1606 HttpReply
*rep
= NULL
;
1607 const char *body_buf
= buf
;
1608 ssize_t body_size
= size
;
1610 ssize_t check_size
= 0;
1611 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http
->uri
, (int) size
);
1612 assert(size
<= CLIENT_SOCK_SZ
);
1613 assert(http
->request
!= NULL
);
1614 dlinkDelete(&http
->active
, &ClientActiveRequests
);
1615 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1616 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1617 fd
, storeUrl(entry
), (int) http
->out
.offset
);
1618 if (conn
->chr
!= http
) {
1619 /* there is another object in progress, defer this one */
1620 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry
));
1621 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1623 } else if (entry
&& EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1624 /* call clientWriteComplete so the client socket gets closed */
1625 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1626 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1628 } else if (size
< 0) {
1629 /* call clientWriteComplete so the client socket gets closed */
1630 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1631 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1633 } else if (size
== 0) {
1634 /* call clientWriteComplete so the client socket gets closed */
1635 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1636 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1639 if (http
->out
.offset
== 0) {
1640 if (Config
.onoff
.log_mime_hdrs
) {
1642 if ((k
= headersEnd(buf
, size
))) {
1643 safe_free(http
->al
.headers
.reply
);
1644 http
->al
.headers
.reply
= xcalloc(k
+ 1, 1);
1645 xstrncpy(http
->al
.headers
.reply
, buf
, k
);
1648 rep
= clientBuildReply(http
, buf
, size
);
1649 if (rep
&& clientReplyBodyTooLarge(rep
->content_length
)) {
1650 ErrorState
*err
= errorCon(ERR_TOO_BIG
, HTTP_FORBIDDEN
);
1651 err
->request
= requestLink(http
->request
);
1652 storeUnregister(http
->entry
, http
);
1653 storeUnlockObject(http
->entry
);
1654 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
1655 null_request_flags
);
1656 errorAppendEntry(http
->entry
, err
);
1657 httpReplyDestroy(rep
);
1660 body_size
= size
- rep
->hdr_sz
;
1661 assert(body_size
>= 0);
1662 body_buf
= buf
+ rep
->hdr_sz
;
1663 http
->range_iter
.prefix_size
= rep
->hdr_sz
;
1664 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1665 body_size
, rep
->hdr_sz
);
1666 } else if (size
< CLIENT_SOCK_SZ
&& entry
->store_status
== STORE_PENDING
) {
1667 /* wait for more to arrive */
1668 storeClientCopy(entry
,
1669 http
->out
.offset
+ size
,
1677 /* reset range iterator */
1678 http
->range_iter
.pos
= HttpHdrRangeInitPos
;
1680 if (http
->request
->method
== METHOD_HEAD
) {
1682 /* do not forward body for HEAD replies */
1684 http
->flags
.done_copying
= 1;
1687 * If we are here, then store_status == STORE_OK and it
1688 * seems we have a HEAD repsponse which is missing the
1689 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1690 * does this). Because clientBuildReply() fails we just
1691 * call this reply a body, set the done_copying flag and
1694 http
->flags
.done_copying
= 1;
1697 /* write headers and/or body if any */
1698 assert(rep
|| (body_buf
&& body_size
));
1699 /* init mb; put status line and headers if any */
1701 mb
= httpReplyPack(rep
);
1702 http
->out
.offset
+= rep
->hdr_sz
;
1703 check_size
+= rep
->hdr_sz
;
1705 headersLog(0, 0, http
->request
->method
, rep
);
1707 httpReplyDestroy(rep
);
1712 /* append body if any */
1713 if (http
->request
->range
) {
1714 /* Only GET requests should have ranges */
1715 assert(http
->request
->method
== METHOD_GET
);
1716 /* clientPackMoreRanges() updates http->out.offset */
1717 /* force the end of the transfer if we are done */
1718 if (!clientPackMoreRanges(http
, body_buf
, body_size
, &mb
))
1719 http
->flags
.done_copying
= 1;
1720 } else if (body_buf
&& body_size
) {
1721 http
->out
.offset
+= body_size
;
1722 check_size
+= body_size
;
1723 memBufAppend(&mb
, body_buf
, body_size
);
1725 if (!http
->request
->range
&& http
->request
->method
== METHOD_GET
)
1726 assert(check_size
== size
);
1728 comm_write_mbuf(fd
, mb
, clientWriteComplete
, http
);
1729 /* if we don't do it, who will? */
1730 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1734 clientKeepaliveNextRequest(clientHttpRequest
* http
)
1736 ConnStateData
*conn
= http
->conn
;
1738 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn
->fd
);
1739 conn
->defer
.until
= 0; /* Kick it to read a new request */
1740 httpRequestFree(http
);
1741 if ((http
= conn
->chr
) == NULL
) {
1742 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1744 fd_note(conn
->fd
, "Reading next request");
1746 * Set the timeout BEFORE calling clientReadRequest().
1748 commSetTimeout(conn
->fd
, Config
.Timeout
.pconn
, requestTimeout
, conn
);
1749 clientReadRequest(conn
->fd
, conn
); /* Read next request */
1751 * Note, the FD may be closed at this point.
1753 } else if ((entry
= http
->entry
) == NULL
) {
1755 * this request is in progress, maybe doing an ACL or a redirect,
1756 * execution will resume after the operation completes.
1759 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1762 if (0 == storeClientCopyPending(entry
, http
)) {
1763 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1764 debug(33, 0) ("clientKeepaliveNextRequest: ENTRY_ABORTED\n");
1765 storeClientCopy(entry
,
1769 memAllocate(MEM_CLIENT_SOCK_BUF
),
1777 clientWriteComplete(int fd
, char *bufnotused
, size_t size
, int errflag
, void *data
)
1779 clientHttpRequest
*http
= data
;
1780 StoreEntry
*entry
= http
->entry
;
1782 http
->out
.size
+= size
;
1783 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1784 fd
, size
, errflag
, (int) http
->out
.offset
, entry
? objectLen(entry
) : 0);
1786 kb_incr(&Counter
.client_http
.kbytes_out
, size
);
1787 if (isTcpHit(http
->log_type
))
1788 kb_incr(&Counter
.client_http
.hit_kbytes_out
, size
);
1792 * just close the socket, httpRequestFree will abort if needed
1795 } else if (NULL
== entry
) {
1796 comm_close(fd
); /* yuk */
1797 } else if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1799 } else if ((done
= clientCheckTransferDone(http
)) != 0 || size
== 0) {
1800 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd
);
1801 /* We're finished case */
1802 if (httpReplyBodySize(http
->request
->method
, entry
->mem_obj
->reply
) < 0) {
1803 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1806 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1808 } else if (clientGotNotEnough(http
)) {
1809 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1811 } else if (http
->request
->flags
.proxy_keepalive
) {
1812 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd
);
1813 clientKeepaliveNextRequest(http
);
1817 } else if (clientReplyBodyTooLarge((int) http
->out
.offset
)) {
1820 /* More data will be coming from primary server; register with
1821 * storage manager. */
1822 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1823 debug(33, 0) ("clientWriteComplete 2: ENTRY_ABORTED\n");
1824 storeClientCopy(entry
,
1828 memAllocate(MEM_CLIENT_SOCK_BUF
),
1835 * client issued a request with an only-if-cached cache-control directive;
1836 * we did not find a cached object that can be returned without
1837 * contacting other servers;
1838 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1841 clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
)
1843 char *url
= http
->uri
;
1844 request_t
*r
= http
->request
;
1845 ErrorState
*err
= NULL
;
1846 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1847 RequestMethodStr
[r
->method
], url
);
1848 http
->al
.http
.code
= HTTP_GATEWAY_TIMEOUT
;
1849 err
= errorCon(ERR_ONLY_IF_CACHED_MISS
, HTTP_GATEWAY_TIMEOUT
);
1850 err
->request
= requestLink(r
);
1851 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1853 storeUnregister(http
->entry
, http
);
1854 storeUnlockObject(http
->entry
);
1856 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1857 errorAppendEntry(http
->entry
, err
);
1861 clientProcessRequest2(clientHttpRequest
* http
)
1863 request_t
*r
= http
->request
;
1865 e
= http
->entry
= storeGetPublic(http
->uri
, r
->method
);
1866 if (r
->method
== METHOD_HEAD
&& e
== NULL
) {
1867 /* We can generate a HEAD reply from a cached GET object */
1868 e
= http
->entry
= storeGetPublic(http
->uri
, METHOD_GET
);
1870 #if USE_CACHE_DIGESTS
1871 http
->lookup_type
= e
? "HIT" : "MISS";
1874 /* this object isn't in the cache */
1875 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
1876 return LOG_TCP_MISS
;
1878 if (Config
.onoff
.offline
) {
1879 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
1883 if (!storeEntryValidToSend(e
)) {
1884 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
1886 return LOG_TCP_MISS
;
1888 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1889 /* Special entries are always hits, no matter what the client says */
1890 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
1895 if (r
->flags
.nocache_hack
) {
1896 /* if nocache_hack is set, nocache should always be clear, right? */
1897 assert(!r
->flags
.nocache
);
1898 ipcacheReleaseInvalid(r
->host
);
1901 if (e
->store_status
== STORE_PENDING
) {
1902 if (r
->flags
.nocache
|| r
->flags
.nocache_hack
) {
1903 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
1905 r
->flags
.nocache
= 0;
1906 r
->flags
.nocache_hack
= 0;
1910 if (r
->flags
.nocache
) {
1911 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
1913 ipcacheReleaseInvalid(r
->host
);
1914 return LOG_TCP_CLIENT_REFRESH_MISS
;
1916 if (r
->range
&& httpHdrRangeWillBeComplex(r
->range
)) {
1918 * Some clients break if we return "200 OK" for a Range
1919 * request. We would have to return "200 OK" for a _complex_
1920 * Range request that is also a HIT. Thus, let's prevent HITs
1921 * on complex Range requests
1923 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
1925 return LOG_TCP_MISS
;
1927 debug(33, 3) ("clientProcessRequest2: default HIT\n");
1933 clientProcessRequest(clientHttpRequest
* http
)
1935 char *url
= http
->uri
;
1936 request_t
*r
= http
->request
;
1937 int fd
= http
->conn
->fd
;
1939 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
1940 RequestMethodStr
[r
->method
],
1942 if (r
->method
== METHOD_CONNECT
) {
1943 http
->log_type
= LOG_TCP_MISS
;
1944 sslStart(fd
, url
, r
, &http
->out
.size
);
1946 } else if (r
->method
== METHOD_PURGE
) {
1947 clientPurgeRequest(http
);
1949 } else if (r
->method
== METHOD_TRACE
) {
1950 if (r
->max_forwards
== 0) {
1951 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1952 storeReleaseRequest(http
->entry
);
1953 storeBuffer(http
->entry
);
1954 rep
= httpReplyCreate();
1955 httpReplySetHeaders(rep
, 1.0, HTTP_OK
, NULL
, "text/plain",
1956 httpRequestPrefixLen(r
), 0, squid_curtime
);
1957 httpReplySwapOut(rep
, http
->entry
);
1958 httpReplyDestroy(rep
);
1959 httpRequestSwapOut(r
, http
->entry
);
1960 storeComplete(http
->entry
);
1964 http
->log_type
= LOG_TCP_MISS
;
1965 } else if (r
->content_length
> 0) {
1966 http
->log_type
= LOG_TCP_MISS
;
1967 /* XXX oof, POST can be cached! */
1968 pumpInit(fd
, r
, http
->uri
);
1970 http
->log_type
= clientProcessRequest2(http
);
1972 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
1973 log_tags
[http
->log_type
],
1975 http
->out
.offset
= 0;
1976 if (NULL
!= http
->entry
) {
1977 storeLockObject(http
->entry
);
1978 storeCreateMemObject(http
->entry
, http
->uri
, http
->log_uri
);
1979 http
->entry
->mem_obj
->method
= r
->method
;
1980 storeClientListAdd(http
->entry
, http
);
1982 delaySetStoreClient(http
->entry
, http
, delayClient(r
));
1984 storeClientCopy(http
->entry
,
1988 memAllocate(MEM_CLIENT_SOCK_BUF
),
1992 /* MISS CASE, http->log_type is already set! */
1993 clientProcessMiss(http
);
1998 * Prepare to fetch the object as it's a cache miss of some kind.
2001 clientProcessMiss(clientHttpRequest
* http
)
2003 char *url
= http
->uri
;
2004 request_t
*r
= http
->request
;
2005 ErrorState
*err
= NULL
;
2006 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
2007 RequestMethodStr
[r
->method
], url
);
2009 * We might have a left-over StoreEntry from a failed cache hit
2013 if (EBIT_TEST(http
->entry
->flags
, ENTRY_SPECIAL
))
2014 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url
);
2015 storeUnregister(http
->entry
, http
);
2016 storeUnlockObject(http
->entry
);
2019 if (clientOnlyIfCached(http
)) {
2020 clientProcessOnlyIfCachedMiss(http
);
2024 * Deny loops when running in accelerator/transproxy mode.
2026 if (http
->flags
.accel
&& r
->flags
.loopdetect
) {
2027 http
->al
.http
.code
= HTTP_FORBIDDEN
;
2028 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
2029 err
->request
= requestLink(r
);
2030 err
->src_addr
= http
->conn
->peer
.sin_addr
;
2031 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
2032 errorAppendEntry(http
->entry
, err
);
2035 assert(http
->out
.offset
== 0);
2036 http
->entry
= clientCreateStoreEntry(http
, r
->method
, r
->flags
);
2037 if (http
->redirect
.status
) {
2038 HttpReply
*rep
= httpReplyCreate();
2039 #if LOG_TCP_REDIRECTS
2040 http
->log_type
= LOG_TCP_REDIRECT
;
2042 storeReleaseRequest(http
->entry
);
2043 httpRedirectReply(rep
, http
->redirect
.status
, http
->redirect
.location
);
2044 httpReplySwapOut(rep
, http
->entry
);
2045 httpReplyDestroy(rep
);
2046 storeComplete(http
->entry
);
2049 if (http
->flags
.internal
)
2050 r
->protocol
= PROTO_INTERNAL
;
2051 fwdStart(http
->conn
->fd
, http
->entry
, r
);
2054 static clientHttpRequest
*
2055 parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
)
2057 clientHttpRequest
*http
= xcalloc(1, sizeof(clientHttpRequest
));
2058 cbdataAdd(http
, cbdataXfree
, 0);
2060 http
->start
= current_time
;
2061 http
->req_sz
= conn
->in
.offset
;
2062 http
->uri
= xstrdup(uri
);
2063 http
->log_uri
= xstrndup(uri
, MAX_URL
);
2064 http
->range_iter
.boundary
= StringNull
;
2065 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2070 * parseHttpRequest()
2073 * NULL on error or incomplete request
2074 * a clientHttpRequest structure on success
2076 static clientHttpRequest
*
2077 parseHttpRequest(ConnStateData
* conn
, method_t
* method_p
, int *status
,
2078 char **prefix_p
, size_t * req_line_sz_p
)
2083 char *req_hdr
= NULL
;
2088 int free_request
= 0;
2089 size_t header_sz
; /* size of headers, not including first line */
2090 size_t prefix_sz
; /* size of whole request (req-line + headers) */
2094 clientHttpRequest
*http
= NULL
;
2096 struct natlookup natLookup
;
2097 static int natfd
= -1;
2100 if ((req_sz
= headersEnd(conn
->in
.buf
, conn
->in
.offset
)) == 0) {
2101 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
2104 *method_p
= METHOD_NONE
;
2107 assert(req_sz
<= conn
->in
.offset
);
2108 /* Use memcpy, not strdup! */
2109 inbuf
= xmalloc(req_sz
+ 1);
2110 xmemcpy(inbuf
, conn
->in
.buf
, req_sz
);
2111 *(inbuf
+ req_sz
) = '\0';
2113 /* pre-set these values to make aborting simpler */
2115 *method_p
= METHOD_NONE
;
2118 /* Barf on NULL characters in the headers */
2119 if (strlen(inbuf
) != req_sz
) {
2120 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
2121 return parseHttpRequestAbort(conn
, "error:invalid-request");
2123 /* Look for request method */
2124 if ((mstr
= strtok(inbuf
, "\t ")) == NULL
) {
2125 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
2126 return parseHttpRequestAbort(conn
, "error:invalid-request-method");
2128 method
= urlParseMethod(mstr
);
2129 if (method
== METHOD_NONE
) {
2130 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr
);
2131 return parseHttpRequestAbort(conn
, "error:unsupported-request-method");
2133 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr
);
2136 /* look for URL+HTTP/x.x */
2137 if ((url
= strtok(NULL
, "\n")) == NULL
) {
2138 debug(33, 1) ("parseHttpRequest: Missing URL\n");
2139 return parseHttpRequestAbort(conn
, "error:missing-url");
2141 while (xisspace(*url
))
2143 t
= url
+ strlen(url
);
2148 if (xisspace(*t
) && !strncmp(t
+ 1, "HTTP/", 5)) {
2153 while (t
> url
&& xisspace(*t
))
2155 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url
);
2156 if (token
== NULL
) {
2157 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
2158 #if RELAXED_HTTP_PARSER
2159 http_ver
= (float) 0.9; /* wild guess */
2161 return parseHttpRequestAbort(conn
, "error:missing-http-ident");
2164 http_ver
= (float) atof(token
+ 5);
2168 * Process headers after request line
2170 req_hdr
= strtok(NULL
, null_string
);
2171 header_sz
= req_sz
- (req_hdr
- inbuf
);
2172 if (0 == header_sz
) {
2173 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
2177 assert(header_sz
> 0);
2178 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr
);
2179 end
= req_hdr
+ header_sz
;
2180 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end
);
2182 prefix_sz
= end
- inbuf
;
2183 *req_line_sz_p
= req_hdr
- inbuf
;
2184 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
2185 (int) prefix_sz
, (int) *req_line_sz_p
);
2186 assert(prefix_sz
<= conn
->in
.offset
);
2188 /* Ok, all headers are received */
2189 http
= xcalloc(1, sizeof(clientHttpRequest
));
2190 cbdataAdd(http
, cbdataXfree
, 0);
2191 http
->http_ver
= http_ver
;
2193 http
->start
= current_time
;
2194 http
->req_sz
= prefix_sz
;
2195 http
->range_iter
.boundary
= StringNull
;
2196 *prefix_p
= xmalloc(prefix_sz
+ 1);
2197 xmemcpy(*prefix_p
, conn
->in
.buf
, prefix_sz
);
2198 *(*prefix_p
+ prefix_sz
) = '\0';
2199 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2201 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p
) + *req_line_sz_p
);
2202 if ((t
= strchr(url
, '#'))) /* remove HTML anchors */
2205 /* handle internal objects */
2206 if (internalCheck(url
)) {
2207 /* prepend our name & port */
2208 http
->uri
= xstrdup(internalLocalUri(NULL
, url
));
2209 http
->flags
.internal
= 1;
2210 http
->flags
.accel
= 1;
2212 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2213 else if (Config2
.Accel
.on
&& *url
== '/') {
2214 /* prepend the accel prefix */
2215 if (opt_accel_uses_host
&& (t
= mime_get_header(req_hdr
, "Host"))) {
2216 /* If a Host: header was specified, use it to build the URL
2217 * instead of the one in the Config file. */
2219 * XXX Use of the Host: header here opens a potential
2220 * security hole. There are no checks that the Host: value
2221 * corresponds to one of your servers. It might, for example,
2222 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2223 * types should be used to prevent httpd-accelerators
2224 * handling requests for non-local servers */
2226 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
+
2228 http
->uri
= xcalloc(url_sz
, 1);
2229 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2230 t
, (int) Config
.Accel
.port
, url
);
2231 } else if (vhost_mode
) {
2232 /* Put the local socket IP address as the hostname */
2233 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
;
2234 http
->uri
= xcalloc(url_sz
, 1);
2236 natLookup
.nl_inport
= http
->conn
->me
.sin_port
;
2237 natLookup
.nl_outport
= http
->conn
->peer
.sin_port
;
2238 natLookup
.nl_inip
= http
->conn
->me
.sin_addr
;
2239 natLookup
.nl_outip
= http
->conn
->peer
.sin_addr
;
2240 natLookup
.nl_flags
= IPN_TCP
;
2242 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
2244 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2246 return parseHttpRequestAbort(conn
, "error:nat-open-failed");
2248 if (ioctl(natfd
, SIOCGNATL
, &natLookup
) < 0) {
2249 if (errno
!= ESRCH
) {
2250 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2253 return parseHttpRequestAbort(conn
, "error:nat-lookup-failed");
2255 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2256 inet_ntoa(http
->conn
->me
.sin_addr
),
2257 (int) Config
.Accel
.port
,
2260 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2261 inet_ntoa(natLookup
.nl_realip
),
2262 (int) Config
.Accel
.port
,
2265 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2266 inet_ntoa(http
->conn
->me
.sin_addr
),
2267 (int) Config
.Accel
.port
,
2270 debug(33, 5) ("VHOST REWRITE: '%s'\n", http
->uri
);
2272 url_sz
= strlen(Config2
.Accel
.prefix
) + strlen(url
) +
2273 Config
.appendDomainLen
+ 1;
2274 http
->uri
= xcalloc(url_sz
, 1);
2275 snprintf(http
->uri
, url_sz
, "%s%s", Config2
.Accel
.prefix
, url
);
2277 http
->flags
.accel
= 1;
2279 /* URL may be rewritten later, so make extra room */
2280 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
2281 http
->uri
= xcalloc(url_sz
, 1);
2282 strcpy(http
->uri
, url
);
2283 http
->flags
.accel
= 0;
2285 if (!stringHasCntl(http
->uri
))
2286 http
->log_uri
= xstrndup(http
->uri
, MAX_URL
);
2288 http
->log_uri
= xstrndup(rfc1738_escape_unescaped(http
->uri
), MAX_URL
);
2289 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2298 clientReadDefer(int fdnotused
, void *data
)
2300 ConnStateData
*conn
= data
;
2301 return conn
->defer
.until
> squid_curtime
;
2305 clientReadRequest(int fd
, void *data
)
2307 ConnStateData
*conn
= data
;
2308 int parser_return_code
= 0;
2310 request_t
*request
= NULL
;
2313 clientHttpRequest
*http
= NULL
;
2314 clientHttpRequest
**H
= NULL
;
2315 char *prefix
= NULL
;
2316 ErrorState
*err
= NULL
;
2317 fde
*F
= &fd_table
[fd
];
2318 int len
= conn
->in
.size
- conn
->in
.offset
- 1;
2319 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd
);
2320 Counter
.syscalls
.sock
.reads
++;
2321 size
= read(fd
, conn
->in
.buf
+ conn
->in
.offset
, len
);
2323 fd_bytes(fd
, size
, FD_READ
);
2324 kb_incr(&Counter
.client_http
.kbytes_in
, size
);
2327 * Don't reset the timeout value here. The timeout value will be
2328 * set to Config.Timeout.request by httpAccept() and
2329 * clientWriteComplete(), and should apply to the request as a
2330 * whole, not individual read() calls. Plus, it breaks our
2331 * lame half-close detection
2333 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, conn
, 0);
2335 if (conn
->chr
== NULL
) {
2336 /* no current or pending requests */
2339 } else if (!Config
.onoff
.half_closed_clients
) {
2340 /* admin doesn't want to support half-closed client sockets */
2344 /* It might be half-closed, we can't tell */
2345 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd
);
2346 F
->flags
.socket_eof
= 1;
2347 conn
->defer
.until
= squid_curtime
+ 1;
2349 fd_note(fd
, "half-closed");
2351 } else if (size
< 0) {
2352 if (!ignoreErrno(errno
)) {
2353 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd
, xstrerror());
2356 } else if (conn
->in
.offset
== 0) {
2357 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd
, xstrerror());
2360 /* Continue to process previously read data */
2363 conn
->in
.offset
+= size
;
2364 /* Skip leading (and trailing) whitespace */
2365 while (conn
->in
.offset
> 0) {
2368 while (conn
->in
.offset
> 0 && xisspace(conn
->in
.buf
[0])) {
2369 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ 1, conn
->in
.offset
- 1);
2372 conn
->in
.buf
[conn
->in
.offset
] = '\0'; /* Terminate the string */
2373 if (conn
->in
.offset
== 0)
2375 /* Limit the number of concurrent requests to 2 */
2376 for (H
= &conn
->chr
, nrequests
= 0; *H
; H
= &(*H
)->next
, nrequests
++);
2377 if (nrequests
>= 2) {
2378 debug(33, 3) ("clientReadRequest: FD %d max concurrent requests reached\n", fd
);
2379 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd
);
2380 conn
->defer
.until
= squid_curtime
+ 100; /* Reset when a request is complete */
2383 /* Process request */
2384 http
= parseHttpRequest(conn
,
2386 &parser_return_code
,
2392 assert(http
->req_sz
> 0);
2393 conn
->in
.offset
-= http
->req_sz
;
2394 assert(conn
->in
.offset
>= 0);
2395 debug(33, 5) ("conn->in.offset = %d\n", (int) conn
->in
.offset
);
2397 * If we read past the end of this request, move the remaining
2398 * data to the beginning
2400 if (conn
->in
.offset
> 0)
2401 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ http
->req_sz
, conn
->in
.offset
);
2402 /* add to the client request queue */
2403 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2406 commSetTimeout(fd
, Config
.Timeout
.lifetime
, NULL
, NULL
);
2407 if (parser_return_code
< 0) {
2408 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd
);
2409 err
= errorCon(ERR_INVALID_REQ
, HTTP_BAD_REQUEST
);
2410 err
->request_hdrs
= xstrdup(conn
->in
.buf
);
2411 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2412 errorAppendEntry(http
->entry
, err
);
2416 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
2417 debug(33, 5) ("Invalid URL: %s\n", http
->uri
);
2418 err
= errorCon(ERR_INVALID_URL
, HTTP_BAD_REQUEST
);
2419 err
->src_addr
= conn
->peer
.sin_addr
;
2420 err
->url
= xstrdup(http
->uri
);
2421 http
->al
.http
.code
= err
->http_status
;
2422 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2423 errorAppendEntry(http
->entry
, err
);
2427 /* compile headers */
2428 /* we should skip request line! */
2429 if (!httpRequestParseHeader(request
, prefix
+ req_line_sz
))
2430 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2432 /* continue anyway? */
2434 request
->flags
.accelerated
= http
->flags
.accel
;
2435 if (!http
->flags
.internal
) {
2436 if (internalCheck(strBuf(request
->urlpath
))) {
2437 if (internalHostnameIs(request
->host
) &&
2438 request
->port
== ntohs(Config
.Sockaddr
.http
->s
.sin_port
)) {
2439 http
->flags
.internal
= 1;
2440 } else if (internalStaticCheck(strBuf(request
->urlpath
))) {
2441 xstrncpy(request
->host
, internalHostname(), SQUIDHOSTNAMELEN
);
2442 request
->port
= ntohs(Config
.Sockaddr
.http
->s
.sin_port
);
2443 http
->flags
.internal
= 1;
2448 * cache the Content-length value in request_t.
2450 request
->content_length
= httpHeaderGetInt(&request
->header
,
2451 HDR_CONTENT_LENGTH
);
2452 request
->flags
.internal
= http
->flags
.internal
;
2454 safe_free(http
->log_uri
);
2455 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
2456 request
->client_addr
= conn
->peer
.sin_addr
;
2457 request
->my_addr
= conn
->me
.sin_addr
;
2458 request
->my_port
= ntohs(conn
->me
.sin_port
);
2459 request
->http_ver
= http
->http_ver
;
2460 if (!urlCheckRequest(request
)) {
2461 err
= errorCon(ERR_UNSUP_REQ
, HTTP_NOT_IMPLEMENTED
);
2462 err
->src_addr
= conn
->peer
.sin_addr
;
2463 err
->request
= requestLink(request
);
2464 request
->flags
.proxy_keepalive
= 0;
2465 http
->al
.http
.code
= err
->http_status
;
2466 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2467 errorAppendEntry(http
->entry
, err
);
2470 if (0 == clientCheckContentLength(request
)) {
2471 err
= errorCon(ERR_INVALID_REQ
, HTTP_LENGTH_REQUIRED
);
2472 err
->src_addr
= conn
->peer
.sin_addr
;
2473 err
->request
= requestLink(request
);
2474 http
->al
.http
.code
= err
->http_status
;
2475 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2476 errorAppendEntry(http
->entry
, err
);
2479 http
->request
= requestLink(request
);
2481 * We need to set the keepalive flag before doing some
2482 * hacks for POST/PUT requests below. Maybe we could
2483 * set keepalive flag even earlier.
2485 clientSetKeepaliveFlag(http
);
2487 * break here if the request has a content-length
2488 * because there is a reqeust body following and we
2489 * don't want to parse it as though it was new request.
2491 if (request
->content_length
>= 0) {
2492 int copy_len
= XMIN(conn
->in
.offset
, request
->content_length
);
2494 assert(conn
->in
.offset
>= copy_len
);
2495 request
->body_sz
= copy_len
;
2496 request
->body
= xmalloc(request
->body_sz
);
2497 xmemcpy(request
->body
, conn
->in
.buf
, request
->body_sz
);
2498 conn
->in
.offset
-= copy_len
;
2499 if (conn
->in
.offset
)
2500 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ copy_len
, conn
->in
.offset
);
2503 * if we didn't get the full body now, then more will
2504 * be arriving on the client socket. Lets cancel
2505 * the read handler until this request gets forwarded.
2507 if (request
->body_sz
< request
->content_length
)
2508 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2509 if (request
->content_length
< 0)
2511 else if (clientRequestBodyTooLarge(request
->content_length
)) {
2512 err
= errorCon(ERR_TOO_BIG
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2513 err
->request
= requestLink(request
);
2514 http
->entry
= clientCreateStoreEntry(http
,
2515 METHOD_NONE
, null_request_flags
);
2516 errorAppendEntry(http
->entry
, err
);
2520 clientAccessCheck(http
);
2521 continue; /* while offset > 0 */
2522 } else if (parser_return_code
== 0) {
2524 * Partial request received; reschedule until parseHttpRequest()
2525 * is happy with the input
2527 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2529 if (conn
->in
.offset
>= Config
.maxRequestHeaderSize
) {
2530 /* The request is too large to handle */
2531 debug(33, 1) ("Request header is too large (%d bytes)\n",
2532 (int) conn
->in
.offset
);
2533 debug(33, 1) ("Config 'request_header_max_size'= %d bytes.\n",
2534 Config
.maxRequestHeaderSize
);
2535 err
= errorCon(ERR_TOO_BIG
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2536 http
= parseHttpRequestAbort(conn
, "error:request-too-large");
2537 /* add to the client request queue */
2538 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2540 http
->entry
= clientCreateStoreEntry(http
, METHOD_NONE
, null_request_flags
);
2541 errorAppendEntry(http
->entry
, err
);
2544 /* Grow the request memory area to accomodate for a large request */
2545 conn
->in
.size
+= REQUEST_BUF_SIZE
;
2546 conn
->in
.buf
= xrealloc(conn
->in
.buf
, conn
->in
.size
);
2547 /* XXX account conn->in.buf */
2548 debug(33, 3) ("Handling a large request, offset=%d inbufsize=%d\n",
2549 (int) conn
->in
.offset
, conn
->in
.size
);
2550 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2557 /* general lifetime handler for HTTP requests */
2559 requestTimeout(int fd
, void *data
)
2561 ConnStateData
*conn
= data
;
2563 debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2564 if (fd_table
[fd
].rwstate
) {
2566 * Some data has been sent to the client, just close the FD
2569 } else if (conn
->nrequests
) {
2571 * assume its a persistent connection; just close it
2578 err
= errorCon(ERR_LIFETIME_EXP
, HTTP_REQUEST_TIMEOUT
);
2579 err
->url
= xstrdup("N/A");
2581 * Normally we shouldn't call errorSend() in client_side.c, but
2582 * it should be okay in this case. Presumably if we get here
2583 * this is the first request for the connection, and no data
2584 * has been written yet
2586 assert(conn
->chr
== NULL
);
2589 * if we don't close() here, we still need a timeout handler!
2591 commSetTimeout(fd
, 30, requestTimeout
, conn
);
2593 * Aha, but we don't want a read handler!
2595 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2600 httpAcceptDefer(void)
2602 static time_t last_warn
= 0;
2603 if (fdNFree() >= RESERVED_FD
)
2605 if (last_warn
+ 15 < squid_curtime
) {
2606 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2607 last_warn
= squid_curtime
;
2612 /* Handle a new connection on HTTP socket. */
2614 httpAccept(int sock
, void *data
)
2618 ConnStateData
*connState
= NULL
;
2619 struct sockaddr_in peer
;
2620 struct sockaddr_in me
;
2621 int max
= INCOMING_HTTP_MAX
;
2623 static aclCheck_t identChecklist
;
2625 commSetSelect(sock
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2626 while (max
-- && !httpAcceptDefer()) {
2627 memset(&peer
, '\0', sizeof(struct sockaddr_in
));
2628 memset(&me
, '\0', sizeof(struct sockaddr_in
));
2629 if ((fd
= comm_accept(sock
, &peer
, &me
)) < 0) {
2630 if (!ignoreErrno(errno
))
2631 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2635 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd
);
2636 connState
= memAllocate(MEM_CONNSTATEDATA
);
2637 connState
->peer
= peer
;
2638 connState
->log_addr
= peer
.sin_addr
;
2639 connState
->log_addr
.s_addr
&= Config
.Addrs
.client_netmask
.s_addr
;
2642 connState
->in
.size
= REQUEST_BUF_SIZE
;
2643 connState
->in
.buf
= xcalloc(connState
->in
.size
, 1);
2644 cbdataAdd(connState
, memFree
, MEM_CONNSTATEDATA
);
2645 /* XXX account connState->in.buf */
2646 comm_add_close_handler(fd
, connStateFree
, connState
);
2647 if (Config
.onoff
.log_fqdn
)
2648 fqdncache_gethostbyaddr(peer
.sin_addr
, FQDN_LOOKUP_IF_MISS
);
2649 commSetTimeout(fd
, Config
.Timeout
.request
, requestTimeout
, connState
);
2651 identChecklist
.src_addr
= peer
.sin_addr
;
2652 identChecklist
.my_addr
= me
.sin_addr
;
2653 identChecklist
.my_port
= ntohs(me
.sin_port
);
2654 if (aclCheckFast(Config
.accessList
.identLookup
, &identChecklist
))
2655 identStart(&me
, &peer
, clientIdentDone
, connState
);
2657 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, connState
, 0);
2658 commSetDefer(fd
, clientReadDefer
, connState
);
2659 clientdbEstablished(peer
.sin_addr
, 1);
2664 #define SENDING_BODY 0
2665 #define SENDING_HDRSONLY 1
2667 clientCheckTransferDone(clientHttpRequest
* http
)
2669 int sending
= SENDING_BODY
;
2670 StoreEntry
*entry
= http
->entry
;
2677 * For now, 'done_copying' is used for special cases like
2678 * Range and HEAD requests.
2680 if (http
->flags
.done_copying
)
2683 * Handle STORE_OK objects.
2684 * objectLen(entry) will be set proprely.
2686 if (entry
->store_status
== STORE_OK
) {
2687 if (http
->out
.offset
>= objectLen(entry
))
2693 * Now, handle STORE_PENDING objects
2695 mem
= entry
->mem_obj
;
2696 assert(mem
!= NULL
);
2697 assert(http
->request
!= NULL
);
2699 if (reply
->hdr_sz
== 0)
2700 return 0; /* haven't found end of headers yet */
2701 else if (reply
->sline
.status
== HTTP_OK
)
2702 sending
= SENDING_BODY
;
2703 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
2704 sending
= SENDING_HDRSONLY
;
2705 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
2706 sending
= SENDING_HDRSONLY
;
2707 else if (reply
->sline
.status
< HTTP_OK
)
2708 sending
= SENDING_HDRSONLY
;
2709 else if (http
->request
->method
== METHOD_HEAD
)
2710 sending
= SENDING_HDRSONLY
;
2712 sending
= SENDING_BODY
;
2714 * Figure out how much data we are supposed to send.
2715 * If we are sending a body and we don't have a content-length,
2716 * then we must wait for the object to become STORE_OK.
2718 if (sending
== SENDING_HDRSONLY
)
2719 sendlen
= reply
->hdr_sz
;
2720 else if (reply
->content_length
< 0)
2723 sendlen
= reply
->content_length
+ reply
->hdr_sz
;
2725 * Now that we have the expected length, did we send it all?
2727 if (http
->out
.offset
< sendlen
)
2734 clientGotNotEnough(clientHttpRequest
* http
)
2736 int cl
= httpReplyBodySize(http
->request
->method
, http
->entry
->mem_obj
->reply
);
2737 int hs
= http
->entry
->mem_obj
->reply
->hdr_sz
;
2739 if (http
->out
.offset
< cl
+ hs
)
2745 * This function is designed to serve a fairly specific purpose.
2746 * Occasionally our vBNS-connected caches can talk to each other, but not
2747 * the rest of the world. Here we try to detect frequent failures which
2748 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2749 * the failure:success ratio goes above 1.0 then we go into "hit only"
2750 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2751 * will only fetch HITs from us if they are using the ICP protocol. We
2752 * stay in this mode for 5 minutes.
2754 * Duane W., Sept 16, 1996
2758 checkFailureRatio(err_type etype
, hier_code hcode
)
2760 static double magic_factor
= 100.0;
2763 if (hcode
== HIER_NONE
)
2765 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
2766 n_bad
= magic_factor
- n_good
;
2769 case ERR_CONNECT_FAIL
:
2770 case ERR_READ_ERROR
:
2776 request_failure_ratio
= n_bad
/ n_good
;
2777 if (hit_only_mode_until
> squid_curtime
)
2779 if (request_failure_ratio
< 1.0)
2781 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
2782 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2783 FAILURE_MODE_TIME
/ 60);
2784 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
2785 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
2789 clientHttpConnectionsOpen(void)
2791 sockaddr_in_list
*s
;
2793 for (s
= Config
.Sockaddr
.http
; s
; s
= s
->next
) {
2795 fd
= comm_open(SOCK_STREAM
,
2798 ntohs(s
->s
.sin_port
),
2805 commSetSelect(fd
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2806 /*commSetDefer(fd, httpAcceptDefer, NULL); */
2807 debug(1, 1) ("Accepting HTTP connections at %s, port %d, FD %d.\n",
2808 inet_ntoa(s
->s
.sin_addr
),
2809 (int) ntohs(s
->s
.sin_port
),
2811 HttpSockets
[NHttpSockets
++] = fd
;
2813 if (NHttpSockets
< 1)
2814 fatal("Cannot open HTTP Port");
2818 clientHttpConnectionsClose(void)
2821 for (i
= 0; i
< NHttpSockets
; i
++) {
2822 if (HttpSockets
[i
] >= 0) {
2823 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets
[i
]);
2824 comm_close(HttpSockets
[i
]);
2825 HttpSockets
[i
] = -1;