~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to contrib/rredir.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: rredir.c,v 1.5 2003/01/23 00:34:52 robertc Exp $ */
 
2
 
 
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
 * ===========================================================================
 
9
 * 
 
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.
 
13
 * 
 
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.
 
17
 * 
 
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 :)
 
21
 * 
 
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.
 
24
 * 
 
25
 * I'm going on holidays now, so I won't be able to respond to e-mail
 
26
 * quickly.
 
27
 * 
 
28
 * Enjoy, Richard.
 
29
 */
 
30
 
 
31
/*
 
32
 * rredir - redirect to local directory
 
33
 * 
 
34
 * version 0.1, 7 sep 1996
 
35
 * - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>)
 
36
 */
 
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
 
 
47
int
 
48
main()
 
49
{
 
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)
 
56
        return 1;
 
57
 
 
58
    /* speed up the access() calls below */
 
59
    if (chdir(ACCESS_LOCAL_DIR) == -1)
 
60
        return 1;
 
61
 
 
62
    /* scan standard input */
 
63
    while (fgets(buf, BUFFER_SIZE, stdin) != NULL) {
 
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;
 
71
 
 
72
        /* determine end of url */
 
73
        if ((s = strchr(buf, ' ')) == NULL)
 
74
            goto dont_redirect;
 
75
        *s = '\0';
 
76
 
 
77
        /* determine first character of filename */
 
78
        if ((s = strrchr(buf, '/')) == NULL)
 
79
            goto dont_redirect;
 
80
        s++;
 
81
 
 
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;
 
86
 
 
87
        /* map filename to lower case */
 
88
        for (t = s; *t != '\0'; t++)
 
89
            *t = (char) tolower((int) *t);
 
90
 
 
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");
 
99
    }
 
100
 
 
101
    return 0;
 
102
}