tokuhirom's blog

PATH_INFO and Router::Simple

Dear Miyagawa san,

Plack::Builder's mount function makes empty string PATH_INFO on following case.

use v5.14.0;
use Plack::Builder;
use Plack::Util;
use Test::More;
use Plack::Test;
use Plack;

note "Plack: $Plack::VERSION";

my $app = builder {
        mount '/app/' => sub {
        my $env = shift;
        [200, [], [$env->{PATH_INFO}]];
    };
};

test_psgi app => $app,
  client => sub {
    my $cb  = shift;
    {
        my $req = HTTP::Request->new( GET => "http://localhost/app" );
        my $res = $cb->($req);
        is $res->content, '';
    }
    {
        my $req = HTTP::Request->new( GET => "http://localhost/app/" );
        my $res = $cb->($req);
        is $res->content, '/';
    }
  };

done_testing;

Result is here

# Plack: 1.0004
ok 1
ok 2
1..2

So, should path router support empty string to replace '/'? i.e. should I apply following patch for Router::Simple?

diff --git a/lib/Router/Simple.pm b/lib/Router/Simple.pm
index de1651a..8dd260a 100644
--- a/lib/Router/Simple.pm
+++ b/lib/Router/Simple.pm
@@ -32,7 +32,13 @@ sub submapper {
 sub _match {
     my ($self, $env) = @_;
 
-    $env = +{ PATH_INFO => $env } unless ref $env;
+    if (ref $env) {
+        if ($env->{PATH_INFO} eq '') {
+            $env->{PATH_INFO} = '/';
+        }
+    } else {
+        $env = +{ PATH_INFO => $env }
+    }
 
     for my $route (@{$self->{routes}}) {
         my $match = $route->match($env);

Created: 2012-10-05T04:23:54Z
Updated: 2012-10-05T04:23:54Z
comments powered by Disqus