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/|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)\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/R/?\z!o) {
97 my ($inbox, $mid) = ($1, $2);
98 r301($ctx, $inbox, $mid, '#R');
100 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
103 # convenience redirects order matters
104 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
108 legacy_redirects($self, $ctx, $path_info);
112 # for CoW-friendliness, MOOOOO!
114 require PublicInbox::Feed;
115 require PublicInbox::View;
116 require PublicInbox::Thread;
121 foreach (qw(PublicInbox::Search PublicInbox::SearchView
122 PublicInbox::Mbox IO::Compress::Gzip
123 PublicInbox::NewsWWW)) {
128 # private functions below
132 if ($ctx && $ctx->{mid}) {
133 require PublicInbox::ExtMsg;
135 return PublicInbox::ExtMsg::ext_msg($ctx);
140 # simple response for errors
141 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
143 # returns undef if valid, array ref response if invalid
145 my ($self, $ctx, $inbox) = @_;
146 my $obj = $ctx->{pi_config}->lookup_name($inbox);
148 $ctx->{git_dir} = $obj->{mainrepo};
149 $ctx->{git} = $obj->git;
150 # for PublicInbox::HTTP::weaken_task:
151 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
152 $ctx->{-inbox} = $obj;
153 $ctx->{inbox} = $inbox;
157 # sometimes linkifiers (not ours!) screw up automatic link
158 # generation and link things intended for nntp:// to https?://,
159 # so try to infer links and redirect them to the appropriate
161 $self->news_www->call($ctx->{cgi}->{env});
164 # returns undef if valid, array ref response if invalid
165 sub invalid_inbox_mid {
166 my ($self, $ctx, $inbox, $mid) = @_;
167 my $ret = invalid_inbox($self, $ctx, $inbox);
170 $ctx->{mid} = $mid = uri_unescape($mid);
171 if ($mid =~ /\A[a-f0-9]{40}\z/) {
172 # this is horiffically wasteful for legacy URLs:
173 if ($mid = mid2blob($ctx)) {
174 require Email::Simple;
175 use PublicInbox::MID qw/mid_clean/;
176 my $s = Email::Simple->new($mid);
177 $ctx->{mid} = mid_clean($s->header('Message-ID'));
183 # /$INBOX/new.atom -> Atom feed, includes replies
186 require PublicInbox::Feed;
187 PublicInbox::Feed::generate($ctx);
190 # /$INBOX/?r=$GIT_COMMIT -> HTML only
193 require PublicInbox::Feed;
194 my $srch = searcher($ctx);
196 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
197 require PublicInbox::SearchView;
198 PublicInbox::SearchView::sres_top_html($ctx);
200 PublicInbox::Feed::generate_html_index($ctx);
204 # just returns a string ref for the blob in the current ctx
207 require PublicInbox::MID;
208 my $path = PublicInbox::MID::mid2path($ctx->{mid});
209 $ctx->{git}->cat_file("HEAD:$path");
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/
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 '';
256 # auto-generate a footer
257 chomp(my $desc = $obj->description);
260 my @urls = @{$obj->cloneurl};
261 my %seen = map { $_ => 1 } @urls;
262 my $cgi = $ctx->{cgi};
263 my $http = $cgi->base->as_string . $obj->{name};
264 $seen{$http} or unshift @urls, $http;
265 my $ssoma_url = PublicInbox::Hval::prurl($ctx->{env}, SSOMA_URL);
266 if (scalar(@urls) == 1) {
267 $urls = "URL for <a\nhref=\"" . $ssoma_url .
268 qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
270 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
271 qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
272 join("\n", map { "\tgit clone --mirror $_" } @urls);
275 my $addr = $obj->{-primary_address};
276 $ctx->{footer} = join("\n",
279 PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
280 '">public-inbox</a>, ' .
281 'anybody may post in plain-text (not HTML):',
287 # search support is optional, returns undef if Xapian is not installed
288 # or not configured for the given GIT_DIR
292 require PublicInbox::Search;
293 $ctx->{srch} = $ctx->{-inbox}->search;
300 <html><head><title>Search not available for this
301 public-inbox</title><body><pre>Search is not available for this public-inbox
302 <a href="../">Return to index</a></pre></body></html>
304 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
307 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
308 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
309 # note: I'm not a big fan of other compression formats since they're
310 # significantly more expensive on CPU than gzip and less-widely available,
311 # especially on older systems. Stick to zlib since that's what git uses.
312 sub get_thread_mbox {
313 my ($ctx, $sfx) = @_;
314 my $srch = searcher($ctx) or return need_search($ctx);
315 require PublicInbox::Mbox;
316 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
320 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
321 sub get_thread_atom {
323 searcher($ctx) or return need_search($ctx);
324 $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
325 require PublicInbox::Feed;
326 PublicInbox::Feed::generate_thread_atom($ctx);
329 sub legacy_redirects {
330 my ($self, $ctx, $path_info) = @_;
332 # single-message pages
333 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
335 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
336 r301($ctx, $1, $2, 'raw');
338 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
342 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
343 r301($ctx, $1, $2, 't/#u');
345 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
346 r301($ctx, $1, $2, "t.mbox$3");
348 # even older legacy redirects
349 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
352 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
353 r301($ctx, $1, $2, 't/#u');
355 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
358 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
359 r301($ctx, $1, $2, 'raw');
361 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
362 r301($ctx, $1, $2, "t$3");
364 # legacy convenience redirects, order still matters
365 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
367 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
368 r301($ctx, $1, $2, 't/#u');
369 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
372 # some Message-IDs have slashes in them and the HTTP server
373 # may try to be clever and unescape them :<
374 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
375 msg_page($self, $ctx, $1, $2, $3);
377 # in case people leave off the trailing slash:
378 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
379 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
380 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
383 $self->news_www->call($ctx->{cgi}->{env});
388 my ($ctx, $inbox, $mid, $suffix) = @_;
389 my $cgi = $ctx->{cgi};
390 my $obj = $ctx->{-inbox};
392 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
393 return $r404 if $r404;
394 $obj = $ctx->{-inbox};
396 my $url = $obj->base_url($cgi);
397 my $qs = $ctx->{env}->{QUERY_STRING};
398 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
399 $url .= $suffix if (defined $suffix);
400 $url .= "?$qs" if $qs ne '';
403 [ Location => $url, 'Content-Type' => 'text/plain' ],
404 [ "Redirecting to $url\n" ] ]
408 my ($self, $ctx, $inbox, $mid, $e) = @_;
410 $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
411 '' eq $e and return get_mid_html($ctx);
412 't/' eq $e and return get_thread($ctx);
413 't.atom' eq $e and return get_thread_atom($ctx);
414 't.mbox' eq $e and return get_thread_mbox($ctx);
415 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
416 'T/' eq $e and return get_thread($ctx, 1);
417 'raw' eq $e and return get_mid_txt($ctx);
419 # legacy, but no redirect for compatibility:
420 'f/' eq $e and return get_mid_html($ctx);
425 my ($env, $git, $path) = @_;
426 PublicInbox::GitHTTPBackend::serve($env, $git, $path);
429 sub serve_mbox_range {
430 my ($self, $ctx, $inbox, $range) = @_;
431 invalid_inbox($self, $ctx, $inbox) || eval {
432 require PublicInbox::Mbox;
434 PublicInbox::Mbox::emit_range($ctx, $range);
440 my $nw = $self->{news_www};
442 require PublicInbox::NewsWWW;
443 $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
447 my ($ctx, $idx, $fn) = @_;
448 require PublicInbox::WwwAttach;
449 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);