-
-
Save kenichi/cbc5745809966986cc87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'http-2', require: 'http/2' | |
gem 'reel', github: 'kenichi/reel', branch: 'http2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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