]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
inbox: cleanup and consolidate object weakening
[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 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;
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 $ctx = { env => $env, www => $self, pi_config => $self->{pi_config} };
43
44         # we don't care about multi-value
45         my %qp = map {
46                 my ($k, $v) = split('=', $_, 2);
47                 $v = '' unless defined $v;
48                 $v =~ tr/+/ /;
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         } 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) {
79                 my $path = $2;
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);
86
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);
96
97         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
98                 my ($inbox, $mid) = ($1, $2);
99                 r301($ctx, $inbox, $mid, '#R');
100
101         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
102                 r301($ctx, $1, $2);
103
104         # convenience redirects order matters
105         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
106                 r301($ctx, $1, $2);
107
108         } else {
109                 legacy_redirects($self, $ctx, $path_info);
110         }
111 }
112
113 # for CoW-friendliness, MOOOOO!
114 sub preload {
115         require PublicInbox::Feed;
116         require PublicInbox::View;
117         require PublicInbox::Thread;
118         require Email::MIME;
119         require Digest::SHA;
120         require POSIX;
121
122         foreach (qw(PublicInbox::Search PublicInbox::SearchView
123                         PublicInbox::Mbox IO::Compress::Gzip
124                         PublicInbox::NewsWWW)) {
125                 eval "require $_;";
126         }
127 }
128
129 # private functions below
130
131 sub r404 {
132         my ($ctx) = @_;
133         if ($ctx && $ctx->{mid}) {
134                 require PublicInbox::ExtMsg;
135                 searcher($ctx);
136                 return PublicInbox::ExtMsg::ext_msg($ctx);
137         }
138         r(404, 'Not Found');
139 }
140
141 # simple response for errors
142 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
143
144 # returns undef if valid, array ref response if invalid
145 sub invalid_inbox {
146         my ($self, $ctx, $inbox) = @_;
147         my $obj = $ctx->{pi_config}->lookup_name($inbox);
148         if (defined $obj) {
149                 $ctx->{git_dir} = $obj->{mainrepo};
150                 $ctx->{git} = $obj->git;
151                 $ctx->{-inbox} = $obj;
152                 $ctx->{inbox} = $inbox;
153                 return;
154         }
155
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
159         # list URL.
160         $self->news_www->call($ctx->{env});
161 }
162
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);
167         return $ret if $ret;
168
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'));
177                 }
178         }
179         undef;
180 }
181
182 # /$INBOX/new.atom                     -> Atom feed, includes replies
183 sub get_atom {
184         my ($ctx) = @_;
185         require PublicInbox::Feed;
186         PublicInbox::Feed::generate($ctx);
187 }
188
189 # /$INBOX/new.html                      -> HTML only
190 sub get_new {
191         my ($ctx) = @_;
192         require PublicInbox::Feed;
193         PublicInbox::Feed::new_html($ctx);
194 }
195
196 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
197 sub get_index {
198         my ($ctx) = @_;
199         require PublicInbox::Feed;
200         my $srch = searcher($ctx);
201         footer($ctx);
202         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
203                 require PublicInbox::SearchView;
204                 PublicInbox::SearchView::sres_top_html($ctx);
205         } else {
206                 PublicInbox::Feed::generate_html_index($ctx);
207         }
208 }
209
210 # just returns a string ref for the blob in the current ctx
211 sub mid2blob {
212         my ($ctx) = @_;
213         $ctx->{-inbox}->msg_by_mid($ctx->{mid});
214 }
215
216 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
217 sub get_mid_txt {
218         my ($ctx) = @_;
219         my $x = mid2blob($ctx) or return r404($ctx);
220         require PublicInbox::Mbox;
221         PublicInbox::Mbox::emit1($ctx, $x);
222 }
223
224 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
225 sub get_mid_html {
226         my ($ctx) = @_;
227         my $x = mid2blob($ctx) or return r404($ctx);
228
229         require PublicInbox::View;
230         my $foot = footer($ctx);
231         require Email::MIME;
232         my $mime = Email::MIME->new($x);
233         searcher($ctx);
234         PublicInbox::View::msg_html($ctx, $mime, $foot);
235 }
236
237 # /$INBOX/$MESSAGE_ID/t/
238 sub get_thread {
239         my ($ctx, $flat) = @_;
240         searcher($ctx) or return need_search($ctx);
241         $ctx->{flat} = $flat;
242         require PublicInbox::View;
243         PublicInbox::View::thread_html($ctx);
244 }
245
246 sub ctx_get {
247         my ($ctx, $key) = @_;
248         my $val = $ctx->{$key};
249         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
250         $val;
251 }
252
253 sub footer {
254         my ($ctx) = @_;
255         return '' unless $ctx;
256         my $obj = $ctx->{-inbox} or return '';
257
258         # auto-generate a footer
259         chomp(my $desc = $obj->description);
260         $desc = PublicInbox::Hval::ascii_html($desc);
261
262         my $urls;
263         my @urls = @{$obj->cloneurl};
264         my %seen = map { $_ => 1 } @urls;
265         my $env = $ctx->{env};
266         my $http = $obj->base_url($env);
267         chop $http;
268         $seen{$http} or unshift @urls, $http;
269         my $ssoma_url = PublicInbox::Hval::prurl($env, SSOMA_URL);
270         if (scalar(@urls) == 1) {
271                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
272                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
273         } else {
274                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
275                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
276                         join("\n", map { "\tgit clone --mirror $_" } @urls);
277         }
278
279         my $addr = $obj->{-primary_address};
280         $ctx->{footer} = join("\n",
281                 '- ' . $desc,
282                 "A <a\nhref=\"" .
283                         PublicInbox::Hval::prurl($ctx->{env}, PI_URL) .
284                         '">public-inbox</a>, ' .
285                         'anybody may post in plain-text (not HTML):',
286                 $addr,
287                 $urls
288         );
289 }
290
291 # search support is optional, returns undef if Xapian is not installed
292 # or not configured for the given GIT_DIR
293 sub searcher {
294         my ($ctx) = @_;
295         eval {
296                 require PublicInbox::Search;
297                 $ctx->{srch} = $ctx->{-inbox}->search;
298         };
299 }
300
301 sub need_search {
302         my ($ctx) = @_;
303         my $msg = <<EOF;
304 <html><head><title>Search not available for this
305 public-inbox</title><body><pre>Search is not available for this public-inbox
306 <a href="../">Return to index</a></pre></body></html>
307 EOF
308         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
309 }
310
311 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
312 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
313 # note: I'm not a big fan of other compression formats since they're
314 # significantly more expensive on CPU than gzip and less-widely available,
315 # especially on older systems.  Stick to zlib since that's what git uses.
316 sub get_thread_mbox {
317         my ($ctx, $sfx) = @_;
318         my $srch = searcher($ctx) or return need_search($ctx);
319         require PublicInbox::Mbox;
320         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
321 }
322
323
324 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
325 sub get_thread_atom {
326         my ($ctx) = @_;
327         searcher($ctx) or return need_search($ctx);
328         require PublicInbox::Feed;
329         PublicInbox::Feed::generate_thread_atom($ctx);
330 }
331
332 sub legacy_redirects {
333         my ($self, $ctx, $path_info) = @_;
334
335         # single-message pages
336         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
337                 r301($ctx, $1, $2);
338         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
339                 r301($ctx, $1, $2, 'raw');
340
341         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
342                 r301($ctx, $1, $2);
343
344         # thread display
345         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
346                 r301($ctx, $1, $2, 't/#u');
347
348         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
349                 r301($ctx, $1, $2, "t.mbox$3");
350
351         # even older legacy redirects
352         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
353                 r301($ctx, $1, $2);
354
355         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
356                 r301($ctx, $1, $2, 't/#u');
357
358         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
359                 r301($ctx, $1, $2);
360
361         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
362                 r301($ctx, $1, $2, 'raw');
363
364         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
365                 r301($ctx, $1, $2, "t$3");
366
367         # legacy convenience redirects, order still matters
368         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
369                 r301($ctx, $1, $2);
370         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
371                 r301($ctx, $1, $2, 't/#u');
372         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
373                 r301($ctx, $1, $2);
374
375         # some Message-IDs have slashes in them and the HTTP server
376         # may try to be clever and unescape them :<
377         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
378                 msg_page($self, $ctx, $1, $2, $3);
379
380         # in case people leave off the trailing slash:
381         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
382                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
383         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
384                 r301($ctx, $1, $2);
385         } else {
386                 $self->news_www->call($ctx->{env});
387         }
388 }
389
390 sub r301 {
391         my ($ctx, $inbox, $mid, $suffix) = @_;
392         my $obj = $ctx->{-inbox};
393         unless ($obj) {
394                 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
395                 return $r404 if $r404;
396                 $obj = $ctx->{-inbox};
397         }
398         my $url = $obj->base_url($ctx->{env});
399         my $qs = $ctx->{env}->{QUERY_STRING};
400         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
401         $url .= $suffix if (defined $suffix);
402         $url .= "?$qs" if $qs ne '';
403
404         [ 301,
405           [ Location => $url, 'Content-Type' => 'text/plain' ],
406           [ "Redirecting to $url\n" ] ]
407 }
408
409 sub msg_page {
410         my ($self, $ctx, $inbox, $mid, $e) = @_;
411         my $ret;
412         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
413         '' eq $e and return get_mid_html($ctx);
414         'T/' eq $e and return get_thread($ctx, 1);
415         't/' eq $e and return get_thread($ctx);
416         't.atom' eq $e and return get_thread_atom($ctx);
417         't.mbox' eq $e and return get_thread_mbox($ctx);
418         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
419         'raw' eq $e and return get_mid_txt($ctx);
420
421         # legacy, but no redirect for compatibility:
422         'f/' eq $e and return get_mid_html($ctx);
423         r404($ctx);
424 }
425
426 sub serve_git {
427         my ($env, $git, $path) = @_;
428         PublicInbox::GitHTTPBackend::serve($env, $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;