Skip to content

Commit

Permalink
avoid overwriting Location in fabricated PSGI responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ap committed Jun 7, 2012
1 parent 75b234f commit 9e71965
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Plack-Middleware-Rewrite

1.005 Thu 07 Jun 2012
- Preserve pre-existing Location header in fabricated responses

1.004 Thu 29 Mar 2012
- Preserve query params in external redirects by dropping unnecessary use of URI
(with thanks to Wallace Reis)
Expand Down
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = Plack-Middleware-Rewrite
version = 1.004
version = 1.005
author = Aristotle Pagaltzis <pagaltzis@gmx.de>
license = Perl_5
copyright_holder = Aristotle Pagaltzis
Expand Down
6 changes: 4 additions & 2 deletions lib/Plack/Middleware/Rewrite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ sub call {
$res = $self->app->( $env );
}
elsif ( $res->[0] =~ /\A3[0-9][0-9]\z/ ) {
my $dest = Plack::Request->new( $env )->uri;
Plack::Util::header_set( $res->[1], Location => $dest );
if ( not Plack::Util::header_exists( $res->[1], 'Location' ) ) {
my $dest = Plack::Request->new( $env )->uri;
Plack::Util::header_set( $res->[1], Location => $dest );
}
}

return $res if not $modify_cb;
Expand Down
8 changes: 8 additions & 0 deletions t/rewrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ $app = builder {
return sub { $_->set( 'Content-Type', $xhtml ) }
if $_[0]{'HTTP_ACCEPT'} =~ m{application/xhtml\+xml(?!\s*;\s*q=0)};

return [ 302, [ Location => 'http://localhost/correct' ], [] ]
if m{^/psgi-redirect};

s{^/baz$}{/quux};
};
$app;
Expand Down Expand Up @@ -68,6 +71,11 @@ test_psgi app => $app, client => sub {
is $res->header( 'Content-Type' ), 'text/plain', '... with headers';
is $res->content, 'Goodbye Web', '... body, and all.';

$req = GET 'http://localhost/psgi-redirect';
$res = $cb->( $req );
is $res->code, 302, 'Fabricated responses can be redirects';
is $res->header( 'Location' ), 'http://localhost/correct', '... with proper destination';

$req = GET 'http://localhost/', Accept => $xhtml;
$res = $cb->( $req );
is $res->code, 200, 'Post-modification leaves the status alone';
Expand Down

0 comments on commit 9e71965

Please sign in to comment.