X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FNewsWWW.pm;h=be1d43efa6312b549d701188184cedae5cc47160;hb=11eec3365d537b20ade6923eaa5fdd18206d4342;hp=b4d747633fc378146c9439c6c5b8af1a1bcb83c9;hpb=9d1e5fadd7d18f4c96ab0509d673040e34225a04;p=public-inbox.git diff --git a/lib/PublicInbox/NewsWWW.pm b/lib/PublicInbox/NewsWWW.pm index b4d74763..be1d43ef 100644 --- a/lib/PublicInbox/NewsWWW.pm +++ b/lib/PublicInbox/NewsWWW.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016 all contributors +# Copyright (C) 2016-2019 all contributors # License: AGPL-3.0+ # # Plack app redirector for mapping /$NEWSGROUP requests to @@ -10,6 +10,7 @@ use strict; use warnings; use PublicInbox::Config; use PublicInbox::MID qw(mid_escape); +use PublicInbox::Hval qw(prurl); sub new { my ($class, $pi_config) = @_; @@ -17,20 +18,41 @@ sub new { bless { pi_config => $pi_config }, $class; } +sub redirect ($$) { + my ($code, $url) = @_; + [ $code, + [ Location => $url, 'Content-Type' => 'text/plain' ], + [ "Redirecting to $url\n" ] ] +} + +sub try_inbox { + my ($ibx, $arg) = @_; + return if scalar(@$arg) > 1; + + # do not pass $env since HTTP_HOST may differ + my $url = $ibx->base_url or return; + + my ($mid) = @$arg; + eval { $ibx->mm->num_for($mid) } or return; + + # 302 since the same message may show up on + # multiple inboxes and inboxes can be added/reordered + $arg->[1] = redirect(302, $url .= mid_escape($mid) . '/'); +} + sub call { my ($self, $env) = @_; - my $path = $env->{PATH_INFO}; - $path =~ s!\A/+!!; - $path =~ s!/+\z!!; # some links may have the article number in them: # /inbox.foo.bar/123456 - my ($ng, $article) = split(m!/+!, $path, 2); - if (my $inbox = $self->{pi_config}->lookup_newsgroup($ng)) { - my $url = PublicInbox::Hval::prurl($env, $inbox->{url}); + my (undef, @parts) = split(m!/!, $env->{PATH_INFO}); + my ($ng, $article) = @parts; + my $pi_config = $self->{pi_config}; + if (my $ibx = $pi_config->lookup_newsgroup($ng)) { + my $url = prurl($env, $ibx->{url}); my $code = 301; - if (defined $article && $article =~ /\A\d+\z/) { - my $mid = eval { $inbox->mm->mid_for($article) }; + if (defined $article && $article =~ /\A[0-9]+\z/) { + my $mid = eval { $ibx->mm->mid_for($article) }; if (defined $mid) { # article IDs are not stable across clones, # do not encourage caching/bookmarking them @@ -38,12 +60,25 @@ sub call { $url .= mid_escape($mid) . '/'; } } + return redirect($code, $url); + } - my $h = [ Location => $url, 'Content-Type' => 'text/plain' ]; + my $res; + my @try = (join('/', @parts)); + + # trailing slash is in the rest of our WWW, so maybe some users + # will assume it: + if ($parts[-1] eq '') { + pop @parts; + push @try, join('/', @parts); + } - return [ $code, $h, [ "Redirecting to $url\n" ] ] + foreach my $mid (@try) { + my $arg = [ $mid ]; + $pi_config->each_inbox(\&try_inbox, $arg); + defined($res = $arg->[1]) and last; } - [ 404, [ 'Content-Type' => 'text/plain' ], [ "404 Not Found\n" ] ]; + $res || [ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ]; } 1;