]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
a2c2a4a6ff2944359ad2a5012c8f2f915279183e
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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_unescape);
19 use PublicInbox::MID qw(mid_escape);
20 require PublicInbox::Git;
21 use PublicInbox::GitHTTPBackend;
22 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
23 our $MID_RE = qr!([^/]+)!;
24 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
25 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
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 $ctx = { env => $env, www => $self };
42
43         # we don't care about multi-value
44         my %qp = map {
45                 utf8::decode($_);
46                 my ($k, $v) = split('=', uri_unescape($_), 2);
47                 $v = '' unless defined $v;
48                 $v =~ tr/+/ /;
49                 ($k, $v)
50         } split(/[&;]+/, $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                 if ($path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
58                         my $path = $2;
59                         return invalid_inbox($ctx, $1) ||
60                                 serve_git($ctx, $path);
61                 } elsif ($path_info =~ m!$INBOX_RE/!o) {
62                         return invalid_inbox($ctx, $1) || mbox_results($ctx);
63                 }
64         }
65         elsif ($method !~ /\AGET|HEAD\z/) {
66                 return r(405, 'Method Not Allowed');
67         }
68
69         # top-level indices and feeds
70         if ($path_info eq '/') {
71                 r404();
72         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
73                 invalid_inbox($ctx, $1) || r301($ctx, $1);
74         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
75                 invalid_inbox($ctx, $1) || get_index($ctx);
76         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
77                 invalid_inbox($ctx, $1) || get_atom($ctx);
78         } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
79                 invalid_inbox($ctx, $1) || get_new($ctx);
80         } elsif ($path_info =~ m!$INBOX_RE/
81                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
82                 my $path = $2;
83                 invalid_inbox($ctx, $1) || serve_git($ctx, $path);
84         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
85                 serve_mbox_range($ctx, $1, $2);
86         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
87                 msg_page($ctx, $1, $2, $3);
88
89         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
90                 my ($idx, $fn) = ($3, $4);
91                 invalid_inbox_mid($ctx, $1, $2) || get_attach($ctx, $idx, $fn);
92         # in case people leave off the trailing slash:
93         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
94                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
95                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
96                 r301($ctx, $inbox, $mid, $suffix);
97
98         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
99                 my ($inbox, $mid) = ($1, $2);
100                 r301($ctx, $inbox, $mid, '#R');
101
102         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
103                 r301($ctx, $1, $2);
104         } elsif ($path_info =~ m!$INBOX_RE/_/text(?:/(.*))?\z!o) {
105                 get_text($ctx, $1, $2);
106
107         # convenience redirects order matters
108         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
109                 r301($ctx, $1, $2);
110
111         } else {
112                 legacy_redirects($ctx, $path_info);
113         }
114 }
115
116 # for CoW-friendliness, MOOOOO!
117 sub preload {
118         require PublicInbox::Feed;
119         require PublicInbox::View;
120         require PublicInbox::SearchThread;
121         require PublicInbox::MIME;
122         require Digest::SHA;
123         require POSIX;
124
125         foreach (qw(PublicInbox::Search PublicInbox::SearchView
126                         PublicInbox::Mbox IO::Compress::Gzip
127                         PublicInbox::NewsWWW)) {
128                 eval "require $_;";
129         }
130 }
131
132 # private functions below
133
134 sub r404 {
135         my ($ctx) = @_;
136         if ($ctx && $ctx->{mid}) {
137                 require PublicInbox::ExtMsg;
138                 searcher($ctx);
139                 return PublicInbox::ExtMsg::ext_msg($ctx);
140         }
141         r(404, 'Not Found');
142 }
143
144 # simple response for errors
145 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
146
147 # returns undef if valid, array ref response if invalid
148 sub invalid_inbox ($$) {
149         my ($ctx, $inbox) = @_;
150         my $www = $ctx->{www};
151         my $obj = $www->{pi_config}->lookup_name($inbox);
152         if (defined $obj) {
153                 $ctx->{git} = $obj->git;
154                 $ctx->{-inbox} = $obj;
155                 return;
156         }
157
158         # sometimes linkifiers (not ours!) screw up automatic link
159         # generation and link things intended for nntp:// to https?://,
160         # so try to infer links and redirect them to the appropriate
161         # list URL.
162         $www->news_www->call($ctx->{env});
163 }
164
165 # returns undef if valid, array ref response if invalid
166 sub invalid_inbox_mid {
167         my ($ctx, $inbox, $mid) = @_;
168         my $ret = invalid_inbox($ctx, $inbox);
169         return $ret if $ret;
170
171         $ctx->{mid} = $mid;
172         if ($mid =~ /\A[a-f0-9]{40}\z/) {
173                 # this is horiffically wasteful for legacy URLs:
174                 if ($mid = mid2blob($ctx)) {
175                         require Email::Simple;
176                         use PublicInbox::MID qw/mid_clean/;
177                         my $s = Email::Simple->new($mid);
178                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
179                 }
180         }
181         undef;
182 }
183
184 # /$INBOX/new.atom                     -> Atom feed, includes replies
185 sub get_atom {
186         my ($ctx) = @_;
187         require PublicInbox::Feed;
188         PublicInbox::Feed::generate($ctx);
189 }
190
191 # /$INBOX/new.html                      -> HTML only
192 sub get_new {
193         my ($ctx) = @_;
194         require PublicInbox::Feed;
195         PublicInbox::Feed::new_html($ctx);
196 }
197
198 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
199 sub get_index {
200         my ($ctx) = @_;
201         require PublicInbox::Feed;
202         searcher($ctx);
203         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
204                 require PublicInbox::SearchView;
205                 PublicInbox::SearchView::sres_top_html($ctx);
206         } else {
207                 PublicInbox::Feed::generate_html_index($ctx);
208         }
209 }
210
211 # just returns a string ref for the blob in the current ctx
212 sub mid2blob {
213         my ($ctx) = @_;
214         $ctx->{-inbox}->msg_by_mid($ctx->{mid});
215 }
216
217 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
218 sub get_mid_txt {
219         my ($ctx) = @_;
220         require PublicInbox::Mbox;
221         PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
222 }
223
224 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
225 sub get_mid_html {
226         my ($ctx) = @_;
227         require PublicInbox::View;
228         searcher($ctx);
229         PublicInbox::View::msg_page($ctx) || r404($ctx);
230 }
231
232 # /$INBOX/$MESSAGE_ID/t/
233 sub get_thread {
234         my ($ctx, $flat) = @_;
235         searcher($ctx) or return need_search($ctx);
236         $ctx->{flat} = $flat;
237         require PublicInbox::View;
238         PublicInbox::View::thread_html($ctx);
239 }
240
241 # /$INBOX/_/text/$KEY/
242 # /$INBOX/_/text/$KEY/raw
243 # KEY may contain slashes
244 sub get_text {
245         my ($ctx, $inbox, $key) = @_;
246         my $r404 = invalid_inbox($ctx, $inbox);
247         return $r404 if $r404;
248
249         require PublicInbox::WwwText;
250         PublicInbox::WwwText::get_text($ctx, $key);
251 }
252
253 sub ctx_get {
254         my ($ctx, $key) = @_;
255         my $val = $ctx->{$key};
256         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
257         $val;
258 }
259
260 # search support is optional, returns undef if Xapian is not installed
261 # or not configured for the given GIT_DIR
262 sub searcher {
263         my ($ctx) = @_;
264         eval {
265                 require PublicInbox::Search;
266                 $ctx->{srch} = $ctx->{-inbox}->search;
267         };
268 }
269
270 sub need_search {
271         my ($ctx) = @_;
272         my $msg = <<EOF;
273 <html><head><title>Search not available for this
274 public-inbox</title><body><pre>Search is not available for this public-inbox
275 <a href="../">Return to index</a></pre></body></html>
276 EOF
277         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
278 }
279
280 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
281 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
282 # note: I'm not a big fan of other compression formats since they're
283 # significantly more expensive on CPU than gzip and less-widely available,
284 # especially on older systems.  Stick to zlib since that's what git uses.
285 sub get_thread_mbox {
286         my ($ctx, $sfx) = @_;
287         my $srch = searcher($ctx) or return need_search($ctx);
288         require PublicInbox::Mbox;
289         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
290 }
291
292
293 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
294 sub get_thread_atom {
295         my ($ctx) = @_;
296         searcher($ctx) or return need_search($ctx);
297         require PublicInbox::Feed;
298         PublicInbox::Feed::generate_thread_atom($ctx);
299 }
300
301 sub legacy_redirects {
302         my ($ctx, $path_info) = @_;
303
304         # single-message pages
305         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
306                 r301($ctx, $1, $2);
307         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
308                 r301($ctx, $1, $2, 'raw');
309
310         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
311                 r301($ctx, $1, $2);
312
313         # thread display
314         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
315                 r301($ctx, $1, $2, 't/#u');
316
317         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
318                 r301($ctx, $1, $2, "t.mbox$3");
319
320         # even older legacy redirects
321         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
322                 r301($ctx, $1, $2);
323
324         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
325                 r301($ctx, $1, $2, 't/#u');
326
327         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
328                 r301($ctx, $1, $2);
329
330         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
331                 r301($ctx, $1, $2, 'raw');
332
333         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
334                 r301($ctx, $1, $2, "t$3");
335
336         # legacy convenience redirects, order still matters
337         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
338                 r301($ctx, $1, $2);
339         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
340                 r301($ctx, $1, $2, 't/#u');
341         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
342                 r301($ctx, $1, $2);
343
344         # some Message-IDs have slashes in them and the HTTP server
345         # may try to be clever and unescape them :<
346         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
347                 msg_page($ctx, $1, $2, $3);
348
349         # in case people leave off the trailing slash:
350         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
351                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
352         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
353                 r301($ctx, $1, $2);
354         } else {
355                 $ctx->{www}->news_www->call($ctx->{env});
356         }
357 }
358
359 sub r301 {
360         my ($ctx, $inbox, $mid, $suffix) = @_;
361         my $obj = $ctx->{-inbox};
362         unless ($obj) {
363                 my $r404 = invalid_inbox($ctx, $inbox);
364                 return $r404 if $r404;
365                 $obj = $ctx->{-inbox};
366         }
367         my $url = $obj->base_url($ctx->{env});
368         my $qs = $ctx->{env}->{QUERY_STRING};
369         $url .= (mid_escape($mid) . '/') if (defined $mid);
370         $url .= $suffix if (defined $suffix);
371         $url .= "?$qs" if $qs ne '';
372
373         [ 301,
374           [ Location => $url, 'Content-Type' => 'text/plain' ],
375           [ "Redirecting to $url\n" ] ]
376 }
377
378 sub msg_page {
379         my ($ctx, $inbox, $mid, $e) = @_;
380         my $ret;
381         $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
382         '' eq $e and return get_mid_html($ctx);
383         'T/' eq $e and return get_thread($ctx, 1);
384         't/' eq $e and return get_thread($ctx);
385         't.atom' eq $e and return get_thread_atom($ctx);
386         't.mbox' eq $e and return get_thread_mbox($ctx);
387         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
388         'raw' eq $e and return get_mid_txt($ctx);
389
390         # legacy, but no redirect for compatibility:
391         'f/' eq $e and return get_mid_html($ctx);
392         r404($ctx);
393 }
394
395 sub serve_git {
396         my ($ctx, $path) = @_;
397         PublicInbox::GitHTTPBackend::serve($ctx->{env}, $ctx->{git}, $path);
398 }
399
400 sub mbox_results {
401         my ($ctx) = @_;
402         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
403                 searcher($ctx) or return need_search($ctx);
404                 require PublicInbox::SearchView;
405                 return PublicInbox::SearchView::mbox_results($ctx);
406         }
407         r404();
408 }
409
410 sub serve_mbox_range {
411         my ($ctx, $inbox, $range) = @_;
412         invalid_inbox($ctx, $inbox) || eval {
413                 require PublicInbox::Mbox;
414                 searcher($ctx);
415                 PublicInbox::Mbox::emit_range($ctx, $range);
416         }
417 }
418
419 sub news_www {
420         my ($self) = @_;
421         $self->{news_www} ||= do {
422                 require PublicInbox::NewsWWW;
423                 PublicInbox::NewsWWW->new($self->{pi_config});
424         }
425 }
426
427 sub get_attach {
428         my ($ctx, $idx, $fn) = @_;
429         require PublicInbox::WwwAttach;
430         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
431 }
432
433 1;