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;
23 # TODO: consider a routing tree now that we have more endpoints:
24 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
25 our $MID_RE = qr!([^/]+)!;
26 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
27 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
30 my ($class, $pi_config) = @_;
31 $pi_config ||= PublicInbox::Config->new;
32 bless { pi_config => $pi_config }, $class;
35 # backwards compatibility, do not use
37 my ($req, $method) = @_;
38 PublicInbox::WWW->new->call($req->env);
44 my $sn = $_[0]->{SCRIPT_NAME};
45 $path_re_cache{$sn} ||= do {
46 $sn = '/'.$sn unless index($sn, '/') == 0;
48 qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
53 my ($self, $env) = @_;
54 my $ctx = { env => $env, www => $self };
56 # we don't care about multi-value
59 my ($k, $v) = split('=', uri_unescape($_), 2);
60 $v = '' unless defined $v;
63 } split(/[&;]+/, $env->{QUERY_STRING});
66 # not using $env->{PATH_INFO} here since that's already decoded
67 my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
68 my $method = $env->{REQUEST_METHOD};
70 if ($method eq 'POST') {
71 if ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?(git-upload-pack)\z!) {
72 my ($part, $path) = ($2, $3);
73 return invalid_inbox($ctx, $1) ||
74 serve_git($ctx, $part, $path);
75 } elsif ($path_info =~ m!$INBOX_RE/!o) {
76 return invalid_inbox($ctx, $1) || mbox_results($ctx);
79 elsif ($method !~ /\AGET|HEAD\z/) {
80 return r(405, 'Method Not Allowed');
83 # top-level indices and feeds
84 if ($path_info eq '/') {
86 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
87 invalid_inbox($ctx, $1) || r301($ctx, $1);
88 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
89 invalid_inbox($ctx, $1) || get_index($ctx);
90 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
91 invalid_inbox($ctx, $1) || get_atom($ctx);
92 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
93 invalid_inbox($ctx, $1) || get_new($ctx);
94 } elsif ($path_info =~ m!$INBOX_RE/(?:(\d+)/)?
95 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
96 my ($part, $path) = ($2, $3);
97 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $path);
98 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
99 serve_mbox_range($ctx, $1, $2);
100 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
101 msg_page($ctx, $1, $2, $3);
103 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
104 my ($idx, $fn) = ($3, $4);
105 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
106 # in case people leave off the trailing slash:
107 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
108 my ($inbox, $mid_ue, $suffix) = ($1, $2, $3);
109 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
110 r301($ctx, $inbox, $mid_ue, $suffix);
112 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
113 my ($inbox, $mid_ue) = ($1, $2);
114 r301($ctx, $inbox, $mid_ue, '#R');
116 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
118 } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
119 get_text($ctx, $1, $2);
121 # convenience redirects order matters
122 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
126 legacy_redirects($ctx, $path_info);
130 # for CoW-friendliness, MOOOOO!
132 require PublicInbox::Feed;
133 require PublicInbox::View;
134 require PublicInbox::SearchThread;
135 require PublicInbox::MIME;
139 foreach (qw(PublicInbox::Search PublicInbox::SearchView
140 PublicInbox::Mbox IO::Compress::Gzip
141 PublicInbox::NewsWWW)) {
146 # private functions below
150 if ($ctx && $ctx->{mid}) {
151 require PublicInbox::ExtMsg;
153 return PublicInbox::ExtMsg::ext_msg($ctx);
158 # simple response for errors
159 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
161 # returns undef if valid, array ref response if invalid
162 sub invalid_inbox ($$) {
163 my ($ctx, $inbox) = @_;
164 my $www = $ctx->{www};
165 my $obj = $www->{pi_config}->lookup_name($inbox);
167 $ctx->{git} = $obj->git;
168 $ctx->{-inbox} = $obj;
172 # sometimes linkifiers (not ours!) screw up automatic link
173 # generation and link things intended for nntp:// to https?://,
174 # so try to infer links and redirect them to the appropriate
176 $www->news_www->call($ctx->{env});
179 # returns undef if valid, array ref response if invalid
180 sub invalid_inbox_mid {
181 my ($ctx, $inbox, $mid_ue) = @_;
182 my $ret = invalid_inbox($ctx, $inbox);
185 my $mid = $ctx->{mid} = uri_unescape($mid_ue);
186 my $ibx = $ctx->{-inbox};
187 if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
188 my ($x2, $x38) = ($1, $2);
189 # this is horrifically wasteful for legacy URLs:
190 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
191 require Email::Simple;
192 my $s = Email::Simple->new($str);
193 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
194 return r301($ctx, $inbox, mid_escape($mid));
199 # /$INBOX/new.atom -> Atom feed, includes replies
202 require PublicInbox::Feed;
203 PublicInbox::Feed::generate($ctx);
206 # /$INBOX/new.html -> HTML only
209 require PublicInbox::Feed;
210 PublicInbox::Feed::new_html($ctx);
213 # /$INBOX/?r=$GIT_COMMIT -> HTML only
216 require PublicInbox::Feed;
218 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
219 require PublicInbox::SearchView;
220 PublicInbox::SearchView::sres_top_html($ctx);
222 PublicInbox::Feed::generate_html_index($ctx);
226 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
229 require PublicInbox::Mbox;
230 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
233 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
236 require PublicInbox::View;
238 PublicInbox::View::msg_page($ctx) || r404($ctx);
241 # /$INBOX/$MESSAGE_ID/t/
243 my ($ctx, $flat) = @_;
244 searcher($ctx) or return need_search($ctx);
245 $ctx->{flat} = $flat;
246 require PublicInbox::View;
247 PublicInbox::View::thread_html($ctx);
250 # /$INBOX/_/text/$KEY/
251 # /$INBOX/_/text/$KEY/raw
252 # KEY may contain slashes
254 my ($ctx, $inbox, $key) = @_;
255 my $r404 = invalid_inbox($ctx, $inbox);
256 return $r404 if $r404;
258 require PublicInbox::WwwText;
259 PublicInbox::WwwText::get_text($ctx, $key);
263 my ($ctx, $key) = @_;
264 my $val = $ctx->{$key};
265 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
269 # search support is optional, returns undef if Xapian is not installed
270 # or not configured for the given GIT_DIR
274 require PublicInbox::Search;
275 $ctx->{srch} = $ctx->{-inbox}->search;
282 <html><head><title>Search not available for this
283 public-inbox</title><body><pre>Search is not available for this public-inbox
284 <a href="../">Return to index</a></pre></body></html>
286 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
289 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
290 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
291 # note: I'm not a big fan of other compression formats since they're
292 # significantly more expensive on CPU than gzip and less-widely available,
293 # especially on older systems. Stick to zlib since that's what git uses.
294 sub get_thread_mbox {
295 my ($ctx, $sfx) = @_;
296 my $srch = searcher($ctx) or return need_search($ctx);
297 require PublicInbox::Mbox;
298 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
302 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
303 sub get_thread_atom {
305 searcher($ctx) or return need_search($ctx);
306 require PublicInbox::Feed;
307 PublicInbox::Feed::generate_thread_atom($ctx);
310 sub legacy_redirects {
311 my ($ctx, $path_info) = @_;
313 # single-message pages
314 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
316 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
317 r301($ctx, $1, $2, 'raw');
319 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
323 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
324 r301($ctx, $1, $2, 't/#u');
326 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
327 r301($ctx, $1, $2, "t.mbox$3");
329 # even older legacy redirects
330 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
333 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
334 r301($ctx, $1, $2, 't/#u');
336 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
339 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
340 r301($ctx, $1, $2, 'raw');
342 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
343 r301($ctx, $1, $2, "t$3");
345 # legacy convenience redirects, order still matters
346 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
348 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
349 r301($ctx, $1, $2, 't/#u');
350 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
353 # some Message-IDs have slashes in them and the HTTP server
354 # may try to be clever and unescape them :<
355 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
356 msg_page($ctx, $1, $2, $3);
358 # in case people leave off the trailing slash:
359 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
360 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
361 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
364 $ctx->{www}->news_www->call($ctx->{env});
369 my ($ctx, $inbox, $mid_ue, $suffix) = @_;
370 my $obj = $ctx->{-inbox};
372 my $r404 = invalid_inbox($ctx, $inbox);
373 return $r404 if $r404;
374 $obj = $ctx->{-inbox};
376 my $url = $obj->base_url($ctx->{env});
377 my $qs = $ctx->{env}->{QUERY_STRING};
378 if (defined $mid_ue) {
379 # common, and much nicer as '@' than '%40':
380 $mid_ue =~ s/%40/@/g;
381 $url .= $mid_ue . '/';
383 $url .= $suffix if (defined $suffix);
384 $url .= "?$qs" if $qs ne '';
387 [ Location => $url, 'Content-Type' => 'text/plain' ],
388 [ "Redirecting to $url\n" ] ]
392 my ($ctx, $inbox, $mid_ue, $e) = @_;
394 $ret = invalid_inbox_mid($ctx, $inbox, $mid_ue) and return $ret;
395 '' eq $e and return get_mid_html($ctx);
396 'T/' eq $e and return get_thread($ctx, 1);
397 't/' eq $e and return get_thread($ctx);
398 't.atom' eq $e and return get_thread_atom($ctx);
399 't.mbox' eq $e and return get_thread_mbox($ctx);
400 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
401 'raw' eq $e and return get_mid_txt($ctx);
403 # legacy, but no redirect for compatibility:
404 'f/' eq $e and return get_mid_html($ctx);
409 my ($ctx, $part, $path) = @_;
410 my $env = $ctx->{env};
411 my $ibx = $ctx->{-inbox};
412 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
413 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
418 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
419 searcher($ctx) or return need_search($ctx);
420 require PublicInbox::SearchView;
421 return PublicInbox::SearchView::mbox_results($ctx);
426 sub serve_mbox_range {
427 my ($ctx, $inbox, $range) = @_;
428 invalid_inbox($ctx, $inbox) || eval {
429 require PublicInbox::Mbox;
431 PublicInbox::Mbox::emit_range($ctx, $range);
437 $self->{news_www} ||= do {
438 require PublicInbox::NewsWWW;
439 PublicInbox::NewsWWW->new($self->{pi_config});
444 my ($ctx, $idx, $fn) = @_;
445 require PublicInbox::WwwAttach;
446 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);