mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Parse request body params only if content type is application/x-www-form-urlencoded
This commit is contained in:
parent
fd5c913f53
commit
f658fbe8af
2 changed files with 49 additions and 8 deletions
|
@ -2,6 +2,8 @@
|
|||
# and converts them into a params hash which you can within the environment
|
||||
# context.
|
||||
class Kemal::ParamParser
|
||||
URL_ENCODED_FORM = "application/x-www-form-urlencoded"
|
||||
|
||||
def initialize(@route, @request)
|
||||
@route_components = route.components
|
||||
@request_components = request.path.not_nil!.split "/"
|
||||
|
@ -14,16 +16,27 @@ class Kemal::ParamParser
|
|||
end
|
||||
|
||||
def parse_request
|
||||
{% for part in %w(query body) %}
|
||||
if {{part.id}} = @request.{{part.id}}
|
||||
HTTP::Params.parse({{part.id}}) do |key, value|
|
||||
@params[key] ||= value
|
||||
end
|
||||
end
|
||||
{% end %}
|
||||
parse_query
|
||||
parse_body
|
||||
@params
|
||||
end
|
||||
|
||||
def parse_body
|
||||
return unless @request.headers["Content-Type"]? == URL_ENCODED_FORM
|
||||
parse_part(@request.body)
|
||||
end
|
||||
|
||||
def parse_query
|
||||
parse_part(@request.query)
|
||||
end
|
||||
|
||||
def parse_part(part)
|
||||
return unless part
|
||||
HTTP::Params.parse(part) do |key, value|
|
||||
@params[key] ||= value
|
||||
end
|
||||
end
|
||||
|
||||
def parse_components
|
||||
@route_components.zip(@request_components) do |route_component, req_component|
|
||||
if route_component.starts_with? ':'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue