Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only hijack Rack socket when first needed #23843

Merged
merged 1 commit into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions actioncable/lib/action_cable/connection/client_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def initialize(env, event_target, stream_event_loop)

def start_driver
return if @driver.nil? || @driver_started
@stream.hijack_rack_socket
@driver_started = true
@driver.start
end
Expand Down
18 changes: 8 additions & 10 deletions actioncable/lib/action_cable/connection/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ module Connection
# This class is heavily based on faye-websocket-ruby
#
# Copyright (c) 2010-2015 James Coglan
class Stream
class Stream # :nodoc:
def initialize(event_loop, socket)
@event_loop = event_loop
@socket_object = socket
@stream_send = socket.env['stream.send']

@rack_hijack_io = nil

hijack_rack_socket
end

def each(&callback)
Expand All @@ -39,16 +37,16 @@ def receive(data)
@socket_object.parse(data)
end

private
def hijack_rack_socket
return unless @socket_object.env['rack.hijack']
def hijack_rack_socket
return unless @socket_object.env['rack.hijack']

@socket_object.env['rack.hijack'].call
@rack_hijack_io = @socket_object.env['rack.hijack_io']
@socket_object.env['rack.hijack'].call
@rack_hijack_io = @socket_object.env['rack.hijack_io']

@event_loop.attach(@rack_hijack_io, self)
end
@event_loop.attach(@rack_hijack_io, self)
end

private
def clean_rack_hijack
return unless @rack_hijack_io
@event_loop.detach(@rack_hijack_io, self)
Expand Down
20 changes: 20 additions & 0 deletions actioncable/test/connection/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ def send_async(method, *args)
end
end

test "rejecting a connection causes a 404" do
run_in_eventmachine do
class CallMeMaybe
def call(*)
raise 'Do not call me!'
end
end

env = Rack::MockRequest.env_for(
"/test",
{ 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket',
'HTTP_ORIGIN' => 'http://rubyonrails.org', 'rack.hijack' => CallMeMaybe.new }
)

connection = ActionCable::Connection::Base.new(@server, env)
response = connection.process
assert_equal 404, response[0]
end
end

private
def open_connection
env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket',
Expand Down