Class: Baykit::BayServer::Docker::Http::H2::H2PacketUnPacker

Inherits:
Protocol::PacketUnPacker
  • Object
show all
Includes:
Agent, Baykit::BayServer::Docker::Http, Protocol, Util
Defined in:
lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb

Defined Under Namespace

Classes: FrameHeaderItem

Constant Summary collapse

STATE_READ_LENGTH =
1
STATE_READ_TYPE =
2
STATE_READ_FLAGS =
3
STATE_READ_STREAM_IDENTIFIER =
4
STATE_READ_FLAME_PAYLOAD =
5
STATE_END =
6
FRAME_LEN_LENGTH =
3
FRAME_LEN_TYPE =
1
FRAME_LEN_FLAGS =
1
FRAME_LEN_STREAM_IDENTIFIER =
4
FLAGS_END_STREAM =
0x1
FLAGS_END_HEADERS =
0x4
FLAGS_PADDED =
0x8
FLAGS_PRIORITY =
0x20
CONNECTION_PREFACE =
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_unpacker, pkt_store, server_mode) ⇒ H2PacketUnPacker

Returns a new instance of H2PacketUnPacker.



70
71
72
73
74
75
76
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 70

def initialize(cmd_unpacker, pkt_store, server_mode)
  @cmd_unpacker = cmd_unpacker
  @pkt_store = pkt_store
  @server_mode = server_mode
  @tmp_buf = SimpleBuffer.new
  reset
end

Instance Attribute Details

#cmd_unpackerObject (readonly)

Returns the value of attribute cmd_unpacker.



62
63
64
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 62

def cmd_unpacker
  @cmd_unpacker
end

#cont_lenObject (readonly)

Returns the value of attribute cont_len.



66
67
68
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 66

def cont_len
  @cont_len
end

#flagsObject (readonly)

Returns the value of attribute flags.



59
60
61
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 59

def flags
  @flags
end

#itemObject (readonly)

Returns the value of attribute item.



55
56
57
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 55

def item
  @item
end

#payload_lenObject (readonly)

Returns the value of attribute payload_len.



58
59
60
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 58

def payload_len
  @payload_len
end

#pkt_storeObject (readonly)

Returns the value of attribute pkt_store.



63
64
65
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 63

def pkt_store
  @pkt_store
end

#posObject (readonly)

Returns the value of attribute pos.



68
69
70
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 68

def pos
  @pos
end

#preface_readObject (readonly)

Returns the value of attribute preface_read.



56
57
58
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 56

def preface_read
  @preface_read
end

#read_bytesObject (readonly)

Returns the value of attribute read_bytes.



67
68
69
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 67

def read_bytes
  @read_bytes
end

#server_modeObject (readonly)

Returns the value of attribute server_mode.



64
65
66
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 64

def server_mode
  @server_mode
end

#stateObject (readonly)

Returns the value of attribute state.



53
54
55
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 53

def state
  @state
end

#stream_idObject (readonly)

Returns the value of attribute stream_id.



60
61
62
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 60

def stream_id
  @stream_id
end

#tmp_bufObject (readonly)

Returns the value of attribute tmp_buf.



54
55
56
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 54

def tmp_buf
  @tmp_buf
end

#typeObject (readonly)

Returns the value of attribute type.



57
58
59
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 57

def type
  @type
end

Instance Method Details

#bytes_received(buf) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 95

