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