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\.\-]+)!;
22 my ($cgi, $method) = @_;
23 $pi_config ||= PublicInbox::Config->new;
24 my %ctx = (cgi => $cgi, pi_config => $pi_config);
25 if ($method !~ /\AGET|HEAD\z/) {
26 return r(405, 'Method Not Allowed');
28 my $path_info = $cgi->path_info;
30 # top-level indices and feeds
31 if ($path_info eq '/') {
33 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
34 invalid_list(\%ctx, $1) || redirect_list_index($cgi);
35 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
36 invalid_list(\%ctx, $1) || get_index(\%ctx);
37 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
38 invalid_list(\%ctx, $1) || get_atom(\%ctx);
40 # single-message pages
41 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
42 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx);
43 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
44 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
47 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
48 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
51 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
52 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
54 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
56 invalid_list_mid(\%ctx, $1, $2) ||
57 get_thread_mbox(\%ctx, $sfx);
59 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/atom\z!o) {
60 invalid_list_mid(\%ctx, $1, $2) || get_thread_atom(\%ctx);
63 } elsif ($path_info =~ m!$LISTNAME_RE/(t|m|f)/(\S+)\.html\z!o) {
65 invalid_list_mid(\%ctx, $1, $3) ||
66 redirect_mid(\%ctx, $pfx, qr/\.html\z/, '/');
67 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f)/(\S+)\.txt\z!o) {
69 invalid_list_mid(\%ctx, $1, $3) ||
70 redirect_mid(\%ctx, $pfx, qr/\.txt\z/, '/raw');
71 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
73 invalid_list_mid(\%ctx, $1, $2) ||
74 redirect_mid(\%ctx, 't', $end, '/mbox.gz');
76 # convenience redirects, order matters
77 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
79 invalid_list_mid(\%ctx, $1, $3) ||
80 redirect_mid(\%ctx, $pfx, qr/\z/, '/');
87 # for CoW-friendliness, MOOOOO!
89 require PublicInbox::Feed;
90 require PublicInbox::View;
91 require PublicInbox::Thread;
92 require PublicInbox::GitCatFile;
98 require PublicInbox::Search;
99 require PublicInbox::Mbox;
100 require IO::Compress::Gzip;
104 # private functions below
106 sub r404 { r(404, 'Not Found') }
108 # simple response for errors
109 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
111 # returns undef if valid, array ref response if invalid
113 my ($ctx, $listname) = @_;
114 my $git_dir = $pi_config->get($listname, "mainrepo");
115 if (defined $git_dir) {
116 $ctx->{git_dir} = $git_dir;
117 $ctx->{listname} = $listname;
123 # returns undef if valid, array ref response if invalid
124 sub invalid_list_mid {
125 my ($ctx, $listname, $mid) = @_;
126 my $ret = invalid_list($ctx, $listname, $mid);
127 $ctx->{mid} = uri_unescape($mid) unless $ret;
131 # /$LISTNAME/atom.xml -> Atom feed, includes replies
134 require PublicInbox::Feed;
135 PublicInbox::Feed::generate($ctx);
138 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
141 require PublicInbox::Feed;
142 my $srch = searcher($ctx);
144 PublicInbox::Feed::generate_html_index($ctx);
147 # just returns a string ref for the blob in the current ctx
150 require PublicInbox::MID;
151 my $path = PublicInbox::MID::mid2path($ctx->{mid});
152 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
153 qw(cat-file blob), "HEAD:$path");
154 my $pid = open my $fh, '-|';
155 defined $pid or die "fork failed: $!\n";
157 open STDERR, '>', '/dev/null'; # ignore errors
158 exec @cmd or die "exec failed: $!\n";
160 my $blob = eval { local $/; <$fh> };
162 $? == 0 ? \$blob : undef;
166 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw mbox
169 my $x = mid2blob($ctx) or return r404();
170 require PublicInbox::Mbox;
171 PublicInbox::Mbox::emit1($x);
174 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
177 my $x = mid2blob($ctx) or return r404();
179 require PublicInbox::View;
180 my $pfx = msg_pfx($ctx);
181 my $foot = footer($ctx);
183 my $mime = Email::MIME->new($x);
185 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
186 [ PublicInbox::View::msg_html($ctx, $mime, $pfx, $foot) ] ];
189 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
192 my $x = mid2blob($ctx) or return r404();
194 require PublicInbox::View;
195 my $foot = footer($ctx);
197 my $mime = Email::MIME->new($x);
199 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
200 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
203 # /$LISTNAME/t/$MESSAGE_ID.html
206 my $srch = searcher($ctx) or return need_search($ctx);
207 require PublicInbox::View;
208 my $foot = footer($ctx);
209 PublicInbox::View::thread_html($ctx, $foot, $srch);
214 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
217 sub redirect_list_index {
219 do_redirect(self_url($cgi) . "/");
223 my ($ctx, $pfx, $old, $sfx) = @_;
224 my $url = self_url($ctx->{cgi});
226 if (lc($pfx) eq 't' && $sfx eq '/') {
227 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
229 $url =~ s/$old/$sfx/;
230 do_redirect($url . $anchor);
233 # only hit when somebody tries to guess URLs manually:
234 sub redirect_mid_txt {
235 my ($ctx, $pfx) = @_;
236 my $listname = $ctx->{listname};
237 my $url = self_url($ctx->{cgi});
238 $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
245 [ Location => $url, 'Content-Type' => 'text/plain' ],
246 [ "Redirecting to $url\n" ]
251 my ($ctx, $key) = @_;
252 my $val = $ctx->{$key};
253 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
260 if (open(my $fh, '<', $path)) {
270 return '' unless $ctx;
271 my $git_dir = ctx_get($ctx, 'git_dir');
273 # favor user-supplied footer
274 my $footer = try_cat("$git_dir/public-inbox/footer.html");
275 if (defined $footer) {
277 $ctx->{footer} = $footer;
281 # auto-generate a footer
282 my $listname = ctx_get($ctx, 'listname');
283 my $desc = try_cat("$git_dir/description");
284 $desc = '$GIT_DIR/description missing' unless defined $desc;
287 my $urls = try_cat("$git_dir/cloneurl");
288 my @urls = split(/\r?\n/, $urls || '');
289 my $nurls = scalar @urls;
291 $urls = '($GIT_DIR/cloneurl missing)';
292 } elsif ($nurls == 1) {
293 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
294 '">ssoma</a>: ' . $urls[0];
296 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
297 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
300 my $addr = $pi_config->get($listname, 'address');
301 if (ref($addr) eq 'ARRAY') {
302 $addr = $addr->[0]; # first address is primary
305 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
307 $ctx->{footer} = join("\n",
309 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
310 'anybody may post in plain-text (not HTML):',
316 # search support is optional, returns undef if Xapian is not installed
317 # or not configured for the given GIT_DIR
321 require PublicInbox::Search;
322 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
329 <html><head><title>Search not available for this
330 public-inbox</title><body><pre>Search is not available for this public-inbox
331 <a href="../">Return to index</a></pre></body></html>
333 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
338 my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
342 # /$LISTNAME/t/$MESSAGE_ID/mbox -> thread as mbox
343 # /$LISTNAME/t/$MESSAGE_ID/mbox.gz -> thread as gzipped mbox
344 # note: I'm not a big fan of other compression formats since they're
345 # significantly more expensive on CPU than gzip and less-widely available,
346 # especially on older systems. Stick to zlib since that's what git uses.
347 sub get_thread_mbox {
348 my ($ctx, $sfx) = @_;
349 my $srch = searcher($ctx) or return need_search($ctx);
350 require PublicInbox::Mbox;
351 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
355 # /$LISTNAME/t/$MESSAGE_ID/atom -> thread as Atom feed
356 sub get_thread_atom {
358 searcher($ctx) or return need_search($ctx);
359 $ctx->{self_url} = self_url($ctx->{cgi});
360 require PublicInbox::Feed;
361 PublicInbox::Feed::generate_thread_atom($ctx);