]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
implement legacy redirects for old URLs
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # We focus on the lowest common denominators here:
5 # - targeted at text-only console browsers (w3m, links, etc..)
6 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
7 # - No JavaScript, graphics or icons allowed.
8 # - Must not rely on static content
9 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
10 package PublicInbox::WWW;
11 use 5.008;
12 use strict;
13 use warnings;
14 use PublicInbox::Config;
15 use URI::Escape qw(uri_escape_utf8 uri_unescape);
16 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
17 use constant PI_URL => 'http://public-inbox.org/';
18 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
19 our $pi_config;
20 BEGIN {
21         $pi_config = PublicInbox::Config->new;
22 }
23
24 sub run {
25         my ($cgi, $method) = @_;
26         my %ctx = (cgi => $cgi, pi_config => $pi_config);
27         if ($method !~ /\AGET|HEAD\z/) {
28                 return r(405, 'Method Not Allowed');
29         }
30         my $path_info = $cgi->path_info;
31
32         # top-level indices and feeds
33         if ($path_info eq '/') {
34                 r404();
35         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
36                 invalid_list(\%ctx, $1) || redirect_list_index($cgi);
37         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
38                 invalid_list(\%ctx, $1) || get_index(\%ctx);
39         } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
40                 invalid_list(\%ctx, $1) || get_atom(\%ctx);
41
42         # single-message pages
43         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
44                 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx);
45         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
46                 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
47
48         # full-message page
49         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
50                 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
51
52         # thread display
53         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
54                 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
55
56         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!x) {
57                 my $sfx = $3;
58                 invalid_list_mid(\%ctx, $1, $2) ||
59                         get_thread_mbox(\%ctx, $sfx);
60
61         # legacy redirects
62         } elsif ($path_info =~ m!$LISTNAME_RE/(t|m|f)/(\S+)\.html\z!o) {
63                 my $pfx = $2;
64                 invalid_list_mid(\%ctx, $1, $3) ||
65                         redirect_mid(\%ctx, $pfx, qr/\.html\z/, '/');
66         } elsif ($path_info =~ m!$LISTNAME_RE/(m|f)/(\S+)\.txt\z!o) {
67                 my $pfx = $2;
68                 invalid_list_mid(\%ctx, $1, $3) ||
69                         redirect_mid(\%ctx, $pfx, qr/\.txt\z/, '/raw');
70         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
71                 my $end = $3;
72                 invalid_list_mid(\%ctx, $1, $2) ||
73                         redirect_mid(\%ctx, 't', $end, '/mbox.gz');
74
75         # convenience redirects, order matters
76         } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
77                 my $pfx = $2;
78                 invalid_list_mid(\%ctx, $1, $3) ||
79                         redirect_mid(\%ctx, $pfx, qr/\z/, '/');
80
81         } else {
82                 r404();
83         }
84 }
85
86 # for CoW-friendliness, MOOOOO!
87 sub preload {
88         require PublicInbox::Feed;
89         require PublicInbox::View;
90         require PublicInbox::Thread;
91         require PublicInbox::GitCatFile;
92         require Email::MIME;
93         require Digest::SHA;
94         require POSIX;
95
96         eval {
97                 require PublicInbox::Search;
98                 require PublicInbox::Mbox;
99                 require IO::Compress::Gzip;
100         };
101 }
102
103 # private functions below
104
105 sub r404 { r(404, 'Not Found') }
106
107 # simple response for errors
108 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
109
110 # returns undef if valid, array ref response if invalid
111 sub invalid_list {
112         my ($ctx, $listname) = @_;
113         my $git_dir = $pi_config->get($listname, "mainrepo");
114         if (defined $git_dir) {
115                 $ctx->{git_dir} = $git_dir;
116                 $ctx->{listname} = $listname;
117                 return;
118         }
119         r404();
120 }
121
122 # returns undef if valid, array ref response if invalid
123 sub invalid_list_mid {
124         my ($ctx, $listname, $mid) = @_;
125         my $ret = invalid_list($ctx, $listname, $mid);
126         $ctx->{mid} = uri_unescape($mid) unless $ret;
127         $ret;
128 }
129
130 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
131 sub get_atom {
132         my ($ctx) = @_;
133         require PublicInbox::Feed;
134         PublicInbox::Feed::generate($ctx);
135 }
136
137 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
138 sub get_index {
139         my ($ctx) = @_;
140         require PublicInbox::Feed;
141         my $srch = searcher($ctx);
142         footer($ctx);
143         PublicInbox::Feed::generate_html_index($ctx);
144 }
145
146 # just returns a string ref for the blob in the current ctx
147 sub mid2blob {
148         my ($ctx) = @_;
149         require PublicInbox::MID;
150         my $path = PublicInbox::MID::mid2path($ctx->{mid});
151         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
152                         qw(cat-file blob), "HEAD:$path");
153         my $pid = open my $fh, '-|';
154         defined $pid or die "fork failed: $!\n";
155         if ($pid == 0) {
156                 open STDERR, '>', '/dev/null'; # ignore errors
157                 exec @cmd or die "exec failed: $!\n";
158         } else {
159                 my $blob = eval { local $/; <$fh> };
160                 close $fh;
161                 $? == 0 ? \$blob : undef;
162         }
163 }
164
165 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw mbox
166 sub get_mid_txt {
167         my ($ctx) = @_;
168         my $x = mid2blob($ctx) or return r404();
169         require PublicInbox::Mbox;
170         PublicInbox::Mbox::emit1($x);
171 }
172
173 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
174 sub get_mid_html {
175         my ($ctx) = @_;
176         my $x = mid2blob($ctx) or return r404();
177
178         require PublicInbox::View;
179         my $pfx = msg_pfx($ctx);
180         my $foot = footer($ctx);
181         require Email::MIME;
182         my $mime = Email::MIME->new($x);
183         searcher($ctx);
184         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
185           [ PublicInbox::View::msg_html($ctx, $mime, $pfx, $foot) ] ];
186 }
187
188 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
189 sub get_full_html {
190         my ($ctx) = @_;
191         my $x = mid2blob($ctx) or return r404();
192
193         require PublicInbox::View;
194         my $foot = footer($ctx);
195         require Email::MIME;
196         my $mime = Email::MIME->new($x);
197         searcher($ctx);
198         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
199           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
200 }
201
202 # /$LISTNAME/t/$MESSAGE_ID.html
203 sub get_thread {
204         my ($ctx) = @_;
205         my $srch = searcher($ctx) or return need_search($ctx);
206         require PublicInbox::View;
207         my $foot = footer($ctx);
208         PublicInbox::View::thread_html($ctx, $foot, $srch);
209 }
210
211 sub self_url {
212         my ($cgi) = @_;
213         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
214 }
215
216 sub redirect_list_index {
217         my ($cgi) = @_;
218         do_redirect(self_url($cgi) . "/");
219 }
220
221 sub redirect_mid {
222         my ($ctx, $pfx, $old, $sfx) = @_;
223         my $url = self_url($ctx->{cgi});
224         my $anchor = '';
225         if (lc($pfx) eq 't' && $sfx eq '/') {
226                 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
227         }
228         $url =~ s/$old/$sfx/;
229         do_redirect($url . $anchor);
230 }
231
232 # only hit when somebody tries to guess URLs manually:
233 sub redirect_mid_txt {
234         my ($ctx, $pfx) = @_;
235         my $listname = $ctx->{listname};
236         my $url = self_url($ctx->{cgi});
237         $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
238         do_redirect($url);
239 }
240
241 sub do_redirect {
242         my ($url) = @_;
243         [ 301,
244           [ Location => $url, 'Content-Type' => 'text/plain' ],
245           [ "Redirecting to $url\n" ]
246         ]
247 }
248
249 sub ctx_get {
250         my ($ctx, $key) = @_;
251         my $val = $ctx->{$key};
252         (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
253         $val;
254 }
255
256 sub try_cat {
257         my ($path) = @_;
258         my $rv;
259         if (open(my $fh, '<', $path)) {
260                 local $/;
261                 $rv = <$fh>;
262                 close $fh;
263         }
264         $rv;
265 }
266
267 sub footer {
268         my ($ctx) = @_;
269         return '' unless $ctx;
270         my $git_dir = ctx_get($ctx, 'git_dir');
271
272         # favor user-supplied footer
273         my $footer = try_cat("$git_dir/public-inbox/footer.html");
274         if (defined $footer) {
275                 chomp $footer;
276                 $ctx->{footer} = $footer;
277                 return $footer;
278         }
279
280         # auto-generate a footer
281         my $listname = ctx_get($ctx, 'listname');
282         my $desc = try_cat("$git_dir/description");
283         $desc = '$GIT_DIR/description missing' unless defined $desc;
284         chomp $desc;
285
286         my $urls = try_cat("$git_dir/cloneurl");
287         my @urls = split(/\r?\n/, $urls || '');
288         my $nurls = scalar @urls;
289         if ($nurls == 0) {
290                 $urls = '($GIT_DIR/cloneurl missing)';
291         } elsif ($nurls == 1) {
292                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
293                         '">ssoma</a>: ' . $urls[0];
294         } else {
295                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
296                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
297         }
298
299         my $addr = $pi_config->get($listname, 'address');
300         if (ref($addr) eq 'ARRAY') {
301                 $addr = $addr->[0]; # first address is primary
302         }
303
304         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
305
306         $ctx->{footer} = join("\n",
307                 '- ' . $desc,
308                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
309                         'anybody may post in plain-text (not HTML):',
310                 $addr,
311                 $urls
312         );
313 }
314
315 # search support is optional, returns undef if Xapian is not installed
316 # or not configured for the given GIT_DIR
317 sub searcher {
318         my ($ctx) = @_;
319         eval {
320                 require PublicInbox::Search;
321                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
322         };
323 }
324
325 sub need_search {
326         my ($ctx) = @_;
327         my $msg = <<EOF;
328 <html><head><title>Search not available for this
329 public-inbox</title><body><pre>Search is not available for this public-inbox
330 <a href="../">Return to index</a></pre></body></html>
331 EOF
332         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
333 }
334
335 sub msg_pfx {
336         my ($ctx) = @_;
337         my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
338         "../../f/$href/";
339 }
340
341 # /$LISTNAME/t/$MESSAGE_ID/mbox           -> thread as mbox
342 # /$LISTNAME/t/$MESSAGE_ID/mbox.gz        -> thread as gzipped mbox
343 # note: I'm not a big fan of other compression formats since they're
344 # significantly more expensive on CPU than gzip and less-widely available,
345 # especially on older systems.  Stick to zlib since that's what git uses.
346 sub get_thread_mbox {
347         my ($ctx, $sfx) = @_;
348         my $srch = searcher($ctx) or return need_search($ctx);
349         require PublicInbox::Mbox;
350         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
351 }
352
353 1;