3 * $Id: client_side.cc,v 1.422 1998/11/12 06:28:00 wessels Exp $
5 * DEBUG: section 33 Client-side Routines
6 * AUTHOR: Duane Wessels
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
40 #include <sys/ioctl.h>
42 #include <netinet/tcp.h>
44 #include <ip_compat.h>
52 #define comm_close comm_lingering_close
55 static const char *const crlf
= "\r\n";
56 static const char *const proxy_auth_challenge_fmt
= "Basic realm=\"%s\"";
58 #define REQUEST_BUF_SIZE 4096
59 #define FAILURE_MODE_TIME 300
63 static CWCB clientWriteComplete
;
64 static PF clientReadRequest
;
65 static PF connStateFree
;
66 static PF requestTimeout
;
67 static int CheckQuickAbort2(const clientHttpRequest
*);
68 static int clientCheckTransferDone(clientHttpRequest
*);
69 static int clientGotNotEnough(clientHttpRequest
*);
70 static void CheckQuickAbort(clientHttpRequest
*);
71 static void checkFailureRatio(err_type
, hier_code
);
72 static void clientProcessMiss(clientHttpRequest
*);
73 static void clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
);
74 static clientHttpRequest
*parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
);
75 static clientHttpRequest
*parseHttpRequest(ConnStateData
*, method_t
*, int *, char **, size_t *);
76 static RH clientRedirectDone
;
77 static STCB clientHandleIMSReply
;
78 static int clientGetsOldEntry(StoreEntry
* new, StoreEntry
* old
, request_t
* request
);
79 static int checkAccelOnly(clientHttpRequest
*);
80 static int clientOnlyIfCached(clientHttpRequest
* http
);
81 static STCB clientSendMoreData
;
82 static STCB clientCacheHit
;
83 static void clientSetKeepaliveFlag(clientHttpRequest
*);
84 static void clientInterpretRequestHeaders(clientHttpRequest
*);
85 static void clientProcessRequest(clientHttpRequest
*);
86 static void clientProcessExpired(void *data
);
87 static void clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
);
88 static HttpReply
*clientConstructProxyAuthReply(clientHttpRequest
* http
);
89 static int clientCachable(clientHttpRequest
* http
);
90 static int clientHierarchical(clientHttpRequest
* http
);
91 static int clientCheckContentLength(request_t
* r
);
92 static int httpAcceptDefer(void);
93 static log_type
clientProcessRequest2(clientHttpRequest
* http
);
96 checkAccelOnly(clientHttpRequest
* http
)
98 /* return TRUE if someone makes a proxy request to us and
99 * we are in httpd-accel only mode */
100 if (!Config2
.Accel
.on
)
102 if (Config
.onoff
.accel_with_proxy
)
104 if (http
->request
->protocol
== PROTO_CACHEOBJ
)
106 if (http
->flags
.accel
)
112 clientAccessCheck(void *data
)
114 clientHttpRequest
*http
= data
;
115 ConnStateData
*conn
= http
->conn
;
117 if (Config
.onoff
.ident_lookup
&& conn
->ident
.state
== IDENT_NONE
) {
118 identStart(-1, conn
, clientAccessCheck
, http
);
121 if (checkAccelOnly(http
)) {
122 clientAccessCheckDone(0, http
);
125 browser
= httpHeaderGetStr(&http
->request
->header
, HDR_USER_AGENT
);
126 http
->acl_checklist
= aclChecklistCreate(Config
.accessList
.http
,
131 aclNBCheck(http
->acl_checklist
, clientAccessCheckDone
, http
);
135 * returns true if client specified that the object must come from the cache
136 * witout contacting origin server
139 clientOnlyIfCached(clientHttpRequest
* http
)
141 const request_t
*r
= http
->request
;
143 return r
->cache_control
&&
144 EBIT_TEST(r
->cache_control
->mask
, CC_ONLY_IF_CACHED
);
148 clientConstructProxyAuthReply(clientHttpRequest
* http
)
152 if (!http
->flags
.accel
) {
153 /* Proxy authorisation needed */
154 err
= errorCon(ERR_CACHE_ACCESS_DENIED
,
155 HTTP_PROXY_AUTHENTICATION_REQUIRED
);
157 /* WWW authorisation needed */
158 err
= errorCon(ERR_CACHE_ACCESS_DENIED
, HTTP_UNAUTHORIZED
);
160 err
->request
= requestLink(http
->request
);
161 rep
= errorBuildReply(err
);
163 /* add Authenticate header */
164 if (!http
->flags
.accel
) {
165 /* Proxy authorisation needed */
166 httpHeaderPutStrf(&rep
->header
, HDR_PROXY_AUTHENTICATE
,
167 proxy_auth_challenge_fmt
, Config
.proxyAuthRealm
);
169 /* WWW Authorisation needed */
170 httpHeaderPutStrf(&rep
->header
, HDR_WWW_AUTHENTICATE
,
171 proxy_auth_challenge_fmt
, Config
.proxyAuthRealm
);
177 clientCreateStoreEntry(clientHttpRequest
* h
, method_t m
, request_flags flags
)
181 * For erroneous requests, we might not have a h->request,
182 * so make a fake one.
184 if (h
->request
== NULL
)
185 h
->request
= requestLink(requestCreate(m
, PROTO_NONE
, NULL
));
186 e
= storeCreateEntry(h
->uri
, h
->log_uri
, flags
, m
);
187 storeClientListAdd(e
, h
);
189 delaySetStoreClient(e
, h
, h
->request
->delay_id
);
191 storeClientCopy(e
, 0, 0, CLIENT_SOCK_SZ
,
192 memAllocate(MEM_CLIENT_SOCK_BUF
), clientSendMoreData
, h
);
198 clientAccessCheckDone(int answer
, void *data
)
200 clientHttpRequest
*http
= data
;
202 ErrorState
*err
= NULL
;
203 debug(33, 5) ("clientAccessCheckDone: '%s' answer=%d\n", http
->uri
, answer
);
204 http
->acl_checklist
= NULL
;
205 if (answer
== ACCESS_ALLOWED
) {
206 safe_free(http
->uri
);
207 http
->uri
= xstrdup(urlCanonical(http
->request
));
208 assert(http
->redirect_state
== REDIRECT_NONE
);
209 http
->redirect_state
= REDIRECT_PENDING
;
210 redirectStart(http
, clientRedirectDone
, http
);
211 } else if (answer
== ACCESS_REQ_PROXY_AUTH
) {
212 http
->log_type
= LOG_TCP_DENIED
;
213 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
215 /* create appropriate response */
216 http
->entry
->mem_obj
->reply
= clientConstructProxyAuthReply(http
);
217 httpReplySwapOut(http
->entry
->mem_obj
->reply
, http
->entry
);
218 storeComplete(http
->entry
);
220 debug(33, 5) ("Access Denied: %s\n", http
->uri
);
221 debug(33, 5) ("AclMatchedName = %s\n",
222 AclMatchedName
? AclMatchedName
: "<null>");
223 http
->log_type
= LOG_TCP_DENIED
;
224 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
,
226 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
);
227 /* NOTE: don't use HTTP_UNAUTHORIZED because then the
228 * stupid browser wants us to authenticate */
229 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
230 err
->request
= requestLink(http
->request
);
231 err
->src_addr
= http
->conn
->peer
.sin_addr
;
233 err
->page_id
= page_id
;
234 errorAppendEntry(http
->entry
, err
);
239 clientRedirectDone(void *data
, char *result
)
241 clientHttpRequest
*http
= data
;
242 request_t
*new_request
= NULL
;
243 request_t
*old_request
= http
->request
;
244 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http
->uri
,
245 result
? result
: "NULL");
246 assert(http
->redirect_state
== REDIRECT_PENDING
);
247 http
->redirect_state
= REDIRECT_DONE
;
249 http_status status
= atoi(result
);
250 if (status
== 301 || status
== 302) {
252 if ((t
= strchr(result
, ':')) != NULL
) {
253 http
->redirect
.status
= status
;
254 http
->redirect
.location
= xstrdup(t
+ 1);
256 debug(33, 1) ("clientRedirectDone: bad input: %s\n", result
);
259 if (strcmp(result
, http
->uri
))
260 new_request
= urlParse(old_request
->method
, result
);
263 safe_free(http
->uri
);
264 http
->uri
= xstrdup(urlCanonical(new_request
));
265 new_request
->http_ver
= old_request
->http_ver
;
266 httpHeaderAppend(&new_request
->header
, &old_request
->header
);
267 new_request
->client_addr
= old_request
->client_addr
;
268 new_request
->flags
.redirected
= 1;
269 if (old_request
->body
) {
270 new_request
->body
= xmalloc(old_request
->body_sz
);
271 xmemcpy(new_request
->body
, old_request
->body
, old_request
->body_sz
);
272 new_request
->body_sz
= old_request
->body_sz
;
274 requestUnlink(old_request
);
275 http
->request
= requestLink(new_request
);
277 clientInterpretRequestHeaders(http
);
278 fd_note(http
->conn
->fd
, http
->uri
);
279 clientProcessRequest(http
);
283 clientProcessExpired(void *data
)
285 clientHttpRequest
*http
= data
;
286 char *url
= http
->uri
;
287 StoreEntry
*entry
= NULL
;
288 debug(33, 3) ("clientProcessExpired: '%s'\n", http
->uri
);
289 assert(http
->entry
->lastmod
>= 0);
291 * check if we are allowed to contact other servers
292 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
293 * a stale entry *if* it matches client requirements
295 if (clientOnlyIfCached(http
)) {
296 clientProcessOnlyIfCachedMiss(http
);
299 http
->request
->flags
.refresh
= 1;
300 http
->old_entry
= http
->entry
;
301 entry
= storeCreateEntry(url
,
303 http
->request
->flags
,
304 http
->request
->method
);
305 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
306 storeClientListAdd(entry
, http
);
307 storeClientListAdd(http
->old_entry
, http
);
309 delaySetStoreClient(entry
, http
, http
->request
->delay_id
);
310 delaySetStoreClient(http
->old_entry
, http
, http
->request
->delay_id
);
312 entry
->lastmod
= http
->old_entry
->lastmod
;
313 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry
->lastmod
);
314 entry
->refcount
++; /* EXPIRED CASE */
316 http
->out
.offset
= 0;
317 fwdStart(http
->conn
->fd
, http
->entry
, http
->request
,
318 http
->conn
->peer
.sin_addr
);
319 /* Register with storage manager to receive updates when data comes in. */
320 if (entry
->store_status
== STORE_ABORTED
)
321 debug(33, 0) ("clientProcessExpired: entry->swap_status == STORE_ABORTED\n");
322 storeClientCopy(entry
,
326 memAllocate(MEM_CLIENT_SOCK_BUF
),
327 clientHandleIMSReply
,
332 clientGetsOldEntry(StoreEntry
* new_entry
, StoreEntry
* old_entry
, request_t
* request
)
334 const http_status status
= new_entry
->mem_obj
->reply
->sline
.status
;
336 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
339 /* If the reply is anything but "Not Modified" then
340 * we must forward it to the client */
341 if (HTTP_NOT_MODIFIED
!= status
) {
342 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status
);
345 /* If the client did not send IMS in the request, then it
346 * must get the old object, not this "Not Modified" reply */
347 if (!request
->flags
.ims
) {
348 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
351 /* If the client IMS time is prior to the entry LASTMOD time we
352 * need to send the old object */
353 if (modifiedSince(old_entry
, request
)) {
354 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
358 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
364 clientHandleIMSReply(void *data
, char *buf
, ssize_t size
)
366 clientHttpRequest
*http
= data
;
367 StoreEntry
*entry
= http
->entry
;
368 MemObject
*mem
= entry
->mem_obj
;
369 const char *url
= storeUrl(entry
);
370 int unlink_request
= 0;
371 StoreEntry
*oldentry
;
373 const http_status status
= mem
->reply
->sline
.status
;
374 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url
, (int) size
);
375 if (size
< 0 && entry
->store_status
!= STORE_ABORTED
)
378 * This storeAbort() call causes wrong behaviour in some situations.
379 * If there are multiple clients hanging on this entry, the second
380 * client will not want the entry to be aborted. This shows up in
381 * the stack trace below:
383 * #0 0xef5f452c in kill ()
384 * #1 0xef5ba620 in abort ()
385 * #2 0x7d680 in __eprintf (string=0x82a70 "%s:%u: failed assertion `%s'\n", expression=0x82a90 "client_side.c", line=1111, filename=0x83468 "size > 0")
386 * #3 0x2ad7c in clientCacheHit (data=0x31ae318, buf=0x17f1450 "", size=0) at client_side.c:1111
387 * #4 0x65cd4 in storeClientCopy2 (e=0x4c48c0, sc=0x2eedd70) at store_client.c:264
388 * #5 0x665b0 in InvokeHandlers (e=0x4c48c0) at store_client.c:510
389 * #6 0x63c90 in storeAbort (e=0x4c48c0, cbflag=1) at store.c:626
390 * #7 0x29190 in clientHandleIMSReply (data=0x31e9518, buf=0x2268810 "", size=-1) at client_side.c:343
391 * #8 0x66450 in storeUnregister (e=0x2910c, data=0x31e9518) at store_client.c:467
392 * #9 0x29a54 in httpRequestFree (data=0x31e9518) at client_side.c:579
393 * #10 0x29eb4 in connStateFree (fd=534528, data=0x314d0d0) at client_side.c:667
394 * #11 0x2f27c in commCallCloseHandlers (fd=42) at comm.c:501
395 * #12 0x2f410 in comm_close (fd=42) at comm.c:565
396 * #13 0x2d054 in clientReadRequest (fd=42, data=0x314d0d0) at client_side.c:2038
397 * #14 0x309c8 in comm_poll (msec=434) at comm_select.c:354
398 * #15 0x4da64 in main (argc=5, argv=0xeffffd4c) at main.c:587
400 storeAbort(entry
, 1);
403 * Lets just try to return here and see what kind of problems that
407 if (entry
->store_status
== STORE_ABORTED
) {
408 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url
);
409 /* We have an existing entry, but failed to validate it */
410 /* Its okay to send the old one anyway */
411 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
412 storeUnregister(entry
, http
);
413 storeUnlockObject(entry
);
414 entry
= http
->entry
= http
->old_entry
;
416 } else if (STORE_PENDING
== entry
->store_status
&& 0 == status
) {
417 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url
);
418 if (size
>= CLIENT_SOCK_SZ
) {
419 /* will not get any bigger than that */
420 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url
);
421 /* use old entry, this repeats the code abovez */
422 http
->log_type
= LOG_TCP_REFRESH_FAIL_HIT
;
423 storeUnregister(entry
, http
);
424 storeUnlockObject(entry
);
425 entry
= http
->entry
= http
->old_entry
;
429 storeClientCopy(entry
,
430 http
->out
.offset
+ size
,
434 clientHandleIMSReply
,
438 } else if (clientGetsOldEntry(entry
, http
->old_entry
, http
->request
)) {
439 /* We initiated the IMS request, the client is not expecting
440 * 304, so put the good one back. First, make sure the old entry
441 * headers have been loaded from disk. */
442 oldentry
= http
->old_entry
;
443 http
->log_type
= LOG_TCP_REFRESH_HIT
;
444 if (oldentry
->mem_obj
->request
== NULL
) {
445 oldentry
->mem_obj
->request
= requestLink(mem
->request
);
448 /* Don't memcpy() the whole reply structure here. For example,
449 * www.thegist.com (Netscape/1.13) returns a content-length for
450 * 304's which seems to be the length of the 304 HEADERS!!! and
451 * not the body they refer to. */
452 httpReplyUpdateOnNotModified(oldentry
->mem_obj
->reply
, mem
->reply
);
453 storeTimestampsSet(oldentry
);
454 storeUnregister(entry
, http
);
455 storeUnlockObject(entry
);
456 entry
= http
->entry
= oldentry
;
457 entry
->timestamp
= squid_curtime
;
458 if (unlink_request
) {
459 requestUnlink(entry
->mem_obj
->request
);
460 entry
->mem_obj
->request
= NULL
;
463 /* the client can handle this reply, whatever it is */
464 http
->log_type
= LOG_TCP_REFRESH_MISS
;
465 if (HTTP_NOT_MODIFIED
== mem
->reply
->sline
.status
) {
466 http
->old_entry
->timestamp
= squid_curtime
;
467 http
->old_entry
->refcount
++;
468 http
->log_type
= LOG_TCP_REFRESH_HIT
;
470 storeUnregister(http
->old_entry
, http
);
471 storeUnlockObject(http
->old_entry
);
474 http
->old_entry
= NULL
; /* done with old_entry */
475 assert(entry
->store_status
!= STORE_ABORTED
);
477 storeClientCopy(entry
,
485 clientSendMoreData(data
, buf
, size
);
490 modifiedSince(StoreEntry
* entry
, request_t
* request
)
493 MemObject
*mem
= entry
->mem_obj
;
494 time_t mod_time
= entry
->lastmod
;
495 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry
));
497 mod_time
= entry
->timestamp
;
498 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time
);
501 /* Find size of the object */
502 object_length
= mem
->reply
->content_length
;
503 if (object_length
< 0)
504 object_length
= contentLen(entry
);
505 if (mod_time
> request
->ims
) {
506 debug(33, 3) ("--> YES: entry newer than client\n");
508 } else if (mod_time
< request
->ims
) {
509 debug(33, 3) ("--> NO: entry older than client\n");
511 } else if (request
->imslen
< 0) {
512 debug(33, 3) ("--> NO: same LMT, no client length\n");
514 } else if (request
->imslen
== object_length
) {
515 debug(33, 3) ("--> NO: same LMT, same length\n");
518 debug(33, 3) ("--> YES: same LMT, different length\n");
524 clientPurgeRequest(clientHttpRequest
* http
)
527 ErrorState
*err
= NULL
;
529 debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config
.onoff
.enable_purge
);
530 if (!Config
.onoff
.enable_purge
) {
531 http
->log_type
= LOG_TCP_DENIED
;
532 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
533 err
->request
= requestLink(http
->request
);
534 err
->src_addr
= http
->conn
->peer
.sin_addr
;
535 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
536 errorAppendEntry(http
->entry
, err
);
539 http
->log_type
= LOG_TCP_MISS
;
540 if ((entry
= storeGetPublic(http
->uri
, METHOD_GET
)) == NULL
) {
541 http
->http_code
= HTTP_NOT_FOUND
;
544 http
->http_code
= HTTP_OK
;
546 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
549 * Make a new entry to hold the reply to be written
552 http
->entry
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
553 httpReplyReset(r
= http
->entry
->mem_obj
->reply
);
554 httpReplySetHeaders(r
, 1.0, http
->http_code
, NULL
, NULL
, 0, 0, -1);
555 httpReplySwapOut(r
, http
->entry
);
556 storeComplete(http
->entry
);
560 checkNegativeHit(StoreEntry
* e
)
562 if (!EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
564 if (e
->expires
<= squid_curtime
)
566 if (e
->store_status
!= STORE_OK
)
572 clientUpdateCounters(clientHttpRequest
* http
)
574 int svc_time
= tvSubMsec(http
->start
, current_time
);
576 HierarchyLogEntry
*H
;
577 Counter
.client_http
.requests
++;
578 if (isTcpHit(http
->log_type
))
579 Counter
.client_http
.hits
++;
580 if (http
->request
->err_type
!= ERR_NONE
)
581 Counter
.client_http
.errors
++;
582 statHistCount(&Counter
.client_http
.all_svc_time
, svc_time
);
584 * The idea here is not to be complete, but to get service times
585 * for only well-defined types. For example, we don't include
586 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
587 * (we *tried* to validate it, but failed).
589 switch (http
->log_type
) {
590 case LOG_TCP_REFRESH_HIT
:
591 statHistCount(&Counter
.client_http
.nh_svc_time
, svc_time
);
593 case LOG_TCP_IMS_HIT
:
594 statHistCount(&Counter
.client_http
.nm_svc_time
, svc_time
);
597 case LOG_TCP_MEM_HIT
:
598 case LOG_TCP_OFFLINE_HIT
:
599 statHistCount(&Counter
.client_http
.hit_svc_time
, svc_time
);
602 case LOG_TCP_CLIENT_REFRESH_MISS
:
603 statHistCount(&Counter
.client_http
.miss_svc_time
, svc_time
);
606 /* make compiler warnings go away */
609 H
= &http
->request
->hier
;
612 Counter
.cd
.times_used
++;
615 Counter
.icp
.times_used
++;
617 if (0 != i
->stop
.tv_sec
&& 0 != i
->start
.tv_sec
)
618 statHistCount(&Counter
.icp
.query_svc_time
,
619 tvSubUsec(i
->start
, i
->stop
));
621 Counter
.icp
.query_timeouts
++;
624 Counter
.netdb
.times_used
++;
632 httpRequestFree(void *data
)
634 clientHttpRequest
*http
= data
;
635 clientHttpRequest
**H
;
636 ConnStateData
*conn
= http
->conn
;
637 StoreEntry
*entry
= http
->entry
;
638 request_t
*request
= http
->request
;
639 MemObject
*mem
= NULL
;
640 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(entry
));
641 if (!clientCheckTransferDone(http
)) {
643 storeUnregister(entry
, http
); /* unregister BEFORE abort */
644 CheckQuickAbort(http
);
645 entry
= http
->entry
; /* reset, IMS might have changed it */
646 if (entry
&& entry
->ping_status
== PING_WAITING
)
647 storeReleaseRequest(entry
);
649 assert(http
->log_type
< LOG_TYPE_MAX
);
651 mem
= entry
->mem_obj
;
652 if (http
->out
.size
|| http
->log_type
) {
653 http
->al
.icp
.opcode
= ICP_INVALID
;
654 http
->al
.url
= http
->log_uri
;
655 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http
->al
.url
);
657 http
->al
.http
.code
= mem
->reply
->sline
.status
;
658 http
->al
.http
.content_type
= strBuf(mem
->reply
->content_type
);
660 http
->al
.cache
.caddr
= conn
->log_addr
;
661 http
->al
.cache
.size
= http
->out
.size
;
662 http
->al
.cache
.code
= http
->log_type
;
663 http
->al
.cache
.msec
= tvSubMsec(http
->start
, current_time
);
664 if (request
->user_ident
[0])
665 http
->al
.cache
.ident
= request
->user_ident
;
667 http
->al
.cache
.ident
= conn
->ident
.ident
;
672 packerToMemInit(&p
, &mb
);
673 httpHeaderPackInto(&request
->header
, &p
);
674 http
->al
.http
.method
= request
->method
;
675 http
->al
.http
.version
= request
->http_ver
;
676 http
->al
.headers
.request
= xstrdup(mb
.buf
);
677 http
->al
.hier
= request
->hier
;
681 accessLogLog(&http
->al
);
682 clientUpdateCounters(http
);
683 clientdbUpdate(conn
->peer
.sin_addr
, http
->log_type
, PROTO_HTTP
, http
->out
.size
);
685 if (http
->acl_checklist
)
686 aclChecklistFree(http
->acl_checklist
);
688 checkFailureRatio(request
->err_type
, http
->al
.hier
.code
);
689 safe_free(http
->uri
);
690 safe_free(http
->log_uri
);
691 safe_free(http
->al
.headers
.request
);
692 safe_free(http
->al
.headers
.reply
);
693 safe_free(http
->redirect
.location
);
694 stringClean(&http
->range_iter
.boundary
);
697 storeUnregister(entry
, http
);
698 storeUnlockObject(entry
);
700 /* old_entry might still be set if we didn't yet get the reply
701 * code in clientHandleIMSReply() */
702 if (http
->old_entry
) {
703 storeUnregister(http
->old_entry
, http
);
704 storeUnlockObject(http
->old_entry
);
705 http
->old_entry
= NULL
;
707 requestUnlink(http
->request
);
708 assert(http
!= http
->next
);
709 assert(http
->conn
->chr
!= NULL
);
710 H
= &http
->conn
->chr
;
722 /* This is a handler normally called by comm_close() */
724 connStateFree(int fd
, void *data
)
726 ConnStateData
*connState
= data
;
727 clientHttpRequest
*http
;
728 debug(33, 3) ("connStateFree: FD %d\n", fd
);
729 assert(connState
!= NULL
);
730 while ((http
= connState
->chr
) != NULL
) {
731 assert(http
->conn
== connState
);
732 assert(connState
->chr
!= connState
->chr
->next
);
733 httpRequestFree(http
);
735 if (connState
->ident
.fd
> -1)
736 comm_close(connState
->ident
.fd
);
737 safe_free(connState
->in
.buf
);
738 /* XXX account connState->in.buf */
739 pconnHistCount(0, connState
->nrequests
);
740 cbdataFree(connState
);
742 /* prevent those nasty RST packets */
744 char buf
[SQUID_TCP_SO_RCVBUF
];
745 while (read(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0);
751 clientInterpretRequestHeaders(clientHttpRequest
* http
)
753 request_t
*request
= http
->request
;
754 const HttpHeader
*req_hdr
= &request
->header
;
755 #if USE_USERAGENT_LOG
758 request
->imslen
= -1;
759 request
->ims
= httpHeaderGetTime(req_hdr
, HDR_IF_MODIFIED_SINCE
);
760 if (request
->ims
> 0)
761 request
->flags
.ims
= 1;
762 if (httpHeaderHas(req_hdr
, HDR_PRAGMA
)) {
763 String s
= httpHeaderGetList(req_hdr
, HDR_PRAGMA
);
764 if (strListIsMember(&s
, "no-cache", ',')) {
766 if (Config
.onoff
.reload_into_ims
)
767 request
->flags
.nocache_hack
= 1;
768 else if (refresh_nocache_hack
)
769 request
->flags
.nocache_hack
= 1;
772 request
->flags
.nocache
= 1;
776 /* ignore range header in non-GETs */
777 if (request
->method
== METHOD_GET
) {
778 request
->range
= httpHeaderGetRange(req_hdr
);
780 request
->flags
.range
= 1;
782 if (httpHeaderHas(req_hdr
, HDR_AUTHORIZATION
))
783 request
->flags
.auth
= 1;
784 if (request
->login
[0] != '\0')
785 request
->flags
.auth
= 1;
786 if (httpHeaderHas(req_hdr
, HDR_VIA
)) {
787 String s
= httpHeaderGetList(req_hdr
, HDR_VIA
);
788 /* ThisCache cannot be a member of Via header, "1.0 ThisCache" can */
789 if (strListIsSubstr(&s
, ThisCache
, ',')) {
790 debugObj(33, 1, "WARNING: Forwarding loop detected for:\n",
791 request
, (ObjPackMethod
) & httpRequestPack
);
792 request
->flags
.loopdetect
= 1;
795 fvdbCountVia(strBuf(s
));
799 #if USE_USERAGENT_LOG
800 if ((str
= httpHeaderGetStr(req_hdr
, HDR_USER_AGENT
)))
801 logUserAgent(fqdnFromAddr(http
->conn
->peer
.sin_addr
), str
);
804 if (httpHeaderHas(req_hdr
, HDR_X_FORWARDED_FOR
)) {
805 String s
= httpHeaderGetList(req_hdr
, HDR_X_FORWARDED_FOR
);
806 fvdbCountForw(strBuf(s
));
810 request
->cache_control
= httpHeaderGetCc(req_hdr
);
811 if (request
->method
== METHOD_TRACE
) {
812 request
->max_forwards
= httpHeaderGetInt(req_hdr
, HDR_MAX_FORWARDS
);
814 if (clientCachable(http
))
815 request
->flags
.cachable
= 1;
816 if (clientHierarchical(http
))
817 request
->flags
.hierarchical
= 1;
819 if (delayClient(http
)) {
820 debug(33, 5) ("clientInterpretRequestHeaders: delay request class %d position %d\n",
821 request
->delay_id
>> 16,
822 request
->delay_id
& 0xFFFF);
825 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
826 request
->flags
.nocache
? "SET" : "NOT SET");
827 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
828 request
->flags
.cachable
? "SET" : "NOT SET");
829 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
830 request
->flags
.hierarchical
? "SET" : "NOT SET");
834 * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive.
835 * This is the client-side persistent connection flag. We need
836 * to set this relatively early in the request processing
837 * to handle hacks for broken servers and clients.
840 clientSetKeepaliveFlag(clientHttpRequest
* http
)
842 request_t
*request
= http
->request
;
843 const HttpHeader
*req_hdr
= &request
->header
;
844 debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %3.1f\n",
846 debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n",
847 RequestMethodStr
[request
->method
]);
848 if (httpMsgIsPersistent(request
->http_ver
, req_hdr
))
849 request
->flags
.proxy_keepalive
= 1;
850 if (request
->method
== METHOD_POST
|| request
->method
== METHOD_PUT
)
851 if (!Config
.onoff
.persistent_client_posts
)
852 request
->flags
.proxy_keepalive
= 0;
856 clientCheckContentLength(request_t
* r
)
858 /* We only require a content-length for "upload" methods */
859 if (!pumpMethod(r
->method
))
861 if (httpHeaderGetInt(&r
->header
, HDR_CONTENT_LENGTH
) < 0)
867 clientCachable(clientHttpRequest
* http
)
869 const char *url
= http
->uri
;
870 request_t
*req
= http
->request
;
871 method_t method
= req
->method
;
873 memset(&ch
, '\0', sizeof(ch
));
875 * Hopefully, nobody really wants 'no_cache' by client's IP
876 * address, but if they do, this should work if they use IP
877 * addresses in their ACLs, or if the client's address is in
880 * This may not work yet for 'dst' and 'dst_domain' ACLs.
882 ch
.src_addr
= http
->conn
->peer
.sin_addr
;
883 ch
.request
= http
->request
;
885 * aclCheckFast returns 1 for ALLOW and 0 for DENY. The default
886 * is ALLOW, so we require 'no_cache DENY foo' in squid.conf
887 * to indicate uncachable objects.
889 if (!aclCheckFast(Config
.accessList
.noCache
, &ch
))
891 if (req
->protocol
== PROTO_HTTP
)
892 return httpCachable(method
);
893 /* FTP is always cachable */
894 if (req
->protocol
== PROTO_GOPHER
)
895 return gopherCachable(url
);
896 if (req
->protocol
== PROTO_WAIS
)
898 if (method
== METHOD_CONNECT
)
900 if (method
== METHOD_TRACE
)
902 if (req
->protocol
== PROTO_CACHEOBJ
)
907 /* Return true if we can query our neighbors for this object */
909 clientHierarchical(clientHttpRequest
* http
)
911 const char *url
= http
->uri
;
912 request_t
*request
= http
->request
;
913 method_t method
= request
->method
;
914 const wordlist
*p
= NULL
;
916 /* IMS needs a private key, so we can use the hierarchy for IMS only
917 * if our neighbors support private keys */
918 if (request
->flags
.ims
&& !neighbors_do_private_keys
)
920 if (request
->flags
.auth
)
922 if (method
== METHOD_TRACE
)
924 if (method
!= METHOD_GET
)
926 /* scan hierarchy_stoplist */
927 for (p
= Config
.hierarchy_stoplist
; p
; p
= p
->next
)
928 if (strstr(url
, p
->key
))
930 if (request
->flags
.loopdetect
)
932 if (request
->protocol
== PROTO_HTTP
)
933 return httpCachable(method
);
934 if (request
->protocol
== PROTO_GOPHER
)
935 return gopherCachable(url
);
936 if (request
->protocol
== PROTO_WAIS
)
938 if (request
->protocol
== PROTO_CACHEOBJ
)
944 isTcpHit(log_type code
)
946 /* this should be a bitmap for better optimization */
947 if (code
== LOG_TCP_HIT
)
949 if (code
== LOG_TCP_IMS_HIT
)
951 if (code
== LOG_TCP_REFRESH_FAIL_HIT
)
953 if (code
== LOG_TCP_REFRESH_HIT
)
955 if (code
== LOG_TCP_NEGATIVE_HIT
)
957 if (code
== LOG_TCP_MEM_HIT
)
959 if (code
== LOG_TCP_OFFLINE_HIT
)
965 * returns true if If-Range specs match reply, false otherwise
968 clientIfRangeMatch(clientHttpRequest
* http
, HttpReply
* rep
)
970 const TimeOrTag spec
= httpHeaderGetTimeOrTag(&http
->request
->header
, HDR_IF_RANGE
);
971 /* check for parsing falure */
976 ETag rep_tag
= httpHeaderGetETag(&rep
->header
, HDR_ETAG
);
977 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
978 spec
.tag
.str
, rep_tag
.str
? rep_tag
.str
: "<none>");
980 return 0; /* entity has no etag to compare with! */
981 if (spec
.tag
.weak
|| rep_tag
.weak
) {
982 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
983 spec
.tag
.str
, rep_tag
.str
);
984 return 0; /* must use strong validator for sub-range requests */
986 return etagIsEqual(&rep_tag
, &spec
.tag
);
988 /* got modification time? */
989 if (spec
.time
>= 0) {
990 return http
->entry
->lastmod
<= spec
.time
;
992 assert(0); /* should not happen */
996 /* adds appropriate Range headers if needed */
998 clientBuildRangeHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1000 HttpHeader
*hdr
= rep
? &rep
->header
: 0;
1001 const char *range_err
= NULL
;
1002 assert(http
->request
->range
);
1003 /* check if we still want to do ranges */
1005 range_err
= "no [parse-able] reply";
1006 else if (rep
->sline
.status
!= HTTP_OK
)
1007 range_err
= "wrong status code";
1008 else if (httpHeaderHas(hdr
, HDR_CONTENT_RANGE
))
1009 range_err
= "origin server does ranges";
1010 else if (rep
->content_length
< 0)
1011 range_err
= "unknown length";
1012 else if (rep
->content_length
!= http
->entry
->mem_obj
->reply
->content_length
)
1013 range_err
= "INCONSISTENT length"; /* a bug? */
1014 else if (httpHeaderHas(&http
->request
->header
, HDR_IF_RANGE
) && !clientIfRangeMatch(http
, rep
))
1015 range_err
= "If-Range match failed";
1016 else if (!httpHdrRangeCanonize(http
->request
->range
, rep
->content_length
))
1017 range_err
= "canonization failed";
1018 else if (httpHdrRangeIsComplex(http
->request
->range
))
1019 range_err
= "too complex range header";
1020 /* get rid of our range specs on error */
1022 debug(33, 2) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err
);
1023 httpHdrRangeDestroy(http
->request
->range
);
1024 http
->request
->range
= NULL
;
1026 const int spec_count
= http
->request
->range
->specs
.count
;
1027 debug(33, 2) ("clientBuildRangeHeader: range spec count: %d clen: %d\n",
1028 spec_count
, rep
->content_length
);
1029 assert(spec_count
> 0);
1030 /* ETags should not be returned with Partial Content replies? */
1031 httpHeaderDelById(hdr
, HDR_ETAG
);
1032 /* append appropriate header(s) */
1033 if (spec_count
== 1) {
1034 HttpHdrRangePos pos
= HttpHdrRangeInitPos
;
1035 const HttpHdrRangeSpec
*spec
= httpHdrRangeGetSpec(http
->request
->range
, &pos
);
1037 /* append Content-Range */
1038 httpHeaderAddContRange(hdr
, *spec
, rep
->content_length
);
1039 /* set new Content-Length to the actual number of OCTETs
1040 * transmitted in the message-body */
1041 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1042 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, spec
->length
);
1043 debug(33, 2) ("clientBuildRangeHeader: actual content length: %d\n", spec
->length
);
1046 /* generate boundary string */
1047 http
->range_iter
.boundary
= httpHdrRangeBoundaryStr(http
);
1048 /* delete old Content-Type, add ours */
1049 httpHeaderDelById(hdr
, HDR_CONTENT_TYPE
);
1050 httpHeaderPutStrf(hdr
, HDR_CONTENT_TYPE
,
1051 "multipart/byteranges; boundary=\"%s\"",
1052 strBuf(http
->range_iter
.boundary
));
1053 /* no need for Content-Length in multipart responses */
1054 /* but we must delete the original one if we cannot (yet)
1055 * calculate the actual length */
1056 httpHeaderDelById(hdr
, HDR_CONTENT_LENGTH
);
1061 /* filters out unwanted entries from original reply header
1062 * adds extra entries if we have more info than origin server
1063 * adds Squid specific entries */
1065 clientBuildReplyHeader(clientHttpRequest
* http
, HttpReply
* rep
)
1067 HttpHeader
*hdr
= &rep
->header
;
1068 int is_hit
= isTcpHit(http
->log_type
);
1069 request_t
*request
= http
->request
;
1070 #if DONT_FILTER_THESE
1071 /* but you might want to if you run Squid as an HTTP accelerator */
1072 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
1073 httpHeaderDelById(hdr
, HDR_ETAG
);
1075 httpHeaderDelById(hdr
, HDR_PROXY_CONNECTION
);
1076 /* here: Keep-Alive is a field-name, not a connection directive! */
1077 httpHeaderDelByName(hdr
, "Keep-Alive");
1078 /* remove Set-Cookie if a hit */
1080 httpHeaderDelById(hdr
, HDR_SET_COOKIE
);
1081 /* handle Connection header */
1082 if (httpHeaderHas(hdr
, HDR_CONNECTION
)) {
1083 /* anything that matches Connection list member will be deleted */
1084 String strConnection
= httpHeaderGetList(hdr
, HDR_CONNECTION
);
1085 const HttpHeaderEntry
*e
;
1086 HttpHeaderPos pos
= HttpHeaderInitPos
;
1088 * think: on-average-best nesting of the two loops (hdrEntry
1089 * and strListItem) @?@
1092 * maybe we should delete standard stuff ("keep-alive","close")
1093 * from strConnection first?
1095 while ((e
= httpHeaderGetEntry(hdr
, &pos
))) {
1096 if (strListIsMember(&strConnection
, strBuf(e
->name
), ','))
1097 httpHeaderDelAt(hdr
, pos
);
1099 httpHeaderDelById(hdr
, HDR_CONNECTION
);
1100 stringClean(&strConnection
);
1104 clientBuildRangeHeader(http
, rep
);
1106 * Add Age header, not that our header must replace Age headers
1107 * from other caches if any
1109 if (http
->entry
->timestamp
> 0) {
1110 httpHeaderDelById(hdr
, HDR_AGE
);
1112 * we do not follow HTTP/1.1 precisely here becuase we rely
1113 * on Date header when computing entry->timestamp; we should
1114 * be using _request_ time if Date header is not available
1115 * or if it is out of sync
1117 httpHeaderPutInt(hdr
, HDR_AGE
,
1118 http
->entry
->timestamp
<= squid_curtime
?
1119 squid_curtime
- http
->entry
->timestamp
: 0);
1121 /* Append X-Cache */
1122 httpHeaderPutStrf(hdr
, HDR_X_CACHE
, "%s from %s",
1123 is_hit
? "HIT" : "MISS", getMyHostname());
1124 #if USE_CACHE_DIGESTS
1125 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1126 httpHeaderPutStrf(hdr
, HDR_X_CACHE_LOOKUP
, "%s from %s:%d",
1127 http
->lookup_type
? http
->lookup_type
: "NONE",
1128 getMyHostname(), Config
.Port
.http
->i
);
1131 * Clear keepalive for NON-HEAD requests with invalid content length
1133 if (request
->method
!= METHOD_HEAD
)
1134 if (http
->entry
->mem_obj
->reply
->content_length
< 0)
1135 request
->flags
.proxy_keepalive
= 0;
1136 /* Signal keep-alive if needed */
1137 httpHeaderPutStr(hdr
,
1138 http
->flags
.accel
? HDR_CONNECTION
: HDR_PROXY_CONNECTION
,
1139 request
->flags
.proxy_keepalive
? "keep-alive" : "close");
1140 #if ADD_X_REQUEST_URI
1142 * Knowing the URI of the request is useful when debugging persistent
1143 * connections in a client; we cannot guarantee the order of http headers,
1144 * but X-Request-URI is likely to be the very last header to ease use from a
1145 * debugger [hdr->entries.count-1].
1147 httpHeaderPutStr(hdr
, HDR_X_REQUEST_URI
,
1148 http
->entry
->mem_obj
->url
? http
->entry
->mem_obj
->url
: http
->uri
);
1153 clientBuildReply(clientHttpRequest
* http
, const char *buf
, size_t size
)
1155 HttpReply
*rep
= httpReplyCreate();
1156 if (httpReplyParse(rep
, buf
)) {
1157 /* enforce 1.0 reply version */
1158 rep
->sline
.version
= 1.0;
1159 /* do header conversions */
1160 clientBuildReplyHeader(http
, rep
);
1161 /* if we do ranges, change status to "Partial Content" */
1162 if (http
->request
->range
)
1163 httpStatusLineSet(&rep
->sline
, rep
->sline
.version
, HTTP_PARTIAL_CONTENT
, NULL
);
1165 /* parsing failure, get rid of the invalid reply */
1166 httpReplyDestroy(rep
);
1168 /* if we were going to do ranges, backoff */
1169 if (http
->request
->range
)
1170 clientBuildRangeHeader(http
, rep
); /* will fail and destroy request->range */
1176 * clientCacheHit should only be called until the HTTP reply headers
1177 * have been parsed. Normally this should be a single call, but
1178 * it might take more than one. As soon as we have the headers,
1179 * we hand off to clientSendMoreData, clientProcessExpired, or
1180 * clientProcessMiss.
1183 clientCacheHit(void *data
, char *buf
, ssize_t size
)
1185 clientHttpRequest
*http
= data
;
1186 StoreEntry
*e
= http
->entry
;
1188 request_t
*r
= http
->request
;
1189 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http
->uri
, (int) size
);
1190 if (http
->entry
== NULL
) {
1191 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1192 debug(33, 3) ("clientCacheHit: request aborted\n");
1194 } else if (size
< 0) {
1195 /* swap in failure */
1196 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1197 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http
->uri
);
1198 http
->log_type
= LOG_TCP_SWAPFAIL_MISS
;
1199 if ((e
= http
->entry
)) {
1201 storeUnregister(e
, http
);
1202 storeUnlockObject(e
);
1204 clientProcessMiss(http
);
1209 assert(e
->store_status
!= STORE_ABORTED
);
1210 if (mem
->reply
->sline
.status
== 0) {
1212 * we don't have full reply headers yet; either wait for more or
1213 * punt to clientProcessMiss.
1215 if (e
->mem_status
== IN_MEMORY
|| e
->store_status
== STORE_OK
) {
1216 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1217 clientProcessMiss(http
);
1218 } else if (size
== CLIENT_SOCK_SZ
&& http
->out
.offset
== 0) {
1219 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1220 clientProcessMiss(http
);
1222 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1224 http
->out
.offset
+ size
,
1234 * Got the headers, now grok them
1236 assert(http
->log_type
== LOG_TCP_HIT
);
1237 if (checkNegativeHit(e
)) {
1238 http
->log_type
= LOG_TCP_NEGATIVE_HIT
;
1239 clientSendMoreData(data
, buf
, size
);
1240 } else if (r
->method
== METHOD_HEAD
) {
1242 * RFC 2068 seems to indicate there is no "conditional HEAD"
1243 * request. We cannot validate a cached object for a HEAD
1244 * request, nor can we return 304.
1246 if (e
->mem_status
== IN_MEMORY
)
1247 http
->log_type
= LOG_TCP_MEM_HIT
;
1248 clientSendMoreData(data
, buf
, size
);
1249 } else if (refreshCheckHTTP(e
, r
) && !http
->flags
.internal
) {
1250 debug(33, 5) ("clientCacheHit: in refreshCheck() block\n");
1252 * We hold a stale copy; it needs to be validated
1255 * The 'need_validation' flag is used to prevent forwarding
1256 * loops between siblings. If our copy of the object is stale,
1257 * then we should probably only use parents for the validation
1258 * request. Otherwise two siblings could generate a loop if
1259 * both have a stale version of the object.
1261 r
->flags
.need_validation
= 1;
1262 if (e
->lastmod
< 0) {
1264 * Previous reply didn't have a Last-Modified header,
1265 * we cannot revalidate it.
1267 http
->log_type
= LOG_TCP_MISS
;
1268 clientProcessMiss(http
);
1269 } else if (r
->flags
.nocache
) {
1271 * This did not match a refresh pattern that overrides no-cache
1272 * we should honour the client no-cache header.
1274 http
->log_type
= LOG_TCP_CLIENT_REFRESH_MISS
;
1275 clientProcessMiss(http
);
1276 } else if (r
->protocol
== PROTO_HTTP
) {
1278 * Object needs to be revalidated
1279 * XXX This could apply to FTP as well, if Last-Modified is known.
1281 http
->log_type
= LOG_TCP_REFRESH_MISS
;
1282 clientProcessExpired(http
);
1285 * We don't know how to re-validate other protocols. Handle
1286 * them as if the object has expired.
1288 http
->log_type
= LOG_TCP_MISS
;
1289 clientProcessMiss(http
);
1291 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1292 } else if (r
->flags
.ims
) {
1294 * Handle If-Modified-Since requests from the client
1296 if (mem
->reply
->sline
.status
!= HTTP_OK
) {
1297 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1298 mem
->reply
->sline
.status
);
1299 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1300 clientProcessMiss(http
);
1301 } else if (modifiedSince(e
, http
->request
)) {
1302 http
->log_type
= LOG_TCP_IMS_HIT
;
1303 clientSendMoreData(data
, buf
, size
);
1305 MemBuf mb
= httpPacked304Reply(e
->mem_obj
->reply
);
1306 http
->log_type
= LOG_TCP_IMS_HIT
;
1307 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1308 storeUnregister(e
, http
);
1309 storeUnlockObject(e
);
1310 e
= clientCreateStoreEntry(http
, http
->request
->method
, null_request_flags
);
1312 httpReplyParse(e
->mem_obj
->reply
, mb
.buf
);
1313 storeAppend(e
, mb
.buf
, mb
.size
);
1319 * plain ol' cache hit
1321 if (e
->mem_status
== IN_MEMORY
)
1322 http
->log_type
= LOG_TCP_MEM_HIT
;
1323 else if (Config
.onoff
.offline
)
1324 http
->log_type
= LOG_TCP_OFFLINE_HIT
;
1325 clientSendMoreData(data
, buf
, size
);
1330 /* extracts a "range" from *buf and appends them to mb, updating all offsets and such */
1332 clientPackRange(clientHttpRequest
* http
, HttpHdrRangeIter
* i
, const char **buf
, ssize_t
* size
, MemBuf
* mb
)
1334 const size_t copy_sz
= i
->debt_size
<= *size
? i
->debt_size
: *size
;
1335 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1338 /* intersection of "have" and "need" ranges must not be empty */
1339 assert(body_off
< i
->spec
->offset
+ i
->spec
->length
);
1340 assert(body_off
+ *size
> i
->spec
->offset
);
1341 /* put boundary and headers at the beginning of range in a multi-range */
1342 if (http
->request
->range
->specs
.count
> 1 && i
->debt_size
== i
->spec
->length
) {
1343 HttpReply
*rep
= http
->entry
->mem_obj
? /* original reply */
1344 http
->entry
->mem_obj
->reply
: NULL
;
1349 debug(33, 5) ("clientPackRange: appending boundary: %s\n", strBuf(i
->boundary
));
1350 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1351 memBufPrintf(mb
, "\r\n--%s\r\n", strBuf(i
->boundary
));
1352 httpHeaderInit(&hdr
, hoReply
);
1353 if (httpHeaderHas(&rep
->header
, HDR_CONTENT_TYPE
))
1354 httpHeaderPutStr(&hdr
, HDR_CONTENT_TYPE
, httpHeaderGetStr(&rep
->header
, HDR_CONTENT_TYPE
));
1355 httpHeaderAddContRange(&hdr
, *i
->spec
, rep
->content_length
);
1356 packerToMemInit(&p
, mb
);
1357 httpHeaderPackInto(&hdr
, &p
);
1359 httpHeaderClean(&hdr
);
1360 /* append <crlf> (we packed a header, not a reply */
1361 memBufPrintf(mb
, "\r\n");
1364 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz
);
1365 memBufAppend(mb
, *buf
, copy_sz
);
1366 /* update offsets */
1368 i
->debt_size
-= copy_sz
;
1369 body_off
+= copy_sz
;
1371 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1372 /* paranoid check */
1373 assert(*size
>= 0 && i
->debt_size
>= 0);
1376 /* returns true if there is still data available to pack more ranges
1377 * increments iterator "i"
1378 * used by clientPackMoreRanges */
1380 clientCanPackMoreRanges(const clientHttpRequest
* http
, HttpHdrRangeIter
* i
, ssize_t size
)
1382 /* first update "i" if needed */
1383 if (!i
->debt_size
) {
1384 if ((i
->spec
= httpHdrRangeGetSpec(http
->request
->range
, &i
->pos
)))
1385 i
->debt_size
= i
->spec
->length
;
1387 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1388 /* continue condition: need_more_data && have_more_data */
1389 return i
->spec
&& size
> 0;
1392 /* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1393 /* returns true if we need more data */
1395 clientPackMoreRanges(clientHttpRequest
* http
, const char *buf
, ssize_t size
, MemBuf
* mb
)
1397 HttpHdrRangeIter
*i
= &http
->range_iter
;
1398 /* offset in range specs does not count the prefix of an http msg */
1399 off_t body_off
= http
->out
.offset
- i
->prefix_size
;
1401 /* check: reply was parsed and range iterator was initialized */
1402 assert(i
->prefix_size
> 0);
1403 /* filter out data according to range specs */
1404 /* note: order of loop conditions is significant! */
1405 while (clientCanPackMoreRanges(http
, i
, size
)) {
1406 off_t start
; /* offset of still missing data */
1408 start
= i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
;
1409 debug(33, 2) ("clientPackMoreRanges: in: offset: %d size: %d\n",
1410 (int) body_off
, size
);
1411 debug(33, 2) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
1412 (int) start
, (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
, i
->debt_size
);
1413 assert(body_off
<= start
); /* we did not miss it */
1414 /* skip up to start */
1415 if (body_off
+ size
> start
) {
1416 const size_t skip_size
= start
- body_off
;
1421 /* has not reached start yet */
1426 /* put next chunk if any */
1428 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1429 clientPackRange(http
, i
, &buf
, &size
, mb
);
1430 body_off
= http
->out
.offset
- i
->prefix_size
; /* sync */
1433 assert(!i
->debt_size
== !i
->spec
); /* paranoid sync condition */
1434 debug(33, 2) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
1435 (int) body_off
, size
, i
->debt_size
);
1437 debug(33, 2) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
1438 (int) i
->pos
, i
->spec
->offset
, (int) (i
->spec
->offset
+ i
->spec
->length
), i
->spec
->length
);
1439 /* skip the data we do not need if possible */
1440 if (i
->debt_size
== i
->spec
->length
) /* at the start of the cur. spec */
1441 body_off
= i
->spec
->offset
;
1443 assert(body_off
== i
->spec
->offset
+ i
->spec
->length
- i
->debt_size
);
1444 } else if (http
->request
->range
->specs
.count
> 1) {
1445 /* put terminating boundary for multiparts */
1446 memBufPrintf(mb
, "\r\n--%s--\r\n", strBuf(i
->boundary
));
1448 http
->out
.offset
= body_off
+ i
->prefix_size
; /* sync */
1449 return i
->debt_size
> 0;
1453 * accepts chunk of a http message in buf, parses prefix, filters headers and
1454 * such, writes processed message to the client's socket
1457 clientSendMoreData(void *data
, char *buf
, ssize_t size
)
1459 clientHttpRequest
*http
= data
;
1460 StoreEntry
*entry
= http
->entry
;
1461 ConnStateData
*conn
= http
->conn
;
1463 HttpReply
*rep
= NULL
;
1464 const char *body_buf
= buf
;
1465 ssize_t body_size
= size
;
1467 ssize_t check_size
= 0;
1468 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http
->uri
, (int) size
);
1469 assert(size
<= CLIENT_SOCK_SZ
);
1470 assert(http
->request
!= NULL
);
1471 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
1472 fd
, storeUrl(entry
), (int) http
->out
.offset
);
1473 if (conn
->chr
!= http
) {
1474 /* there is another object in progress, defer this one */
1475 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry
));
1476 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1478 } else if (entry
&& entry
->store_status
== STORE_ABORTED
) {
1479 /* call clientWriteComplete so the client socket gets closed */
1480 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1481 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1483 } else if (size
< 0) {
1484 /* call clientWriteComplete so the client socket gets closed */
1485 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1486 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1488 } else if (size
== 0) {
1489 /* call clientWriteComplete so the client socket gets closed */
1490 clientWriteComplete(fd
, NULL
, 0, COMM_OK
, http
);
1491 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1494 if (http
->out
.offset
== 0) {
1495 if (Config
.onoff
.log_mime_hdrs
) {
1497 if ((k
= headersEnd(buf
, size
))) {
1498 safe_free(http
->al
.headers
.reply
);
1499 http
->al
.headers
.reply
= xcalloc(k
+ 1, 1);
1500 xstrncpy(http
->al
.headers
.reply
, buf
, k
);
1503 rep
= clientBuildReply(http
, buf
, size
);
1505 body_size
= size
- rep
->hdr_sz
;
1506 assert(body_size
>= 0);
1507 body_buf
= buf
+ rep
->hdr_sz
;
1508 http
->range_iter
.prefix_size
= rep
->hdr_sz
;
1509 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1510 body_size
, rep
->hdr_sz
);
1511 } else if (size
< CLIENT_SOCK_SZ
&& entry
->store_status
== STORE_PENDING
) {
1512 /* wait for more to arrive */
1513 storeClientCopy(entry
,
1514 http
->out
.offset
+ size
,
1522 /* reset range iterator */
1523 http
->range_iter
.pos
= HttpHdrRangeInitPos
;
1525 if (http
->request
->method
== METHOD_HEAD
) {
1527 /* do not forward body for HEAD replies */
1529 http
->flags
.done_copying
= 1;
1532 * If we are here, then store_status == STORE_OK and it
1533 * seems we have a HEAD repsponse which is missing the
1534 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1535 * does this). Because clientBuildReply() fails we just
1536 * call this reply a body, set the done_copying flag and
1539 http
->flags
.done_copying
= 1;
1542 /* write headers and/or body if any */
1543 assert(rep
|| (body_buf
&& body_size
));
1544 /* init mb; put status line and headers if any */
1546 mb
= httpReplyPack(rep
);
1547 http
->out
.offset
+= rep
->hdr_sz
;
1548 check_size
+= rep
->hdr_sz
;
1549 httpReplyDestroy(rep
);
1552 /* leave space for growth incase we do ranges */
1553 memBufInit(&mb
, CLIENT_SOCK_SZ
, 2 * CLIENT_SOCK_SZ
);
1555 /* append body if any */
1556 if (body_buf
&& body_size
) {
1557 if (http
->request
->range
) {
1558 /* Only GET requests should have ranges */
1559 assert(http
->request
->method
== METHOD_GET
);
1560 /* clientPackMoreRanges() updates http->out.offset */
1561 /* force the end of the transfer if we are done */
1562 if (!clientPackMoreRanges(http
, body_buf
, body_size
, &mb
))
1563 http
->flags
.done_copying
= 1;
1565 http
->out
.offset
+= body_size
;
1566 check_size
+= body_size
;
1567 memBufAppend(&mb
, body_buf
, body_size
);
1570 if (!http
->request
->range
&& http
->request
->method
== METHOD_GET
)
1571 assert(check_size
== size
);
1573 comm_write_mbuf(fd
, mb
, clientWriteComplete
, http
);
1574 /* if we don't do it, who will? */
1575 memFree(MEM_CLIENT_SOCK_BUF
, buf
);
1579 clientKeepaliveNextRequest(clientHttpRequest
* http
)
1581 ConnStateData
*conn
= http
->conn
;
1583 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn
->fd
);
1584 conn
->defer
.until
= 0; /* Kick it to read a new request */
1585 httpRequestFree(http
);
1586 if ((http
= conn
->chr
) == NULL
) {
1587 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next req\n",
1589 fd_note(conn
->fd
, "Reading next request");
1591 * Set the timeout BEFORE calling clientReadRequest().
1593 commSetTimeout(conn
->fd
, 15, requestTimeout
, conn
);
1594 clientReadRequest(conn
->fd
, conn
); /* Read next request */
1596 * Note, the FD may be closed at this point.
1598 } else if ((entry
= http
->entry
) == NULL
) {
1600 * this request is in progress, maybe doing an ACL or a redirect,
1601 * execution will resume after the operation completes.
1604 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1607 if (0 == storeClientCopyPending(entry
, http
)) {
1608 if (entry
->store_status
== STORE_ABORTED
)
1609 debug(33, 0) ("clientKeepaliveNextRequest: entry->swap_status == STORE_ABORTED\n");
1610 storeClientCopy(entry
,
1614 memAllocate(MEM_CLIENT_SOCK_BUF
),
1622 clientWriteComplete(int fd
, char *bufnotused
, size_t size
, int errflag
, void *data
)
1624 clientHttpRequest
*http
= data
;
1625 StoreEntry
*entry
= http
->entry
;
1627 http
->out
.size
+= size
;
1628 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
1629 fd
, size
, errflag
, (int) http
->out
.offset
, entry
? objectLen(entry
) : 0);
1631 kb_incr(&Counter
.client_http
.kbytes_out
, size
);
1632 if (isTcpHit(http
->log_type
))
1633 kb_incr(&Counter
.client_http
.hit_kbytes_out
, size
);
1637 * just close the socket, httpRequestFree will abort if needed
1640 } else if (NULL
== entry
) {
1641 comm_close(fd
); /* yuk */
1642 } else if (entry
->store_status
== STORE_ABORTED
) {
1644 } else if ((done
= clientCheckTransferDone(http
)) != 0 || size
== 0) {
1645 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd
);
1646 /* We're finished case */
1647 if (http
->entry
->mem_obj
->reply
->content_length
< 0) {
1648 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
1651 debug(33, 5) ("clientWriteComplete: closing, !done\n");
1653 } else if (clientGotNotEnough(http
)) {
1654 debug(33, 5) ("clientWriteComplete: client didn't get all it expected\n");
1656 } else if (http
->request
->flags
.proxy_keepalive
) {
1657 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd
);
1658 clientKeepaliveNextRequest(http
);
1663 /* More data will be coming from primary server; register with
1664 * storage manager. */
1665 if (entry
->store_status
== STORE_ABORTED
)
1666 debug(33, 0) ("clientWriteComplete 2: entry->swap_status == STORE_ABORTED\n");
1667 storeClientCopy(entry
,
1671 memAllocate(MEM_CLIENT_SOCK_BUF
),
1678 * client issued a request with an only-if-cached cache-control directive;
1679 * we did not find a cached object that can be returned without
1680 * contacting other servers;
1681 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1684 clientProcessOnlyIfCachedMiss(clientHttpRequest
* http
)
1686 char *url
= http
->uri
;
1687 request_t
*r
= http
->request
;
1688 ErrorState
*err
= NULL
;
1689 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1690 RequestMethodStr
[r
->method
], url
);
1691 http
->al
.http
.code
= HTTP_GATEWAY_TIMEOUT
;
1692 err
= errorCon(ERR_ONLY_IF_CACHED_MISS
, HTTP_GATEWAY_TIMEOUT
);
1693 err
->request
= requestLink(r
);
1694 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1695 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1696 errorAppendEntry(http
->entry
, err
);
1700 clientProcessRequest2(clientHttpRequest
* http
)
1702 request_t
*r
= http
->request
;
1704 e
= http
->entry
= storeGetPublic(http
->uri
, r
->method
);
1705 if (r
->method
== METHOD_HEAD
&& e
== NULL
) {
1706 /* We can generate a HEAD reply from a cached GET object */
1707 e
= http
->entry
= storeGetPublic(http
->uri
, METHOD_GET
);
1709 #if USE_CACHE_DIGESTS
1710 http
->lookup_type
= e
? "HIT" : "MISS";
1713 /* this object isn't in the cache */
1714 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
1715 return LOG_TCP_MISS
;
1717 if (Config
.onoff
.offline
) {
1718 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
1722 if (!storeEntryValidToSend(e
)) {
1723 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
1725 return LOG_TCP_MISS
;
1727 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1728 /* Special entries are always hits, no matter what the client says */
1729 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
1734 if (r
->flags
.nocache_hack
) {
1735 /* if nocache_hack is set, nocache should always be clear, right? */
1736 assert(!r
->flags
.nocache
);
1737 ipcacheReleaseInvalid(r
->host
);
1740 if (e
->store_status
== STORE_PENDING
) {
1741 if (r
->flags
.nocache
|| r
->flags
.nocache_hack
) {
1742 debug(33, 3) ("Clearing no-cache for STORE_PENDING request\n\t%s\n",
1744 r
->flags
.nocache
= 0;
1745 r
->flags
.nocache_hack
= 0;
1749 if (r
->flags
.nocache
) {
1750 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
1752 ipcacheReleaseInvalid(r
->host
);
1753 return LOG_TCP_CLIENT_REFRESH_MISS
;
1755 if (r
->range
&& httpHdrRangeWillBeComplex(r
->range
)) {
1757 * Some clients break if we return "200 OK" for a Range
1758 * request. We would have to return "200 OK" for a _complex_
1759 * Range request that is also a HIT. Thus, let's prevent HITs
1760 * on complex Range requests
1762 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
1764 return LOG_TCP_MISS
;
1766 debug(33, 3) ("clientProcessRequest2: default HIT\n");
1772 clientProcessRequest(clientHttpRequest
* http
)
1774 char *url
= http
->uri
;
1775 request_t
*r
= http
->request
;
1776 int fd
= http
->conn
->fd
;
1778 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
1779 RequestMethodStr
[r
->method
],
1781 if (r
->method
== METHOD_CONNECT
) {
1782 http
->log_type
= LOG_TCP_MISS
;
1783 sslStart(fd
, url
, r
, &http
->out
.size
);
1785 } else if (r
->method
== METHOD_PURGE
) {
1786 clientPurgeRequest(http
);
1788 } else if (r
->method
== METHOD_TRACE
) {
1789 if (r
->max_forwards
== 0) {
1790 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1791 storeReleaseRequest(http
->entry
);
1792 storeBuffer(http
->entry
);
1793 rep
= httpReplyCreate();
1794 httpReplySetHeaders(rep
, 1.0, HTTP_OK
, NULL
, "text/plain",
1795 httpRequestPrefixLen(r
), 0, squid_curtime
);
1796 httpReplySwapOut(rep
, http
->entry
);
1797 httpReplyDestroy(rep
);
1798 httpRequestSwapOut(r
, http
->entry
);
1799 storeComplete(http
->entry
);
1803 http
->log_type
= LOG_TCP_MISS
;
1804 } else if (pumpMethod(r
->method
)) {
1805 http
->log_type
= LOG_TCP_MISS
;
1806 /* XXX oof, POST can be cached! */
1807 pumpInit(fd
, r
, http
->uri
);
1809 http
->log_type
= clientProcessRequest2(http
);
1811 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
1812 log_tags
[http
->log_type
],
1814 http
->out
.offset
= 0;
1815 if (NULL
!= http
->entry
) {
1816 storeLockObject(http
->entry
);
1817 storeCreateMemObject(http
->entry
, http
->uri
, http
->log_uri
);
1818 storeClientListAdd(http
->entry
, http
);
1820 delaySetStoreClient(http
->entry
, http
, http
->request
->delay_id
);
1822 http
->entry
->refcount
++;
1823 storeClientCopy(http
->entry
,
1827 memAllocate(MEM_CLIENT_SOCK_BUF
),
1832 http
->log_type
= LOG_TCP_MISS
;
1833 clientProcessMiss(http
);
1838 * Prepare to fetch the object as it's a cache miss of some kind.
1841 clientProcessMiss(clientHttpRequest
* http
)
1843 char *url
= http
->uri
;
1844 request_t
*r
= http
->request
;
1845 ErrorState
*err
= NULL
;
1846 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
1847 RequestMethodStr
[r
->method
], url
);
1849 * We might have a left-over StoreEntry from a failed cache hit
1853 if (EBIT_TEST(http
->entry
->flags
, ENTRY_SPECIAL
))
1854 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url
);
1855 storeUnregister(http
->entry
, http
);
1856 storeUnlockObject(http
->entry
);
1859 if (clientOnlyIfCached(http
)) {
1860 clientProcessOnlyIfCachedMiss(http
);
1864 * Deny loops when running in accelerator/transproxy mode.
1866 if (http
->flags
.accel
&& r
->flags
.loopdetect
) {
1867 http
->al
.http
.code
= HTTP_FORBIDDEN
;
1868 err
= errorCon(ERR_ACCESS_DENIED
, HTTP_FORBIDDEN
);
1869 err
->request
= requestLink(r
);
1870 err
->src_addr
= http
->conn
->peer
.sin_addr
;
1871 http
->entry
= clientCreateStoreEntry(http
, r
->method
, null_request_flags
);
1872 errorAppendEntry(http
->entry
, err
);
1875 assert(http
->out
.offset
== 0);
1876 http
->entry
= clientCreateStoreEntry(http
, r
->method
, r
->flags
);
1877 http
->entry
->refcount
++;
1878 if (http
->redirect
.status
) {
1879 HttpReply
*rep
= httpReplyCreate();
1880 storeReleaseRequest(http
->entry
);
1881 httpRedirectReply(rep
, http
->redirect
.status
, http
->redirect
.location
);
1882 httpReplySwapOut(rep
, http
->entry
);
1883 httpReplyDestroy(rep
);
1884 storeComplete(http
->entry
);
1887 if (http
->flags
.internal
)
1888 r
->protocol
= PROTO_INTERNAL
;
1889 fwdStart(http
->conn
->fd
, http
->entry
, r
, http
->conn
->peer
.sin_addr
);
1892 static clientHttpRequest
*
1893 parseHttpRequestAbort(ConnStateData
* conn
, const char *uri
)
1895 clientHttpRequest
*http
= xcalloc(1, sizeof(clientHttpRequest
));
1896 cbdataAdd(http
, MEM_NONE
);
1898 http
->start
= current_time
;
1899 http
->req_sz
= conn
->in
.offset
;
1900 http
->uri
= xstrdup(uri
);
1901 http
->log_uri
= xstrndup(uri
, MAX_URL
);
1902 http
->range_iter
.boundary
= StringNull
;
1907 * parseHttpRequest()
1910 * NULL on error or incomplete request
1911 * a clientHttpRequest structure on success
1913 static clientHttpRequest
*
1914 parseHttpRequest(ConnStateData
* conn
, method_t
* method_p
, int *status
,
1915 char **prefix_p
, size_t * req_line_sz_p
)
1920 char *req_hdr
= NULL
;
1925 int free_request
= 0;
1926 size_t header_sz
; /* size of headers, not including first line */
1927 size_t prefix_sz
; /* size of whole request (req-line + headers) */
1931 clientHttpRequest
*http
= NULL
;
1933 struct natlookup natLookup
;
1934 static int natfd
= -1;
1937 if ((req_sz
= headersEnd(conn
->in
.buf
, conn
->in
.offset
)) == 0) {
1938 debug(33, 5) ("Incomplete request, waiting for end of headers\n");
1941 *method_p
= METHOD_NONE
;
1944 assert(req_sz
<= conn
->in
.offset
);
1945 /* Use memcpy, not strdup! */
1946 inbuf
= xmalloc(req_sz
+ 1);
1947 xmemcpy(inbuf
, conn
->in
.buf
, req_sz
);
1948 *(inbuf
+ req_sz
) = '\0';
1950 /* pre-set these values to make aborting simpler */
1952 *method_p
= METHOD_NONE
;
1955 /* Barf on NULL characters in the headers */
1956 if (strlen(inbuf
) != req_sz
) {
1957 debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n");
1958 return parseHttpRequestAbort(conn
, "error:invalid-request");
1960 /* Look for request method */
1961 if ((mstr
= strtok(inbuf
, "\t ")) == NULL
) {
1962 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
1963 return parseHttpRequestAbort(conn
, "error:invalid-request-method");
1965 method
= urlParseMethod(mstr
);
1966 if (method
== METHOD_NONE
) {
1967 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr
);
1968 return parseHttpRequestAbort(conn
, "error:unsupported-request-method");
1970 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr
);
1973 /* look for URL+HTTP/x.x */
1974 if ((url
= strtok(NULL
, "\n")) == NULL
) {
1975 debug(33, 1) ("parseHttpRequest: Missing URL\n");
1976 return parseHttpRequestAbort(conn
, "error:missing-url");
1978 while (isspace(*url
))
1980 t
= url
+ strlen(url
);
1985 if (isspace(*t
) && !strncmp(t
+ 1, "HTTP/", 5)) {
1990 while (t
> url
&& isspace(*t
))
1992 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url
);
1993 if (token
== NULL
) {
1994 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
1995 #if RELAXED_HTTP_PARSER
1996 http_ver
= (float) 0.9; /* wild guess */
1998 return parseHttpRequestAbort(conn
, "error:missing-http-ident");
2001 http_ver
= (float) atof(token
+ 5);
2005 * Process headers after request line
2007 req_hdr
= strtok(NULL
, null_string
);
2008 header_sz
= req_sz
- (req_hdr
- inbuf
);
2009 if (0 == header_sz
) {
2010 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
2014 assert(header_sz
> 0);
2015 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr
);
2016 end
= req_hdr
+ header_sz
;
2017 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end
);
2019 prefix_sz
= end
- inbuf
;
2020 *req_line_sz_p
= req_hdr
- inbuf
;
2021 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
2022 (int) prefix_sz
, (int) *req_line_sz_p
);
2023 assert(prefix_sz
<= conn
->in
.offset
);
2025 /* Ok, all headers are received */
2026 http
= xcalloc(1, sizeof(clientHttpRequest
));
2027 cbdataAdd(http
, MEM_NONE
);
2028 http
->http_ver
= http_ver
;
2030 http
->start
= current_time
;
2031 http
->req_sz
= prefix_sz
;
2032 http
->range_iter
.boundary
= StringNull
;
2033 *prefix_p
= xmalloc(prefix_sz
+ 1);
2034 xmemcpy(*prefix_p
, conn
->in
.buf
, prefix_sz
);
2035 *(*prefix_p
+ prefix_sz
) = '\0';
2037 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p
) + *req_line_sz_p
);
2038 if ((t
= strchr(url
, '#'))) /* remove HTML anchors */
2041 /* handle internal objects */
2042 if (internalCheck(url
)) {
2043 /* prepend our name & port */
2044 http
->uri
= xstrdup(internalLocalUri(NULL
, url
));
2045 http
->flags
.internal
= 1;
2046 http
->flags
.accel
= 1;
2048 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
2049 else if (Config2
.Accel
.on
&& *url
== '/') {
2050 /* prepend the accel prefix */
2051 if (opt_accel_uses_host
&& (t
= mime_get_header(req_hdr
, "Host"))) {
2052 /* If a Host: header was specified, use it to build the URL
2053 * instead of the one in the Config file. */
2055 * XXX Use of the Host: header here opens a potential
2056 * security hole. There are no checks that the Host: value
2057 * corresponds to one of your servers. It might, for example,
2058 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
2059 * types should be used to prevent httpd-accelerators
2060 * handling requests for non-local servers */
2062 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
+
2064 http
->uri
= xcalloc(url_sz
, 1);
2065 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2066 t
, (int) Config
.Accel
.port
, url
);
2067 } else if (vhost_mode
) {
2068 /* Put the local socket IP address as the hostname */
2069 url_sz
= strlen(url
) + 32 + Config
.appendDomainLen
;
2070 http
->uri
= xcalloc(url_sz
, 1);
2072 natLookup
.nl_inport
= http
->conn
->me
.sin_port
;
2073 natLookup
.nl_outport
= http
->conn
->peer
.sin_port
;
2074 natLookup
.nl_inip
= http
->conn
->me
.sin_addr
;
2075 natLookup
.nl_outip
= http
->conn
->peer
.sin_addr
;
2076 natLookup
.nl_flags
= IPN_TCP
;
2078 natfd
= open(IPL_NAT
, O_RDONLY
, 0);
2080 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2082 return parseHttpRequestAbort(conn
, "error:nat-open-failed");
2084 if (ioctl(natfd
, SIOCGNATL
, &natLookup
) < 0) {
2085 if (errno
!= ESRCH
) {
2086 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2089 return parseHttpRequestAbort(conn
, "error:nat-lookup-failed");
2091 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2092 inet_ntoa(http
->conn
->me
.sin_addr
),
2093 (int) Config
.Accel
.port
,
2096 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2097 inet_ntoa(natLookup
.nl_realip
),
2098 (int) Config
.Accel
.port
,
2101 snprintf(http
->uri
, url_sz
, "http://%s:%d%s",
2102 inet_ntoa(http
->conn
->me
.sin_addr
),
2103 (int) Config
.Accel
.port
,
2106 debug(33, 5) ("VHOST REWRITE: '%s'\n", http
->uri
);
2108 url_sz
= strlen(Config2
.Accel
.prefix
) + strlen(url
) +
2109 Config
.appendDomainLen
+ 1;
2110 http
->uri
= xcalloc(url_sz
, 1);
2111 snprintf(http
->uri
, url_sz
, "%s%s", Config2
.Accel
.prefix
, url
);
2113 http
->flags
.accel
= 1;
2115 /* URL may be rewritten later, so make extra room */
2116 url_sz
= strlen(url
) + Config
.appendDomainLen
+ 5;
2117 http
->uri
= xcalloc(url_sz
, 1);
2118 strcpy(http
->uri
, url
);
2119 http
->flags
.accel
= 0;
2121 if (!stringHasWhitespace(http
->uri
))
2122 http
->log_uri
= xstrndup(http
->uri
, MAX_URL
);
2124 http
->log_uri
= xstrndup(rfc1738_escape(http
->uri
), MAX_URL
);
2125 debug(33, 5) ("parseHttpRequest: Complete request received\n");
2134 clientReadDefer(int fdnotused
, void *data
)
2136 ConnStateData
*conn
= data
;
2137 return conn
->defer
.until
> squid_curtime
;
2141 clientReadRequest(int fd
, void *data
)
2143 ConnStateData
*conn
= data
;
2144 int parser_return_code
= 0;
2146 request_t
*request
= NULL
;
2149 clientHttpRequest
*http
= NULL
;
2150 clientHttpRequest
**H
= NULL
;
2151 char *prefix
= NULL
;
2152 ErrorState
*err
= NULL
;
2153 fde
*F
= &fd_table
[fd
];
2154 int len
= conn
->in
.size
- conn
->in
.offset
- 1;
2155 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd
);
2156 Counter
.syscalls
.sock
.reads
++;
2157 size
= read(fd
, conn
->in
.buf
+ conn
->in
.offset
, len
);
2159 fd_bytes(fd
, size
, FD_READ
);
2160 kb_incr(&Counter
.client_http
.kbytes_in
, size
);
2163 * Don't reset the timeout value here. The timeout value will be
2164 * set to Config.Timeout.request by httpAccept() and
2165 * clientWriteComplete(), and should apply to the request as a
2166 * whole, not individual read() calls. Plus, it breaks our
2167 * lame half-close detection
2169 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, conn
, 0);
2171 if (conn
->chr
== NULL
) {
2172 /* no current or pending requests */
2175 } else if (!Config
.onoff
.half_closed_clients
) {
2176 /* admin doesn't want to support half-closed client sockets */
2180 /* It might be half-closed, we can't tell */
2181 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd
);
2182 F
->flags
.socket_eof
= 1;
2183 conn
->defer
.until
= squid_curtime
+ 1;
2185 fd_note(fd
, "half-closed");
2187 } else if (size
< 0) {
2188 if (!ignoreErrno(errno
)) {
2189 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd
, xstrerror());
2192 } else if (conn
->in
.offset
== 0) {
2193 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd
, xstrerror());
2196 /* Continue to process previously read data */
2199 conn
->in
.offset
+= size
;
2200 /* Skip leading (and trailing) whitespace */
2201 while (conn
->in
.offset
> 0) {
2204 while (conn
->in
.offset
> 0 && isspace(conn
->in
.buf
[0])) {
2205 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ 1, conn
->in
.offset
- 1);
2208 conn
->in
.buf
[conn
->in
.offset
] = '\0'; /* Terminate the string */
2209 if (conn
->in
.offset
== 0)
2211 /* Limit the number of concurrent requests to 2 */
2212 for (H
= &conn
->chr
, nrequests
= 0; *H
; H
= &(*H
)->next
, nrequests
++);
2213 if (nrequests
>= 2) {
2214 debug(33, 2) ("clientReadRequest: FD %d max concurrent requests reached\n", fd
);
2215 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd
);
2216 conn
->defer
.until
= squid_curtime
+ 100; /* Reset when a request is complete */
2219 /* Process request */
2220 http
= parseHttpRequest(conn
,
2222 &parser_return_code
,
2228 assert(http
->req_sz
> 0);
2229 conn
->in
.offset
-= http
->req_sz
;
2230 assert(conn
->in
.offset
>= 0);
2231 debug(33, 5) ("conn->in.offset = %d\n", (int) conn
->in
.offset
);
2233 * If we read past the end of this request, move the remaining
2234 * data to the beginning
2236 if (conn
->in
.offset
> 0)
2237 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ http
->req_sz
, conn
->in
.offset
);
2238 /* add to the client request queue */
2239 for (H
= &conn
->chr
; *H
; H
= &(*H
)->next
);
2242 commSetTimeout(fd
, Config
.Timeout
.lifetime
, NULL
, NULL
);
2243 if (parser_return_code
< 0) {
2244 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd
);
2245 err
= errorCon(ERR_INVALID_REQ
, HTTP_BAD_REQUEST
);
2246 err
->request_hdrs
= xstrdup(conn
->in
.buf
);
2247 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2248 errorAppendEntry(http
->entry
, err
);
2252 if ((request
= urlParse(method
, http
->uri
)) == NULL
) {
2253 debug(33, 5) ("Invalid URL: %s\n", http
->uri
);
2254 err
= errorCon(ERR_INVALID_URL
, HTTP_BAD_REQUEST
);
2255 err
->src_addr
= conn
->peer
.sin_addr
;
2256 err
->url
= xstrdup(http
->uri
);
2257 http
->al
.http
.code
= err
->http_status
;
2258 http
->entry
= clientCreateStoreEntry(http
, method
, null_request_flags
);
2259 errorAppendEntry(http
->entry
, err
);
2263 /* compile headers */
2264 /* we should skip request line! */
2265 if (!httpRequestParseHeader(request
, prefix
+ req_line_sz
))
2266 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2268 /* continue anyway? */
2270 request
->flags
.accelerated
= http
->flags
.accel
;
2271 if (!http
->flags
.internal
) {
2272 if (internalCheck(strBuf(request
->urlpath
))) {
2273 if (0 == strcasecmp(request
->host
, internalHostname())) {
2274 if (request
->port
== Config
.Port
.http
->i
)
2275 http
->flags
.internal
= 1;
2276 } else if (internalStaticCheck(strBuf(request
->urlpath
))) {
2277 xstrncpy(request
->host
, internalHostname(), SQUIDHOSTNAMELEN
);
2278 request
->port
= Config
.Port
.http
->i
;
2279 http
->flags
.internal
= 1;
2283 request
->flags
.internal
= http
->flags
.internal
;
2285 safe_free(http
->log_uri
);
2286 http
->log_uri
= xstrdup(urlCanonicalClean(request
));
2287 request
->client_addr
= conn
->peer
.sin_addr
;
2288 request
->http_ver
= http
->http_ver
;
2289 if (!urlCheckRequest(request
)) {
2290 err
= errorCon(ERR_UNSUP_REQ
, HTTP_NOT_IMPLEMENTED
);
2291 err
->src_addr
= conn
->peer
.sin_addr
;
2292 err
->request
= requestLink(request
);
2293 http
->al
.http
.code
= err
->http_status
;
2294 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2295 errorAppendEntry(http
->entry
, err
);
2298 if (0 == clientCheckContentLength(request
)) {
2299 err
= errorCon(ERR_INVALID_REQ
, HTTP_LENGTH_REQUIRED
);
2300 err
->src_addr
= conn
->peer
.sin_addr
;
2301 err
->request
= requestLink(request
);
2302 http
->al
.http
.code
= err
->http_status
;
2303 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2304 errorAppendEntry(http
->entry
, err
);
2307 http
->request
= requestLink(request
);
2309 * We need to set the keepalive flag before doing some
2310 * hacks for POST/PUT requests below. Maybe we could
2311 * set keepalive flag even earlier.
2313 clientSetKeepaliveFlag(http
);
2315 * break here for NON-GET because most likely there is a
2316 * reqeust body following and we don't want to parse it
2317 * as though it was new request
2319 if (request
->method
!= METHOD_GET
) {
2320 int cont_len
= httpHeaderGetInt(&request
->header
, HDR_CONTENT_LENGTH
);
2321 int copy_len
= conn
->in
.offset
;
2322 if (cont_len
< copy_len
&& request
->flags
.proxy_keepalive
)
2323 copy_len
= cont_len
;
2325 assert(conn
->in
.offset
>= copy_len
);
2326 request
->body_sz
= copy_len
;
2327 request
->body
= xmalloc(request
->body_sz
);
2328 xmemcpy(request
->body
, conn
->in
.buf
, request
->body_sz
);
2329 conn
->in
.offset
-= copy_len
;
2330 if (conn
->in
.offset
)
2331 xmemmove(conn
->in
.buf
, conn
->in
.buf
+ copy_len
, conn
->in
.offset
);
2334 * if we didn't get the full body now, then more will
2335 * be arriving on the client socket. Lets cancel
2336 * the read handler until this request gets forwarded.
2338 if (request
->body_sz
< cont_len
)
2339 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
2341 clientAccessCheck(http
);
2342 continue; /* while offset > 0 */
2343 } else if (parser_return_code
== 0) {
2345 * Partial request received; reschedule until parseHttpRequest()
2346 * is happy with the input
2348 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2350 if (conn
->in
.offset
>= Config
.maxRequestSize
) {
2351 /* The request is too large to handle */
2352 debug(33, 0) ("Request won't fit in buffer.\n");
2353 debug(33, 0) ("Config 'request_size'= %d bytes.\n",
2354 Config
.maxRequestSize
);
2355 debug(33, 0) ("This request = %d bytes.\n",
2356 (int) conn
->in
.offset
);
2357 err
= errorCon(ERR_INVALID_REQ
, HTTP_REQUEST_ENTITY_TOO_LARGE
);
2358 http
->entry
= clientCreateStoreEntry(http
, request
->method
, null_request_flags
);
2359 errorAppendEntry(http
->entry
, err
);
2362 /* Grow the request memory area to accomodate for a large request */
2363 conn
->in
.size
+= REQUEST_BUF_SIZE
;
2364 conn
->in
.buf
= xrealloc(conn
->in
.buf
, conn
->in
.size
);
2365 /* XXX account conn->in.buf */
2366 debug(33, 2) ("Handling a large request, offset=%d inbufsize=%d\n",
2367 (int) conn
->in
.offset
, conn
->in
.size
);
2368 k
= conn
->in
.size
- 1 - conn
->in
.offset
;
2375 /* general lifetime handler for HTTP requests */
2377 requestTimeout(int fd
, void *data
)
2379 ConnStateData
*conn
= data
;
2381 debug(33, 2) ("requestTimeout: FD %d: lifetime is expired.\n", fd
);
2382 if (fd_table
[fd
].rwstate
) {
2384 * Some data has been sent to the client, just close the FD
2387 } else if (conn
->nrequests
) {
2389 * assume its a persistent connection; just close it
2396 err
= errorCon(ERR_LIFETIME_EXP
, HTTP_REQUEST_TIMEOUT
);
2397 err
->url
= xstrdup("N/A");
2399 * Normally we shouldn't call errorSend() in client_side.c, but
2400 * it should be okay in this case. Presumably if we get here
2401 * this is the first request for the connection, and no data
2402 * has been written yet
2404 assert(conn
->chr
== NULL
);
2407 * if we don't close() here, we still need a timeout handler!
2409 commSetTimeout(fd
, 30, requestTimeout
, conn
);
2414 httpAcceptDefer(void)
2416 static time_t last_warn
= 0;
2417 if (fdNFree() >= RESERVED_FD
)
2419 if (last_warn
+ 15 < squid_curtime
) {
2420 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2421 last_warn
= squid_curtime
;
2426 /* Handle a new connection on HTTP socket. */
2428 httpAccept(int sock
, void *data
)
2432 ConnStateData
*connState
= NULL
;
2433 struct sockaddr_in peer
;
2434 struct sockaddr_in me
;
2435 int max
= INCOMING_HTTP_MAX
;
2436 commSetSelect(sock
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2437 while (max
-- && !httpAcceptDefer()) {
2438 memset(&peer
, '\0', sizeof(struct sockaddr_in
));
2439 memset(&me
, '\0', sizeof(struct sockaddr_in
));
2440 if ((fd
= comm_accept(sock
, &peer
, &me
)) < 0) {
2441 if (!ignoreErrno(errno
))
2442 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
2446 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd
);
2447 connState
= xcalloc(1, sizeof(ConnStateData
));
2448 connState
->peer
= peer
;
2449 connState
->log_addr
= peer
.sin_addr
;
2450 connState
->log_addr
.s_addr
&= Config
.Addrs
.client_netmask
.s_addr
;
2453 connState
->ident
.fd
= -1;
2454 connState
->in
.size
= REQUEST_BUF_SIZE
;
2455 connState
->in
.buf
= xcalloc(connState
->in
.size
, 1);
2456 cbdataAdd(connState
, MEM_NONE
);
2457 /* XXX account connState->in.buf */
2458 comm_add_close_handler(fd
, connStateFree
, connState
);
2459 if (Config
.onoff
.log_fqdn
)
2460 fqdncache_gethostbyaddr(peer
.sin_addr
, FQDN_LOOKUP_IF_MISS
);
2461 commSetTimeout(fd
, Config
.Timeout
.request
, requestTimeout
, connState
);
2462 commSetSelect(fd
, COMM_SELECT_READ
, clientReadRequest
, connState
, 0);
2463 commSetDefer(fd
, clientReadDefer
, connState
);
2468 /* return 1 if the request should be aborted */
2470 CheckQuickAbort2(const clientHttpRequest
* http
)
2476 if (!http
->request
->flags
.cachable
)
2478 if (EBIT_TEST(http
->entry
->flags
, KEY_PRIVATE
))
2480 if (http
->entry
->mem_obj
== NULL
)
2482 expectlen
= http
->entry
->mem_obj
->reply
->content_length
;
2483 curlen
= (int) http
->entry
->mem_obj
->inmem_hi
;
2484 minlen
= (int) Config
.quickAbort
.min
<< 10;
2488 if (curlen
> expectlen
)
2489 /* bad content length */
2491 if ((expectlen
- curlen
) < minlen
)
2492 /* only little more left */
2494 if ((expectlen
- curlen
) > (Config
.quickAbort
.max
<< 10))
2495 /* too much left to go */
2497 if (expectlen
< 100)
2500 if ((curlen
/ (expectlen
/ 100)) > Config
.quickAbort
.pct
)
2501 /* past point of no return */
2508 CheckQuickAbort(clientHttpRequest
* http
)
2510 StoreEntry
*entry
= http
->entry
;
2511 /* Note, set entry here because http->entry might get changed (for IMS
2512 * requests) during the storeAbort() call */
2515 if (storePendingNClients(entry
) > 0)
2517 if (entry
->store_status
!= STORE_PENDING
)
2519 if (CheckQuickAbort2(http
) == 0)
2521 debug(33, 3) ("CheckQuickAbort: ABORTING %s\n", storeUrl(entry
));
2522 Counter
.aborted_requests
++;
2523 storeAbort(entry
, 1);
2526 #define SENDING_BODY 0
2527 #define SENDING_HDRSONLY 1
2529 clientCheckTransferDone(clientHttpRequest
* http
)
2531 int sending
= SENDING_BODY
;
2532 StoreEntry
*entry
= http
->entry
;
2539 * For now, 'done_copying' is used for special cases like
2540 * Range and HEAD requests.
2542 if (http
->flags
.done_copying
)
2545 * Handle STORE_OK and STORE_ABORTED objects.
2546 * objectLen(entry) will be set proprely.
2548 if (entry
->store_status
!= STORE_PENDING
) {
2549 if (http
->out
.offset
>= objectLen(entry
))
2555 * Now, handle STORE_PENDING objects
2557 mem
= entry
->mem_obj
;
2558 assert(mem
!= NULL
);
2559 assert(http
->request
!= NULL
);
2561 if (reply
->hdr_sz
== 0)
2562 return 0; /* haven't found end of headers yet */
2563 else if (reply
->sline
.status
== HTTP_OK
)
2564 sending
= SENDING_BODY
;
2565 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
2566 sending
= SENDING_HDRSONLY
;
2567 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
2568 sending
= SENDING_HDRSONLY
;
2569 else if (reply
->sline
.status
< HTTP_OK
)
2570 sending
= SENDING_HDRSONLY
;
2571 else if (http
->request
->method
== METHOD_HEAD
)
2572 sending
= SENDING_HDRSONLY
;
2574 sending
= SENDING_BODY
;
2576 * Figure out how much data we are supposed to send.
2577 * If we are sending a body and we don't have a content-length,
2578 * then we must wait for the object to become STORE_OK or
2581 if (sending
== SENDING_HDRSONLY
)
2582 sendlen
= reply
->hdr_sz
;
2583 else if (reply
->content_length
< 0)
2586 sendlen
= reply
->content_length
+ reply
->hdr_sz
;
2588 * Now that we have the expected length, did we send it all?
2590 if (http
->out
.offset
< sendlen
)
2597 clientGotNotEnough(clientHttpRequest
* http
)
2599 int cl
= http
->entry
->mem_obj
->reply
->content_length
;
2600 int hs
= http
->entry
->mem_obj
->reply
->hdr_sz
;
2602 if (http
->out
.offset
< cl
+ hs
)
2608 * This function is designed to serve a fairly specific purpose.
2609 * Occasionally our vBNS-connected caches can talk to each other, but not
2610 * the rest of the world. Here we try to detect frequent failures which
2611 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2612 * the failure:success ratio goes above 1.0 then we go into "hit only"
2613 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2614 * will only fetch HITs from us if they are using the ICP protocol. We
2615 * stay in this mode for 5 minutes.
2617 * Duane W., Sept 16, 1996
2621 checkFailureRatio(err_type etype
, hier_code hcode
)
2623 static double magic_factor
= 100.0;
2626 if (hcode
== HIER_NONE
)
2628 n_good
= magic_factor
/ (1.0 + request_failure_ratio
);
2629 n_bad
= magic_factor
- n_good
;
2632 case ERR_CONNECT_FAIL
:
2633 case ERR_READ_ERROR
:
2639 request_failure_ratio
= n_bad
/ n_good
;
2640 if (hit_only_mode_until
> squid_curtime
)
2642 if (request_failure_ratio
< 1.0)
2644 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio
);
2645 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
2646 FAILURE_MODE_TIME
/ 60);
2647 hit_only_mode_until
= squid_curtime
+ FAILURE_MODE_TIME
;
2648 request_failure_ratio
= 0.8; /* reset to something less than 1.0 */
2652 clientHttpConnectionsOpen(void)
2656 for (u
= Config
.Port
.http
; u
; u
= u
->next
) {
2658 fd
= comm_open(SOCK_STREAM
,
2660 Config
.Addrs
.tcp_incoming
,
2668 commSetSelect(fd
, COMM_SELECT_READ
, httpAccept
, NULL
, 0);
2669 /*commSetDefer(fd, httpAcceptDefer, NULL); */
2670 debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n",
2672 HttpSockets
[NHttpSockets
++] = fd
;
2674 if (NHttpSockets
< 1)
2675 fatal("Cannot open HTTP Port");
2679 clientHttpConnectionsClose(void)
2682 for (i
= 0; i
< NHttpSockets
; i
++) {
2683 if (HttpSockets
[i
] >= 0) {
2684 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets
[i
]);
2685 comm_close(HttpSockets
[i
]);
2686 HttpSockets
[i
] = -1;