Skip to content

Commit e6a8fe8

Browse files
authored
Fix handling of optional compression. (#17)
1 parent 1c6d7f3 commit e6a8fe8

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

lib/protocol/websocket/extension/compression/deflate.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,29 @@ def to_s
5454
attr :context_takeover
5555

5656
def pack_text_frame(buffer, compress: true, **options)
57-
buffer = self.deflate(buffer)
57+
if compress
58+
buffer = self.deflate(buffer)
59+
end
5860

5961
frame = @parent.pack_text_frame(buffer, **options)
6062

61-
frame.flags |= Frame::RSV1
63+
if compress
64+
frame.flags |= Frame::RSV1
65+
end
6266

6367
return frame
6468
end
6569

6670
def pack_binary_frame(buffer, compress: false, **options)
67-
buffer = self.deflate(buffer)
71+
if compress
72+
buffer = self.deflate(buffer)
73+
end
6874

6975
frame = @parent.pack_binary_frame(buffer, **options)
7076

71-
frame.flags |= Frame::RSV1
77+
if compress
78+
frame.flags |= Frame::RSV1
79+
end
7280

7381
return frame
7482
end

lib/protocol/websocket/extension/compression/inflate.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ def unpack_frames(frames, **options)
5454

5555
frame = frames.first
5656

57-
if frame.flags & Frame::RSV1
57+
if frame.flag?(Frame::RSV1)
5858
buffer = self.inflate(buffer)
59+
frame.flags &= ~Frame::RSV1
5960
end
6061

61-
frame.flags &= ~Frame::RSV1
62-
6362
return buffer
6463
end
6564

lib/protocol/websocket/frame.rb

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def initialize(finished = true, payload = nil, flags: 0, opcode: self.class::OPC
3434
@payload = payload
3535
end
3636

37+
def flag?(value)
38+
@flags & value != 0
39+
end
40+
3741
def <=> other
3842
to_ary <=> other.to_ary
3943
end

test/protocol/websocket/extension/compression.rb

+25-1
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,36 @@
4343
end
4444
end
4545

46+
it "can send and receive a text message without compression" do
47+
Async::WebSocket::Client.connect(endpoint) do |client|
48+
expect(client.writer).to be_a(Protocol::WebSocket::Extension::Compression::Deflate)
49+
expect(client.reader).to be_a(Protocol::WebSocket::Extension::Compression::Inflate)
50+
51+
client.send_text("Hello World", compress: false)
52+
client.flush
53+
54+
expect(client.read).to be == "Hello World"
55+
end
56+
end
57+
4658
it "can send and receive a binary message using compression" do
4759
Async::WebSocket::Client.connect(endpoint) do |client|
4860
expect(client.writer).to be_a(Protocol::WebSocket::Extension::Compression::Deflate)
4961
expect(client.reader).to be_a(Protocol::WebSocket::Extension::Compression::Inflate)
5062

51-
client.send_binary("Hello World")
63+
client.send_binary("Hello World", compress: true)
64+
client.flush
65+
66+
expect(client.read).to be == "Hello World"
67+
end
68+
end
69+
70+
it "can send and receive a binary message without compression" do
71+
Async::WebSocket::Client.connect(endpoint) do |client|
72+
expect(client.writer).to be_a(Protocol::WebSocket::Extension::Compression::Deflate)
73+
expect(client.reader).to be_a(Protocol::WebSocket::Extension::Compression::Inflate)
74+
75+
client.send_binary("Hello World", compress: false)
5276
client.flush
5377

5478
expect(client.read).to be == "Hello World"

0 commit comments

Comments
 (0)