]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
d6b07bf7ff03be20db2abb3893a54c8c41fcb71f
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # Main web interface for mailing list archives
5 #
6 # We focus on the lowest common denominators here:
7 # - targeted at text-only console browsers (w3m, links, etc..)
8 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
9 # - No JavaScript, graphics or icons allowed.
10 # - Must not rely on static content
11 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
12 package PublicInbox::WWW;
13 use 5.008;
14 use strict;
15 use warnings;
16 use Plack::Request;
17 use PublicInbox::Config;
18 use PublicInbox::Hval;
19 use URI::Escape qw(uri_escape_utf8 uri_unescape);
20 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
21 use constant PI_URL => '//public-inbox.org/';
22 require PublicInbox::Git;
23 use PublicInbox::GitHTTPBackend;
24 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
25 our $MID_RE = qr!([^/]+)!;
26 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
27 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
28
29 sub new {
30         my ($class, $pi_config) = @_;
31         $pi_config ||= PublicInbox::Config->new;
32         bless { pi_config => $pi_config }, $class;
33 }
34
35 # backwards compatibility, do not use
36 sub run {
37         my ($req, $method) = @_;
38         PublicInbox::WWW->new->call($req->env);
39 }
40
41 sub call {
42         my ($self, $env) = @_;
43         my $cgi = Plack::Request->new($env);
44         my $ctx = { cgi => $cgi, env => $env, www => $self,
45                 pi_config => $self->{pi_config} };
46
47         # we don't care about multi-value
48         my %qp = map {
49                 my ($k, $v) = split('=', $_, 2);
50                 $v = '' unless defined $v;
51                 $v =~ tr/+/ /;
52                 ($k, $v)
53         } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
54         $ctx->{qp} = \%qp;
55
56         my $path_info = $env->{PATH_INFO};
57         my $method = $env->{REQUEST_METHOD};
58
59         if ($method eq 'POST' &&
60                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
61                 my $path = $2;
62                 return (invalid_inbox($self, $ctx, $1) ||
63                         serve_git($env, $ctx->{git}, $path));
64         }
65         elsif ($method !~ /\AGET|HEAD\z/) {
66                 return r(405, 'Method Not Allowed');
67         }
68
69         # top-level indices and feeds
70         if ($path_info eq '/') {
71                 r404();
72         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
73                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
74         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
75                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
76         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
77                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
78
79         } elsif ($path_info =~ m!$INBOX_RE/
80                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
81                 my $path = $2;
82                 invalid_inbox($self, $ctx, $1) ||
83                         serve_git($env, $ctx->{git}, $path);
84         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
85                 serve_mbox_range($self, $ctx, $1, $2);
86         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
87                 msg_page($self, $ctx, $1, $2, $3);
88
89         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
90                 my ($idx, $fn) = ($3, $4);
91                 invalid_inbox_mid($self, $ctx, $1, $2) ||
92                         get_attach($ctx, $idx, $fn);
93         # in case people leave off the trailing slash:
94         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
95                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
96                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
97                 r301($ctx, $inbox, $mid, $suffix);
98
99         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
100                 my ($inbox, $mid) = ($1, $2);
101                 r301($ctx, $inbox, $mid, '#R');
102
103         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
104                 r301($ctx, $1, $2);
105
106         # convenience redirects order matters
107         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
108                 r301($ctx, $1, $2);
109
110         } else {
111                 legacy_redirects($self, $ctx, $path_info);
112         }
113 }
114
115 # for CoW-friendliness, MOOOOO!
116 sub preload {
117         require PublicInbox::Feed;
118         require PublicInbox::View;
119         require PublicInbox::Thread;
120         require Email::MIME;
121         require Digest::SHA;
122         require POSIX;
123
124         foreach (qw(PublicInbox::Search PublicInbox::SearchView
125                         PublicInbox::Mbox IO::Compress::Gzip
126                         PublicInbox::NewsWWW)) {
127                 eval "require $_;";
128         }
129 }
130
131 # private functions below
132
133 sub r404 {
134         my ($ctx) = @_;
135         if ($ctx && $ctx->{mid}) {
136                 require PublicInbox::ExtMsg;
137                 searcher($ctx);
138                 return PublicInbox::ExtMsg::ext_msg($ctx);
139         }
140         r(404, 'Not Found');
141 }
142
143 # simple response for errors
144 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
145
146 # returns undef if valid, array ref response if invalid
147 sub invalid_inbox {
148         my ($self, $ctx, $inbox) = @_;
149         my $obj = $ctx->{pi_config}->lookup_name($inbox);
150         if (defined $obj) {
151                 $ctx->{git_dir} = $obj->{mainrepo};
152                 $ctx->{git} = $obj->git;
153                 # for PublicInbox::HTTP::weaken_task:
154                 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
155                 $ctx->{-inbox} = $obj;
156                 $ctx->{inbox} = $inbox;
157                 return;
158         }
159
160         # sometimes linkifiers (not ours!) screw up automatic link
161         # generation and link things intended for nntp:// to https?://,
162         # so try to infer links and redirect them to the appropriate
163         # list URL.
164         $self->news_www->call($ctx->{cgi}->{env});
165 }
166
167 # returns undef if valid, array ref response if invalid
168 sub invalid_inbox_mid {
169         my ($self, $ctx, $inbox, $mid) = @_;
170         my $ret = invalid_inbox($self, $ctx, $inbox);
171         return $ret if $ret;
172
173         $ctx->{mid} = $mid = uri_unescape($mid);
174         if ($mid =~ /\A[a-f0-9]{40}\z/) {
175                 # this is horiffically wasteful for legacy URLs:
176                 if ($mid = mid2blob($ctx)) {
177                         require Email::Simple;
178                         use PublicInbox::MID qw/mid_clean/;
179                         my $s = Email::Simple->new($mid);
180                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
181                 }
182         }
183         undef;
184 }
185
186 # /$INBOX/new.atom                     -> Atom feed, includes replies
187 sub get_atom {
188         my ($ctx) = @_;
189         require PublicInbox::Feed;
190         PublicInbox::Feed::generate($ctx);
191 }
192
193 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
194 sub get_index {
195         my ($ctx) = @_;
196         require PublicInbox::Feed;
197         my $srch = searcher($ctx);
198         footer($ctx);
199         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
200                 require PublicInbox::SearchView;
201                 PublicInbox::SearchView::sres_top_html($ctx);
202         } else {
203                 PublicInbox::Feed::generate_html_index($ctx);
204         }
205 }
206
207 # just returns a string ref for the blob in the current ctx
208 sub mid2blob {
209         my ($ctx) = @_;
210         $ctx->{-inbox}->msg_by_mid($ctx->{mid});
211 }
212
213 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
214 sub get_mid_txt {
215         my ($ctx) = @_;
216         my $x = mid2blob($ctx) or return r404($ctx);
217         require PublicInbox::Mbox;
218         PublicInbox::Mbox::emit1($ctx, $x);
219 }
220
221 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
222 sub get_mid_html {
223         my ($ctx) = @_;
224         my $x = mid2blob($ctx) or return r404($ctx);
225
226         require PublicInbox::View;
227         my $foot = footer($ctx);
228         require Email::MIME;
229         my $mime = Email::MIME->new($x);
230         searcher($ctx);
231         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
232           PublicInbox::View::msg_html($ctx, $mime, $foot) ];
233 }
234
235 # /$INBOX/$MESSAGE_ID/t/
236 sub get_thread {
237         my ($ctx, $flat) = @_;
238         my $srch = searcher($ctx) or return need_search($ctx);
239         require PublicInbox::View;
240         my $foot = footer($ctx);
241         $ctx->{flat} = $flat;
242         PublicInbox::View::thread_html($ctx, $foot, $srch);
243 }
244
245 sub ctx_get {
246         my ($ctx, $key) = @_;
247         my $val = $ctx->{$key};
248         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
249         $val;
250 }
251
252 sub footer {
253         my ($ctx) = @_;
254         return '' unless $ctx;
255         my $obj = $ctx->{-inbox} or return '';
256
257         # auto-generate a footer
258         chomp(my $desc = $obj->description);
259         $desc = PublicInbox::Hval::ascii_html($desc);
260
261         my $urls;
262         my @urls = @{$obj->cloneurl};
263         my %seen = map { $_ => 1 } @urls;
264         my $cgi = $ctx->{cgi};
265         my $http = $cgi->base->as_string . $obj->{name};
266         $seen{$http} or unshift @urls, $http;
267         my $ssoma_url = PublicInbox::Hval::prurl($ctx->{env}, SSOMA_URL);
268         if (scalar(@urls) == 1) {
269                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
270                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
271         } else {
272                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
273                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
274                         join("\n", map { "\tgit clone --mirror $_" } @urls);
275         }
276
277         my $addr = $obj->{-primary_address};
278         $ctx->{footer} = join("\n",
279                 '- ' . $desc,
280                 "A <a\nhref=\"" .
281                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
282                         '">public-inbox</a>, ' .
283                         'anybody may post in plain-text (not HTML):',
284                 $addr,
285                 $urls
286         );
287 }
288
289 # search support is optional, returns undef if Xapian is not installed
290 # or not configured for the given GIT_DIR
291 sub searcher {
292         my ($ctx) = @_;
293         eval {
294                 require PublicInbox::Search;
295                 $ctx->{srch} = $ctx->{-inbox}->search;
296         };
297 }
298
299 sub need_search {
300         my ($ctx) = @_;
301         my $msg = <<EOF;
302 <html><head><title>Search not available for this
303 public-inbox</title><body><pre>Search is not available for this public-inbox
304 <a href="../">Return to index</a></pre></body></html>
305 EOF
306         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
307 }
308
309 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
310 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
311 # note: I'm not a big fan of other compression formats since they're
312 # significantly more expensive on CPU than gzip and less-widely available,
313 # especially on older systems.  Stick to zlib since that's what git uses.
314 sub get_thread_mbox {
315         my ($ctx, $sfx) = @_;
316         my $srch = searcher($ctx) or return need_search($ctx);
317         require PublicInbox::Mbox;
318         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
319 }
320
321
322 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
323 sub get_thread_atom {
324         my ($ctx) = @_;
325         searcher($ctx) or return need_search($ctx);
326         $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
327         require PublicInbox::Feed;
328         PublicInbox::Feed::generate_thread_atom($ctx);
329 }
330
331 sub legacy_redirects {
332         my ($self, $ctx, $path_info) = @_;
333
334         # single-message pages
335         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
336                 r301($ctx, $1, $2);
337         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
338                 r301($ctx, $1, $2, 'raw');
339
340         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
341                 r301($ctx, $1, $2);
342
343         # thread display
344         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
345                 r301($ctx, $1, $2, 't/#u');
346
347         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
348                 r301($ctx, $1, $2, "t.mbox$3");
349
350         # even older legacy redirects
351         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
352                 r301($ctx, $1, $2);
353
354         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
355                 r301($ctx, $1, $2, 't/#u');
356
357         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
358                 r301($ctx, $1, $2);
359
360         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
361                 r301($ctx, $1, $2, 'raw');
362
363         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
364                 r301($ctx, $1, $2, "t$3");
365
366         # legacy convenience redirects, order still matters
367         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
368                 r301($ctx, $1, $2);
369         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
370                 r301($ctx, $1, $2, 't/#u');
371         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
372                 r301($ctx, $1, $2);
373
374         # some Message-IDs have slashes in them and the HTTP server
375         # may try to be clever and unescape them :<
376         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
377                 msg_page($self, $ctx, $1, $2, $3);
378
379         # in case people leave off the trailing slash:
380         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
381                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
382         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
383                 r301($ctx, $1, $2);
384         } else {
385                 $self->news_www->call($ctx->{cgi}->{env});
386         }
387 }
388
389 sub r301 {
390         my ($ctx, $inbox, $mid, $suffix) = @_;
391         my $cgi = $ctx->{cgi};
392         my $obj = $ctx->{-inbox};
393         unless ($obj) {
394                 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
395                 return $r404 if $r404;
396                 $obj = $ctx->{-inbox};
397         }
398         my $url = $obj->base_url($cgi);
399         my $qs = $ctx->{env}->{QUERY_STRING};
400         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
401         $url .= $suffix if (defined $suffix);
402         $url .= "?$qs" if $qs ne '';
403
404         [ 301,
405           [ Location => $url, 'Content-Type' => 'text/plain' ],
406           [ "Redirecting to $url\n" ] ]
407 }
408
409 sub msg_page {
410         my ($self, $ctx, $inbox, $mid, $e) = @_;
411         my $ret;
412         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
413         '' eq $e and return get_mid_html($ctx);
414         't/' eq $e and return get_thread($ctx);
415         't.atom' eq $e and return get_thread_atom($ctx);
416         't.mbox' eq $e and return get_thread_mbox($ctx);
417         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
418         'T/' eq $e and return get_thread($ctx, 1);
419         'raw' eq $e and return get_mid_txt($ctx);
420
421         # legacy, but no redirect for compatibility:
422         'f/' eq $e and return get_mid_html($ctx);
423         r404($ctx);
424 }
425
426 sub serve_git {
427         my ($env, $git, $path) = @_;
428         PublicInbox::GitHTTPBackend::serve($env, $git, $path);
429 }
430
431 sub serve_mbox_range {
432         my ($self, $ctx, $inbox, $range) = @_;
433         invalid_inbox($self, $ctx, $inbox) || eval {
434                 require PublicInbox::Mbox;
435                 searcher($ctx);
436                 PublicInbox::Mbox::emit_range($ctx, $range);
437         }
438 }
439
440 sub news_www {
441         my ($self) = @_;
442         my $nw = $self->{news_www};
443         return $nw if $nw;
444         require PublicInbox::NewsWWW;
445         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
446 }
447
448 sub get_attach {
449         my ($ctx, $idx, $fn) = @_;
450         require PublicInbox::WwwAttach;
451         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
452 }
453
454 1;