aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/frame_utils.rb
blob: 69ae3d2a42879ee86cf65c9b4c391c27ebc5d82b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Plum::BinaryString

module Plum
  module FrameUtils
    # Splits the DATA frame into multiple frames if the payload size exceeds max size.
    #
    # @param max [Integer] The maximum size of a frame payload.
    # @return [Array<Frame>] The splitted frames.
    def split_data(max)
      return [self] if self.length <= max
      raise "Frame type must be DATA" unless self.type == :data

      fragments = self.payload.each_byteslice(max).to_a
      frames = fragments.map {|fragment| Frame.new(type: :data, flags: [], stream_id: self.stream_id, payload: fragment) }
      frames.first.flags = self.flags - [:end_stream]
      frames.last.flags = self.flags & [:end_stream]
      frames
    end

    # Splits the HEADERS or PUSH_PROMISE frame into multiple frames if the payload size exceeds max size.
    #
    # @param max [Integer] The maximum size of a frame payload.
    # @return [Array<Frame>] The splitted frames.
    def split_headers(max)
      return [self] if self.length <= max
      raise "Frame type must be HEADERS or PUSH_PROMISE" unless [:headers, :push_promise].include?(self.type)

      fragments = self.payload.each_byteslice(max).to_a
      frames = fragments.map {|fragment| Frame.new(type: :continuation, flags: [], stream_id: self.stream_id, payload: fragment) }
      frames.first.type_value = self.type_value
      frames.first.flags = self.flags - [:end_headers]
      frames.last.flags = self.flags & [:end_headers]
      frames
    end

    # Parses SETTINGS frame payload. Ignores unknown settings type (see RFC7540 6.5.2).
    #
    # @return [Hash<Symbol, Integer>] The parsed strings.
    def parse_settings
      settings = {}
      payload.each_byteslice(6) do |param|
        id = param.uint16
        name = Frame::SETTINGS_TYPE.key(id)
        # ignore unknown settings type
        settings[name] = param.uint32(2) if name
      end
      settings
    end
  end
end