]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Add URL-rewriter based on local file existence
authorAmos Jeffries <squid3@treenet.co.nz>
Sat, 6 Dec 2014 13:43:32 +0000 (05:43 -0800)
committerAmos Jeffries <squid3@treenet.co.nz>
Sat, 6 Dec 2014 13:43:32 +0000 (05:43 -0800)
Not exactly new. The basic code for this has been bundled with Squid in
contrib/ since 1996.

* Shuffle the contrib/rredir.* helpers code into helpers/url_rewrite/LFS/

* Add missing copyright boilerplate, CONTRIBUTORS and CREDITS entries

* Add Makefile support to install the perl helper

Redesign perl helper:

* update for strict Perl syntax

* update to current (Squid-3.4+) helper protocol

* replace hard-coded rewriter config with flexible command-line
  parameters

* add debug support

* add concurrency support

* add helper documentation

CONTRIBUTORS
CREDITS
configure.ac
contrib/Makefile.am
contrib/rredir.pl [deleted file]
helpers/url_rewrite/LFS/Makefile.am [new file with mode: 0644]
helpers/url_rewrite/LFS/required.m4 [new file with mode: 0644]
helpers/url_rewrite/LFS/rredir.cc [moved from contrib/rredir.c with 91% similarity]
helpers/url_rewrite/LFS/url_lfs_rewrite.pl.in [new file with mode: 0755]
helpers/url_rewrite/Makefile.am
helpers/url_rewrite/modules.m4

index e753f9ac1459c81867f51c6aedf391221e1aa4f9..7e04143e8f842700452c6d9b3c90a72c26c04e59 100644 (file)
@@ -224,6 +224,7 @@ Thank you!
     Pedro Lineu Orso <orso@pop.hsbcbamerindus.com.br>
     Pedro Ribeiro <pribeiro@isel.pt>
     Pete Bentley <pete@demon.net>
+    Peter Eisenhauer <pe@pipetronix.de>
     Peter Hidas <peter.hidas@safeland.hu>
     Peter Pramberger <peter@pramberger.at>
     Philip Allison <philip.allison@smoothwall.net>
diff --git a/CREDITS b/CREDITS
index 91f3ca8141f3d835938b4a63ab1888e2f3fb3d31..c7ae1188a16c058942d089ec57277e4d05d947d4 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -1387,6 +1387,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 
 ==============================================================================
 
+helpers/url_rewrite/LFS/rredir.cc:
+
+ * version 0.1, 7 sep 1996
+ * - initial version (Richard Huveneers <Richard.Huveneers@hekkihek.hacom.nl>)
+
+==============================================================================
+
 icons/silk/:
 
   Silk icon set 1.3
