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\.\-]+)!;
21 $pi_config = PublicInbox::Config->new;
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');
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) || 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);
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);
49 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
50 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
53 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
54 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
56 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!x) {
58 invalid_list_mid(\%ctx, $1, $2) ||
59 get_thread_mbox(\%ctx, $sfx);
62 } elsif ($path_info =~ m!$LISTNAME_RE/(t|m|f)/(\S+)\.html\z!o) {
64 invalid_list_mid(\%ctx, $1, $3) ||
65 redirect_mid(\%ctx, $pfx, qr/\.html\z/, '/');
66 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f)/(\S+)\.txt\z!o) {
68 invalid_list_mid(\%ctx, $1, $3) ||
69 redirect_mid(\%ctx, $pfx, qr/\.txt\z/, '/raw');
70 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
72 invalid_list_mid(\%ctx, $1, $2) ||
73 redirect_mid(\%ctx, 't', $end, '/mbox.gz');
75 # convenience redirects, order matters
76 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
78 invalid_list_mid(\%ctx, $1, $3) ||
79 redirect_mid(\%ctx, $pfx, qr/\z/, '/');
86 # for CoW-friendliness, MOOOOO!
88 require PublicInbox::Feed;
89 require PublicInbox::View;
90 require PublicInbox::Thread;
91 require PublicInbox::GitCatFile;
97 require PublicInbox::Search;
98 require PublicInbox::Mbox;
99 require IO::Compress::Gzip;
103 # private functions below
105 sub r404 { r(404, 'Not Found') }
107 # simple response for errors
108 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
110 # returns undef if valid, array ref response if invalid
112 my ($ctx, $listname) = @_;
113 my $git_dir = $pi_config->get($listname, "mainrepo");
114 if (defined $git_dir) {
115 $ctx->{git_dir} = $git_dir;
116 $ctx->{listname} = $listname;
122 # returns undef if valid, array ref response if invalid
123 sub invalid_list_mid {
124 my ($ctx, $listname, $mid) = @_;
125 my $ret = invalid_list($ctx, $listname, $mid);
126 $ctx->{mid} = uri_unescape($mid) unless $ret;
130 # /$LISTNAME/atom.xml -> Atom feed, includes replies
133 require PublicInbox::Feed;
134 PublicInbox::Feed::generate($ctx);
137 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
140 require PublicInbox::Feed;
141 my $srch = searcher($ctx);
143 PublicInbox::Feed::generate_html_index($ctx);
146 # just returns a string ref for the blob in the current ctx
149 require PublicInbox::MID;
150 my $path = PublicInbox::MID::mid2path($ctx->{mid});
151 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
152 qw(cat-file blob), "HEAD:$path");
153 my $pid = open my $fh, '-|';
154 defined $pid or die "fork failed: $!\n";
156 open STDERR, '>', '/dev/null'; # ignore errors
157 exec @cmd or die "exec failed: $!\n";
159 my $blob = eval { local $/; <$fh> };
161 $? == 0 ? \$blob : undef;
165 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw mbox
168 my $x = mid2blob($ctx) or return r404();
169 require PublicInbox::Mbox;
170 PublicInbox::Mbox::emit1($x);
173 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
176 my $x = mid2blob($ctx) or return r404();
178 require PublicInbox::View;
179 my $pfx = msg_pfx($ctx);
180 my $foot = footer($ctx);
182 my $mime = Email::MIME->new($x);
184 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
185 [ PublicInbox::View::msg_html($ctx, $mime, $pfx, $foot) ] ];
188 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
191 my $x = mid2blob($ctx) or return r404();
193 require PublicInbox::View;
194 my $foot = footer($ctx);
196 my $mime = Email::MIME->new($x);
198 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
199 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
202 # /$LISTNAME/t/$MESSAGE_ID.html
205 my $srch = searcher($ctx) or return need_search($ctx);
206 require PublicInbox::View;
207 my $foot = footer($ctx);
208 PublicInbox::View::thread_html($ctx, $foot, $srch);
213 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
216 sub redirect_list_index {
218 do_redirect(self_url($cgi) . "/");
222 my ($ctx, $pfx, $old, $sfx) = @_;
223 my $url = self_url($ctx->{cgi});
225 if (lc($pfx) eq 't' && $sfx eq '/') {
226 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
228 $url =~ s/$old/$sfx/;
229 do_redirect($url . $anchor);
232 # only hit when somebody tries to guess URLs manually:
233 sub redirect_mid_txt {
234 my ($ctx, $pfx) = @_;
235 my $listname = $ctx->{listname};
236 my $url = self_url($ctx->{cgi});
237 $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
244 [ Location => $url, 'Content-Type' => 'text/plain' ],
245 [ "Redirecting to $url\n" ]
250 my ($ctx, $key) = @_;
251 my $val = $ctx->{$key};
252 (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
259 if (open(my $fh, '<', $path)) {
269 return '' unless $ctx;
270 my $git_dir = ctx_get($ctx, 'git_dir');
272 # favor user-supplied footer
273 my $footer = try_cat("$git_dir/public-inbox/footer.html");
274 if (defined $footer) {
276 $ctx->{footer} = $footer;
280 # auto-generate a footer
281 my $listname = ctx_get($ctx, 'listname');
282 my $desc = try_cat("$git_dir/description");
283 $desc = '$GIT_DIR/description missing' unless defined $desc;
286 my $urls = try_cat("$git_dir/cloneurl");
287 my @urls = split(/\r?\n/, $urls || '');
288 my $nurls = scalar @urls;
290 $urls = '($GIT_DIR/cloneurl missing)';
291 } elsif ($nurls == 1) {
292 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
293 '">ssoma</a>: ' . $urls[0];
295 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
296 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
299 my $addr = $pi_config->get($listname, 'address');
300 if (ref($addr) eq 'ARRAY') {
301 $addr = $addr->[0]; # first address is primary
304 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
306 $ctx->{footer} = join("\n",
308 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
309 'anybody may post in plain-text (not HTML):',
315 # search support is optional, returns undef if Xapian is not installed
316 # or not configured for the given GIT_DIR
320 require PublicInbox::Search;
321 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
328 <html><head><title>Search not available for this
329 public-inbox</title><body><pre>Search is not available for this public-inbox
330 <a href="../">Return to index</a></pre></body></html>
332 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
337 my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
341 # /$LISTNAME/t/$MESSAGE_ID/mbox -> thread as mbox
342 # /$LISTNAME/t/$MESSAGE_ID/mbox.gz -> thread as gzipped mbox
343 # note: I'm not a big fan of other compression formats since they're
344 # significantly more expensive on CPU than gzip and less-widely available,
345 # especially on older systems. Stick to zlib since that's what git uses.
346 sub get_thread_mbox {
347 my ($ctx, $sfx) = @_;
348 my $srch = searcher($ctx) or return need_search($ctx);
349 require PublicInbox::Mbox;
350 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);