[Catalyst-commits] r8022 - in Catalyst-Plugin-SmartURI/1.000/trunk:
. lib/Catalyst/Plugin t
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Sat Jun 28 11:03:28 BST 2008
Author: caelum
Date: 2008-06-28 11:03:27 +0100 (Sat, 28 Jun 2008)
New Revision: 8022
Modified:
Catalyst-Plugin-SmartURI/1.000/trunk/Changes
Catalyst-Plugin-SmartURI/1.000/trunk/META.yml
Catalyst-Plugin-SmartURI/1.000/trunk/Makefile.PL
Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm
Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t
Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t
Catalyst-Plugin-SmartURI/1.000/trunk/t/03-uri-class.t
Log:
Added 'host-header' disposition
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/Changes
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/Changes 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/Changes 2008-06-28 10:03:27 UTC (rev 8022)
@@ -1,16 +1,23 @@
Revision history for Catalyst-Plugin-SmartURI
0.01 2008-03-08 03:30:31
- First version.
+ First version.
0.01_01 2008-04-01 08:33:13
- First dev release.
+ First dev release.
0.02 2008-06-03 21:58:17
- Moved Catalyst::SmartURI into a separate dist as URI::SmartURI
+ Moved Catalyst::SmartURI into a separate dist as URI::SmartURI
0.021 2008-06-16 22:20:42
- Minor speedup, avoiding unnecessary string evals.
+ Minor speedup, avoiding unnecessary string evals.
0.022 2008-06-24 16:52:55
- Fix weird bug with initialization that sometimes happens.
+ Fix weird bug with initialization that sometimes happens.
+
+0.023 2008-06-28 01:14:49
+ Weaken saved context.
+
+0.024 2008-06-28 03:01:53
+ Added 'host-header' disposition.
+ Converted conf key to 'Plugin::SmartURI'
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/META.yml
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/META.yml 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/META.yml 2008-06-28 10:03:27 UTC (rev 8022)
@@ -20,5 +20,7 @@
Class::Accessor::Fast: 0
Class::C3::Componentised: 0
Class::Data::Inheritable: 0
+ Task::Weaken: 0
URI::SmartURI: 0
-version: 0.022
+ parent: 0
+version: 0.024
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/Makefile.PL
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/Makefile.PL 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/Makefile.PL 2008-06-28 10:03:27 UTC (rev 8022)
@@ -9,6 +9,8 @@
requires 'Class::Accessor::Fast';
requires 'Class::Data::Inheritable';
requires 'URI::SmartURI';
+requires 'Task::Weaken';
+requires 'parent';
build_requires 'Test::More';
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/lib/Catalyst/Plugin/SmartURI.pm 2008-06-28 10:03:27 UTC (rev 8022)
@@ -2,27 +2,39 @@
use strict;
use warnings;
-use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
+use parent qw/Class::Accessor::Fast Class::Data::Inheritable/;
+use Class::C3;
+use Class::C3::Componentised;
+use Scalar::Util 'weaken';
+
+__PACKAGE__->mk_accessors(qw/uri_disposition uri_class/);
+
+my $context; # keep a copy for the Request class to use
+
=head1 NAME
Catalyst::Plugin::SmartURI - Configurable URIs for Catalyst
=head1 VERSION
-Version 0.01
+Version 0.024
=cut
-our $VERSION = '0.022';
+our $VERSION = '0.024';
=head1 SYNOPSIS
- smarturi:
- disposition: hostless # application-wide
+In your .conf:
+ <Plugin::SmartURI>
+ disposition host-header # application-wide
+ </Plugin::SmartURI>
- $c->uri_disposition('absolute'); # per request
+Per request:
+ $c->uri_disposition('absolute');
+Methods on URIs:
<a href="[% c.uri_for('/foo').relative %]" ...
Configure whether $c->uri_for and $c->req->uri_with return absolute, hostless or
@@ -51,18 +63,22 @@
=head1 CONFIGURATION
-In myapp.yml:
+In myapp.conf:
- smarturi:
- dispostion: absolute
- uri_class: 'URI::SmartURI'
+ <Plugin::SmartURI>
+ dispostion absolute
+ uri_class URI::SmartURI
+ </Plugin::SmartURI>
=over
=item disposition
-One of 'absolute', 'hostless' or 'relative'. Defaults to 'absolute'.
+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>.
@@ -84,7 +100,7 @@
=over
-=item $c->uri_disposition('absolute'|'hostless'|'relative')
+=item $c->uri_disposition('absolute'|'hostless'|'relative'|'host-header')
Set URI disposition to use for the duration of the request.
@@ -102,13 +118,6 @@
=cut
-use Class::C3;
-use Class::C3::Componentised;
-
-__PACKAGE__->mk_accessors(qw/uri_disposition uri_class/);
-
-my $context; # keep a copy for the Request class to use
-
sub uri_for {
my $c = shift;
@@ -128,7 +137,7 @@
sub setup {
my $app = shift;
- my $config = $app->config->{smarturi};
+ my $config =$app->config->{'Plugin::SmartURI'} || $app->config->{smarturi};
$config->{uri_class} ||= 'URI::SmartURI';
$config->{disposition} ||= 'absolute';
@@ -157,20 +166,33 @@
my ($c, $uri) = @_;
my $disposition = $c->uri_disposition || 'absolute';
my $uri_class = $c->uri_class || 'URI::SmartURI';
+# Need the || for $c->welcome_message, otherwise initialization works fine.
eval "require $uri_class",$loaded{$uri_class}++ unless $loaded{$uri_class};
- $uri_class->new($uri, { reference => $c->req->uri })->$disposition
+ my $res;
+ if ($disposition eq 'host-header') {
+ $res = $uri_class->new($uri, { reference => $c->req->uri })->absolute;
+ my $host = $c->req->header('Host');
+ $host =~ s/:(\d+)$//;
+ $res->host($host);
+ $res->port($1) if $1;
+ } 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;
- my $config = $app->config->{smarturi};
+ my $config =$app->config->{'Plugin::SmartURI'} || $app->config->{smarturi};
# Also save a copy of the context for the Request class to use.
my $c = $context = $app->next::method(@_);
+ weaken $context;
$c->uri_class($config->{uri_class});
$c->uri_disposition($config->{disposition});
@@ -238,8 +260,6 @@
I'd like to extend on L<Catalyst::Plugin::RequireSSL>, and make a plugin that
rewrites URIs for actions with an SSL attribute.
-Make a disposition that is based on the Host header.
-
=head1 COPYRIGHT & LICENSE
Copyright (c) 2008 Rafael Kitover
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/t/01-basic.t 2008-06-28 10:03:27 UTC (rev 8022)
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 6;
{
package TestApp;
@@ -32,9 +32,24 @@
$c->res->output($c->uri_for('/dummy'));
}
+ sub host_header : Global {
+ my ($self, $c) = @_;
+ $c->req->header(Host => 'www.dongs.com');
+ $c->uri_disposition('host-header');
+ $c->res->output($c->uri_for('/dummy'));
+ }
+
+
+ sub host_header_with_port : Global {
+ my ($self, $c) = @_;
+ $c->req->header(Host => 'www.hlagh.com:8080');
+ $c->uri_disposition('host-header');
+ $c->res->output($c->uri_for('/dummy'));
+ }
+
sub dummy : Global {}
- __PACKAGE__->config->{smarturi}{disposition} = 'hostless';
+ __PACKAGE__->config->{'Plugin::SmartURI'}{disposition} = 'hostless';
__PACKAGE__->setup();
}
@@ -52,4 +67,10 @@
is(get('/test_uri_object'), '/test_uri_object',
'URI objects are functional');
+is(get('/host_header'), 'http://www.dongs.com/dummy',
+ 'host-header disposition');
+
+is(get('/host_header_with_port'), 'http://www.hlagh.com:8080/dummy',
+ 'host-header disposition with port');
+
# vim: expandtab shiftwidth=4 ts=4 tw=80:
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/t/02-c-a-rest-compat.t 2008-06-28 10:03:27 UTC (rev 8022)
@@ -24,7 +24,7 @@
$c->res->output($c->req->uri_with({foo => 'bar'}));
}
- __PACKAGE__->config->{smarturi}{disposition} = 'hostless';
+ __PACKAGE__->config->{'Plugin::SmartURI'}{disposition} = 'hostless';
__PACKAGE__->setup();
}
Modified: Catalyst-Plugin-SmartURI/1.000/trunk/t/03-uri-class.t
===================================================================
--- Catalyst-Plugin-SmartURI/1.000/trunk/t/03-uri-class.t 2008-06-28 00:45:21 UTC (rev 8021)
+++ Catalyst-Plugin-SmartURI/1.000/trunk/t/03-uri-class.t 2008-06-28 10:03:27 UTC (rev 8022)
@@ -23,7 +23,7 @@
$c->res->output($c->uri_for('/foo')->mtfnpy)
}
- __PACKAGE__->config->{smarturi}{uri_class} = 'MyURI';
+ __PACKAGE__->config->{'Plugin::SmartURI'}{uri_class} = 'MyURI';
__PACKAGE__->setup;
}
More information about the Catalyst-commits
mailing list