From 53d12534b787ca939467f7faa559653898643d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Kadir=20Ak=C4=B1n?= Date: Sun, 6 Mar 2016 21:43:13 +0200 Subject: [PATCH] Remove ParamContainer abstraction to make it faster. --- spec/param_parser_spec.cr | 28 ++++++++++++------------- src/kemal/context.cr | 2 +- src/kemal/param_parser.cr | 44 +++++++++++++++------------------------ src/kemal/request.cr | 2 +- 4 files changed, 33 insertions(+), 43 deletions(-) diff --git a/spec/param_parser_spec.cr b/spec/param_parser_spec.cr index c825b4e..f2d6c63 100644 --- a/spec/param_parser_spec.cr +++ b/spec/param_parser_spec.cr @@ -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 diff --git a/src/kemal/context.cr b/src/kemal/context.cr index 41a55dc..f744236 100644 --- a/src/kemal/context.cr +++ b/src/kemal/context.cr @@ -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) diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index 673c806..48e97f8 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -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 diff --git a/src/kemal/request.cr b/src/kemal/request.cr index dfc4935..92317c2 100644 --- a/src/kemal/request.cr +++ b/src/kemal/request.cr @@ -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