blib/lib/Catalyst/Plugin/SmartURI.pm

filter: Statementstmt 99.0% 100 / 101 Branchbran 75.0% 24 / 32 Conditioncond 36.3% 8 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 17 / 17 Podpod 57.1% 4 / 7 totaltotal 79.2% 153 / 193 CC 25 SCAR 19.6
CC
25
Cov
85%
prepare_uri
14.2
setup
9.3
referer
4.1
CRAP 7.1
↑ Summary
1 package Catalyst::Plugin::SmartURI;
2 5 our $AUTHORITY = 'cpan:RKITOVER';
3 5 $Catalyst::Plugin::SmartURI::VERSION = '0.041';
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Class::C3::Componentised;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Catalyst::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 has uri_disposition => (is => 'rw', isa => 'Str');
16 5 has uri_class => (is => 'rw', isa => 'Str');
17  
18 5 my $context; # keep a weakend copy for the Request class to use
19  
20 5 my ($conf_disposition, $conf_uri_class); # configured values
21  
22 =head1 NAME
23  
24 Catalyst::Plugin::SmartURI - Configurable URIs for Catalyst
25  
26 =head1 SYNOPSIS
27  
28 In your lib/MyApp.pm, load the plugin and your other plugins, for example:
29  
30 use Catalyst qw/
31 -Debug
32 ConfigLoader
33 Static::Simple
34 Session
35 Session::Store::Memcached
36 Session::State::Cookie
37 Authentication
38 Authorization::Roles
39 +CatalystX::SimpleLogin
40 SmartURI
41 /;
42  
43 In your .conf:
44  
45 <Plugin::SmartURI>
46 disposition host-header # application-wide
47 uri_class URI::SmartURI # by default
48 </Plugin::SmartURI>
49  
50 Per request:
51  
52 $c->uri_disposition('absolute');
53  
54 Methods on URIs:
55  
56 <a href="[% c.uri_for('/foo').relative %]" ...
57  
58 =head1 DESCRIPTION
59  
60 Configure whether C<< $c->uri_for >> and C<< $c->req->uri_with >> return absolute, hostless or
61 relative URIs, or URIs based on the 'Host' header. Also allows configuring which
62 URI class to use. Works on application-wide or per-request basis.
63  
64 This is useful in situations where you're for example, redirecting to a lighttpd
65 from a firewall rule, instead of a real proxy, and you want your links and
66 redirects to still work correctly.
67  
68 To use your own URI class, just subclass L<URI::SmartURI> and set
69 C<uri_class>, or write a class that follows the same interface.
70  
71 This plugin installs a custom C<< $c->request_class >>, however it does so in a way
72 that won't break if you've already set C<< $c->request_class >> yourself, ie. by
73 using L<Catalyst::Action::REST> (thanks mst!).
74  
75 There is a minor performance penalty in perls older than 5.10, due to
76 L<Class::C3>, but only at initialization time.
77  
78 =head1 METHODS
79  
80 =head2 $c->uri_for
81  
82 =head2 $c->req->uri_with
83  
84 Returns a C<< $c->uri_class >> object (L<URI::SmartURI> by default) in the configured
85 C<< $c->uri_disposition >>.
86  
87 =head2 $c->req->uri
88  
89 Returns a C<< $c->uri_class >> object. If the context hasn't been prepared yet, uses
90 the configured value for C<uri_class>.
91  
92 C<< $c->req->uri->relative >> will be relative to C<< $c->req->base >>.
93  
94 =head2 $c->req->referer
95  
96 Returns a C<< $c->uri_class >> object for the referer (or configured C<uri_class> if
97 there's no context) with reference set to C<< $c->req->uri >> if it comes from
98 C<< $c->req->base >>.
99  
100 In other words, if referer is your app, you can do
101 C<< $c->req->referer->relative >> and it will do the right thing.
102  
103 =head1 CONFIGURATION
104  
105 In myapp.conf:
106  
107 <Plugin::SmartURI>
108 disposition absolute
109 uri_class URI::SmartURI
110 </Plugin::SmartURI>
111  
112 =over
113  
114 =item disposition
115  
116 One of 'absolute', 'hostless', 'relative' or 'host-header'. Defaults to
117 'absolute'.
118  
119 The special disposition 'host-header' uses the value of your 'Host:' header.
120  
121 =item uri_class
122  
123 The class to use for URIs, defaults to L<URI::SmartURI>.
124  
125 =back
126  
127 =head1 PER REQUEST
128  
129 package MyAPP::Controller::RSSFeed;
130  
131 ...
132  
133 sub begin : Private {
134 my ($self, $c) = @_;
135  
136 $c->uri_class('Your::URI::Class::For::Request');
137 $c->uri_disposition('absolute');
138 }
139  
140 =over
141  
142 =item $c->uri_disposition('absolute'|'hostless'|'relative'|'host-header')
143  
144 Set URI disposition to use for the duration of the request.
145  
146 =item $c->uri_class($class)
147  
148 Set the URI class to use for C<< $c->uri_for >> and C<< $c->req->uri_with >> for the
149 duration of the request.
150  
151 =back
152  
153 =head1 EXTENDING
154  
155 C<< $c->prepare_uri >> actually creates the URI, which you can override.
156  
157 =cut
158  
159 6 ▶sub uri_for {
Subroutine uri_for: called CC=1 SCAR=0.0
Pod documented
160 6 my $c = shift;
161  
162 6 $c->prepare_uri($c->next::method(@_))
163 }
164  
165 {
166 5 package Catalyst::Request::SmartURI;
167 5 our $AUTHORITY = 'cpan:RKITOVER';
168 5 $Catalyst::Request::SmartURI::VERSION = '0.041';
169 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 5 extends 'Catalyst::Request';
171 5 ▶ use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 2 ▶ sub uri_with {
Subroutine uri_with: called CC=1 SCAR=0.0
Pod documented
174 2 my $req = shift;
175  
176 2 $context->prepare_uri($req->next::method(@_))
177 }
178  
179 46 ▶ sub uri {
Subroutine uri: called CC=4 SCAR=13.9
Pod documented
180 46 my $req = shift;
181  
182 46 ▶ my $uri_class = $context ? $context->uri_class : $conf_uri_class;
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 19 27 46
183  
184 46 my $uri = $req->next::method(@_);
185  
186 46 ▶ return $uri if not defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 1 45 46
187  
188 $req->next::method(
189 $uri_class->new(
190 $req->next::method(@_),
191 45 ▶ ($req->{base} ? { reference => $req->base } : ())
Branch: 1 branch
BranchTrueFalseTotal
$req->{'base'} ? : 32 13 45
192 )
193 )
194 }
195  
196 4 ▶ sub referer {
Subroutine referer: called CC=4 SCAR=14.2
Pod documented
197 4 my $req = shift;
198  
199 4 ▶ my $uri_class = $context ? $context->uri_class : $conf_uri_class;
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 4 0 4
200  
201 4 my $referer = $req->next::method(@_);
202  
203 4 ▶ return $referer if not defined $referer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $referer 1 3 4
204  
205 3 my $base = $req->base;
206 3 my $uri = $req->uri;
207  
208 3 ▶ if ($referer =~ /^$base/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($referer =~ /^$base/) { } 0 3 3
209 0 return $uri_class->new($referer, { reference => $uri })
210 } else {
211 3 return $uri_class->new($referer);
212 }
213 }
214  
215 5 __PACKAGE__->meta->make_immutable;
216 }
217  
218 4 ▶sub setup {
Subroutine setup: called CC=8 SCAR=22.3
Pod undocumented
219 4 my $app = shift;
220 4 ▶ my $config =$app->config->{'Plugin::SmartURI'} || $app->config->{smarturi};
Condition33%1/3
$app->config->{'Plugin::SmartURI'} || $app->config->{'smarturi'}
l!l&&r!l&&!r
400
MC/DC0%0/2
$app->config->{'Plugin::SmartURI'} || $app->config->{'smarturi'}
$app->config->{'Plugin::SmartURI'} $app->config->{'smarturi'}
Truth table33%1/3
A || B
A= $app->config->{'Plugin::SmartURI'}B= $app->config->{'smarturi'}
ABresult
1X1
011
000
221  
222 4 ($conf_uri_class, $conf_disposition) = @$config{qw/uri_class disposition/};
223 4 ▶ $conf_uri_class ||= 'URI::SmartURI';
Condition50%1/2
$conf_uri_class ||= "URI::SmartURI"
l!l
40
MC/DC0%0/1
$conf_uri_class ||= "URI::SmartURI"
$conf_uri_class
Truth table50%1/2
A
A= $conf_uri_class
Aresult
11
00
224 4 ▶ $conf_disposition ||= 'absolute';
Condition50%1/2
$conf_disposition ||= "absolute"
l!l
40
MC/DC0%0/1
$conf_disposition ||= "absolute"
$conf_disposition
Truth table50%1/2
A
A= $conf_disposition
Aresult
11
00
225  
226 4 eval { Class::Load::load_class($conf_uri_class) };
227 4 ▶ Catalyst::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
228 message => "Could not load configured uri_class $conf_uri_class: $@"
229 ) if $@;
230  
231 4 my $request_class = $app->request_class;
232  
233 4 ▶ unless ($request_class->isa('Catalyst::Request::SmartURI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request_class->isa("Catalyst::Request::SmartURI")) 4 0 4
234 4 my $new_request_class = $app.'::Request::SmartURI';
235  
236 my $inject_rest = (not $request_class->isa('Catalyst::Request::REST'))
237 4 ▶ && eval { Class::Load::load_class('Catalyst::Request::REST') };
Condition33%1/3
!$request_class->isa('Catalyst::Request::REST') && eval { do { Class::Load::load_class('Catalyst::Request::REST') } }
!ll&&!rl&&r
004
MC/DC0%0/2
!$request_class->isa('Catalyst::Request::REST') && eval { do { Class::Load::load_class('Catalyst::Request::REST') } }
!$request_class->isa('Catalyst::Request::REST') eval { do { Class::Load::load_class('Catalyst::Request::REST') } }
Truth table33%1/3
not(A) && B
A= !$request_class->isa('Catalyst::Request::REST')B= eval { do { Class::Load::load_class('Catalyst::Request::REST') } }
ABresult
0X0
100
111
238  
239 4 ▶ Class::C3::Componentised->inject_base(
Branch: 1 branch
BranchTrueFalseTotal
$inject_rest ? : 4 0 4
240 $new_request_class,
241 'Catalyst::Request::SmartURI',
242 ($inject_rest ?
243 'Catalyst::Request::REST' : ()),
244 $request_class,
245 );
246 4 Class::C3::reinitialize();
247  
248 4 $app->request_class($new_request_class);
249 }
250  
251 4 $app->next::method(@_)
252 }
253  
254 8 ▶sub prepare_uri {
Subroutine prepare_uri: called CC=12 SCAR=26.6
Pod undocumented
255 8 my ($c, $uri) = @_;
256 8 ▶ my $disposition = $c->uri_disposition || $conf_disposition;
Condition33%1/3
$c->uri_disposition || $conf_disposition
l!l&&r!l&&!r
800
MC/DC0%0/2
$c->uri_disposition || $conf_disposition
$c->uri_disposition $conf_disposition
Truth table33%1/3
A || B
A= $c->uri_dispositionB= $conf_disposition
ABresult
1X1
011
000
257 8 ▶ my $uri_class = $c->uri_class || $conf_uri_class;
Condition33%1/3
$c->uri_class || $conf_uri_class
l!l&&r!l&&!r
800
MC/DC0%0/2
$c->uri_class || $conf_uri_class
$c->uri_class $conf_uri_class
Truth table33%1/3
A || B
A= $c->uri_classB= $conf_uri_class
ABresult
1X1
011
000
258 # Need the || for $c->welcome_message, otherwise initialization works fine.
259  
260 8 eval { Class::Load::load_class($uri_class) };
261 8 ▶ Catalyst::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
262 message => "Could not load configured uri_class $uri_class: $@"
263 ) if $@;
264  
265 8 my $res;
266 8 ▶ if ($disposition eq 'host-header') {
Branch: 1 branch
BranchTrueFalseTotal
if ($disposition eq 'host-header') { } 2 6 8
267 2 $res = $uri_class->new($uri, { reference => $c->req->uri })->absolute;
268 2 my $host = $c->req->header('Host');
269 2 ▶ my $port = $host =~ s/:(\d+)$// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$host =~ s/:(\d+)$// ? : 1 1 2
270  
271 2 ▶ if ($port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 1 1 2
272 1 ▶ $port = '' if $c->req->uri->scheme eq 'http' && $port == 80;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->uri->scheme eq "http" and $port == 80 0 1 1
Condition33%1/3
$c->req->uri->scheme eq "http" and $port == 80
!ll&&!rl&&r
010
MC/DC0%0/2
$c->req->uri->scheme eq "http" and $port == 80
$c->req->uri->scheme eq "http" $port == 80
Truth table33%1/3
A and B
A= $c->req->uri->scheme eq "http"B= $port == 80
ABresult
0X0
100
111
273 1 ▶ $port = '' if $c->req->uri->scheme eq 'https' && $port == 443;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->uri->scheme eq "https" and $port == 443 0 1 1
Condition33%1/3
$c->req->uri->scheme eq "https" and $port == 443
!ll&&!rl&&r
100
MC/DC0%0/2
$c->req->uri->scheme eq "https" and $port == 443
$c->req->uri->scheme eq "https" $port == 443
Truth table33%1/3
A and B
A= $c->req->uri->scheme eq "https"B= $port == 443
ABresult
0X0
100
111
274 }
275  
276 2 $res->host($host);
277 2 ▶ $res->port($port) if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 1 1 2
278 } else {
279 6 $res = $uri_class->new($uri, { reference => $c->req->uri })->$disposition
280 }
281  
282 8 $res
283 }
284  
285 # Reset accessors to configured values at beginning of request.
286 13 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod undocumented
287 13 my $app = shift;
288  
289 # Also save a copy of the context for the Request class to use.
290 13 my $c = $context = $app->next::method(@_);
291 13 weaken $context;
292  
293 13 $c->uri_class($conf_uri_class);
294 13 $c->uri_disposition($conf_disposition);
295  
296 13 $c
297 }
298  
299 5 __PACKAGE__->meta->make_immutable;
300  
301 =head1 SEE ALSO
302  
303 L<URI::SmartURI>, L<Catalyst>, L<URI>
304  
305 =head1 AUTHOR
306  
307 Rafael Kitover, C<< <rkitover at cpan.org> >>
308  
309 =head1 BUGS
310  
311 Please report any bugs or feature requests to
312 C<bug-catalyst-plugin-smarturi at rt.cpan.org>, or through the web
313 interface at
314 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-SmartURI>. I
315 will be notified, and then you'll automatically be notified of progress on your
316 bug as I make changes.
317  
318 =head1 SUPPORT
319  
320 You can find documentation for this module with the perldoc command.
321  
322 perldoc Catalyst::Plugin::SmartURI
323  
324 You can also look for information at:
325  
326 =over 4
327  
328 =item * RT: CPAN's request tracker
329  
330 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-SmartURI>
331  
332 =item * AnnoCPAN: Annotated CPAN documentation
333  
334 L<http://annocpan.org/dist/Catalyst-Plugin-SmartURI>
335  
336 =item * CPAN Ratings
337  
338 L<http://cpanratings.perl.org/d/Catalyst-Plugin-SmartURI>
339  
340 =item * Search CPAN
341  
342 L<http://search.cpan.org/dist/Catalyst-Plugin-SmartURI>
343  
344 =back
345  
346 =head1 ACKNOWLEDGEMENTS
347  
348 from #catalyst:
349  
350 vipul came up with the idea
351  
352 mst came up with the design and implementation details for the current version
353  
354 kd reviewed my code and offered suggestions
355  
356 =head1 TODO
357  
358 I'd like to extend on L<Catalyst::Plugin::RequireSSL>, and make a plugin that
359 rewrites URIs for actions with an SSL attribute.
360  
361 =head1 COPYRIGHT & LICENSE
362  
363 Copyright (c) 2008 Rafael Kitover
364  
365 This program is free software; you can redistribute it and/or modify it under
366 the same terms as Perl itself.
367  
368 =cut
369  
370 5 __PACKAGE__; # End of Catalyst::Plugin::SmartURI
371  
372 # vim: expandtab shiftwidth=4 tw=80:
↑ Summary