-use PublicInbox::Config;
-use URI::Escape qw(uri_escape_utf8 uri_unescape);
-our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
-our $pi_config;
-BEGIN {
- $pi_config = PublicInbox::Config->new;
- # TODO: detect and reload config as needed
- if ($ENV{MOD_PERL}) {
- require CGI;
- $CGI::NOSTICKY = 1;
- CGI->compile;
- }
-}
-
-if ($ENV{PI_PLACKUP}) {
- psgi_app();
-} else {
- # some servers (Ruby webrick) include scheme://host[:port] here,
- # which confuses CGI.pm when generating self_url.
- # RFC 3875 does not mention REQUEST_URI at all,
- # so nuke it since CGI.pm functions without it.
- require CGI;
- delete $ENV{REQUEST_URI};
- my $req = CGI->new;
- my $ret = main($req, $req->request_method);
- binmode STDOUT;
- if (@ARGV && $ARGV[0] eq 'static') {
- print $ret->[2]->[0];
- } else { # CGI
- cgi_print($ret);
- }
-}
-
-# private functions below
-
-sub main {
- my ($cgi, $method) = @_;
- my %ctx;
- if ($method !~ /\AGET|HEAD\z/) {
- return r(405, 'Method Not Allowed');
- }
- my $path_info = $cgi->path_info;
-
- # top-level indices and feeds
- if ($path_info eq '/') {
- r404();
- } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
- invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
- } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
- invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
- } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\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);
-
- # full-message page
- } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
- invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
-
- # convenience redirects, order matters
- } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
- invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
-
- } else {
- r404();
- }
-}
-
-sub r404 { r(404, 'Not Found') }
-
-# simple response for errors
-sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
-
-# returns undef if valid, array ref response if invalid
-sub invalid_list {
- my ($ctx, $listname) = @_;
- my $git_dir = $pi_config->get($listname, "mainrepo");
- if (defined $git_dir) {
- $ctx->{git_dir} = $git_dir;
- $ctx->{listname} = $listname;
- return;