]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http/url_rewriters/LFS/url_lfs_rewrite.pl.in
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / http / url_rewriters / LFS / url_lfs_rewrite.pl.in
1 #!@PERL@
2
3 use strict;
4 use Getopt::Long;
5 use Pod::Usage;
6
7 =pod
8
9 =head1 NAME
10
11 url_lfs_rewrite - a URL-rewriter based on local file existence
12
13 =head1 SYNOPSIS
14
15 url_lfs_rewrite [--debug] --local-dir=/var/www/ [options]
16
17 =head1 DESCRIPTION
18
19 Direct all request to files who are in a local directory to
20 a local web server hosting this directory.
21
22 This program uses Squid concurrency support.
23
24 =head1 OPTIONS
25
26 =over 12
27
28 =item B<--debug>
29
30 Write debug info to stderr.
31
32 =item B<--local-dir>
33
34 Directory path under which the scripts searches for files.
35
36 =item B<--to-scheme>
37
38 Scheme to use for the redirected URL.
39
40 Default: http
41
42 =item B<--to-host>
43
44 Domain name to use for the redirected URL.
45
46 Default: localhost
47
48 =item B<--to-path>
49
50 URL path to add as prefix for the redirected URL path.
51
52 If set it must end with a '/'.
53
54 Default: use the original URL path.
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
82 This helper can redirect to any web server but only does so if there is
83 a file matching the URL path segment in the local filesystem. Normal
84 configuration requires a web server running on localhost serving up files
85 from a local disk (eg. \var\www\localhost). Configuration of that web
86 server is not covered here.
87
88 =head1 AUTHOR
89
90 This program and documentation was written by I<Amos Jeffries <squid3@treenet.co.nz>>
91
92 Based on prior work in B<rredir.pl> by I<Peter Eisenhauer <pe@pipetronix.de>>.
93 First Version: 26. May 1997
94
95 =head1 COPYRIGHT
96
97 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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
105 Bug reports need to be made in English.
106 See http://wiki.squid-cache.org/SquidFaq/BugReporting for details of what you need to include with your bug report.
107
108 Report bugs or bug fixes using http://bugs.squid-cache.org/
109
110 Report serious security bugs to I<Squid Bugs <squid-bugs@lists.squid-cache.org>>
111
112 Report ideas for new improvements to the I<Squid Developers mailing list <squid-dev@lists.squid-cache.org>>
113
114 =head1 SEE ALSO
115
116 squid (8), GPL (7),
117
118 The Squid FAQ wiki http://wiki.squid-cache.org/SquidFaq
119
120 The Squid Configuration Manual http://www.squid-cache.org/Doc/config/
121
122 =cut
123
124 use File::Basename;
125 use URI::URL;
126
127 # command line parameters
128 my $debug = 0;
129 my $access_local_dir = undef;
130 my $redirect_scheme = "http";
131 my $redirect_host = "localhost";
132 my $redirect_path = "";
133
134 GetOptions(
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;
144 my $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
149 while ( <> ) {
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 }