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