Remove ParamContainer abstraction to make it faster.

This commit is contained in:
Fatih Kadir Akın 2016-03-06 21:43:13 +02:00
parent e53ba1d9da
commit 53d12534b7
4 changed files with 33 additions and 43 deletions

View file

@ -7,7 +7,7 @@ describe "ParamParser" do
"Hello #{hasan}" "Hello #{hasan}"
end end
request = HTTP::Request.new("POST", "/?hasan=cemal") request = HTTP::Request.new("POST", "/?hasan=cemal")
query_params = Kemal::ParamParser.new(request).params.query query_params = Kemal::ParamParser.new(request).query
query_params["hasan"].should eq "cemal" query_params["hasan"].should eq "cemal"
end end
@ -19,7 +19,7 @@ describe "ParamParser" do
request = HTTP::Request.new("POST", "/hello/cemal") request = HTTP::Request.new("POST", "/hello/cemal")
# Radix tree MUST be run to parse url params. # Radix tree MUST be run to parse url params.
io_with_context = create_request_and_return_io(kemal, request) io_with_context = create_request_and_return_io(kemal, request)
url_params = Kemal::ParamParser.new(request).params.url url_params = Kemal::ParamParser.new(request).url
url_params["hasan"].should eq "cemal" url_params["hasan"].should eq "cemal"
end end
@ -38,10 +38,10 @@ describe "ParamParser" do
headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"}, headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"},
) )
query_params = Kemal::ParamParser.new(request).params.query query_params = Kemal::ParamParser.new(request).query
query_params.should eq({"hasan" => "cemal"}) query_params.should eq({"hasan" => "cemal"})
body_params = Kemal::ParamParser.new(request).params.body body_params = Kemal::ParamParser.new(request).body
body_params.should eq({"name" => "serdar", "age" => "99"}) body_params.should eq({"name" => "serdar", "age" => "99"})
end end
@ -56,7 +56,7 @@ describe "ParamParser" do
headers: HTTP::Headers{"Content-Type": "application/json"}, headers: HTTP::Headers{"Content-Type": "application/json"},
) )
json_params = Kemal::ParamParser.new(request).params.json json_params = Kemal::ParamParser.new(request).json
json_params.should eq({"name": "Serdar"}) json_params.should eq({"name": "Serdar"})
end end
@ -70,7 +70,7 @@ describe "ParamParser" do
headers: HTTP::Headers{"Content-Type": "application/json"}, headers: HTTP::Headers{"Content-Type": "application/json"},
) )
json_params = Kemal::ParamParser.new(request).params.json json_params = Kemal::ParamParser.new(request).json
json_params.should eq({"_json": [1]}) json_params.should eq({"_json": [1]})
end end
@ -84,10 +84,10 @@ describe "ParamParser" do
headers: HTTP::Headers{"Content-Type": "application/json"}, headers: HTTP::Headers{"Content-Type": "application/json"},
) )
query_params = Kemal::ParamParser.new(request).params.query query_params = Kemal::ParamParser.new(request).query
query_params.should eq({"foo": "bar"}) query_params.should eq({"foo": "bar"})
json_params = Kemal::ParamParser.new(request).params.json json_params = Kemal::ParamParser.new(request).json
json_params.should eq({"_json": [1]}) json_params.should eq({"_json": [1]})
end end
@ -100,16 +100,16 @@ describe "ParamParser" do
headers: HTTP::Headers{"Content-Type": "application/json"}, headers: HTTP::Headers{"Content-Type": "application/json"},
) )
url_params = Kemal::ParamParser.new(request).params.url url_params = Kemal::ParamParser.new(request).url
url_params.should eq({} of String => String) url_params.should eq({} of String => String)
query_params = Kemal::ParamParser.new(request).params.query query_params = Kemal::ParamParser.new(request).query
query_params.should eq({} of String => String) query_params.should eq({} of String => String)
body_params = Kemal::ParamParser.new(request).params.body body_params = Kemal::ParamParser.new(request).body
body_params.should eq({} of String => String) body_params.should eq({} of String => String)
json_params = Kemal::ParamParser.new(request).params.json json_params = Kemal::ParamParser.new(request).json
json_params.should eq({} of String => AllParamTypes) json_params.should eq({} of String => AllParamTypes)
end end
end end
@ -130,10 +130,10 @@ describe "ParamParser" do
headers: HTTP::Headers{"Content-Type": "text/plain"}, headers: HTTP::Headers{"Content-Type": "text/plain"},
) )
query_params = Kemal::ParamParser.new(request).params.query query_params = Kemal::ParamParser.new(request).query
query_params.should eq({"hasan" => "cemal"}) query_params.should eq({"hasan" => "cemal"})
body_params = Kemal::ParamParser.new(request).params.body body_params = Kemal::ParamParser.new(request).body
body_params.should eq({} of String => String) body_params.should eq({} of String => String)
end end
end end

