]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
cgi: remove dependency on IPC::Run in CGI
[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 #
5 # We focus on the lowest common denominators here:
6 # - targeted at text-only console browsers (lynx, w3m, etc..)
7 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
8 # - No JavaScript, graphics or icons allowed.
9 # - Must not rely on static content
10 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
11
12 use 5.008;
13 use strict;
14 use warnings;
15 use PublicInbox::Config;
16 use URI::Escape qw(uri_escape_utf8 uri_unescape);
17 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
18 our $pi_config;
19 BEGIN {
20         $pi_config = PublicInbox::Config->new;
21         # TODO: detect and reload config as needed
22         if ($ENV{MOD_PERL}) {
23                 require CGI;
24                 no warnings;
25                 $CGI::NOSTICKY = 1;
26                 CGI->compile;
27         }
28 }
29
30 if ($ENV{PI_PLACKUP}) {
31         psgi_app();
32 } else {
33         # some servers (Ruby webrick) include scheme://host[:port] here,
34         # which confuses CGI.pm when generating self_url.
35         # RFC 3875 does not mention REQUEST_URI at all,
36         # so nuke it since CGI.pm functions without it.
37         require CGI;
38         delete $ENV{REQUEST_URI};
39         my $req = CGI->new;
40         my $ret = main($req, $req->request_method);
41         binmode STDOUT;
42         if (@ARGV && $ARGV[0] eq 'static') {
43                 print $ret->[2]->[0];
44         } else { # CGI
45                 cgi_print($ret);
46         }
47 }
48
49 # private functions below
50
51 sub main {
52         my ($cgi, $method) = @_;
53         my %ctx;
54         if ($method !~ /\AGET|HEAD\z/) {
55                 return r(405, 'Method Not Allowed');
56         }
57         my $path_info = $cgi->path_info;
58
59         # top-level indices and feeds
60         if ($path_info eq '/') {
61                 r404();
62         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
63                 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
64         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
65                 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
66         } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
67                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
68
69         # single-message pages
70         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
71                 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
72         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
73                 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
74
75         # full-message page
76         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
77                 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
78
79         # convenience redirects, order matters
80         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
81                 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
82
83         } else {
84                 r404();
85         }
86 }
87
88 sub r404 { r(404, 'Not Found') }
89
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
92
93 # returns undef if valid, array ref response if invalid
94 sub invalid_list {
95         my ($ctx, $listname) = @_;
96         my $git_dir = $pi_config->get($listname, "mainrepo");
97         if (defined $git_dir) {
98                 $ctx->{git_dir} = $git_dir;
99                 $ctx->{listname} = $listname;
100                 return;
101         }
102         r404();
103 }
104
105 # returns undef if valid, array ref response if invalid
106 sub invalid_list_mid {
107         my ($ctx, $listname, $mid) = @_;
108         my $ret = invalid_list($ctx, $listname, $mid);
109         $ctx->{mid} = uri_unescape($mid) unless $ret;
110         $ret;
111 }
112
113 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
114 sub get_atom {
115         my ($ctx, $cgi, $top) = @_;
116         require PublicInbox::Feed;
117         [ 200, [ 'Content-Type' => 'application/xml' ],
118           [ PublicInbox::Feed->generate({
119                         git_dir => $ctx->{git_dir},
120                         listname => $ctx->{listname},
121                         pi_config => $pi_config,
122                         cgi => $cgi,
123                         top => $top,
124                 }) ]
125         ];
126 }
127
128 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
129 sub get_index {
130         my ($ctx, $cgi, $top) = @_;
131         require PublicInbox::Feed;
132         [ 200, [ 'Content-Type' => 'text/html' ],
133           [ PublicInbox::Feed->generate_html_index({
134                         git_dir => $ctx->{git_dir},
135                         listname => $ctx->{listname},
136                         pi_config => $pi_config,
137                         cgi => $cgi,
138                         top => $top,
139                 }) ]
140         ];
141 }
142
143 # just returns a string ref for the blob in the current ctx
144 sub mid2blob {
145         my ($ctx) = @_;
146         require Digest::SHA;
147         my $hex = Digest::SHA::sha1_hex($ctx->{mid});
148         $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
149                         die "BUG: not a SHA-1 hex: $hex";
150
151         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
152                         qw(cat-file blob), "HEAD:$1/$2");
153         my $cmd = join(' ', @cmd);
154         my $pid = open my $fh, '-|';
155         defined $pid or die "fork failed: $!\n";
156         if ($pid == 0) {
157                 open STDERR, '>', '/dev/null'; # ignore errors
158                 exec @cmd;
159                 exit 1;
160         } else {
161                 my $blob = eval { local $/; <$fh> };
162                 close $fh;
163                 $? == 0 ? \$blob : undef;
164         }
165 }
166
167 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
168 sub get_mid_txt {
169         my ($ctx, $cgi) = @_;
170         my $x = mid2blob($ctx);
171         $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
172 }
173
174 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
175 sub get_mid_html {
176         my ($ctx, $cgi) = @_;
177         my $x = mid2blob($ctx);
178         return r404() unless $x;
179
180         require PublicInbox::View;
181         my $mid_href = PublicInbox::Hval::ascii_html(
182                                                 uri_escape_utf8($ctx->{mid}));
183         my $pfx = "../f/$mid_href.html";
184         require Email::MIME;
185         [ 200, [ 'Content-Type' => 'text/html' ],
186                 [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
187 }
188
189 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
190 sub get_full_html {
191         my ($ctx, $cgi) = @_;
192         my $x = mid2blob($ctx);
193         return r404() unless $x;
194         require PublicInbox::View;
195         require Email::MIME;
196         [ 200, [ 'Content-Type' => 'text/html' ],
197                 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
198 }
199
200 sub self_url {
201         my ($cgi) = @_;
202         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
203 }
204
205 sub redirect_list_index {
206         my ($ctx, $cgi) = @_;
207         do_redirect(self_url($cgi) . "/");
208 }
209
210 sub redirect_mid {
211         my ($ctx, $cgi) = @_;
212         my $url = self_url($cgi);
213         $url =~ s!/f/!/m/!;
214         do_redirect($url . '.html');
215 }
216
217 sub do_redirect {
218         my ($url) = @_;
219         [ 301,
220           [ Location => $url, 'Content-Type' => 'text/plain' ],
221           [ "Redirecting to $url\n" ]
222         ]
223 }
224
225 sub psgi_app {
226         # preload so we are CoW friendly
227         require PublicInbox::Feed;
228         require PublicInbox::View;
229         require Mail::Thread;
230         require Digest::SHA;
231         require POSIX;
232         require XML::Atom::SimpleFeed;
233         require Plack::Request;
234         sub {
235                 my $req = Plack::Request->new(@_);
236                 main($req, $req->method);
237         };
238 }
239
240 sub cgi_print {
241         my ($ret) = @_;
242         my ($status, $headers, $body) = @$ret;
243         my %codes = (
244                 200 => 'OK',
245                 301 => 'Moved Permanently',
246                 404 => 'Not Found',
247                 405 => 'Method Not Allowed',
248         );
249
250         print "Status: $status $codes{$status}\r\n";
251         my @tmp = @$headers;
252         while (my ($k, $v) = splice(@tmp, 0, 2)) {
253                 print "$k: $v\r\n";
254         }
255         print "\r\n", $body->[0];
256 }