-
Notifications
You must be signed in to change notification settings - Fork 112
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
Allow not validating target #456
Changes from 2 commits
404a702
573a863
f8fbd26
5f2e805
6ec9faa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ defmodule Mint.HTTP1.Request do | |
|
||
import Mint.HTTP1.Parse | ||
|
||
def encode(method, target, headers, body) do | ||
def encode(method, target, headers, body, skip_target_validation \\ false) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this duplicates the default but I didn't:
Also opted for a plain boolean, could also be turned into an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's enforce this argument, this is a private interface. Also, if we can, we could move the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try moving it over, the case sensitive headers option is also handled purely in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved it over after a short coffee break ☕ One thing that is slightly odd now is that target validation is in defp encode_headers(headers) do
Enum.reduce(headers, "", fn {name, value}, acc ->
validate_header_name!(name)
validate_header_value!(name, value)
[acc, name, ": ", value, "\r\n"]
end)
end Might be worth to move. The reason I didn't yet is because it'd mean enumerating the headers another time and I'm not sure if that's a performance concern that matters in Happy to change or keep depending on what you think @whatyouhide ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it this way, don't worry about it for now. |
||
body = [ | ||
encode_request_line(method, target), | ||
encode_request_line(method, target, skip_target_validation), | ||
encode_headers(headers), | ||
"\r\n", | ||
encode_body(body) | ||
|
@@ -16,8 +16,8 @@ defmodule Mint.HTTP1.Request do | |
{:mint, reason} -> {:error, reason} | ||
end | ||
|
||
defp encode_request_line(method, target) do | ||
validate_target!(target) | ||
defp encode_request_line(method, target, skip_target_validation) do | ||
unless skip_target_validation, do: validate_target!(target) | ||
[method, ?\s, target, " HTTP/1.1\r\n"] | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -548,7 +548,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a small convenience change I made in a separate commit - hope it's not too much noise but I also didn't wanna do it in a separate PR. Happy to roll it back! |
||
|
||
\ | ||
""") | ||
|
@@ -570,7 +570,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
|
||
\ | ||
""") | ||
|
@@ -591,7 +591,7 @@ defmodule Mint.HTTP1Test do | |
GET / HTTP/1.1 | ||
content-length: 4 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
|
||
body\ | ||
""") | ||
|
@@ -607,7 +607,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
|
||
\ | ||
""") | ||
|
@@ -626,7 +626,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
content-length: 10 | ||
|
||
body\ | ||
|
@@ -760,6 +760,38 @@ defmodule Mint.HTTP1Test do | |
|
||
""") | ||
end | ||
|
||
@invalid_target "%%" | ||
test "targets are validated by default", %{port: port, server_ref: server_ref} do | ||
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port) | ||
PragTob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert_receive {^server_ref, _server_socket} | ||
|
||
assert {:error, %Mint.HTTP1{}, | ||
%Mint.HTTPError{reason: {:invalid_request_target, @invalid_target}}} = | ||
HTTP1.request(conn, "GET", @invalid_target, [], "") | ||
end | ||
|
||
test "target validation may be skipped based on connection options", %{ | ||
port: port, | ||
server_ref: server_ref | ||
} do | ||
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, skip_target_validation: true) | ||
|
||
assert_receive {^server_ref, server_socket} | ||
|
||
assert {:ok, _conn, _ref} = HTTP1.request(conn, "GET", @invalid_target, [], "body") | ||
|
||
assert receive_request_string(server_socket) == | ||
request_string(""" | ||
GET %% HTTP/1.1 | ||
content-length: 4 | ||
host: localhost:#{port} | ||
user-agent: #{mint_user_agent()} | ||
|
||
body\ | ||
""") | ||
end | ||
end | ||
|
||
describe "streaming requests" do | ||
|
@@ -772,7 +804,7 @@ defmodule Mint.HTTP1Test do | |
GET / HTTP/1.1 | ||
transfer-encoding: chunked | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
|
||
\ | ||
""") | ||
|
@@ -795,7 +827,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
transfer-encoding: chunked | ||
|
||
\ | ||
|
@@ -815,7 +847,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
transfer-encoding: gzip,chunked | ||
|
||
\ | ||
|
@@ -839,7 +871,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
transfer-encoding: identity | ||
|
||
\ | ||
|
@@ -859,7 +891,7 @@ defmodule Mint.HTTP1Test do | |
request_string(""" | ||
GET / HTTP/1.1 | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
content-length: 5 | ||
|
||
\ | ||
|
@@ -877,7 +909,7 @@ defmodule Mint.HTTP1Test do | |
GET / HTTP/1.1 | ||
transfer-encoding: chunked | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
|
||
\ | ||
""") | ||
|
@@ -897,7 +929,7 @@ defmodule Mint.HTTP1Test do | |
POST / HTTP/1.1 | ||
transfer-encoding: chunked | ||
host: localhost:#{port} | ||
user-agent: mint/#{Mix.Project.config()[:version]} | ||
user-agent: #{mint_user_agent()} | ||
|
||
\ | ||
""") | ||
|
@@ -997,4 +1029,6 @@ defmodule Mint.HTTP1Test do | |
defp stream_message_bytewise(<<>>, conn, responses) do | ||
{:ok, conn, responses} | ||
end | ||
|
||
defp mint_user_agent, do: "mint/#{Mix.Project.config()[:version]}" | ||
PragTob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added it to keep on par with
case_sensititve_headers
but I'm not sure if this would be v 1.7.0 if it was released (technically it's a feature).Also, should I add it to the Changelog then? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.7.0 is fine, but we will add it to the changelog when cutting a release.