]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
Merge branch 'unsubscribe'
[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;
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;
27
28 sub new {
29         my ($class, $pi_config) = @_;
30         $pi_config ||= PublicInbox::Config->new;
31         bless { pi_config => $pi_config }, $class;
32 }
33
34 # backwards compatibility, do not use
35 sub run {
36         my ($req, $method) = @_;
37         PublicInbox::WWW->new->call($req->env);
38 }
39
40 sub call {
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} };
45
46         # we don't care about multi-value
47         my %qp = map {
48                 my ($k, $v) = split('=', $_, 2);
49                 ($k, $v)
50         } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
51         $ctx->{qp} = \%qp;
52
53         my $path_info = $env->{PATH_INFO};
54         my $method = $env->{REQUEST_METHOD};
55
56         if ($method eq 'POST' &&
57                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
58                 my $path = $2;
59                 return (invalid_inbox($self, $ctx, $1) ||
60                         serve_git($env, $ctx->{git}, $path));
61         }
62         elsif ($method !~ /\AGET|HEAD\z/) {
63                 return r(405, 'Method Not Allowed');
64         }
65
66         # top-level indices and feeds
67         if ($path_info eq '/') {
68                 r404();
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
76         } elsif ($path_info =~ m!$INBOX_RE/
77                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
78                 my $path = $2;
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);
85
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);
95
96         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
97                 my ($inbox, $mid) = ($1, $2);
98                 r301($ctx, $inbox, $mid, '#R');
99
100         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
101                 r301($ctx, $1, $2);
102
103         # convenience redirects order matters
104         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
105                 r301($ctx, $1, $2);
106
107         } else {
108                 legacy_redirects($self, $ctx, $path_info);
109         }
110 }
111
112 # for CoW-friendliness, MOOOOO!
113 sub preload {
114         require PublicInbox::Feed;
115         require PublicInbox::View;
116         require PublicInbox::Thread;
117         require Email::MIME;
118         require Digest::SHA;
119         require POSIX;
120
121         foreach (qw(PublicInbox::Search PublicInbox::SearchView
122                         PublicInbox::Mbox IO::Compress::Gzip
123                         PublicInbox::NewsWWW)) {
124                 eval "require $_;";
125         }
126 }
127
128 # private functions below
129
130 sub r404 {
131         my ($ctx) = @_;
132         if ($ctx && $ctx->{mid}) {
133                 require PublicInbox::ExtMsg;
134                 searcher($ctx);
135                 return PublicInbox::ExtMsg::ext_msg($ctx);
136         }
137         r(404, 'Not Found');
138 }
139
140 # simple response for errors
141 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
142
143 # returns undef if valid, array ref response if invalid
144 sub invalid_inbox {
145         my ($self, $ctx, $inbox) = @_;
146         my $obj = $ctx->{pi_config}->lookup_name($inbox);
147         if (defined $obj) {
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;
154                 return;
155         }
156
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
160         # list URL.
161         $self->news_www->call($ctx->{cgi}->{env});
162 }
163
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);
168         return $ret if $ret;
169
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'));
178                 }
179         }
180         undef;
181 }
182
183 # /$INBOX/new.atom                     -> Atom feed, includes replies
184 sub get_atom {
185         my ($ctx) = @_;
186         require PublicInbox::Feed;
187         PublicInbox::Feed::generate($ctx);
188 }
189
190 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
191 sub get_index {
192         my ($ctx) = @_;
193         require PublicInbox::Feed;
194         my $srch = searcher($ctx);
195         footer($ctx);
196         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
197                 require PublicInbox::SearchView;
198                 PublicInbox::SearchView::sres_top_html($ctx);
199         } else {
200                 PublicInbox::Feed::generate_html_index($ctx);
201         }
202 }
203
204 # just returns a string ref for the blob in the current ctx
205 sub mid2blob {
206         my ($ctx) = @_;
207         require PublicInbox::MID;
208         my $path = PublicInbox::MID::mid2path($ctx->{mid});
209         $ctx->{git}->cat_file("HEAD:$path");
210 }
211
212 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
213 sub get_mid_txt {
214         my ($ctx) = @_;
215         my $x = mid2blob($ctx) or return r404($ctx);
216         require PublicInbox::Mbox;
217         PublicInbox::Mbox::emit1($ctx, $x);
218 }
219
220 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
221 sub get_mid_html {
222         my ($ctx) = @_;
223         my $x = mid2blob($ctx) or return r404($ctx);
224
225         require PublicInbox::View;
226         my $foot = footer($ctx);
227         require Email::MIME;
228         my $mime = Email::MIME->new($x);
229         searcher($ctx);
230         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
231           [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
232 }
233
234 # /$INBOX/$MESSAGE_ID/t/
235 sub get_thread {
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);
242 }
243
244 sub ctx_get {
245         my ($ctx, $key) = @_;
246         my $val = $ctx->{$key};
247         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
248         $val;
249 }
250
251 sub footer {
252         my ($ctx) = @_;
253         return '' unless $ctx;
254         my $obj = $ctx->{-inbox} or return '';
255
256         # auto-generate a footer
257         chomp(my $desc = $obj->description);
258
259         my $urls;
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>);
269         } else {
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);
273         }
274
275         my $addr = $obj->{-primary_address};
276         $ctx->{footer} = join("\n",
277                 '- ' . $desc,
278                 "A <a\nhref=\"" .
279                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
280                         '">public-inbox</a>, ' .
281                         'anybody may post in plain-text (not HTML):',
282                 $addr,
283                 $urls
284         );
285 }
286
287 # search support is optional, returns undef if Xapian is not installed
288 # or not configured for the given GIT_DIR
289 sub searcher {
290         my ($ctx) = @_;
291         eval {
292                 require PublicInbox::Search;
293                 $ctx->{srch} = $ctx->{-inbox}->search;
294         };
295 }
296
297 sub need_search {
298         my ($ctx) = @_;
299         my $msg = <<EOF;
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>
303 EOF
304         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
305 }
306
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);
317 }
318
319
320 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
321 sub get_thread_atom {
322         my ($ctx) = @_;
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);
327 }
328
329 sub legacy_redirects {
330         my ($self, $ctx, $path_info) = @_;
331
332         # single-message pages
333         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
334                 r301($ctx, $1, $2);
335         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
336                 r301($ctx, $1, $2, 'raw');
337
338         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
339                 r301($ctx, $1, $2);
340
341         # thread display
342         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
343                 r301($ctx, $1, $2, 't/#u');
344
345         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
346                 r301($ctx, $1, $2, "t.mbox$3");
347
348         # even older legacy redirects
349         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
350                 r301($ctx, $1, $2);
351
352         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
353                 r301($ctx, $1, $2, 't/#u');
354
355         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
356                 r301($ctx, $1, $2);
357
358         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
359                 r301($ctx, $1, $2, 'raw');
360
361         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
362                 r301($ctx, $1, $2, "t$3");
363
364         # legacy convenience redirects, order still matters
365         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
366                 r301($ctx, $1, $2);
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) {
370                 r301($ctx, $1, $2);
371
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);
376
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) {
381                 r301($ctx, $1, $2);
382         } else {
383                 $self->news_www->call($ctx->{cgi}->{env});
384         }
385 }
386
387 sub r301 {
388         my ($ctx, $inbox, $mid, $suffix) = @_;
389         my $cgi = $ctx->{cgi};
390         my $obj = $ctx->{-inbox};
391         unless ($obj) {
392                 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
393                 return $r404 if $r404;
394                 $obj = $ctx->{-inbox};
395         }
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 '';
401
402         [ 301,
403           [ Location => $url, 'Content-Type' => 'text/plain' ],
404           [ "Redirecting to $url\n" ] ]
405 }
406
407 sub msg_page {
408         my ($self, $ctx, $inbox, $mid, $e) = @_;
409         my $ret;
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);
418
419         # legacy, but no redirect for compatibility:
420         'f/' eq $e and return get_mid_html($ctx);
421         r404($ctx);
422 }
423
424 sub serve_git {
425         my ($env, $git, $path) = @_;
426         PublicInbox::GitHTTPBackend::serve($env, $git, $path);
427 }
428
429 sub serve_mbox_range {
430         my ($self, $ctx, $inbox, $range) = @_;
431         invalid_inbox($self, $ctx, $inbox) || eval {
432                 require PublicInbox::Mbox;
433                 searcher($ctx);
434                 PublicInbox::Mbox::emit_range($ctx, $range);
435         }
436 }
437
438 sub news_www {
439         my ($self) = @_;
440         my $nw = $self->{news_www};
441         return $nw if $nw;
442         require PublicInbox::NewsWWW;
443         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
444 }
445
446 sub get_attach {
447         my ($ctx, $idx, $fn) = @_;
448         require PublicInbox::WwwAttach;
449         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
450 }
451
452 1;