index 98a26e7ff8bf91f9345aa7fd2f589ea15f03ea8d..2c598b658e8b34cec3c414b1b2656ffbafbd5acf 100644 (file)
@@ -3829,6 +3829,7 @@ AC_CONFIG_FILES([
        helpers/log_daemon/file/Makefile
        helpers/url_rewrite/Makefile
        helpers/url_rewrite/fake/Makefile
+       helpers/url_rewrite/LFS/Makefile
        helpers/ssl/Makefile
        helpers/storeid_rewrite/Makefile
        helpers/storeid_rewrite/file/Makefile
index 1ee4ce9169c0a36afc9e81141175ec0c2cea97ad..c92ff15314c3985790f2cbc3d365edbc2fa94ef7 100644 (file)
@@ -17,8 +17,6 @@ EXTRA_DIST = \
        squid.options \
        config.site \
        squid.rc \
-       rredir.c \
-       rredir.pl \
        user-agents.pl \
        url-normalizer.pl \
        nextstep/Makefile \
diff --git a/contrib/rredir.pl b/contrib/rredir.pl
deleted file mode 100755 (executable)
index ec053a0..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/usr/bin/perl -T -w
-#
-# rredir.pl
-#
-# Author: Peter Eisenhauer <pe@pipetronix.de>
-# First Version: 26. May 1997
-#
-# Description: Direct all request to files who are in a local dir to
-# this directory
-# 
-use File::Basename;
-use URI::URL;
-
-# customization part
-
-# Local Domainame from which no redirects should be done
-$localdomain = 'pipetronix.de';
-# Local domainame qouted for regexps
-$regexlocaldomain = quotemeta($localdomain);
-# Path under which the scripts accesses the local dir (must end with /)
-$access_local_dir='/opt/utils/etc/httpd/htdocs/local-rredir/';
-# Information for the redirected URL (redirect_path must end with /)
-$redirect_scheme = 'http';
-$redirect_host = 'ws-server.pipetronix.de';
-$redirect_path = 'local-rredir/';
-
-# end of customization part
-
-# flush after every print
-$| = 1;
-
-# Process lines of the form 'URL ip-address/fqdn ident method'
-# See release notes of Squid 1.1 for details
-while ( <> ) {
-    ($url, $addr, $fqdn, $ident, $method) = m:(\S*) (\S*)/(\S*) (\S*) (\S*):;
-
-    $url = url $url;
-    $host = lc($url->host);
-
-    # do not process hosts in local domain or unqualified hostnames
-    if ( $host =~ /$regexlocaldomain/ || $host !~ /\./ ) {
-       next;
-    }
-
-    # just the file, without any host or path parts
-    # and just in case: lowercase the file name, so you should make sure
-    # all the files in the local dir are only lowercase !!
-    $file = lc(basename($url->path));
-
-    # look if in local dir, if yes redirect
-    if ( $file && -r $access_local_dir . $file
-       && $file ne '.' && $file ne '..' ) {
-       $url->scheme($redirect_scheme);
-       $url->host($redirect_host);
-       $url->path($redirect_path . $file);
-    }
-
-} continue {
-    print "$url $addr/$fqdn $ident $method\n"
-}
diff --git a/helpers/url_rewrite/LFS/Makefile.am b/helpers/url_rewrite/LFS/Makefile.am
new file mode 100644 (file)
index 0000000..1551d19
--- /dev/null
@@ -0,0 +1,30 @@
+## Copyright (C) 1996-2014 The Squid Software Foundation and contributors
+##
+## Squid software is distributed under GPLv2+ license and includes
+## contributions from numerous individuals and organizations.
+## Please see the COPYING and CONTRIBUTORS files for details.
+##
+
+include $(top_srcdir)/src/Common.am
+
+# C/C++ version of the helper (circa. 1996)
+# libexec_PROGRAMS = url_lfs_rewrite
+# url_lfs_rewrite_SOURCES = rredir.cc
+# url_lfs_rewrite_LDADD = $(COMPAT_LIB)
+
+# Perl helper
+libexec_SCRIPTS = url_lfs_rewrite
+man_MANS = url_lfs_rewrite.8
+
+EXTRA_DIST= \
+       required.m4 \
+       url_lfs_rewrite.8 \
+       url_lfs_rewrite.pl.in
+
+url_lfs_rewrite.8: url_lfs_rewrite
+       pod2man url_lfs_rewrite url_lfs_rewrite.8
+
+url_lfs_rewrite: url_lfs_rewrite.pl.in
+       $(subst_perlshell)
+
+CLEANFILES += url_lfs_rewrite url_lfs_rewrite.8
diff --git a/helpers/url_rewrite/LFS/required.m4 b/helpers/url_rewrite/LFS/required.m4
new file mode 100644 (file)
index 0000000..890eb21
--- /dev/null
@@ -0,0 +1,10 @@
+## Copyright (C) 1996-2014 The Squid Software Foundation and contributors
+##
+## Squid software is distributed under GPLv2+ license and includes
+## contributions from numerous individuals and organizations.
+## Please see the COPYING and CONTRIBUTORS files for details.
+##
+
+if test "x$PERL" != "x" -a "x$POD2MAN" != "x"; then
+  BUILD_HELPER="LFS"
+fi
similarity index 91%
rename from contrib/rredir.c
rename to helpers/url_rewrite/LFS/rredir.cc
index 4017191fc3367eed5ab322695b54f71eb0f7d88a..5c02b0cbc73e8f1208f039f2515639701c4bba77 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
 #include "squid.h"
 
 /*
diff --git a/helpers/url_rewrite/LFS/url_lfs_rewrite.pl.in b/helpers/url_rewrite/LFS/url_lfs_rewrite.pl.in
new file mode 100755 (executable)
index 0000000..a2b7e73
--- /dev/null
@@ -0,0 +1,183 @@
+#!@PERL@
+
+=pod
+
+=head1 NAME
+
+B<url_lfs_rewrite>
+
+=head1 SYNOPSIS
+
+ url_lfs_rewrite [--debug] --local-dir=/var/www/ [options]
+
+=head1 DESCRIPTION
+
+Direct all request to files who are in a local directory to
+a local web server hosting this directory.
+
+This program uses Squid concurrency support.
+
+=head1 OPTIONS
+
+=over 12
+
+=item B<--debug>
+
+Display debug output.
+
+=item B<--local-dir>
+
+Directory path under which the scripts searches for files.
+
+=item B<--to-scheme>
+
+Scheme to use for the redirected URL.
+
+Default: http
+
+=item B<--to-host>
+
+Domain name to use for the redirected URL.
+
+Default: localhost
+
+=item B<--to-path>
+
+URL path to add as prefix for the redirected URL path.
+
+If set it must end with a '/'.
+
+Default: use the original URL path.
+
+=back
+
+=head1 KNOWN ISSUES
+
+* The --local-dir parameter must end with a '/'. Otherwise no
+  file paths will be found.
+
+* URL with no filename in the path can match directories on the local
+  filesystem and be wrongly redirected to the local web server.
+
+* Any scheme name accepted by the Perl URL library can be used
+  as the --to-scheme parameter. However only schemes supported by
+  Squid will work.
+
+* URL containing query-string are not handled well and will not
+  be rewritten even if the base script or file exists on the local
+  system.
+
+=head1 CONFIGURATION
+
+  url_rewrite_program /path/to/url_lfs_rewrite --local-dir=\var\www\localhost
+  url_rewrite_children 20 startup=1 idle=1 concurrency=25
+  url_rewrite_access deny CONNECT
+  url_rewrite_access deny to_localhost
+
+
+This helper can redirect to any web server but only does so if there is
+a file matching the URL path segment in the local filesystem. Normal
+configuration requires a web server running on localhost serving up files
+from a local disk (eg. \var\www\localhost). Configuration of that web
+server is not covered here.
+
+=head1 AUTHOR
+
+This program and documentation was written by I<Amos Jeffries <squid3@treenet.co.nz>>
+
+Based on prior work in B<rredir.pl> by I<Peter Eisenhauer <pe@pipetronix.de>>.
+First Version: 26. May 1997
+
+=head1 COPYRIGHT
+
+ * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+
+=head1 REPORTING BUGS
+
+Bug reports need to be made in English.
+See http://wiki.squid-cache.org/SquidFaq/BugReporting for details of what you need to include with your bug report.
+
+Report bugs or bug fixes using http://bugs.squid-cache.org/
+
+Report serious security bugs to I<Squid Bugs <squid-bugs@squid-cache.org>>
+
+Report ideas for new improvements to the I<Squid Developers mailing list <squid-dev@squid-cache.org>>
+
+=head1 SEE ALSO
+
+squid (8), GPL (7),
+
+The Squid FAQ wiki http://wiki.squid-cache.org/SquidFaq
+
+The Squid Configuration Manual http://www.squid-cache.org/Doc/config/
+
+=cut
+
+use strict;
+use Getopt::Long;
+use Pod::Usage;
+use File::Basename;
+use URI::URL;
+
+# command line parameters
+my $debug = 0;
+my $access_local_dir = undef;
+my $redirect_scheme = "http";
+my $redirect_host = "localhost";
+my $redirect_path = "";
+
+GetOptions(
+       'debug' => \$debug,
+       'local-dir=s' => \$access_local_dir,
+       'to-scheme=s' => \$redirect_scheme,
+       'to-host=s' => \$redirect_host,
+       'to-path=s' => \$redirect_path,
+);
+
+# flush after every print
+$| = 1;
+my $status = undef;
+
+# Process lines of the form 'channel-ID URL ip-address/fqdn ident method'
+# See http://wiki.squid-cache.org/Features/AddonHelpers for details
+
+while ( <> ) {
+    my ($cid, $url, $remainder) = split;
+
+    $url = url $url;
+    my $host = lc($url->host);
+
+    # do not process hosts with unqualified hostnames
+    if ($host !~ /\./ ) {
+       $status = $cid . " ERR message=\"unqualified hostname\"";
+       print "found unqualified hostname.\n" if $debug;
+       next;
+    }
+
+    # just the file, without any host or path parts
+    # and just in case: lowercase the file name, so you should make sure
+    # all the files in the local dir are only lowercase !!
+    my $file = lc(basename($url->path));
+
+    # look if in local dir, if yes redirect
+    if ( $file && -r $access_local_dir . $file
+       && $file ne '.' && $file ne '..' ) {
+
+       $url->scheme($redirect_scheme);
+       $url->host($redirect_host);
+       $url->path($redirect_path . $file);
+
+       $status = $cid . " OK rewrite-url=\"" . $url . "\"";
+       print "file found: " . $file . "\n" if $debug;
+    } else {
+       $status = $cid . " ERR";
+       print "file not found: " . $file . "\n" if $debug;
+    }
+
+} continue {
+    print $status . "\n";
+}
index bea9db2cebb4a69040295ae882ca8eeaf0281504..60c49b6412e76cd643740ab68aa2098a11eee6f5 100644 (file)
@@ -5,5 +5,5 @@
 ## Please see the COPYING and CONTRIBUTORS files for details.
 ##
 
-DIST_SUBDIRS   = fake
+DIST_SUBDIRS   = fake LFS
 SUBDIRS                = $(URL_REWRITE_HELPERS)
index c55d2b1ecf8df2c46a9a8dd415f2038ae804b75d..86e6bef21274be1089d3a2b372fa1cd4957f91de 100644 (file)
@@ -27,6 +27,9 @@ if test "x$enable_url_rewrite_helpers" != "xno" ; then
     if test "x$helper" = "xfake" ; then
       m4_include([helpers/url_rewrite/fake/required.m4])
 
+    elif test "x$helper" = "xLFS" ; then
+      m4_include([helpers/url_rewrite/LFS/required.m4])
+
     # modules not yet converted to autoconf macros (or third party drop-in's)
     elif test -f "$dir/config.test" && sh "$dir/config.test" "$squid_host_os"; then
       BUILD_HELPER="$helper"