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) = @_;
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(\%ctx, $cgi);
37 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
38 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi);
39 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
40 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi);
42 # single-message pages
43 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
44 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
45 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
46 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
49 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
50 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
53 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
54 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx, $cgi);
56 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.mbox\.gz!o) {
58 invalid_list_mid(\%ctx, $1, $2) || get_thread_mbox(\%ctx, $cgi);
60 } elsif ($path_info =~ m!$LISTNAME_RE/f/\S+\.txt\z!o) {
61 invalid_list_mid(\%ctx, $1, $2) ||
62 redirect_mid_txt(\%ctx, $cgi);
64 # convenience redirects, order matters
65 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
67 invalid_list_mid(\%ctx, $1, $3) ||
68 redirect_mid(\%ctx, $cgi, $2);
75 # for CoW-friendliness, MOOOOO!
77 require PublicInbox::Feed;
78 require PublicInbox::View;
79 require PublicInbox::Thread;
83 require XML::Atom::SimpleFeed;
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/atom.xml -> Atom feed, includes replies
115 my ($ctx, $cgi) = @_;
116 require PublicInbox::Feed;
117 $ctx->{pi_config} = $pi_config;
119 [ 200, [ 'Content-Type' => 'application/xml' ],
120 [ PublicInbox::Feed->generate($ctx) ] ]
123 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
125 my ($ctx, $cgi) = @_;
126 require PublicInbox::Feed;
127 my $srch = searcher($ctx);
128 $ctx->{pi_config} = $pi_config;
131 PublicInbox::Feed::generate_html_index($ctx);
134 # just returns a string ref for the blob in the current ctx
137 require PublicInbox::MID;
138 my $path = PublicInbox::MID::mid2path($ctx->{mid});
139 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
140 qw(cat-file blob), "HEAD:$path");
141 my $cmd = join(' ', @cmd);
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/m/$MESSAGE_ID.txt -> raw original
156 my ($ctx, $cgi) = @_;
157 my $x = mid2blob($ctx);
158 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
161 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
163 my ($ctx, $cgi) = @_;
164 my $x = mid2blob($ctx);
165 return r404() unless $x;
167 require PublicInbox::View;
168 my $pfx = msg_pfx($ctx);
169 my $foot = footer($ctx);
171 my $mime = Email::MIME->new($x);
172 my $srch = searcher($ctx);
173 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
174 [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
177 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
179 my ($ctx, $cgi) = @_;
180 my $x = mid2blob($ctx);
181 return r404() unless $x;
182 require PublicInbox::View;
183 my $foot = footer($ctx);
185 my $mime = Email::MIME->new($x);
186 my $srch = searcher($ctx);
187 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
188 [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
191 # /$LISTNAME/t/$MESSAGE_ID.html
193 my ($ctx, $cgi) = @_;
194 my $srch = searcher($ctx) or return need_search($ctx);
195 require PublicInbox::View;
196 my $foot = footer($ctx);
197 PublicInbox::View::thread_html($ctx, $foot, $srch);
202 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
205 sub redirect_list_index {
206 my ($ctx, $cgi) = @_;
207 do_redirect(self_url($cgi) . "/");
211 my ($ctx, $cgi, $pfx) = @_;
212 my $url = self_url($cgi);
214 if (lc($pfx) eq 't') {
215 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
217 do_redirect($url . ".html$anchor");
220 # only hit when somebody tries to guess URLs manually:
221 sub redirect_mid_txt {
222 my ($ctx, $cgi, $pfx) = @_;
223 my $listname = $ctx->{listname};
224 my $url = self_url($cgi);
225 $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
232 [ Location => $url, 'Content-Type' => 'text/plain' ],
233 [ "Redirecting to $url\n" ]
238 my ($ctx, $key) = @_;
239 my $val = $ctx->{$key};
240 (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
247 if (open(my $fh, '<', $path)) {
257 return '' unless $ctx;
258 my $git_dir = ctx_get($ctx, 'git_dir');
260 # favor user-supplied footer
261 my $footer = try_cat("$git_dir/public-inbox/footer.html");
262 if (defined $footer) {
264 $ctx->{footer} = $footer;
268 # auto-generate a footer
269 my $listname = ctx_get($ctx, 'listname');
270 my $desc = try_cat("$git_dir/description");
271 $desc = '$GIT_DIR/description missing' unless defined $desc;
274 my $urls = try_cat("$git_dir/cloneurl");
275 my @urls = split(/\r?\n/, $urls || '');
276 my $nurls = scalar @urls;
278 $urls = '($GIT_DIR/cloneurl missing)';
279 } elsif ($nurls == 1) {
280 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
281 '">ssoma</a>: ' . $urls[0];
283 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
284 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
287 my $addr = $pi_config->get($listname, 'address');
288 if (ref($addr) eq 'ARRAY') {
289 $addr = $addr->[0]; # first address is primary
292 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
294 $ctx->{footer} = join("\n",
296 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
297 'anybody may post in plain-text (not HTML):',
303 # search support is optional, returns undef if Xapian is not installed
304 # or not configured for the given GIT_DIR
308 require PublicInbox::Search;
309 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
316 <html><head><title>Search not available for this
317 public-inbox</title><body><pre>Search is not available for this public-inbox
318 <a href="../">Return to index</a></pre></body></html>
320 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
325 my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
329 # /$LISTNAME/t/$MESSAGE_ID.mbox.gz -> search results as gzipped mbox
330 # note: I'm not a big fan of other compression formats since they're
331 # significantly more expensive on CPU than gzip and less-widely available,
332 # especially on older systems. Stick to zlib since that's what git uses.
333 sub get_thread_mbox {
334 my ($ctx, $cgi) = @_;
335 my $srch = searcher($ctx) or return need_search($ctx);
336 require PublicInbox::Mbox;
337 PublicInbox::Mbox::thread_mbox($ctx, $srch);