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