Skip to content

Commit 181b0a0

Browse files
committed
Fix handling of padding.
Fixes <socketry/async-http#112>.
1 parent f69b0e7 commit 181b0a0

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/protocol/http2/padded.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def pack(data, padding_size: nil, maximum_size: nil)
2929

3030
buffer = String.new.b
3131

32-
buffer << padding_size.chr
32+
buffer << padding_size
3333
buffer << data
3434

35-
if padding_size > 1
36-
buffer << "\0" * (padding_size - 1)
35+
if padding_size
36+
buffer << ("\0" * padding_size)
3737
end
3838

3939
super buffer
@@ -47,7 +47,9 @@ def pack(data, padding_size: nil, maximum_size: nil)
4747
def unpack
4848
if padded?
4949
padding_size = @payload[0].ord
50-
data_size = @payload.bytesize - padding_size
50+
51+
# 1 byte for the padding octet, and padding_size bytes for the padding itself:
52+
data_size = @payload.bytesize - (1 + padding_size)
5153

5254
if data_size < 0
5355
raise ProtocolError, "Invalid padding length: #{padding_size}"

test/protocol/http2/data_frame.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def before
5454
it "adds appropriate padding" do
5555
frame.pack "Hello World!", padding_size: 4
5656

57-
expect(frame.length).to be == 16
57+
expect(frame.length).to be == 17
5858
expect(frame.payload[0].ord).to be == 4
5959
expect(frame.unpack).to be == "Hello World!"
6060

6161
stream = StringIO.new
6262
frame.write(stream)
6363

64-
expect(stream.string.bytesize).to be == 25
64+
expect(stream.string.bytesize).to be == 26
6565
stream.rewind
6666

6767
frame2 = subject.new
@@ -74,12 +74,12 @@ def before
7474
it "detects invalid padding" do
7575
frame.pack "Hello World!", padding_size: 4
7676

77-
expect(frame.length).to be == 16
77+
expect(frame.length).to be == 17
7878
expect(frame.payload[0].ord).to be == 4
7979
expect(frame.unpack).to be == "Hello World!"
8080

8181
# Artifically set the padding to be the entire payload:
82-
frame.payload[0] = (frame.payload.bytesize).chr
82+
frame.payload[0] = (frame.payload.bytesize - 1).chr
8383
expect(frame.unpack).to be == ""
8484

8585
# Artifically set the padding to be larger than the payload:

0 commit comments

Comments
 (0)