Permalink
Browse files

pass the connection's remote IP when creating the Request

  • Loading branch information...
1 parent d02572a commit 403fe82abb67bf006bf15a5e0bcb86eadc1c8eb6 @jonasschneider committed Jan 6, 2012
Showing with 18 additions and 11 deletions.
  1. +2 −5 lib/momentum/connection.rb
  2. +1 −1 lib/momentum/request_stream.rb
  3. +15 −5 spec/momentum/request_stream_spec.rb
@@ -2,6 +2,7 @@ module Momentum
class Connection < EventMachine::Connection
HTTP_RESPONSE = "HTTP/1.0 505 HTTP Version not supported\r\nConnection: close\r\n\r\n<h1>505 HTTP Version not supported</h1>This is a SPDY server."
attr_accessor :backend
+ attr_reader :peer
def initialize(*args)
super
@@ -52,15 +53,11 @@ def initialize(*args)
def post_init
peername = get_peername
if peername
- @peer = remote_address
+ @peer = Socket.unpack_sockaddr_in(peername).pop
logger.info "Connection from: #{@peer}"
end
end
- def remote_address
- Socket.unpack_sockaddr_in(peername).pop
- end
-
def send_data(data)
logger.debug "<< #{hex data[0..19]} (len=#{data.size}, first 20 shown)" if trace?
super(data)
@@ -17,7 +17,7 @@ def add_body(chunk)
end
def process_request!
- @request = Request.new(headers: @headers, body: @body)
+ @request = Request.new(headers: @headers, body: @body, remote_addr: @session.peer)
logger.info "[#{@stream_id}] got a request to #{@request.uri}"
reply = @backend.prepare(@request)
@@ -12,7 +12,7 @@
let(:request_headers) { { 'method' => 'get', 'version' => 'HTTP/1.1', 'url' => '/', 'host' => 'localhost', 'scheme' => 'http' } }
let(:backend) { double(:prepare => backend_response) }
- let(:session) { double('Session', :logger => double(:debug => true, :info => true)) }
+ let(:session) { double('Session').as_null_object }
let(:stream) do
described_class.new(1, session, backend).tap do |stream|
stream.add_headers(request_headers)
@@ -30,9 +30,21 @@
stream.process_request!
end
- context "Request body" do
- let(:session) { double('Session').as_null_object }
+ context "spdy_info[:remote_addr]" do
+ let(:address) { '127.0.0.1' }
+
+ it "gets set to the connection's remote IP" do
+ session.stub(:peer) { address }
+
+ backend.should_receive(:prepare) do |req|
+ req.spdy_info[:remote_addr].should == address
+ end
+
+ stream.process_request!
+ end
+ end
+ context "Request body" do
it "passes request body" do
backend.stub(:prepare) do |req|
req.spdy_info[:body].should == 'ohai'
@@ -44,8 +56,6 @@
end
context "Request headers" do
- let(:session) { double('Session').as_null_object }
-
it "passes request headers" do
backend = Object.new
backend.stub(:prepare) do |req|

0 comments on commit 403fe82

Please sign in to comment.