From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.7 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: meta@public-inbox.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id B3FAD2021D; Sat, 9 Jan 2016 10:55:28 +0000 (UTC) Date: Sat, 9 Jan 2016 10:55:28 +0000 From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 4/3] www: fix redirection loops Message-ID: <20160109105528.GA12191@dcvr.yhbt.net> References: <20160109094522.2484-1-e@80x24.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160109094522.2484-1-e@80x24.org> List-Id: Sometimes users forget trailing slashes; but we should not punish them with infinite loops. --- lib/PublicInbox/WWW.pm | 4 +++- t/plack.t | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm index 411db16..d5635d8 100644 --- a/lib/PublicInbox/WWW.pm +++ b/lib/PublicInbox/WWW.pm @@ -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) { diff --git a/t/plack.t b/t/plack.t index f61b3be..b77cdba 100644 --- 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')); -- EW