]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_side.cc
added clientdbFreeMemory()
[thirdparty/squid.git] / src / client_side.cc
CommitLineData
3c66d057 1
dd11e0b7 2/*
de336bbe 3 * $Id: client_side.cc,v 1.224 1998/03/11 22:18:47 rousskov 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
7a2f978b 34static const char *const crlf = "\r\n";
cb69b4c7 35static const char *const proxy_auth_challenge =
ee1679df 36"Basic realm=\"Squid proxy-caching web server\"";
7a2f978b 37
38#define REQUEST_BUF_SIZE 4096
39#define FAILURE_MODE_TIME 300
40
41/* Local functions */
42
1b02b5be 43static CWCB clientHandleIMSComplete;
fc5d6f7f 44static CWCB clientWriteComplete;
7a2f978b 45static PF clientReadRequest;
46static PF connStateFree;
47static PF requestTimeout;
1b02b5be 48static STCB clientGetHeadersForIMS;
7a2f978b 49static int CheckQuickAbort2(const clientHttpRequest *);
1b02b5be 50static int clientCheckTransferDone(clientHttpRequest *);
7a2f978b 51static void CheckQuickAbort(clientHttpRequest *);
88aad2e5 52static void checkFailureRatio(err_type, hier_code);
fb63215a 53static void clientProcessMiss(clientHttpRequest *);
7a2f978b 54static void clientAppendReplyHeader(char *, const char *, size_t *, size_t);
55size_t clientBuildReplyHeader(clientHttpRequest *, char *, size_t *, char *, size_t);
56static clientHttpRequest *parseHttpRequest(ConnStateData *, method_t *, int *, char **, size_t *);
582b6456 57static RH clientRedirectDone;
1b02b5be 58static STCB clientHandleIMSReply;
f5b8bbc4 59static int clientGetsOldEntry(StoreEntry * new, StoreEntry * old, request_t * request);
60static int checkAccelOnly(clientHttpRequest *);
7a2f978b 61static STCB clientSendMoreData;
62static STCB clientCacheHit;
50ddd7a4 63static void clientParseRequestHeaders(clientHttpRequest *);
fb63215a 64static void clientProcessRequest(clientHttpRequest *);
65static void clientProcessExpired(void *data);
cb69b4c7 66static HttpReply *clientConstructProxyAuthReply(clientHttpRequest * http);
50ddd7a4 67static int clientCachable(clientHttpRequest * http);
68static int clientHierarchical(clientHttpRequest * http);
a7c05555 69static int isTcpHit(log_type code);
ea6f43cd 70
38d7734b 71static int
382d851a 72checkAccelOnly(clientHttpRequest * http)
38d7734b 73{
74 /* return TRUE if someone makes a proxy request to us and
75 * we are in httpd-accel only mode */
f1dc9b30 76 if (!Config2.Accel.on)
38d7734b 77 return 0;
17a0a4ee 78 if (Config.onoff.accel_with_proxy)
38d7734b 79 return 0;
382d851a 80 if (http->request->protocol == PROTO_CACHEOBJ)
38d7734b 81 return 0;
382d851a 82 if (http->accel)
38d7734b 83 return 0;
84 return 1;
85}
86
b8d8561b 87void
382d851a 88clientAccessCheck(void *data)
f88bb09c 89{
382d851a 90 clientHttpRequest *http = data;
91 ConnStateData *conn = http->conn;
75e88d56 92 char *browser;
17a0a4ee 93 if (Config.onoff.ident_lookup && conn->ident.state == IDENT_NONE) {
382d851a 94 identStart(-1, conn, clientAccessCheck);
ba1d2afa 95 return;
96 }
382d851a 97 if (checkAccelOnly(http)) {
98 clientAccessCheckDone(0, http);
75e88d56 99 return;
f88bb09c 100 }
f182d1c5 101 browser = mime_get_header(http->request->headers, "User-Agent");
f1dc9b30 102 http->acl_checklist = aclChecklistCreate(Config.accessList.http,
382d851a 103 http->request,
104 conn->peer.sin_addr,
75e88d56 105 browser,
382d851a 106 conn->ident.ident);
107 aclNBCheck(http->acl_checklist, clientAccessCheckDone, http);
f88bb09c 108}
109
cb69b4c7 110static HttpReply *
ee1679df 111clientConstructProxyAuthReply(clientHttpRequest * http)
cb69b4c7 112{
113 ErrorState *err = errorCon(ERR_CACHE_ACCESS_DENIED, HTTP_PROXY_AUTHENTICATION_REQUIRED);
114 HttpReply *rep;
115 err->request = requestLink(http->request);
116 rep = errorBuildReply(err);
117 errorStateFree(err);
118 /* add Authenticate header */
119 httpHeaderSetStr(&rep->hdr, HDR_PROXY_AUTHENTICATE, proxy_auth_challenge);
120 return rep;
121}
fc5d6f7f 122
23d92c64 123StoreEntry *
f44af445 124clientCreateStoreEntry(clientHttpRequest * h, method_t m, int flags)
79e0dc20 125{
126 StoreEntry *e;
40defb17 127 request_t *r;
128 /*
129 * For erroneous requests, we might not have a h->request,
130 * so make a fake one.
131 */
132 if (h->request == NULL) {
7021844c 133 r = memAllocate(MEM_REQUEST_T);
40defb17 134 r->method = m;
135 r->protocol = PROTO_NONE;
136 h->request = requestLink(r);
137 }
23d92c64 138 e = storeCreateEntry(h->uri, h->log_uri, flags, m);
79e0dc20 139 storeClientListAdd(e, h);
7021844c 140 storeClientCopy(e, 0, 0, 4096, memAllocate(MEM_4K_BUF), clientSendMoreData, h);
79e0dc20 141 return e;
142}
143
144
b8d8561b 145void
75e88d56 146clientAccessCheckDone(int answer, void *data)
f88bb09c 147{
382d851a 148 clientHttpRequest *http = data;
02922e76 149#if 0
e92d33a5 150 char *redirectUrl = NULL;
02922e76 151#else
152 int page_id = -1;
153#endif
9b312a19 154 ErrorState *err = NULL;
1bea1d83 155 HttpReply *rep;
23d92c64 156 debug(33, 5) ("clientAccessCheckDone: '%s' answer=%d\n", http->uri, answer);
382d851a 157 http->acl_checklist = NULL;
fc5d6f7f 158 if (answer == ACCESS_ALLOWED) {
23d92c64 159 urlCanonical(http->request, http->uri);
ce66013b 160 assert(http->redirect_state == REDIRECT_NONE);
382d851a 161 http->redirect_state = REDIRECT_PENDING;
162 redirectStart(http, clientRedirectDone, http);
fc5d6f7f 163 } else if (answer == ACCESS_REQ_PROXY_AUTH) {
164 http->al.http.code = HTTP_PROXY_AUTHENTICATION_REQUIRED;
165 http->log_type = LOG_TCP_DENIED;
f44af445 166 http->entry = clientCreateStoreEntry(http, http->request->method, 0);
1bea1d83 167 /* create appropreate response */
168 rep = clientConstructProxyAuthReply(http);
169 httpReplySwapOut(rep, http->entry);
170 /* do not need it anymore */
171 httpReplyDestroy(rep);
f88bb09c 172 } else {
23d92c64 173 debug(33, 5) ("Access Denied: %s\n", http->uri);
02922e76 174 debug(33, 5) ("AclMatchedName = %s\n", AclMatchedName ? AclMatchedName : "<null>");
79a15e0a 175 http->log_type = LOG_TCP_DENIED;
f44af445 176 http->entry = clientCreateStoreEntry(http, http->request->method, 0);
02922e76 177 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName);
178 /* NOTE: don't use HTTP_UNAUTHORIZED because then the
179 * stupid browser wants us to authenticate */
180 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
181 err->request = requestLink(http->request);
182 err->src_addr = http->conn->peer.sin_addr;
183 if (page_id > 0)
184 err->page_id = page_id;
185 errorAppendEntry(http->entry, err);
f88bb09c 186 }
187}
188
b8d8561b 189static void
190clientRedirectDone(void *data, char *result)
f88bb09c 191{
382d851a 192 clientHttpRequest *http = data;
88738790 193 size_t l;
c0cdaf99 194 request_t *new_request = NULL;
382d851a 195 request_t *old_request = http->request;
23d92c64 196 debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http->uri,
f88bb09c 197 result ? result : "NULL");
ce66013b 198 assert(http->redirect_state == REDIRECT_PENDING);
382d851a 199 http->redirect_state = REDIRECT_DONE;
c0cdaf99 200 if (result)
20cc1450 201 new_request = urlParse(old_request->method, result);
c0cdaf99 202 if (new_request) {
23d92c64 203 safe_free(http->uri);
88738790 204 /* need to malloc because the URL returned by the redirector might
205 * not be big enough to append the local domain
206 * -- David Lamkin drl@net-tel.co.uk */
207 l = strlen(result) + Config.appendDomainLen + 5;
23d92c64 208 http->uri = xcalloc(l, 1);
209 xstrncpy(http->uri, result, l);
20cc1450 210 new_request->http_ver = old_request->http_ver;
16b9d3df 211 new_request->headers = xstrdup(old_request->headers);
2357f74a 212 new_request->headers_sz = old_request->headers_sz;
6cf028ab 213 if (old_request->body) {
214 new_request->body = xmalloc(old_request->body_sz);
215 xmemcpy(new_request->body, old_request->body, old_request->body_sz);
216 new_request->body_sz = old_request->body_sz;
217 }
20cc1450 218 requestUnlink(old_request);
382d851a 219 http->request = requestLink(new_request);
23d92c64 220 urlCanonical(http->request, http->uri);
f88bb09c 221 }
50ddd7a4 222 clientParseRequestHeaders(http);
23d92c64 223 fd_note(http->conn->fd, http->uri);
fb63215a 224 clientProcessRequest(http);
e81957b7 225}
226
fb63215a 227static void
228clientProcessExpired(void *data)
620da955 229{
382d851a 230 clientHttpRequest *http = data;
23d92c64 231 char *url = http->uri;
620da955 232 StoreEntry *entry = NULL;
23d92c64 233 debug(33, 3) ("clientProcessExpired: '%s'\n", http->uri);
79a15e0a 234 EBIT_SET(http->request->flags, REQ_REFRESH);
382d851a 235 http->old_entry = http->entry;
620da955 236 entry = storeCreateEntry(url,
23d92c64 237 http->log_uri,
382d851a 238 http->request->flags,
239 http->request->method);
620da955 240 /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */
fe96bbe6 241 storeClientListAdd(entry, http);
242 storeClientListAdd(http->old_entry, http);
382d851a 243 entry->lastmod = http->old_entry->lastmod;
50ddd7a4 244 debug(33, 5) ("clientProcessExpired: setting lmt = %d\n",
620da955 245 entry->lastmod);
d1a43e28 246 entry->refcount++; /* EXPIRED CASE */
382d851a 247 http->entry = entry;
248 http->out.offset = 0;
fb63215a 249 protoDispatch(http->conn->fd, http->entry, http->request);
f990cccc 250 /* Register with storage manager to receive updates when data comes in. */
0e473d70 251 if (entry->store_status == STORE_ABORTED)
6cf028ab 252 debug(33, 0) ("clientProcessExpired: entry->swap_status == STORE_ABORTED\n");
d89d1fb6 253 storeClientCopy(entry,
fe96bbe6 254 http->out.offset,
d89d1fb6 255 http->out.offset,
256 4096,
7021844c 257 memAllocate(MEM_4K_BUF),
1b02b5be 258 clientHandleIMSReply,
d89d1fb6 259 http);
620da955 260}
261
91f4d519 262static int
263clientGetsOldEntry(StoreEntry * new_entry, StoreEntry * old_entry, request_t * request)
264{
265 /* If the reply is anything but "Not Modified" then
266 * we must forward it to the client */
cb69b4c7 267 const http_status status = new_entry->mem_obj->reply->sline.status;
268 if (status != 304) {
269 debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", status);
91f4d519 270 return 0;
271 }
272 /* If the client did not send IMS in the request, then it
273 * must get the old object, not this "Not Modified" reply */
79a15e0a 274 if (!EBIT_TEST(request->flags, REQ_IMS)) {
a3d5953d 275 debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n");
91f4d519 276 return 1;
277 }
278 /* If the client IMS time is prior to the entry LASTMOD time we
279 * need to send the old object */
280 if (modifiedSince(old_entry, request)) {
a3d5953d 281 debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n", request->ims);
91f4d519 282 return 1;
283 }
a3d5953d 284 debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n");
91f4d519 285 return 0;
286}
287
288
52d4522b 289static void
1b02b5be 290clientHandleIMSReply(void *data, char *buf, ssize_t size)
620da955 291{
382d851a 292 clientHttpRequest *http = data;
382d851a 293 StoreEntry *entry = http->entry;
620da955 294 MemObject *mem = entry->mem_obj;
9fb13bb6 295 const char *url = storeUrl(entry);
e92d33a5 296 int unlink_request = 0;
76cff1d4 297 StoreEntry *oldentry;
93f9abd0 298 debug(33, 3) ("clientHandleIMSReply: %s, %d bytes\n", url, (int) size);
3f6c0fb2 299 memFree(MEM_4K_BUF, buf);
76a5501f 300 buf = NULL;
620da955 301 /* unregister this handler */
070d29eb 302 if (size < 0 || entry->store_status == STORE_ABORTED) {
1b02b5be 303 debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url);
c54e9052 304 /* We have an existing entry, but failed to validate it */
accc6d47 305 /* Its okay to send the old one anyway */
306 http->log_type = LOG_TCP_REFRESH_FAIL_HIT;
307 storeUnregister(entry, http);
308 storeUnlockObject(entry);
309 entry = http->entry = http->old_entry;
310 entry->refcount++;
cb69b4c7 311 } else if (mem->reply->sline.status == 0) {
1b02b5be 312 debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url);
0e473d70 313 if (entry->store_status == STORE_ABORTED)
6cf028ab 314 debug(33, 0) ("clientHandleIMSReply: entry->swap_status == STORE_ABORTED\n");
d89d1fb6 315 storeClientCopy(entry,
fe96bbe6 316 http->out.offset + size,
d89d1fb6 317 http->out.offset,
318 4096,
7021844c 319 memAllocate(MEM_4K_BUF),
1b02b5be 320 clientHandleIMSReply,
d89d1fb6 321 http);
52d4522b 322 return;
382d851a 323 } else if (clientGetsOldEntry(entry, http->old_entry, http->request)) {
620da955 324 /* We initiated the IMS request, the client is not expecting
76cff1d4 325 * 304, so put the good one back. First, make sure the old entry
326 * headers have been loaded from disk. */
382d851a 327 oldentry = http->old_entry;
382d851a 328 http->log_type = LOG_TCP_REFRESH_HIT;
d89d1fb6 329 if (oldentry->mem_obj->request == NULL) {
330 oldentry->mem_obj->request = requestLink(mem->request);
331 unlink_request = 1;
e92d33a5 332 }
64763c37 333 /* Don't memcpy() the whole reply structure here. For example,
334 * www.thegist.com (Netscape/1.13) returns a content-length for
335 * 304's which seems to be the length of the 304 HEADERS!!! and
336 * not the body they refer to. */
cb69b4c7 337 httpReplyUpdateOnNotModified(entry->mem_obj->reply, oldentry->mem_obj->reply);
d89d1fb6 338 storeTimestampsSet(oldentry);
382d851a 339 storeUnregister(entry, http);
6d54aea8 340 storeUnlockObject(entry);
382d851a 341 entry = http->entry = oldentry;
41fad779 342 entry->timestamp = squid_curtime;
657266fe 343 if (unlink_request) {
e92d33a5 344 requestUnlink(entry->mem_obj->request);
657266fe 345 entry->mem_obj->request = NULL;
346 }
620da955 347 } else {
348 /* the client can handle this reply, whatever it is */
382d851a 349 http->log_type = LOG_TCP_REFRESH_MISS;
cb69b4c7 350 if (mem->reply->sline.status == 304) {
382d851a 351 http->old_entry->timestamp = squid_curtime;
352 http->old_entry->refcount++;
353 http->log_type = LOG_TCP_REFRESH_HIT;
d1a43e28 354 }
382d851a 355 storeUnregister(http->old_entry, http);
356 storeUnlockObject(http->old_entry);
620da955 357 }
382d851a 358 http->old_entry = NULL; /* done with old_entry */
e8479da9 359 /* use clientCacheHit() here as the callback because we might
6a54c60e 360 * be swapping in from disk, and the file might not really be
361 * there */
0e473d70 362 if (entry->store_status == STORE_ABORTED) {
6cf028ab 363 debug(33, 0) ("clientHandleIMSReply: IMS swapin failed on aborted object\n");
364 http->log_type = LOG_TCP_SWAPFAIL_MISS;
365 clientProcessMiss(http);
366 return;
367 }
d89d1fb6 368 storeClientCopy(entry,
fe96bbe6 369 http->out.offset,
d89d1fb6 370 http->out.offset,
371 4096,
7021844c 372 memAllocate(MEM_4K_BUF),
e8479da9 373 clientCacheHit,
d89d1fb6 374 http);
620da955 375}
91f4d519 376
377int
304d07cb 378modifiedSince(StoreEntry * entry, request_t * request)
91f4d519 379{
380 int object_length;
381 MemObject *mem = entry->mem_obj;
9fb13bb6 382 debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry));
91f4d519 383 if (entry->lastmod < 0)
384 return 1;
385 /* Find size of the object */
cb69b4c7 386 object_length = httpReplyContentLen(mem->reply);
387 if (object_length < 0)
07304bf9 388 object_length = contentLen(entry);
91f4d519 389 if (entry->lastmod > request->ims) {
a3d5953d 390 debug(33, 3) ("--> YES: entry newer than client\n");
91f4d519 391 return 1;
392 } else if (entry->lastmod < request->ims) {
a3d5953d 393 debug(33, 3) ("--> NO: entry older than client\n");
91f4d519 394 return 0;
395 } else if (request->imslen < 0) {
a3d5953d 396 debug(33, 3) ("--> NO: same LMT, no client length\n");
91f4d519 397 return 0;
398 } else if (request->imslen == object_length) {
a3d5953d 399 debug(33, 3) ("--> NO: same LMT, same length\n");
91f4d519 400 return 0;
401 } else {
a3d5953d 402 debug(33, 3) ("--> YES: same LMT, different length\n");
91f4d519 403 return 1;
404 }
405}
b3b64e58 406
407char *
382d851a 408clientConstructTraceEcho(clientHttpRequest * http)
b3b64e58 409{
410 LOCAL_ARRAY(char, line, 256);
411 LOCAL_ARRAY(char, buf, 8192);
412 size_t len;
413 memset(buf, '\0', 8192);
042461c3 414 snprintf(buf, 8192, "HTTP/1.0 200 OK\r\n");
56878878 415 snprintf(line, 256, "Date: %s\r\n", mkrfc1123(squid_curtime));
b3b64e58 416 strcat(buf, line);
042461c3 417 snprintf(line, 256, "Server: Squid/%s\r\n", SQUID_VERSION);
b3b64e58 418 strcat(buf, line);
042461c3 419 snprintf(line, 256, "Content-Type: message/http\r\n");
b3b64e58 420 strcat(buf, line);
421 strcat(buf, "\r\n");
422 len = strlen(buf);
382d851a 423 httpBuildRequestHeader(http->request,
424 http->request,
b3b64e58 425 NULL, /* entry */
b3b64e58 426 NULL, /* in_len */
427 buf + len,
428 8192 - len,
603a02fd 429 http->conn->fd,
430 0); /* flags */
382d851a 431 http->log_type = LOG_TCP_MISS;
4869e8a1 432 http->http_code = HTTP_OK;
b3b64e58 433 return buf;
434}
a90eae18 435
436void
382d851a 437clientPurgeRequest(clientHttpRequest * http)
a90eae18 438{
382d851a 439 int fd = http->conn->fd;
a90eae18 440 StoreEntry *entry;
9b312a19 441 ErrorState *err = NULL;
9fb13bb6 442 const cache_key *k;
cb69b4c7 443 MemBuf mb;
9b312a19 444 debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config.onoff.enable_purge);
17a0a4ee 445 if (!Config.onoff.enable_purge) {
79a15e0a 446 http->log_type = LOG_TCP_DENIED;
fe40a877 447 err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN);
9b312a19 448 err->request = requestLink(http->request);
449 err->src_addr = http->conn->peer.sin_addr;
f44af445 450 http->entry = clientCreateStoreEntry(http, http->request->method, 0);
79e0dc20 451 errorAppendEntry(http->entry, err);
a90eae18 452 return;
453 }
382d851a 454 http->log_type = LOG_TCP_MISS;
23d92c64 455 k = storeKeyPublic(http->uri, METHOD_GET);
9fb13bb6 456 if ((entry = storeGet(k)) == NULL) {
9b312a19 457 http->http_code = HTTP_NOT_FOUND;
a90eae18 458 } else {
459 storeRelease(entry);
9b312a19 460 http->http_code = HTTP_OK;
a90eae18 461 }
cb69b4c7 462 mb = httpPackedReply(1.0, http->http_code, NULL, 0, 0, -1);
463 comm_write_mbuf(fd, mb, clientWriteComplete, http);
a90eae18 464}
e33ba616 465
466int
ea3a2a69 467checkNegativeHit(StoreEntry * e)
e33ba616 468{
79a15e0a 469 if (!EBIT_TEST(e->flag, ENTRY_NEGCACHED))
ea3a2a69 470 return 0;
471 if (e->expires <= squid_curtime)
472 return 0;
473 if (e->store_status != STORE_OK)
474 return 0;
475 return 1;
e33ba616 476}
7a2f978b 477
a7c05555 478void
0e473d70 479clientUpdateCounters(clientHttpRequest * http)
a7c05555 480{
ee1679df 481 int svc_time = tvSubMsec(http->start, current_time);
482 icp_ping_data *i;
0e473d70 483 Counter.client_http.requests++;
484 kb_incr(&Counter.client_http.kbytes_in, http->req_sz);
485 kb_incr(&Counter.client_http.kbytes_out, http->out.size);
486 if (isTcpHit(http->log_type)) {
487 Counter.client_http.hits++;
488 kb_incr(&Counter.client_http.hit_kbytes_out, http->out.size);
489 }
490 if (http->request->err_type != ERR_NONE)
491 Counter.client_http.errors++;
12cf1be2 492 statHistCount(&Counter.client_http.all_svc_time, svc_time);
ee1679df 493 /*
494 * The idea here is not to be complete, but to get service times
495 * for only well-defined types. For example, we don't include
496 * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit
497 * (we *tried* to validate it, but failed).
498 */
499 switch (http->log_type) {
500 case LOG_TCP_IMS_HIT:
12cf1be2 501 statHistCount(&Counter.client_http.nm_svc_time, svc_time);
ee1679df 502 break;
503 case LOG_TCP_HIT:
504 case LOG_TCP_MEM_HIT:
12cf1be2 505 statHistCount(&Counter.client_http.hit_svc_time, svc_time);
ee1679df 506 break;
507 case LOG_TCP_MISS:
508 case LOG_TCP_CLIENT_REFRESH_MISS:
12cf1be2 509 statHistCount(&Counter.client_http.miss_svc_time, svc_time);
ee1679df 510 break;
511 default:
512 /* make compiler warnings go away */
513 break;
514 }
515 i = &http->request->hier.icp;
516 if (0 != i->stop.tv_sec)
12cf1be2 517 statHistCount(&Counter.icp.query_svc_time, tvSubUsec(i->start, i->stop));
a7c05555 518}
519
7a2f978b 520static void
521httpRequestFree(void *data)
522{
523 clientHttpRequest *http = data;
524 clientHttpRequest **H;
525 ConnStateData *conn = http->conn;
526 StoreEntry *entry = http->entry;
527 request_t *request = http->request;
528 MemObject *mem = NULL;
93f9abd0 529 debug(33, 3) ("httpRequestFree: %s\n", storeUrl(entry));
1b02b5be 530 if (!clientCheckTransferDone(http)) {
7a2f978b 531 if (entry)
532 storeUnregister(entry, http); /* unregister BEFORE abort */
533 CheckQuickAbort(http);
534 entry = http->entry; /* reset, IMS might have changed it */
535 if (entry && entry->ping_status == PING_WAITING)
536 storeReleaseRequest(entry);
79d39a72 537 protoUnregister(entry, request);
7a2f978b 538 }
539 assert(http->log_type < LOG_TYPE_MAX);
540 if (entry)
541 mem = entry->mem_obj;
542 if (http->out.size || http->log_type) {
543 http->al.icp.opcode = 0;
ef65d6ca 544 http->al.url = http->log_uri;
545 debug(33, 9) ("httpRequestFree: al.url='%s'\n", http->al.url);
7a2f978b 546 if (mem) {
cb69b4c7 547 http->al.http.code = mem->reply->sline.status;
548 http->al.http.content_type = httpReplyContentType(mem->reply);
7a2f978b 549 }
550 http->al.cache.caddr = conn->log_addr;
551 http->al.cache.size = http->out.size;
552 http->al.cache.code = http->log_type;
553 http->al.cache.msec = tvSubMsec(http->start, current_time);
554 http->al.cache.ident = conn->ident.ident;
555 if (request) {
556 http->al.http.method = request->method;
557 http->al.headers.request = request->headers;
558 http->al.hier = request->hier;
559 }
560 accessLogLog(&http->al);
a7c05555 561 clientUpdateCounters(http);
7a2f978b 562 clientdbUpdate(conn->peer.sin_addr, http->log_type, PROTO_HTTP);
563 }
564 if (http->redirect_state == REDIRECT_PENDING)
23d92c64 565 redirectUnregister(http->uri, http);
7a2f978b 566 if (http->acl_checklist)
567 aclChecklistFree(http->acl_checklist);
88aad2e5 568 if (request)
c24fca87 569 checkFailureRatio(request->err_type, http->al.hier.code);
23d92c64 570 safe_free(http->uri);
571 safe_free(http->log_uri);
7a2f978b 572 safe_free(http->al.headers.reply);
573 if (entry) {
574 http->entry = NULL;
575 storeUnregister(entry, http);
576 storeUnlockObject(entry);
577 }
578 /* old_entry might still be set if we didn't yet get the reply
1b02b5be 579 * code in clientHandleIMSReply() */
7a2f978b 580 if (http->old_entry) {
581 storeUnregister(http->old_entry, http);
582 storeUnlockObject(http->old_entry);
583 http->old_entry = NULL;
584 }
585 requestUnlink(http->request);
586 assert(http != http->next);
587 assert(http->conn->chr != NULL);
588 H = &http->conn->chr;
589 while (*H) {
590 if (*H == http)
591 break;
592 H = &(*H)->next;
593 }
594 assert(*H != NULL);
595 *H = http->next;
596 http->next = NULL;
597 cbdataFree(http);
598}
599
600/* This is a handler normally called by comm_close() */
601static void
602connStateFree(int fd, void *data)
603{
604 ConnStateData *connState = data;
605 clientHttpRequest *http;
93f9abd0 606 debug(33, 3) ("connStateFree: FD %d\n", fd);
7a2f978b 607 assert(connState != NULL);
79d39a72 608 while ((http = connState->chr) != NULL) {
7a2f978b 609 assert(http->conn == connState);
610 assert(connState->chr != connState->chr->next);
611 httpRequestFree(http);
612 }
613 if (connState->ident.fd > -1)
614 comm_close(connState->ident.fd);
615 safe_free(connState->in.buf);
59c4d35b 616 /* XXX account connState->in.buf */
7a2f978b 617 pconnHistCount(0, connState->nrequests);
618 cbdataFree(connState);
619}
620
621static void
50ddd7a4 622clientParseRequestHeaders(clientHttpRequest * http)
7a2f978b 623{
624 request_t *request = http->request;
625 char *request_hdr = request->headers;
626 char *t = NULL;
627 request->ims = -2;
628 request->imslen = -1;
629 if ((t = mime_get_header(request_hdr, "If-Modified-Since"))) {
79a15e0a 630 EBIT_SET(request->flags, REQ_IMS);
7a2f978b 631 request->ims = parse_rfc1123(t);
632 while ((t = strchr(t, ';'))) {
633 for (t++; isspace(*t); t++);
634 if (strncasecmp(t, "length=", 7) == 0)
635 request->imslen = atoi(t + 7);
636 }
637 }
638 if ((t = mime_get_header(request_hdr, "Pragma"))) {
639 if (!strcasecmp(t, "no-cache"))
79a15e0a 640 EBIT_SET(request->flags, REQ_NOCACHE);
7a2f978b 641 }
642 if (mime_get_header(request_hdr, "Range")) {
79a15e0a 643 EBIT_SET(request->flags, REQ_NOCACHE);
644 EBIT_SET(request->flags, REQ_RANGE);
7a2f978b 645 } else if (mime_get_header(request_hdr, "Request-Range")) {
79a15e0a 646 EBIT_SET(request->flags, REQ_NOCACHE);
647 EBIT_SET(request->flags, REQ_RANGE);
7a2f978b 648 }
649 if (mime_get_header(request_hdr, "Authorization"))
79a15e0a 650 EBIT_SET(request->flags, REQ_AUTH);
7a2f978b 651 if (request->login[0] != '\0')
79a15e0a 652 EBIT_SET(request->flags, REQ_AUTH);
7a2f978b 653 if ((t = mime_get_header(request_hdr, "Proxy-Connection"))) {
654 if (!strcasecmp(t, "Keep-Alive"))
79a15e0a 655 EBIT_SET(request->flags, REQ_PROXY_KEEPALIVE);
7a2f978b 656 }
657 if ((t = mime_get_header(request_hdr, "Via")))
658 if (strstr(t, ThisCache)) {
659 if (!http->accel) {
93f9abd0 660 debug(33, 1) ("WARNING: Forwarding loop detected for '%s'\n",
23d92c64 661 http->uri);
93f9abd0 662 debug(33, 1) ("--> %s\n", t);
7a2f978b 663 }
79a15e0a 664 EBIT_SET(request->flags, REQ_LOOPDETECT);
7a2f978b 665 }
666#if USE_USERAGENT_LOG
667 if ((t = mime_get_header(request_hdr, "User-Agent")))
668 logUserAgent(fqdnFromAddr(http->conn->peer.sin_addr), t);
669#endif
670 request->max_age = -1;
671 if ((t = mime_get_header(request_hdr, "Cache-control"))) {
672 if (!strncasecmp(t, "Max-age=", 8))
673 request->max_age = atoi(t + 8);
674 }
675 if (request->method == METHOD_TRACE) {
676 if ((t = mime_get_header(request_hdr, "Max-Forwards")))
677 request->max_forwards = atoi(t);
678 }
50ddd7a4 679 if (clientCachable(http))
680 EBIT_SET(request->flags, REQ_CACHABLE);
681 if (clientHierarchical(http))
682 EBIT_SET(request->flags, REQ_HIERARCHICAL);
93f9abd0 683 debug(33, 5) ("clientParseRequestHeaders: REQ_NOCACHE = %s\n",
50ddd7a4 684 EBIT_TEST(request->flags, REQ_NOCACHE) ? "SET" : "NOT SET");
93f9abd0 685 debug(33, 5) ("clientParseRequestHeaders: REQ_CACHABLE = %s\n",
50ddd7a4 686 EBIT_TEST(request->flags, REQ_CACHABLE) ? "SET" : "NOT SET");
93f9abd0 687 debug(33, 5) ("clientParseRequestHeaders: REQ_HIERARCHICAL = %s\n",
50ddd7a4 688 EBIT_TEST(request->flags, REQ_HIERARCHICAL) ? "SET" : "NOT SET");
7a2f978b 689}
690
691static int
50ddd7a4 692clientCachable(clientHttpRequest * http)
7a2f978b 693{
23d92c64 694 const char *url = http->uri;
7a2f978b 695 request_t *req = http->request;
696 method_t method = req->method;
697 const wordlist *p;
698 for (p = Config.cache_stoplist; p; p = p->next) {
699 if (strstr(url, p->key))
700 return 0;
701 }
702 if (Config.cache_stop_relist)
703 if (aclMatchRegex(Config.cache_stop_relist, url))
704 return 0;
705 if (req->protocol == PROTO_HTTP)
706 return httpCachable(method);
707 /* FTP is always cachable */
708 if (req->protocol == PROTO_GOPHER)
709 return gopherCachable(url);
710 if (req->protocol == PROTO_WAIS)
711 return 0;
712 if (method == METHOD_CONNECT)
713 return 0;
714 if (method == METHOD_TRACE)
715 return 0;
716 if (req->protocol == PROTO_CACHEOBJ)
717 return 0;
718 return 1;
719}
720
721/* Return true if we can query our neighbors for this object */
722static int
50ddd7a4 723clientHierarchical(clientHttpRequest * http)
7a2f978b 724{
23d92c64 725 const char *url = http->uri;
7a2f978b 726 request_t *request = http->request;
727 method_t method = request->method;
728 const wordlist *p = NULL;
729
730 /* IMS needs a private key, so we can use the hierarchy for IMS only
731 * if our neighbors support private keys */
79a15e0a 732 if (EBIT_TEST(request->flags, REQ_IMS) && !neighbors_do_private_keys)
7a2f978b 733 return 0;
79a15e0a 734 if (EBIT_TEST(request->flags, REQ_AUTH))
7a2f978b 735 return 0;
736 if (method == METHOD_TRACE)
737 return 1;
738 if (method != METHOD_GET)
739 return 0;
740 /* scan hierarchy_stoplist */
741 for (p = Config.hierarchy_stoplist; p; p = p->next)
742 if (strstr(url, p->key))
743 return 0;
79a15e0a 744 if (EBIT_TEST(request->flags, REQ_LOOPDETECT))
7a2f978b 745 return 0;
746 if (request->protocol == PROTO_HTTP)
747 return httpCachable(method);
748 if (request->protocol == PROTO_GOPHER)
749 return gopherCachable(url);
750 if (request->protocol == PROTO_WAIS)
751 return 0;
752 if (request->protocol == PROTO_CACHEOBJ)
753 return 0;
754 return 1;
755}
756
a7c05555 757static int
7a2f978b 758isTcpHit(log_type code)
759{
760 /* this should be a bitmap for better optimization */
761 if (code == LOG_TCP_HIT)
762 return 1;
763 if (code == LOG_TCP_IMS_HIT)
764 return 1;
765 if (code == LOG_TCP_REFRESH_FAIL_HIT)
766 return 1;
767 if (code == LOG_TCP_REFRESH_HIT)
768 return 1;
769 if (code == LOG_TCP_NEGATIVE_HIT)
770 return 1;
771 if (code == LOG_TCP_MEM_HIT)
772 return 1;
773 return 0;
774}
775
776static void
777clientAppendReplyHeader(char *hdr, const char *line, size_t * sz, size_t max)
778{
779 size_t n = *sz + strlen(line) + 2;
780 if (n >= max)
781 return;
782 strcpy(hdr + (*sz), line);
783 strcat(hdr + (*sz), crlf);
784 *sz = n;
785}
786
adba4a64 787/* this entire function has to be rewriten using new interfaces @?@ @?@ */
7a2f978b 788size_t
789clientBuildReplyHeader(clientHttpRequest * http,
790 char *hdr_in,
791 size_t * in_len,
792 char *hdr_out,
793 size_t out_sz)
794{
ab013258 795 LOCAL_ARRAY(char, no_forward, 1024);
7a2f978b 796 char *xbuf;
797 char *ybuf;
798 char *t = NULL;
799 char *end = NULL;
800 size_t len = 0;
801 size_t hdr_len = 0;
802 size_t l;
803 end = mime_headers_end(hdr_in);
804 if (end == NULL) {
93f9abd0 805 debug(33, 3) ("clientBuildReplyHeader: DIDN'T FIND END-OF-HEADERS\n");
7a2f978b 806 return 0;
807 }
7021844c 808 xbuf = memAllocate(MEM_4K_BUF);
809 ybuf = memAllocate(MEM_4K_BUF);
7a2f978b 810 for (t = hdr_in; t < end; t += strcspn(t, crlf), t += strspn(t, crlf)) {
811 hdr_len = t - hdr_in;
812 l = strcspn(t, crlf) + 1;
813 xstrncpy(xbuf, t, l > 4096 ? 4096 : l);
ae1acd0b 814 /* enforce 1.0 reply version, this hack will be rewritten */
2ac76861 815 if (!hdr_len && !strncasecmp(xbuf, "HTTP/", 5) && l > 8 &&
816 (isspace(xbuf[8]) || isspace(xbuf[9])))
817 xmemmove(xbuf + 5, "1.0 ", 4);
7a2f978b 818#if 0
819 if (strncasecmp(xbuf, "Accept-Ranges:", 14) == 0)
820 continue;
821 if (strncasecmp(xbuf, "Etag:", 5) == 0)
822 continue;
823#endif
824 if (strncasecmp(xbuf, "Proxy-Connection:", 17) == 0)
825 continue;
ab013258 826 if (strncasecmp(xbuf, "Connection:", 11) == 0) {
827 handleConnectionHeader(0, no_forward, &xbuf[11]);
7a2f978b 828 continue;
ab013258 829 }
7a2f978b 830 if (strncasecmp(xbuf, "Keep-Alive:", 11) == 0)
831 continue;
832 if (strncasecmp(xbuf, "Set-Cookie:", 11) == 0)
833 if (isTcpHit(http->log_type))
834 continue;
ab013258 835 if (!handleConnectionHeader(1, no_forward, xbuf))
836 clientAppendReplyHeader(hdr_out, xbuf, &len, out_sz - 512);
7a2f978b 837 }
838 hdr_len = end - hdr_in;
839 /* Append X-Cache: */
02941a5d 840 snprintf(ybuf, 4096, "X-Cache: %s from %s",
841 isTcpHit(http->log_type) ? "HIT" : "MISS",
842 getMyHostname());
7a2f978b 843 clientAppendReplyHeader(hdr_out, ybuf, &len, out_sz);
844 /* Append Proxy-Connection: */
79a15e0a 845 if (EBIT_TEST(http->request->flags, REQ_PROXY_KEEPALIVE)) {
7a2f978b 846 snprintf(ybuf, 4096, "Proxy-Connection: Keep-Alive");
847 clientAppendReplyHeader(hdr_out, ybuf, &len, out_sz);
848 }
849 clientAppendReplyHeader(hdr_out, null_string, &len, out_sz);
850 if (in_len)
851 *in_len = hdr_len;
852 if ((l = strlen(hdr_out)) != len) {
853 debug_trap("clientBuildReplyHeader: size mismatch");
854 len = l;
855 }
93f9abd0 856 debug(33, 3) ("clientBuildReplyHeader: OUTPUT:\n%s\n", hdr_out);
3f6c0fb2 857 memFree(MEM_4K_BUF, xbuf);
858 memFree(MEM_4K_BUF, ybuf);
de336bbe 859 /* temporary kludge to test headers, remove it @?@ @?@ */
860 {
861 extern void httpHeaderTestParser(const char *hstr);
862 httpHeaderTestParser(hdr_out);
863 }
864 /* end of kludge */
7a2f978b 865 return len;
866}
867
4c323c5f 868static void
7a2f978b 869clientCacheHit(void *data, char *buf, ssize_t size)
870{
871 clientHttpRequest *http = data;
93f9abd0 872 debug(33, 3) ("clientCacheHit: %s, %d bytes\n", http->uri, (int) size);
7a2f978b 873 if (size >= 0) {
874 clientSendMoreData(data, buf, size);
875 } else if (http->entry == NULL) {
93f9abd0 876 debug(33, 3) ("clientCacheHit: request aborted\n");
7a2f978b 877 } else {
878 /* swap in failure */
93f9abd0 879 debug(33, 3) ("clientCacheHit: swapin failure for %s\n", http->uri);
7a2f978b 880 http->log_type = LOG_TCP_SWAPFAIL_MISS;
6cf028ab 881 if (http->entry)
882 storeRelease(http->entry);
fb63215a 883 clientProcessMiss(http);
7a2f978b 884 }
885}
886
4c323c5f 887static void
7a2f978b 888clientSendMoreData(void *data, char *buf, ssize_t size)
889{
890 clientHttpRequest *http = data;
891 StoreEntry *entry = http->entry;
892 ConnStateData *conn = http->conn;
893 int fd = conn->fd;
894 char *p = NULL;
895 size_t hdrlen;
896 size_t l = 0;
897 size_t writelen;
898 char *newbuf;
3f6c0fb2 899 FREE *freefunc = memFree4K;
7a2f978b 900 int hack = 0;
901 char C = '\0';
93f9abd0 902 debug(33, 5) ("clientSendMoreData: %s, %d bytes\n", http->uri, (int) size);
7a2f978b 903 assert(size <= SM_PAGE_SIZE);
40defb17 904 assert(http->request != NULL);
93f9abd0 905 debug(33, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n",
7a2f978b 906 fd, storeUrl(entry), http->out.offset);
907 if (conn->chr != http) {
908 /* there is another object in progress, defer this one */
a43b3862 909 debug(0, 0) ("clientSendMoreData: Deferring %s\n", storeUrl(entry));
7a2f978b 910 freefunc(buf);
911 return;
912 } else if (entry->store_status == STORE_ABORTED) {
913 freefunc(buf);
914 return;
915 } else if (size < 0) {
916 freefunc(buf);
917 return;
918 } else if (size == 0) {
919 clientWriteComplete(fd, NULL, 0, DISK_OK, http);
920 freefunc(buf);
921 return;
922 }
923 writelen = size;
40defb17 924 if (http->out.offset == 0) {
7a2f978b 925 if (Config.onoff.log_mime_hdrs) {
926 if ((p = mime_headers_end(buf))) {
927 safe_free(http->al.headers.reply);
928 http->al.headers.reply = xcalloc(1 + p - buf, 1);
929 xstrncpy(http->al.headers.reply, buf, p - buf);
930 }
931 }
932 /* make sure 'buf' is null terminated somewhere */
933 if (size == SM_PAGE_SIZE) {
934 hack = 1;
935 size--;
936 C = *(buf + size);
937 }
938 *(buf + size) = '\0';
7021844c 939 newbuf = memAllocate(MEM_8K_BUF);
7a2f978b 940 hdrlen = 0;
ab013258 941
7a2f978b 942 l = clientBuildReplyHeader(http, buf, &hdrlen, newbuf, 8192);
943 if (hack)
944 *(buf + size++) = C;
945 if (l != 0) {
946 writelen = l + size - hdrlen;
947 assert(writelen <= 8192);
948 /*
949 * l is the length of the new headers in newbuf
950 * hdrlen is the length of the old headers in buf
951 * size - hdrlen is the amount of body in buf
952 */
93f9abd0 953 debug(33, 3) ("clientSendMoreData: Appending %d bytes after headers\n",
7a2f978b 954 (int) (size - hdrlen));
6cf028ab 955 if (((size - hdrlen) + l) > 8192) {
0e473d70 956 debug(0, 0) ("Size, hdrlen, l %d, %d, %d\n", size, hdrlen, l);
6cf028ab 957 return;
958 }
7a2f978b 959 xmemcpy(newbuf + l, buf + hdrlen, size - hdrlen);
960 /* replace buf with newbuf */
961 freefunc(buf);
962 buf = newbuf;
3f6c0fb2 963 freefunc = memFree8K;
7a2f978b 964 newbuf = NULL;
965 } else {
3f6c0fb2 966 memFree(MEM_8K_BUF, newbuf);
7a2f978b 967 newbuf = NULL;
968 if (size < SM_PAGE_SIZE && entry->store_status == STORE_PENDING) {
969 /* wait for more to arrive */
970 storeClientCopy(entry,
971 http->out.offset + size,
972 http->out.offset,
973 SM_PAGE_SIZE,
974 buf,
975 clientSendMoreData,
976 http);
977 return;
978 }
979 }
980 }
981 http->out.offset += size;
40defb17 982 /*
983 * ick, this is gross
984 */
7a2f978b 985 if (http->request->method == METHOD_HEAD) {
986 if ((p = mime_headers_end(buf))) {
987 *p = '\0';
988 writelen = p - buf;
989 /* force end */
f68b5d81 990 if (entry->store_status == STORE_PENDING)
c24fca87 991 http->out.offset = entry->mem_obj->inmem_hi;
f68b5d81 992 else
07304bf9 993 http->out.offset = objectLen(entry);
7a2f978b 994 }
995 }
996 comm_write(fd, buf, writelen, clientWriteComplete, http, freefunc);
997}
998
fc5d6f7f 999static void
79a15e0a 1000clientWriteComplete(int fd, char *bufnotused, size_t size, int errflag, void *data)
7a2f978b 1001{
1002 clientHttpRequest *http = data;
1003 ConnStateData *conn;
1004 StoreEntry *entry = http->entry;
b34ed725 1005 int done;
7a2f978b 1006 http->out.size += size;
93f9abd0 1007 debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n",
07304bf9 1008 fd, size, errflag, http->out.offset, objectLen(entry));
7a2f978b 1009 if (errflag) {
1010 CheckQuickAbort(http);
7a2f978b 1011 comm_close(fd);
1012 } else if (entry->store_status == STORE_ABORTED) {
7a2f978b 1013 comm_close(fd);
a200bbd2 1014 } else if ((done = clientCheckTransferDone(http)) != 0 || size == 0) {
93f9abd0 1015 debug(33, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd);
7a2f978b 1016 /* We're finished case */
cb69b4c7 1017 if (httpReplyContentLen(http->entry->mem_obj->reply) < 0 || !done ||
b34ed725 1018 EBIT_TEST(entry->flag, ENTRY_BAD_LENGTH)) {
1019 /*
1020 * Client connection closed due to unknown or invalid
1021 * content length. Persistent connection is not possible.
1022 * This catches most cases, but probably not all.
1023 */
7a2f978b 1024 comm_close(fd);
79a15e0a 1025 } else if (EBIT_TEST(http->request->flags, REQ_PROXY_KEEPALIVE)) {
93f9abd0 1026 debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd);
7a2f978b 1027 conn = http->conn;
b34ed725 1028 conn->defer.until = 0; /* Kick it to read a new request */
7a2f978b 1029 httpRequestFree(http);
79d39a72 1030 if ((http = conn->chr) != NULL) {
93f9abd0 1031 debug(33, 1) ("clientWriteComplete: FD %d Sending next request\n", fd);
6cf028ab 1032 if (!storeClientCopyPending(http->entry, http)) {
0e473d70 1033 if (entry->store_status == STORE_ABORTED)
6cf028ab 1034 debug(33, 0) ("clientWriteComplete: entry->swap_status == STORE_ABORTED\n");
8daca701 1035 storeClientCopy(http->entry,
1036 http->out.offset,
1037 http->out.offset,
1038 SM_PAGE_SIZE,
7021844c 1039 memAllocate(MEM_4K_BUF),
8daca701 1040 clientSendMoreData,
1041 http);
6cf028ab 1042 }
7a2f978b 1043 } else {
93f9abd0 1044 debug(33, 5) ("clientWriteComplete: FD %d reading next request\n", fd);
7a2f978b 1045 fd_note(fd, "Reading next request");
0ca54a51 1046 /*
1047 * Set the timeout BEFORE calling clientReadRequest().
1048 */
7a2f978b 1049 commSetTimeout(fd, 15, requestTimeout, conn);
47130615 1050 clientReadRequest(fd, conn); /* Read next request */
0ca54a51 1051 /*
1052 * Note, the FD may be closed at this point.
1053 */
7a2f978b 1054 }
1055 } else {
1056 comm_close(fd);
1057 }
1058 } else {
1059 /* More data will be coming from primary server; register with
1060 * storage manager. */
0e473d70 1061 if (entry->store_status == STORE_ABORTED)
6cf028ab 1062 debug(33, 0) ("clientWriteComplete 2: entry->swap_status == STORE_ABORTED\n");
7a2f978b 1063 storeClientCopy(entry,
1064 http->out.offset,
1065 http->out.offset,
1066 SM_PAGE_SIZE,
7021844c 1067 memAllocate(MEM_4K_BUF),
7a2f978b 1068 clientSendMoreData,
1069 http);
1070 }
1071}
1072
1073static void
1b02b5be 1074clientGetHeadersForIMS(void *data, char *buf, ssize_t size)
7a2f978b 1075{
1076 clientHttpRequest *http = data;
7a2f978b 1077 StoreEntry *entry = http->entry;
dd83ba69 1078 MemObject *mem;
93f9abd0 1079 debug(33, 3) ("clientGetHeadersForIMS: %s, %d bytes\n",
1080 http->uri, (int) size);
7a2f978b 1081 assert(size <= SM_PAGE_SIZE);
3f6c0fb2 1082 memFree(MEM_4K_BUF, buf);
1b02b5be 1083 buf = NULL;
6cf028ab 1084 if (size < 0 || entry->store_status == STORE_ABORTED) {
8d4f9bc8 1085 /*
1086 * There are different reasons why we might have size < 0. One
1087 * being that we failed to open a swapfile. Another being that
1088 * the request was cancelled from the client-side. If the client
1089 * cancelled the request, then http->entry will be NULL.
1090 */
1091 if (entry != NULL) {
1092 debug(33, 1) ("clientGetHeadersForIMS: storeClientCopy failed for '%s'\n",
1093 storeKeyText(entry->key));
1094 clientProcessMiss(http);
1095 }
7a2f978b 1096 return;
1097 }
dd83ba69 1098 mem = entry->mem_obj;
cb69b4c7 1099 if (mem->reply->sline.status == 0) {
7a2f978b 1100 if (entry->mem_status == IN_MEMORY) {
fb63215a 1101 clientProcessMiss(http);
7a2f978b 1102 return;
1103 }
93f9abd0 1104 if (size == SM_PAGE_SIZE && http->out.offset == 0) {
1105 /*
1106 * We can't get any more headers than this, so bail
1107 */
1108 debug(33, 1) ("clientGetHeadersForIMS: failed, forcing cache miss\n");
1109 clientProcessMiss(http);
1110 return;
1111 }
1112 debug(33, 3) ("clientGetHeadersForIMS: waiting for HTTP reply headers\n");
7a2f978b 1113 /* All headers are not yet available, wait for more data */
0e473d70 1114 if (entry->store_status == STORE_ABORTED)
6cf028ab 1115 debug(33, 0) ("clientGetHeadersForIMS: entry->swap_status == STORE_ABORTED\n");
7a2f978b 1116 storeClientCopy(entry,
1117 http->out.offset + size,
1118 http->out.offset,
1119 SM_PAGE_SIZE,
7021844c 1120 memAllocate(MEM_4K_BUF),
1b02b5be 1121 clientGetHeadersForIMS,
7a2f978b 1122 http);
1123 return;
1124 }
1125 /* All headers are available, check if object is modified or not */
1126 /* ---------------------------------------------------------------
cb69b4c7 1127 * Removed check for reply->sline.status != 200 because of a potential
7a2f978b 1128 * problem with ICP. We will return a HIT for any public, cached
1129 * object. This includes other responses like 301, 410, as coded in
1130 * http.c. It is Bad(tm) to return UDP_HIT and then, if the reply
50ddd7a4 1131 * code is not 200, hand off to clientProcessMiss(), which may disallow
7a2f978b 1132 * the request based on 'miss_access' rules. Alternatively, we might
1133 * consider requiring returning UDP_HIT only for 200's. This
1134 * problably means an entry->flag bit, which would be lost during
1135 * restart because the flags aren't preserved across restarts.
1136 * --DW 3/11/96.
1137 * ---------------------------------------------------------------- */
1138#ifdef CHECK_REPLY_CODE_NOTEQUAL_200
1139 /* Only objects with statuscode==200 can be "Not modified" */
cb69b4c7 1140 if (mem->reply->sline.status != 200) {
93f9abd0 1141 debug(33, 4) ("clientGetHeadersForIMS: Reply code %d!=200\n",
cb69b4c7 1142 mem->reply->sline.status);
fb63215a 1143 clientProcessMiss(http);
7a2f978b 1144 return;
1145 }
7a2f978b 1146#endif
1b02b5be 1147 http->log_type = LOG_TCP_IMS_HIT;
7a2f978b 1148 entry->refcount++;
1149 if (modifiedSince(entry, http->request)) {
0e473d70 1150 if (entry->store_status == STORE_ABORTED)
6cf028ab 1151 debug(33, 0) ("clientGetHeadersForIMS 2: entry->swap_status == STORE_ABORTED\n");
7a2f978b 1152 storeClientCopy(entry,
1153 http->out.offset,
1154 http->out.offset,
1155 SM_PAGE_SIZE,
7021844c 1156 memAllocate(MEM_4K_BUF),
7a2f978b 1157 clientSendMoreData,
1158 http);
1159 return;
1160 }
93f9abd0 1161 debug(33, 4) ("clientGetHeadersForIMS: Not modified '%s'\n", storeUrl(entry));
cb69b4c7 1162 comm_write_mbuf(http->conn->fd, httpPacked304Reply(mem->reply),
1163 clientHandleIMSComplete, http);
7a2f978b 1164}
1165
1166static void
1b02b5be 1167clientHandleIMSComplete(int fd, char *bufnotused, size_t size, int flag, void *data)
7a2f978b 1168{
1169 clientHttpRequest *http = data;
1170 StoreEntry *entry = http->entry;
93f9abd0 1171 debug(33, 5) ("clientHandleIMSComplete: Not Modified sent '%s'\n", storeUrl(entry));
7a2f978b 1172 /* Set up everything for the logging */
1173 storeUnregister(entry, http);
1174 storeUnlockObject(entry);
1175 http->entry = NULL;
1176 http->out.size += size;
1177 http->al.http.code = 304;
79a15e0a 1178 if (flag != COMM_ERR_CLOSING)
7a2f978b 1179 comm_close(fd);
1180}
1181
50ddd7a4 1182static log_type
1183clientProcessRequest2(clientHttpRequest * http)
1184{
1185 const request_t *r = http->request;
23d92c64 1186 const cache_key *key = storeKeyPublic(http->uri, r->method);
50ddd7a4 1187 StoreEntry *e;
1188 if ((e = http->entry = storeGet(key)) == NULL) {
1189 /* this object isn't in the cache */
1190 return LOG_TCP_MISS;
1191 } else if (EBIT_TEST(e->flag, ENTRY_SPECIAL)) {
1192 if (e->mem_status == IN_MEMORY)
1193 return LOG_TCP_MEM_HIT;
1194 else
1195 return LOG_TCP_HIT;
1196 } else if (!storeEntryValidToSend(e)) {
1197 storeRelease(e);
1198 http->entry = NULL;
1199 return LOG_TCP_MISS;
1200 } else if (EBIT_TEST(r->flags, REQ_NOCACHE)) {
1201 /* NOCACHE should always eject a negative cached object */
1202 if (EBIT_TEST(e->flag, ENTRY_NEGCACHED))
1203 storeRelease(e);
1204 /* NOCACHE+IMS should not eject a valid object */
1205 else if (EBIT_TEST(r->flags, REQ_IMS))
1206 (void) 0;
1207 /* Request-Range should not eject a valid object */
1208 else if (EBIT_TEST(r->flags, REQ_RANGE))
1209 (void) 0;
1210 else
1211 storeRelease(e);
1212 ipcacheReleaseInvalid(r->host);
1213 http->entry = NULL;
ee1679df 1214 return LOG_TCP_CLIENT_REFRESH_MISS;
50ddd7a4 1215 } else if (checkNegativeHit(e)) {
1216 return LOG_TCP_NEGATIVE_HIT;
1217 } else if (refreshCheck(e, r, 0)) {
1218 /* The object is in the cache, but it needs to be validated. Use
1219 * LOG_TCP_REFRESH_MISS for the time being, maybe change it to
1b02b5be 1220 * _HIT later in clientHandleIMSReply() */
50ddd7a4 1221 if (r->protocol == PROTO_HTTP)
1222 return LOG_TCP_REFRESH_MISS;
1223 else
1224 return LOG_TCP_MISS; /* XXX zoinks */
1225 } else if (EBIT_TEST(r->flags, REQ_IMS)) {
1226 /* User-initiated IMS request for something we think is valid */
1227 return LOG_TCP_IMS_MISS;
1228 } else if (e->mem_status == IN_MEMORY) {
1229 return LOG_TCP_MEM_HIT;
1230 } else {
1231 return LOG_TCP_HIT;
1232 }
1233}
1234
4c323c5f 1235static void
fb63215a 1236clientProcessRequest(clientHttpRequest * http)
7a2f978b 1237{
23d92c64 1238 char *url = http->uri;
7a2f978b 1239 StoreEntry *entry = NULL;
50ddd7a4 1240 request_t *r = http->request;
fb63215a 1241 int fd = http->conn->fd;
1bea1d83 1242 HttpReply *rep;
93f9abd0 1243 debug(33, 4) ("clientProcessRequest: %s '%s'\n",
50ddd7a4 1244 RequestMethodStr[r->method],
7a2f978b 1245 url);
fb63215a 1246 if (r->method == METHOD_CONNECT) {
7a2f978b 1247 http->log_type = LOG_TCP_MISS;
ea0efb98 1248 sslStart(fd, url, r, &http->out.size);
1249 return;
fb63215a 1250 } else if (r->method == METHOD_PURGE) {
ea0efb98 1251 clientPurgeRequest(http);
1252 return;
fb63215a 1253 } else if (r->method == METHOD_TRACE) {
50ddd7a4 1254 if (r->max_forwards == 0) {
b17b22eb 1255 http->entry = clientCreateStoreEntry(http, r->method, 0);
1256 storeReleaseRequest(http->entry);
1257 storeBuffer(http->entry);
1bea1d83 1258 rep = httpReplyCreate();
1259 httpReplySetHeaders(rep, 1.0, HTTP_OK, NULL, "text/plain", r->headers_sz, 0, squid_curtime);
1260 httpReplySwapOut(rep, http->entry);
1261 httpReplyDestroy(rep);
b17b22eb 1262 storeAppend(http->entry, r->headers, r->headers_sz);
1263 storeComplete(http->entry);
7a2f978b 1264 return;
1265 }
1266 /* yes, continue */
fb63215a 1267 } else if (r->protocol != PROTO_HTTP) {
1268 (void) 0; /* fallthrough */
54220df8 1269#if OLD_POST_CODE
fb63215a 1270 } else if (r->method == METHOD_POST) {
7a2f978b 1271 http->log_type = LOG_TCP_MISS;
ea0efb98 1272 passStart(fd, url, r, &http->out.size);
1273 return;
54220df8 1274#else
1275 }
1276 if ( r->method == METHOD_PUT || r->method == METHOD_POST ) {
fb63215a 1277 http->log_type = LOG_TCP_MISS;
54220df8 1278 pumpInit(fd, r, http->uri);
1279#endif
7a2f978b 1280 }
fb63215a 1281 http->log_type = clientProcessRequest2(http);
93f9abd0 1282 debug(33, 4) ("clientProcessRequest: %s for '%s'\n",
7a2f978b 1283 log_tags[http->log_type],
23d92c64 1284 http->uri);
50ddd7a4 1285 if ((entry = http->entry) != NULL) {
7a2f978b 1286 storeLockObject(entry);
23d92c64 1287 storeCreateMemObject(entry, http->uri, http->log_uri);
7a2f978b 1288 storeClientListAdd(entry, http);
1289 }
7a2f978b 1290 http->out.offset = 0;
1291 switch (http->log_type) {
1292 case LOG_TCP_HIT:
1293 case LOG_TCP_NEGATIVE_HIT:
1294 case LOG_TCP_MEM_HIT:
1295 entry->refcount++; /* HIT CASE */
0e473d70 1296 if (entry->store_status == STORE_ABORTED)
6cf028ab 1297 debug(33, 0) ("clientProcessRequest: entry->swap_status == STORE_ABORTED\n");
7a2f978b 1298 storeClientCopy(entry,
1299 http->out.offset,
1300 http->out.offset,
1301 SM_PAGE_SIZE,
7021844c 1302 memAllocate(MEM_4K_BUF),
7a2f978b 1303 clientCacheHit,
1304 http);
1305 break;
1306 case LOG_TCP_IMS_MISS:
0e473d70 1307 if (entry->store_status == STORE_ABORTED)
6cf028ab 1308 debug(33, 0) ("clientProcessRequest 2: entry->swap_status == STORE_ABORTED\n");
7a2f978b 1309 storeClientCopy(entry,
1310 http->out.offset,
1311 http->out.offset,
1312 SM_PAGE_SIZE,
7021844c 1313 memAllocate(MEM_4K_BUF),
1b02b5be 1314 clientGetHeadersForIMS,
7a2f978b 1315 http);
1316 break;
1317 case LOG_TCP_REFRESH_MISS:
fb63215a 1318 clientProcessExpired(http);
7a2f978b 1319 break;
1320 default:
fb63215a 1321 clientProcessMiss(http);
7a2f978b 1322 break;
1323 }
1324}
1325
1326/*
1327 * Prepare to fetch the object as it's a cache miss of some kind.
1328 */
1329static void
fb63215a 1330clientProcessMiss(clientHttpRequest * http)
7a2f978b 1331{
23d92c64 1332 char *url = http->uri;
f44af445 1333 request_t *r = http->request;
1334 char *request_hdr = r->headers;
7a2f978b 1335 aclCheck_t ch;
1336 int answer;
1337 ErrorState *err = NULL;
93f9abd0 1338 debug(33, 4) ("clientProcessMiss: '%s %s'\n",
f44af445 1339 RequestMethodStr[r->method], url);
93f9abd0 1340 debug(33, 10) ("clientProcessMiss: request_hdr:\n%s\n", request_hdr);
79e0dc20 1341 /*
1342 * We might have a left-over StoreEntry from a failed cache hit
1343 * or IMS request.
1344 */
1345 if (http->entry) {
1346 storeUnregister(http->entry, http);
1347 storeUnlockObject(http->entry);
1348 http->entry = NULL;
1349 }
1350 /*
1351 * Check if this host is allowed to fetch MISSES from us (miss_access)
1352 */
7a2f978b 1353 memset(&ch, '\0', sizeof(aclCheck_t));
1354 ch.src_addr = http->conn->peer.sin_addr;
f44af445 1355 ch.request = r;
7a2f978b 1356 answer = aclCheckFast(Config.accessList.miss, &ch);
1357 if (answer == 0) {
1358 http->al.http.code = HTTP_FORBIDDEN;
bdb3c273 1359 err = errorCon(ERR_FORWARDING_DENIED, HTTP_FORBIDDEN);
f44af445 1360 err->request = requestLink(r);
7a2f978b 1361 err->src_addr = http->conn->peer.sin_addr;
f44af445 1362 http->entry = clientCreateStoreEntry(http, r->method, 0);
79e0dc20 1363 errorAppendEntry(http->entry, err);
7a2f978b 1364 return;
1365 }
79e0dc20 1366 assert(http->out.offset == 0);
f44af445 1367 http->entry = clientCreateStoreEntry(http, r->method, r->flags);
79e0dc20 1368 http->entry->mem_obj->fd = http->conn->fd;
1369 http->entry->refcount++;
f44af445 1370 protoDispatch(http->conn->fd, http->entry, r);
7a2f978b 1371}
1372
1373/*
1374 * parseHttpRequest()
1375 *
1376 * Returns
1377 * NULL on error or incomplete request
1378 * a clientHttpRequest structure on success
1379 */
1380static clientHttpRequest *
1381parseHttpRequest(ConnStateData * conn, method_t * method_p, int *status,
1382 char **headers_p, size_t * headers_sz_p)
1383{
1384 char *inbuf = NULL;
1385 char *mstr = NULL;
1386 char *url = NULL;
1387 char *req_hdr = NULL;
7a2f978b 1388 float http_ver;
1389 char *token = NULL;
1390 char *t = NULL;
1391 char *end = NULL;
1392 int free_request = 0;
1393 size_t header_sz; /* size of headers, not including first line */
1394 size_t req_sz; /* size of whole request */
1395 size_t url_sz;
1396 method_t method;
1397 clientHttpRequest *http = NULL;
1398
1399 /* Make sure a complete line has been received */
1400 if (strchr(conn->in.buf, '\n') == NULL) {
93f9abd0 1401 debug(33, 5) ("Incomplete request line, waiting for more data\n");
7a2f978b 1402 *status = 0;
1403 return NULL;
1404 }
1405 /* Use xmalloc/xmemcpy instead of xstrdup because inbuf might
1406 * contain NULL bytes; especially for POST data */
1407 inbuf = xmalloc(conn->in.offset + 1);
1408 xmemcpy(inbuf, conn->in.buf, conn->in.offset);
1409 *(inbuf + conn->in.offset) = '\0';
1410
1411 /* Look for request method */
1412 if ((mstr = strtok(inbuf, "\t ")) == NULL) {
93f9abd0 1413 debug(33, 1) ("parseHttpRequest: Can't get request method\n");
3775d53c 1414 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1415 cbdataAdd(http, MEM_NONE);
3775d53c 1416 http->conn = conn;
1417 http->start = current_time;
1418 http->req_sz = conn->in.offset;
23d92c64 1419 http->uri = xstrdup("error:invalid-request-method");
1420 http->log_uri = xstrdup("error:invalid-request-method");
3775d53c 1421 *headers_sz_p = conn->in.offset;
1422 *headers_p = inbuf;
1423 *method_p = METHOD_NONE;
7a2f978b 1424 *status = -1;
3775d53c 1425 return http;
7a2f978b 1426 }
1427 method = urlParseMethod(mstr);
1428 if (method == METHOD_NONE) {
93f9abd0 1429 debug(33, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr);
3775d53c 1430 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1431 cbdataAdd(http, MEM_NONE);
3775d53c 1432 http->conn = conn;
1433 http->start = current_time;
1434 http->req_sz = conn->in.offset;
23d92c64 1435 http->uri = xstrdup("error:unsupported-request-method");
1436 http->log_uri = xstrdup("error:unsupported-request-method");
3775d53c 1437 *headers_sz_p = conn->in.offset;
1438 *headers_p = inbuf;
1439 *method_p = METHOD_NONE;
7a2f978b 1440 *status = -1;
3775d53c 1441 return http;
7a2f978b 1442 }
93f9abd0 1443 debug(33, 5) ("parseHttpRequest: Method is '%s'\n", mstr);
3775d53c 1444 *method_p = method;
7a2f978b 1445
1446 /* look for URL */
1447 if ((url = strtok(NULL, "\r\n\t ")) == NULL) {
93f9abd0 1448 debug(33, 1) ("parseHttpRequest: Missing URL\n");
3775d53c 1449 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1450 cbdataAdd(http, MEM_NONE);
3775d53c 1451 http->conn = conn;
1452 http->start = current_time;
1453 http->req_sz = conn->in.offset;
23d92c64 1454 http->uri = xstrdup("error:missing-url");
1455 http->log_uri = xstrdup("error:missing-url");
3775d53c 1456 *headers_sz_p = conn->in.offset;
1457 *headers_p = inbuf;
7a2f978b 1458 *status = -1;
3775d53c 1459 return http;
7a2f978b 1460 }
93f9abd0 1461 debug(33, 5) ("parseHttpRequest: Request is '%s'\n", url);
7a2f978b 1462
1463 token = strtok(NULL, null_string);
1464 for (t = token; t && *t && *t != '\n' && *t != '\r'; t++);
46c7dbf0 1465 if (t == NULL || *t == '\0' || t == token || strncmp(token, "HTTP/", 5)) {
93f9abd0 1466 debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n");
3775d53c 1467 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1468 cbdataAdd(http, MEM_NONE);
3775d53c 1469 http->conn = conn;
1470 http->start = current_time;
1471 http->req_sz = conn->in.offset;
23d92c64 1472 http->uri = xstrdup("error:missing-http-ident");
1473 http->log_uri = xstrdup("error:missing-http-ident");
3775d53c 1474 *headers_sz_p = conn->in.offset;
1475 *headers_p = inbuf;
7a2f978b 1476 *status = -1;
3775d53c 1477 return http;
7a2f978b 1478 }
84116757 1479 http_ver = (float) atof(token + 5);
7a2f978b 1480
1481 /* Check if headers are received */
1482 if ((end = mime_headers_end(t)) == NULL) {
1483 xfree(inbuf);
1484 *status = 0;
1485 return NULL;
1486 }
7a2f978b 1487 req_hdr = t;
c24fca87 1488 /*
1489 * Skip whitespace at the end of the frist line, up to the
1490 * first newline.
1491 */
1492 while (isspace(*req_hdr))
1493 if (*(req_hdr++) == '\n')
1494 break;
1495 if (end <= req_hdr) {
1496 /* Invalid request */
93f9abd0 1497 debug(33, 3) ("parseHttpRequest: No request headers?\n");
c24fca87 1498 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1499 cbdataAdd(http, MEM_NONE);
c24fca87 1500 http->conn = conn;
1501 http->start = current_time;
1502 http->req_sz = conn->in.offset;
1503 http->uri = xstrdup("error:no-request-headers");
1504 http->log_uri = xstrdup("error:no-request-headers");
1505 *headers_sz_p = conn->in.offset;
1506 *headers_p = inbuf;
1507 *status = -1;
1508 return http;
1509 }
7a2f978b 1510 header_sz = end - req_hdr;
1511 req_sz = end - inbuf;
1512
1513 /* Ok, all headers are received */
1514 http = xcalloc(1, sizeof(clientHttpRequest));
3f6c0fb2 1515 cbdataAdd(http, MEM_NONE);
7a2f978b 1516 http->http_ver = http_ver;
1517 http->conn = conn;
1518 http->start = current_time;
1519 http->req_sz = req_sz;
1520 *headers_sz_p = header_sz;
1521 *headers_p = xmalloc(header_sz + 1);
1522 xmemcpy(*headers_p, req_hdr, header_sz);
1523 *(*headers_p + header_sz) = '\0';
1524
93f9abd0 1525 debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", *headers_p);
de336bbe 1526 /* temporary kludge to test headers, remove it @?@ @?@ */
1527 {
1528 extern void httpHeaderTestParser(const char *hstr);
1529 httpHeaderTestParser(*headers_p);
1530 }
1531 /* end of kludge */
7a2f978b 1532
23d92c64 1533 /* Assign http->uri */
7a2f978b 1534 if ((t = strchr(url, '\n'))) /* remove NL */
1535 *t = '\0';
1536 if ((t = strchr(url, '\r'))) /* remove CR */
1537 *t = '\0';
1538 if ((t = strchr(url, '#'))) /* remove HTML anchors */
1539 *t = '\0';
1540
1541 /* see if we running in Config2.Accel.on, if so got to convert it to URL */
1542 if (Config2.Accel.on && *url == '/') {
1543 /* prepend the accel prefix */
dbfed404 1544 if (opt_accel_uses_host && (t = mime_get_header(req_hdr, "Host"))) {
7a2f978b 1545 /* If a Host: header was specified, use it to build the URL
1546 * instead of the one in the Config file. */
1547 /*
1548 * XXX Use of the Host: header here opens a potential
1549 * security hole. There are no checks that the Host: value
1550 * corresponds to one of your servers. It might, for example,
1551 * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL
1552 * types should be used to prevent httpd-accelerators
1553 * handling requests for non-local servers */
1554 strtok(t, " :/;@");
12fc602c 1555 url_sz = strlen(url) + 32 + Config.appendDomainLen +
1556 strlen(t);
23d92c64 1557 http->uri = xcalloc(url_sz, 1);
1558 snprintf(http->uri, url_sz, "http://%s:%d%s",
7a2f978b 1559 t, (int) Config.Accel.port, url);
dbfed404 1560 } else if (vhost_mode) {
1561 /* Put the local socket IP address as the hostname */
1562 url_sz = strlen(url) + 32 + Config.appendDomainLen;
1563 http->uri = xcalloc(url_sz, 1);
1564 snprintf(http->uri, url_sz, "http://%s:%d%s",
1565 inet_ntoa(http->conn->me.sin_addr),
1566 (int) Config.Accel.port,
1567 url);
93f9abd0 1568 debug(33, 5) ("VHOST REWRITE: '%s'\n", http->uri);
7a2f978b 1569 } else {
1570 url_sz = strlen(Config2.Accel.prefix) + strlen(url) +
1571 Config.appendDomainLen + 1;
23d92c64 1572 http->uri = xcalloc(url_sz, 1);
1573 snprintf(http->uri, url_sz, "%s%s", Config2.Accel.prefix, url);
7a2f978b 1574 }
1575 http->accel = 1;
1576 } else {
1577 /* URL may be rewritten later, so make extra room */
1578 url_sz = strlen(url) + Config.appendDomainLen + 5;
23d92c64 1579 http->uri = xcalloc(url_sz, 1);
1580 strcpy(http->uri, url);
7a2f978b 1581 http->accel = 0;
1582 }
23d92c64 1583 http->log_uri = xstrdup(http->uri);
93f9abd0 1584 debug(33, 5) ("parseHttpRequest: Complete request received\n");
7a2f978b 1585 if (free_request)
1586 safe_free(url);
1587 xfree(inbuf);
7a2f978b 1588 *status = 1;
1589 return http;
1590}
1591
1592static int
79d39a72 1593clientReadDefer(int fdnotused, void *data)
7a2f978b 1594{
1595 ConnStateData *conn = data;
1596 return conn->defer.until > squid_curtime;
1597}
1598
1599static void
1600clientReadRequest(int fd, void *data)
1601{
1602 ConnStateData *conn = data;
1603 int parser_return_code = 0;
1604 int k;
1605 request_t *request = NULL;
7a2f978b 1606 int size;
7a2f978b 1607 method_t method;
1608 clientHttpRequest *http = NULL;
1609 clientHttpRequest **H = NULL;
1610 char *headers;
1611 size_t headers_sz;
1612 ErrorState *err = NULL;
1613 fde *F = &fd_table[fd];
44db45e8 1614 int len = conn->in.size - conn->in.offset - 1;
93f9abd0 1615 debug(33, 4) ("clientReadRequest: FD %d: reading request...\n", fd);
7a2f978b 1616 size = read(fd, conn->in.buf + conn->in.offset, len);
1617 fd_bytes(fd, size, FD_READ);
44db45e8 1618 /*
1619 * Don't reset the timeout value here. The timeout value will be
1620 * set to Config.Timeout.request by httpAccept() and
1621 * clientWriteComplete(), and should apply to the request as a
1622 * whole, not individual read() calls. Plus, it breaks our
1623 * lame half-close detection
1624 */
1625 commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, conn, 0);
7a2f978b 1626 if (size == 0) {
1627 if (conn->chr == NULL) {
1628 /* no current or pending requests */
1629 comm_close(fd);
1630 return;
1631 }
1632 /* It might be half-closed, we can't tell */
93f9abd0 1633 debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd);
79a15e0a 1634 EBIT_SET(F->flags, FD_SOCKET_EOF);
7a2f978b 1635 conn->defer.until = squid_curtime + 1;
1636 conn->defer.n++;
44db45e8 1637 fd_note(fd, "half-closed");
7a2f978b 1638 return;
1639 } else if (size < 0) {
6d3caf23 1640 if (!ignoreErrno(errno)) {
7a2f978b 1641 debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd, xstrerror());
1642 comm_close(fd);
ebf4efff 1643 return;
47130615 1644 } else if (conn->in.offset == 0) {
ebf4efff 1645 debug(50, 2) ("clientReadRequest: FD %d: no data to process\n");
1646 return;
7a2f978b 1647 }
ebf4efff 1648 /* Continue to process previously read data */
47130615 1649 size = 0;
7a2f978b 1650 }
1651 conn->in.offset += size;
1652 conn->in.buf[conn->in.offset] = '\0'; /* Terminate the string */
7a2f978b 1653 while (conn->in.offset > 0) {
ebf4efff 1654 int nrequests;
1655 /* Limit the number of concurrent requests to 2 */
1656 for (H = &conn->chr, nrequests = 0; *H; H = &(*H)->next, nrequests++);
1657 if (nrequests >= 2) {
93f9abd0 1658 debug(33, 2) ("clientReadRequest: FD %d max concurrent requests reached\n", fd);
1659 debug(33, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd);
47130615 1660 conn->defer.until = squid_curtime + 100; /* Reset when a request is complete */
ebf4efff 1661 break;
1662 }
1663 /* Process request */
7a2f978b 1664 http = parseHttpRequest(conn,
1665 &method,
1666 &parser_return_code,
1667 &headers,
1668 &headers_sz);
1669 if (http) {
1670 assert(http->req_sz > 0);
1671 conn->in.offset -= http->req_sz;
1672 assert(conn->in.offset >= 0);
6fa92aa2 1673 /*
1674 * If we read past the end of this request, move the remaining
1675 * data to the beginning
1676 */
1677 if (conn->in.offset > 0)
dbfed404 1678 xmemmove(conn->in.buf, conn->in.buf + http->req_sz, conn->in.offset);
ebf4efff 1679 /* add to the client request queue */
7a2f978b 1680 for (H = &conn->chr; *H; H = &(*H)->next);
1681 *H = http;
1682 conn->nrequests++;
1683 commSetTimeout(fd, Config.Timeout.lifetime, NULL, NULL);
3775d53c 1684 if (parser_return_code < 0) {
93f9abd0 1685 debug(33, 1) ("clientReadRequest: FD %d Invalid Request\n", fd);
3775d53c 1686 err = errorCon(ERR_INVALID_REQ, HTTP_BAD_REQUEST);
1687 err->request_hdrs = xstrdup(conn->in.buf);
1688 http->entry = clientCreateStoreEntry(http, method, 0);
1689 errorAppendEntry(http->entry, err);
1690 break;
1691 }
23d92c64 1692 if ((request = urlParse(method, http->uri)) == NULL) {
93f9abd0 1693 debug(33, 5) ("Invalid URL: %s\n", http->uri);
7a2f978b 1694 err = errorCon(ERR_INVALID_URL, HTTP_BAD_REQUEST);
1695 err->src_addr = conn->peer.sin_addr;
23d92c64 1696 err->url = xstrdup(http->uri);
7a2f978b 1697 http->al.http.code = err->http_status;
f44af445 1698 http->entry = clientCreateStoreEntry(http, method, 0);
79e0dc20 1699 errorAppendEntry(http->entry, err);
7a2f978b 1700 safe_free(headers);
1701 break;
1702 }
23d92c64 1703 safe_free(http->log_uri);
1704 http->log_uri = xstrdup(urlCanonicalClean(request));
7a2f978b 1705 request->client_addr = conn->peer.sin_addr;
1706 request->http_ver = http->http_ver;
1707 request->headers = headers;
1708 request->headers_sz = headers_sz;
1709 if (!urlCheckRequest(request)) {
1710 err = errorCon(ERR_UNSUP_REQ, HTTP_NOT_IMPLEMENTED);
1711 err->src_addr = conn->peer.sin_addr;
7a2f978b 1712 err->request = requestLink(request);
1713 http->al.http.code = err->http_status;
f44af445 1714 http->entry = clientCreateStoreEntry(http, request->method, 0);
79e0dc20 1715 errorAppendEntry(http->entry, err);
3775d53c 1716 break;
7a2f978b 1717 }
1718 http->request = requestLink(request);
cf26e54c 1719 clientAccessCheck(http);
3775d53c 1720 /*
1721 * break here for NON-GET because most likely there is a
7a2f978b 1722 * reqeust body following and we don't want to parse it
3775d53c 1723 * as though it was new request
1724 */
7a2f978b 1725 if (request->method != METHOD_GET) {
1726 if (conn->in.offset) {
1727 request->body_sz = conn->in.offset;
1728 request->body = xmalloc(request->body_sz);
1729 xmemcpy(request->body, conn->in.buf, request->body_sz);
1730 conn->in.offset = 0;
1731 }
1732 break;
1733 }
7a2f978b 1734 continue; /* while offset > 0 */
1735 } else if (parser_return_code == 0) {
1736 /*
1737 * Partial request received; reschedule until parseHttpRequest()
1738 * is happy with the input
1739 */
1740 k = conn->in.size - 1 - conn->in.offset;
1741 if (k == 0) {
1742 if (conn->in.offset >= Config.maxRequestSize) {
1743 /* The request is too large to handle */
93f9abd0 1744 debug(33, 0) ("Request won't fit in buffer.\n");
1745 debug(33, 0) ("Config 'request_size'= %d bytes.\n",
7a2f978b 1746 Config.maxRequestSize);
93f9abd0 1747 debug(33, 0) ("This request = %d bytes.\n",
7a2f978b 1748 conn->in.offset);
1749 err = errorCon(ERR_INVALID_REQ, HTTP_REQUEST_ENTITY_TOO_LARGE);
f44af445 1750 http->entry = clientCreateStoreEntry(http, request->method, 0);
79e0dc20 1751 errorAppendEntry(http->entry, err);
7a2f978b 1752 return;
1753 }
1754 /* Grow the request memory area to accomodate for a large request */
1755 conn->in.size += REQUEST_BUF_SIZE;
1756 conn->in.buf = xrealloc(conn->in.buf, conn->in.size);
59c4d35b 1757 /* XXX account conn->in.buf */
93f9abd0 1758 debug(33, 2) ("Handling a large request, offset=%d inbufsize=%d\n",
7a2f978b 1759 conn->in.offset, conn->in.size);
1760 k = conn->in.size - 1 - conn->in.offset;
1761 }
7a2f978b 1762 break;
7a2f978b 1763 }
1764 }
1765}
1766
1767/* general lifetime handler for HTTP requests */
1768static void
1769requestTimeout(int fd, void *data)
1770{
1771 ConnStateData *conn = data;
1772 ErrorState *err;
93f9abd0 1773 debug(33, 2) ("requestTimeout: FD %d: lifetime is expired.\n", fd);
7a2f978b 1774 if (fd_table[fd].rwstate) {
79e0dc20 1775 /*
1776 * Some data has been sent to the client, just close the FD
1777 */
7a2f978b 1778 comm_close(fd);
1779 } else if (conn->nrequests) {
79e0dc20 1780 /*
1781 * assume its a persistent connection; just close it
1782 */
7a2f978b 1783 comm_close(fd);
1784 } else {
79e0dc20 1785 /*
1786 * Generate an error
1787 */
7a2f978b 1788 err = errorCon(ERR_LIFETIME_EXP, HTTP_REQUEST_TIMEOUT);
7a2f978b 1789 err->url = xstrdup("N/A");
79e0dc20 1790 /*
1791 * Normally we shouldn't call errorSend() in client_side.c, but
1792 * it should be okay in this case. Presumably if we get here
1793 * this is the first request for the connection, and no data
1794 * has been written yet
1795 */
1796 assert(conn->chr == NULL);
7a2f978b 1797 errorSend(fd, err);
79e0dc20 1798 /*
1799 * if we don't close() here, we still need a timeout handler!
1800 */
7a2f978b 1801 commSetTimeout(fd, 30, requestTimeout, conn);
1802 }
1803}
1804
1805int
79d39a72 1806httpAcceptDefer(int fdnotused, void *notused)
7a2f978b 1807{
22f5d1ca 1808 return fdNFree() < RESERVED_FD;
7a2f978b 1809}
1810
bf8e5903 1811/* Handle a new connection on HTTP socket. */
7a2f978b 1812void
1813httpAccept(int sock, void *notused)
1814{
1815 int fd = -1;
1816 ConnStateData *connState = NULL;
1817 struct sockaddr_in peer;
1818 struct sockaddr_in me;
1819 memset(&peer, '\0', sizeof(struct sockaddr_in));
1820 memset(&me, '\0', sizeof(struct sockaddr_in));
1821 commSetSelect(sock, COMM_SELECT_READ, httpAccept, NULL, 0);
1822 if ((fd = comm_accept(sock, &peer, &me)) < 0) {
1823 debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n",
1824 sock, xstrerror());
1825 return;
1826 }
93f9abd0 1827 debug(33, 4) ("httpAccept: FD %d: accepted\n", fd);
7a2f978b 1828 connState = xcalloc(1, sizeof(ConnStateData));
1829 connState->peer = peer;
1830 connState->log_addr = peer.sin_addr;
1831 connState->log_addr.s_addr &= Config.Addrs.client_netmask.s_addr;
1832 connState->me = me;
1833 connState->fd = fd;
1834 connState->ident.fd = -1;
1835 connState->in.size = REQUEST_BUF_SIZE;
1836 connState->in.buf = xcalloc(connState->in.size, 1);
3f6c0fb2 1837 cbdataAdd(connState, MEM_NONE);
59c4d35b 1838 /* XXX account connState->in.buf */
7a2f978b 1839 comm_add_close_handler(fd, connStateFree, connState);
1840 if (Config.onoff.log_fqdn)
1841 fqdncache_gethostbyaddr(peer.sin_addr, FQDN_LOOKUP_IF_MISS);
1842 commSetTimeout(fd, Config.Timeout.request, requestTimeout, connState);
1843 commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, connState, 0);
1844 commSetDefer(fd, clientReadDefer, connState);
1845}
1846
1847void
1848AppendUdp(icpUdpData * item)
1849{
1850 item->next = NULL;
1851 if (UdpQueueHead == NULL) {
1852 UdpQueueHead = item;
1853 UdpQueueTail = item;
1854 } else if (UdpQueueTail == UdpQueueHead) {
1855 UdpQueueTail = item;
1856 UdpQueueHead->next = item;
1857 } else {
1858 UdpQueueTail->next = item;
1859 UdpQueueTail = item;
1860 }
1861}
1862
1863/* return 1 if the request should be aborted */
1864static int
1865CheckQuickAbort2(const clientHttpRequest * http)
1866{
1867 long curlen;
1868 long minlen;
1869 long expectlen;
1870
79a15e0a 1871 if (!EBIT_TEST(http->request->flags, REQ_CACHABLE))
7a2f978b 1872 return 1;
79a15e0a 1873 if (EBIT_TEST(http->entry->flag, KEY_PRIVATE))
7a2f978b 1874 return 1;
1875 if (http->entry->mem_obj == NULL)
1876 return 1;
cb69b4c7 1877 expectlen = httpReplyContentLen(http->entry->mem_obj->reply);
7a2f978b 1878 curlen = http->entry->mem_obj->inmem_hi;
1879 minlen = Config.quickAbort.min;
1880 if (minlen < 0)
1881 /* disabled */
1882 return 0;
1883 if (curlen > expectlen)
1884 /* bad content length */
1885 return 1;
1886 if ((expectlen - curlen) < minlen)
1887 /* only little more left */
1888 return 0;
1889 if ((expectlen - curlen) > Config.quickAbort.max)
1890 /* too much left to go */
1891 return 1;
1892 if ((curlen / (expectlen / 128U)) > Config.quickAbort.pct)
1893 /* past point of no return */
1894 return 0;
1895 return 1;
1896}
1897
1898
1899static void
1900CheckQuickAbort(clientHttpRequest * http)
1901{
1902 StoreEntry *entry = http->entry;
1903 /* Note, set entry here because http->entry might get changed (for IMS
1904 * requests) during the storeAbort() call */
1905 if (entry == NULL)
1906 return;
1907 if (storePendingNClients(entry) > 1)
1908 return;
1909 if (entry->store_status != STORE_PENDING)
1910 return;
1911 if (CheckQuickAbort2(http) == 0)
1912 return;
93f9abd0 1913 debug(33, 3) ("CheckQuickAbort: ABORTING %s\n", storeUrl(entry));
7a2f978b 1914 storeAbort(entry, 1);
1915}
1916
978e455f 1917#define SENDING_BODY 0
1918#define SENDING_HDRSONLY 1
7a2f978b 1919static int
1b02b5be 1920clientCheckTransferDone(clientHttpRequest * http)
7a2f978b 1921{
978e455f 1922 int sending = SENDING_BODY;
7a2f978b 1923 StoreEntry *entry = http->entry;
978e455f 1924 MemObject *mem;
1925 http_reply *reply;
1926 int sendlen;
cb69b4c7 1927 int clen;
7a2f978b 1928 if (entry == NULL)
1929 return 0;
978e455f 1930 /*
1931 * Handle STORE_OK and STORE_ABORTED objects.
07304bf9 1932 * objectLen(entry) will be set proprely.
978e455f 1933 */
1934 if (entry->store_status != STORE_PENDING) {
07304bf9 1935 if (http->out.offset >= objectLen(entry))
7a2f978b 1936 return 1;
513f05a6 1937 else
1938 return 0;
1939 }
978e455f 1940 /*
1941 * Now, handle STORE_PENDING objects
1942 */
1943 mem = entry->mem_obj;
1944 assert(mem != NULL);
1945 assert(http->request != NULL);
1946 reply = mem->reply;
1947 if (reply->hdr_sz == 0)
b34ed725 1948 return 0; /* haven't found end of headers yet */
cb69b4c7 1949 else if (reply->sline.status == HTTP_OK)
b34ed725 1950 sending = SENDING_BODY;
cb69b4c7 1951 else if (reply->sline.status == HTTP_NO_CONTENT)
b34ed725 1952 sending = SENDING_HDRSONLY;
cb69b4c7 1953 else if (reply->sline.status == HTTP_NOT_MODIFIED)
b34ed725 1954 sending = SENDING_HDRSONLY;
cb69b4c7 1955 else if (reply->sline.status < HTTP_OK)
a3c60429 1956 sending = SENDING_HDRSONLY;
978e455f 1957 else if (http->request->method == METHOD_HEAD)
b34ed725 1958 sending = SENDING_HDRSONLY;
978e455f 1959 else
1960 sending = SENDING_BODY;
1961 /*
1962 * Figure out how much data we are supposed to send.
1963 * If we are sending a body and we don't have a content-length,
1964 * then we must wait for the object to become STORE_OK or
1965 * STORE_ABORTED.
1966 */
cb69b4c7 1967 clen = httpReplyContentLen(reply);
978e455f 1968 if (sending == SENDING_HDRSONLY)
1969 sendlen = reply->hdr_sz;
cb69b4c7 1970 else if (clen < 0)
978e455f 1971 return 0;
1972 else
cb69b4c7 1973 sendlen = clen + reply->hdr_sz;
978e455f 1974 /*
1975 * Now that we have the expected length, did we send it all?
1976 */
1977 if (http->out.offset < sendlen)
1978 return 0;
1979 else
b34ed725 1980 return 1;
7a2f978b 1981}
1982
7a2f978b 1983/*
1984 * This function is designed to serve a fairly specific purpose.
1985 * Occasionally our vBNS-connected caches can talk to each other, but not
1986 * the rest of the world. Here we try to detect frequent failures which
1987 * make the cache unusable (e.g. DNS lookup and connect() failures). If
1988 * the failure:success ratio goes above 1.0 then we go into "hit only"
1989 * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors
1990 * will only fetch HITs from us if they are using the ICP protocol. We
1991 * stay in this mode for 5 minutes.
1992 *
1993 * Duane W., Sept 16, 1996
1994 */
1995
1996static void
88aad2e5 1997checkFailureRatio(err_type etype, hier_code hcode)
7a2f978b 1998{
88aad2e5 1999 static double magic_factor = 100.0;
7a2f978b 2000 double n_good;
2001 double n_bad;
2002 if (hcode == HIER_NONE)
2003 return;
88aad2e5 2004 n_good = magic_factor / (1.0 + request_failure_ratio);
7a2f978b 2005 n_bad = magic_factor - n_good;
88aad2e5 2006 switch (etype) {
7a2f978b 2007 case ERR_DNS_FAIL:
2008 case ERR_CONNECT_FAIL:
2009 case ERR_READ_ERROR:
2010 n_bad++;
2011 break;
2012 default:
2013 n_good++;
2014 }
88aad2e5 2015 request_failure_ratio = n_bad / n_good;
7a2f978b 2016 if (hit_only_mode_until > squid_curtime)
2017 return;
88aad2e5 2018 if (request_failure_ratio < 1.0)
7a2f978b 2019 return;
93f9abd0 2020 debug(33, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio);
2021 debug(33, 0) ("Going into hit-only-mode for %d minutes...\n",
7a2f978b 2022 FAILURE_MODE_TIME / 60);
2023 hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME;
88aad2e5 2024 request_failure_ratio = 0.8; /* reset to something less than 1.0 */
7a2f978b 2025}
15df8349 2026
2027void
2028clientHttpConnectionsOpen(void)
2029{
2030 ushortlist *u;
2031 int fd;
15df8349 2032 for (u = Config.Port.http; u; u = u->next) {
8daca701 2033 enter_suid();
2034 fd = comm_open(SOCK_STREAM,
2035 0,
2036 Config.Addrs.tcp_incoming,
2037 u->i,
2038 COMM_NONBLOCKING,
2039 "HTTP Socket");
2040 leave_suid();
2041 if (fd < 0)
2042 continue;
2043 comm_listen(fd);
2044 commSetSelect(fd, COMM_SELECT_READ, httpAccept, NULL, 0);
2045 commSetDefer(fd, httpAcceptDefer, NULL);
2046 debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n",
2047 (int) u->i, fd);
2048 HttpSockets[NHttpSockets++] = fd;
15df8349 2049 }
2050 if (NHttpSockets < 1)
8daca701 2051 fatal("Cannot open HTTP Port");
15df8349 2052}
c0fbae16 2053
ab013258 2054int
2055handleConnectionHeader(int flag, char *where, char *what)
2056{
2057 char *t, *p, *wh;
2058 int i;
2059 LOCAL_ARRAY(char, mbuf, 256);
2060
2061 if (flag) { /* lookup mode */
2062 if (where[0] == '\0' || what[0] == '\0')
2063 return 0;
2064 p = xstrdup(what);
2065 t = strtok(p, ":");
2066 if (t == NULL)
2067 return 0;
2068 debug(20, 3) ("handleConnectionHeader: %s\n AND %s (%p)\n", where, t, p);
2069 i = strstr(where, t) ? 1 : 0;
2070 xfree(p);
2071 return (i);
2072 }
2073 where[0] = '\0';
2074 wh = xstrdup(what);
2075 t = strtok(wh, ",");
2076 while (t != NULL) {
2077
2078#ifdef BE_PARANOID
2079 static char no_conn[] = "Expires:Host:Content-length:Content-type:";
2080
2081 if (handleConnectionHeader(1, no_conn, t)) {
2082 debug(1, 1) ("handleConnectionHeader: problematic header %s\n", t);
2083 t = strtok(NULL, ",\n");
2084 continue;
2085 }
2086#endif
2087 if ((p = strchr(t, ':')))
2088 xstrncpy(mbuf, t, p - t + 1);
2089 else
2090 snprintf(mbuf, 256, "%s:", t);
2091 strcat(where, mbuf);
2092 t = strtok(NULL, ",\n");
2093 }
2094 debug(20, 3) ("handleConnectionHeader: we have %s\n", where);
2095 xfree(wh);
2096 return 1;
2097}
2098
c0fbae16 2099void
2100clientHttpConnectionsClose(void)
2101{
2102 int i;
2103 for (i = 0; i < NHttpSockets; i++) {
2104 if (HttpSockets[i] >= 0) {
2105 debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets[i]);
2106 comm_close(HttpSockets[i]);
2107 HttpSockets[i] = -1;
2108 }
2109 }
2110 NHttpSockets = 0;
2111}