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

Inherits:
Object
  • Object
show all
Includes:
Agent, Common::InboundHandler, Command, H2Handler, Protocol, Tours, Util, WaterCraft
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
# 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()
end

Instance Attribute Details

#analyzerObject (readonly)

Returns the value of attribute analyzer.



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

def analyzer
  @analyzer
end

#header_readObject (readonly)

Returns the value of attribute header_read.



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

def header_read
  @header_read
end

#http_protocolObject (readonly)

Returns the value of attribute http_protocol.



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

def http_protocol
  @http_protocol
end

#protocol_handlerObject (readonly)

Returns the value of attribute protocol_handler.



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

def protocol_handler
  @protocol_handler
end

#req_cont_lenObject (readonly)

Returns the value of attribute req_cont_len.



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

def req_cont_len
  @req_cont_len
end

#req_cont_readObject (readonly)

Returns the value of attribute req_cont_read.



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

def req_cont_read
  @req_cont_read
end

#req_header_tblObject (readonly)

Returns the value of attribute req_header_tbl.



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

def req_header_tbl
  @req_header_tbl
end

#res_header_tblObject (readonly)

Returns the value of attribute res_header_tbl.



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

def res_header_tbl
  @res_header_tbl
end

#settingsObject (readonly)

Returns the value of attribute settings.



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

def settings
  @settings
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



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

def window_size
  @window_size
end

Instance Method Details

#handle_data(cmd) ⇒ Object



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

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.req.headers.content_length <= 0
    raise ProtocolException.new("Post content not allowed")
  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



374
375
376
377
378
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 374

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



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

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

  cmd.header_blocks.each do |blk|
    if blk.op == HeaderBlock::UPDATE_DYNAMIC_TABLE_SIZE
      BayLog.trace("%s header block update table size: %d", tur, blk.size)
      @req_header_tbl.set_size(blk.size)
      next
    end
    @analyzer.analyze_header_block(blk, @req_header_tbl)
    if BayServer.harbor.trace_header
      BayLog.info("%s req header: %s=%s :%s", tur, @analyzer.name, @analyzer.value, blk);
    end

    if @analyzer.name == nil
      next

    elsif @analyzer.name[0] != ":"
      tur.req.headers.add(@analyzer.name, @analyzer.value)

    elsif @analyzer.method != nil
      tur.req.method = @analyzer.method

    elsif @analyzer.path != nil
      tur.req.uri = @analyzer.path

    elsif @analyzer.scheme != nil

    elsif @analyzer.status != nil
      raise RuntimeError.new("Illegal State")
    end
  end

  if cmd.flags.end_headers?
    tur.req.protocol = "HTTP/2.0"
    BayLog.debug("%s H2 read header method=%s protocol=%s uri=%s contlen=%d",
                 ship, tur.req.method, tur.req.protocol, tur.req.uri, tur.req.headers.content_length)

    req_cont_len = tur.req.headers.content_length()

    if req_cont_len > 0
      tur.req.set_limit(req_cont_len)
    end

    begin
      start_tour tur

      if tur.req.headers.content_length <= 0
        end_req_content(Tour::TOUR_ID_NOCHECK, tur)
      end
    rescue HttpException => e
      BayLog.debug("%s Http error occurred: %s", self, e);
      if req_cont_len <= 0
        # no post data
        tur.req.abort
        tur.res.send_http_exception(Tour::TOUR_ID_NOCHECK, e)

        return NextSocketAction::CONTINUE
      else
        # Delay send
        tur.error = e
        tur.req.set_content_handler(ReqContentHandler::DEV_NULL)
        return NextSocketAction::CONTINUE
      end
    end

  end

  NextSocketAction::CONTINUE
end

#handle_ping(cmd) ⇒ Object



380
381
382
383
384
385
386
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 380

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 154

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



313
314
315
316
317
318
319
320
321
322
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 313

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



388
389
390
391
392
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 388

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



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 324

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



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

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



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

def init(proto_handler)
  @protocol_handler = proto_handler
end

#on_protocol_error(err) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 133

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

#resetObject

implements Reusable



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

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

#send_end_tour(tur, keep_alive, &callback) ⇒ Object



126
127
128
129
130
131
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 126

def send_end_tour(tur, keep_alive, &callback)
  BayLog.debug("%s send_end_tour. tur=%s keep=%s", self, tur, keep_alive)
  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



120
121
122
123
124
# File 'lib/baykit/bayserver/docker/http/h2/h2_inbound_handler.rb', line 120

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



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

def send_res_headers(tur)
  cmd = CmdHeaders.new(tur.req.key)
  bld = HeaderBlockBuilder.new()
  blk = bld.build_header_block(":status", tur.res.headers.status.to_s, @res_header_tbl)
  cmd.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)
        cmd.header_blocks.append(blk)
      end
    end
  end

  cmd.flags.set_end_headers(true)
  cmd.excluded = true
  cmd.flags.set_padded(false)
  @protocol_handler.post(cmd)
end