Skip to content

Commit

Permalink
Allow the Dispatcher to set is_behind_proxy for Core::Request:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xsawyerx committed Jul 23, 2014
1 parent 32d772f commit 9112e2f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
11 changes: 0 additions & 11 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/Dancer2/Core/Dispatcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/Dancer2/Core/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ has body_is_parsed => (
);

has is_behind_proxy => (
is => 'rw',
is => 'ro',
isa => Bool,
lazy => 1,
default => sub {0},
);

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

0 comments on commit 9112e2f

Please sign in to comment.