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