1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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;
17 use PublicInbox::Config;
18 use PublicInbox::Hval;
19 use URI::Escape qw(uri_escape_utf8 uri_unescape);
20 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
21 use constant PI_URL => '//public-inbox.org/';
22 require PublicInbox::Git;
23 use PublicInbox::GitHTTPBackend;
24 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
25 our $MID_RE = qr!([^/]+)!;
26 our $END_RE = qr!(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);
42 my ($self, $env) = @_;
43 my $cgi = Plack::Request->new($env);
44 my $ctx = { cgi => $cgi, env => $env, www => $self,
45 pi_config => $self->{pi_config} };
47 # we don't care about multi-value
49 my ($k, $v) = split('=', $_, 2);
50 $v = '' unless defined $v;
53 } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
56 my $path_info = $env->{PATH_INFO};
57 my $method = $env->{REQUEST_METHOD};
59 if ($method eq 'POST' &&
60 $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
62 return (invalid_inbox($self, $ctx, $1) ||
63 serve_git($env, $ctx->{git}, $path));
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($self, $ctx, $1) || r301($ctx, $1);
74 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
75 invalid_inbox($self, $ctx, $1) || get_index($ctx);
76 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
77 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
79 } elsif ($path_info =~ m!$INBOX_RE/
80 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
82 invalid_inbox($self, $ctx, $1) ||
83 serve_git($env, $ctx->{git}, $path);
84 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
85 serve_mbox_range($self, $ctx, $1, $2);
86 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
87 msg_page($self, $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($self, $ctx, $1, $2) ||
92 get_attach($ctx, $idx, $fn);
93 # in case people leave off the trailing slash:
94 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(?:T|T/|t)\z!o) {
95 my ($inbox, $mid) = ($1, $2);
96 r301($ctx, $inbox, $mid, 't/#u');
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) {
105 # convenience redirects order matters
106 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
110 legacy_redirects($self, $ctx, $path_info);
114 # for CoW-friendliness, MOOOOO!
116 require PublicInbox::Feed;
117 require PublicInbox::View;
118 require PublicInbox::Thread;
123 foreach (qw(PublicInbox::Search PublicInbox::SearchView
124 PublicInbox::Mbox IO::Compress::Gzip
125 PublicInbox::NewsWWW)) {
130 # private functions below
134 if ($ctx && $ctx->{mid}) {
135 require PublicInbox::ExtMsg;
137 return PublicInbox::ExtMsg::ext_msg($ctx);
142 # simple response for errors
143 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
145 # returns undef if valid, array ref response if invalid
147 my ($self, $ctx, $inbox) = @_;
148 my $obj = $ctx->{pi_config}->lookup_name($inbox);
150 $ctx->{git_dir} = $obj->{mainrepo};
151 $ctx->{git} = $obj->git;
152 # for PublicInbox::HTTP::weaken_task:
153 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
154 $ctx->{-inbox} = $obj;
155 $ctx->{inbox} = $inbox;
159 # sometimes linkifiers (not ours!) screw up automatic link
160 # generation and link things intended for nntp:// to https?://,
161 # so try to infer links and redirect them to the appropriate
163 $self->news_www->call($ctx->{cgi}->{env});
166 # returns undef if valid, array ref response if invalid
167 sub invalid_inbox_mid {
168 my ($self, $ctx, $inbox, $mid) = @_;
169 my $ret = invalid_inbox($self, $ctx, $inbox);
172 $ctx->{mid} = $mid = uri_unescape($mid);
173 if ($mid =~ /\A[a-f0-9]{40}\z/) {
174 # this is horiffically wasteful for legacy URLs:
175 if ($mid = mid2blob($ctx)) {
176 require Email::Simple;
177 use PublicInbox::MID qw/mid_clean/;
178 my $s = Email::Simple->new($mid);
179 $ctx->{mid} = mid_clean($s->header('Message-ID'));
185 # /$INBOX/new.atom -> Atom feed, includes replies
188 require PublicInbox::Feed;
189 PublicInbox::Feed::generate($ctx);
192 # /$INBOX/?r=$GIT_COMMIT -> HTML only
195 require PublicInbox::Feed;
196 my $srch = searcher($ctx);
198 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
199 require PublicInbox::SearchView;
200 PublicInbox::SearchView::sres_top_html($ctx);
202 PublicInbox::Feed::generate_html_index($ctx);
206 # just returns a string ref for the blob in the current ctx
209 $ctx->{-inbox}->msg_by_mid($ctx->{mid});
212 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
215 my $x = mid2blob($ctx) or return r404($ctx);
216 require PublicInbox::Mbox;
217 PublicInbox::Mbox::emit1($ctx, $x);
220 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
223 my $x = mid2blob($ctx) or return r404($ctx);
225 require PublicInbox::View;
226 my $foot = footer($ctx);
228 my $mime = Email::MIME->new($x);
230 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
231 PublicInbox::View::msg_html($ctx, $mime, $foot) ];
234 # /$INBOX/$MESSAGE_ID/t/
237 searcher($ctx) or return need_search($ctx);
238 require PublicInbox::View;
239 PublicInbox::View::thread_html($ctx);
243 my ($ctx, $key) = @_;
244 my $val = $ctx->{$key};
245 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
251 return '' unless $ctx;
252 my $obj = $ctx->{-inbox} or return '';
254 # auto-generate a footer
255 chomp(my $desc = $obj->description);
256 $desc = PublicInbox::Hval::ascii_html($desc);
259 my @urls = @{$obj->cloneurl};
260 my %seen = map { $_ => 1 } @urls;
261 my $cgi = $ctx->{cgi};
262 my $http = $cgi->base->as_string . $obj->{name};
263 $seen{$http} or unshift @urls, $http;
264 my $ssoma_url = PublicInbox::Hval::prurl($ctx->{env}, SSOMA_URL);
265 if (scalar(@urls) == 1) {
266 $urls = "URL for <a\nhref=\"" . $ssoma_url .
267 qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
269 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
270 qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
271 join("\n", map { "\tgit clone --mirror $_" } @urls);
274 my $addr = $obj->{-primary_address};
275 $ctx->{footer} = join("\n",
278 PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
279 '">public-inbox</a>, ' .
280 'anybody may post in plain-text (not HTML):',
286 # search support is optional, returns undef if Xapian is not installed
287 # or not configured for the given GIT_DIR
291 require PublicInbox::Search;
292 $ctx->{srch} = $ctx->{-inbox}->search;
299 <html><head><title>Search not available for this
300 public-inbox</title><body><pre>Search is not available for this public-inbox
301 <a href="../">Return to index</a></pre></body></html>
303 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
306 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
307 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
308 # note: I'm not a big fan of other compression formats since they're
309 # significantly more expensive on CPU than gzip and less-widely available,
310 # especially on older systems. Stick to zlib since that's what git uses.
311 sub get_thread_mbox {
312 my ($ctx, $sfx) = @_;
313 my $srch = searcher($ctx) or return need_search($ctx);
314 require PublicInbox::Mbox;
315 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
319 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
320 sub get_thread_atom {
322 searcher($ctx) or return need_search($ctx);
323 $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
324 require PublicInbox::Feed;
325 PublicInbox::Feed::generate_thread_atom($ctx);
328 sub legacy_redirects {
329 my ($self, $ctx, $path_info) = @_;
331 # single-message pages
332 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
334 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
335 r301($ctx, $1, $2, 'raw');
337 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
341 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
342 r301($ctx, $1, $2, 't/#u');
344 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
345 r301($ctx, $1, $2, "t.mbox$3");
347 # even older legacy redirects
348 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
351 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
352 r301($ctx, $1, $2, 't/#u');
354 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
357 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
358 r301($ctx, $1, $2, 'raw');
360 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
361 r301($ctx, $1, $2, "t$3");
363 # legacy convenience redirects, order still matters
364 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
366 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
367 r301($ctx, $1, $2, 't/#u');
368 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
371 # some Message-IDs have slashes in them and the HTTP server
372 # may try to be clever and unescape them :<
373 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
374 msg_page($self, $ctx, $1, $2, $3);
376 # in case people leave off the trailing slash:
377 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
378 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
379 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
382 $self->news_www->call($ctx->{cgi}->{env});
387 my ($ctx, $inbox, $mid, $suffix) = @_;
388 my $cgi = $ctx->{cgi};
389 my $obj = $ctx->{-inbox};
391 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
392 return $r404 if $r404;
393 $obj = $ctx->{-inbox};
395 my $url = $obj->base_url($cgi);
396 my $qs = $ctx->{env}->{QUERY_STRING};
397 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
398 $url .= $suffix if (defined $suffix);
399 $url .= "?$qs" if $qs ne '';
402 [ Location => $url, 'Content-Type' => 'text/plain' ],
403 [ "Redirecting to $url\n" ] ]
407 my ($self, $ctx, $inbox, $mid, $e) = @_;
409 $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
410 '' eq $e and return get_mid_html($ctx);
411 't/' eq $e and return get_thread($ctx);
412 't.atom' eq $e and return get_thread_atom($ctx);
413 't.mbox' eq $e and return get_thread_mbox($ctx);
414 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
415 'raw' eq $e and return get_mid_txt($ctx);
417 # legacy, but no redirect for compatibility:
418 'f/' eq $e and return get_mid_html($ctx);
423 my ($env, $git, $path) = @_;
424 PublicInbox::GitHTTPBackend::serve($env, $git, $path);
427 sub serve_mbox_range {
428 my ($self, $ctx, $inbox, $range) = @_;
429 invalid_inbox($self, $ctx, $inbox) || eval {
430 require PublicInbox::Mbox;
432 PublicInbox::Mbox::emit_range($ctx, $range);
438 my $nw = $self->{news_www};
440 require PublicInbox::NewsWWW;
441 $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
445 my ($ctx, $idx, $fn) = @_;
446 require PublicInbox::WwwAttach;
447 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);