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