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