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!([^/]+)!;
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');
29 my $path_info = $cgi->path_info;
31 # top-level indices and feeds
32 if ($path_info eq '/') {
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);
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) {
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);
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);
57 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/f/\z!o) {
58 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
60 # convenience redirects order matters
61 } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
65 legacy_redirects(\%ctx, $path_info);
69 # for CoW-friendliness, MOOOOO!
71 require PublicInbox::Feed;
72 require PublicInbox::View;
73 require PublicInbox::Thread;
74 require PublicInbox::GitCatFile;
80 require PublicInbox::Search;
81 require PublicInbox::Mbox;
82 require IO::Compress::Gzip;
86 # private functions below
88 sub r404 { r(404, 'Not Found') }
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
93 # returns undef if valid, array ref response if invalid
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;
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;
113 # /$LISTNAME/new.atom -> Atom feed, includes replies
116 require PublicInbox::Feed;
117 PublicInbox::Feed::generate($ctx);
120 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
123 require PublicInbox::Feed;
124 my $srch = searcher($ctx);
126 PublicInbox::Feed::generate_html_index($ctx);
129 # just returns a string ref for the blob in the current 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";
139 open STDERR, '>', '/dev/null'; # ignore errors
140 exec @cmd or die "exec failed: $!\n";
142 my $blob = eval { local $/; <$fh> };
144 $? == 0 ? \$blob : undef;
148 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
151 my $x = mid2blob($ctx) or return r404();
152 require PublicInbox::Mbox;
153 PublicInbox::Mbox::emit1($x);
156 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
159 my $x = mid2blob($ctx) or return r404();
161 require PublicInbox::View;
162 my $foot = footer($ctx);
164 my $mime = Email::MIME->new($x);
166 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
167 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
170 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
173 my $x = mid2blob($ctx) or return r404();
175 require PublicInbox::View;
176 my $foot = footer($ctx);
178 my $mime = Email::MIME->new($x);
180 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
181 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
184 # /$LISTNAME/$MESSAGE_ID/t/
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);
195 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
199 my ($ctx, $key) = @_;
200 my $val = $ctx->{$key};
201 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
208 if (open(my $fh, '<', $path)) {
218 return '' unless $ctx;
219 my $git_dir = ctx_get($ctx, 'git_dir');
221 # favor user-supplied footer
222 my $footer = try_cat("$git_dir/public-inbox/footer.html");
223 if (defined $footer) {
225 $ctx->{footer} = $footer;
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;
235 my $urls = try_cat("$git_dir/cloneurl");
236 my @urls = split(/\r?\n/, $urls || '');
237 my $nurls = scalar @urls;
239 $urls = '($GIT_DIR/cloneurl missing)';
240 } elsif ($nurls == 1) {
241 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
242 '">ssoma</a>: ' . $urls[0];
244 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
245 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
248 my $addr = $pi_config->get($listname, 'address');
249 if (ref($addr) eq 'ARRAY') {
250 $addr = $addr->[0]; # first address is primary
253 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
255 $ctx->{footer} = join("\n",
257 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
258 'anybody may post in plain-text (not HTML):',
264 # search support is optional, returns undef if Xapian is not installed
265 # or not configured for the given GIT_DIR
269 require PublicInbox::Search;
270 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
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>
281 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
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);
297 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
298 sub get_thread_atom {
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);
306 sub legacy_redirects {
307 my ($ctx, $path_info) = @_;
309 # single-message pages
310 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
312 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
313 r301($ctx, $1, $2, 'raw');
315 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
316 r301($ctx, $1, $2, 'f/');
319 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
320 r301($ctx, $1, $2, 't/#u');
322 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
323 r301($ctx, $1, $2, "t.mbox$3");
325 # even older legacy redirects
326 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
329 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
330 r301($ctx, $1, $2, 't/#u');
332 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
333 r301($ctx, $1, $2, 'f/');
335 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
336 r301($ctx, $1, $2, 'raw');
338 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
339 r301($ctx, $1, $2, "t$3");
341 # legacy convenience redirects, order still matters
342 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
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/');
355 my ($ctx, $listname, $mid, $suffix) = @_;
356 my $cgi = $ctx->{cgi};
358 if (ref($cgi) eq 'CGI') {
359 $url = $cgi->url(-base) . '/';
361 $url = $cgi->base->as_string;
364 $url .= $listname . '/';
365 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
366 $url .= $suffix if (defined $suffix);
369 [ Location => $url, 'Content-Type' => 'text/plain' ],
370 [ "Redirecting to $url\n" ] ]