]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
split out WWW package and CGI/PSGI-specific parts
[public-inbox.git] / public-inbox.cgi
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 use strict;
5 use warnings;
6 require PublicInbox::WWW;
7 use CGI qw/-nosticky/;
8 our $NO_SCRIPT_NAME;
9 BEGIN {
10         $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
11         CGI->compile if $ENV{MOD_PERL};
12 }
13
14 # some servers (Ruby webrick) include scheme://host[:port] here,
15 # which confuses CGI.pm when generating self_url.
16 # RFC 3875 does not mention REQUEST_URI at all,
17 # so nuke it since CGI.pm functions without it.
18 delete $ENV{REQUEST_URI};
19 $ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
20 my $req = CGI->new;
21 my $ret = PublicInbox::WWW::run($req, $req->request_method);
22 binmode STDOUT;
23 if (@ARGV && $ARGV[0] eq 'static') {
24         print $ret->[2]->[0]; # only show the body
25 } else { # CGI
26         my ($status, $headers, $body) = @$ret;
27         my %codes = (
28                 200 => 'OK',
29                 301 => 'Moved Permanently',
30                 404 => 'Not Found',
31                 405 => 'Method Not Allowed',
32         );
33
34         print "Status: $status $codes{$status}\r\n";
35         my @tmp = @$headers;
36         while (my ($k, $v) = splice(@tmp, 0, 2)) {
37                 print "$k: $v\r\n";
38         }
39         print "\r\n", $body->[0];
40 }