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

net/url: better handling or documentation of IPv6 addresses in url.URL.Host #61276

Closed
jjlin opened this issue Jul 10, 2023 · 3 comments
Closed
Labels
Documentation NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@jjlin
Copy link

jjlin commented Jul 10, 2023

What did you do?

https://go.dev/play/p/Lypv_o9VMRf

host := "fc00::121:0:2:27"
url := url.URL{
	Scheme: "https",
	Host:   host,
	Path:   "/some/path",
}
fmt.Println(url.String())

What did you expect to see?

https://[fc00::121:0:2:27]/some/path

What did you see instead?

https://fc00::121:0:2:27/some/path

This ends up being interpreted as https://[fc00::121:0:2]:27/some/path.

The documentation for url.URL.Host is host or host:port, which leaves it unclear as to how a literal IPv6 address would be treated.

I think it makes more sense to treat a literal IPv6 address as just that, and require the user to specify [<ipv6-address>]:<port> if that's what they actually wanted, though I appreciate that there would be backward compatibility issues with that.

So at this point, it's probably best to just clarify the docs and add a reference to net.JoinHostPort. This could perhaps be done via #61093.

@cherrymui cherrymui added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jul 11, 2023
@cherrymui cherrymui added this to the Backlog milestone Jul 11, 2023
@cherrymui
Copy link
Member

cc @neild @rsc

@neild
Copy link
Contributor

neild commented Jul 11, 2023

The documentation probably could be better here.

URL.Host is the host or host:port from the authority component of the URL. (https://www.rfc-editor.org/rfc/rfc3986#section-3.2) When the host subcomponent contains square brackets, the square brackets are present in URL.Host.

So, yes, if you want an IPv6 address in a URL, you need to set URL.Host to [<ipv6-address>] or [<ipv6-address>]:<port>.

In retrospect, perhaps URL should have had separate Host and Port fields.

@gopherbot
Copy link

Change https://go.dev/cl/508976 mentions this issue: net/url: document requirements for IPv6 addresses in URL.Host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants