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 PublicInbox::Config;
18 use PublicInbox::Hval;
19 use URI::Escape qw(uri_unescape);
20 use PublicInbox::MID qw(mid_escape);
21 require PublicInbox::Git;
22 use PublicInbox::GitHTTPBackend;
23 use PublicInbox::UserContent;
25 # TODO: consider a routing tree now that we have more endpoints:
26 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
27 our $MID_RE = qr!([^/]+)!;
28 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
29 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
30 our $OID_RE = qr![a-f0-9]{7,40}!;
33 my ($class, $pi_config) = @_;
34 $pi_config ||= PublicInbox::Config->new;
35 bless { pi_config => $pi_config }, $class;
38 # backwards compatibility, do not use
40 my ($req, $method) = @_;
41 PublicInbox::WWW->new->call($req->env);
47 my $sn = $_[0]->{SCRIPT_NAME};
48 $path_re_cache{$sn} ||= do {
49 $sn = '/'.$sn unless index($sn, '/') == 0;
51 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
56 my ($self, $env) = @_;
57 my $ctx = { env => $env, www => $self };
59 # we don't care about multi-value
62 my ($k, $v) = split('=', uri_unescape($_), 2);
63 $v = '' unless defined $v;
66 } split(/[&;]+/, $env->{QUERY_STRING});
69 # not using $env->{PATH_INFO} here since that's already decoded
70 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
71 my $method = $env->{REQUEST_METHOD};
73 if ($method eq 'POST') {
74 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
75 my ($part, $path) = ($2, $3);
76 return invalid_inbox($ctx, $1) ||
77 serve_git($ctx, $part, $path);
78 } elsif ($path_info =~ m!$INBOX_RE/!o) {
79 return invalid_inbox($ctx, $1) || mbox_results($ctx);
82 elsif ($method !~ /\AGET|HEAD\z/) {
83 return r(405, 'Method Not Allowed');
86 # top-level indices and feeds
87 if ($path_info eq '/') {
89 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
90 invalid_inbox($ctx, $1) || r301($ctx, $1);
91 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
92 invalid_inbox($ctx, $1) || get_index($ctx);
93 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
94 invalid_inbox($ctx, $1) || get_atom($ctx);
95 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
96 invalid_inbox($ctx, $1) || get_new($ctx);
97 } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
98 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
99 my ($part, $path) = ($2, $3);
100 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
101 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
102 serve_mbox_range($ctx, $1, $2);
103 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
104 msg_page($ctx, $1, $2, $3);
106 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
107 my ($idx, $fn) = ($3, $4);
108 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
109 # in case people leave off the trailing slash:
110 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
111 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
112 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
113 r301($ctx, $inbox, $mid_ue, $suffix);
115 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
116 my ($inbox, $mid_ue) = ($1, $2);
117 r301($ctx, $inbox, $mid_ue, '#R');
119 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
121 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
122 get_text($ctx, $1, $2);
123 } elsif ($path_info =~ m!$INBOX_RE/([\w\-\.]+)\.css\z!o) {
124 get_css($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/([\w\.\-]+)\z!o) {
128 get_vcs_object($ctx, $1, $2, $3);
129 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
130 r301($ctx, $1, $2, 's/');
131 # convenience redirects order matters
132 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
136 legacy_redirects($ctx, $path_info);
140 # for CoW-friendliness, MOOOOO!
143 require PublicInbox::Feed;
144 require PublicInbox::View;
145 require PublicInbox::SearchThread;
146 require PublicInbox::MIME;
150 foreach (qw(PublicInbox::Search PublicInbox::SearchView
151 PublicInbox::Mbox IO::Compress::Gzip
152 PublicInbox::NewsWWW)) {
156 $self->stylesheets_prepare($_) for ('', '../', '../../');
160 # private functions below
164 if ($ctx && $ctx->{mid}) {
165 require PublicInbox::ExtMsg;
167 return PublicInbox::ExtMsg::ext_msg($ctx);
172 # simple response for errors
173 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
175 # returns undef if valid, array ref response if invalid
176 sub invalid_inbox ($$) {
177 my ($ctx, $inbox) = @_;
178 my $www = $ctx->{www};
179 my $obj = $www->{pi_config}->lookup_name($inbox);
181 $ctx->{git} = $obj->git;
182 $ctx->{-inbox} = $obj;
186 # sometimes linkifiers (not ours!) screw up automatic link
187 # generation and link things intended for nntp:// to https?://,
188 # so try to infer links and redirect them to the appropriate
190 $www->news_www->call($ctx->{env});
193 # returns undef if valid, array ref response if invalid
194 sub invalid_inbox_mid {
195 my ($ctx, $inbox, $mid_ue) = @_;
196 my $ret = invalid_inbox($ctx, $inbox);
199 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
200 my $ibx = $ctx->{-inbox};
201 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
202 my ($x2, $x38) = ($1, $2);
203 # this is horrifically wasteful for legacy URLs:
204 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
205 require Email::Simple;
206 my $s = Email::Simple->new($str);
207 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
208 return r301($ctx, $inbox, mid_escape($mid));
213 # /$INBOX/new.atom -> Atom feed, includes replies
216 require PublicInbox::Feed;
217 PublicInbox::Feed::generate($ctx);
220 # /$INBOX/new.html -> HTML only
223 require PublicInbox::Feed;
224 PublicInbox::Feed::new_html($ctx);
227 # /$INBOX/?r=$GIT_COMMIT -> HTML only
230 require PublicInbox::Feed;
232 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
233 require PublicInbox::SearchView;
234 PublicInbox::SearchView::sres_top_html($ctx);
236 PublicInbox::Feed::generate_html_index($ctx);
240 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
243 require PublicInbox::Mbox;
244 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
247 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
250 require PublicInbox::View;
252 PublicInbox::View::msg_page($ctx) || r404($ctx);
255 # /$INBOX/$MESSAGE_ID/t/
257 my ($ctx, $flat) = @_;
258 searcher($ctx) or return need_search($ctx);
259 $ctx->{flat} = $flat;
260 require PublicInbox::View;
261 PublicInbox::View::thread_html($ctx);
264 # /$INBOX/_/text/$KEY/
265 # /$INBOX/_/text/$KEY/raw
266 # KEY may contain slashes
268 my ($ctx, $inbox, $key) = @_;
269 my $r404 = invalid_inbox($ctx, $inbox);
270 return $r404 if $r404;
272 require PublicInbox::WwwText;
273 PublicInbox::WwwText::get_text($ctx, $key);
276 # show git objects (blobs and commits)
277 # /$INBOX/_/$OBJECT_ID/show
278 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
279 # KEY may contain slashes
280 sub get_vcs_object ($$$;$) {
281 my ($ctx, $inbox, $oid, $filename) = @_;
282 my $r404 = invalid_inbox($ctx, $inbox);
283 return $r404 if $r404;
284 require PublicInbox::ViewVCS;
285 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
289 my ($ctx, $key) = @_;
290 my $val = $ctx->{$key};
291 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
295 # search support is optional, returns undef if Xapian is not installed
296 # or not configured for the given GIT_DIR
300 require PublicInbox::Search;
301 $ctx->{srch} = $ctx->{-inbox}->search;
308 <html><head><title>Search not available for this
309 public-inbox</title><body><pre>Search is not available for this public-inbox
310 <a href="../">Return to index</a></pre></body></html>
312 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
315 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
316 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
317 # note: I'm not a big fan of other compression formats since they're
318 # significantly more expensive on CPU than gzip and less-widely available,
319 # especially on older systems. Stick to zlib since that's what git uses.
320 sub get_thread_mbox {
321 my ($ctx, $sfx) = @_;
322 my $srch = searcher($ctx) or return need_search($ctx);
323 require PublicInbox::Mbox;
324 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
328 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
329 sub get_thread_atom {
331 searcher($ctx) or return need_search($ctx);
332 require PublicInbox::Feed;
333 PublicInbox::Feed::generate_thread_atom($ctx);
336 sub legacy_redirects {
337 my ($ctx, $path_info) = @_;
339 # single-message pages
340 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
342 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
343 r301($ctx, $1, $2, 'raw');
345 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
349 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
350 r301($ctx, $1, $2, 't/#u');
352 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
353 r301($ctx, $1, $2, "t.mbox$3");
355 # even older legacy redirects
356 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
359 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
360 r301($ctx, $1, $2, 't/#u');
362 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
365 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
366 r301($ctx, $1, $2, 'raw');
368 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
369 r301($ctx, $1, $2, "t$3");
371 # legacy convenience redirects, order still matters
372 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
374 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
375 r301($ctx, $1, $2, 't/#u');
376 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
379 # some Message-IDs have slashes in them and the HTTP server
380 # may try to be clever and unescape them :<
381 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
382 msg_page($ctx, $1, $2, $3);
384 # in case people leave off the trailing slash:
385 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
386 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
387 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
390 $ctx->{www}->news_www->call($ctx->{env});
395 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
396 my $obj = $ctx->{-inbox};
398 my $r404 = invalid_inbox($ctx, $inbox);
399 return $r404 if $r404;
400 $obj = $ctx->{-inbox};
402 my $url = $obj->base_url($ctx->{env});
403 my $qs = $ctx->{env}->{QUERY_STRING};
404 if (defined $mid_ue) {
405 # common, and much nicer as '@' than '%40':
406 $mid_ue =~ s/%40/@/g;
407 $url .= $mid_ue . '/';
409 $url .= $suffix if (defined $suffix);
410 $url .= "?$qs" if $qs ne '';
413 [ Location => $url, 'Content-Type' => 'text/plain' ],
414 [ "Redirecting to $url\n" ] ]
418 my ($ctx, $inbox, $mid_ue, $e) = @_;
420 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
421 '' eq $e and return get_mid_html($ctx);
422 'T/' eq $e and return get_thread($ctx, 1);
423 't/' eq $e and return get_thread($ctx);
424 't.atom' eq $e and return get_thread_atom($ctx);
425 't.mbox' eq $e and return get_thread_mbox($ctx);
426 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
427 'raw' eq $e and return get_mid_txt($ctx);
429 # legacy, but no redirect for compatibility:
430 'f/' eq $e and return get_mid_html($ctx);
435 my ($ctx, $part, $path) = @_;
436 my $env = $ctx->{env};
437 my $ibx = $ctx->{-inbox};
438 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
439 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
444 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
445 searcher($ctx) or return need_search($ctx);
446 require PublicInbox::SearchView;
447 return PublicInbox::SearchView::mbox_results($ctx);
452 sub serve_mbox_range {
453 my ($ctx, $inbox, $range) = @_;
454 invalid_inbox($ctx, $inbox) || eval {
455 require PublicInbox::Mbox;
457 PublicInbox::Mbox::emit_range($ctx, $range);
463 $self->{news_www} ||= do {
464 require PublicInbox::NewsWWW;
465 PublicInbox::NewsWWW->new($self->{pi_config});
470 my ($ctx, $idx, $fn) = @_;
471 require PublicInbox::WwwAttach;
472 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
475 # User-generated content (UGC) may have excessively long lines
476 # and screw up rendering on some browsers, so we use pre-wrap.
478 # We also force everything to the same scaled font-size because GUI
479 # browsers (tested both Firefox and surf (webkit)) uses a larger font
480 # for the Search <form> element than the rest of the page. Font size
481 # uniformity is important to people who rely on gigantic fonts.
482 # Finally, we use monospace to ensure the Search field and button
483 # has the same size and spacing as everything else which is
484 # <pre>-formatted anyways.
485 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
487 sub stylesheets_prepare ($$) {
488 my ($self, $upfx) = @_;
490 require CSS::Minifier;
491 sub { CSS::Minifier::minify(input => $_[0]) };
493 require CSS::Minifier::XS;
494 sub { CSS::Minifier::XS::minify($_[0]) };
498 my $stylesheets = $self->{pi_config}->{css} || [];
502 foreach my $s (@$stylesheets) {
505 foreach my $k (qw(media title href)) {
506 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
511 if (defined $attr->{href}) {
514 open(my $fh, '<', $_) or do {
515 warn "failed to open $_: $!\n";
518 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
520 my $local = do { local $/; <$fh> };
521 if ($local =~ /\S/) {
522 $ctime = sprintf('%x',(stat($fh))[10]);
523 $local = $mini->($local);
525 $css_map->{$key} = $local;
526 $attr->{href} = "$upfx$key.css?$ctime";
527 if (defined($attr->{title})) {
529 } elsif (($attr->{media}||'screen') eq 'screen') {
530 $attr->{-inline} = $local;
536 my $buf = "<style>$STYLE";
538 my @ext; # for media=print and whatnot
539 foreach my $attr (@$links) {
540 if (defined(my $str = delete $attr->{-inline})) {
551 foreach my $attr (@$links) {
552 delete $attr->{-inline};
553 $buf .= "<link\ntype=text/css\nrel=stylesheet";
554 while (my ($k, $v) = each %$attr) {
555 $v = qq{"$v"} if $v =~ /[\s=]/;
560 $self->{"-style-$upfx"} = $buf;
562 $self->{-style_inline} = $buf;
564 $self->{-css_map} = $css_map;
567 # returns an HTML fragment with <style> or <link> tags in them
568 # Called by WwwStream by nearly every HTML page
570 my ($self, $upfx) = @_;
571 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
572 stylesheets_prepare($self, $upfx);
573 $self->{-style_inline} || $self->{"-style-$upfx"}
577 # /$INBOX/$KEY.css endpoint
578 # CSS is configured globally for all inboxes, but we access them on
579 # a per-inbox basis. This allows administrators to setup per-inbox
580 # static routes to intercept the request before it hits PSGI
582 my ($ctx, $inbox, $key) = @_;
583 my $r404 = invalid_inbox($ctx, $inbox);
584 return $r404 if $r404;
585 my $self = $ctx->{www};
586 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
587 my $css = $css_map->{$key};
588 if (!defined($css) && $key eq 'userContent') {
589 my $env = $ctx->{env};
590 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
592 defined $css or return r404();
593 my $h = [ 'Content-Length', bytes::length($css),
594 'Content-Type', 'text/css' ];
595 PublicInbox::GitHTTPBackend::cache_one_year($h);
596 [ 200, $h, [ $css ] ];