Skip to content

Fix handling of optional compression. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/protocol/websocket/extension/compression/deflate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,29 @@ def to_s
attr :context_takeover

def pack_text_frame(buffer, compress: true, **options)
buffer = self.deflate(buffer)
if compress
buffer = self.deflate(buffer)
end

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

frame.flags |= Frame::RSV1
if compress
frame.flags |= Frame::RSV1
end

return frame
end

def pack_binary_frame(buffer, compress: false, **options)
buffer = self.deflate(buffer)
if compress
buffer = self.deflate(buffer)
end

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

frame.flags |= Frame::RSV1
if compress
frame.flags |= Frame::RSV1
end

return frame
end
Expand Down
5 changes: 2 additions & 3 deletions lib/protocol/websocket/extension/compression/inflate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ def unpack_frames(frames, **options)

frame = frames.first

if frame.flags & Frame::RSV1
if frame.flag?(Frame::RSV1)
buffer = self.inflate(buffer)
frame.flags &= ~Frame::RSV1
end

frame.flags &= ~Frame::RSV1

return buffer
end

Expand Down
4 changes: 4 additions & 0 deletions lib/protocol/websocket/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def initialize(finished = true, payload = nil, flags: 0, opcode: self.class::OPC
@payload = payload
end

def flag?(value)
@flags & value != 0
end

def <=> other
to_ary <=> other.to_ary
end
Expand Down
26 changes: 25 additions & 1 deletion test/protocol/websocket/extension/compression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,36 @@
end
end

it "can send and receive a text message without compression" do
Async::WebSocket::Client.connect(endpoint) do |client|
expect(client.writer).to be_a(Protocol::WebSocket::Extension::Compression::Deflate)
expect(client.reader).to be_a(Protocol::WebSocket::Extension::Compression::Inflate)

client.send_text("Hello World", compress: false)
client.flush

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

it "can send and receive a binary message using compression" do
Async::WebSocket::Client.connect(endpoint) do |client|
expect(client.writer).to be_a(Protocol::WebSocket::Extension::Compression::Deflate)
expect(client.reader).to be_a(Protocol::WebSocket::Extension::Compression::Inflate)

client.send_binary("Hello World")
client.send_binary("Hello World", compress: true)
client.flush

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

it "can send and receive a binary message without compression" do
Async::WebSocket::Client.connect(endpoint) do |client|
expect(client.writer).to be_a(Protocol::WebSocket::Extension::Compression::Deflate)
expect(client.reader).to be_a(Protocol::WebSocket::Extension::Compression::Inflate)

client.send_binary("Hello World", compress: false)
client.flush

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