blib/lib/Apache/AliasList.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #! /usr/bin/perl
2  
3 =head1 NAME
4  
5 Apache::AliasList - Apache translation handler to process lists of aliases
6  
7 =head1 SYNOPSIS
8  
9 In F<httpd.conf>:
10  
11 PerlTransHandler Apache::AliasList
12 PerlSetVar AliasList /path/to/alias.list
13  
14 In F<alias.list>:
15  
16 # Comment lines ignored
17 /alias /full/uri/spec.html
18  
19 If http://domain/alias is requested, the document at
20 http://domain/full/uri/spec.html will be delivered.
21  
22 =head1 DESCRIPTION
23  
24 When using a content management system, it is common for URIs to become
25 quite long and complex. In many cases it is therefore desirable to provide
26 a shorter, more descriptive URI (e.g. to convey verbally or in print).
27  
28 Apache provides an C<Alias> directive which can be used to make these
29 translations in the F<httpd.conf> configuration file. This approach however
30 has the disadvantage that a server restart is required for any changes to
31 take effect. Apache::AliasList removes this requirement by moving the alias
32 definitions into a separate file which can be updated without restarting
33 the server.
34  
35 When an incoming request matches one of the listed aliases, an internal
36 redirect is performed - this keeps the original URI in the location bar
37 of the user's browser.
38  
39 If the incoming request matches the target of any defined aliases,
40 Apache::AliasList will issue an HTTP status 302 response to redirect the
41 client to the source URI of the alias. This has the effect of changing
42 the URI shown in the client browser's location bar
43  
44 =head2 Configuration
45  
46 Create an F<alias.list> file:
47  
48 # Comments start with '#'
49 /old_uri /location/to/be/redirected/to
50 /test http://can.redirect.to/external/sites
51  
52 Add the following directives to F<httpd.conf>:
53  
54 PerlTransHandler Apache::AliasList
55 PerlSetVar AliasList /full/path/to/alias.list
56  
57 =head2 Example
58  
59 There is an alias between the pages http://perl.jonallen.info/modules
60 and http://perl.jonallen.info/bin/view/Main/PerlModules.
61  
62 Accessing the URI http://perl.jonallen.info/modules will trigger the
63 internal redirect. The address shown in the browser does not change, but
64 the content returned is from the longer URI.
65  
66 Requesting the page http://perl.jonallen.info/bin/view/Main/PerlModules
67 causes the external redirect to be issued, taking the client to
68 http://perl.jonallen.info/modules. This URI will then be processed as
69 described above.
70  
71 =head1 TODO
72  
73 Extend the reverse map feature to act as a content filter, substituting
74 URI links with their aliases in the returned HTML document.
75  
76 =head1 AUTHOR
77  
78 Written by Jon Allen (JJ) <jj@jonallen.info>
79  
80 =head1 COPYRIGHT
81  
82 Copyright (C) 2004 Jon Allen
83  
84 This library is free software; you can redistribute it and/or modify
85 it under the same terms as Perl itself.
86  
87 =head1 SEE ALSO
88  
89 http://perl.jonallen.info
90  
91 =cut
92  
93 package Apache::AliasList;
94  
95 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 0 ▶use Apache::Constants qw(:common REDIRECT);
Subroutine BEGIN: called CC=1 SCAR=2.6
98 use File::stat;
99  
100 our $VERSION = '0.08';
101 our $list_mtime = 0;
102 our @aliaslist = ();
103 our %forward_map = ();
104 our %reverse_map = ();
105  
106 sub handler {
107 my $r = shift;
108 my $handler = $r->current_callback;
109 DISPATCH: {
110 if ($handler eq 'PerlTransHandler') {return &PerlTransHandler($r,@_)}
111 return DECLINED;
112 }
113 }
114  
115 sub PerlTransHandler {
116 my $r = shift;
117 return DECLINED unless ($r->is_initial_req);
118
119 (my $uri = $r->uri) =~ s!([^/]+)/+$!$1!;
120
121 # Reload the alias.list file if it has been modified since the last reload
122 my $aliasfile = $r->dir_config('AliasList') or return DECLINED;
123 my $st = stat($aliasfile);
124 if ($st->mtime > $list_mtime) {
125 $list_mtime = $st->mtime;
126 @aliaslist = &load_alias_list($r);
127 %forward_map = &generate_forward_map(@aliaslist);
128 %reverse_map = &generate_reverse_map(@aliaslist);
129 }
130
131 if ($forward_map{$uri}) {
132 $uri = $forward_map{$uri};
133 # Send a Redirect message if the new URI is a full (http://...)
134 # address, otherwise just change the URI to redirect internally
135 if ($uri =~ m!^[a-zA-Z]+://!) {
136 $r->content_type('text/html');
137 $r->header_out(Location => $uri);
138 return REDIRECT;
139 } else {
140 $r->uri($uri);
141 return DECLINED;
142 }
143 }
144  
145 if ($reverse_map{$uri}) {
146 $uri = $reverse_map{$uri};
147 $r->content_type('text/html');
148 $r->header_out(Location => $uri);
149 return REDIRECT;
150 }
151  
152 return DECLINED;
153 }
154  
155 sub load_alias_list {
156 my $r = shift;
157 my $aliaslist = $r->dir_config('AliasList');
158 open ALIASLIST,"< $aliaslist";
159 my @list = (<ALIASLIST>);
160 close ALIASLIST;
161 return @list;
162 }
163  
164 sub generate_forward_map {
165 my %map = ();
166 foreach (@_) {
167 s/#.*//; # Remove comments
168 if (/^\s*(\S+)\s+(\S+)/) {
169 $map{$1} = $2;
170 }
171 }
172 return %map;
173 }
174  
175 sub generate_reverse_map {
176 my %map = ();
177 foreach (@_) {
178 s/#.*//; # Remove comments
179 if (/^\s*(\S+)\s+(\S+)/) {
180 $map{$2} = $1;
181 }
182 }
183 return %map;
184 }
185  
186 1;
↑ Summary