mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Merge pull request #123 from f/master
Upgrade ParamParser to make it more convinient.
This commit is contained in:
commit
a194bebe31
7 changed files with 92 additions and 56 deletions
|
@ -18,7 +18,7 @@ at_exit do
|
|||
|
||||
# This route serves the built-in images for not_found and exceptions.
|
||||
get "/__kemal__/:image" do |env|
|
||||
image = env.params["image"]
|
||||
image = env.params.url["image"]
|
||||
file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current)
|
||||
if File.exists? file_path
|
||||
env.response.headers.add "Content-Type", "application/octet-stream"
|
||||
|
|
|
@ -4,7 +4,7 @@ class HTTP::Server
|
|||
class Context
|
||||
def params
|
||||
@request.url_params = route_lookup.params
|
||||
@params ||= Kemal::ParamParser.new(@request).parse
|
||||
@params ||= Kemal::ParamParser.new(@request)
|
||||
end
|
||||
|
||||
def redirect(url, status_code = 302)
|
||||
|
|
|
@ -10,34 +10,37 @@ class Kemal::ParamParser
|
|||
APPLICATION_JSON = "application/json"
|
||||
|
||||
def initialize(@request)
|
||||
@params = {} of String => AllParamTypes
|
||||
@url = {} of String => String
|
||||
@query = {} of String => String
|
||||
@body = {} of String => String
|
||||
@json = {} of String => AllParamTypes
|
||||
end
|
||||
|
||||
def parse
|
||||
parse_request
|
||||
end
|
||||
{% for method in %w(url query body json) %}
|
||||
def {{method.id}}
|
||||
# check memoization
|
||||
return @{{method.id}} if @{{method.id}}_parsed
|
||||
|
||||
def parse_request
|
||||
parse_query
|
||||
parse_body
|
||||
parse_json
|
||||
parse_url_params
|
||||
@params
|
||||
parse_{{method.id}}
|
||||
# memoize
|
||||
@{{method.id}}_parsed = true
|
||||
@{{method.id}}
|
||||
end
|
||||
{% end %}
|
||||
|
||||
def parse_body
|
||||
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
|
||||
parse_part(@request.body)
|
||||
@body = parse_part(@request.body)
|
||||
end
|
||||
|
||||
def parse_query
|
||||
parse_part(@request.query)
|
||||
@query = parse_part(@request.query)
|
||||
end
|
||||
|
||||
def parse_url_params
|
||||
def parse_url
|
||||
if params = @request.url_params
|
||||
params.each do |key, value|
|
||||
@params[key] = value
|
||||
@url[key as String] = value as String
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -52,18 +55,21 @@ class Kemal::ParamParser
|
|||
body = @request.body as String
|
||||
case json = JSON.parse(body).raw
|
||||
when Hash
|
||||
json.each do |k, v|
|
||||
@params[k as String] = v as AllParamTypes
|
||||
json.each do |key, value|
|
||||
@json[key as String] = value as AllParamTypes
|
||||
end
|
||||
when Array
|
||||
@params["_json"] = json
|
||||
@json["_json"] = json
|
||||
end
|
||||
end
|
||||
|
||||
def parse_part(part)
|
||||
return unless part
|
||||
HTTP::Params.parse(part) do |key, value|
|
||||
@params[key] ||= value
|
||||
part_params = {} of String => String
|
||||
if part
|
||||
HTTP::Params.parse(part) do |key, value|
|
||||
part_params[key as String] ||= value as String
|
||||
end
|
||||
end
|
||||
part_params
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ class HTTP::Request
|
|||
private def check_for_method_override!
|
||||
@override_method = @method
|
||||
if @method == "POST"
|
||||
params = Kemal::ParamParser.new(self).parse_request
|
||||
params = Kemal::ParamParser.new(self).body
|
||||
if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"])
|
||||
@override_method = params["_method"]
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue