]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
56040676f6ae1142a1b133a3ce2c8a80c741af81
[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 or die "exec failed: $!\n";
159         } else {
160                 my $blob = eval { local $/; <$fh> };
161                 close $fh;
162                 $? == 0 ? \$blob : undef;
163         }
164 }
165
166 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
167 sub get_mid_txt {
168         my ($ctx, $cgi) = @_;
169         my $x = mid2blob($ctx);
170         $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
171 }
172
173 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
174 sub get_mid_html {
175         my ($ctx, $cgi) = @_;
176         my $x = mid2blob($ctx);
177         return r404() unless $x;
178
179         require PublicInbox::View;
180         my $mid_href = PublicInbox::Hval::ascii_html(
181                                                 uri_escape_utf8($ctx->{mid}));
182         my $pfx = "../f/$mid_href.html";
183         require Email::MIME;
184         [ 200, [ 'Content-Type' => 'text/html' ],
185                 [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
186 }
187
188 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
189 sub get_full_html {
190         my ($ctx, $cgi) = @_;
191         my $x = mid2blob($ctx);
192         return r404() unless $x;
193         require PublicInbox::View;
194         require Email::MIME;
195         [ 200, [ 'Content-Type' => 'text/html' ],
196                 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
197 }
198
199 sub self_url {
200         my ($cgi) = @_;
201         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
202 }
203
204 sub redirect_list_index {
205         my ($ctx, $cgi) = @_;
206         do_redirect(self_url($cgi) . "/");
207 }
208
209 sub redirect_mid {
210         my ($ctx, $cgi) = @_;
211         my $url = self_url($cgi);
212         $url =~ s!/f/!/m/!;
213         do_redirect($url . '.html');
214 }
215
216 sub do_redirect {
217         my ($url) = @_;
218         [ 301,
219           [ Location => $url, 'Content-Type' => 'text/plain' ],
220           [ "Redirecting to $url\n" ]
221         ]
222 }
223
224 sub psgi_app {
225         # preload so we are CoW friendly
226         require PublicInbox::Feed;
227         require PublicInbox::View;
228         require Mail::Thread;
229         require Digest::SHA;
230         require POSIX;
231         require XML::Atom::SimpleFeed;
232         require Plack::Request;
233         sub {
234                 my $req = Plack::Request->new(@_);
235                 main($req, $req->method);
236         };
237 }
238
239 sub cgi_print {
240         my ($ret) = @_;
241         my ($status, $headers, $body) = @$ret;
242         my %codes = (
243                 200 => 'OK',
244                 301 => 'Moved Permanently',
245                 404 => 'Not Found',
246                 405 => 'Method Not Allowed',
247         );
248
249         print "Status: $status $codes{$status}\r\n";
250         my @tmp = @$headers;
251         while (my ($k, $v) = splice(@tmp, 0, 2)) {
252                 print "$k: $v\r\n";
253         }
254         print "\r\n", $body->[0];
255 }