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