1 # Copyright (C) 2014-2018 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
19 use PublicInbox::Config;
20 use PublicInbox::Hval;
21 use URI::Escape qw(uri_unescape);
22 use PublicInbox::MID qw(mid_escape);
23 require PublicInbox::Git;
24 use PublicInbox::GitHTTPBackend;
25 use PublicInbox::UserContent;
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,40}!;
35 my ($class, $pi_config) = @_;
36 $pi_config ||= PublicInbox::Config->new;
37 bless { pi_config => $pi_config }, $class;
40 # backwards compatibility, do not use
42 my ($req, $method) = @_;
43 PublicInbox::WWW->new->call($req->env);
49 my $sn = $_[0]->{SCRIPT_NAME};
50 $path_re_cache{$sn} ||= do {
51 $sn = '/'.$sn unless index($sn, '/') == 0;
53 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
58 my ($self, $env) = @_;
59 my $ctx = { env => $env, www => $self };
61 # we don't care about multi-value
65 my ($k, $v) = split('=', $_, 2);
66 $v = uri_unescape($v // '');
67 # none of the keys we care about will need escaping
69 } split(/[&;]+/, $env->{QUERY_STRING});
71 # avoiding $env->{PATH_INFO} here since that's already decoded
72 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
73 $path_info //= $env->{PATH_INFO};
74 my $method = $env->{REQUEST_METHOD};
76 if ($method eq 'POST') {
77 if ($path_info =~ m!$INBOX_RE/(?:([0-9]+)/)?
78 (git-upload-pack)\z!x) {
79 my ($part, $path) = ($2, $3);
80 return invalid_inbox($ctx, $1) ||
81 serve_git($ctx, $part, $path);
82 } elsif ($path_info =~ m!$INBOX_RE/!o) {
83 return invalid_inbox($ctx, $1) || mbox_results($ctx);
86 elsif ($method !~ /\AGET|HEAD\z/) {
87 return r(405, 'Method Not Allowed');
90 # top-level indices and feeds
91 if ($path_info eq '/') {
92 www_listing($self)->call($env);
93 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
94 invalid_inbox($ctx, $1) || r301($ctx, $1);
95 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
96 invalid_inbox($ctx, $1) || get_index($ctx);
97 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
98 invalid_inbox($ctx, $1) || get_atom($ctx);
99 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
100 invalid_inbox($ctx, $1) || get_new($ctx);
101 } elsif ($path_info =~ m!$INBOX_RE/(?:([0-9]+)/)?
102 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
103 my ($part, $path) = ($2, $3);
104 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
105 } elsif ($path_info =~ m!$INBOX_RE/([a-zA-Z0-9_\-]+).mbox\.gz\z!o) {
106 serve_mbox_range($ctx, $1, $2);
107 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
108 msg_page($ctx, $1, $2, $3);
110 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
111 my ($idx, $fn) = ($3, $4);
112 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
113 # in case people leave off the trailing slash:
114 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
115 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
116 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
117 r301($ctx, $inbox, $mid_ue, $suffix);
119 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
120 my ($inbox, $mid_ue) = ($1, $2);
121 r301($ctx, $inbox, $mid_ue, '#R');
123 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
125 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
126 get_text($ctx, $1, $2);
127 } elsif ($path_info =~ m!$INBOX_RE/([a-zA-Z0-9_\-\.]+)\.css\z!o) {
128 get_css($ctx, $1, $2);
129 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
130 get_vcs_object($ctx, $1, $2);
131 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/
132 ($PublicInbox::Hval::FN)\z!ox) {
133 get_vcs_object($ctx, $1, $2, $3);
134 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
135 r301($ctx, $1, $2, 's/');
136 # convenience redirects order matters
137 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
141 legacy_redirects($ctx, $path_info);
145 # for CoW-friendliness, MOOOOO!
148 require PublicInbox::Feed;
149 require PublicInbox::View;
150 require PublicInbox::SearchThread;
151 require PublicInbox::MIME;
155 require PublicInbox::Search;
156 PublicInbox::Search::load_xapian();
158 foreach (qw(PublicInbox::SearchView
159 PublicInbox::Mbox IO::Compress::Gzip
160 PublicInbox::NewsWWW)) {
165 $self->stylesheets_prepare($_) for ('', '../', '../../');
170 # private functions below
174 if ($ctx && $ctx->{mid}) {
175 require PublicInbox::ExtMsg;
176 return PublicInbox::ExtMsg::ext_msg($ctx);
181 # simple response for errors
182 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
184 sub news_cgit_fallback ($) {
186 my $www = $ctx->{www};
187 my $env = $ctx->{env};
188 my $res = $www->news_www->call($env);
189 $res->[0] == 404 ? $www->cgit->call($env) : $res;
192 # returns undef if valid, array ref response if invalid
193 sub invalid_inbox ($$) {
194 my ($ctx, $inbox) = @_;
195 my $ibx = $ctx->{www}->{pi_config}->lookup_name($inbox);
197 $ctx->{-inbox} = $ibx;
201 # sometimes linkifiers (not ours!) screw up automatic link
202 # generation and link things intended for nntp:// to https?://,
203 # so try to infer links and redirect them to the appropriate
205 news_cgit_fallback($ctx);
208 # returns undef if valid, array ref response if invalid
209 sub invalid_inbox_mid {
210 my ($ctx, $inbox, $mid_ue) = @_;
211 my $ret = invalid_inbox($ctx, $inbox);
214 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
215 my $ibx = $ctx->{-inbox};
216 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
217 my ($x2, $x38) = ($1, $2);
218 # this is horrifically wasteful for legacy URLs:
219 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
220 require Email::Simple;
221 my $s = Email::Simple->new($str);
222 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
223 return r301($ctx, $inbox, mid_escape($mid));
228 # /$INBOX/new.atom -> Atom feed, includes replies
231 require PublicInbox::Feed;
232 PublicInbox::Feed::generate($ctx);
235 # /$INBOX/new.html -> HTML only
238 require PublicInbox::Feed;
239 PublicInbox::Feed::new_html($ctx);
242 # /$INBOX/?r=$GIT_COMMIT -> HTML only
245 require PublicInbox::Feed;
246 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
247 require PublicInbox::SearchView;
248 PublicInbox::SearchView::sres_top_html($ctx);
250 PublicInbox::Feed::generate_html_index($ctx);
254 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
257 require PublicInbox::Mbox;
258 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
261 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
264 require PublicInbox::View;
265 PublicInbox::View::msg_page($ctx) || r404($ctx);
268 # /$INBOX/$MESSAGE_ID/t/
270 my ($ctx, $flat) = @_;
271 $ctx->{-inbox}->over or return need($ctx, 'Overview');
272 $ctx->{flat} = $flat;
273 require PublicInbox::View;
274 PublicInbox::View::thread_html($ctx);
277 # /$INBOX/_/text/$KEY/
278 # /$INBOX/_/text/$KEY/raw
279 # KEY may contain slashes
281 my ($ctx, $inbox, $key) = @_;
282 my $r404 = invalid_inbox($ctx, $inbox);
283 return $r404 if $r404;
285 require PublicInbox::WwwText;
286 PublicInbox::WwwText::get_text($ctx, $key);
289 # show git objects (blobs and commits)
290 # /$INBOX/_/$OBJECT_ID/show
291 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
292 # KEY may contain slashes
293 sub get_vcs_object ($$$;$) {
294 my ($ctx, $inbox, $oid, $filename) = @_;
295 my $r404 = invalid_inbox($ctx, $inbox);
296 return $r404 if $r404;
297 require PublicInbox::ViewVCS;
298 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
302 my ($ctx, $key) = @_;
303 my $val = $ctx->{$key};
304 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
309 my ($ctx, $extra) = @_;
311 <html><head><title>$extra not available for this
312 public-inbox</title><body><pre>$extra is not available for this public-inbox
313 <a href="../">Return to index</a></pre></body></html>
315 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
318 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
319 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
320 # note: I'm not a big fan of other compression formats since they're
321 # significantly more expensive on CPU than gzip and less-widely available,
322 # especially on older systems. Stick to zlib since that's what git uses.
323 sub get_thread_mbox {
324 my ($ctx, $sfx) = @_;
325 my $over = $ctx->{-inbox}->over or return need($ctx, 'Overview');
326 require PublicInbox::Mbox;
327 PublicInbox::Mbox::thread_mbox($ctx, $over, $sfx);
331 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
332 sub get_thread_atom {
334 $ctx->{-inbox}->over or return need($ctx, 'Overview');
335 require PublicInbox::Feed;
336 PublicInbox::Feed::generate_thread_atom($ctx);
339 sub legacy_redirects {
340 my ($ctx, $path_info) = @_;
342 # single-message pages
343 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
345 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
346 r301($ctx, $1, $2, 'raw');
348 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
352 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
353 r301($ctx, $1, $2, 't/#u');
355 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
356 r301($ctx, $1, $2, "t.mbox$3");
358 # even older legacy redirects
359 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
362 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
363 r301($ctx, $1, $2, 't/#u');
365 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
368 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
369 r301($ctx, $1, $2, 'raw');
371 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
372 r301($ctx, $1, $2, "t$3");
374 # legacy convenience redirects, order still matters
375 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
377 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
378 r301($ctx, $1, $2, 't/#u');
379 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
382 # some Message-IDs have slashes in them and the HTTP server
383 # may try to be clever and unescape them :<
384 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
385 msg_page($ctx, $1, $2, $3);
387 # in case people leave off the trailing slash:
388 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
389 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
390 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
393 news_cgit_fallback($ctx);
398 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
399 my $ibx = $ctx->{-inbox};
401 my $r404 = invalid_inbox($ctx, $inbox);
402 return $r404 if $r404;
403 $ibx = $ctx->{-inbox};
405 my $url = $ibx->base_url($ctx->{env});
406 my $qs = $ctx->{env}->{QUERY_STRING};
407 if (defined $mid_ue) {
408 # common, and much nicer as '@' than '%40':
409 $mid_ue =~ s/%40/@/g;
410 $url .= $mid_ue . '/';
412 $url .= $suffix if (defined $suffix);
413 $url .= "?$qs" if $qs ne '';
416 [ Location => $url, 'Content-Type' => 'text/plain' ],
417 [ "Redirecting to $url\n" ] ]
421 my ($ctx, $inbox, $mid_ue, $e) = @_;
423 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
424 '' eq $e and return get_mid_html($ctx);
425 'T/' eq $e and return get_thread($ctx, 1);
426 't/' eq $e and return get_thread($ctx);
427 't.atom' eq $e and return get_thread_atom($ctx);
428 't.mbox' eq $e and return get_thread_mbox($ctx);
429 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
430 'raw' eq $e and return get_mid_txt($ctx);
432 # legacy, but no redirect for compatibility:
433 'f/' eq $e and return get_mid_html($ctx);
438 my ($ctx, $part, $path) = @_;
439 my $env = $ctx->{env};
440 my $ibx = $ctx->{-inbox};
441 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
442 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
447 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
448 $ctx->{-inbox}->search or return need($ctx, 'search');
449 require PublicInbox::SearchView;
450 return PublicInbox::SearchView::mbox_results($ctx);
455 sub serve_mbox_range {
456 my ($ctx, $inbox, $range) = @_;
457 invalid_inbox($ctx, $inbox) || eval {
458 require PublicInbox::Mbox;
459 PublicInbox::Mbox::emit_range($ctx, $range);
465 $self->{news_www} ||= do {
466 require PublicInbox::NewsWWW;
467 PublicInbox::NewsWWW->new($self->{pi_config});
473 $self->{cgit} ||= do {
474 my $pi_config = $self->{pi_config};
476 if (defined($pi_config->{'publicinbox.cgitrc'})) {
477 require PublicInbox::Cgit;
478 PublicInbox::Cgit->new($pi_config);
480 Plack::Util::inline_object(call => sub { r404() });
487 $self->{www_listing} ||= do {
488 require PublicInbox::WwwListing;
489 PublicInbox::WwwListing->new($self);
494 my ($ctx, $idx, $fn) = @_;
495 require PublicInbox::WwwAttach;
496 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
499 # User-generated content (UGC) may have excessively long lines
500 # and screw up rendering on some browsers, so we use pre-wrap.
502 # We also force everything to the same scaled font-size because GUI
503 # browsers (tested both Firefox and surf (webkit)) uses a larger font
504 # for the Search <form> element than the rest of the page. Font size
505 # uniformity is important to people who rely on gigantic fonts.
506 # Finally, we use monospace to ensure the Search field and button
507 # has the same size and spacing as everything else which is
508 # <pre>-formatted anyways.
509 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
511 sub stylesheets_prepare ($$) {
512 my ($self, $upfx) = @_;
514 require CSS::Minifier;
515 sub { CSS::Minifier::minify(input => $_[0]) };
517 require CSS::Minifier::XS;
518 sub { CSS::Minifier::XS::minify($_[0]) };
522 my $stylesheets = $self->{pi_config}->{css} || [];
526 foreach my $s (@$stylesheets) {
529 foreach my $k (qw(media title href)) {
530 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
535 if (defined $attr->{href}) {
539 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
540 if ($key !~ /\A[a-zA-Z0-9_\-\.]+\z/) {
541 warn "ignoring $fn, non-ASCII word character\n";
544 open(my $fh, '<', $fn) or do {
545 warn "failed to open $fn: $!\n";
549 my $local = do { local $/; <$fh> };
550 if ($local =~ /\S/) {
551 $ctime = sprintf('%x',(stat($fh))[10]);
552 $local = $mini->($local);
555 # do not let BOFHs override userContent.css:
556 if ($local =~ /!\s*important\b/i) {
557 warn "ignoring $fn since it uses `!important'\n";
561 $css_map->{$key} = $local;
562 $attr->{href} = "$upfx$key.css?$ctime";
563 if (defined($attr->{title})) {
565 } elsif (($attr->{media}||'screen') eq 'screen') {
566 $attr->{-inline} = $local;
572 my $buf = "<style>$STYLE";
574 my @ext; # for media=print and whatnot
575 foreach my $attr (@$links) {
576 if (defined(my $str = delete $attr->{-inline})) {
587 foreach my $attr (@$links) {
588 delete $attr->{-inline};
589 $buf .= "<link\ntype=text/css\nrel=stylesheet";
590 while (my ($k, $v) = each %$attr) {
591 $v = qq{"$v"} if $v =~ /[\s=]/;
596 $self->{"-style-$upfx"} = $buf;
598 $self->{-style_inline} = $buf;
600 $self->{-css_map} = $css_map;
603 # returns an HTML fragment with <style> or <link> tags in them
604 # Called by WwwStream by nearly every HTML page
606 my ($self, $upfx) = @_;
607 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
608 stylesheets_prepare($self, $upfx);
609 $self->{-style_inline} || $self->{"-style-$upfx"}
613 # /$INBOX/$KEY.css endpoint
614 # CSS is configured globally for all inboxes, but we access them on
615 # a per-inbox basis. This allows administrators to setup per-inbox
616 # static routes to intercept the request before it hits PSGI
618 my ($ctx, $inbox, $key) = @_;
619 my $r404 = invalid_inbox($ctx, $inbox);
620 return $r404 if $r404;
621 my $self = $ctx->{www};
622 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
623 my $css = $css_map->{$key};
624 if (!defined($css) && $key eq 'userContent') {
625 my $env = $ctx->{env};
626 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
628 defined $css or return r404();
629 my $h = [ 'Content-Length', bytes::length($css),
630 'Content-Type', 'text/css' ];
631 PublicInbox::GitHTTPBackend::cache_one_year($h);
632 [ 200, $h, [ $css ] ];