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