1 # Copyright (C) 2014-2020 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 use PublicInbox::GitHTTPBackend;
23 use PublicInbox::UserContent;
24 use PublicInbox::WwwStatic qw(r path_info_raw);
27 # TODO: consider a routing tree now that we have more endpoints:
28 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
29 our $MID_RE = qr!([^/]+)!;
30 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
31 our $ATTACH_RE = qr!([0-9][0-9\.]*)-($PublicInbox::Hval::FN)!;
32 our $OID_RE = qr![a-f0-9]{7,}!;
35 my ($class, $pi_cfg) = @_;
36 bless { pi_cfg => $pi_cfg // PublicInbox::Config->new }, $class;
39 # backwards compatibility, do not use
41 my ($req, $method) = @_;
42 PublicInbox::WWW->new->call($req->env);
46 my ($self, $env) = @_;
47 my $ctx = { env => $env, www => $self };
49 # we don't care about multi-value
53 my ($k, $v) = split('=', $_, 2);
54 $v = uri_unescape($v // '');
55 # none of the keys we care about will need escaping
57 } split(/[&;]+/, $env->{QUERY_STRING});
59 my $path_info = path_info_raw($env);
60 my $method = $env->{REQUEST_METHOD};
62 if ($method eq 'POST') {
63 if ($path_info =~ m!$INBOX_RE/(?:(?:git/)?([0-9]+)(?:\.git)?/)?
64 (git-upload-pack)\z!x) {
65 my ($epoch, $path) = ($2, $3);
66 return invalid_inbox($ctx, $1) ||
67 serve_git($ctx, $epoch, $path);
68 } elsif ($path_info =~ m!$INBOX_RE/(\w+)\.sql\.gz\z!o) {
69 return get_altid_dump($ctx, $1, $2);
70 } elsif ($path_info =~ m!$INBOX_RE/!o) {
71 return invalid_inbox($ctx, $1) || mbox_results($ctx);
74 elsif ($method !~ /\A(?:GET|HEAD)\z/) {
78 # top-level indices and feeds
79 if ($path_info eq '/') {
80 require PublicInbox::WwwListing;
81 PublicInbox::WwwListing->response($ctx);
82 } elsif ($path_info eq '/manifest.js.gz') {
83 require PublicInbox::ManifestJsGz;
84 PublicInbox::ManifestJsGz->response($ctx);
85 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
86 invalid_inbox($ctx, $1) || r301($ctx, $1);
87 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
88 invalid_inbox($ctx, $1) || get_index($ctx);
89 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
90 invalid_inbox($ctx, $1) || get_atom($ctx);
91 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
92 invalid_inbox($ctx, $1) || get_new($ctx);
93 } elsif ($path_info =~ m!$INBOX_RE/description\z!o) {
94 get_description($ctx, $1);
95 } elsif ($path_info =~ m!$INBOX_RE/(?:(?:git/)?([0-9]+)(?:\.git)?/)?
96 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
97 my ($epoch, $path) = ($2, $3);
98 invalid_inbox($ctx, $1) || serve_git($ctx, $epoch, $path);
99 } elsif ($path_info =~ m!$INBOX_RE/([a-zA-Z0-9_\-]+).mbox\.gz\z!o) {
100 serve_mbox_range($ctx, $1, $2);
101 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
102 msg_page($ctx, $1, $2, $3);
104 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
105 my ($idx, $fn) = ($3, $4);
106 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
107 # in case people leave off the trailing slash:
108 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
109 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
110 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
111 r301($ctx, $inbox, $mid_ue, $suffix);
113 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
114 my ($inbox, $mid_ue) = ($1, $2);
115 r301($ctx, $inbox, $mid_ue, '#R');
117 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
119 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
120 get_text($ctx, $1, $2);
121 } elsif ($path_info =~ m!$INBOX_RE/([a-zA-Z0-9_\-\.]+)\.css\z!o) {
122 get_css($ctx, $1, $2);
123 } elsif ($path_info =~ m!$INBOX_RE/manifest\.js\.gz\z!o) {
124 get_inbox_manifest($ctx, $1, $2);
125 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
126 get_vcs_object($ctx, $1, $2);
127 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/
128 ($PublicInbox::Hval::FN)\z!ox) {
129 get_vcs_object($ctx, $1, $2, $3);
130 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
131 r301($ctx, $1, $2, 's/');
132 } elsif ($path_info =~ m!$INBOX_RE/(\w+)\.sql\.gz\z!o) {
133 get_altid_dump($ctx, $1, $2);
134 # convenience redirects order matters
135 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
139 legacy_redirects($ctx, $path_info);
143 # for CoW-friendliness, MOOOOO! Even for single-process setups,
144 # we want to get all immortal allocations done early to avoid heap
145 # fragmentation since common allocators favor a large contiguous heap.
149 # populate caches used by Encode internally, since emails
150 # may show up with any encoding.
152 Encode::find_encoding($_) for Encode->encodings(':all');
154 require PublicInbox::ExtMsg;
155 require PublicInbox::Feed;
156 require PublicInbox::View;
157 require PublicInbox::SearchThread;
158 require PublicInbox::Eml;
159 require PublicInbox::Mbox;
160 require PublicInbox::ViewVCS;
161 require PublicInbox::WwwText;
162 require PublicInbox::WwwAttach;
164 require PublicInbox::Search;
165 PublicInbox::Search::load_xapian();
167 for (qw(SearchView MboxGz WwwAltId)) {
168 eval "require PublicInbox::$_;";
171 my $pi_cfg = $self->{pi_cfg};
172 if (defined($pi_cfg->{'publicinbox.cgitrc'})) {
173 $pi_cfg->limiter('-cgit');
176 $self->stylesheets_prepare($_) for ('', '../', '../../');
178 $pi_cfg->each_inbox(\&preload_inbox);
190 # private functions below
194 if ($ctx && $ctx->{mid}) {
195 require PublicInbox::ExtMsg;
196 return PublicInbox::ExtMsg::ext_msg($ctx);
201 sub news_cgit_fallback ($) {
203 my $www = $ctx->{www};
204 my $env = $ctx->{env};
205 my $res = $www->news_www->call($env);
206 $res->[0] == 404 ? $www->cgit->call($env) : $res;
209 # returns undef if valid, array ref response if invalid
210 sub invalid_inbox ($$) {
211 my ($ctx, $inbox) = @_;
212 my $ibx = $ctx->{www}->{pi_cfg}->lookup_name($inbox) //
213 $ctx->{www}->{pi_cfg}->lookup_ei($inbox);
219 # sometimes linkifiers (not ours!) screw up automatic link
220 # generation and link things intended for nntp:// to https?://,
221 # so try to infer links and redirect them to the appropriate
223 news_cgit_fallback($ctx);
226 # returns undef if valid, array ref response if invalid
227 sub invalid_inbox_mid {
228 my ($ctx, $inbox, $mid_ue) = @_;
229 my $ret = invalid_inbox($ctx, $inbox);
232 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
233 my $ibx = $ctx->{ibx};
234 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
235 my ($x2, $x38) = ($1, $2);
236 # this is horrifically wasteful for legacy URLs:
237 my $str = $ctx->{ibx}->msg_by_path("$x2/$x38") or return;
238 my $s = PublicInbox::Eml->new($str);
239 $mid = PublicInbox::MID::mid_clean($s->header_raw('Message-ID'));
240 return r301($ctx, $inbox, mid_escape($mid));
245 # /$INBOX/new.atom -> Atom feed, includes replies
248 require PublicInbox::Feed;
249 PublicInbox::Feed::generate($ctx);
252 # /$INBOX/new.html -> HTML only
255 require PublicInbox::Feed;
256 PublicInbox::Feed::new_html($ctx);
259 # /$INBOX/?r=$GIT_COMMIT -> HTML only
262 require PublicInbox::Feed;
263 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
264 require PublicInbox::SearchView;
265 PublicInbox::SearchView::sres_top_html($ctx);
267 PublicInbox::Feed::generate_html_index($ctx);
271 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
274 require PublicInbox::Mbox;
275 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
278 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
281 require PublicInbox::View;
282 PublicInbox::View::msg_page($ctx) || r404($ctx);
285 # /$INBOX/$MESSAGE_ID/t/
287 my ($ctx, $flat) = @_;
288 $ctx->{ibx}->over or return need($ctx, 'Overview');
289 $ctx->{flat} = $flat;
290 require PublicInbox::View;
291 PublicInbox::View::thread_html($ctx);
294 # /$INBOX/_/text/$KEY/
295 # /$INBOX/_/text/$KEY/raw
296 # KEY may contain slashes
298 my ($ctx, $inbox, $key) = @_;
299 my $r404 = invalid_inbox($ctx, $inbox);
300 return $r404 if $r404;
302 require PublicInbox::WwwText;
303 PublicInbox::WwwText::get_text($ctx, $key);
306 # show git objects (blobs and commits)
307 # /$INBOX/$GIT_OBJECT_ID/s/
308 # /$INBOX/$GIT_OBJECT_ID/s/$FILENAME
309 sub get_vcs_object ($$$;$) {
310 my ($ctx, $inbox, $oid, $filename) = @_;
311 my $r404 = invalid_inbox($ctx, $inbox);
312 return $r404 if $r404;
313 require PublicInbox::ViewVCS;
314 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
318 my ($ctx, $inbox, $altid_pfx) =@_;
319 my $r404 = invalid_inbox($ctx, $inbox);
320 return $r404 if $r404;
321 eval { require PublicInbox::WwwAltId } or return need($ctx, 'sqlite3');
322 PublicInbox::WwwAltId::sqldump($ctx, $altid_pfx);
326 my ($ctx, $extra) = @_;
327 require PublicInbox::WwwStream;
328 PublicInbox::WwwStream::html_oneshot($ctx, 501, \<<EOF);
329 <pre>$extra is not available for this public-inbox
330 <a\nhref="../">Return to index</a></pre>
334 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
335 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
336 # note: I'm not a big fan of other compression formats since they're
337 # significantly more expensive on CPU than gzip and less-widely available,
338 # especially on older systems. Stick to zlib since that's what git uses.
339 sub get_thread_mbox {
340 my ($ctx, $sfx) = @_;
341 my $over = $ctx->{ibx}->over or return need($ctx, 'Overview');
342 require PublicInbox::Mbox;
343 PublicInbox::Mbox::thread_mbox($ctx, $over, $sfx);
347 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
348 sub get_thread_atom {
350 $ctx->{ibx}->over or return need($ctx, 'Overview');
351 require PublicInbox::Feed;
352 PublicInbox::Feed::generate_thread_atom($ctx);
355 sub legacy_redirects {
356 my ($ctx, $path_info) = @_;
358 # single-message pages
359 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
361 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
362 r301($ctx, $1, $2, 'raw');
364 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
368 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
369 r301($ctx, $1, $2, 't/#u');
371 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
372 r301($ctx, $1, $2, "t.mbox$3");
374 # even older legacy redirects
375 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
378 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
379 r301($ctx, $1, $2, 't/#u');
381 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
384 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
385 r301($ctx, $1, $2, 'raw');
387 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
388 r301($ctx, $1, $2, "t$3");
390 # legacy convenience redirects, order still matters
391 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
393 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
394 r301($ctx, $1, $2, 't/#u');
395 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
398 # some Message-IDs have slashes in them and the HTTP server
399 # may try to be clever and unescape them :<
400 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
401 msg_page($ctx, $1, $2, $3);
403 # in case people leave off the trailing slash:
404 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
405 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
406 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
409 news_cgit_fallback($ctx);
414 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
415 my $ibx = $ctx->{ibx};
417 my $r404 = invalid_inbox($ctx, $inbox);
418 return $r404 if $r404;
421 my $url = $ibx->base_url($ctx->{env});
422 my $qs = $ctx->{env}->{QUERY_STRING};
423 if (defined $mid_ue) {
424 # common, and much nicer as '@' than '%40':
425 $mid_ue =~ s/%40/@/g;
426 $url .= $mid_ue . '/';
428 $url .= $suffix if (defined $suffix);
429 $url .= "?$qs" if $qs ne '';
432 [ Location => $url, 'Content-Type' => 'text/plain' ],
433 [ "Redirecting to $url\n" ] ]
437 my ($ctx, $inbox, $mid_ue, $e) = @_;
439 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
440 '' eq $e and return get_mid_html($ctx);
441 'T/' eq $e and return get_thread($ctx, 1);
442 't/' eq $e and return get_thread($ctx);
443 't.atom' eq $e and return get_thread_atom($ctx);
444 't.mbox' eq $e and return get_thread_mbox($ctx);
445 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
446 'raw' eq $e and return get_mid_txt($ctx);
448 # legacy, but no redirect for compatibility:
449 'f/' eq $e and return get_mid_html($ctx);
454 my ($ctx, $epoch, $path) = @_;
455 my $env = $ctx->{env};
456 my $ibx = $ctx->{ibx};
457 my $git = defined $epoch ? $ibx->git_epoch($epoch) : $ibx->git;
458 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
463 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
464 $ctx->{ibx}->isrch or return need($ctx, 'search');
465 require PublicInbox::SearchView;
466 return PublicInbox::SearchView::mbox_results($ctx);
471 sub serve_mbox_range {
472 my ($ctx, $inbox, $range) = @_;
473 invalid_inbox($ctx, $inbox) || eval {
474 require PublicInbox::Mbox;
475 PublicInbox::Mbox::emit_range($ctx, $range);
481 $self->{news_www} ||= do {
482 require PublicInbox::NewsWWW;
483 PublicInbox::NewsWWW->new($self->{pi_cfg});
489 $self->{cgit} ||= do {
490 my $pi_cfg = $self->{pi_cfg};
492 if (defined($pi_cfg->{'publicinbox.cgitrc'})) {
493 require PublicInbox::Cgit;
494 PublicInbox::Cgit->new($pi_cfg);
497 Plack::Util::inline_object(call => sub { r404() });
502 # GET $INBOX/manifest.js.gz
503 sub get_inbox_manifest ($$$) {
504 my ($ctx, $inbox, $key) = @_;
505 my $r404 = invalid_inbox($ctx, $inbox);
506 return $r404 if $r404;
507 require PublicInbox::ManifestJsGz;
508 PublicInbox::ManifestJsGz::per_inbox($ctx);
512 my ($ctx, $idx, $fn) = @_;
513 require PublicInbox::WwwAttach;
514 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
517 # User-generated content (UGC) may have excessively long lines
518 # and screw up rendering on some browsers, so we use pre-wrap.
520 # We also force everything to the same scaled font-size because GUI
521 # browsers (tested both Firefox and surf (webkit)) uses a larger font
522 # for the Search <form> element than the rest of the page. Font size
523 # uniformity is important to people who rely on gigantic fonts.
524 # Finally, we use monospace to ensure the Search field and button
525 # has the same size and spacing as everything else which is
526 # <pre>-formatted anyways.
527 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
529 sub stylesheets_prepare ($$) {
530 my ($self, $upfx) = @_;
532 require CSS::Minifier;
533 sub { CSS::Minifier::minify(input => $_[0]) };
535 require CSS::Minifier::XS;
536 sub { CSS::Minifier::XS::minify($_[0]) };
540 my $stylesheets = $self->{pi_cfg}->{css} || [];
544 foreach my $s (@$stylesheets) {
547 foreach my $k (qw(media title href)) {
548 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
553 if (defined $attr->{href}) {
557 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
558 if ($key !~ /\A[a-zA-Z0-9_\-\.]+\z/) {
559 warn "ignoring $fn, non-ASCII word character\n";
562 open(my $fh, '<', $fn) or do {
563 warn "failed to open $fn: $!\n";
567 my $local = do { local $/; <$fh> };
568 if ($local =~ /\S/) {
569 $ctime = sprintf('%x',(stat($fh))[10]);
570 $local = $mini->($local);
573 # do not let BOFHs override userContent.css:
574 if ($local =~ /!\s*important\b/i) {
575 warn "ignoring $fn since it uses `!important'\n";
579 $css_map->{$key} = $local;
580 $attr->{href} = "$upfx$key.css?$ctime";
581 if (defined($attr->{title})) {
583 } elsif (($attr->{media}||'screen') eq 'screen') {
584 $attr->{-inline} = $local;
590 my $buf = "<style>$STYLE";
592 my @ext; # for media=print and whatnot
593 foreach my $attr (@$links) {
594 if (defined(my $str = delete $attr->{-inline})) {
605 foreach my $attr (@$links) {
606 delete $attr->{-inline};
607 $buf .= "<link\ntype=text/css\nrel=stylesheet";
608 while (my ($k, $v) = each %$attr) {
609 $v = qq{"$v"} if $v =~ /[\s=]/;
614 $self->{"-style-$upfx"} = $buf;
616 $self->{-style_inline} = $buf;
618 $self->{-css_map} = $css_map;
621 # returns an HTML fragment with <style> or <link> tags in them
622 # Called by WwwStream by nearly every HTML page
624 my ($self, $upfx) = @_;
625 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
626 stylesheets_prepare($self, $upfx);
627 $self->{-style_inline} || $self->{"-style-$upfx"}
631 # /$INBOX/$KEY.css endpoint
632 # CSS is configured globally for all inboxes, but we access them on
633 # a per-inbox basis. This allows administrators to setup per-inbox
634 # static routes to intercept the request before it hits PSGI
636 my ($ctx, $inbox, $key) = @_;
637 my $r404 = invalid_inbox($ctx, $inbox);
638 return $r404 if $r404;
639 my $self = $ctx->{www};
640 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
641 my $css = $css_map->{$key};
642 if (!defined($css) && $key eq 'userContent') {
643 my $env = $ctx->{env};
644 $css = PublicInbox::UserContent::sample($ctx->{ibx}, $env);
646 defined $css or return r404();
647 my $h = [ 'Content-Length', bytes::length($css),
648 'Content-Type', 'text/css' ];
649 PublicInbox::GitHTTPBackend::cache_one_year($h);
650 [ 200, $h, [ $css ] ];
653 sub get_description {
654 my ($ctx, $inbox) = @_;
655 invalid_inbox($ctx, $inbox) || do {
656 my $d = $ctx->{ibx}->description . "\n";
657 [ 200, [ 'Content-Length', bytes::length($d),
658 'Content-Type', 'text/plain' ], [ $d ] ];