]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
favor "--git-dir=..." over "--git-dir ..."
[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                 $CGI::NOSTICKY = 1;
25                 CGI->compile;
26         }
27 }
28
29 if ($ENV{PI_PLACKUP}) {
30         psgi_app();
31 } else {
32         # some servers (Ruby webrick) include scheme://host[:port] here,
33         # which confuses CGI.pm when generating self_url.
34         # RFC 3875 does not mention REQUEST_URI at all,
35         # so nuke it since CGI.pm functions without it.
36         require CGI;
37         delete $ENV{REQUEST_URI};
38         my $req = CGI->new;
39         my $ret = main($req, $req->request_method);
40         binmode STDOUT;
41         if (@ARGV && $ARGV[0] eq 'static') {
42                 print $ret->[2]->[0];
43         } else { # CGI
44                 cgi_print($ret);
45         }
46 }
47
48 # private functions below
49
50 sub main {
51         my ($cgi, $method) = @_;
52         my %ctx;
53         if ($method !~ /\AGET|HEAD\z/) {
54                 return r(405, 'Method Not Allowed');
55         }
56         my $path_info = $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         require Digest::SHA;
146         my $hex = Digest::SHA::sha1_hex($ctx->{mid});
147         $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
148                         die "BUG: not a SHA-1 hex: $hex";
149         require IPC::Run;
150         my ($in, $blob, $err);
151         open my $null, '+<', '/dev/null' or die "open: $!\n";
152         IPC::Run::run(['git', "--git-dir=$ctx->{git_dir}",
153                         qw(cat-file blob), "HEAD:$1/$2"],
154                         $null, \$blob, $null);
155         $? == 0 ? \$blob : undef;
156 }
157
158 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
159 sub get_mid_txt {
160         my ($ctx, $cgi) = @_;
161         my $x = mid2blob($ctx);
162         $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
163 }
164
165 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
166 sub get_mid_html {
167         my ($ctx, $cgi) = @_;
168         my $x = mid2blob($ctx);
169         return r404() unless $x;
170
171         require PublicInbox::View;
172         my $mid_href = PublicInbox::Hval::ascii_html(
173                                                 uri_escape_utf8($ctx->{mid}));
174         my $pfx = "../f/$mid_href.html";
175         require Email::MIME;
176         [ 200, [ 'Content-Type' => 'text/html' ],
177                 [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
178 }
179
180 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
181 sub get_full_html {
182         my ($ctx, $cgi) = @_;
183         my $x = mid2blob($ctx);
184         return r404() unless $x;
185         require PublicInbox::View;
186         require Email::MIME;
187         [ 200, [ 'Content-Type' => 'text/html' ],
188                 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
189 }
190
191 sub self_url {
192         my ($cgi) = @_;
193         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
194 }
195
196 sub redirect_list_index {
197         my ($ctx, $cgi) = @_;
198         do_redirect(self_url($cgi) . "/");
199 }
200
201 sub redirect_mid {
202         my ($ctx, $cgi) = @_;
203         my $url = self_url($cgi);
204         $url =~ s!/f/!/m/!;
205         do_redirect($url . '.html');
206 }
207
208 sub do_redirect {
209         my ($url) = @_;
210         [ 301,
211           [ Location => $url, 'Content-Type' => 'text/plain' ],
212           [ "Redirecting to $url\n" ]
213         ]
214 }
215
216 sub psgi_app {
217         # preload so we are CoW friendly
218         require PublicInbox::Feed;
219         require PublicInbox::View;
220         require Mail::Thread;
221         require Digest::SHA;
222         require POSIX;
223         require XML::Atom::SimpleFeed;
224         require Plack::Request;
225         sub {
226                 my $req = Plack::Request->new(@_);
227                 main($req, $req->method);
228         };
229 }
230
231 sub cgi_print {
232         my ($ret) = @_;
233         my ($status, $headers, $body) = @$ret;
234         my %codes = (
235                 200 => 'OK',
236                 301 => 'Moved Permanently',
237                 404 => 'Not Found',
238                 405 => 'Method Not Allowed',
239         );
240
241         print "Status: $status $codes{$status}\r\n";
242         my @tmp = @$headers;
243         while (my ($k, $v) = splice(@tmp, 0, 2)) {
244                 print "$k: $v\r\n";
245         }
246         print "\r\n", $body->[0];
247 }