]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_side.cc
little StatHist cleanup and debugging
[thirdparty/squid.git] / src / client_side.cc
CommitLineData
3c66d057 1
dd11e0b7 2/*
d548ee64 3 * $Id: client_side.cc,v 1.412 1998/10/14 21:11:58 wessels Exp $
dd11e0b7 4 *
5 * DEBUG: section 33 Client-side Routines
6 * AUTHOR: Duane Wessels
7 *
42c04c16 8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
e25c139f 9 * ----------------------------------------------------------
dd11e0b7 10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
e25c139f 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.
dd11e0b7 19 *
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.
24 *
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.
29 *
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
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
dd11e0b7 34 */
f88bb09c 35
36#include "squid.h"
37
5cafc1d6 38#if IPF_TRANSPARENT
39#if HAVE_SYS_IOCTL_H
40#include <sys/ioctl.h>
41#endif
42#include <netinet/tcp.h>
43#include <net/if.h>
44#include <ip_compat.h>
45#include <ip_fil.h>
46#include <ip_nat.h>
47#endif
48
49
50
5492ad1d 51#if LINGERING_CLOSE
52#define comm_close comm_lingering_close
53#endif
54
7a2f978b 55static const char *const crlf = "\r\n";
f715bd3a 56static const char *const proxy_auth_challenge_fmt = "Basic realm=\"%s\"";
7a2f978b 57
58#define REQUEST_BUF_SIZE 4096
59#define FAILURE_MODE_TIME 300
60
61/* Local functions */
62
fc5d6f7f 63static CWCB clientWriteComplete;
7a2f978b 64static PF clientReadRequest;
65static PF connStateFree;
66static PF requestTimeout;
7a2f978b 67static int CheckQuickAbort2(const clientHttpRequest *);
1b02b5be 68static int clientCheckTransferDone(clientHttpRequest *);
7a2f978b 69static void CheckQuickAbort(clientHttpRequest *);
88aad2e5 70static void checkFailureRatio(err_type, hier_code);
fb63215a 71static void clientProcessMiss(clientHttpRequest *);
eeb423fb 72static void clientBuildReplyHeader(clientHttpRequest * http, HttpReply * rep);
99edd1c3 73static clientHttpRequest *parseHttpRequestAbort(ConnStateData * conn, const char *uri);
7a2f978b 74static clientHttpRequest *parseHttpRequest(ConnStateData *, method_t *, int *, char **, size_t *);
582b6456 75static RH clientRedirectDone;
1b02b5be 76static STCB clientHandleIMSReply;
f5b8bbc4 77static int clientGetsOldEntry(StoreEntry * new, StoreEntry * old, request_t * request);
78static int checkAccelOnly(clientHttpRequest *);
7c1d4010 79static int clientOnlyIfCached(clientHttpRequest * http);
7a2f978b 80static STCB clientSendMoreData;
81static STCB clientCacheHit;
99edd1c3 82static void clientInterpretRequestHeaders(clientHttpRequest *);
fb63215a 83static void clientProcessRequest(clientHttpRequest *);
84static void clientProcessExpired(void *data);
038eb4ed 85static void clientProcessOnlyIfCachedMiss(clientHttpRequest * http);
cb69b4c7 86static HttpReply *clientConstructProxyAuthReply(clientHttpRequest * http);
50ddd7a4 87static int clientCachable(clientHttpRequest * http);
88static int clientHierarchical(clientHttpRequest * http);
efb9218c 89static int clientCheckContentLength(request_t * r);
ba4f8e5a 90static int httpAcceptDefer(void);
49d3fcb0 91static log_type clientProcessRequest2(clientHttpRequest * http);
ea6f43cd 92
38d7734b 93static int
382d851a 94checkAccelOnly(clientHttpRequest * http)
38d7734b 95{
96 /* return TRUE if someone makes a proxy request to us and
97 * we are in httpd-accel only mode */
f1dc9b30 98 if (!Config2.Accel.on)
38d7734b 99 return 0;
17a0a4ee 100 if (Config.onoff.accel_with_proxy)
38d7734b 101 return 0;
382d851a 102 if (http->request->protocol == PROTO_CACHEOBJ)
38d7734b 103 return 0;
77ed547a 104 if (http->flags.accel)
38d7734b 105 return 0;
106 return 1;
107}
108
b8d8561b 109void
382d851a 110clientAccessCheck(void *data)
f88bb09c 111{
382d851a 112 clientHttpRequest *http = data;
113 ConnStateData *conn = http->conn;
99edd1c3 114 const char *browser;
17a0a4ee 115 if (Config.onoff.ident_lookup && conn->ident.state == IDENT_NONE) {
425ad52e 116 identStart(-1, conn, clientAccessCheck, http);
ba1d2afa 117 return;
118 }
382d851a 119 if (checkAccelOnly(http)) {
120 clientAccessCheckDone(0, http);
75e88d56 121 return;
f88bb09c 122 }
99edd1c3 123 browser = httpHeaderGetStr(&http->request->header, HDR_USER_AGENT);
f1dc9b30 124 http->acl_checklist = aclChecklistCreate(Config.accessList.http,
382d851a 125 http->request,
126 conn->peer.sin_addr,
75e88d56 127 browser,
382d851a 128 conn->ident.ident);
129 aclNBCheck(http->acl_checklist, clientAccessCheckDone, http);
f88bb09c 130}
131
7c1d4010 132/*
133 * returns true if client specified that the object must come from the cache
134 * witout contacting origin server
135 */
136static int
137clientOnlyIfCached(clientHttpRequest * http)
138{
139 const request_t *r = http->request;
140 assert(r);
ea285003 141 return r->cache_control &&
8e092300 142 EBIT_TEST(r->cache_control->mask, CC_ONLY_IF_CACHED);
7c1d4010 143}
144
cb69b4c7 145static HttpReply *
ee1679df 146clientConstructProxyAuthReply(clientHttpRequest * http)
cb69b4c7 147{
148 ErrorState *err = errorCon(ERR_CACHE_ACCESS_DENIED, HTTP_PROXY_AUTHENTICATION_REQUIRED);
149 HttpReply *rep;
150 err->request = requestLink(http->request);
151 rep = errorBuildReply(err);
152 errorStateFree(err);
153 /* add Authenticate header */
f715bd3a 154 httpHeaderPutStrf(&rep->header, HDR_PROXY_AUTHENTICATE, proxy_auth_challenge_fmt, Config.proxyAuthRealm);
cb69b4c7 155 return rep;
156}
fc5d6f7f 157
23d92c64 158StoreEntry *
92695e5e 159clientCreateStoreEntry(clientHttpRequest * h, method_t m, request_flags flags)
79e0dc20 160{
161 StoreEntry *e;
40defb17 162 /*
163 * For erroneous requests, we might not have a h->request,
164 * so make a fake one.
165 */
99edd1c3 166 if (h->request == NULL)
167 h->request = requestLink(requestCreate(m, PROTO_NONE, NULL));
23d92c64 168 e = storeCreateEntry(h->uri, h->log_uri, flags, m);
79e0dc20 169 storeClientListAdd(e, h);
447e176b 170#if DELAY_POOLS
171 delaySetStoreClient(e, h, h->request->delay_id);
172#endif
cc27d775 173 storeClientCopy(e, 0, 0, CLIENT_SOCK_SZ, memAllocate(MEM_CLIENT_SOCK_BUF), clientSendMoreData, h);
79e0dc20 174 return e;
175}
176
177
b8d8561b 178void
75e88d56 179clientAccessCheckDone(int answer, void *data)
f88bb09c 180{
382d851a 181 clientHttpRequest *http = data;
02922e76 182 int page_id = -1;
9b312a19 183 ErrorState *err = NULL;
23d92c64 184 debug(33, 5) ("clientAccessCheckDone: '%s' answer=%d\n", http->uri, answer);
382d851a 185 http->acl_checklist = NULL;
fc5d6f7f 186 if (answer == ACCESS_ALLOWED) {
9b5d1d21 187 safe_free(http->uri);
188 http->uri = xstrdup(urlCanonical(http->request));
ce66013b 189 assert(http->redirect_state == REDIRECT_NONE);
382d851a 190 http->redirect_state = REDIRECT_PENDING;
191 redirectStart(http, clientRedirectDone, http);
fc5d6f7f 192 } else if (answer == ACCESS_REQ_PROXY_AUTH) {
fc5d6f7f 193 http->log_type = LOG_TCP_DENIED;
92695e5e 194 http->entry = clientCreateStoreEntry(http, http->request->method, null_request_flags);
def67559 195 /* create appropriate response */
196 http->entry->mem_obj->reply = clientConstructProxyAuthReply(http);
197 httpReplySwapOut(http->entry->mem_obj->reply, http->entry);
0254527a 198 storeComplete(http->entry);
f88bb09c 199 } else {
23d92c64 200 debug(33, 5) ("Access Denied: %s\n", http->uri);
901e234d 201 debug(33, 5) ("AclMatchedName = %s\n",
6f47fbc7 202 AclMatchedName ? AclMatchedName : "<null>");
79a15e0a 203 http->log_type = LOG_TCP_DENIED;
92695e5e 204 http->entry = clientCreateStoreEntry(http, http->request->method, null_request_flags);
02922e76 205 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName);
206 /* NOTE: don't use HTTP_UNAUTHORIZED because then the
207 * stupid browser wants us to authenticate */
208 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
209 err->request = requestLink(http->request);
210 err->src_addr = http->conn->peer.sin_addr;
211 if (page_id > 0)
212 err->page_id = page_id;
213 errorAppendEntry(http->entry, err);
f88bb09c 214 }
215}
216
b8d8561b 217static void
218clientRedirectDone(void *data, char *result)
f88bb09c 219{
382d851a 220 clientHttpRequest *http = data;
c0cdaf99 221 request_t *new_request = NULL;
382d851a 222 request_t *old_request = http->request;
23d92c64 223 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http->uri,
f88bb09c 224 result ? result : "NULL");
ce66013b 225 assert(http->redirect_state == REDIRECT_PENDING);
382d851a 226 http->redirect_state = REDIRECT_DONE;
77ed547a 227 if (result && strcmp(result, http->uri))
20cc1450 228 new_request = urlParse(old_request->method, result);
c0cdaf99 229 if (new_request) {
23d92c64 230 safe_free(http->uri);
9b5d1d21 231 http->uri = xstrdup(urlCanonical(new_request));
20cc1450 232 new_request->http_ver = old_request->http_ver;
2246b732 233 httpHeaderAppend(&new_request->header, &old_request->header);
77ed547a 234 new_request->client_addr = old_request->client_addr;
92695e5e 235 new_request->flags.redirected = 1;
6cf028ab 236 if (old_request->body) {
237 new_request->body = xmalloc(old_request->body_sz);
238 xmemcpy(new_request->body, old_request->body, old_request->body_sz);
239 new_request->body_sz = old_request->body_sz;
240 }
20cc1450 241 requestUnlink(old_request);
382d851a 242 http->request = requestLink(new_request);
f88bb09c 243 }
99edd1c3 244 clientInterpretRequestHeaders(http);
23d92c64 245 fd_note(http->conn->fd, http->uri);
fb63215a 246 clientProcessRequest(http);
e81957b7 247}
248
fb63215a 249static void
250clientProcessExpired(void *data)
620da955 251{
382d851a 252 clientHttpRequest *http = data;
23d92c64 253 char *url = http->uri;
620da955 254 StoreEntry *entry = NULL;
23d92c64 255 debug(33, 3) ("clientProcessExpired: '%s'\n", http->uri);
b9d34cc1 256 assert(http->entry->lastmod >= 0);
7c1d4010 257 /*
258 * check if we are allowed to contact other servers
259 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
6106c6fc 260 * a stale entry *if* it matches client requirements
7c1d4010 261 */
262 if (clientOnlyIfCached(http)) {
263 clientProcessOnlyIfCachedMiss(http);
264 return;
265 }
92695e5e 266 http->request->flags.refresh = 1;
382d851a 267 http->old_entry = http->entry;
620da955 268 entry = storeCreateEntry(url,
23d92c64 269 http->log_uri,
382d851a 270 http->request->flags,
271 http->request->method);
620da955 272 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
fe96bbe6 273 storeClientListAdd(entry, http);
274 storeClientListAdd(http->old_entry, http);
447e176b 275#if DELAY_POOLS
276 delaySetStoreClient(entry, http, http->request->delay_id);
277 delaySetStoreClient(http->old_entry, http, http->request->delay_id);
278#endif
382d851a 279 entry->lastmod = http->old_entry->lastmod;
1c3e77cd 280 debug(33, 5) ("clientProcessExpired: lastmod %d\n", (int) entry->lastmod);
d1a43e28 281 entry->refcount++; /* EXPIRED CASE */
382d851a 282 http->entry = entry;
283 http->out.offset = 0;
d1061d2b 284 fwdStart(http->conn->fd, http->entry, http->request,
285 http->conn->peer.sin_addr);
f990cccc 286 /* Register with storage manager to receive updates when data comes in. */
0e473d70 287 if (entry->store_status == STORE_ABORTED)
6cf028ab 288 debug(33, 0) ("clientProcessExpired: entry->swap_status == STORE_ABORTED\n");
d89d1fb6 289 storeClientCopy(entry,
fe96bbe6 290 http->out.offset,
d89d1fb6 291 http->out.offset,
cc27d775 292 CLIENT_SOCK_SZ,
293 memAllocate(MEM_CLIENT_SOCK_BUF),
1b02b5be 294 clientHandleIMSReply,
d89d1fb6 295 http);
620da955 296}
297
91f4d519 298static int
299clientGetsOldEntry(StoreEntry * new_entry, StoreEntry * old_entry, request_t * request)
300{
71a8a965 301 const http_status status = new_entry->mem_obj->reply->sline.status;
302 if (0 == status) {
303 debug(33, 5) ("clientGetsOldEntry: YES, broken HTTP reply\n");
304 return 1;
305 }
91f4d519 306 /* If the reply is anything but "Not Modified" then
307 * we must forward it to the client */
71a8a965 308 if (HTTP_NOT_MODIFIED != status) {
cb69b4c7 309 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status);
91f4d519 310 return 0;
311 }
312 /* If the client did not send IMS in the request, then it
313 * must get the old object, not this "Not Modified" reply */
92695e5e 314 if (!request->flags.ims) {
a3d5953d 315 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
91f4d519 316 return 1;
317 }
318 /* If the client IMS time is prior to the entry LASTMOD time we
319 * need to send the old object */
320 if (modifiedSince(old_entry, request)) {
5f6ac48b 321 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n",
1afe05c5 322 (int) request->ims);
91f4d519 323 return 1;
324 }
a3d5953d 325 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
91f4d519 326 return 0;
327}
328
329
52d4522b 330static void
1b02b5be 331clientHandleIMSReply(void *data, char *buf, ssize_t size)
620da955 332{
382d851a 333 clientHttpRequest *http = data;
382d851a 334 StoreEntry *entry = http->entry;
620da955 335 MemObject *mem = entry->mem_obj;
9fb13bb6 336 const char *url = storeUrl(entry);
e92d33a5 337 int unlink_request = 0;
76cff1d4 338 StoreEntry *oldentry;
49d3fcb0 339 int recopy = 1;
71a8a965 340 const http_status status = mem->reply->sline.status;
93f9abd0 341 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url, (int) size);
6801f8a8 342 if (size < 0 && entry->store_status != STORE_ABORTED)
d87ebd78 343#if OLD_CODE
344 /*
345 * This storeAbort() call causes wrong behaviour in some situations.
346 * If there are multiple clients hanging on this entry, the second
347 * client will not want the entry to be aborted. This shows up in
348 * the stack trace below:
349 *
350 * #0 0xef5f452c in kill ()
351 * #1 0xef5ba620 in abort ()
352 * #2 0x7d680 in __eprintf (string=0x82a70 "%s:%u: failed assertion `%s'\n", expression=0x82a90 "client_side.c", line=1111, filename=0x83468 "size > 0")
353 * #3 0x2ad7c in clientCacheHit (data=0x31ae318, buf=0x17f1450 "", size=0) at client_side.c:1111
354 * #4 0x65cd4 in storeClientCopy2 (e=0x4c48c0, sc=0x2eedd70) at store_client.c:264
355 * #5 0x665b0 in InvokeHandlers (e=0x4c48c0) at store_client.c:510
356 * #6 0x63c90 in storeAbort (e=0x4c48c0, cbflag=1) at store.c:626
357 * #7 0x29190 in clientHandleIMSReply (data=0x31e9518, buf=0x2268810 "", size=-1) at client_side.c:343
358 * #8 0x66450 in storeUnregister (e=0x2910c, data=0x31e9518) at store_client.c:467
359 * #9 0x29a54 in httpRequestFree (data=0x31e9518) at client_side.c:579
360 * #10 0x29eb4 in connStateFree (fd=534528, data=0x314d0d0) at client_side.c:667
361 * #11 0x2f27c in commCallCloseHandlers (fd=42) at comm.c:501
362 * #12 0x2f410 in comm_close (fd=42) at comm.c:565
363 * #13 0x2d054 in clientReadRequest (fd=42, data=0x314d0d0) at client_side.c:2038
364 * #14 0x309c8 in comm_poll (msec=434) at comm_select.c:354
365 * #15 0x4da64 in main (argc=5, argv=0xeffffd4c) at main.c:587
366 */
6801f8a8 367 storeAbort(entry, 1);
d87ebd78 368#endif
ac18f51a 369 /*
370 * Lets just try to return here and see what kind of problems that
371 * causes
372 */
373 return;
6801f8a8 374 if (entry->store_status == STORE_ABORTED) {
1b02b5be 375 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url);
c54e9052 376 /* We have an existing entry, but failed to validate it */
accc6d47 377 /* Its okay to send the old one anyway */
378 http->log_type = LOG_TCP_REFRESH_FAIL_HIT;
379 storeUnregister(entry, http);
380 storeUnlockObject(entry);
381 entry = http->entry = http->old_entry;
382 entry->refcount++;
71a8a965 383 } else if (STORE_PENDING == entry->store_status && 0 == status) {
1b02b5be 384 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url);
cc27d775 385 if (size >= CLIENT_SOCK_SZ) {
4455552a 386 /* will not get any bigger than that */
387 debug(33, 3) ("clientHandleIMSReply: Reply is too large '%s', using old entry\n", url);
878570db 388 /* use old entry, this repeats the code abovez */
4455552a 389 http->log_type = LOG_TCP_REFRESH_FAIL_HIT;
390 storeUnregister(entry, http);
743eebfa 391 storeUnlockObject(entry);
4455552a 392 entry = http->entry = http->old_entry;
393 entry->refcount++;
878570db 394 /* continue */
4455552a 395 } else {
4455552a 396 storeClientCopy(entry,
397 http->out.offset + size,
398 http->out.offset,
cc27d775 399 CLIENT_SOCK_SZ,
49d3fcb0 400 buf,
4455552a 401 clientHandleIMSReply,
402 http);
878570db 403 return;
4455552a 404 }
382d851a 405 } else if (clientGetsOldEntry(entry, http->old_entry, http->request)) {
620da955 406 /* We initiated the IMS request, the client is not expecting
76cff1d4 407 * 304, so put the good one back. First, make sure the old entry
408 * headers have been loaded from disk. */
382d851a 409 oldentry = http->old_entry;
382d851a 410 http->log_type = LOG_TCP_REFRESH_HIT;
d89d1fb6 411 if (oldentry->mem_obj->request == NULL) {
412 oldentry->mem_obj->request = requestLink(mem->request);
413 unlink_request = 1;
e92d33a5 414 }
64763c37 415 /* Don't memcpy() the whole reply structure here. For example,
416 * www.thegist.com (Netscape/1.13) returns a content-length for
417 * 304's which seems to be the length of the 304 HEADERS!!! and
418 * not the body they refer to. */
71a8a965 419 httpReplyUpdateOnNotModified(oldentry->mem_obj->reply, mem->reply);
d89d1fb6 420 storeTimestampsSet(oldentry);
382d851a 421 storeUnregister(entry, http);
6d54aea8 422 storeUnlockObject(entry);
382d851a 423 entry = http->entry = oldentry;
41fad779 424 entry->timestamp = squid_curtime;
657266fe 425 if (unlink_request) {
e92d33a5 426 requestUnlink(entry->mem_obj->request);
657266fe 427 entry->mem_obj->request = NULL;
428 }
620da955 429 } else {
430 /* the client can handle this reply, whatever it is */
382d851a 431 http->log_type = LOG_TCP_REFRESH_MISS;
71a8a965 432 if (HTTP_NOT_MODIFIED == mem->reply->sline.status) {
382d851a 433 http->old_entry->timestamp = squid_curtime;
434 http->old_entry->refcount++;
435 http->log_type = LOG_TCP_REFRESH_HIT;
d1a43e28 436 }
382d851a 437 storeUnregister(http->old_entry, http);
438 storeUnlockObject(http->old_entry);
49d3fcb0 439 recopy = 0;
620da955 440 }
382d851a 441 http->old_entry = NULL; /* done with old_entry */
49d3fcb0 442 assert(entry->store_status != STORE_ABORTED);
443 if (recopy) {
444 storeClientCopy(entry,
445 http->out.offset,
446 http->out.offset,
cc27d775 447 CLIENT_SOCK_SZ,
49d3fcb0 448 buf,
449 clientSendMoreData,
450 http);
451 } else {
452 clientSendMoreData(data, buf, size);
6cf028ab 453 }
620da955 454}
91f4d519 455
456int
304d07cb 457modifiedSince(StoreEntry * entry, request_t * request)
91f4d519 458{
459 int object_length;
460 MemObject *mem = entry->mem_obj;
1c3e77cd 461 time_t mod_time = entry->lastmod;
9fb13bb6 462 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry));
1c3e77cd 463 if (mod_time < 0)
464 mod_time = entry->timestamp;
0cdcddb9 465 debug(33, 3) ("modifiedSince: mod_time = %d\n", (int) mod_time);
1c3e77cd 466 if (mod_time < 0)
91f4d519 467 return 1;
468 /* Find size of the object */
038eb4ed 469 object_length = mem->reply->content_length;
cb69b4c7 470 if (object_length < 0)
07304bf9 471 object_length = contentLen(entry);
1c3e77cd 472 if (mod_time > request->ims) {
a3d5953d 473 debug(33, 3) ("--> YES: entry newer than client\n");
91f4d519 474 return 1;
1c3e77cd 475 } else if (mod_time < request->ims) {
a3d5953d 476 debug(33, 3) ("--> NO: entry older than client\n");
91f4d519 477 return 0;
478 } else if (request->imslen < 0) {
a3d5953d 479 debug(33, 3) ("--> NO: same LMT, no client length\n");
91f4d519 480 return 0;
481 } else if (request->imslen == object_length) {
a3d5953d 482 debug(33, 3) ("--> NO: same LMT, same length\n");
91f4d519 483 return 0;
484 } else {
a3d5953d 485 debug(33, 3) ("--> YES: same LMT, different length\n");
91f4d519 486 return 1;
487 }
488}
b3b64e58 489
a90eae18 490void
382d851a 491clientPurgeRequest(clientHttpRequest * http)
a90eae18 492{
a90eae18 493 StoreEntry *entry;
9b312a19 494 ErrorState *err = NULL;
8d6da567 495 HttpReply *r;
9b312a19 496 debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config.onoff.enable_purge);
17a0a4ee 497 if (!Config.onoff.enable_purge) {
79a15e0a 498 http->log_type = LOG_TCP_DENIED;
fe40a877 499 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
9b312a19 500 err->request = requestLink(http->request);
501 err->src_addr = http->conn->peer.sin_addr;
92695e5e 502 http->entry = clientCreateStoreEntry(http, http->request->method, null_request_flags);
79e0dc20 503 errorAppendEntry(http->entry, err);
a90eae18 504 return;
505 }
382d851a 506 http->log_type = LOG_TCP_MISS;
2f9aa365 507 if ((entry = storeGetPublic(http->uri, METHOD_GET)) == NULL) {
9b312a19 508 http->http_code = HTTP_NOT_FOUND;
a90eae18 509 } else {
510 storeRelease(entry);
9b312a19 511 http->http_code = HTTP_OK;
a90eae18 512 }
49d3fcb0 513 debug(33, 4) ("clientPurgeRequest: Not modified '%s'\n",
afa9f1cd 514 storeUrl(entry));
515 /*
516 * Make a new entry to hold the reply to be written
517 * to the client.
518 */
92695e5e 519 http->entry = clientCreateStoreEntry(http, http->request->method, null_request_flags);
8d6da567 520 httpReplyReset(r = http->entry->mem_obj->reply);
521 httpReplySetHeaders(r, 1.0, http->http_code, NULL, NULL, 0, 0, -1);
522 httpReplySwapOut(r, http->entry);
afa9f1cd 523 storeComplete(http->entry);
a90eae18 524}
e33ba616 525
526int
ea3a2a69 527checkNegativeHit(StoreEntry * e)
e33ba616 528{
d46a87a8 529 if (!EBIT_TEST(e->flags, ENTRY_NEGCACHED))
ea3a2a69 530 return 0;
531 if (e->expires <= squid_curtime)
532 return 0;
533 if (e->store_status != STORE_OK)
534 return 0;
535 return 1;
e33ba616 536}
7a2f978b 537
a7c05555 538void
0e473d70 539clientUpdateCounters(clientHttpRequest * http)
a7c05555 540{
ee1679df 541 int svc_time = tvSubMsec(http->start, current_time);
b4e7f82d 542 ping_data *i;
39edba21 543 HierarchyLogEntry *H;
0e473d70 544 Counter.client_http.requests++;
d8b83480 545 if (isTcpHit(http->log_type))
0e473d70 546 Counter.client_http.hits++;
0e473d70 547 if (http->request->err_type != ERR_NONE)
548 Counter.client_http.errors++;
12cf1be2 549 statHistCount(&Counter.client_http.all_svc_time, svc_time);
ee1679df 550 /*
551 * The idea here is not to be complete, but to get service times
552 * for only well-defined types. For example, we don't include
553 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
554 * (we *tried* to validate it, but failed).
555 */
556 switch (http->log_type) {
7c9c84ad 557 case LOG_TCP_REFRESH_HIT:
558 statHistCount(&Counter.client_http.nh_svc_time, svc_time);
559 break;
ee1679df 560 case LOG_TCP_IMS_HIT:
12cf1be2 561 statHistCount(&Counter.client_http.nm_svc_time, svc_time);
ee1679df 562 break;
563 case LOG_TCP_HIT:
564 case LOG_TCP_MEM_HIT:
b540e168 565 case LOG_TCP_OFFLINE_HIT:
12cf1be2 566 statHistCount(&Counter.client_http.hit_svc_time, svc_time);
ee1679df 567 break;
568 case LOG_TCP_MISS:
569 case LOG_TCP_CLIENT_REFRESH_MISS:
12cf1be2 570 statHistCount(&Counter.client_http.miss_svc_time, svc_time);
ee1679df 571 break;
572 default:
573 /* make compiler warnings go away */
574 break;
575 }
39edba21 576 H = &http->request->hier;
69c95dd3 577 switch (H->alg) {
578 case PEER_SA_DIGEST:
17b6e784 579 Counter.cd.times_used++;
69c95dd3 580 break;
581 case PEER_SA_ICP:
582 Counter.icp.times_used++;
b4e7f82d 583 i = &H->ping;
69c95dd3 584 if (0 != i->stop.tv_sec && 0 != i->start.tv_sec)
585 statHistCount(&Counter.icp.query_svc_time,
586 tvSubUsec(i->start, i->stop));
587 if (i->timeout)
588 Counter.icp.query_timeouts++;
589 break;
590 case PEER_SA_NETDB:
591 Counter.netdb.times_used++;
592 break;
593 default:
594 break;
17b6e784 595 }
a7c05555 596}
597
7a2f978b 598static void
599httpRequestFree(void *data)
600{
601 clientHttpRequest *http = data;
602 clientHttpRequest **H;
603 ConnStateData *conn = http->conn;
604 StoreEntry *entry = http->entry;
605 request_t *request = http->request;
606 MemObject *mem = NULL;
93f9abd0 607 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(entry));
1b02b5be 608 if (!clientCheckTransferDone(http)) {
7a2f978b 609 if (entry)
610 storeUnregister(entry, http); /* unregister BEFORE abort */
611 CheckQuickAbort(http);
612 entry = http->entry; /* reset, IMS might have changed it */
613 if (entry && entry->ping_status == PING_WAITING)
614 storeReleaseRequest(entry);
7a2f978b 615 }
616 assert(http->log_type < LOG_TYPE_MAX);
617 if (entry)
618 mem = entry->mem_obj;
619 if (http->out.size || http->log_type) {
728da2ee 620 http->al.icp.opcode = ICP_INVALID;
ef65d6ca 621 http->al.url = http->log_uri;
622 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http->al.url);
7a2f978b 623 if (mem) {
cb69b4c7 624 http->al.http.code = mem->reply->sline.status;
038eb4ed 625 http->al.http.content_type = strBuf(mem->reply->content_type);
7a2f978b 626 }
627 http->al.cache.caddr = conn->log_addr;
628 http->al.cache.size = http->out.size;
629 http->al.cache.code = http->log_type;
630 http->al.cache.msec = tvSubMsec(http->start, current_time);
def67559 631 if (request->user_ident[0])
632 http->al.cache.ident = request->user_ident;
633 else
634 http->al.cache.ident = conn->ident.ident;
7a2f978b 635 if (request) {
2246b732 636 Packer p;
637 MemBuf mb;
638 memBufDefInit(&mb);
639 packerToMemInit(&p, &mb);
640 httpHeaderPackInto(&request->header, &p);
7a2f978b 641 http->al.http.method = request->method;
2246b732 642 http->al.headers.request = xstrdup(mb.buf);
7a2f978b 643 http->al.hier = request->hier;
2246b732 644 packerClean(&p);
645 memBufClean(&mb);
7a2f978b 646 }
647 accessLogLog(&http->al);
a7c05555 648 clientUpdateCounters(http);
6f47fbc7 649 clientdbUpdate(conn->peer.sin_addr, http->log_type, PROTO_HTTP, http->out.size);
7a2f978b 650 }
7a2f978b 651 if (http->acl_checklist)
652 aclChecklistFree(http->acl_checklist);
88aad2e5 653 if (request)
c24fca87 654 checkFailureRatio(request->err_type, http->al.hier.code);
23d92c64 655 safe_free(http->uri);
656 safe_free(http->log_uri);
2246b732 657 safe_free(http->al.headers.request);
7a2f978b 658 safe_free(http->al.headers.reply);
d192d11f 659 stringClean(&http->range_iter.boundary);
7a2f978b 660 if (entry) {
661 http->entry = NULL;
662 storeUnregister(entry, http);
663 storeUnlockObject(entry);
664 }
665 /* old_entry might still be set if we didn't yet get the reply
1b02b5be 666 * code in clientHandleIMSReply() */
7a2f978b 667 if (http->old_entry) {
668 storeUnregister(http->old_entry, http);
669 storeUnlockObject(http->old_entry);
670 http->old_entry = NULL;
671 }
672 requestUnlink(http->request);
673 assert(http != http->next);
674 assert(http->conn->chr != NULL);
675 H = &http->conn->chr;
676 while (*H) {
677 if (*H == http)
678 break;
679 H = &(*H)->next;
680 }
681 assert(*H != NULL);
682 *H = http->next;
683 http->next = NULL;
684 cbdataFree(http);
685}
686
687/* This is a handler normally called by comm_close() */
688static void
689connStateFree(int fd, void *data)
690{
691 ConnStateData *connState = data;
692 clientHttpRequest *http;
93f9abd0 693 debug(33, 3) ("connStateFree: FD %d\n", fd);
7a2f978b 694 assert(connState != NULL);
79d39a72 695 while ((http = connState->chr) != NULL) {
7a2f978b 696 assert(http->conn == connState);
697 assert(connState->chr != connState->chr->next);
698 httpRequestFree(http);
699 }
700 if (connState->ident.fd > -1)
701 comm_close(connState->ident.fd);
702 safe_free(connState->in.buf);
59c4d35b 703 /* XXX account connState->in.buf */
7a2f978b 704 pconnHistCount(0, connState->nrequests);
705 cbdataFree(connState);
706}
707
708static void
99edd1c3 709clientInterpretRequestHeaders(clientHttpRequest * http)
7a2f978b 710{
711 request_t *request = http->request;
99edd1c3 712 const HttpHeader *req_hdr = &request->header;
713#if USE_USERAGENT_LOG
714 const char *str;
715#endif
99edd1c3 716 request->imslen = -1;
717 request->ims = httpHeaderGetTime(req_hdr, HDR_IF_MODIFIED_SINCE);
718 if (request->ims > 0)
92695e5e 719 request->flags.ims = 1;
99edd1c3 720 if (httpHeaderHas(req_hdr, HDR_PRAGMA)) {
721 String s = httpHeaderGetList(req_hdr, HDR_PRAGMA);
465dc415 722 if (strListIsMember(&s, "no-cache", ',')) {
9f60cfdf 723#if HTTP_VIOLATIONS
cbe3a719 724 if (Config.onoff.reload_into_ims)
92695e5e 725 request->flags.nocache_hack = 1;
cbe3a719 726 else if (refresh_nocache_hack)
92695e5e 727 request->flags.nocache_hack = 1;
465dc415 728 else
9f60cfdf 729#endif
92695e5e 730 request->flags.nocache = 1;
465dc415 731 }
99edd1c3 732 stringClean(&s);
7a2f978b 733 }
4b315324 734 /* ignore range header in non-GETs */
735 if (request->method == METHOD_GET) {
736 request->range = httpHeaderGetRange(req_hdr);
737 if (request->range)
92695e5e 738 request->flags.range = 1;
4b315324 739 }
99edd1c3 740 if (httpHeaderHas(req_hdr, HDR_AUTHORIZATION))
92695e5e 741 request->flags.auth = 1;
7a2f978b 742 if (request->login[0] != '\0')
92695e5e 743 request->flags.auth = 1;
99edd1c3 744 if (httpMsgIsPersistent(request->http_ver, req_hdr))
92695e5e 745 request->flags.proxy_keepalive = 1;
99edd1c3 746 if (httpHeaderHas(req_hdr, HDR_VIA)) {
747 String s = httpHeaderGetList(req_hdr, HDR_VIA);
edf3ffdc 748 /* ThisCache cannot be a member of Via header, "1.0 ThisCache" can */
749 if (strListIsSubstr(&s, ThisCache, ',')) {
9330543e 750 debug(33, 1) ("WARNING: Forwarding loop detected for '%s'\n",
751 http->uri);
752 debug(33, 1) ("--> %s\n", strBuf(s));
92695e5e 753 request->flags.loopdetect = 1;
7a2f978b 754 }
d21f1c54 755#if FORW_VIA_DB
ea285003 756 fvdbCountVia(strBuf(s));
d21f1c54 757#endif
99edd1c3 758 stringClean(&s);
d21f1c54 759 }
7a2f978b 760#if USE_USERAGENT_LOG
99edd1c3 761 if ((str = httpHeaderGetStr(req_hdr, HDR_USER_AGENT)))
762 logUserAgent(fqdnFromAddr(http->conn->peer.sin_addr), str);
d21f1c54 763#endif
764#if FORW_VIA_DB
99edd1c3 765 if (httpHeaderHas(req_hdr, HDR_X_FORWARDED_FOR)) {
766 String s = httpHeaderGetList(req_hdr, HDR_X_FORWARDED_FOR);
767 fvdbCountForw(strBuf(s));
768 stringClean(&s);
769 }
7a2f978b 770#endif
99edd1c3 771 request->cache_control = httpHeaderGetCc(req_hdr);
7a2f978b 772 if (request->method == METHOD_TRACE) {
99edd1c3 773 request->max_forwards = httpHeaderGetInt(req_hdr, HDR_MAX_FORWARDS);
7a2f978b 774 }
50ddd7a4 775 if (clientCachable(http))
92695e5e 776 request->flags.cachable = 1;
50ddd7a4 777 if (clientHierarchical(http))
92695e5e 778 request->flags.hierarchical = 1;
95e36d02 779#if DELAY_POOLS
780 if (delayClient(http)) {
781 debug(33, 5) ("clientInterpretRequestHeaders: delay request class %d position %d\n",
447e176b 782 request->delay_id >> 16,
783 request->delay_id & 0xFFFF);
95e36d02 784 }
785#endif
99edd1c3 786 debug(33, 5) ("clientInterpretRequestHeaders: REQ_NOCACHE = %s\n",
92695e5e 787 request->flags.nocache ? "SET" : "NOT SET");
99edd1c3 788 debug(33, 5) ("clientInterpretRequestHeaders: REQ_CACHABLE = %s\n",
92695e5e 789 request->flags.cachable ? "SET" : "NOT SET");
99edd1c3 790 debug(33, 5) ("clientInterpretRequestHeaders: REQ_HIERARCHICAL = %s\n",
92695e5e 791 request->flags.hierarchical ? "SET" : "NOT SET");
7a2f978b 792}
793
31be8b80 794static int
efb9218c 795clientCheckContentLength(request_t * r)
31be8b80 796{
205080e2 797 /* We only require a content-length for "upload" methods */
798 if (!pumpMethod(r->method))
0cdcddb9 799 return 1;
205080e2 800 if (httpHeaderGetInt(&r->header, HDR_CONTENT_LENGTH) < 0)
31be8b80 801 return 0;
802 return 1;
803}
804
7a2f978b 805static int
50ddd7a4 806clientCachable(clientHttpRequest * http)
7a2f978b 807{
23d92c64 808 const char *url = http->uri;
7a2f978b 809 request_t *req = http->request;
810 method_t method = req->method;
bd05e3e3 811 aclCheck_t ch;
812 memset(&ch, '\0', sizeof(ch));
813 /*
814 * Hopefully, nobody really wants 'no_cache' by client's IP
815 * address, but if they do, this should work if they use IP
816 * addresses in their ACLs, or if the client's address is in
817 * the FQDN cache.
818 *
819 * This may not work yet for 'dst' and 'dst_domain' ACLs.
820 */
821 ch.src_addr = http->conn->peer.sin_addr;
822 ch.request = http->request;
79a51871 823 /*
824 * aclCheckFast returns 1 for ALLOW and 0 for DENY. The default
825 * is ALLOW, so we require 'no_cache DENY foo' in squid.conf
826 * to indicate uncachable objects.
827 */
95291419 828 if (!aclCheckFast(Config.accessList.noCache, &ch))
4b4cd312 829 return 0;
7a2f978b 830 if (req->protocol == PROTO_HTTP)
831 return httpCachable(method);
832 /* FTP is always cachable */
833 if (req->protocol == PROTO_GOPHER)
834 return gopherCachable(url);
835 if (req->protocol == PROTO_WAIS)
836 return 0;
837 if (method == METHOD_CONNECT)
838 return 0;
839 if (method == METHOD_TRACE)
840 return 0;
841 if (req->protocol == PROTO_CACHEOBJ)
842 return 0;
843 return 1;
844}
845
846/* Return true if we can query our neighbors for this object */
847static int
50ddd7a4 848clientHierarchical(clientHttpRequest * http)
7a2f978b 849{
23d92c64 850 const char *url = http->uri;
7a2f978b 851 request_t *request = http->request;
852 method_t method = request->method;
853 const wordlist *p = NULL;
854
855 /* IMS needs a private key, so we can use the hierarchy for IMS only
856 * if our neighbors support private keys */
92695e5e 857 if (request->flags.ims && !neighbors_do_private_keys)
7a2f978b 858 return 0;
92695e5e 859 if (request->flags.auth)
7a2f978b 860 return 0;
861 if (method == METHOD_TRACE)
862 return 1;
863 if (method != METHOD_GET)
864 return 0;
865 /* scan hierarchy_stoplist */
866 for (p = Config.hierarchy_stoplist; p; p = p->next)
867 if (strstr(url, p->key))
868 return 0;
92695e5e 869 if (request->flags.loopdetect)
7a2f978b 870 return 0;
871 if (request->protocol == PROTO_HTTP)
872 return httpCachable(method);
873 if (request->protocol == PROTO_GOPHER)
874 return gopherCachable(url);
875 if (request->protocol == PROTO_WAIS)
876 return 0;
877 if (request->protocol == PROTO_CACHEOBJ)
878 return 0;
879 return 1;
880}
881
cf50a0af 882int
7a2f978b 883isTcpHit(log_type code)
884{
885 /* this should be a bitmap for better optimization */
886 if (code == LOG_TCP_HIT)
887 return 1;
888 if (code == LOG_TCP_IMS_HIT)
889 return 1;
890 if (code == LOG_TCP_REFRESH_FAIL_HIT)
891 return 1;
892 if (code == LOG_TCP_REFRESH_HIT)
893 return 1;
894 if (code == LOG_TCP_NEGATIVE_HIT)
895 return 1;
896 if (code == LOG_TCP_MEM_HIT)
897 return 1;
b540e168 898 if (code == LOG_TCP_OFFLINE_HIT)
899 return 1;
7a2f978b 900 return 0;
901}
902
1c3e77cd 903/*
904 * returns true if If-Range specs match reply, false otherwise
905 */
a9771e51 906static int
907clientIfRangeMatch(clientHttpRequest * http, HttpReply * rep)
908{
909 const TimeOrTag spec = httpHeaderGetTimeOrTag(&http->request->header, HDR_IF_RANGE);
910 /* check for parsing falure */
911 if (!spec.valid)
912 return 0;
913 /* got an ETag? */
914 if (spec.tag.str) {
915 ETag rep_tag = httpHeaderGetETag(&rep->header, HDR_ETAG);
ebb6e9a0 916 debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n",
a9771e51 917 spec.tag.str, rep_tag.str ? rep_tag.str : "<none>");
918 if (!rep_tag.str)
ebb6e9a0 919 return 0; /* entity has no etag to compare with! */
a9771e51 920 if (spec.tag.weak || rep_tag.weak) {
9834a631 921 debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n",
a9771e51 922 spec.tag.str, rep_tag.str);
ebb6e9a0 923 return 0; /* must use strong validator for sub-range requests */
a9771e51 924 }
925 return etagIsEqual(&rep_tag, &spec.tag);
926 }
927 /* got modification time? */
928 if (spec.time >= 0) {
929 return http->entry->lastmod <= spec.time;
930 }
ebb6e9a0 931 assert(0); /* should not happen */
a9771e51 932 return 0;
933}
934
e17d81d3 935/* adds appropriate Range headers if needed */
936static void
937clientBuildRangeHeader(clientHttpRequest * http, HttpReply * rep)
938{
5e2dba16 939 HttpHeader *hdr = rep ? &rep->header : 0;
e17d81d3 940 const char *range_err = NULL;
941 assert(http->request->range);
942 /* check if we still want to do ranges */
9834a631 943 if (!rep)
944 range_err = "no [parse-able] reply";
945 else if (rep->sline.status != HTTP_OK)
e17d81d3 946 range_err = "wrong status code";
ebb6e9a0 947 else if (httpHeaderHas(hdr, HDR_CONTENT_RANGE))
e17d81d3 948 range_err = "origin server does ranges";
ebb6e9a0 949 else if (rep->content_length < 0)
e17d81d3 950 range_err = "unknown length";
ebb6e9a0 951 else if (rep->content_length != http->entry->mem_obj->reply->content_length)
952 range_err = "INCONSISTENT length"; /* a bug? */
953 else if (httpHeaderHas(&http->request->header, HDR_IF_RANGE) && !clientIfRangeMatch(http, rep))
a9771e51 954 range_err = "If-Range match failed";
ebb6e9a0 955 else if (!httpHdrRangeCanonize(http->request->range, rep->content_length))
e17d81d3 956 range_err = "canonization failed";
ebb6e9a0 957 else if (httpHdrRangeIsComplex(http->request->range))
e17d81d3 958 range_err = "too complex range header";
959 /* get rid of our range specs on error */
960 if (range_err) {
cdedf7db 961 debug(33, 2) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err);
e17d81d3 962 httpHdrRangeDestroy(http->request->range);
963 http->request->range = NULL;
964 } else {
965 const int spec_count = http->request->range->specs.count;
cdedf7db 966 debug(33, 2) ("clientBuildRangeHeader: range spec count: %d clen: %d\n",
e17d81d3 967 spec_count, rep->content_length);
968 assert(spec_count > 0);
a9771e51 969 /* ETags should not be returned with Partial Content replies? */
970 httpHeaderDelById(hdr, HDR_ETAG);
ebb6e9a0 971 /* append appropriate header(s) */
e17d81d3 972 if (spec_count == 1) {
973 HttpHdrRangePos pos = HttpHdrRangeInitPos;
889b534a 974 const HttpHdrRangeSpec *spec = httpHdrRangeGetSpec(http->request->range, &pos);
975 assert(spec);
e17d81d3 976 /* append Content-Range */
889b534a 977 httpHeaderAddContRange(hdr, *spec, rep->content_length);
e17d81d3 978 /* set new Content-Length to the actual number of OCTETs
979 * transmitted in the message-body */
980 httpHeaderDelById(hdr, HDR_CONTENT_LENGTH);
889b534a 981 httpHeaderPutInt(hdr, HDR_CONTENT_LENGTH, spec->length);
982 debug(33, 2) ("clientBuildRangeHeader: actual content length: %d\n", spec->length);
e17d81d3 983 } else {
984 /* multipart! */
985 /* generate boundary string */
986 http->range_iter.boundary = httpHdrRangeBoundaryStr(http);
987 /* delete old Content-Type, add ours */
988 httpHeaderDelById(hdr, HDR_CONTENT_TYPE);
989 httpHeaderPutStrf(hdr, HDR_CONTENT_TYPE,
990 "multipart/byteranges; boundary=\"%s\"",
991 strBuf(http->range_iter.boundary));
992 /* no need for Content-Length in multipart responses */
cded937d 993 /* but we must delete the original one if we cannot (yet)
994 * calculate the actual length */
995 httpHeaderDelById(hdr, HDR_CONTENT_LENGTH);
e17d81d3 996 }
997 }
998}
999
d192d11f 1000/* filters out unwanted entries from original reply header
1001 * adds extra entries if we have more info than origin server
1002 * adds Squid specific entries */
2246b732 1003static void
eeb423fb 1004clientBuildReplyHeader(clientHttpRequest * http, HttpReply * rep)
2246b732 1005{
1006 HttpHeader *hdr = &rep->header;
1007 int is_hit = isTcpHit(http->log_type);
7673173d 1008 request_t *request = http->request;
2246b732 1009#if DONT_FILTER_THESE
1010 /* but you might want to if you run Squid as an HTTP accelerator */
d192d11f 1011 /* httpHeaderDelById(hdr, HDR_ACCEPT_RANGES); */
2246b732 1012 httpHeaderDelById(hdr, HDR_ETAG);
1013#endif
1014 httpHeaderDelById(hdr, HDR_PROXY_CONNECTION);
1015 /* here: Keep-Alive is a field-name, not a connection directive! */
1016 httpHeaderDelByName(hdr, "Keep-Alive");
1017 /* remove Set-Cookie if a hit */
1018 if (is_hit)
1019 httpHeaderDelById(hdr, HDR_SET_COOKIE);
1020 /* handle Connection header */
1021 if (httpHeaderHas(hdr, HDR_CONNECTION)) {
1022 /* anything that matches Connection list member will be deleted */
1023 String strConnection = httpHeaderGetList(hdr, HDR_CONNECTION);
1024 const HttpHeaderEntry *e;
1025 HttpHeaderPos pos = HttpHeaderInitPos;
7673173d 1026 /*
1027 * think: on-average-best nesting of the two loops (hdrEntry
1028 * and strListItem) @?@
1029 */
1030 /*
1031 * maybe we should delete standard stuff ("keep-alive","close")
1032 * from strConnection first?
1033 */
2246b732 1034 while ((e = httpHeaderGetEntry(hdr, &pos))) {
1035 if (strListIsMember(&strConnection, strBuf(e->name), ','))
1036 httpHeaderDelAt(hdr, pos);
1037 }
1038 httpHeaderDelById(hdr, HDR_CONNECTION);
1039 stringClean(&strConnection);
1040 }
d192d11f 1041 /* Handle Ranges */
7673173d 1042 if (request->range)
e17d81d3 1043 clientBuildRangeHeader(http, rep);
7673173d 1044 /*
1045 * Add Age header, not that our header must replace Age headers
1046 * from other caches if any
1047 */
cc66f616 1048 if (http->entry->timestamp > 0) {
1049 httpHeaderDelById(hdr, HDR_AGE);
7673173d 1050 /*
1051 * we do not follow HTTP/1.1 precisely here becuase we rely
1052 * on Date header when computing entry->timestamp; we should
1053 * be using _request_ time if Date header is not available
1054 * or if it is out of sync
1055 */
cc66f616 1056 httpHeaderPutInt(hdr, HDR_AGE,
7673173d 1057 http->entry->timestamp <= squid_curtime ?
1058 squid_curtime - http->entry->timestamp : 0);
cc66f616 1059 }
2246b732 1060 /* Append X-Cache */
1061 httpHeaderPutStrf(hdr, HDR_X_CACHE, "%s from %s",
1062 is_hit ? "HIT" : "MISS", getMyHostname());
1063#if USE_CACHE_DIGESTS
1064 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1065 httpHeaderPutStrf(hdr, HDR_X_CACHE_LOOKUP, "%s from %s:%d",
1066 http->lookup_type ? http->lookup_type : "NONE",
1067 getMyHostname(), Config.Port.http->i);
1068#endif
1069 /* Only replies with valid Content-Length can be sent with keep-alive */
7673173d 1070 if (request->method != METHOD_HEAD &&
2246b732 1071 http->entry->mem_obj->reply->content_length < 0)
92695e5e 1072 request->flags.proxy_keepalive = 0;
2246b732 1073 /* Signal keep-alive if needed */
7673173d 1074 httpHeaderPutStr(hdr,
1075 http->flags.accel ? HDR_CONNECTION : HDR_PROXY_CONNECTION,
92695e5e 1076 request->flags.proxy_keepalive ? "keep-alive" : "close");
2246b732 1077#if ADD_X_REQUEST_URI
1078 /*
1079 * Knowing the URI of the request is useful when debugging persistent
1080 * connections in a client; we cannot guarantee the order of http headers,
1081 * but X-Request-URI is likely to be the very last header to ease use from a
1082 * debugger [hdr->entries.count-1].
1083 */
eeb423fb 1084 httpHeaderPutStr(hdr, HDR_X_REQUEST_URI,
2246b732 1085 http->entry->mem_obj->url ? http->entry->mem_obj->url : http->uri);
1086#endif
1087}
1088
eeb423fb 1089static HttpReply *
2246b732 1090clientBuildReply(clientHttpRequest * http, const char *buf, size_t size)
1091{
1092 HttpReply *rep = httpReplyCreate();
2246b732 1093 if (httpReplyParse(rep, buf)) {
1094 /* enforce 1.0 reply version */
1095 rep->sline.version = 1.0;
1096 /* do header conversions */
1097 clientBuildReplyHeader(http, rep);
d192d11f 1098 /* if we do ranges, change status to "Partial Content" */
1099 if (http->request->range)
1100 httpStatusLineSet(&rep->sline, rep->sline.version, HTTP_PARTIAL_CONTENT, NULL);
2246b732 1101 } else {
1102 /* parsing failure, get rid of the invalid reply */
1103 httpReplyDestroy(rep);
1104 rep = NULL;
9834a631 1105 /* if we were going to do ranges, backoff */
1106 if (http->request->range)
782b5f40 1107 clientBuildRangeHeader(http, rep); /* will fail and destroy request->range */
2246b732 1108 }
1109 return rep;
1110}
7a2f978b 1111
85071fe3 1112/*
1113 * clientCacheHit should only be called until the HTTP reply headers
1114 * have been parsed. Normally this should be a single call, but
1115 * it might take more than one. As soon as we have the headers,
1116 * we hand off to clientSendMoreData, clientProcessExpired, or
1117 * clientProcessMiss.
1118 */
4c323c5f 1119static void
7a2f978b 1120clientCacheHit(void *data, char *buf, ssize_t size)
1121{
1122 clientHttpRequest *http = data;
49d3fcb0 1123 StoreEntry *e = http->entry;
1124 MemObject *mem;
1125 request_t *r = http->request;
93f9abd0 1126 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http->uri, (int) size);
49d3fcb0 1127 if (http->entry == NULL) {
cc27d775 1128 memFree(MEM_CLIENT_SOCK_BUF, buf);
93f9abd0 1129 debug(33, 3) ("clientCacheHit: request aborted\n");
49d3fcb0 1130 return;
1131 } else if (size < 0) {
7a2f978b 1132 /* swap in failure */
cc27d775 1133 memFree(MEM_CLIENT_SOCK_BUF, buf);
93f9abd0 1134 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http->uri);
7a2f978b 1135 http->log_type = LOG_TCP_SWAPFAIL_MISS;
fdfc0d63 1136 if ((e = http->entry)) {
1137 http->entry = NULL;
1138 storeUnregister(e, http);
fdfc0d63 1139 storeUnlockObject(e);
1140 }
fb63215a 1141 clientProcessMiss(http);
49d3fcb0 1142 return;
1143 }
1144 assert(size > 0);
1145 mem = e->mem_obj;
1146 assert(e->store_status != STORE_ABORTED);
1147 if (mem->reply->sline.status == 0) {
1148 /*
1149 * we don't have full reply headers yet; either wait for more or
1150 * punt to clientProcessMiss.
1151 */
25944e56 1152 if (e->mem_status == IN_MEMORY || e->store_status == STORE_OK) {
cc27d775 1153 memFree(MEM_CLIENT_SOCK_BUF, buf);
49d3fcb0 1154 clientProcessMiss(http);
cc27d775 1155 } else if (size == CLIENT_SOCK_SZ && http->out.offset == 0) {
1156 memFree(MEM_CLIENT_SOCK_BUF, buf);
49d3fcb0 1157 clientProcessMiss(http);
1158 } else {
1159 debug(33, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
1160 storeClientCopy(e,
1161 http->out.offset + size,
1162 http->out.offset,
cc27d775 1163 CLIENT_SOCK_SZ,
49d3fcb0 1164 buf,
1165 clientCacheHit,
1166 http);
1167 }
1168 return;
1169 }
1170 /*
1171 * Got the headers, now grok them
1172 */
1173 assert(http->log_type == LOG_TCP_HIT);
1174 if (checkNegativeHit(e)) {
1175 http->log_type = LOG_TCP_NEGATIVE_HIT;
1176 clientSendMoreData(data, buf, size);
d657ffad 1177 } else if (r->method == METHOD_HEAD) {
1178 /*
1179 * RFC 2068 seems to indicate there is no "conditional HEAD"
1180 * request. We cannot validate a cached object for a HEAD
1181 * request, nor can we return 304.
1182 */
1183 if (e->mem_status == IN_MEMORY)
1184 http->log_type = LOG_TCP_MEM_HIT;
1185 clientSendMoreData(data, buf, size);
e25c139f 1186 } else if (refreshCheck(e, r, 0) && !http->flags.internal) {
49d3fcb0 1187 /*
1188 * We hold a stale copy; it needs to be validated
1189 */
9689d97c 1190 /*
1191 * The 'need_validation' flag is used to prevent forwarding
1192 * loops between siblings. If our copy of the object is stale,
1193 * then we should probably only use parents for the validation
1194 * request. Otherwise two siblings could generate a loop if
1195 * both have a stale version of the object.
1196 */
1197 r->flags.need_validation = 1;
0cdcddb9 1198 if (e->lastmod < 0) {
1199 /*
1c3e77cd 1200 * Previous reply didn't have a Last-Modified header,
1201 * we cannot revalidate it.
1202 */
1203 http->log_type = LOG_TCP_MISS;
1204 clientProcessMiss(http);
92695e5e 1205 } else if (r->flags.nocache) {
cbe3a719 1206 /*
1207 * This did not match a refresh pattern that overrides no-cache
1208 * we should honour the client no-cache header.
1209 */
1210 http->log_type = LOG_TCP_CLIENT_REFRESH_MISS;
1211 clientProcessMiss(http);
1c3e77cd 1212 } else if (r->protocol == PROTO_HTTP) {
cbe3a719 1213 /*
1214 * Object needs to be revalidated
1215 * XXX This could apply to FTP as well, if Last-Modified is known.
1216 */
49d3fcb0 1217 http->log_type = LOG_TCP_REFRESH_MISS;
1218 clientProcessExpired(http);
1219 } else {
cbe3a719 1220 /*
1221 * We don't know how to re-validate other protocols. Handle
1222 * them as if the object has expired.
1223 */
49d3fcb0 1224 http->log_type = LOG_TCP_MISS;
1225 clientProcessMiss(http);
1226 }
cc27d775 1227 memFree(MEM_CLIENT_SOCK_BUF, buf);
92695e5e 1228 } else if (r->flags.ims) {
49d3fcb0 1229 /*
1230 * Handle If-Modified-Since requests from the client
1231 */
1232 if (mem->reply->sline.status != HTTP_OK) {
1233 debug(33, 4) ("clientCacheHit: Reply code %d != 200\n",
1234 mem->reply->sline.status);
cc27d775 1235 memFree(MEM_CLIENT_SOCK_BUF, buf);
49d3fcb0 1236 clientProcessMiss(http);
1237 } else if (modifiedSince(e, http->request)) {
815ef411 1238 http->log_type = LOG_TCP_IMS_HIT;
49d3fcb0 1239 clientSendMoreData(data, buf, size);
1240 } else {
1241 MemBuf mb = httpPacked304Reply(e->mem_obj->reply);
1242 http->log_type = LOG_TCP_IMS_HIT;
cc27d775 1243 memFree(MEM_CLIENT_SOCK_BUF, buf);
49d3fcb0 1244 storeUnregister(e, http);
1245 storeUnlockObject(e);
92695e5e 1246 e = clientCreateStoreEntry(http, http->request->method, null_request_flags);
49d3fcb0 1247 http->entry = e;
1248 httpReplyParse(e->mem_obj->reply, mb.buf);
1249 storeAppend(e, mb.buf, mb.size);
1250 memBufClean(&mb);
1251 storeComplete(e);
1252 }
1253 } else {
1254 /*
1255 * plain ol' cache hit
1256 */
1257 if (e->mem_status == IN_MEMORY)
1258 http->log_type = LOG_TCP_MEM_HIT;
b540e168 1259 else if (Config.onoff.offline)
1260 http->log_type = LOG_TCP_OFFLINE_HIT;
49d3fcb0 1261 clientSendMoreData(data, buf, size);
7a2f978b 1262 }
1263}
1264
d192d11f 1265
1266/* extracts a "range" from *buf and appends them to mb, updating all offsets and such */
1267static void
ebb6e9a0 1268clientPackRange(clientHttpRequest * http, HttpHdrRangeIter * i, const char **buf, ssize_t * size, MemBuf * mb)
d192d11f 1269{
1270 const size_t copy_sz = i->debt_size <= *size ? i->debt_size : *size;
1271 off_t body_off = http->out.offset - i->prefix_size;
1272 assert(*size > 0);
889b534a 1273 assert(i->spec);
d192d11f 1274 /* intersection of "have" and "need" ranges must not be empty */
889b534a 1275 assert(body_off < i->spec->offset + i->spec->length);
1276 assert(body_off + *size > i->spec->offset);
d192d11f 1277 /* put boundary and headers at the beginning of range in a multi-range */
889b534a 1278 if (http->request->range->specs.count > 1 && i->debt_size == i->spec->length) {
ebb6e9a0 1279 HttpReply *rep = http->entry->mem_obj ? /* original reply */
1280 http->entry->mem_obj->reply : NULL;
d192d11f 1281 HttpHeader hdr;
1282 Packer p;
1283 assert(rep);
1284 /* put boundary */
cdedf7db 1285 debug(33, 5) ("clientPackRange: appending boundary: %s\n", strBuf(i->boundary));
d192d11f 1286 /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1287 memBufPrintf(mb, "\r\n--%s\r\n", strBuf(i->boundary));
1288 httpHeaderInit(&hdr, hoReply);
1289 if (httpHeaderHas(&rep->header, HDR_CONTENT_TYPE))
1290 httpHeaderPutStr(&hdr, HDR_CONTENT_TYPE, httpHeaderGetStr(&rep->header, HDR_CONTENT_TYPE));
889b534a 1291 httpHeaderAddContRange(&hdr, *i->spec, rep->content_length);
d192d11f 1292 packerToMemInit(&p, mb);
1293 httpHeaderPackInto(&hdr, &p);
1294 packerClean(&p);
1295 httpHeaderClean(&hdr);
1296 /* append <crlf> (we packed a header, not a reply */
1297 memBufPrintf(mb, "\r\n");
1298 }
1299 /* append */
cdedf7db 1300 debug(33, 3) ("clientPackRange: appending %d bytes\n", copy_sz);
d192d11f 1301 memBufAppend(mb, *buf, copy_sz);
1302 /* update offsets */
1303 *size -= copy_sz;
1304 i->debt_size -= copy_sz;
1305 body_off += copy_sz;
1306 *buf += copy_sz;
ebb6e9a0 1307 http->out.offset = body_off + i->prefix_size; /* sync */
d192d11f 1308 /* paranoid check */
1309 assert(*size >= 0 && i->debt_size >= 0);
1310}
1311
889b534a 1312/* returns true if there is still data available to pack more ranges
1313 * increments iterator "i"
1314 * used by clientPackMoreRanges */
1315static int
ebb6e9a0 1316clientCanPackMoreRanges(const clientHttpRequest * http, HttpHdrRangeIter * i, ssize_t size)
889b534a 1317{
1318 /* first update "i" if needed */
1319 if (!i->debt_size) {
1320 if ((i->spec = httpHdrRangeGetSpec(http->request->range, &i->pos)))
1321 i->debt_size = i->spec->length;
1322 }
ebb6e9a0 1323 assert(!i->debt_size == !i->spec); /* paranoid sync condition */
889b534a 1324 /* continue condition: need_more_data && have_more_data */
1325 return i->spec && size > 0;
1326}
d192d11f 1327
1328/* extracts "ranges" from buf and appends them to mb, updating all offsets and such */
1329/* returns true if we need more data */
1330static int
ebb6e9a0 1331clientPackMoreRanges(clientHttpRequest * http, const char *buf, ssize_t size, MemBuf * mb)
d192d11f 1332{
1333 HttpHdrRangeIter *i = &http->range_iter;
d192d11f 1334 /* offset in range specs does not count the prefix of an http msg */
1335 off_t body_off = http->out.offset - i->prefix_size;
1336 assert(size >= 0);
9834a631 1337 /* check: reply was parsed and range iterator was initialized */
1338 assert(i->prefix_size > 0);
d192d11f 1339 /* filter out data according to range specs */
1340 /* note: order of loop conditions is significant! */
889b534a 1341 while (clientCanPackMoreRanges(http, i, size)) {
ebb6e9a0 1342 off_t start; /* offset of still missing data */
889b534a 1343 assert(i->spec);
1344 start = i->spec->offset + i->spec->length - i->debt_size;
cdedf7db 1345 debug(33, 2) ("clientPackMoreRanges: in: offset: %d size: %d\n",
ebb6e9a0 1346 (int) body_off, size);
cdedf7db 1347 debug(33, 2) ("clientPackMoreRanges: out: start: %d spec[%d]: [%d, %d), len: %d debt: %d\n",
ebb6e9a0 1348 (int) start, (int) i->pos, i->spec->offset, (int) (i->spec->offset + i->spec->length), i->spec->length, i->debt_size);
1349 assert(body_off <= start); /* we did not miss it */
d192d11f 1350 /* skip up to start */
1351 if (body_off + size > start) {
1352 const size_t skip_size = start - body_off;
1353 body_off = start;
1354 size -= skip_size;
1355 buf += skip_size;
1356 } else {
1357 /* has not reached start yet */
1358 body_off += size;
1359 size = 0;
1360 buf = NULL;
1361 }
1362 /* put next chunk if any */
1363 if (size) {
ebb6e9a0 1364 http->out.offset = body_off + i->prefix_size; /* sync */
d192d11f 1365 clientPackRange(http, i, &buf, &size, mb);
ebb6e9a0 1366 body_off = http->out.offset - i->prefix_size; /* sync */
d192d11f 1367 }
1368 }
ebb6e9a0 1369 assert(!i->debt_size == !i->spec); /* paranoid sync condition */
cdedf7db 1370 debug(33, 2) ("clientPackMoreRanges: buf exhausted: in: offset: %d size: %d need_more: %d\n",
ebb6e9a0 1371 (int) body_off, size, i->debt_size);
889b534a 1372 if (i->debt_size) {
1373 debug(33, 2) ("clientPackMoreRanges: need more: spec[%d]: [%d, %d), len: %d\n",
ebb6e9a0 1374 (int) i->pos, i->spec->offset, (int) (i->spec->offset + i->spec->length), i->spec->length);
889b534a 1375 /* skip the data we do not need if possible */
ebb6e9a0 1376 if (i->debt_size == i->spec->length) /* at the start of the cur. spec */
889b534a 1377 body_off = i->spec->offset;
d192d11f 1378 else
889b534a 1379 assert(body_off == i->spec->offset + i->spec->length - i->debt_size);
ebb6e9a0 1380 } else if (http->request->range->specs.count > 1) {
d192d11f 1381 /* put terminating boundary for multiparts */
1382 memBufPrintf(mb, "\r\n--%s--\r\n", strBuf(i->boundary));
1383 }
ebb6e9a0 1384 http->out.offset = body_off + i->prefix_size; /* sync */
889b534a 1385 return i->debt_size > 0;
d192d11f 1386}
1387
2246b732 1388/*
1389 * accepts chunk of a http message in buf, parses prefix, filters headers and
1390 * such, writes processed message to the client's socket
1391 */
4c323c5f 1392static void
7a2f978b 1393clientSendMoreData(void *data, char *buf, ssize_t size)
1394{
1395 clientHttpRequest *http = data;
1396 StoreEntry *entry = http->entry;
1397 ConnStateData *conn = http->conn;
1398 int fd = conn->fd;
2246b732 1399 HttpReply *rep = NULL;
2246b732 1400 const char *body_buf = buf;
1401 ssize_t body_size = size;
9f086470 1402 MemBuf mb;
bcd3b08a 1403 ssize_t check_size = 0;
93f9abd0 1404 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http->uri, (int) size);
cc27d775 1405 assert(size <= CLIENT_SOCK_SZ);
40defb17 1406 assert(http->request != NULL);
93f9abd0 1407 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
5f6ac48b 1408 fd, storeUrl(entry), (int) http->out.offset);
7a2f978b 1409 if (conn->chr != http) {
1410 /* there is another object in progress, defer this one */
aebbcd07 1411 debug(33, 1) ("clientSendMoreData: Deferring %s\n", storeUrl(entry));
cc27d775 1412 memFree(MEM_CLIENT_SOCK_BUF, buf);
7a2f978b 1413 return;
fdfc0d63 1414 } else if (entry && entry->store_status == STORE_ABORTED) {
d6e928c9 1415 /* call clientWriteComplete so the client socket gets closed */
1416 clientWriteComplete(fd, NULL, 0, COMM_OK, http);
cc27d775 1417 memFree(MEM_CLIENT_SOCK_BUF, buf);
7a2f978b 1418 return;
1419 } else if (size < 0) {
d6e928c9 1420 /* call clientWriteComplete so the client socket gets closed */
1421 clientWriteComplete(fd, NULL, 0, COMM_OK, http);
cc27d775 1422 memFree(MEM_CLIENT_SOCK_BUF, buf);
7a2f978b 1423 return;
1424 } else if (size == 0) {
d6e928c9 1425 /* call clientWriteComplete so the client socket gets closed */
1426 clientWriteComplete(fd, NULL, 0, COMM_OK, http);
cc27d775 1427 memFree(MEM_CLIENT_SOCK_BUF, buf);
7a2f978b 1428 return;
1429 }
40defb17 1430 if (http->out.offset == 0) {
7a2f978b 1431 if (Config.onoff.log_mime_hdrs) {
2334c194 1432 size_t k;
1433 if ((k = headersEnd(buf, size))) {
7a2f978b 1434 safe_free(http->al.headers.reply);
1afe05c5 1435 http->al.headers.reply = xcalloc(k + 1, 1);
2334c194 1436 xstrncpy(http->al.headers.reply, buf, k);
7a2f978b 1437 }
1438 }
2246b732 1439 rep = clientBuildReply(http, buf, size);
1440 if (rep) {
1441 body_size = size - rep->hdr_sz;
1442 assert(body_size >= 0);
1f7e91a1 1443 body_buf = buf + rep->hdr_sz;
d192d11f 1444 http->range_iter.prefix_size = rep->hdr_sz;
2246b732 1445 debug(33, 3) ("clientSendMoreData: Appending %d bytes after %d bytes of headers\n",
1446 body_size, rep->hdr_sz);
cc27d775 1447 } else if (size < CLIENT_SOCK_SZ && entry->store_status == STORE_PENDING) {
9f086470 1448 /* wait for more to arrive */
1449 storeClientCopy(entry,
1450 http->out.offset + size,
1451 http->out.offset,
cc27d775 1452 CLIENT_SOCK_SZ,
9f086470 1453 buf,
1454 clientSendMoreData,
1455 http);
1456 return;
7a2f978b 1457 }
d192d11f 1458 /* reset range iterator */
1459 http->range_iter.pos = HttpHdrRangeInitPos;
7a2f978b 1460 }
7a2f978b 1461 if (http->request->method == METHOD_HEAD) {
9f086470 1462 if (rep) {
1463 /* do not forward body for HEAD replies */
1464 body_size = 0;
9f086470 1465 http->flags.done_copying = 1;
1466 } else {
782b5f40 1467 /*
1468 * If we are here, then store_status == STORE_OK and it
1469 * seems we have a HEAD repsponse which is missing the
1470 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
1471 * does this). Because clientBuildReply() fails we just
1472 * call this reply a body, set the done_copying flag and
1473 * continue...
1474 */
1475 http->flags.done_copying = 1;
7a2f978b 1476 }
1477 }
d192d11f 1478 /* write headers and/or body if any */
1f7e91a1 1479 assert(rep || (body_buf && body_size));
9f086470 1480 /* init mb; put status line and headers if any */
1481 if (rep) {
1482 mb = httpReplyPack(rep);
bcd3b08a 1483 http->out.offset += rep->hdr_sz;
1484 check_size += rep->hdr_sz;
9f086470 1485 httpReplyDestroy(rep);
1486 rep = NULL;
1487 } else {
1488 /* leave space for growth incase we do ranges */
cc27d775 1489 memBufInit(&mb, CLIENT_SOCK_SZ, 2 * CLIENT_SOCK_SZ);
9f086470 1490 }
1491 /* append body if any */
1f7e91a1 1492 if (body_buf && body_size) {
9f086470 1493 if (http->request->range) {
bcd3b08a 1494 /* Only GET requests should have ranges */
1495 assert(http->request->method == METHOD_GET);
1496 /* clientPackMoreRanges() updates http->out.offset */
9f086470 1497 /* force the end of the transfer if we are done */
1498 if (!clientPackMoreRanges(http, body_buf, body_size, &mb))
1499 http->flags.done_copying = 1;
2246b732 1500 } else {
bcd3b08a 1501 http->out.offset += body_size;
1502 check_size += body_size;
9f086470 1503 memBufAppend(&mb, body_buf, body_size);
2246b732 1504 }
2246b732 1505 }
bbe3cbdc 1506 if (!http->request->range && http->request->method == METHOD_GET)
bcd3b08a 1507 assert(check_size == size);
9f086470 1508 /* write */
1509 comm_write_mbuf(fd, mb, clientWriteComplete, http);
2246b732 1510 /* if we don't do it, who will? */
cc27d775 1511 memFree(MEM_CLIENT_SOCK_BUF, buf);
7a2f978b 1512}
1513
1a92a1e2 1514static
1515void
1516clientKeepaliveNextRequest(clientHttpRequest * http)
1517{
1518 ConnStateData *conn = http->conn;
1519 StoreEntry *entry;
ebb6e9a0 1520 debug(33, 3) ("clientKeepaliveNextRequest: FD %d\n", conn->fd);
1a92a1e2 1521 conn->defer.until = 0; /* Kick it to read a new request */
1522 httpRequestFree(http);
1523 if ((http = conn->chr) != NULL) {
1524 debug(33, 1) ("clientKeepaliveNextRequest: FD %d Sending next\n",
1525 conn->fd);
1526 entry = http->entry;
7ddc902f 1527 assert(entry);
1a92a1e2 1528 if (0 == storeClientCopyPending(entry, http)) {
1529 if (entry->store_status == STORE_ABORTED)
c07aed63 1530 debug(33, 0) ("clientKeepaliveNextRequest: entry->swap_status == STORE_ABORTED\n");
1a92a1e2 1531 storeClientCopy(entry,
1532 http->out.offset,
1533 http->out.offset,
cc27d775 1534 CLIENT_SOCK_SZ,
1535 memAllocate(MEM_CLIENT_SOCK_BUF),
1a92a1e2 1536 clientSendMoreData,
1537 http);
1538 }
1539 } else {
c07aed63 1540 debug(33, 5) ("clientKeepaliveNextRequest: FD %d reading next request\n",
1a92a1e2 1541 conn->fd);
1542 fd_note(conn->fd, "Reading next request");
1543 /*
1544 * Set the timeout BEFORE calling clientReadRequest().
1545 */
1546 commSetTimeout(conn->fd, 15, requestTimeout, conn);
1547 clientReadRequest(conn->fd, conn); /* Read next request */
1548 /*
1549 * Note, the FD may be closed at this point.
1550 */
1551 }
1552}
1553
fc5d6f7f 1554static void
79a15e0a 1555clientWriteComplete(int fd, char *bufnotused, size_t size, int errflag, void *data)
7a2f978b 1556{
1557 clientHttpRequest *http = data;
7a2f978b 1558 StoreEntry *entry = http->entry;
b34ed725 1559 int done;
7a2f978b 1560 http->out.size += size;
93f9abd0 1561 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
007b8be4 1562 fd, size, errflag, (int) http->out.offset, entry ? objectLen(entry) : 0);
d8b83480 1563 if (size > 0) {
ea285003 1564 kb_incr(&Counter.client_http.kbytes_out, size);
1565 if (isTcpHit(http->log_type))
d8b83480 1566 kb_incr(&Counter.client_http.hit_kbytes_out, size);
1567 }
7a2f978b 1568 if (errflag) {
e55650e3 1569 /*
1570 * just close the socket, httpRequestFree will abort if needed
1571 */
7a2f978b 1572 comm_close(fd);
fdfc0d63 1573 } else if (NULL == entry) {
1574 comm_close(fd); /* yuk */
7a2f978b 1575 } else if (entry->store_status == STORE_ABORTED) {
7a2f978b 1576 comm_close(fd);
a200bbd2 1577 } else if ((done = clientCheckTransferDone(http)) != 0 || size == 0) {
93f9abd0 1578 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd);
7a2f978b 1579 /* We're finished case */
c07aed63 1580 if (http->entry->mem_obj->reply->content_length < 0) {
ebb6e9a0 1581 debug(33, 5) ("clientWriteComplete: closing, content_length < 0\n");
c07aed63 1582 comm_close(fd);
1583 } else if (!done) {
ebb6e9a0 1584 debug(33, 5) ("clientWriteComplete: closing, !done\n");
c07aed63 1585 comm_close(fd);
d46a87a8 1586 } else if (EBIT_TEST(entry->flags, ENTRY_BAD_LENGTH)) {
ebb6e9a0 1587 debug(33, 5) ("clientWriteComplete: closing, ENTRY_BAD_LENGTH\n");
7a2f978b 1588 comm_close(fd);
92695e5e 1589 } else if (http->request->flags.proxy_keepalive) {
93f9abd0 1590 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd);
1a92a1e2 1591 clientKeepaliveNextRequest(http);
7a2f978b 1592 } else {
1593 comm_close(fd);
1594 }
1595 } else {
1596 /* More data will be coming from primary server; register with
1597 * storage manager. */
0e473d70 1598 if (entry->store_status == STORE_ABORTED)
6cf028ab 1599 debug(33, 0) ("clientWriteComplete 2: entry->swap_status == STORE_ABORTED\n");
7a2f978b 1600 storeClientCopy(entry,
1601 http->out.offset,
1602 http->out.offset,
cc27d775 1603 CLIENT_SOCK_SZ,
1604 memAllocate(MEM_CLIENT_SOCK_BUF),
7a2f978b 1605 clientSendMoreData,
1606 http);
1607 }
1608}
1609
038eb4ed 1610/*
99edd1c3 1611 * client issued a request with an only-if-cached cache-control directive;
038eb4ed 1612 * we did not find a cached object that can be returned without
99edd1c3 1613 * contacting other servers;
038eb4ed 1614 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
1615 */
1616static void
1617clientProcessOnlyIfCachedMiss(clientHttpRequest * http)
1618{
1619 char *url = http->uri;
1620 request_t *r = http->request;
1621 ErrorState *err = NULL;
1622 debug(33, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
1623 RequestMethodStr[r->method], url);
038eb4ed 1624 http->al.http.code = HTTP_GATEWAY_TIMEOUT;
1625 err = errorCon(ERR_ONLY_IF_CACHED_MISS, HTTP_GATEWAY_TIMEOUT);
1626 err->request = requestLink(r);
1627 err->src_addr = http->conn->peer.sin_addr;
92695e5e 1628 http->entry = clientCreateStoreEntry(http, r->method, null_request_flags);
038eb4ed 1629 errorAppendEntry(http->entry, err);
1630}
1631
50ddd7a4 1632static log_type
1633clientProcessRequest2(clientHttpRequest * http)
1634{
1635 const request_t *r = http->request;
50ddd7a4 1636 StoreEntry *e;
2f9aa365 1637 e = http->entry = storeGetPublic(http->uri, r->method);
0c7ccf77 1638 if (r->method == METHOD_HEAD && e == NULL) {
1639 /* We can generate a HEAD reply from a cached GET object */
2f9aa365 1640 e = http->entry = storeGetPublic(http->uri, METHOD_GET);
0c7ccf77 1641 }
6cfa8966 1642#if USE_CACHE_DIGESTS
57b1ff70 1643 http->lookup_type = e ? "HIT" : "MISS";
1ca847dd 1644#endif
49d3fcb0 1645 if (NULL == e) {
50ddd7a4 1646 /* this object isn't in the cache */
ac18f51a 1647 debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n");
50ddd7a4 1648 return LOG_TCP_MISS;
d87ebd78 1649 }
1650 if (Config.onoff.offline) {
ac18f51a 1651 debug(33, 3) ("clientProcessRequest2: offline HIT\n");
b540e168 1652 http->entry = e;
1653 return LOG_TCP_HIT;
d87ebd78 1654 }
1655 if (!storeEntryValidToSend(e)) {
ac18f51a 1656 debug(33, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
50ddd7a4 1657 http->entry = NULL;
1658 return LOG_TCP_MISS;
41587298 1659 }
1660 if (EBIT_TEST(e->flags, ENTRY_SPECIAL)) {
abe471ef 1661 /* Special entries are always hits, no matter what the client says */
ac18f51a 1662 debug(33, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
abe471ef 1663 http->entry = e;
1664 return LOG_TCP_HIT;
41587298 1665 }
9f60cfdf 1666#if HTTP_VIOLATIONS
41587298 1667 if (r->flags.nocache_hack) {
ac18f51a 1668 /* if nocache_hack is set, nocache should always be clear, right? */
1669 assert(!r->flags.nocache);
50ddd7a4 1670 ipcacheReleaseInvalid(r->host);
41587298 1671 /* continue! */
1672 }
9f60cfdf 1673#endif
41587298 1674 if (r->flags.nocache) {
ac18f51a 1675 debug(33, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
50ddd7a4 1676 http->entry = NULL;
cbe3a719 1677 ipcacheReleaseInvalid(r->host);
ee1679df 1678 return LOG_TCP_CLIENT_REFRESH_MISS;
7ddc902f 1679 }
1680 if (r->range && httpHdrRangeWillBeComplex(r->range)) {
1681 /*
1682 * Some clients break if we return "200 OK" for a Range
1683 * request. We would have to return "200 OK" for a _complex_
1684 * Range request that is also a HIT. Thus, let's prevent HITs
1685 * on complex Range requests
1686 */
ac18f51a 1687 debug(33, 3) ("clientProcessRequest2: complex range MISS\n");
5d679edb 1688 http->entry = NULL;
1689 return LOG_TCP_MISS;
50ddd7a4 1690 }
ac18f51a 1691 debug(33, 3) ("clientProcessRequest2: default HIT\n");
41587298 1692 http->entry = e;
1693 return LOG_TCP_HIT;
50ddd7a4 1694}
1695
4c323c5f 1696static void
fb63215a 1697clientProcessRequest(clientHttpRequest * http)
7a2f978b 1698{
23d92c64 1699 char *url = http->uri;
50ddd7a4 1700 request_t *r = http->request;
fb63215a 1701 int fd = http->conn->fd;
1bea1d83 1702 HttpReply *rep;
93f9abd0 1703 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
50ddd7a4 1704 RequestMethodStr[r->method],
7a2f978b 1705 url);
fb63215a 1706 if (r->method == METHOD_CONNECT) {
7a2f978b 1707 http->log_type = LOG_TCP_MISS;
ea0efb98 1708 sslStart(fd, url, r, &http->out.size);
1709 return;
fb63215a 1710 } else if (r->method == METHOD_PURGE) {
ea0efb98 1711 clientPurgeRequest(http);
1712 return;
fb63215a 1713 } else if (r->method == METHOD_TRACE) {
50ddd7a4 1714 if (r->max_forwards == 0) {
92695e5e 1715 http->entry = clientCreateStoreEntry(http, r->method, null_request_flags);
b17b22eb 1716 storeReleaseRequest(http->entry);
1717 storeBuffer(http->entry);
1bea1d83 1718 rep = httpReplyCreate();
31be8b80 1719 httpReplySetHeaders(rep, 1.0, HTTP_OK, NULL, "text/plain",
2246b732 1720 httpRequestPrefixLen(r), 0, squid_curtime);
1bea1d83 1721 httpReplySwapOut(rep, http->entry);
1722 httpReplyDestroy(rep);
2246b732 1723 httpRequestSwapOut(r, http->entry);
b17b22eb 1724 storeComplete(http->entry);
7a2f978b 1725 return;
1726 }
1727 /* yes, continue */
6addbd81 1728 http->log_type = LOG_TCP_MISS;
efb9218c 1729 } else if (pumpMethod(r->method)) {
fb63215a 1730 http->log_type = LOG_TCP_MISS;
31be8b80 1731 /* XXX oof, POST can be cached! */
54220df8 1732 pumpInit(fd, r, http->uri);
31be8b80 1733 } else {
efb9218c 1734 http->log_type = clientProcessRequest2(http);
7a2f978b 1735 }
93f9abd0 1736 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
7a2f978b 1737 log_tags[http->log_type],
23d92c64 1738 http->uri);
7a2f978b 1739 http->out.offset = 0;
49d3fcb0 1740 if (NULL != http->entry) {
1741 storeLockObject(http->entry);
1742 storeCreateMemObject(http->entry, http->uri, http->log_uri);
1743 storeClientListAdd(http->entry, http);
447e176b 1744#if DELAY_POOLS
1745 delaySetStoreClient(http->entry, http, http->request->delay_id);
1746#endif
49d3fcb0 1747 http->entry->refcount++;
1748 storeClientCopy(http->entry,
7a2f978b 1749 http->out.offset,
1750 http->out.offset,
cc27d775 1751 CLIENT_SOCK_SZ,
1752 memAllocate(MEM_CLIENT_SOCK_BUF),
7a2f978b 1753 clientCacheHit,
1754 http);
49d3fcb0 1755 } else {
1756 /* MISS CASE */
1757 http->log_type = LOG_TCP_MISS;
fb63215a 1758 clientProcessMiss(http);
7a2f978b 1759 }
1760}
1761
1762/*
1763 * Prepare to fetch the object as it's a cache miss of some kind.
1764 */
1765static void
fb63215a 1766clientProcessMiss(clientHttpRequest * http)
7a2f978b 1767{
23d92c64 1768 char *url = http->uri;
f44af445 1769 request_t *r = http->request;
7a2f978b 1770 ErrorState *err = NULL;
93f9abd0 1771 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
f44af445 1772 RequestMethodStr[r->method], url);
79e0dc20 1773 /*
1774 * We might have a left-over StoreEntry from a failed cache hit
1775 * or IMS request.
1776 */
1777 if (http->entry) {
d46a87a8 1778 if (EBIT_TEST(http->entry->flags, ENTRY_SPECIAL))
12784378 1779 debug(33, 0) ("clientProcessMiss: miss on a special object (%s).\n", url);
79e0dc20 1780 storeUnregister(http->entry, http);
1781 storeUnlockObject(http->entry);
1782 http->entry = NULL;
1783 }
7c1d4010 1784 if (clientOnlyIfCached(http)) {
1785 clientProcessOnlyIfCachedMiss(http);
1786 return;
1787 }
9330543e 1788 /*
1789 * Deny loops when running in accelerator/transproxy mode.
1790 */
92695e5e 1791 if (http->flags.accel && r->flags.loopdetect) {
9330543e 1792 http->al.http.code = HTTP_FORBIDDEN;
1793 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
1794 err->request = requestLink(r);
1795 err->src_addr = http->conn->peer.sin_addr;
92695e5e 1796 http->entry = clientCreateStoreEntry(http, r->method, null_request_flags);
9330543e 1797 errorAppendEntry(http->entry, err);
1798 return;
1799 }
79e0dc20 1800 assert(http->out.offset == 0);
f44af445 1801 http->entry = clientCreateStoreEntry(http, r->method, r->flags);
79e0dc20 1802 http->entry->refcount++;
1da5651f 1803 if (http->flags.internal)
1804 r->protocol = PROTO_INTERNAL;
d1061d2b 1805 fwdStart(http->conn->fd, http->entry, r, http->conn->peer.sin_addr);
7a2f978b 1806}
1807
99edd1c3 1808static clientHttpRequest *
1809parseHttpRequestAbort(ConnStateData * conn, const char *uri)
1810{
1811 clientHttpRequest *http = xcalloc(1, sizeof(clientHttpRequest));
1812 cbdataAdd(http, MEM_NONE);
1813 http->conn = conn;
1814 http->start = current_time;
1815 http->req_sz = conn->in.offset;
1816 http->uri = xstrdup(uri);
1817 http->log_uri = xstrdup(uri);
d192d11f 1818 http->range_iter.boundary = StringNull;
99edd1c3 1819 return http;
1820}
1821
7a2f978b 1822/*
1823 * parseHttpRequest()
1824 *
1825 * Returns
1826 * NULL on error or incomplete request
1827 * a clientHttpRequest structure on success
1828 */
1829static clientHttpRequest *
1830parseHttpRequest(ConnStateData * conn, method_t * method_p, int *status,
ea285003 1831 char **prefix_p, size_t * req_line_sz_p)
7a2f978b 1832{
1833 char *inbuf = NULL;
1834 char *mstr = NULL;
1835 char *url = NULL;
1836 char *req_hdr = NULL;
7a2f978b 1837 float http_ver;
1838 char *token = NULL;
1839 char *t = NULL;
2334c194 1840 char *end;
7a2f978b 1841 int free_request = 0;
1842 size_t header_sz; /* size of headers, not including first line */
ea285003 1843 size_t prefix_sz; /* size of whole request (req-line + headers) */
7a2f978b 1844 size_t url_sz;
1845 method_t method;
1846 clientHttpRequest *http = NULL;
5cafc1d6 1847#if IPF_TRANSPARENT
1848 struct natlookup natLookup;
1849 static int natfd = -1;
1850#endif
7a2f978b 1851
1852 /* Make sure a complete line has been received */
99edd1c3 1853 if ((t = strchr(conn->in.buf, '\n')) == NULL) {
93f9abd0 1854 debug(33, 5) ("Incomplete request line, waiting for more data\n");
7a2f978b 1855 *status = 0;
08e70b8f 1856 *prefix_p = NULL;
1857 *method_p = METHOD_NONE;
7a2f978b 1858 return NULL;
1859 }
99edd1c3 1860 *req_line_sz_p = t - conn->in.buf;
7a2f978b 1861 /* Use xmalloc/xmemcpy instead of xstrdup because inbuf might
1862 * contain NULL bytes; especially for POST data */
1863 inbuf = xmalloc(conn->in.offset + 1);
1864 xmemcpy(inbuf, conn->in.buf, conn->in.offset);
1865 *(inbuf + conn->in.offset) = '\0';
1866
99edd1c3 1867 /* pre-set these values to make aborting simpler */
1868 *prefix_p = inbuf;
1869 *method_p = METHOD_NONE;
1870 *status = -1;
1871
7a2f978b 1872 /* Look for request method */
1873 if ((mstr = strtok(inbuf, "\t ")) == NULL) {
93f9abd0 1874 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
99edd1c3 1875 return parseHttpRequestAbort(conn, "error:invalid-request-method");
7a2f978b 1876 }
1877 method = urlParseMethod(mstr);
1878 if (method == METHOD_NONE) {
93f9abd0 1879 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr);
99edd1c3 1880 return parseHttpRequestAbort(conn, "error:unsupported-request-method");
7a2f978b 1881 }
93f9abd0 1882 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr);
3775d53c 1883 *method_p = method;
7a2f978b 1884
d548ee64 1885 /* look for URL+HTTP/x.x */
1886 if ((url = strtok(NULL, "\r\n")) == NULL) {
93f9abd0 1887 debug(33, 1) ("parseHttpRequest: Missing URL\n");
99edd1c3 1888 return parseHttpRequestAbort(conn, "error:missing-url");
7a2f978b 1889 }
d548ee64 1890 t = url + strlen(url);
1891 assert(*t == '\0');
1892 token = NULL;
1893 while (t > url) {
1894 t--;
1895 if (isspace(*t) && !strncmp(t + 1, "HTTP/", 5)) {
1896 token = t + 1;
1897 break;
1898 }
1899 }
1900 while (t > url && isspace(*t))
1901 *(t--) = '\0';
1902 debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url);
1903 if (token == NULL) {
93f9abd0 1904 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
46052490 1905#if RELAXED_HTTP_PARSER
1906 http_ver = (float) 0.9; /* wild guess */
1907#else
99edd1c3 1908 return parseHttpRequestAbort(conn, "error:missing-http-ident");
7518fce5 1909#endif
99edd1c3 1910 } else {
7518fce5 1911 http_ver = (float) atof(token + 5);
99edd1c3 1912 }
7a2f978b 1913
1914 /* Check if headers are received */
754b9ce9 1915 req_hdr = t;
8d9810cd 1916 header_sz = headersEnd(req_hdr, conn->in.offset - (req_hdr - inbuf));
2334c194 1917 if (0 == header_sz) {
1a92a1e2 1918 debug(33, 3) ("parseHttpRequest: header_sz == 0\n");
7a2f978b 1919 *status = 0;
1920 return NULL;
1921 }
754b9ce9 1922 /*
99edd1c3 1923 * Skip whitespace at the end of the first line, up to the
754b9ce9 1924 * first newline.
1925 */
1926 while (isspace(*req_hdr)) {
1927 header_sz--;
1928 if (*(req_hdr++) == '\n')
1929 break;
1930 }
1931 assert(header_sz > 0);
1932 debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr);
2334c194 1933 end = req_hdr + header_sz;
04990e2d 1934 debug(33, 3) ("parseHttpRequest: end = {%s}\n", end);
99edd1c3 1935
99edd1c3 1936 prefix_sz = end - inbuf;
1937 *req_line_sz_p = req_hdr - inbuf;
ea285003 1938 debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n",
99edd1c3 1939 (int) prefix_sz, (int) *req_line_sz_p);
1940 assert(prefix_sz <= conn->in.offset);
7a2f978b 1941
1942 /* Ok, all headers are received */
1943 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1944 cbdataAdd(http, MEM_NONE);
7a2f978b 1945 http->http_ver = http_ver;
1946 http->conn = conn;
1947 http->start = current_time;
99edd1c3 1948 http->req_sz = prefix_sz;
d192d11f 1949 http->range_iter.boundary = StringNull;
99edd1c3 1950 *prefix_p = xmalloc(prefix_sz + 1);
1951 xmemcpy(*prefix_p, conn->in.buf, prefix_sz);
1952 *(*prefix_p + prefix_sz) = '\0';
7a2f978b 1953
ea285003 1954 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", (*prefix_p) + *req_line_sz_p);
23d92c64 1955 /* Assign http->uri */
7a2f978b 1956 if ((t = strchr(url, '\n'))) /* remove NL */
1957 *t = '\0';
1958 if ((t = strchr(url, '\r'))) /* remove CR */
1959 *t = '\0';
1960 if ((t = strchr(url, '#'))) /* remove HTML anchors */
1961 *t = '\0';
1962
4162ee3b 1963 /* handle internal objects */
1da5651f 1964 if (internalCheck(url)) {
4162ee3b 1965 /* prepend our name & port */
1da5651f 1966 http->uri = xstrdup(internalLocalUri(NULL, url));
77ed547a 1967 http->flags.internal = 1;
4162ee3b 1968 }
7a2f978b 1969 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
4162ee3b 1970 else if (Config2.Accel.on && *url == '/') {
7a2f978b 1971 /* prepend the accel prefix */
dbfed404 1972 if (opt_accel_uses_host && (t = mime_get_header(req_hdr, "Host"))) {
7a2f978b 1973 /* If a Host: header was specified, use it to build the URL
1974 * instead of the one in the Config file. */
1975 /*
1976 * XXX Use of the Host: header here opens a potential
1977 * security hole. There are no checks that the Host: value
1978 * corresponds to one of your servers. It might, for example,
1979 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
1980 * types should be used to prevent httpd-accelerators
1981 * handling requests for non-local servers */
1982 strtok(t, " :/;@");
12fc602c 1983 url_sz = strlen(url) + 32 + Config.appendDomainLen +
1984 strlen(t);
23d92c64 1985 http->uri = xcalloc(url_sz, 1);
1986 snprintf(http->uri, url_sz, "http://%s:%d%s",
7a2f978b 1987 t, (int) Config.Accel.port, url);
dbfed404 1988 } else if (vhost_mode) {
1989 /* Put the local socket IP address as the hostname */
1990 url_sz = strlen(url) + 32 + Config.appendDomainLen;
1991 http->uri = xcalloc(url_sz, 1);
5cafc1d6 1992#if IPF_TRANSPARENT
135171fe 1993 natLookup.nl_inport = http->conn->me.sin_port;
1994 natLookup.nl_outport = http->conn->peer.sin_port;
1995 natLookup.nl_inip = http->conn->me.sin_addr;
1996 natLookup.nl_outip = http->conn->peer.sin_addr;
1997 natLookup.nl_flags = IPN_TCP;
1998 if (natfd < 0)
1999 natfd = open(IPL_NAT, O_RDONLY, 0);
2000 if (natfd < 0) {
2001 debug(50, 1) ("parseHttpRequest: NAT open failed: %s\n",
2002 xstrerror());
ac18f51a 2003 return parseHttpRequestAbort(conn, "error:nat-open-failed");
5cafc1d6 2004 }
135171fe 2005 if (ioctl(natfd, SIOCGNATL, &natLookup) < 0) {
ac18f51a 2006 if (errno != ESRCH) {
2007 debug(50, 1) ("parseHttpRequest: NAT lookup failed: ioctl(SIOCGNATL)\n");
2008 close(natfd);
2009 natfd = -1;
2010 return parseHttpRequestAbort(conn, "error:nat-lookup-failed");
2011 } else
2012 snprintf(http->uri, url_sz, "http://%s:%d%s",
2013 inet_ntoa(http->conn->me.sin_addr),
2014 (int) Config.Accel.port,
2015 url);
2016 } else
2017 snprintf(http->uri, url_sz, "http://%s:%d%s",
2018 inet_ntoa(natLookup.nl_realip),
2019 (int) Config.Accel.port,
2020 url);
5cafc1d6 2021#else
dbfed404 2022 snprintf(http->uri, url_sz, "http://%s:%d%s",
2023 inet_ntoa(http->conn->me.sin_addr),
2024 (int) Config.Accel.port,
2025 url);
5cafc1d6 2026#endif
93f9abd0 2027 debug(33, 5) ("VHOST REWRITE: '%s'\n", http->uri);
7a2f978b 2028 } else {
2029 url_sz = strlen(Config2.Accel.prefix) + strlen(url) +
2030 Config.appendDomainLen + 1;
23d92c64 2031 http->uri = xcalloc(url_sz, 1);
2032 snprintf(http->uri, url_sz, "%s%s", Config2.Accel.prefix, url);
7a2f978b 2033 }
77ed547a 2034 http->flags.accel = 1;
7a2f978b 2035 } else {
2036 /* URL may be rewritten later, so make extra room */
2037 url_sz = strlen(url) + Config.appendDomainLen + 5;
23d92c64 2038 http->uri = xcalloc(url_sz, 1);
2039 strcpy(http->uri, url);
77ed547a 2040 http->flags.accel = 0;
7a2f978b 2041 }
d548ee64 2042 if (!stringHasWhitespace(http->uri))
2043 http->log_uri = xstrdup(http->uri);
2044 else
2045 http->log_uri = xstrdup(rfc1738_escape(http->uri));
93f9abd0 2046 debug(33, 5) ("parseHttpRequest: Complete request received\n");
7a2f978b 2047 if (free_request)
2048 safe_free(url);
2049 xfree(inbuf);
7a2f978b 2050 *status = 1;
2051 return http;
2052}
2053
2054static int
79d39a72 2055clientReadDefer(int fdnotused, void *data)
7a2f978b 2056{
2057 ConnStateData *conn = data;
2058 return conn->defer.until > squid_curtime;
2059}
2060
2061static void
2062clientReadRequest(int fd, void *data)
2063{
2064 ConnStateData *conn = data;
2065 int parser_return_code = 0;
2066 int k;
2067 request_t *request = NULL;
7a2f978b 2068 int size;
7a2f978b 2069 method_t method;
2070 clientHttpRequest *http = NULL;
2071 clientHttpRequest **H = NULL;
08e70b8f 2072 char *prefix = NULL;
7a2f978b 2073 ErrorState *err = NULL;
2074 fde *F = &fd_table[fd];
44db45e8 2075 int len = conn->in.size - conn->in.offset - 1;
93f9abd0 2076 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd);
886f2785 2077 Counter.syscalls.sock.reads++;
7a2f978b 2078 size = read(fd, conn->in.buf + conn->in.offset, len);
d8b83480 2079 if (size > 0) {
ea285003 2080 fd_bytes(fd, size, FD_READ);
2081 kb_incr(&Counter.client_http.kbytes_in, size);
d8b83480 2082 }
44db45e8 2083 /*
2084 * Don't reset the timeout value here. The timeout value will be
2085 * set to Config.Timeout.request by httpAccept() and
2086 * clientWriteComplete(), and should apply to the request as a
2087 * whole, not individual read() calls. Plus, it breaks our
2088 * lame half-close detection
2089 */
2090 commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, conn, 0);
7a2f978b 2091 if (size == 0) {
2092 if (conn->chr == NULL) {
2093 /* no current or pending requests */
2094 comm_close(fd);
2095 return;
ea285003 2096 } else if (!Config.onoff.half_closed_clients) {
2097 /* admin doesn't want to support half-closed client sockets */
2098 comm_close(fd);
2099 return;
7a2f978b 2100 }
2101 /* It might be half-closed, we can't tell */
93f9abd0 2102 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd);
58a6c186 2103 F->flags.socket_eof = 1;
7a2f978b 2104 conn->defer.until = squid_curtime + 1;
2105 conn->defer.n++;
44db45e8 2106 fd_note(fd, "half-closed");
7a2f978b 2107 return;
2108 } else if (size < 0) {
6d3caf23 2109 if (!ignoreErrno(errno)) {
7a2f978b 2110 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd, xstrerror());
2111 comm_close(fd);
ebf4efff 2112 return;
47130615 2113 } else if (conn->in.offset == 0) {
7c1d4010 2114 debug(50, 2) ("clientReadRequest: FD %d: no data to process (%s)\n", fd, xstrerror());
ebf4efff 2115 return;
7a2f978b 2116 }
ebf4efff 2117 /* Continue to process previously read data */
47130615 2118 size = 0;
7a2f978b 2119 }
2120 conn->in.offset += size;
5ede6c8f 2121 /* Skip leading (and trailing) whitespace */
7a2f978b 2122 while (conn->in.offset > 0) {
ebf4efff 2123 int nrequests;
cff0c749 2124 size_t req_line_sz;
9a6f98a5 2125 while (conn->in.offset > 0 && isspace(conn->in.buf[0])) {
2126 xmemmove(conn->in.buf, conn->in.buf + 1, conn->in.offset - 1);
2127 conn->in.offset--;
2128 }
2129 conn->in.buf[conn->in.offset] = '\0'; /* Terminate the string */
2130 if (conn->in.offset == 0)
2131 break;
ebf4efff 2132 /* Limit the number of concurrent requests to 2 */
2133 for (H = &conn->chr, nrequests = 0; *H; H = &(*H)->next, nrequests++);
2134 if (nrequests >= 2) {
93f9abd0 2135 debug(33, 2) ("clientReadRequest: FD %d max concurrent requests reached\n", fd);
2136 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd);
47130615 2137 conn->defer.until = squid_curtime + 100; /* Reset when a request is complete */
ebf4efff 2138 break;
2139 }
2140 /* Process request */
7a2f978b 2141 http = parseHttpRequest(conn,
2142 &method,
2143 &parser_return_code,
99edd1c3 2144 &prefix,
2145 &req_line_sz);
13f11a4a 2146 if (!http)
2147 safe_free(prefix);
7a2f978b 2148 if (http) {
2149 assert(http->req_sz > 0);
2150 conn->in.offset -= http->req_sz;
2151 assert(conn->in.offset >= 0);
ebb6e9a0 2152 debug(33, 5) ("conn->in.offset = %d\n", (int) conn->in.offset);
6fa92aa2 2153 /*
2154 * If we read past the end of this request, move the remaining
2155 * data to the beginning
2156 */
2157 if (conn->in.offset > 0)
dbfed404 2158 xmemmove(conn->in.buf, conn->in.buf + http->req_sz, conn->in.offset);
ebf4efff 2159 /* add to the client request queue */
7a2f978b 2160 for (H = &conn->chr; *H; H = &(*H)->next);
2161 *H = http;
2162 conn->nrequests++;
2163 commSetTimeout(fd, Config.Timeout.lifetime, NULL, NULL);
3775d53c 2164 if (parser_return_code < 0) {
93f9abd0 2165 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd);
3775d53c 2166 err = errorCon(ERR_INVALID_REQ, HTTP_BAD_REQUEST);
2167 err->request_hdrs = xstrdup(conn->in.buf);
92695e5e 2168 http->entry = clientCreateStoreEntry(http, method, null_request_flags);
3775d53c 2169 errorAppendEntry(http->entry, err);
13f11a4a 2170 safe_free(prefix);
3775d53c 2171 break;
2172 }
23d92c64 2173 if ((request = urlParse(method, http->uri)) == NULL) {
93f9abd0 2174 debug(33, 5) ("Invalid URL: %s\n", http->uri);
7a2f978b 2175 err = errorCon(ERR_INVALID_URL, HTTP_BAD_REQUEST);
2176 err->src_addr = conn->peer.sin_addr;
23d92c64 2177 err->url = xstrdup(http->uri);
7a2f978b 2178 http->al.http.code = err->http_status;
92695e5e 2179 http->entry = clientCreateStoreEntry(http, method, null_request_flags);
79e0dc20 2180 errorAppendEntry(http->entry, err);
99edd1c3 2181 safe_free(prefix);
7a2f978b 2182 break;
99edd1c3 2183 } else {
2184 /* compile headers */
2185 /* we should skip request line! */
ea285003 2186 if (!httpRequestParseHeader(request, prefix + req_line_sz))
99edd1c3 2187 debug(33, 1) ("Failed to parse request headers: %s\n%s\n",
2188 http->uri, prefix);
2189 /* continue anyway? */
7a2f978b 2190 }
ba26bca4 2191 if (!http->flags.internal) {
2192 if (internalCheck(strBuf(request->urlpath))) {
2193 if (0 == strcasecmp(request->host, getMyHostname())) {
4b4cd312 2194 if (request->port == Config.Port.http->i)
2195 http->flags.internal = 1;
ba26bca4 2196 } else if (internalStaticCheck(strBuf(request->urlpath))) {
5999b776 2197 xstrncpy(request->host, getMyHostname(), SQUIDHOSTNAMELEN);
2198 request->port = Config.Port.http->i;
2199 http->flags.internal = 1;
ba26bca4 2200 }
2201 }
2202 }
13f11a4a 2203 safe_free(prefix);
23d92c64 2204 safe_free(http->log_uri);
2205 http->log_uri = xstrdup(urlCanonicalClean(request));
7a2f978b 2206 request->client_addr = conn->peer.sin_addr;
2207 request->http_ver = http->http_ver;
7a2f978b 2208 if (!urlCheckRequest(request)) {
2209 err = errorCon(ERR_UNSUP_REQ, HTTP_NOT_IMPLEMENTED);
2210 err->src_addr = conn->peer.sin_addr;
7a2f978b 2211 err->request = requestLink(request);
2212 http->al.http.code = err->http_status;
92695e5e 2213 http->entry = clientCreateStoreEntry(http, request->method, null_request_flags);
79e0dc20 2214 errorAppendEntry(http->entry, err);
3775d53c 2215 break;
7a2f978b 2216 }
31be8b80 2217 if (0 == clientCheckContentLength(request)) {
2218 err = errorCon(ERR_INVALID_REQ, HTTP_LENGTH_REQUIRED);
2219 err->src_addr = conn->peer.sin_addr;
2220 err->request = requestLink(request);
2221 http->al.http.code = err->http_status;
92695e5e 2222 http->entry = clientCreateStoreEntry(http, request->method, null_request_flags);
31be8b80 2223 errorAppendEntry(http->entry, err);
2224 break;
2225 }
7a2f978b 2226 http->request = requestLink(request);
3775d53c 2227 /*
2228 * break here for NON-GET because most likely there is a
7a2f978b 2229 * reqeust body following and we don't want to parse it
3775d53c 2230 * as though it was new request
2231 */
7a2f978b 2232 if (request->method != METHOD_GET) {
ebb6e9a0 2233 int cont_len = httpHeaderGetInt(&request->header, HDR_CONTENT_LENGTH);
2234 int copy_len = XMIN(cont_len, conn->in.offset);
e34195c0 2235 if (copy_len > 0) {
ebb6e9a0 2236 assert(conn->in.offset >= copy_len);
2237 request->body_sz = copy_len;
7a2f978b 2238 request->body = xmalloc(request->body_sz);
2239 xmemcpy(request->body, conn->in.buf, request->body_sz);
ebb6e9a0 2240 conn->in.offset -= copy_len;
2241 if (conn->in.offset)
2242 xmemmove(conn->in.buf, conn->in.buf + copy_len, conn->in.offset);
7a2f978b 2243 }
f7237ed2 2244 /*
2aecf121 2245 * if we didn't get the full body now, then more will
2246 * be arriving on the client socket. Lets cancel
2247 * the read handler until this request gets forwarded.
f7237ed2 2248 */
2aecf121 2249 if (request->body_sz < cont_len)
2250 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
7a2f978b 2251 }
2aecf121 2252 clientAccessCheck(http);
7a2f978b 2253 continue; /* while offset > 0 */
2254 } else if (parser_return_code == 0) {
2255 /*
2256 * Partial request received; reschedule until parseHttpRequest()
2257 * is happy with the input
2258 */
2259 k = conn->in.size - 1 - conn->in.offset;
2260 if (k == 0) {
2261 if (conn->in.offset >= Config.maxRequestSize) {
2262 /* The request is too large to handle */
93f9abd0 2263 debug(33, 0) ("Request won't fit in buffer.\n");
2264 debug(33, 0) ("Config 'request_size'= %d bytes.\n",
7a2f978b 2265 Config.maxRequestSize);
93f9abd0 2266 debug(33, 0) ("This request = %d bytes.\n",
5f6ac48b 2267 (int) conn->in.offset);
7a2f978b 2268 err = errorCon(ERR_INVALID_REQ, HTTP_REQUEST_ENTITY_TOO_LARGE);
92695e5e 2269 http->entry = clientCreateStoreEntry(http, request->method, null_request_flags);
79e0dc20 2270 errorAppendEntry(http->entry, err);
7a2f978b 2271 return;
2272 }
2273 /* Grow the request memory area to accomodate for a large request */
2274 conn->in.size += REQUEST_BUF_SIZE;
2275 conn->in.buf = xrealloc(conn->in.buf, conn->in.size);
59c4d35b 2276 /* XXX account conn->in.buf */
93f9abd0 2277 debug(33, 2) ("Handling a large request, offset=%d inbufsize=%d\n",
5f6ac48b 2278 (int) conn->in.offset, conn->in.size);
7a2f978b 2279 k = conn->in.size - 1 - conn->in.offset;
2280 }
7a2f978b 2281 break;
7a2f978b 2282 }
2283 }
2284}
2285
2286/* general lifetime handler for HTTP requests */
2287static void
2288requestTimeout(int fd, void *data)
2289{
2290 ConnStateData *conn = data;
2291 ErrorState *err;
93f9abd0 2292 debug(33, 2) ("requestTimeout: FD %d: lifetime is expired.\n", fd);
7a2f978b 2293 if (fd_table[fd].rwstate) {
79e0dc20 2294 /*
2295 * Some data has been sent to the client, just close the FD
2296 */
7a2f978b 2297 comm_close(fd);
2298 } else if (conn->nrequests) {
79e0dc20 2299 /*
2300 * assume its a persistent connection; just close it
2301 */
7a2f978b 2302 comm_close(fd);
2303 } else {
79e0dc20 2304 /*
2305 * Generate an error
2306 */
7a2f978b 2307 err = errorCon(ERR_LIFETIME_EXP, HTTP_REQUEST_TIMEOUT);
7a2f978b 2308 err->url = xstrdup("N/A");
79e0dc20 2309 /*
2310 * Normally we shouldn't call errorSend() in client_side.c, but
2311 * it should be okay in this case. Presumably if we get here
2312 * this is the first request for the connection, and no data
2313 * has been written yet
2314 */
2315 assert(conn->chr == NULL);
7a2f978b 2316 errorSend(fd, err);
79e0dc20 2317 /*
2318 * if we don't close() here, we still need a timeout handler!
2319 */
7a2f978b 2320 commSetTimeout(fd, 30, requestTimeout, conn);
2321 }
2322}
2323
ba4f8e5a 2324static int
2325httpAcceptDefer(void)
7a2f978b 2326{
3d6629c6 2327 static time_t last_warn = 0;
2328 if (fdNFree() >= RESERVED_FD)
2329 return 0;
2330 if (last_warn + 15 < squid_curtime) {
2331 debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n");
2332 last_warn = squid_curtime;
2333 }
2334 return 1;
7a2f978b 2335}
2336
bf8e5903 2337/* Handle a new connection on HTTP socket. */
7a2f978b 2338void
ba4f8e5a 2339httpAccept(int sock, void *data)
7a2f978b 2340{
ba4f8e5a 2341 int *N = data;
7a2f978b 2342 int fd = -1;
2343 ConnStateData *connState = NULL;
2344 struct sockaddr_in peer;
2345 struct sockaddr_in me;
309ad3b6 2346 int max = INCOMING_HTTP_MAX;
c411b98a 2347 commSetSelect(sock, COMM_SELECT_READ, httpAccept, NULL, 0);
ba4f8e5a 2348 while (max-- && !httpAcceptDefer()) {
e2525655 2349 memset(&peer, '\0', sizeof(struct sockaddr_in));
2350 memset(&me, '\0', sizeof(struct sockaddr_in));
e2525655 2351 if ((fd = comm_accept(sock, &peer, &me)) < 0) {
2352 if (!ignoreErrno(errno))
eeb423fb 2353 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
e2525655 2354 sock, xstrerror());
2355 break;
2356 }
2357 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd);
2358 connState = xcalloc(1, sizeof(ConnStateData));
2359 connState->peer = peer;
2360 connState->log_addr = peer.sin_addr;
2361 connState->log_addr.s_addr &= Config.Addrs.client_netmask.s_addr;
2362 connState->me = me;
2363 connState->fd = fd;
2364 connState->ident.fd = -1;
2365 connState->in.size = REQUEST_BUF_SIZE;
2366 connState->in.buf = xcalloc(connState->in.size, 1);
2367 cbdataAdd(connState, MEM_NONE);
2368 /* XXX account connState->in.buf */
2369 comm_add_close_handler(fd, connStateFree, connState);
2370 if (Config.onoff.log_fqdn)
2371 fqdncache_gethostbyaddr(peer.sin_addr, FQDN_LOOKUP_IF_MISS);
2372 commSetTimeout(fd, Config.Timeout.request, requestTimeout, connState);
2373 commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, connState, 0);
2374 commSetDefer(fd, clientReadDefer, connState);
ba4f8e5a 2375 (*N)++;
7a2f978b 2376 }
7a2f978b 2377}
2378
7a2f978b 2379/* return 1 if the request should be aborted */
2380static int
2381CheckQuickAbort2(const clientHttpRequest * http)
2382{
4efca7da 2383 int curlen;
2384 int minlen;
2385 int expectlen;
7a2f978b 2386
92695e5e 2387 if (!http->request->flags.cachable)
7a2f978b 2388 return 1;
d46a87a8 2389 if (EBIT_TEST(http->entry->flags, KEY_PRIVATE))
7a2f978b 2390 return 1;
2391 if (http->entry->mem_obj == NULL)
2392 return 1;
038eb4ed 2393 expectlen = http->entry->mem_obj->reply->content_length;
4efca7da 2394 curlen = (int) http->entry->mem_obj->inmem_hi;
59cb9808 2395 minlen = (int) Config.quickAbort.min << 10;
7a2f978b 2396 if (minlen < 0)
2397 /* disabled */
2398 return 0;
2399 if (curlen > expectlen)
2400 /* bad content length */
2401 return 1;
2402 if ((expectlen - curlen) < minlen)
2403 /* only little more left */
2404 return 0;
59cb9808 2405 if ((expectlen - curlen) > (Config.quickAbort.max << 10))
7a2f978b 2406 /* too much left to go */
2407 return 1;
37d0359b 2408 if (expectlen < 100)
4efca7da 2409 /* avoid FPE */
2410 return 0;
37d0359b 2411 if ((curlen / (expectlen / 100)) > Config.quickAbort.pct)
7a2f978b 2412 /* past point of no return */
2413 return 0;
2414 return 1;
2415}
2416
2417
2418static void
2419CheckQuickAbort(clientHttpRequest * http)
2420{
2421 StoreEntry *entry = http->entry;
2422 /* Note, set entry here because http->entry might get changed (for IMS
2423 * requests) during the storeAbort() call */
2424 if (entry == NULL)
2425 return;
36547bcf 2426 if (storePendingNClients(entry) > 0)
7a2f978b 2427 return;
2428 if (entry->store_status != STORE_PENDING)
2429 return;
2430 if (CheckQuickAbort2(http) == 0)
2431 return;
93f9abd0 2432 debug(33, 3) ("CheckQuickAbort: ABORTING %s\n", storeUrl(entry));
59cb9808 2433 Counter.aborted_requests++;
7a2f978b 2434 storeAbort(entry, 1);
2435}
2436
978e455f 2437#define SENDING_BODY 0
2438#define SENDING_HDRSONLY 1
7a2f978b 2439static int
1b02b5be 2440clientCheckTransferDone(clientHttpRequest * http)
7a2f978b 2441{
978e455f 2442 int sending = SENDING_BODY;
7a2f978b 2443 StoreEntry *entry = http->entry;
978e455f 2444 MemObject *mem;
2445 http_reply *reply;
2446 int sendlen;
7a2f978b 2447 if (entry == NULL)
2448 return 0;
9f086470 2449 /*
2450 * For now, 'done_copying' is used for special cases like
2451 * Range and HEAD requests.
2452 */
2453 if (http->flags.done_copying)
2454 return 1;
978e455f 2455 /*
2456 * Handle STORE_OK and STORE_ABORTED objects.
07304bf9 2457 * objectLen(entry) will be set proprely.
978e455f 2458 */
2459 if (entry->store_status != STORE_PENDING) {
07304bf9 2460 if (http->out.offset >= objectLen(entry))
7a2f978b 2461 return 1;
513f05a6 2462 else
2463 return 0;
2464 }
978e455f 2465 /*
2466 * Now, handle STORE_PENDING objects
2467 */
2468 mem = entry->mem_obj;
2469 assert(mem != NULL);
2470 assert(http->request != NULL);
2471 reply = mem->reply;
2472 if (reply->hdr_sz == 0)
b34ed725 2473 return 0; /* haven't found end of headers yet */
cb69b4c7 2474 else if (reply->sline.status == HTTP_OK)
b34ed725 2475 sending = SENDING_BODY;
cb69b4c7 2476 else if (reply->sline.status == HTTP_NO_CONTENT)
b34ed725 2477 sending = SENDING_HDRSONLY;
cb69b4c7 2478 else if (reply->sline.status == HTTP_NOT_MODIFIED)
b34ed725 2479 sending = SENDING_HDRSONLY;
cb69b4c7 2480 else if (reply->sline.status < HTTP_OK)
a3c60429 2481 sending = SENDING_HDRSONLY;
978e455f 2482 else if (http->request->method == METHOD_HEAD)
b34ed725 2483 sending = SENDING_HDRSONLY;
978e455f 2484 else
2485 sending = SENDING_BODY;
2486 /*
2487 * Figure out how much data we are supposed to send.
2488 * If we are sending a body and we don't have a content-length,
2489 * then we must wait for the object to become STORE_OK or
2490 * STORE_ABORTED.
2491 */
2492 if (sending == SENDING_HDRSONLY)
2493 sendlen = reply->hdr_sz;
038eb4ed 2494 else if (reply->content_length < 0)
978e455f 2495 return 0;
2496 else
038eb4ed 2497 sendlen = reply->content_length + reply->hdr_sz;
978e455f 2498 /*
2499 * Now that we have the expected length, did we send it all?
2500 */
2501 if (http->out.offset < sendlen)
2502 return 0;
2503 else
b34ed725 2504 return 1;
7a2f978b 2505}
2506
7a2f978b 2507/*
2508 * This function is designed to serve a fairly specific purpose.
2509 * Occasionally our vBNS-connected caches can talk to each other, but not
2510 * the rest of the world. Here we try to detect frequent failures which
2511 * make the cache unusable (e.g. DNS lookup and connect() failures). If
2512 * the failure:success ratio goes above 1.0 then we go into "hit only"
2513 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
2514 * will only fetch HITs from us if they are using the ICP protocol. We
2515 * stay in this mode for 5 minutes.
2516 *
2517 * Duane W., Sept 16, 1996
2518 */
2519
2520static void
88aad2e5 2521checkFailureRatio(err_type etype, hier_code hcode)
7a2f978b 2522{
88aad2e5 2523 static double magic_factor = 100.0;
7a2f978b 2524 double n_good;
2525 double n_bad;
2526 if (hcode == HIER_NONE)
2527 return;
88aad2e5 2528 n_good = magic_factor / (1.0 + request_failure_ratio);
7a2f978b 2529 n_bad = magic_factor - n_good;
88aad2e5 2530 switch (etype) {
7a2f978b 2531 case ERR_DNS_FAIL:
2532 case ERR_CONNECT_FAIL:
2533 case ERR_READ_ERROR:
2534 n_bad++;
2535 break;
2536 default:
2537 n_good++;
2538 }
88aad2e5 2539 request_failure_ratio = n_bad / n_good;
7a2f978b 2540 if (hit_only_mode_until > squid_curtime)
2541 return;
88aad2e5 2542 if (request_failure_ratio < 1.0)
7a2f978b 2543 return;
93f9abd0 2544 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio);
2545 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
7a2f978b 2546 FAILURE_MODE_TIME / 60);
2547 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
88aad2e5 2548 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
7a2f978b 2549}
15df8349 2550
2551void
2552clientHttpConnectionsOpen(void)
2553{
2554 ushortlist *u;
2555 int fd;
15df8349 2556 for (u = Config.Port.http; u; u = u->next) {
8daca701 2557 enter_suid();
2558 fd = comm_open(SOCK_STREAM,
2559 0,
2560 Config.Addrs.tcp_incoming,
2561 u->i,
2562 COMM_NONBLOCKING,
2563 "HTTP Socket");
2564 leave_suid();
2565 if (fd < 0)
2566 continue;
2567 comm_listen(fd);
2568 commSetSelect(fd, COMM_SELECT_READ, httpAccept, NULL, 0);
eeb423fb 2569 /*commSetDefer(fd, httpAcceptDefer, NULL); */
8daca701 2570 debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n",
2571 (int) u->i, fd);
2572 HttpSockets[NHttpSockets++] = fd;
15df8349 2573 }
2574 if (NHttpSockets < 1)
8daca701 2575 fatal("Cannot open HTTP Port");
15df8349 2576}
c0fbae16 2577
2578void
2579clientHttpConnectionsClose(void)
2580{
2581 int i;
2582 for (i = 0; i < NHttpSockets; i++) {
2583 if (HttpSockets[i] >= 0) {
2584 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets[i]);
2585 comm_close(HttpSockets[i]);
2586 HttpSockets[i] = -1;
2587 }
2588 }
2589 NHttpSockets = 0;
2590}