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
Improve parsing of HTTP_HOST header #2605
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,36 @@ def new_connection | |
TCPSocket.new(@host, @port).tap {|sock| @ios << sock} | ||
end | ||
|
||
def test_normalize_host_header_missing | ||
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. There is probably a smarter way to check those headers? |
||
server_run app: ->(env) do | ||
[200, {}, [env["SERVER_NAME"], "\n", env["SERVER_PORT"]]] | ||
end | ||
|
||
data = send_http_and_read "GET / HTTP/1.0\r\n\r\n" | ||
assert_equal "localhost\n80", data.split("\r\n").last | ||
end | ||
|
||
def test_normalize_host_header_ipv4 | ||
server_run app: ->(env) do | ||
[200, {}, [env["SERVER_NAME"], "\n", env["SERVER_PORT"]]] | ||
end | ||
|
||
data = send_http_and_read "GET / HTTP/1.0\r\nHost: 123.123.123.123:456\r\n\r\n" | ||
assert_equal "123.123.123.123\n456", data.split("\r\n").last | ||
end | ||
|
||
def test_normalize_host_header_ipv6 | ||
server_run app: ->(env) do | ||
[200, {}, [env["SERVER_NAME"], "\n", env["SERVER_PORT"]]] | ||
end | ||
|
||
data = send_http_and_read "GET / HTTP/1.0\r\nHost: ::ffff:127.0.0.1:9292\r\n\r\n" | ||
assert_equal "::ffff:127.0.0.1\n9292", data.split("\r\n").last | ||
|
||
data = send_http_and_read "GET / HTTP/1.0\r\nHost: ::1:9292\r\n\r\n" | ||
assert_equal "::1\n9292", data.split("\r\n").last | ||
end | ||
|
||
def test_proper_stringio_body | ||
data = nil | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does it make sense to extract this to a constant?
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.
I have run the benchmark
hello.sh
with your changes, and it seems that extracting to a constant will make it faster. You can give it a try if that makes sense to you as well 😃No constant
Extract to constant
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.
@kuei0221
Thanks for the benchmark results. I tried to reproduce the results and could not find such a difference between requests/s of inline regexp and constant regexp. There was a small difference between the old (
index(":")
) and new version).I'll try with different ruby version later today.
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.
You can ignore my benchmark result if that doesn't make sense to you ( it defer from machine to machine, and I believe my machine is not very good 😂 )
I think it is a good idea to make it a constant in
const.rb
, so we don't need to allocated this value everytime it is called.The following comment in that file give us a good reason to do so:
puma/lib/puma/const.rb
Lines 90 to 97 in a717b27