]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: cleanup expensive fallback for legacy URLs
[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/(?:(\d+)/)?(git-upload-pack)\z!) {
58                         my ($part, $path) = ($2, $3);
59                         return invalid_inbox($ctx, $1) ||
60                                 serve_git($ctx, $part, $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/(?:(\d+)/)?
81                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
82                 my ($part, $path) = ($2, $3);
83                 invalid_inbox($ctx, $1) || serve_git($ctx, $part, $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         my $ibx = $ctx->{-inbox};
173         if ($mid =~ m!\A([a-f0-9]{2})([a-f0-9]{38})\z!) {
174                 my ($x2, $x38) = ($1, $2);
175                 # this is horrifically wasteful for legacy URLs:
176                 my $str = $ctx->{-inbox}->msg_by_path("$x2/$x38") or return;
177                 require Email::Simple;
178                 my $s = Email::Simple->new($str);
179                 $mid = PublicInbox::MID::mid_clean($s->header('Message-ID'));
180                 return r301($ctx, $inbox, $mid);
181         }
182         undef;
183 }
184
185 # /$INBOX/new.atom                     -> Atom feed, includes replies
186 sub get_atom {
187         my ($ctx) = @_;
188         require PublicInbox::Feed;
189         PublicInbox::Feed::generate($ctx);
190 }
191
192 # /$INBOX/new.html                      -> HTML only
193 sub get_new {
194         my ($ctx) = @_;
195         require PublicInbox::Feed;
196         PublicInbox::Feed::new_html($ctx);
197 }
198
199 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
200 sub get_index {
201         my ($ctx) = @_;
202         require PublicInbox::Feed;
203         searcher($ctx);
204         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
205                 require PublicInbox::SearchView;
206                 PublicInbox::SearchView::sres_top_html($ctx);
207         } else {
208                 PublicInbox::Feed::generate_html_index($ctx);
209         }
210 }
211
212 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
213 sub get_mid_txt {
214         my ($ctx) = @_;
215         require PublicInbox::Mbox;
216         PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
217 }
218
219 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
220 sub get_mid_html {
221         my ($ctx) = @_;
222         require PublicInbox::View;
223         searcher($ctx);
224         PublicInbox::View::msg_page($ctx) || r404($ctx);
225 }
226
227 # /$INBOX/$MESSAGE_ID/t/
228 sub get_thread {
229         my ($ctx, $flat) = @_;
230         searcher($ctx) or return need_search($ctx);
231         $ctx->{flat} = $flat;
232         require PublicInbox::View;
233         PublicInbox::View::thread_html($ctx);
234 }
235
236 # /$INBOX/_/text/$KEY/
237 # /$INBOX/_/text/$KEY/raw
238 # KEY may contain slashes
239 sub get_text {
240         my ($ctx, $inbox, $key) = @_;
241         my $r404 = invalid_inbox($ctx, $inbox);
242         return $r404 if $r404;
243
244         require PublicInbox::WwwText;
245         PublicInbox::WwwText::get_text($ctx, $key);
246 }
247
248 sub ctx_get {
249         my ($ctx, $key) = @_;
250         my $val = $ctx->{$key};
251         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
252         $val;
253 }
254
255 # search support is optional, returns undef if Xapian is not installed
256 # or not configured for the given GIT_DIR
257 sub searcher {
258         my ($ctx) = @_;
259         eval {
260                 require PublicInbox::Search;
261                 $ctx->{srch} = $ctx->{-inbox}->search;
262         };
263 }
264
265 sub need_search {
266         my ($ctx) = @_;
267         my $msg = <<EOF;
268 <html><head><title>Search not available for this
269 public-inbox</title><body><pre>Search is not available for this public-inbox
270 <a href="../">Return to index</a></pre></body></html>
271 EOF
272         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
273 }
274
275 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
276 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
277 # note: I'm not a big fan of other compression formats since they're
278 # significantly more expensive on CPU than gzip and less-widely available,
279 # especially on older systems.  Stick to zlib since that's what git uses.
280 sub get_thread_mbox {
281         my ($ctx, $sfx) = @_;
282         my $srch = searcher($ctx) or return need_search($ctx);
283         require PublicInbox::Mbox;
284         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
285 }
286
287
288 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
289 sub get_thread_atom {
290         my ($ctx) = @_;
291         searcher($ctx) or return need_search($ctx);
292         require PublicInbox::Feed;
293         PublicInbox::Feed::generate_thread_atom($ctx);
294 }
295
296 sub legacy_redirects {
297         my ($ctx, $path_info) = @_;
298
299         # single-message pages
300         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
301                 r301($ctx, $1, $2);
302         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
303                 r301($ctx, $1, $2, 'raw');
304
305         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
306                 r301($ctx, $1, $2);
307
308         # thread display
309         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
310                 r301($ctx, $1, $2, 't/#u');
311
312         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
313                 r301($ctx, $1, $2, "t.mbox$3");
314
315         # even older legacy redirects
316         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
317                 r301($ctx, $1, $2);
318
319         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
320                 r301($ctx, $1, $2, 't/#u');
321
322         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
323                 r301($ctx, $1, $2);
324
325         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
326                 r301($ctx, $1, $2, 'raw');
327
328         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
329                 r301($ctx, $1, $2, "t$3");
330
331         # legacy convenience redirects, order still matters
332         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
333                 r301($ctx, $1, $2);
334         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
335                 r301($ctx, $1, $2, 't/#u');
336         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
337                 r301($ctx, $1, $2);
338
339         # some Message-IDs have slashes in them and the HTTP server
340         # may try to be clever and unescape them :<
341         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
342                 msg_page($ctx, $1, $2, $3);
343
344         # in case people leave off the trailing slash:
345         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
346                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
347         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
348                 r301($ctx, $1, $2);
349         } else {
350                 $ctx->{www}->news_www->call($ctx->{env});
351         }
352 }
353
354 sub r301 {
355         my ($ctx, $inbox, $mid, $suffix) = @_;
356         my $obj = $ctx->{-inbox};
357         unless ($obj) {
358                 my $r404 = invalid_inbox($ctx, $inbox);
359                 return $r404 if $r404;
360                 $obj = $ctx->{-inbox};
361         }
362         my $url = $obj->base_url($ctx->{env});
363         my $qs = $ctx->{env}->{QUERY_STRING};
364         $url .= (mid_escape($mid) . '/') if (defined $mid);
365         $url .= $suffix if (defined $suffix);
366         $url .= "?$qs" if $qs ne '';
367
368         [ 301,
369           [ Location => $url, 'Content-Type' => 'text/plain' ],
370           [ "Redirecting to $url\n" ] ]
371 }
372
373 sub msg_page {
374         my ($ctx, $inbox, $mid, $e) = @_;
375         my $ret;
376         $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
377         '' eq $e and return get_mid_html($ctx);
378         'T/' eq $e and return get_thread($ctx, 1);
379         't/' eq $e and return get_thread($ctx);
380         't.atom' eq $e and return get_thread_atom($ctx);
381         't.mbox' eq $e and return get_thread_mbox($ctx);
382         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
383         'raw' eq $e and return get_mid_txt($ctx);
384
385         # legacy, but no redirect for compatibility:
386         'f/' eq $e and return get_mid_html($ctx);
387         r404($ctx);
388 }
389
390 sub serve_git {
391         my ($ctx, $part, $path) = @_;
392         my $env = $ctx->{env};
393         my $ibx = $ctx->{-inbox};
394         my $git = defined $part ? $ibx->git_part($part) : $ibx->git;
395         $git ? PublicInbox::GitHTTPBackend::serve($env, $git, $path) : r404();
396 }
397
398 sub mbox_results {
399         my ($ctx) = @_;
400         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
401                 searcher($ctx) or return need_search($ctx);
402                 require PublicInbox::SearchView;
403                 return PublicInbox::SearchView::mbox_results($ctx);
404         }
405         r404();
406 }
407
408 sub serve_mbox_range {
409         my ($ctx, $inbox, $range) = @_;
410         invalid_inbox($ctx, $inbox) || eval {
411                 require PublicInbox::Mbox;
412                 searcher($ctx);
413                 PublicInbox::Mbox::emit_range($ctx, $range);
414         }
415 }
416
417 sub news_www {
418         my ($self) = @_;
419         $self->{news_www} ||= do {
420                 require PublicInbox::NewsWWW;
421                 PublicInbox::NewsWWW->new($self->{pi_config});
422         }
423 }
424
425 sub get_attach {
426         my ($ctx, $idx, $fn) = @_;
427         require PublicInbox::WwwAttach;
428         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
429 }
430
431 1;