about summary refs log tree commit homepage
path: root/public-inbox.cgi
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2014-04-14 07:53:53 +0000
committerEric Wong <e@80x24.org>2014-04-14 08:04:12 +0000
commit513defd67b678c8b8f2ab571e44d20a30b49c6f8 (patch)
treed1f7cd1ca4ae8a23476996e61b42abc39957989c /public-inbox.cgi
parent4e519086a895d161df658c7d5028dafbb4cfb30f (diff)
downloadpublic-inbox-513defd67b678c8b8f2ab571e44d20a30b49c6f8.tar.gz
It is common to type upper-level URLs without the slash,
redirect users to the correct page for usability.
Diffstat (limited to 'public-inbox.cgi')
-rwxr-xr-xpublic-inbox.cgi21
1 files changed, 21 insertions, 0 deletions
diff --git a/public-inbox.cgi b/public-inbox.cgi
index 1765cf45..17eb5b7b 100755
--- a/public-inbox.cgi
+++ b/public-inbox.cgi
@@ -46,6 +46,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/) {
@@ -77,6 +83,8 @@ sub main {
         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
                 redirect_mid_html($cgi, $1, $2);
 
+        } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
+                invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
         } else {
                 r404();
         }
@@ -182,6 +190,19 @@ 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 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) = @_;