]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
hval: implement common UI for protocol-relative URLs
[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 qw(try_cat);
17 use URI::Escape qw(uri_escape_utf8 uri_unescape);
18 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
19 use constant PI_URL => '//public-inbox.org/';
20 require PublicInbox::Git;
21 use PublicInbox::GitHTTPBackend;
22 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
23 our $MID_RE = qr!([^/]+)!;
24 our $END_RE = qr!(f/|T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
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 $cgi = Plack::Request->new($env);
41         my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
42         my $path_info = $cgi->path_info;
43
44         my $method = $cgi->method;
45         if ($method eq 'POST' &&
46                  $path_info =~ m!$LISTNAME_RE/(git-upload-pack)\z!) {
47                 my $path = $2;
48                 return (invalid_list($ctx, $1) ||
49                         serve_git($cgi, $ctx->{git}, $path));
50         }
51         elsif ($method !~ /\AGET|HEAD\z/) {
52                 return r(405, 'Method Not Allowed');
53         }
54
55         # top-level indices and feeds
56         if ($path_info eq '/') {
57                 r404();
58         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
59                 invalid_list($ctx, $1) || r301($ctx, $1);
60         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
61                 invalid_list($ctx, $1) || get_index($ctx);
62         } elsif ($path_info =~ m!$LISTNAME_RE/(?:atom\.xml|new\.atom)\z!o) {
63                 invalid_list($ctx, $1) || get_atom($ctx);
64
65         } elsif ($path_info =~ m!$LISTNAME_RE/
66                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
67                 my $path = $2;
68                 invalid_list($ctx, $1) || serve_git($cgi, $ctx->{git}, $path);
69         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
70                 msg_page($ctx, $1, $2, $3);
71
72         # in case people leave off the trailing slash:
73         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t|R)\z!o) {
74                 my ($listname, $mid, $suffix) = ($1, $2, $3);
75                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
76                 r301($ctx, $listname, $mid, $suffix);
77
78         # convenience redirects order matters
79         } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
80                 r301($ctx, $1, $2);
81
82         } else {
83                 legacy_redirects($ctx, $path_info);
84         }
85 }
86
87 # for CoW-friendliness, MOOOOO!
88 sub preload {
89         require PublicInbox::Feed;
90         require PublicInbox::View;
91         require PublicInbox::Thread;
92         require Email::MIME;
93         require Digest::SHA;
94         require POSIX;
95
96         eval {
97                 require PublicInbox::Search;
98                 require PublicInbox::SearchView;
99                 require PublicInbox::Mbox;
100                 require IO::Compress::Gzip;
101         };
102 }
103
104 # private functions below
105
106 sub r404 {
107         my ($ctx) = @_;
108         if ($ctx && $ctx->{mid}) {
109                 require PublicInbox::ExtMsg;
110                 searcher($ctx);
111                 return PublicInbox::ExtMsg::ext_msg($ctx);
112         }
113         r(404, 'Not Found');
114 }
115
116 # simple response for errors
117 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
118
119 # returns undef if valid, array ref response if invalid
120 sub invalid_list {
121         my ($ctx, $listname) = @_;
122         my $git_dir = $ctx->{pi_config}->get($listname, "mainrepo");
123         if (defined $git_dir) {
124                 $ctx->{git_dir} = $git_dir;
125                 $ctx->{git} = PublicInbox::Git->new($git_dir);
126                 $ctx->{listname} = $listname;
127                 return;
128         }
129         r404();
130 }
131
132 # returns undef if valid, array ref response if invalid
133 sub invalid_list_mid {
134         my ($ctx, $listname, $mid) = @_;
135         my $ret = invalid_list($ctx, $listname, $mid);
136         return $ret if $ret;
137
138         $ctx->{mid} = $mid = uri_unescape($mid);
139         if ($mid =~ /\A[a-f0-9]{40}\z/) {
140                 if ($mid = mid2blob($ctx)) {
141                         require Email::Simple;
142                         use PublicInbox::MID qw/mid_clean/;
143                         $mid = Email::Simple->new($mid);
144                         $ctx->{mid} = mid_clean($mid->header('Message-ID'));
145                 }
146         }
147         undef;
148 }
149
150 # /$LISTNAME/new.atom                     -> Atom feed, includes replies
151 sub get_atom {
152         my ($ctx) = @_;
153         require PublicInbox::Feed;
154         PublicInbox::Feed::generate($ctx);
155 }
156
157 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
158 sub get_index {
159         my ($ctx) = @_;
160         require PublicInbox::Feed;
161         my $srch = searcher($ctx);
162         footer($ctx);
163         if (defined $ctx->{cgi}->param('q')) {
164                 require PublicInbox::SearchView;
165                 PublicInbox::SearchView::sres_top_html($ctx);
166         } else {
167                 PublicInbox::Feed::generate_html_index($ctx);
168         }
169 }
170
171 # just returns a string ref for the blob in the current ctx
172 sub mid2blob {
173         my ($ctx) = @_;
174         require PublicInbox::MID;
175         my $path = PublicInbox::MID::mid2path($ctx->{mid});
176         $ctx->{git}->cat_file("HEAD:$path");
177 }
178
179 # /$LISTNAME/$MESSAGE_ID/raw                    -> raw mbox
180 sub get_mid_txt {
181         my ($ctx) = @_;
182         my $x = mid2blob($ctx) or return r404($ctx);
183         require PublicInbox::Mbox;
184         PublicInbox::Mbox::emit1($ctx, $x);
185 }
186
187 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
188 sub get_mid_html {
189         my ($ctx) = @_;
190         my $x = mid2blob($ctx) or return r404($ctx);
191
192         require PublicInbox::View;
193         my $foot = footer($ctx);
194         require Email::MIME;
195         my $mime = Email::MIME->new($x);
196         searcher($ctx);
197         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
198           [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
199 }
200
201 # /$LISTNAME/$MESSAGE_ID/f/                   -> HTML content (fullquotes)
202 sub get_full_html {
203         my ($ctx) = @_;
204         my $x = mid2blob($ctx) or return r404($ctx);
205
206         require PublicInbox::View;
207         my $foot = footer($ctx);
208         require Email::MIME;
209         my $mime = Email::MIME->new($x);
210         searcher($ctx);
211         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
212           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
213 }
214
215 # /$LISTNAME/$MESSAGE_ID/R/                   -> HTML content (fullquotes)
216 sub get_reply_html {
217         my ($ctx) = @_;
218         my $x = mid2blob($ctx) or return r404($ctx);
219
220         require PublicInbox::View;
221         my $foot = footer($ctx);
222         require Email::MIME;
223         my $hdr = Email::MIME->new($x)->header_obj;
224         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
225           [ PublicInbox::View::msg_reply($ctx, $hdr, $foot)] ];
226 }
227
228 # /$LISTNAME/$MESSAGE_ID/t/
229 sub get_thread {
230         my ($ctx, $flat) = @_;
231         my $srch = searcher($ctx) or return need_search($ctx);
232         require PublicInbox::View;
233         my $foot = footer($ctx);
234         $ctx->{flat} = $flat;
235         PublicInbox::View::thread_html($ctx, $foot, $srch);
236 }
237
238 sub ctx_get {
239         my ($ctx, $key) = @_;
240         my $val = $ctx->{$key};
241         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
242         $val;
243 }
244
245 sub footer {
246         my ($ctx) = @_;
247         return '' unless $ctx;
248         my $git_dir = ctx_get($ctx, 'git_dir');
249
250         # favor user-supplied footer
251         my $footer = try_cat("$git_dir/public-inbox/footer.html");
252         if (defined $footer) {
253                 chomp $footer;
254                 $ctx->{footer} = $footer;
255                 return $footer;
256         }
257
258         # auto-generate a footer
259         my $listname = ctx_get($ctx, 'listname');
260         my $desc = try_cat("$git_dir/description");
261         $desc = '$GIT_DIR/description missing' unless defined $desc;
262         chomp $desc;
263
264         my $urls = try_cat("$git_dir/cloneurl");
265         my @urls = split(/\r?\n/, $urls || '');
266         my %seen = map { $_ => 1 } @urls;
267         my $cgi = $ctx->{cgi};
268         my $http = $cgi->base->as_string . $listname;
269         $seen{$http} or unshift @urls, $http;
270         my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
271         if (scalar(@urls) == 1) {
272                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
273                         qq(">ssoma</a> or <b>git clone --mirror \$URL</b> :) .
274                         $urls[0];
275         } else {
276                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
277                         qq(">ssoma</a> or <b>git clone --mirror \$URL</b>\n) .
278                         join("\n", map { "\t$_" } @urls);
279         }
280
281         my $addr = $ctx->{pi_config}->get($listname, 'address');
282         if (ref($addr) eq 'ARRAY') {
283                 $addr = $addr->[0]; # first address is primary
284         }
285
286         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
287
288         $ctx->{footer} = join("\n",
289                 '- ' . $desc,
290                 "A <a\nhref=\"" .
291                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
292                         '">public-inbox</a>, ' .
293                         'anybody may post in plain-text (not HTML):',
294                 $addr,
295                 $urls
296         );
297 }
298
299 # search support is optional, returns undef if Xapian is not installed
300 # or not configured for the given GIT_DIR
301 sub searcher {
302         my ($ctx) = @_;
303         eval {
304                 require PublicInbox::Search;
305                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
306         };
307 }
308
309 sub need_search {
310         my ($ctx) = @_;
311         my $msg = <<EOF;
312 <html><head><title>Search not available for this
313 public-inbox</title><body><pre>Search is not available for this public-inbox
314 <a href="../">Return to index</a></pre></body></html>
315 EOF
316         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
317 }
318
319 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
320 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
321 # note: I'm not a big fan of other compression formats since they're
322 # significantly more expensive on CPU than gzip and less-widely available,
323 # especially on older systems.  Stick to zlib since that's what git uses.
324 sub get_thread_mbox {
325         my ($ctx, $sfx) = @_;
326         my $srch = searcher($ctx) or return need_search($ctx);
327         require PublicInbox::Mbox;
328         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
329 }
330
331
332 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
333 sub get_thread_atom {
334         my ($ctx) = @_;
335         searcher($ctx) or return need_search($ctx);
336         $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
337         require PublicInbox::Feed;
338         PublicInbox::Feed::generate_thread_atom($ctx);
339 }
340
341 sub legacy_redirects {
342         my ($ctx, $path_info) = @_;
343
344         # single-message pages
345         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
346                 r301($ctx, $1, $2);
347         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
348                 r301($ctx, $1, $2, 'raw');
349
350         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
351                 r301($ctx, $1, $2, 'f/');
352
353         # thread display
354         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
355                 r301($ctx, $1, $2, 't/#u');
356
357         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
358                 r301($ctx, $1, $2, "t.mbox$3");
359
360         # even older legacy redirects
361         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
362                 r301($ctx, $1, $2);
363
364         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
365                 r301($ctx, $1, $2, 't/#u');
366
367         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
368                 r301($ctx, $1, $2, 'f/');
369
370         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
371                 r301($ctx, $1, $2, 'raw');
372
373         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
374                 r301($ctx, $1, $2, "t$3");
375
376         # legacy convenience redirects, order still matters
377         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
378                 r301($ctx, $1, $2);
379         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
380                 r301($ctx, $1, $2, 't/#u');
381         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
382                 r301($ctx, $1, $2, 'f/');
383
384         # some Message-IDs have slashes in them and the HTTP server
385         # may try to be clever and unescape them :<
386         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
387                 msg_page($ctx, $1, $2, $3);
388
389         # in case people leave off the trailing slash:
390         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
391                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
392         } else {
393                 r404();
394         }
395 }
396
397 sub r301 {
398         my ($ctx, $listname, $mid, $suffix) = @_;
399         my $cgi = $ctx->{cgi};
400         my $url;
401         my $qs = $cgi->env->{QUERY_STRING};
402         $url = $cgi->base->as_string . $listname . '/';
403         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
404         $url .= $suffix if (defined $suffix);
405         $url .= "?$qs" if $qs ne '';
406
407         [ 301,
408           [ Location => $url, 'Content-Type' => 'text/plain' ],
409           [ "Redirecting to $url\n" ] ]
410 }
411
412 sub msg_page {
413         my ($ctx, $list, $mid, $e) = @_;
414         unless (invalid_list_mid($ctx, $list, $mid)) {
415                 '' eq $e and return get_mid_html($ctx);
416                 't/' eq $e and return get_thread($ctx);
417                 't.atom' eq $e and return get_thread_atom($ctx);
418                 't.mbox' eq $e and return get_thread_mbox($ctx);
419                 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
420                 'T/' eq $e and return get_thread($ctx, 1);
421                 'raw' eq $e and return get_mid_txt($ctx);
422                 'f/' eq $e and return get_full_html($ctx);
423                 'R/' eq $e and return get_reply_html($ctx);
424         }
425         r404($ctx);
426 }
427
428 sub serve_git {
429         my ($cgi, $git, $path) = @_;
430         PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
431 }
432
433 1;