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