1 # Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Main web interface for mailing list archives
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 # and diff/syntax-highlighting (optional)
10 # - No JavaScript, graphics or icons allowed.
11 # - Must not rely on static content
12 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
13 package PublicInbox::WWW;
17 use bytes (); # only for bytes::length
18 use PublicInbox::Config;
19 use PublicInbox::Hval;
20 use URI::Escape qw(uri_unescape);
21 use PublicInbox::MID qw(mid_escape);
22 require PublicInbox::Git;
23 use PublicInbox::GitHTTPBackend;
24 use PublicInbox::UserContent;
26 # TODO: consider a routing tree now that we have more endpoints:
27 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
28 our $MID_RE = qr!([^/]+)!;
29 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
30 our $ATTACH_RE = qr!([0-9][0-9\.]*)-($PublicInbox::Hval::FN)!;
31 our $OID_RE = qr![a-f0-9]{7,40}!;
34 my ($class, $pi_config) = @_;
35 $pi_config ||= PublicInbox::Config->new;
36 bless { pi_config => $pi_config }, $class;
39 # backwards compatibility, do not use
41 my ($req, $method) = @_;
42 PublicInbox::WWW->new->call($req->env);
48 my $sn = $_[0]->{SCRIPT_NAME};
49 $path_re_cache{$sn} ||= do {
50 $sn = '/'.$sn unless index($sn, '/') == 0;
52 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
57 my ($self, $env) = @_;
58 my $ctx = { env => $env, www => $self };
60 # we don't care about multi-value
64 my ($k, $v) = split('=', $_, 2);
65 $v = uri_unescape($v // '');
66 # none of the keys we care about will need escaping
68 } split(/[&;]+/, $env->{QUERY_STRING});
70 # avoiding $env->{PATH_INFO} here since that's already decoded
71 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
72 $path_info //= $env->{PATH_INFO};
73 my $method = $env->{REQUEST_METHOD};
75 if ($method eq 'POST') {
76 if ($path_info =~ m!$INBOX_RE/(?:(?:git/)?([0-9]+)(?:\.git)?/)?
77 (git-upload-pack)\z!x) {
78 my ($epoch, $path) = ($2, $3);
79 return invalid_inbox($ctx, $1) ||
80 serve_git($ctx, $epoch, $path);
81 } elsif ($path_info =~ m!$INBOX_RE/!o) {
82 return invalid_inbox($ctx, $1) || mbox_results($ctx);
85 elsif ($method !~ /\AGET|HEAD\z/) {
86 return r(405, 'Method Not Allowed');
89 # top-level indices and feeds
90 if ($path_info eq '/' || $path_info eq '/manifest.js.gz') {
91 www_listing($self)->call($env);
92 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
93 invalid_inbox($ctx, $1) || r301($ctx, $1);
94 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
95 invalid_inbox($ctx, $1) || get_index($ctx);
96 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
97 invalid_inbox($ctx, $1) || get_atom($ctx);
98 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
99 invalid_inbox($ctx, $1) || get_new($ctx);
100 } elsif ($path_info =~ m!$INBOX_RE/(?:(?:git/)?([0-9]+)(?:\.git)?/)?
101 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
102 my ($epoch, $path) = ($2, $3);
103 invalid_inbox($ctx, $1) || serve_git($ctx, $epoch, $path);
104 } elsif ($path_info =~ m!$INBOX_RE/([a-zA-Z0-9_\-]+).mbox\.gz\z!o) {
105 serve_mbox_range($ctx, $1, $2);
106 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
107 msg_page($ctx, $1, $2, $3);
109 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
110 my ($idx, $fn) = ($3, $4);
111 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
112 # in case people leave off the trailing slash:
113 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
114 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
115 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
116 r301($ctx, $inbox, $mid_ue, $suffix);
118 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
119 my ($inbox, $mid_ue) = ($1, $2);
120 r301($ctx, $inbox, $mid_ue, '#R');
122 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
124 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
125 get_text($ctx, $1, $2);
126 } elsif ($path_info =~ m!$INBOX_RE/([a-zA-Z0-9_\-\.]+)\.css\z!o) {
127 get_css($ctx, $1, $2);
128 } elsif ($path_info =~ m!$INBOX_RE/manifest\.js\.gz\z!o) {
129 get_inbox_manifest($ctx, $1, $2);
130 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
131 get_vcs_object($ctx, $1, $2);
132 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/
133 ($PublicInbox::Hval::FN)\z!ox) {
134 get_vcs_object($ctx, $1, $2, $3);
135 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
136 r301($ctx, $1, $2, 's/');
137 # convenience redirects order matters
138 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
142 legacy_redirects($ctx, $path_info);
146 # for CoW-friendliness, MOOOOO!
149 require PublicInbox::Feed;
150 require PublicInbox::View;
151 require PublicInbox::SearchThread;
152 require PublicInbox::MIME;
156 require PublicInbox::Search;
157 PublicInbox::Search::load_xapian();
159 foreach (qw(PublicInbox::SearchView
160 PublicInbox::Mbox IO::Compress::Gzip
161 PublicInbox::NewsWWW)) {
166 $self->stylesheets_prepare($_) for ('', '../', '../../');
171 # private functions below
175 if ($ctx && $ctx->{mid}) {
176 require PublicInbox::ExtMsg;
177 return PublicInbox::ExtMsg::ext_msg($ctx);
182 # simple response for errors
183 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
185 sub news_cgit_fallback ($) {
187 my $www = $ctx->{www};
188 my $env = $ctx->{env};
189 my $res = $www->news_www->call($env);
190 $res->[0] == 404 ? $www->cgit->call($env) : $res;
193 # returns undef if valid, array ref response if invalid
194 sub invalid_inbox ($$) {
195 my ($ctx, $inbox) = @_;
196 my $ibx = $ctx->{www}->{pi_config}->lookup_name($inbox);
198 $ctx->{-inbox} = $ibx;
202 # sometimes linkifiers (not ours!) screw up automatic link
203 # generation and link things intended for nntp:// to https?://,
204 # so try to infer links and redirect them to the appropriate
206 news_cgit_fallback($ctx);
209 # returns undef if valid, array ref response if invalid
210 sub invalid_inbox_mid {
211 my ($ctx, $inbox, $mid_ue) = @_;
212 my $ret = invalid_inbox($ctx, $inbox);
215 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
216 my $ibx = $ctx->{-inbox};
217 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
218 my ($x2, $x38) = ($1, $2);
219 # this is horrifically wasteful for legacy URLs:
220 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
221 require Email::Simple;
222 my $s = Email::Simple->new($str);
223 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
224 return r301($ctx, $inbox, mid_escape($mid));
229 # /$INBOX/new.atom -> Atom feed, includes replies
232 require PublicInbox::Feed;
233 PublicInbox::Feed::generate($ctx);
236 # /$INBOX/new.html -> HTML only
239 require PublicInbox::Feed;
240 PublicInbox::Feed::new_html($ctx);
243 # /$INBOX/?r=$GIT_COMMIT -> HTML only
246 require PublicInbox::Feed;
247 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
248 require PublicInbox::SearchView;
249 PublicInbox::SearchView::sres_top_html($ctx);
251 PublicInbox::Feed::generate_html_index($ctx);
255 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
258 require PublicInbox::Mbox;
259 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
262 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
265 require PublicInbox::View;
266 PublicInbox::View::msg_page($ctx) || r404($ctx);
269 # /$INBOX/$MESSAGE_ID/t/
271 my ($ctx, $flat) = @_;
272 $ctx->{-inbox}->over or return need($ctx, 'Overview');
273 $ctx->{flat} = $flat;
274 require PublicInbox::View;
275 PublicInbox::View::thread_html($ctx);
278 # /$INBOX/_/text/$KEY/
279 # /$INBOX/_/text/$KEY/raw
280 # KEY may contain slashes
282 my ($ctx, $inbox, $key) = @_;
283 my $r404 = invalid_inbox($ctx, $inbox);
284 return $r404 if $r404;
286 require PublicInbox::WwwText;
287 PublicInbox::WwwText::get_text($ctx, $key);
290 # show git objects (blobs and commits)
291 # /$INBOX/_/$OBJECT_ID/show
292 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
293 # KEY may contain slashes
294 sub get_vcs_object ($$$;$) {
295 my ($ctx, $inbox, $oid, $filename) = @_;
296 my $r404 = invalid_inbox($ctx, $inbox);
297 return $r404 if $r404;
298 require PublicInbox::ViewVCS;
299 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
303 my ($ctx, $extra) = @_;
305 <html><head><title>$extra not available for this
306 public-inbox</title><body><pre>$extra is not available for this public-inbox
307 <a href="../">Return to index</a></pre></body></html>
309 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
312 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
313 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
314 # note: I'm not a big fan of other compression formats since they're
315 # significantly more expensive on CPU than gzip and less-widely available,
316 # especially on older systems. Stick to zlib since that's what git uses.
317 sub get_thread_mbox {
318 my ($ctx, $sfx) = @_;
319 my $over = $ctx->{-inbox}->over or return need($ctx, 'Overview');
320 require PublicInbox::Mbox;
321 PublicInbox::Mbox::thread_mbox($ctx, $over, $sfx);
325 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
326 sub get_thread_atom {
328 $ctx->{-inbox}->over or return need($ctx, 'Overview');
329 require PublicInbox::Feed;
330 PublicInbox::Feed::generate_thread_atom($ctx);
333 sub legacy_redirects {
334 my ($ctx, $path_info) = @_;
336 # single-message pages
337 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
339 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
340 r301($ctx, $1, $2, 'raw');
342 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
346 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
347 r301($ctx, $1, $2, 't/#u');
349 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
350 r301($ctx, $1, $2, "t.mbox$3");
352 # even older legacy redirects
353 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
356 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
357 r301($ctx, $1, $2, 't/#u');
359 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
362 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
363 r301($ctx, $1, $2, 'raw');
365 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
366 r301($ctx, $1, $2, "t$3");
368 # legacy convenience redirects, order still matters
369 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
371 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
372 r301($ctx, $1, $2, 't/#u');
373 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
376 # some Message-IDs have slashes in them and the HTTP server
377 # may try to be clever and unescape them :<
378 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
379 msg_page($ctx, $1, $2, $3);
381 # in case people leave off the trailing slash:
382 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
383 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
384 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
387 news_cgit_fallback($ctx);
392 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
393 my $ibx = $ctx->{-inbox};
395 my $r404 = invalid_inbox($ctx, $inbox);
396 return $r404 if $r404;
397 $ibx = $ctx->{-inbox};
399 my $url = $ibx->base_url($ctx->{env});
400 my $qs = $ctx->{env}->{QUERY_STRING};
401 if (defined $mid_ue) {
402 # common, and much nicer as '@' than '%40':
403 $mid_ue =~ s/%40/@/g;
404 $url .= $mid_ue . '/';
406 $url .= $suffix if (defined $suffix);
407 $url .= "?$qs" if $qs ne '';
410 [ Location => $url, 'Content-Type' => 'text/plain' ],
411 [ "Redirecting to $url\n" ] ]
415 my ($ctx, $inbox, $mid_ue, $e) = @_;
417 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
418 '' eq $e and return get_mid_html($ctx);
419 'T/' eq $e and return get_thread($ctx, 1);
420 't/' eq $e and return get_thread($ctx);
421 't.atom' eq $e and return get_thread_atom($ctx);
422 't.mbox' eq $e and return get_thread_mbox($ctx);
423 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
424 'raw' eq $e and return get_mid_txt($ctx);
426 # legacy, but no redirect for compatibility:
427 'f/' eq $e and return get_mid_html($ctx);
432 my ($ctx, $epoch, $path) = @_;
433 my $env = $ctx->{env};
434 my $ibx = $ctx->{-inbox};
435 my $git = defined $epoch ? $ibx->git_epoch($epoch) : $ibx->git;
436 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
441 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
442 $ctx->{-inbox}->search or return need($ctx, 'search');
443 require PublicInbox::SearchView;
444 return PublicInbox::SearchView::mbox_results($ctx);
449 sub serve_mbox_range {
450 my ($ctx, $inbox, $range) = @_;
451 invalid_inbox($ctx, $inbox) || eval {
452 require PublicInbox::Mbox;
453 PublicInbox::Mbox::emit_range($ctx, $range);
459 $self->{news_www} ||= do {
460 require PublicInbox::NewsWWW;
461 PublicInbox::NewsWWW->new($self->{pi_config});
467 $self->{cgit} ||= do {
468 my $pi_config = $self->{pi_config};
470 if (defined($pi_config->{'publicinbox.cgitrc'})) {
471 require PublicInbox::Cgit;
472 PublicInbox::Cgit->new($pi_config);
475 Plack::Util::inline_object(call => sub { r404() });
482 $self->{www_listing} ||= do {
483 require PublicInbox::WwwListing;
484 PublicInbox::WwwListing->new($self);
488 # GET $INBOX/manifest.js.gz
489 sub get_inbox_manifest ($$$) {
490 my ($ctx, $inbox, $key) = @_;
491 my $r404 = invalid_inbox($ctx, $inbox);
492 return $r404 if $r404;
493 require PublicInbox::WwwListing;
494 PublicInbox::WwwListing::js($ctx->{env}, [$ctx->{-inbox}]);
498 my ($ctx, $idx, $fn) = @_;
499 require PublicInbox::WwwAttach;
500 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
503 # User-generated content (UGC) may have excessively long lines
504 # and screw up rendering on some browsers, so we use pre-wrap.
506 # We also force everything to the same scaled font-size because GUI
507 # browsers (tested both Firefox and surf (webkit)) uses a larger font
508 # for the Search <form> element than the rest of the page. Font size
509 # uniformity is important to people who rely on gigantic fonts.
510 # Finally, we use monospace to ensure the Search field and button
511 # has the same size and spacing as everything else which is
512 # <pre>-formatted anyways.
513 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
515 sub stylesheets_prepare ($$) {
516 my ($self, $upfx) = @_;
518 require CSS::Minifier;
519 sub { CSS::Minifier::minify(input => $_[0]) };
521 require CSS::Minifier::XS;
522 sub { CSS::Minifier::XS::minify($_[0]) };
526 my $stylesheets = $self->{pi_config}->{css} || [];
530 foreach my $s (@$stylesheets) {
533 foreach my $k (qw(media title href)) {
534 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
539 if (defined $attr->{href}) {
543 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
544 if ($key !~ /\A[a-zA-Z0-9_\-\.]+\z/) {
545 warn "ignoring $fn, non-ASCII word character\n";
548 open(my $fh, '<', $fn) or do {
549 warn "failed to open $fn: $!\n";
553 my $local = do { local $/; <$fh> };
554 if ($local =~ /\S/) {
555 $ctime = sprintf('%x',(stat($fh))[10]);
556 $local = $mini->($local);
559 # do not let BOFHs override userContent.css:
560 if ($local =~ /!\s*important\b/i) {
561 warn "ignoring $fn since it uses `!important'\n";
565 $css_map->{$key} = $local;
566 $attr->{href} = "$upfx$key.css?$ctime";
567 if (defined($attr->{title})) {
569 } elsif (($attr->{media}||'screen') eq 'screen') {
570 $attr->{-inline} = $local;
576 my $buf = "<style>$STYLE";
578 my @ext; # for media=print and whatnot
579 foreach my $attr (@$links) {
580 if (defined(my $str = delete $attr->{-inline})) {
591 foreach my $attr (@$links) {
592 delete $attr->{-inline};
593 $buf .= "<link\ntype=text/css\nrel=stylesheet";
594 while (my ($k, $v) = each %$attr) {
595 $v = qq{"$v"} if $v =~ /[\s=]/;
600 $self->{"-style-$upfx"} = $buf;
602 $self->{-style_inline} = $buf;
604 $self->{-css_map} = $css_map;
607 # returns an HTML fragment with <style> or <link> tags in them
608 # Called by WwwStream by nearly every HTML page
610 my ($self, $upfx) = @_;
611 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
612 stylesheets_prepare($self, $upfx);
613 $self->{-style_inline} || $self->{"-style-$upfx"}
617 # /$INBOX/$KEY.css endpoint
618 # CSS is configured globally for all inboxes, but we access them on
619 # a per-inbox basis. This allows administrators to setup per-inbox
620 # static routes to intercept the request before it hits PSGI
622 my ($ctx, $inbox, $key) = @_;
623 my $r404 = invalid_inbox($ctx, $inbox);
624 return $r404 if $r404;
625 my $self = $ctx->{www};
626 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
627 my $css = $css_map->{$key};
628 if (!defined($css) && $key eq 'userContent') {
629 my $env = $ctx->{env};
630 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
632 defined $css or return r404();
633 my $h = [ 'Content-Length', bytes::length($css),
634 'Content-Type', 'text/css' ];
635 PublicInbox::GitHTTPBackend::cache_one_year($h);
636 [ 200, $h, [ $css ] ];