1
/* $Id: rredir.c,v 1.5 2003/01/23 00:34:52 robertc Exp $ */
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
* ===========================================================================
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.
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.
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 :)
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.
25
* I'm going on holidays now, so I won't be able to respond to e-mail
32
* rredir - redirect to local directory
34
* version 0.1, 7 sep 1996
35
* - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>)
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)
50
char buf[BUFFER_SIZE];
54
/* make standard output line buffered */
55
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
58
/* speed up the access() calls below */
59
if (chdir(ACCESS_LOCAL_DIR) == -1)
62
/* scan standard input */
63
while (fgets(buf, BUFFER_SIZE, stdin) != NULL) {
64
/* check for too long urls */
65
if (strchr(buf, '\n') == NULL) {
72
/* determine end of url */
73
if ((s = strchr(buf, ' ')) == NULL)
77
/* determine first character of filename */
78
if ((s = strrchr(buf, '/')) == NULL)
82
/* security: do not redirect to hidden files, the current
83
* directory or the parent directory */
84
if (*s == '.' || *s == '\0')
87
/* map filename to lower case */
88
for (t = s; *t != '\0'; t++)
89
*t = (char) tolower((int) *t);
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);