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