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!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
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
64 my ($k, $v) = split('=', uri_unescape($_), 2);
65 $v = '' unless defined $v;
68 } 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/(?:(\d+)/)?(git-upload-pack)\z!) {
78 my ($part, $path) = ($2, $3);
79 return invalid_inbox($ctx, $1) ||
80 serve_git($ctx, $part, $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 '/') {
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/(?:(\d+)/)?
101 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
102 my ($part, $path) = ($2, $3);
103 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
104 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).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/([\w\-\.]+)\.css\z!o) {
127 get_css($ctx, $1, $2);
128 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
129 get_vcs_object($ctx, $1, $2);
130 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/([\w\.\-]+)\z!o) {
131 get_vcs_object($ctx, $1, $2, $3);
132 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
133 r301($ctx, $1, $2, 's/');
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!
146 require PublicInbox::Feed;
147 require PublicInbox::View;
148 require PublicInbox::SearchThread;
149 require PublicInbox::MIME;
153 foreach (qw(PublicInbox::Search PublicInbox::SearchView
154 PublicInbox::Mbox IO::Compress::Gzip
155 PublicInbox::NewsWWW)) {
160 $self->stylesheets_prepare($_) for ('', '../', '../../');
165 # private functions below
169 if ($ctx && $ctx->{mid}) {
170 require PublicInbox::ExtMsg;
171 return PublicInbox::ExtMsg::ext_msg($ctx);
176 # simple response for errors
177 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
179 sub news_cgit_fallback ($) {
181 my $www = $ctx->{www};
182 my $env = $ctx->{env};
183 my $res = $www->news_www->call($env);
184 $res->[0] == 404 ? $www->cgit->call($env) : $res;
187 # returns undef if valid, array ref response if invalid
188 sub invalid_inbox ($$) {
189 my ($ctx, $inbox) = @_;
190 my $ibx = $ctx->{www}->{pi_config}->lookup_name($inbox);
192 $ctx->{-inbox} = $ibx;
196 # sometimes linkifiers (not ours!) screw up automatic link
197 # generation and link things intended for nntp:// to https?://,
198 # so try to infer links and redirect them to the appropriate
200 news_cgit_fallback($ctx);
203 # returns undef if valid, array ref response if invalid
204 sub invalid_inbox_mid {
205 my ($ctx, $inbox, $mid_ue) = @_;
206 my $ret = invalid_inbox($ctx, $inbox);
209 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
210 my $ibx = $ctx->{-inbox};
211 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
212 my ($x2, $x38) = ($1, $2);
213 # this is horrifically wasteful for legacy URLs:
214 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
215 require Email::Simple;
216 my $s = Email::Simple->new($str);
217 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
218 return r301($ctx, $inbox, mid_escape($mid));
223 # /$INBOX/new.atom -> Atom feed, includes replies
226 require PublicInbox::Feed;
227 PublicInbox::Feed::generate($ctx);
230 # /$INBOX/new.html -> HTML only
233 require PublicInbox::Feed;
234 PublicInbox::Feed::new_html($ctx);
237 # /$INBOX/?r=$GIT_COMMIT -> HTML only
240 require PublicInbox::Feed;
241 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
242 require PublicInbox::SearchView;
243 PublicInbox::SearchView::sres_top_html($ctx);
245 PublicInbox::Feed::generate_html_index($ctx);
249 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
252 require PublicInbox::Mbox;
253 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
256 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
259 require PublicInbox::View;
260 PublicInbox::View::msg_page($ctx) || r404($ctx);
263 # /$INBOX/$MESSAGE_ID/t/
265 my ($ctx, $flat) = @_;
266 $ctx->{-inbox}->over or return need($ctx, 'Overview');
267 $ctx->{flat} = $flat;
268 require PublicInbox::View;
269 PublicInbox::View::thread_html($ctx);
272 # /$INBOX/_/text/$KEY/
273 # /$INBOX/_/text/$KEY/raw
274 # KEY may contain slashes
276 my ($ctx, $inbox, $key) = @_;
277 my $r404 = invalid_inbox($ctx, $inbox);
278 return $r404 if $r404;
280 require PublicInbox::WwwText;
281 PublicInbox::WwwText::get_text($ctx, $key);
284 # show git objects (blobs and commits)
285 # /$INBOX/_/$OBJECT_ID/show
286 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
287 # KEY may contain slashes
288 sub get_vcs_object ($$$;$) {
289 my ($ctx, $inbox, $oid, $filename) = @_;
290 my $r404 = invalid_inbox($ctx, $inbox);
291 return $r404 if $r404;
292 require PublicInbox::ViewVCS;
293 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
297 my ($ctx, $key) = @_;
298 my $val = $ctx->{$key};
299 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
304 my ($ctx, $extra) = @_;
306 <html><head><title>$extra not available for this
307 public-inbox</title><body><pre>$extra is not available for this public-inbox
308 <a href="../">Return to index</a></pre></body></html>
310 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
313 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
314 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
315 # note: I'm not a big fan of other compression formats since they're
316 # significantly more expensive on CPU than gzip and less-widely available,
317 # especially on older systems. Stick to zlib since that's what git uses.
318 sub get_thread_mbox {
319 my ($ctx, $sfx) = @_;
320 my $over = $ctx->{-inbox}->over or return need($ctx, 'Overview');
321 require PublicInbox::Mbox;
322 PublicInbox::Mbox::thread_mbox($ctx, $over, $sfx);
326 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
327 sub get_thread_atom {
329 $ctx->{-inbox}->over or return need($ctx, 'Overview');
330 require PublicInbox::Feed;
331 PublicInbox::Feed::generate_thread_atom($ctx);
334 sub legacy_redirects {
335 my ($ctx, $path_info) = @_;
337 # single-message pages
338 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
340 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
341 r301($ctx, $1, $2, 'raw');
343 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
347 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
348 r301($ctx, $1, $2, 't/#u');
350 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
351 r301($ctx, $1, $2, "t.mbox$3");
353 # even older legacy redirects
354 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
357 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
358 r301($ctx, $1, $2, 't/#u');
360 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
363 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
364 r301($ctx, $1, $2, 'raw');
366 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
367 r301($ctx, $1, $2, "t$3");
369 # legacy convenience redirects, order still matters
370 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
372 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
373 r301($ctx, $1, $2, 't/#u');
374 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
377 # some Message-IDs have slashes in them and the HTTP server
378 # may try to be clever and unescape them :<
379 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
380 msg_page($ctx, $1, $2, $3);
382 # in case people leave off the trailing slash:
383 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
384 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
385 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
388 news_cgit_fallback($ctx);
393 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
394 my $ibx = $ctx->{-inbox};
396 my $r404 = invalid_inbox($ctx, $inbox);
397 return $r404 if $r404;
398 $ibx = $ctx->{-inbox};
400 my $url = $ibx->base_url($ctx->{env});
401 my $qs = $ctx->{env}->{QUERY_STRING};
402 if (defined $mid_ue) {
403 # common, and much nicer as '@' than '%40':
404 $mid_ue =~ s/%40/@/g;
405 $url .= $mid_ue . '/';
407 $url .= $suffix if (defined $suffix);
408 $url .= "?$qs" if $qs ne '';
411 [ Location => $url, 'Content-Type' => 'text/plain' ],
412 [ "Redirecting to $url\n" ] ]
416 my ($ctx, $inbox, $mid_ue, $e) = @_;
418 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
419 '' eq $e and return get_mid_html($ctx);
420 'T/' eq $e and return get_thread($ctx, 1);
421 't/' eq $e and return get_thread($ctx);
422 't.atom' eq $e and return get_thread_atom($ctx);
423 't.mbox' eq $e and return get_thread_mbox($ctx);
424 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
425 'raw' eq $e and return get_mid_txt($ctx);
427 # legacy, but no redirect for compatibility:
428 'f/' eq $e and return get_mid_html($ctx);
433 my ($ctx, $part, $path) = @_;
434 my $env = $ctx->{env};
435 my $ibx = $ctx->{-inbox};
436 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
437 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
442 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
443 $ctx->{-inbox}->search or return need($ctx, 'search');
444 require PublicInbox::SearchView;
445 return PublicInbox::SearchView::mbox_results($ctx);
450 sub serve_mbox_range {
451 my ($ctx, $inbox, $range) = @_;
452 invalid_inbox($ctx, $inbox) || eval {
453 require PublicInbox::Mbox;
454 PublicInbox::Mbox::emit_range($ctx, $range);
460 $self->{news_www} ||= do {
461 require PublicInbox::NewsWWW;
462 PublicInbox::NewsWWW->new($self->{pi_config});
468 $self->{cgit} ||= do {
469 my $pi_config = $self->{pi_config};
471 if (defined($pi_config->{'publicinbox.cgitrc'})) {
472 require PublicInbox::Cgit;
473 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);
489 my ($ctx, $idx, $fn) = @_;
490 require PublicInbox::WwwAttach;
491 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
494 # User-generated content (UGC) may have excessively long lines
495 # and screw up rendering on some browsers, so we use pre-wrap.
497 # We also force everything to the same scaled font-size because GUI
498 # browsers (tested both Firefox and surf (webkit)) uses a larger font
499 # for the Search <form> element than the rest of the page. Font size
500 # uniformity is important to people who rely on gigantic fonts.
501 # Finally, we use monospace to ensure the Search field and button
502 # has the same size and spacing as everything else which is
503 # <pre>-formatted anyways.
504 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
506 sub stylesheets_prepare ($$) {
507 my ($self, $upfx) = @_;
509 require CSS::Minifier;
510 sub { CSS::Minifier::minify(input => $_[0]) };
512 require CSS::Minifier::XS;
513 sub { CSS::Minifier::XS::minify($_[0]) };
517 my $stylesheets = $self->{pi_config}->{css} || [];
521 foreach my $s (@$stylesheets) {
524 foreach my $k (qw(media title href)) {
525 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
530 if (defined $attr->{href}) {
534 open(my $fh, '<', $fn) or do {
535 warn "failed to open $fn: $!\n";
538 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
540 my $local = do { local $/; <$fh> };
541 if ($local =~ /\S/) {
542 $ctime = sprintf('%x',(stat($fh))[10]);
543 $local = $mini->($local);
546 # do not let BOFHs override userContent.css:
547 if ($local =~ /!\s*important\b/i) {
548 warn "ignoring $fn since it uses `!important'\n";
552 $css_map->{$key} = $local;
553 $attr->{href} = "$upfx$key.css?$ctime";
554 if (defined($attr->{title})) {
556 } elsif (($attr->{media}||'screen') eq 'screen') {
557 $attr->{-inline} = $local;
563 my $buf = "<style>$STYLE";
565 my @ext; # for media=print and whatnot
566 foreach my $attr (@$links) {
567 if (defined(my $str = delete $attr->{-inline})) {
578 foreach my $attr (@$links) {
579 delete $attr->{-inline};
580 $buf .= "<link\ntype=text/css\nrel=stylesheet";
581 while (my ($k, $v) = each %$attr) {
582 $v = qq{"$v"} if $v =~ /[\s=]/;
587 $self->{"-style-$upfx"} = $buf;
589 $self->{-style_inline} = $buf;
591 $self->{-css_map} = $css_map;
594 # returns an HTML fragment with <style> or <link> tags in them
595 # Called by WwwStream by nearly every HTML page
597 my ($self, $upfx) = @_;
598 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
599 stylesheets_prepare($self, $upfx);
600 $self->{-style_inline} || $self->{"-style-$upfx"}
604 # /$INBOX/$KEY.css endpoint
605 # CSS is configured globally for all inboxes, but we access them on
606 # a per-inbox basis. This allows administrators to setup per-inbox
607 # static routes to intercept the request before it hits PSGI
609 my ($ctx, $inbox, $key) = @_;
610 my $r404 = invalid_inbox($ctx, $inbox);
611 return $r404 if $r404;
612 my $self = $ctx->{www};
613 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
614 my $css = $css_map->{$key};
615 if (!defined($css) && $key eq 'userContent') {
616 my $env = $ctx->{env};
617 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
619 defined $css or return r404();
620 my $h = [ 'Content-Length', bytes::length($css),
621 'Content-Type', 'text/css' ];
622 PublicInbox::GitHTTPBackend::cache_one_year($h);
623 [ 200, $h, [ $css ] ];