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}"
end
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"
end
@ -19,7 +19,7 @@ describe "ParamParser" do
request = HTTP::Request.new("POST", "/hello/cemal")
# Radix tree MUST be run to parse url params.
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"
end
@ -38,10 +38,10 @@ describe "ParamParser" do
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"})
body_params = Kemal::ParamParser.new(request).params.body
body_params = Kemal::ParamParser.new(request).body
body_params.should eq({"name" => "serdar", "age" => "99"})
end
@ -56,7 +56,7 @@ describe "ParamParser" do
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"})
end
@ -70,7 +70,7 @@ describe "ParamParser" do
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]})
end
@ -84,10 +84,10 @@ describe "ParamParser" do
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"})
json_params = Kemal::ParamParser.new(request).params.json
json_params = Kemal::ParamParser.new(request).json
json_params.should eq({"_json": [1]})
end
@ -100,16 +100,16 @@ describe "ParamParser" do
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)
query_params = Kemal::ParamParser.new(request).params.query
query_params = Kemal::ParamParser.new(request).query
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)
json_params = Kemal::ParamParser.new(request).params.json
json_params = Kemal::ParamParser.new(request).json
json_params.should eq({} of String => AllParamTypes)
end
end
@ -130,10 +130,10 @@ describe "ParamParser" do
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"})
body_params = Kemal::ParamParser.new(request).params.body
body_params = Kemal::ParamParser.new(request).body
body_params.should eq({} of String => String)
end
end

View file

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

View file

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

View file

@ -11,7 +11,7 @@ class HTTP::Request
private def check_for_method_override!
@override_method = @method
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"])
@override_method = params["_method"]
end