about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-04-21 11:14:53 -0700
committerEric Wong <normalperson@yhbt.net>2009-04-21 11:16:38 -0700
commit79080e97c0ef1863c1e80a9ddf30c69323af1ca7 (patch)
tree1285a6d91ab86827f51615990eef364e238e761b
parent588577cc4852f9dce1bd011451399ce96c00023c (diff)
downloadunicorn-79080e97c0ef1863c1e80a9ddf30c69323af1ca7.tar.gz
This removes the #unicorn_peeraddr methods from TCPSocket and
UNIXSocket core classes.  Instead, just move that logic into the
only place it needs to be used in HttpRequest.
-rw-r--r--lib/unicorn/http_request.rb20
-rw-r--r--lib/unicorn/socket.rb13
-rw-r--r--test/unit/test_request.rb11
-rw-r--r--test/unit/test_socket_helper.rb45
4 files changed, 13 insertions, 76 deletions
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index d170505..9be3b11 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -55,6 +55,16 @@ module Unicorn
     # This does minimal exception trapping and it is up to the caller
     # to handle any socket errors (e.g. user aborted upload).
     def read(socket)
+      # From http://www.ietf.org/rfc/rfc3875:
+      # "Script authors should be aware that the REMOTE_ADDR and
+      #  REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
+      #  may not identify the ultimate source of the request.  They
+      #  identify the client for the immediate request to the server;
+      #  that client may be a proxy, gateway, or other intermediary
+      #  acting on behalf of the actual source client."
+      @params[Const::REMOTE_ADDR] =
+                    TCPSocket === socket ? socket.peeraddr.last : '127.0.0.1'
+
       # short circuit the common case with small GET requests first
       @parser.execute(@params, read_socket(socket)) and
           return handle_body(socket)
@@ -70,7 +80,7 @@ module Unicorn
       rescue HttpParserError => e
         @logger.error "HTTP parse error, malformed request " \
                       "(#{@params[Const::HTTP_X_FORWARDED_FOR] ||
-                          socket.unicorn_peeraddr}): #{e.inspect}"
+                          @params[Const::REMOTE_ADDR]}): #{e.inspect}"
         @logger.error "REQUEST DATA: #{data.inspect}\n---\n" \
                       "PARAMS: #{@params.inspect}\n---\n"
         raise e
@@ -120,14 +130,6 @@ module Unicorn
       # over the HTTP response.
       # @params["unicorn.client"] = socket
 
-      # From http://www.ietf.org/rfc/rfc3875:
-      # "Script authors should be aware that the REMOTE_ADDR and
-      #  REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
-      #  may not identify the ultimate source of the request.  They
-      #  identify the client for the immediate request to the server;
-      #  that client may be a proxy, gateway, or other intermediary
-      #  acting on behalf of the actual source client."
-      @params[Const::REMOTE_ADDR] = socket.unicorn_peeraddr
       @params[Const::RACK_INPUT] = @body
       @params.update(DEF_PARAMS)
     end
diff --git a/lib/unicorn/socket.rb b/lib/unicorn/socket.rb
index 5a3bfd7..850ad85 100644
--- a/lib/unicorn/socket.rb
+++ b/lib/unicorn/socket.rb
@@ -1,18 +1,5 @@
 require 'socket'
 
-class UNIXSocket
-  UNICORN_PEERADDR = '127.0.0.1'.freeze
-  def unicorn_peeraddr
-    UNICORN_PEERADDR
-  end
-end
-
-class TCPSocket
-  def unicorn_peeraddr
-    peeraddr.last
-  end
-end
-
 module Unicorn
   module SocketHelper
     include Socket::Constants
diff --git a/test/unit/test_request.rb b/test/unit/test_request.rb
index dbe4069..7c438da 100644
--- a/test/unit/test_request.rb
+++ b/test/unit/test_request.rb
@@ -19,11 +19,7 @@ include Unicorn
 
 class RequestTest < Test::Unit::TestCase
 
-  class MockRequest < StringIO
-    def unicorn_peeraddr
-      '666.666.666.666'
-    end
-  end
+  class MockRequest < StringIO; end
 
   def setup
     @request = HttpRequest.new(Logger.new($stderr))
@@ -123,7 +119,7 @@ class RequestTest < Test::Unit::TestCase
     res = env = nil
     assert_nothing_raised { env = @request.read(client) }
     assert_equal "http", env['rack.url_scheme']
-    assert_equal '666.666.666.666', env['REMOTE_ADDR']
+    assert_equal '127.0.0.1', env['REMOTE_ADDR']
     assert_nothing_raised { res = @lint.call(env) }
   end
 
@@ -146,9 +142,6 @@ class RequestTest < Test::Unit::TestCase
     buf = (' ' * bs).freeze
     length = bs * count
     client = Tempfile.new('big_put')
-    def client.unicorn_peeraddr
-      '1.1.1.1'
-    end
     client.syswrite(
       "PUT / HTTP/1.1\r\n" \
       "Host: foo\r\n" \
diff --git a/test/unit/test_socket_helper.rb b/test/unit/test_socket_helper.rb
index 0608e24..3e28cb9 100644
--- a/test/unit/test_socket_helper.rb
+++ b/test/unit/test_socket_helper.rb
@@ -123,49 +123,4 @@ class TestSocketHelper < Test::Unit::TestCase
     sock_name(@unix_server)
   end
 
-  def test_tcp_unicorn_peeraddr
-    test_bind_listen_tcp
-    @tcp_server = server_cast(@tcp_listener)
-    tmp = Tempfile.new 'shared'
-    pid = fork do
-      client = @tcp_server.accept
-      IO.select([client])
-      assert_equal GET_SLASH, client.sysread(GET_SLASH.size)
-      tmp.syswrite "#{client.unicorn_peeraddr}"
-      exit 0
-    end
-    host, port = sock_name(@tcp_server).split(/:/)
-    client = TCPSocket.new(host, port.to_i)
-    client.syswrite(GET_SLASH)
-
-    pid, status = Process.waitpid2(pid)
-    assert_nothing_raised { client.close }
-    assert status.success?
-    tmp.sysseek 0
-    assert_equal @test_addr, tmp.sysread(4096)
-    tmp.sysseek 0
-  end
-
-  def test_unix_unicorn_peeraddr
-    test_bind_listen_unix
-    @unix_server = server_cast(@unix_listener)
-    tmp = Tempfile.new 'shared'
-    pid = fork do
-      client = @unix_server.accept
-      IO.select([client])
-      assert_equal GET_SLASH, client.sysread(4096)
-      tmp.syswrite "#{client.unicorn_peeraddr}"
-      exit 0
-    end
-    client = UNIXSocket.new(@unix_listener_path)
-    client.syswrite(GET_SLASH)
-
-    pid, status = Process.waitpid2(pid)
-    assert_nothing_raised { client.close }
-    assert status.success?
-    tmp.sysseek 0
-    assert_equal '127.0.0.1', tmp.sysread(4096)
-    tmp.sysseek 0
-  end
-
 end