]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
config: remove try_cat
[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/|R/|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, pi_config => $self->{pi_config}, www => $self};
44         my $path_info = $cgi->path_info;
45
46         my $method = $cgi->method;
47         if ($method eq 'POST' &&
48                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
49                 my $path = $2;
50                 return (invalid_inbox($self, $ctx, $1) ||
51                         serve_git($cgi, $ctx->{git}, $path));
52         }
53         elsif ($method !~ /\AGET|HEAD\z/) {
54                 return r(405, 'Method Not Allowed');
55         }
56
57         # top-level indices and feeds
58         if ($path_info eq '/') {
59                 r404();
60         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
61                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
62         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
63                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
64         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
65                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
66
67         } elsif ($path_info =~ m!$INBOX_RE/
68                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
69                 my $path = $2;
70                 invalid_inbox($self, $ctx, $1) ||
71                         serve_git($cgi, $ctx->{git}, $path);
72         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
73                 serve_mbox_range($self, $ctx, $1, $2);
74         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
75                 msg_page($self, $ctx, $1, $2, $3);
76
77         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
78                 my ($idx, $fn) = ($3, $4);
79                 invalid_inbox_mid($self, $ctx, $1, $2) ||
80                         get_attach($ctx, $idx, $fn);
81         # in case people leave off the trailing slash:
82         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
83                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
84                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
85                 r301($ctx, $inbox, $mid, $suffix);
86
87         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
88                 r301($ctx, $1, $2);
89
90         # convenience redirects order matters
91         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
92                 r301($ctx, $1, $2);
93
94         } else {
95                 legacy_redirects($self, $ctx, $path_info);
96         }
97 }
98
99 # for CoW-friendliness, MOOOOO!
100 sub preload {
101         require PublicInbox::Feed;
102         require PublicInbox::View;
103         require PublicInbox::Thread;
104         require Email::MIME;
105         require Digest::SHA;
106         require POSIX;
107
108         foreach (qw(PublicInbox::Search PublicInbox::SearchView
109                         PublicInbox::Mbox IO::Compress::Gzip
110                         PublicInbox::NewsWWW PublicInbox::NewsGroup)) {
111                 eval "require $_;";
112         }
113 }
114
115 # private functions below
116
117 sub r404 {
118         my ($ctx) = @_;
119         if ($ctx && $ctx->{mid}) {
120                 require PublicInbox::ExtMsg;
121                 searcher($ctx);
122                 return PublicInbox::ExtMsg::ext_msg($ctx);
123         }
124         r(404, 'Not Found');
125 }
126
127 # simple response for errors
128 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
129
130 # returns undef if valid, array ref response if invalid
131 sub invalid_inbox {
132         my ($self, $ctx, $inbox) = @_;
133         my $obj = $ctx->{pi_config}->lookup_name($inbox);
134         if (defined $obj) {
135                 $ctx->{git_dir} = $obj->{mainrepo};
136                 $ctx->{git} = $obj->git;
137                 # for PublicInbox::HTTP::weaken_task:
138                 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
139                 $ctx->{-inbox} = $obj;
140                 $ctx->{inbox} = $inbox;
141                 return;
142         }
143
144         # sometimes linkifiers (not ours!) screw up automatic link
145         # generation and link things intended for nntp:// to https?://,
146         # so try to infer links and redirect them to the appropriate
147         # list URL.
148         $self->news_www->call($ctx->{cgi}->{env});
149 }
150
151 # returns undef if valid, array ref response if invalid
152 sub invalid_inbox_mid {
153         my ($self, $ctx, $inbox, $mid) = @_;
154         my $ret = invalid_inbox($self, $ctx, $inbox);
155         return $ret if $ret;
156
157         $ctx->{mid} = $mid = uri_unescape($mid);
158         if ($mid =~ /\A[a-f0-9]{40}\z/) {
159                 # this is horiffically wasteful for legacy URLs:
160                 if ($mid = mid2blob($ctx)) {
161                         require Email::Simple;
162                         use PublicInbox::MID qw/mid_clean/;
163                         my $s = Email::Simple->new($mid);
164                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
165                 }
166         }
167         undef;
168 }
169
170 # /$INBOX/new.atom                     -> Atom feed, includes replies
171 sub get_atom {
172         my ($ctx) = @_;
173         require PublicInbox::Feed;
174         PublicInbox::Feed::generate($ctx);
175 }
176
177 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
178 sub get_index {
179         my ($ctx) = @_;
180         require PublicInbox::Feed;
181         my $srch = searcher($ctx);
182         footer($ctx);
183         if (defined $ctx->{cgi}->param('q')) {
184                 require PublicInbox::SearchView;
185                 PublicInbox::SearchView::sres_top_html($ctx);
186         } else {
187                 PublicInbox::Feed::generate_html_index($ctx);
188         }
189 }
190
191 # just returns a string ref for the blob in the current ctx
192 sub mid2blob {
193         my ($ctx) = @_;
194         require PublicInbox::MID;
195         my $path = PublicInbox::MID::mid2path($ctx->{mid});
196         $ctx->{git}->cat_file("HEAD:$path");
197 }
198
199 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
200 sub get_mid_txt {
201         my ($ctx) = @_;
202         my $x = mid2blob($ctx) or return r404($ctx);
203         require PublicInbox::Mbox;
204         PublicInbox::Mbox::emit1($ctx, $x);
205 }
206
207 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
208 sub get_mid_html {
209         my ($ctx) = @_;
210         my $x = mid2blob($ctx) or return r404($ctx);
211
212         require PublicInbox::View;
213         my $foot = footer($ctx);
214         require Email::MIME;
215         my $mime = Email::MIME->new($x);
216         searcher($ctx);
217         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
218           [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
219 }
220
221 # /$INBOX/$MESSAGE_ID/R/                   -> HTML content (fullquotes)
222 sub get_reply_html {
223         my ($ctx) = @_;
224         my $x = mid2blob($ctx) or return r404($ctx);
225
226         require PublicInbox::View;
227         my $foot = footer($ctx);
228         require Email::MIME;
229         my $hdr = Email::MIME->new($x)->header_obj;
230         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
231           [ PublicInbox::View::msg_reply($ctx, $hdr, $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($cgi->{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 = $cgi->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
422         'R/' eq $e and return get_reply_html($ctx);
423         r404($ctx);
424 }
425
426 sub serve_git {
427         my ($cgi, $git, $path) = @_;
428         PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
429 }
430
431 sub serve_mbox_range {
432         my ($self, $ctx, $inbox, $range) = @_;
433         invalid_inbox($self, $ctx, $inbox) || eval {
434                 require PublicInbox::Mbox;
435                 searcher($ctx);
436                 PublicInbox::Mbox::emit_range($ctx, $range);
437         }
438 }
439
440 sub news_www {
441         my ($self) = @_;
442         my $nw = $self->{news_www};
443         return $nw if $nw;
444         require PublicInbox::NewsWWW;
445         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
446 }
447
448 sub get_attach {
449         my ($ctx, $idx, $fn) = @_;
450         require PublicInbox::WwwAttach;
451         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
452 }
453
454 1;