X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=public-inbox.cgi;h=b9b484be38b5eb584892fcdb3e3670f4b38e7cb0;hb=48dc8ff5e67714985897047c189bdf019b796a60;hp=6a6f31e23ef81acd8045001190a3e7991c012290;hpb=6e4654d5c509a2772f9fa8dcd0c46e05ca5feaea;p=public-inbox.git diff --git a/public-inbox.cgi b/public-inbox.cgi index 6a6f31e2..b9b484be 100755 --- a/public-inbox.cgi +++ b/public-inbox.cgi @@ -15,7 +15,7 @@ use warnings; 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); +use URI::Escape qw(uri_escape uri_unescape); our $LISTNAME_RE = qr!\A/([\w\.\-]+)!; our $pi_config; BEGIN { @@ -45,21 +45,27 @@ 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/) { return r("405 Method Not Allowed"); } - my $path_info = decode_utf8($ENV{PATH_INFO}); + my $path_info = decode_utf8($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, 1); - } 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) { + 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 @@ -67,14 +73,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(); @@ -84,7 +90,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 { @@ -102,12 +108,11 @@ sub invalid_list { sub invalid_list_mid { my ($ctx, $listname, $mid) = @_; my $ret = invalid_list($ctx, $listname, $mid) and return $ret; - $ctx->{mid} = $mid; + $ctx->{mid} = uri_unescape($mid); undef; } -# /$LISTNAME/index.atom.xml -> Atom feed -# /$LISTNAME/all.atom.xml -> Atom feed, includes replies +# /$LISTNAME/atom.xml -> Atom feed, includes replies sub get_atom { my ($ctx, $cgi, $top) = @_; require PublicInbox::Feed; @@ -122,7 +127,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; @@ -141,7 +146,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`; @@ -161,10 +167,9 @@ 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 - require PublicInbox::View; + my $mid_href = PublicInbox::View::ascii_html(uri_escape($ctx->{mid})); + my $pfx = "../f/$mid_href.html"; require Email::MIME; [ "200 OK", {'Content-Type' => 'text/html'}, PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)]; @@ -181,6 +186,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) = @_;