]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
33313bf55dd2b549bacd14413874e08e0701e87d
[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/atom\.xml\z!o) {
69                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
70
71         # single-message pages
72         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
73                 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
74         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
75                 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
76
77         # full-message page
78         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
79                 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
80
81         # convenience redirect
82         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
83                 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
84
85         } else {
86                 r404();
87         }
88 }
89
90 sub r404 { r("404 Not Found") }
91
92 # simple response for errors
93 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, $_[0]."\n" ] }
94
95 # returns undef if valid, array ref response if invalid
96 sub invalid_list {
97         my ($ctx, $listname) = @_;
98         my $git_dir = $pi_config->get($listname, "mainrepo");
99         if (defined $git_dir) {
100                 $ctx->{git_dir} = $git_dir;
101                 $ctx->{listname} = $listname;
102                 return undef;
103         }
104         r404();
105 }
106
107 # returns undef if valid, array ref response if invalid
108 sub invalid_list_mid {
109         my ($ctx, $listname, $mid) = @_;
110         my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
111         $ctx->{mid} = uri_unescape($mid);
112         undef;
113 }
114
115 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
116 sub get_atom {
117         my ($ctx, $cgi, $top) = @_;
118         require PublicInbox::Feed;
119         [ '200 OK', { 'Content-Type' => 'application/xml' },
120           PublicInbox::Feed->generate({
121                         git_dir => $ctx->{git_dir},
122                         listname => $ctx->{listname},
123                         pi_config => $pi_config,
124                         cgi => $cgi,
125                         top => $top,
126                 })
127         ];
128 }
129
130 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
131 sub get_index {
132         my ($ctx, $cgi, $top) = @_;
133         require PublicInbox::Feed;
134         [ '200 OK', { 'Content-Type' => 'text/html' },
135           PublicInbox::Feed->generate_html_index({
136                         git_dir => $ctx->{git_dir},
137                         listname => $ctx->{listname},
138                         pi_config => $pi_config,
139                         cgi => $cgi,
140                         top => $top,
141                 })
142         ];
143 }
144
145 # just returns a string ref for the blob in the current ctx
146 sub mid2blob {
147         my ($ctx) = @_;
148         local $ENV{GIT_DIR} = $ctx->{git_dir};
149         require Digest::SHA;
150         my $hex = Digest::SHA::sha1_hex($ctx->{mid});
151         $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
152                         die "BUG: not a SHA-1 hex: $hex";
153         my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
154         $? == 0 ? \$blob : undef;
155 }
156
157 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
158 sub get_mid_txt {
159         my ($ctx, $cgi) = @_;
160         my $x = mid2blob($ctx);
161         $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
162 }
163
164 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
165 sub get_mid_html {
166         my ($ctx, $cgi) = @_;
167         my $x = mid2blob($ctx);
168         return r404() unless $x;
169
170         my $pfx = "../f/" . uri_escape($ctx->{mid}) . ".html";
171         require PublicInbox::View;
172         require Email::MIME;
173         [ "200 OK", {'Content-Type' => 'text/html'},
174                 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
175 }
176
177 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
178 sub get_full_html {
179         my ($ctx, $cgi) = @_;
180         my $x = mid2blob($ctx);
181         return r404() unless $x;
182         require PublicInbox::View;
183         require Email::MIME;
184         [ "200 OK", {'Content-Type' => 'text/html'},
185                 PublicInbox::View->as_html(Email::MIME->new($$x))];
186 }
187
188 sub redirect_list_index {
189         my ($ctx, $cgi) = @_;
190         do_redirect($cgi->self_url . "/");
191 }
192
193 sub redirect_mid {
194         my ($ctx, $cgi) = @_;
195         my $url = $cgi->self_url;
196         $url =~ s!/f/!/m/!;
197         do_redirect($url . '.html');
198 }
199
200 sub do_redirect {
201         my ($url) = @_;
202         [ '301 Moved Permanently',
203           { Location => $url, 'Content-Type' => 'text/plain' },
204           "Redirecting to $url\n"
205         ]
206 }
207
208 # only used for CGI and static file generation modes
209 sub set_binmode {
210         my ($headers) = @_;
211         if ($headers->{'Content-Type'} eq 'text/plain') {
212                 # no way to validate raw messages, mixed encoding is possible.
213                 binmode STDOUT;
214         } else { # strict encoding for HTML and XML
215                 binmode STDOUT, ':encoding(UTF-8)';
216         }
217 }