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;
24 # TODO: consider a routing tree now that we have more endpoints:
25 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
26 our $MID_RE = qr!([^/]+)!;
27 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
28 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
29 our $OID_RE = qr![a-f0-9]{7,40}!;
32 my ($class, $pi_config) = @_;
33 $pi_config ||= PublicInbox::Config->new;
34 bless { pi_config => $pi_config }, $class;
37 # backwards compatibility, do not use
39 my ($req, $method) = @_;
40 PublicInbox::WWW->new->call($req->env);
46 my $sn = $_[0]->{SCRIPT_NAME};
47 $path_re_cache{$sn} ||= do {
48 $sn = '/'.$sn unless index($sn, '/') == 0;
50 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
55 my ($self, $env) = @_;
56 my $ctx = { env => $env, www => $self };
58 # we don't care about multi-value
61 my ($k, $v) = split('=', uri_unescape($_), 2);
62 $v = '' unless defined $v;
65 } split(/[&;]+/, $env->{QUERY_STRING});
68 # not using $env->{PATH_INFO} here since that's already decoded
69 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
70 my $method = $env->{REQUEST_METHOD};
72 if ($method eq 'POST') {
73 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
74 my ($part, $path) = ($2, $3);
75 return invalid_inbox($ctx, $1) ||
76 serve_git($ctx, $part, $path);
77 } elsif ($path_info =~ m!$INBOX_RE/!o) {
78 return invalid_inbox($ctx, $1) || mbox_results($ctx);
81 elsif ($method !~ /\AGET|HEAD\z/) {
82 return r(405, 'Method Not Allowed');
85 # top-level indices and feeds
86 if ($path_info eq '/') {
88 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
89 invalid_inbox($ctx, $1) || r301($ctx, $1);
90 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
91 invalid_inbox($ctx, $1) || get_index($ctx);
92 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
93 invalid_inbox($ctx, $1) || get_atom($ctx);
94 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
95 invalid_inbox($ctx, $1) || get_new($ctx);
96 } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
97 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
98 my ($part, $path) = ($2, $3);
99 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
100 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
101 serve_mbox_range($ctx, $1, $2);
102 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
103 msg_page($ctx, $1, $2, $3);
105 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
106 my ($idx, $fn) = ($3, $4);
107 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
108 # in case people leave off the trailing slash:
109 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
110 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
111 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
112 r301($ctx, $inbox, $mid_ue, $suffix);
114 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
115 my ($inbox, $mid_ue) = ($1, $2);
116 r301($ctx, $inbox, $mid_ue, '#R');
118 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
120 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
121 get_text($ctx, $1, $2);
122 } elsif ($path_info =~ m!$INBOX_RE/([\w\-\.]+)\.css\z!o) {
124 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/\z!o) {
125 get_vcs_object($ctx, $1, $2);
126 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s/([\w\.\-]+)\z!o) {
127 get_vcs_object($ctx, $1, $2, $3);
128 } elsif ($path_info =~ m!$INBOX_RE/($OID_RE)/s\z!o) {
129 r301($ctx, $1, $2, 's/');
130 # convenience redirects order matters
131 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
135 legacy_redirects($ctx, $path_info);
139 # for CoW-friendliness, MOOOOO!
142 require PublicInbox::Feed;
143 require PublicInbox::View;
144 require PublicInbox::SearchThread;
145 require PublicInbox::MIME;
149 foreach (qw(PublicInbox::Search PublicInbox::SearchView
150 PublicInbox::Mbox IO::Compress::Gzip
151 PublicInbox::NewsWWW)) {
155 $self->stylesheets_prepare($_) for ('', '../', '../../');
159 # private functions below
163 if ($ctx && $ctx->{mid}) {
164 require PublicInbox::ExtMsg;
166 return PublicInbox::ExtMsg::ext_msg($ctx);
171 # simple response for errors
172 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
174 # returns undef if valid, array ref response if invalid
175 sub invalid_inbox ($$) {
176 my ($ctx, $inbox) = @_;
177 my $www = $ctx->{www};
178 my $obj = $www->{pi_config}->lookup_name($inbox);
180 $ctx->{git} = $obj->git;
181 $ctx->{-inbox} = $obj;
185 # sometimes linkifiers (not ours!) screw up automatic link
186 # generation and link things intended for nntp:// to https?://,
187 # so try to infer links and redirect them to the appropriate
189 $www->news_www->call($ctx->{env});
192 # returns undef if valid, array ref response if invalid
193 sub invalid_inbox_mid {
194 my ($ctx, $inbox, $mid_ue) = @_;
195 my $ret = invalid_inbox($ctx, $inbox);
198 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
199 my $ibx = $ctx->{-inbox};
200 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
201 my ($x2, $x38) = ($1, $2);
202 # this is horrifically wasteful for legacy URLs:
203 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
204 require Email::Simple;
205 my $s = Email::Simple->new($str);
206 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
207 return r301($ctx, $inbox, mid_escape($mid));
212 # /$INBOX/new.atom -> Atom feed, includes replies
215 require PublicInbox::Feed;
216 PublicInbox::Feed::generate($ctx);
219 # /$INBOX/new.html -> HTML only
222 require PublicInbox::Feed;
223 PublicInbox::Feed::new_html($ctx);
226 # /$INBOX/?r=$GIT_COMMIT -> HTML only
229 require PublicInbox::Feed;
231 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
232 require PublicInbox::SearchView;
233 PublicInbox::SearchView::sres_top_html($ctx);
235 PublicInbox::Feed::generate_html_index($ctx);
239 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
242 require PublicInbox::Mbox;
243 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
246 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
249 require PublicInbox::View;
251 PublicInbox::View::msg_page($ctx) || r404($ctx);
254 # /$INBOX/$MESSAGE_ID/t/
256 my ($ctx, $flat) = @_;
257 searcher($ctx) or return need_search($ctx);
258 $ctx->{flat} = $flat;
259 require PublicInbox::View;
260 PublicInbox::View::thread_html($ctx);
263 # /$INBOX/_/text/$KEY/
264 # /$INBOX/_/text/$KEY/raw
265 # KEY may contain slashes
267 my ($ctx, $inbox, $key) = @_;
268 my $r404 = invalid_inbox($ctx, $inbox);
269 return $r404 if $r404;
271 require PublicInbox::WwwText;
272 PublicInbox::WwwText::get_text($ctx, $key);
275 # show git objects (blobs and commits)
276 # /$INBOX/_/$OBJECT_ID/show
277 # /$INBOX/_/${OBJECT_ID}_${FILENAME}
278 # KEY may contain slashes
279 sub get_vcs_object ($$$;$) {
280 my ($ctx, $inbox, $oid, $filename) = @_;
281 my $r404 = invalid_inbox($ctx, $inbox);
282 return $r404 if $r404;
283 require PublicInbox::ViewVCS;
284 PublicInbox::ViewVCS::show($ctx, $oid, $filename);
288 my ($ctx, $key) = @_;
289 my $val = $ctx->{$key};
290 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
294 # search support is optional, returns undef if Xapian is not installed
295 # or not configured for the given GIT_DIR
299 require PublicInbox::Search;
300 $ctx->{srch} = $ctx->{-inbox}->search;
307 <html><head><title>Search not available for this
308 public-inbox</title><body><pre>Search is not available for this public-inbox
309 <a href="../">Return to index</a></pre></body></html>
311 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
314 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
315 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
316 # note: I'm not a big fan of other compression formats since they're
317 # significantly more expensive on CPU than gzip and less-widely available,
318 # especially on older systems. Stick to zlib since that's what git uses.
319 sub get_thread_mbox {
320 my ($ctx, $sfx) = @_;
321 my $srch = searcher($ctx) or return need_search($ctx);
322 require PublicInbox::Mbox;
323 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
327 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
328 sub get_thread_atom {
330 searcher($ctx) or return need_search($ctx);
331 require PublicInbox::Feed;
332 PublicInbox::Feed::generate_thread_atom($ctx);
335 sub legacy_redirects {
336 my ($ctx, $path_info) = @_;
338 # single-message pages
339 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
341 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
342 r301($ctx, $1, $2, 'raw');
344 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
348 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
349 r301($ctx, $1, $2, 't/#u');
351 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
352 r301($ctx, $1, $2, "t.mbox$3");
354 # even older legacy redirects
355 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
358 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
359 r301($ctx, $1, $2, 't/#u');
361 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
364 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
365 r301($ctx, $1, $2, 'raw');
367 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
368 r301($ctx, $1, $2, "t$3");
370 # legacy convenience redirects, order still matters
371 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
373 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
374 r301($ctx, $1, $2, 't/#u');
375 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
378 # some Message-IDs have slashes in them and the HTTP server
379 # may try to be clever and unescape them :<
380 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
381 msg_page($ctx, $1, $2, $3);
383 # in case people leave off the trailing slash:
384 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
385 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
386 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
389 $ctx->{www}->news_www->call($ctx->{env});
394 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
395 my $obj = $ctx->{-inbox};
397 my $r404 = invalid_inbox($ctx, $inbox);
398 return $r404 if $r404;
399 $obj = $ctx->{-inbox};
401 my $url = $obj->base_url($ctx->{env});
402 my $qs = $ctx->{env}->{QUERY_STRING};
403 if (defined $mid_ue) {
404 # common, and much nicer as '@' than '%40':
405 $mid_ue =~ s/%40/@/g;
406 $url .= $mid_ue . '/';
408 $url .= $suffix if (defined $suffix);
409 $url .= "?$qs" if $qs ne '';
412 [ Location => $url, 'Content-Type' => 'text/plain' ],
413 [ "Redirecting to $url\n" ] ]
417 my ($ctx, $inbox, $mid_ue, $e) = @_;
419 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
420 '' eq $e and return get_mid_html($ctx);
421 'T/' eq $e and return get_thread($ctx, 1);
422 't/' eq $e and return get_thread($ctx);
423 't.atom' eq $e and return get_thread_atom($ctx);
424 't.mbox' eq $e and return get_thread_mbox($ctx);
425 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
426 'raw' eq $e and return get_mid_txt($ctx);
428 # legacy, but no redirect for compatibility:
429 'f/' eq $e and return get_mid_html($ctx);
434 my ($ctx, $part, $path) = @_;
435 my $env = $ctx->{env};
436 my $ibx = $ctx->{-inbox};
437 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
438 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
443 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
444 searcher($ctx) or return need_search($ctx);
445 require PublicInbox::SearchView;
446 return PublicInbox::SearchView::mbox_results($ctx);
451 sub serve_mbox_range {
452 my ($ctx, $inbox, $range) = @_;
453 invalid_inbox($ctx, $inbox) || eval {
454 require PublicInbox::Mbox;
456 PublicInbox::Mbox::emit_range($ctx, $range);
462 $self->{news_www} ||= do {
463 require PublicInbox::NewsWWW;
464 PublicInbox::NewsWWW->new($self->{pi_config});
469 my ($ctx, $idx, $fn) = @_;
470 require PublicInbox::WwwAttach;
471 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
474 # User-generated content (UGC) may have excessively long lines
475 # and screw up rendering on some browsers, so we use pre-wrap.
477 # We also force everything to the same scaled font-size because GUI
478 # browsers (tested both Firefox and surf (webkit)) uses a larger font
479 # for the Search <form> element than the rest of the page. Font size
480 # uniformity is important to people who rely on gigantic fonts.
481 # Finally, we use monospace to ensure the Search field and button
482 # has the same size and spacing as everything else which is
483 # <pre>-formatted anyways.
484 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
486 sub stylesheets_prepare ($$) {
487 my ($self, $upfx) = @_;
489 require CSS::Minifier;
490 sub { CSS::Minifier::minify(input => $_[0]) };
492 require CSS::Minifier::XS;
493 sub { CSS::Minifier::XS::minify($_[0]) };
497 my $stylesheets = $self->{pi_config}->{css} || [];
501 foreach my $s (@$stylesheets) {
504 foreach my $k (qw(media title href)) {
505 if (s/\s*$k='([^']+)'// || s/\s*$k=(\S+)//) {
510 if (defined $attr->{href}) {
513 open(my $fh, '<', $_) or do {
514 warn "failed to open $_: $!\n";
517 my ($key) = (m!([^/]+?)(?:\.css)?\z!i);
519 my $local = do { local $/; <$fh> };
520 if ($local =~ /\S/) {
521 $ctime = sprintf('%x',(stat($fh))[10]);
522 $local = $mini->($local);
524 $css_map->{$key} = $local;
525 $attr->{href} = "$upfx$key.css?$ctime";
526 if (defined($attr->{title})) {
528 } elsif (($attr->{media}||'screen') eq 'screen') {
529 $attr->{-inline} = $local;
535 my $buf = "<style>$STYLE";
537 my @ext; # for media=print and whatnot
538 foreach my $attr (@$links) {
539 if (defined(my $str = delete $attr->{-inline})) {
550 foreach my $attr (@$links) {
551 delete $attr->{-inline};
552 $buf .= "<link\ntype=text/css\nrel=stylesheet";
553 while (my ($k, $v) = each %$attr) {
554 $v = qq{"$v"} if $v =~ /[\s=]/;
559 $self->{"-style-$upfx"} = $buf;
561 $self->{-style_inline} = $buf;
563 $self->{-css_map} = $css_map;
566 # returns an HTML fragment with <style> or <link> tags in them
567 # Called by WwwStream by nearly every HTML page
569 my ($self, $upfx) = @_;
570 $self->{-style_inline} || $self->{"-style-$upfx"} || do {
571 stylesheets_prepare($self, $upfx);
572 $self->{-style_inline} || $self->{"-style-$upfx"}
576 # /$INBOX/$KEY.css endpoint
577 # CSS is configured globally for all inboxes, but we access them on
578 # a per-inbox basis. This allows administrators to setup per-inbox
579 # static routes to intercept the request before it hits PSGI
581 my ($self, $key) = @_;
582 my $css_map = $self->{-css_map} || stylesheets_prepare($self, '');
583 defined(my $css = $css_map->{$key}) or return r404();
584 my $h = [ 'Content-Length', bytes::length($css),
585 'Content-Type', 'text/css' ];
586 PublicInbox::GitHTTPBackend::cache_one_year($h);
587 [ 200, $h, [ $css ] ];