]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www: fix redirection loops
authorEric Wong <e@80x24.org>
Sat, 9 Jan 2016 10:53:10 +0000 (10:53 +0000)
committerEric Wong <e@80x24.org>
Sat, 9 Jan 2016 10:53:14 +0000 (10:53 +0000)
Sometimes users forget trailing slashes; but we should not punish
them with infinite loops.

lib/PublicInbox/WWW.pm
t/plack.t

index 411db16f4eaffaaee7f788b0fb356d3751884ffd..d5635d849fcea047c0402f22cacbdab87ce913ea 100644 (file)
@@ -47,7 +47,9 @@ sub run {
 
        # in case people leave off the trailing slash:
        } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
-               r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
+               my ($listname, $mid, $suffix) = ($1, $2, $3);
+               $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
+               r301($ctx, $listname, $mid, $suffix);
 
        # convenience redirects order matters
        } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
index f61b3bec8afb7423cfaf9f31e253922b1b106199..b77cdba552c516ecaabe3b757d8f732ea75b5865 100644 (file)
--- a/t/plack.t
+++ b/t/plack.t
@@ -81,6 +81,30 @@ EOF
                is($to, $res->header('Location'), 'redirect location matches');
        });
 
+       my $pfx = 'http://example.com/test';
+       foreach my $t (qw(t T)) {
+               test_psgi($app, sub {
+                       my ($cb) = @_;
+                       my $u = $pfx . "/blah%40example.com/$t";
+                       my $res = $cb->(GET($u));
+                       is(301, $res->code, "redirect for missing /");
+                       my $location = $res->header('Location');
+                       like($location, qr!/\Q$t\E/#u\z!,
+                               'redirected with missing /');
+               });
+       }
+       foreach my $t (qw(f)) {
+               test_psgi($app, sub {
+                       my ($cb) = @_;
+                       my $u = $pfx . "/blah%40example.com/$t";
+                       my $res = $cb->(GET($u));
+                       is(301, $res->code, "redirect for missing /");
+                       my $location = $res->header('Location');
+                       like($location, qr!/\Q$t\E/\z!,
+                               'redirected with missing /');
+               });
+       }
+
        test_psgi($app, sub {
                my ($cb) = @_;
                my $atomurl = 'http://example.com/test/new.atom';
@@ -92,7 +116,6 @@ EOF
                        'index generated');
        });
 
-       my $pfx = 'http://example.com/test';
        test_psgi($app, sub {
                my ($cb) = @_;
                my $res = $cb->(GET($pfx . '/atom.xml'));