Class: Tipi::DefaultHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/tipi/handler.rb

Constant Summary collapse

ALPN_PROTOCOLS =
%w[h2 http/1.1].freeze
H2_PROTOCOL =
'h2'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DefaultHandler

Returns a new instance of DefaultHandler.



9
10
11
12
13
14
# File 'lib/tipi/handler.rb', line 9

def initialize(config)
  @config = config

  app_path = ARGV.first || './config.ru'
  @app = Tipi::RackAdapter.load(app_path)
end

Instance Method Details

#call(socket) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/tipi/handler.rb', line 16

def call(socket)
  socket.no_delay if socket.respond_to?(:no_delay)
  adapter = protocol_adapter(socket, {})
  adapter.each(&@app)
ensure
  socket.close
end

#protocol_adapter(socket, opts) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tipi/handler.rb', line 27

def protocol_adapter(socket, opts)
  use_http2 = socket.respond_to?(:alpn_protocol) &&
              socket.alpn_protocol == H2_PROTOCOL

  klass = use_http2 ? HTTP2Adapter : HTTP1Adapter
  klass.new(socket, opts)
end