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_escape_utf8 uri_unescape);
19 require PublicInbox::Git;
20 use PublicInbox::GitHTTPBackend;
21 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
22 our $MID_RE = qr!([^/]+)!;
23 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
24 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
27 my ($class, $pi_config) = @_;
28 $pi_config ||= PublicInbox::Config->new;
29 bless { pi_config => $pi_config }, $class;
32 # backwards compatibility, do not use
34 my ($req, $method) = @_;
35 PublicInbox::WWW->new->call($req->env);
39 my ($self, $env) = @_;
40 my $ctx = { env => $env, www => $self };
42 # we don't care about multi-value
44 my ($k, $v) = split('=', $_, 2);
45 $v = '' unless defined $v;
48 } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
51 my $path_info = $env->{PATH_INFO};
52 my $method = $env->{REQUEST_METHOD};
54 if ($method eq 'POST' &&
55 $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
57 return invalid_inbox($ctx, $1) || serve_git($ctx, $path);
59 elsif ($method !~ /\AGET|HEAD\z/) {
60 return r(405, 'Method Not Allowed');
63 # top-level indices and feeds
64 if ($path_info eq '/') {
66 } elsif ($path_info =~ m!$INBOX_RE\z!o) {
67 invalid_inbox($ctx, $1) || r301($ctx, $1);
68 } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
69 invalid_inbox($ctx, $1) || get_index($ctx);
70 } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
71 invalid_inbox($ctx, $1) || get_atom($ctx);
72 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
73 invalid_inbox($ctx, $1) || get_new($ctx);
74 } elsif ($path_info =~ m!$INBOX_RE/
75 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
77 invalid_inbox($ctx, $1) || serve_git($ctx, $path);
78 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
79 serve_mbox_range($ctx, $1, $2);
80 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
81 msg_page($ctx, $1, $2, $3);
83 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
84 my ($idx, $fn) = ($3, $4);
85 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
86 # in case people leave off the trailing slash:
87 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
88 my ($inbox, $mid, $suffix) = ($1, $2, $3);
89 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
90 r301($ctx, $inbox, $mid, $suffix);
92 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
93 my ($inbox, $mid) = ($1, $2);
94 r301($ctx, $inbox, $mid, '#R');
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($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
140 sub invalid_inbox ($$) {
141 my ($ctx, $inbox) = @_;
142 my $www = $ctx->{www};
143 my $obj = $www->{pi_config}->lookup_name($inbox);
145 $ctx->{git_dir} = $obj->{mainrepo};
146 $ctx->{git} = $obj->git;
147 $ctx->{-inbox} = $obj;
148 $ctx->{inbox} = $inbox;
152 # sometimes linkifiers (not ours!) screw up automatic link
153 # generation and link things intended for nntp:// to https?://,
154 # so try to infer links and redirect them to the appropriate
156 $www->news_www->call($ctx->{env});
159 # returns undef if valid, array ref response if invalid
160 sub invalid_inbox_mid {
161 my ($ctx, $inbox, $mid) = @_;
162 my $ret = invalid_inbox($ctx, $inbox);
165 $ctx->{mid} = $mid = uri_unescape($mid);
166 if ($mid =~ /\A[a-f0-9]{40}\z/) {
167 # this is horiffically wasteful for legacy URLs:
168 if ($mid = mid2blob($ctx)) {
169 require Email::Simple;
170 use PublicInbox::MID qw/mid_clean/;
171 my $s = Email::Simple->new($mid);
172 $ctx->{mid} = mid_clean($s->header('Message-ID'));
178 # /$INBOX/new.atom -> Atom feed, includes replies
181 require PublicInbox::Feed;
182 PublicInbox::Feed::generate($ctx);
185 # /$INBOX/new.html -> HTML only
188 require PublicInbox::Feed;
189 PublicInbox::Feed::new_html($ctx);
192 # /$INBOX/?r=$GIT_COMMIT -> HTML only
195 require PublicInbox::Feed;
197 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
198 require PublicInbox::SearchView;
199 PublicInbox::SearchView::sres_top_html($ctx);
201 PublicInbox::Feed::generate_html_index($ctx);
205 # just returns a string ref for the blob in the current ctx
208 $ctx->{-inbox}->msg_by_mid($ctx->{mid});
211 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
214 my $x = mid2blob($ctx) or return r404($ctx);
215 require PublicInbox::Mbox;
216 PublicInbox::Mbox::emit1($ctx, $x);
219 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
222 my $x = mid2blob($ctx) or return r404($ctx);
224 require PublicInbox::View;
226 my $mime = Email::MIME->new($x);
228 PublicInbox::View::msg_html($ctx, $mime);
231 # /$INBOX/$MESSAGE_ID/t/
233 my ($ctx, $flat) = @_;
234 searcher($ctx) or return need_search($ctx);
235 $ctx->{flat} = $flat;
236 require PublicInbox::View;
237 PublicInbox::View::thread_html($ctx);
241 my ($ctx, $key) = @_;
242 my $val = $ctx->{$key};
243 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
247 # search support is optional, returns undef if Xapian is not installed
248 # or not configured for the given GIT_DIR
252 require PublicInbox::Search;
253 $ctx->{srch} = $ctx->{-inbox}->search;
260 <html><head><title>Search not available for this
261 public-inbox</title><body><pre>Search is not available for this public-inbox
262 <a href="../">Return to index</a></pre></body></html>
264 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
267 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
268 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
269 # note: I'm not a big fan of other compression formats since they're
270 # significantly more expensive on CPU than gzip and less-widely available,
271 # especially on older systems. Stick to zlib since that's what git uses.
272 sub get_thread_mbox {
273 my ($ctx, $sfx) = @_;
274 my $srch = searcher($ctx) or return need_search($ctx);
275 require PublicInbox::Mbox;
276 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
280 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
281 sub get_thread_atom {
283 searcher($ctx) or return need_search($ctx);
284 require PublicInbox::Feed;
285 PublicInbox::Feed::generate_thread_atom($ctx);
288 sub legacy_redirects {
289 my ($ctx, $path_info) = @_;
291 # single-message pages
292 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
294 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
295 r301($ctx, $1, $2, 'raw');
297 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
301 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
302 r301($ctx, $1, $2, 't/#u');
304 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
305 r301($ctx, $1, $2, "t.mbox$3");
307 # even older legacy redirects
308 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
311 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
312 r301($ctx, $1, $2, 't/#u');
314 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
317 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
318 r301($ctx, $1, $2, 'raw');
320 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
321 r301($ctx, $1, $2, "t$3");
323 # legacy convenience redirects, order still matters
324 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
326 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
327 r301($ctx, $1, $2, 't/#u');
328 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
331 # some Message-IDs have slashes in them and the HTTP server
332 # may try to be clever and unescape them :<
333 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
334 msg_page($ctx, $1, $2, $3);
336 # in case people leave off the trailing slash:
337 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
338 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
339 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
342 $ctx->{www}->news_www->call($ctx->{env});
347 my ($ctx, $inbox, $mid, $suffix) = @_;
348 my $obj = $ctx->{-inbox};
350 my $r404 = invalid_inbox($ctx, $inbox);
351 return $r404 if $r404;
352 $obj = $ctx->{-inbox};
354 my $url = $obj->base_url($ctx->{env});
355 my $qs = $ctx->{env}->{QUERY_STRING};
356 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
357 $url .= $suffix if (defined $suffix);
358 $url .= "?$qs" if $qs ne '';
361 [ Location => $url, 'Content-Type' => 'text/plain' ],
362 [ "Redirecting to $url\n" ] ]
366 my ($ctx, $inbox, $mid, $e) = @_;
368 $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
369 '' eq $e and return get_mid_html($ctx);
370 'T/' eq $e and return get_thread($ctx, 1);
371 't/' eq $e and return get_thread($ctx);
372 't.atom' eq $e and return get_thread_atom($ctx);
373 't.mbox' eq $e and return get_thread_mbox($ctx);
374 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
375 'raw' eq $e and return get_mid_txt($ctx);
377 # legacy, but no redirect for compatibility:
378 'f/' eq $e and return get_mid_html($ctx);
383 my ($ctx, $path) = @_;
384 PublicInbox::GitHTTPBackend::serve($ctx->{env}, $ctx->{git}, $path);
387 sub serve_mbox_range {
388 my ($ctx, $inbox, $range) = @_;
389 invalid_inbox($ctx, $inbox) || eval {
390 require PublicInbox::Mbox;
392 PublicInbox::Mbox::emit_range($ctx, $range);
398 $self->{news_www} ||= do {
399 require PublicInbox::NewsWWW;
400 PublicInbox::NewsWWW->new($self->{pi_config});
405 my ($ctx, $idx, $fn) = @_;
406 require PublicInbox::WwwAttach;
407 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);