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