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 (lynx, w3m, 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, 0);
39 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
40 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
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 # convenience redirects, order matters
57 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t)/(\S+)\z!o) {
59 invalid_list_mid(\%ctx, $1, $3) ||
60 redirect_mid(\%ctx, $cgi, $2);
67 # for CoW-friendliness, MOOOOO!
69 require PublicInbox::Feed;
70 require PublicInbox::View;
71 require PublicInbox::Thread;
75 require XML::Atom::SimpleFeed;
78 # private functions below
80 sub r404 { r(404, 'Not Found') }
82 # simple response for errors
83 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
85 # returns undef if valid, array ref response if invalid
87 my ($ctx, $listname) = @_;
88 my $git_dir = $pi_config->get($listname, "mainrepo");
89 if (defined $git_dir) {
90 $ctx->{git_dir} = $git_dir;
91 $ctx->{listname} = $listname;
97 # returns undef if valid, array ref response if invalid
98 sub invalid_list_mid {
99 my ($ctx, $listname, $mid) = @_;
100 my $ret = invalid_list($ctx, $listname, $mid);
101 $ctx->{mid} = uri_unescape($mid) unless $ret;
105 # /$LISTNAME/atom.xml -> Atom feed, includes replies
107 my ($ctx, $cgi, $top) = @_;
108 require PublicInbox::Feed;
109 [ 200, [ 'Content-Type' => 'application/xml' ],
110 [ PublicInbox::Feed->generate({
111 git_dir => $ctx->{git_dir},
112 listname => $ctx->{listname},
113 pi_config => $pi_config,
120 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
122 my ($ctx, $cgi, $top) = @_;
123 require PublicInbox::Feed;
124 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
125 [ PublicInbox::Feed->generate_html_index({
126 git_dir => $ctx->{git_dir},
127 listname => $ctx->{listname},
128 pi_config => $pi_config,
130 footer => footer($ctx),
136 # just returns a string ref for the blob in the current ctx
139 my $hex = $ctx->{mid};
140 my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
142 unless (defined $x38) {
143 # compatibility with old links
145 $hex = Digest::SHA::sha1_hex($hex);
146 ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
147 defined $x38 or die "BUG: not a SHA-1 hex: $hex";
150 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
151 qw(cat-file blob), "HEAD:$x2/$x38");
152 my $cmd = join(' ', @cmd);
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 original
167 my ($ctx, $cgi) = @_;
168 my $x = mid2blob($ctx);
169 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
172 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
174 my ($ctx, $cgi) = @_;
175 my $x = mid2blob($ctx);
176 return r404() unless $x;
178 require PublicInbox::View;
179 my $pfx = msg_pfx($ctx);
180 my $foot = footer($ctx);
182 my $mime = Email::MIME->new($x);
183 my $srch = searcher($ctx);
184 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
185 [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
188 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
190 my ($ctx, $cgi) = @_;
191 my $x = mid2blob($ctx);
192 return r404() unless $x;
193 require PublicInbox::View;
194 my $foot = footer($ctx);
196 my $mime = Email::MIME->new($x);
197 my $srch = searcher($ctx);
198 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
199 [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
202 # /$LISTNAME/t/$MESSAGE_ID.html
204 my ($ctx, $cgi) = @_;
205 my $srch = searcher($ctx) or return need_search($ctx);
206 require PublicInbox::View;
207 my $foot = footer($ctx);
208 my $body = PublicInbox::View->thread_html($ctx, $foot, $srch) or
210 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
216 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
219 sub redirect_list_index {
220 my ($ctx, $cgi) = @_;
221 do_redirect(self_url($cgi) . "/");
225 my ($ctx, $cgi, $pfx) = @_;
226 my $url = self_url($cgi);
228 if (lc($pfx) eq 't') {
229 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
231 do_redirect($url . ".html$anchor");
237 [ Location => $url, 'Content-Type' => 'text/plain' ],
238 [ "Redirecting to $url\n" ]
243 my ($ctx, $key) = @_;
244 my $val = $ctx->{$key};
245 (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
252 if (open(my $fh, '<', $path)) {
262 return '' unless $ctx;
263 my $git_dir = ctx_get($ctx, 'git_dir');
265 # favor user-supplied footer
266 my $footer = try_cat("$git_dir/public-inbox/footer.html");
267 if (defined $footer) {
272 # auto-generate a footer
273 my $listname = ctx_get($ctx, 'listname');
274 my $desc = try_cat("$git_dir/description");
275 $desc = '$GIT_DIR/description missing' unless defined $desc;
278 my $urls = try_cat("$git_dir/cloneurl");
279 my @urls = split(/\r?\n/, $urls || '');
280 my $nurls = scalar @urls;
282 $urls = '($GIT_DIR/cloneurl missing)';
283 } elsif ($nurls == 1) {
284 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
285 '">ssoma</a>: ' . $urls[0];
287 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
288 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
291 my $addr = $pi_config->get($listname, 'address');
292 if (ref($addr) eq 'ARRAY') {
293 $addr = $addr->[0]; # first address is primary
296 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
300 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
301 'anybody may post in plain-text (not HTML):',
307 # search support is optional, returns undef if Xapian is not installed
308 # or not configured for the given GIT_DIR
312 require PublicInbox::Search;
313 PublicInbox::Search->new($ctx->{git_dir});
320 <html><head><title>Search not available for this
321 public-inbox</title><body><pre>Search is not available for this public-inbox
322 <a href="../">Return to index</a></pre></body></html>
324 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
329 my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));