]> git.ipfire.org Git - thirdparty/squid.git/blame - src/forward.cc
NPR bug
[thirdparty/squid.git] / src / forward.cc
CommitLineData
41462d93 1
2/*
77b32a34 3 * $Id: forward.cc,v 1.41 1999/01/08 21:12:10 wessels Exp $
41462d93 4 *
5 * DEBUG: section 17 Request Forwarding
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
e25c139f 9 * ----------------------------------------------------------
41462d93 10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
e25c139f 13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
41462d93 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 *
41462d93 34 */
35
36
37#include "squid.h"
38
db1cd23c 39static PSC fwdStartComplete;
6801f8a8 40static void fwdDispatch(FwdState *);
910169e5 41static void fwdConnectStart(FwdState * fwdState);
42static void fwdStateFree(FwdState * fwdState);
43static PF fwdConnectTimeout;
44static PF fwdServerClosed;
45static CNCB fwdConnectDone;
68bd6892 46static int fwdCheckRetry(FwdState * fwdState);
db1cd23c 47static int fwdReforward(FwdState *);
48static void fwdStartFail(FwdState *);
8ddcc35d 49static void fwdLogReplyStatus(int tries, http_status status);
50static OBJH fwdStats;
51
52#define MAX_FWD_STATS_IDX 9
9977e14b 53static int FwdReplyCodes[MAX_FWD_STATS_IDX + 1][HTTP_INVALID_HEADER + 1];
db1cd23c 54
55static void
56fwdServerFree(FwdServer * fs)
57{
58 if (fs->peer)
59 cbdataUnlock(fs->peer);
60 memFree(fs, MEM_FWD_SERVER);
61}
62
63static void
64fwdServersFree(FwdServer ** FS)
65{
66 FwdServer *fs;
67 while ((fs = *FS)) {
68 *FS = fs->next;
69 fwdServerFree(fs);
70 }
71}
41462d93 72
73static void
4ca83e82 74fwdStateFree(FwdState * fwdState)
41462d93 75{
f563eea9 76 StoreEntry *e = fwdState->entry;
77 ErrorState *err;
6801f8a8 78 int sfd;
79 static int loop_detect = 0;
0bdf2621 80 debug(17, 3) ("fwdStateFree: %p\n", fwdState);
6801f8a8 81 assert(loop_detect++ == 0);
f563eea9 82 assert(e->mem_obj);
1640c2df 83 if (e->store_status == STORE_PENDING) {
84 if (e->mem_obj->inmem_hi == 0) {
85 assert(fwdState->fail.err_code);
86 err = errorCon(fwdState->fail.err_code, fwdState->fail.http_code);
87 err->request = requestLink(fwdState->request);
88 err->xerrno = fwdState->fail.xerrno;
89 errorAppendEntry(e, err);
1640c2df 90 }
f563eea9 91 }
db1cd23c 92 fwdServersFree(&fwdState->servers);
41462d93 93 requestUnlink(fwdState->request);
4ca83e82 94 fwdState->request = NULL;
f563eea9 95 storeUnregisterAbort(e);
96 storeUnlockObject(e);
4ca83e82 97 fwdState->entry = NULL;
6801f8a8 98 sfd = fwdState->server_fd;
99 if (sfd > -1) {
100 comm_remove_close_handler(sfd, fwdServerClosed, fwdState);
101 fwdState->server_fd = -1;
32b2931b 102 debug(17, 3) ("fwdStateFree: closing FD %d\n", sfd);
6801f8a8 103 comm_close(sfd);
104 }
41462d93 105 cbdataFree(fwdState);
6801f8a8 106 loop_detect--;
41462d93 107}
108
68bd6892 109static int
110fwdCheckRetry(FwdState * fwdState)
111{
1640c2df 112 if (fwdState->entry->store_status != STORE_PENDING)
113 return 0;
68bd6892 114 if (fwdState->entry->mem_obj->inmem_hi > 0)
115 return 0;
116 if (fwdState->n_tries > 10)
117 return 0;
118 if (squid_curtime - fwdState->start > 120)
119 return 0;
1d22e062 120 if (pumpMethod(fwdState->request->method))
121 if (0 == pumpRestart(fwdState->request))
122 return 0;
68bd6892 123 return 1;
124}
125
910169e5 126static void
127fwdServerClosed(int fd, void *data)
128{
4ca83e82 129 FwdState *fwdState = data;
68bd6892 130 debug(17, 3) ("fwdServerClosed: FD %d %s\n", fd, storeUrl(fwdState->entry));
6801f8a8 131 assert(fwdState->server_fd == fd);
132 fwdState->server_fd = -1;
68bd6892 133 if (fwdCheckRetry(fwdState)) {
32b2931b 134 debug(17, 3) ("fwdServerClosed: re-forwarding (%d tries, %d secs)\n",
9dbf253d 135 fwdState->n_tries,
136 (int) (squid_curtime - fwdState->start));
f563eea9 137 fwdConnectStart(fwdState);
138 } else {
139 fwdStateFree(fwdState);
140 }
910169e5 141}
142
41462d93 143static void
144fwdConnectDone(int server_fd, int status, void *data)
145{
146 FwdState *fwdState = data;
db1cd23c 147 FwdServer *fs = fwdState->servers;
41462d93 148 ErrorState *err;
db1cd23c 149 request_t *request = fwdState->request;
6801f8a8 150 assert(fwdState->server_fd == server_fd);
41462d93 151 if (status == COMM_ERR_DNS) {
152 debug(17, 4) ("fwdConnectDone: Unknown host: %s\n",
db1cd23c 153 request->host);
41462d93 154 err = errorCon(ERR_DNS_FAIL, HTTP_SERVICE_UNAVAILABLE);
155 err->dnsserver_msg = xstrdup(dns_error_message);
db1cd23c 156 err->request = requestLink(request);
41462d93 157 errorAppendEntry(fwdState->entry, err);
158 comm_close(server_fd);
159 } else if (status != COMM_OK) {
db1cd23c 160 assert(fs);
41462d93 161 err = errorCon(ERR_CONNECT_FAIL, HTTP_SERVICE_UNAVAILABLE);
162 err->xerrno = errno;
db1cd23c 163 if (fs->peer) {
164 err->host = xstrdup(fs->peer->host);
165 err->port = fs->peer->http_port;
166 } else {
167 err->host = xstrdup(request->host);
168 err->port = request->port;
169 }
170 err->request = requestLink(request);
41462d93 171 errorAppendEntry(fwdState->entry, err);
db1cd23c 172 if (fs->peer)
173 peerCheckConnectStart(fs->peer);
41462d93 174 comm_close(server_fd);
175 } else {
176 fd_note(server_fd, storeUrl(fwdState->entry));
177 fd_table[server_fd].uses++;
6801f8a8 178 fwdDispatch(fwdState);
41462d93 179 }
41462d93 180}
181
182static void
183fwdConnectTimeout(int fd, void *data)
184{
185 FwdState *fwdState = data;
186 StoreEntry *entry = fwdState->entry;
187 ErrorState *err;
188 debug(17, 3) ("fwdConnectTimeout: FD %d: '%s'\n", fd, storeUrl(entry));
6801f8a8 189 assert(fd == fwdState->server_fd);
41462d93 190 if (entry->mem_obj->inmem_hi == 0) {
191 err = errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT);
192 err->request = requestLink(fwdState->request);
193 errorAppendEntry(entry, err);
41462d93 194 }
195 comm_close(fd);
196}
197
198static void
199fwdConnectStart(FwdState * fwdState)
200{
201 const char *url = storeUrl(fwdState->entry);
202 int fd;
203 ErrorState *err;
db1cd23c 204 FwdServer *fs = fwdState->servers;
205 const char *host;
206 unsigned short port;
207 assert(fs);
cddc721b 208 assert(fwdState->server_fd == -1);
41462d93 209 debug(17, 3) ("fwdConnectStart: %s\n", url);
db1cd23c 210 if (fs->peer) {
211 host = fs->peer->host;
212 port = fs->peer->http_port;
213 } else {
214 host = fwdState->request->host;
215 port = fwdState->request->port;
216 }
0bdf2621 217 hierarchyNote(&fwdState->request->hier, fs->code, host);
db1cd23c 218 if ((fd = pconnPop(host, port)) >= 0) {
41462d93 219 debug(17, 3) ("fwdConnectStart: reusing pconn FD %d\n", fd);
9dbf253d 220 fwdState->server_fd = fd;
9977e14b 221 fwdState->n_tries++;
910169e5 222 comm_add_close_handler(fd, fwdServerClosed, fwdState);
41462d93 223 fwdConnectDone(fd, COMM_OK, fwdState);
224 return;
225 }
226 fd = comm_open(SOCK_STREAM,
227 0,
228 Config.Addrs.tcp_outgoing,
229 0,
230 COMM_NONBLOCKING,
231 url);
232 if (fd < 0) {
233 debug(50, 4) ("fwdConnectStart: %s\n", xstrerror());
234 err = errorCon(ERR_SOCKET_FAILURE, HTTP_INTERNAL_SERVER_ERROR);
235 err->xerrno = errno;
236 err->request = requestLink(fwdState->request);
237 errorAppendEntry(fwdState->entry, err);
910169e5 238 fwdStateFree(fwdState);
41462d93 239 return;
240 }
6801f8a8 241 fwdState->server_fd = fd;
68bd6892 242 fwdState->n_tries++;
910169e5 243 comm_add_close_handler(fd, fwdServerClosed, fwdState);
41462d93 244 commSetTimeout(fd,
245 Config.Timeout.connect,
246 fwdConnectTimeout,
247 fwdState);
db1cd23c 248 commConnectStart(fd, host, port, fwdConnectDone, fwdState);
41462d93 249}
250
251static void
db1cd23c 252fwdStartComplete(FwdServer * servers, void *data)
41462d93 253{
254 FwdState *fwdState = data;
db1cd23c 255 if (servers != NULL) {
256 fwdState->servers = servers;
257 fwdConnectStart(fwdState);
41462d93 258 } else {
db1cd23c 259 fwdStartFail(fwdState);
41462d93 260 }
41462d93 261}
262
263static void
db1cd23c 264fwdStartFail(FwdState * fwdState)
41462d93 265{
41462d93 266 ErrorState *err;
41462d93 267 err = errorCon(ERR_CANNOT_FORWARD, HTTP_SERVICE_UNAVAILABLE);
268 err->request = requestLink(fwdState->request);
269 errorAppendEntry(fwdState->entry, err);
910169e5 270 fwdStateFree(fwdState);
41462d93 271}
910169e5 272
41462d93 273static void
6801f8a8 274fwdDispatch(FwdState * fwdState)
41462d93 275{
276 peer *p;
277 request_t *request = fwdState->request;
278 StoreEntry *entry = fwdState->entry;
279 debug(17, 5) ("fwdDispatch: FD %d: Fetching '%s %s'\n",
910169e5 280 fwdState->client_fd,
41462d93 281 RequestMethodStr[request->method],
282 storeUrl(entry));
d46a87a8 283 /*assert(!EBIT_TEST(entry->flags, ENTRY_DISPATCHED)); */
41462d93 284 assert(entry->ping_status != PING_WAITING);
4ca83e82 285 assert(entry->lock_count);
d46a87a8 286 EBIT_SET(entry->flags, ENTRY_DISPATCHED);
41462d93 287 netdbPingSite(request->host);
e0ebe27c 288 /*
289 * Assert that server_fd is set. This is to guarantee that fwdState
290 * is attached to something and will be deallocated when server_fd
291 * is closed.
292 */
293 assert(fwdState->server_fd > -1);
41462d93 294 if (fwdState->servers && (p = fwdState->servers->peer)) {
295 p->stats.fetches++;
db1cd23c 296 httpStart(fwdState);
41462d93 297 } else {
298 switch (request->protocol) {
299 case PROTO_HTTP:
db1cd23c 300 httpStart(fwdState);
41462d93 301 break;
302 case PROTO_GOPHER:
db1cd23c 303 gopherStart(fwdState);
41462d93 304 break;
305 case PROTO_FTP:
db1cd23c 306 ftpStart(fwdState);
41462d93 307 break;
308 case PROTO_WAIS:
db1cd23c 309 waisStart(fwdState);
41462d93 310 break;
311 case PROTO_CACHEOBJ:
e0ebe27c 312 case PROTO_INTERNAL:
41462d93 313 case PROTO_URN:
ae0b4725 314 fatal_dump("Should never get here");
41462d93 315 break;
316 case PROTO_WHOIS:
db1cd23c 317 whoisStart(fwdState);
41462d93 318 break;
41462d93 319 default:
ce45d320 320 debug(17, 1) ("fwdDispatch: Cannot retrieve '%s'\n",
321 storeUrl(entry));
c68e9c6b 322 fwdFail(fwdState, ERR_UNSUP_REQ, HTTP_BAD_REQUEST, -1);
323 comm_close(fwdState->server_fd);
ce45d320 324 break;
41462d93 325 }
326 }
327}
328
db1cd23c 329static int
330fwdReforward(FwdState * fwdState)
331{
332 StoreEntry *e = fwdState->entry;
333 FwdServer *fs = fwdState->servers;
334 http_status s;
335 assert(e->store_status == STORE_PENDING);
336 assert(e->mem_obj);
d6eb18d6 337 if (!EBIT_TEST(e->flags, ENTRY_FWD_HDR_WAIT)) {
338 debug(17, 3) ("fwdReforward: No, ENTRY_FWD_HDR_WAIT isn't set\n");
339 return 0;
340 }
db1cd23c 341 if (fwdState->n_tries > 9)
342 return 0;
d2946396 343 if (pumpMethod(fwdState->request->method))
344 if (0 == pumpRestart(fwdState->request))
345 return 0;
db1cd23c 346 assert(fs);
347 fwdState->servers = fs->next;
348 fwdServerFree(fs);
349 if (fwdState->servers == NULL) {
350 debug(17, 3) ("fwdReforward: No forward-servers left\n");
351 return 0;
352 }
353 s = e->mem_obj->reply->sline.status;
354 debug(17, 3) ("fwdReforward: status %d\n", (int) s);
355 switch (s) {
356 case HTTP_FORBIDDEN:
357 case HTTP_INTERNAL_SERVER_ERROR:
358 case HTTP_NOT_IMPLEMENTED:
359 case HTTP_BAD_GATEWAY:
360 case HTTP_SERVICE_UNAVAILABLE:
361 case HTTP_GATEWAY_TIMEOUT:
362 return 1;
363 default:
364 return 0;
365 }
366 /* NOTREACHED */
367}
368
41462d93 369/* PUBLIC FUNCTIONS */
370
41462d93 371void
db1cd23c 372fwdStart(int fd, StoreEntry * e, request_t * r, struct in_addr client_addr)
41462d93 373{
374 FwdState *fwdState;
d1061d2b 375 aclCheck_t ch;
376 int answer;
377 ErrorState *err;
5843eb62 378 /*
db1cd23c 379 * client_addr == no_addr indicates this is an "internal" request
5843eb62 380 * from peer_digest.c, asn.c, netdb.c, etc and should always
381 * be allowed. yuck, I know.
d1061d2b 382 */
db1cd23c 383 if (client_addr.s_addr != no_addr.s_addr) {
5843eb62 384 /*
385 * Check if this host is allowed to fetch MISSES from us (miss_access)
386 */
387 memset(&ch, '\0', sizeof(aclCheck_t));
db1cd23c 388 ch.src_addr = client_addr;
5843eb62 389 ch.request = r;
390 answer = aclCheckFast(Config.accessList.miss, &ch);
391 if (answer == 0) {
392 err = errorCon(ERR_FORWARDING_DENIED, HTTP_FORBIDDEN);
393 err->request = requestLink(r);
db1cd23c 394 err->src_addr = client_addr;
5843eb62 395 errorAppendEntry(e, err);
396 return;
397 }
d1061d2b 398 }
399 debug(17, 3) ("fwdStart: '%s'\n", storeUrl(e));
400 e->mem_obj->request = requestLink(r);
401 e->mem_obj->fd = fd;
402 switch (r->protocol) {
0cdcddb9 403 /*
404 * Note, don't create fwdState for these requests
405 */
e0ebe27c 406 case PROTO_INTERNAL:
d1061d2b 407 internalStart(r, e);
e0ebe27c 408 return;
409 case PROTO_CACHEOBJ:
d1061d2b 410 cachemgrStart(fd, r, e);
e0ebe27c 411 return;
ae0b4725 412 case PROTO_URN:
413 urnStart(r, e);
414 return;
e0ebe27c 415 default:
416 break;
417 }
798b0889 418 fwdState = memAllocate(MEM_FWD_STATE);
db1cd23c 419 cbdataAdd(fwdState, memFree, MEM_FWD_STATE);
d1061d2b 420 fwdState->entry = e;
910169e5 421 fwdState->client_fd = fd;
6801f8a8 422 fwdState->server_fd = -1;
d1061d2b 423 fwdState->request = requestLink(r);
f563eea9 424 fwdState->start = squid_curtime;
d1061d2b 425 storeLockObject(e);
426 storeRegisterAbort(e, fwdAbort, fwdState);
db1cd23c 427 peerSelect(r, e, fwdStartComplete, fwdState);
41462d93 428}
429
41462d93 430int
431fwdCheckDeferRead(int fdnotused, void *data)
432{
433 StoreEntry *e = data;
434 MemObject *mem = e->mem_obj;
435 if (mem == NULL)
436 return 0;
447e176b 437#if DELAY_POOLS
438 if (delayMostBytesWanted(mem, 1) == 0)
439 return 1;
440#endif
41462d93 441 if (mem->inmem_hi - storeLowestMemReaderOffset(e) < READ_AHEAD_GAP)
442 return 0;
443 return 1;
444}
910169e5 445
446void
4ca83e82 447fwdFail(FwdState * fwdState, int err_code, http_status http_code, int xerrno)
910169e5 448{
d46a87a8 449 assert(EBIT_TEST(fwdState->entry->flags, ENTRY_FWD_HDR_WAIT));
32b2931b 450 debug(17, 3) ("fwdFail: %s \"%s\"\n\t%s\n",
4ca83e82 451 err_type_str[err_code],
452 httpStatusString(http_code),
453 storeUrl(fwdState->entry));
454 fwdState->fail.err_code = err_code;
455 fwdState->fail.http_code = http_code;
456 fwdState->fail.xerrno = xerrno;
910169e5 457}
6801f8a8 458
459/*
460 * Called when someone else calls StoreAbort() on this entry
461 */
462void
463fwdAbort(void *data)
464{
9dbf253d 465 FwdState *fwdState = data;
32b2931b 466 debug(17, 3) ("fwdAbort: %s\n", storeUrl(fwdState->entry));
9dbf253d 467 fwdStateFree(fwdState);
6801f8a8 468}
0b49cd31 469
470/*
471 * Frees fwdState without closing FD or generating an abort
472 */
473void
9dbf253d 474fwdUnregister(int fd, FwdState * fwdState)
0b49cd31 475{
9dbf253d 476 debug(17, 3) ("fwdUnregister: %s\n", storeUrl(fwdState->entry));
477 assert(fd = fwdState->server_fd);
478 comm_remove_close_handler(fd, fwdServerClosed, fwdState);
479 fwdState->server_fd = -1;
0b49cd31 480}
db1cd23c 481
482/*
483 * server-side modules call fwdComplete() when they are done
484 * downloading an object. Then, we either 1) re-forward the
485 * request somewhere else if needed, or 2) call storeComplete()
486 * to finish it off
487 */
488void
489fwdComplete(FwdState * fwdState)
490{
491 StoreEntry *e = fwdState->entry;
492 assert(e->store_status == STORE_PENDING);
8a28f65f 493 debug(17, 3) ("fwdComplete: %s\n\tstatus %d\n", storeUrl(e),
db1cd23c 494 e->mem_obj->reply->sline.status);
9977e14b 495 fwdLogReplyStatus(fwdState->n_tries, e->mem_obj->reply->sline.status);
d6eb18d6 496 if (fwdReforward(fwdState)) {
77b32a34 497 debug(17, 3) ("fwdComplete: re-forwarding %d %s\n",
0bdf2621 498 e->mem_obj->reply->sline.status,
499 storeUrl(e));
8a28f65f 500 if (fwdState->server_fd > -1)
0bdf2621 501 fwdUnregister(fwdState->server_fd, fwdState);
db1cd23c 502 storeEntryReset(e);
503 fwdStartComplete(fwdState->servers, fwdState);
504 } else {
d6eb18d6 505 debug(17, 3) ("fwdComplete: not re-forwarding status %d\n",
506 e->mem_obj->reply->sline.status);
db1cd23c 507 EBIT_CLR(e->flags, ENTRY_FWD_HDR_WAIT);
508 storeComplete(e);
d6eb18d6 509 /*
510 * If fwdState isn't associated with a server FD, it
511 * won't get freed unless we do it here.
512 */
513 if (fwdState->server_fd < 0)
514 fwdStateFree(fwdState);
db1cd23c 515 }
516}
8ddcc35d 517
518void
519fwdInit(void)
520{
9977e14b 521 cachemgrRegister("forward",
522 "Request Forwarding Statistics",
523 fwdStats, 0, 1);
8ddcc35d 524}
525
526static void
527fwdLogReplyStatus(int tries, http_status status)
528{
529 if (status > HTTP_INVALID_HEADER)
530 return;
531 assert(tries);
532 tries--;
533 if (tries > MAX_FWD_STATS_IDX)
534 tries = MAX_FWD_STATS_IDX;
535 FwdReplyCodes[tries][status]++;
536}
537
538static void
9977e14b 539fwdStats(StoreEntry * s)
8ddcc35d 540{
9977e14b 541 int i;
542 int j;
543 storeAppendPrintf(s, "Status");
544 for (j = 0; j <= MAX_FWD_STATS_IDX; j++) {
545 storeAppendPrintf(s, "\ttry#%d", j + 1);
546 }
547 storeAppendPrintf(s, "\n");
548 for (i = 0; i <= (int) HTTP_INVALID_HEADER; i++) {
549 if (FwdReplyCodes[0][i] == 0)
550 continue;
551 storeAppendPrintf(s, "%3d", i);
552 for (j = 0; j <= MAX_FWD_STATS_IDX; j++) {
553 storeAppendPrintf(s, "\t%d", FwdReplyCodes[j][i]);
8ddcc35d 554 }
9977e14b 555 storeAppendPrintf(s, "\n");
556 }
8ddcc35d 557}