]> Sergey Matveev's repositories - public-inbox.git/blobdiff - public-inbox.cgi
cgi: delay some requires
[public-inbox.git] / public-inbox.cgi
index 1765cf4549bc51b334ee5061e2176df0a3038040..03202c61736afa7887887ab7a8786a8584bc2105 100755 (executable)
@@ -15,9 +15,8 @@ use warnings;
 use CGI qw(:cgi :escapeHTML -nosticky); # PSGI/FastCGI/mod_perl compat
 use Encode qw(decode_utf8);
 use PublicInbox::Config;
-use URI::Escape qw(uri_unescape);
-use Digest::SHA qw(sha1_hex);
-our $LISTNAME_RE = qr!\A(?:/.*?)?/([\w\.\-]+)!;
+use URI::Escape qw(uri_escape uri_unescape);
+our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
 our $pi_config;
 BEGIN {
        $pi_config = PublicInbox::Config->new;
@@ -46,6 +45,12 @@ if (@ARGV && $ARGV[0] eq 'static') {
 # private functions below
 
 sub main {
+       # 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.
+       delete $ENV{REQUEST_URI};
+
        my $cgi = CGI->new;
        my %ctx;
        if ($cgi->request_method !~ /\AGET|HEAD\z/) {
@@ -56,8 +61,10 @@ sub main {
        # top-level indices and feeds
        if ($path_info eq "/") {
                r404();
-       } elsif ($path_info =~ m!$LISTNAME_RE/(?:index\.html)?\z!o) {
-               invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
+       } 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/index\.atom\.xml\z!o) {
                invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
        } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
@@ -68,14 +75,14 @@ sub main {
                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);
 
        # 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);
-       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
-               redirect_mid_html($cgi, $1, $2);
+
+       # convenience redirect
+       } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
 
        } else {
                r404();
@@ -85,7 +92,7 @@ sub main {
 sub r404 { r("404 Not Found") }
 
 # simple response for errors
-sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
+sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, $_[0]."\n" ] }
 
 # returns undef if valid, array ref response if invalid
 sub invalid_list {
@@ -123,7 +130,7 @@ sub get_atom {
        ];
 }
 
-# /$LISTNAME/?before=$GIT_COMMIT                 -> HTML only
+# /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
 sub get_index {
        my ($ctx, $cgi, $top) = @_;
        require PublicInbox::Feed;
@@ -142,7 +149,8 @@ sub get_index {
 sub mid2blob {
        my ($ctx) = @_;
        local $ENV{GIT_DIR} = $ctx->{git_dir};
-       my $hex = sha1_hex($ctx->{mid});
+       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 $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
@@ -162,9 +170,7 @@ sub get_mid_html {
        my $x = mid2blob($ctx);
        return r404() unless $x;
 
-       my $pfx = $cgi->self_url;
-       $pfx =~ s!/m/.+\z!/!; # FIXME: this is not robust
-
+       my $pfx = "../f/" . uri_escape($ctx->{mid}) . ".html";
        require PublicInbox::View;
        require Email::MIME;
        [ "200 OK", {'Content-Type' => 'text/html'},
@@ -182,6 +188,26 @@ sub get_full_html {
                PublicInbox::View->as_html(Email::MIME->new($$x))];
 }
 
+sub redirect_list_index {
+       my ($ctx, $cgi) = @_;
+       do_redirect($cgi->self_url . "/");
+}
+
+sub redirect_mid {
+       my ($ctx, $cgi) = @_;
+       my $url = $cgi->self_url;
+       $url =~ s!/f/!/m/!;
+       do_redirect($url . '.html');
+}
+
+sub do_redirect {
+       my ($url) = @_;
+       [ '301 Moved Permanently',
+         { Location => $url, 'Content-Type' => 'text/plain' },
+         "Redirecting to $url\n"
+       ]
+}
+
 # only used for CGI and static file generation modes
 sub set_binmode {
        my ($headers) = @_;