]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox.cgi
fix quoted URL generation in feeds
[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         require PublicInbox::View;
171         my $mid_href = PublicInbox::View::ascii_html(uri_escape($ctx->{mid}));
172         my $pfx = "../f/$mid_href.html";
173         require Email::MIME;
174         [ "200 OK", {'Content-Type' => 'text/html'},
175                 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
176 }
177
178 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
179 sub get_full_html {
180         my ($ctx, $cgi) = @_;
181         my $x = mid2blob($ctx);
182         return r404() unless $x;
183         require PublicInbox::View;
184         require Email::MIME;
185         [ "200 OK", {'Content-Type' => 'text/html'},
186                 PublicInbox::View->as_html(Email::MIME->new($$x))];
187 }
188
189 sub redirect_list_index {
190         my ($ctx, $cgi) = @_;
191         do_redirect($cgi->self_url . "/");
192 }
193
194 sub redirect_mid {
195         my ($ctx, $cgi) = @_;
196         my $url = $cgi->self_url;
197         $url =~ s!/f/!/m/!;
198         do_redirect($url . '.html');
199 }
200
201 sub do_redirect {
202         my ($url) = @_;
203         [ '301 Moved Permanently',
204           { Location => $url, 'Content-Type' => 'text/plain' },
205           "Redirecting to $url\n"
206         ]
207 }
208
209 # only used for CGI and static file generation modes
210 sub set_binmode {
211         my ($headers) = @_;
212         if ($headers->{'Content-Type'} eq 'text/plain') {
213                 # no way to validate raw messages, mixed encoding is possible.
214                 binmode STDOUT;
215         } else { # strict encoding for HTML and XML
216                 binmode STDOUT, ':encoding(UTF-8)';
217         }
218 }