-# /$LISTNAME/atom.xml -> Atom feed, includes replies
-sub get_atom {
- my ($ctx, $cgi, $top) = @_;
- require PublicInbox::Feed;
- [ 200, [ 'Content-Type' => 'application/xml' ],
- [ PublicInbox::Feed->generate({
- git_dir => $ctx->{git_dir},
- listname => $ctx->{listname},
- pi_config => $pi_config,
- cgi => $cgi,
- top => $top,
- }) ]
- ];
-}
-
-# /$LISTNAME/?r=$GIT_COMMIT -> HTML only
-sub get_index {
- my ($ctx, $cgi, $top) = @_;
- require PublicInbox::Feed;
- [ 200, [ 'Content-Type' => 'text/html' ],
- [ PublicInbox::Feed->generate_html_index({
- git_dir => $ctx->{git_dir},
- listname => $ctx->{listname},
- pi_config => $pi_config,
- cgi => $cgi,
- top => $top,
- }) ]
- ];
-}
-
-# just returns a string ref for the blob in the current ctx
-sub mid2blob {
- my ($ctx) = @_;
- require Digest::SHA;
- my $hex = Digest::SHA::sha1_hex($ctx->{mid});
- $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
- die "BUG: not a SHA-1 hex: $hex";
-
- my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
- qw(cat-file blob), "HEAD:$1/$2");
- my $cmd = join(' ', @cmd);
- my $pid = open my $fh, '-|';
- defined $pid or die "fork failed: $!\n";
- if ($pid == 0) {
- open STDERR, '>', '/dev/null'; # ignore errors
- exec @cmd or die "exec failed: $!\n";
- } else {
- my $blob = eval { local $/; <$fh> };
- close $fh;
- $? == 0 ? \$blob : undef;
- }
-}
-
-# /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
-sub get_mid_txt {
- my ($ctx, $cgi) = @_;
- my $x = mid2blob($ctx);
- $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
-}
-
-# /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
-sub get_mid_html {
- my ($ctx, $cgi) = @_;
- my $x = mid2blob($ctx);
- return r404() unless $x;
-
- require PublicInbox::View;
- my $mid_href = PublicInbox::Hval::ascii_html(
- uri_escape_utf8($ctx->{mid}));
- my $pfx = "../f/$mid_href.html";
- require Email::MIME;
- [ 200, [ 'Content-Type' => 'text/html' ],
- [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
-}
-
-# /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
-sub get_full_html {
- my ($ctx, $cgi) = @_;
- my $x = mid2blob($ctx);
- return r404() unless $x;
- require PublicInbox::View;
- require Email::MIME;
- [ 200, [ 'Content-Type' => 'text/html' ],
- [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
-}
-
-sub self_url {
- my ($cgi) = @_;
- ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
-}
-
-sub redirect_list_index {
- my ($ctx, $cgi) = @_;
- do_redirect(self_url($cgi) . "/");
-}
-
-sub redirect_mid {
- my ($ctx, $cgi) = @_;
- my $url = self_url($cgi);
- $url =~ s!/f/!/m/!;
- do_redirect($url . '.html');
-}
-
-sub do_redirect {
- my ($url) = @_;
- [ 301,
- [ Location => $url, 'Content-Type' => 'text/plain' ],
- [ "Redirecting to $url\n" ]
- ]
-}
-
-sub psgi_app {
- # preload so we are CoW friendly
- require PublicInbox::Feed;
- require PublicInbox::View;
- require Mail::Thread;
- require Digest::SHA;
- require POSIX;
- require XML::Atom::SimpleFeed;
- require Plack::Request;
- sub {
- my $req = Plack::Request->new(@_);
- main($req, $req->method);
- };
-}
-
-sub cgi_print {
- my ($ret) = @_;
- my ($status, $headers, $body) = @$ret;
- my %codes = (
- 200 => 'OK',
- 301 => 'Moved Permanently',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- );
-
- print "Status: $status $codes{$status}\r\n";
- my @tmp = @$headers;
- while (my ($k, $v) = splice(@tmp, 0, 2)) {
- print "$k: $v\r\n";
- }
- print "\r\n", $body->[0];
-}