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 qw(try_cat);
18 use URI::Escape qw(uri_escape_utf8 uri_unescape);
19 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
20 use constant PI_URL => '//public-inbox.org/';
21 require PublicInbox::Git;
22 use PublicInbox::GitHTTPBackend;
23 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
24 our $MID_RE = qr!([^/]+)!;
25 our $END_RE = qr!(T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
26 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
29 my ($class, $pi_config) = @_;
30 $pi_config ||= PublicInbox::Config->new;
31 bless { pi_config => $pi_config }, $class;
34 # backwards compatibility, do not use
36 my ($req, $method) = @_;
37 PublicInbox::WWW->new->call($req->env);
41 my ($self, $env) = @_;
42 my $cgi = Plack::Request->new($env);
43 my $ctx = {cgi => $cgi, pi_config => $self->{pi_config}, www => $self};
44 my $path_info = $cgi->path_info;
46 my $method = $cgi->method;
47 if ($method eq 'POST' &&
48 $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
50 return (invalid_inbox($self, $ctx, $1) ||
51 serve_git($cgi, $ctx->{git}, $path));
53 elsif ($method !~ /\AGET|HEAD\z/) {
54 return r(405, 'Method Not Allowed');
57 # top-level indices and feeds
58 if ($path_info eq '/') {
60 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
61 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
62 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
63 invalid_inbox($self, $ctx, $1) || get_index($ctx);
64 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
65 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
67 } elsif ($path_info =~ m!$INBOX_RE/
68 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
70 invalid_inbox($self, $ctx, $1) ||
71 serve_git($cgi, $ctx->{git}, $path);
72 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
73 serve_mbox_range($self, $ctx, $1, $2);
74 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
75 msg_page($self, $ctx, $1, $2, $3);
77 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
78 my ($idx, $fn) = ($3, $4);
79 invalid_inbox_mid($self, $ctx, $1, $2) ||
80 get_attach($ctx, $idx, $fn);
81 # in case people leave off the trailing slash:
82 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
83 my ($inbox, $mid, $suffix) = ($1, $2, $3);
84 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
85 r301($ctx, $inbox, $mid, $suffix);
87 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
90 # convenience redirects order matters
91 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
95 legacy_redirects($self, $ctx, $path_info);
99 # for CoW-friendliness, MOOOOO!
101 require PublicInbox::Feed;
102 require PublicInbox::View;
103 require PublicInbox::Thread;
108 foreach (qw(PublicInbox::Search PublicInbox::SearchView
109 PublicInbox::Mbox IO::Compress::Gzip
110 PublicInbox::NewsWWW PublicInbox::NewsGroup)) {
115 # private functions below
119 if ($ctx && $ctx->{mid}) {
120 require PublicInbox::ExtMsg;
122 return PublicInbox::ExtMsg::ext_msg($ctx);
127 # simple response for errors
128 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
130 # returns undef if valid, array ref response if invalid
132 my ($self, $ctx, $inbox) = @_;
133 my $obj = $ctx->{pi_config}->lookup_name($inbox);
135 $ctx->{git_dir} = $obj->{mainrepo};
136 $ctx->{git} = $obj->git;
137 # for PublicInbox::HTTP::weaken_task:
138 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
139 $ctx->{-inbox} = $obj;
140 $ctx->{inbox} = $inbox;
144 # sometimes linkifiers (not ours!) screw up automatic link
145 # generation and link things intended for nntp:// to https?://,
146 # so try to infer links and redirect them to the appropriate
148 $self->news_www->call($ctx->{cgi}->{env});
151 # returns undef if valid, array ref response if invalid
152 sub invalid_inbox_mid {
153 my ($self, $ctx, $inbox, $mid) = @_;
154 my $ret = invalid_inbox($self, $ctx, $inbox);
157 $ctx->{mid} = $mid = uri_unescape($mid);
158 if ($mid =~ /\A[a-f0-9]{40}\z/) {
159 # this is horiffically wasteful for legacy URLs:
160 if ($mid = mid2blob($ctx)) {
161 require Email::Simple;
162 use PublicInbox::MID qw/mid_clean/;
163 my $s = Email::Simple->new($mid);
164 $ctx->{mid} = mid_clean($s->header('Message-ID'));
170 # /$INBOX/new.atom -> Atom feed, includes replies
173 require PublicInbox::Feed;
174 PublicInbox::Feed::generate($ctx);
177 # /$INBOX/?r=$GIT_COMMIT -> HTML only
180 require PublicInbox::Feed;
181 my $srch = searcher($ctx);
183 if (defined $ctx->{cgi}->param('q')) {
184 require PublicInbox::SearchView;
185 PublicInbox::SearchView::sres_top_html($ctx);
187 PublicInbox::Feed::generate_html_index($ctx);
191 # just returns a string ref for the blob in the current ctx
194 require PublicInbox::MID;
195 my $path = PublicInbox::MID::mid2path($ctx->{mid});
196 $ctx->{git}->cat_file("HEAD:$path");
199 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
202 my $x = mid2blob($ctx) or return r404($ctx);
203 require PublicInbox::Mbox;
204 PublicInbox::Mbox::emit1($ctx, $x);
207 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
210 my $x = mid2blob($ctx) or return r404($ctx);
212 require PublicInbox::View;
213 my $foot = footer($ctx);
215 my $mime = Email::MIME->new($x);
217 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
218 [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
221 # /$INBOX/$MESSAGE_ID/R/ -> HTML content (fullquotes)
224 my $x = mid2blob($ctx) or return r404($ctx);
226 require PublicInbox::View;
227 my $foot = footer($ctx);
229 my $hdr = Email::MIME->new($x)->header_obj;
230 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
231 [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
234 # /$INBOX/$MESSAGE_ID/t/
236 my ($ctx, $flat) = @_;
237 my $srch = searcher($ctx) or return need_search($ctx);
238 require PublicInbox::View;
239 my $foot = footer($ctx);
240 $ctx->{flat} = $flat;
241 PublicInbox::View::thread_html($ctx, $foot, $srch);
245 my ($ctx, $key) = @_;
246 my $val = $ctx->{$key};
247 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
253 return '' unless $ctx;
254 my $obj = $ctx->{-inbox} or return '';
255 my $footer = $obj->footer_html;
256 return $ctx->{footer} = $footer if $footer;
258 # auto-generate a footer
259 chomp(my $desc = $obj->description);
262 my @urls = @{$obj->cloneurl};
263 my %seen = map { $_ => 1 } @urls;
264 my $cgi = $ctx->{cgi};
265 my $http = $cgi->base->as_string . $obj->{name};
266 $seen{$http} or unshift @urls, $http;
267 my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
268 if (scalar(@urls) == 1) {
269 $urls = "URL for <a\nhref=\"" . $ssoma_url .
270 qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
272 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
273 qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
274 join("\n", map { "\tgit clone --mirror $_" } @urls);
277 my $addr = $obj->{-primary_address};
278 $ctx->{footer} = join("\n",
281 PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
282 '">public-inbox</a>, ' .
283 'anybody may post in plain-text (not HTML):',
289 # search support is optional, returns undef if Xapian is not installed
290 # or not configured for the given GIT_DIR
294 require PublicInbox::Search;
295 $ctx->{srch} = $ctx->{-inbox}->search;
302 <html><head><title>Search not available for this
303 public-inbox</title><body><pre>Search is not available for this public-inbox
304 <a href="../">Return to index</a></pre></body></html>
306 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
309 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
310 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
311 # note: I'm not a big fan of other compression formats since they're
312 # significantly more expensive on CPU than gzip and less-widely available,
313 # especially on older systems. Stick to zlib since that's what git uses.
314 sub get_thread_mbox {
315 my ($ctx, $sfx) = @_;
316 my $srch = searcher($ctx) or return need_search($ctx);
317 require PublicInbox::Mbox;
318 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
322 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
323 sub get_thread_atom {
325 searcher($ctx) or return need_search($ctx);
326 $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
327 require PublicInbox::Feed;
328 PublicInbox::Feed::generate_thread_atom($ctx);
331 sub legacy_redirects {
332 my ($self, $ctx, $path_info) = @_;
334 # single-message pages
335 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
337 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
338 r301($ctx, $1, $2, 'raw');
340 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
344 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
345 r301($ctx, $1, $2, 't/#u');
347 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
348 r301($ctx, $1, $2, "t.mbox$3");
350 # even older legacy redirects
351 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
354 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
355 r301($ctx, $1, $2, 't/#u');
357 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
360 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
361 r301($ctx, $1, $2, 'raw');
363 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
364 r301($ctx, $1, $2, "t$3");
366 # legacy convenience redirects, order still matters
367 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
369 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
370 r301($ctx, $1, $2, 't/#u');
371 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
374 # some Message-IDs have slashes in them and the HTTP server
375 # may try to be clever and unescape them :<
376 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
377 msg_page($self, $ctx, $1, $2, $3);
379 # in case people leave off the trailing slash:
380 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
381 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
382 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
385 $self->news_www->call($ctx->{cgi}->{env});
390 my ($ctx, $inbox, $mid, $suffix) = @_;
391 my $cgi = $ctx->{cgi};
392 my $obj = $ctx->{-inbox};
394 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
395 return $r404 if $r404;
396 $obj = $ctx->{-inbox};
398 my $url = $obj->base_url($cgi);
399 my $qs = $cgi->env->{QUERY_STRING};
400 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
401 $url .= $suffix if (defined $suffix);
402 $url .= "?$qs" if $qs ne '';
405 [ Location => $url, 'Content-Type' => 'text/plain' ],
406 [ "Redirecting to $url\n" ] ]
410 my ($self, $ctx, $inbox, $mid, $e) = @_;
412 $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
413 '' eq $e and return get_mid_html($ctx);
414 't/' eq $e and return get_thread($ctx);
415 't.atom' eq $e and return get_thread_atom($ctx);
416 't.mbox' eq $e and return get_thread_mbox($ctx);
417 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
418 'T/' eq $e and return get_thread($ctx, 1);
419 'raw' eq $e and return get_mid_txt($ctx);
421 # legacy, but no redirect for compatibility:
422 'f/' eq $e and return get_mid_html($ctx);
424 'R/' eq $e and return get_reply_html($ctx);
429 my ($cgi, $git, $path) = @_;
430 PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
433 sub serve_mbox_range {
434 my ($self, $ctx, $inbox, $range) = @_;
435 invalid_inbox($self, $ctx, $inbox) || eval {
436 require PublicInbox::Mbox;
438 PublicInbox::Mbox::emit_range($ctx, $range);
444 my $nw = $self->{news_www};
446 require PublicInbox::NewsWWW;
447 $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
451 my ($ctx, $idx, $fn) = @_;
452 require PublicInbox::WwwAttach;
453 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);