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