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