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