matsumotory / mruby-http2

HTTP2 Module for mruby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP2 Module for mruby

Build Status Gitter

HTTP2 module for mruby using nghttp2. You can access Web site using HTTP/2 protocol from mruby applications or devices with mruby, and run HTTP/2 Web server easily.

HTTP/2 is a replacement for how HTTP is expressed "on the wire". It is not a ground-up rewrite of the protocol; HTTP methods, status codes and semantics will be the same, and it should be possible to use the same APIs as HTTP/1.x (possibly with some small additions) to represent the protocol.

Trusterd is a HTTP/2 Web Server implementation using mruby-http2.

Benchmark

Please see link.

Development Environment

Requrie: vagrant

vagrant up
vagrant ssh
cd mruby-http2
rake test

Install by mrbgems

Download mruby

git clone https://github.com/mruby/mruby.git

Add conf.gem line to mruby/build_config.rb

MRuby::Build.new do |conf|

  # ... (snip) ...

  conf.gem :github => 'matsumotory/mruby-http2'
end

build

cd mruby
rake

Example

mruby-http2 Test Site

https://http2.matsumoto-r.jp:58080/index.html

HTTP2::Client

HTTP2 get

Access to nghttpd HTTP/2 Server

r = HTTP2::Client.get 'https://127.0.0.1:8080/index.html'

r.response
r.body
r.request_headers
r.response_headers
r.status
r.body
r.body_length
r.stream_id
HTTP2 get reuslt
{:body=>"hello mruby-http2!!\n", :body_length=>20, :recieve_bytes=>20.0, :response_headers=>{"last-modified"=>"Wed, 19 Mar 2014 08:41:00 GMT", "server"=>"nghttpd nghttp2/0.4.0-DEV", ":status"=>"200", "date"=>"Thu, 20 Mar 2014 11:38:17 GMT", "cache-control"=>"max-age=3600", "content-length"=>"20"}, :frame_send_header_goway=>true, :request_headers=>{"user-agent"=>"mruby-http2/0.0.1", "accept"=>"*/*", ":authority"=>"127.0.0.1:8080", ":scheme"=>"https", "accept-encoding"=>"gzip", ":method"=>"GET", ":path"=>"/index.html"}, :stream_id=>1}
"hello mruby-http2!!\n"
{"user-agent"=>"mruby-http2/0.0.1", "accept"=>"*/*", ":authority"=>"127.0.0.1:8080", ":scheme"=>"https", "accept-encoding"=>"gzip", ":method"=>"GET", ":path"=>"/index.html"}
{"last-modified"=>"Wed, 19 Mar 2014 08:41:00 GMT", "server"=>"nghttpd nghttp2/0.4.0-DEV", ":status"=>"200", "date"=>"Thu, 20 Mar 2014 11:38:17 GMT", "cache-control"=>"max-age=3600", "content-length"=>"20"}
200
"hello mruby-http2!!\n"
20
1
Set callback block from Ruby

Access to nghttpd HTTP/2 Server

r = HTTP2::Client.get 'https://127.0.0.1:8080/index.html'

p r.response
p r.body
p r.request_headers
p r.response_headers
p r.status
p r.body
p r.body_length
p r.stream_id

p "---- set callback version ----"

s = HTTP2::Client.new
s.uri = 'https://127.0.0.1:8080/index.html'
s.on_header_callback {
  p "header callback"
}
s.send_callback {
  p "send_callback"
}
s.recv_callback {
  p "recv_callback"
}
s.before_frame_send_callback {
  p "before_frame_send_callback"
}
s.on_frame_send_callback {
  p "on_frame_send_callback"
}
s.on_frame_recv_callback {
  p "on_frame_recv_callback"
}
s.on_stream_close_callback {
  p "on_stream_close_callback"
}
s.on_data_chunk_recv_callback {
  p "on_data_chunk_recv_callback"
}
r = s.get
p r.response
Result
"---- set callback version ----"
"recv_callback"
"on_frame_recv_callback"
"recv_callback"
"before_frame_send_callback"
"send_callback"
"on_frame_send_callback"
"before_frame_send_callback"
"send_callback"
"on_frame_send_callback"
"recv_callback"
"header callback"
"header callback"
"header callback"
"header callback"
"header callback"
"header callback"
"on_frame_recv_callback"
"on_data_chunk_recv_callback"
"on_frame_recv_callback"
"on_frame_recv_callback"
"on_stream_close_callback"
"recv_callback"
"before_frame_send_callback"
"send_callback"
"on_frame_send_callback"
{:body=>"hello mruby-http2!!\n", :body_length=>20, :recieve_bytes=>20.0, :response_headers=>{"last-modified"=>"Wed, 19 Mar 2014 08:41:00 GMT", "server"=>"nghttpd nghttp2/0.4.0-DEV", ":status"=>"200", "date"=>"Thu, 20 Mar 2014 11:39:34 GMT", "cache-control"=>"max-age=3600", "content-length"=>"20"}, :frame_send_header_goway=>true, :request_headers=>{"user-agent"=>"mruby-http2/0.0.1", "accept"=>"*/*", ":authority"=>"127.0.0.1:8080", ":scheme"=>"https", "accept-encoding"=>"gzip", ":method"=>"GET", ":path"=>"/index.html"}, :stream_id=>1}

