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