]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
update copyright headers and email addresses
[public-inbox.git] / public-inbox.cgi
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 use strict;
5 use warnings;
6 use IO::Handle;
7 use PublicInbox::WWW;
8 use CGI qw/-nosticky/;
9 our $NO_SCRIPT_NAME;
10 our %HTTP_CODES;
11 BEGIN {
12         $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
13         if ($ENV{MOD_PERL}) {
14                 CGI->compile;
15                 PublicInbox::WWW->preload;
16         }
17
18         %HTTP_CODES = (
19                 200 => 'OK',
20                 300 => 'Multiple Choices',
21                 301 => 'Moved Permanently',
22                 302 => 'Found',
23                 404 => 'Not Found',
24                 405 => 'Method Not Allowed',
25                 501 => 'Not Implemented',
26         );
27 }
28
29 # some servers (Ruby webrick) include scheme://host[:port] here,
30 # which confuses CGI.pm when generating self_url.
31 # RFC 3875 does not mention REQUEST_URI at all,
32 # so nuke it since CGI.pm functions without it.
33 delete $ENV{REQUEST_URI};
34 $ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
35 my $req = CGI->new;
36 my $ret = PublicInbox::WWW::run($req, $req->request_method);
37
38 my $out = select;
39 $out->binmode;
40
41 if (ref($ret) eq 'CODE') {
42         $ret->(*dump_header);
43 } else {
44         my ($status, $headers, $body) = @$ret;
45
46         dump_header([$status, $headers])->write($body->[0]);
47 }
48
49 sub dump_header {
50         my ($res) = @_;
51         my $fh = select;
52         my ($status, $headers) = @$res;
53         $fh->write("Status: $status $HTTP_CODES{$status}\r\n");
54         my @tmp = @$headers;
55         while (my ($k, $v) = splice(@tmp, 0, 2)) {
56                 $fh->write("$k: $v\r\n");
57         }
58         $fh->write("\r\n");
59         $fh;
60 }