Skip to content

Commit

Permalink
added reverse_proxy attributes to Mojo::Server::Daemon and Mojo::Mess…
Browse files Browse the repository at this point in the history
…age::Request
  • Loading branch information
kraih committed Apr 19, 2014
1 parent 8076a27 commit d97370f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 17 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

4.94 2014-04-19
4.94 2014-04-20
- Added reverse_proxy attribute to Mojo::Server::Daemon.
- Added reverse_proxy attribute to Mojo::Message::Request.
- Added prefork and upgrade_timeout attributes to Mojo::Server::Hypnotoad.
- Added configure method to Mojo::Server::Hypnotoad.
- Relaxed name handling in Mojo::Headers a little.
Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/Message/Request.pm
Expand Up @@ -8,6 +8,7 @@ use Mojo::URL;
has env => sub { {} };
has method => 'GET';
has url => sub { Mojo::URL->new };
has 'reverse_proxy';

my $START_LINE_RE = qr/
^
Expand Down Expand Up @@ -170,7 +171,7 @@ sub parse {

# "X-Forwarded-HTTPS"
$base->scheme('https')
if $ENV{MOJO_REVERSE_PROXY} && $headers->header('X-Forwarded-HTTPS');
if $self->reverse_proxy && $headers->header('X-Forwarded-HTTPS');

return $self;
}
Expand Down Expand Up @@ -327,6 +328,13 @@ HTTP request URL, defaults to a L<Mojo::URL> object.
say $req->url->to_abs->host;
say $req->url->to_abs->path;
=head2 reverse_proxy
my $bool = $req->reverse_proxy;
$req = $req->reverse_proxy($bool);
Request has been performed through a reverse proxy.
=head1 METHODS
L<Mojo::Message::Request> inherits all methods from L<Mojo::Message> and
Expand Down
14 changes: 12 additions & 2 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -12,8 +12,9 @@ has [qw(backlog silent)];
has inactivity_timeout => sub { $ENV{MOJO_INACTIVITY_TIMEOUT} // 15 };
has ioloop => sub { Mojo::IOLoop->singleton };
has listen => sub { [split ',', $ENV{MOJO_LISTEN} || 'http://*:3000'] };
has max_clients => 1000;
has max_requests => 25;
has max_clients => 1000;
has max_requests => 25;
has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };

