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