]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/storeid_rewrite/file/storeid_file_rewrite.pl.in
Boilerplate: update copyright blurbs on Squid helpers
[thirdparty/squid.git] / helpers / storeid_rewrite / file / storeid_file_rewrite.pl.in
1 #!@PERL@
2 use strict;
3 use warnings;
4 $|=1;
5
6 =pod
7
8 =head1 NAME
9
10 storeid_file_rewrite - File based Store-ID helper for Squid
11
12 =head1 SYNOPSIS
13
14 storeid_file_rewrite filepath
15
16 =head1 DESCRIPTION
17
18 This program acts as a store_id helper program, rewriting URLs passed
19 by Squid into storage-ids that can be used to achieve better caching
20 for websites that use different URLs for the same content.
21
22 It takes a text file with two tab separated columns.
23 Column 1: Regular expression to match against the URL
24 Column 2: Rewrite rule to generate a Store-ID
25 Eg:
26 ^http:\/\/[^\.]+\.dl\.sourceforge\.net\/(.*) http://dl.sourceforge.net.squid.internal/$1
27
28 Rewrite rules are matched in the same order as they appear in the rules file.
29 So for best performance, sort it in order of frequency of occurrence.
30
31 This program will automatically detect the existence of a concurrecy channel-ID and adjust appropriately.
32 It may be used with any value 0 or above for the store_id_children concurrency= parameter.
33
34 For more information please see http://wiki.squid-cache.org/Features/StoreID
35
36 =cut
37
38 my @rules; # array of [regex, replacement string]
39
40 die "Usage: $0 <rewrite-file>\n" unless $#ARGV == 0;
41
42 # read config file
43 open RULES, $ARGV[0] or die "Error opening $ARGV[0]: $!";
44 while (<RULES>) {
45 chomp;
46 next if /^\s*#?$/;
47 if (/^\s*([^\t]+?)\s*\t+\s*([^\t]+?)\s*$/) {
48 push(@rules, [qr/$1/, $2]);
49 } else {
50 print STDERR "$0: Parse error in $ARGV[0] (line $.)\n";
51 }
52 }
53 close RULES;
54
55 # read urls from squid and do the replacement
56 URL: while (<STDIN>) {
57 chomp;
58 last if $_ eq 'quit';
59
60 my $channel = "";
61 if (s/^(\d+\s+)//o) {
62 $channel = $1;
63 }
64
65 foreach my $rule (@rules) {
66 if (my @match = /$rule->[0]/) {
67 $_ = $rule->[1];
68
69 for (my $i=1; $i<=scalar(@match); $i++) {
70 s/\$$i/$match[$i-1]/g;
71 }
72 print $channel, "OK store-id=$_\n";
73 next URL;
74 }
75 }
76 print $channel, "ERR\n";
77 }
78
79 =pod
80
81 =head1 COPYRIGHT
82
83 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
84 *
85 * Squid software is distributed under GPLv2+ license and includes
86 * contributions from numerous individuals and organizations.
87 * Please see the COPYING and CONTRIBUTORS files for details.
88
89 Copyright (C) 2013 Alan Mizrahi <alan@mizrahi.com.ve>
90 Based on code from Eliezer Croitoru <eliezer@ngtech.co.il>
91
92 This program is free software; you can redistribute it and/or modify
93 it under the terms of the GNU General Public License as published by
94 the Free Software Foundation; either version 2 of the License, or
95 (at your option) any later version.
96
97 This program is distributed in the hope that it will be useful,
98 but WITHOUT ANY WARRANTY; without even the implied warranty of
99 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
100 GNU General Public License for more details.
101
102 You should have received a copy of the GNU General Public License
103 along with this program; if not, write to the Free Software
104 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
105
106 =cut