-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: add OmitHost bool to URL #46059
Comments
I'll also add that among other URL parsers tested, Go is almost unique in not distinguishing between Python represents an exception, but it normalizes |
We don't call that section Authority, we just call it Host. |
This proposal has been added to the active column of the proposals project |
Ah, sorry I mixed up Host and Hostname(). OmitHost is a good name. |
It sounds like people are in favor of adding OmitHost to URL. Will retitle. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Kindly cc-ing @willpoint and @kirbyquerby to send a fix. |
Change https://go.dev/cl/391294 mentions this issue: |
Since Go 1.19 the net/url URL.Parse function is changed when there is no host authority. This change broke the unix socket URL handling in the reverse proxy code. This change adds auto detection for the behavior and thus supports both old and new Go versions without having to add build constraints. Related: golang/go#46059
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/OFrp4cOVW8j
What did you expect to see?
All the parsed URLs roundtrip.
What did you see instead?
The issue here is that
myscheme:/path
andmyscheme:///path
are treated as the same URL, both parsing toYet, they are materially different URLs. According to RFC 3986,
myscheme:/path
should be treated as having apath-absolute
, which does not have anauthority
defined. On the other hand,myscheme:///path
does have anauthority
, albeit an empty one.Whether
authority
is undefined or empty is important for the recomposition algorithm in the RFC (i.e.,URL.String
):I can imagine two different ways to solve this problem:
Add a ForceAuthority boolean to url.URL, such that a true value implies an authority that is present even if Host is "" and User is nil. This has the advantage of being analogous to ForceQuery. However, it can run into compatibility problems: existing code that creates a
file
URL from scratch will now have their URL serialized tofile:/home/...
rather than the expectedfile:///home/...
.Add a NoAuthority boolean to url.URL, such that a false value implies an authority is present. url.Parse will set this field if a
/
(but not//
) is present right after the scheme. This maintains the previous behavior for any manually created URLs, but fixes the Parse/String roundtrip for URLs with a single slash.I propose approach 2.
As a historical note, this limitation was known when introduced in cdd6ae1 (Go 1.1) to fix #4189:
go/src/net/url/url_test.go
Lines 166 to 174 in b38b1b2
/cc @rsc @bradfitz
The text was updated successfully, but these errors were encountered: