Merge pull request #907 from ceh/win-urlparse

config/module: fix URL file path handling on Windows
This commit is contained in:
Mitchell Hashimoto 2015-02-02 12:29:58 +01:00
commit cfbf39d1de
2 changed files with 56 additions and 57 deletions

View File

@ -2,7 +2,6 @@ package module
import ( import (
"fmt" "fmt"
"net/url"
"path/filepath" "path/filepath"
) )
@ -67,7 +66,7 @@ func Detect(src string, pwd string) (string, error) {
} }
} }
if subDir != "" { if subDir != "" {
u, err := url.Parse(result) u, err := urlParse(result)
if err != nil { if err != nil {
return "", fmt.Errorf("Error parsing URL: %s", err) return "", fmt.Errorf("Error parsing URL: %s", err)
} }

View File

@ -9,13 +9,8 @@ import (
func urlParse(rawURL string) (*url.URL, error) { func urlParse(rawURL string) (*url.URL, error) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
if len(rawURL) > 1 && rawURL[1] == ':' { // Make sure we're using "/" on Windows. URLs are "/"-based.
// Assume we're dealing with a file path. rawURL = filepath.ToSlash(rawURL)
rawURL = fmtFileURL(rawURL)
} else {
// Make sure we're using "/" on Windows. URLs are "/"-based.
rawURL = filepath.ToSlash(rawURL)
}
} }
u, err := url.Parse(rawURL) u, err := url.Parse(rawURL)
if err != nil { if err != nil {
@ -26,13 +21,18 @@ func urlParse(rawURL string) (*url.URL, error) {
return u, err return u, err
} }
if u.Scheme != "file" { if len(rawURL) > 1 && rawURL[1] == ':' {
return u, err // Assume we're dealing with a drive letter file path on Windows.
// We need to adjust the URL Path for drive letter file paths
// because url.Parse("c:/users/user") yields URL Scheme = "c"
// and URL path = "/users/user".
u.Path = fmt.Sprintf("%s:%s", u.Scheme, u.Path)
u.Scheme = ""
} }
// Remove leading slash for absolute file paths on Windows. // Remove leading slash for absolute file paths on Windows.
// For example, url.Parse yields u.Path = "/C:/Users/user" for // For example, url.Parse yields u.Path = "/C:/Users/user" for
// rawurl = "file:///C:/Users/user", which is an incorrect syntax. // rawURL = "file:///C:/Users/user", which is an incorrect syntax.
if len(u.Path) > 2 && u.Path[0] == '/' && u.Path[2] == ':' { if len(u.Path) > 2 && u.Path[0] == '/' && u.Path[2] == ':' {
u.Path = u.Path[1:] u.Path = u.Path[1:]
} }