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