]>
Commit | Line | Data |
---|---|---|
3c66d057 | 1 | |
dd11e0b7 | 2 | /* |
69e81830 | 3 | * $Id: client_side.cc,v 1.204 1998/02/04 07:22:16 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/ |
dd11e0b7 | 9 | * -------------------------------------------------------- |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
13 | * National Laboratory for Applied Network Research and funded by | |
14 | * the National Science Foundation. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
29 | * | |
30 | */ | |
f88bb09c | 31 | |
32 | #include "squid.h" | |
33 | ||
7a2f978b | 34 | static const char *const crlf = "\r\n"; |
fc5d6f7f | 35 | static const char *const proxy_auth_line = |
36 | "Proxy-Authenticate: Basic realm=\"Squid proxy-caching web server\"\r\n"; | |
7a2f978b | 37 | |
38 | #define REQUEST_BUF_SIZE 4096 | |
39 | #define FAILURE_MODE_TIME 300 | |
40 | ||
41 | /* Local functions */ | |
42 | ||
1b02b5be | 43 | static CWCB clientHandleIMSComplete; |
fc5d6f7f | 44 | static CWCB clientWriteComplete; |
7a2f978b | 45 | static PF clientReadRequest; |
46 | static PF connStateFree; | |
47 | static PF requestTimeout; | |
1b02b5be | 48 | static STCB clientGetHeadersForIMS; |
49 | static char *clientConstruct304reply(struct _http_reply *); | |
7a2f978b | 50 | static int CheckQuickAbort2(const clientHttpRequest *); |
1b02b5be | 51 | static int clientCheckTransferDone(clientHttpRequest *); |
7a2f978b | 52 | static void CheckQuickAbort(clientHttpRequest *); |
88aad2e5 | 53 | static void checkFailureRatio(err_type, hier_code); |
fb63215a | 54 | static void clientProcessMiss(clientHttpRequest *); |
7a2f978b | 55 | static void clientAppendReplyHeader(char *, const char *, size_t *, size_t); |
56 | size_t clientBuildReplyHeader(clientHttpRequest *, char *, size_t *, char *, size_t); | |
57 | static clientHttpRequest *parseHttpRequest(ConnStateData *, method_t *, int *, char **, size_t *); | |
582b6456 | 58 | static RH clientRedirectDone; |
1b02b5be | 59 | static STCB clientHandleIMSReply; |
f5b8bbc4 | 60 | static int clientGetsOldEntry(StoreEntry * new, StoreEntry * old, request_t * request); |
61 | static int checkAccelOnly(clientHttpRequest *); | |
7a2f978b | 62 | static STCB clientSendMoreData; |
63 | static STCB clientCacheHit; | |
50ddd7a4 | 64 | static void clientParseRequestHeaders(clientHttpRequest *); |
fb63215a | 65 | static void clientProcessRequest(clientHttpRequest *); |
66 | static void clientProcessExpired(void *data); | |
fc5d6f7f | 67 | static char *clientConstructProxyAuthReply(clientHttpRequest * http); |
50ddd7a4 | 68 | static int clientCachable(clientHttpRequest * http); |
69 | static int clientHierarchical(clientHttpRequest * http); | |
a7c05555 | 70 | static int isTcpHit(log_type code); |
ea6f43cd | 71 | |
38d7734b | 72 | static int |
382d851a | 73 | checkAccelOnly(clientHttpRequest * http) |
38d7734b | 74 | { |
75 | /* return TRUE if someone makes a proxy request to us and | |
76 | * we are in httpd-accel only mode */ | |
f1dc9b30 | 77 | if (!Config2.Accel.on) |
38d7734b | 78 | return 0; |
17a0a4ee | 79 | if (Config.onoff.accel_with_proxy) |
38d7734b | 80 | return 0; |
382d851a | 81 | if (http->request->protocol == PROTO_CACHEOBJ) |
38d7734b | 82 | return 0; |
382d851a | 83 | if (http->accel) |
38d7734b | 84 | return 0; |
85 | return 1; | |
86 | } | |
87 | ||
b8d8561b | 88 | void |
382d851a | 89 | clientAccessCheck(void *data) |
f88bb09c | 90 | { |
382d851a | 91 | clientHttpRequest *http = data; |
92 | ConnStateData *conn = http->conn; | |
75e88d56 | 93 | char *browser; |
17a0a4ee | 94 | if (Config.onoff.ident_lookup && conn->ident.state == IDENT_NONE) { |
382d851a | 95 | identStart(-1, conn, clientAccessCheck); |
ba1d2afa | 96 | return; |
97 | } | |
382d851a | 98 | if (checkAccelOnly(http)) { |
99 | clientAccessCheckDone(0, http); | |
75e88d56 | 100 | return; |
f88bb09c | 101 | } |
f182d1c5 | 102 | browser = mime_get_header(http->request->headers, "User-Agent"); |
f1dc9b30 | 103 | http->acl_checklist = aclChecklistCreate(Config.accessList.http, |
382d851a | 104 | http->request, |
105 | conn->peer.sin_addr, | |
75e88d56 | 106 | browser, |
382d851a | 107 | conn->ident.ident); |
108 | aclNBCheck(http->acl_checklist, clientAccessCheckDone, http); | |
f88bb09c | 109 | } |
110 | ||
fc5d6f7f | 111 | static char * |
112 | clientConstructProxyAuthReply(clientHttpRequest * http) | |
113 | { | |
114 | LOCAL_ARRAY(char, buf, 8192); | |
115 | LOCAL_ARRAY(char, content, 4096); | |
116 | char *hdr; | |
117 | memset(buf, '\0', 8192); | |
118 | memset(content, '\0', 4096); | |
119 | snprintf(content, 4096, | |
120 | "<TITLE>Cache Access Denied</TITLE>\n" | |
121 | "<H2>Cache Access Denied</H2>\n" | |
122 | "<P>\n" | |
123 | "Sorry, you are not currently allowed to request:\n" | |
124 | "<PRE> %s</PRE>\n" | |
125 | "from this cache until you have authenticated yourself.\n" | |
126 | "\n<p>" | |
127 | "You need to use Netscape version 2.0 or greater, or Microsoft\n" | |
128 | "Internet Explorer 3.0 or an HTTP/1.1 compliant browser for this\n" | |
129 | "to work. Please contact the <a href=\"mailto:%s\">cache\n" | |
130 | "administrator</a> if you have difficulties authenticating\n" | |
131 | "yourself, or\n" | |
132 | "<a href=\"http://%s/cgi-bin/chpasswd.cgi\">change</a> your\n" | |
133 | "default password.\n" | |
134 | "<P>\n" | |
135 | "%s\n" | |
136 | "<HR>\n" | |
137 | "<ADDRESS>\n" | |
138 | "Generated by %s/%s@%s\n" | |
139 | "</ADDRESS>\n", | |
23d92c64 | 140 | http->uri, |
fc5d6f7f | 141 | Config.adminEmail, |
142 | getMyHostname(), | |
143 | Config.errHtmlText, | |
144 | appname, | |
145 | version_string, | |
146 | getMyHostname()); | |
147 | hdr = httpReplyHeader(1.0, | |
148 | HTTP_PROXY_AUTHENTICATION_REQUIRED, | |
149 | "text/html", | |
150 | strlen(content), | |
9daad6f0 | 151 | 0, |
fc5d6f7f | 152 | squid_curtime); |
153 | snprintf(buf, 8192, "%s%s\r\n%s", | |
154 | hdr, | |
155 | proxy_auth_line, | |
156 | content); | |
157 | return buf; | |
158 | } | |
159 | ||
23d92c64 | 160 | StoreEntry * |
f44af445 | 161 | clientCreateStoreEntry(clientHttpRequest * h, method_t m, int flags) |
79e0dc20 | 162 | { |
163 | StoreEntry *e; | |
40defb17 | 164 | request_t *r; |
165 | /* | |
166 | * For erroneous requests, we might not have a h->request, | |
167 | * so make a fake one. | |
168 | */ | |
169 | if (h->request == NULL) { | |
3f6c0fb2 | 170 | r = memAllocate(MEM_REQUEST_T, 1); |
40defb17 | 171 | r->method = m; |
172 | r->protocol = PROTO_NONE; | |
173 | h->request = requestLink(r); | |
174 | } | |
23d92c64 | 175 | e = storeCreateEntry(h->uri, h->log_uri, flags, m); |
79e0dc20 | 176 | storeClientListAdd(e, h); |
3f6c0fb2 | 177 | storeClientCopy(e, 0, 0, 4096, memAllocate(MEM_4K_BUF, 1), clientSendMoreData, h); |
79e0dc20 | 178 | return e; |
179 | } | |
180 | ||
181 | ||
b8d8561b | 182 | void |
75e88d56 | 183 | clientAccessCheckDone(int answer, void *data) |
f88bb09c | 184 | { |
382d851a | 185 | clientHttpRequest *http = data; |
e92d33a5 | 186 | char *redirectUrl = NULL; |
fc5d6f7f | 187 | char *buf; |
9b312a19 | 188 | ErrorState *err = NULL; |
23d92c64 | 189 | debug(33, 5) ("clientAccessCheckDone: '%s' answer=%d\n", http->uri, answer); |
382d851a | 190 | http->acl_checklist = NULL; |
fc5d6f7f | 191 | if (answer == ACCESS_ALLOWED) { |
23d92c64 | 192 | urlCanonical(http->request, http->uri); |
ce66013b | 193 | assert(http->redirect_state == REDIRECT_NONE); |
382d851a | 194 | http->redirect_state = REDIRECT_PENDING; |
195 | redirectStart(http, clientRedirectDone, http); | |
fc5d6f7f | 196 | } else if (answer == ACCESS_REQ_PROXY_AUTH) { |
197 | http->al.http.code = HTTP_PROXY_AUTHENTICATION_REQUIRED; | |
198 | http->log_type = LOG_TCP_DENIED; | |
199 | buf = clientConstructProxyAuthReply(http); | |
f44af445 | 200 | http->entry = clientCreateStoreEntry(http, http->request->method, 0); |
79e0dc20 | 201 | storeAppend(http->entry, buf, strlen(buf)); |
f88bb09c | 202 | } else { |
23d92c64 | 203 | debug(33, 5) ("Access Denied: %s\n", http->uri); |
79a15e0a | 204 | http->log_type = LOG_TCP_DENIED; |
f44af445 | 205 | http->entry = clientCreateStoreEntry(http, http->request->method, 0); |
85034133 | 206 | redirectUrl = aclGetDenyInfoUrl(&Config.denyInfoList, AclMatchedName); |
e92d33a5 | 207 | if (redirectUrl) { |
fe40a877 | 208 | err = errorCon(ERR_ACCESS_DENIED, HTTP_MOVED_TEMPORARILY); |
209 | err->request = requestLink(http->request); | |
210 | err->src_addr = http->conn->peer.sin_addr; | |
9b312a19 | 211 | err->redirect_url = xstrdup(redirectUrl); |
79e0dc20 | 212 | errorAppendEntry(http->entry, err); |
e92d33a5 | 213 | } else { |
d2989683 | 214 | /* NOTE: don't use HTTP_UNAUTHORIZED because then the |
437e2060 | 215 | * stupid browser wants us to authenticate */ |
fe40a877 | 216 | err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN); |
217 | err->request = requestLink(http->request); | |
218 | err->src_addr = http->conn->peer.sin_addr; | |
79e0dc20 | 219 | errorAppendEntry(http->entry, err); |
e92d33a5 | 220 | } |
f88bb09c | 221 | } |
222 | } | |
223 | ||
b8d8561b | 224 | static void |
225 | clientRedirectDone(void *data, char *result) | |
f88bb09c | 226 | { |
382d851a | 227 | clientHttpRequest *http = data; |
88738790 | 228 | size_t l; |
c0cdaf99 | 229 | request_t *new_request = NULL; |
382d851a | 230 | request_t *old_request = http->request; |
23d92c64 | 231 | debug(33, 5) ("clientRedirectDone: '%s' result=%s\n", http->uri, |
f88bb09c | 232 | result ? result : "NULL"); |
ce66013b | 233 | assert(http->redirect_state == REDIRECT_PENDING); |
382d851a | 234 | http->redirect_state = REDIRECT_DONE; |
c0cdaf99 | 235 | if (result) |
20cc1450 | 236 | new_request = urlParse(old_request->method, result); |
c0cdaf99 | 237 | if (new_request) { |
23d92c64 | 238 | safe_free(http->uri); |
88738790 | 239 | /* need to malloc because the URL returned by the redirector might |
240 | * not be big enough to append the local domain | |
241 | * -- David Lamkin drl@net-tel.co.uk */ | |
242 | l = strlen(result) + Config.appendDomainLen + 5; | |
23d92c64 | 243 | http->uri = xcalloc(l, 1); |
244 | xstrncpy(http->uri, result, l); | |
20cc1450 | 245 | new_request->http_ver = old_request->http_ver; |
16b9d3df | 246 | new_request->headers = xstrdup(old_request->headers); |
2357f74a | 247 | new_request->headers_sz = old_request->headers_sz; |
6cf028ab | 248 | if (old_request->body) { |
249 | new_request->body = xmalloc(old_request->body_sz); | |
250 | xmemcpy(new_request->body, old_request->body, old_request->body_sz); | |
251 | new_request->body_sz = old_request->body_sz; | |
252 | } | |
20cc1450 | 253 | requestUnlink(old_request); |
382d851a | 254 | http->request = requestLink(new_request); |
23d92c64 | 255 | urlCanonical(http->request, http->uri); |
f88bb09c | 256 | } |
50ddd7a4 | 257 | clientParseRequestHeaders(http); |
23d92c64 | 258 | fd_note(http->conn->fd, http->uri); |
fb63215a | 259 | clientProcessRequest(http); |
e81957b7 | 260 | } |
261 | ||
fb63215a | 262 | static void |
263 | clientProcessExpired(void *data) | |
620da955 | 264 | { |
382d851a | 265 | clientHttpRequest *http = data; |
23d92c64 | 266 | char *url = http->uri; |
620da955 | 267 | StoreEntry *entry = NULL; |
23d92c64 | 268 | debug(33, 3) ("clientProcessExpired: '%s'\n", http->uri); |
79a15e0a | 269 | EBIT_SET(http->request->flags, REQ_REFRESH); |
382d851a | 270 | http->old_entry = http->entry; |
620da955 | 271 | entry = storeCreateEntry(url, |
23d92c64 | 272 | http->log_uri, |
382d851a | 273 | http->request->flags, |
274 | http->request->method); | |
620da955 | 275 | /* NOTE, don't call storeLockObject(), storeCreateEntry() does it */ |
fe96bbe6 | 276 | storeClientListAdd(entry, http); |
277 | storeClientListAdd(http->old_entry, http); | |
382d851a | 278 | entry->lastmod = http->old_entry->lastmod; |
50ddd7a4 | 279 | debug(33, 5) ("clientProcessExpired: setting lmt = %d\n", |
620da955 | 280 | entry->lastmod); |
d1a43e28 | 281 | entry->refcount++; /* EXPIRED CASE */ |
382d851a | 282 | http->entry = entry; |
283 | http->out.offset = 0; | |
fb63215a | 284 | protoDispatch(http->conn->fd, http->entry, http->request); |
f990cccc | 285 | /* Register with storage manager to receive updates when data comes in. */ |
0e473d70 | 286 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 287 | debug(33, 0) ("clientProcessExpired: entry->swap_status == STORE_ABORTED\n"); |
d89d1fb6 | 288 | storeClientCopy(entry, |
fe96bbe6 | 289 | http->out.offset, |
d89d1fb6 | 290 | http->out.offset, |
291 | 4096, | |
3f6c0fb2 | 292 | memAllocate(MEM_4K_BUF, 1), |
1b02b5be | 293 | clientHandleIMSReply, |
d89d1fb6 | 294 | http); |
620da955 | 295 | } |
296 | ||
91f4d519 | 297 | static int |
298 | clientGetsOldEntry(StoreEntry * new_entry, StoreEntry * old_entry, request_t * request) | |
299 | { | |
300 | /* If the reply is anything but "Not Modified" then | |
301 | * we must forward it to the client */ | |
302 | if (new_entry->mem_obj->reply->code != 304) { | |
a3d5953d | 303 | debug(33, 5) ("clientGetsOldEntry: NO, reply=%d\n", new_entry->mem_obj->reply->code); |
91f4d519 | 304 | return 0; |
305 | } | |
306 | /* If the client did not send IMS in the request, then it | |
307 | * must get the old object, not this "Not Modified" reply */ | |
79a15e0a | 308 | if (!EBIT_TEST(request->flags, REQ_IMS)) { |
a3d5953d | 309 | debug(33, 5) ("clientGetsOldEntry: YES, no client IMS\n"); |
91f4d519 | 310 | return 1; |
311 | } | |
312 | /* If the client IMS time is prior to the entry LASTMOD time we | |
313 | * need to send the old object */ | |
314 | if (modifiedSince(old_entry, request)) { | |
a3d5953d | 315 | debug(33, 5) ("clientGetsOldEntry: YES, modified since %d\n", request->ims); |
91f4d519 | 316 | return 1; |
317 | } | |
a3d5953d | 318 | debug(33, 5) ("clientGetsOldEntry: NO, new one is fine\n"); |
91f4d519 | 319 | return 0; |
320 | } | |
321 | ||
322 | ||
52d4522b | 323 | static void |
1b02b5be | 324 | clientHandleIMSReply(void *data, char *buf, ssize_t size) |
620da955 | 325 | { |
382d851a | 326 | clientHttpRequest *http = data; |
382d851a | 327 | StoreEntry *entry = http->entry; |
620da955 | 328 | MemObject *mem = entry->mem_obj; |
9fb13bb6 | 329 | const char *url = storeUrl(entry); |
e92d33a5 | 330 | int unlink_request = 0; |
76cff1d4 | 331 | StoreEntry *oldentry; |
1b02b5be | 332 | debug(33, 3) ("clientHandleIMSReply: %s\n", url); |
3f6c0fb2 | 333 | memFree(MEM_4K_BUF, buf); |
76a5501f | 334 | buf = NULL; |
620da955 | 335 | /* unregister this handler */ |
070d29eb | 336 | if (size < 0 || entry->store_status == STORE_ABORTED) { |
1b02b5be | 337 | debug(33, 3) ("clientHandleIMSReply: ABORTED '%s'\n", url); |
c54e9052 | 338 | /* We have an existing entry, but failed to validate it */ |
accc6d47 | 339 | /* Its okay to send the old one anyway */ |
340 | http->log_type = LOG_TCP_REFRESH_FAIL_HIT; | |
341 | storeUnregister(entry, http); | |
342 | storeUnlockObject(entry); | |
343 | entry = http->entry = http->old_entry; | |
344 | entry->refcount++; | |
a18b8172 | 345 | } else if (mem->reply->code == 0) { |
1b02b5be | 346 | debug(33, 3) ("clientHandleIMSReply: Incomplete headers for '%s'\n", url); |
0e473d70 | 347 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 348 | debug(33, 0) ("clientHandleIMSReply: entry->swap_status == STORE_ABORTED\n"); |
d89d1fb6 | 349 | storeClientCopy(entry, |
fe96bbe6 | 350 | http->out.offset + size, |
d89d1fb6 | 351 | http->out.offset, |
352 | 4096, | |
3f6c0fb2 | 353 | memAllocate(MEM_4K_BUF, 1), |
1b02b5be | 354 | clientHandleIMSReply, |
d89d1fb6 | 355 | http); |
52d4522b | 356 | return; |
382d851a | 357 | } else if (clientGetsOldEntry(entry, http->old_entry, http->request)) { |
620da955 | 358 | /* We initiated the IMS request, the client is not expecting |
76cff1d4 | 359 | * 304, so put the good one back. First, make sure the old entry |
360 | * headers have been loaded from disk. */ | |
382d851a | 361 | oldentry = http->old_entry; |
382d851a | 362 | http->log_type = LOG_TCP_REFRESH_HIT; |
d89d1fb6 | 363 | if (oldentry->mem_obj->request == NULL) { |
364 | oldentry->mem_obj->request = requestLink(mem->request); | |
365 | unlink_request = 1; | |
e92d33a5 | 366 | } |
64763c37 | 367 | /* Don't memcpy() the whole reply structure here. For example, |
368 | * www.thegist.com (Netscape/1.13) returns a content-length for | |
369 | * 304's which seems to be the length of the 304 HEADERS!!! and | |
370 | * not the body they refer to. */ | |
371 | storeCopyNotModifiedReplyHeaders(entry->mem_obj, oldentry->mem_obj); | |
d89d1fb6 | 372 | storeTimestampsSet(oldentry); |
382d851a | 373 | storeUnregister(entry, http); |
6d54aea8 | 374 | storeUnlockObject(entry); |
382d851a | 375 | entry = http->entry = oldentry; |
41fad779 | 376 | entry->timestamp = squid_curtime; |
657266fe | 377 | if (unlink_request) { |
e92d33a5 | 378 | requestUnlink(entry->mem_obj->request); |
657266fe | 379 | entry->mem_obj->request = NULL; |
380 | } | |
620da955 | 381 | } else { |
382 | /* the client can handle this reply, whatever it is */ | |
382d851a | 383 | http->log_type = LOG_TCP_REFRESH_MISS; |
d1a43e28 | 384 | if (mem->reply->code == 304) { |
382d851a | 385 | http->old_entry->timestamp = squid_curtime; |
386 | http->old_entry->refcount++; | |
387 | http->log_type = LOG_TCP_REFRESH_HIT; | |
d1a43e28 | 388 | } |
382d851a | 389 | storeUnregister(http->old_entry, http); |
390 | storeUnlockObject(http->old_entry); | |
620da955 | 391 | } |
382d851a | 392 | http->old_entry = NULL; /* done with old_entry */ |
e8479da9 | 393 | /* use clientCacheHit() here as the callback because we might |
6a54c60e | 394 | * be swapping in from disk, and the file might not really be |
395 | * there */ | |
0e473d70 | 396 | if (entry->store_status == STORE_ABORTED) { |
6cf028ab | 397 | debug(33, 0) ("clientHandleIMSReply: IMS swapin failed on aborted object\n"); |
398 | http->log_type = LOG_TCP_SWAPFAIL_MISS; | |
399 | clientProcessMiss(http); | |
400 | return; | |
401 | } | |
d89d1fb6 | 402 | storeClientCopy(entry, |
fe96bbe6 | 403 | http->out.offset, |
d89d1fb6 | 404 | http->out.offset, |
405 | 4096, | |
3f6c0fb2 | 406 | memAllocate(MEM_4K_BUF, 1), |
e8479da9 | 407 | clientCacheHit, |
d89d1fb6 | 408 | http); |
620da955 | 409 | } |
91f4d519 | 410 | |
411 | int | |
304d07cb | 412 | modifiedSince(StoreEntry * entry, request_t * request) |
91f4d519 | 413 | { |
414 | int object_length; | |
415 | MemObject *mem = entry->mem_obj; | |
9fb13bb6 | 416 | debug(33, 3) ("modifiedSince: '%s'\n", storeUrl(entry)); |
91f4d519 | 417 | if (entry->lastmod < 0) |
418 | return 1; | |
419 | /* Find size of the object */ | |
1790d392 | 420 | if (mem->reply->content_length >= 0) |
91f4d519 | 421 | object_length = mem->reply->content_length; |
422 | else | |
423 | object_length = entry->object_len - mem->reply->hdr_sz; | |
424 | if (entry->lastmod > request->ims) { | |
a3d5953d | 425 | debug(33, 3) ("--> YES: entry newer than client\n"); |
91f4d519 | 426 | return 1; |
427 | } else if (entry->lastmod < request->ims) { | |
a3d5953d | 428 | debug(33, 3) ("--> NO: entry older than client\n"); |
91f4d519 | 429 | return 0; |
430 | } else if (request->imslen < 0) { | |
a3d5953d | 431 | debug(33, 3) ("--> NO: same LMT, no client length\n"); |
91f4d519 | 432 | return 0; |
433 | } else if (request->imslen == object_length) { | |
a3d5953d | 434 | debug(33, 3) ("--> NO: same LMT, same length\n"); |
91f4d519 | 435 | return 0; |
436 | } else { | |
a3d5953d | 437 | debug(33, 3) ("--> YES: same LMT, different length\n"); |
91f4d519 | 438 | return 1; |
439 | } | |
440 | } | |
b3b64e58 | 441 | |
442 | char * | |
382d851a | 443 | clientConstructTraceEcho(clientHttpRequest * http) |
b3b64e58 | 444 | { |
445 | LOCAL_ARRAY(char, line, 256); | |
446 | LOCAL_ARRAY(char, buf, 8192); | |
447 | size_t len; | |
448 | memset(buf, '\0', 8192); | |
042461c3 | 449 | snprintf(buf, 8192, "HTTP/1.0 200 OK\r\n"); |
56878878 | 450 | snprintf(line, 256, "Date: %s\r\n", mkrfc1123(squid_curtime)); |
b3b64e58 | 451 | strcat(buf, line); |
042461c3 | 452 | snprintf(line, 256, "Server: Squid/%s\r\n", SQUID_VERSION); |
b3b64e58 | 453 | strcat(buf, line); |
042461c3 | 454 | snprintf(line, 256, "Content-Type: message/http\r\n"); |
b3b64e58 | 455 | strcat(buf, line); |
456 | strcat(buf, "\r\n"); | |
457 | len = strlen(buf); | |
382d851a | 458 | httpBuildRequestHeader(http->request, |
459 | http->request, | |
b3b64e58 | 460 | NULL, /* entry */ |
b3b64e58 | 461 | NULL, /* in_len */ |
462 | buf + len, | |
463 | 8192 - len, | |
603a02fd | 464 | http->conn->fd, |
465 | 0); /* flags */ | |
382d851a | 466 | http->log_type = LOG_TCP_MISS; |
4869e8a1 | 467 | http->http_code = HTTP_OK; |
b3b64e58 | 468 | return buf; |
469 | } | |
a90eae18 | 470 | |
471 | void | |
382d851a | 472 | clientPurgeRequest(clientHttpRequest * http) |
a90eae18 | 473 | { |
382d851a | 474 | int fd = http->conn->fd; |
9b312a19 | 475 | char *msg; |
a90eae18 | 476 | StoreEntry *entry; |
9b312a19 | 477 | ErrorState *err = NULL; |
9fb13bb6 | 478 | const cache_key *k; |
9b312a19 | 479 | debug(33, 3) ("Config.onoff.enable_purge = %d\n", Config.onoff.enable_purge); |
17a0a4ee | 480 | if (!Config.onoff.enable_purge) { |
79a15e0a | 481 | http->log_type = LOG_TCP_DENIED; |
fe40a877 | 482 | err = errorCon(ERR_ACCESS_DENIED, HTTP_FORBIDDEN); |
9b312a19 | 483 | err->request = requestLink(http->request); |
484 | err->src_addr = http->conn->peer.sin_addr; | |
f44af445 | 485 | http->entry = clientCreateStoreEntry(http, http->request->method, 0); |
79e0dc20 | 486 | errorAppendEntry(http->entry, err); |
a90eae18 | 487 | return; |
488 | } | |
382d851a | 489 | http->log_type = LOG_TCP_MISS; |
23d92c64 | 490 | k = storeKeyPublic(http->uri, METHOD_GET); |
9fb13bb6 | 491 | if ((entry = storeGet(k)) == NULL) { |
9b312a19 | 492 | http->http_code = HTTP_NOT_FOUND; |
a90eae18 | 493 | } else { |
494 | storeRelease(entry); | |
9b312a19 | 495 | http->http_code = HTTP_OK; |
a90eae18 | 496 | } |
9b312a19 | 497 | msg = httpReplyHeader(1.0, http->http_code, NULL, 0, 0, -1); |
69e81830 | 498 | if ((int) strlen(msg) < 8190) |
9b312a19 | 499 | strcat(msg, "\r\n"); |
500 | comm_write(fd, xstrdup(msg), strlen(msg), clientWriteComplete, http, xfree); | |
a90eae18 | 501 | } |
e33ba616 | 502 | |
503 | int | |
ea3a2a69 | 504 | checkNegativeHit(StoreEntry * e) |
e33ba616 | 505 | { |
79a15e0a | 506 | if (!EBIT_TEST(e->flag, ENTRY_NEGCACHED)) |
ea3a2a69 | 507 | return 0; |
508 | if (e->expires <= squid_curtime) | |
509 | return 0; | |
510 | if (e->store_status != STORE_OK) | |
511 | return 0; | |
512 | return 1; | |
e33ba616 | 513 | } |
7a2f978b | 514 | |
a7c05555 | 515 | void |
0e473d70 | 516 | clientUpdateCounters(clientHttpRequest * http) |
a7c05555 | 517 | { |
0e473d70 | 518 | Counter.client_http.requests++; |
519 | kb_incr(&Counter.client_http.kbytes_in, http->req_sz); | |
520 | kb_incr(&Counter.client_http.kbytes_out, http->out.size); | |
521 | if (isTcpHit(http->log_type)) { | |
522 | Counter.client_http.hits++; | |
523 | kb_incr(&Counter.client_http.hit_kbytes_out, http->out.size); | |
524 | } | |
525 | if (http->request->err_type != ERR_NONE) | |
526 | Counter.client_http.errors++; | |
a7c05555 | 527 | } |
528 | ||
7a2f978b | 529 | static void |
530 | httpRequestFree(void *data) | |
531 | { | |
532 | clientHttpRequest *http = data; | |
533 | clientHttpRequest **H; | |
534 | ConnStateData *conn = http->conn; | |
535 | StoreEntry *entry = http->entry; | |
536 | request_t *request = http->request; | |
537 | MemObject *mem = NULL; | |
538 | debug(12, 3) ("httpRequestFree: %s\n", storeUrl(entry)); | |
1b02b5be | 539 | if (!clientCheckTransferDone(http)) { |
7a2f978b | 540 | if (entry) |
541 | storeUnregister(entry, http); /* unregister BEFORE abort */ | |
542 | CheckQuickAbort(http); | |
543 | entry = http->entry; /* reset, IMS might have changed it */ | |
544 | if (entry && entry->ping_status == PING_WAITING) | |
545 | storeReleaseRequest(entry); | |
79d39a72 | 546 | protoUnregister(entry, request); |
7a2f978b | 547 | } |
548 | assert(http->log_type < LOG_TYPE_MAX); | |
549 | if (entry) | |
550 | mem = entry->mem_obj; | |
551 | if (http->out.size || http->log_type) { | |
552 | http->al.icp.opcode = 0; | |
23d92c64 | 553 | http->al.url = http->uri; |
7a2f978b | 554 | if (mem) { |
555 | http->al.http.code = mem->reply->code; | |
556 | http->al.http.content_type = mem->reply->content_type; | |
557 | } | |
558 | http->al.cache.caddr = conn->log_addr; | |
559 | http->al.cache.size = http->out.size; | |
560 | http->al.cache.code = http->log_type; | |
561 | http->al.cache.msec = tvSubMsec(http->start, current_time); | |
562 | http->al.cache.ident = conn->ident.ident; | |
563 | if (request) { | |
564 | http->al.http.method = request->method; | |
565 | http->al.headers.request = request->headers; | |
566 | http->al.hier = request->hier; | |
567 | } | |
568 | accessLogLog(&http->al); | |
a7c05555 | 569 | clientUpdateCounters(http); |
7a2f978b | 570 | clientdbUpdate(conn->peer.sin_addr, http->log_type, PROTO_HTTP); |
571 | } | |
572 | if (http->redirect_state == REDIRECT_PENDING) | |
23d92c64 | 573 | redirectUnregister(http->uri, http); |
7a2f978b | 574 | if (http->acl_checklist) |
575 | aclChecklistFree(http->acl_checklist); | |
88aad2e5 | 576 | if (request) |
c24fca87 | 577 | checkFailureRatio(request->err_type, http->al.hier.code); |
23d92c64 | 578 | safe_free(http->uri); |
579 | safe_free(http->log_uri); | |
7a2f978b | 580 | safe_free(http->al.headers.reply); |
581 | if (entry) { | |
582 | http->entry = NULL; | |
583 | storeUnregister(entry, http); | |
584 | storeUnlockObject(entry); | |
585 | } | |
586 | /* old_entry might still be set if we didn't yet get the reply | |
1b02b5be | 587 | * code in clientHandleIMSReply() */ |
7a2f978b | 588 | if (http->old_entry) { |
589 | storeUnregister(http->old_entry, http); | |
590 | storeUnlockObject(http->old_entry); | |
591 | http->old_entry = NULL; | |
592 | } | |
593 | requestUnlink(http->request); | |
594 | assert(http != http->next); | |
595 | assert(http->conn->chr != NULL); | |
596 | H = &http->conn->chr; | |
597 | while (*H) { | |
598 | if (*H == http) | |
599 | break; | |
600 | H = &(*H)->next; | |
601 | } | |
602 | assert(*H != NULL); | |
603 | *H = http->next; | |
604 | http->next = NULL; | |
605 | cbdataFree(http); | |
606 | } | |
607 | ||
608 | /* This is a handler normally called by comm_close() */ | |
609 | static void | |
610 | connStateFree(int fd, void *data) | |
611 | { | |
612 | ConnStateData *connState = data; | |
613 | clientHttpRequest *http; | |
614 | debug(12, 3) ("connStateFree: FD %d\n", fd); | |
615 | assert(connState != NULL); | |
79d39a72 | 616 | while ((http = connState->chr) != NULL) { |
7a2f978b | 617 | assert(http->conn == connState); |
618 | assert(connState->chr != connState->chr->next); | |
619 | httpRequestFree(http); | |
620 | } | |
621 | if (connState->ident.fd > -1) | |
622 | comm_close(connState->ident.fd); | |
623 | safe_free(connState->in.buf); | |
624 | meta_data.misc -= connState->in.size; | |
625 | pconnHistCount(0, connState->nrequests); | |
626 | cbdataFree(connState); | |
627 | } | |
628 | ||
629 | static void | |
50ddd7a4 | 630 | clientParseRequestHeaders(clientHttpRequest * http) |
7a2f978b | 631 | { |
632 | request_t *request = http->request; | |
633 | char *request_hdr = request->headers; | |
634 | char *t = NULL; | |
635 | request->ims = -2; | |
636 | request->imslen = -1; | |
637 | if ((t = mime_get_header(request_hdr, "If-Modified-Since"))) { | |
79a15e0a | 638 | EBIT_SET(request->flags, REQ_IMS); |
7a2f978b | 639 | request->ims = parse_rfc1123(t); |
640 | while ((t = strchr(t, ';'))) { | |
641 | for (t++; isspace(*t); t++); | |
642 | if (strncasecmp(t, "length=", 7) == 0) | |
643 | request->imslen = atoi(t + 7); | |
644 | } | |
645 | } | |
646 | if ((t = mime_get_header(request_hdr, "Pragma"))) { | |
647 | if (!strcasecmp(t, "no-cache")) | |
79a15e0a | 648 | EBIT_SET(request->flags, REQ_NOCACHE); |
7a2f978b | 649 | } |
650 | if (mime_get_header(request_hdr, "Range")) { | |
79a15e0a | 651 | EBIT_SET(request->flags, REQ_NOCACHE); |
652 | EBIT_SET(request->flags, REQ_RANGE); | |
7a2f978b | 653 | } else if (mime_get_header(request_hdr, "Request-Range")) { |
79a15e0a | 654 | EBIT_SET(request->flags, REQ_NOCACHE); |
655 | EBIT_SET(request->flags, REQ_RANGE); | |
7a2f978b | 656 | } |
657 | if (mime_get_header(request_hdr, "Authorization")) | |
79a15e0a | 658 | EBIT_SET(request->flags, REQ_AUTH); |
7a2f978b | 659 | if (request->login[0] != '\0') |
79a15e0a | 660 | EBIT_SET(request->flags, REQ_AUTH); |
7a2f978b | 661 | if ((t = mime_get_header(request_hdr, "Proxy-Connection"))) { |
662 | if (!strcasecmp(t, "Keep-Alive")) | |
79a15e0a | 663 | EBIT_SET(request->flags, REQ_PROXY_KEEPALIVE); |
7a2f978b | 664 | } |
665 | if ((t = mime_get_header(request_hdr, "Via"))) | |
666 | if (strstr(t, ThisCache)) { | |
667 | if (!http->accel) { | |
668 | debug(12, 1) ("WARNING: Forwarding loop detected for '%s'\n", | |
23d92c64 | 669 | http->uri); |
7a2f978b | 670 | debug(12, 1) ("--> %s\n", t); |
671 | } | |
79a15e0a | 672 | EBIT_SET(request->flags, REQ_LOOPDETECT); |
7a2f978b | 673 | } |
674 | #if USE_USERAGENT_LOG | |
675 | if ((t = mime_get_header(request_hdr, "User-Agent"))) | |
676 | logUserAgent(fqdnFromAddr(http->conn->peer.sin_addr), t); | |
677 | #endif | |
678 | request->max_age = -1; | |
679 | if ((t = mime_get_header(request_hdr, "Cache-control"))) { | |
680 | if (!strncasecmp(t, "Max-age=", 8)) | |
681 | request->max_age = atoi(t + 8); | |
682 | } | |
683 | if (request->method == METHOD_TRACE) { | |
684 | if ((t = mime_get_header(request_hdr, "Max-Forwards"))) | |
685 | request->max_forwards = atoi(t); | |
686 | } | |
50ddd7a4 | 687 | if (clientCachable(http)) |
688 | EBIT_SET(request->flags, REQ_CACHABLE); | |
689 | if (clientHierarchical(http)) | |
690 | EBIT_SET(request->flags, REQ_HIERARCHICAL); | |
691 | debug(12, 5) ("clientParseRequestHeaders: REQ_NOCACHE = %s\n", | |
692 | EBIT_TEST(request->flags, REQ_NOCACHE) ? "SET" : "NOT SET"); | |
693 | debug(12, 5) ("clientParseRequestHeaders: REQ_CACHABLE = %s\n", | |
694 | EBIT_TEST(request->flags, REQ_CACHABLE) ? "SET" : "NOT SET"); | |
695 | debug(12, 5) ("clientParseRequestHeaders: REQ_HIERARCHICAL = %s\n", | |
696 | EBIT_TEST(request->flags, REQ_HIERARCHICAL) ? "SET" : "NOT SET"); | |
7a2f978b | 697 | } |
698 | ||
699 | static int | |
50ddd7a4 | 700 | clientCachable(clientHttpRequest * http) |
7a2f978b | 701 | { |
23d92c64 | 702 | const char *url = http->uri; |
7a2f978b | 703 | request_t *req = http->request; |
704 | method_t method = req->method; | |
705 | const wordlist *p; | |
706 | for (p = Config.cache_stoplist; p; p = p->next) { | |
707 | if (strstr(url, p->key)) | |
708 | return 0; | |
709 | } | |
710 | if (Config.cache_stop_relist) | |
711 | if (aclMatchRegex(Config.cache_stop_relist, url)) | |
712 | return 0; | |
713 | if (req->protocol == PROTO_HTTP) | |
714 | return httpCachable(method); | |
715 | /* FTP is always cachable */ | |
716 | if (req->protocol == PROTO_GOPHER) | |
717 | return gopherCachable(url); | |
718 | if (req->protocol == PROTO_WAIS) | |
719 | return 0; | |
720 | if (method == METHOD_CONNECT) | |
721 | return 0; | |
722 | if (method == METHOD_TRACE) | |
723 | return 0; | |
724 | if (req->protocol == PROTO_CACHEOBJ) | |
725 | return 0; | |
726 | return 1; | |
727 | } | |
728 | ||
729 | /* Return true if we can query our neighbors for this object */ | |
730 | static int | |
50ddd7a4 | 731 | clientHierarchical(clientHttpRequest * http) |
7a2f978b | 732 | { |
23d92c64 | 733 | const char *url = http->uri; |
7a2f978b | 734 | request_t *request = http->request; |
735 | method_t method = request->method; | |
736 | const wordlist *p = NULL; | |
737 | ||
738 | /* IMS needs a private key, so we can use the hierarchy for IMS only | |
739 | * if our neighbors support private keys */ | |
79a15e0a | 740 | if (EBIT_TEST(request->flags, REQ_IMS) && !neighbors_do_private_keys) |
7a2f978b | 741 | return 0; |
79a15e0a | 742 | if (EBIT_TEST(request->flags, REQ_AUTH)) |
7a2f978b | 743 | return 0; |
744 | if (method == METHOD_TRACE) | |
745 | return 1; | |
746 | if (method != METHOD_GET) | |
747 | return 0; | |
748 | /* scan hierarchy_stoplist */ | |
749 | for (p = Config.hierarchy_stoplist; p; p = p->next) | |
750 | if (strstr(url, p->key)) | |
751 | return 0; | |
79a15e0a | 752 | if (EBIT_TEST(request->flags, REQ_LOOPDETECT)) |
7a2f978b | 753 | return 0; |
754 | if (request->protocol == PROTO_HTTP) | |
755 | return httpCachable(method); | |
756 | if (request->protocol == PROTO_GOPHER) | |
757 | return gopherCachable(url); | |
758 | if (request->protocol == PROTO_WAIS) | |
759 | return 0; | |
760 | if (request->protocol == PROTO_CACHEOBJ) | |
761 | return 0; | |
762 | return 1; | |
763 | } | |
764 | ||
a7c05555 | 765 | static int |
7a2f978b | 766 | isTcpHit(log_type code) |
767 | { | |
768 | /* this should be a bitmap for better optimization */ | |
769 | if (code == LOG_TCP_HIT) | |
770 | return 1; | |
771 | if (code == LOG_TCP_IMS_HIT) | |
772 | return 1; | |
773 | if (code == LOG_TCP_REFRESH_FAIL_HIT) | |
774 | return 1; | |
775 | if (code == LOG_TCP_REFRESH_HIT) | |
776 | return 1; | |
777 | if (code == LOG_TCP_NEGATIVE_HIT) | |
778 | return 1; | |
779 | if (code == LOG_TCP_MEM_HIT) | |
780 | return 1; | |
781 | return 0; | |
782 | } | |
783 | ||
784 | static void | |
785 | clientAppendReplyHeader(char *hdr, const char *line, size_t * sz, size_t max) | |
786 | { | |
787 | size_t n = *sz + strlen(line) + 2; | |
788 | if (n >= max) | |
789 | return; | |
790 | strcpy(hdr + (*sz), line); | |
791 | strcat(hdr + (*sz), crlf); | |
792 | *sz = n; | |
793 | } | |
794 | ||
795 | size_t | |
796 | clientBuildReplyHeader(clientHttpRequest * http, | |
797 | char *hdr_in, | |
798 | size_t * in_len, | |
799 | char *hdr_out, | |
800 | size_t out_sz) | |
801 | { | |
ab013258 | 802 | LOCAL_ARRAY(char, no_forward, 1024); |
7a2f978b | 803 | char *xbuf; |
804 | char *ybuf; | |
805 | char *t = NULL; | |
806 | char *end = NULL; | |
807 | size_t len = 0; | |
808 | size_t hdr_len = 0; | |
809 | size_t l; | |
810 | end = mime_headers_end(hdr_in); | |
811 | if (end == NULL) { | |
812 | debug(12, 3) ("clientBuildReplyHeader: DIDN'T FIND END-OF-HEADERS\n"); | |
7a2f978b | 813 | return 0; |
814 | } | |
3f6c0fb2 | 815 | xbuf = memAllocate(MEM_4K_BUF, 1); |
816 | ybuf = memAllocate(MEM_4K_BUF, 1); | |
7a2f978b | 817 | for (t = hdr_in; t < end; t += strcspn(t, crlf), t += strspn(t, crlf)) { |
818 | hdr_len = t - hdr_in; | |
819 | l = strcspn(t, crlf) + 1; | |
820 | xstrncpy(xbuf, t, l > 4096 ? 4096 : l); | |
7a2f978b | 821 | #if 0 |
822 | if (strncasecmp(xbuf, "Accept-Ranges:", 14) == 0) | |
823 | continue; | |
824 | if (strncasecmp(xbuf, "Etag:", 5) == 0) | |
825 | continue; | |
826 | #endif | |
827 | if (strncasecmp(xbuf, "Proxy-Connection:", 17) == 0) | |
828 | continue; | |
ab013258 | 829 | if (strncasecmp(xbuf, "Connection:", 11) == 0) { |
830 | handleConnectionHeader(0, no_forward, &xbuf[11]); | |
7a2f978b | 831 | continue; |
ab013258 | 832 | } |
7a2f978b | 833 | if (strncasecmp(xbuf, "Keep-Alive:", 11) == 0) |
834 | continue; | |
835 | if (strncasecmp(xbuf, "Set-Cookie:", 11) == 0) | |
836 | if (isTcpHit(http->log_type)) | |
837 | continue; | |
ab013258 | 838 | if (!handleConnectionHeader(1, no_forward, xbuf)) |
839 | clientAppendReplyHeader(hdr_out, xbuf, &len, out_sz - 512); | |
7a2f978b | 840 | } |
841 | hdr_len = end - hdr_in; | |
842 | /* Append X-Cache: */ | |
843 | snprintf(ybuf, 4096, "X-Cache: %s", isTcpHit(http->log_type) ? "HIT" : "MISS"); | |
844 | clientAppendReplyHeader(hdr_out, ybuf, &len, out_sz); | |
845 | /* Append Proxy-Connection: */ | |
79a15e0a | 846 | if (EBIT_TEST(http->request->flags, REQ_PROXY_KEEPALIVE)) { |
7a2f978b | 847 | snprintf(ybuf, 4096, "Proxy-Connection: Keep-Alive"); |
848 | clientAppendReplyHeader(hdr_out, ybuf, &len, out_sz); | |
849 | } | |
850 | clientAppendReplyHeader(hdr_out, null_string, &len, out_sz); | |
851 | if (in_len) | |
852 | *in_len = hdr_len; | |
853 | if ((l = strlen(hdr_out)) != len) { | |
854 | debug_trap("clientBuildReplyHeader: size mismatch"); | |
855 | len = l; | |
856 | } | |
857 | debug(12, 3) ("clientBuildReplyHeader: OUTPUT:\n%s\n", hdr_out); | |
3f6c0fb2 | 858 | memFree(MEM_4K_BUF, xbuf); |
859 | memFree(MEM_4K_BUF, ybuf); | |
7a2f978b | 860 | return len; |
861 | } | |
862 | ||
4c323c5f | 863 | static void |
7a2f978b | 864 | clientCacheHit(void *data, char *buf, ssize_t size) |
865 | { | |
866 | clientHttpRequest *http = data; | |
867 | if (size >= 0) { | |
868 | clientSendMoreData(data, buf, size); | |
869 | } else if (http->entry == NULL) { | |
870 | debug(12, 3) ("clientCacheHit: request aborted\n"); | |
871 | } else { | |
872 | /* swap in failure */ | |
6cf028ab | 873 | debug(12, 1) ("KARMA: clientCacheHit fail\n"); |
7a2f978b | 874 | http->log_type = LOG_TCP_SWAPFAIL_MISS; |
6cf028ab | 875 | if (http->entry) |
876 | storeRelease(http->entry); | |
fb63215a | 877 | clientProcessMiss(http); |
7a2f978b | 878 | } |
879 | } | |
880 | ||
4c323c5f | 881 | static void |
7a2f978b | 882 | clientSendMoreData(void *data, char *buf, ssize_t size) |
883 | { | |
884 | clientHttpRequest *http = data; | |
885 | StoreEntry *entry = http->entry; | |
886 | ConnStateData *conn = http->conn; | |
887 | int fd = conn->fd; | |
888 | char *p = NULL; | |
889 | size_t hdrlen; | |
890 | size_t l = 0; | |
891 | size_t writelen; | |
892 | char *newbuf; | |
3f6c0fb2 | 893 | FREE *freefunc = memFree4K; |
7a2f978b | 894 | int hack = 0; |
895 | char C = '\0'; | |
896 | assert(size <= SM_PAGE_SIZE); | |
40defb17 | 897 | assert(http->request != NULL); |
ab013258 | 898 | debug(12, 5) ("clientSendMoreData: FD %d '%s', out.offset=%d \n", |
7a2f978b | 899 | fd, storeUrl(entry), http->out.offset); |
900 | if (conn->chr != http) { | |
901 | /* there is another object in progress, defer this one */ | |
a43b3862 | 902 | debug(0, 0) ("clientSendMoreData: Deferring %s\n", storeUrl(entry)); |
7a2f978b | 903 | freefunc(buf); |
904 | return; | |
905 | } else if (entry->store_status == STORE_ABORTED) { | |
906 | freefunc(buf); | |
907 | return; | |
908 | } else if (size < 0) { | |
909 | freefunc(buf); | |
910 | return; | |
911 | } else if (size == 0) { | |
912 | clientWriteComplete(fd, NULL, 0, DISK_OK, http); | |
913 | freefunc(buf); | |
914 | return; | |
915 | } | |
916 | writelen = size; | |
40defb17 | 917 | if (http->out.offset == 0) { |
7a2f978b | 918 | if (Config.onoff.log_mime_hdrs) { |
919 | if ((p = mime_headers_end(buf))) { | |
920 | safe_free(http->al.headers.reply); | |
921 | http->al.headers.reply = xcalloc(1 + p - buf, 1); | |
922 | xstrncpy(http->al.headers.reply, buf, p - buf); | |
923 | } | |
924 | } | |
925 | /* make sure 'buf' is null terminated somewhere */ | |
926 | if (size == SM_PAGE_SIZE) { | |
927 | hack = 1; | |
928 | size--; | |
929 | C = *(buf + size); | |
930 | } | |
931 | *(buf + size) = '\0'; | |
3f6c0fb2 | 932 | newbuf = memAllocate(MEM_8K_BUF, 1); |
7a2f978b | 933 | hdrlen = 0; |
ab013258 | 934 | |
7a2f978b | 935 | l = clientBuildReplyHeader(http, buf, &hdrlen, newbuf, 8192); |
936 | if (hack) | |
937 | *(buf + size++) = C; | |
938 | if (l != 0) { | |
939 | writelen = l + size - hdrlen; | |
940 | assert(writelen <= 8192); | |
941 | /* | |
942 | * l is the length of the new headers in newbuf | |
943 | * hdrlen is the length of the old headers in buf | |
944 | * size - hdrlen is the amount of body in buf | |
945 | */ | |
946 | debug(12, 3) ("clientSendMoreData: Appending %d bytes after headers\n", | |
947 | (int) (size - hdrlen)); | |
6cf028ab | 948 | if (((size - hdrlen) + l) > 8192) { |
0e473d70 | 949 | debug(0, 0) ("Size, hdrlen, l %d, %d, %d\n", size, hdrlen, l); |
6cf028ab | 950 | return; |
951 | } | |
7a2f978b | 952 | xmemcpy(newbuf + l, buf + hdrlen, size - hdrlen); |
953 | /* replace buf with newbuf */ | |
954 | freefunc(buf); | |
955 | buf = newbuf; | |
3f6c0fb2 | 956 | freefunc = memFree8K; |
7a2f978b | 957 | newbuf = NULL; |
958 | } else { | |
3f6c0fb2 | 959 | memFree(MEM_8K_BUF, newbuf); |
7a2f978b | 960 | newbuf = NULL; |
961 | if (size < SM_PAGE_SIZE && entry->store_status == STORE_PENDING) { | |
962 | /* wait for more to arrive */ | |
963 | storeClientCopy(entry, | |
964 | http->out.offset + size, | |
965 | http->out.offset, | |
966 | SM_PAGE_SIZE, | |
967 | buf, | |
968 | clientSendMoreData, | |
969 | http); | |
970 | return; | |
971 | } | |
972 | } | |
973 | } | |
974 | http->out.offset += size; | |
40defb17 | 975 | /* |
976 | * ick, this is gross | |
977 | */ | |
7a2f978b | 978 | if (http->request->method == METHOD_HEAD) { |
979 | if ((p = mime_headers_end(buf))) { | |
980 | *p = '\0'; | |
981 | writelen = p - buf; | |
982 | /* force end */ | |
f68b5d81 | 983 | if (entry->store_status == STORE_PENDING) |
c24fca87 | 984 | http->out.offset = entry->mem_obj->inmem_hi; |
f68b5d81 | 985 | else |
c24fca87 | 986 | http->out.offset = entry->object_len; |
7a2f978b | 987 | } |
988 | } | |
989 | comm_write(fd, buf, writelen, clientWriteComplete, http, freefunc); | |
990 | } | |
991 | ||
fc5d6f7f | 992 | static void |
79a15e0a | 993 | clientWriteComplete(int fd, char *bufnotused, size_t size, int errflag, void *data) |
7a2f978b | 994 | { |
995 | clientHttpRequest *http = data; | |
996 | ConnStateData *conn; | |
997 | StoreEntry *entry = http->entry; | |
b34ed725 | 998 | int done; |
7a2f978b | 999 | http->out.size += size; |
1000 | debug(12, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n", | |
1001 | fd, size, errflag, http->out.offset, entry->object_len); | |
1002 | if (errflag) { | |
1003 | CheckQuickAbort(http); | |
7a2f978b | 1004 | comm_close(fd); |
1005 | } else if (entry->store_status == STORE_ABORTED) { | |
7a2f978b | 1006 | comm_close(fd); |
a200bbd2 | 1007 | } else if ((done = clientCheckTransferDone(http)) != 0 || size == 0) { |
7a2f978b | 1008 | debug(12, 5) ("clientWriteComplete: FD %d transfer is DONE\n", fd); |
1009 | /* We're finished case */ | |
b34ed725 | 1010 | if (http->entry->mem_obj->reply->content_length < 0 || !done || |
1011 | EBIT_TEST(entry->flag, ENTRY_BAD_LENGTH)) { | |
1012 | /* | |
1013 | * Client connection closed due to unknown or invalid | |
1014 | * content length. Persistent connection is not possible. | |
1015 | * This catches most cases, but probably not all. | |
1016 | */ | |
7a2f978b | 1017 | comm_close(fd); |
79a15e0a | 1018 | } else if (EBIT_TEST(http->request->flags, REQ_PROXY_KEEPALIVE)) { |
7a2f978b | 1019 | debug(12, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd); |
1020 | conn = http->conn; | |
b34ed725 | 1021 | conn->defer.until = 0; /* Kick it to read a new request */ |
7a2f978b | 1022 | httpRequestFree(http); |
79d39a72 | 1023 | if ((http = conn->chr) != NULL) { |
7a2f978b | 1024 | debug(12, 1) ("clientWriteComplete: FD %d Sending next request\n", fd); |
6cf028ab | 1025 | if (!storeClientCopyPending(http->entry, http)) { |
0e473d70 | 1026 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 1027 | debug(33, 0) ("clientWriteComplete: entry->swap_status == STORE_ABORTED\n"); |
8daca701 | 1028 | storeClientCopy(http->entry, |
1029 | http->out.offset, | |
1030 | http->out.offset, | |
1031 | SM_PAGE_SIZE, | |
3f6c0fb2 | 1032 | memAllocate(MEM_4K_BUF, 1), |
8daca701 | 1033 | clientSendMoreData, |
1034 | http); | |
6cf028ab | 1035 | } |
7a2f978b | 1036 | } else { |
0ca54a51 | 1037 | debug(12, 5) ("clientWriteComplete: FD %d reading next request\n", fd); |
7a2f978b | 1038 | fd_note(fd, "Reading next request"); |
0ca54a51 | 1039 | /* |
1040 | * Set the timeout BEFORE calling clientReadRequest(). | |
1041 | */ | |
7a2f978b | 1042 | commSetTimeout(fd, 15, requestTimeout, conn); |
47130615 | 1043 | clientReadRequest(fd, conn); /* Read next request */ |
0ca54a51 | 1044 | /* |
1045 | * Note, the FD may be closed at this point. | |
1046 | */ | |
7a2f978b | 1047 | } |
1048 | } else { | |
1049 | comm_close(fd); | |
1050 | } | |
1051 | } else { | |
1052 | /* More data will be coming from primary server; register with | |
1053 | * storage manager. */ | |
0e473d70 | 1054 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 1055 | debug(33, 0) ("clientWriteComplete 2: entry->swap_status == STORE_ABORTED\n"); |
7a2f978b | 1056 | storeClientCopy(entry, |
1057 | http->out.offset, | |
1058 | http->out.offset, | |
1059 | SM_PAGE_SIZE, | |
3f6c0fb2 | 1060 | memAllocate(MEM_4K_BUF, 1), |
7a2f978b | 1061 | clientSendMoreData, |
1062 | http); | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | static void | |
1b02b5be | 1067 | clientGetHeadersForIMS(void *data, char *buf, ssize_t size) |
7a2f978b | 1068 | { |
1069 | clientHttpRequest *http = data; | |
7a2f978b | 1070 | StoreEntry *entry = http->entry; |
1071 | MemObject *mem = entry->mem_obj; | |
1072 | char *reply = NULL; | |
1073 | assert(size <= SM_PAGE_SIZE); | |
3f6c0fb2 | 1074 | memFree(MEM_4K_BUF, buf); |
1b02b5be | 1075 | buf = NULL; |
6cf028ab | 1076 | if (size < 0 || entry->store_status == STORE_ABORTED) { |
1b02b5be | 1077 | debug(12, 1) ("clientGetHeadersForIMS: storeClientCopy failed for '%s'\n", |
7a2f978b | 1078 | storeKeyText(entry->key)); |
fb63215a | 1079 | clientProcessMiss(http); |
7a2f978b | 1080 | return; |
1081 | } | |
1082 | if (mem->reply->code == 0) { | |
1083 | if (entry->mem_status == IN_MEMORY) { | |
fb63215a | 1084 | clientProcessMiss(http); |
7a2f978b | 1085 | return; |
1086 | } | |
1087 | /* All headers are not yet available, wait for more data */ | |
0e473d70 | 1088 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 1089 | debug(33, 0) ("clientGetHeadersForIMS: entry->swap_status == STORE_ABORTED\n"); |
7a2f978b | 1090 | storeClientCopy(entry, |
1091 | http->out.offset + size, | |
1092 | http->out.offset, | |
1093 | SM_PAGE_SIZE, | |
3f6c0fb2 | 1094 | memAllocate(MEM_4K_BUF, 1), |
1b02b5be | 1095 | clientGetHeadersForIMS, |
7a2f978b | 1096 | http); |
1097 | return; | |
1098 | } | |
1099 | /* All headers are available, check if object is modified or not */ | |
1100 | /* --------------------------------------------------------------- | |
1101 | * Removed check for reply->code != 200 because of a potential | |
1102 | * problem with ICP. We will return a HIT for any public, cached | |
1103 | * object. This includes other responses like 301, 410, as coded in | |
1104 | * http.c. It is Bad(tm) to return UDP_HIT and then, if the reply | |
50ddd7a4 | 1105 | * code is not 200, hand off to clientProcessMiss(), which may disallow |
7a2f978b | 1106 | * the request based on 'miss_access' rules. Alternatively, we might |
1107 | * consider requiring returning UDP_HIT only for 200's. This | |
1108 | * problably means an entry->flag bit, which would be lost during | |
1109 | * restart because the flags aren't preserved across restarts. | |
1110 | * --DW 3/11/96. | |
1111 | * ---------------------------------------------------------------- */ | |
1112 | #ifdef CHECK_REPLY_CODE_NOTEQUAL_200 | |
1113 | /* Only objects with statuscode==200 can be "Not modified" */ | |
1114 | if (mem->reply->code != 200) { | |
1b02b5be | 1115 | debug(12, 4) ("clientGetHeadersForIMS: Reply code %d!=200\n", |
7a2f978b | 1116 | mem->reply->code); |
fb63215a | 1117 | clientProcessMiss(http); |
7a2f978b | 1118 | return; |
1119 | } | |
7a2f978b | 1120 | #endif |
1b02b5be | 1121 | http->log_type = LOG_TCP_IMS_HIT; |
7a2f978b | 1122 | entry->refcount++; |
1123 | if (modifiedSince(entry, http->request)) { | |
0e473d70 | 1124 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 1125 | debug(33, 0) ("clientGetHeadersForIMS 2: entry->swap_status == STORE_ABORTED\n"); |
7a2f978b | 1126 | storeClientCopy(entry, |
1127 | http->out.offset, | |
1128 | http->out.offset, | |
1129 | SM_PAGE_SIZE, | |
3f6c0fb2 | 1130 | memAllocate(MEM_4K_BUF, 1), |
7a2f978b | 1131 | clientSendMoreData, |
1132 | http); | |
1133 | return; | |
1134 | } | |
1b02b5be | 1135 | debug(12, 4) ("clientGetHeadersForIMS: Not modified '%s'\n", storeUrl(entry)); |
1136 | reply = clientConstruct304reply(mem->reply); | |
fb63215a | 1137 | comm_write(http->conn->fd, |
7a2f978b | 1138 | xstrdup(reply), |
1139 | strlen(reply), | |
1b02b5be | 1140 | clientHandleIMSComplete, |
7a2f978b | 1141 | http, |
1142 | xfree); | |
1143 | } | |
1144 | ||
1145 | static void | |
1b02b5be | 1146 | clientHandleIMSComplete(int fd, char *bufnotused, size_t size, int flag, void *data) |
7a2f978b | 1147 | { |
1148 | clientHttpRequest *http = data; | |
1149 | StoreEntry *entry = http->entry; | |
1b02b5be | 1150 | debug(12, 5) ("clientHandleIMSComplete: Not Modified sent '%s'\n", storeUrl(entry)); |
7a2f978b | 1151 | /* Set up everything for the logging */ |
1152 | storeUnregister(entry, http); | |
1153 | storeUnlockObject(entry); | |
1154 | http->entry = NULL; | |
1155 | http->out.size += size; | |
1156 | http->al.http.code = 304; | |
79a15e0a | 1157 | if (flag != COMM_ERR_CLOSING) |
7a2f978b | 1158 | comm_close(fd); |
1159 | } | |
1160 | ||
50ddd7a4 | 1161 | static log_type |
1162 | clientProcessRequest2(clientHttpRequest * http) | |
1163 | { | |
1164 | const request_t *r = http->request; | |
23d92c64 | 1165 | const cache_key *key = storeKeyPublic(http->uri, r->method); |
50ddd7a4 | 1166 | StoreEntry *e; |
1167 | if ((e = http->entry = storeGet(key)) == NULL) { | |
1168 | /* this object isn't in the cache */ | |
1169 | return LOG_TCP_MISS; | |
1170 | } else if (EBIT_TEST(e->flag, ENTRY_SPECIAL)) { | |
1171 | if (e->mem_status == IN_MEMORY) | |
1172 | return LOG_TCP_MEM_HIT; | |
1173 | else | |
1174 | return LOG_TCP_HIT; | |
1175 | } else if (!storeEntryValidToSend(e)) { | |
1176 | storeRelease(e); | |
1177 | http->entry = NULL; | |
1178 | return LOG_TCP_MISS; | |
1179 | } else if (EBIT_TEST(r->flags, REQ_NOCACHE)) { | |
1180 | /* NOCACHE should always eject a negative cached object */ | |
1181 | if (EBIT_TEST(e->flag, ENTRY_NEGCACHED)) | |
1182 | storeRelease(e); | |
1183 | /* NOCACHE+IMS should not eject a valid object */ | |
1184 | else if (EBIT_TEST(r->flags, REQ_IMS)) | |
1185 | (void) 0; | |
1186 | /* Request-Range should not eject a valid object */ | |
1187 | else if (EBIT_TEST(r->flags, REQ_RANGE)) | |
1188 | (void) 0; | |
1189 | else | |
1190 | storeRelease(e); | |
1191 | ipcacheReleaseInvalid(r->host); | |
1192 | http->entry = NULL; | |
1193 | return LOG_TCP_CLIENT_REFRESH; | |
1194 | } else if (checkNegativeHit(e)) { | |
1195 | return LOG_TCP_NEGATIVE_HIT; | |
1196 | } else if (refreshCheck(e, r, 0)) { | |
1197 | /* The object is in the cache, but it needs to be validated. Use | |
1198 | * LOG_TCP_REFRESH_MISS for the time being, maybe change it to | |
1b02b5be | 1199 | * _HIT later in clientHandleIMSReply() */ |
50ddd7a4 | 1200 | if (r->protocol == PROTO_HTTP) |
1201 | return LOG_TCP_REFRESH_MISS; | |
1202 | else | |
1203 | return LOG_TCP_MISS; /* XXX zoinks */ | |
1204 | } else if (EBIT_TEST(r->flags, REQ_IMS)) { | |
1205 | /* User-initiated IMS request for something we think is valid */ | |
1206 | return LOG_TCP_IMS_MISS; | |
1207 | } else if (e->mem_status == IN_MEMORY) { | |
1208 | return LOG_TCP_MEM_HIT; | |
1209 | } else { | |
1210 | return LOG_TCP_HIT; | |
1211 | } | |
1212 | } | |
1213 | ||
4c323c5f | 1214 | static void |
fb63215a | 1215 | clientProcessRequest(clientHttpRequest * http) |
7a2f978b | 1216 | { |
23d92c64 | 1217 | char *url = http->uri; |
7a2f978b | 1218 | StoreEntry *entry = NULL; |
50ddd7a4 | 1219 | request_t *r = http->request; |
fb63215a | 1220 | int fd = http->conn->fd; |
b17b22eb | 1221 | char *hdr; |
50ddd7a4 | 1222 | debug(12, 4) ("clientProcessRequest: %s '%s'\n", |
1223 | RequestMethodStr[r->method], | |
7a2f978b | 1224 | url); |
fb63215a | 1225 | if (r->method == METHOD_CONNECT) { |
7a2f978b | 1226 | http->log_type = LOG_TCP_MISS; |
ea0efb98 | 1227 | sslStart(fd, url, r, &http->out.size); |
1228 | return; | |
fb63215a | 1229 | } else if (r->method == METHOD_PURGE) { |
ea0efb98 | 1230 | clientPurgeRequest(http); |
1231 | return; | |
fb63215a | 1232 | } else if (r->method == METHOD_TRACE) { |
50ddd7a4 | 1233 | if (r->max_forwards == 0) { |
b17b22eb | 1234 | http->entry = clientCreateStoreEntry(http, r->method, 0); |
1235 | storeReleaseRequest(http->entry); | |
1236 | storeBuffer(http->entry); | |
1237 | hdr = httpReplyHeader(1.0, | |
164f7660 | 1238 | HTTP_OK, |
1239 | "text/plain", | |
1240 | r->headers_sz, | |
1241 | 0, | |
1242 | squid_curtime); | |
b17b22eb | 1243 | storeAppend(http->entry, hdr, strlen(hdr)); |
1244 | storeAppend(http->entry, "\r\n", 2); | |
1245 | storeAppend(http->entry, r->headers, r->headers_sz); | |
1246 | storeComplete(http->entry); | |
7a2f978b | 1247 | return; |
1248 | } | |
1249 | /* yes, continue */ | |
fb63215a | 1250 | } else if (r->protocol != PROTO_HTTP) { |
1251 | (void) 0; /* fallthrough */ | |
1252 | } else if (r->method == METHOD_POST) { | |
7a2f978b | 1253 | http->log_type = LOG_TCP_MISS; |
ea0efb98 | 1254 | passStart(fd, url, r, &http->out.size); |
1255 | return; | |
fb63215a | 1256 | } else if (r->method == METHOD_PUT) { |
1257 | http->log_type = LOG_TCP_MISS; | |
ea0efb98 | 1258 | passStart(fd, url, r, &http->out.size); |
1259 | return; | |
7a2f978b | 1260 | } |
fb63215a | 1261 | http->log_type = clientProcessRequest2(http); |
50ddd7a4 | 1262 | debug(12, 4) ("clientProcessRequest: %s for '%s'\n", |
7a2f978b | 1263 | log_tags[http->log_type], |
23d92c64 | 1264 | http->uri); |
50ddd7a4 | 1265 | if ((entry = http->entry) != NULL) { |
7a2f978b | 1266 | storeLockObject(entry); |
23d92c64 | 1267 | storeCreateMemObject(entry, http->uri, http->log_uri); |
7a2f978b | 1268 | storeClientListAdd(entry, http); |
1269 | } | |
7a2f978b | 1270 | http->out.offset = 0; |
1271 | switch (http->log_type) { | |
1272 | case LOG_TCP_HIT: | |
1273 | case LOG_TCP_NEGATIVE_HIT: | |
1274 | case LOG_TCP_MEM_HIT: | |
1275 | entry->refcount++; /* HIT CASE */ | |
0e473d70 | 1276 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 1277 | debug(33, 0) ("clientProcessRequest: entry->swap_status == STORE_ABORTED\n"); |
7a2f978b | 1278 | storeClientCopy(entry, |
1279 | http->out.offset, | |
1280 | http->out.offset, | |
1281 | SM_PAGE_SIZE, | |
3f6c0fb2 | 1282 | memAllocate(MEM_4K_BUF, 1), |
7a2f978b | 1283 | clientCacheHit, |
1284 | http); | |
1285 | break; | |
1286 | case LOG_TCP_IMS_MISS: | |
0e473d70 | 1287 | if (entry->store_status == STORE_ABORTED) |
6cf028ab | 1288 | debug(33, 0) ("clientProcessRequest 2: entry->swap_status == STORE_ABORTED\n"); |
7a2f978b | 1289 | storeClientCopy(entry, |
1290 | http->out.offset, | |
1291 | http->out.offset, | |
1292 | SM_PAGE_SIZE, | |
3f6c0fb2 | 1293 | memAllocate(MEM_4K_BUF, 1), |
1b02b5be | 1294 | clientGetHeadersForIMS, |
7a2f978b | 1295 | http); |
1296 | break; | |
1297 | case LOG_TCP_REFRESH_MISS: | |
fb63215a | 1298 | clientProcessExpired(http); |
7a2f978b | 1299 | break; |
1300 | default: | |
fb63215a | 1301 | clientProcessMiss(http); |
7a2f978b | 1302 | break; |
1303 | } | |
1304 | } | |
1305 | ||
1306 | /* | |
1307 | * Prepare to fetch the object as it's a cache miss of some kind. | |
1308 | */ | |
1309 | static void | |
fb63215a | 1310 | clientProcessMiss(clientHttpRequest * http) |
7a2f978b | 1311 | { |
23d92c64 | 1312 | char *url = http->uri; |
f44af445 | 1313 | request_t *r = http->request; |
1314 | char *request_hdr = r->headers; | |
7a2f978b | 1315 | aclCheck_t ch; |
1316 | int answer; | |
1317 | ErrorState *err = NULL; | |
50ddd7a4 | 1318 | debug(12, 4) ("clientProcessMiss: '%s %s'\n", |
f44af445 | 1319 | RequestMethodStr[r->method], url); |
50ddd7a4 | 1320 | debug(12, 10) ("clientProcessMiss: request_hdr:\n%s\n", request_hdr); |
79e0dc20 | 1321 | /* |
1322 | * We might have a left-over StoreEntry from a failed cache hit | |
1323 | * or IMS request. | |
1324 | */ | |
1325 | if (http->entry) { | |
1326 | storeUnregister(http->entry, http); | |
1327 | storeUnlockObject(http->entry); | |
1328 | http->entry = NULL; | |
1329 | } | |
1330 | /* | |
1331 | * Check if this host is allowed to fetch MISSES from us (miss_access) | |
1332 | */ | |
7a2f978b | 1333 | memset(&ch, '\0', sizeof(aclCheck_t)); |
1334 | ch.src_addr = http->conn->peer.sin_addr; | |
f44af445 | 1335 | ch.request = r; |
7a2f978b | 1336 | answer = aclCheckFast(Config.accessList.miss, &ch); |
1337 | if (answer == 0) { | |
1338 | http->al.http.code = HTTP_FORBIDDEN; | |
bdb3c273 | 1339 | err = errorCon(ERR_FORWARDING_DENIED, HTTP_FORBIDDEN); |
f44af445 | 1340 | err->request = requestLink(r); |
7a2f978b | 1341 | err->src_addr = http->conn->peer.sin_addr; |
f44af445 | 1342 | http->entry = clientCreateStoreEntry(http, r->method, 0); |
79e0dc20 | 1343 | errorAppendEntry(http->entry, err); |
7a2f978b | 1344 | return; |
1345 | } | |
79e0dc20 | 1346 | assert(http->out.offset == 0); |
f44af445 | 1347 | http->entry = clientCreateStoreEntry(http, r->method, r->flags); |
79e0dc20 | 1348 | http->entry->mem_obj->fd = http->conn->fd; |
1349 | http->entry->refcount++; | |
f44af445 | 1350 | protoDispatch(http->conn->fd, http->entry, r); |
7a2f978b | 1351 | } |
1352 | ||
1353 | /* | |
1354 | * parseHttpRequest() | |
1355 | * | |
1356 | * Returns | |
1357 | * NULL on error or incomplete request | |
1358 | * a clientHttpRequest structure on success | |
1359 | */ | |
1360 | static clientHttpRequest * | |
1361 | parseHttpRequest(ConnStateData * conn, method_t * method_p, int *status, | |
1362 | char **headers_p, size_t * headers_sz_p) | |
1363 | { | |
1364 | char *inbuf = NULL; | |
1365 | char *mstr = NULL; | |
1366 | char *url = NULL; | |
1367 | char *req_hdr = NULL; | |
7a2f978b | 1368 | float http_ver; |
1369 | char *token = NULL; | |
1370 | char *t = NULL; | |
1371 | char *end = NULL; | |
1372 | int free_request = 0; | |
1373 | size_t header_sz; /* size of headers, not including first line */ | |
1374 | size_t req_sz; /* size of whole request */ | |
1375 | size_t url_sz; | |
1376 | method_t method; | |
1377 | clientHttpRequest *http = NULL; | |
1378 | ||
1379 | /* Make sure a complete line has been received */ | |
1380 | if (strchr(conn->in.buf, '\n') == NULL) { | |
1381 | debug(12, 5) ("Incomplete request line, waiting for more data\n"); | |
1382 | *status = 0; | |
1383 | return NULL; | |
1384 | } | |
1385 | /* Use xmalloc/xmemcpy instead of xstrdup because inbuf might | |
1386 | * contain NULL bytes; especially for POST data */ | |
1387 | inbuf = xmalloc(conn->in.offset + 1); | |
1388 | xmemcpy(inbuf, conn->in.buf, conn->in.offset); | |
1389 | *(inbuf + conn->in.offset) = '\0'; | |
1390 | ||
1391 | /* Look for request method */ | |
1392 | if ((mstr = strtok(inbuf, "\t ")) == NULL) { | |
1393 | debug(12, 1) ("parseHttpRequest: Can't get request method\n"); | |
3775d53c | 1394 | http = xcalloc(1, sizeof(clientHttpRequest)); |
3f6c0fb2 | 1395 | cbdataAdd(http, MEM_NONE); |
3775d53c | 1396 | http->conn = conn; |
1397 | http->start = current_time; | |
1398 | http->req_sz = conn->in.offset; | |
23d92c64 | 1399 | http->uri = xstrdup("error:invalid-request-method"); |
1400 | http->log_uri = xstrdup("error:invalid-request-method"); | |
3775d53c | 1401 | *headers_sz_p = conn->in.offset; |
1402 | *headers_p = inbuf; | |
1403 | *method_p = METHOD_NONE; | |
7a2f978b | 1404 | *status = -1; |
3775d53c | 1405 | return http; |
7a2f978b | 1406 | } |
1407 | method = urlParseMethod(mstr); | |
1408 | if (method == METHOD_NONE) { | |
1409 | debug(12, 1) ("parseHttpRequest: Unsupported method '%s'\n", mstr); | |
3775d53c | 1410 | http = xcalloc(1, sizeof(clientHttpRequest)); |
3f6c0fb2 | 1411 | cbdataAdd(http, MEM_NONE); |
3775d53c | 1412 | http->conn = conn; |
1413 | http->start = current_time; | |
1414 | http->req_sz = conn->in.offset; | |
23d92c64 | 1415 | http->uri = xstrdup("error:unsupported-request-method"); |
1416 | http->log_uri = xstrdup("error:unsupported-request-method"); | |
3775d53c | 1417 | *headers_sz_p = conn->in.offset; |
1418 | *headers_p = inbuf; | |
1419 | *method_p = METHOD_NONE; | |
7a2f978b | 1420 | *status = -1; |
3775d53c | 1421 | return http; |
7a2f978b | 1422 | } |
1423 | debug(12, 5) ("parseHttpRequest: Method is '%s'\n", mstr); | |
3775d53c | 1424 | *method_p = method; |
7a2f978b | 1425 | |
1426 | /* look for URL */ | |
1427 | if ((url = strtok(NULL, "\r\n\t ")) == NULL) { | |
1428 | debug(12, 1) ("parseHttpRequest: Missing URL\n"); | |
3775d53c | 1429 | http = xcalloc(1, sizeof(clientHttpRequest)); |
3f6c0fb2 | 1430 | cbdataAdd(http, MEM_NONE); |
3775d53c | 1431 | http->conn = conn; |
1432 | http->start = current_time; | |
1433 | http->req_sz = conn->in.offset; | |
23d92c64 | 1434 | http->uri = xstrdup("error:missing-url"); |
1435 | http->log_uri = xstrdup("error:missing-url"); | |
3775d53c | 1436 | *headers_sz_p = conn->in.offset; |
1437 | *headers_p = inbuf; | |
7a2f978b | 1438 | *status = -1; |
3775d53c | 1439 | return http; |
7a2f978b | 1440 | } |
1441 | debug(12, 5) ("parseHttpRequest: Request is '%s'\n", url); | |
1442 | ||
1443 | token = strtok(NULL, null_string); | |
1444 | for (t = token; t && *t && *t != '\n' && *t != '\r'; t++); | |
46c7dbf0 | 1445 | if (t == NULL || *t == '\0' || t == token || strncmp(token, "HTTP/", 5)) { |
7a2f978b | 1446 | debug(12, 3) ("parseHttpRequest: Missing HTTP identifier\n"); |
3775d53c | 1447 | http = xcalloc(1, sizeof(clientHttpRequest)); |
3f6c0fb2 | 1448 | cbdataAdd(http, MEM_NONE); |
3775d53c | 1449 | http->conn = conn; |
1450 | http->start = current_time; | |
1451 | http->req_sz = conn->in.offset; | |
23d92c64 | 1452 | http->uri = xstrdup("error:missing-http-ident"); |
1453 | http->log_uri = xstrdup("error:missing-http-ident"); | |
3775d53c | 1454 | *headers_sz_p = conn->in.offset; |
1455 | *headers_p = inbuf; | |
7a2f978b | 1456 | *status = -1; |
3775d53c | 1457 | return http; |
7a2f978b | 1458 | } |
84116757 | 1459 | http_ver = (float) atof(token + 5); |
7a2f978b | 1460 | |
1461 | /* Check if headers are received */ | |
1462 | if ((end = mime_headers_end(t)) == NULL) { | |
1463 | xfree(inbuf); | |
1464 | *status = 0; | |
1465 | return NULL; | |
1466 | } | |
7a2f978b | 1467 | req_hdr = t; |
c24fca87 | 1468 | /* |
1469 | * Skip whitespace at the end of the frist line, up to the | |
1470 | * first newline. | |
1471 | */ | |
1472 | while (isspace(*req_hdr)) | |
1473 | if (*(req_hdr++) == '\n') | |
1474 | break; | |
1475 | if (end <= req_hdr) { | |
1476 | /* Invalid request */ | |
1477 | debug(12, 3) ("parseHttpRequest: No request headers?\n"); | |
1478 | http = xcalloc(1, sizeof(clientHttpRequest)); | |
3f6c0fb2 | 1479 | cbdataAdd(http, MEM_NONE); |
c24fca87 | 1480 | http->conn = conn; |
1481 | http->start = current_time; | |
1482 | http->req_sz = conn->in.offset; | |
1483 | http->uri = xstrdup("error:no-request-headers"); | |
1484 | http->log_uri = xstrdup("error:no-request-headers"); | |
1485 | *headers_sz_p = conn->in.offset; | |
1486 | *headers_p = inbuf; | |
1487 | *status = -1; | |
1488 | return http; | |
1489 | } | |
7a2f978b | 1490 | header_sz = end - req_hdr; |
1491 | req_sz = end - inbuf; | |
1492 | ||
1493 | /* Ok, all headers are received */ | |
1494 | http = xcalloc(1, sizeof(clientHttpRequest)); | |
3f6c0fb2 | 1495 | cbdataAdd(http, MEM_NONE); |
7a2f978b | 1496 | http->http_ver = http_ver; |
1497 | http->conn = conn; | |
1498 | http->start = current_time; | |
1499 | http->req_sz = req_sz; | |
1500 | *headers_sz_p = header_sz; | |
1501 | *headers_p = xmalloc(header_sz + 1); | |
1502 | xmemcpy(*headers_p, req_hdr, header_sz); | |
1503 | *(*headers_p + header_sz) = '\0'; | |
1504 | ||
1505 | debug(12, 5) ("parseHttpRequest: Request Header is\n%s\n", *headers_p); | |
1506 | ||
23d92c64 | 1507 | /* Assign http->uri */ |
7a2f978b | 1508 | if ((t = strchr(url, '\n'))) /* remove NL */ |
1509 | *t = '\0'; | |
1510 | if ((t = strchr(url, '\r'))) /* remove CR */ | |
1511 | *t = '\0'; | |
1512 | if ((t = strchr(url, '#'))) /* remove HTML anchors */ | |
1513 | *t = '\0'; | |
1514 | ||
1515 | /* see if we running in Config2.Accel.on, if so got to convert it to URL */ | |
1516 | if (Config2.Accel.on && *url == '/') { | |
1517 | /* prepend the accel prefix */ | |
dbfed404 | 1518 | if (opt_accel_uses_host && (t = mime_get_header(req_hdr, "Host"))) { |
7a2f978b | 1519 | /* If a Host: header was specified, use it to build the URL |
1520 | * instead of the one in the Config file. */ | |
1521 | /* | |
1522 | * XXX Use of the Host: header here opens a potential | |
1523 | * security hole. There are no checks that the Host: value | |
1524 | * corresponds to one of your servers. It might, for example, | |
1525 | * refer to www.playboy.com. The 'dst' and/or 'dst_domain' ACL | |
1526 | * types should be used to prevent httpd-accelerators | |
1527 | * handling requests for non-local servers */ | |
1528 | strtok(t, " :/;@"); | |
12fc602c | 1529 | url_sz = strlen(url) + 32 + Config.appendDomainLen + |
1530 | strlen(t); | |
23d92c64 | 1531 | http->uri = xcalloc(url_sz, 1); |
1532 | snprintf(http->uri, url_sz, "http://%s:%d%s", | |
7a2f978b | 1533 | t, (int) Config.Accel.port, url); |
dbfed404 | 1534 | } else if (vhost_mode) { |
1535 | /* Put the local socket IP address as the hostname */ | |
1536 | url_sz = strlen(url) + 32 + Config.appendDomainLen; | |
1537 | http->uri = xcalloc(url_sz, 1); | |
1538 | snprintf(http->uri, url_sz, "http://%s:%d%s", | |
1539 | inet_ntoa(http->conn->me.sin_addr), | |
1540 | (int) Config.Accel.port, | |
1541 | url); | |
1542 | debug(12, 5) ("VHOST REWRITE: '%s'\n", http->uri); | |
7a2f978b | 1543 | } else { |
1544 | url_sz = strlen(Config2.Accel.prefix) + strlen(url) + | |
1545 | Config.appendDomainLen + 1; | |
23d92c64 | 1546 | http->uri = xcalloc(url_sz, 1); |
1547 | snprintf(http->uri, url_sz, "%s%s", Config2.Accel.prefix, url); | |
7a2f978b | 1548 | } |
1549 | http->accel = 1; | |
1550 | } else { | |
1551 | /* URL may be rewritten later, so make extra room */ | |
1552 | url_sz = strlen(url) + Config.appendDomainLen + 5; | |
23d92c64 | 1553 | http->uri = xcalloc(url_sz, 1); |
1554 | strcpy(http->uri, url); | |
7a2f978b | 1555 | http->accel = 0; |
1556 | } | |
23d92c64 | 1557 | http->log_uri = xstrdup(http->uri); |
7a2f978b | 1558 | debug(12, 5) ("parseHttpRequest: Complete request received\n"); |
1559 | if (free_request) | |
1560 | safe_free(url); | |
1561 | xfree(inbuf); | |
7a2f978b | 1562 | *status = 1; |
1563 | return http; | |
1564 | } | |
1565 | ||
1566 | static int | |
79d39a72 | 1567 | clientReadDefer(int fdnotused, void *data) |
7a2f978b | 1568 | { |
1569 | ConnStateData *conn = data; | |
1570 | return conn->defer.until > squid_curtime; | |
1571 | } | |
1572 | ||
1573 | static void | |
1574 | clientReadRequest(int fd, void *data) | |
1575 | { | |
1576 | ConnStateData *conn = data; | |
1577 | int parser_return_code = 0; | |
1578 | int k; | |
1579 | request_t *request = NULL; | |
7a2f978b | 1580 | int size; |
7a2f978b | 1581 | method_t method; |
1582 | clientHttpRequest *http = NULL; | |
1583 | clientHttpRequest **H = NULL; | |
1584 | char *headers; | |
1585 | size_t headers_sz; | |
1586 | ErrorState *err = NULL; | |
1587 | fde *F = &fd_table[fd]; | |
44db45e8 | 1588 | int len = conn->in.size - conn->in.offset - 1; |
7a2f978b | 1589 | debug(12, 4) ("clientReadRequest: FD %d: reading request...\n", fd); |
1590 | size = read(fd, conn->in.buf + conn->in.offset, len); | |
1591 | fd_bytes(fd, size, FD_READ); | |
44db45e8 | 1592 | /* |
1593 | * Don't reset the timeout value here. The timeout value will be | |
1594 | * set to Config.Timeout.request by httpAccept() and | |
1595 | * clientWriteComplete(), and should apply to the request as a | |
1596 | * whole, not individual read() calls. Plus, it breaks our | |
1597 | * lame half-close detection | |
1598 | */ | |
1599 | commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, conn, 0); | |
7a2f978b | 1600 | if (size == 0) { |
1601 | if (conn->chr == NULL) { | |
1602 | /* no current or pending requests */ | |
1603 | comm_close(fd); | |
1604 | return; | |
1605 | } | |
1606 | /* It might be half-closed, we can't tell */ | |
1607 | debug(12, 5) ("clientReadRequest: FD %d closed?\n", fd); | |
79a15e0a | 1608 | EBIT_SET(F->flags, FD_SOCKET_EOF); |
7a2f978b | 1609 | conn->defer.until = squid_curtime + 1; |
1610 | conn->defer.n++; | |
44db45e8 | 1611 | fd_note(fd, "half-closed"); |
7a2f978b | 1612 | return; |
1613 | } else if (size < 0) { | |
6d3caf23 | 1614 | if (!ignoreErrno(errno)) { |
7a2f978b | 1615 | debug(50, 2) ("clientReadRequest: FD %d: %s\n", fd, xstrerror()); |
1616 | comm_close(fd); | |
ebf4efff | 1617 | return; |
47130615 | 1618 | } else if (conn->in.offset == 0) { |
ebf4efff | 1619 | debug(50, 2) ("clientReadRequest: FD %d: no data to process\n"); |
1620 | return; | |
7a2f978b | 1621 | } |
ebf4efff | 1622 | /* Continue to process previously read data */ |
47130615 | 1623 | size = 0; |
7a2f978b | 1624 | } |
1625 | conn->in.offset += size; | |
1626 | conn->in.buf[conn->in.offset] = '\0'; /* Terminate the string */ | |
7a2f978b | 1627 | while (conn->in.offset > 0) { |
ebf4efff | 1628 | int nrequests; |
1629 | /* Limit the number of concurrent requests to 2 */ | |
1630 | for (H = &conn->chr, nrequests = 0; *H; H = &(*H)->next, nrequests++); | |
1631 | if (nrequests >= 2) { | |
1632 | debug(12, 2) ("clientReadRequest: FD %d max concurrent requests reached\n", fd); | |
1633 | debug(12, 5) ("clientReadRequest: FD %d defering new request until one is done\n", fd); | |
47130615 | 1634 | conn->defer.until = squid_curtime + 100; /* Reset when a request is complete */ |
ebf4efff | 1635 | break; |
1636 | } | |
1637 | /* Process request */ | |
7a2f978b | 1638 | http = parseHttpRequest(conn, |
1639 | &method, | |
1640 | &parser_return_code, | |
1641 | &headers, | |
1642 | &headers_sz); | |
1643 | if (http) { | |
1644 | assert(http->req_sz > 0); | |
1645 | conn->in.offset -= http->req_sz; | |
1646 | assert(conn->in.offset >= 0); | |
6fa92aa2 | 1647 | /* |
1648 | * If we read past the end of this request, move the remaining | |
1649 | * data to the beginning | |
1650 | */ | |
1651 | if (conn->in.offset > 0) | |
dbfed404 | 1652 | xmemmove(conn->in.buf, conn->in.buf + http->req_sz, conn->in.offset); |
ebf4efff | 1653 | /* add to the client request queue */ |
7a2f978b | 1654 | for (H = &conn->chr; *H; H = &(*H)->next); |
1655 | *H = http; | |
1656 | conn->nrequests++; | |
1657 | commSetTimeout(fd, Config.Timeout.lifetime, NULL, NULL); | |
3775d53c | 1658 | if (parser_return_code < 0) { |
1659 | debug(12, 1) ("clientReadRequest: FD %d Invalid Request\n", fd); | |
1660 | err = errorCon(ERR_INVALID_REQ, HTTP_BAD_REQUEST); | |
1661 | err->request_hdrs = xstrdup(conn->in.buf); | |
1662 | http->entry = clientCreateStoreEntry(http, method, 0); | |
1663 | errorAppendEntry(http->entry, err); | |
1664 | break; | |
1665 | } | |
23d92c64 | 1666 | if ((request = urlParse(method, http->uri)) == NULL) { |
1667 | debug(12, 5) ("Invalid URL: %s\n", http->uri); | |
7a2f978b | 1668 | err = errorCon(ERR_INVALID_URL, HTTP_BAD_REQUEST); |
1669 | err->src_addr = conn->peer.sin_addr; | |
23d92c64 | 1670 | err->url = xstrdup(http->uri); |
7a2f978b | 1671 | http->al.http.code = err->http_status; |
f44af445 | 1672 | http->entry = clientCreateStoreEntry(http, method, 0); |
79e0dc20 | 1673 | errorAppendEntry(http->entry, err); |
7a2f978b | 1674 | safe_free(headers); |
1675 | break; | |
1676 | } | |
23d92c64 | 1677 | safe_free(http->log_uri); |
1678 | http->log_uri = xstrdup(urlCanonicalClean(request)); | |
7a2f978b | 1679 | request->client_addr = conn->peer.sin_addr; |
1680 | request->http_ver = http->http_ver; | |
1681 | request->headers = headers; | |
1682 | request->headers_sz = headers_sz; | |
1683 | if (!urlCheckRequest(request)) { | |
1684 | err = errorCon(ERR_UNSUP_REQ, HTTP_NOT_IMPLEMENTED); | |
1685 | err->src_addr = conn->peer.sin_addr; | |
7a2f978b | 1686 | err->request = requestLink(request); |
1687 | http->al.http.code = err->http_status; | |
f44af445 | 1688 | http->entry = clientCreateStoreEntry(http, request->method, 0); |
79e0dc20 | 1689 | errorAppendEntry(http->entry, err); |
3775d53c | 1690 | break; |
7a2f978b | 1691 | } |
1692 | http->request = requestLink(request); | |
cf26e54c | 1693 | clientAccessCheck(http); |
3775d53c | 1694 | /* |
1695 | * break here for NON-GET because most likely there is a | |
7a2f978b | 1696 | * reqeust body following and we don't want to parse it |
3775d53c | 1697 | * as though it was new request |
1698 | */ | |
7a2f978b | 1699 | if (request->method != METHOD_GET) { |
1700 | if (conn->in.offset) { | |
1701 | request->body_sz = conn->in.offset; | |
1702 | request->body = xmalloc(request->body_sz); | |
1703 | xmemcpy(request->body, conn->in.buf, request->body_sz); | |
1704 | conn->in.offset = 0; | |
1705 | } | |
1706 | break; | |
1707 | } | |
7a2f978b | 1708 | continue; /* while offset > 0 */ |
1709 | } else if (parser_return_code == 0) { | |
1710 | /* | |
1711 | * Partial request received; reschedule until parseHttpRequest() | |
1712 | * is happy with the input | |
1713 | */ | |
1714 | k = conn->in.size - 1 - conn->in.offset; | |
1715 | if (k == 0) { | |
1716 | if (conn->in.offset >= Config.maxRequestSize) { | |
1717 | /* The request is too large to handle */ | |
1718 | debug(12, 0) ("Request won't fit in buffer.\n"); | |
1719 | debug(12, 0) ("Config 'request_size'= %d bytes.\n", | |
1720 | Config.maxRequestSize); | |
1721 | debug(12, 0) ("This request = %d bytes.\n", | |
1722 | conn->in.offset); | |
1723 | err = errorCon(ERR_INVALID_REQ, HTTP_REQUEST_ENTITY_TOO_LARGE); | |
f44af445 | 1724 | http->entry = clientCreateStoreEntry(http, request->method, 0); |
79e0dc20 | 1725 | errorAppendEntry(http->entry, err); |
7a2f978b | 1726 | return; |
1727 | } | |
1728 | /* Grow the request memory area to accomodate for a large request */ | |
1729 | conn->in.size += REQUEST_BUF_SIZE; | |
1730 | conn->in.buf = xrealloc(conn->in.buf, conn->in.size); | |
1731 | meta_data.misc += REQUEST_BUF_SIZE; | |
1732 | debug(12, 2) ("Handling a large request, offset=%d inbufsize=%d\n", | |
1733 | conn->in.offset, conn->in.size); | |
1734 | k = conn->in.size - 1 - conn->in.offset; | |
1735 | } | |
7a2f978b | 1736 | break; |
7a2f978b | 1737 | } |
1738 | } | |
1739 | } | |
1740 | ||
1741 | /* general lifetime handler for HTTP requests */ | |
1742 | static void | |
1743 | requestTimeout(int fd, void *data) | |
1744 | { | |
1745 | ConnStateData *conn = data; | |
1746 | ErrorState *err; | |
1747 | debug(12, 2) ("requestTimeout: FD %d: lifetime is expired.\n", fd); | |
1748 | if (fd_table[fd].rwstate) { | |
79e0dc20 | 1749 | /* |
1750 | * Some data has been sent to the client, just close the FD | |
1751 | */ | |
7a2f978b | 1752 | comm_close(fd); |
1753 | } else if (conn->nrequests) { | |
79e0dc20 | 1754 | /* |
1755 | * assume its a persistent connection; just close it | |
1756 | */ | |
7a2f978b | 1757 | comm_close(fd); |
1758 | } else { | |
79e0dc20 | 1759 | /* |
1760 | * Generate an error | |
1761 | */ | |
7a2f978b | 1762 | err = errorCon(ERR_LIFETIME_EXP, HTTP_REQUEST_TIMEOUT); |
7a2f978b | 1763 | err->url = xstrdup("N/A"); |
79e0dc20 | 1764 | /* |
1765 | * Normally we shouldn't call errorSend() in client_side.c, but | |
1766 | * it should be okay in this case. Presumably if we get here | |
1767 | * this is the first request for the connection, and no data | |
1768 | * has been written yet | |
1769 | */ | |
1770 | assert(conn->chr == NULL); | |
7a2f978b | 1771 | errorSend(fd, err); |
79e0dc20 | 1772 | /* |
1773 | * if we don't close() here, we still need a timeout handler! | |
1774 | */ | |
7a2f978b | 1775 | commSetTimeout(fd, 30, requestTimeout, conn); |
1776 | } | |
1777 | } | |
1778 | ||
1779 | int | |
79d39a72 | 1780 | httpAcceptDefer(int fdnotused, void *notused) |
7a2f978b | 1781 | { |
1782 | return !fdstat_are_n_free_fd(RESERVED_FD); | |
1783 | } | |
1784 | ||
bf8e5903 | 1785 | /* Handle a new connection on HTTP socket. */ |
7a2f978b | 1786 | void |
1787 | httpAccept(int sock, void *notused) | |
1788 | { | |
1789 | int fd = -1; | |
1790 | ConnStateData *connState = NULL; | |
1791 | struct sockaddr_in peer; | |
1792 | struct sockaddr_in me; | |
1793 | memset(&peer, '\0', sizeof(struct sockaddr_in)); | |
1794 | memset(&me, '\0', sizeof(struct sockaddr_in)); | |
1795 | commSetSelect(sock, COMM_SELECT_READ, httpAccept, NULL, 0); | |
1796 | if ((fd = comm_accept(sock, &peer, &me)) < 0) { | |
1797 | debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n", | |
1798 | sock, xstrerror()); | |
1799 | return; | |
1800 | } | |
7a2f978b | 1801 | debug(12, 4) ("httpAccept: FD %d: accepted\n", fd); |
1802 | connState = xcalloc(1, sizeof(ConnStateData)); | |
1803 | connState->peer = peer; | |
1804 | connState->log_addr = peer.sin_addr; | |
1805 | connState->log_addr.s_addr &= Config.Addrs.client_netmask.s_addr; | |
1806 | connState->me = me; | |
1807 | connState->fd = fd; | |
1808 | connState->ident.fd = -1; | |
1809 | connState->in.size = REQUEST_BUF_SIZE; | |
1810 | connState->in.buf = xcalloc(connState->in.size, 1); | |
3f6c0fb2 | 1811 | cbdataAdd(connState, MEM_NONE); |
7a2f978b | 1812 | meta_data.misc += connState->in.size; |
1813 | comm_add_close_handler(fd, connStateFree, connState); | |
1814 | if (Config.onoff.log_fqdn) | |
1815 | fqdncache_gethostbyaddr(peer.sin_addr, FQDN_LOOKUP_IF_MISS); | |
1816 | commSetTimeout(fd, Config.Timeout.request, requestTimeout, connState); | |
1817 | commSetSelect(fd, COMM_SELECT_READ, clientReadRequest, connState, 0); | |
1818 | commSetDefer(fd, clientReadDefer, connState); | |
1819 | } | |
1820 | ||
1821 | void | |
1822 | AppendUdp(icpUdpData * item) | |
1823 | { | |
1824 | item->next = NULL; | |
1825 | if (UdpQueueHead == NULL) { | |
1826 | UdpQueueHead = item; | |
1827 | UdpQueueTail = item; | |
1828 | } else if (UdpQueueTail == UdpQueueHead) { | |
1829 | UdpQueueTail = item; | |
1830 | UdpQueueHead->next = item; | |
1831 | } else { | |
1832 | UdpQueueTail->next = item; | |
1833 | UdpQueueTail = item; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | /* return 1 if the request should be aborted */ | |
1838 | static int | |
1839 | CheckQuickAbort2(const clientHttpRequest * http) | |
1840 | { | |
1841 | long curlen; | |
1842 | long minlen; | |
1843 | long expectlen; | |
1844 | ||
79a15e0a | 1845 | if (!EBIT_TEST(http->request->flags, REQ_CACHABLE)) |
7a2f978b | 1846 | return 1; |
79a15e0a | 1847 | if (EBIT_TEST(http->entry->flag, KEY_PRIVATE)) |
7a2f978b | 1848 | return 1; |
1849 | if (http->entry->mem_obj == NULL) | |
1850 | return 1; | |
1851 | expectlen = http->entry->mem_obj->reply->content_length; | |
1852 | curlen = http->entry->mem_obj->inmem_hi; | |
1853 | minlen = Config.quickAbort.min; | |
1854 | if (minlen < 0) | |
1855 | /* disabled */ | |
1856 | return 0; | |
1857 | if (curlen > expectlen) | |
1858 | /* bad content length */ | |
1859 | return 1; | |
1860 | if ((expectlen - curlen) < minlen) | |
1861 | /* only little more left */ | |
1862 | return 0; | |
1863 | if ((expectlen - curlen) > Config.quickAbort.max) | |
1864 | /* too much left to go */ | |
1865 | return 1; | |
1866 | if ((curlen / (expectlen / 128U)) > Config.quickAbort.pct) | |
1867 | /* past point of no return */ | |
1868 | return 0; | |
1869 | return 1; | |
1870 | } | |
1871 | ||
1872 | ||
1873 | static void | |
1874 | CheckQuickAbort(clientHttpRequest * http) | |
1875 | { | |
1876 | StoreEntry *entry = http->entry; | |
1877 | /* Note, set entry here because http->entry might get changed (for IMS | |
1878 | * requests) during the storeAbort() call */ | |
1879 | if (entry == NULL) | |
1880 | return; | |
1881 | if (storePendingNClients(entry) > 1) | |
1882 | return; | |
1883 | if (entry->store_status != STORE_PENDING) | |
1884 | return; | |
1885 | if (CheckQuickAbort2(http) == 0) | |
1886 | return; | |
1887 | debug(12, 3) ("CheckQuickAbort: ABORTING %s\n", storeUrl(entry)); | |
1888 | storeAbort(entry, 1); | |
1889 | } | |
1890 | ||
978e455f | 1891 | #define SENDING_BODY 0 |
1892 | #define SENDING_HDRSONLY 1 | |
7a2f978b | 1893 | static int |
1b02b5be | 1894 | clientCheckTransferDone(clientHttpRequest * http) |
7a2f978b | 1895 | { |
978e455f | 1896 | int sending = SENDING_BODY; |
7a2f978b | 1897 | StoreEntry *entry = http->entry; |
978e455f | 1898 | MemObject *mem; |
1899 | http_reply *reply; | |
1900 | int sendlen; | |
7a2f978b | 1901 | if (entry == NULL) |
1902 | return 0; | |
978e455f | 1903 | /* |
1904 | * Handle STORE_OK and STORE_ABORTED objects. | |
1905 | * entry->object_len will be set proprely. | |
1906 | */ | |
1907 | if (entry->store_status != STORE_PENDING) { | |
7a2f978b | 1908 | if (http->out.offset >= entry->object_len) |
1909 | return 1; | |
513f05a6 | 1910 | else |
1911 | return 0; | |
1912 | } | |
978e455f | 1913 | /* |
1914 | * Now, handle STORE_PENDING objects | |
1915 | */ | |
1916 | mem = entry->mem_obj; | |
1917 | assert(mem != NULL); | |
1918 | assert(http->request != NULL); | |
1919 | reply = mem->reply; | |
1920 | if (reply->hdr_sz == 0) | |
b34ed725 | 1921 | return 0; /* haven't found end of headers yet */ |
978e455f | 1922 | else if (reply->code == HTTP_OK) |
b34ed725 | 1923 | sending = SENDING_BODY; |
978e455f | 1924 | else if (reply->code == HTTP_NO_CONTENT) |
b34ed725 | 1925 | sending = SENDING_HDRSONLY; |
978e455f | 1926 | else if (reply->code == HTTP_NOT_MODIFIED) |
b34ed725 | 1927 | sending = SENDING_HDRSONLY; |
a3c60429 | 1928 | else if (reply->code < HTTP_OK) |
1929 | sending = SENDING_HDRSONLY; | |
978e455f | 1930 | else if (http->request->method == METHOD_HEAD) |
b34ed725 | 1931 | sending = SENDING_HDRSONLY; |
978e455f | 1932 | else |
1933 | sending = SENDING_BODY; | |
1934 | /* | |
1935 | * Figure out how much data we are supposed to send. | |
1936 | * If we are sending a body and we don't have a content-length, | |
1937 | * then we must wait for the object to become STORE_OK or | |
1938 | * STORE_ABORTED. | |
1939 | */ | |
1940 | if (sending == SENDING_HDRSONLY) | |
1941 | sendlen = reply->hdr_sz; | |
1942 | else if (reply->content_length < 0) | |
1943 | return 0; | |
1944 | else | |
1945 | sendlen = reply->content_length + reply->hdr_sz; | |
1946 | /* | |
1947 | * Now that we have the expected length, did we send it all? | |
1948 | */ | |
1949 | if (http->out.offset < sendlen) | |
1950 | return 0; | |
1951 | else | |
b34ed725 | 1952 | return 1; |
7a2f978b | 1953 | } |
1954 | ||
1955 | static char * | |
1b02b5be | 1956 | clientConstruct304reply(struct _http_reply *source) |
7a2f978b | 1957 | { |
1958 | LOCAL_ARRAY(char, line, 256); | |
1959 | LOCAL_ARRAY(char, reply, 8192); | |
7a2f978b | 1960 | memset(reply, '\0', 8192); |
1961 | strcpy(reply, "HTTP/1.0 304 Not Modified\r\n"); | |
1962 | if (source->date > -1) { | |
1963 | snprintf(line, 256, "Date: %s\r\n", mkrfc1123(source->date)); | |
1964 | strcat(reply, line); | |
1965 | } | |
1966 | if ((int) strlen(source->content_type) > 0) { | |
1967 | snprintf(line, 256, "Content-type: %s\r\n", source->content_type); | |
1968 | strcat(reply, line); | |
1969 | } | |
1970 | if (source->content_length) { | |
1971 | snprintf(line, 256, "Content-length: %d\r\n", source->content_length); | |
1972 | strcat(reply, line); | |
1973 | } | |
1974 | if (source->expires > -1) { | |
1975 | snprintf(line, 256, "Expires: %s\r\n", mkrfc1123(source->expires)); | |
1976 | strcat(reply, line); | |
1977 | } | |
1978 | if (source->last_modified > -1) { | |
1979 | snprintf(line, 256, "Last-modified: %s\r\n", | |
1980 | mkrfc1123(source->last_modified)); | |
1981 | strcat(reply, line); | |
1982 | } | |
1983 | strcat(reply, "\r\n"); | |
1984 | return reply; | |
1985 | } | |
1986 | ||
1987 | /* | |
1988 | * This function is designed to serve a fairly specific purpose. | |
1989 | * Occasionally our vBNS-connected caches can talk to each other, but not | |
1990 | * the rest of the world. Here we try to detect frequent failures which | |
1991 | * make the cache unusable (e.g. DNS lookup and connect() failures). If | |
1992 | * the failure:success ratio goes above 1.0 then we go into "hit only" | |
1993 | * mode where we only return UDP_HIT or UDP_MISS_NOFETCH. Neighbors | |
1994 | * will only fetch HITs from us if they are using the ICP protocol. We | |
1995 | * stay in this mode for 5 minutes. | |
1996 | * | |
1997 | * Duane W., Sept 16, 1996 | |
1998 | */ | |
1999 | ||
2000 | static void | |
88aad2e5 | 2001 | checkFailureRatio(err_type etype, hier_code hcode) |
7a2f978b | 2002 | { |
88aad2e5 | 2003 | static double magic_factor = 100.0; |
7a2f978b | 2004 | double n_good; |
2005 | double n_bad; | |
2006 | if (hcode == HIER_NONE) | |
2007 | return; | |
88aad2e5 | 2008 | n_good = magic_factor / (1.0 + request_failure_ratio); |
7a2f978b | 2009 | n_bad = magic_factor - n_good; |
88aad2e5 | 2010 | switch (etype) { |
7a2f978b | 2011 | case ERR_DNS_FAIL: |
2012 | case ERR_CONNECT_FAIL: | |
2013 | case ERR_READ_ERROR: | |
2014 | n_bad++; | |
2015 | break; | |
2016 | default: | |
2017 | n_good++; | |
2018 | } | |
88aad2e5 | 2019 | request_failure_ratio = n_bad / n_good; |
7a2f978b | 2020 | if (hit_only_mode_until > squid_curtime) |
2021 | return; | |
88aad2e5 | 2022 | if (request_failure_ratio < 1.0) |
7a2f978b | 2023 | return; |
88aad2e5 | 2024 | debug(12, 0) ("Failure Ratio at %4.2f\n", request_failure_ratio); |
7a2f978b | 2025 | debug(12, 0) ("Going into hit-only-mode for %d minutes...\n", |
2026 | FAILURE_MODE_TIME / 60); | |
2027 | hit_only_mode_until = squid_curtime + FAILURE_MODE_TIME; | |
88aad2e5 | 2028 | request_failure_ratio = 0.8; /* reset to something less than 1.0 */ |
7a2f978b | 2029 | } |
15df8349 | 2030 | |
2031 | void | |
2032 | clientHttpConnectionsOpen(void) | |
2033 | { | |
2034 | ushortlist *u; | |
2035 | int fd; | |
15df8349 | 2036 | for (u = Config.Port.http; u; u = u->next) { |
8daca701 | 2037 | enter_suid(); |
2038 | fd = comm_open(SOCK_STREAM, | |
2039 | 0, | |
2040 | Config.Addrs.tcp_incoming, | |
2041 | u->i, | |
2042 | COMM_NONBLOCKING, | |
2043 | "HTTP Socket"); | |
2044 | leave_suid(); | |
2045 | if (fd < 0) | |
2046 | continue; | |
2047 | comm_listen(fd); | |
2048 | commSetSelect(fd, COMM_SELECT_READ, httpAccept, NULL, 0); | |
2049 | commSetDefer(fd, httpAcceptDefer, NULL); | |
2050 | debug(1, 1) ("Accepting HTTP connections on port %d, FD %d.\n", | |
2051 | (int) u->i, fd); | |
2052 | HttpSockets[NHttpSockets++] = fd; | |
15df8349 | 2053 | } |
2054 | if (NHttpSockets < 1) | |
8daca701 | 2055 | fatal("Cannot open HTTP Port"); |
15df8349 | 2056 | } |
c0fbae16 | 2057 | |
ab013258 | 2058 | int |
2059 | handleConnectionHeader(int flag, char *where, char *what) | |
2060 | { | |
2061 | char *t, *p, *wh; | |
2062 | int i; | |
2063 | LOCAL_ARRAY(char, mbuf, 256); | |
2064 | ||
2065 | if (flag) { /* lookup mode */ | |
2066 | if (where[0] == '\0' || what[0] == '\0') | |
2067 | return 0; | |
2068 | p = xstrdup(what); | |
2069 | t = strtok(p, ":"); | |
2070 | if (t == NULL) | |
2071 | return 0; | |
2072 | debug(20, 3) ("handleConnectionHeader: %s\n AND %s (%p)\n", where, t, p); | |
2073 | i = strstr(where, t) ? 1 : 0; | |
2074 | xfree(p); | |
2075 | return (i); | |
2076 | } | |
2077 | where[0] = '\0'; | |
2078 | wh = xstrdup(what); | |
2079 | t = strtok(wh, ","); | |
2080 | while (t != NULL) { | |
2081 | ||
2082 | #ifdef BE_PARANOID | |
2083 | static char no_conn[] = "Expires:Host:Content-length:Content-type:"; | |
2084 | ||
2085 | if (handleConnectionHeader(1, no_conn, t)) { | |
2086 | debug(1, 1) ("handleConnectionHeader: problematic header %s\n", t); | |
2087 | t = strtok(NULL, ",\n"); | |
2088 | continue; | |
2089 | } | |
2090 | #endif | |
2091 | if ((p = strchr(t, ':'))) | |
2092 | xstrncpy(mbuf, t, p - t + 1); | |
2093 | else | |
2094 | snprintf(mbuf, 256, "%s:", t); | |
2095 | strcat(where, mbuf); | |
2096 | t = strtok(NULL, ",\n"); | |
2097 | } | |
2098 | debug(20, 3) ("handleConnectionHeader: we have %s\n", where); | |
2099 | xfree(wh); | |
2100 | return 1; | |
2101 | } | |
2102 | ||
c0fbae16 | 2103 | void |
2104 | clientHttpConnectionsClose(void) | |
2105 | { | |
2106 | int i; | |
2107 | for (i = 0; i < NHttpSockets; i++) { | |
2108 | if (HttpSockets[i] >= 0) { | |
2109 | debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets[i]); | |
2110 | comm_close(HttpSockets[i]); | |
2111 | HttpSockets[i] = -1; | |
2112 | } | |
2113 | } | |
2114 | NHttpSockets = 0; | |
2115 | } |