]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/http/url_rewriters/LFS/rredir.cc
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
12 * From: richard@hekkihek.hacom.nl (Richard Huveneers)
13 * To: squid-users@nlanr.net
14 * Subject: Save 15% on your bandwidth...
15 * Date: 12 Sep 1996 21:21:55 GMT
16 * ===========================================================================
18 * I have downloaded the multi-megabyte files from Netscape and Microsoft
19 * that our users like to download from every mirror in the world,
20 * defeating the usual caching.
22 * I put these files in a separate directory and installed a basic
23 * redirector for Squid that checks if the file (so hostname and pathname
24 * are disregarded) is present in this directory.
26 * After a few days of testing (the redirector looks very stable) it looks
27 * like this is saving us approx. 15% on our cache flow. Also, our own WWW
28 * server has become more popular than ever :)
30 * I'm sure this code will be useful to others too, so I've attached it at
31 * the end of this message. Improvements, extensions etc. are welcome.
33 * I'm going on holidays now, so I won't be able to respond to e-mail
40 * rredir - redirect to local directory
42 * version 0.1, 7 sep 1996
43 * - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>)
50 #define ACCESS_LOCAL_DIR "/var/lib/httpd/htdocs/local/rredir"
51 #define REDIRECT_TO_URL "http://www.hacom.nl/local/rredir"
52 #define BUFFER_SIZE (16*1024)
57 char buf
[BUFFER_SIZE
];
61 /* make standard output line buffered */
62 if (setvbuf(stdout
, NULL
, _IOLBF
, 0) != 0)
65 /* speed up the access() calls below */
66 if (chdir(ACCESS_LOCAL_DIR
) == -1)
69 /* scan standard input */
70 while (fgets(buf
, BUFFER_SIZE
, stdin
) != NULL
) {
71 /* check for too long urls */
72 if (strchr(buf
, '\n') == NULL
) {
79 /* determine end of url */
80 if ((s
= strchr(buf
, ' ')) == NULL
)
84 /* determine first character of filename */
85 if ((s
= strrchr(buf
, '/')) == NULL
)
89 /* security: do not redirect to hidden files, the current
90 * directory or the parent directory */
91 if (*s
== '.' || *s
== '\0')
94 /* map filename to lower case */
95 for (t
= s
; *t
!= '\0'; t
++)
96 *t
= (char) tolower((int) *t
);
98 /* check for a local copy of this file */
99 if (access(s
, R_OK
) == 0) {
100 (void) printf("%s/%s\n", REDIRECT_TO_URL
, s
);