]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
declare Inbox object for reusability
[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 $obj = $ctx->{pi_config}->lookup_name($inbox);
129         if (defined $obj) {
130                 $ctx->{git_dir} = $obj->{mainrepo};
131                 $ctx->{git} = $obj->git;
132                 $ctx->{-inbox} = $obj;
133                 $ctx->{inbox} = $inbox;
134                 return;
135         }
136
137         # sometimes linkifiers (not ours!) screw up automatic link
138         # generation and link things intended for nntp:// to https?://,
139         # so try to infer links and redirect them to the appropriate
140         # list URL.
141         $self->news_www->call($ctx->{cgi}->{env});
142 }
143
144 # returns undef if valid, array ref response if invalid
145 sub invalid_inbox_mid {
146         my ($self, $ctx, $inbox, $mid) = @_;
147         my $ret = invalid_inbox($self, $ctx, $inbox, $mid);
148         return $ret if $ret;
149
150         $ctx->{mid} = $mid = uri_unescape($mid);
151         if ($mid =~ /\A[a-f0-9]{40}\z/) {
152                 # this is horiffically wasteful for legacy URLs:
153                 if ($mid = mid2blob($ctx)) {
154                         require Email::Simple;
155                         use PublicInbox::MID qw/mid_clean/;
156                         my $s = Email::Simple->new($mid);
157                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
158                 }
159         }
160         undef;
161 }
162
163 # /$INBOX/new.atom                     -> Atom feed, includes replies
164 sub get_atom {
165         my ($ctx) = @_;
166         require PublicInbox::Feed;
167         PublicInbox::Feed::generate($ctx);
168 }
169
170 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
171 sub get_index {
172         my ($ctx) = @_;
173         require PublicInbox::Feed;
174         my $srch = searcher($ctx);
175         footer($ctx);
176         if (defined $ctx->{cgi}->param('q')) {
177                 require PublicInbox::SearchView;
178                 PublicInbox::SearchView::sres_top_html($ctx);
179         } else {
180                 PublicInbox::Feed::generate_html_index($ctx);
181         }
182 }
183
184 # just returns a string ref for the blob in the current ctx
185 sub mid2blob {
186         my ($ctx) = @_;
187         require PublicInbox::MID;
188         my $path = PublicInbox::MID::mid2path($ctx->{mid});
189         $ctx->{git}->cat_file("HEAD:$path");
190 }
191
192 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
193 sub get_mid_txt {
194         my ($ctx) = @_;
195         my $x = mid2blob($ctx) or return r404($ctx);
196         require PublicInbox::Mbox;
197         PublicInbox::Mbox::emit1($ctx, $x);
198 }
199
200 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
201 sub get_mid_html {
202         my ($ctx) = @_;
203         my $x = mid2blob($ctx) or return r404($ctx);
204
205         require PublicInbox::View;
206         my $foot = footer($ctx);
207         require Email::MIME;
208         my $mime = Email::MIME->new($x);
209         searcher($ctx);
210         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
211           [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
212 }
213
214 # /$INBOX/$MESSAGE_ID/R/                   -> HTML content (fullquotes)
215 sub get_reply_html {
216         my ($ctx) = @_;
217         my $x = mid2blob($ctx) or return r404($ctx);
218
219         require PublicInbox::View;
220         my $foot = footer($ctx);
221         require Email::MIME;
222         my $hdr = Email::MIME->new($x)->header_obj;
223         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
224           [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
225 }
226
227 # /$INBOX/$MESSAGE_ID/t/
228 sub get_thread {
229         my ($ctx, $flat) = @_;
230         my $srch = searcher($ctx) or return need_search($ctx);
231         require PublicInbox::View;
232         my $foot = footer($ctx);
233         $ctx->{flat} = $flat;
234         PublicInbox::View::thread_html($ctx, $foot, $srch);
235 }
236
237 sub ctx_get {
238         my ($ctx, $key) = @_;
239         my $val = $ctx->{$key};
240         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
241         $val;
242 }
243
244 sub footer {
245         my ($ctx) = @_;
246         return '' unless $ctx;
247         my $obj = $ctx->{-inbox} or return '';
248         my $footer = $obj->footer_html;
249         return $ctx->{footer} = $footer if $footer;
250
251         # auto-generate a footer
252         chomp(my $desc = $obj->description);
253
254         my $urls;
255         my @urls = @{$obj->cloneurl};
256         my %seen = map { $_ => 1 } @urls;
257         my $cgi = $ctx->{cgi};
258         my $http = $cgi->base->as_string . $obj->{name};
259         $seen{$http} or unshift @urls, $http;
260         my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
261         if (scalar(@urls) == 1) {
262                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
263                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
264         } else {
265                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
266                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
267                         join("\n", map { "\tgit clone --mirror $_" } @urls);
268         }
269
270         my $addr = $obj->{-primary_address};
271         $ctx->{footer} = join("\n",
272                 '- ' . $desc,
273                 "A <a\nhref=\"" .
274                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
275                         '">public-inbox</a>, ' .
276                         'anybody may post in plain-text (not HTML):',
277                 $addr,
278                 $urls
279         );
280 }
281
282 # search support is optional, returns undef if Xapian is not installed
283 # or not configured for the given GIT_DIR
284 sub searcher {
285         my ($ctx) = @_;
286         eval {
287                 require PublicInbox::Search;
288                 $ctx->{srch} = $ctx->{-inbox}->search;
289         };
290 }
291
292 sub need_search {
293         my ($ctx) = @_;
294         my $msg = <<EOF;
295 <html><head><title>Search not available for this
296 public-inbox</title><body><pre>Search is not available for this public-inbox
297 <a href="../">Return to index</a></pre></body></html>
298 EOF
299         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
300 }
301
302 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
303 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
304 # note: I'm not a big fan of other compression formats since they're
305 # significantly more expensive on CPU than gzip and less-widely available,
306 # especially on older systems.  Stick to zlib since that's what git uses.
307 sub get_thread_mbox {
308         my ($ctx, $sfx) = @_;
309         my $srch = searcher($ctx) or return need_search($ctx);
310         require PublicInbox::Mbox;
311         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
312 }
313
314
315 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
316 sub get_thread_atom {
317         my ($ctx) = @_;
318         searcher($ctx) or return need_search($ctx);
319         $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
320         require PublicInbox::Feed;
321         PublicInbox::Feed::generate_thread_atom($ctx);
322 }
323
324 sub legacy_redirects {
325         my ($self, $ctx, $path_info) = @_;
326
327         # single-message pages
328         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
329                 r301($ctx, $1, $2);
330         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
331                 r301($ctx, $1, $2, 'raw');
332
333         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
334                 r301($ctx, $1, $2);
335
336         # thread display
337         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
338                 r301($ctx, $1, $2, 't/#u');
339
340         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
341                 r301($ctx, $1, $2, "t.mbox$3");
342
343         # even older legacy redirects
344         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
345                 r301($ctx, $1, $2);
346
347         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
348                 r301($ctx, $1, $2, 't/#u');
349
350         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
351                 r301($ctx, $1, $2);
352
353         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
354                 r301($ctx, $1, $2, 'raw');
355
356         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
357                 r301($ctx, $1, $2, "t$3");
358
359         # legacy convenience redirects, order still matters
360         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
361                 r301($ctx, $1, $2);
362         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
363                 r301($ctx, $1, $2, 't/#u');
364         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
365                 r301($ctx, $1, $2);
366
367         # some Message-IDs have slashes in them and the HTTP server
368         # may try to be clever and unescape them :<
369         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
370                 msg_page($self, $ctx, $1, $2, $3);
371
372         # in case people leave off the trailing slash:
373         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
374                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
375         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
376                 r301($ctx, $1, $2);
377         } else {
378                 $self->news_www->call($ctx->{cgi}->{env});
379         }
380 }
381
382 sub r301 {
383         my ($ctx, $inbox, $mid, $suffix) = @_;
384         my $cgi = $ctx->{cgi};
385         my $url;
386         my $qs = $cgi->env->{QUERY_STRING};
387         $url = $cgi->base->as_string . $inbox . '/';
388         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
389         $url .= $suffix if (defined $suffix);
390         $url .= "?$qs" if $qs ne '';
391
392         [ 301,
393           [ Location => $url, 'Content-Type' => 'text/plain' ],
394           [ "Redirecting to $url\n" ] ]
395 }
396
397 sub msg_page {
398         my ($self, $ctx, $inbox, $mid, $e) = @_;
399         my $ret;
400         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
401         '' eq $e and return get_mid_html($ctx);
402         't/' eq $e and return get_thread($ctx);
403         't.atom' eq $e and return get_thread_atom($ctx);
404         't.mbox' eq $e and return get_thread_mbox($ctx);
405         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
406         'T/' eq $e and return get_thread($ctx, 1);
407         'raw' eq $e and return get_mid_txt($ctx);
408
409         # legacy, but no redirect for compatibility:
410         'f/' eq $e and return get_mid_html($ctx);
411
412         'R/' eq $e and return get_reply_html($ctx);
413         r404($ctx);
414 }
415
416 sub serve_git {
417         my ($cgi, $git, $path) = @_;
418         PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
419 }
420
421 sub serve_mbox_range {
422         my ($self, $ctx, $inbox, $range) = @_;
423         invalid_inbox($self, $ctx, $inbox) || eval {
424                 require PublicInbox::Mbox;
425                 searcher($ctx);
426                 PublicInbox::Mbox::emit_range($ctx, $range);
427         }
428 }
429
430 sub news_www {
431         my ($self) = @_;
432         my $nw = $self->{news_www};
433         return $nw if $nw;
434         require PublicInbox::NewsWWW;
435         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
436 }
437
438 1;