]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
mbox: support /$INBOX/all.mbox.gz endpoint
[public-inbox.git] / lib / PublicInbox / WWW.pm
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)
3 #
4 # Main web interface for mailing list archives
5 #
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;
13 use 5.008;
14 use strict;
15 use warnings;
16 use Plack::Request;
17 use PublicInbox::Config qw(try_cat);
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
27 sub new {
28         my ($class, $pi_config) = @_;
29         $pi_config ||= PublicInbox::Config->new;
30         bless { pi_config => $pi_config }, $class;
31 }
32
33 # backwards compatibility, do not use
34 sub run {
35         my ($req, $method) = @_;
36         PublicInbox::WWW->new->call($req->env);
37 }
38
39 sub call {
40         my ($self, $env) = @_;
41         my $cgi = Plack::Request->new($env);
42         my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
43         my $path_info = $cgi->path_info;
44
45         my $method = $cgi->method;
46         if ($method eq 'POST' &&
47                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
48                 my $path = $2;
49                 return (invalid_inbox($self, $ctx, $1) ||
50                         serve_git($cgi, $ctx->{git}, $path));
51         }
52         elsif ($method !~ /\AGET|HEAD\z/) {
53                 return r(405, 'Method Not Allowed');
54         }
55
56         # top-level indices and feeds
57         if ($path_info eq '/') {
58                 r404();
59         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
60                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
61         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
62                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
63         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
64                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
65
66         } elsif ($path_info =~ m!$INBOX_RE/
67                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
68                 my $path = $2;
69                 invalid_inbox($self, $ctx, $1) ||
70                         serve_git($cgi, $ctx->{git}, $path);
71         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
72                 serve_mbox_range($self, $ctx, $1, $2);
73         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
74                 msg_page($self, $ctx, $1, $2, $3);
75
76         # in case people leave off the trailing slash:
77         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
78                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
79                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
80                 r301($ctx, $inbox, $mid, $suffix);
81
82         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
83                 r301($ctx, $1, $2);
84
85         # convenience redirects order matters
86         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
87                 r301($ctx, $1, $2);
88
89         } else {
90                 legacy_redirects($self, $ctx, $path_info);
91         }
92 }
93
94 # for CoW-friendliness, MOOOOO!
95 sub preload {
96         require PublicInbox::Feed;
97         require PublicInbox::View;
98         require PublicInbox::Thread;
99         require Email::MIME;
100         require Digest::SHA;
101         require POSIX;
102
103         foreach (qw(PublicInbox::Search PublicInbox::SearchView
104                         PublicInbox::Mbox IO::Compress::Gzip
105                         PublicInbox::NewsWWW PublicInbox::NewsGroup)) {
106                 eval "require $_;";
107         }
108 }
109
110 # private functions below
111
112 sub r404 {
113         my ($ctx) = @_;
114         if ($ctx && $ctx->{mid}) {
115                 require PublicInbox::ExtMsg;
116                 searcher($ctx);
117                 return PublicInbox::ExtMsg::ext_msg($ctx);
118         }
119         r(404, 'Not Found');
120 }
121
122 # simple response for errors
123 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
124
125 # returns undef if valid, array ref response if invalid
126 sub invalid_inbox {
127         my ($self, $ctx, $inbox, $mid) = @_;
128         my $git_dir = $ctx->{pi_config}->get($inbox, "mainrepo");
129         if (defined $git_dir) {
130                 $ctx->{git_dir} = $git_dir;
131                 $ctx->{git} = PublicInbox::Git->new($git_dir);
132                 $ctx->{inbox} = $inbox;
133                 return;
134         }
135
136         # sometimes linkifiers (not ours!) screw up automatic link
137         # generation and link things intended for nntp:// to https?://,
138         # so try to infer links and redirect them to the appropriate
139         # list URL.
140         $self->news_www->call($ctx->{cgi}->{env});
141 }
142
143 # returns undef if valid, array ref response if invalid
144 sub invalid_inbox_mid {
145         my ($self, $ctx, $inbox, $mid) = @_;
146         my $ret = invalid_inbox($self, $ctx, $inbox, $mid);
147         return $ret if $ret;
148
149         $ctx->{mid} = $mid = uri_unescape($mid);
150         if ($mid =~ /\A[a-f0-9]{40}\z/) {
151                 # this is horiffically wasteful for legacy URLs:
152                 if ($mid = mid2blob($ctx)) {
153                         require Email::Simple;
154                         use PublicInbox::MID qw/mid_clean/;
155                         my $s = Email::Simple->new($mid);
156                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
157                 }
158         }
159         undef;
160 }
161
162 # /$INBOX/new.atom                     -> Atom feed, includes replies
163 sub get_atom {
164         my ($ctx) = @_;
165         require PublicInbox::Feed;
166         PublicInbox::Feed::generate($ctx);
167 }
168
169 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
170 sub get_index {
171         my ($ctx) = @_;
172         require PublicInbox::Feed;
173         my $srch = searcher($ctx);
174         footer($ctx);
175         if (defined $ctx->{cgi}->param('q')) {
176                 require PublicInbox::SearchView;
177                 PublicInbox::SearchView::sres_top_html($ctx);
178         } else {
179                 PublicInbox::Feed::generate_html_index($ctx);
180         }
181 }
182
183 # just returns a string ref for the blob in the current ctx
184 sub mid2blob {
185         my ($ctx) = @_;
186         require PublicInbox::MID;
187         my $path = PublicInbox::MID::mid2path($ctx->{mid});
188         $ctx->{git}->cat_file("HEAD:$path");
189 }
190
191 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
192 sub get_mid_txt {
193         my ($ctx) = @_;
194         my $x = mid2blob($ctx) or return r404($ctx);
195         require PublicInbox::Mbox;
196         PublicInbox::Mbox::emit1($ctx, $x);
197 }
198
199 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
200 sub get_mid_html {
201         my ($ctx) = @_;
202         my $x = mid2blob($ctx) or return r404($ctx);
203
204         require PublicInbox::View;
205         my $foot = footer($ctx);
206         require Email::MIME;
207         my $mime = Email::MIME->new($x);
208         searcher($ctx);
209         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
210           [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
211 }
212
213 # /$INBOX/$MESSAGE_ID/R/                   -> HTML content (fullquotes)
214 sub get_reply_html {
215         my ($ctx) = @_;
216         my $x = mid2blob($ctx) or return r404($ctx);
217
218         require PublicInbox::View;
219         my $foot = footer($ctx);
220         require Email::MIME;
221         my $hdr = Email::MIME->new($x)->header_obj;
222         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
223           [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
224 }
225
226 # /$INBOX/$MESSAGE_ID/t/
227 sub get_thread {
228         my ($ctx, $flat) = @_;
229         my $srch = searcher($ctx) or return need_search($ctx);
230         require PublicInbox::View;
231         my $foot = footer($ctx);
232         $ctx->{flat} = $flat;
233         PublicInbox::View::thread_html($ctx, $foot, $srch);
234 }
235
236 sub ctx_get {
237         my ($ctx, $key) = @_;
238         my $val = $ctx->{$key};
239         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
240         $val;
241 }
242
243 sub footer {
244         my ($ctx) = @_;
245         return '' unless $ctx;
246         my $git_dir = ctx_get($ctx, 'git_dir');
247
248         # favor user-supplied footer
249         my $footer = try_cat("$git_dir/public-inbox/footer.html");
250         if (defined $footer) {
251                 chomp $footer;
252                 $ctx->{footer} = $footer;
253                 return $footer;
254         }
255
256         # auto-generate a footer
257         my $inbox = ctx_get($ctx, 'inbox');
258         my $desc = try_cat("$git_dir/description");
259         $desc = '$GIT_DIR/description missing' unless defined $desc;
260         chomp $desc;
261
262         my $urls = try_cat("$git_dir/cloneurl");
263         my @urls = split(/\r?\n/, $urls || '');
264         my %seen = map { $_ => 1 } @urls;
265         my $cgi = $ctx->{cgi};
266         my $http = $cgi->base->as_string . $inbox;
267         $seen{$http} or unshift @urls, $http;
268         my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
269         if (scalar(@urls) == 1) {
270                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
271                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
272         } else {
273                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
274                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
275                         join("\n", map { "\tgit clone --mirror $_" } @urls);
276         }
277
278         my $addr = $ctx->{pi_config}->get($inbox, 'address');
279         if (ref($addr) eq 'ARRAY') {
280                 $addr = $addr->[0]; # first address is primary
281         }
282
283         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
284
285         $ctx->{footer} = join("\n",
286                 '- ' . $desc,
287                 "A <a\nhref=\"" .
288                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
289                         '">public-inbox</a>, ' .
290                         'anybody may post in plain-text (not HTML):',
291                 $addr,
292                 $urls
293         );
294 }
295
296 # search support is optional, returns undef if Xapian is not installed
297 # or not configured for the given GIT_DIR
298 sub searcher {
299         my ($ctx) = @_;
300         eval {
301                 require PublicInbox::Search;
302                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
303         };
304 }
305
306 sub need_search {
307         my ($ctx) = @_;
308         my $msg = <<EOF;
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>
312 EOF
313         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
314 }
315
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);
326 }
327
328
329 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
330 sub get_thread_atom {
331         my ($ctx) = @_;
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);
336 }
337
338 sub legacy_redirects {
339         my ($self, $ctx, $path_info) = @_;
340
341         # single-message pages
342         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
343                 r301($ctx, $1, $2);
344         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
345                 r301($ctx, $1, $2, 'raw');
346
347         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
348                 r301($ctx, $1, $2);
349
350         # thread display
351         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
352                 r301($ctx, $1, $2, 't/#u');
353
354         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
355                 r301($ctx, $1, $2, "t.mbox$3");
356
357         # even older legacy redirects
358         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
359                 r301($ctx, $1, $2);
360
361         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
362                 r301($ctx, $1, $2, 't/#u');
363
364         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
365                 r301($ctx, $1, $2);
366
367         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
368                 r301($ctx, $1, $2, 'raw');
369
370         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
371                 r301($ctx, $1, $2, "t$3");
372
373         # legacy convenience redirects, order still matters
374         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
375                 r301($ctx, $1, $2);
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) {
379                 r301($ctx, $1, $2);
380
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);
385
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) {
390                 r301($ctx, $1, $2);
391         } else {
392                 $self->news_www->call($ctx->{cgi}->{env});
393         }
394 }
395
396 sub r301 {
397         my ($ctx, $inbox, $mid, $suffix) = @_;
398         my $cgi = $ctx->{cgi};
399         my $url;
400         my $qs = $cgi->env->{QUERY_STRING};
401         $url = $cgi->base->as_string . $inbox . '/';
402         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
403         $url .= $suffix if (defined $suffix);
404         $url .= "?$qs" if $qs ne '';
405
406         [ 301,
407           [ Location => $url, 'Content-Type' => 'text/plain' ],
408           [ "Redirecting to $url\n" ] ]
409 }
410
411 sub msg_page {
412         my ($self, $ctx, $inbox, $mid, $e) = @_;
413         my $ret;
414         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
415         '' eq $e and return get_mid_html($ctx);
416         't/' eq $e and return get_thread($ctx);
417         't.atom' eq $e and return get_thread_atom($ctx);
418         't.mbox' eq $e and return get_thread_mbox($ctx);
419         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
420         'T/' eq $e and return get_thread($ctx, 1);
421         'raw' eq $e and return get_mid_txt($ctx);
422
423         # legacy, but no redirect for compatibility:
424         'f/' eq $e and return get_mid_html($ctx);
425
426         'R/' eq $e and return get_reply_html($ctx);
427         r404($ctx);
428 }
429
430 sub serve_git {
431         my ($cgi, $git, $path) = @_;
432         PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
433 }
434
435 sub serve_mbox_range {
436         my ($self, $ctx, $inbox, $range) = @_;
437         invalid_inbox($self, $ctx, $inbox) || eval {
438                 require PublicInbox::Mbox;
439                 searcher($ctx);
440                 PublicInbox::Mbox::emit_range($ctx, $range);
441         }
442 }
443
444 sub news_www {
445         my ($self) = @_;
446         my $nw = $self->{news_www};
447         return $nw if $nw;
448         require PublicInbox::NewsWWW;
449         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
450 }
451
452 1;