]> Sergey Matveev's repositories - public-inbox.git/commitdiff
allow running as a Plack app without CGI emulation
authorEric Wong <e@80x24.org>
Mon, 28 Apr 2014 04:56:47 +0000 (04:56 +0000)
committerEric Wong <e@80x24.org>
Mon, 28 Apr 2014 04:56:47 +0000 (04:56 +0000)
This might be slightly cleaner, though generating the base URL
now has an ugly condition in it.

lib/PublicInbox/Feed.pm
public-inbox.cgi

index bddba912a5cc720a9989d830a72da803b708cada..220703709264b988c3a503f99fa7cc789a56e04a 100644 (file)
@@ -109,16 +109,13 @@ sub nav_footer {
        my $old_r = $cgi->param('r');
        my $prev = '    ';
        my $next = '    ';
-       my %opts = (-path => 1, -query => 1, -relative => 1);
 
        if ($last) {
-               $cgi->param('r', $last);
-               $next = $cgi->url(%opts);
+               $next = $cgi->path_info . "?r=$last";
                $next = qq!<a href="$next">next</a>!;
        }
        if ($first && $old_r) {
-               $cgi->param('r', "$first..");
-               $prev = $cgi->url(%opts);
+               $prev = $cgi->path_info . "?r=$first..";
                $prev = qq!<a href="$prev">prev</a>!;
        }
        "$prev $next";
@@ -213,14 +210,20 @@ sub get_feedopts {
        }
        my $url_base;
        if ($cgi) {
-               my $cgi_url = $cgi->url(-path=>1, -relative=>1);
-               my $base = $cgi->url(-base);
-               $url_base = $cgi_url;
+               my $path_info = $cgi->path_info;
+               my $base;
+               if (ref($cgi) eq 'CGI') {
+                       $base = $cgi->url(-base);
+               } else {
+                       $base = "${$cgi->base}";
+                       $base =~ s!/\z!!;
+               }
+               $url_base = $path_info;
                if ($url_base =~ s!/(?:|index\.html)?\z!!) {
                        $rv{atomurl} = "$base$url_base/atom.xml";
                } else {
                        $url_base =~ s!/atom\.xml\z!!;
-                       $rv{atomurl} = $base . $cgi_url;
+                       $rv{atomurl} = $base . $path_info;
                        $url_base = $base . $url_base; # XXX is this needed?
                }
        } else {
index 87cc6943b1b344abe75e38ea5d6e0c4cb5466690..7f7e59ca6e0a50b30a629b9240736fc3f5aa7bee 100755 (executable)
@@ -30,7 +30,13 @@ BEGIN {
 if ($ENV{PI_PLACKUP}) {
        psgi_app();
 } else {
-       my $ret = main();
+       # some servers (Ruby webrick) include scheme://host[:port] here,
+       # which confuses CGI.pm when generating self_url.
+       # RFC 3875 does not mention REQUEST_URI at all,
+       # so nuke it since CGI.pm functions without it.
+       delete $ENV{REQUEST_URI};
+       my $req = CGI->new;
+       my $ret = main($req, $req->request_method);
        binmode STDOUT;
        if (@ARGV && $ARGV[0] eq 'static') {
                print $ret->[2]->[0];
@@ -42,15 +48,9 @@ if ($ENV{PI_PLACKUP}) {
 # private functions below
 
 sub main {
-       # some servers (Ruby webrick) include scheme://host[:port] here,
-       # which confuses CGI.pm when generating self_url.
-       # RFC 3875 does not mention REQUEST_URI at all,
-       # so nuke it since CGI.pm functions without it.
-       delete $ENV{REQUEST_URI};
-
-       my $cgi = CGI->new;
+       my ($cgi, $method) = @_;
        my %ctx;
-       if ($cgi->request_method !~ /\AGET|HEAD\z/) {
+       if ($method !~ /\AGET|HEAD\z/) {
                return r(405, 'Method Not Allowed');
        }
        my $path_info = $enc_utf8->decode($cgi->path_info);
@@ -205,8 +205,6 @@ sub do_redirect {
 }
 
 sub psgi_app {
-       require CGI::Emulate::PSGI;
-
        # preload so we are CoW friendly
        require PublicInbox::Feed;
        require PublicInbox::View;
@@ -214,12 +212,12 @@ sub psgi_app {
        require Digest::SHA;
        require POSIX;
        require XML::Atom::SimpleFeed;
-       eval { require Git };
+       require Plack::Request;
+       eval { require Git }; # optional
        sub {
-               my ($e) = @_;
-               local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($e));
-               main();
-       }
+               my $req = Plack::Request->new(@_);
+               main($req, $req->method);
+       };
 }
 
 sub cgi_print {