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 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, env => $env, www => $self,
44 pi_config => $self->{pi_config} };
46 # we don't care about multi-value
48 my ($k, $v) = split('=', $_, 2);
50 } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
53 my $path_info = $env->{PATH_INFO};
54 my $method = $env->{REQUEST_METHOD};
56 if ($method eq 'POST' &&
57 $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
59 return (invalid_inbox($self, $ctx, $1) ||
60 serve_git($env, $ctx->{git}, $path));
62 elsif ($method !~ /\AGET|HEAD\z/) {
63 return r(405, 'Method Not Allowed');
66 # top-level indices and feeds
67 if ($path_info eq '/') {
69 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
70 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
71 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
72 invalid_inbox($self, $ctx, $1) || get_index($ctx);
73 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
74 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
76 } elsif ($path_info =~ m!$INBOX_RE/
77 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
79 invalid_inbox($self, $ctx, $1) ||
80 serve_git($env, $ctx->{git}, $path);
81 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
82 serve_mbox_range($self, $ctx, $1, $2);
83 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
84 msg_page($self, $ctx, $1, $2, $3);
86 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
87 my ($idx, $fn) = ($3, $4);
88 invalid_inbox_mid($self, $ctx, $1, $2) ||
89 get_attach($ctx, $idx, $fn);
90 # in case people leave off the trailing slash:
91 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
92 my ($inbox, $mid, $suffix) = ($1, $2, $3);
93 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
94 r301($ctx, $inbox, $mid, $suffix);
96 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
99 # convenience redirects order matters
100 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
104 legacy_redirects($self, $ctx, $path_info);
108 # for CoW-friendliness, MOOOOO!
110 require PublicInbox::Feed;
111 require PublicInbox::View;
112 require PublicInbox::Thread;
117 foreach (qw(PublicInbox::Search PublicInbox::SearchView
118 PublicInbox::Mbox IO::Compress::Gzip
119 PublicInbox::NewsWWW)) {
124 # private functions below
128 if ($ctx && $ctx->{mid}) {
129 require PublicInbox::ExtMsg;
131 return PublicInbox::ExtMsg::ext_msg($ctx);
136 # simple response for errors
137 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
139 # returns undef if valid, array ref response if invalid
141 my ($self, $ctx, $inbox) = @_;
142 my $obj = $ctx->{pi_config}->lookup_name($inbox);
144 $ctx->{git_dir} = $obj->{mainrepo};
145 $ctx->{git} = $obj->git;
146 # for PublicInbox::HTTP::weaken_task:
147 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
148 $ctx->{-inbox} = $obj;
149 $ctx->{inbox} = $inbox;
153 # sometimes linkifiers (not ours!) screw up automatic link
154 # generation and link things intended for nntp:// to https?://,
155 # so try to infer links and redirect them to the appropriate
157 $self->news_www->call($ctx->{cgi}->{env});
160 # returns undef if valid, array ref response if invalid
161 sub invalid_inbox_mid {
162 my ($self, $ctx, $inbox, $mid) = @_;
163 my $ret = invalid_inbox($self, $ctx, $inbox);
166 $ctx->{mid} = $mid = uri_unescape($mid);
167 if ($mid =~ /\A[a-f0-9]{40}\z/) {
168 # this is horiffically wasteful for legacy URLs:
169 if ($mid = mid2blob($ctx)) {
170 require Email::Simple;
171 use PublicInbox::MID qw/mid_clean/;
172 my $s = Email::Simple->new($mid);
173 $ctx->{mid} = mid_clean($s->header('Message-ID'));
179 # /$INBOX/new.atom -> Atom feed, includes replies
182 require PublicInbox::Feed;
183 PublicInbox::Feed::generate($ctx);
186 # /$INBOX/?r=$GIT_COMMIT -> HTML only
189 require PublicInbox::Feed;
190 my $srch = searcher($ctx);
192 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
193 require PublicInbox::SearchView;
194 PublicInbox::SearchView::sres_top_html($ctx);
196 PublicInbox::Feed::generate_html_index($ctx);
200 # just returns a string ref for the blob in the current ctx
203 require PublicInbox::MID;
204 my $path = PublicInbox::MID::mid2path($ctx->{mid});
205 $ctx->{git}->cat_file("HEAD:$path");
208 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
211 my $x = mid2blob($ctx) or return r404($ctx);
212 require PublicInbox::Mbox;
213 PublicInbox::Mbox::emit1($ctx, $x);
216 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
219 my $x = mid2blob($ctx) or return r404($ctx);
221 require PublicInbox::View;
222 my $foot = footer($ctx);
224 my $mime = Email::MIME->new($x);
226 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
227 [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
230 # /$INBOX/$MESSAGE_ID/R/ -> HTML content (fullquotes)
233 my $x = mid2blob($ctx) or return r404($ctx);
235 require PublicInbox::View;
236 my $foot = footer($ctx);
238 my $hdr = Email::MIME->new($x)->header_obj;
239 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
240 [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
243 # /$INBOX/$MESSAGE_ID/t/
245 my ($ctx, $flat) = @_;
246 my $srch = searcher($ctx) or return need_search($ctx);
247 require PublicInbox::View;
248 my $foot = footer($ctx);
249 $ctx->{flat} = $flat;
250 PublicInbox::View::thread_html($ctx, $foot, $srch);
254 my ($ctx, $key) = @_;
255 my $val = $ctx->{$key};
256 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
262 return '' unless $ctx;
263 my $obj = $ctx->{-inbox} or return '';
265 # auto-generate a footer
266 chomp(my $desc = $obj->description);
269 my @urls = @{$obj->cloneurl};
270 my %seen = map { $_ => 1 } @urls;
271 my $cgi = $ctx->{cgi};
272 my $http = $cgi->base->as_string . $obj->{name};
273 $seen{$http} or unshift @urls, $http;
274 my $ssoma_url = PublicInbox::Hval::prurl($ctx->{env}, SSOMA_URL);
275 if (scalar(@urls) == 1) {
276 $urls = "URL for <a\nhref=\"" . $ssoma_url .
277 qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
279 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
280 qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
281 join("\n", map { "\tgit clone --mirror $_" } @urls);
284 my $addr = $obj->{-primary_address};
285 $ctx->{footer} = join("\n",
288 PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
289 '">public-inbox</a>, ' .
290 'anybody may post in plain-text (not HTML):',
296 # search support is optional, returns undef if Xapian is not installed
297 # or not configured for the given GIT_DIR
301 require PublicInbox::Search;
302 $ctx->{srch} = $ctx->{-inbox}->search;
309 <html><head><title>Search not available for this
310 public-inbox</title><body><pre>Search is not available for this public-inbox
311 <a href="../">Return to index</a></pre></body></html>
313 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
316 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
317 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
318 # note: I'm not a big fan of other compression formats since they're
319 # significantly more expensive on CPU than gzip and less-widely available,
320 # especially on older systems. Stick to zlib since that's what git uses.
321 sub get_thread_mbox {
322 my ($ctx, $sfx) = @_;
323 my $srch = searcher($ctx) or return need_search($ctx);
324 require PublicInbox::Mbox;
325 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
329 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
330 sub get_thread_atom {
332 searcher($ctx) or return need_search($ctx);
333 $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
334 require PublicInbox::Feed;
335 PublicInbox::Feed::generate_thread_atom($ctx);
338 sub legacy_redirects {
339 my ($self, $ctx, $path_info) = @_;
341 # single-message pages
342 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
344 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
345 r301($ctx, $1, $2, 'raw');
347 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
351 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
352 r301($ctx, $1, $2, 't/#u');
354 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
355 r301($ctx, $1, $2, "t.mbox$3");
357 # even older legacy redirects
358 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
361 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
362 r301($ctx, $1, $2, 't/#u');
364 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
367 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
368 r301($ctx, $1, $2, 'raw');
370 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
371 r301($ctx, $1, $2, "t$3");
373 # legacy convenience redirects, order still matters
374 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
376 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
377 r301($ctx, $1, $2, 't/#u');
378 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
381 # some Message-IDs have slashes in them and the HTTP server
382 # may try to be clever and unescape them :<
383 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
384 msg_page($self, $ctx, $1, $2, $3);
386 # in case people leave off the trailing slash:
387 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
388 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
389 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
392 $self->news_www->call($ctx->{cgi}->{env});
397 my ($ctx, $inbox, $mid, $suffix) = @_;
398 my $cgi = $ctx->{cgi};
399 my $obj = $ctx->{-inbox};
401 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
402 return $r404 if $r404;
403 $obj = $ctx->{-inbox};
405 my $url = $obj->base_url($cgi);
406 my $qs = $ctx->{env}->{QUERY_STRING};
407 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
408 $url .= $suffix if (defined $suffix);
409 $url .= "?$qs" if $qs ne '';
412 [ Location => $url, 'Content-Type' => 'text/plain' ],
413 [ "Redirecting to $url\n" ] ]
417 my ($self, $ctx, $inbox, $mid, $e) = @_;
419 $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
420 '' eq $e and return get_mid_html($ctx);
421 't/' eq $e and return get_thread($ctx);
422 't.atom' eq $e and return get_thread_atom($ctx);
423 't.mbox' eq $e and return get_thread_mbox($ctx);
424 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
425 'T/' eq $e and return get_thread($ctx, 1);
426 'raw' eq $e and return get_mid_txt($ctx);
428 # legacy, but no redirect for compatibility:
429 'f/' eq $e and return get_mid_html($ctx);
431 'R/' eq $e and return get_reply_html($ctx);
436 my ($env, $git, $path) = @_;
437 PublicInbox::GitHTTPBackend::serve($env, $git, $path);
440 sub serve_mbox_range {
441 my ($self, $ctx, $inbox, $range) = @_;
442 invalid_inbox($self, $ctx, $inbox) || eval {
443 require PublicInbox::Mbox;
445 PublicInbox::Mbox::emit_range($ctx, $range);
451 my $nw = $self->{news_www};
453 require PublicInbox::NewsWWW;
454 $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
458 my ($ctx, $idx, $fn) = @_;
459 require PublicInbox::WwwAttach;
460 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);