Class: Baykit::BayServer::Docker::Http::H2::H2InboundHandler

Inherits:
Object
  • Object
show all
Includes:
Agent, Common::InboundHandler, Command, H2Handler, Protocol, Tours, Util
Defined in:
lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb

Defined Under Namespace

Classes: InboundProtocolHandlerFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeH2InboundHandler

Returns a new instance of H2InboundHandler.



64
65
66
67
68
69
70
71
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 64

def initialize
  @window_size = BayServer.harbor.tour_buffer_size
  @settings = H2Settings.new
  @analyzer = HeaderBlockAnalyzer.new
  @req_header_tbl = HeaderTable.create_dynamic_table()
  @res_header_tbl = HeaderTable.create_dynamic_table()
  @header_buffer = SimpleBuffer.new
end

Instance Attribute Details

#analyzerObject (readonly)

Returns the value of attribute analyzer.



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

def analyzer
  @analyzer
end

#header_bufferObject (readonly)

Returns the value of attribute header_buffer.



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

def header_buffer
  @header_buffer
end

#header_readObject (readonly)

Returns the value of attribute header_read.



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

def header_read
  @header_read
end

#http_protocolObject (readonly)

Returns the value of attribute http_protocol.



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

def http_protocol
  @http_protocol
end

#protocol_handlerObject (readonly)

Returns the value of attribute protocol_handler.



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

def protocol_handler
  @protocol_handler
end

#req_cont_lenObject (readonly)

Returns the value of attribute req_cont_len.



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

def req_cont_len
  @req_cont_len
end

#req_cont_readObject (readonly)

Returns the value of attribute req_cont_read.



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

def req_cont_read
  @req_cont_read
end

#req_header_tblObject (readonly)

Returns the value of attribute req_header_tbl.



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

def req_header_tbl
  @req_header_tbl
end

#res_header_tblObject (readonly)

Returns the value of attribute res_header_tbl.



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

def res_header_tbl
  @res_header_tbl
end

#settingsObject (readonly)

Returns the value of attribute settings.



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

def settings
  @settings
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



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

def window_size
  @window_size
end

Instance Method Details

#handle_continuation(cmd) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 373

def handle_continuation(cmd)
  BayLog.debug("%s handle_continuation: stm=%d", ship, cmd.stream_id)
  tur = get_tour(cmd.stream_id)
  if tur == nil
    raise ArgumentError("Invalid stream id: " + cmd.stream_id)
  end

  @header_buffer.put(cmd.data, cmd.start, cmd.length)
  if cmd.flags.end_headers?
    return on_end_header(tur, @header_buffer.bytes, 0, @header_buffer.length)
  end

  return NextSocketAction::CONTINUE
end

#handle_data(cmd) ⇒ Object



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
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 231

def handle_data(cmd)
  BayLog.debug("%s handle_data: stm=%d len=%d", ship, cmd.stream_id, cmd.length)

  tur = get_tour(cmd.stream_id)
  if tur == nil
    raise RuntimeError.new("Invalid stream id: #{cmd.stream_id}")
  end
  if !tur.reading?
    raise ProtocolException.new("%s Tour is not reading.", tur)
  end

  begin
    success = false
    if cmd.length > 0
      tid = tur.tour_id

      success = tur.req.post_req_content(Tour::TOUR_ID_NOCHECK, cmd.data, cmd.start, cmd.length) do |len, resume|
        tur.check_tour_id(tid)
        if len > 0
          upd = CmdWindowUpdate.new(cmd.stream_id)
          upd.window_size_increment = len
          upd2 = CmdWindowUpdate.new( 0)
          upd2.window_size_increment = len
          begin
            @protocol_handler.post(upd)
            @protocol_handler.post(upd2)
          rescue IOError => e
            BayLog.error_e(e)
          end
        end

        if resume
          tur.ship.resume_read(Ship::SHIP_ID_NOCHECK)
        end
      end

      if tur.req.bytes_posted >= tur.req.headers.content_length
        if tur.error != nil
          # Error has occurred on header completed
          BayLog.debug("%s Delay report error", tur)
          raise tur.error
        else
          end_req_content(tur.id(), tur)
        end
      end
    end

    if !success
      return NextSocketAction::SUSPEND
    else
      return NextSocketAction::CONTINUE
    end

  rescue HttpException => e
    tur.req.abort
    tur.res.send_http_exception(Tour::TOUR_ID_NOCHECK, e)
    return NextSocketAction::CONTINUE
  end

