Skip to content

Transition into closed state only occurs on READING a close frame. #14

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

Merged
merged 6 commits into from
Jul 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions lib/protocol/websocket/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@ def open!
return self
end

# If not already closed, transition the connection to the closed state and send a close frame.
def close!(...)
@state = :closed
unless @state == :closed
@state = :closed

begin
send_close(...)
rescue
# Ignore errors.
end
end

return self
end
Expand All @@ -71,16 +80,9 @@ def closed?
@state == :closed
end

# Immediately transition the connection to the closed state and close the underlying connection.
def close(...)
unless @state == :closed
close!

begin
send_close(...)
rescue
# Ignore.
end
end
close!(...)

@framer.close
end
Expand All @@ -101,11 +103,9 @@ def read_frame
return frame
rescue ProtocolError => error
close(error.code, error.message)

raise
rescue
close(Error::PROTOCOL_ERROR, $!.message)

rescue => error
close(Error::PROTOCOL_ERROR, error.message)
raise
end

Expand Down Expand Up @@ -142,11 +142,8 @@ def receive_continuation(frame)
def receive_close(frame)
code, reason = frame.unpack

# If we're already closed, then we don't need to send a close frame. Otherwise, according to the RFC, we should echo the close frame. However, it's possible it will fail to send if the connection is already closed.
unless @state == :closed
close!
send_close(code, reason)
end
# On receiving a close frame, we must enter the closed state:
close!(code, reason)

if code and code != Error::NO_ERROR
raise ClosedError.new reason, code
Expand Down Expand Up @@ -202,6 +199,7 @@ def send_binary(buffer, **options)
write_frame(@writer.pack_binary_frame(buffer, **options))
end

# Send a control frame with data containing a specified control sequence to begin the closing handshake. Does not close the connection, until the remote end responds with a close frame.
def send_close(code = Error::NO_ERROR, reason = "")
frame = CloseFrame.new(mask: @mask)
frame.pack(code, reason)
Expand Down Expand Up @@ -246,7 +244,6 @@ def read(**options)
end
rescue ProtocolError => error
close(error.code, error.message)

raise
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/protocol/websocket/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@
end

with '#receive_close' do
it "does not close the underlying connection" do
close_frame = Protocol::WebSocket::CloseFrame.new
close_frame.pack

expect(client).not.to receive(:close)
close_frame.apply(connection)
end

it "raises an exception when the close frame has an error code" do
close_frame = Protocol::WebSocket::CloseFrame.new
close_frame.pack(1001, "Fake error message")
Expand Down