]> Sergey Matveev's repositories - public-inbox.git/blobdiff - public-inbox.cgi
www: make interface more OO
[public-inbox.git] / public-inbox.cgi
index 57935c50d62fc1ed8d282a04ee2e3dede66a62a9..ee9510c1f012757163076cf8b991af64f78ac6d8 100755 (executable)
@@ -1,40 +1,32 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
-# 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;
-require PublicInbox::WWW;
-use CGI qw/-nosticky/;
-our $NO_SCRIPT_NAME;
-BEGIN {
-       $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
-       CGI->compile if $ENV{MOD_PERL};
-}
+use Plack::Loader;
+use Plack::Builder;
+use Plack::Request;
+use Plack::Handler::CGI;
+use PublicInbox::WWW;
+BEGIN { PublicInbox::WWW->preload if $ENV{MOD_PERL} }
+my $www = PublicInbox::WWW->new;
+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' ];
+       }
 
-# 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);
-binmode STDOUT;
-if (@ARGV && $ARGV[0] eq 'static') {
-       print $ret->[2]->[0]; # only show the body
-} else { # CGI
-       my ($status, $headers, $body) = @$ret;
-       my %codes = (
-               200 => 'OK',
-               301 => 'Moved Permanently',
-               404 => 'Not Found',
-               405 => 'Method Not Allowed',
-       );
+       # 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';
 
-       print "Status: $status $codes{$status}\r\n";
-       my @tmp = @$headers;
-       while (my ($k, $v) = splice(@tmp, 0, 2)) {
-               print "$k: $v\r\n";
-       }
-       print "\r\n", $body->[0];
-}
+       enable 'Head';
+       sub { $www->call(@_) };
+};
+Plack::Handler::CGI->new->run($app);