-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 HTTP::Client to use ipv6 addresses #6147
Conversation
src/http/client.cr
Outdated
# URI host may have ipv6 enclosed by brackets and is fine according to | ||
# rfc3986. The host http header must contains the brackets, ie: the same | ||
# value as returned by URI#host | ||
ip_host = @host[0] == '[' && @host[-1] == ']' ? @host[1..-2] : @host |
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.
@host.starts_with?('[') && @host.ends_with?(']')
is x2 faster.
@host.lstrip.rstrip
is said to be 7x than @host[1..-2]
. I wonder if adding a method to strip both at the start and at the end can make sense.
host = "[test]"
Benchmark.ips do |x|
x.report("index") do
host[1..-2]
end
x.report("strip") do
host.lstrip.rstrip
end
end
index 29.23M ( 34.21ns) (± 4.66%) 7.72× slower
strip 225.75M ( 4.43ns) (± 3.75%) fastest
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.
Thats because lstrip
doesn't do what you want here? i.e. it doesn't do anything so of course it's faster.
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.
.lchop('[').rchop(']')
should be shorter here though. Surely the same speed.
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.
Sorry for the mistake, I confuse the two. Change strip
to chop
to have lchop.rchop
With chops the program is slower
src/http/client.cr
Outdated
# URI host may have ipv6 enclosed by brackets and is fine according to | ||
# rfc3986. The host http header must contains the brackets, ie: the same | ||
# value as returned by URI#host | ||
ip_host = @host[0] == '[' && @host[-1] == ']' ? @host[1..-2] : @host |
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 feel like this line should be automatic. The glue code to support ipv6 should be in TCPSocket.new
or in URI
.
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.
Both Ruby and Go expose a hostname
, which is just the host
without []
if it's ipv6. Perhaps we should copy them.
I moved the unwrap to URI, but since in http client Regarding benchmark, the original code wins: require "benchmark"
host = "[test#{Random.new.next_int}]"
pp host
pp host[0] == '[' && host[-1] == ']' ? host[1..-2] : host
Benchmark.ips do |x|
x.report("index") do
host[0] == '[' && host[-1] == ']' ? host[1..-2] : host
end
x.report("chop") do
host.lchop('[').rchop(']')
end
end
|
@bcardiff Sorry for the mistake. |
We can remove the ugly |
@RX14 that won't work. There are initializers that use String and not URI. |
@bcardiff that's fine - if someone passes |
@RX14 I don't agree. clients initialized with Otherwise we need to add logic to wrap the host upon client. It can be done, but is a different issue. Bottom line |
Well I really hate Then add |
But that would lead to two implementation of the same thing. Let's leave I'm satisfied with the state of the PR. |
A bit of copy and paste really isn't that bad, especially for a snippet of code so small and unlikely to change as this. DRY shouldn't be taken to extremes, and I think |
I agree. Copying once is okayish. Copying a second time should trigger a refactor, but once is acceptable. |
Done, the PR should be squashed on merge. (I have the feeling that URI.hostname will come back sooner that later, but ok) |
Fixes #5514
The
URI#host
is fine to have the brackets since they are part of rfc3986 host description.The
HTTP::Client
needs to extract them before creating theTCPSocket
.