]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package url |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "net/url" | |
6 | "path/filepath" | |
7 | "strings" | |
8 | ) | |
9 | ||
10 | func parse(rawURL string) (*url.URL, error) { | |
11 | // Make sure we're using "/" since URLs are "/"-based. | |
12 | rawURL = filepath.ToSlash(rawURL) | |
13 | ||
107c1cdb ND |
14 | if len(rawURL) > 1 && rawURL[1] == ':' { |
15 | // Assume we're dealing with a drive letter. In which case we | |
16 | // force the 'file' scheme to avoid "net/url" URL.String() prepending | |
17 | // our url with "./". | |
18 | rawURL = "file://" + rawURL | |
19 | } | |
20 | ||
bae9f6d2 JC |
21 | u, err := url.Parse(rawURL) |
22 | if err != nil { | |
23 | return nil, err | |
24 | } | |
25 | ||
bae9f6d2 JC |
26 | if len(u.Host) > 1 && u.Host[1] == ':' && strings.HasPrefix(rawURL, "file://") { |
27 | // Assume we're dealing with a drive letter file path where the drive | |
28 | // letter has been parsed into the URL Host. | |
29 | u.Path = fmt.Sprintf("%s%s", u.Host, u.Path) | |
30 | u.Host = "" | |
31 | } | |
32 | ||
33 | // Remove leading slash for absolute file paths. | |
34 | if len(u.Path) > 2 && u.Path[0] == '/' && u.Path[2] == ':' { | |
35 | u.Path = u.Path[1:] | |
36 | } | |
37 | ||
38 | return u, err | |
39 | } |