]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NewsWWW.pm
hval: export prurl and add prototype
[public-inbox.git] / lib / PublicInbox / NewsWWW.pm
index d7fcb0daec1d2d35161c82e15333dc09e4daed23..be1d43efa6312b549d701188184cedae5cc47160 100644 (file)
@@ -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) = @_;
@@ -24,16 +25,19 @@ sub redirect ($$) {
          [ "Redirecting to $url\n" ] ]
 }
 
-sub try_inbox ($$) {
-       my ($ibx, $mid) = @_;
+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
-       redirect(302, $url .= mid_escape($mid) . '/');
+       $arg->[1] = redirect(302, $url .= mid_escape($mid) . '/');
 }
 
 sub call {
@@ -44,11 +48,11 @@ sub call {
        my (undef, @parts) = split(m!/!, $env->{PATH_INFO});
        my ($ng, $article) = @parts;
        my $pi_config = $self->{pi_config};
-       if (my $inbox = $pi_config->lookup_newsgroup($ng)) {
-               my $url = PublicInbox::Hval::prurl($env, $inbox->{url});
+       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
@@ -70,10 +74,9 @@ sub call {
        }
 
        foreach my $mid (@try) {
-               $pi_config->each_inbox(sub {
-                       $res ||= try_inbox($_[0], $mid);
-               });
-               last if defined $res;
+               my $arg = [ $mid ];
+               $pi_config->each_inbox(\&try_inbox, $arg);
+               defined($res = $arg->[1]) and last;
        }
        $res || [ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ];
 }