+sub need_search {
+ my ($ctx) = @_;
+ my $msg = <<EOF;
+<html><head><title>Search not available for this
+public-inbox</title><body><pre>Search is not available for this public-inbox
+<a href="../">Return to index</a></pre></body></html>
+EOF
+ [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
+}
+
+# /$INBOX/$MESSAGE_ID/t.mbox -> thread as mbox
+# /$INBOX/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
+# note: I'm not a big fan of other compression formats since they're
+# significantly more expensive on CPU than gzip and less-widely available,
+# especially on older systems. Stick to zlib since that's what git uses.
+sub get_thread_mbox {
+ my ($ctx, $sfx) = @_;
+ my $srch = searcher($ctx) or return need_search($ctx);
+ require PublicInbox::Mbox;
+ PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
+}
+
+
+# /$INBOX/$MESSAGE_ID/t.atom -> thread as Atom feed
+sub get_thread_atom {
+ my ($ctx) = @_;
+ searcher($ctx) or return need_search($ctx);
+ $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
+ require PublicInbox::Feed;
+ PublicInbox::Feed::generate_thread_atom($ctx);
+}
+
+sub legacy_redirects {
+ my ($self, $ctx, $path_info) = @_;
+
+ # single-message pages
+ if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
+ r301($ctx, $1, $2);
+ } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
+ r301($ctx, $1, $2, 'raw');
+
+ } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
+ r301($ctx, $1, $2);
+
+ # thread display
+ } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
+ r301($ctx, $1, $2, 't/#u');
+
+ } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
+ r301($ctx, $1, $2, "t.mbox$3");
+
+ # even older legacy redirects
+ } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
+ r301($ctx, $1, $2);
+
+ } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
+ r301($ctx, $1, $2, 't/#u');
+
+ } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
+ r301($ctx, $1, $2);
+
+ } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
+ r301($ctx, $1, $2, 'raw');
+
+ } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
+ r301($ctx, $1, $2, "t$3");
+
+ # legacy convenience redirects, order still matters
+ } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
+ r301($ctx, $1, $2);
+ } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
+ r301($ctx, $1, $2, 't/#u');
+ } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
+ r301($ctx, $1, $2);
+
+ # some Message-IDs have slashes in them and the HTTP server
+ # may try to be clever and unescape them :<
+ } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
+ msg_page($self, $ctx, $1, $2, $3);
+
+ # in case people leave off the trailing slash:
+ } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
+ r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
+ } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
+ r301($ctx, $1, $2);
+ } else {
+ $self->news_www->call($ctx->{cgi}->{env});
+ }
+}
+
+sub r301 {
+ my ($ctx, $inbox, $mid, $suffix) = @_;
+ my $cgi = $ctx->{cgi};
+ my $obj = $ctx->{-inbox};
+ unless ($obj) {
+ my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
+ return $r404 if $r404;
+ $obj = $ctx->{-inbox};
+ }
+ my $url = $obj->base_url($ctx->{env});
+ my $qs = $ctx->{env}->{QUERY_STRING};
+ $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
+ $url .= $suffix if (defined $suffix);
+ $url .= "?$qs" if $qs ne '';
+
+ [ 301,
+ [ Location => $url, 'Content-Type' => 'text/plain' ],
+ [ "Redirecting to $url\n" ] ]
+}
+
+sub msg_page {
+ my ($self, $ctx, $inbox, $mid, $e) = @_;
+ my $ret;
+ $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
+ '' eq $e and return get_mid_html($ctx);
+ 'T/' eq $e and return get_thread($ctx, 1);
+ 't/' eq $e and return get_thread($ctx);
+ 't.atom' eq $e and return get_thread_atom($ctx);
+ 't.mbox' eq $e and return get_thread_mbox($ctx);
+ 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
+ 'raw' eq $e and return get_mid_txt($ctx);
+
+ # legacy, but no redirect for compatibility:
+ 'f/' eq $e and return get_mid_html($ctx);
+ r404($ctx);
+}
+
+sub serve_git {
+ my ($env, $git, $path) = @_;
+ PublicInbox::GitHTTPBackend::serve($env, $git, $path);
+}
+
+sub serve_mbox_range {
+ my ($self, $ctx, $inbox, $range) = @_;
+ invalid_inbox($self, $ctx, $inbox) || eval {
+ require PublicInbox::Mbox;
+ searcher($ctx);
+ PublicInbox::Mbox::emit_range($ctx, $range);
+ }
+}
+
+sub news_www {
+ my ($self) = @_;
+ my $nw = $self->{news_www};
+ return $nw if $nw;
+ require PublicInbox::NewsWWW;
+ $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
+}
+
+sub get_attach {
+ my ($ctx, $idx, $fn) = @_;
+ require PublicInbox::WwwAttach;
+ PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
+}
+