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
18 use PublicInbox::Config;
19 use PublicInbox::Hval;
20 use URI::Escape qw(uri_unescape);
21 use PublicInbox::MID qw(mid_escape);
22 require PublicInbox::Git;
23 use PublicInbox::GitHTTPBackend;
24 use PublicInbox::UserContent;
26 # TODO: consider a routing tree now that we have more endpoints:
27 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
28 our $MID_RE = qr!([^/]+)!;
29 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
30 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
31 our $OID_RE = qr![a-f0-9]{7,40}!;
34 my ($class, $pi_config) = @_;
35 $pi_config ||= PublicInbox::Config->new;
36 bless { pi_config => $pi_config }, $class;
39 # backwards compatibility, do not use
41 my ($req, $method) = @_;
42 PublicInbox::WWW->new->call($req->env);
48 my $sn = $_[0]->{SCRIPT_NAME};
49 $path_re_cache{$sn} ||= do {
50 $sn = '/'.$sn unless index($sn, '/') == 0;
52 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
57 my ($self, $env) = @_;
58 my $ctx = { env => $env, www => $self };
60 # we don't care about multi-value
63 my ($k, $v) = split('=', uri_unescape($_), 2);
64 $v = '' unless defined $v;
67 } split(/[&;]+/, $env->{QUERY_STRING});
70 # not using $env->{PATH_INFO} here since that's already decoded
71 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
72 my $method = $env->{REQUEST_METHOD};
74 if ($method eq 'POST') {
75 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
76 my ($part, $path) = ($2, $3);
77 return invalid_inbox($ctx, $1) ||
78 serve_git($ctx, $part, $path);
79 } elsif ($path_info =~ m!$INBOX_RE/!o) {
80 return invalid_inbox($ctx, $1) || mbox_results($ctx);
83 elsif ($method !~ /\AGET|HEAD\z/) {
84 return r(405, 'Method Not Allowed');
87 # top-level indices and feeds
88 if ($path_info eq '/') {
90 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
91 invalid_inbox($ctx, $1) || r301($ctx, $1);
92 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
93 invalid_inbox($ctx, $1) || get_index($ctx);
94 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
95 invalid_inbox($ctx, $1) || get_atom($ctx);
96 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
97 invalid_inbox($ctx, $1) || get_new($ctx);
98 } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
99 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
100 my ($part, $path) = ($2, $3);
101 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
102 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
103 serve_mbox_range($ctx, $1, $2);
104 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
105 msg_page($ctx, $1, $2, $3);
107 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
108 my ($idx, $fn) = ($3, $4);
109 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
110 # in case people leave off the trailing slash:
111 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
112 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
113 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
114 r301($ctx, $inbox, $mid_ue, $suffix);
116 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
117 my ($inbox, $mid_ue) = ($1, $2);
118 r301($ctx, $inbox, $mid_ue, '#R');
120 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
122 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
123 get_text($ctx, $1, $2);
124 } elsif ($path_info =~ m!$INBOX_RE/([\w\-\.]+)\.css\z!o) {
125 get_css($ctx, $1, $2);
126 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
127 get_vcs_object($ctx, $1, $2);
128 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/([\w\.\-]+)\z!o) {
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 # convenience redirects order matters
133 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
137 legacy_redirects($ctx, $path_info);
141 # for CoW-friendliness, MOOOOO!
144 require PublicInbox::Feed;
145 require PublicInbox::View;
146 require PublicInbox::SearchThread;
147 require PublicInbox::MIME;
151 foreach (qw(PublicInbox::Search PublicInbox::SearchView
152 PublicInbox::Mbox IO::Compress::Gzip
153 PublicInbox::NewsWWW)) {
157 $self->stylesheets_prepare($_) for ('', '../', '../../');
161 # private functions below
165 if ($ctx && $ctx->{mid}) {
166 require PublicInbox::ExtMsg;
168 return PublicInbox::ExtMsg::ext_msg($ctx);
173 # simple response for errors
174 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
176 # returns undef if valid, array ref response if invalid
177 sub invalid_inbox ($$) {
178 my ($ctx, $inbox) = @_;
179 my $www = $ctx->{www};
180 my $obj = $www->{pi_config}->lookup_name($inbox);
182 $ctx->{git} = $obj->git;
183 $ctx->{-inbox} = $obj;
187 # sometimes linkifiers (not ours!) screw up automatic link
188 # generation and link things intended for nntp:// to https?://,
189 # so try to infer links and redirect them to the appropriate
191 $www->news_www->call($ctx->{env});
194 # returns undef if valid, array ref response if invalid
195 sub invalid_inbox_mid {
196 my ($ctx, $inbox, $mid_ue) = @_;
197 my $ret = invalid_inbox($ctx, $inbox);
200 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
201 my $ibx = $ctx->{-inbox};
202 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
203 my ($x2, $x38) = ($1, $2);
204 # this is horrifically wasteful for legacy URLs:
205 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
206 require Email::Simple;
207 my $s = Email::Simple->new($str);
208 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
209 return r301($ctx, $inbox, mid_escape($mid));
214 # /$INBOX/new.atom -> Atom feed, includes replies
217 require PublicInbox::Feed;
218 PublicInbox::Feed::generate($ctx);
221 # /$INBOX/new.html -> HTML only
224 require PublicInbox::Feed;
225 PublicInbox::Feed::new_html($ctx);
228 # /$INBOX/?r=$GIT_COMMIT -> HTML only
231 require PublicInbox::Feed;
233 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
234 require PublicInbox::SearchView;
235 PublicInbox::SearchView::sres_top_html($ctx);
237 PublicInbox::Feed::generate_html_index($ctx);
241 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
244 require PublicInbox::Mbox;
245 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
248 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
251 require PublicInbox::View;
253 PublicInbox::View::msg_page($ctx) || r404($ctx);
256 # /$INBOX/$MESSAGE_ID/t/
258 my ($ctx, $flat) = @_;
259 searcher($ctx) or return need_search($ctx);
260 $ctx->{flat} = $flat;
261 require PublicInbox::View;
262 PublicInbox::View::thread_html($ctx);
265 # /$INBOX/_/text/$KEY/
266 # /$INBOX/_/text/$KEY/raw
267 # KEY may contain slashes
269 my ($ctx, $inbox, $key) = @_;
270 my $r404 = invalid_inbox($ctx, $inbox);
271 return $r404 if $r404;
273 require PublicInbox::WwwText;
274 PublicInbox::WwwText::get_text($ctx, $key);
277 # show git objects (blobs and commits)
278 # /$INBOX/_/$OBJECT_ID/show
279 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
280 # KEY may contain slashes
281 sub get_vcs_object ($$$;$) {
282 my ($ctx, $inbox, $oid, $filename) = @_;
283 my $r404 = invalid_inbox($ctx, $inbox);
284 return $r404 if $r404;
285 require PublicInbox::ViewVCS;
286 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
290 my ($ctx, $key) = @_;
291 my $val = $ctx->{$key};
292 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
296 # search support is optional, returns undef if Xapian is not installed
297 # or not configured for the given GIT_DIR
301 require PublicInbox::Search;
302 $ctx->{srch} = $ctx->{-inbox}->search;
309 <html><head><title>Search not available for this
310 public-inbox</title><body><pre>Search is not available for this public-inbox
311 <a href="../">Return to index</a></pre></body></html>
313 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
316 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
317 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
318 # note: I'm not a big fan of other compression formats since they're
319 # significantly more expensive on CPU than gzip and less-widely available,
320 # especially on older systems. Stick to zlib since that's what git uses.
321 sub get_thread_mbox {
322 my ($ctx, $sfx) = @_;
323 my $srch = searcher($ctx) or return need_search($ctx);
324 require PublicInbox::Mbox;
325 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
329 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
330 sub get_thread_atom {
332 searcher($ctx) or return need_search($ctx);
333 require PublicInbox::Feed;
334 PublicInbox::Feed::generate_thread_atom($ctx);
337 sub legacy_redirects {
338 my ($ctx, $path_info) = @_;
340 # single-message pages
341 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
343 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
344 r301($ctx, $1, $2, 'raw');
346 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
350 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
351 r301($ctx, $1, $2, 't/#u');
353 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
354 r301($ctx, $1, $2, "t.mbox$3");
356 # even older legacy redirects
357 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
360 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
361 r301($ctx, $1, $2, 't/#u');
363 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
366 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
367 r301($ctx, $1, $2, 'raw');
369 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
370 r301($ctx, $1, $2, "t$3");
372 # legacy convenience redirects, order still matters
373 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
375 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
376 r301($ctx, $1, $2, 't/#u');
377 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
380 # some Message-IDs have slashes in them and the HTTP server
381 # may try to be clever and unescape them :<
382 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
383 msg_page($ctx, $1, $2, $3);
385 # in case people leave off the trailing slash:
386 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
387 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
388 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
391 $ctx->{www}->news_www->call($ctx->{env});
396 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
397 my $obj = $ctx->{-inbox};
399 my $r404 = invalid_inbox($ctx, $inbox);
400 return $r404 if $r404;
401 $obj = $ctx->{-inbox};
403 my $url = $obj->base_url($ctx->{env});
404 my $qs = $ctx->{env}->{QUERY_STRING};
405 if (defined $mid_ue) {
406 # common, and much nicer as '@' than '%40':
407 $mid_ue =~ s/%40/@/g;
408 $url .= $mid_ue . '/';
410 $url .= $suffix if (defined $suffix);
411 $url .= "?$qs" if $qs ne '';
414 [ Location => $url, 'Content-Type' => 'text/plain' ],
415 [ "Redirecting to $url\n" ] ]
419 my ($ctx, $inbox, $mid_ue, $e) = @_;
421 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
422 '' eq $e and return get_mid_html($ctx);
423 'T/' eq $e and return get_thread($ctx, 1);
424 't/' eq $e and return get_thread($ctx);
425 't.atom' eq $e and return get_thread_atom($ctx);
426 't.mbox' eq $e and return get_thread_mbox($ctx);
427 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
428 'raw' eq $e and return get_mid_txt($ctx);
430 # legacy, but no redirect for compatibility:
431 'f/' eq $e and return get_mid_html($ctx);
436 my ($ctx, $part, $path) = @_;
437 my $env = $ctx->{env};
438 my $ibx = $ctx->{-inbox};
439 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
440 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
445 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
446 searcher($ctx) or return need_search($ctx);
447 require PublicInbox::SearchView;
448 return PublicInbox::SearchView::mbox_results($ctx);
453 sub serve_mbox_range {
454 my ($ctx, $inbox, $range) = @_;
455 invalid_inbox($ctx, $inbox) || eval {
456 require PublicInbox::Mbox;
458 PublicInbox::Mbox::emit_range($ctx, $range);
464 $self->{news_www} ||= do {
465 require PublicInbox::NewsWWW;
466 PublicInbox::NewsWWW->new($self->{pi_config});
471 my ($ctx, $idx, $fn) = @_;
472 require PublicInbox::WwwAttach;
473 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
476 # User-generated content (UGC) may have excessively long lines
477 # and screw up rendering on some browsers, so we use pre-wrap.
479 # We also force everything to the same scaled font-size because GUI
480 # browsers (tested both Firefox and surf (webkit)) uses a larger font
481 # for the Search <form> element than the rest of the page. Font size
482 # uniformity is important to people who rely on gigantic fonts.
483 # Finally, we use monospace to ensure the Search field and button
484 # has the same size and spacing as everything else which is
485 # <pre>-formatted anyways.
486 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
488 sub stylesheets_prepare ($$) {
489 my ($self, $upfx) = @_;
491 require CSS::Minifier;
492 sub { CSS::Minifier::minify(input => $_[0]) };
494 require CSS::Minifier::XS;
495 sub { CSS::Minifier::XS::minify($_[0]) };
499 my $stylesheets = $self->{pi_config}->{css} || [];
503 foreach my $s (@$stylesheets) {
506 foreach my $k (qw(media title href)) {
507 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
512 if (defined $attr->{href}) {
516 open(my $fh, '<', $fn) or do {
517 warn "failed to open $fn: $!\n";
520 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
522 my $local = do { local $/; <$fh> };
523 if ($local =~ /\S/) {
524 $ctime = sprintf('%x',(stat($fh))[10]);
525 $local = $mini->($local);
528 # do not let BOFHs override userContent.css:
529 if ($local =~ /!\s*important\b/i) {
530 warn "ignoring $fn since it uses `!important'\n";
534 $css_map->{$key} = $local;
535 $attr->{href} = "$upfx$key.css?$ctime";
536 if (defined($attr->{title})) {
538 } elsif (($attr->{media}||'screen') eq 'screen') {
539 $attr->{-inline} = $local;
545 my $buf = "<style>$STYLE";
547 my @ext; # for media=print and whatnot
548 foreach my $attr (@$links) {
549 if (defined(my $str = delete $attr->{-inline})) {
560 foreach my $attr (@$links) {
561 delete $attr->{-inline};
562 $buf .= "<link\ntype=text/css\nrel=stylesheet";
563 while (my ($k, $v) = each %$attr) {
564 $v = qq{"$v"} if $v =~ /[\s=]/;
569 $self->{"-style-$upfx"} = $buf;
571 $self->{-style_inline} = $buf;
573 $self->{-css_map} = $css_map;
576 # returns an HTML fragment with <style> or <link> tags in them
577 # Called by WwwStream by nearly every HTML page
579 my ($self, $upfx) = @_;
580 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
581 stylesheets_prepare($self, $upfx);
582 $self->{-style_inline} || $self->{"-style-$upfx"}
586 # /$INBOX/$KEY.css endpoint
587 # CSS is configured globally for all inboxes, but we access them on
588 # a per-inbox basis. This allows administrators to setup per-inbox
589 # static routes to intercept the request before it hits PSGI
591 my ($ctx, $inbox, $key) = @_;
592 my $r404 = invalid_inbox($ctx, $inbox);
593 return $r404 if $r404;
594 my $self = $ctx->{www};
595 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
596 my $css = $css_map->{$key};
597 if (!defined($css) && $key eq 'userContent') {
598 my $env = $ctx->{env};
599 $css = PublicInbox::UserContent::sample($ctx->{-inbox}, $env);
601 defined $css or return r404();
602 my $h = [ 'Content-Length', bytes::length($css),
603 'Content-Type', 'text/css' ];
604 PublicInbox::GitHTTPBackend::cache_one_year($h);
605 [ 200, $h, [ $css ] ];