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