2015-11-06 18:24:38 +00:00
|
|
|
require "json"
|
|
|
|
|
2015-10-29 15:16:29 +00:00
|
|
|
# ParamParser parses the request contents including query_params and body
|
|
|
|
# and converts them into a params hash which you can within the environment
|
|
|
|
# context.
|
2015-11-06 18:24:38 +00:00
|
|
|
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
|
|
|
|
|
2016-03-06 19:43:13 +00:00
|
|
|
class Kemal::ParamParser
|
|
|
|
URL_ENCODED_FORM = "application/x-www-form-urlencoded"
|
|
|
|
APPLICATION_JSON = "application/json"
|
|
|
|
|
|
|
|
def initialize(@request)
|
2016-03-06 12:08:59 +00:00
|
|
|
@url = {} of String => String
|
|
|
|
@query = {} of String => String
|
|
|
|
@body = {} of String => String
|
|
|
|
@json = {} of String => AllParamTypes
|
2015-10-28 18:30:27 +00:00
|
|
|
end
|
|
|
|
|
2016-03-06 20:29:23 +00:00
|
|
|
{% for method in %w(url query body json) %}
|
|
|
|
def {{method.id}}
|
|
|
|
parse_{{method.id}}
|
|
|
|
@{{method.id}}
|
2015-10-28 18:30:27 +00:00
|
|
|
end
|
2016-03-06 20:29:23 +00:00
|
|
|
{% end %}
|
2015-10-28 18:30:27 +00:00
|
|
|
|
2015-11-05 10:38:06 +00:00
|
|
|
def parse_body
|
2015-12-02 19:57:23 +00:00
|
|
|
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
|
2016-03-06 19:43:13 +00:00
|
|
|
@body = parse_part(@request.body)
|
2015-11-05 10:38:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_query
|
2016-03-06 19:43:13 +00:00
|
|
|
@query = parse_part(@request.query)
|
2015-11-05 10:38:06 +00:00
|
|
|
end
|
|
|
|
|
2016-03-06 20:29:23 +00:00
|
|
|
def parse_url
|
2016-02-03 20:08:54 +00:00
|
|
|
if params = @request.url_params
|
|
|
|
params.each do |key, value|
|
2016-03-06 19:43:13 +00:00
|
|
|
@url[key as String] = value as String
|
2016-01-12 22:06:19 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-12 19:37:12 +00:00
|
|
|
end
|
|
|
|
|
2015-12-31 12:03:11 +00:00
|
|
|
# Parses JSON request body if Content-Type is `application/json`.
|
|
|
|
# If request body is a JSON Hash then all the params are parsed and added into `params`.
|
|
|
|
# If request body is a JSON Array it's added into `params` as `_json` and can be accessed
|
|
|
|
# like params["_json"]
|
2015-11-06 18:24:38 +00:00
|
|
|
def parse_json
|
2015-11-10 00:35:28 +00:00
|
|
|
return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
|
2015-11-10 11:30:16 +00:00
|
|
|
|
2015-11-06 18:24:38 +00:00
|
|
|
body = @request.body as String
|
2015-12-24 16:09:33 +00:00
|
|
|
case json = JSON.parse(body).raw
|
2015-11-10 11:30:16 +00:00
|
|
|
when Hash
|
2016-03-06 11:22:24 +00:00
|
|
|
json.each do |key, value|
|
2016-03-06 19:43:13 +00:00
|
|
|
@json[key as String] = value as AllParamTypes
|
2015-11-10 11:30:16 +00:00
|
|
|
end
|
|
|
|
when Array
|
2016-03-06 19:43:13 +00:00
|
|
|
@json["_json"] = json
|
2015-11-06 18:24:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-05 10:38:06 +00:00
|
|
|
def parse_part(part)
|
2016-03-06 11:22:24 +00:00
|
|
|
part_params = {} of String => String
|
|
|
|
if part
|
|
|
|
HTTP::Params.parse(part) do |key, value|
|
|
|
|
part_params[key as String] ||= value as String
|
|
|
|
end
|
2015-11-05 10:38:06 +00:00
|
|
|
end
|
2016-03-06 11:22:24 +00:00
|
|
|
part_params
|
2015-11-05 10:38:06 +00:00
|
|
|
end
|
2016-03-06 12:08:59 +00:00
|
|
|
|
2015-10-28 18:30:27 +00:00
|
|
|
end
|
2016-03-06 12:08:59 +00:00
|
|
|
|