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)
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::SearchView;
71 require PublicInbox::Mbox;
72 require IO::Compress::Gzip;
76 # private functions below
80 if ($ctx && $ctx->{mid}) {
81 require PublicInbox::ExtMsg;
83 return PublicInbox::ExtMsg::ext_msg($ctx);
88 # simple response for errors
89 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
91 # returns undef if valid, array ref response if invalid
93 my ($ctx, $listname) = @_;
94 my $git_dir = $pi_config->get($listname, "mainrepo");
95 if (defined $git_dir) {
96 $ctx->{git_dir} = $git_dir;
97 $ctx->{listname} = $listname;
103 # returns undef if valid, array ref response if invalid
104 sub invalid_list_mid {
105 my ($ctx, $listname, $mid) = @_;
106 my $ret = invalid_list($ctx, $listname, $mid);
109 $ctx->{mid} = $mid = uri_unescape($mid);
110 if ($mid =~ /\A[a-f0-9]{40}\z/) {
111 if ($mid = mid2blob($ctx)) {
112 require Email::Simple;
113 use PublicInbox::MID qw/mid_clean/;
114 $mid = Email::Simple->new($mid);
115 $ctx->{mid} = mid_clean($mid->header('Message-ID'));
121 # /$LISTNAME/new.atom -> Atom feed, includes replies
124 require PublicInbox::Feed;
125 PublicInbox::Feed::generate($ctx);
128 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
131 require PublicInbox::Feed;
132 my $srch = searcher($ctx);
134 if (defined $ctx->{cgi}->param('q')) {
135 require PublicInbox::SearchView;
136 PublicInbox::SearchView::sres_top_html($ctx);
138 PublicInbox::Feed::generate_html_index($ctx);
142 # just returns a string ref for the blob in the current ctx
145 require PublicInbox::MID;
146 my $path = PublicInbox::MID::mid2path($ctx->{mid});
147 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
148 qw(cat-file blob), "HEAD:$path");
149 my $pid = open my $fh, '-|';
150 defined $pid or die "fork failed: $!\n";
152 open STDERR, '>', '/dev/null'; # ignore errors
153 exec @cmd or die "exec failed: $!\n";
155 my $blob = eval { local $/; <$fh> };
157 $? == 0 ? \$blob : undef;
161 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
164 my $x = mid2blob($ctx) or return r404($ctx);
165 require PublicInbox::Mbox;
166 PublicInbox::Mbox::emit1($x);
169 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
172 my $x = mid2blob($ctx) or return r404($ctx);
174 require PublicInbox::View;
175 my $foot = footer($ctx);
177 my $mime = Email::MIME->new($x);
179 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
180 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
183 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
186 my $x = mid2blob($ctx) or return r404($ctx);
188 require PublicInbox::View;
189 my $foot = footer($ctx);
191 my $mime = Email::MIME->new($x);
193 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
194 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
197 # /$LISTNAME/$MESSAGE_ID/t/
199 my ($ctx, $flat) = @_;
200 my $srch = searcher($ctx) or return need_search($ctx);
201 require PublicInbox::View;
202 my $foot = footer($ctx);
203 $ctx->{flat} = $flat;
204 PublicInbox::View::thread_html($ctx, $foot, $srch);
209 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
213 my ($ctx, $key) = @_;
214 my $val = $ctx->{$key};
215 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
222 if (open(my $fh, '<', $path)) {
232 return '' unless $ctx;
233 my $git_dir = ctx_get($ctx, 'git_dir');
235 # favor user-supplied footer
236 my $footer = try_cat("$git_dir/public-inbox/footer.html");
237 if (defined $footer) {
239 $ctx->{footer} = $footer;
243 # auto-generate a footer
244 my $listname = ctx_get($ctx, 'listname');
245 my $desc = try_cat("$git_dir/description");
246 $desc = '$GIT_DIR/description missing' unless defined $desc;
249 my $urls = try_cat("$git_dir/cloneurl");
250 my @urls = split(/\r?\n/, $urls || '');
251 my $nurls = scalar @urls;
253 $urls = '($GIT_DIR/cloneurl missing)';
254 } elsif ($nurls == 1) {
255 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
256 '">ssoma</a>: ' . $urls[0];
258 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
259 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
262 my $addr = $pi_config->get($listname, 'address');
263 if (ref($addr) eq 'ARRAY') {
264 $addr = $addr->[0]; # first address is primary
267 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
269 $ctx->{footer} = join("\n",
271 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
272 'anybody may post in plain-text (not HTML):',
278 # search support is optional, returns undef if Xapian is not installed
279 # or not configured for the given GIT_DIR
283 require PublicInbox::Search;
284 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
291 <html><head><title>Search not available for this
292 public-inbox</title><body><pre>Search is not available for this public-inbox
293 <a href="../">Return to index</a></pre></body></html>
295 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
298 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
299 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
300 # note: I'm not a big fan of other compression formats since they're
301 # significantly more expensive on CPU than gzip and less-widely available,
302 # especially on older systems. Stick to zlib since that's what git uses.
303 sub get_thread_mbox {
304 my ($ctx, $sfx) = @_;
305 my $srch = searcher($ctx) or return need_search($ctx);
306 require PublicInbox::Mbox;
307 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
311 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
312 sub get_thread_atom {
314 searcher($ctx) or return need_search($ctx);
315 $ctx->{self_url} = self_url($ctx->{cgi});
316 require PublicInbox::Feed;
317 PublicInbox::Feed::generate_thread_atom($ctx);
320 sub legacy_redirects {
321 my ($ctx, $path_info) = @_;
323 # single-message pages
324 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
326 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
327 r301($ctx, $1, $2, 'raw');
329 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
330 r301($ctx, $1, $2, 'f/');
333 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
334 r301($ctx, $1, $2, 't/#u');
336 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
337 r301($ctx, $1, $2, "t.mbox$3");
339 # even older legacy redirects
340 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
343 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
344 r301($ctx, $1, $2, 't/#u');
346 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
347 r301($ctx, $1, $2, 'f/');
349 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
350 r301($ctx, $1, $2, 'raw');
352 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
353 r301($ctx, $1, $2, "t$3");
355 # legacy convenience redirects, order still matters
356 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
358 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
359 r301($ctx, $1, $2, 't/#u');
360 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
361 r301($ctx, $1, $2, 'f/');
363 # some Message-IDs have slashes in them and the HTTP server
364 # may try to be clever and unescape them :<
365 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
366 msg_page($ctx, $1, $2, $3);
368 # in case people leave off the trailing slash:
369 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
370 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
377 my ($ctx, $listname, $mid, $suffix) = @_;
378 my $cgi = $ctx->{cgi};
380 if (ref($cgi) eq 'CGI') {
381 $url = $cgi->url(-base) . '/';
383 $url = $cgi->base->as_string;
386 $url .= $listname . '/';
387 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
388 $url .= $suffix if (defined $suffix);
391 [ Location => $url, 'Content-Type' => 'text/plain' ],
392 [ "Redirecting to $url\n" ] ]
396 my ($ctx, $list, $mid, $e) = @_;
397 unless (invalid_list_mid($ctx, $list, $mid)) {
398 '' eq $e and return get_mid_html($ctx);
399 't/' eq $e and return get_thread($ctx);
400 't.atom' eq $e and return get_thread_atom($ctx);
401 't.mbox' eq $e and return get_thread_mbox($ctx);
402 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
403 'T/' eq $e and return get_thread($ctx, 1);
404 'raw' eq $e and return get_mid_txt($ctx);
405 'f/' eq $e and return get_full_html($ctx);