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);
49 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/T/\z!o) {
51 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
53 # single-message pages
54 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/\z!o) {
55 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx);
56 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/raw\z!o) {
57 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
60 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/f/\z!o) {
61 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
63 # convenience redirects order matters
64 } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
68 legacy_redirects(\%ctx, $path_info);
72 # for CoW-friendliness, MOOOOO!
74 require PublicInbox::Feed;
75 require PublicInbox::View;
76 require PublicInbox::Thread;
77 require PublicInbox::GitCatFile;
83 require PublicInbox::Search;
84 require PublicInbox::Mbox;
85 require IO::Compress::Gzip;
89 # private functions below
93 if ($ctx && $ctx->{mid}) {
94 require PublicInbox::ExtMsg;
95 return PublicInbox::ExtMsg::ext_msg($ctx);
100 # simple response for errors
101 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
103 # returns undef if valid, array ref response if invalid
105 my ($ctx, $listname) = @_;
106 my $git_dir = $pi_config->get($listname, "mainrepo");
107 if (defined $git_dir) {
108 $ctx->{git_dir} = $git_dir;
109 $ctx->{listname} = $listname;
115 # returns undef if valid, array ref response if invalid
116 sub invalid_list_mid {
117 my ($ctx, $listname, $mid) = @_;
118 my $ret = invalid_list($ctx, $listname, $mid);
121 $ctx->{mid} = $mid = uri_unescape($mid);
122 if ($mid =~ /\A[a-f0-9]{40}\z/) {
123 if ($mid = mid2blob($ctx)) {
124 require Email::Simple;
125 use PublicInbox::MID qw/mid_clean/;
126 $mid = Email::Simple->new($mid);
127 $ctx->{mid} = mid_clean($mid->header('Message-ID'));
133 # /$LISTNAME/new.atom -> Atom feed, includes replies
136 require PublicInbox::Feed;
137 PublicInbox::Feed::generate($ctx);
140 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
143 require PublicInbox::Feed;
144 my $srch = searcher($ctx);
146 PublicInbox::Feed::generate_html_index($ctx);
149 # just returns a string ref for the blob in the current ctx
152 require PublicInbox::MID;
153 my $path = PublicInbox::MID::mid2path($ctx->{mid});
154 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
155 qw(cat-file blob), "HEAD:$path");
156 my $pid = open my $fh, '-|';
157 defined $pid or die "fork failed: $!\n";
159 open STDERR, '>', '/dev/null'; # ignore errors
160 exec @cmd or die "exec failed: $!\n";
162 my $blob = eval { local $/; <$fh> };
164 $? == 0 ? \$blob : undef;
168 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
171 my $x = mid2blob($ctx) or return r404($ctx);
172 require PublicInbox::Mbox;
173 PublicInbox::Mbox::emit1($x);
176 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
179 my $x = mid2blob($ctx) or return r404($ctx);
181 require PublicInbox::View;
182 my $foot = footer($ctx);
184 my $mime = Email::MIME->new($x);
186 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
187 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
190 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
193 my $x = mid2blob($ctx) or return r404($ctx);
195 require PublicInbox::View;
196 my $foot = footer($ctx);
198 my $mime = Email::MIME->new($x);
200 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
201 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
204 # /$LISTNAME/$MESSAGE_ID/t/
207 my $srch = searcher($ctx) or return need_search($ctx);
208 require PublicInbox::View;
209 my $foot = footer($ctx);
210 PublicInbox::View::thread_html($ctx, $foot, $srch);
215 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
219 my ($ctx, $key) = @_;
220 my $val = $ctx->{$key};
221 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
228 if (open(my $fh, '<', $path)) {
238 return '' unless $ctx;
239 my $git_dir = ctx_get($ctx, 'git_dir');
241 # favor user-supplied footer
242 my $footer = try_cat("$git_dir/public-inbox/footer.html");
243 if (defined $footer) {
245 $ctx->{footer} = $footer;
249 # auto-generate a footer
250 my $listname = ctx_get($ctx, 'listname');
251 my $desc = try_cat("$git_dir/description");
252 $desc = '$GIT_DIR/description missing' unless defined $desc;
255 my $urls = try_cat("$git_dir/cloneurl");
256 my @urls = split(/\r?\n/, $urls || '');
257 my $nurls = scalar @urls;
259 $urls = '($GIT_DIR/cloneurl missing)';
260 } elsif ($nurls == 1) {
261 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
262 '">ssoma</a>: ' . $urls[0];
264 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
265 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
268 my $addr = $pi_config->get($listname, 'address');
269 if (ref($addr) eq 'ARRAY') {
270 $addr = $addr->[0]; # first address is primary
273 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
275 $ctx->{footer} = join("\n",
277 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
278 'anybody may post in plain-text (not HTML):',
284 # search support is optional, returns undef if Xapian is not installed
285 # or not configured for the given GIT_DIR
289 require PublicInbox::Search;
290 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
297 <html><head><title>Search not available for this
298 public-inbox</title><body><pre>Search is not available for this public-inbox
299 <a href="../">Return to index</a></pre></body></html>
301 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
304 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
305 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
306 # note: I'm not a big fan of other compression formats since they're
307 # significantly more expensive on CPU than gzip and less-widely available,
308 # especially on older systems. Stick to zlib since that's what git uses.
309 sub get_thread_mbox {
310 my ($ctx, $sfx) = @_;
311 my $srch = searcher($ctx) or return need_search($ctx);
312 require PublicInbox::Mbox;
313 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
317 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
318 sub get_thread_atom {
320 searcher($ctx) or return need_search($ctx);
321 $ctx->{self_url} = self_url($ctx->{cgi});
322 require PublicInbox::Feed;
323 PublicInbox::Feed::generate_thread_atom($ctx);
326 sub legacy_redirects {
327 my ($ctx, $path_info) = @_;
329 # single-message pages
330 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
332 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
333 r301($ctx, $1, $2, 'raw');
335 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
336 r301($ctx, $1, $2, 'f/');
339 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
340 r301($ctx, $1, $2, 't/#u');
342 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
343 r301($ctx, $1, $2, "t.mbox$3");
345 # even older legacy redirects
346 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
349 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
350 r301($ctx, $1, $2, 't/#u');
352 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
353 r301($ctx, $1, $2, 'f/');
355 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
356 r301($ctx, $1, $2, 'raw');
358 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
359 r301($ctx, $1, $2, "t$3");
361 # legacy convenience redirects, order still matters
362 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
364 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
365 r301($ctx, $1, $2, 't/#u');
366 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
367 r301($ctx, $1, $2, 'f/');
375 my ($ctx, $listname, $mid, $suffix) = @_;
376 my $cgi = $ctx->{cgi};
378 if (ref($cgi) eq 'CGI') {
379 $url = $cgi->url(-base) . '/';
381 $url = $cgi->base->as_string;
384 $url .= $listname . '/';
385 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
386 $url .= $suffix if (defined $suffix);
389 [ Location => $url, 'Content-Type' => 'text/plain' ],
390 [ "Redirecting to $url\n" ] ]