]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
www: stricter regexp for 405 errors
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 4b7177c1b157b2e4c2b5a7f8841123f287c928a1..3ce7cc2a7b6ddb2ef74616898e60accd3f92b0ae 100644 (file)
@@ -15,7 +15,6 @@ use 5.010_001;
 use strict;
 use warnings;
 use bytes (); # only for bytes::length
-use Plack::Util;
 use PublicInbox::Config;
 use PublicInbox::Hval;
 use URI::Escape qw(uri_unescape);
@@ -23,6 +22,7 @@ use PublicInbox::MID qw(mid_escape);
 require PublicInbox::Git;
 use PublicInbox::GitHTTPBackend;
 use PublicInbox::UserContent;
+use PublicInbox::WwwStatic qw(r path_info_raw);
 
 # TODO: consider a routing tree now that we have more endpoints:
 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
@@ -43,17 +43,6 @@ sub run {
        PublicInbox::WWW->new->call($req->env);
 }
 
-my %path_re_cache;
-
-sub path_re ($) {
-       my $sn = $_[0]->{SCRIPT_NAME};
-       $path_re_cache{$sn} ||= do {
-               $sn = '/'.$sn unless index($sn, '/') == 0;
-               $sn =~ s!/\z!!;
-               qr!\A(?:https?://[^/]+)?\Q$sn\E(/[^\?\#]+)!;
-       };
-}
-
 sub call {
        my ($self, $env) = @_;
        my $ctx = { env => $env, www => $self };
@@ -68,9 +57,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,8 +70,8 @@ sub call {
                        return invalid_inbox($ctx, $1) || mbox_results($ctx);
                }
        }
-       elsif ($method !~ /\AGET|HEAD\z/) {
-               return r(405, 'Method Not Allowed');
+       elsif ($method !~ /\A(?:GET|HEAD)\z/) {
+               return r(405);
        }
 
        # top-level indices and feeds
@@ -177,12 +164,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};
@@ -472,6 +456,7 @@ sub cgit {
                        require PublicInbox::Cgit;
                        PublicInbox::Cgit->new($pi_config);
                } else {
+                       require Plack::Util;
                        Plack::Util::inline_object(call => sub { r404() });
                }
        }