]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www: fix for running under mount paths
authorEric Wong <e@80x24.org>
Sun, 15 May 2016 06:31:50 +0000 (06:31 +0000)
committerEric Wong <e@80x24.org>
Mon, 16 May 2016 07:52:56 +0000 (07:52 +0000)
We try to avoid issues like these by using relative URLs
in hrefs, but we can't avoid the problem with Location:
for redirects and Atom feeds which are likely to be
rehosted elsewhere.

We also reorder some of the code to work around a weird
issue on the psgi-plack mailing list:
<20160516073750.GA11931@dcvr.yhbt.net>
(Somewhere on https://groups.google.com/group/psgi-plack
 but it's probably not bookmarkable)

lib/PublicInbox/Feed.pm
lib/PublicInbox/Inbox.pm
lib/PublicInbox/WWW.pm
t/inbox.t [new file with mode: 0644]
t/psgi_mount.t [new file with mode: 0644]

index 0d3bc81dfc410e42931f8d0b7c75a53387fda68c..52fe0db4ac9ece1d5431a363b1646c1150e12449 100644 (file)
@@ -61,9 +61,9 @@ sub atom_header {
 
 sub emit_atom {
        my ($cb, $ctx) = @_;
+       my $feed_opts = get_feedopts($ctx);
        my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
        my $max = $ctx->{max} || MAX_PER_PAGE;
-       my $feed_opts = get_feedopts($ctx);
        my $x = atom_header($feed_opts);
        my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
        each_recent_blob($ctx, sub {
@@ -95,8 +95,8 @@ sub emit_atom_thread {
        my ($cb, $ctx) = @_;
        my $res = $ctx->{srch}->get_thread($ctx->{mid});
        return _no_thread($cb) unless $res->{total};
-       my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
        my $feed_opts = get_feedopts($ctx);
+       my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
 
        my $html_url = $feed_opts->{atomurl} = $ctx->{self_url};
        $html_url =~ s!/t\.atom\z!/!;
@@ -112,10 +112,10 @@ sub emit_atom_thread {
 
 sub emit_html_index {
        my ($res, $ctx) = @_;
+       my $feed_opts = get_feedopts($ctx);
        my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
 
        my $max = $ctx->{max} || MAX_PER_PAGE;
-       my $feed_opts = get_feedopts($ctx);
 
        my $title = ascii_html($feed_opts->{description} || '');
        my ($footer, $param, $last);
@@ -261,15 +261,15 @@ sub get_feedopts {
        my ($ctx) = @_;
        my $pi_config = $ctx->{pi_config};
        my $inbox = $ctx->{inbox};
+       my $obj = $ctx->{-inbox};
        my $cgi = $ctx->{cgi};
-       my %rv;
-       if (open my $fh, '<', "$ctx->{git_dir}/description") {
-               chomp($rv{description} = <$fh>);
-       } else {
-               $rv{description} = '($GIT_DIR/description missing)';
-       }
+       my %rv = ( description => $obj ? $obj->description : 'FIXME' );
 
-       if ($pi_config && defined $inbox && $inbox ne '') {
+       if ($obj) {
+               $rv{address} = $obj->{address};
+               $rv{id_addr} = $obj->{-primary_address};
+       } elsif ($pi_config && defined $inbox && $inbox ne '') {
+               # TODO: remove
                my $addr = $pi_config->get($inbox, 'address') || "";
                $rv{address} = $addr;
                $addr = $addr->[0] if ref($addr);
@@ -278,19 +278,19 @@ sub get_feedopts {
        $rv{id_addr} ||= 'public-inbox@example.com';
 
        my $url_base;
-       if ($cgi) {
-               $url_base = $cgi->base->as_string . $inbox;
+       if ($obj) {
+               $url_base = $obj->base_url($cgi); # CGI may be undef
                if (my $mid = $ctx->{mid}) { # per-thread feed:
-                       $rv{atomurl} = "$url_base/$mid/t.atom";
+                       $rv{atomurl} = "$url_base$mid/t.atom";
                } else {
-                       $rv{atomurl} = "$url_base/new.atom";
+                       $rv{atomurl} = $url_base."new.atom";
                }
        } else {
-               $url_base = "http://example.com";
-               $rv{atomurl} = "$url_base/new.atom";
+               $url_base = 'http://example.com/';
+               $rv{atomurl} = $url_base.'new.atom';
        }
-       $rv{url} ||= "$url_base/";
-       $rv{midurl} = "$url_base/";
+       $rv{url} ||= $url_base;
+       $rv{midurl} = $url_base;
 
        \%rv;
 }
index 5d9fdb36b1fc66af10f4ecfc6e564f5d4d8d1f4f..4bcab96f61469df0ff22a806f64741e12d1d281d 100644 (file)
@@ -64,6 +64,7 @@ sub cloneurl {
        $self->{cloneurl} = \@url;
 }
 
+# TODO: can we remove this?
 sub footer_html {
        my ($self) = @_;
        my $footer = $self->{footer};
@@ -73,4 +74,23 @@ sub footer_html {
        $self->{footer} = $footer;
 }
 
+sub base_url {
+       my ($self, $prq) = @_; # Plack::Request
+       if (defined $prq) {
+               my $url = $prq->base->as_string;
+               $url .= '/' if $url !~ m!/\z!; # for mount in Plack::Builder
+               $url .= $self->{name} . '/';
+       } else {
+               # either called from a non-PSGI environment (e.g. NNTP/POP3)
+               $self->{-base_url} ||= do {
+                       my $url = $self->{url};
+                       # expand protocol-relative URLs to HTTPS if we're
+                       # not inside a web server
+                       $url = "https:$url" if $url =~ m!\A//!;
+                       $url .= '/' if $url !~ m!/\z!;
+                       $url;
+               };
+       }
+}
+
 1;
index 94ab032f145cb06fed15deadf08c8ecf3c450b75..95288a7b4d99379224e8ee262dfac154183488c8 100644 (file)
@@ -39,7 +39,7 @@ sub run {
 sub call {
        my ($self, $env) = @_;
        my $cgi = Plack::Request->new($env);
-       my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
+       my $ctx = {cgi => $cgi, pi_config => $self->{pi_config}, www => $self};
        my $path_info = $cgi->path_info;
 
        my $method = $cgi->method;
@@ -124,7 +124,7 @@ sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
 
 # returns undef if valid, array ref response if invalid
 sub invalid_inbox {
-       my ($self, $ctx, $inbox, $mid) = @_;
+       my ($self, $ctx, $inbox) = @_;
        my $obj = $ctx->{pi_config}->lookup_name($inbox);
        if (defined $obj) {
                $ctx->{git_dir} = $obj->{mainrepo};
@@ -144,7 +144,7 @@ sub invalid_inbox {
 # returns undef if valid, array ref response if invalid
 sub invalid_inbox_mid {
        my ($self, $ctx, $inbox, $mid) = @_;
-       my $ret = invalid_inbox($self, $ctx, $inbox, $mid);
+       my $ret = invalid_inbox($self, $ctx, $inbox);
        return $ret if $ret;
 
        $ctx->{mid} = $mid = uri_unescape($mid);
@@ -382,9 +382,14 @@ sub legacy_redirects {
 sub r301 {
        my ($ctx, $inbox, $mid, $suffix) = @_;
        my $cgi = $ctx->{cgi};
-       my $url;
+       my $obj = $ctx->{-inbox};
+       unless ($obj) {
+               my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
+               return $r404 if $r404;
+               $obj = $ctx->{-inbox};
+       }
+       my $url = $obj->base_url($cgi);
        my $qs = $cgi->env->{QUERY_STRING};
-       $url = $cgi->base->as_string . $inbox . '/';
        $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
        $url .= $suffix if (defined $suffix);
        $url .= "?$qs" if $qs ne '';
diff --git a/t/inbox.t b/t/inbox.t
new file mode 100644 (file)
index 0000000..45ba1df
--- /dev/null
+++ b/t/inbox.t
@@ -0,0 +1,12 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use_ok 'PublicInbox::Inbox';
+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'});
+is($x->base_url, 'http://example.com/test/', 'added trailing slash');
+
+done_testing();
diff --git a/t/psgi_mount.t b/t/psgi_mount.t
new file mode 100644 (file)
index 0000000..c1c1b0c
--- /dev/null
@@ -0,0 +1,78 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use Email::MIME;
+use File::Temp qw/tempdir/;
+my $tmpdir = tempdir('psgi-path-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+my $maindir = "$tmpdir/main.git";
+my $addr = 'test-public@example.com';
+my $cfgpfx = "publicinbox.test";
+my @mods = qw(HTTP::Request::Common Plack::Request Plack::Test URI::Escape);
+foreach my $mod (@mods) {
+       eval "require $mod";
+       plan skip_all => "$mod missing for plack.t" if $@;
+}
+use_ok $_ foreach @mods;
+use PublicInbox::Import;
+use PublicInbox::Git;
+use PublicInbox::Config;
+use PublicInbox::WWW;
+use Plack::Builder;
+use Plack::App::URLMap;
+my $config = PublicInbox::Config->new({
+       "$cfgpfx.address" => $addr,
+       "$cfgpfx.mainrepo" => $maindir,
+});
+is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
+my $git = PublicInbox::Git->new($maindir);
+my $im = PublicInbox::Import->new($git, 'test', $addr);
+{
+       my $mime = Email::MIME->new(<<EOF);
+From: Me <me\@example.com>
+To: You <you\@example.com>
+Cc: $addr
+Message-Id: <blah\@example.com>
+Subject: hihi
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+zzzzzz
+EOF
+       $im->add($mime);
+       $im->done;
+}
+
+my $www = PublicInbox::WWW->new($config);
+my $app = builder {
+       enable 'Head';
+       mount '/a' => builder { sub { $www->call(@_) } };
+       mount '/b' => builder { sub { $www->call(@_) } };
+};
+
+test_psgi($app, sub {
+       my ($cb) = @_;
+       my $res;
+       # Atom feed:
+       $res = $cb->(GET('/a/test/new.atom'));
+       like($res->content, qr!\bhttp://[^/]+/a/test/!,
+               'URLs which exist in Atom feed are mount-aware');
+       unlike($res->content, qr!\b\Qhttp://[^/]+/test/\E!,
+               'No URLs which are not mount-aware');
+
+       # redirects
+       $res = $cb->(GET('/a/test/blah%40example.com/'));
+       is($res->code, 200, 'OK with URLMap mount');
+       $res = $cb->(GET('/a/test/blah%40example.com/raw'));
+       is($res->code, 200, 'OK with URLMap mount');
+       $res = $cb->(GET('/a/test/m/blah%40example.com.html'));
+       is($res->header('Location'),
+               'http://localhost/a/test/blah%40example.com/',
+               'redirect functions properly under mount');
+
+       $res = $cb->(GET('/test/blah%40example.com/'));
+       is($res->code, 404, 'intentional 404 with URLMap mount');
+
+});
+
+done_testing();