use CGI qw(:cgi :escapeHTML -nosticky); # PSGI/FastCGI/mod_perl compat
use Encode qw(decode_utf8);
use PublicInbox::Config;
+use Digest::SHA qw(sha1_hex);
our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
our $pi_config;
BEGIN {
sub main {
my $cgi = CGI->new;
+ my %ctx;
if ($cgi->request_method !~ /\AGET|HEAD\z/) {
return r("405 Method Not Allowed");
}
my $path_info = decode_utf8($ENV{PATH_INFO});
+
+ # top-level indices and feeds
if ($path_info eq "/") {
- r("404 Not Found");
+ r404();
} elsif ($path_info =~ m!$LISTNAME_RE/?\z!o) {
- get_list_log($cgi, $1);
- } elsif ($path_info =~ m!$LISTNAME_RE/all\z!o) {
- get_list_all($cgi, $1);
+ invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
} elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
- get_atom_index($cgi, $1);
+ invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
} elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
- get_atom_all($cgi, $1);
- } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.txt\z!o) {
- get_mid_txt($cgi, $1, $2);
- } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.html\z!o) {
- get_mid_html($cgi, $1, $2);
- } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\z!o) {
+ invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
+
+ # single-message pages
+ } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
+ invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
+ } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
+ invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
+ } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
redirect_mid_html($cgi, $1, $2);
} else {
- r("404 Not Found");
+ r404();
}
}
+sub r404 { r("404 Not Found") }
+
# simple response for errors
sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
-# /$LISTNAME/all.atom.xml -> Atom feed, includes replies
-sub get_atom_all {
- my ($cgi, $listname) = @_;
+# returns undef if valid, array ref response if invalid
+sub invalid_list {
+ my ($ctx, $listname) = @_;
my $git_dir = $pi_config->get($listname, "mainrepo");
- defined $git_dir or return r("404 Not Found");
+ if (defined $git_dir) {
+ $ctx->{git_dir} = $git_dir;
+ $ctx->{listname} = $listname;
+ return undef;
+ }
+ r404();
+}
+
+# returns undef if valid, array ref response if invalid
+sub invalid_list_mid {
+ my ($ctx, $listname, $mid) = @_;
+ my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
+ $ctx->{mid} = $mid;
+ undef;
+}
+sub get_atom {
+ my ($ctx, $cgi, $top) = @_;
require PublicInbox::Feed;
- [ '200 OK',
- { 'Content-Type' => 'application/xml; charset=us-ascii' },
+ [ '200 OK', { 'Content-Type' => 'application/xml' },
PublicInbox::Feed->generate({
- git_dir => $git_dir,
+ git_dir => $ctx->{git_dir},
+ listname => $ctx->{listname},
pi_config => $pi_config,
- listname => $listname,
- cgi => $cgi
+ cgi => $cgi,
+ top => $top,
})
];
}
-# /$LISTNAME/index.atom.xml -> Atom feed
-sub get_atom_index {
- my ($cgi, $listname) = @_;
- my $git_dir = $pi_config->get($listname, "mainrepo");
- defined $git_dir or return r("404 Not Found");
-
+sub get_index {
+ my ($ctx, $cgi, $top) = @_;
require PublicInbox::Feed;
- [ '200 OK',
- { 'Content-Type' => 'application/xml; charset=us-ascii' },
- PublicInbox::Feed->generate({
- git_dir => $git_dir,
+ [ '200 OK', { 'Content-Type' => 'text/html' },
+ PublicInbox::Feed->generate_html_index({
+ git_dir => $ctx->{git_dir},
+ listname => $ctx->{listname},
pi_config => $pi_config,
- listname => $listname,
cgi => $cgi,
- top => 1
+ top => $top,
})
];
}
+
+sub mid2blob {
+ my ($ctx) = @_;
+ local $ENV{GIT_DIR} = $ctx->{git_dir};
+ my $hex = 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 $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
+ $? == 0 ? \$blob : undef;
+}
+
+sub get_mid_txt {
+ my ($ctx, $cgi) = @_;
+ my $x = mid2blob($ctx);
+ $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
+}