def bytes_received(buf)
  suspend = false

  @pos = 0
  if @server_mode && !@preface_read
    len = CONNECTION_PREFACE.length - @tmp_buf.length
    if len > buf.length
      len = buf.length
    end
    @tmp_buf.put(buf, @pos, len)
    @pos += len
    if @tmp_buf.length == CONNECTION_PREFACE.length
      @tmp_buf.length.times do |i|
        if CONNECTION_PREFACE[i] != @tmp_buf.buf[i]
          raise ProtocolException.new "Invalid connection preface: #{@tmp_buf.bytes[0, @tmp_buf.length]}"
        end
      end
      pkt = @pkt_store.rent(H2Type::PREFACE)
      pkt.new_data_accessor().put_bytes(@tmp_buf.buf, 0, @tmp_buf.length)
      nstat = @cmd_unpacker.packet_received(pkt)
      @pkt_store.Return(pkt)
      if nstat != NextSocketAction::CONTINUE
        return nstat
      end

      BayLog.debug("h2: Connection preface OK")
      @preface_read = true
      @tmp_buf.reset()
    end
  end

  while @state != STATE_END && pos < buf.length
    case @state
    when STATE_READ_LENGTH
      if read_header_item(buf)
        @payload_len = ((@item.get(@tmp_buf, 0) & 0xFF) << 16 |
                        (@item.get(@tmp_buf, 1) & 0xFF) << 8 |
                        (@item.get(@tmp_buf, 2) & 0xFF))
        @item = FrameHeaderItem.new(@tmp_buf.length, FRAME_LEN_TYPE)
        change_state STATE_READ_TYPE
      end

    when STATE_READ_TYPE
      if read_header_item(buf)
        @type = @item.get(@tmp_buf, 0)
        @item = FrameHeaderItem.new(@tmp_buf.length, FRAME_LEN_FLAGS)
        change_state STATE_READ_FLAGS
      end

    when STATE_READ_FLAGS
      if read_header_item(buf)
        @flags = @item.get(@tmp_buf, 0)
        @item = FrameHeaderItem.new(@tmp_buf.length, FRAME_LEN_STREAM_IDENTIFIER)
        change_state STATE_READ_STREAM_IDENTIFIER
      end

    when STATE_READ_STREAM_IDENTIFIER
      if read_header_item(buf)
        @stream_id =
          ((@item.get(@tmp_buf, 0) & 0x7F) << 24) |
            (@item.get(@tmp_buf, 1) << 16) |
            (@item.get(@tmp_buf, 2) << 8) |
            @item.get(@tmp_buf, 3)

        @item = FrameHeaderItem.new(@tmp_buf.length, @payload_len)
        change_state STATE_READ_FLAME_PAYLOAD
      end

    when STATE_READ_FLAME_PAYLOAD
      if read_header_item(buf)
        change_state STATE_END
      end

    else
      raise RuntimeError.new "Illegal State"

    end

    if @state == STATE_END
      pkt = @pkt_store.rent(@type)
      pkt.stream_id = @stream_id
      pkt.flags = H2Flags.new(@flags)
      pkt.new_header_accessor().put_bytes(@tmp_buf.buf, 0, H2Packet::FRAME_HEADER_LEN)
      pkt.new_data_accessor().put_bytes(@tmp_buf.buf, H2Packet::FRAME_HEADER_LEN, @tmp_buf.length - H2Packet::FRAME_HEADER_LEN)

      begin
        next_act = @cmd_unpacker.packet_received(pkt)
      ensure
        @pkt_store.Return(pkt)
        reset_state()
      end

      if next_act == NextSocketAction::SUSPEND
        suspend = true
      elsif next_act != NextSocketAction::CONTINUE
        return next_act
      end
    end
  end

  if suspend
    return NextSocketAction::SUSPEND
  else
    return NextSocketAction::CONTINUE
  end
end

#resetObject



78
79
80
81
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 78

def reset()
  reset_state()
  @preface_read = false
end

#reset_stateObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/baykit/bayserver/docker/http/h2/h2_packet_unpacker.rb', line 83

def reset_state()
  change_state STATE_READ_LENGTH
  @item = FrameHeaderItem.new(0, FRAME_LEN_LENGTH)
  @cont_len = 0
  @read_bytes = 0
  @tmp_buf.reset
  @type = nil
  @flags = 0
  @stream_id = 0
  @payload_len = 0
end