[dw-free] Automatic .../index redirects do not preserve get arguments.
[commit: http://hg.dwscoalition.org/dw-free/rev/3c1dde8411c1]
http://bugs.dwscoalition.org/show_bug.cgi?id=3972
Preserve URL arguments over redirects.
Patch by
exor674.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3972
Preserve URL arguments over redirects.
Patch by
Files modified:
- cgi-bin/DW/Routing.pm
- cgi-bin/DW/Routing/Test.pm
- t/routing-indexes.t
--------------------------------------------------------------------------------
diff -r 98e530f23e6f -r 3c1dde8411c1 cgi-bin/DW/Routing.pm
--- a/cgi-bin/DW/Routing.pm Sun Oct 02 20:34:03 2011 -0500
+++ b/cgi-bin/DW/Routing.pm Mon Oct 03 13:41:38 2011 +0800
@@ -248,7 +248,13 @@
# controller sub for register_redirect
sub _redirect_helper {
my $r = DW::Request->get;
- return $r->redirect( $_[0]->args );
+ my $data = $_[0]->args;
+
+ if ( $data->{full_uri} ) {
+ return $r->redirect( $data->{dest} );
+ } else {
+ return $r->redirect( LJ::create_url( $data->{dest}, keep_args => $data->{keep_args} ) );
+ }
}
=head1 Registration API
@@ -324,6 +330,7 @@
formats => $hash->{formats},
format => $hash->{format},
no_redirects => 1,
+ keep_args => 1,
);
$class->register_redirect( $2, $1, %opts ) if $2;
$string_choices{'app' . $1} = $hash if $hash->{app};
@@ -344,6 +351,14 @@
=item Opts ( see register_string )
+=over
+
+=item keep_args - Persist GET arguments over redirect ( same as the keep_args argument to create_url ).
+
+=item full_uri - A full URI ( http://...../foo v.s. /foo )
+
+=back
+
=back
=cut
@@ -351,7 +366,11 @@
sub register_redirect {
my ( $class, $string, $dest, %opts ) = @_;
- $opts{args} = $dest;
+ my $args = { dest => $dest };
+ $args->{keep_args} = delete $opts{keep_args} || 0;
+ $args->{full_uri} = delete $opts{full_uri} || 0;
+
+ $opts{args} = $args;
$class->register_string( $string, \&_redirect_helper, %opts );
}
@@ -415,7 +434,7 @@
=head1 COPYRIGHT AND LICENSE
-Copyright (c) 2009-2010 by Dreamwidth Studios, LLC.
+Copyright (c) 2009-2011 by Dreamwidth Studios, LLC.
This program is free software; you may redistribute it and/or modify it under
the same terms as Perl itself. For a copy of the license, please reference
diff -r 98e530f23e6f -r 3c1dde8411c1 cgi-bin/DW/Routing/Test.pm
--- a/cgi-bin/DW/Routing/Test.pm Sun Oct 02 20:34:03 2011 -0500
+++ b/cgi-bin/DW/Routing/Test.pm Mon Oct 03 13:41:38 2011 +0800
@@ -118,11 +118,16 @@
sub handle_redirect {
my ( $uri, $expected ) = @_;
- my $tb = $CLASS->builder;
$CLASS->builder->subtest($uri, sub {
- my $req = HTTP::Request->new(GET=>"$uri");
+ my $tb = $CLASS->builder;
+ $tb->plan( tests => 3 );
- my $opts = DW::Routing->get_call_opts( uri => $uri );
+ my $req = HTTP::Request->new(GET=>"http://www.example.com$uri");
+ $req->header( Host => 'www.example.com' );
+ DW::Request->reset;
+ my $r = DW::Request::Standard->new($req);
+
+ my $opts = DW::Routing->get_call_opts( );
unless ( $opts ) {
ok( 0, "opts exists" );
@@ -135,16 +140,26 @@
unless ( $hash && $hash->{sub} ) {
ok( 0, "improper opts" );
_skip("opts are improper");
+ return;
}
is( $hash->{sub}, \&DW::Routing::_redirect_helper );
- is( $hash->{args}, $expected );
+ # Safe to call!
+
+ my $rv = DW::Routing->call_hash( $opts );
+
+ is( $rv, $r->REDIRECT );
+ if ( substr($expected,0,1) == '/' ) {
+ is( $r->header_out( 'Location' ), "http://www.example.com$expected" );
+ } else {
+ is( $r->header_out( 'Location' ), $expected );
+ }
+
});
}
sub handle_server_error {
my ( $name, $uri, $format, %opts ) = @_;
- my $tb = $CLASS->builder;
$CLASS->builder->subtest($name, sub {
DW::Request->reset;
diff -r 98e530f23e6f -r 3c1dde8411c1 t/routing-indexes.t
--- a/t/routing-indexes.t Sun Oct 02 20:34:03 2011 -0500
+++ b/t/routing-indexes.t Mon Oct 03 13:41:38 2011 +0800
@@ -16,7 +16,7 @@
use strict;
use lib "$ENV{LJHOME}/cgi-bin";
-use DW::Routing::Test tests => 5;
+use DW::Routing::Test tests => 6;
$DW::Routing::T_TESTING_ERRORS = 1;
@@ -29,10 +29,13 @@
handle_request( "/xx3/" , "/xx3/", 1, "it_worked_redir" );
handle_request( "/xx3/index" , "/xx3/index", 1, "it_worked_redir" );
handle_redirect( '/xx3', '/xx3/' );
-# 3
+
+handle_redirect( '/xx3?kittens=cute', '/xx3/?kittens=cute' );
+
+# 4
DW::Routing->register_string( "/index", \&handler, app => 1, args => "it_worked_redir" );
handle_request( "/" , "/", 1, "it_worked_redir" );
handle_request( "/index" , "/index", 1, "it_worked_redir" );
-# 5
+# 6
--------------------------------------------------------------------------------
