X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=e6c5d6256f21a81548b07958a952e3c3bb25c31d;hb=4b0f97fc6eb94b7d347403cecbf9a71ec5322e37;hp=35ab188c9bff35acf05fa07ebd3edadc3b634f90;hpb=eebe046fdd1c0b74d65920dd6df6977f470f64a1;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 35ab188..e6c5d62 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -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'; @@ -301,7 +304,8 @@ sub finalize_error { - # Trick IE + # Trick IE. Old versions of IE would display their own error page instead + # of ours if we'd give it less than 512 bytes. $c->res->{body} .= ( ' ' x 512 ); # Return 500 @@ -432,7 +436,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 +522,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 +535,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 @@ -744,16 +744,35 @@ header. The amount of input data that has already been read. -=head2 $self->run($c) +=head2 $self->run($app, $server) -Start the engine. Implemented by the various engine classes. +Start the engine. Builds a PSGI application and calls the +run method on the server passed in.. =cut sub run { - my ($self, $app) = @_; + my ($self, $app, @args) = @_; + my $server = pop @args if blessed $args[-1]; + $server ||= Plack::Loader->auto(); # We're not being called from a script, + # so auto detect what backend to run on. + # This does *NOT* cover mod_perl. + # FIXME - Do something sensible with the options we're passed + my $psgi = $self->build_psgi_app($app, @args); + $server->run($psgi); +} + +=head2 build_psgi_app ($app, @args) + +Builds and returns a PSGI application closure, wrapping it in the reverse proxy +middleware if the using_frontend_proxy config setting is set. - return sub { +=cut + +sub build_psgi_app { + my ($self, $app, @args) = @_; + + my $psgi_app = sub { my ($env) = @_; return sub { @@ -762,6 +781,18 @@ 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; } =head2 $self->write($c, $buffer)