]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/url_rewrite/LFS/url_lfs_rewrite.pl.in
Add URL-rewriter based on local file existence
[thirdparty/squid.git] / helpers / url_rewrite / LFS / url_lfs_rewrite.pl.in
1 #!@PERL@
2
3 =pod
4
5 =head1 NAME
6
7 B<url_lfs_rewrite>
8
9 =head1 SYNOPSIS
10
11 url_lfs_rewrite [--debug] --local-dir=/var/www/ [options]
12
13 =head1 DESCRIPTION
14
15 Direct all request to files who are in a local directory to
16 a local web server hosting this directory.
17
18 This program uses Squid concurrency support.
19
20 =head1 OPTIONS
21
22 =over 12
23
24 =item B<--debug>
25
26 Display debug output.
27
28 =item B<--local-dir>
29
30 Directory path under which the scripts searches for files.
31
32 =item B<--to-scheme>
33
34 Scheme to use for the redirected URL.
35
36 Default: http
37
38 =item B<--to-host>
39
40 Domain name to use for the redirected URL.
41
42 Default: localhost
43
44 =item B<--to-path>
45
46 URL path to add as prefix for the redirected URL path.
47
48 If set it must end with a '/'.
49
50 Default: use the original URL path.
51
52 =back
53
54 =head1 KNOWN ISSUES
55
56 * The --local-dir parameter must end with a '/'. Otherwise no
57 file paths will be found.
58
59 * URL with no filename in the path can match directories on the local
60 filesystem and be wrongly redirected to the local web server.
61
62 * Any scheme name accepted by the Perl URL library can be used
63 as the --to-scheme parameter. However only schemes supported by
64 Squid will work.
65
66 * URL containing query-string are not handled well and will not
67 be rewritten even if the base script or file exists on the local
68 system.
69
70 =head1 CONFIGURATION
71
72 url_rewrite_program /path/to/url_lfs_rewrite --local-dir=\var\www\localhost
73 url_rewrite_children 20 startup=1 idle=1 concurrency=25
74 url_rewrite_access deny CONNECT
75 url_rewrite_access deny to_localhost
76
77
78 This helper can redirect to any web server but only does so if there is
79 a file matching the URL path segment in the local filesystem. Normal
80 configuration requires a web server running on localhost serving up files
81 from a local disk (eg. \var\www\localhost). Configuration of that web
82 server is not covered here.
83
84 =head1 AUTHOR
85
86 This program and documentation was written by I<Amos Jeffries <squid3@treenet.co.nz>>
87
88 Based on prior work in B<rredir.pl> by I<Peter Eisenhauer <pe@pipetronix.de>>.
89 First Version: 26. May 1997
90
91 =head1 COPYRIGHT
92
93 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
94 *
95 * Squid software is distributed under GPLv2+ license and includes
96 * contributions from numerous individuals and organizations.
97 * Please see the COPYING and CONTRIBUTORS files for details.
98
99 =head1 REPORTING BUGS
100
101 Bug reports need to be made in English.
102 See http://wiki.squid-cache.org/SquidFaq/BugReporting for details of what you need to include with your bug report.
103
104 Report bugs or bug fixes using http://bugs.squid-cache.org/
105
106 Report serious security bugs to I<Squid Bugs <squid-bugs@squid-cache.org>>
107
108 Report ideas for new improvements to the I<Squid Developers mailing list <squid-dev@squid-cache.org>>
109
110 =head1 SEE ALSO
111
112 squid (8), GPL (7),
113
114 The Squid FAQ wiki http://wiki.squid-cache.org/SquidFaq
115
116 The Squid Configuration Manual http://www.squid-cache.org/Doc/config/
117
118 =cut
119
120 use strict;
121 use Getopt::Long;
122 use Pod::Usage;
123 use File::Basename;
124 use URI::URL;
125
126 # command line parameters
127 my $debug = 0;
128 my $access_local_dir = undef;
129 my $redirect_scheme = "http";
130 my $redirect_host = "localhost";
131 my $redirect_path = "";
132
133 GetOptions(
134 'debug' => \$debug,
135 'local-dir=s' => \$access_local_dir,
136 'to-scheme=s' => \$redirect_scheme,
137 'to-host=s' => \$redirect_host,
138 'to-path=s' => \$redirect_path,
139 );
140
141 # flush after every print
142 $| = 1;
143 my $status = undef;
144
145 # Process lines of the form 'channel-ID URL ip-address/fqdn ident method'
146 # See http://wiki.squid-cache.org/Features/AddonHelpers for details
147
148 while ( <> ) {
149 my ($cid, $url, $remainder) = split;
150
151 $url = url $url;
152 my $host = lc($url->host);
153
154 # do not process hosts with unqualified hostnames
155 if ($host !~ /\./ ) {
156 $status = $cid . " ERR message=\"unqualified hostname\"";
157 print "found unqualified hostname.\n" if $debug;
158 next;
159 }
160
161 # just the file, without any host or path parts
162 # and just in case: lowercase the file name, so you should make sure
163 # all the files in the local dir are only lowercase !!
164 my $file = lc(basename($url->path));
165
166 # look if in local dir, if yes redirect
167 if ( $file && -r $access_local_dir . $file
168 && $file ne '.' && $file ne '..' ) {
169
170 $url->scheme($redirect_scheme);
171 $url->host($redirect_host);
172 $url->path($redirect_path . $file);
173
174 $status = $cid . " OK rewrite-url=\"" . $url . "\"";
175 print "file found: " . $file . "\n" if $debug;
176 } else {
177 $status = $cid . " ERR";
178 print "file not found: " . $file . "\n" if $debug;
179 }
180
181 } continue {
182 print $status . "\n";
183 }