Skip to content
This repository
Browse code

DEPRECATED: $req->hostname is deprecated, and doesn't do

IP-to-hostname lookups anymore.

Added $req->remote_host which just returns REMOTE_HOST.
  • Loading branch information...
commit ebf84a53fe82c55641001e90f28eb08bbc6855c9 1 parent c3b0718
Tatsuhiko Miyagawa authored January 20, 2010
41  lib/Plack/Request.pm
@@ -8,10 +8,15 @@ use HTTP::Headers;
8 8
 use URI::QueryParam;
9 9
 use Carp ();
10 10
 
11  
-use Socket qw[AF_INET inet_aton]; # for _build_hostname
12 11
 use Plack::Request::Upload;
13 12
 use URI;
14 13
 
  14
+sub _deprecated {
  15
+    my $self = shift;
  16
+    my $method = (caller(1))[3];
  17
+    Carp::carp("$method is deprecated. Use Piglet::Request instead.");
  18
+}
  19
+
15 20
 sub new {
16 21
     my($class, $env) = @_;
17 22
     Carp::confess(q{$env is required})
@@ -25,6 +30,7 @@ sub new {
25 30
 sub env { $_[0]->{env} }
26 31
 
27 32
 sub address     { $_[0]->env->{REMOTE_ADDR} }
  33
+sub remote_host { $_[0]->env->{REMOTE_HOST} }
28 34
 sub protocol    { $_[0]->env->{SERVER_PROTOCOL} }
29 35
 sub method      { $_[0]->env->{REQUEST_METHOD} }
30 36
 sub port        { $_[0]->env->{SERVER_PORT} }
@@ -33,6 +39,12 @@ sub request_uri { $_[0]->env->{REQUEST_URI} }
33 39
 sub url_scheme  { $_[0]->env->{'psgi.url_scheme'} }
34 40
 sub session     { $_[0]->env->{'psgix.session'} }
35 41
 
  42
+sub hostname {
  43
+    my $self = shift;
  44
+    _deprecated;
  45
+    $self->remote_host || $self->address;
  46
+}
  47
+
36 48
 sub secure {
37 49
     $_[0]->url_scheme eq 'https';
38 50
 }
@@ -110,33 +122,6 @@ sub header           { shift->headers->header(@_) }
110 122
 sub referer          { shift->headers->referer(@_) }
111 123
 sub user_agent       { shift->headers->user_agent(@_) }
112 124
 
113  
-sub hostname {
114  
-    my $self = shift;
115  
-    if (defined $_[0]) {
116  
-        $self->{hostname} = $_[0];
117  
-    } elsif (!defined $self->{hostname}) {
118  
-        $self->{hostname} = $self->env->{REMOTE_HOST} || $self->_resolve_hostname;
119  
-    }
120  
-    $self->{hostname};
121  
-}
122  
-
123  
-sub _resolve_hostname {
124  
-    my ( $self, ) = @_;
125  
-    gethostbyaddr( inet_aton( $self->address ), AF_INET );
126  
-}
127  
-# for win32 hacks
128  
-BEGIN {
129  
-    if ($^O eq 'MSWin32') {
130  
-        no warnings 'redefine';
131  
-        *_build_hostname = sub {
132  
-            my ( $self, ) = @_;
133  
-            my $address = $self->address;
134  
-            return 'localhost' if $address eq '127.0.0.1';
135  
-            return gethostbyaddr( inet_aton( $address ), AF_INET );
136  
-        };
137  
-    }
138  
-}
139  
-
140 125
 # TODO: This attribute should be private. I will remove deps for HTTP::Body
141 126
 sub _http_body {
142 127
     my $self = shift;
4  t/Plack-Request/hostname.t
@@ -7,8 +7,8 @@ use Plack::Request;
7 7
 plan tests => 2;
8 8
 
9 9
 my $req = Plack::Request->new({ REMOTE_HOST => "foo.example.com" });
10  
-is $req->hostname, "foo.example.com";
  10
+is $req->remote_host, "foo.example.com";
11 11
 
12 12
 $req = Plack::Request->new({ REMOTE_HOST => '', REMOTE_ADDR => '127.0.0.1' });
13  
-is $req->hostname, "localhost";
  13
+is $req->address, "127.0.0.1";
14 14
 

0 notes on commit ebf84a5

Please sign in to comment.
Something went wrong with that request. Please try again.