end

#handle_go_away(cmd) ⇒ Object



353
354
355
356
357
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 353

def handle_go_away(cmd)
  BayLog.debug("%s received GoAway: lastStm=%d code=%d desc=%s debug=%s",
               ship, cmd.last_stream_id, cmd.error_code, H2ErrorCode.msg.get(cmd.error_code.to_s.to_sym), cmd.debug_data);
  return NextSocketAction::CLOSE
end

#handle_headers(cmd) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 208

def handle_headers(cmd)
  BayLog.debug("%s handle_headers: stm=%d dep=%d weight=%d", ship, cmd.stream_id, cmd.stream_dependency, cmd.weight)

  tur = get_tour(cmd.stream_id)
  if tur == nil
    BayLog.error(BayMessage.get(:INT_NO_MORE_TOURS))
    tur = ship.get_tour(cmd.stream_id, true)
    tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus::SERVICE_UNAVAILABLE, "No available tours")
    return NextSocketAction::CONTINUE
  end
  if !tur.preparing?
    raise ProtocolException.new("%s Tour is not preparing.", tur)
  end

  if cmd.flags.end_headers?
    return on_end_header(tur, cmd.data, cmd.start, cmd.length)
  else
    @header_buffer.put(cmd.data, cmd.start, cmd.length)
  end

  NextSocketAction::CONTINUE
end

#handle_ping(cmd) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 359

def handle_ping(cmd)
  BayLog.debug("%s handle_ping: stm=%d", ship, cmd.stream_id)

  res = CmdPing.new(cmd.stream_id, H2Flags.new(H2Flags::FLAGS_ACK), cmd.opaque_data)
  @protocol_handler.post(res)
  return NextSocketAction::CONTINUE
end

#handle_preface(cmd) ⇒ Object

implements H2CommandHandler



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 189

def handle_preface(cmd)
  BayLog.debug("%s h2: handle_preface: proto=%s", ship, cmd.protocol)

  @http_protocol = cmd.protocol

  set = CmdSettings.new(H2ProtocolHandler::CTL_STREAM_ID)
  set.stream_id = 0
  set.items.append(CmdSettings::Item.new(CmdSettings::MAX_CONCURRENT_STREAMS, TourStore::MAX_TOURS))
  set.items.append(CmdSettings::Item.new(CmdSettings::INITIAL_WINDOW_SIZE, @window_size))
  @protocol_handler.post(set)

  set = CmdSettings.new(H2ProtocolHandler::CTL_STREAM_ID)
  set.stream_id = 0
  set.flags.set_ack(true)

  return NextSocketAction::CONTINUE
end

#handle_priority(cmd) ⇒ Object



292
293
294
295
296
297
298
299
300
301
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 292

def handle_priority(cmd)
  if cmd.stream_id == 0
    raise ProtocolException.new("Invalid stream id")
  end

  BayLog.debug("%s handlePriority: stmid=%d dep=%d, wgt=%d",
               ship, cmd.stream_id, cmd.stream_dependency, cmd.weight);

  return NextSocketAction::CONTINUE
end

#handle_rst_stream(cmd) ⇒ Object



367
368
369
370
371
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 367

def handle_rst_stream(cmd)
  BayLog.warn("%s received RstStream: stmid=%d code=%d desc=%s",
               ship, cmd.stream_id, cmd.error_code, H2ErrorCode.msg.get(cmd.error_code.to_s.to_sym))
  return NextSocketAction::CONTINUE
end

#handle_settings(cmd) ⇒ Object



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
333
334
335
336
337
338
339
340
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 303

def handle_settings(cmd)
  BayLog.debug("%s handleSettings: stmid=%d", ship, cmd.stream_id);

  if cmd.flags.ack?
    return NextSocketAction::CONTINUE
  end

  cmd.items.each do |item|
    BayLog.debug("%s handle: Setting id=%d, value=%d", ship, item.id, item.value);
    case item.id
    when CmdSettings::HEADER_TABLE_SIZE
      @settings.header_table_size = item.value

    when CmdSettings::ENABLE_PUSH
      @settings.enable_push = (item.value != 0)

    when CmdSettings::MAX_CONCURRENT_STREAMS
      @settings.max_concurrent_streams = item.value

    when CmdSettings::INITIAL_WINDOW_SIZE
      @settings.initial_window_size = item.value

    when CmdSettings::MAX_FRAME_SIZE
      @settings.max_frame_size = item.value

    when CmdSettings::MAX_HEADER_LIST_SIZE
      @settings.max_header_list_size = item.value

    else
      BayLog.debug("Invalid settings id (Ignore): %d", item.id)

    end
  end

  res = CmdSettings.new(0, H2Flags.new(H2Flags::FLAGS_ACK))
  @protocol_handler.post(res)
  return NextSocketAction::CONTINUE
