rredir.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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 
9 #include "squid.h"
10 
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  * ===========================================================================
17  *
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.
21  *
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.
25  *
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 :)
29  *
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.
32  *
33  * I'm going on holidays now, so I won't be able to respond to e-mail
34  * quickly.
35  *
36  * Enjoy, Richard.
37  */
38 
39 /*
40  * rredir - redirect to local directory
41  *
42  * version 0.1, 7 sep 1996
43  * - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>)
44  */
45 
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 
54 int
56 {
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)
63  exit(EXIT_FAILURE);
64 
65  /* speed up the access() calls below */
66  if (chdir(ACCESS_LOCAL_DIR) == -1)
67  exit(EXIT_FAILURE);
68 
69  /* scan standard input */
70  while (fgets(buf, BUFFER_SIZE, stdin) != NULL) {
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;
78 
79  /* determine end of url */
80  if ((s = strchr(buf, ' ')) == NULL)
81  goto dont_redirect;
82  *s = '\0';
83 
84  /* determine first character of filename */
85  if ((s = strrchr(buf, '/')) == NULL)
86  goto dont_redirect;
87  s++;
88 
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;
93 
94  /* map filename to lower case */
95  for (t = s; *t != '\0'; t++)
96  *t = (char) tolower((int) *t);
97 
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  }
103 dont_redirect:
104  tlu = 0;
105  (void) printf("\n");
106  }
107 
108  return EXIT_SUCCESS;
109 }
110 
int main()
Definition: rredir.cc:55
#define BUFFER_SIZE
Definition: rredir.cc:52
#define ACCESS_LOCAL_DIR
Definition: rredir.cc:50
void const char * buf
Definition: stub_helper.cc:16
#define REDIRECT_TO_URL
Definition: rredir.cc:51
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors