module Polyphony::HTTP::Server

Constants

ALPN_PROTOCOLS
H2_PROTOCOL

Public Class Methods

accept_loop(server, opts, &handler) click to toggle source
# File lib/polyphony/http/server.rb, line 31
def accept_loop(server, opts, &handler)
  loop do
    client = server.accept
    spin { client_loop(client, opts, &handler) }
    snooze
  rescue OpenSSL::SSL::SSLError
    # disregard
  end
end
client_loop(client, opts, &handler) click to toggle source
# File lib/polyphony/http/server.rb, line 41
def client_loop(client, opts, &handler)
  client.no_delay if client.respond_to?(:no_delay)
  adapter = protocol_adapter(client, opts)
  adapter.each(&handler)
ensure
  client.close
end
listen(host, port, opts = {}) click to toggle source
# File lib/polyphony/http/server.rb, line 22
def listen(host, port, opts = {})
  opts[:alpn_protocols] = ALPN_PROTOCOLS
  Net.tcp_listen(host, port, opts).tap do |socket|
    socket.define_singleton_method(:each) do |&block|
      ::Polyphony::HTTP::Server.accept_loop(socket, opts, &block)
    end
  end
end
protocol_adapter(socket, opts) click to toggle source
# File lib/polyphony/http/server.rb, line 49
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
serve(host, port, opts = {}, &handler) click to toggle source
# File lib/polyphony/http/server.rb, line 14
def serve(host, port, opts = {}, &handler)
  opts[:alpn_protocols] = ALPN_PROTOCOLS
  server = Net.tcp_listen(host, port, opts)
  accept_loop(server, opts, &handler)
ensure
  server.close
end