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 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 $ctx = { env => $env, www => $self, pi_config => $self->{pi_config} };
44 # we don't care about multi-value
46 my ($k, $v) = split('=', $_, 2);
47 $v = '' unless defined $v;
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);
75 } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
76 invalid_inbox($self, $ctx, $1) || get_new($ctx);
77 } elsif ($path_info =~ m!$INBOX_RE/
78 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
80 invalid_inbox($self, $ctx, $1) ||
81 serve_git($env, $ctx->{git}, $path);
82 } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
83 serve_mbox_range($self, $ctx, $1, $2);
84 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
85 msg_page($self, $ctx, $1, $2, $3);
87 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
88 my ($idx, $fn) = ($3, $4);
89 invalid_inbox_mid($self, $ctx, $1, $2) ||
90 get_attach($ctx, $idx, $fn);
91 # in case people leave off the trailing slash:
92 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
93 my ($inbox, $mid, $suffix) = ($1, $2, $3);
94 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
95 r301($ctx, $inbox, $mid, $suffix);
97 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
98 my ($inbox, $mid) = ($1, $2);
99 r301($ctx, $inbox, $mid, '#R');
101 } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
104 # convenience redirects order matters
105 } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
109 legacy_redirects($self, $ctx, $path_info);
113 # for CoW-friendliness, MOOOOO!
115 require PublicInbox::Feed;
116 require PublicInbox::View;
117 require PublicInbox::Thread;
122 foreach (qw(PublicInbox::Search PublicInbox::SearchView
123 PublicInbox::Mbox IO::Compress::Gzip
124 PublicInbox::NewsWWW)) {
129 # private functions below
133 if ($ctx && $ctx->{mid}) {
134 require PublicInbox::ExtMsg;
136 return PublicInbox::ExtMsg::ext_msg($ctx);
141 # simple response for errors
142 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
144 # returns undef if valid, array ref response if invalid
146 my ($self, $ctx, $inbox) = @_;
147 my $obj = $ctx->{pi_config}->lookup_name($inbox);
149 $ctx->{git_dir} = $obj->{mainrepo};
150 $ctx->{git} = $obj->git;
151 $ctx->{-inbox} = $obj;
152 $ctx->{inbox} = $inbox;
156 # sometimes linkifiers (not ours!) screw up automatic link
157 # generation and link things intended for nntp:// to https?://,
158 # so try to infer links and redirect them to the appropriate
160 $self->news_www->call($ctx->{env});
163 # returns undef if valid, array ref response if invalid
164 sub invalid_inbox_mid {
165 my ($self, $ctx, $inbox, $mid) = @_;
166 my $ret = invalid_inbox($self, $ctx, $inbox);
169 $ctx->{mid} = $mid = uri_unescape($mid);
170 if ($mid =~ /\A[a-f0-9]{40}\z/) {
171 # this is horiffically wasteful for legacy URLs:
172 if ($mid = mid2blob($ctx)) {
173 require Email::Simple;
174 use PublicInbox::MID qw/mid_clean/;
175 my $s = Email::Simple->new($mid);
176 $ctx->{mid} = mid_clean($s->header('Message-ID'));
182 # /$INBOX/new.atom -> Atom feed, includes replies
185 require PublicInbox::Feed;
186 PublicInbox::Feed::generate($ctx);
189 # /$INBOX/new.html -> HTML only
192 require PublicInbox::Feed;
193 PublicInbox::Feed::new_html($ctx);
196 # /$INBOX/?r=$GIT_COMMIT -> HTML only
199 require PublicInbox::Feed;
200 my $srch = searcher($ctx);
201 if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
202 require PublicInbox::SearchView;
203 PublicInbox::SearchView::sres_top_html($ctx);
205 PublicInbox::Feed::generate_html_index($ctx);
209 # just returns a string ref for the blob in the current ctx
212 $ctx->{-inbox}->msg_by_mid($ctx->{mid});
215 # /$INBOX/$MESSAGE_ID/raw -> raw mbox
218 my $x = mid2blob($ctx) or return r404($ctx);
219 require PublicInbox::Mbox;
220 PublicInbox::Mbox::emit1($ctx, $x);
223 # /$INBOX/$MESSAGE_ID/ -> HTML content (short quotes)
226 my $x = mid2blob($ctx) or return r404($ctx);
228 require PublicInbox::View;
230 my $mime = Email::MIME->new($x);
232 PublicInbox::View::msg_html($ctx, $mime);
235 # /$INBOX/$MESSAGE_ID/t/
237 my ($ctx, $flat) = @_;
238 searcher($ctx) or return need_search($ctx);
239 $ctx->{flat} = $flat;
240 require PublicInbox::View;
241 PublicInbox::View::thread_html($ctx);
245 my ($ctx, $key) = @_;
246 my $val = $ctx->{$key};
247 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
251 # search support is optional, returns undef if Xapian is not installed
252 # or not configured for the given GIT_DIR
256 require PublicInbox::Search;
257 $ctx->{srch} = $ctx->{-inbox}->search;
264 <html><head><title>Search not available for this
265 public-inbox</title><body><pre>Search is not available for this public-inbox
266 <a href="../">Return to index</a></pre></body></html>
268 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
271 # /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
272 # /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
273 # note: I'm not a big fan of other compression formats since they're
274 # significantly more expensive on CPU than gzip and less-widely available,
275 # especially on older systems. Stick to zlib since that's what git uses.
276 sub get_thread_mbox {
277 my ($ctx, $sfx) = @_;
278 my $srch = searcher($ctx) or return need_search($ctx);
279 require PublicInbox::Mbox;
280 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
284 # /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
285 sub get_thread_atom {
287 searcher($ctx) or return need_search($ctx);
288 require PublicInbox::Feed;
289 PublicInbox::Feed::generate_thread_atom($ctx);
292 sub legacy_redirects {
293 my ($self, $ctx, $path_info) = @_;
295 # single-message pages
296 if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
298 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
299 r301($ctx, $1, $2, 'raw');
301 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
305 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
306 r301($ctx, $1, $2, 't/#u');
308 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
309 r301($ctx, $1, $2, "t.mbox$3");
311 # even older legacy redirects
312 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
315 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
316 r301($ctx, $1, $2, 't/#u');
318 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
321 } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
322 r301($ctx, $1, $2, 'raw');
324 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
325 r301($ctx, $1, $2, "t$3");
327 # legacy convenience redirects, order still matters
328 } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
330 } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
331 r301($ctx, $1, $2, 't/#u');
332 } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
335 # some Message-IDs have slashes in them and the HTTP server
336 # may try to be clever and unescape them :<
337 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
338 msg_page($self, $ctx, $1, $2, $3);
340 # in case people leave off the trailing slash:
341 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
342 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
343 } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
346 $self->news_www->call($ctx->{env});
351 my ($ctx, $inbox, $mid, $suffix) = @_;
352 my $obj = $ctx->{-inbox};
354 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
355 return $r404 if $r404;
356 $obj = $ctx->{-inbox};
358 my $url = $obj->base_url($ctx->{env});
359 my $qs = $ctx->{env}->{QUERY_STRING};
360 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
361 $url .= $suffix if (defined $suffix);
362 $url .= "?$qs" if $qs ne '';
365 [ Location => $url, 'Content-Type' => 'text/plain' ],
366 [ "Redirecting to $url\n" ] ]
370 my ($self, $ctx, $inbox, $mid, $e) = @_;
372 $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
373 '' eq $e and return get_mid_html($ctx);
374 'T/' eq $e and return get_thread($ctx, 1);
375 't/' eq $e and return get_thread($ctx);
376 't.atom' eq $e and return get_thread_atom($ctx);
377 't.mbox' eq $e and return get_thread_mbox($ctx);
378 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
379 'raw' eq $e and return get_mid_txt($ctx);
381 # legacy, but no redirect for compatibility:
382 'f/' eq $e and return get_mid_html($ctx);
387 my ($env, $git, $path) = @_;
388 PublicInbox::GitHTTPBackend::serve($env, $git, $path);
391 sub serve_mbox_range {
392 my ($self, $ctx, $inbox, $range) = @_;
393 invalid_inbox($self, $ctx, $inbox) || eval {
394 require PublicInbox::Mbox;
396 PublicInbox::Mbox::emit_range($ctx, $range);
402 my $nw = $self->{news_www};
404 require PublicInbox::NewsWWW;
405 $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
409 my ($ctx, $idx, $fn) = @_;
410 require PublicInbox::WwwAttach;
411 PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);