diff options
-rw-r--r-- | examples/rack.ru | 22 | ||||
-rw-r--r-- | examples/sinatra.rb | 14 | ||||
-rw-r--r-- | lib/plum/rack/connection.rb | 69 | ||||
-rw-r--r-- | lib/rack/handler/plum.rb | 6 |
4 files changed, 61 insertions, 50 deletions
diff --git a/examples/rack.ru b/examples/rack.ru new file mode 100644 index 0000000..7558c80 --- /dev/null +++ b/examples/rack.ru @@ -0,0 +1,22 @@ +$LOAD_PATH << File.expand_path("../../lib", __FILE__) +require "plum/rack" + +class App2 + def call(env) + if env["REQUEST_METHOD"] == "GET" && env["PATH_INFO"] == "/" + [ + 200, + { "Content-Type" => "text/html" }, + ["Hello World!"] + ] + else + [ + 404, + { "Content-Type" => "text/html" }, + [""] + ] + end + end +end + +run App2.new diff --git a/examples/sinatra.rb b/examples/sinatra.rb deleted file mode 100644 index 83c2113..0000000 --- a/examples/sinatra.rb +++ /dev/null @@ -1,14 +0,0 @@ -$LOAD_PATH << File.expand_path("../../lib", __FILE__) -require "plum/rack" -require "sinatra" - -set :server, :plum -enable :logging, :dump_errors, :raise_errors - -get "/" do - "get: #{params}" -end - -post "/" do - "post: " + params.to_s -end diff --git a/lib/plum/rack/connection.rb b/lib/plum/rack/connection.rb index 2ab33e5..400a098 100644 --- a/lib/plum/rack/connection.rb +++ b/lib/plum/rack/connection.rb @@ -34,7 +34,7 @@ module Plum headers = data = nil stream.on(:open) { headers = nil - data = "".b + data = "".force_encoding(Encoding::BINARY) } stream.on(:headers) { |h| @@ -103,44 +103,43 @@ module Plum end def new_env(h, data) - headers = h.group_by { |k, v| k }.map { |k, kvs| - if k == "cookie" - [k, kvs.map(&:last).join("; ")] - else - [k, kvs.first.last] - end - }.to_h - - cmethod = headers[":method"] - cpath = headers[":path"] - cpath_name, cpath_query = cpath.split("?", 2).map(&:to_s) - cauthority = headers[":authority"] - cscheme = headers[":scheme"] ebase = { - "REQUEST_METHOD" => cmethod, "SCRIPT_NAME" => "", - "PATH_INFO" => cpath_name, - "QUERY_STRING" => cpath_query.to_s, - "SERVER_NAME" => cauthority.split(":").first, - "SERVER_PORT" => (cauthority.split(":").last || 443), # TODO: forwarded header (RFC 7239) - } - - headers.each {|key, value| - unless key.start_with?(":") && key.include?(".") - ebase["HTTP_" + key.gsub("-", "_").upcase] = value - end - } - - ebase.merge!({ "rack.version" => ::Rack::VERSION, - "rack.url_scheme" => cscheme, "rack.input" => StringIO.new(data), "rack.errors" => $stderr, "rack.multithread" => true, "rack.multiprocess" => false, "rack.run_once" => false, "rack.hijack?" => false, - }) + } + + h.each { |k, v| + case k + when ":method" + ebase["REQUEST_METHOD"] = v + when ":path" + cpath_name, cpath_query = v.split("?", 2) + ebase["PATH_INFO"] = cpath_name + ebase["QUERY_STRING"] = cpath_query || "" + when ":authority" + chost, cport = v.split(":", 2) + ebase["SERVER_NAME"] = chost + ebase["SERVER_PORT"] = (cport || 443).to_i + when ":scheme" + ebase["rack.url_scheme"] = v + else + if k.start_with?(":") + # unknown HTTP/2 pseudo-headers + else + if "cookie" == k && headers["HTTP_COOKIE"] + ebase["HTTP_COOKIE"] << "; " << v + else + ebase["HTTP_" << k.tr("-", "_").upcase!] = v + end + end + end + } ebase end @@ -156,12 +155,12 @@ module Plum next end - key = key.downcase.gsub(/^x-/, "") - vs = v_.split("\n") - if key == "set-cookie" - rbase[key] = vs.join("; ") # RFC 7540 8.1.2.5 + key = key.downcase + if "set-cookie".freeze == key + rbase[key] = v_.gsub("\n", "; ") # RFC 7540 8.1.2.5 else - rbase[key] = vs.join(",") # RFC 7230 7 + key = key.byteshift(2) if key.start_with?("x-") + rbase[key] = v_.tr("\n", ",") # RFC 7230 7 end end diff --git a/lib/rack/handler/plum.rb b/lib/rack/handler/plum.rb index c3ca543..30eed72 100644 --- a/lib/rack/handler/plum.rb +++ b/lib/rack/handler/plum.rb @@ -16,10 +16,14 @@ module Rack ) @server = ::Plum::Rack::Server.new(app, config) - yield @server if block_given? + yield @server if block_given? # TODO @server.start end + def self.shutdown + @server.stop if @server + end + def self.valid_options { "Host=HOST" => "Hostname to listen on (default: #{default_options[:Host]})", |