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