Improve param parsing and remove url_params from HTTP::Request

This commit is contained in:
Serdar Dogruyol 2018-06-30 16:49:04 +03:00
parent ad5dc053c4
commit d2ef57a128
8 changed files with 41 additions and 55 deletions

View file

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

View file

@ -1,7 +0,0 @@
class HTTP::Request
property url_params : Hash(String, String)?
def content_type
@headers["Content-Type"]?
end
end

View file

@ -11,8 +11,7 @@ module Kemal
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any)
getter files
def initialize(@request : HTTP::Request)
@url = {} of String => String
def initialize(@request : HTTP::Request, @url : Hash(String, String) = {} of String => String)
@query = HTTP::Params.new({} of String => Array(String))
@body = HTTP::Params.new({} of String => Array(String))
@json = {} of String => AllParamTypes
@ -42,7 +41,7 @@ module Kemal
{% end %}
private def parse_body
content_type = @request.content_type
content_type = @request.headers["Content-Type"]?
return unless content_type
if content_type.try(&.starts_with?(URL_ENCODED_FORM))
@body = parse_part(@request.body)
@ -59,11 +58,7 @@ module Kemal
end
private def parse_url
if params = @request.url_params
params.each do |key, value|
@url[key] = unescape_url_param(value)
end
end
@url.each { |key, value| @url[key] = unescape_url_param(value) }
end
private def parse_file_upload

View file

@ -11,7 +11,6 @@ module Kemal
def call(context : HTTP::Server::Context)
return call_next(context) unless context.ws_route_defined? && websocket_upgrade_request?(context)
context.request.url_params ||= context.ws_route_lookup.params
content = context.websocket.call(context)
context.response.print(content)
context