Use the Ruby Encoding constants instead of their string names

This commit is contained in:
James Coglan
2021-02-26 14:44:50 +00:00
parent c73f5bcf10
commit 219c409844
9 changed files with 18 additions and 21 deletions
+4 -7
View File
@@ -44,9 +44,6 @@ module WebSocket
MAX_LENGTH = 0x3ffffff
STATES = [:connecting, :open, :closing, :closed]
BINARY = 'ASCII-8BIT'
UNICODE = 'UTF-8'
ConnectEvent = Struct.new(nil)
OpenEvent = Struct.new(nil)
MessageEvent = Struct.new(:data)
@@ -118,7 +115,7 @@ module WebSocket
end
def text(message)
message = message.encode(UNICODE) unless message.encoding.name == UNICODE
message = message.encode(Encoding::UTF_8) unless message.encoding == Encoding::UTF_8
frame(message, :text)
end
@@ -201,11 +198,11 @@ module WebSocket
case string
when Array then
string = string.pack('C*')
encoding ||= BINARY
encoding ||= Encoding::BINARY
when String then
encoding ||= UNICODE
encoding ||= Encoding::UTF_8
end
unless string.encoding.name == encoding
unless string.encoding == encoding
string = string.dup if string.frozen?
string.force_encoding(encoding)
end
+1 -1
View File
@@ -56,7 +56,7 @@ module WebSocket
when 2 then
if octet == 0xFF
@stage = 0
emit(:message, MessageEvent.new(Driver.encode(@buffer, UNICODE)))
emit(:message, MessageEvent.new(Driver.encode(@buffer, Encoding::UTF_8)))
else
if @length
@skipped += 1
+1 -1
View File
@@ -9,7 +9,7 @@ module WebSocket
input = (@socket.env['rack.input'] || StringIO.new('')).read
input = input.dup if input.frozen?
@stage = -1
@body = input.force_encoding(BINARY)
@body = input.force_encoding(Encoding::BINARY)
@headers.clear
@headers['Upgrade'] = 'WebSocket'
+3 -3
View File
@@ -358,7 +358,7 @@ module WebSocket
when OPCODES[:close] then
code = (bytesize >= 2) ? payload.unpack(PACK_FORMATS[2]).first : nil
reason = (bytesize > 2) ? Driver.encode(bytes[2..-1] || [], UNICODE) : nil
reason = (bytesize > 2) ? Driver.encode(bytes[2..-1] || [], Encoding::UTF_8) : nil
unless (bytesize == 0) or
(code && code >= MIN_RESERVED_ERROR && code <= MAX_RESERVED_ERROR) or
@@ -377,7 +377,7 @@ module WebSocket
emit(:ping, PingEvent.new(payload))
when OPCODES[:pong] then
message = Driver.encode(payload, UNICODE)
message = Driver.encode(payload, Encoding::UTF_8)
callback = @ping_callbacks[message]
@ping_callbacks.delete(message)
callback.call if callback
@@ -395,7 +395,7 @@ module WebSocket
case message.opcode
when OPCODES[:text] then
payload = Driver.encode(payload, UNICODE)
payload = Driver.encode(payload, Encoding::UTF_8)
when OPCODES[:binary]
payload = payload.bytes.to_a
end
+1 -1
View File
@@ -14,7 +14,7 @@ module WebSocket
@rsv2 = false
@rsv3 = false
@opcode = nil
@data = String.new('').force_encoding(BINARY)
@data = String.new('').force_encoding(Encoding::BINARY)
end
def <<(frame)
+3 -3
View File
@@ -6,13 +6,13 @@ module WebSocket
MINIMUM_AUTOMATIC_PRUNE_OFFSET = 128
def initialize
@buffer = String.new('').force_encoding(BINARY)
@buffer = String.new('').force_encoding(Encoding::BINARY)
@offset = 0
end
def put(chunk)
return unless chunk and chunk.bytesize > 0
@buffer << chunk.force_encoding(BINARY)
@buffer << chunk.force_encoding(Encoding::BINARY)
end
# Read bytes from the data:
@@ -42,7 +42,7 @@ module WebSocket
buffer_size = @buffer.bytesize
if @offset > buffer_size
@buffer = String.new('').force_encoding(BINARY)
@buffer = String.new('').force_encoding(Encoding::BINARY)
else
@buffer = @buffer.byteslice(@offset, buffer_size - @offset)
end
+1 -1
View File
@@ -6,7 +6,7 @@ require File.expand_path('../websocket/driver/draft75_examples', __FILE__)
module EncodingHelper
def encode(message)
message.respond_to?(:force_encoding) ?
message.force_encoding("UTF-8") :
message.force_encoding(Encoding::UTF_8) :
message
end
+1 -1
View File
@@ -90,7 +90,7 @@ describe WebSocket::Driver::Draft75 do
"WebSocket-Origin: http://www.example.com\r\n" +
"WebSocket-Location: ws://www.example.com/socket\r\n" +
"\r\n")
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00Hi\xFF", WebSocket::Driver::BINARY)
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00Hi\xFF", Encoding::BINARY)
driver.frame("Hi")
driver.start
+3 -3
View File
@@ -11,7 +11,7 @@ describe WebSocket::Driver::Draft76 do
let :response do
string = "\xB4\x9Cn@S\x04\x04&\xE5\e\xBFl\xB7\x9F\x1D\xF9"
string.force_encoding("ASCII-8BIT") if string.respond_to?(:force_encoding)
string.force_encoding(Encoding::BINARY) if string.respond_to?(:force_encoding)
string
end
@@ -142,7 +142,7 @@ describe WebSocket::Driver::Draft76 do
"Sec-WebSocket-Location: ws://www.example.com/socket\r\n" +
"\r\n")
expect(socket).to receive(:write).with(response)
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00Hi\xFF", WebSocket::Driver::BINARY)
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00Hi\xFF", Encoding::BINARY)
driver.frame("Hi")
driver.start
@@ -196,7 +196,7 @@ describe WebSocket::Driver::Draft76 do
it "sends any frames queued before the handshake was complete" do
expect(socket).to receive(:write).with(response)
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00hello\xFF", WebSocket::Driver::BINARY)
expect(socket).to receive(:write).with(WebSocket::Driver.encode "\x00hello\xFF", Encoding::BINARY)
driver.frame("hello")
driver.parse(body)
expect(@bytes).to eq [0, 104, 101, 108, 108, 111, 255]