]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
d666a1b76adfc6dfd605bb2fe35b7b75a8138500
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # We focus on the lowest common denominators here:
5 # - targeted at text-only console browsers (w3m, links, etc..)
6 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
7 # - No JavaScript, graphics or icons allowed.
8 # - Must not rely on static content
9 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
10 package PublicInbox::WWW;
11 use 5.008;
12 use strict;
13 use warnings;
14 use PublicInbox::Config;
15 use URI::Escape qw(uri_escape_utf8 uri_unescape);
16 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
17 use constant PI_URL => 'http://public-inbox.org/';
18 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
19 our $MID_RE = qr!([^/]+)!;
20 our $pi_config;
21
22 sub run {
23         my ($cgi, $method) = @_;
24         $pi_config ||= PublicInbox::Config->new;
25         my %ctx = (cgi => $cgi, pi_config => $pi_config);
26         if ($method !~ /\AGET|HEAD\z/) {
27                 return r(405, 'Method Not Allowed');
28         }
29         my $path_info = $cgi->path_info;
30
31         # top-level indices and feeds
32         if ($path_info eq '/') {
33                 r404();
34         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
35                 invalid_list(\%ctx, $1) || r301(\%ctx, $1);
36         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
37                 invalid_list(\%ctx, $1) || get_index(\%ctx);
38         } elsif ($path_info =~ m!$LISTNAME_RE/(?:atom\.xml|new\.atom)\z!o) {
39                 invalid_list(\%ctx, $1) || get_atom(\%ctx);
40
41         # thread display
42         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/t/\z!o) {
43                 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
44         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/t\.mbox(\.gz)?\z!o) {
45                 my $sfx = $3;
46                 invalid_list_mid(\%ctx, $1, $2) || get_thread_mbox(\%ctx, $sfx);
47         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/t\.atom\z!o) {
48                 invalid_list_mid(\%ctx, $1, $2) || get_thread_atom(\%ctx);
49
50         # single-message pages
51         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/\z!o) {
52                 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx);
53         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/raw\z!o) {
54                 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
55
56         # full-message page
57         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/f/\z!o) {
58                 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
59
60         # convenience redirects order matters
61         } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
62                 r301(\%ctx, $1, $2);
63
64         } else {
65                 legacy_redirects(\%ctx, $path_info);
66         }
67 }
68
69 # for CoW-friendliness, MOOOOO!
70 sub preload {
71         require PublicInbox::Feed;
72         require PublicInbox::View;
73         require PublicInbox::Thread;
74         require PublicInbox::GitCatFile;
75         require Email::MIME;
76         require Digest::SHA;
77         require POSIX;
78
79         eval {
80                 require PublicInbox::Search;
81                 require PublicInbox::Mbox;
82                 require IO::Compress::Gzip;
83         };
84 }
85
86 # private functions below
87
88 sub r404 { r(404, 'Not Found') }
89
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
92
93 # returns undef if valid, array ref response if invalid
94 sub invalid_list {
95         my ($ctx, $listname) = @_;
96         my $git_dir = $pi_config->get($listname, "mainrepo");
97         if (defined $git_dir) {
98                 $ctx->{git_dir} = $git_dir;
99                 $ctx->{listname} = $listname;
100                 return;
101         }
102         r404();
103 }
104
105 # returns undef if valid, array ref response if invalid
106 sub invalid_list_mid {
107         my ($ctx, $listname, $mid) = @_;
108         my $ret = invalid_list($ctx, $listname, $mid);
109         $ctx->{mid} = uri_unescape($mid) unless $ret;
110         $ret;
111 }
112
113 # /$LISTNAME/new.atom                     -> Atom feed, includes replies
114 sub get_atom {
115         my ($ctx) = @_;
116         require PublicInbox::Feed;
117         PublicInbox::Feed::generate($ctx);
118 }
119
120 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
121 sub get_index {
122         my ($ctx) = @_;
123         require PublicInbox::Feed;
124         my $srch = searcher($ctx);
125         footer($ctx);
126         PublicInbox::Feed::generate_html_index($ctx);
127 }
128
129 # just returns a string ref for the blob in the current ctx
130 sub mid2blob {
131         my ($ctx) = @_;
132         require PublicInbox::MID;
133         my $path = PublicInbox::MID::mid2path($ctx->{mid});
134         my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
135                         qw(cat-file blob), "HEAD:$path");
136         my $pid = open my $fh, '-|';
137         defined $pid or die "fork failed: $!\n";
138         if ($pid == 0) {
139                 open STDERR, '>', '/dev/null'; # ignore errors
140                 exec @cmd or die "exec failed: $!\n";
141         } else {
142                 my $blob = eval { local $/; <$fh> };
143                 close $fh;
144                 $? == 0 ? \$blob : undef;
145         }
146 }
147
148 # /$LISTNAME/$MESSAGE_ID/raw                    -> raw mbox
149 sub get_mid_txt {
150         my ($ctx) = @_;
151         my $x = mid2blob($ctx) or return r404();
152         require PublicInbox::Mbox;
153         PublicInbox::Mbox::emit1($x);
154 }
155
156 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
157 sub get_mid_html {
158         my ($ctx) = @_;
159         my $x = mid2blob($ctx) or return r404();
160
161         require PublicInbox::View;
162         my $foot = footer($ctx);
163         require Email::MIME;
164         my $mime = Email::MIME->new($x);
165         searcher($ctx);
166         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
167           [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
168 }
169
170 # /$LISTNAME/$MESSAGE_ID/f/                   -> HTML content (fullquotes)
171 sub get_full_html {
172         my ($ctx) = @_;
173         my $x = mid2blob($ctx) or return r404();
174
175         require PublicInbox::View;
176         my $foot = footer($ctx);
177         require Email::MIME;
178         my $mime = Email::MIME->new($x);
179         searcher($ctx);
180         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
181           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
182 }
183
184 # /$LISTNAME/$MESSAGE_ID/t/
185 sub get_thread {
186         my ($ctx) = @_;
187         my $srch = searcher($ctx) or return need_search($ctx);
188         require PublicInbox::View;
189         my $foot = footer($ctx);
190         PublicInbox::View::thread_html($ctx, $foot, $srch);
191 }
192
193 sub self_url {
194         my ($cgi) = @_;
195         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
196 }
197
198 sub ctx_get {
199         my ($ctx, $key) = @_;
200         my $val = $ctx->{$key};
201         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
202         $val;
203 }
204
205 sub try_cat {
206         my ($path) = @_;
207         my $rv;
208         if (open(my $fh, '<', $path)) {
209                 local $/;
210                 $rv = <$fh>;
211                 close $fh;
212         }
213         $rv;
214 }
215
216 sub footer {
217         my ($ctx) = @_;
218         return '' unless $ctx;
219         my $git_dir = ctx_get($ctx, 'git_dir');
220
221         # favor user-supplied footer
222         my $footer = try_cat("$git_dir/public-inbox/footer.html");
223         if (defined $footer) {
224                 chomp $footer;
225                 $ctx->{footer} = $footer;
226                 return $footer;
227         }
228
229         # auto-generate a footer
230         my $listname = ctx_get($ctx, 'listname');
231         my $desc = try_cat("$git_dir/description");
232         $desc = '$GIT_DIR/description missing' unless defined $desc;
233         chomp $desc;
234
235         my $urls = try_cat("$git_dir/cloneurl");
236         my @urls = split(/\r?\n/, $urls || '');
237         my $nurls = scalar @urls;
238         if ($nurls == 0) {
239                 $urls = '($GIT_DIR/cloneurl missing)';
240         } elsif ($nurls == 1) {
241                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
242                         '">ssoma</a>: ' . $urls[0];
243         } else {
244                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
245                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
246         }
247
248         my $addr = $pi_config->get($listname, 'address');
249         if (ref($addr) eq 'ARRAY') {
250                 $addr = $addr->[0]; # first address is primary
251         }
252
253         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
254
255         $ctx->{footer} = join("\n",
256                 '- ' . $desc,
257                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
258                         'anybody may post in plain-text (not HTML):',
259                 $addr,
260                 $urls
261         );
262 }
263
264 # search support is optional, returns undef if Xapian is not installed
265 # or not configured for the given GIT_DIR
266 sub searcher {
267         my ($ctx) = @_;
268         eval {
269                 require PublicInbox::Search;
270                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
271         };
272 }
273
274 sub need_search {
275         my ($ctx) = @_;
276         my $msg = <<EOF;
277 <html><head><title>Search not available for this
278 public-inbox</title><body><pre>Search is not available for this public-inbox
279 <a href="../">Return to index</a></pre></body></html>
280 EOF
281         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
282 }
283
284 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
285 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
286 # note: I'm not a big fan of other compression formats since they're
287 # significantly more expensive on CPU than gzip and less-widely available,
288 # especially on older systems.  Stick to zlib since that's what git uses.
289 sub get_thread_mbox {
290         my ($ctx, $sfx) = @_;
291         my $srch = searcher($ctx) or return need_search($ctx);
292         require PublicInbox::Mbox;
293         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
294 }
295
296
297 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
298 sub get_thread_atom {
299         my ($ctx) = @_;
300         searcher($ctx) or return need_search($ctx);
301         $ctx->{self_url} = self_url($ctx->{cgi});
302         require PublicInbox::Feed;
303         PublicInbox::Feed::generate_thread_atom($ctx);
304 }
305
306 sub legacy_redirects {
307         my ($ctx, $path_info) = @_;
308
309         # single-message pages
310         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
311                 r301($ctx, $1, $2);
312         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
313                 r301($ctx, $1, $2, 'raw');
314
315         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
316                 r301($ctx, $1, $2, 'f/');
317
318         # thread display
319         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
320                 r301($ctx, $1, $2, 't/#u');
321
322         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
323                 r301($ctx, $1, $2, "t.mbox$3");
324
325         # even older legacy redirects
326         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
327                 r301($ctx, $1, $2);
328
329         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
330                 r301($ctx, $1, $2, 't/#u');
331
332         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
333                 r301($ctx, $1, $2, 'f/');
334
335         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
336                 r301($ctx, $1, $2, 'raw');
337
338         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
339                 r301($ctx, $1, $2, "t$3");
340
341         # legacy convenience redirects, order still matters
342         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
343                 r301($ctx, $1, $2);
344         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
345                 r301($ctx, $1, $2, 't/#u');
346         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
347                 r301($ctx, $1, $2, 'f/');
348
349         } else {
350                 r404();
351         }
352 }
353
354 sub r301 {
355         my ($ctx, $listname, $mid, $suffix) = @_;
356         my $cgi = $ctx->{cgi};
357         my $url;
358         if (ref($cgi) eq 'CGI') {
359                 $url = $cgi->url(-base) . '/';
360         } else {
361                 $url = $cgi->base->as_string;
362         }
363
364         $url .= $listname . '/';
365         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
366         $url .= $suffix if (defined $suffix);
367
368         [ 301,
369           [ Location => $url, 'Content-Type' => 'text/plain' ],
370           [ "Redirecting to $url\n" ] ]
371 }
372
373 1;