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