Skip to content

Commit

Permalink
added original_remote_address attribute to Mojo::Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 27, 2014
1 parent 41abd0f commit 9614de4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

5.34 2014-08-26
5.34 2014-08-28
- Added original_remote_address attribute to Mojo::Transaction.

5.33 2014-08-24
- Improved Mojo::Date to be able to handle higher precision times.
Expand Down
21 changes: 14 additions & 7 deletions lib/Mojo/Transaction.pm
Expand Up @@ -5,7 +5,8 @@ use Carp 'croak';
use Mojo::Message::Request;
use Mojo::Message::Response;

has [qw(kept_alive local_address local_port remote_port)];
has [
qw(kept_alive local_address local_port original_remote_address remote_port)];
has req => sub { Mojo::Message::Request->new };
has res => sub { Mojo::Message::Response->new };

Expand Down Expand Up @@ -47,10 +48,7 @@ sub remote_address {
my $self = shift;

# New address
if (@_) {
$self->{remote_address} = shift;
return $self;
}
return $self->original_remote_address(@_) if @_;

# Reverse proxy
if ($self->req->reverse_proxy) {
Expand All @@ -59,7 +57,7 @@ sub remote_address {
$forwarded =~ /([^,\s]+)$/ and return $self->{forwarded_for} = $1;
}

return $self->{remote_address};
return $self->original_remote_address;
}

sub resume { shift->_state(qw(write resume)) }
Expand Down Expand Up @@ -155,6 +153,13 @@ Local interface address.
Local interface port.
=head2 original_remote_address
my $address = $tx->original_remote_address;
$tx = $tx->original_remote_address('127.0.0.1');
Remote interface address.
=head2 remote_port
my $port = $tx->remote_port;
Expand Down Expand Up @@ -246,7 +251,9 @@ Resume transaction.
my $address = $tx->remote_address;
$tx = $tx->remote_address('127.0.0.1');
Remote interface address.
Same as L</"original_remote_address"> or the last value of the
C<X-Forwarded-For> header if L<Mojo::Message::Request/"reverse_proxy"> is
true.
=head2 server_close
Expand Down
9 changes: 5 additions & 4 deletions t/mojo/daemon.t
Expand Up @@ -194,12 +194,13 @@ is $tx->res->code, 200, 'right status';
is $tx->res->body, $result, 'right content';
ok $tx->local_address, 'has local address';
ok $tx->local_port > 0, 'has local port';
ok $tx->remote_address, 'has local address';
ok $tx->remote_port > 0, 'has local port';
ok $tx->original_remote_address, 'has original remote address';
ok $tx->remote_address, 'has remote address';
ok $tx->remote_port > 0, 'has remote port';
ok $local_address, 'has local address';
ok $local_port > 0, 'has local port';
ok $remote_address, 'has local address';
ok $remote_port > 0, 'has local port';
ok $remote_address, 'has remote address';
ok $remote_port > 0, 'has remote port';

# Pipelined
my $daemon
Expand Down
5 changes: 3 additions & 2 deletions t/mojo/user_agent_online.t
Expand Up @@ -234,8 +234,9 @@ is $tx->res->code, 200, 'right status';
ok $tx->kept_alive, 'connection was kept alive';
ok $tx->local_address, 'has local address';
ok $tx->local_port > 0, 'has local port';
ok $tx->remote_address, 'has local address';
ok $tx->remote_port > 0, 'has local port';
ok $tx->original_remote_address, 'has original remote address';
ok $tx->remote_address, 'has remote address';
ok $tx->remote_port > 0, 'has remote port';

# Simple request with redirect
$ua->max_redirects(3);
Expand Down
8 changes: 5 additions & 3 deletions t/mojolicious/lite_app.t
Expand Up @@ -203,8 +203,9 @@ get '/root' => sub { shift->render(text => 'root fallback!') };
get '/template.txt' => {template => 'template', format => 'txt'};

get ':number' => [number => qr/0/] => sub {
my $c = shift;
my $url = $c->req->url->to_abs;
my $c = shift;
my $url = $c->req->url->to_abs;
$c->res->headers->header('X-Original' => $c->tx->original_remote_address);
my $address = $c->tx->remote_address;
my $num = $c->param('number');
$c->render(text => "$url-$address-$num");
Expand Down Expand Up @@ -741,7 +742,8 @@ $t->get_ok('/.html')->status_is(200)
local $ENV{MOJO_REVERSE_PROXY} = 1;
$t->ua->server->restart;
$t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
->status_is(200)->content_like(qr!http://localhost:\d+/0-192\.0\.2\.1-0$!);
->status_is(200)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
->content_like(qr!http://localhost:\d+/0-192\.0\.2\.1-0$!);
}

# Reverse proxy with "X-Forwarded-Proto"
Expand Down

0 comments on commit 9614de4

Please sign in to comment.