Add Kemal::OverrideMethodHandler

This commit is contained in:
Serdar Dogruyol 2018-10-26 15:00:24 +03:00
parent b389022b35
commit 3f7c8b4577
2 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,24 @@
module Kemal
class OverrideMethodHandler
include HTTP::Handler
INSTANCE = new
ALLOWED_METHODS = ["PUT", "PATCH", "DELETE"]
OVERRIDE_METHOD = "POST"
OVERRIDE_METHOD_PARAM_KEY = "_method"
def call(context)
request = context.request
if request.method == OVERRIDE_METHOD
if context.params.body.has_key?(OVERRIDE_METHOD_PARAM_KEY) && override_method_valid?(context.params.body["_method"])
request.method = context.params.body["_method"].upcase
end
end
call_next(context)
end
def override_method_valid?(override_method : String)
ALLOWED_METHODS.includes?(override_method.upcase)
end
end
end