Skip to content

Commit

Permalink
Make behind_proxy a global var
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
veryrusty committed May 22, 2014
1 parent fea1cd0 commit 5973481
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 0 additions & 3 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions lib/Dancer2/Core/Role/ConfigReader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
} },
);

Expand Down
15 changes: 11 additions & 4 deletions lib/Dancer2/Core/Runner.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5973481

Please sign in to comment.