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