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)
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;
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|)!;
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');
30 my $path_info = $cgi->path_info;
32 # top-level indices and feeds
33 if ($path_info eq '/') {
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);
42 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
43 msg_page($ctx, $1, $2, $3);
45 # in case people leave off the trailing slash:
46 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
47 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
49 # convenience redirects order matters
50 } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
54 legacy_redirects($ctx, $path_info);
58 # for CoW-friendliness, MOOOOO!
60 require PublicInbox::Feed;
61 require PublicInbox::View;
62 require PublicInbox::Thread;
63 require PublicInbox::GitCatFile;
69 require PublicInbox::Search;
70 require PublicInbox::Mbox;
71 require IO::Compress::Gzip;
75 # private functions below
79 if ($ctx && $ctx->{mid}) {
80 require PublicInbox::ExtMsg;
81 return PublicInbox::ExtMsg::ext_msg($ctx);
86 # simple response for errors
87 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
89 # returns undef if valid, array ref response if invalid
91 my ($ctx, $listname) = @_;
92 my $git_dir = $pi_config->get($listname, "mainrepo");
93 if (defined $git_dir) {
94 $ctx->{git_dir} = $git_dir;
95 $ctx->{listname} = $listname;
101 # returns undef if valid, array ref response if invalid
102 sub invalid_list_mid {
103 my ($ctx, $listname, $mid) = @_;
104 my $ret = invalid_list($ctx, $listname, $mid);
107 $ctx->{mid} = $mid = uri_unescape($mid);
108 if ($mid =~ /\A[a-f0-9]{40}\z/) {
109 if ($mid = mid2blob($ctx)) {
110 require Email::Simple;
111 use PublicInbox::MID qw/mid_clean/;
112 $mid = Email::Simple->new($mid);
113 $ctx->{mid} = mid_clean($mid->header('Message-ID'));
119 # /$LISTNAME/new.atom -> Atom feed, includes replies
122 require PublicInbox::Feed;
123 PublicInbox::Feed::generate($ctx);
126 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
129 require PublicInbox::Feed;
130 my $srch = searcher($ctx);
132 PublicInbox::Feed::generate_html_index($ctx);
135 # just returns a string ref for the blob in the current ctx
138 require PublicInbox::MID;
139 my $path = PublicInbox::MID::mid2path($ctx->{mid});
140 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
141 qw(cat-file blob), "HEAD:$path");
142 my $pid = open my $fh, '-|';
143 defined $pid or die "fork failed: $!\n";
145 open STDERR, '>', '/dev/null'; # ignore errors
146 exec @cmd or die "exec failed: $!\n";
148 my $blob = eval { local $/; <$fh> };
150 $? == 0 ? \$blob : undef;
154 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
157 my $x = mid2blob($ctx) or return r404($ctx);
158 require PublicInbox::Mbox;
159 PublicInbox::Mbox::emit1($x);
162 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
165 my $x = mid2blob($ctx) or return r404($ctx);
167 require PublicInbox::View;
168 my $foot = footer($ctx);
170 my $mime = Email::MIME->new($x);
172 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
173 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
176 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
179 my $x = mid2blob($ctx) or return r404($ctx);
181 require PublicInbox::View;
182 my $foot = footer($ctx);
184 my $mime = Email::MIME->new($x);
186 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
187 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
190 # /$LISTNAME/$MESSAGE_ID/t/
192 my ($ctx, $flat) = @_;
193 my $srch = searcher($ctx) or return need_search($ctx);
194 require PublicInbox::View;
195 my $foot = footer($ctx);
196 $ctx->{flat} = $flat;
197 PublicInbox::View::thread_html($ctx, $foot, $srch);
202 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
206 my ($ctx, $key) = @_;
207 my $val = $ctx->{$key};
208 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
215 if (open(my $fh, '<', $path)) {
225 return '' unless $ctx;
226 my $git_dir = ctx_get($ctx, 'git_dir');
228 # favor user-supplied footer
229 my $footer = try_cat("$git_dir/public-inbox/footer.html");
230 if (defined $footer) {
232 $ctx->{footer} = $footer;
236 # auto-generate a footer
237 my $listname = ctx_get($ctx, 'listname');
238 my $desc = try_cat("$git_dir/description");
239 $desc = '$GIT_DIR/description missing' unless defined $desc;
242 my $urls = try_cat("$git_dir/cloneurl");
243 my @urls = split(/\r?\n/, $urls || '');
244 my $nurls = scalar @urls;
246 $urls = '($GIT_DIR/cloneurl missing)';
247 } elsif ($nurls == 1) {
248 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
249 '">ssoma</a>: ' . $urls[0];
251 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
252 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
255 my $addr = $pi_config->get($listname, 'address');
256 if (ref($addr) eq 'ARRAY') {
257 $addr = $addr->[0]; # first address is primary
260 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
262 $ctx->{footer} = join("\n",
264 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
265 'anybody may post in plain-text (not HTML):',
271 # search support is optional, returns undef if Xapian is not installed
272 # or not configured for the given GIT_DIR
276 require PublicInbox::Search;
277 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
284 <html><head><title>Search not available for this
285 public-inbox</title><body><pre>Search is not available for this public-inbox
286 <a href="../">Return to index</a></pre></body></html>
288 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
291 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
292 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
293 # note: I'm not a big fan of other compression formats since they're
294 # significantly more expensive on CPU than gzip and less-widely available,
295 # especially on older systems. Stick to zlib since that's what git uses.
296 sub get_thread_mbox {
297 my ($ctx, $sfx) = @_;
298 my $srch = searcher($ctx) or return need_search($ctx);
299 require PublicInbox::Mbox;
300 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
304 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
305 sub get_thread_atom {
307 searcher($ctx) or return need_search($ctx);
308 $ctx->{self_url} = self_url($ctx->{cgi});
309 require PublicInbox::Feed;
310 PublicInbox::Feed::generate_thread_atom($ctx);
313 sub legacy_redirects {
314 my ($ctx, $path_info) = @_;
316 # single-message pages
317 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
319 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
320 r301($ctx, $1, $2, 'raw');
322 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
323 r301($ctx, $1, $2, 'f/');
326 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
327 r301($ctx, $1, $2, 't/#u');
329 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
330 r301($ctx, $1, $2, "t.mbox$3");
332 # even older legacy redirects
333 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
336 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
337 r301($ctx, $1, $2, 't/#u');
339 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
340 r301($ctx, $1, $2, 'f/');
342 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
343 r301($ctx, $1, $2, 'raw');
345 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
346 r301($ctx, $1, $2, "t$3");
348 # legacy convenience redirects, order still matters
349 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
351 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
352 r301($ctx, $1, $2, 't/#u');
353 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
354 r301($ctx, $1, $2, 'f/');
356 # some Message-IDs have slashes in them and the HTTP server
357 # may try to be clever and unescape them :<
358 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
359 msg_page($ctx, $1, $2, $3);
361 # in case people leave off the trailing slash:
362 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
363 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
370 my ($ctx, $listname, $mid, $suffix) = @_;
371 my $cgi = $ctx->{cgi};
373 if (ref($cgi) eq 'CGI') {
374 $url = $cgi->url(-base) . '/';
376 $url = $cgi->base->as_string;
379 $url .= $listname . '/';
380 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
381 $url .= $suffix if (defined $suffix);
384 [ Location => $url, 'Content-Type' => 'text/plain' ],
385 [ "Redirecting to $url\n" ] ]
389 my ($ctx, $list, $mid, $e) = @_;
390 unless (invalid_list_mid($ctx, $list, $mid)) {
391 '' eq $e and return get_mid_html($ctx);
392 't/' eq $e and return get_thread($ctx);
393 't.atom' eq $e and return get_thread_atom($ctx);
394 't.mbox' eq $e and return get_thread_mbox($ctx);
395 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
396 'T/' eq $e and return get_thread($ctx, 1);
397 'raw' eq $e and return get_mid_txt($ctx);
398 'f/' eq $e and return get_full_html($ctx);