]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: $MESSAGE_ID/raw endpoint supports "duplicates"
[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                 $ctx->{inbox} = $inbox;
156                 return;
157         }
158
159         # sometimes linkifiers (not ours!) screw up automatic link
160         # generation and link things intended for nntp:// to https?://,
161         # so try to infer links and redirect them to the appropriate
162         # list URL.
163         $www->news_www->call($ctx->{env});
164 }
165
166 # returns undef if valid, array ref response if invalid
167 sub invalid_inbox_mid {
168         my ($ctx, $inbox, $mid) = @_;
169         my $ret = invalid_inbox($ctx, $inbox);
170         return $ret if $ret;
171
172         $ctx->{mid} = $mid;
173         if ($mid =~ /\A[a-f0-9]{40}\z/) {
174                 # this is horiffically wasteful for legacy URLs:
175                 if ($mid = mid2blob($ctx)) {
176                         require Email::Simple;
177                         use PublicInbox::MID qw/mid_clean/;
178                         my $s = Email::Simple->new($mid);
179                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
180                 }
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 # just returns a string ref for the blob in the current ctx
213 sub mid2blob {
214         my ($ctx) = @_;
215         $ctx->{-inbox}->msg_by_mid($ctx->{mid});
216 }
217
218 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
219 sub get_mid_txt {
220         my ($ctx) = @_;
221         require PublicInbox::Mbox;
222         PublicInbox::Mbox::emit_raw($ctx) || r404($ctx);
223 }
224
225 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
226 sub get_mid_html {
227         my ($ctx) = @_;
228         my $x = mid2blob($ctx) or return r404($ctx);
229
230         require PublicInbox::View;
231         require PublicInbox::MIME;
232         my $mime = PublicInbox::MIME->new($x);
233         searcher($ctx);
234         PublicInbox::View::msg_html($ctx, $mime);
235 }
236
237 # /$INBOX/$MESSAGE_ID/t/
238 sub get_thread {
239         my ($ctx, $flat) = @_;
240         searcher($ctx) or return need_search($ctx);
241         $ctx->{flat} = $flat;
242         require PublicInbox::View;
243         PublicInbox::View::thread_html($ctx);
244 }
245
246 # /$INBOX/_/text/$KEY/
247 # /$INBOX/_/text/$KEY/raw
248 # KEY may contain slashes
249 sub get_text {
250         my ($ctx, $inbox, $key) = @_;
251         my $r404 = invalid_inbox($ctx, $inbox);
252         return $r404 if $r404;
253
254         require PublicInbox::WwwText;
255         PublicInbox::WwwText::get_text($ctx, $key);
256 }
257
258 sub ctx_get {
259         my ($ctx, $key) = @_;
260         my $val = $ctx->{$key};
261         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
262         $val;
263 }
264
265 # search support is optional, returns undef if Xapian is not installed
266 # or not configured for the given GIT_DIR
267 sub searcher {
268         my ($ctx) = @_;
269         eval {
270                 require PublicInbox::Search;
271                 $ctx->{srch} = $ctx->{-inbox}->search;
272         };
273 }
274
275 sub need_search {
276         my ($ctx) = @_;
277         my $msg = <<EOF;
278 <html><head><title>Search not available for this
279 public-inbox</title><body><pre>Search is not available for this public-inbox
280 <a href="../">Return to index</a></pre></body></html>
281 EOF
282         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
283 }
284
285 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
286 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
287 # note: I'm not a big fan of other compression formats since they're
288 # significantly more expensive on CPU than gzip and less-widely available,
289 # especially on older systems.  Stick to zlib since that's what git uses.
290 sub get_thread_mbox {
291         my ($ctx, $sfx) = @_;
292         my $srch = searcher($ctx) or return need_search($ctx);
293         require PublicInbox::Mbox;
294         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
295 }
296
297
298 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
299 sub get_thread_atom {
300         my ($ctx) = @_;
301         searcher($ctx) or return need_search($ctx);
302         require PublicInbox::Feed;
303         PublicInbox::Feed::generate_thread_atom($ctx);
304 }
305
306 sub legacy_redirects {
307         my ($ctx, $path_info) = @_;
308
309         # single-message pages
310         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
311                 r301($ctx, $1, $2);
312         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
313                 r301($ctx, $1, $2, 'raw');
314
315         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
316                 r301($ctx, $1, $2);
317
318         # thread display
319         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
320                 r301($ctx, $1, $2, 't/#u');
321
322         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
323                 r301($ctx, $1, $2, "t.mbox$3");
324
325         # even older legacy redirects
326         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
327                 r301($ctx, $1, $2);
328
329         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
330                 r301($ctx, $1, $2, 't/#u');
331
332         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
333                 r301($ctx, $1, $2);
334
335         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
336                 r301($ctx, $1, $2, 'raw');
337
338         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
339                 r301($ctx, $1, $2, "t$3");
340
341         # legacy convenience redirects, order still matters
342         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
343                 r301($ctx, $1, $2);
344         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
345                 r301($ctx, $1, $2, 't/#u');
346         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
347                 r301($ctx, $1, $2);
348
349         # some Message-IDs have slashes in them and the HTTP server
350         # may try to be clever and unescape them :<
351         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
352                 msg_page($ctx, $1, $2, $3);
353
354         # in case people leave off the trailing slash:
355         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
356                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
357         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
358                 r301($ctx, $1, $2);
359         } else {
360                 $ctx->{www}->news_www->call($ctx->{env});
361         }
362 }
363
364 sub r301 {
365         my ($ctx, $inbox, $mid, $suffix) = @_;
366         my $obj = $ctx->{-inbox};
367         unless ($obj) {
368                 my $r404 = invalid_inbox($ctx, $inbox);
369                 return $r404 if $r404;
370                 $obj = $ctx->{-inbox};
371         }
372         my $url = $obj->base_url($ctx->{env});
373         my $qs = $ctx->{env}->{QUERY_STRING};
374         $url .= (mid_escape($mid) . '/') if (defined $mid);
375         $url .= $suffix if (defined $suffix);
376         $url .= "?$qs" if $qs ne '';
377
378         [ 301,
379           [ Location => $url, 'Content-Type' => 'text/plain' ],
380           [ "Redirecting to $url\n" ] ]
381 }
382
383 sub msg_page {
384         my ($ctx, $inbox, $mid, $e) = @_;
385         my $ret;
386         $ret = invalid_inbox_mid($ctx, $inbox, $mid) and return $ret;
387         '' eq $e and return get_mid_html($ctx);
388         'T/' eq $e and return get_thread($ctx, 1);
389         't/' eq $e and return get_thread($ctx);
390         't.atom' eq $e and return get_thread_atom($ctx);
391         't.mbox' eq $e and return get_thread_mbox($ctx);
392         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
393         'raw' eq $e and return get_mid_txt($ctx);
394
395         # legacy, but no redirect for compatibility:
396         'f/' eq $e and return get_mid_html($ctx);
397         r404($ctx);
398 }
399
400 sub serve_git {
401         my ($ctx, $path) = @_;
402         PublicInbox::GitHTTPBackend::serve($ctx->{env}, $ctx->{git}, $path);
403 }
404
405 sub mbox_results {
406         my ($ctx) = @_;
407         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
408                 searcher($ctx) or return need_search($ctx);
409                 require PublicInbox::SearchView;
410                 return PublicInbox::SearchView::mbox_results($ctx);
411         }
412         r404();
413 }
414
415 sub serve_mbox_range {
416         my ($ctx, $inbox, $range) = @_;
417         invalid_inbox($ctx, $inbox) || eval {
418                 require PublicInbox::Mbox;
419                 searcher($ctx);
420                 PublicInbox::Mbox::emit_range($ctx, $range);
421         }
422 }
423
424 sub news_www {
425         my ($self) = @_;
426         $self->{news_www} ||= do {
427                 require PublicInbox::NewsWWW;
428                 PublicInbox::NewsWWW->new($self->{pi_config});
429         }
430 }
431
432 sub get_attach {
433         my ($ctx, $idx, $fn) = @_;
434         require PublicInbox::WwwAttach;
435         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
436 }
437
438 1;