-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: net/url: add FromFilePath and ToFilePath #32456
Comments
There is at least one place where we have already gotten this wrong in the standard library: Line 85 in ac8dbe7
|
I assume most uses will be string-based - is there a reason why you've designed the API around |
@mvdan, two reasons:
|
I think there's enough subtlety in the implementation here - even though the docs sound simple - that this is worth a design doc laying out the issues that an implementation will have to address. There's no rush on this, @bcmills, but I think that's the next step. Will mark it on-hold for design doc. |
Is there any progress on adding this method? ^_^ |
Change https://go.dev/cl/438175 mentions this issue: |
A file URI takes the form of file://host/path https://en.wikipedia.org/wiki/File_URI_scheme On windows, for example, vulndb located in c:\dir\vulndb will be file:///c:/dir/vulndb Previously, the code took `file://` prefix and attempted to use the remaining as a directory of local vulndb. On windows, that caused to os.Stat on /c:/dir/vulndb when a correctly encoded URI was passed in. Turned out file-uri parsing is a known, complex issue. See golang/go#32456 for context. This CL includes the code copied from the Go project. https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/cmd/go/internal/web/url.go Updated the tests to exercise the windows-like file path correctly when testing on windows. Previously, tests depended on relative paths or assumed an incorrect form of windows file uri (e.g. file://C:\workdir\gopath\src\golang.org\x\vuln\cmd\govulncheck/testdata/vulndb) Change-Id: I5fc451e5ca44649b9623daee28ee3210a7b2b96c Reviewed-on: https://go-review.googlesource.com/c/vuln/+/438175 Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
A file URI takes the form of file://host/path https://en.wikipedia.org/wiki/File_URI_scheme On windows, for example, vulndb located in c:\dir\vulndb will be file:///c:/dir/vulndb Previously, the code took `file://` prefix and attempted to use the remaining as a directory of local vulndb. On windows, that caused to os.Stat on /c:/dir/vulndb when a correctly encoded URI was passed in. Turned out file-uri parsing is a known, complex issue. See golang/go#32456 for context. This CL includes the code copied from the Go project. https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/cmd/go/internal/web/url.go Updated the tests to exercise the windows-like file path correctly when testing on windows. Previously, tests depended on relative paths or assumed an incorrect form of windows file uri (e.g. file://C:\workdir\gopath\src\golang.org\x\vuln\cmd\govulncheck/testdata/vulndb) Change-Id: I5fc451e5ca44649b9623daee28ee3210a7b2b96c Reviewed-on: https://go-review.googlesource.com/c/vuln/+/438175 Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
A file URI takes the form of file://host/path https://en.wikipedia.org/wiki/File_URI_scheme On windows, for example, vulndb located in c:\dir\vulndb will be file:///c:/dir/vulndb Previously, the code took `file://` prefix and attempted to use the remaining as a directory of local vulndb. On windows, that caused to os.Stat on /c:/dir/vulndb when a correctly encoded URI was passed in. Turned out file-uri parsing is a known, complex issue. See golang/go#32456 for context. This CL includes the code copied from the Go project. https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/cmd/go/internal/web/url.go Updated the tests to exercise the windows-like file path correctly when testing on windows. Previously, tests depended on relative paths or assumed an incorrect form of windows file uri (e.g. file://C:\workdir\gopath\src\golang.org\x\vuln\cmd\govulncheck/testdata/vulndb) Change-Id: I5fc451e5ca44649b9623daee28ee3210a7b2b96c Reviewed-on: https://go-review.googlesource.com/c/vuln/+/438175 Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
A file URI takes the form of file://host/path https://en.wikipedia.org/wiki/File_URI_scheme On windows, for example, vulndb located in c:\dir\vulndb will be file:///c:/dir/vulndb Previously, the code took `file://` prefix and attempted to use the remaining as a directory of local vulndb. On windows, that caused to os.Stat on /c:/dir/vulndb when a correctly encoded URI was passed in. Turned out file-uri parsing is a known, complex issue. See golang/go#32456 for context. This CL includes the code copied from the Go project. https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/cmd/go/internal/web/url.go Updated the tests to exercise the windows-like file path correctly when testing on windows. Previously, tests depended on relative paths or assumed an incorrect form of windows file uri (e.g. file://C:\workdir\gopath\src\golang.org\x\vuln\cmd\govulncheck/testdata/vulndb) Change-Id: I5fc451e5ca44649b9623daee28ee3210a7b2b96c Reviewed-on: https://go-review.googlesource.com/c/vuln/+/438175 Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
I'm not sure what detail of our own we would need to add in a design doc laying out the issues — there are already at least two such documents:
The behavior currently implemented in POSIX and Plan 9 paths should be handled as described in RFC 8089 appendix D.1, accepting three forms:
Windows paths should be handled as described in https://learn.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows, with reference to RFC 8089 appendix E.2 and E.3.
Note that using the |
In the course of investigating #27698, I learned that the conversion between URLs and file paths on Windows is rather non-trivial. (See also #6027.)
According to https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/, a file path beginning with a UNC prefix should use the UNC hostname as the “host” part of the URL, with only two leading slashes, whereas a file path beginning with a drive letter should omit the “host” part and prepend a slash to the remainder of the path.
Once you know those rules, the implementation in each direction is only about ten lines of code, but it's easy to get wrong (for example, by neglecting the possibility of a UNC prefix) if you haven't thought about it in depth.
I propose that we add two functions to the
net/url
package to make these cases more discoverable, with the following signatures:CC @alexbrainman
The text was updated successfully, but these errors were encountered: