From 5973481e5526f12a3e9231a1a2d759033f3ff9e8 Mon Sep 17 00:00:00 2001 From: Russell Jenkins Date: Thu, 22 May 2014 22:34:42 +1000 Subject: [PATCH] Make behind_proxy a global var Conditionally apply BehindProxy middleware if behind_proxy is set. Making the "behind_proxy" option a global trigger that updates the Dancer 2::Runner object when set behind_proxy => 1; is run so the reverse proxy middleware can be conditionally wrapped around applications psgi coderef. We now 'use' the middleware directly rather than letting Placl::Builder load it for us. May allow earlier version of Placl to be used as a dependency. --- lib/Dancer2/Core/App.pm | 3 --- lib/Dancer2/Core/Role/ConfigReader.pm | 5 +++++ lib/Dancer2/Core/Runner.pm | 15 +++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/Dancer2/Core/App.pm b/lib/Dancer2/Core/App.pm index 21da2c697..2443694e1 100644 --- a/lib/Dancer2/Core/App.pm +++ b/lib/Dancer2/Core/App.pm @@ -413,9 +413,6 @@ sub _init_for_context { return if !defined $self->context; return if !defined $self->context->request; - - $self->context->request->is_behind_proxy(1) - if $self->setting('behind_proxy'); } sub supported_hooks { diff --git a/lib/Dancer2/Core/Role/ConfigReader.pm b/lib/Dancer2/Core/Role/ConfigReader.pm index ce2b96c44..ba8f1c737 100644 --- a/lib/Dancer2/Core/Role/ConfigReader.pm +++ b/lib/Dancer2/Core/Role/ConfigReader.pm @@ -89,6 +89,11 @@ has global_triggers => ( my ( $self, $handler ) = @_; Dancer2->runner->config->{'apphandler'} = $handler; }, + + behind_proxy => sub { + my ( $self, $flag ) = @_; + Dancer2->runner->config->{'behind_proxy'} = $flag; + }, } }, ); diff --git a/lib/Dancer2/Core/Runner.pm b/lib/Dancer2/Core/Runner.pm index 4ff0a4c73..2a36ae4fd 100644 --- a/lib/Dancer2/Core/Runner.pm +++ b/lib/Dancer2/Core/Runner.pm @@ -6,7 +6,10 @@ use Dancer2::Core::MIME; use Dancer2::Core::Types; use Dancer2::Core::Dispatcher; use HTTP::Server::PSGI; -use Plack::Builder qw(); + +use Plack::Middleware::Head; +use Plack::Middleware::Conditional; +use Dancer2::Middleware::BehindProxy; with 'Dancer2::Core::Role::ConfigReader'; @@ -240,9 +243,13 @@ sub psgi_app { return $response; }; - my $builder = Plack::Builder->new; - $builder->add_middleware('Head'); - return $builder->wrap($psgi); + $psgi = Plack::Middleware::Conditional->wrap( + $psgi, + builder => sub { Dancer2::Middleware::BehindProxy->wrap($_[0]) }, + condition => sub { $self->config->{'behind_proxy'} }, + ); + $psgi = Plack::Middleware::Head->wrap($psgi); + return $psgi; } sub print_banner {