From 72fcb20f8c5cd79ad5a16af2c9632e11ef909c4f Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 31 Jan 2019 16:05:10 +0100 Subject: [PATCH] parse url on windows: fix bug where and absolute path got prefixed with "./" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RFC 3986 ยง4.2 A path segment that contains a colon character (e.g., "this:that") cannot be used as the first segment of a relative-path reference, as it would be mistaken for a scheme name. Such a segment must be preceded by a dot-segment (e.g., "./this:that") to make a relative-path reference. Read "net/url".URL.String() for more info --- helper/url/url_test.go | 4 ++-- helper/url/url_windows.go | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/helper/url/url_test.go b/helper/url/url_test.go index ce3226b4..1015f4af 100644 --- a/helper/url/url_test.go +++ b/helper/url/url_test.go @@ -36,10 +36,10 @@ var parseTests = []parseTest{ var winParseTests = []parseTest{ { rawURL: `C:\`, - scheme: ``, + scheme: `file`, host: ``, path: `C:/`, - str: `C:/`, + str: `file://C:/`, err: false, }, { diff --git a/helper/url/url_windows.go b/helper/url/url_windows.go index 4655226f..4280ec59 100644 --- a/helper/url/url_windows.go +++ b/helper/url/url_windows.go @@ -11,19 +11,18 @@ func parse(rawURL string) (*url.URL, error) { // Make sure we're using "/" since URLs are "/"-based. rawURL = filepath.ToSlash(rawURL) + if len(rawURL) > 1 && rawURL[1] == ':' { + // Assume we're dealing with a drive letter. In which case we + // force the 'file' scheme to avoid "net/url" URL.String() prepending + // our url with "./". + rawURL = "file://" + rawURL + } + u, err := url.Parse(rawURL) if err != nil { return nil, err } - if len(rawURL) > 1 && rawURL[1] == ':' { - // Assume we're dealing with a drive letter file path where the drive - // letter has been parsed into the URL Scheme, and the rest of the path - // has been parsed into the URL Path without the leading ':' character. - u.Path = fmt.Sprintf("%s:%s", string(rawURL[0]), u.Path) - u.Scheme = "" - } - if len(u.Host) > 1 && u.Host[1] == ':' && strings.HasPrefix(rawURL, "file://") { // Assume we're dealing with a drive letter file path where the drive // letter has been parsed into the URL Host.