Start making the path handling like it is in trunk. Merge extra tests, add using...
Tomas Doran [Mon, 24 May 2010 15:44:08 +0000 (15:44 +0000)]
lib/Catalyst/Engine.pm
t/aggregate/unit_core_engine-prepare_path.t

index 8092888..17ec97b 100644 (file)
@@ -523,10 +523,19 @@ sub prepare_path {
     my $base_path = $env->{SCRIPT_NAME} || "/";
 
     # set the request URI
-    my $req_uri = $env->{REQUEST_URI};
-    $req_uri =~ s/\?.*$//;
-    my $path = $req_uri;
-    $path =~ s{^/+}{};
+    my $path;
+    if (!$ctx->config->{use_request_uri_for_path}) {
+        $path = $base_path . $env->{PATH_INFO};
+        $path =~ s{^/+}{};
+        $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
+        $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
+    }
+    else {
+        my $req_uri = $env->{REQUEST_URI};
+        $req_uri =~ s/\?.*$//;
+        $path = $req_uri;
+        $path =~ s{^/+}{};
+    }
 
     # Using URI directly is way too slow, so we construct the URLs manually
     my $uri_class = "URI::$scheme";
index 06f4a50..1fa9750 100644 (file)
@@ -8,7 +8,7 @@ use Catalyst::Engine;
 
 # mod_rewrite to app root for non / based app
 {
-    my $r = get_req (
+    my $r = get_req (0,
         SCRIPT_NAME => '/comics/dispatch.cgi',
         REQUEST_URI => '/comics/',
     );
@@ -18,7 +18,7 @@ use Catalyst::Engine;
 
 # mod_rewrite to sub path under app root for non / based app
 {
-    my $r = get_req (
+    my $r = get_req (0,
         PATH_INFO  => '/foo/bar.gif',
         SCRIPT_NAME => '/comics/dispatch.cgi',
         REQUEST_URI => '/comics/foo/bar.gif',
@@ -29,7 +29,7 @@ use Catalyst::Engine;
 
 # Standard CGI hit for non / based app
 {
-    my $r = get_req (
+    my $r = get_req (0,
         PATH_INFO => '/static/css/blueprint/screen.css',
         SCRIPT_NAME => '/~bobtfish/Gitalist/script/gitalist.cgi',
         REQUEST_URI => '/~bobtfish/Gitalist/script/gitalist.cgi/static/css/blueprint/screen.css',
@@ -39,7 +39,7 @@ use Catalyst::Engine;
 }
 # / %2F %252F escaping case.
 {
-    my $r = get_req (
+    my $r = get_req (1,
         PATH_INFO => '/%2F/%2F',
         SCRIPT_NAME => '/~bobtfish/Gitalist/script/gitalist.cgi',
         REQUEST_URI => '/~bobtfish/Gitalist/script/gitalist.cgi/%252F/%252F',
@@ -51,7 +51,7 @@ use Catalyst::Engine;
 # Using rewrite rules to ask for a sub-path in your app.
 # E.g. RewriteRule ^(.*)$ /path/to/fastcgi/domainprofi.fcgi/iframeredirect$1 [L,NS]
 {
-    my $r = get_req (
+    my $r = get_req (0,
         PATH_INFO => '/iframeredirect/info',
         SCRIPT_NAME => '',
         REQUEST_URI => '/info',
@@ -60,6 +60,55 @@ use Catalyst::Engine;
     is ''.$r->base, 'http://www.foo.com/';
 }
 
+# nginx example from espent with path /"foo"
+{
+    my $r = get_req (0,
+        PATH_INFO => '"foo"',
+        SCRIPT_NAME => '/',
+        REQUEST_URI => '/%22foo%22',
+    );
+    is ''.$r->path, '%22foo%22';
+    is ''.$r->uri, 'http://www.foo.com/%22foo%22';
+    is ''.$r->base, 'http://www.foo.com/';
+}
+
+# nginx example from espent with path /"foo" and the app based at /oslobilder
+{
+    my $r = get_req (1,
+        PATH_INFO => 'oslobilder/"foo"',
+        SCRIPT_NAME => '/oslobilder/',
+        REQUEST_URI => '/oslobilder/%22foo%22',
+    );
+    is ''.$r->path, '%22foo%22', 'path correct';
+    is ''.$r->uri, 'http://www.foo.com/oslobilder/%22foo%22', 'uri correct';
+    is ''.$r->base, 'http://www.foo.com/oslobilder/', 'base correct';
+}
+{
+    local $TODO = 'Another mod_rewrite case';
+    my $r = get_req (0,
+        PATH_INFO => '/auth/login',
+        SCRIPT_NAME => '/tx',
+        REQUEST_URI => '/login',
+    );
+    is ''.$r->path, 'auth/login', 'path correct';
+    is ''.$r->uri, 'http://www.foo.com/tx/auth/login', 'uri correct';
+    is ''.$r->base, 'http://www.foo.com/tx/', 'base correct';
+}
+
+# test req->base and c->uri_for work correctly after an internally redirected request
+# (i.e. REDIRECT_URL set) when the PATH_INFO contains a regex
+{
+    my $path = '/engine/request/uri/Rx(here)';
+    my $r = get_req (0,
+        SCRIPT_NAME => '/',
+        PATH_INFO => $path,
+        REQUEST_URI => $path,
+        REDIRECT_URL => $path,
+    );
+
+    is $r->path, 'engine/request/uri/Rx(here)', 'URI contains correct path';
+    is $r->base, 'http://www.foo.com/', 'Base is correct';
+}
 
 
 # FIXME - Test proxy logic
@@ -69,6 +118,8 @@ use Catalyst::Engine;
 #       - Test scheme (secure request on port 80)
 
 sub get_req {
+    my $use_request_uri_for_path = shift;
+
     my %template = (
         HTTP_HOST => 'www.foo.com',
         PATH_INFO => '/',
@@ -78,6 +129,9 @@ sub get_req {
         env => { %template, @_ },
     );
     my $i = TestApp->new;
+    $i->setup_finished(0);
+    $i->config(use_request_uri_for_path => $use_request_uri_for_path);
+    $i->setup_finished(1);
     $engine->prepare_path($i);
     return $i->req;
 }