]>
Commit | Line | Data |
---|---|---|
75072239 | 1 | #include "config.h" |
184d950e | 2 | |
684c2720 | 3 | /* |
4 | * From: richard@hekkihek.hacom.nl (Richard Huveneers) | |
5 | * To: squid-users@nlanr.net | |
6 | * Subject: Save 15% on your bandwidth... | |
7 | * Date: 12 Sep 1996 21:21:55 GMT | |
8 | * =========================================================================== | |
26ac0430 | 9 | * |
684c2720 | 10 | * I have downloaded the multi-megabyte files from Netscape and Microsoft |
11 | * that our users like to download from every mirror in the world, | |
12 | * defeating the usual caching. | |
26ac0430 | 13 | * |
684c2720 | 14 | * I put these files in a separate directory and installed a basic |
15 | * redirector for Squid that checks if the file (so hostname and pathname | |
16 | * are disregarded) is present in this directory. | |
26ac0430 | 17 | * |
684c2720 | 18 | * After a few days of testing (the redirector looks very stable) it looks |
19 | * like this is saving us approx. 15% on our cache flow. Also, our own WWW | |
20 | * server has become more popular than ever :) | |
26ac0430 | 21 | * |
684c2720 | 22 | * I'm sure this code will be useful to others too, so I've attached it at |
23 | * the end of this message. Improvements, extensions etc. are welcome. | |
26ac0430 | 24 | * |
684c2720 | 25 | * I'm going on holidays now, so I won't be able to respond to e-mail |
26 | * quickly. | |
26ac0430 | 27 | * |
684c2720 | 28 | * Enjoy, Richard. |
29 | */ | |
99585d35 | 30 | |
31 | /* | |
684c2720 | 32 | * rredir - redirect to local directory |
26ac0430 | 33 | * |
684c2720 | 34 | * version 0.1, 7 sep 1996 |
35 | * - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>) | |
36 | */ | |
99585d35 | 37 | |
38 | #include <stdio.h> | |
39 | #include <unistd.h> | |
40 | #include <string.h> | |
41 | #include <ctype.h> | |
42 | ||
43 | #define ACCESS_LOCAL_DIR "/var/lib/httpd/htdocs/local/rredir" | |
44 | #define REDIRECT_TO_URL "http://www.hacom.nl/local/rredir" | |
45 | #define BUFFER_SIZE (16*1024) | |
46 | ||
684c2720 | 47 | int |
48 | main() | |
99585d35 | 49 | { |
684c2720 | 50 | char buf[BUFFER_SIZE]; |
51 | char *s, *t; | |
52 | int tlu = 0; | |
53 | ||
54 | /* make standard output line buffered */ | |
55 | if (setvbuf(stdout, NULL, _IOLBF, 0) != 0) | |
26ac0430 | 56 | return 1; |
684c2720 | 57 | |
58 | /* speed up the access() calls below */ | |
59 | if (chdir(ACCESS_LOCAL_DIR) == -1) | |
26ac0430 | 60 | return 1; |
684c2720 | 61 | |
62 | /* scan standard input */ | |
63 | while (fgets(buf, BUFFER_SIZE, stdin) != NULL) { | |
26ac0430 AJ |
64 | /* check for too long urls */ |
65 | if (strchr(buf, '\n') == NULL) { | |
66 | tlu = 1; | |
67 | continue; | |
68 | } | |
69 | if (tlu) | |
70 | goto dont_redirect; | |
684c2720 | 71 | |
26ac0430 AJ |
72 | /* determine end of url */ |
73 | if ((s = strchr(buf, ' ')) == NULL) | |
74 | goto dont_redirect; | |
75 | *s = '\0'; | |
684c2720 | 76 | |
26ac0430 AJ |
77 | /* determine first character of filename */ |
78 | if ((s = strrchr(buf, '/')) == NULL) | |
79 | goto dont_redirect; | |
80 | s++; | |
684c2720 | 81 | |
26ac0430 AJ |
82 | /* security: do not redirect to hidden files, the current |
83 | * directory or the parent directory */ | |
84 | if (*s == '.' || *s == '\0') | |
85 | goto dont_redirect; | |
684c2720 | 86 | |
26ac0430 AJ |
87 | /* map filename to lower case */ |
88 | for (t = s; *t != '\0'; t++) | |
89 | *t = (char) tolower((int) *t); | |
684c2720 | 90 | |
26ac0430 AJ |
91 | /* check for a local copy of this file */ |
92 | if (access(s, R_OK) == 0) { | |
93 | (void) printf("%s/%s\n", REDIRECT_TO_URL, s); | |
94 | continue; | |
95 | } | |
96 | dont_redirect: | |
97 | tlu = 0; | |
98 | (void) printf("\n"); | |
684c2720 | 99 | } |
100 | ||
101 | return 0; | |
99585d35 | 102 | } |