Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP::Request#form_params #13418

Merged
merged 4 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions spec/std/http/request_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,30 @@ module HTTP
request.query = new_query
request.query_params.to_s.should eq(new_query)
end
end

describe "#form_params" do
it "returns can safely be called on get requests" do
request = Request.from_io(IO::Memory.new("GET /api/v3/some/resource HTTP/1.1\r\n\r\n")).as(Request)
request.form_params?.should eq(nil)
request.form_params.size.should eq(0)
end

it "returns parsed HTTP::Params" do
request = Request.new("POST", "/form", HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"}, HTTP::Params.encode({"test" => "foobar"}))
request.form_params?.should_not eq(nil)
request.form_params.size.should eq(1)
request.form_params["test"].should eq("foobar")
end

it "returns ignors invalid content-type" do
request = Request.new("POST", "/form", nil, HTTP::Params.encode({"test" => "foobar"}))
request.form_params?.should eq(nil)
request.form_params.size.should eq(0)
end
end

describe "#hostname" do
it "gets request hostname from the headers" do
request = Request.from_io(IO::Memory.new("GET / HTTP/1.1\r\nHost: host.example.org:3000\r\nReferer:\r\n\r\n")).as(Request)
request.hostname.should eq("host.example.org")
Expand Down Expand Up @@ -472,7 +495,9 @@ module HTTP
request = Request.new("GET", "/")
request.hostname.should be_nil
end
end

describe "#host_with_port" do
it "gets request host with port from the headers" do
request = Request.from_io(IO::Memory.new("GET / HTTP/1.1\r\nHost: host.example.org:3000\r\nReferer:\r\n\r\n")).as(Request)
request.host_with_port.should eq("host.example.org:3000")
Expand Down
19 changes: 19 additions & 0 deletions src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class HTTP::Request
property version : String
@cookies : Cookies?
@query_params : URI::Params?
@form_params : HTTP::Params?
@uri : URI?

# The network address that sent the request to an HTTP server.
Expand Down Expand Up @@ -80,6 +81,24 @@ class HTTP::Request
@query_params ||= uri.query_params
end

# Returns a convenience wrapper to parse form params, see `URI::Params`.
# Returns `nil` in case the content type `"application/x-www-form-urlencoded"`
# is not present or the body is `nil`.
def form_params? : HTTP::Params?
@form_params ||= begin
if headers["Content-Type"]? == "application/x-www-form-urlencoded"
if body = self.body
HTTP::Params.parse(body.gets_to_end)
end
end
end
end

# Returns a convenience wrapper to parse form params, see `URI::Params`.
def form_params : HTTP::Params
form_params? || HTTP::Params.new
end

def resource : String
update_uri
@uri.try(&.request_target) || @resource
Expand Down