View file

@ -5,7 +5,7 @@ class HTTP::Server
def params def params
@request.url_params = route_lookup.params @request.url_params = route_lookup.params
@params ||= Kemal::ParamParser.new(@request).params @params ||= Kemal::ParamParser.new(@request)
end end
def redirect(url, status_code = 302) def redirect(url, status_code = 302)

View file

@ -5,35 +5,26 @@ require "json"
# context. # context.
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type) alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
class Kemal::ParamContainer
property url
property query
property body
property json
def initialize
@url = {} of String => String
@query = {} of String => String
@body = {} of String => String
@json = {} of String => AllParamTypes
end
def all
@url.merge(@query).merge(@body).merge(@json)
end
end
class Kemal::ParamParser class Kemal::ParamParser
URL_ENCODED_FORM = "application/x-www-form-urlencoded" URL_ENCODED_FORM = "application/x-www-form-urlencoded"
APPLICATION_JSON = "application/json" APPLICATION_JSON = "application/json"
getter url
getter query
getter body
getter json
def initialize(@request) def initialize(@request)
@param_container = Kemal::ParamContainer.new @url = {} of String => String
@query = {} of String => String
@body = {} of String => String
@json = {} of String => AllParamTypes
parse_request
end end
def params def params
parse_request @query.merge(@body).merge(@json).merge(@url)
@param_container
end end
def parse_request def parse_request
@ -45,17 +36,17 @@ class Kemal::ParamParser
def parse_body def parse_body
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil? return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
@param_container.body = parse_part(@request.body) @body = parse_part(@request.body)
end end
def parse_query def parse_query
@param_container.query = parse_part(@request.query) @query = parse_part(@request.query)
end end
def parse_url_params def parse_url_params
if params = @request.url_params if params = @request.url_params
params.each do |key, value| params.each do |key, value|
@param_container.url[key as String] = value as String @url[key as String] = value as String
end end
end end
end end
@ -71,10 +62,10 @@ class Kemal::ParamParser
case json = JSON.parse(body).raw case json = JSON.parse(body).raw
when Hash when Hash
json.each do |key, value| json.each do |key, value|
@param_container.json[key as String] = value as AllParamTypes @json[key as String] = value as AllParamTypes
end end
when Array when Array
@param_container.json["_json"] = json @json["_json"] = json
end end
end end
@ -88,6 +79,5 @@ class Kemal::ParamParser
part_params part_params
end end
delegate url, body, query, json, @param_container
end end

View file

@ -11,7 +11,7 @@ class HTTP::Request
private def check_for_method_override! private def check_for_method_override!
@override_method = @method @override_method = @method
if @method == "POST" if @method == "POST"
params = Kemal::ParamParser.new(self).params.all params = Kemal::ParamParser.new(self).params
if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"]) if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"])
@override_method = params["_method"] @override_method = params["_method"]
end end