]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
wwwstatic: move r(...) functions here
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 251979d5db6ee88af2defb614f2eed43ea5edb6e..99f9f1dc258d2af531349e22eec173c64abd4119 100644 (file)
@@ -22,6 +22,7 @@ use PublicInbox::MID qw(mid_escape);
 require PublicInbox::Git;
 use PublicInbox::GitHTTPBackend;
 use PublicInbox::UserContent;
+use PublicInbox::WwwStatic qw(r);
 
 # TODO: consider a routing tree now that we have more endpoints:
 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
@@ -42,15 +43,17 @@ sub run {
        PublicInbox::WWW->new->call($req->env);
 }
 
+# PATH_INFO is decoded, and we want the undecoded original
 my %path_re_cache;
-
-sub path_re ($) {
-       my $sn = $_[0]->{SCRIPT_NAME};
-       $path_re_cache{$sn} ||= do {
+sub path_info_raw ($) {
+       my ($env) = @_;
+       my $sn = $env->{SCRIPT_NAME};
+       my $re = $path_re_cache{$sn} ||= do {
                $sn = '/'.$sn unless index($sn, '/') == 0;
                $sn =~ s!/\z!!;
                qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
        };
+       $env->{REQUEST_URI} =~ $re ? $1 : $env->{PATH_INFO};
 }
 
 sub call {
@@ -67,9 +70,7 @@ sub call {
                $k => $v;
        } split(/[&;]+/, $env->{QUERY_STRING});
 
-       # avoiding $env->{PATH_INFO} here since that's already decoded
-       my ($path_info) = ($env->{REQUEST_URI} =~ path_re($env));
-       $path_info //= $env->{PATH_INFO};
+       my $path_info = path_info_raw($env);
        my $method = $env->{REQUEST_METHOD};
 
        if ($method eq 'POST') {
@@ -83,7 +84,7 @@ sub call {
                }
        }
        elsif ($method !~ /\AGET|HEAD\z/) {
-               return r(405, 'Method Not Allowed');
+               return r(405);
        }
 
        # top-level indices and feeds
@@ -176,12 +177,9 @@ sub r404 {
                require PublicInbox::ExtMsg;
                return PublicInbox::ExtMsg::ext_msg($ctx);
        }
-       r(404, 'Not Found');
+       r(404);
 }
 
-# simple response for errors
-sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
-
 sub news_cgit_fallback ($) {
        my ($ctx) = @_;
        my $www = $ctx->{www};