Browse files

support code option

  • Loading branch information...
1 parent 08c0e0f commit 0c9980d2d270c43737c0260650072a4785f21e54 @yappo committed Aug 18, 2011
Showing with 20 additions and 6 deletions.
  1. +11 −4 lib/Plack/Middleware/ErrorpageRedirectloopDefence.pm
  2. +9 −2 sample.psgi
View
15 lib/Plack/Middleware/ErrorpageRedirectloopDefence.pm
@@ -5,17 +5,24 @@ use parent qw(Plack::Middleware);
our $VERSION = '0.09';
use Plack::Request;
+use Plack::Util::Accessor qw/code/;
+
+my $res_notcontent = [ 204, [ 'Content-Type', 'text/html', 'Content-Length', '0' ], [ '' ] ];
sub call {
my $self = shift;
my $env = shift;
my $req = Plack::Request->new($env);
+ my $res = $self->app->($env);
+ return $res unless $req->uri eq ($req->header('referer') || '');
- if ($req->uri eq $req->header('referer')) {
- return [ 204, [ 'Content-Type', 'text/html', 'Content-Length', '0' ], [ '' ] ];
- }
+ my $code = $self->code;
+ return $res unless $code;
- $self->app->($env);
+ if (((ref $code || '') eq 'Regexp' && $res->[0] =~ $code) || $res->[0].'' eq "$code") {
+ return $res_notcontent;
+ }
+ return $res;
}
View
11 sample.psgi
@@ -19,11 +19,18 @@ my $html = <<'HTML';
HTML
builder {
- enable 'ErrorpageRedirectloopDefence';
+ enable 'ErrorpageRedirectloopDefence',
+ code => qr/[45]\d\d/;
+# code => '502';
+
sub {
my $req = Plack::Request->new(shift);
my $body = sprintf $html, $req->uri, time(), $req->uri;
- [ 502, [ 'Content-Type', 'text/html', 'Content-Length', length($body) ], [ $body ] ];
+ if ($req->path eq '/ok') {
+ return [ 200, [ 'Content-Type', 'text/html', 'Content-Length', length($body) ], [ $body ] ];
+ } else {
+ return [ 502, [ 'Content-Type', 'text/html', 'Content-Length', length($body) ], [ $body ] ];
+ }
};
};

0 comments on commit 0c9980d

Please sign in to comment.