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