Skip to content

Commit 9112e2f

Browse files
committed
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.
1 parent 32d772f commit 9112e2f

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

lib/Dancer2/Core/App.pm

-11
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ has context => (
254254
sub setup_context {
255255
my ( $self, $ctx ) = @_;
256256

257-
$self->_init_for_context($ctx);
258-
259257
for my $type ( @{ $self->supported_engines } ) {
260258
my $attr = "${type}_engine";
261259
my $engine = $self->$attr or next;
@@ -419,15 +417,6 @@ sub _init_hooks {
419417
);
420418
}
421419

422-
sub _init_for_context {
423-
my ($self) = @_;
424-
425-
$self->has_request or return;
426-
427-
$self->request->is_behind_proxy(1)
428-
if $self->setting('behind_proxy');
429-
}
430-
431420
sub supported_hooks {
432421
qw/
433422
core.app.before_request

lib/Dancer2/Core/Dispatcher.pm

+3-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ sub build_request {
116116
# If we have an app, send the serialization engine
117117
my $engine = $app->engine('serializer');
118118
my $request = Dancer2::Core::Request->new(
119-
env => $env,
120-
( serializer => $engine ) x!! $engine,
119+
env => $env,
120+
is_behind_proxy => Dancer2->runner->config->{'behind_proxy'} || 0,
121+
( serializer => $engine ) x!! $engine,
121122
);
122123

123124
# Log deserialization errors

lib/Dancer2/Core/Request.pm

+2-1
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@ has body_is_parsed => (
369369
);
370370

371371
has is_behind_proxy => (
372-
is => 'rw',
372+
is => 'ro',
373373
isa => Bool,
374+
lazy => 1,
374375
default => sub {0},
375376
);
376377

lib/Dancer2/Core/Role/ConfigReader.pm

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ has global_triggers => (
8989
my ( $self, $handler ) = @_;
9090
Dancer2->runner->config->{'apphandler'} = $handler;
9191
},
92+
93+
behind_proxy => sub {
94+
my ( $self, $flag ) = @_;
95+
Dancer2->runner->config->{'behind_proxy'} = $flag;
96+
},
9297
} },
9398
);
9499

0 commit comments

Comments
 (0)