Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 5 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 37 additions and 5 deletions.
  1. +17 −3 lib/em-websocket/connection.rb
  2. +2 −2 lib/em-websocket/message_processor_06.rb
  3. +18 −0 spec/integration/shared_examples.rb
View
20 lib/em-websocket/connection.rb
@@ -129,16 +129,26 @@ def send_flash_cross_domain_file
close_connection_after_writing
end
+ # Cache encodings since it's moderately expensive to look them up each time
+ ENCODING_SUPPORTED = "string".respond_to?(:force_encoding)
+ UTF8 = Encoding.find("UTF-8") if ENCODING_SUPPORTED
+ BINARY = Encoding.find("BINARY") if ENCODING_SUPPORTED
+
+ # Send a WebSocket text frame.
+ #
+ # A WebSocketError may be raised if the connection is in an opening or a
+ # closing state, or if the passed in data is not valid UTF-8
+ #
def send(data)
# If we're using Ruby 1.9, be pedantic about encodings
- if data.respond_to?(:force_encoding)
+ if ENCODING_SUPPORTED
# Also accept ascii only data in other encodings for convenience
- unless (data.encoding == Encoding.find("UTF-8") && data.valid_encoding?) || data.ascii_only?
+ unless (data.encoding == UTF8 && data.valid_encoding?) || data.ascii_only?
raise WebSocketError, "Data sent to WebSocket must be valid UTF-8 but was #{data.encoding} (valid: #{data.valid_encoding?})"
end
# This labels the encoding as binary so that it can be combined with
# the BINARY framing
- data.force_encoding("BINARY")
+ data.force_encoding(BINARY)
else
# TODO: Check that data is valid UTF-8
end
@@ -148,6 +158,10 @@ def send(data)
else
raise WebSocketError, "Cannot send data before onopen callback"
end
+
+ # Revert data back to the original encoding (which we assume is UTF-8)
+ # Doing this to avoid duping the string - there may be a better way
+ data.force_encoding(UTF8)
end
# Send a ping to the client. The client must respond with a pong.
View
4 lib/em-websocket/message_processor_06.rb
@@ -20,8 +20,8 @@ def message(message_type, extension_data, application_data)
debug [:close_frame_received, status_code, application_data]
if @state == :closing
- # We can close connection immediately since there is no more data
- # is allowed to be sent or received on this connection
+ # We can close connection immediately since no more data may be
+ # sent or received on this connection
@connection.close_connection
@state = :closed
else
View
18 spec/integration/shared_examples.rb
@@ -124,5 +124,23 @@
start_client { }
}
end
+
+ it "should not change the encoding of strings sent to send [antiregression]" do
+ em {
+ start_server { |server|
+ server.onopen {
+ s = "example string"
+ s.force_encoding("UTF-8")
+
+ server.send(s)
+
+ s.encoding.should == Encoding.find("UTF-8")
+ done
+ }
+ }
+
+ start_client { }
+ }
+ end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.