]> Sergey Matveev's repositories - public-inbox.git/blobdiff - public-inbox.cgi
remove direct CGI.pm support
[public-inbox.git] / public-inbox.cgi
index 4b74a62ae04f59710e32774e611d6e9d3b04cb71..e73e23ca7663662c907021a695a857fbdd6af4cb 100755 (executable)
@@ -1,62 +1,35 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
-# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+# Copyright (C) 2014-2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ or later <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Enables using PublicInbox::WWW as a CGI script
 use strict;
 use warnings;
-use IO::Handle;
+use Plack::Loader;
+use Plack::Builder;
+use Plack::Request;
+use Plack::Handler::CGI;
 use PublicInbox::WWW;
-use CGI qw/-nosticky/;
-our $NO_SCRIPT_NAME;
-our %HTTP_CODES;
-BEGIN {
-       $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
-       if ($ENV{MOD_PERL}) {
-               CGI->compile;
-               PublicInbox::WWW->preload;
-       }
-
-       %HTTP_CODES = (
-               200 => 'OK',
-               300 => 'Multiple Choices',
-               301 => 'Moved Permanently',
-               302 => 'Found',
-               404 => 'Not Found',
-               405 => 'Method Not Allowed',
-               501 => 'Not Implemented',
-       );
-}
-
-# 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};
-$ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
-my $req = CGI->new;
-my $ret = PublicInbox::WWW::run($req, $req->request_method);
+BEGIN { PublicInbox::WWW->preload if $ENV{MOD_PERL} }
 
-my $out = select;
-$out->binmode;
-
-if (ref($ret) eq 'CODE') {
-       $ret->(*dump_header);
-} else {
-       my ($status, $headers, $body) = @$ret;
+my $have_deflater = eval { require Plack::Middleware::Deflater; 1 };
+my $app = builder {
+       if ($have_deflater) {
+               enable 'Deflater',
+                       content_type => [ 'text/html', 'text/plain',
+                                       'application/atom+xml' ];
+       }
 
-       dump_header([$status, $headers])->write($body->[0]);
-}
+       # Enable to ensure redirects and Atom feed URLs are generated
+       # properly when running behind a reverse proxy server which
+       # sets X-Forwarded-For and X-Forwarded-Proto request headers.
+       # See Plack::Middleware::ReverseProxy documentation for details
+       # enable 'ReverseProxy';
 
-sub dump_header {
-       my ($res) = @_;
-       my $fh = select;
-       my ($status, $headers) = @$res;
-       $fh->write("Status: $status $HTTP_CODES{$status}\r\n");
-       my @tmp = @$headers;
-       while (my ($k, $v) = splice(@tmp, 0, 2)) {
-               $fh->write("$k: $v\r\n");
+       enable 'Head';
+       sub {
+               my $req = Plack::Request->new(@_);
+               PublicInbox::WWW::run($req, $req->method);
        }
-       $fh->write("\r\n");
-       $fh;
-}
+};
+Plack::Handler::CGI->new->run($app);