Canonicalize the url becuse we're building it from the always-undecoded REQUEST_URI...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 35ab188..c8e9c7c 100644 (file)
@@ -11,6 +11,9 @@ use HTTP::Body;
 use HTTP::Headers;
 use URI::QueryParam;
 use Moose::Util::TypeConstraints;
+use Plack::Loader;
+use Plack::Middleware::Conditional;
+use Plack::Middleware::ReverseProxy;
 
 use namespace::clean -except => 'meta';
 
@@ -432,7 +435,7 @@ sub prepare_connection {
     $request->protocol( $env->{SERVER_PROTOCOL} );
     $request->remote_user( $env->{REMOTE_USER} );
     $request->method( $env->{REQUEST_METHOD} );
-    $request->secure( $env->{'psgi.url_scheme'} eq 'https' );
+    $request->secure( $env->{'psgi.url_scheme'} eq 'https' ? 1 : 0 );
 
     return;
 }
@@ -518,7 +521,7 @@ sub prepare_path {
     # set the request URI
     my $req_uri = $env->{REQUEST_URI};
     $req_uri =~ s/\?.*$//;
-    my $path = $self->unescape_uri($req_uri);
+    my $path = $req_uri;
     $path =~ s{^/+}{};
 
     # Using URI directly is way too slow, so we construct the URLs manually
@@ -531,14 +534,10 @@ sub prepare_path {
         $host .= ":$port";
     }
 
-    # Escape the path
-    $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
-    $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
-
     my $query = $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : '';
     my $uri   = $scheme . '://' . $host . '/' . $path . $query;
 
-    $ctx->request->uri( bless \$uri, $uri_class );
+    $ctx->request->uri( (bless \$uri, $uri_class)->canonical );
 
     # set the base URI
     # base must end in a slash
@@ -751,9 +750,15 @@ Start the engine. Implemented by the various engine classes.
 =cut
 
 sub run {
-    my ($self, $app) = @_;
+    my ($self, $app, @args) = @_;
+    # FIXME - Do something sensible with the options we're passed
+    $self->_run_psgi_app($self->_build_psgi_app($app, @args), @args);
+}
+
+sub _build_psgi_app {
+    my ($self, $app, @args) = @_;
 
-    return sub {
+    my $psgi_app = sub {
         my ($env) = @_;
 
         return sub {
@@ -762,6 +767,24 @@ sub run {
             $app->handle_request(env => $env);
         };
     };
+
+    $psgi_app = Plack::Middleware::Conditional->wrap(
+        $psgi_app,
+        condition => sub {
+            my ($env) = @_;
+            return if $app->config->{ignore_frontend_proxy};
+            return $env->{REMOTE_ADDR} eq '127.0.0.1' || $app->config->{using_frontend_proxy};
+        },
+        builder   => sub { Plack::Middleware::ReverseProxy->wrap($_[0]) },
+    );
+
+    return $psgi_app;
+}
+
+sub _run_psgi_app {
+    my ($self, $psgi_app, @args) = @_;
+    # FIXME - Need to be able to specify engine and pass options..
+    Plack::Loader->auto(port => $args[0])->run($psgi_app);
 }
 
 =head2 $self->write($c, $buffer)