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