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