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 # - No JavaScript, graphics or icons allowed.
10 # - Must not rely on static content
11 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
12 package PublicInbox::WWW;
16 use PublicInbox::Config;
17 use PublicInbox::Hval;
18 use URI::Escape qw(uri_unescape);
19 use PublicInbox::MID qw(mid_escape);
20 require PublicInbox::Git;
21 use PublicInbox::GitHTTPBackend;
22 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
23 our $MID_RE = qr!([^/]+)!;
24 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
25 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
28 my ($class, $pi_config) = @_;
29 $pi_config ||= PublicInbox::Config->new;
30 bless { pi_config => $pi_config }, $class;
33 # backwards compatibility, do not use
35 my ($req, $method) = @_;
36 PublicInbox::WWW->new->call($req->env);
40 my ($self, $env) = @_;
41 my $ctx = { env => $env, www => $self };
43 # we don't care about multi-value
46 my ($k, $v) = split('=', uri_unescape($_), 2);
47 $v = '' unless defined $v;
50 } split(/[&;]+/, $env->{QUERY_STRING});
53 my $path_info = $env->{PATH_INFO};
54 my $method = $env->{REQUEST_METHOD};
56 if ($method eq 'POST') {
57 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
58 my ($part, $path) = ($2, $3);
59 return invalid_inbox($ctx, $1) ||
60 serve_git($ctx, $part, $path);
61 } elsif ($path_info =~ m!$INBOX_RE/!o) {
62 return invalid_inbox($ctx, $1) || mbox_results($ctx);
65 elsif ($method !~ /\AGET|HEAD\z/) {
66 return r(405, 'Method Not Allowed');
69 # top-level indices and feeds
70 if ($path_info eq '/') {
72 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
73 invalid_inbox($ctx, $1) || r301($ctx, $1);
74 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
75 invalid_inbox($ctx, $1) || get_index($ctx);
76 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
77 invalid_inbox($ctx, $1) || get_atom($ctx);
78 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
79 invalid_inbox($ctx, $1) || get_new($ctx);
80 } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
81 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
82 my ($part, $path) = ($2, $3);
83 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
84 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
85 serve_mbox_range($ctx, $1, $2);
86 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
87 msg_page($ctx, $1, $2, $3);
89 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
90 my ($idx, $fn) = ($3, $4);
91 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
92 # in case people leave off the trailing slash:
93 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
94 my ($inbox, $mid, $suffix) = ($1, $2, $3);
95 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
96 r301($ctx, $inbox, $mid, $suffix);
98 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
99 my ($inbox, $mid) = ($1, $2);
100 r301($ctx, $inbox, $mid, '#R');
102 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
104 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
105 get_text($ctx, $1, $2);
107 # convenience redirects order matters
108 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
112 legacy_redirects($ctx, $path_info);
116 # for CoW-friendliness, MOOOOO!
118 require PublicInbox::Feed;
119 require PublicInbox::View;
120 require PublicInbox::SearchThread;
121 require PublicInbox::MIME;
125 foreach (qw(PublicInbox::Search PublicInbox::SearchView
126 PublicInbox::Mbox IO::Compress::Gzip
127 PublicInbox::NewsWWW)) {
132 # private functions below
136 if ($ctx && $ctx->{mid}) {
137 require PublicInbox::ExtMsg;
139 return PublicInbox::ExtMsg::ext_msg($ctx);
144 # simple response for errors
145 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
147 # returns undef if valid, array ref response if invalid
148 sub invalid_inbox ($$) {
149 my ($ctx, $inbox) = @_;
150 my $www = $ctx->{www};
151 my $obj = $www->{pi_config}->lookup_name($inbox);
153 $ctx->{git} = $obj->git;
154 $ctx->{-inbox} = $obj;
158 # sometimes linkifiers (not ours!) screw up automatic link
159 # generation and link things intended for nntp:// to https?://,
160 # so try to infer links and redirect them to the appropriate
162 $www->news_www->call($ctx->{env});
165 # returns undef if valid, array ref response if invalid
166 sub invalid_inbox_mid {
167 my ($ctx, $inbox, $mid) = @_;
168 my $ret = invalid_inbox($ctx, $inbox);
172 my $ibx = $ctx->{-inbox};
173 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
174 my ($x2, $x38) = ($1, $2);
175 # this is horrifically wasteful for legacy URLs:
176 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
177 require Email::Simple;
178 my $s = Email::Simple->new($str);
179 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
180 return r301($ctx, $inbox, $mid);
185 # /$INBOX/new.atom -> Atom feed, includes replies
188 require PublicInbox::Feed;
189 PublicInbox::Feed::generate($ctx);
192 # /$INBOX/new.html -> HTML only
195 require PublicInbox::Feed;
196 PublicInbox::Feed::new_html($ctx);
199 # /$INBOX/?r=$GIT_COMMIT -> HTML only
202 require PublicInbox::Feed;
204 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
205 require PublicInbox::SearchView;
206 PublicInbox::SearchView::sres_top_html($ctx);
208 PublicInbox::Feed::generate_html_index($ctx);
212 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
215 require PublicInbox::Mbox;
216 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
219 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
222 require PublicInbox::View;
224 PublicInbox::View::msg_page($ctx) || r404($ctx);
227 # /$INBOX/$MESSAGE_ID/t/
229 my ($ctx, $flat) = @_;
230 searcher($ctx) or return need_search($ctx);
231 $ctx->{flat} = $flat;
232 require PublicInbox::View;
233 PublicInbox::View::thread_html($ctx);
236 # /$INBOX/_/text/$KEY/
237 # /$INBOX/_/text/$KEY/raw
238 # KEY may contain slashes
240 my ($ctx, $inbox, $key) = @_;
241 my $r404 = invalid_inbox($ctx, $inbox);
242 return $r404 if $r404;
244 require PublicInbox::WwwText;
245 PublicInbox::WwwText::get_text($ctx, $key);
249 my ($ctx, $key) = @_;
250 my $val = $ctx->{$key};
251 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
255 # search support is optional, returns undef if Xapian is not installed
256 # or not configured for the given GIT_DIR
260 require PublicInbox::Search;
261 $ctx->{srch} = $ctx->{-inbox}->search;
268 <html><head><title>Search not available for this
269 public-inbox</title><body><pre>Search is not available for this public-inbox
270 <a href="../">Return to index</a></pre></body></html>
272 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
275 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
276 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
277 # note: I'm not a big fan of other compression formats since they're
278 # significantly more expensive on CPU than gzip and less-widely available,
279 # especially on older systems. Stick to zlib since that's what git uses.
280 sub get_thread_mbox {
281 my ($ctx, $sfx) = @_;
282 my $srch = searcher($ctx) or return need_search($ctx);
283 require PublicInbox::Mbox;
284 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
288 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
289 sub get_thread_atom {
291 searcher($ctx) or return need_search($ctx);
292 require PublicInbox::Feed;
293 PublicInbox::Feed::generate_thread_atom($ctx);
296 sub legacy_redirects {
297 my ($ctx, $path_info) = @_;
299 # single-message pages
300 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
302 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
303 r301($ctx, $1, $2, 'raw');
305 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
309 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
310 r301($ctx, $1, $2, 't/#u');
312 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
313 r301($ctx, $1, $2, "t.mbox$3");
315 # even older legacy redirects
316 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
319 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
320 r301($ctx, $1, $2, 't/#u');
322 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
325 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
326 r301($ctx, $1, $2, 'raw');
328 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
329 r301($ctx, $1, $2, "t$3");
331 # legacy convenience redirects, order still matters
332 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
334 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
335 r301($ctx, $1, $2, 't/#u');
336 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
339 # some Message-IDs have slashes in them and the HTTP server
340 # may try to be clever and unescape them :<
341 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
342 msg_page($ctx, $1, $2, $3);
344 # in case people leave off the trailing slash:
345 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
346 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
347 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
350 $ctx->{www}->news_www->call($ctx->{env});
355 my ($ctx, $inbox, $mid, $suffix) = @_;
356 my $obj = $ctx->{-inbox};
358 my $r404 = invalid_inbox($ctx, $inbox);
359 return $r404 if $r404;
360 $obj = $ctx->{-inbox};
362 my $url = $obj->base_url($ctx->{env});
363 my $qs = $ctx->{env}->{QUERY_STRING};
364 $url .= (mid_escape($mid) . '/') if (defined $mid);
365 $url .= $suffix if (defined $suffix);
366 $url .= "?$qs" if $qs ne '';
369 [ Location => $url, 'Content-Type' => 'text/plain' ],
370 [ "Redirecting to $url\n" ] ]
374 my ($ctx, $inbox, $mid, $e) = @_;
376 $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
377 '' eq $e and return get_mid_html($ctx);
378 'T/' eq $e and return get_thread($ctx, 1);
379 't/' eq $e and return get_thread($ctx);
380 't.atom' eq $e and return get_thread_atom($ctx);
381 't.mbox' eq $e and return get_thread_mbox($ctx);
382 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
383 'raw' eq $e and return get_mid_txt($ctx);
385 # legacy, but no redirect for compatibility:
386 'f/' eq $e and return get_mid_html($ctx);
391 my ($ctx, $part, $path) = @_;
392 my $env = $ctx->{env};
393 my $ibx = $ctx->{-inbox};
394 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
395 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
400 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
401 searcher($ctx) or return need_search($ctx);
402 require PublicInbox::SearchView;
403 return PublicInbox::SearchView::mbox_results($ctx);
408 sub serve_mbox_range {
409 my ($ctx, $inbox, $range) = @_;
410 invalid_inbox($ctx, $inbox) || eval {
411 require PublicInbox::Mbox;
413 PublicInbox::Mbox::emit_range($ctx, $range);
419 $self->{news_www} ||= do {
420 require PublicInbox::NewsWWW;
421 PublicInbox::NewsWWW->new($self->{pi_config});
426 my ($ctx, $idx, $fn) = @_;
427 require PublicInbox::WwwAttach;
428 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);