Skip to content

Commit befd449

Browse files
committed
Prefer text frames over binary; if the input is in some non-UTF-8 text encoding, then transcode it
1 parent e3155f7 commit befd449

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/websocket/driver/hybi.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ def frame(buffer, type = nil, code = nil)
163163
message = Message.new
164164
frame = Frame.new
165165

166-
payload = Driver.encode(buffer)
167-
payload = [code, payload].pack('S>a*') if code
168-
type ||= (payload.encoding == Encoding::UTF_8) ? :text : :binary
166+
is_binary = (Array === buffer or buffer.encoding == Encoding::BINARY)
167+
payload = Driver.encode(buffer, is_binary ? nil : Encoding::UTF_8)
168+
payload = [code, payload].pack('S>a*') if code
169+
type ||= is_binary ? :binary : :text
169170

170171
message.rsv1 = message.rsv2 = message.rsv3 = false
171172
message.opcode = OPCODES[type]

0 commit comments

Comments
 (0)