Class: Protocol::HTTP2::Frame

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/protocol/http2/frame.rb

Constant Summary collapse

VALID_STREAM_ID =

Stream Identifier cannot be bigger than this: http2.github.stream/http2-spec/#rfc.section.4.1

0..0x7fffffff
VALID_LENGTH =

The absolute maximum bounds for the length field:

0..0xffffff
LENGTH_HISHIFT =

Used for generating 24-bit frame length:

16
LENGTH_LOMASK =
0xFFFF
TYPE =

The base class does not have any specific type index:

nil
HEADER_FORMAT =
"CnCCN".freeze
STREAM_ID_MASK =
0x7fffffff

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream_id = 0, flags = 0, type = self.class::TYPE, length = nil, payload = nil) ⇒ Frame

Returns a new instance of Frame.



38
39
40
41
42
43
44
# File 'lib/protocol/http2/frame.rb', line 38

def initialize(stream_id = 0, flags = 0, type = self.class::TYPE, length = nil, payload = nil)
	@stream_id = stream_id
	@flags = flags
	@type = type
	@length = length
	@payload = payload
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



72
73
74
# File 'lib/protocol/http2/frame.rb', line 72

def flags
  @flags
end

#lengthObject

The generic frame header uses the following binary representation:

----------------------------------------------- | Length (24) | ---------------—————--------------- | Type (8) | Flags (8) | -————----------------——————————-+ |R| Stream Identifier (31) | ==============================================================+ | Frame Payload (0…) … ---------------------------------------------------------------



70
71
72
# File 'lib/protocol/http2/frame.rb', line 70

def length
  @length
end

#payloadObject

Returns the value of attribute payload.



74
75
76
# File 'lib/protocol/http2/frame.rb', line 74

def payload
  @payload
end

#stream_idObject

Returns the value of attribute stream_id.



73
74
75
# File 'lib/protocol/http2/frame.rb', line 73

def stream_id
  @stream_id
end

#typeObject

Returns the value of attribute type.



71
72
73
# File 'lib/protocol/http2/frame.rb', line 71

def type
  @type
end

Class Method Details

.parse_header(buffer) ⇒ Object

Decodes common 9-byte header.



138
139
140
141
142
143
144
145
146
# File 'lib/protocol/http2/frame.rb', line 138

def self.parse_header(buffer)
	length_hi, length_lo, type, flags, stream_id = buffer.unpack(HEADER_FORMAT)
	length = (length_hi << LENGTH_HISHIFT) | length_lo
	stream_id = stream_id & STREAM_ID_MASK
	
	# puts "parse_header: length=#{length} type=#{type} flags=#{flags} stream_id=#{stream_id}"
	
	return length, type, flags, stream_id
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/protocol/http2/frame.rb', line 50

def <=> other
	to_ary <=> other.to_ary
end

#apply(connection) ⇒ Object



199
200
201
# File 'lib/protocol/http2/frame.rb', line 199

def apply(connection)
	connection.receive_frame(self)
end

#clear_flags(mask) ⇒ Object



93
94
95
# File 'lib/protocol/http2/frame.rb', line 93

def clear_flags(mask)
	@flags &= ~mask
end

#connection?Boolean

Check if frame is a connection frame: SETTINGS, PING, GOAWAY, and any frame addressed to stream ID = 0.

Returns:

  • (Boolean)


105
106
107
# File 'lib/protocol/http2/frame.rb', line 105

def connection?
	@stream_id.zero?
end

#flag_set?(mask) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/protocol/http2/frame.rb', line 97

def flag_set?(mask)
	@flags & mask != 0
end

#headerString

Generates common 9-byte frame header.

Returns:

  • (String)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/protocol/http2/frame.rb', line 116

def header
	unless VALID_LENGTH.include? @length
		raise ProtocolError, "Invalid frame length: #{@length.inspect}"
	end
	
	unless VALID_STREAM_ID.include? @stream_id
		raise ProtocolError, "Invalid stream identifier: #{@stream_id.inspect}"
	end
	
	[
		# These are guaranteed correct due to the length check above.
		@length >> LENGTH_HISHIFT,
		@length & LENGTH_LOMASK,
		@type,
		@flags,
		@stream_id
	].pack(HEADER_FORMAT)
end

#inspectObject



203
204
205
# File 'lib/protocol/http2/frame.rb', line 203

def inspect
	"\#<#{self.class} stream_id=#{@stream_id} flags=#{@flags} payload=#{self.unpack}>"
end

#pack(payload, maximum_size: nil) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/protocol/http2/frame.rb', line 80

def pack(payload, maximum_size: nil)
	@payload = payload
	@length = payload.bytesize
	
	if maximum_size and @length > maximum_size
		raise ProtocolError, "Frame length bigger than maximum allowed: #{@length} > #{maximum_size}"
	end
end

#read(stream, maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/protocol/http2/frame.rb', line 165

def read(stream, maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE)
	read_header(stream) unless @length
	
	if @length > maximum_frame_size
		raise FrameSizeError, "#{self.class} (type=#{@type}) frame length #{@length} exceeds maximum frame size #{maximum_frame_size}!"
	end
	
	read_payload(stream)
end

#read_header(stream) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/protocol/http2/frame.rb', line 148

def read_header(stream)
	if buffer = stream.read(9) and buffer.bytesize == 9
		@length, @type, @flags, @stream_id = Frame.parse_header(buffer)
		# puts "read_header: #{@length} #{@type} #{@flags} #{@stream_id}"
	else
		raise EOFError, "Could not read frame header!"
	end
end

#read_payload(stream) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/protocol/http2/frame.rb', line 157

def read_payload(stream)
	if payload = stream.read(@length) and payload.bytesize == @length
		@payload = payload
	else
		raise EOFError, "Could not read frame payload!"
	end
end

#set_flags(mask) ⇒ Object



89
90
91
# File 'lib/protocol/http2/frame.rb', line 89

def set_flags(mask)
	@flags |= mask
end

#to_aryObject



54
55
56
# File 'lib/protocol/http2/frame.rb', line 54

def to_ary
	[@length, @type, @flags, @stream_id, @payload]
end

#unpackObject



76
77
78
# File 'lib/protocol/http2/frame.rb', line 76

def unpack
	@payload
end

#valid_type?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/protocol/http2/frame.rb', line 46

def valid_type?
	@type == self.class::TYPE
end

#write(stream) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/protocol/http2/frame.rb', line 183

def write(stream)
	# Validate the payload size:
	if @payload.nil?
		if @length != 0
			raise ProtocolError, "Invalid frame length: #{@length} != 0"
		end
	else
		if @length != @payload.bytesize
			raise ProtocolError, "Invalid payload size: #{@length} != #{@payload.bytesize}"
		end
	end
	
	self.write_header(stream)
	self.write_payload(stream)
end

#write_header(stream) ⇒ Object



175
176
177
# File 'lib/protocol/http2/frame.rb', line 175

def write_header(stream)
	stream.write self.header
end

#write_payload(stream) ⇒ Object



179
180
181
# File 'lib/protocol/http2/frame.rb', line 179

def write_payload(stream)
	stream.write(@payload) if @payload
end