Skip to content

Commit

Permalink
Add remote_host() and address() to Request.
Browse files Browse the repository at this point in the history
They prefer forwarded host and address information, when present. This should
allow the proper remote host name to be included in the registration
administration email when PGXN Manager is running behind a rerverse proxy.
Closes #39.
  • Loading branch information
theory committed Jun 5, 2013
1 parent 4d4651d commit 7426502
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Revision history for Perl extension PGXN::Manager
properly.
- Updated the HOWTO to properly order things in the `Makefile` and to
include a `dist` target.
- Added `remote_host()` and `address()` to PGXN::API::Request to return
the forwarded host name and address, if present. This should allow
the proper host information to be included in the registration admin
email (issue #38).

0.14.1 2012-01-11T18:29:26Z
- Greatly improved the `check_mirrors` utility, making it work with the
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ way to separate these is to set up two reverse proxy servers: One to serve
location / {
proxy_pass http://127.0.0.1:7496/pub/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-HTTPS "";
proxy_set_header X-Forwaded-Proto http;
proxy_set_header X-Forwarded-Port 80;
Expand All @@ -273,7 +274,8 @@ way to separate these is to set up two reverse proxy servers: One to serve
location / {
proxy_pass http://127.0.0.1:7496/auth/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-HTTPS ON;
proxy_set_header X-Forwaded-Proto https;
proxy_set_header X-Forwarded-Port 443;
Expand Down
21 changes: 21 additions & 0 deletions lib/PGXN/Manager/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ sub is_xhr {
shift->env->{HTTP_X_REQUESTED_WITH} eq 'XMLHttpRequest';
}

sub address {
my $env = $_[0]->env;
return $env->{X_FORWARDED_FOR} || $env->{REMOTE_ADDR};
}

sub remote_host {
my $env = $_[0]->env;
return $env->{X_FORWARDED_HOST} || $env->{REMOTE_HOST};
}

# Eliminates use of env->{'plack.request.query'}?
sub query_parameters {
my $self = shift;
Expand Down Expand Up @@ -209,6 +219,17 @@ Returns true if the request is an C<XMLHttpRequest> request and false if not.
This is specific to L<jQuery|http://jquery.org> sending the
C<X-Requested-With> header.
=head3 C<address>
Returns the (possibly forwarded) IP address of the client (C<X_FORWARDED_FOR>
or C<REMOTE_ADDR>).
=head3 C<remote_host>
Returns the (possibly forwarded) remote host (C<X_FORWARDED_HOST> or
C<REMOTE_HOST>) of the client. It may be empty, in which case you have to get
the IP address using C<address> method and resolve on your own.
=head3 C<query_parameters>
=head3 C<body_parameters>
Expand Down
1 change: 1 addition & 0 deletions t/pod-spelling.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ reindex
Reindexes
reindexes
JSON
IP
10 changes: 9 additions & 1 deletion t/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use 5.10.0;
use utf8;
use Test::More tests => 50;
use Test::More tests => 54;
#use Test::More 'no_plan';
use HTTP::Request::Common;
use HTTP::Message::PSGI;
Expand Down Expand Up @@ -175,3 +175,11 @@ is $req->param('q'), "メインページ", 'q param should be decoded';
is_deeply [$req->parameters->get_all('q')], ['テスト', 'メインページ'],
'All q values should be decoded';

##############################################################################
# Test remote_host() and address().
is $req->remote_host, 'localhost', 'remote_host should be "localhost"';
is $req->address, '127.0.0.1', 'remote_host should be "127.0.0.1"';
$req->env->{X_FORWARDED_HOST} = 'foo';
is $req->remote_host, 'foo', 'remote_host should prefer X-Forwarded-host';
$req->env->{X_FORWARDED_FOR} = '192.168.0.1';
is $req->address, '192.168.0.1', 'remote_host should prefer X-Forwarded-For';

0 comments on commit 7426502

Please sign in to comment.