Added support for magic param _method just like Rails, Sinatra.

Following are the changes made in this commit:

- Added support for magic param `_method` just like Rails, Sinatra
etc.. Browsers which don't support `PUT`, `PATCH` and `DELETE` methods
can simulate them by sending `method` param in request body.

- The default Content-Type to parse request body submitted via forms
etc. is `application/x-www-form-urlencoded`. But if we send Ajax
request in Chrome ($.post) then by-default Content-Type is set to
`application/x-www-form-urlencoded; charset utf-8` which was not
getting matched. I changed the code from `==` to match against a
regular expression using `=~`.

- Print name and color of overridden HTTP method via Logger instead of
printing name and color or request's incoming HTTP method.

Making necessary changes as pointed by @sdogruyol

- Changed method name from`is_override_method_valid?` to
`override_method_valid?`.

- Refactored `if` condition in `override_method_valid?`.
This commit is contained in:
Imran Latif 2015-12-03 00:57:23 +05:00
parent dfea8df0f7
commit c42f1f88e9
4 changed files with 73 additions and 2 deletions

View file

@ -29,7 +29,7 @@ class Kemal::ParamParser
end
def parse_body
return unless @request.headers["Content-Type"]? == URL_ENCODED_FORM
return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil?
parse_part(@request.body)
end

4
src/kemal/request.cr Normal file
View file

@ -0,0 +1,4 @@
# Opening HTTP::Request to add override_method property
class HTTP::Request
property override_method
end

View file

@ -10,7 +10,9 @@ class Kemal::Route
end
def match?(request)
return nil unless request.method == @method
check_for_method_override!(request)
return nil unless request.override_method == @method
components = request.path.not_nil!.split "/"
return nil unless components.size == @components.size
@components.zip(components) do |route_component, req_component|
@ -20,4 +22,22 @@ class Kemal::Route
end
true
end
# checks if request params contain _method param to override request incoming method
def check_for_method_override!(request)
request.override_method = request.method
if request.method == "POST"
params = Kemal::ParamParser.new(self, request).parse_request
if params.has_key?("_method") && self.override_method_valid?(params["_method"])
request.override_method = params["_method"]
end
end
end
# checks if method contained in _method param is valid one
def override_method_valid?(override_method)
return false unless override_method.is_a?(String)
override_method = override_method.upcase
return (override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE")
end
end