Module: Protocol::HTTP2::Padded

Included in:
DataFrame, HeadersFrame, PushPromiseFrame
Defined in:
lib/protocol/http2/padded.rb

Overview

Certain frames can have padding: http2.github.io/http2-spec/#padding

--------------- |Pad Length? (8)| ---------------———————————————–+ | Data (*) … --------------------------------------------------------------- | Padding (*) … ---------------------------------------------------------------

Instance Method Summary collapse

Instance Method Details

#pack(data, padding_size: nil, maximum_size: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/protocol/http2/padded.rb', line 26

def pack(data, padding_size: nil, maximum_size: nil)
	if padding_size
		set_flags(PADDED)
		
		buffer = String.new.b
		
		buffer << padding_size
		buffer << data
		
		if padding_size
			buffer << ("\0" * padding_size)
		end
		
		super buffer
	else
		clear_flags(PADDED)
		
		super data
	end
end

#padded?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/protocol/http2/padded.rb', line 22

def padded?
	flag_set?(PADDED)
end

#unpackObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/protocol/http2/padded.rb', line 47

def unpack
	if padded?
		padding_size = @payload[0].ord
		
		# 1 byte for the padding octet, and padding_size bytes for the padding itself:
		data_size = @payload.bytesize - (1 + padding_size)
		
		if data_size < 0
			raise ProtocolError, "Invalid padding length: #{padding_size}"
		end
		
		return @payload.byteslice(1, data_size)
	else
		return @payload
	end
end