Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add body part for redirects #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/Plack/Middleware/Rewrite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ sub call {
my $dest = Plack::Request->new( $env )->uri;
Plack::Util::header_set( $res->[1], Location => $dest );
}
if ($res->[0] != 304 && !Plack::Util::content_length($res->[2])) {
my $location = Plack::Util::header_get($res->[1], 'Location');
my $body = <<"EOF";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Moved</title>
</head>
<body>
<p>This item has moved <a href="$location">here</a>.</p>
</body>
</html>
EOF
$res->[2] = [$body];
}
}

return $res if not $modify_cb;
Expand Down
13 changes: 11 additions & 2 deletions t/rewrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ $app = builder {
return [ 302, [ Location => 'http://localhost/correct' ], [] ]
if m{^/psgi-redirect};

return 304 if /not-modified/;

s{^/baz$}{/quux};
};
$app;
Expand Down Expand Up @@ -57,13 +59,20 @@ test_psgi app => $app, client => sub {
$res = $cb->( $req );
is $res->code, 301, 'Redirects change the status';
is $res->header( 'Location' ), 'http://localhost/bar/', '... and produce the right Location';
ok !$res->content, '... and prevent execution of the wrapped app';
like $res->content, qr{this\s+item\s+has\s+moved}i,
'... and prevent execution of the wrapped app';

$req = GET 'http://localhost/not-modified';
$res = $cb->( $req );
is $res->code, 304, 'Redirects change the status';
ok !$res->content, '... no body for 304 response';

$req = GET 'http://localhost/foo?q=baz';
$res = $cb->( $req );
is $res->code, 301, 'Redirects change the status';
is $res->header( 'Location' ), 'http://localhost/bar/?q=baz', '... and produce the right Location';
ok !$res->content, '... and prevent execution of the wrapped app';
like $res->content, qr{this\s+item\s+has\s+moved}i,
'... and prevent execution of the wrapped app';

$req = GET 'http://localhost/die';
$res = $cb->( $req );
Expand Down