]> git.ipfire.org Git - thirdparty/squid.git/blame - src/http/url_rewriters/LFS/rredir.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / http / url_rewriters / LFS / rredir.cc
CommitLineData
dc5f403f 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
dc5f403f
AJ
3 *
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.
7 */
8
f7f3304a 9#include "squid.h"
184d950e 10
684c2720 11/*
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 * ===========================================================================
26ac0430 17 *
684c2720 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.
26ac0430 21 *
684c2720 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.
26ac0430 25 *
684c2720 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 :)
26ac0430 29 *
684c2720 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.
26ac0430 32 *
684c2720 33 * I'm going on holidays now, so I won't be able to respond to e-mail
34 * quickly.
26ac0430 35 *
684c2720 36 * Enjoy, Richard.
37 */
99585d35 38
39/*
684c2720 40 * rredir - redirect to local directory
26ac0430 41 *
684c2720 42 * version 0.1, 7 sep 1996
43 * - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>)
44 */
99585d35 45
99585d35 46#include <unistd.h>
47#include <string.h>
48#include <ctype.h>
49
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)
53
684c2720 54int
55main()
99585d35 56{
684c2720 57 char buf[BUFFER_SIZE];
58 char *s, *t;
59 int tlu = 0;
60
61 /* make standard output line buffered */
62 if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
24885773 63 exit(EXIT_FAILURE);
684c2720 64
65 /* speed up the access() calls below */
66 if (chdir(ACCESS_LOCAL_DIR) == -1)
24885773 67 exit(EXIT_FAILURE);
684c2720 68
69 /* scan standard input */
70 while (fgets(buf, BUFFER_SIZE, stdin) != NULL) {
26ac0430
AJ
71 /* check for too long urls */
72 if (strchr(buf, '\n') == NULL) {
73 tlu = 1;
74 continue;
75 }
76 if (tlu)
77 goto dont_redirect;
684c2720 78
26ac0430
AJ
79 /* determine end of url */
80 if ((s = strchr(buf, ' ')) == NULL)
81 goto dont_redirect;
82 *s = '\0';
684c2720 83
26ac0430
AJ
84 /* determine first character of filename */
85 if ((s = strrchr(buf, '/')) == NULL)
86 goto dont_redirect;
87 s++;
684c2720 88
26ac0430
AJ
89 /* security: do not redirect to hidden files, the current
90 * directory or the parent directory */
91 if (*s == '.' || *s == '\0')
92 goto dont_redirect;
684c2720 93
26ac0430
AJ
94 /* map filename to lower case */
95 for (t = s; *t != '\0'; t++)
96 *t = (char) tolower((int) *t);
684c2720 97
26ac0430
AJ
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);
101 continue;
102 }
103dont_redirect:
104 tlu = 0;
105 (void) printf("\n");
684c2720 106 }
107
24885773 108 return EXIT_SUCCESS;
99585d35 109}
f53969cc 110