Class: Protocol::HTTP2::Frame
- Inherits:
-
Object
- Object
- Protocol::HTTP2::Frame
show all
- Includes:
- Comparable
- Defined in:
- lib/protocol/http2/frame.rb
Constant Summary
collapse
- VALID_STREAM_ID =
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
'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.
53
54
55
56
57
58
59
|
# File 'lib/protocol/http2/frame.rb', line 53
def initialize(stream_id = 0, flags = 0, type = self.class::TYPE, length = nil, payload = nil)
@length = length
@type = type
@flags = flags
@stream_id = stream_id
@payload = payload
end
|
Instance Attribute Details
#flags ⇒ Object
Returns the value of attribute flags.
87
88
89
|
# File 'lib/protocol/http2/frame.rb', line 87
def flags
@flags
end
|
#length ⇒ Object
The generic frame header uses the following binary representation:
-----------------------------------------------
| Length (24) | ---------------
—————---------------
| Type (8) | Flags (8) | -
————----------------
——————————-+ |R| Stream Identifier (31) | ==============================================================+ | Frame Payload (0…) … ---------------------------------------------------------------
85
86
87
|
# File 'lib/protocol/http2/frame.rb', line 85
def length
@length
end
|
#payload ⇒ Object
Returns the value of attribute payload.
89
90
91
|
# File 'lib/protocol/http2/frame.rb', line 89
def payload
@payload
end
|
#stream_id ⇒ Object
Returns the value of attribute stream_id.
88
89
90
|
# File 'lib/protocol/http2/frame.rb', line 88
def stream_id
@stream_id
end
|
#type ⇒ Object
Returns the value of attribute type.
86
87
88
|
# File 'lib/protocol/http2/frame.rb', line 86
def type
@type
end
|
Class Method Details
Decodes common 9-byte header.
153
154
155
156
157
158
159
160
161
|
# File 'lib/protocol/http2/frame.rb', line 153
def self.(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
return length, type, flags, stream_id
end
|
Instance Method Details
#<=>(other) ⇒ Object
65
66
67
|
# File 'lib/protocol/http2/frame.rb', line 65
def <=> other
to_ary <=> other.to_ary
end
|
#apply(connection) ⇒ Object
203
204
205
|
# File 'lib/protocol/http2/frame.rb', line 203
def apply(connection)
connection.receive_frame(self)
end
|
#clear_flags(mask) ⇒ Object
108
109
110
|
# File 'lib/protocol/http2/frame.rb', line 108
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.
120
121
122
|
# File 'lib/protocol/http2/frame.rb', line 120
def connection?
@stream_id.zero?
end
|
#flag_set?(mask) ⇒ Boolean
112
113
114
|
# File 'lib/protocol/http2/frame.rb', line 112
def flag_set?(mask)
@flags & mask != 0
end
|
Generates common 9-byte frame header.
#inspect ⇒ Object
207
208
209
|
# File 'lib/protocol/http2/frame.rb', line 207
def inspect
"\#<#{self.class} stream_id=#{@stream_id} flags=#{@flags} #{self.unpack}>"
end
|
#pack(payload, maximum_size: nil) ⇒ Object
95
96
97
98
99
100
101
102
|
# File 'lib/protocol/http2/frame.rb', line 95
def pack(payload, maximum_size: nil)
@payload = payload
@length = payload.bytesize
if maximum_size and @length > maximum_size
raise ProtocolError, "Frame length #{@length} bigger than maximum allowed: #{maximum_size}"
end
end
|
#read(stream, maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE) ⇒ Object
176
177
178
179
180
181
182
183
184
|
# File 'lib/protocol/http2/frame.rb', line 176
def read(stream, maximum_frame_size = MAXIMUM_ALLOWED_FRAME_SIZE)
(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
|
163
164
165
166
167
168
169
170
|
# File 'lib/protocol/http2/frame.rb', line 163
def (stream)
if buffer = stream.read(9)
@length, @type, @flags, @stream_id = Frame.(buffer)
else
raise EOFError, "Could not read frame header!"
end
end
|
#read_payload(stream) ⇒ Object
172
173
174
|
# File 'lib/protocol/http2/frame.rb', line 172
def read_payload(stream)
@payload = stream.read(@length)
end
|
#set_flags(mask) ⇒ Object
104
105
106
|
# File 'lib/protocol/http2/frame.rb', line 104
def set_flags(mask)
@flags |= mask
end
|
#to_ary ⇒ Object
69
70
71
|
# File 'lib/protocol/http2/frame.rb', line 69
def to_ary
[@length, @type, @flags, @stream_id, @payload]
end
|
#unpack ⇒ Object
91
92
93
|
# File 'lib/protocol/http2/frame.rb', line 91
def unpack
@payload
end
|
#valid_type? ⇒ Boolean
61
62
63
|
# File 'lib/protocol/http2/frame.rb', line 61
def valid_type?
@type == self.class::TYPE
end
|
#write(stream) ⇒ Object
194
195
196
197
198
199
200
201
|
# File 'lib/protocol/http2/frame.rb', line 194
def write(stream)
if @payload and @length != @payload.bytesize
raise ProtocolError, "Invalid payload size: #{@length} != #{@payload.bytesize}"
end
self.(stream)
self.write_payload(stream)
end
|
186
187
188
|
# File 'lib/protocol/http2/frame.rb', line 186
def (stream)
stream.write self.
end
|
#write_payload(stream) ⇒ Object
190
191
192
|
# File 'lib/protocol/http2/frame.rb', line 190
def write_payload(stream)
stream.write(@payload) if @payload
end
|