Class: HTTP2::Framer
- Inherits:
-
Object
- Object
- HTTP2::Framer
- Includes:
- BufferUtils, Error, PackingExtensions
- Defined in:
- lib/http/2/framer.rb
Overview
Performs encoding, decoding, and validation of binary HTTP/2 frames.
Constant Summary collapse
- DEFAULT_MAX_FRAME_SIZE =
Default value of max frame size (16384 bytes)
2 << 13
- MAX_STREAM_ID =
Maximum stream ID (2^31)
0x7fffffff
- MAX_WINDOWINC =
Maximum window increment value (2^31)
0x7fffffff
- FRAME_TYPES =
HTTP/2 frame type mapping as defined by the spec
{ data: 0x0, headers: 0x1, priority: 0x2, rst_stream: 0x3, settings: 0x4, push_promise: 0x5, ping: 0x6, goaway: 0x7, window_update: 0x8, continuation: 0x9, altsvc: 0xa, origin: 0xc }.freeze
- FRAME_TYPES_WITH_PADDING =
%i[data headers push_promise].freeze
- FRAME_FLAGS =
Per frame flags as defined by the spec
{ data: { end_stream: 0, padded: 3, compressed: 5 }, headers: { end_stream: 0, end_headers: 2, padded: 3, priority: 5 }, priority: {}, rst_stream: {}, settings: { ack: 0 }, push_promise: { end_headers: 2, padded: 3 }, ping: { ack: 0 }, goaway: {}, window_update: {}, continuation: { end_headers: 2 }, altsvc: {}, origin: { reserved: 1, reserved2: 2, reserved3: 4, reserved4: 8 } }.each_value(&:freeze).freeze
- DEFINED_SETTINGS =
Default settings as defined by the spec
{ settings_header_table_size: 1, settings_enable_push: 2, settings_max_concurrent_streams: 3, settings_initial_window_size: 4, settings_max_frame_size: 5, settings_max_header_list_size: 6 }.freeze
- DEFINED_ERRORS =
Default error types as defined by the spec
{ no_error: 0, protocol_error: 1, internal_error: 2, flow_control_error: 3, settings_timeout: 4, stream_closed: 5, frame_size_error: 6, refused_stream: 7, cancel: 8, compression_error: 9, connect_error: 10, enhance_your_calm: 11, inadequate_security: 12, http_1_1_required: 13 }.freeze
- FRAME_LENGTH_HISHIFT =
16
- FRAME_LENGTH_LOMASK =
0xFFFF
Instance Attribute Summary collapse
-
#local_max_frame_size ⇒ Object
maximum frame size.
-
#remote_max_frame_size ⇒ Object
maximum frame size.
Instance Method Summary collapse
-
#common_header(frame, buffer:) ⇒ String
Generates common 9-byte frame header.
-
#generate(frame) ⇒ Object
Generates encoded HTTP/2 frame.
-
#initialize(local_max_frame_size = DEFAULT_MAX_FRAME_SIZE, remote_max_frame_size = DEFAULT_MAX_FRAME_SIZE) ⇒ Framer
constructor
Initializes new framer object.
-
#parse(buf) ⇒ Object
Decodes complete HTTP/2 frame from provided buffer.
-
#read_common_header(buf) ⇒ Hash
Decodes common 9-byte header.
Methods included from BufferUtils
#read_str, #read_uint32, #shift_byte
Methods included from PackingExtensions
Constructor Details
#initialize(local_max_frame_size = DEFAULT_MAX_FRAME_SIZE, remote_max_frame_size = DEFAULT_MAX_FRAME_SIZE) ⇒ Framer
Initializes new framer object.
116 117 118 119 120 |
# File 'lib/http/2/framer.rb', line 116 def initialize(local_max_frame_size = DEFAULT_MAX_FRAME_SIZE, remote_max_frame_size = DEFAULT_MAX_FRAME_SIZE) @local_max_frame_size = local_max_frame_size @remote_max_frame_size = remote_max_frame_size end |
Instance Attribute Details
#local_max_frame_size ⇒ Object
maximum frame size
15 16 17 |
# File 'lib/http/2/framer.rb', line 15 def local_max_frame_size @local_max_frame_size end |
#remote_max_frame_size ⇒ Object
maximum frame size
15 16 17 |
# File 'lib/http/2/framer.rb', line 15 def remote_max_frame_size @remote_max_frame_size end |
Instance Method Details
#common_header(frame, buffer:) ⇒ String
Generates common 9-byte frame header.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/http/2/framer.rb', line 128 def common_header(frame, buffer:) raise CompressionError, "Invalid frame type (#{frame[:type]})" unless FRAME_TYPES[frame[:type]] raise CompressionError, "Frame size is too large: #{frame[:length]}" if frame[:length] > @remote_max_frame_size raise CompressionError, "Frame size is invalid: #{frame[:length]}" if frame[:length] < 0 raise CompressionError, "Stream ID (#{frame[:stream]}) is too large" if frame[:stream] > MAX_STREAM_ID if frame[:type] == :window_update && frame[:increment] > MAX_WINDOWINC raise CompressionError, "Window increment (#{frame[:increment]}) is too large" end pack([ (frame[:length] >> FRAME_LENGTH_HISHIFT), (frame[:length] & FRAME_LENGTH_LOMASK), FRAME_TYPES[frame[:type]], frame[:flags].reduce(0) do |acc, f| position = FRAME_FLAGS[frame[:type]][f] raise CompressionError, "Invalid frame flag (#{f}) for #{frame[:type]}" unless position acc | (1 << position) end, frame[:stream] ], HEADERPACK, buffer: buffer, offset: 0) # 8+16,8,8,32 end |
#generate(frame) ⇒ Object
Generates encoded HTTP/2 frame.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/http/2/framer.rb', line 179 def generate(frame) bytes = "".b length = 0 frame[:flags] ||= [] frame[:stream] ||= 0 case frame[:type] when :data bytes << frame[:payload] bytes.force_encoding(Encoding::BINARY) length += frame[:payload].bytesize when :headers if frame[:weight] || frame[:dependency] || !frame[:exclusive].nil? unless frame[:weight] && frame[:dependency] && !frame[:exclusive].nil? raise CompressionError, "Must specify all of priority parameters for #{frame[:type]}" end frame[:flags] += [:priority] unless frame[:flags].include? :priority end if frame[:flags].include? :priority pack([(frame[:exclusive] ? EBIT : 0) | (frame[:dependency] & RBIT)], UINT32, buffer: bytes) pack([frame[:weight] - 1], UINT8, buffer: bytes) length += 5 end bytes << frame[:payload] length += frame[:payload].bytesize when :priority unless frame[:weight] && frame[:dependency] && !frame[:exclusive].nil? raise CompressionError, "Must specify all of priority parameters for #{frame[:type]}" end pack([(frame[:exclusive] ? EBIT : 0) | (frame[:dependency] & RBIT)], UINT32, buffer: bytes) pack([frame[:weight] - 1], UINT8, buffer: bytes) length += 5 when :rst_stream pack_error(frame[:error], buffer: bytes) length += 4 when :settings raise CompressionError, "Invalid stream ID (#{frame[:stream]})" if (frame[:stream]).nonzero? frame[:payload].each do |(k, v)| if k.is_a? Integer # rubocop:disable Style/GuardClause DEFINED_SETTINGS.value?(k) || next else k = DEFINED_SETTINGS[k] raise CompressionError, "Unknown settings ID for #{k}" if k.nil? end pack([k], UINT16, buffer: bytes) pack([v], UINT32, buffer: bytes) length += 6 end when :push_promise pack([frame[:promise_stream] & RBIT], UINT32, buffer: bytes) bytes << frame[:payload] length += 4 + frame[:payload].bytesize when :ping raise CompressionError, "Invalid payload size (#{frame[:payload].size} != 8 bytes)" if frame[:payload].bytesize != 8 bytes << frame[:payload] length += 8 when :goaway pack([frame[:last_stream] & RBIT], UINT32, buffer: bytes) pack_error(frame[:error], buffer: bytes) length += 8 if frame[:payload] bytes << frame[:payload] length += frame[:payload].bytesize end when :window_update pack([frame[:increment] & RBIT], UINT32, buffer: bytes) length += 4 when :continuation bytes << frame[:payload] length += frame[:payload].bytesize when :altsvc pack([frame[:max_age], frame[:port]], UINT32 + UINT16, buffer: bytes) length += 6 if frame[:proto] raise CompressionError, "Proto too long" if frame[:proto].bytesize > 255 pack([frame[:proto].bytesize], UINT8, buffer: bytes) bytes << frame[:proto] length += 1 + frame[:proto].bytesize else pack([0], UINT8, buffer: bytes) length += 1 end if frame[:host] raise CompressionError, "Host too long" if frame[:host].bytesize > 255 pack([frame[:host].bytesize], UINT8, buffer: bytes) bytes << frame[:host] length += 1 + frame[:host].bytesize else pack([0], UINT8, buffer: bytes) length += 1 end if frame[:origin] bytes << frame[:origin] length += frame[:origin].bytesize end when :origin frame[:payload].each do |origin| pack([origin.bytesize], UINT16, buffer: bytes) bytes << origin length += 2 + origin.bytesize end end # Process padding. # frame[:padding] gives number of extra octets to be added. # - http://tools.ietf.org/html/draft-ietf-httpbis-http2-16#section-6.1 if frame[:padding] unless FRAME_TYPES_WITH_PADDING.include?(frame[:type]) raise CompressionError, "Invalid padding flag for #{frame[:type]}" end padlen = frame[:padding] if padlen <= 0 || padlen > 256 || padlen + length > @remote_max_frame_size raise CompressionError, "Invalid padding #{padlen}" end length += padlen pack([padlen -= 1], UINT8, buffer: bytes, offset: 0) frame[:flags] << :padded # Padding: Padding octets that contain no application semantic value. # Padding octets MUST be set to zero when sending and ignored when # receiving. bytes << ("\0" * padlen) end frame[:length] = length common_header(frame, buffer: bytes) end |
#parse(buf) ⇒ Object
Decodes complete HTTP/2 frame from provided buffer. If the buffer does not contain enough data, no further work is performed.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/http/2/framer.rb', line 338 def parse(buf) return if buf.size < 9 frame = read_common_header(buf) return if buf.size < 9 + frame[:length] raise ProtocolError, "payload too large" if frame[:length] > @local_max_frame_size read_str(buf, 9) payload = read_str(buf, frame[:length]) # Implementations MUST discard frames # that have unknown or unsupported types. # - http://tools.ietf.org/html/draft-ietf-httpbis-http2-16#section-5.5 return frame if frame[:type].nil? # Process padding padlen = 0 if FRAME_TYPES_WITH_PADDING.include?(frame[:type]) padded = frame[:flags].include?(:padded) if padded padlen = read_str(payload, 1).unpack1(UINT8) frame[:padding] = padlen + 1 raise ProtocolError, "padding too long" if padlen > payload.bytesize payload = payload.byteslice(0, payload.bytesize - padlen) if padlen > 0 frame[:length] -= frame[:padding] frame[:flags].delete(:padded) end end case frame[:type] when :data, :ping, :continuation frame[:payload] = read_str(payload, frame[:length]) when :headers if frame[:flags].include? :priority e_sd = read_uint32(payload) frame[:dependency] = e_sd & RBIT frame[:exclusive] = e_sd.anybits?(EBIT) weight = payload.byteslice(0, 1).ord + 1 frame[:weight] = weight payload = payload.byteslice(1..-1) end frame[:payload] = read_str(payload, frame[:length]) when :priority raise FrameSizeError, "Invalid length for PRIORITY_STREAM (#{frame[:length]} != 5)" if frame[:length] != 5 e_sd = read_uint32(payload) frame[:dependency] = e_sd & RBIT frame[:exclusive] = e_sd.anybits?(EBIT) weight = payload.byteslice(0, 1).ord + 1 frame[:weight] = weight payload = payload.byteslice(1..-1) when :rst_stream raise FrameSizeError, "Invalid length for RST_STREAM (#{frame[:length]} != 4)" if frame[:length] != 4 frame[:error] = unpack_error read_uint32(payload) when :settings # NOTE: frame[:length] might not match the number of frame[:payload] # because unknown extensions are ignored. frame[:payload] = [] raise ProtocolError, "Invalid settings payload length" unless (frame[:length] % 6).zero? raise ProtocolError, "Invalid stream ID (#{frame[:stream]})" if (frame[:stream]).nonzero? (frame[:length] / 6).times do id = read_str(payload, 2).unpack1(UINT16) val = read_uint32(payload) # Unsupported or unrecognized settings MUST be ignored. # Here we send it along. name, = DEFINED_SETTINGS.find { |_name, v| v == id } frame[:payload] << [name, val] if name end when :push_promise frame[:promise_stream] = read_uint32(payload) & RBIT frame[:payload] = read_str(payload, frame[:length]) when :goaway frame[:last_stream] = read_uint32(payload) & RBIT frame[:error] = unpack_error read_uint32(payload) size = frame[:length] - 8 # for last_stream and error frame[:payload] = read_str(payload, size) if size > 0 when :window_update if frame[:length] % 4 != 0 raise FrameSizeError, "Invalid length for WINDOW_UPDATE (#{frame[:length]} not multiple of 4)" end frame[:increment] = read_uint32(payload) & RBIT when :altsvc frame[:max_age], frame[:port] = read_str(payload, 6).unpack(UINT32 + UINT16) len = payload.byteslice(0, 1).ord payload = payload.byteslice(1..-1) frame[:proto] = read_str(payload, len) if len > 0 len = payload.byteslice(0, 1).ord payload = payload.byteslice(1..-1) frame[:host] = read_str(payload, len) if len > 0 frame[:origin] = read_str(payload, payload.size) unless payload.empty? when :origin origins = [] until payload.empty? len = read_str(payload, 2).unpack1(UINT16) origins << read_str(payload, len) end frame[:payload] = origins # else # Unknown frame type is explicitly allowed end frame end |
#read_common_header(buf) ⇒ Hash
Decodes common 9-byte header.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/http/2/framer.rb', line 159 def read_common_header(buf) frame = {} len_hi, len_lo, type, flags, stream = buf.byteslice(0, 9).unpack(HEADERPACK) frame[:length] = (len_hi << FRAME_LENGTH_HISHIFT) | len_lo frame[:type], = FRAME_TYPES.find { |_t, pos| type == pos } if frame[:type] frame[:flags] = FRAME_FLAGS[frame[:type]].each_with_object([]) do |(name, pos), acc| acc << name if flags.anybits?((1 << pos)) end end frame[:stream] = stream & RBIT frame end |