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 if ($mid =~ /\A[a-f0-9]{40}\z/) {
173 # this is horiffically wasteful for legacy URLs:
174 if ($mid = mid2blob($ctx)) {
175 require Email::Simple;
176 use PublicInbox::MID qw/mid_clean/;
177 my $s = Email::Simple->new($mid);
178 $ctx->{mid} = mid_clean($s->header('Message-ID'));
184 # /$INBOX/new.atom -> Atom feed, includes replies
187 require PublicInbox::Feed;
188 PublicInbox::Feed::generate($ctx);
191 # /$INBOX/new.html -> HTML only
194 require PublicInbox::Feed;
195 PublicInbox::Feed::new_html($ctx);
198 # /$INBOX/?r=$GIT_COMMIT -> HTML only
201 require PublicInbox::Feed;
203 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
204 require PublicInbox::SearchView;
205 PublicInbox::SearchView::sres_top_html($ctx);
207 PublicInbox::Feed::generate_html_index($ctx);
211 # just returns a string ref for the blob in the current ctx
214 $ctx->{-inbox}->msg_by_mid($ctx->{mid});
217 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
220 require PublicInbox::Mbox;
221 PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
224 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
227 require PublicInbox::View;
229 PublicInbox::View::msg_page($ctx) || r404($ctx);
232 # /$INBOX/$MESSAGE_ID/t/
234 my ($ctx, $flat) = @_;
235 searcher($ctx) or return need_search($ctx);
236 $ctx->{flat} = $flat;
237 require PublicInbox::View;
238 PublicInbox::View::thread_html($ctx);
241 # /$INBOX/_/text/$KEY/
242 # /$INBOX/_/text/$KEY/raw
243 # KEY may contain slashes
245 my ($ctx, $inbox, $key) = @_;
246 my $r404 = invalid_inbox($ctx, $inbox);
247 return $r404 if $r404;
249 require PublicInbox::WwwText;
250 PublicInbox::WwwText::get_text($ctx, $key);
254 my ($ctx, $key) = @_;
255 my $val = $ctx->{$key};
256 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
260 # search support is optional, returns undef if Xapian is not installed
261 # or not configured for the given GIT_DIR
265 require PublicInbox::Search;
266 $ctx->{srch} = $ctx->{-inbox}->search;
273 <html><head><title>Search not available for this
274 public-inbox</title><body><pre>Search is not available for this public-inbox
275 <a href="../">Return to index</a></pre></body></html>
277 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
280 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
281 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
282 # note: I'm not a big fan of other compression formats since they're
283 # significantly more expensive on CPU than gzip and less-widely available,
284 # especially on older systems. Stick to zlib since that's what git uses.
285 sub get_thread_mbox {
286 my ($ctx, $sfx) = @_;
287 my $srch = searcher($ctx) or return need_search($ctx);
288 require PublicInbox::Mbox;
289 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
293 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
294 sub get_thread_atom {
296 searcher($ctx) or return need_search($ctx);
297 require PublicInbox::Feed;
298 PublicInbox::Feed::generate_thread_atom($ctx);
301 sub legacy_redirects {
302 my ($ctx, $path_info) = @_;
304 # single-message pages
305 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
307 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
308 r301($ctx, $1, $2, 'raw');
310 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
314 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
315 r301($ctx, $1, $2, 't/#u');
317 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
318 r301($ctx, $1, $2, "t.mbox$3");
320 # even older legacy redirects
321 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
324 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
325 r301($ctx, $1, $2, 't/#u');
327 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
330 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
331 r301($ctx, $1, $2, 'raw');
333 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
334 r301($ctx, $1, $2, "t$3");
336 # legacy convenience redirects, order still matters
337 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
339 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
340 r301($ctx, $1, $2, 't/#u');
341 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
344 # some Message-IDs have slashes in them and the HTTP server
345 # may try to be clever and unescape them :<
346 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
347 msg_page($ctx, $1, $2, $3);
349 # in case people leave off the trailing slash:
350 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
351 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
352 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
355 $ctx->{www}->news_www->call($ctx->{env});
360 my ($ctx, $inbox, $mid, $suffix) = @_;
361 my $obj = $ctx->{-inbox};
363 my $r404 = invalid_inbox($ctx, $inbox);
364 return $r404 if $r404;
365 $obj = $ctx->{-inbox};
367 my $url = $obj->base_url($ctx->{env});
368 my $qs = $ctx->{env}->{QUERY_STRING};
369 $url .= (mid_escape($mid) . '/') if (defined $mid);
370 $url .= $suffix if (defined $suffix);
371 $url .= "?$qs" if $qs ne '';
374 [ Location => $url, 'Content-Type' => 'text/plain' ],
375 [ "Redirecting to $url\n" ] ]
379 my ($ctx, $inbox, $mid, $e) = @_;
381 $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
382 '' eq $e and return get_mid_html($ctx);
383 'T/' eq $e and return get_thread($ctx, 1);
384 't/' eq $e and return get_thread($ctx);
385 't.atom' eq $e and return get_thread_atom($ctx);
386 't.mbox' eq $e and return get_thread_mbox($ctx);
387 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
388 'raw' eq $e and return get_mid_txt($ctx);
390 # legacy, but no redirect for compatibility:
391 'f/' eq $e and return get_mid_html($ctx);
396 my ($ctx, $part, $path) = @_;
397 my $env = $ctx->{env};
398 my $ibx = $ctx->{-inbox};
399 my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
400 $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
405 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
406 searcher($ctx) or return need_search($ctx);
407 require PublicInbox::SearchView;
408 return PublicInbox::SearchView::mbox_results($ctx);
413 sub serve_mbox_range {
414 my ($ctx, $inbox, $range) = @_;
415 invalid_inbox($ctx, $inbox) || eval {
416 require PublicInbox::Mbox;
418 PublicInbox::Mbox::emit_range($ctx, $range);
424 $self->{news_www} ||= do {
425 require PublicInbox::NewsWWW;
426 PublicInbox::NewsWWW->new($self->{pi_config});
431 my ($ctx, $idx, $fn) = @_;
432 require PublicInbox::WwwAttach;
433 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);