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