]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: force two element key-value pairs in query
[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 Plack::Request;
17 use PublicInbox::Config;
18 use URI::Escape qw(uri_escape_utf8 uri_unescape);
19 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
20 use constant PI_URL => '//public-inbox.org/';
21 require PublicInbox::Git;
22 use PublicInbox::GitHTTPBackend;
23 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
24 our $MID_RE = qr!([^/]+)!;
25 our $END_RE = qr!(T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
26 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
27
28 sub new {
29         my ($class, $pi_config) = @_;
30         $pi_config ||= PublicInbox::Config->new;
31         bless { pi_config => $pi_config }, $class;
32 }
33
34 # backwards compatibility, do not use
35 sub run {
36         my ($req, $method) = @_;
37         PublicInbox::WWW->new->call($req->env);
38 }
39
40 sub call {
41         my ($self, $env) = @_;
42         my $cgi = Plack::Request->new($env);
43         my $ctx = { cgi => $cgi, env => $env, www => $self,
44                 pi_config => $self->{pi_config} };
45
46         # we don't care about multi-value
47         my %qp = map {
48                 my ($k, $v) = split('=', $_, 2);
49                 ($k, $v)
50         } split(/[&;]/, uri_unescape($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                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
58                 my $path = $2;
59                 return (invalid_inbox($self, $ctx, $1) ||
60                         serve_git($env, $ctx->{git}, $path));
61         }
62         elsif ($method !~ /\AGET|HEAD\z/) {
63                 return r(405, 'Method Not Allowed');
64         }
65
66         # top-level indices and feeds
67         if ($path_info eq '/') {
68                 r404();
69         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
70                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
71         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
72                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
73         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
74                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
75
76         } elsif ($path_info =~ m!$INBOX_RE/
77                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
78                 my $path = $2;
79                 invalid_inbox($self, $ctx, $1) ||
80                         serve_git($env, $ctx->{git}, $path);
81         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
82                 serve_mbox_range($self, $ctx, $1, $2);
83         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
84                 msg_page($self, $ctx, $1, $2, $3);
85
86         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
87                 my ($idx, $fn) = ($3, $4);
88                 invalid_inbox_mid($self, $ctx, $1, $2) ||
89                         get_attach($ctx, $idx, $fn);
90         # in case people leave off the trailing slash:
91         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
92                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
93                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
94                 r301($ctx, $inbox, $mid, $suffix);
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($self, $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 ($self, $ctx, $inbox) = @_;
142         my $obj = $ctx->{pi_config}->lookup_name($inbox);
143         if (defined $obj) {
144                 $ctx->{git_dir} = $obj->{mainrepo};
145                 $ctx->{git} = $obj->git;
146                 # for PublicInbox::HTTP::weaken_task:
147                 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
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         $self->news_www->call($ctx->{cgi}->{env});
158 }
159
160 # returns undef if valid, array ref response if invalid
161 sub invalid_inbox_mid {
162         my ($self, $ctx, $inbox, $mid) = @_;
163         my $ret = invalid_inbox($self, $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/?r=$GIT_COMMIT                 -> HTML only
187 sub get_index {
188         my ($ctx) = @_;
189         require PublicInbox::Feed;
190         my $srch = searcher($ctx);
191         footer($ctx);
192         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
193                 require PublicInbox::SearchView;
194                 PublicInbox::SearchView::sres_top_html($ctx);
195         } else {
196                 PublicInbox::Feed::generate_html_index($ctx);
197         }
198 }
199
200 # just returns a string ref for the blob in the current ctx
201 sub mid2blob {
202         my ($ctx) = @_;
203         require PublicInbox::MID;
204         my $path = PublicInbox::MID::mid2path($ctx->{mid});
205         $ctx->{git}->cat_file("HEAD:$path");
206 }
207
208 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
209 sub get_mid_txt {
210         my ($ctx) = @_;
211         my $x = mid2blob($ctx) or return r404($ctx);
212         require PublicInbox::Mbox;
213         PublicInbox::Mbox::emit1($ctx, $x);
214 }
215
216 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
217 sub get_mid_html {
218         my ($ctx) = @_;
219         my $x = mid2blob($ctx) or return r404($ctx);
220
221         require PublicInbox::View;
222         my $foot = footer($ctx);
223         require Email::MIME;
224         my $mime = Email::MIME->new($x);
225         searcher($ctx);
226         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
227           [ PublicInbox::View::msg_html($ctx, $mime, $foot) ] ];
228 }
229
230 # /$INBOX/$MESSAGE_ID/R/                   -> HTML content (fullquotes)
231 sub get_reply_html {
232         my ($ctx) = @_;
233         my $x = mid2blob($ctx) or return r404($ctx);
234
235         require PublicInbox::View;
236         my $foot = footer($ctx);
237         require Email::MIME;
238         my $hdr = Email::MIME->new($x)->header_obj;
239         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
240           [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
241 }
242
243 # /$INBOX/$MESSAGE_ID/t/
244 sub get_thread {
245         my ($ctx, $flat) = @_;
246         my $srch = searcher($ctx) or return need_search($ctx);
247         require PublicInbox::View;
248         my $foot = footer($ctx);
249         $ctx->{flat} = $flat;
250         PublicInbox::View::thread_html($ctx, $foot, $srch);
251 }
252
253 sub ctx_get {
254         my ($ctx, $key) = @_;
255         my $val = $ctx->{$key};
256         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
257         $val;
258 }
259
260 sub footer {
261         my ($ctx) = @_;
262         return '' unless $ctx;
263         my $obj = $ctx->{-inbox} or return '';
264
265         # auto-generate a footer
266         chomp(my $desc = $obj->description);
267
268         my $urls;
269         my @urls = @{$obj->cloneurl};
270         my %seen = map { $_ => 1 } @urls;
271         my $cgi = $ctx->{cgi};
272         my $http = $cgi->base->as_string . $obj->{name};
273         $seen{$http} or unshift @urls, $http;
274         my $ssoma_url = PublicInbox::Hval::prurl($ctx->{env}, SSOMA_URL);
275         if (scalar(@urls) == 1) {
276                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
277                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
278         } else {
279                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
280                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
281                         join("\n", map { "\tgit clone --mirror $_" } @urls);
282         }
283
284         my $addr = $obj->{-primary_address};
285         $ctx->{footer} = join("\n",
286                 '- ' . $desc,
287                 "A <a\nhref=\"" .
288                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
289                         '">public-inbox</a>, ' .
290                         'anybody may post in plain-text (not HTML):',
291                 $addr,
292                 $urls
293         );
294 }
295
296 # search support is optional, returns undef if Xapian is not installed
297 # or not configured for the given GIT_DIR
298 sub searcher {
299         my ($ctx) = @_;
300         eval {
301                 require PublicInbox::Search;
302                 $ctx->{srch} = $ctx->{-inbox}->search;
303         };
304 }
305
306 sub need_search {
307         my ($ctx) = @_;
308         my $msg = <<EOF;
309 <html><head><title>Search not available for this
310 public-inbox</title><body><pre>Search is not available for this public-inbox
311 <a href="../">Return to index</a></pre></body></html>
312 EOF
313         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
314 }
315
316 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
317 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
318 # note: I'm not a big fan of other compression formats since they're
319 # significantly more expensive on CPU than gzip and less-widely available,
320 # especially on older systems.  Stick to zlib since that's what git uses.
321 sub get_thread_mbox {
322         my ($ctx, $sfx) = @_;
323         my $srch = searcher($ctx) or return need_search($ctx);
324         require PublicInbox::Mbox;
325         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
326 }
327
328
329 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
330 sub get_thread_atom {
331         my ($ctx) = @_;
332         searcher($ctx) or return need_search($ctx);
333         $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
334         require PublicInbox::Feed;
335         PublicInbox::Feed::generate_thread_atom($ctx);
336 }
337
338 sub legacy_redirects {
339         my ($self, $ctx, $path_info) = @_;
340
341         # single-message pages
342         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
343                 r301($ctx, $1, $2);
344         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
345                 r301($ctx, $1, $2, 'raw');
346
347         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
348                 r301($ctx, $1, $2);
349
350         # thread display
351         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
352                 r301($ctx, $1, $2, 't/#u');
353
354         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
355                 r301($ctx, $1, $2, "t.mbox$3");
356
357         # even older legacy redirects
358         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
359                 r301($ctx, $1, $2);
360
361         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
362                 r301($ctx, $1, $2, 't/#u');
363
364         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
365                 r301($ctx, $1, $2);
366
367         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
368                 r301($ctx, $1, $2, 'raw');
369
370         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
371                 r301($ctx, $1, $2, "t$3");
372
373         # legacy convenience redirects, order still matters
374         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
375                 r301($ctx, $1, $2);
376         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
377                 r301($ctx, $1, $2, 't/#u');
378         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
379                 r301($ctx, $1, $2);
380
381         # some Message-IDs have slashes in them and the HTTP server
382         # may try to be clever and unescape them :<
383         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
384                 msg_page($self, $ctx, $1, $2, $3);
385
386         # in case people leave off the trailing slash:
387         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
388                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
389         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
390                 r301($ctx, $1, $2);
391         } else {
392                 $self->news_www->call($ctx->{cgi}->{env});
393         }
394 }
395
396 sub r301 {
397         my ($ctx, $inbox, $mid, $suffix) = @_;
398         my $cgi = $ctx->{cgi};
399         my $obj = $ctx->{-inbox};
400         unless ($obj) {
401                 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
402                 return $r404 if $r404;
403                 $obj = $ctx->{-inbox};
404         }
405         my $url = $obj->base_url($cgi);
406         my $qs = $ctx->{env}->{QUERY_STRING};
407         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
408         $url .= $suffix if (defined $suffix);
409         $url .= "?$qs" if $qs ne '';
410
411         [ 301,
412           [ Location => $url, 'Content-Type' => 'text/plain' ],
413           [ "Redirecting to $url\n" ] ]
414 }
415
416 sub msg_page {
417         my ($self, $ctx, $inbox, $mid, $e) = @_;
418         my $ret;
419         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
420         '' eq $e and return get_mid_html($ctx);
421         't/' eq $e and return get_thread($ctx);
422         't.atom' eq $e and return get_thread_atom($ctx);
423         't.mbox' eq $e and return get_thread_mbox($ctx);
424         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
425         'T/' eq $e and return get_thread($ctx, 1);
426         'raw' eq $e and return get_mid_txt($ctx);
427
428         # legacy, but no redirect for compatibility:
429         'f/' eq $e and return get_mid_html($ctx);
430
431         'R/' eq $e and return get_reply_html($ctx);
432         r404($ctx);
433 }
434
435 sub serve_git {
436         my ($env, $git, $path) = @_;
437         PublicInbox::GitHTTPBackend::serve($env, $git, $path);
438 }
439
440 sub serve_mbox_range {
441         my ($self, $ctx, $inbox, $range) = @_;
442         invalid_inbox($self, $ctx, $inbox) || eval {
443                 require PublicInbox::Mbox;
444                 searcher($ctx);
445                 PublicInbox::Mbox::emit_range($ctx, $range);
446         }
447 }
448
449 sub news_www {
450         my ($self) = @_;
451         my $nw = $self->{news_www};
452         return $nw if $nw;
453         require PublicInbox::NewsWWW;
454         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
455 }
456
457 sub get_attach {
458         my ($ctx, $idx, $fn) = @_;
459         require PublicInbox::WwwAttach;
460         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
461 }
462
463 1;