-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Rebased) Pr/behind proxy middleware #1660
Open
xsawyerx
wants to merge
5
commits into
main
Choose a base branch
from
pr/behind_proxy_middleware-rebased
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d172de8
Plack middleware for request header munging when behind a reverse proxy
veryrusty 5b495cd
Apply BehindProxy middleware when behind_proxy is set
veryrusty 047a1c8
Remove behind_proxy handling from D2::Core::Request
veryrusty 2767a9c
Add ReverseProxy and ReverseProxyPath middlewares as deps
veryrusty 2faca00
Update test and remove lingering keys in request
xsawyerx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package Dancer2::Middleware::BehindProxy; | ||
# ABSTRACT: Support Dancer2 apps when operating behing a reverse proxy | ||
|
||
use warnings; | ||
use strict; | ||
|
||
use parent 'Plack::Middleware'; | ||
use Plack::Middleware::ReverseProxy; | ||
use Plack::Middleware::ReverseProxyPath; | ||
|
||
sub call { | ||
my($self, $env) = @_; | ||
|
||
# Plack::Middleware::ReverseProxy only supports | ||
# HTTP_X_FORWARDED_PROTO whereas Dancer2 also supports | ||
# HTTP_X_FORWARDED_PROTOCOL and HTTP_FORWARDED_PROTO | ||
for my $header (qw/HTTP_X_FORWARDED_PROTOCOL HTTP_FORWARDED_PROTO/) { | ||
if ( ! $env->{HTTP_X_FORWARDED_PROTO} | ||
&& $env->{$header} ) | ||
{ | ||
$env->{HTTP_X_FORWARDED_PROTO} = $env->{$header}; | ||
last; | ||
} | ||
} | ||
|
||
# Pr#503 added support for HTTP_X_FORWARDED_HOST containing multiple | ||
# values. Plack::Middleware::ReverseProxy takes the last (most recent) | ||
# whereas that #503 takes the first. | ||
if ( $env->{HTTP_X_FORWARDED_HOST} ) { | ||
my @hosts = split /\s*,\s*/, $env->{HTTP_X_FORWARDED_HOST}, 2; | ||
$env->{HTTP_X_FORWARDED_HOST} = $hosts[0]; | ||
} | ||
|
||
# Plack::Middleware::ReverseProxyPath uses X-Forwarded-Script-Name | ||
# whereas Dancer previously supported HTTP_REQUEST_BASE | ||
if ( ! $env->{HTTP_X_FORWARDED_SCRIPT_NAME} | ||
&& $env->{HTTP_REQUEST_BASE} ) | ||
{ | ||
$env->{HTTP_X_FORWARDED_SCRIPT_NAME} = $env->{HTTP_REQUEST_BASE}; | ||
} | ||
|
||
# Wrap in reverse proxy middleware and call the wrapped app | ||
my $app = Plack::Middleware::ReverseProxyPath->wrap($self->app); | ||
$app = Plack::Middleware::ReverseProxy->wrap($app); | ||
return $app->($env); | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
|
||
=head1 DESCRIPTION | ||
|
||
Modifies request headers supported by L<Dancer2> altered by reverse proxies before | ||
wraping the request in the commonly used reverse proxy PSGI middlewares; | ||
L<Plack::Middleware::ReverseProxy> and L<Plack::Middleware::ReverseProxyPath>. | ||
|
||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: behind