kemal/src/kemal/param_parser.cr

83 lines
2.3 KiB
Crystal
Raw Normal View History

2015-11-06 18:24:38 +00:00
require "json"
require "uri"
2015-11-06 18:24:38 +00:00
2016-07-17 11:43:13 +00:00
module Kemal
# ParamParser parses the request contents including query_params and body
# and converts them into a params hash which you can within the environment
# context.
class ParamParser
URL_ENCODED_FORM = "application/x-www-form-urlencoded"
APPLICATION_JSON = "application/json"
2016-11-10 13:38:29 +00:00
# :nodoc:
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)
2016-07-17 11:43:13 +00:00
def initialize(@request : HTTP::Request)
@url = {} of String => String
@query = HTTP::Params.new({} of String => Array(String))
@body = HTTP::Params.new({} of String => Array(String))
@json = {} of String => AllParamTypes
@url_parsed = false
@query_parsed = false
@body_parsed = false
@json_parsed = false
end
2015-10-28 18:30:27 +00:00
private def unescape_url_param(value : String)
value.size == 0 ? value : URI.unescape(value)
rescue
value
end
2016-07-17 11:43:13 +00:00
{% for method in %w(url query body json) %}
def {{method.id}}
# check memoization
return @{{method.id}} if @{{method.id}}_parsed
2016-03-06 20:39:10 +00:00
2016-07-17 11:43:13 +00:00
parse_{{method.id}}
# memoize
@{{method.id}}_parsed = true
@{{method.id}}
end
{% end %}
2015-10-28 18:30:27 +00:00
2016-07-17 11:43:13 +00:00
def parse_body
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
@body = parse_part(@request.body)
end
2016-07-17 11:43:13 +00:00
def parse_query
@query = parse_part(@request.query)
end
2016-07-17 11:43:13 +00:00
def parse_url
if params = @request.url_params
params.each do |key, value|
@url[key.as(String)] = unescape_url_param(value).as(String)
2016-07-17 11:43:13 +00:00
end
2016-01-12 22:06:19 +00:00
end
end
2016-01-12 19:37:12 +00:00
2016-07-17 11:43:13 +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"]
def parse_json
return unless @request.body && @request.headers["Content-Type"]?.try(&.starts_with?(APPLICATION_JSON))
2015-11-10 11:30:16 +00:00
body = @request.body.to_s
2016-07-17 11:43:13 +00:00
case json = JSON.parse(body).raw
when Hash
json.each do |key, value|
@json[key.as(String)] = value.as(AllParamTypes)
end
when Array
@json["_json"] = json
2015-11-10 11:30:16 +00:00
end
2015-11-06 18:24:38 +00:00
end
2016-07-17 11:43:13 +00:00
def parse_part(part)
HTTP::Params.parse(part.to_s || "")
2016-07-17 11:43:13 +00:00
end
end
2015-10-28 18:30:27 +00:00
end