source: squid-ssl/trunk/fuentes/helpers/storeid_rewrite/file/storeid_file_rewrite @ 5495

Last change on this file since 5495 was 5495, checked in by Juanma, 23 months ago

Initial release

File size: 3.8 KB
RevLine 
[5495]1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use 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
19This program acts as a store_id helper program, rewriting URLs passed
20by Squid into storage-ids that can be used to achieve better caching
21for websites that use different URLs for the same content.
22
23It takes a text file with two tab separated columns.
24Column 1: Regular expression to match against the URL
25Column 2: Rewrite rule to generate a Store-ID
26Eg:
27^http:\/\/[^\.]+\.dl\.sourceforge\.net\/(.*)    http://dl.sourceforge.net.squid.internal/$1
28
29Rewrite rules are matched in the same order as they appear in the rules file.
30So for best performance, sort it in order of frequency of occurrence.
31
32This program will automatically detect the existence of a concurrecy channel-ID and adjust appropriately.
33It may be used with any value 0 or above for the store_id_children concurrency= parameter.
34
35=head1 OPTIONS
36
37The only command line parameter this helper takes is the regex rules file name.
38
39=head1 AUTHOR
40
41This program and documentation was written by I<Alan Mizrahi <alan@mizrahi.com.ve>>
42
43Based on prior work by I<Eliezer Croitoru <eliezer@ngtech.co.il>>
44
45=head1 COPYRIGHT
46
47 * Copyright (C) 1996-2015 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
72Questions on the usage of this program can be sent to the I<Squid Users mailing list <squid-users@squid-cache.org>>
73
74=head1 REPORTING BUGS
75
76Bug reports need to be made in English.
77See http://wiki.squid-cache.org/SquidFaq/BugReporting for details of what you need to include with your bug report.
78
79Report bugs or bug fixes using http://bugs.squid-cache.org/
80
81Report serious security bugs to I<Squid Bugs <squid-bugs@squid-cache.org>>
82
83Report ideas for new improvements to the I<Squid Developers mailing list <squid-dev@squid-cache.org>>
84
85=head1 SEE ALSO
86
87squid (8), GPL (7),
88
89The Squid wiki http://wiki.squid-cache.org/Features/StoreID
90
91The Squid Configuration Manual http://www.squid-cache.org/Doc/config/
92
93=cut
94
95my @rules; # array of [regex, replacement string]
96
97die "Usage: $0 <rewrite-file>\n" unless $#ARGV == 0;
98
99# read config file
100open RULES, $ARGV[0] or die "Error opening $ARGV[0]: $!";
101while (<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}
110close RULES;
111
112$|=1;
113# read urls from squid and do the replacement
114URL: 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}
Note: See TracBrowser for help on using the repository browser.