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