From 9112e2f88283ae21b5bfd8e47ed827ae711924d5 Mon Sep 17 00:00:00 2001 From: Sawyer X Date: Thu, 29 May 2014 17:53:14 +0300 Subject: [PATCH] Allow the Dispatcher to set is_behind_proxy for Core::Request: Currently, Core::Request's is_behind_proxy is set by Core::App whenever a new Core::Context is provided (using a trigger, no less). There are various problems with this: * The App shouldn't care about is_behind_proxy * The attribute shouldn't be set every time, since it's global * Core::Context is unrelated to this Instead, we made behind_proxy a global variable (as done by GH #590) with its own trigger. Then it is set on instantiation by Core::Dispatcher. Then we can remove Core::App's _init_for_context method completely. GH #590 approaches this issue in an entirely different and better way, by suggesting we simply correct the environment before all of this happens using a middleware. For now we're just doing enough to untangle Core::Context more. --- lib/Dancer2/Core/App.pm | 11 ----------- lib/Dancer2/Core/Dispatcher.pm | 5 +++-- lib/Dancer2/Core/Request.pm | 3 ++- lib/Dancer2/Core/Role/ConfigReader.pm | 5 +++++ 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/Dancer2/Core/App.pm b/lib/Dancer2/Core/App.pm index f05b259ca..8ef750ee2 100644 --- a/lib/Dancer2/Core/App.pm +++ b/lib/Dancer2/Core/App.pm @@ -254,8 +254,6 @@ has context => ( sub setup_context { my ( $self, $ctx ) = @_; - $self->_init_for_context($ctx); - for my $type ( @{ $self->supported_engines } ) { my $attr = "${type}_engine"; my $engine = $self->$attr or next; @@ -419,15 +417,6 @@ sub _init_hooks { ); } -sub _init_for_context { - my ($self) = @_; - - $self->has_request or return; - - $self->request->is_behind_proxy(1) - if $self->setting('behind_proxy'); -} - sub supported_hooks { qw/ core.app.before_request diff --git a/lib/Dancer2/Core/Dispatcher.pm b/lib/Dancer2/Core/Dispatcher.pm index 957144f7d..faf3713c2 100644 --- a/lib/Dancer2/Core/Dispatcher.pm +++ b/lib/Dancer2/Core/Dispatcher.pm @@ -116,8 +116,9 @@ sub build_request { # If we have an app, send the serialization engine my $engine = $app->engine('serializer'); my $request = Dancer2::Core::Request->new( - env => $env, - ( serializer => $engine ) x!! $engine, + env => $env, + is_behind_proxy => Dancer2->runner->config->{'behind_proxy'} || 0, + ( serializer => $engine ) x!! $engine, ); # Log deserialization errors diff --git a/lib/Dancer2/Core/Request.pm b/lib/Dancer2/Core/Request.pm index 51d2847d4..78c5f55cb 100644 --- a/lib/Dancer2/Core/Request.pm +++ b/lib/Dancer2/Core/Request.pm @@ -369,8 +369,9 @@ has body_is_parsed => ( ); has is_behind_proxy => ( - is => 'rw', + is => 'ro', isa => Bool, + lazy => 1, default => sub {0}, ); 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; + }, } }, );