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;
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
45 my ($k, $v) = split('=', uri_unescape($_), 2);
46 $v = '' unless defined $v;
49 } split(/[&;]/, $env->{QUERY_STRING});
52 my $path_info = $env->{PATH_INFO};
53 my $method = $env->{REQUEST_METHOD};
55 if ($method eq 'POST' &&
56 $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
58 return invalid_inbox($ctx, $1) || serve_git($ctx, $path);
60 elsif ($method !~ /\AGET|HEAD\z/) {
61 return r(405, 'Method Not Allowed');
64 # top-level indices and feeds
65 if ($path_info eq '/') {
67 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
68 invalid_inbox($ctx, $1) || r301($ctx, $1);
69 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
70 invalid_inbox($ctx, $1) || get_index($ctx);
71 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
72 invalid_inbox($ctx, $1) || get_atom($ctx);
73 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
74 invalid_inbox($ctx, $1) || get_new($ctx);
75 } elsif ($path_info =~ m!$INBOX_RE/
76 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
78 invalid_inbox($ctx, $1) || serve_git($ctx, $path);
79 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
80 serve_mbox_range($ctx, $1, $2);
81 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
82 msg_page($ctx, $1, $2, $3);
84 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
85 my ($idx, $fn) = ($3, $4);
86 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
87 # in case people leave off the trailing slash:
88 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
89 my ($inbox, $mid, $suffix) = ($1, $2, $3);
90 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
91 r301($ctx, $inbox, $mid, $suffix);
93 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
94 my ($inbox, $mid) = ($1, $2);
95 r301($ctx, $inbox, $mid, '#R');
97 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
100 # convenience redirects order matters
101 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
105 legacy_redirects($ctx, $path_info);
109 # for CoW-friendliness, MOOOOO!
111 require PublicInbox::Feed;
112 require PublicInbox::View;
113 require PublicInbox::Thread;
118 foreach (qw(PublicInbox::Search PublicInbox::SearchView
119 PublicInbox::Mbox IO::Compress::Gzip
120 PublicInbox::NewsWWW)) {
125 # private functions below
129 if ($ctx && $ctx->{mid}) {
130 require PublicInbox::ExtMsg;
132 return PublicInbox::ExtMsg::ext_msg($ctx);
137 # simple response for errors
138 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
140 # returns undef if valid, array ref response if invalid
141 sub invalid_inbox ($$) {
142 my ($ctx, $inbox) = @_;
143 my $www = $ctx->{www};
144 my $obj = $www->{pi_config}->lookup_name($inbox);
146 $ctx->{git_dir} = $obj->{mainrepo};
147 $ctx->{git} = $obj->git;
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 $www->news_www->call($ctx->{env});
160 # returns undef if valid, array ref response if invalid
161 sub invalid_inbox_mid {
162 my ($ctx, $inbox, $mid) = @_;
163 my $ret = invalid_inbox($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/new.html -> HTML only
189 require PublicInbox::Feed;
190 PublicInbox::Feed::new_html($ctx);
193 # /$INBOX/?r=$GIT_COMMIT -> HTML only
196 require PublicInbox::Feed;
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;
227 my $mime = Email::MIME->new($x);
229 PublicInbox::View::msg_html($ctx, $mime);
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);
242 my ($ctx, $key) = @_;
243 my $val = $ctx->{$key};
244 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
248 # search support is optional, returns undef if Xapian is not installed
249 # or not configured for the given GIT_DIR
253 require PublicInbox::Search;
254 $ctx->{srch} = $ctx->{-inbox}->search;
261 <html><head><title>Search not available for this
262 public-inbox</title><body><pre>Search is not available for this public-inbox
263 <a href="../">Return to index</a></pre></body></html>
265 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
268 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
269 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
270 # note: I'm not a big fan of other compression formats since they're
271 # significantly more expensive on CPU than gzip and less-widely available,
272 # especially on older systems. Stick to zlib since that's what git uses.
273 sub get_thread_mbox {
274 my ($ctx, $sfx) = @_;
275 my $srch = searcher($ctx) or return need_search($ctx);
276 require PublicInbox::Mbox;
277 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
281 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
282 sub get_thread_atom {
284 searcher($ctx) or return need_search($ctx);
285 require PublicInbox::Feed;
286 PublicInbox::Feed::generate_thread_atom($ctx);
289 sub legacy_redirects {
290 my ($ctx, $path_info) = @_;
292 # single-message pages
293 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
295 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
296 r301($ctx, $1, $2, 'raw');
298 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
302 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
303 r301($ctx, $1, $2, 't/#u');
305 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
306 r301($ctx, $1, $2, "t.mbox$3");
308 # even older legacy redirects
309 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
312 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
313 r301($ctx, $1, $2, 't/#u');
315 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
318 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
319 r301($ctx, $1, $2, 'raw');
321 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
322 r301($ctx, $1, $2, "t$3");
324 # legacy convenience redirects, order still matters
325 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
327 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
328 r301($ctx, $1, $2, 't/#u');
329 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
332 # some Message-IDs have slashes in them and the HTTP server
333 # may try to be clever and unescape them :<
334 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
335 msg_page($ctx, $1, $2, $3);
337 # in case people leave off the trailing slash:
338 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
339 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
340 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
343 $ctx->{www}->news_www->call($ctx->{env});
348 my ($ctx, $inbox, $mid, $suffix) = @_;
349 my $obj = $ctx->{-inbox};
351 my $r404 = invalid_inbox($ctx, $inbox);
352 return $r404 if $r404;
353 $obj = $ctx->{-inbox};
355 my $url = $obj->base_url($ctx->{env});
356 my $qs = $ctx->{env}->{QUERY_STRING};
357 $url .= (mid_escape($mid) . '/') if (defined $mid);
358 $url .= $suffix if (defined $suffix);
359 $url .= "?$qs" if $qs ne '';
362 [ Location => $url, 'Content-Type' => 'text/plain' ],
363 [ "Redirecting to $url\n" ] ]
367 my ($ctx, $inbox, $mid, $e) = @_;
369 $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
370 '' eq $e and return get_mid_html($ctx);
371 'T/' eq $e and return get_thread($ctx, 1);
372 't/' eq $e and return get_thread($ctx);
373 't.atom' eq $e and return get_thread_atom($ctx);
374 't.mbox' eq $e and return get_thread_mbox($ctx);
375 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
376 'raw' eq $e and return get_mid_txt($ctx);
378 # legacy, but no redirect for compatibility:
379 'f/' eq $e and return get_mid_html($ctx);
384 my ($ctx, $path) = @_;
385 PublicInbox::GitHTTPBackend::serve($ctx->{env}, $ctx->{git}, $path);
388 sub serve_mbox_range {
389 my ($ctx, $inbox, $range) = @_;
390 invalid_inbox($ctx, $inbox) || eval {
391 require PublicInbox::Mbox;
393 PublicInbox::Mbox::emit_range($ctx, $range);
399 $self->{news_www} ||= do {
400 require PublicInbox::NewsWWW;
401 PublicInbox::NewsWWW->new($self->{pi_config});
406 my ($ctx, $idx, $fn) = @_;
407 require PublicInbox::WwwAttach;
408 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);