aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-10-25 11:11:45 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-10-25 11:11:45 +0900
commit085c88260516d398ad44acb53617dd669dad53fe (patch)
tree7a385468a756f6427d0678b853344f7aae395fa8 /lib
parent4a645bc4cdce3ead0668e2b5be50d473739b8a8d (diff)
downloadplum-085c88260516d398ad44acb53617dd669dad53fe.tar.gz
improve performance
Diffstat (limited to 'lib')
-rw-r--r--lib/plum/rack/connection.rb69
-rw-r--r--lib/rack/handler/plum.rb6
2 files changed, 39 insertions, 36 deletions
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]})",