Skip to content

Commit

Permalink
Treat HTTP::Request body as IO. Fixes #257
Browse files Browse the repository at this point in the history
  • Loading branch information
sdogruyol committed Nov 24, 2016
1 parent f11b0e0 commit 3b9a3f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/kemal/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class HTTP::Server

def params
@request.url_params ||= route_lookup.params
@params ||= Kemal::ParamParser.new(@request)
@params ||= if @request.param_parser
@request.param_parser.not_nil!
else
Kemal::ParamParser.new(@request)
end
end

def redirect(url, status_code = 302)
Expand Down
16 changes: 14 additions & 2 deletions src/kemal/param_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,20 @@ module Kemal
end
end

def parse_part(part)
HTTP::Params.parse(part.to_s || "")
def parse_part(part : IO?)
if part
HTTP::Params.parse(part.gets_to_end)
else
HTTP::Params.parse("")
end
end

def parse_part(part : String?)
if part
HTTP::Params.parse(part.to_s)
else
HTTP::Params.parse("")
end
end
end
end
4 changes: 3 additions & 1 deletion src/kemal/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class HTTP::Request
property override_method
property url_params : Hash(String, String)?
getter param_parser : Kemal::ParamParser?

def override_method
@override_method ||= check_for_method_override!
Expand All @@ -18,7 +19,8 @@ class HTTP::Request
private def check_for_method_override!
@override_method = @method
if @method == "POST"
params = Kemal::ParamParser.new(self).body
@param_parser = Kemal::ParamParser.new(self)
params = @param_parser.not_nil!.body
if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"])
@override_method = params["_method"]
end
Expand Down

0 comments on commit 3b9a3f8

Please sign in to comment.