]> Sergey Matveev's repositories - public-inbox.git/commitdiff
config: support multi-value inbox.*.*url
authorEric Wong <e@80x24.org>
Thu, 2 Jan 2020 03:09:30 +0000 (03:09 +0000)
committerEric Wong <e@80x24.org>
Thu, 2 Jan 2020 20:56:24 +0000 (20:56 +0000)
Since the beginning of this project, we've implicitly supported
inboxes with multiple URLs by relying on the Host: header sent
by the client ($env->{HTTP_HOST}).

We now offer the option to explicitly configure multiple URLs for
every inbox along with the ability to do a best-effort match for
matching hostnames.

lib/PublicInbox/Config.pm
lib/PublicInbox/Hval.pm
lib/PublicInbox/Inbox.pm
lib/PublicInbox/WwwListing.pm
t/config.t
t/feed.t
t/inbox.t
t/nntp.t

index 8ecf549d47a42b8bc07c859389effb4adef3722d..ffc31f833c592457a353a91f81eae71de43570d0 100644 (file)
@@ -362,8 +362,8 @@ sub _fill {
        my ($self, $pfx) = @_;
        my $ibx = {};
 
-       foreach my $k (qw(inboxdir filter url newsgroup
-                       infourl watch watchheader httpbackendmax
+       foreach my $k (qw(inboxdir filter newsgroup
+                       watch watchheader httpbackendmax
                        replyto feedmax nntpserver indexlevel)) {
                my $v = $self->{"$pfx.$k"};
                $ibx->{$k} = $v if defined $v;
@@ -383,7 +383,8 @@ sub _fill {
        }
        # TODO: more arrays, we should support multi-value for
        # more things to encourage decentralization
-       foreach my $k (qw(address altid nntpmirror coderepo hide listid)) {
+       foreach my $k (qw(address altid nntpmirror coderepo hide listid url
+                       infourl)) {
                if (defined(my $v = $self->{"$pfx.$k"})) {
                        $ibx->{$k} = _array($v);
                }
index 4a79439f8e27f23f320ed05e288b5d8848150fef..cd3a4df34bec61bad715fbf1546dec1c74338d2e 100644 (file)
@@ -86,6 +86,12 @@ sub raw {
 
 sub prurl {
        my ($env, $u) = @_;
+       if (ref($u) eq 'ARRAY') {
+               my $h = $env->{HTTP_HOST} // $env->{SERVER_NAME};
+               my @host_match = grep(/\b\Q$h\E\b/, @$u);
+               $u = $host_match[0] // $u->[0];
+               # fall through to below:
+       }
        index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
 }
 
index f294c0d515f74aa4e8eb584d70d73d38fd10e603..a3cdcbc08f6c7f6626b40a2cebbbb73b6be14e0d 100644 (file)
@@ -252,7 +252,7 @@ sub base_url {
        } else {
                # either called from a non-PSGI environment (e.g. NNTP/POP3)
                $self->{-base_url} ||= do {
-                       my $url = $self->{url} or return undef;
+                       my $url = $self->{url}->[0] or return undef;
                        # expand protocol-relative URLs to HTTPS if we're
                        # not inside a web server
                        $url = "https:$url" if $url =~ m!\A//!;
index 1faade6cd39959c581481755ccec4863092b147c..4f076b7d24041f17cced038d802414cc27a6dc0a 100644 (file)
@@ -32,7 +32,7 @@ sub list_all ($$$) {
 sub list_match_domain_i {
        my ($ibx, $arg) = @_;
        my ($list, $hide_key, $re) = @$arg;
-       if (!$ibx->{-hide}->{$hide_key} && $ibx->{url} =~ $re) {
+       if (!$ibx->{-hide}->{$hide_key} && grep($re, @{$ibx->{url}})) {
                push @$list, $ibx;
        }
 }
@@ -90,7 +90,7 @@ sub ibx_entry {
   ${\$ibx->description}
 
        if (defined(my $info_url = $ibx->{infourl})) {
-               $tmp .= '  ' . $info_url . "\n";
+               $tmp .= '  ' . PublicInbox::Hval::prurl($env, $info_url) . "\n";
        }
        $tmp;
 }
index db3f9b2aa5ec6c6873ce7c7953db58a06e838ab8..1ec864b471d4f39de3471a6b62a04dde03d78660 100644 (file)
@@ -27,7 +27,7 @@ my ($tmpdir, $for_destroy) = tmpdir();
                'inboxdir' => '/home/pi/meta-main.git',
                'address' => [ 'meta@public-inbox.org' ],
                'domain' => 'public-inbox.org',
-               'url' => 'http://example.com/meta',
+               'url' => [ 'http://example.com/meta' ],
                -primary_address => 'meta@public-inbox.org',
                'name' => 'meta',
                feedmax => 25,
@@ -48,7 +48,7 @@ my ($tmpdir, $for_destroy) = tmpdir();
                'domain' => 'public-inbox.org',
                'name' => 'test',
                feedmax => 25,
-               'url' => 'http://example.com/test',
+               'url' => [ 'http://example.com/test' ],
                -httpbackend_limiter => undef,
                nntpserver => undef,
        }, "lookup matches expected output for test");
index 97468c730f0dc242cf06484cecb72adc70c1cdef..b2a944875c4acfbd6f676301b98fac1f0aa9220f 100644 (file)
--- a/t/feed.t
+++ b/t/feed.t
@@ -29,7 +29,7 @@ my $ibx = PublicInbox::Inbox->new({
        address => 'test@example',
        name => 'testbox',
        inboxdir => $git_dir,
-       url => 'http://example.com/test',
+       url => [ 'http://example.com/test' ],
        feedmax => 3,
 });
 my $git = $ibx->git;
index c967429023d161c0e46b5253edd4159399a5c0ae..a466dd1364bd29879b2985cd460afdc2dfdd82cd 100644 (file)
--- a/t/inbox.t
+++ b/t/inbox.t
@@ -4,9 +4,9 @@ use strict;
 use warnings;
 use Test::More;
 use_ok 'PublicInbox::Inbox';
-my $x = PublicInbox::Inbox->new({url => '//example.com/test/'});
+my $x = PublicInbox::Inbox->new({url => [ '//example.com/test/' ]});
 is($x->base_url, 'https://example.com/test/', 'expanded protocol-relative');
-$x = PublicInbox::Inbox->new({url => 'http://example.com/test'});
+$x = PublicInbox::Inbox->new({url => [ 'http://example.com/test' ]});
 is($x->base_url, 'http://example.com/test/', 'added trailing slash');
 
 $x = PublicInbox::Inbox->new({});
index 961b3d6af1e2da8b29aa3aec6cb1bb86e228d11a..938eef385bb5f2b12e3abccd5954d9db183c2f0c 100644 (file)
--- a/t/nntp.t
+++ b/t/nntp.t
@@ -104,7 +104,7 @@ use_ok 'PublicInbox::Inbox';
                                        -primary_address => 'a@example.com',
                                        newsgroup => 'test',
                                        domain => 'example.com',
-                                       url => '//example.com/a'});
+                                       url => [ '//example.com/a' ]});
        is($ng->base_url, $u, 'URL expanded');
        my $mid = 'a@b';
        my $mime = Email::MIME->new("Message-ID: <$mid>\r\n\r\n");