end

#handle_window_update(cmd) ⇒ Object



342
343
344
345
346
347
348
349
350
351
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 342

def handle_window_update(cmd)
  BayLog.debug("%s handleWindowUpdate: stmid=%d siz=%d", ship,  cmd.stream_id, cmd.window_size_increment);

  if cmd.window_size_increment == 0
    raise ProtocolException.new("Invalid increment value")
  end

  window_size = cmd.window_size_increment
  return NextSocketAction::CONTINUE
end

#init(proto_handler) ⇒ Object



84
85
86
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 84

def init(proto_handler)
  @protocol_handler = proto_handler
end

#on_protocol_error(err) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 168

def on_protocol_error(err)
  BayLog.error_e err
  cmd = CmdGoAway.new(H2ProtocolHandler::CTL_STREAM_ID)
  cmd.stream_id = H2ProtocolHandler::CTL_STREAM_ID
  cmd.last_stream_id = H2ProtocolHandler::CTL_STREAM_ID
  cmd.error_code = H2ErrorCode::PROTOCOL_ERROR
  cmd.debug_data = "Thank you!"
  begin
    @protocol_handler.post(cmd)
    @protocol_handler.ship.post_close()
  rescue IOError => e
    BayLog.error_e(e)
  end
  return false
end

#resetObject

implements Reusable



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

def reset()
  @header_read = false
  @req_cont_len = 0
  @req_cont_read = 0
  @header_buffer.reset
end

#send_end_tour(tur, &callback) ⇒ Object



161
162
163
164
165
166
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 161

def send_end_tour(tur, &callback)
  BayLog.debug("%s send_end_tour. tur=%s", self, tur)
  cmd = CmdData.new(tur.req.key, nil, [], 0, 0)
  cmd.flags.set_end_stream(true)
  @protocol_handler.post(cmd, &callback)
end

#send_res_content(tur, bytes, ofs, len, &callback) ⇒ Object



155
156
157
158
159
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 155

def send_res_content(tur, bytes, ofs, len, &callback)
  BayLog.debug("%s send_res_content len=%d", self, len)
  cmd = CmdData.new(tur.req.key, nil, bytes, ofs, len);
  @protocol_handler.post(cmd, &callback)
end

#send_res_headers(tur) ⇒ Object

implements InboundHandler



92
93
94
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
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 92

def send_res_headers(tur)
  bld = HeaderBlockBuilder.new()

  header_blocks = []

  blk = bld.build_header_block(":status", tur.res.headers.status.to_s, @res_header_tbl)
  header_blocks << blk

  # headers
  if BayServer.harbor.trace_header
    BayLog.info("%s res status: %d", tur, tur.res.headers.status)
  end
  tur.res.headers.names.each do |name|
    if name.casecmp?("connection")
      BayLog.trace("%s Connection header is discarded", tur)
    else
      tur.res.headers.values(name).each do |value|
        if BayServer.harbor.trace_header
          BayLog.info("%s H2 res header: %s=%s", tur, name, value)
        end
        blk = bld.build_header_block(name, value, @res_header_tbl)
        header_blocks.append(blk)
      end
    end
  end

  buf = SimpleBuffer.new
  HeaderBlockRenderer.new(buf).render_header_blocks(header_blocks)

  pos = 0
  len = buf.length
  while len > 0
    chunk_len = [len, H2Packet::DEFAULT_PAYLOAD_MAXLEN].min

    if pos == 0
      hcmd = CmdHeaders.new(tur.req.key)
      hcmd.excluded = false
      hcmd.data = buf.bytes
      hcmd.start = pos
      hcmd.length = len
      cmd = hcmd

    else
      ccmd = CmdContinuation.new(tur.req.key)
      ccmd.data = buf.bytes
      ccmd.start = pos
      ccmd.length = len
      cmd = ccmd

    end

    cmd.flags.set_padded(false)

    pos += chunk_len
    len -= chunk_len
    if len == 0
        cmd.flags.set_end_headers(true)
    end

    @protocol_handler.post(cmd)
  end
end