X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FNewsWWW.pm;h=61d9ae7cacecbbc4facaeb05d119acdfaf433e95;hb=0d38f65c490466837ae091afa7a7b6f59d04ce7c;hp=01e34d7b136b9d87e1931f4bac43b635c048de95;hpb=3d41aa23f35501ca92aab8aa42980fa73f7fa74f;p=public-inbox.git diff --git a/lib/PublicInbox/NewsWWW.pm b/lib/PublicInbox/NewsWWW.pm index 01e34d7b..61d9ae7c 100644 --- a/lib/PublicInbox/NewsWWW.pm +++ b/lib/PublicInbox/NewsWWW.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2018 all contributors +# Copyright (C) 2016-2020 all contributors # License: AGPL-3.0+ # # Plack app redirector for mapping /$NEWSGROUP requests to @@ -10,27 +10,48 @@ use strict; use warnings; use PublicInbox::Config; use PublicInbox::MID qw(mid_escape); +use PublicInbox::Hval qw(prurl); sub new { - my ($class, $pi_config) = @_; - $pi_config ||= PublicInbox::Config->new; - bless { pi_config => $pi_config }, $class; + my ($class, $pi_cfg) = @_; + bless { pi_cfg => $pi_cfg // PublicInbox::Config->new }, $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_cfg = $self->{pi_cfg}; + if (my $ibx = $pi_cfg->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 +59,41 @@ sub call { $url .= mid_escape($mid) . '/'; } } + return redirect($code, $url); + } - my $h = [ Location => $url, 'Content-Type' => 'text/plain' ]; + my @try = (join('/', @parts)); - return [ $code, $h, [ "Redirecting to $url\n" ] ] + # 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); + } + my $ALL = $pi_cfg->ALL; + if (my $over = $ALL ? $ALL->over : undef) { + my $by_eidx_key = $pi_cfg->{-by_eidx_key}; + for my $mid (@try) { + my ($id, $prev); + while (my $x = $over->next_by_mid($mid, \$id, \$prev)) { + my $xr3 = $over->get_xref3($x->{num}); + for (@$xr3) { + s/:[0-9]+:$x->{blob}\z// or next; + my $ibx = $by_eidx_key->{$_} // next; + my $url = $ibx->base_url or next; + $url .= mid_escape($mid) . '/'; + return redirect(302, $url); + } + } + } + } else { # slow path, scan every inbox + for my $mid (@try) { + my $arg = [ $mid ]; # [1] => result + $pi_cfg->each_inbox(\&try_inbox, $arg); + return $arg->[1] if $arg->[1]; + } } - [ 404, [ 'Content-Type' => 'text/plain' ], [ "404 Not Found\n" ] ]; + [ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ]; } 1;