2015-12-02 19:57:23 +00:00
|
|
|
class HTTP::Request
|
2017-05-08 14:28:21 +00:00
|
|
|
property override_method
|
2016-04-12 11:41:09 +00:00
|
|
|
property url_params : Hash(String, String)?
|
2016-11-24 09:07:39 +00:00
|
|
|
getter param_parser : Kemal::ParamParser?
|
2016-02-16 20:07:58 +00:00
|
|
|
|
|
|
|
def override_method
|
|
|
|
@override_method ||= check_for_method_override!
|
|
|
|
end
|
|
|
|
|
2016-11-18 21:05:22 +00:00
|
|
|
# Checks if method contained in _method param is valid one
|
2017-08-24 20:32:43 +00:00
|
|
|
def self.override_method_valid?(override_method : String)
|
2016-11-18 21:05:22 +00:00
|
|
|
return false unless override_method.is_a?(String)
|
|
|
|
override_method = override_method.upcase
|
|
|
|
override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE"
|
|
|
|
end
|
|
|
|
|
2016-02-16 20:07:58 +00:00
|
|
|
# Checks if request params contain _method param to override request incoming method
|
|
|
|
private def check_for_method_override!
|
|
|
|
@override_method = @method
|
|
|
|
if @method == "POST"
|
2016-11-24 09:07:39 +00:00
|
|
|
@param_parser = Kemal::ParamParser.new(self)
|
|
|
|
params = @param_parser.not_nil!.body
|
2016-02-16 20:07:58 +00:00
|
|
|
if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"])
|
2017-05-08 14:28:21 +00:00
|
|
|
@override_method = params["_method"]
|
2016-02-16 20:07:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@override_method
|
|
|
|
end
|
2015-12-06 15:55:21 +00:00
|
|
|
end
|