Skip to content

Commit

Permalink
pass the connection's remote IP when creating the Request
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschneider committed Jan 6, 2012
1 parent d02572a commit 403fe82
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
7 changes: 2 additions & 5 deletions lib/momentum/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/momentum/request_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 15 additions & 5 deletions spec/momentum/request_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'
Expand All @@ -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|
Expand Down

0 comments on commit 403fe82

Please sign in to comment.