]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: move fallback after legacy matches
[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 $MID_RE = qr!([^/]+)!;
20 our $END_RE = qr!(f/|T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
21 our $pi_config;
22
23 sub run {
24         my ($cgi, $method) = @_;
25         $pi_config ||= PublicInbox::Config->new;
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) || r301($ctx, $1);
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|new\.atom)\z!o) {
40                 invalid_list($ctx, $1) || get_atom($ctx);
41
42         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
43                 msg_page($ctx, $1, $2, $3);
44
45         # convenience redirects order matters
46         } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
47                 r301($ctx, $1, $2);
48
49         } else {
50                 legacy_redirects($ctx, $path_info);
51         }
52 }
53
54 # for CoW-friendliness, MOOOOO!
55 sub preload {
56         require PublicInbox::Feed;
57         require PublicInbox::View;
58         require PublicInbox::Thread;
59         require PublicInbox::GitCatFile;
60         require Email::MIME;
61         require Digest::SHA;
62         require POSIX;
63
64         eval {
65                 require PublicInbox::Search;
66                 require PublicInbox::Mbox;
67                 require IO::Compress::Gzip;
68         };
69 }
70
71 # private functions below
72
73 sub r404 {
74         my ($ctx) = @_;
75         if ($ctx && $ctx->{mid}) {
76                 require PublicInbox::ExtMsg;
77                 return PublicInbox::ExtMsg::ext_msg($ctx);
78         }
79         r(404, 'Not Found');
80 }
81
82 # simple response for errors
83 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
84
85 # returns undef if valid, array ref response if invalid
86 sub invalid_list {
87         my ($ctx, $listname) = @_;
88         my $git_dir = $pi_config->get($listname, "mainrepo");
89         if (defined $git_dir) {
90                 $ctx->{git_dir} = $git_dir;
91                 $ctx->{listname} = $listname;
92                 return;
93         }
94         r404();
95 }
96
97 # returns undef if valid, array ref response if invalid
98 sub invalid_list_mid {
99         my ($ctx, $listname, $mid) = @_;
100         my $ret = invalid_list($ctx, $listname, $mid);
101         return $ret if $ret;
102
103         $ctx->{mid} = $mid = uri_unescape($mid);
104         if ($mid =~ /\A[a-f0-9]{40}\z/) {
105                 if ($mid = mid2blob($ctx)) {
106                         require Email::Simple;
107                         use PublicInbox::MID qw/mid_clean/;
108                         $mid = Email::Simple->new($mid);
109                         $ctx->{mid} = mid_clean($mid->header('Message-ID'));
110                 }
111         }
112         undef;
113 }
114
115 # /$LISTNAME/new.atom                     -> Atom feed, includes replies
116 sub get_atom {
117         my ($ctx) = @_;
118         require PublicInbox::Feed;
119         PublicInbox::Feed::generate($ctx);
120 }
121
122 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
123 sub get_index {
124         my ($ctx) = @_;
125         require PublicInbox::Feed;
126         my $srch = searcher($ctx);
127         footer($ctx);
128         PublicInbox::Feed::generate_html_index($ctx);
129 }
130
131 # just returns a string ref for the blob in the current ctx
132 sub mid2blob {
133         my ($ctx) = @_;
134         require PublicInbox::MID;
135         my $path = PublicInbox::MID::mid2path($ctx->{mid});
136         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
137                         qw(cat-file blob), "HEAD:$path");
138         my $pid = open my $fh, '-|';
139         defined $pid or die "fork failed: $!\n";
140         if ($pid == 0) {
141                 open STDERR, '>', '/dev/null'; # ignore errors
142                 exec @cmd or die "exec failed: $!\n";
143         } else {
144                 my $blob = eval { local $/; <$fh> };
145                 close $fh;
146                 $? == 0 ? \$blob : undef;
147         }
148 }
149
150 # /$LISTNAME/$MESSAGE_ID/raw                    -> raw mbox
151 sub get_mid_txt {
152         my ($ctx) = @_;
153         my $x = mid2blob($ctx) or return r404($ctx);
154         require PublicInbox::Mbox;
155         PublicInbox::Mbox::emit1($x);
156 }
157
158 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
159 sub get_mid_html {
160         my ($ctx) = @_;
161         my $x = mid2blob($ctx) or return r404($ctx);
162
163         require PublicInbox::View;
164         my $foot = footer($ctx);
165         require Email::MIME;
166         my $mime = Email::MIME->new($x);
167         searcher($ctx);
168         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
169           [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
170 }
171
172 # /$LISTNAME/$MESSAGE_ID/f/                   -> HTML content (fullquotes)
173 sub get_full_html {
174         my ($ctx) = @_;
175         my $x = mid2blob($ctx) or return r404($ctx);
176
177         require PublicInbox::View;
178         my $foot = footer($ctx);
179         require Email::MIME;
180         my $mime = Email::MIME->new($x);
181         searcher($ctx);
182         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
183           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
184 }
185
186 # /$LISTNAME/$MESSAGE_ID/t/
187 sub get_thread {
188         my ($ctx, $flat) = @_;
189         my $srch = searcher($ctx) or return need_search($ctx);
190         require PublicInbox::View;
191         my $foot = footer($ctx);
192         $ctx->{flat} = $flat;
193         PublicInbox::View::thread_html($ctx, $foot, $srch);
194 }
195
196 sub self_url {
197         my ($cgi) = @_;
198         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
199 }
200
201 sub ctx_get {
202         my ($ctx, $key) = @_;
203         my $val = $ctx->{$key};
204         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
205         $val;
206 }
207
208 sub try_cat {
209         my ($path) = @_;
210         my $rv;
211         if (open(my $fh, '<', $path)) {
212                 local $/;
213                 $rv = <$fh>;
214                 close $fh;
215         }
216         $rv;
217 }
218
219 sub footer {
220         my ($ctx) = @_;
221         return '' unless $ctx;
222         my $git_dir = ctx_get($ctx, 'git_dir');
223
224         # favor user-supplied footer
225         my $footer = try_cat("$git_dir/public-inbox/footer.html");
226         if (defined $footer) {
227                 chomp $footer;
228                 $ctx->{footer} = $footer;
229                 return $footer;
230         }
231
232         # auto-generate a footer
233         my $listname = ctx_get($ctx, 'listname');
234         my $desc = try_cat("$git_dir/description");
235         $desc = '$GIT_DIR/description missing' unless defined $desc;
236         chomp $desc;
237
238         my $urls = try_cat("$git_dir/cloneurl");
239         my @urls = split(/\r?\n/, $urls || '');
240         my $nurls = scalar @urls;
241         if ($nurls == 0) {
242                 $urls = '($GIT_DIR/cloneurl missing)';
243         } elsif ($nurls == 1) {
244                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
245                         '">ssoma</a>: ' . $urls[0];
246         } else {
247                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
248                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
249         }
250
251         my $addr = $pi_config->get($listname, 'address');
252         if (ref($addr) eq 'ARRAY') {
253                 $addr = $addr->[0]; # first address is primary
254         }
255
256         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
257
258         $ctx->{footer} = join("\n",
259                 '- ' . $desc,
260                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
261                         'anybody may post in plain-text (not HTML):',
262                 $addr,
263                 $urls
264         );
265 }
266
267 # search support is optional, returns undef if Xapian is not installed
268 # or not configured for the given GIT_DIR
269 sub searcher {
270         my ($ctx) = @_;
271         eval {
272                 require PublicInbox::Search;
273                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
274         };
275 }
276
277 sub need_search {
278         my ($ctx) = @_;
279         my $msg = <<EOF;
280 <html><head><title>Search not available for this
281 public-inbox</title><body><pre>Search is not available for this public-inbox
282 <a href="../">Return to index</a></pre></body></html>
283 EOF
284         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
285 }
286
287 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
288 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
289 # note: I'm not a big fan of other compression formats since they're
290 # significantly more expensive on CPU than gzip and less-widely available,
291 # especially on older systems.  Stick to zlib since that's what git uses.
292 sub get_thread_mbox {
293         my ($ctx, $sfx) = @_;
294         my $srch = searcher($ctx) or return need_search($ctx);
295         require PublicInbox::Mbox;
296         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
297 }
298
299
300 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
301 sub get_thread_atom {
302         my ($ctx) = @_;
303         searcher($ctx) or return need_search($ctx);
304         $ctx->{self_url} = self_url($ctx->{cgi});
305         require PublicInbox::Feed;
306         PublicInbox::Feed::generate_thread_atom($ctx);
307 }
308
309 sub legacy_redirects {
310         my ($ctx, $path_info) = @_;
311
312         # single-message pages
313         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
314                 r301($ctx, $1, $2);
315         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
316                 r301($ctx, $1, $2, 'raw');
317
318         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
319                 r301($ctx, $1, $2, 'f/');
320
321         # thread display
322         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
323                 r301($ctx, $1, $2, 't/#u');
324
325         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
326                 r301($ctx, $1, $2, "t.mbox$3");
327
328         # even older legacy redirects
329         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
330                 r301($ctx, $1, $2);
331
332         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
333                 r301($ctx, $1, $2, 't/#u');
334
335         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
336                 r301($ctx, $1, $2, 'f/');
337
338         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
339                 r301($ctx, $1, $2, 'raw');
340
341         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
342                 r301($ctx, $1, $2, "t$3");
343
344         # legacy convenience redirects, order still matters
345         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
346                 r301($ctx, $1, $2);
347         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
348                 r301($ctx, $1, $2, 't/#u');
349         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
350                 r301($ctx, $1, $2, 'f/');
351
352         # some Message-IDs have slashes in them and the HTTP server
353         # may try to be clever and unescape them :<
354         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
355                 msg_page($ctx, $1, $2, $3);
356
357         } else {
358                 r404();
359         }
360 }
361
362 sub r301 {
363         my ($ctx, $listname, $mid, $suffix) = @_;
364         my $cgi = $ctx->{cgi};
365         my $url;
366         if (ref($cgi) eq 'CGI') {
367                 $url = $cgi->url(-base) . '/';
368         } else {
369                 $url = $cgi->base->as_string;
370         }
371
372         $url .= $listname . '/';
373         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
374         $url .= $suffix if (defined $suffix);
375
376         [ 301,
377           [ Location => $url, 'Content-Type' => 'text/plain' ],
378           [ "Redirecting to $url\n" ] ]
379 }
380
381 sub msg_page {
382         my ($ctx, $list, $mid, $e) = @_;
383         unless (invalid_list_mid($ctx, $list, $mid)) {
384                 '' eq $e and return get_mid_html($ctx);
385                 't/' eq $e and return get_thread($ctx);
386                 't.atom' eq $e and return get_thread_atom($ctx);
387                 't.mbox' eq $e and return get_thread_mbox($ctx);
388                 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
389                 'T/' eq $e and return get_thread($ctx, 1);
390                 'raw' eq $e and return get_mid_txt($ctx);
391                 'f/' eq $e and return get_full_html($ctx);
392         }
393         r404($ctx);
394 }
395
396 1;