3 * $Id: client_side.cc,v 1.500 2000/10/04 01:46:30 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>
44 #if HAVE_IP_FIL_COMPAT_H
45 #include <ip_fil_compat.h>
46 #elif HAVE_NETINET_IP_FIL_COMPAT_H
47 #include <netinet/ip_fil_compat.h>
48 #elif HAVE_IP_COMPAT_H
49 #include <ip_compat.h>
50 #elif HAVE_NETINET_IP_COMPAT_H
51 #include <netinet/ip_compat.h>
55 #elif HAVE_NETINET_IP_FIL_H
56 #include <netinet/ip_fil.h>
60 #elif HAVE_NETINET_IP_NAT_H
61 #include <netinet/ip_nat.h>
68 #define comm_close comm_lingering_close
71 static const char *const crlf
= "\r\n";
73 #define REQUEST_BUF_SIZE 4096
74 #define FAILURE_MODE_TIME 300
78 static CWCB clientWriteComplete
;
79 static CWCB clientWriteBodyComplete
;
80 static PF clientReadRequest
;
81 static PF connStateFree
;
82 static PF requestTimeout
;
83 static int clientCheckTransferDone(clientHttpRequest
*);
84 static int clientGotNotEnough(clientHttpRequest
*);
85 static void checkFailureRatio(err_type
, hier_code
);
86 static void clientProcessMiss(clientHttpRequest
*);
87 static void clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
);
88 static clientHttpRequest
*parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
);
89 static clientHttpRequest
*parseHttpRequest(ConnStateData
*, method_t
*, int *, char **, size_t *);
90 static RH clientRedirectDone
;
91 static void clientCheckNoCache(clientHttpRequest
*);
92 static void clientCheckNoCacheDone(int answer
, void *data
);
93 static STCB clientHandleIMSReply
;
94 static int clientGetsOldEntry(StoreEntry
* new, StoreEntry
* old
, request_t
* request
);
95 static int checkAccelOnly(clientHttpRequest
*);
97 static IDCB clientIdentDone
;
99 static int clientOnlyIfCached(clientHttpRequest
* http
);
100 static STCB clientSendMoreData
;
101 static STCB clientCacheHit
;
102 static void clientSetKeepaliveFlag(clientHttpRequest
*);
103 static void clientPackRangeHdr(const HttpReply
* rep
, const HttpHdrRangeSpec
* spec
, String boundary
, MemBuf
* mb
);
104 static void clientPackTermBound(String boundary
, MemBuf
* mb
);
105 static void clientInterpretRequestHeaders(clientHttpRequest
*);
106 static void clientProcessRequest(clientHttpRequest
*);
107 static void clientProcessExpired(void *data
);
108 static void clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
);
109 static int clientCachable(clientHttpRequest
* http
);
110 static int clientHierarchical(clientHttpRequest
* http
);
111 static int clientCheckContentLength(request_t
* r
);
112 static DEFER httpAcceptDefer
;
113 static log_type
clientProcessRequest2(clientHttpRequest
* http
);
114 static int clientReplyBodyTooLarge(int clen
);
115 static int clientRequestBodyTooLarge(int clen
);
118 checkAccelOnly(clientHttpRequest
* http
)
120 /* return TRUE if someone makes a proxy request to us and
121 * we are in httpd-accel only mode */
122 if (!Config2
.Accel
.on
)
124 if (Config
.onoff
.accel_with_proxy
)
126 if (http
->request
->protocol
== PROTO_CACHEOBJ
)
128 if (http
->flags
.accel
)
135 clientIdentDone(const char *ident
, void *data
)
137 ConnStateData
*conn
= data
;
139 xstrncpy(conn
->ident
, ident
, sizeof(conn
->ident
));
141 xstrncpy(conn
->ident
, "-", sizeof(conn
->ident
));
146 clientAclChecklistCreate(const acl_access
* acl
, const clientHttpRequest
* http
)
149 ConnStateData
*conn
= http
->conn
;
150 ch
= aclChecklistCreate(acl
,
155 * hack for ident ACL. It needs to get full addresses, and a
156 * place to store the ident result on persistent connections...
159 cbdataLock(ch
->conn
);
165 clientAccessCheck(void *data
)
167 clientHttpRequest
*http
= data
;
168 if (checkAccelOnly(http
)) {
169 /* deny proxy requests in accel_only mode */
170 clientAccessCheckDone(ACCESS_DENIED
, http
);
173 http
->acl_checklist
= clientAclChecklistCreate(Config
.accessList
.http
, http
);
174 aclNBCheck(http
->acl_checklist
, clientAccessCheckDone
, http
);
178 * returns true if client specified that the object must come from the cache
179 * witout contacting origin server
182 clientOnlyIfCached(clientHttpRequest
* http
)
184 const request_t
*r
= http
->request
;
186 return r
->cache_control
&&
187 EBIT_TEST(r
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
191 clientCreateStoreEntry(clientHttpRequest
* h
, method_t m
, request_flags flags
)
195 * For erroneous requests, we might not have a h->request,
196 * so make a fake one.
198 if (h
->request
== NULL
)
199 h
->request
= requestLink(requestCreate(m
, PROTO_NONE
, null_string
));
200 e
= storeCreateEntry(h
->uri
, h
->log_uri
, flags
, m
);
201 h
->sc
= storeClientListAdd(e
, h
);
203 delaySetStoreClient(h
->sc
, delayClient(h
->request
));
205 storeClientCopy(h
->sc
, e
, 0, 0, CLIENT_SOCK_SZ
,
206 memAllocate(MEM_CLIENT_SOCK_BUF
), clientSendMoreData
, h
);
211 clientAccessCheckDone(int answer
, void *data
)
213 clientHttpRequest
*http
= data
;
216 ErrorState
*err
= NULL
;
217 debug(33, 2) ("The request %s %s is %s, because it matched '%s'\n",
218 RequestMethodStr
[http
->request
->method
], http
->uri
,
219 answer
== ACCESS_ALLOWED
? "ALLOWED" : "DENIED",
220 AclMatchedName
? AclMatchedName
: "NO ACL's");
221 http
->acl_checklist
= NULL
;
222 if (answer
== ACCESS_ALLOWED
) {
223 safe_free(http
->uri
);
224 http
->uri
= xstrdup(urlCanonical(http
->request
));
225 assert(http
->redirect_state
== REDIRECT_NONE
);
226 http
->redirect_state
= REDIRECT_PENDING
;
227 redirectStart(http
, clientRedirectDone
, http
);
229 debug(33, 5) ("Access Denied: %s\n", http
->uri
);
230 debug(33, 5) ("AclMatchedName = %s\n",
231 AclMatchedName
? AclMatchedName
: "<null>");
233 * NOTE: get page_id here, based on AclMatchedName because
234 * if USE_DELAY_POOLS is enabled, then AclMatchedName gets
235 * clobbered in the clientCreateStoreEntry() call
236 * just below. Pedro Ribeiro <pribeiro@isel.pt>
238 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
239 http
->log_type
= LOG_TCP_DENIED
;
240 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
242 if (answer
== ACCESS_REQ_PROXY_AUTH
|| aclIsProxyAuth(AclMatchedName
)) {
243 if (!http
->flags
.accel
) {
244 /* Proxy authorisation needed */
245 status
= HTTP_PROXY_AUTHENTICATION_REQUIRED
;
247 /* WWW authorisation needed */
248 status
= HTTP_UNAUTHORIZED
;
251 page_id
= ERR_CACHE_ACCESS_DENIED
;
253 status
= HTTP_FORBIDDEN
;
255 page_id
= ERR_ACCESS_DENIED
;
257 err
= errorCon(page_id
, status
);
258 err
->request
= requestLink(http
->request
);
259 err
->src_addr
= http
->conn
->peer
.sin_addr
;
260 errorAppendEntry(http
->entry
, err
);
265 clientRedirectDone(void *data
, char *result
)
267 clientHttpRequest
*http
= data
;
268 request_t
*new_request
= NULL
;
269 request_t
*old_request
= http
->request
;
270 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
271 result
? result
: "NULL");
272 assert(http
->redirect_state
== REDIRECT_PENDING
);
273 http
->redirect_state
= REDIRECT_DONE
;
275 http_status status
= atoi(result
);
276 if (status
== 301 || status
== 302) {
278 if ((t
= strchr(result
, ':')) != NULL
) {
279 http
->redirect
.status
= status
;
280 http
->redirect
.location
= xstrdup(t
+ 1);
282 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result
);
285 if (strcmp(result
, http
->uri
))
286 new_request
= urlParse(old_request
->method
, result
);
289 safe_free(http
->uri
);
290 http
->uri
= xstrdup(urlCanonical(new_request
));
291 new_request
->http_ver
= old_request
->http_ver
;
292 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
293 new_request
->client_addr
= old_request
->client_addr
;
294 new_request
->my_addr
= old_request
->my_addr
;
295 new_request
->my_port
= old_request
->my_port
;
296 new_request
->flags
.redirected
= 1;
297 if (old_request
->user_ident
[0])
298 xstrncpy(new_request
->user_ident
, old_request
->user_ident
,
300 if (old_request
->body
) {
301 new_request
->body
= xmalloc(old_request
->body_sz
);
302 xmemcpy(new_request
->body
, old_request
->body
, old_request
->body_sz
);
303 new_request
->body_sz
= old_request
->body_sz
;
305 new_request
->content_length
= old_request
->content_length
;
306 new_request
->flags
.proxy_keepalive
= old_request
->flags
.proxy_keepalive
;
307 requestUnlink(old_request
);
308 http
->request
= requestLink(new_request
);
310 clientInterpretRequestHeaders(http
);
312 headersLog(0, 1, request
->method
, request
);
314 fd_note(http
->conn
->fd
, http
->uri
);
315 clientCheckNoCache(http
);
319 clientCheckNoCache(clientHttpRequest
* http
)
321 if (Config
.accessList
.noCache
&& http
->request
->flags
.cachable
) {
322 http
->acl_checklist
= clientAclChecklistCreate(Config
.accessList
.noCache
, http
);
323 aclNBCheck(http
->acl_checklist
, clientCheckNoCacheDone
, http
);
325 clientCheckNoCacheDone(http
->request
->flags
.cachable
, http
);
330 clientCheckNoCacheDone(int answer
, void *data
)
332 clientHttpRequest
*http
= data
;
333 http
->request
->flags
.cachable
= answer
;
334 http
->acl_checklist
= NULL
;
335 clientProcessRequest(http
);
339 clientProcessExpired(void *data
)
341 clientHttpRequest
*http
= data
;
342 char *url
= http
->uri
;
343 StoreEntry
*entry
= NULL
;
344 debug(33, 3) ("clientProcessExpired: '%s'\n", http
->uri
);
345 assert(http
->entry
->lastmod
>= 0);
347 * check if we are allowed to contact other servers
348 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
349 * a stale entry *if* it matches client requirements
351 if (clientOnlyIfCached(http
)) {
352 clientProcessOnlyIfCachedMiss(http
);
355 http
->request
->flags
.refresh
= 1;
356 http
->old_entry
= http
->entry
;
357 http
->old_sc
= http
->sc
;
359 * Assert that 'http' is already a client of old_entry. If
360 * it is not, then the beginning of the object data might get
361 * freed from memory before we need to access it.
363 assert(http
->sc
->callback_data
== http
);
364 entry
= storeCreateEntry(url
,
366 http
->request
->flags
,
367 http
->request
->method
);
368 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
369 http
->sc
= storeClientListAdd(entry
, http
);
371 /* delay_id is already set on original store client */
372 delaySetStoreClient(http
->sc
, delayClient(http
->request
));
374 http
->request
->lastmod
= http
->old_entry
->lastmod
;
375 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry
->lastmod
);
377 http
->out
.offset
= 0;
378 fwdStart(http
->conn
->fd
, http
->entry
, http
->request
);
379 /* Register with storage manager to receive updates when data comes in. */
380 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
381 debug(33, 0) ("clientProcessExpired: found ENTRY_ABORTED object\n");
382 storeClientCopy(http
->sc
, entry
,
386 memAllocate(MEM_CLIENT_SOCK_BUF
),
387 clientHandleIMSReply
,
392 clientGetsOldEntry(StoreEntry
* new_entry
, StoreEntry
* old_entry
, request_t
* request
)
394 const http_status status
= new_entry
->mem_obj
->reply
->sline
.status
;
396 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
399 /* If the reply is a failure then send the old object as a last
401 if (status
>= 500 && status
< 600) {
402 debug(33, 3) ("clientGetsOldEntry: YES, failure reply=%d\n", status
);
405 /* If the reply is anything but "Not Modified" then
406 * we must forward it to the client */
407 if (HTTP_NOT_MODIFIED
!= status
) {
408 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status
);
411 /* If the client did not send IMS in the request, then it
412 * must get the old object, not this "Not Modified" reply */
413 if (!request
->flags
.ims
) {
414 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
417 /* If the client IMS time is prior to the entry LASTMOD time we
418 * need to send the old object */
419 if (modifiedSince(old_entry
, request
)) {
420 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
424 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
430 clientHandleIMSReply(void *data
, char *buf
, ssize_t size
)
432 clientHttpRequest
*http
= data
;
433 StoreEntry
*entry
= http
->entry
;
435 const char *url
= storeUrl(entry
);
436 int unlink_request
= 0;
437 StoreEntry
*oldentry
;
440 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url
, (int) size
);
442 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
445 if (size
< 0 && !EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
446 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
449 mem
= entry
->mem_obj
;
450 status
= mem
->reply
->sline
.status
;
451 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
452 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url
);
453 /* We have an existing entry, but failed to validate it */
454 /* Its okay to send the old one anyway */
455 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
456 storeUnregister(http
->sc
, entry
, http
);
457 storeUnlockObject(entry
);
458 entry
= http
->entry
= http
->old_entry
;
459 http
->sc
= http
->old_sc
;
460 } else if (STORE_PENDING
== entry
->store_status
&& 0 == status
) {
461 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url
);
462 if (size
>= CLIENT_SOCK_SZ
) {
463 /* will not get any bigger than that */
464 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url
);
465 /* use old entry, this repeats the code abovez */
466 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
467 storeUnregister(http
->sc
, entry
, http
);
468 storeUnlockObject(entry
);
469 entry
= http
->entry
= http
->old_entry
;
470 http
->sc
= http
->old_sc
;
473 storeClientCopy(http
->sc
, entry
,
474 http
->out
.offset
+ size
,
478 clientHandleIMSReply
,
482 } else if (clientGetsOldEntry(entry
, http
->old_entry
, http
->request
)) {
483 /* We initiated the IMS request, the client is not expecting
484 * 304, so put the good one back. First, make sure the old entry
485 * headers have been loaded from disk. */
486 oldentry
= http
->old_entry
;
487 http
->log_type
= LOG_TCP_REFRESH_HIT
;
488 if (oldentry
->mem_obj
->request
== NULL
) {
489 oldentry
->mem_obj
->request
= requestLink(mem
->request
);
492 /* Don't memcpy() the whole reply structure here. For example,
493 * www.thegist.com (Netscape/1.13) returns a content-length for
494 * 304's which seems to be the length of the 304 HEADERS!!! and
495 * not the body they refer to. */
496 httpReplyUpdateOnNotModified(oldentry
->mem_obj
->reply
, mem
->reply
);
497 storeTimestampsSet(oldentry
);
498 storeUnregister(http
->sc
, entry
, http
);
499 http
->sc
= http
->old_sc
;
500 storeUnlockObject(entry
);
501 entry
= http
->entry
= oldentry
;
502 entry
->timestamp
= squid_curtime
;
503 if (unlink_request
) {
504 requestUnlink(entry
->mem_obj
->request
);
505 entry
->mem_obj
->request
= NULL
;
508 /* the client can handle this reply, whatever it is */
509 http
->log_type
= LOG_TCP_REFRESH_MISS
;
510 if (HTTP_NOT_MODIFIED
== mem
->reply
->sline
.status
) {
511 httpReplyUpdateOnNotModified(http
->old_entry
->mem_obj
->reply
,
513 storeTimestampsSet(http
->old_entry
);
514 http
->log_type
= LOG_TCP_REFRESH_HIT
;
516 storeUnregister(http
->old_sc
, http
->old_entry
, http
);
517 storeUnlockObject(http
->old_entry
);
520 http
->old_entry
= NULL
; /* done with old_entry */
522 assert(!EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
524 storeClientCopy(http
->sc
, entry
,
532 clientSendMoreData(data
, buf
, size
);
537 modifiedSince(StoreEntry
* entry
, request_t
* request
)
540 MemObject
*mem
= entry
->mem_obj
;
541 time_t mod_time
= entry
->lastmod
;
542 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry
));
544 mod_time
= entry
->timestamp
;
545 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time
);
548 /* Find size of the object */
549 object_length
= mem
->reply
->content_length
;
550 if (object_length
< 0)
551 object_length
= contentLen(entry
);
552 if (mod_time
> request
->ims
) {
553 debug(33, 3) ("--> YES: entry newer than client\n");
555 } else if (mod_time
< request
->ims
) {
556 debug(33, 3) ("--> NO: entry older than client\n");
558 } else if (request
->imslen
< 0) {
559 debug(33, 3) ("--> NO: same LMT, no client length\n");
561 } else if (request
->imslen
== object_length
) {
562 debug(33, 3) ("--> NO: same LMT, same length\n");
565 debug(33, 3) ("--> YES: same LMT, different length\n");
571 clientPurgeRequest(clientHttpRequest
* http
)
574 ErrorState
*err
= NULL
;
577 debug(33, 3) ("Config2.onoff.enable_purge = %d\n", Config2
.onoff
.enable_purge
);
578 if (!Config2
.onoff
.enable_purge
) {
579 http
->log_type
= LOG_TCP_DENIED
;
580 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
581 err
->request
= requestLink(http
->request
);
582 err
->src_addr
= http
->conn
->peer
.sin_addr
;
583 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
584 errorAppendEntry(http
->entry
, err
);
587 http
->log_type
= LOG_TCP_MISS
;
588 /* Release both IP and object cache entries */
589 ipcacheInvalidate(http
->request
->host
);
590 if ((entry
= storeGetPublic(http
->uri
, METHOD_GET
)) == NULL
) {
591 status
= HTTP_NOT_FOUND
;
596 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
599 * Make a new entry to hold the reply to be written
602 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
603 httpReplyReset(r
= http
->entry
->mem_obj
->reply
);
604 httpReplySetHeaders(r
, 1.0, status
, NULL
, NULL
, 0, 0, -1);
605 httpReplySwapOut(r
, http
->entry
);
606 storeComplete(http
->entry
);
610 checkNegativeHit(StoreEntry
* e
)
612 if (!EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
614 if (e
->expires
<= squid_curtime
)
616 if (e
->store_status
!= STORE_OK
)
622 clientUpdateCounters(clientHttpRequest
* http
)
624 int svc_time
= tvSubMsec(http
->start
, current_time
);
626 HierarchyLogEntry
*H
;
627 statCounter
.client_http
.requests
++;
628 if (isTcpHit(http
->log_type
))
629 statCounter
.client_http
.hits
++;
630 if (http
->log_type
== LOG_TCP_HIT
)
631 statCounter
.client_http
.disk_hits
++;
632 else if (http
->log_type
== LOG_TCP_MEM_HIT
)
633 statCounter
.client_http
.mem_hits
++;
634 if (http
->request
->err_type
!= ERR_NONE
)
635 statCounter
.client_http
.errors
++;
636 statHistCount(&statCounter
.client_http
.all_svc_time
, svc_time
);
638 * The idea here is not to be complete, but to get service times
639 * for only well-defined types. For example, we don't include
640 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
641 * (we *tried* to validate it, but failed).
643 switch (http
->log_type
) {
644 case LOG_TCP_REFRESH_HIT
:
645 statHistCount(&statCounter
.client_http
.nh_svc_time
, svc_time
);
647 case LOG_TCP_IMS_HIT
:
648 statHistCount(&statCounter
.client_http
.nm_svc_time
, svc_time
);
651 case LOG_TCP_MEM_HIT
:
652 case LOG_TCP_OFFLINE_HIT
:
653 statHistCount(&statCounter
.client_http
.hit_svc_time
, svc_time
);
656 case LOG_TCP_CLIENT_REFRESH_MISS
:
657 statHistCount(&statCounter
.client_http
.miss_svc_time
, svc_time
);
660 /* make compiler warnings go away */
663 H
= &http
->request
->hier
;
666 statCounter
.cd
.times_used
++;
669 statCounter
.icp
.times_used
++;
671 if (0 != i
->stop
.tv_sec
&& 0 != i
->start
.tv_sec
)
672 statHistCount(&statCounter
.icp
.query_svc_time
,
673 tvSubUsec(i
->start
, i
->stop
));
675 statCounter
.icp
.query_timeouts
++;
678 statCounter
.netdb
.times_used
++;
686 httpRequestFree(void *data
)
688 clientHttpRequest
*http
= data
;
689 clientHttpRequest
**H
;
690 ConnStateData
*conn
= http
->conn
;
692 request_t
*request
= http
->request
;
693 MemObject
*mem
= NULL
;
694 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(http
->entry
));
695 if (!clientCheckTransferDone(http
)) {
698 * DW: this seems odd here, is it really needed? It causes
699 * incomplete transfers to get logged with "000" status
700 * code because http->entry becomes NULL.
702 if ((e
= http
->entry
)) {
704 storeUnregister(http
->sc
, e
, http
);
705 storeUnlockObject(e
);
708 if (http
->entry
&& http
->entry
->ping_status
== PING_WAITING
)
709 storeReleaseRequest(http
->entry
);
711 assert(http
->log_type
< LOG_TYPE_MAX
);
713 mem
= http
->entry
->mem_obj
;
714 if (http
->out
.size
|| http
->log_type
) {
715 http
->al
.icp
.opcode
= ICP_INVALID
;
716 http
->al
.url
= http
->log_uri
;
717 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http
->al
.url
);
719 http
->al
.http
.code
= mem
->reply
->sline
.status
;
720 http
->al
.http
.content_type
= strBuf(mem
->reply
->content_type
);
722 http
->al
.cache
.caddr
= conn
->log_addr
;
723 http
->al
.cache
.size
= http
->out
.size
;
724 http
->al
.cache
.code
= http
->log_type
;
725 http
->al
.cache
.msec
= tvSubMsec(http
->start
, current_time
);
730 packerToMemInit(&p
, &mb
);
731 httpHeaderPackInto(&request
->header
, &p
);
732 http
->al
.http
.method
= request
->method
;
733 http
->al
.http
.version
= request
->http_ver
;
734 http
->al
.headers
.request
= xstrdup(mb
.buf
);
735 http
->al
.hier
= request
->hier
;
736 if (request
->user_ident
[0])
737 http
->al
.cache
.ident
= request
->user_ident
;
739 http
->al
.cache
.ident
= conn
->ident
;
743 accessLogLog(&http
->al
);
744 clientUpdateCounters(http
);
745 clientdbUpdate(conn
->peer
.sin_addr
, http
->log_type
, PROTO_HTTP
, http
->out
.size
);
747 if (http
->acl_checklist
)
748 aclChecklistFree(http
->acl_checklist
);
750 checkFailureRatio(request
->err_type
, http
->al
.hier
.code
);
751 safe_free(http
->uri
);
752 safe_free(http
->log_uri
);
753 safe_free(http
->al
.headers
.request
);
754 safe_free(http
->al
.headers
.reply
);
755 safe_free(http
->redirect
.location
);
756 stringClean(&http
->range_iter
.boundary
);
757 if ((e
= http
->entry
)) {
759 storeUnregister(http
->sc
, e
, http
);
761 storeUnlockObject(e
);
763 /* old_entry might still be set if we didn't yet get the reply
764 * code in clientHandleIMSReply() */
765 if ((e
= http
->old_entry
)) {
766 http
->old_entry
= NULL
;
767 storeUnregister(http
->old_sc
, e
, http
);
769 storeUnlockObject(e
);
771 requestUnlink(http
->request
);
772 assert(http
!= http
->next
);
773 assert(http
->conn
->chr
!= NULL
);
774 H
= &http
->conn
->chr
;
783 dlinkDelete(&http
->active
, &ClientActiveRequests
);
787 /* This is a handler normally called by comm_close() */
789 connStateFree(int fd
, void *data
)
791 ConnStateData
*connState
= data
;
792 clientHttpRequest
*http
;
793 debug(33, 3) ("connStateFree: FD %d\n", fd
);
794 assert(connState
!= NULL
);
795 clientdbEstablished(connState
->peer
.sin_addr
, -1); /* decrement */
796 while ((http
= connState
->chr
) != NULL
) {
797 assert(http
->conn
== connState
);
798 assert(connState
->chr
!= connState
->chr
->next
);
799 httpRequestFree(http
);
801 safe_free(connState
->in
.buf
);
802 /* XXX account connState->in.buf */
803 pconnHistCount(0, connState
->nrequests
);
804 cbdataFree(connState
);
806 /* prevent those nasty RST packets */
808 char buf
[SQUID_TCP_SO_RCVBUF
];
809 while (read(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0);
815 clientInterpretRequestHeaders(clientHttpRequest
* http
)
817 request_t
*request
= http
->request
;
818 const HttpHeader
*req_hdr
= &request
->header
;
820 #if defined(USE_USERAGENT_LOG) || defined(USE_REFERER_LOG)
823 request
->imslen
= -1;
824 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
825 if (request
->ims
> 0)
826 request
->flags
.ims
= 1;
827 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
828 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
829 if (strListIsMember(&s
, "no-cache", ','))
833 request
->cache_control
= httpHeaderGetCc(req_hdr
);
834 if (request
->cache_control
)
835 if (EBIT_TEST(request
->cache_control
->mask
, CC_NO_CACHE
))
839 if (Config
.onoff
.reload_into_ims
)
840 request
->flags
.nocache_hack
= 1;
841 else if (refresh_nocache_hack
)
842 request
->flags
.nocache_hack
= 1;
845 request
->flags
.nocache
= 1;
847 /* ignore range header in non-GETs */
848 if (request
->method
== METHOD_GET
) {
849 request
->range
= httpHeaderGetRange(req_hdr
);
851 request
->flags
.range
= 1;
853 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
854 request
->flags
.auth
= 1;
855 if (request
->login
[0] != '\0')
856 request
->flags
.auth
= 1;
857 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
858 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
860 * ThisCache cannot be a member of Via header, "1.0 ThisCache" can.
861 * Note ThisCache2 has a space prepended to the hostname so we don't
862 * accidentally match super-domains.
864 if (strListIsSubstr(&s
, ThisCache2
, ',')) {
865 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
866 request
, (ObjPackMethod
) & httpRequestPack
);
867 request
->flags
.loopdetect
= 1;
870 fvdbCountVia(strBuf(s
));
874 #if USE_USERAGENT_LOG
875 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
876 logUserAgent(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
);
879 if ((str
= httpHeaderGetStr(req_hdr
, HDR_REFERER
)))
880 logReferer(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
,
884 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
885 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
886 fvdbCountForw(strBuf(s
));
890 if (request
->method
== METHOD_TRACE
) {
891 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
893 if (clientCachable(http
))
894 request
->flags
.cachable
= 1;
895 if (clientHierarchical(http
))
896 request
->flags
.hierarchical
= 1;
897 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
898 request
->flags
.nocache
? "SET" : "NOT SET");
899 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
900 request
->flags
.cachable
? "SET" : "NOT SET");
901 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
902 request
->flags
.hierarchical
? "SET" : "NOT SET");
906 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
907 * This is the client-side persistent connection flag. We need
908 * to set this relatively early in the request processing
909 * to handle hacks for broken servers and clients.
912 clientSetKeepaliveFlag(clientHttpRequest
* http
)
914 request_t
*request
= http
->request
;
915 const HttpHeader
*req_hdr
= &request
->header
;
916 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
918 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
919 RequestMethodStr
[request
->method
]);
920 if (!Config
.onoff
.client_pconns
)
921 request
->flags
.proxy_keepalive
= 0;
922 else if (httpMsgIsPersistent(request
->http_ver
, req_hdr
))
923 request
->flags
.proxy_keepalive
= 1;
927 clientCheckContentLength(request_t
* r
)
932 /* PUT/POST requires a request entity */
933 return (r
->content_length
>= 0);
936 /* We do not want to see a request entity on GET/HEAD requests */
937 return (r
->content_length
<= 0);
939 /* For other types of requests we don't care */
946 clientCachable(clientHttpRequest
* http
)
948 const char *url
= http
->uri
;
949 request_t
*req
= http
->request
;
950 method_t method
= req
->method
;
951 if (req
->protocol
== PROTO_HTTP
)
952 return httpCachable(method
);
953 /* FTP is always cachable */
954 if (req
->protocol
== PROTO_GOPHER
)
955 return gopherCachable(url
);
956 if (req
->protocol
== PROTO_WAIS
)
958 if (method
== METHOD_CONNECT
)
960 if (method
== METHOD_TRACE
)
962 if (req
->protocol
== PROTO_CACHEOBJ
)
967 /* Return true if we can query our neighbors for this object */
969 clientHierarchical(clientHttpRequest
* http
)
971 const char *url
= http
->uri
;
972 request_t
*request
= http
->request
;
973 method_t method
= request
->method
;
974 const wordlist
*p
= NULL
;
976 /* IMS needs a private key, so we can use the hierarchy for IMS only
977 * if our neighbors support private keys */
978 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
980 if (request
->flags
.auth
)
982 if (method
== METHOD_TRACE
)
984 if (method
!= METHOD_GET
)
986 /* scan hierarchy_stoplist */
987 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
988 if (strstr(url
, p
->key
))
990 if (request
->flags
.loopdetect
)
992 if (request
->protocol
== PROTO_HTTP
)
993 return httpCachable(method
);
994 if (request
->protocol
== PROTO_GOPHER
)
995 return gopherCachable(url
);
996 if (request
->protocol
== PROTO_WAIS
)
998 if (request
->protocol
== PROTO_CACHEOBJ
)
1004 isTcpHit(log_type code
)
1006 /* this should be a bitmap for better optimization */
1007 if (code
== LOG_TCP_HIT
)
1009 if (code
== LOG_TCP_IMS_HIT
)
1011 if (code
== LOG_TCP_REFRESH_FAIL_HIT
)
1013 if (code
== LOG_TCP_REFRESH_HIT
)
1015 if (code
== LOG_TCP_NEGATIVE_HIT
)
1017 if (code
== LOG_TCP_MEM_HIT
)
1019 if (code
== LOG_TCP_OFFLINE_HIT
)
1025 * returns true if If-Range specs match reply, false otherwise
1028 clientIfRangeMatch(clientHttpRequest
* http
, HttpReply
* rep
)
1030 const TimeOrTag spec
= httpHeaderGetTimeOrTag(&http
->request
->header
, HDR_IF_RANGE
);
1031 /* check for parsing falure */
1036 ETag rep_tag
= httpHeaderGetETag(&rep
->header
, HDR_ETAG
);
1037 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
1038 spec
.tag
.str
, rep_tag
.str
? rep_tag
.str
: "<none>");
1040 return 0; /* entity has no etag to compare with! */
1041 if (spec
.tag
.weak
|| rep_tag
.weak
) {
1042 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
1043 spec
.tag
.str
, rep_tag
.str
);
1044 return 0; /* must use strong validator for sub-range requests */
1046 return etagIsEqual(&rep_tag
, &spec
.tag
);
1048 /* got modification time? */
1049 if (spec
.time
>= 0) {
1050 return http
->entry
->lastmod
<= spec
.time
;
1052 assert(0); /* should not happen */
1056 /* returns expected content length for multi-range replies
1057 * note: assumes that httpHdrRangeCanonize has already been called
1058 * warning: assumes that HTTP headers for individual ranges at the
1059 * time of the actuall assembly will be exactly the same as
1060 * the headers when clientMRangeCLen() is called */
1062 clientMRangeCLen(clientHttpRequest
* http
)
1065 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1066 const HttpHdrRangeSpec
*spec
;
1069 assert(http
->entry
->mem_obj
);
1072 while ((spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
))) {
1074 /* account for headers for this range */
1076 clientPackRangeHdr(http
->entry
->mem_obj
->reply
,
1077 spec
, http
->range_iter
.boundary
, &mb
);
1080 /* account for range content */
1081 clen
+= spec
->length
;
1083 debug(33, 6) ("clientMRangeCLen: (clen += %d + %d) == %d\n",
1084 mb
.size
, spec
->length
, clen
);
1086 /* account for the terminating boundary */
1088 clientPackTermBound(http
->range_iter
.boundary
, &mb
);
1095 /* adds appropriate Range headers if needed */
1097 clientBuildRangeHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1099 HttpHeader
*hdr
= rep
? &rep
->header
: 0;
1100 const char *range_err
= NULL
;
1101 request_t
*request
= http
->request
;
1102 assert(request
->range
);
1103 /* check if we still want to do ranges */
1105 range_err
= "no [parse-able] reply";
1106 else if (rep
->sline
.status
!= HTTP_OK
)
1107 range_err
= "wrong status code";
1108 else if (httpHeaderHas(hdr
, HDR_CONTENT_RANGE
))
1109 range_err
= "origin server does ranges";
1110 else if (rep
->content_length
< 0)
1111 range_err
= "unknown length";
1112 else if (rep
->content_length
!= http
->entry
->mem_obj
->reply
->content_length
)
1113 range_err
= "INCONSISTENT length"; /* a bug? */
1114 else if (httpHeaderHas(&http
->request
->header
, HDR_IF_RANGE
) && !clientIfRangeMatch(http
, rep
))
1115 range_err
= "If-Range match failed";
1116 else if (!httpHdrRangeCanonize(http
->request
->range
, rep
->content_length
))
1117 range_err
= "canonization failed";
1118 else if (httpHdrRangeIsComplex(http
->request
->range
))
1119 range_err
= "too complex range header";
1120 else if (!request
->flags
.cachable
) /* from we_do_ranges in http.c */
1121 range_err
= "non-cachable request";
1122 /* get rid of our range specs on error */
1124 debug(33, 3) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err
);
1125 httpHdrRangeDestroy(http
->request
->range
);
1126 http
->request
->range
= NULL
;
1128 const int spec_count
= http
->request
->range
->specs
.count
;
1129 int actual_clen
= -1;
1131 debug(33, 3) ("clientBuildRangeHeader: range spec count: %d virgin clen: %d\n",
1132 spec_count
, rep
->content_length
);
1133 assert(spec_count
> 0);
1134 /* ETags should not be returned with Partial Content replies? */
1135 httpHeaderDelById(hdr
, HDR_ETAG
);
1136 /* append appropriate header(s) */
1137 if (spec_count
== 1) {
1138 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1139 const HttpHdrRangeSpec
*spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
);
1141 /* append Content-Range */
1142 httpHeaderAddContRange(hdr
, *spec
, rep
->content_length
);
1143 /* set new Content-Length to the actual number of bytes
1144 * transmitted in the message-body */
1145 actual_clen
= spec
->length
;
1148 /* generate boundary string */
1149 http
->range_iter
.boundary
= httpHdrRangeBoundaryStr(http
);
1150 /* delete old Content-Type, add ours */
1151 httpHeaderDelById(hdr
, HDR_CONTENT_TYPE
);
1152 httpHeaderPutStrf(hdr
, HDR_CONTENT_TYPE
,
1153 "multipart/byteranges; boundary=\"%s\"",
1154 strBuf(http
->range_iter
.boundary
));
1155 /* Content-Length is not required in multipart responses
1156 * but it is always nice to have one */
1157 actual_clen
= clientMRangeCLen(http
);
1160 /* replace Content-Length header */
1161 assert(actual_clen
>= 0);
1162 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1163 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, actual_clen
);
1164 debug(33, 3) ("clientBuildRangeHeader: actual content length: %d\n", actual_clen
);
1169 * filters out unwanted entries from original reply header
1170 * adds extra entries if we have more info than origin server
1171 * adds Squid specific entries
1174 clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1176 HttpHeader
*hdr
= &rep
->header
;
1177 int is_hit
= isTcpHit(http
->log_type
);
1178 request_t
*request
= http
->request
;
1179 #if DONT_FILTER_THESE
1180 /* but you might want to if you run Squid as an HTTP accelerator */
1181 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1182 httpHeaderDelById(hdr
, HDR_ETAG
);
1184 httpHeaderDelById(hdr
, HDR_PROXY_CONNECTION
);
1185 /* here: Keep-Alive is a field-name, not a connection directive! */
1186 httpHeaderDelByName(hdr
, "Keep-Alive");
1187 /* remove Set-Cookie if a hit */
1189 httpHeaderDelById(hdr
, HDR_SET_COOKIE
);
1190 /* handle Connection header */
1191 if (httpHeaderHas(hdr
, HDR_CONNECTION
)) {
1192 /* anything that matches Connection list member will be deleted */
1193 String strConnection
= httpHeaderGetList(hdr
, HDR_CONNECTION
);
1194 const HttpHeaderEntry
*e
;
1195 HttpHeaderPos pos
= HttpHeaderInitPos
;
1197 * think: on-average-best nesting of the two loops (hdrEntry
1198 * and strListItem) @?@
1201 * maybe we should delete standard stuff ("keep-alive","close")
1202 * from strConnection first?
1204 while ((e
= httpHeaderGetEntry(hdr
, &pos
))) {
1205 if (strListIsMember(&strConnection
, strBuf(e
->name
), ','))
1206 httpHeaderDelAt(hdr
, pos
);
1208 httpHeaderDelById(hdr
, HDR_CONNECTION
);
1209 stringClean(&strConnection
);
1213 clientBuildRangeHeader(http
, rep
);
1215 * Add a estimated Age header on cache hits.
1219 * Remove any existing Age header sent by upstream caches
1220 * (note that the existing header is passed along unmodified
1223 httpHeaderDelById(hdr
, HDR_AGE
);
1225 * This adds the calculated object age. Note that the details of the
1226 * age calculation is performed by adjusting the timestamp in
1227 * storeTimestampsSet(), not here.
1229 * BROWSER WORKAROUND: IE sometimes hangs when receiving a 0 Age
1230 * header, so don't use it unless there is a age to report. Please
1231 * note that Age is only used to make a conservative estimation of
1232 * the objects age, so a Age: 0 header does not add any useful
1233 * information to the reply in any case.
1235 if (http
->entry
->timestamp
> -1)
1236 if (http
->entry
->timestamp
< squid_curtime
)
1237 httpHeaderPutInt(hdr
, HDR_AGE
,
1238 squid_curtime
- http
->entry
->timestamp
);
1240 /* Append X-Cache */
1241 httpHeaderPutStrf(hdr
, HDR_X_CACHE
, "%s from %s",
1242 is_hit
? "HIT" : "MISS", getMyHostname());
1243 #if USE_CACHE_DIGESTS
1244 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1245 httpHeaderPutStrf(hdr
, HDR_X_CACHE_LOOKUP
, "%s from %s:%d",
1246 http
->lookup_type
? http
->lookup_type
: "NONE",
1247 getMyHostname(), ntohs(Config
.Sockaddr
.http
->s
.sin_port
));
1249 if (httpReplyBodySize(request
->method
, rep
) < 0) {
1250 debug(33, 3) ("clientBuildReplyHeader: can't keep-alive, unknown body size\n");
1251 request
->flags
.proxy_keepalive
= 0;
1253 /* Signal keep-alive if needed */
1254 httpHeaderPutStr(hdr
,
1255 http
->flags
.accel
? HDR_CONNECTION
: HDR_PROXY_CONNECTION
,
1256 request
->flags
.proxy_keepalive
? "keep-alive" : "close");
1257 #if ADD_X_REQUEST_URI
1259 * Knowing the URI of the request is useful when debugging persistent
1260 * connections in a client; we cannot guarantee the order of http headers,
1261 * but X-Request-URI is likely to be the very last header to ease use from a
1262 * debugger [hdr->entries.count-1].
1264 httpHeaderPutStr(hdr
, HDR_X_REQUEST_URI
,
1265 http
->entry
->mem_obj
->url
? http
->entry
->mem_obj
->url
: http
->uri
);
1270 clientBuildReply(clientHttpRequest
* http
, const char *buf
, size_t size
)
1272 HttpReply
*rep
= httpReplyCreate();
1273 size_t k
= headersEnd(buf
, size
);
1274 if (k
&& httpReplyParse(rep
, buf
, k
)) {
1275 /* enforce 1.0 reply version */
1276 rep
->sline
.version
= 1.0;
1277 /* do header conversions */
1278 clientBuildReplyHeader(http
, rep
);
1279 /* if we do ranges, change status to "Partial Content" */
1280 if (http
->request
->range
)
1281 httpStatusLineSet(&rep
->sline
, rep
->sline
.version
,
1282 HTTP_PARTIAL_CONTENT
, NULL
);
1284 /* parsing failure, get rid of the invalid reply */
1285 httpReplyDestroy(rep
);
1287 /* if we were going to do ranges, backoff */
1288 if (http
->request
->range
) {
1289 /* this will fail and destroy request->range */
1290 clientBuildRangeHeader(http
, rep
);
1297 * clientCacheHit should only be called until the HTTP reply headers
1298 * have been parsed. Normally this should be a single call, but
1299 * it might take more than one. As soon as we have the headers,
1300 * we hand off to clientSendMoreData, clientProcessExpired, or
1301 * clientProcessMiss.
1304 clientCacheHit(void *data
, char *buf
, ssize_t size
)
1306 clientHttpRequest
*http
= data
;
1307 StoreEntry
*e
= http
->entry
;
1309 request_t
*r
= http
->request
;
1310 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http
->uri
, (int) size
);
1311 if (http
->entry
== NULL
) {
1312 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1313 debug(33, 3) ("clientCacheHit: request aborted\n");
1315 } else if (size
< 0) {
1316 /* swap in failure */
1317 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1318 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http
->uri
);
1319 http
->log_type
= LOG_TCP_SWAPFAIL_MISS
;
1320 if ((e
= http
->entry
)) {
1322 storeUnregister(http
->sc
, e
, http
);
1324 storeUnlockObject(e
);
1326 clientProcessMiss(http
);
1331 assert(!EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
1332 if (mem
->reply
->sline
.status
== 0) {
1334 * we don't have full reply headers yet; either wait for more or
1335 * punt to clientProcessMiss.
1337 if (e
->mem_status
== IN_MEMORY
|| e
->store_status
== STORE_OK
) {
1338 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1339 clientProcessMiss(http
);
1340 } else if (size
== CLIENT_SOCK_SZ
&& http
->out
.offset
== 0) {
1341 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1342 clientProcessMiss(http
);
1344 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1345 storeClientCopy(http
->sc
, e
,
1346 http
->out
.offset
+ size
,
1356 * Got the headers, now grok them
1358 assert(http
->log_type
== LOG_TCP_HIT
);
1359 if (checkNegativeHit(e
)) {
1360 http
->log_type
= LOG_TCP_NEGATIVE_HIT
;
1361 clientSendMoreData(data
, buf
, size
);
1362 } else if (r
->method
== METHOD_HEAD
) {
1364 * RFC 2068 seems to indicate there is no "conditional HEAD"
1365 * request. We cannot validate a cached object for a HEAD
1366 * request, nor can we return 304.
1368 if (e
->mem_status
== IN_MEMORY
)
1369 http
->log_type
= LOG_TCP_MEM_HIT
;
1370 clientSendMoreData(data
, buf
, size
);
1371 } else if (refreshCheckHTTP(e
, r
) && !http
->flags
.internal
) {
1372 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1374 * We hold a stale copy; it needs to be validated
1377 * The 'need_validation' flag is used to prevent forwarding
1378 * loops between siblings. If our copy of the object is stale,
1379 * then we should probably only use parents for the validation
1380 * request. Otherwise two siblings could generate a loop if
1381 * both have a stale version of the object.
1383 r
->flags
.need_validation
= 1;
1384 if (e
->lastmod
< 0) {
1386 * Previous reply didn't have a Last-Modified header,
1387 * we cannot revalidate it.
1389 http
->log_type
= LOG_TCP_MISS
;
1390 clientProcessMiss(http
);
1391 } else if (r
->flags
.nocache
) {
1393 * This did not match a refresh pattern that overrides no-cache
1394 * we should honour the client no-cache header.
1396 http
->log_type
= LOG_TCP_CLIENT_REFRESH_MISS
;
1397 clientProcessMiss(http
);
1398 } else if (r
->protocol
== PROTO_HTTP
) {
1400 * Object needs to be revalidated
1401 * XXX This could apply to FTP as well, if Last-Modified is known.
1403 http
->log_type
= LOG_TCP_REFRESH_MISS
;
1404 clientProcessExpired(http
);
1407 * We don't know how to re-validate other protocols. Handle
1408 * them as if the object has expired.
1410 http
->log_type
= LOG_TCP_MISS
;
1411 clientProcessMiss(http
);
1413 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1414 } else if (r
->flags
.ims
) {
1416 * Handle If-Modified-Since requests from the client
1418 if (mem
->reply
->sline
.status
!= HTTP_OK
) {
1419 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1420 mem
->reply
->sline
.status
);
1421 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1422 http
->log_type
= LOG_TCP_MISS
;
1423 clientProcessMiss(http
);
1424 } else if (modifiedSince(e
, http
->request
)) {
1425 http
->log_type
= LOG_TCP_IMS_HIT
;
1426 clientSendMoreData(data
, buf
, size
);
1428 time_t timestamp
= e
->timestamp
;
1429 MemBuf mb
= httpPacked304Reply(e
->mem_obj
->reply
);
1430 http
->log_type
= LOG_TCP_IMS_HIT
;
1431 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1432 storeUnregister(http
->sc
, e
, http
);
1434 storeUnlockObject(e
);
1435 e
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
1437 * Copy timestamp from the original entry so the 304
1438 * reply has a meaningful Age: header.
1440 e
->timestamp
= timestamp
;
1442 httpReplyParse(e
->mem_obj
->reply
, mb
.buf
, mb
.size
);
1443 storeAppend(e
, mb
.buf
, mb
.size
);
1449 * plain ol' cache hit
1451 if (e
->mem_status
== IN_MEMORY
)
1452 http
->log_type
= LOG_TCP_MEM_HIT
;
1453 else if (Config
.onoff
.offline
)
1454 http
->log_type
= LOG_TCP_OFFLINE_HIT
;
1455 clientSendMoreData(data
, buf
, size
);
1459 /* put terminating boundary for multiparts */
1461 clientPackTermBound(String boundary
, MemBuf
* mb
)
1463 memBufPrintf(mb
, "\r\n--%s--\r\n", strBuf(boundary
));
1464 debug(33, 6) ("clientPackTermBound: buf offset: %d\n", mb
->size
);
1467 /* appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */
1469 clientPackRangeHdr(const HttpReply
* rep
, const HttpHdrRangeSpec
* spec
, String boundary
, MemBuf
* mb
)
1477 debug(33, 5) ("clientPackRangeHdr: appending boundary: %s\n", strBuf(boundary
));
1478 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1479 memBufPrintf(mb
, "\r\n--%s\r\n", strBuf(boundary
));
1481 /* stuff the header with required entries and pack it */
1482 httpHeaderInit(&hdr
, hoReply
);
1483 if (httpHeaderHas(&rep
->header
, HDR_CONTENT_TYPE
))
1484 httpHeaderPutStr(&hdr
, HDR_CONTENT_TYPE
, httpHeaderGetStr(&rep
->header
, HDR_CONTENT_TYPE
));
1485 httpHeaderAddContRange(&hdr
, *spec
, rep
->content_length
);
1486 packerToMemInit(&p
, mb
);
1487 httpHeaderPackInto(&hdr
, &p
);
1489 httpHeaderClean(&hdr
);
1491 /* append <crlf> (we packed a header, not a reply) */
1492 memBufPrintf(mb
, crlf
);
1496 * extracts a "range" from *buf and appends them to mb, updating
1497 * all offsets and such.
1500 clientPackRange(clientHttpRequest
* http
,
1501 HttpHdrRangeIter
* i
,
1506 const ssize_t copy_sz
= i
->debt_size
<= *size
? i
->debt_size
: *size
;
1507 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1511 * intersection of "have" and "need" ranges must not be empty
1513 assert(body_off
< i
->spec
->offset
+ i
->spec
->length
);
1514 assert(body_off
+ *size
> i
->spec
->offset
);
1516 * put boundary and headers at the beginning of a range in a
1519 if (http
->request
->range
->specs
.count
> 1 && i
->debt_size
== i
->spec
->length
) {
1520 assert(http
->entry
->mem_obj
);
1522 http
->entry
->mem_obj
->reply
, /* original reply */
1523 i
->spec
, /* current range */
1524 i
->boundary
, /* boundary, the same for all */
1531 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz
);
1532 memBufAppend(mb
, *buf
, copy_sz
);
1537 i
->debt_size
-= copy_sz
;
1538 body_off
+= copy_sz
;
1540 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1544 assert(*size
>= 0 && i
->debt_size
>= 0);
1547 /* returns true if there is still data available to pack more ranges
1548 * increments iterator "i"
1549 * used by clientPackMoreRanges */
1551 clientCanPackMoreRanges(const clientHttpRequest
* http
, HttpHdrRangeIter
* i
, ssize_t size
)
1553 /* first update "i" if needed */
1554 if (!i
->debt_size
) {
1555 if ((i
->spec
= httpHdrRangeGetSpec(http
->request
->range
, &i
->pos
)))
1556 i
->debt_size
= i
->spec
->length
;
1558 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1559 /* continue condition: need_more_data && have_more_data */
1560 return i
->spec
&& size
> 0;
1563 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1564 /* returns true if we need more data */
1566 clientPackMoreRanges(clientHttpRequest
* http
, const char *buf
, ssize_t size
, MemBuf
* mb
)
1568 HttpHdrRangeIter
*i
= &http
->range_iter
;
1569 /* offset in range specs does not count the prefix of an http msg */
1570 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1572 /* check: reply was parsed and range iterator was initialized */
1573 assert(i
->prefix_size
> 0);
1574 /* filter out data according to range specs */
1575 while (clientCanPackMoreRanges(http
, i
, size
)) {
1576 off_t start
; /* offset of still missing data */
1578 start
= i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
;
1579 debug(33, 3) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1580 (int) body_off
, size
);
1581 debug(33, 3) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1582 (int) start
, (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
, i
->debt_size
);
1583 assert(body_off
<= start
); /* we did not miss it */
1584 /* skip up to start */
1585 if (body_off
+ size
> start
) {
1586 const size_t skip_size
= start
- body_off
;
1591 /* has not reached start yet */
1596 /* put next chunk if any */
1598 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1599 clientPackRange(http
, i
, &buf
, &size
, mb
);
1600 body_off
= http
->out
.offset
- i
->prefix_size
; /* sync */
1603 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1604 debug(33, 3) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1605 (int) body_off
, size
, i
->debt_size
);
1607 debug(33, 3) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1608 (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
);
1609 /* skip the data we do not need if possible */
1610 if (i
->debt_size
== i
->spec
->length
) /* at the start of the cur. spec */
1611 body_off
= i
->spec
->offset
;
1613 assert(body_off
== i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
);
1614 } else if (http
->request
->range
->specs
.count
> 1) {
1615 /* put terminating boundary for multiparts */
1616 clientPackTermBound(i
->boundary
, mb
);
1618 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1619 return i
->debt_size
> 0;
1623 clientReplyBodyTooLarge(int clen
)
1625 if (0 == Config
.maxReplyBodySize
)
1626 return 0; /* disabled */
1628 return 0; /* unknown */
1629 if (clen
> Config
.maxReplyBodySize
)
1630 return 1; /* too large */
1635 clientRequestBodyTooLarge(int clen
)
1637 if (0 == Config
.maxRequestBodySize
)
1638 return 0; /* disabled */
1640 return 0; /* unknown, bug? */
1641 if (clen
> Config
.maxRequestBodySize
)
1642 return 1; /* too large */
1647 * accepts chunk of a http message in buf, parses prefix, filters headers and
1648 * such, writes processed message to the client's socket
1651 clientSendMoreData(void *data
, char *buf
, ssize_t size
)
1653 clientHttpRequest
*http
= data
;
1654 StoreEntry
*entry
= http
->entry
;
1655 ConnStateData
*conn
= http
->conn
;
1657 HttpReply
*rep
= NULL
;
1658 const char *body_buf
= buf
;
1659 ssize_t body_size
= size
;
1661 ssize_t check_size
= 0;
1662 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http
->uri
, (int) size
);
1663 assert(size
<= CLIENT_SOCK_SZ
);
1664 assert(http
->request
!= NULL
);
1665 dlinkDelete(&http
->active
, &ClientActiveRequests
);
1666 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1667 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1668 fd
, storeUrl(entry
), (int) http
->out
.offset
);
1669 if (conn
->chr
!= http
) {
1670 /* there is another object in progress, defer this one */
1671 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry
));
1672 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1674 } else if (entry
&& EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1675 /* call clientWriteComplete so the client socket gets closed */
1676 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1677 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1679 } else if (size
< 0) {
1680 /* call clientWriteComplete so the client socket gets closed */
1681 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1682 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1684 } else if (size
== 0) {
1685 /* call clientWriteComplete so the client socket gets closed */
1686 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1687 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1690 if (http
->out
.offset
== 0) {
1691 if (Config
.onoff
.log_mime_hdrs
) {
1693 if ((k
= headersEnd(buf
, size
))) {
1694 safe_free(http
->al
.headers
.reply
);
1695 http
->al
.headers
.reply
= xcalloc(k
+ 1, 1);
1696 xstrncpy(http
->al
.headers
.reply
, buf
, k
);
1699 rep
= clientBuildReply(http
, buf
, size
);
1700 if (rep
&& clientReplyBodyTooLarge(rep
->content_length
)) {
1701 ErrorState
*err
= errorCon(ERR_TOO_BIG
, HTTP_FORBIDDEN
);
1702 err
->request
= requestLink(http
->request
);
1703 storeUnregister(http
->sc
, http
->entry
, http
);
1705 storeUnlockObject(http
->entry
);
1706 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
1707 null_request_flags
);
1708 errorAppendEntry(http
->entry
, err
);
1709 httpReplyDestroy(rep
);
1712 body_size
= size
- rep
->hdr_sz
;
1713 assert(body_size
>= 0);
1714 body_buf
= buf
+ rep
->hdr_sz
;
1715 http
->range_iter
.prefix_size
= rep
->hdr_sz
;
1716 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1717 body_size
, rep
->hdr_sz
);
1718 } else if (size
< CLIENT_SOCK_SZ
&& entry
->store_status
== STORE_PENDING
) {
1719 /* wait for more to arrive */
1720 storeClientCopy(http
->sc
, entry
,
1721 http
->out
.offset
+ size
,
1729 /* reset range iterator */
1730 http
->range_iter
.pos
= HttpHdrRangeInitPos
;
1731 } else if (!http
->request
->range
) {
1732 /* Avoid copying to MemBuf for non-range requests */
1733 /* Note, if we're here, then 'rep' is known to be NULL */
1734 http
->out
.offset
+= body_size
;
1735 comm_write(fd
, buf
, size
, clientWriteBodyComplete
, http
, NULL
);
1736 /* NULL because clientWriteBodyComplete frees it */
1739 if (http
->request
->method
== METHOD_HEAD
) {
1741 /* do not forward body for HEAD replies */
1743 http
->flags
.done_copying
= 1;
1746 * If we are here, then store_status == STORE_OK and it
1747 * seems we have a HEAD repsponse which is missing the
1748 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1749 * does this). Because clientBuildReply() fails we just
1750 * call this reply a body, set the done_copying flag and
1753 http
->flags
.done_copying
= 1;
1756 /* write headers and/or body if any */
1757 assert(rep
|| (body_buf
&& body_size
));
1758 /* init mb; put status line and headers if any */
1760 mb
= httpReplyPack(rep
);
1761 http
->out
.offset
+= rep
->hdr_sz
;
1762 check_size
+= rep
->hdr_sz
;
1764 headersLog(0, 0, http
->request
->method
, rep
);
1766 httpReplyDestroy(rep
);
1771 /* append body if any */
1772 if (http
->request
->range
) {
1773 /* Only GET requests should have ranges */
1774 assert(http
->request
->method
== METHOD_GET
);
1775 /* clientPackMoreRanges() updates http->out.offset */
1776 /* force the end of the transfer if we are done */
1777 if (!clientPackMoreRanges(http
, body_buf
, body_size
, &mb
))
1778 http
->flags
.done_copying
= 1;
1779 } else if (body_buf
&& body_size
) {
1780 http
->out
.offset
+= body_size
;
1781 check_size
+= body_size
;
1782 memBufAppend(&mb
, body_buf
, body_size
);
1784 if (!http
->request
->range
&& http
->request
->method
== METHOD_GET
)
1785 assert(check_size
== size
);
1787 comm_write_mbuf(fd
, mb
, clientWriteComplete
, http
);
1788 /* if we don't do it, who will? */
1789 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1793 * clientWriteBodyComplete is called for MEM_CLIENT_SOCK_BUF's
1794 * written directly to the client socket, versus copying to a MemBuf
1795 * and going through comm_write_mbuf. Most non-range responses after
1796 * the headers probably go through here.
1799 clientWriteBodyComplete(int fd
, char *buf
, size_t size
, int errflag
, void *data
)
1802 * NOTE: clientWriteComplete doesn't currently use its "buf"
1803 * (second) argument, so we pass in NULL.
1805 clientWriteComplete(fd
, NULL
, size
, errflag
, data
);
1806 memFree(buf
, MEM_CLIENT_SOCK_BUF
);
1810 clientKeepaliveNextRequest(clientHttpRequest
* http
)
1812 ConnStateData
*conn
= http
->conn
;
1814 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn
->fd
);
1815 conn
->defer
.until
= 0; /* Kick it to read a new request */
1816 httpRequestFree(http
);
1817 if ((http
= conn
->chr
) == NULL
) {
1818 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1820 fd_note(conn
->fd
, "Reading next request");
1822 * Set the timeout BEFORE calling clientReadRequest().
1824 commSetTimeout(conn
->fd
, Config
.Timeout
.pconn
, requestTimeout
, conn
);
1825 clientReadRequest(conn
->fd
, conn
); /* Read next request */
1827 * Note, the FD may be closed at this point.
1829 } else if ((entry
= http
->entry
) == NULL
) {
1831 * this request is in progress, maybe doing an ACL or a redirect,
1832 * execution will resume after the operation completes.
1835 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1838 if (0 == storeClientCopyPending(http
->sc
, entry
, http
)) {
1839 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1840 debug(33, 0) ("clientKeepaliveNextRequest: ENTRY_ABORTED\n");
1841 storeClientCopy(http
->sc
, entry
,
1845 memAllocate(MEM_CLIENT_SOCK_BUF
),
1853 clientWriteComplete(int fd
, char *bufnotused
, size_t size
, int errflag
, void *data
)
1855 clientHttpRequest
*http
= data
;
1856 StoreEntry
*entry
= http
->entry
;
1858 http
->out
.size
+= size
;
1859 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1860 fd
, size
, errflag
, (int) http
->out
.offset
, entry
? objectLen(entry
) : 0);
1862 kb_incr(&statCounter
.client_http
.kbytes_out
, size
);
1863 if (isTcpHit(http
->log_type
))
1864 kb_incr(&statCounter
.client_http
.hit_kbytes_out
, size
);
1868 * just close the socket, httpRequestFree will abort if needed
1871 } else if (NULL
== entry
) {
1872 comm_close(fd
); /* yuk */
1873 } else if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1875 } else if ((done
= clientCheckTransferDone(http
)) != 0 || size
== 0) {
1876 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd
);
1877 /* We're finished case */
1878 if (httpReplyBodySize(http
->request
->method
, entry
->mem_obj
->reply
) < 0) {
1879 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1882 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1884 } else if (clientGotNotEnough(http
)) {
1885 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1887 } else if (http
->request
->flags
.proxy_keepalive
) {
1888 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd
);
1889 clientKeepaliveNextRequest(http
);
1893 } else if (clientReplyBodyTooLarge((int) http
->out
.offset
)) {
1896 /* More data will be coming from primary server; register with
1897 * storage manager. */
1898 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
1899 debug(33, 0) ("clientWriteComplete 2: ENTRY_ABORTED\n");
1900 storeClientCopy(http
->sc
, entry
,
1904 memAllocate(MEM_CLIENT_SOCK_BUF
),
1911 * client issued a request with an only-if-cached cache-control directive;
1912 * we did not find a cached object that can be returned without
1913 * contacting other servers;
1914 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1917 clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
)
1919 char *url
= http
->uri
;
1920 request_t
*r
= http
->request
;
1921 ErrorState
*err
= NULL
;
1922 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1923 RequestMethodStr
[r
->method
], url
);
1924 http
->al
.http
.code
= HTTP_GATEWAY_TIMEOUT
;
1925 err
= errorCon(ERR_ONLY_IF_CACHED_MISS
, HTTP_GATEWAY_TIMEOUT
);
1926 err
->request
= requestLink(r
);
1927 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1929 storeUnregister(http
->sc
, http
->entry
, http
);
1931 storeUnlockObject(http
->entry
);
1933 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1934 errorAppendEntry(http
->entry
, err
);
1938 * Return true if the first range offset is larger than the configured
1939 * limit, UNLESS the request is a hit AND the bits we want are in
1943 clientCheckRangeOffsetLimit(StoreEntry
* entry
, HttpHdrRange
* range
)
1945 ssize_t range_start
;
1947 * If the range offset limit is disabled, don't force a miss.
1949 if (-1 == Config
.rangeOffsetLimit
)
1952 * If the first range offset is less than the configured limit
1953 * we won't force a cache miss.
1955 range_start
= httpHdrRangeFirstOffset(range
);
1956 if (Config
.rangeOffsetLimit
>= range_start
)
1959 * Now we know it's possibly a hit. If we already have the
1960 * whole object cached, we won't force a miss.
1962 if (STORE_OK
== entry
->store_status
)
1963 return 0; /* we have the whole object */
1965 * Now we have a hit on a PENDING object. We need to see
1966 * if the part we want is already cached. If so, we don't
1969 assert(NULL
!= entry
->mem_obj
);
1970 if (range_start
<= entry
->mem_obj
->inmem_hi
)
1973 * Even though we have a PENDING copy of the object, we
1974 * don't want to wait to reach the first range offset,
1975 * so we force a miss for a new range request to the
1982 clientProcessRequest2(clientHttpRequest
* http
)
1984 request_t
*r
= http
->request
;
1986 e
= http
->entry
= storeGetPublic(http
->uri
, r
->method
);
1987 if (r
->method
== METHOD_HEAD
&& e
== NULL
) {
1988 /* We can generate a HEAD reply from a cached GET object */
1989 e
= http
->entry
= storeGetPublic(http
->uri
, METHOD_GET
);
1991 /* Release negatively cached IP-cache entries on reload */
1992 if (r
->flags
.nocache
)
1993 ipcacheInvalidate(r
->host
);
1995 else if (r
->flags
.nocache_hack
)
1996 ipcacheInvalidate(r
->host
);
1998 #if USE_CACHE_DIGESTS
1999 http
->lookup_type
= e
? "HIT" : "MISS";
2002 /* this object isn't in the cache */
2003 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
2004 return LOG_TCP_MISS
;
2006 if (Config
.onoff
.offline
) {
2007 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
2011 if (!storeEntryValidToSend(e
)) {
2012 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
2014 return LOG_TCP_MISS
;
2016 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
2017 /* Special entries are always hits, no matter what the client says */
2018 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
2023 if (e
->store_status
== STORE_PENDING
) {
2024 if (r
->flags
.nocache
|| r
->flags
.nocache_hack
) {
2025 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
2027 r
->flags
.nocache
= 0;
2028 r
->flags
.nocache_hack
= 0;
2032 if (r
->flags
.nocache
) {
2033 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
2035 ipcacheInvalidate(r
->host
);
2036 return LOG_TCP_CLIENT_REFRESH_MISS
;
2038 if (NULL
== r
->range
) {
2040 } else if (httpHdrRangeWillBeComplex(r
->range
)) {
2042 * Some clients break if we return "200 OK" for a Range
2043 * request. We would have to return "200 OK" for a _complex_
2044 * Range request that is also a HIT. Thus, let's prevent HITs
2045 * on complex Range requests
2047 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
2049 r
->flags
.we_dont_do_ranges
= 1;
2050 return LOG_TCP_MISS
;
2051 } else if (clientCheckRangeOffsetLimit(e
, r
->range
)) {
2052 debug(33, 1) ("clientProcessRequest2: forcing miss due to range_offset_limit\n");
2054 r
->flags
.we_dont_do_ranges
= 1;
2055 return LOG_TCP_MISS
;
2057 debug(33, 3) ("clientProcessRequest2: default HIT\n");
2063 clientProcessRequest(clientHttpRequest
* http
)
2065 char *url
= http
->uri
;
2066 request_t
*r
= http
->request
;
2067 int fd
= http
->conn
->fd
;
2069 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
2070 RequestMethodStr
[r
->method
],
2072 if (r
->method
== METHOD_CONNECT
) {
2073 http
->log_type
= LOG_TCP_MISS
;
2074 sslStart(fd
, url
, r
, &http
->out
.size
);
2076 } else if (r
->method
== METHOD_PURGE
) {
2077 clientPurgeRequest(http
);
2079 } else if (r
->method
== METHOD_TRACE
) {
2080 if (r
->max_forwards
== 0) {
2081 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
2082 storeReleaseRequest(http
->entry
);
2083 storeBuffer(http
->entry
);
2084 rep
= httpReplyCreate();
2085 httpReplySetHeaders(rep
, 1.0, HTTP_OK
, NULL
, "text/plain",
2086 httpRequestPrefixLen(r
), 0, squid_curtime
);
2087 httpReplySwapOut(rep
, http
->entry
);
2088 httpReplyDestroy(rep
);
2089 httpRequestSwapOut(r
, http
->entry
);
2090 storeComplete(http
->entry
);
2094 http
->log_type
= LOG_TCP_MISS
;
2095 } else if (r
->content_length
> 0) {
2096 http
->log_type
= LOG_TCP_MISS
;
2097 /* XXX oof, POST can be cached! */
2098 pumpInit(fd
, r
, http
->uri
);
2100 http
->log_type
= clientProcessRequest2(http
);
2102 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
2103 log_tags
[http
->log_type
],
2105 http
->out
.offset
= 0;
2106 if (NULL
!= http
->entry
) {
2107 storeLockObject(http
->entry
);
2108 storeCreateMemObject(http
->entry
, http
->uri
, http
->log_uri
);
2109 http
->entry
->mem_obj
->method
= r
->method
;
2110 http
->sc
= storeClientListAdd(http
->entry
, http
);
2112 delaySetStoreClient(http
->sc
, delayClient(r
));
2114 storeClientCopy(http
->sc
, http
->entry
,
2118 memAllocate(MEM_CLIENT_SOCK_BUF
),
2122 /* MISS CASE, http->log_type is already set! */
2123 clientProcessMiss(http
);
2128 * Prepare to fetch the object as it's a cache miss of some kind.
2131 clientProcessMiss(clientHttpRequest
* http
)
2133 char *url
= http
->uri
;
2134 request_t
*r
= http
->request
;
2135 ErrorState
*err
= NULL
;
2136 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
2137 RequestMethodStr
[r
->method
], url
);
2139 * We might have a left-over StoreEntry from a failed cache hit
2143 if (EBIT_TEST(http
->entry
->flags
, ENTRY_SPECIAL
)) {
2144 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url
);
2145 debug(33, 0) ("\tlog_type = %s\n", log_tags
[http
->log_type
]);
2146 storeEntryDump(http
->entry
, 1);
2148 storeUnregister(http
->sc
, http
->entry
, http
);
2150 storeUnlockObject(http
->entry
);
2153 if (clientOnlyIfCached(http
)) {
2154 clientProcessOnlyIfCachedMiss(http
);
2158 * Deny loops when running in accelerator/transproxy mode.
2160 if (http
->flags
.accel
&& r
->flags
.loopdetect
) {
2161 http
->al
.http
.code
= HTTP_FORBIDDEN
;
2162 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
2163 err
->request
= requestLink(r
);
2164 err
->src_addr
= http
->conn
->peer
.sin_addr
;
2165 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
2166 errorAppendEntry(http
->entry
, err
);
2169 assert(http
->out
.offset
== 0);
2170 http
->entry
= clientCreateStoreEntry(http
, r
->method
, r
->flags
);
2171 if (http
->redirect
.status
) {
2172 HttpReply
*rep
= httpReplyCreate();
2173 #if LOG_TCP_REDIRECTS
2174 http
->log_type
= LOG_TCP_REDIRECT
;
2176 storeReleaseRequest(http
->entry
);
2177 httpRedirectReply(rep
, http
->redirect
.status
, http
->redirect
.location
);
2178 httpReplySwapOut(rep
, http
->entry
);
2179 httpReplyDestroy(rep
);
2180 storeComplete(http
->entry
);
2183 if (http
->flags
.internal
)
2184 r
->protocol
= PROTO_INTERNAL
;
2185 fwdStart(http
->conn
->fd
, http
->entry
, r
);
2188 static clientHttpRequest
*
2189 parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
)
2191 clientHttpRequest
*http
= xcalloc(1, sizeof(clientHttpRequest
));
2192 cbdataAdd(http
, cbdataXfree
, 0);
2194 http
->start
= current_time
;
2195 http
->req_sz
= conn
->in
.offset
;
2196 http
->uri
= xstrdup(uri
);
2197 http
->log_uri
= xstrndup(uri
, MAX_URL
);
2198 http
->range_iter
.boundary
= StringNull
;
2199 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2204 * parseHttpRequest()
2207 * NULL on error or incomplete request
2208 * a clientHttpRequest structure on success
2210 static clientHttpRequest
*
2211 parseHttpRequest(ConnStateData
* conn
, method_t
* method_p
, int *status
,
2212 char **prefix_p
, size_t * req_line_sz_p
)
2217 char *req_hdr
= NULL
;
2222 int free_request
= 0;
2223 size_t header_sz
; /* size of headers, not including first line */
2224 size_t prefix_sz
; /* size of whole request (req-line + headers) */
2228 clientHttpRequest
*http
= NULL
;
2230 struct natlookup natLookup
;
2231 static int natfd
= -1;
2232 static int siocgnatl_cmd
= SIOCGNATL
& 0xff;
2236 if ((req_sz
= headersEnd(conn
->in
.buf
, conn
->in
.offset
)) == 0) {
2237 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
2240 *method_p
= METHOD_NONE
;
2243 assert(req_sz
<= conn
->in
.offset
);
2244 /* Use memcpy, not strdup! */
2245 inbuf
= xmalloc(req_sz
+ 1);
2246 xmemcpy(inbuf
, conn
->in
.buf
, req_sz
);
2247 *(inbuf
+ req_sz
) = '\0';
2249 /* pre-set these values to make aborting simpler */
2251 *method_p
= METHOD_NONE
;
2254 /* Barf on NULL characters in the headers */
2255 if (strlen(inbuf
) != req_sz
) {
2256 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
2257 return parseHttpRequestAbort(conn
, "error:invalid-request");
2259 /* Look for request method */
2260 if ((mstr
= strtok(inbuf
, "\t ")) == NULL
) {
2261 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
2262 return parseHttpRequestAbort(conn
, "error:invalid-request-method");
2264 method
= urlParseMethod(mstr
);
2265 if (method
== METHOD_NONE
) {
2266 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr
);
2267 return parseHttpRequestAbort(conn
, "error:unsupported-request-method");
2269 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr
);
2272 /* look for URL+HTTP/x.x */
2273 if ((url
= strtok(NULL
, "\n")) == NULL
) {
2274 debug(33, 1) ("parseHttpRequest: Missing URL\n");
2275 return parseHttpRequestAbort(conn
, "error:missing-url");
2277 while (xisspace(*url
))
2279 t
= url
+ strlen(url
);
2284 if (xisspace(*t
) && !strncmp(t
+ 1, "HTTP/", 5)) {
2289 while (t
> url
&& xisspace(*t
))
2291 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url
);
2292 if (token
== NULL
) {
2293 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
2294 #if RELAXED_HTTP_PARSER
2295 http_ver
= (float) 0.9; /* wild guess */
2297 return parseHttpRequestAbort(conn
, "error:missing-http-ident");
2300 http_ver
= (float) atof(token
+ 5);
2304 * Process headers after request line
2306 req_hdr
= strtok(NULL
, null_string
);
2307 header_sz
= req_sz
- (req_hdr
- inbuf
);
2308 if (0 == header_sz
) {
2309 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
2313 assert(header_sz
> 0);
2314 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr
);
2315 end
= req_hdr
+ header_sz
;
2316 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end
);
2318 prefix_sz
= end
- inbuf
;
2319 *req_line_sz_p
= req_hdr
- inbuf
;
2320 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
2321 (int) prefix_sz
, (int) *req_line_sz_p
);
2322 assert(prefix_sz
<= conn
->in
.offset
);
2324 /* Ok, all headers are received */
2325 http
= xcalloc(1, sizeof(clientHttpRequest
));
2326 cbdataAdd(http
, cbdataXfree
, 0);
2327 http
->http_ver
= http_ver
;
2329 http
->start
= current_time
;
2330 http
->req_sz
= prefix_sz
;
2331 http
->range_iter
.boundary
= StringNull
;
2332 *prefix_p
= xmalloc(prefix_sz
+ 1);
2333 xmemcpy(*prefix_p
, conn
->in
.buf
, prefix_sz
);
2334 *(*prefix_p
+ prefix_sz
) = '\0';
2335 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
2337 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p
) + *req_line_sz_p
);
2338 if ((t
= strchr(url
, '#'))) /* remove HTML anchors */
2341 /* handle internal objects */
2342 if (internalCheck(url
)) {
2343 /* prepend our name & port */
2344 http
->uri
= xstrdup(internalLocalUri(NULL
, url
));
2345 http
->flags
.internal
= 1;
2346 http
->flags
.accel
= 1;
2348 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2349 else if (Config2
.Accel
.on
&& *url
== '/') {
2350 /* prepend the accel prefix */
2351 if (opt_accel_uses_host
&& (t
= mime_get_header(req_hdr
, "Host"))) {
2352 int vport
= (int) Config
.Accel
.port
;
2354 /* If a Host: header was specified, use it to build the URL
2355 * instead of the one in the Config file. */
2357 * XXX Use of the Host: header here opens a potential
2358 * security hole. There are no checks that the Host: value
2359 * corresponds to one of your servers. It might, for example,
2360 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2361 * types should be used to prevent httpd-accelerators
2362 * handling requests for non-local servers */
2364 if ((q
= strchr(t
, ':'))) {
2368 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
+
2370 http
->uri
= xcalloc(url_sz
, 1);
2371 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2373 } else if (vhost_mode
) {
2375 /* Put the local socket IP address as the hostname */
2376 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
;
2377 http
->uri
= xcalloc(url_sz
, 1);
2379 vport
= (int) ntohs(http
->conn
->me
.sin_port
);
2381 vport
= (int) Config
.Accel
.port
;
2383 natLookup
.nl_inport
= http
->conn
->me
.sin_port
;
2384 natLookup
.nl_outport
= http
->conn
->peer
.sin_port
;
2385 natLookup
.nl_inip
= http
->conn
->me
.sin_addr
;
2386 natLookup
.nl_outip
= http
->conn
->peer
.sin_addr
;
2387 natLookup
.nl_flags
= IPN_TCP
;
2389 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
2391 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2393 return parseHttpRequestAbort(conn
, "error:nat-open-failed");
2396 * IP-Filter changed the type for SIOCGNATL between
2397 * 3.3 and 3.4. It also changed the cmd value for
2398 * SIOCGNATL, so at least we can detect it. We could
2399 * put something in configure and use ifdefs here, but
2400 * this seems simpler.
2402 if (63 == siocgnatl_cmd
) {
2403 struct natlookup
*nlp
= &natLookup
;
2404 x
= ioctl(natfd
, SIOCGNATL
, &nlp
);
2406 x
= ioctl(natfd
, SIOCGNATL
, &natLookup
);
2409 if (errno
!= ESRCH
) {
2410 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2413 return parseHttpRequestAbort(conn
, "error:nat-lookup-failed");
2415 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2416 inet_ntoa(http
->conn
->me
.sin_addr
),
2419 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2420 inet_ntoa(natLookup
.nl_realip
),
2423 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2424 inet_ntoa(http
->conn
->me
.sin_addr
),
2427 debug(33, 5) ("VHOST REWRITE: '%s'\n", http
->uri
);
2429 url_sz
= strlen(Config2
.Accel
.prefix
) + strlen(url
) +
2430 Config
.appendDomainLen
+ 1;
2431 http
->uri
= xcalloc(url_sz
, 1);
2432 snprintf(http
->uri
, url_sz
, "%s%s", Config2
.Accel
.prefix
, url
);
2434 http
->flags
.accel
= 1;
2436 /* URL may be rewritten later, so make extra room */
2437 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
2438 http
->uri
= xcalloc(url_sz
, 1);
2439 strcpy(http
->uri
, url
);
2440 http
->flags
.accel
= 0;
2442 if (!stringHasCntl(http
->uri
))
2443 http
->log_uri
= xstrndup(http
->uri
, MAX_URL
);
2445 http
->log_uri
= xstrndup(rfc1738_escape_unescaped(http
->uri
), MAX_URL
);
2446 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2455 clientReadDefer(int fdnotused
, void *data
)
2457 ConnStateData
*conn
= data
;
2458 return conn
->defer
.until
> squid_curtime
;
2462 clientReadRequest(int fd
, void *data
)
2464 ConnStateData
*conn
= data
;
2465 int parser_return_code
= 0;
2467 request_t
*request
= NULL
;
2470 clientHttpRequest
*http
= NULL
;
2471 clientHttpRequest
**H
= NULL
;
2472 char *prefix
= NULL
;
2473 ErrorState
*err
= NULL
;
2474 fde
*F
= &fd_table
[fd
];
2475 int len
= conn
->in
.size
- conn
->in
.offset
- 1;
2476 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd
);
2477 statCounter
.syscalls
.sock
.reads
++;
2478 size
= read(fd
, conn
->in
.buf
+ conn
->in
.offset
, len
);
2480 fd_bytes(fd
, size
, FD_READ
);
2481 kb_incr(&statCounter
.client_http
.kbytes_in
, size
);
2484 * Don't reset the timeout value here. The timeout value will be
2485 * set to Config.Timeout.request by httpAccept() and
2486 * clientWriteComplete(), and should apply to the request as a
2487 * whole, not individual read() calls. Plus, it breaks our
2488 * lame half-close detection
2490 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, conn
, 0);
2492 if (conn
->chr
== NULL
) {
2493 /* no current or pending requests */
2496 } else if (!Config
.onoff
.half_closed_clients
) {
2497 /* admin doesn't want to support half-closed client sockets */
2501 /* It might be half-closed, we can't tell */
2502 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd
);
2503 F
->flags
.socket_eof
= 1;
2504 conn
->defer
.until
= squid_curtime
+ 1;
2506 fd_note(fd
, "half-closed");
2508 } else if (size
< 0) {
2509 if (!ignoreErrno(errno
)) {
2510 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd
, xstrerror());
2513 } else if (conn
->in
.offset
== 0) {
2514 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd
, xstrerror());
2517 /* Continue to process previously read data */
2520 conn
->in
.offset
+= size
;
2521 /* Skip leading (and trailing) whitespace */
2522 while (conn
->in
.offset
> 0) {
2525 while (conn
->in
.offset
> 0 && xisspace(conn
->in
.buf
[0])) {
2526 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ 1, conn
->in
.offset
- 1);
2529 conn
->in
.buf
[conn
->in
.offset
] = '\0'; /* Terminate the string */
2530 if (conn
->in
.offset
== 0)
2532 /* Limit the number of concurrent requests to 2 */
2533 for (H
= &conn
->chr
, nrequests
= 0; *H
; H
= &(*H
)->next
, nrequests
++);
2534 if (nrequests
>= 2) {
2535 debug(33, 3) ("clientReadRequest: FD %d max concurrent requests reached\n", fd
);
2536 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd
);
2537 conn
->defer
.until
= squid_curtime
+ 100; /* Reset when a request is complete */
2540 /* Process request */
2541 http
= parseHttpRequest(conn
,
2543 &parser_return_code
,
2549 assert(http
->req_sz
> 0);
2550 conn
->in
.offset
-= http
->req_sz
;
2551 assert(conn
->in
.offset
>= 0);
2552 debug(33, 5) ("conn->in.offset = %d\n", (int) conn
->in
.offset
);
2554 * If we read past the end of this request, move the remaining
2555 * data to the beginning
2557 if (conn
->in
.offset
> 0)
2558 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ http
->req_sz
, conn
->in
.offset
);
2559 /* add to the client request queue */
2560 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2563 commSetTimeout(fd
, Config
.Timeout
.lifetime
, NULL
, NULL
);
2564 if (parser_return_code
< 0) {
2565 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd
);
2566 err
= errorCon(ERR_INVALID_REQ
, HTTP_BAD_REQUEST
);
2567 err
->request_hdrs
= xstrdup(conn
->in
.buf
);
2568 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2569 errorAppendEntry(http
->entry
, err
);
2573 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
2574 debug(33, 5) ("Invalid URL: %s\n", http
->uri
);
2575 err
= errorCon(ERR_INVALID_URL
, HTTP_BAD_REQUEST
);
2576 err
->src_addr
= conn
->peer
.sin_addr
;
2577 err
->url
= xstrdup(http
->uri
);
2578 http
->al
.http
.code
= err
->http_status
;
2579 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2580 errorAppendEntry(http
->entry
, err
);
2584 /* compile headers */
2585 /* we should skip request line! */
2586 if (!httpRequestParseHeader(request
, prefix
+ req_line_sz
))
2587 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2589 /* continue anyway? */
2591 request
->flags
.accelerated
= http
->flags
.accel
;
2592 if (!http
->flags
.internal
) {
2593 if (internalCheck(strBuf(request
->urlpath
))) {
2594 if (internalHostnameIs(request
->host
) &&
2595 request
->port
== ntohs(Config
.Sockaddr
.http
->s
.sin_port
)) {
2596 http
->flags
.internal
= 1;
2597 } else if (internalStaticCheck(strBuf(request
->urlpath
))) {
2598 xstrncpy(request
->host
, internalHostname(), SQUIDHOSTNAMELEN
);
2599 request
->port
= ntohs(Config
.Sockaddr
.http
->s
.sin_port
);
2600 http
->flags
.internal
= 1;
2605 * cache the Content-length value in request_t.
2607 request
->content_length
= httpHeaderGetInt(&request
->header
,
2608 HDR_CONTENT_LENGTH
);
2609 request
->flags
.internal
= http
->flags
.internal
;
2611 safe_free(http
->log_uri
);
2612 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
2613 request
->client_addr
= conn
->peer
.sin_addr
;
2614 request
->my_addr
= conn
->me
.sin_addr
;
2615 request
->my_port
= ntohs(conn
->me
.sin_port
);
2616 request
->http_ver
= http
->http_ver
;
2617 if (!urlCheckRequest(request
)) {
2618 err
= errorCon(ERR_UNSUP_REQ
, HTTP_NOT_IMPLEMENTED
);
2619 err
->src_addr
= conn
->peer
.sin_addr
;
2620 err
->request
= requestLink(request
);
2621 request
->flags
.proxy_keepalive
= 0;
2622 http
->al
.http
.code
= err
->http_status
;
2623 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2624 errorAppendEntry(http
->entry
, err
);
2627 if (0 == clientCheckContentLength(request
)) {
2628 err
= errorCon(ERR_INVALID_REQ
, HTTP_LENGTH_REQUIRED
);
2629 err
->src_addr
= conn
->peer
.sin_addr
;
2630 err
->request
= requestLink(request
);
2631 http
->al
.http
.code
= err
->http_status
;
2632 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2633 errorAppendEntry(http
->entry
, err
);
2636 http
->request
= requestLink(request
);
2638 * We need to set the keepalive flag before doing some
2639 * hacks for POST/PUT requests below. Maybe we could
2640 * set keepalive flag even earlier.
2642 clientSetKeepaliveFlag(http
);
2644 * break here if the request has a content-length
2645 * because there is a reqeust body following and we
2646 * don't want to parse it as though it was new request.
2648 if (request
->content_length
>= 0) {
2649 int copy_len
= XMIN(conn
->in
.offset
, request
->content_length
);
2651 assert(conn
->in
.offset
>= copy_len
);
2652 request
->body_sz
= copy_len
;
2653 request
->body
= xmalloc(request
->body_sz
);
2654 xmemcpy(request
->body
, conn
->in
.buf
, request
->body_sz
);
2655 conn
->in
.offset
-= copy_len
;
2656 if (conn
->in
.offset
)
2657 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ copy_len
, conn
->in
.offset
);
2660 * if we didn't get the full body now, then more will
2661 * be arriving on the client socket. Lets cancel
2662 * the read handler until this request gets forwarded.
2664 if (request
->body_sz
< request
->content_length
)
2665 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2666 if (request
->content_length
< 0)
2668 else if (clientRequestBodyTooLarge(request
->content_length
)) {
2669 err
= errorCon(ERR_TOO_BIG
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2670 err
->request
= requestLink(request
);
2671 http
->entry
= clientCreateStoreEntry(http
,
2672 METHOD_NONE
, null_request_flags
);
2673 errorAppendEntry(http
->entry
, err
);
2677 clientAccessCheck(http
);
2678 continue; /* while offset > 0 */
2679 } else if (parser_return_code
== 0) {
2681 * Partial request received; reschedule until parseHttpRequest()
2682 * is happy with the input
2684 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2686 if (conn
->in
.offset
>= Config
.maxRequestHeaderSize
) {
2687 /* The request is too large to handle */
2688 debug(33, 1) ("Request header is too large (%d bytes)\n",
2689 (int) conn
->in
.offset
);
2690 debug(33, 1) ("Config 'request_header_max_size'= %d bytes.\n",
2691 Config
.maxRequestHeaderSize
);
2692 err
= errorCon(ERR_TOO_BIG
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2693 http
= parseHttpRequestAbort(conn
, "error:request-too-large");
2694 /* add to the client request queue */
2695 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2697 http
->entry
= clientCreateStoreEntry(http
, METHOD_NONE
, null_request_flags
);
2698 errorAppendEntry(http
->entry
, err
);
2701 /* Grow the request memory area to accomodate for a large request */
2702 conn
->in
.size
+= REQUEST_BUF_SIZE
;
2703 conn
->in
.buf
= xrealloc(conn
->in
.buf
, conn
->in
.size
);
2704 /* XXX account conn->in.buf */
2705 debug(33, 3) ("Handling a large request, offset=%d inbufsize=%d\n",
2706 (int) conn
->in
.offset
, conn
->in
.size
);
2707 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2714 /* general lifetime handler for HTTP requests */
2716 requestTimeout(int fd
, void *data
)
2718 #if THIS_CONFUSES_PERSISTENT_CONNECTION_AWARE_BROWSERS_AND_USERS
2719 ConnStateData
*conn
= data
;
2721 debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2722 if (fd_table
[fd
].rwstate
) {
2724 * Some data has been sent to the client, just close the FD
2727 } else if (conn
->nrequests
) {
2729 * assume its a persistent connection; just close it
2736 err
= errorCon(ERR_LIFETIME_EXP
, HTTP_REQUEST_TIMEOUT
);
2737 err
->url
= xstrdup("N/A");
2739 * Normally we shouldn't call errorSend() in client_side.c, but
2740 * it should be okay in this case. Presumably if we get here
2741 * this is the first request for the connection, and no data
2742 * has been written yet
2744 assert(conn
->chr
== NULL
);
2747 * if we don't close() here, we still need a timeout handler!
2749 commSetTimeout(fd
, 30, requestTimeout
, conn
);
2751 * Aha, but we don't want a read handler!
2753 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2757 * Just close the connection to not confuse browsers
2758 * using persistent connections. Some browsers opens
2759 * an connection and then does not use it until much
2760 * later (presumeably because the request triggering
2761 * the open has already been completed on another
2764 debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2770 httpAcceptDefer(int fdunused
, void *dataunused
)
2772 static time_t last_warn
= 0;
2773 if (fdNFree() >= RESERVED_FD
)
2775 if (last_warn
+ 15 < squid_curtime
) {
2776 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2777 last_warn
= squid_curtime
;
2782 /* Handle a new connection on HTTP socket. */
2784 httpAccept(int sock
, void *data
)
2788 ConnStateData
*connState
= NULL
;
2789 struct sockaddr_in peer
;
2790 struct sockaddr_in me
;
2791 int max
= INCOMING_HTTP_MAX
;
2793 static aclCheck_t identChecklist
;
2795 commSetSelect(sock
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2796 while (max
-- && !httpAcceptDefer(sock
, NULL
)) {
2797 memset(&peer
, '\0', sizeof(struct sockaddr_in
));
2798 memset(&me
, '\0', sizeof(struct sockaddr_in
));
2799 if ((fd
= comm_accept(sock
, &peer
, &me
)) < 0) {
2800 if (!ignoreErrno(errno
))
2801 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2805 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd
);
2806 connState
= memAllocate(MEM_CONNSTATEDATA
);
2807 connState
->peer
= peer
;
2808 connState
->log_addr
= peer
.sin_addr
;
2809 connState
->log_addr
.s_addr
&= Config
.Addrs
.client_netmask
.s_addr
;
2812 connState
->in
.size
= REQUEST_BUF_SIZE
;
2813 connState
->in
.buf
= xcalloc(connState
->in
.size
, 1);
2814 cbdataAdd(connState
, memFree
, MEM_CONNSTATEDATA
);
2815 /* XXX account connState->in.buf */
2816 comm_add_close_handler(fd
, connStateFree
, connState
);
2817 if (Config
.onoff
.log_fqdn
)
2818 fqdncache_gethostbyaddr(peer
.sin_addr
, FQDN_LOOKUP_IF_MISS
);
2819 commSetTimeout(fd
, Config
.Timeout
.request
, requestTimeout
, connState
);
2821 identChecklist
.src_addr
= peer
.sin_addr
;
2822 identChecklist
.my_addr
= me
.sin_addr
;
2823 identChecklist
.my_port
= ntohs(me
.sin_port
);
2824 if (aclCheckFast(Config
.accessList
.identLookup
, &identChecklist
))
2825 identStart(&me
, &peer
, clientIdentDone
, connState
);
2827 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, connState
, 0);
2828 commSetDefer(fd
, clientReadDefer
, connState
);
2829 clientdbEstablished(peer
.sin_addr
, 1);
2835 #define SENDING_BODY 0
2836 #define SENDING_HDRSONLY 1
2838 clientCheckTransferDone(clientHttpRequest
* http
)
2840 int sending
= SENDING_BODY
;
2841 StoreEntry
*entry
= http
->entry
;
2848 * For now, 'done_copying' is used for special cases like
2849 * Range and HEAD requests.
2851 if (http
->flags
.done_copying
)
2854 * Handle STORE_OK objects.
2855 * objectLen(entry) will be set proprely.
2857 if (entry
->store_status
== STORE_OK
) {
2858 if (http
->out
.offset
>= objectLen(entry
))
2864 * Now, handle STORE_PENDING objects
2866 mem
= entry
->mem_obj
;
2867 assert(mem
!= NULL
);
2868 assert(http
->request
!= NULL
);
2870 if (reply
->hdr_sz
== 0)
2871 return 0; /* haven't found end of headers yet */
2872 else if (reply
->sline
.status
== HTTP_OK
)
2873 sending
= SENDING_BODY
;
2874 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
2875 sending
= SENDING_HDRSONLY
;
2876 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
2877 sending
= SENDING_HDRSONLY
;
2878 else if (reply
->sline
.status
< HTTP_OK
)
2879 sending
= SENDING_HDRSONLY
;
2880 else if (http
->request
->method
== METHOD_HEAD
)
2881 sending
= SENDING_HDRSONLY
;
2883 sending
= SENDING_BODY
;
2885 * Figure out how much data we are supposed to send.
2886 * If we are sending a body and we don't have a content-length,
2887 * then we must wait for the object to become STORE_OK.
2889 if (sending
== SENDING_HDRSONLY
)
2890 sendlen
= reply
->hdr_sz
;
2891 else if (reply
->content_length
< 0)
2894 sendlen
= reply
->content_length
+ reply
->hdr_sz
;
2896 * Now that we have the expected length, did we send it all?
2898 if (http
->out
.offset
< sendlen
)
2905 clientGotNotEnough(clientHttpRequest
* http
)
2907 int cl
= httpReplyBodySize(http
->request
->method
, http
->entry
->mem_obj
->reply
);
2908 int hs
= http
->entry
->mem_obj
->reply
->hdr_sz
;
2910 if (http
->out
.offset
< cl
+ hs
)
2916 * This function is designed to serve a fairly specific purpose.
2917 * Occasionally our vBNS-connected caches can talk to each other, but not
2918 * the rest of the world. Here we try to detect frequent failures which
2919 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2920 * the failure:success ratio goes above 1.0 then we go into "hit only"
2921 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2922 * will only fetch HITs from us if they are using the ICP protocol. We
2923 * stay in this mode for 5 minutes.
2925 * Duane W., Sept 16, 1996
2929 checkFailureRatio(err_type etype
, hier_code hcode
)
2931 static double magic_factor
= 100.0;
2934 if (hcode
== HIER_NONE
)
2936 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
2937 n_bad
= magic_factor
- n_good
;
2940 case ERR_CONNECT_FAIL
:
2941 case ERR_READ_ERROR
:
2947 request_failure_ratio
= n_bad
/ n_good
;
2948 if (hit_only_mode_until
> squid_curtime
)
2950 if (request_failure_ratio
< 1.0)
2952 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
2953 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2954 FAILURE_MODE_TIME
/ 60);
2955 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
2956 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
2960 clientHttpConnectionsOpen(void)
2962 sockaddr_in_list
*s
;
2964 for (s
= Config
.Sockaddr
.http
; s
; s
= s
->next
) {
2965 if (MAXHTTPPORTS
== NHttpSockets
) {
2966 debug(1, 1) ("WARNING: You have too many 'http_port' lines.\n");
2967 debug(1, 1) (" The limit is %d\n", MAXHTTPPORTS
);
2971 fd
= comm_open(SOCK_STREAM
,
2974 ntohs(s
->s
.sin_port
),
2981 commSetSelect(fd
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2983 * We need to set a defer handler here so that we don't
2984 * peg the CPU with select() when we hit the FD limit.
2986 commSetDefer(fd
, httpAcceptDefer
, NULL
);
2987 debug(1, 1) ("Accepting HTTP connections at %s, port %d, FD %d.\n",
2988 inet_ntoa(s
->s
.sin_addr
),
2989 (int) ntohs(s
->s
.sin_port
),
2991 HttpSockets
[NHttpSockets
++] = fd
;
2993 if (NHttpSockets
< 1)
2994 fatal("Cannot open HTTP Port");
2998 clientHttpConnectionsClose(void)
3001 for (i
= 0; i
< NHttpSockets
; i
++) {
3002 if (HttpSockets
[i
] >= 0) {
3003 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets
[i
]);
3004 comm_close(HttpSockets
[i
]);
3005 HttpSockets
[i
] = -1;