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 # not using $env->{PATH_INFO} here since that's already decoded
72 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
73 my $method = $env->{REQUEST_METHOD};
75 if ($method eq 'POST') {
76 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
77 my ($part, $path) = ($2, $3);
78 return invalid_inbox($ctx, $1) ||
79 serve_git($ctx, $part, $path);
80 } elsif ($path_info =~ m!$INBOX_RE/!o) {
81 return invalid_inbox($ctx, $1) || mbox_results($ctx);
84 elsif ($method !~ /\AGET|HEAD\z/) {
85 return r(405, 'Method Not Allowed');
88 # top-level indices and feeds
89 if ($path_info eq '/') {
91 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
92 invalid_inbox($ctx, $1) || r301($ctx, $1);
93 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
94 invalid_inbox($ctx, $1) || get_index($ctx);
95 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
96 invalid_inbox($ctx, $1) || get_atom($ctx);
97 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
98 invalid_inbox($ctx, $1) || get_new($ctx);
99 } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
100 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
101 my ($part, $path) = ($2, $3);
102 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
103 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
104 serve_mbox_range($ctx, $1, $2);
105 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
106 msg_page($ctx, $1, $2, $3);
108 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
109 my ($idx, $fn) = ($3, $4);
110 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
111 # in case people leave off the trailing slash:
112 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
113 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
114 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
115 r301($ctx, $inbox, $mid_ue, $suffix);
117 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
118 my ($inbox, $mid_ue) = ($1, $2);
119 r301($ctx, $inbox, $mid_ue, '#R');
121 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
123 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
124 get_text($ctx, $1, $2);
125 } elsif ($path_info =~ m!$INBOX_RE/([\w\-\.]+)\.css\z!o) {
126 get_css($ctx, $1, $2);
127 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
128 get_vcs_object($ctx, $1, $2);
129 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/([\w\.\-]+)\z!o) {
130 get_vcs_object($ctx, $1, $2, $3);
131 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
132 r301($ctx, $1, $2, 's/');
133 # convenience redirects order matters
134 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
138 legacy_redirects($ctx, $path_info);
142 # for CoW-friendliness, MOOOOO!
145 require PublicInbox::Feed;
146 require PublicInbox::View;
147 require PublicInbox::SearchThread;
148 require PublicInbox::MIME;
152 foreach (qw(PublicInbox::Search PublicInbox::SearchView
153 PublicInbox::Mbox IO::Compress::Gzip
154 PublicInbox::NewsWWW)) {
159 $self->stylesheets_prepare($_) for ('', '../', '../../');
163 # private functions below
167 if ($ctx && $ctx->{mid}) {
168 require PublicInbox::ExtMsg;
170 return PublicInbox::ExtMsg::ext_msg($ctx);
175 # simple response for errors
176 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
178 sub news_cgit_fallback ($) {
180 my $www = $ctx->{www};
181 my $env = $ctx->{env};
182 my $res = $www->news_www->call($env);
183 $res->[0] == 404 ? $www->cgit->call($env) : $res;
186 # returns undef if valid, array ref response if invalid
187 sub invalid_inbox ($$) {
188 my ($ctx, $inbox) = @_;
189 my $obj = $ctx->{www}->{pi_config}->lookup_name($inbox);
191 $ctx->{git} = $obj->git;
192 $ctx->{-inbox} = $obj;
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;
242 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
243 require PublicInbox::SearchView;
244 PublicInbox::SearchView::sres_top_html($ctx);
246 PublicInbox::Feed::generate_html_index($ctx);
250 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
253 require PublicInbox::Mbox;
254 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
257 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
260 require PublicInbox::View;
262 PublicInbox::View::msg_page($ctx) || r404($ctx);
265 # /$INBOX/$MESSAGE_ID/t/
267 my ($ctx, $flat) = @_;
268 searcher($ctx) or return need_search($ctx);
269 $ctx->{flat} = $flat;
270 require PublicInbox::View;
271 PublicInbox::View::thread_html($ctx);
274 # /$INBOX/_/text/$KEY/
275 # /$INBOX/_/text/$KEY/raw
276 # KEY may contain slashes
278 my ($ctx, $inbox, $key) = @_;
279 my $r404 = invalid_inbox($ctx, $inbox);
280 return $r404 if $r404;
282 require PublicInbox::WwwText;
283 PublicInbox::WwwText::get_text($ctx, $key);
286 # show git objects (blobs and commits)
287 # /$INBOX/_/$OBJECT_ID/show
288 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
289 # KEY may contain slashes
290 sub get_vcs_object ($$$;$) {
291 my ($ctx, $inbox, $oid, $filename) = @_;
292 my $r404 = invalid_inbox($ctx, $inbox);
293 return $r404 if $r404;
294 require PublicInbox::ViewVCS;
295 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
299 my ($ctx, $key) = @_;
300 my $val = $ctx->{$key};
301 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
305 # search support is optional, returns undef if Xapian is not installed
306 # or not configured for the given GIT_DIR
310 require PublicInbox::Search;
311 $ctx->{srch} = $ctx->{-inbox}->search;
318 <html><head><title>Search not available for this
319 public-inbox</title><body><pre>Search is not available for this public-inbox
320 <a href="../">Return to index</a></pre></body></html>
322 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
325 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
326 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
327 # note: I'm not a big fan of other compression formats since they're
328 # significantly more expensive on CPU than gzip and less-widely available,
329 # especially on older systems. Stick to zlib since that's what git uses.
330 sub get_thread_mbox {
331 my ($ctx, $sfx) = @_;
332 my $srch = searcher($ctx) or return need_search($ctx);
333 require PublicInbox::Mbox;
334 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
338 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
339 sub get_thread_atom {
341 searcher($ctx) or return need_search($ctx);
342 require PublicInbox::Feed;
343 PublicInbox::Feed::generate_thread_atom($ctx);
346 sub legacy_redirects {
347 my ($ctx, $path_info) = @_;
349 # single-message pages
350 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
352 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
353 r301($ctx, $1, $2, 'raw');
355 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
359 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
360 r301($ctx, $1, $2, 't/#u');
362 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
363 r301($ctx, $1, $2, "t.mbox$3");
365 # even older legacy redirects
366 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
369 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
370 r301($ctx, $1, $2, 't/#u');
372 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
375 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
376 r301($ctx, $1, $2, 'raw');
378 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
379 r301($ctx, $1, $2, "t$3");
381 # legacy convenience redirects, order still matters
382 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
384 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
385 r301($ctx, $1, $2, 't/#u');
386 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
389 # some Message-IDs have slashes in them and the HTTP server
390 # may try to be clever and unescape them :<
391 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
392 msg_page($ctx, $1, $2, $3);
394 # in case people leave off the trailing slash:
395 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
396 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
397 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
400 news_cgit_fallback($ctx);
405 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
406 my $obj = $ctx->{-inbox};
408 my $r404 = invalid_inbox($ctx, $inbox);
409 return $r404 if $r404;
410 $obj = $ctx->{-inbox};
412 my $url = $obj->base_url($ctx->{env});
413 my $qs = $ctx->{env}->{QUERY_STRING};
414 if (defined $mid_ue) {
415 # common, and much nicer as '@' than '%40':
416 $mid_ue =~ s/%40/@/g;
417 $url .= $mid_ue . '/';
419 $url .= $suffix if (defined $suffix);
420 $url .= "?$qs" if $qs ne '';
423 [ Location => $url, 'Content-Type' => 'text/plain' ],
424 [ "Redirecting to $url\n" ] ]
428 my ($ctx, $inbox, $mid_ue, $e) = @_;
430 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
431 '' eq $e and return get_mid_html($ctx);
432 'T/' eq $e and return get_thread($ctx, 1);
433 't/' eq $e and return get_thread($ctx);
434 't.atom' eq $e and return get_thread_atom($ctx);
435 't.mbox' eq $e and return get_thread_mbox($ctx);
436 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
437 'raw' eq $e and return get_mid_txt($ctx);
439 # legacy, but no redirect for compatibility:
440 'f/' eq $e and return get_mid_html($ctx);
445 my ($ctx, $part, $path) = @_;
446 my $env = $ctx->{env};
447 my $ibx = $ctx->{-inbox};
448 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
449 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
454 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
455 searcher($ctx) or return need_search($ctx);
456 require PublicInbox::SearchView;
457 return PublicInbox::SearchView::mbox_results($ctx);
462 sub serve_mbox_range {
463 my ($ctx, $inbox, $range) = @_;
464 invalid_inbox($ctx, $inbox) || eval {
465 require PublicInbox::Mbox;
467 PublicInbox::Mbox::emit_range($ctx, $range);
473 $self->{news_www} ||= do {
474 require PublicInbox::NewsWWW;
475 PublicInbox::NewsWWW->new($self->{pi_config});
481 $self->{cgit} ||= do {
482 my $pi_config = $self->{pi_config};
484 if (defined($pi_config->{'publicinbox.cgitrc'})) {
485 require PublicInbox::Cgit;
486 PublicInbox::Cgit->new($pi_config);
488 Plack::Util::inline_object(call => sub { r404() });
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 open(my $fh, '<', $fn) or do {
540 warn "failed to open $fn: $!\n";
543 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
545 my $local = do { local $/; <$fh> };
546 if ($local =~ /\S/) {
547 $ctime = sprintf('%x',(stat($fh))[10]);
548 $local = $mini->($local);
551 # do not let BOFHs override userContent.css:
552 if ($local =~ /!\s*important\b/i) {
553 warn "ignoring $fn since it uses `!important'\n";
557 $css_map->{$key} = $local;
558 $attr->{href} = "$upfx$key.css?$ctime";
559 if (defined($attr->{title})) {
561 } elsif (($attr->{media}||'screen') eq 'screen') {
562 $attr->{-inline} = $local;
568 my $buf = "<style>$STYLE";
570 my @ext; # for media=print and whatnot
571 foreach my $attr (@$links) {
572 if (defined(my $str = delete $attr->{-inline})) {
583 foreach my $attr (@$links) {
584 delete $attr->{-inline};
585 $buf .= "<link\ntype=text/css\nrel=stylesheet";
586 while (my ($k, $v) = each %$attr) {
587 $v = qq{"$v"} if $v =~ /[\s=]/;
592 $self->{"-style-$upfx"} = $buf;
594 $self->{-style_inline} = $buf;
596 $self->{-css_map} = $css_map;
599 # returns an HTML fragment with <style> or <link> tags in them
600 # Called by WwwStream by nearly every HTML page
602 my ($self, $upfx) = @_;
603 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
604 stylesheets_prepare($self, $upfx);
605 $self->{-style_inline} || $self->{"-style-$upfx"}
609 # /$INBOX/$KEY.css endpoint
610 # CSS is configured globally for all inboxes, but we access them on
611 # a per-inbox basis. This allows administrators to setup per-inbox
612 # static routes to intercept the request before it hits PSGI
614 my ($ctx, $inbox, $key) = @_;
615 my $r404 = invalid_inbox($ctx, $inbox);
616 return $r404 if $r404;
617 my $self = $ctx->{www};
618 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
619 my $css = $css_map->{$key};
620 if (!defined($css) && $key eq 'userContent') {
621 my $env = $ctx->{env};
622 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
624 defined $css or return r404();
625 my $h = [ 'Content-Length', bytes::length($css),
626 'Content-Type', 'text/css' ];
627 PublicInbox::GitHTTPBackend::cache_one_year($h);
628 [ 200, $h, [ $css ] ];