]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store/id_rewriters/file/storeid_file_rewrite.pl.in
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / store / id_rewriters / file / storeid_file_rewrite.pl.in
1 #!@PERL@
2
3 use strict;
4 use warnings;
5 use Pod::Usage;
6
7 =pod
8
9 =head1 NAME
10
11 storeid_file_rewrite - File based Store-ID helper for Squid
12
13 =head1 SYNOPSIS
14
15 storeid_file_rewrite filepath
16
17 =head1 DESCRIPTION
18
19 This program acts as a store_id helper program, rewriting URLs passed
20 by Squid into storage-ids that can be used to achieve better caching
21 for websites that use different URLs for the same content.
22
23 It takes a text file with two tab separated columns.
24 Column 1: Regular expression to match against the URL
25 Column 2: Rewrite rule to generate a Store-ID
26 Eg:
27 ^http:\/\/[^\.]+\.dl\.sourceforge\.net\/(.*) http://dl.sourceforge.net.squid.internal/$1
28
29 Rewrite rules are matched in the same order as they appear in the rules file.
30 So for best performance, sort it in order of frequency of occurrence.
31
32 This program will automatically detect the existence of a concurrency channel-ID and adjust appropriately.
33 It may be used with any value 0 or above for the store_id_children concurrency= parameter.
34
35 =head1 OPTIONS
36
37 The only command line parameter this helper takes is the regex rules file name.
38
39 =head1 AUTHOR
40
41 This program and documentation was written by I<Alan Mizrahi <alan@mizrahi.com.ve>>
42
43 Based on prior work by I<Eliezer Croitoru <eliezer@ngtech.co.il>>
44
45 =head1 COPYRIGHT
46
47 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
48 *
49 * Squid software is distributed under GPLv2+ license and includes
50 * contributions from numerous individuals and organizations.
51 * Please see the COPYING and CONTRIBUTORS files for details.
52
53 Copyright (C) 2013 Alan Mizrahi <alan@mizrahi.com.ve>
54 Based on code from Eliezer Croitoru <eliezer@ngtech.co.il>
55
56 This program is free software; you can redistribute it and/or modify
57 it under the terms of the GNU General Public License as published by
58 the Free Software Foundation; either version 2 of the License, or
59 (at your option) any later version.
60
61 This program is distributed in the hope that it will be useful,
62 but WITHOUT ANY WARRANTY; without even the implied warranty of
63 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 GNU General Public License for more details.
65
66 You should have received a copy of the GNU General Public License
67 along with this program; if not, write to the Free Software
68 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
69
70 =head1 QUESTIONS
71
72 Questions on the usage of this program can be sent to the I<Squid Users mailing list <squid-users@lists.squid-cache.org>>
73
74 =head1 REPORTING BUGS
75
76 Bug reports need to be made in English.
77 See http://wiki.squid-cache.org/SquidFaq/BugReporting for details of what you need to include with your bug report.
78
79 Report bugs or bug fixes using http://bugs.squid-cache.org/
80
81 Report serious security bugs to I<Squid Bugs <squid-bugs@lists.squid-cache.org>>
82
83 Report ideas for new improvements to the I<Squid Developers mailing list <squid-dev@lists.squid-cache.org>>
84
85 =head1 SEE ALSO
86
87 squid (8), GPL (7),
88
89 The Squid wiki http://wiki.squid-cache.org/Features/StoreID
90
91 The Squid Configuration Manual http://www.squid-cache.org/Doc/config/
92
93 =cut
94
95 my @rules; # array of [regex, replacement string]
96
97 die "Usage: $0 <rewrite-file>\n" unless $#ARGV == 0;
98
99 # read config file
100 open RULES, $ARGV[0] or die "Error opening $ARGV[0]: $!";
101 while (<RULES>) {
102 chomp;
103 next if /^\s*#?$/;
104 if (/^\s*([^\t]+?)\s*\t+\s*([^\t]+?)\s*$/) {
105 push(@rules, [qr/$1/, $2]);
106 } else {
107 print STDERR "$0: Parse error in $ARGV[0] (line $.)\n";
108 }
109 }
110 close RULES;
111
112 $|=1;
113 # read urls from squid and do the replacement
114 URL: while (<STDIN>) {
115 chomp;
116 last if $_ eq 'quit';
117
118 my $channel = "";
119 if (s/^(\d+\s+)//o) {
120 $channel = $1;
121 }
122
123 foreach my $rule (@rules) {
124 if (my @match = /$rule->[0]/) {
125 $_ = $rule->[1];
126
127 for (my $i=1; $i<=scalar(@match); $i++) {
128 s/\$$i/$match[$i-1]/g;
129 }
130 print $channel, "OK store-id=$_\n";
131 next URL;
132 }
133 }
134 print $channel, "ERR\n";
135 }