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