HTTP2::Server

run HTTP/2 server
If you want to get more information, Please see Trusterd HTTP/2 Web Server using mruby-http2.
root_dir = "/usr/local/trusterd"

s = HTTP2::Server.new({

  :port           => 8080,
  :document_root  => "#{root_dir}/htdocs",
  :server_name    => "mruby-http2 server",

  :key            => "#{root_dir}/ssl/server.key",
  :crt            => "#{root_dir}/ssl/server.crt",

})

s.run
request from HTTP2::Client
r = HTTP2::Client.get 'https://127.0.0.1:8080/index.html'

p r.response
p r.body
p r.request_headers
p r.response_headers
p r.status
p r.body
p r.body_length
p r.stream_id

p "---- set callback version ----"

s = HTTP2::Client.new
s.uri = 'https://127.0.0.1:8080/index.html'
s.on_header_callback {
  p "header callback"
}
s.send_callback {
  p "send_callback"
}
s.recv_callback {
  p "recv_callback"
}
s.before_frame_send_callback {
  p "before_frame_send_callback"
}
s.on_frame_send_callback {
  p "on_frame_send_callback"
}
s.on_frame_recv_callback {
  p "on_frame_recv_callback"
}
s.on_stream_close_callback {
  p "on_stream_close_callback"
}
s.on_data_chunk_recv_callback {
  p "on_data_chunk_recv_callback"
}
r = s.get
p r.response
response
{:body=>"hello mruby-http2!!\n", :body_length=>20, :recieve_bytes=>20.0, :response_headers=>{":status"=>"200"}, :frame_send_header_goway=>true, :request_headers=>{"user-agent"=>"mruby-http2/0.0.1", "accept"=>"*/*", ":authority"=>"127.0.0.1:8080", ":scheme"=>"https", "accept-encoding"=>"gzip", ":method"=>"GET", ":path"=>"/index.html"}, :stream_id=>1}
"hello mruby-http2!!\n"
{"user-agent"=>"mruby-http2/0.0.1", "accept"=>"*/*", ":authority"=>"127.0.0.1:8080", ":scheme"=>"https", "accept-encoding"=>"gzip", ":method"=>"GET", ":path"=>"/index.html"}
{":status"=>"200"}
200
"hello mruby-http2!!\n"
20
1
"---- set callback version ----"
"recv_callback"
"before_frame_send_callback"
"send_callback"
"on_frame_send_callback"
"recv_callback"
"on_frame_recv_callback"
"recv_callback"
"before_frame_send_callback"
"send_callback"
"on_frame_send_callback"
"recv_callback"
"header callback"
"on_frame_recv_callback"
"recv_callback"
"recv_callback"
"on_data_chunk_recv_callback"
"on_frame_recv_callback"
"recv_callback"
"recv_callback"
"on_frame_recv_callback"
"on_stream_close_callback"
"recv_callback"
"before_frame_send_callback"
"send_callback"
"on_frame_send_callback"
{:body=>"hello mruby-http2!!\n", :body_length=>20, :recieve_bytes=>20.0, :response_headers=>{":status"=>"200"}, :frame_send_header_goway=>true, :request_headers=>{"user-agent"=>"mruby-http2/0.0.1", "accept"=>"*/*", ":authority"=>"127.0.0.1:8080", ":scheme"=>"https", "accept-encoding"=>"gzip", ":method"=>"GET", ":path"=>"/index.html"}, :stream_id=>1}

License

under the MIT License:

About

HTTP2 Module for mruby


Languages

Language:C 96.0%Language:Ruby 3.6%Language:Shell 0.4%Language:HTML 0.0%