sub DESTROY {
my $self = shift;
Expand Down Expand Up @@ -67,6 +68,7 @@ sub _build_tx {
my $handle = $self->ioloop->stream($id)->handle;
$tx->local_address($handle->sockhost)->local_port($handle->sockport);
$tx->remote_address($handle->peerhost)->remote_port($handle->peerport);
$tx->req->reverse_proxy(1) if $self->reverse_proxy;
$tx->req->url->base->scheme('https') if $c->{tls};

# Handle upgrades and requests
Expand Down Expand Up @@ -409,6 +411,14 @@ Maximum number of concurrent client connections, defaults to C<1000>.
Maximum number of keep-alive requests per connection, defaults to C<25>.
=head2 reverse_proxy
my $bool = $daemon->reverse_proxy;
$daemon = $daemon->reverse_proxy($bool);
This server operates behind a reverse proxy, defaults to the value of the
C<MOJO_REVERSE_PROXY> environment variable.
=head2 silent
my $bool = $daemon->silent;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -23,7 +23,7 @@ sub configure {
$self->upgrade_timeout($c->{upgrade_timeout}) if $c->{upgrade_timeout};

# Prefork settings
$ENV{MOJO_REVERSE_PROXY} = $c->{proxy} if defined $c->{proxy};
$prefork->reverse_proxy($c->{proxy}) if defined $c->{proxy};
$prefork->max_clients($c->{clients}) if $c->{clients};
$prefork->max_requests($c->{keep_alive_requests})
if $c->{keep_alive_requests};
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction.pm
Expand Up @@ -60,7 +60,7 @@ sub remote_address {
}

# Reverse proxy
if ($ENV{MOJO_REVERSE_PROXY}) {
if ($self->req->reverse_proxy) {
return $self->{forwarded_for} if $self->{forwarded_for};
my $forwarded = $self->req->headers->header('X-Forwarded-For') // '';
$forwarded =~ /([^,\s]+)$/ and return $self->{forwarded_for} = $1;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Command/daemon.pm
Expand Up @@ -17,9 +17,9 @@ sub run {
'g|group=s' => sub { $daemon->group($_[1]) },
'i|inactivity=i' => sub { $daemon->inactivity_timeout($_[1]) },
'l|listen=s' => \my @listen,
'p|proxy' => sub { $ENV{MOJO_REVERSE_PROXY} = 1 },
'r|requests=i' => sub { $daemon->max_requests($_[1]) },
'u|user=s' => sub { $daemon->user($_[1]) };
'p|proxy' => sub { $daemon->reverse_proxy(1) },
'r|requests=i' => sub { $daemon->max_requests($_[1]) },
'u|user=s' => sub { $daemon->user($_[1]) };

$daemon->listen(\@listen) if @listen;
$daemon->run;
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Command/prefork.pm
Expand Up @@ -27,10 +27,10 @@ sub run {
'l|listen=s' => \my @listen,
'multi-accept=i' => sub { $prefork->multi_accept($_[1]) },
'P|pid-file=s' => sub { $prefork->pid_file($_[1]) },
'p|proxy' => sub { $ENV{MOJO_REVERSE_PROXY} = 1 },
'r|requests=i' => sub { $prefork->max_requests($_[1]) },
'u|user=s' => sub { $prefork->user($_[1]) },
'w|workers=i' => sub { $prefork->workers($_[1]) };
'p|proxy' => sub { $prefork->reverse_proxy(1) },
'r|requests=i' => sub { $prefork->max_requests($_[1]) },
'u|user=s' => sub { $prefork->user($_[1]) },
'w|workers=i' => sub { $prefork->workers($_[1]) };

$prefork->listen(\@listen) if @listen;
$prefork->run;
Expand Down
7 changes: 3 additions & 4 deletions t/mojo/hypnotoad.t
Expand Up @@ -21,7 +21,6 @@ use Mojo::Util qw(slurp spurt);

# Configure
{
local $ENV{MOJO_REVERSE_PROXY};
my $hypnotoad = Mojo::Server::Hypnotoad->new;
$hypnotoad->prefork->app->config->{myserver} = {
accept_interval => 33,
Expand Down Expand Up @@ -63,9 +62,9 @@ use Mojo::Util qw(slurp spurt);
is $hypnotoad->prefork->max_requests, 3, 'right value';
is $hypnotoad->prefork->multi_accept, 16, 'right value';
is $hypnotoad->prefork->pid_file, '/foo/bar.pid', 'right value';
is $hypnotoad->prefork->user, 'tester', 'right value';
is $hypnotoad->prefork->workers, 7, 'right value';
ok $ENV{MOJO_REVERSE_PROXY}, 'reverse proxy enabled';
ok $hypnotoad->prefork->reverse_proxy, 'reverse proxy enabled';
is $hypnotoad->prefork->user, 'tester', 'right value';
is $hypnotoad->prefork->workers, 7, 'right value';
is $hypnotoad->upgrade_timeout, 45, 'right value';
}

Expand Down
2 changes: 2 additions & 0 deletions t/mojolicious/lite_app.t
Expand Up @@ -731,13 +731,15 @@ $t->get_ok('/0' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
# Reverse proxy with "X-Forwarded-For"
{
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$!);
}

# Reverse proxy with "X-Forwarded-HTTPS"
{
local $ENV{MOJO_REVERSE_PROXY} = 1;
$t->ua->server->restart;
$t->get_ok('/0' => {'X-Forwarded-HTTPS' => 1})->status_is(200)
->content_like(qr!^https://localhost:\d+/0-!)->content_like(qr/-0$/)
->content_unlike(qr!-192\.0\.2\.1-0$!);
Expand Down

0 comments on commit d97370f

Please sign in to comment.