Skip to content

Instantly share code, notes, and snippets.

@kenichi

kenichi/Gemfile Secret

Created July 6, 2015 23:01
Show Gist options
  • Save kenichi/cbc5745809966986cc87 to your computer and use it in GitHub Desktop.
Save kenichi/cbc5745809966986cc87 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'http-2', require: 'http/2'
gem 'reel', github: 'kenichi/reel', branch: 'http2'
#!/usr/bin/env ruby
require 'bundler'
Bundler.require
Reel::Connection::HTTP2.on :stream do |s|
res = [
"hi there.",
"how's it going?",
"yay!"
]
s[:stream].headers({
':status' => '200',
'content-length' => res.join(nil).bytesize.to_s,
'content-type' => 'text/plain',
}, end_stream: false)
s[:stream].data res[0], end_stream: false
Celluloid.sleep 3
s[:stream].data res[1], end_stream: false
Celluloid.sleep 1
s[:stream].data res[2]
end
module Reel
class Server
class HTTP2S < Server
def initialize(host, port, options={}, &callback)
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.cert = OpenSSL::X509::Certificate.new options.fetch(:cert)
ssl_context.key = OpenSSL::PKey::RSA.new options.fetch(:key)
ssl_context.npn_protocols = ['h2']
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
@tcpserver = Celluloid::IO::TCPServer.new(host, port)
server = Celluloid::IO::SSLServer.new(@tcpserver, ssl_context)
options.merge!(host: host, port: port)
options[:rescue] = [ OpenSSL::SSL::SSLError ]
super(server, options, &callback)
end
end
end
end
class R < Reel::Server::HTTP2S
def initialize
puts "listening 127.0.0.1:4567..."
super '127.0.0.1', 4567, {
cert: File.read('public.pem'),
key: File.read('private.pem')
}, &method(:on_connection)
end
def on_connection connection
connection.each_request do |request|
request.respond 200, ''
end
end
end
R.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment