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