From 219c409844c80ce70c4b75500bca83727edffd98 Mon Sep 17 00:00:00 2001 From: James Coglan Date: Fri, 26 Feb 2021 14:44:50 +0000 Subject: [PATCH] Use the Ruby Encoding constants instead of their string names --- lib/websocket/driver.rb | 11 ++++------- lib/websocket/driver/draft75.rb | 2 +- lib/websocket/driver/draft76.rb | 2 +- lib/websocket/driver/hybi.rb | 6 +++--- lib/websocket/driver/hybi/message.rb | 2 +- lib/websocket/driver/stream_reader.rb | 6 +++--- spec/spec_helper.rb | 2 +- spec/websocket/driver/draft75_spec.rb | 2 +- spec/websocket/driver/draft76_spec.rb | 6 +++--- 9 files changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/websocket/driver.rb b/lib/websocket/driver.rb index 6f440de..0d25da7 100644 --- a/lib/websocket/driver.rb +++ b/lib/websocket/driver.rb @@ -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 diff --git a/lib/websocket/driver/draft75.rb b/lib/websocket/driver/draft75.rb index 041b725..2cf58da 100644 --- a/lib/websocket/driver/draft75.rb +++ b/lib/websocket/driver/draft75.rb @@ -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 diff --git a/lib/websocket/driver/draft76.rb b/lib/websocket/driver/draft76.rb index 8ee84ca..112c382 100644 --- a/lib/websocket/driver/draft76.rb +++ b/lib/websocket/driver/draft76.rb @@ -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' diff --git a/lib/websocket/driver/hybi.rb b/lib/websocket/driver/hybi.rb index 1d57366..d062410 100644 --- a/lib/websocket/driver/hybi.rb +++ b/lib/websocket/driver/hybi.rb @@ -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 diff --git a/lib/websocket/driver/hybi/message.rb b/lib/websocket/driver/hybi/message.rb index 7a62f9e..2f1a666 100644 --- a/lib/websocket/driver/hybi/message.rb +++ b/lib/websocket/driver/hybi/message.rb @@ -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) diff --git a/lib/websocket/driver/stream_reader.rb b/lib/websocket/driver/stream_reader.rb index 0ce8683..d63b2f7 100644 --- a/lib/websocket/driver/stream_reader.rb +++ b/lib/websocket/driver/stream_reader.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74e25f8..78478bf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/websocket/driver/draft75_spec.rb b/spec/websocket/driver/draft75_spec.rb index 4cbd88d..1ed7156 100644 --- a/spec/websocket/driver/draft75_spec.rb +++ b/spec/websocket/driver/draft75_spec.rb @@ -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 diff --git a/spec/websocket/driver/draft76_spec.rb b/spec/websocket/driver/draft76_spec.rb index 2284ce3..6563749 100644 --- a/spec/websocket/driver/draft76_spec.rb +++ b/spec/websocket/driver/draft76_spec.rb @@ -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]