Skip to content

Commit

Permalink
deprecated Mojo::Headers->x_forwarded_for
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 2, 2012
1 parent e2ce2dc commit 626d29b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 45 deletions.
5 changes: 2 additions & 3 deletions Changes
@@ -1,15 +1,14 @@
This file documents the revision history for Perl extension Mojolicious.

2.47 2012-02-02 00:00:00
2.47 2012-02-03 00:00:00
- Deprecated Mojo::Server::Daemon->prepare_ioloop in favor of
Mojo::Server::Daemon->start.
- Deprecated Mojo::Headers->x_forwarded_for.
- Added EXPERIMENTAL drain event to Mojo::Content.
- Added EXPERIMENTAL drain event to Mojo::Transaction::WebSocket.
- Added EXPERIMENTAL support for RSV1-3 flags to
Mojo::Transaction::WebSocket.
- Added lock_timeout parameter to Hypnotoad.
- Added x_forwarded_host, x_forwarded_https and x_requested_with
methods to Mojo::Headers.
- Removed experimental status from JSON Pointer support.
- Removed Cygwin exception from Hypnotoad.
- Renamed resume and pause methods in Mojo::IOLoop::Server to start
Expand Down
37 changes: 7 additions & 30 deletions lib/Mojo/Headers.pm
Expand Up @@ -14,8 +14,7 @@ my @HEADERS = (
qw/Proxy-Authenticate Proxy-Authorization Range Sec-WebSocket-Accept/,
qw/Sec-WebSocket-Key Sec-WebSocket-Origin Sec-WebSocket-Protocol/,
qw/Sec-WebSocket-Version Server Set-Cookie Status Trailer/,
qw/Transfer-Encoding Upgrade User-Agent WWW-Authenticate X-Forwarded-For/,
qw/X-Forwarded-Host X-Forwarded-HTTPS X-Requested-With/
qw/Transfer-Encoding Upgrade User-Agent WWW-Authenticate/
);
{
no strict 'refs';
Expand Down Expand Up @@ -196,6 +195,12 @@ sub to_string {
return join "\x0d\x0a", @headers;
}

# DEPRECATED in Leaf Fluttering In Wind!
sub x_forwarded_for {
warn "Mojo::Headers->x_forwarded_for is DEPRECATED!\n";
shift->header('X-Forwarded-For' => @_);
}

1;
__END__
Expand Down Expand Up @@ -588,34 +593,6 @@ Shortcut for the C<User-Agent> header.
Shortcut for the C<WWW-Authenticate> header.
=head2 C<x_forwarded_for>
my $x_forwarded_for = $headers->x_forwarded_for;
$headers = $headers->x_forwarded_for('127.0.0.1');
Shortcut for the C<X-Forwarded-For> header.
=head2 C<x_forwarded_host>
my $x_forwarded_host = $headers->x_forwarded_host;
$headers = $headers->x_forwarded_host('mojolicio.us');
Shortcut for the C<X-Forwarded-Host> header.
=head2 C<x_forwarded_https>
my $x_forwarded_https = $headers->x_forwarded_https;
$headers = $headers->x_forwarded_https(1);
Shortcut for the C<X-Forwarded-HTTPS> header.
=head2 C<x_requested_with>
my $x_requested_with = $headers->x_requested_with;
$headers = $headers->x_requested_with('XMLHttpRequest');
Shortcut for the C<X-Requested-With> header.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
8 changes: 5 additions & 3 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -91,7 +91,9 @@ sub is_secure {
return ($url->scheme || $url->base->scheme || '') eq 'https';
}

sub is_xhr { (shift->headers->x_requested_with || '') =~ /XMLHttpRequest/i }
sub is_xhr {
(shift->headers->header('X-Requested-With') || '') =~ /XMLHttpRequest/i;
}

sub param {
my $self = shift;
Expand Down Expand Up @@ -153,15 +155,15 @@ sub parse {
if ($ENV{MOJO_REVERSE_PROXY}) {

# "X-Forwarded-Host"
if (my $host = $headers->x_forwarded_host) {
if (my $host = $headers->header('X-Forwarded-Host')) {
if ($host =~ $HOST_RE) {
$base->host($1);
$base->port($2) if defined $2;
}
}

# "X-Forwarded-HTTPS"
if ($headers->x_forwarded_https) { $base->scheme('https') }
$base->scheme('https') if $headers->header('X-Forwarded-HTTPS');
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction.pm
Expand Up @@ -59,8 +59,8 @@ sub remote_address {
# Reverse proxy
if ($ENV{MOJO_REVERSE_PROXY}) {
return $self->{forwarded_for} if $self->{forwarded_for};
return $self->{forwarded_for} = $1
if ($self->req->headers->x_forwarded_for || '') =~ /([^,\s]+)$/;
($self->req->headers->header('X-Forwarded-For') || '') =~ /([^,\s]+)$/
and return $self->{forwarded_for} = $1;
}

return $self->{remote_address};
Expand Down
7 changes: 1 addition & 6 deletions t/mojo/headers.t
@@ -1,7 +1,7 @@
use Mojo::Base -strict;

# "Remember, you can always find East by staring directly at the sun."
use Test::More tests => 92;
use Test::More tests => 88;

# "So, have a merry Christmas, a happy Hanukkah, a kwaazy Kwanza,
# a tip-top Tet, and a solemn, dignified, Ramadan.
Expand Down Expand Up @@ -92,11 +92,6 @@ is $headers->transfer_encoding('foo')->transfer_encoding, 'foo',
is $headers->upgrade('foo')->upgrade, 'foo', 'right value';
is $headers->user_agent('foo')->user_agent, 'foo', 'right value';
is $headers->www_authenticate('foo')->www_authenticate, 'foo', 'right value';
is $headers->x_forwarded_for('foo')->x_forwarded_for, 'foo', 'right value';
is $headers->x_forwarded_host('foo')->x_forwarded_host, 'foo', 'right value';
is $headers->x_forwarded_https('foo')->x_forwarded_https, 'foo',
'right value';
is $headers->x_requested_with('foo')->x_requested_with, 'foo', 'right value';

# Clone
$headers = Mojo::Headers->new;
Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -12,7 +12,7 @@ my @leaf = (
qw/add_hook comment connect connection_timeout is_done keep_alive_timeout/,
qw/listen max_redirects on_close on_error on_finish on_lock on_process/,
qw/on_read on_unlock port prepare_ioloop run_hook run_hook_reverse/,
qw/timeout version write/
qw/timeout version write x_forwarded_for/
);

# "Marge, I'm going to miss you so much. And it's not just the sex.
Expand Down

0 comments on commit 626d29b

Please sign in to comment.