Merge pull request #56 from fredbi/fix-file-uri
loader: fixed file uri scheme loader for windows
Ivan Porto Carrero authored 3 years ago
GitHub committed 3 years ago
18 | 18 | "io/ioutil" |
19 | 19 | "log" |
20 | 20 | "net/http" |
21 | "net/url" | |
21 | 22 | "path/filepath" |
23 | "runtime" | |
22 | 24 | "strings" |
23 | 25 | "time" |
24 | 26 | ) |
52 | 54 | return remote |
53 | 55 | } |
54 | 56 | return func(pth string) ([]byte, error) { |
55 | upth, err := pathUnescape(strings.TrimPrefix(pth, `file://`)) | |
57 | upth, err := pathUnescape(pth) | |
56 | 58 | if err != nil { |
57 | 59 | return nil, err |
58 | 60 | } |
61 | ||
62 | if strings.HasPrefix(pth, `file://`) { | |
63 | if runtime.GOOS == "windows" { | |
64 | // support for canonical file URIs on windows. | |
65 | // Zero tolerance here for dodgy URIs. | |
66 | u, _ := url.Parse(upth) | |
67 | if u.Host != "" { | |
68 | // assume UNC name (volume share) | |
69 | // file://host/share/folder\... ==> \\host\share\path\folder | |
70 | // NOTE: UNC port not yet supported | |
71 | upth = strings.Join([]string{`\`, u.Host, u.Path}, `\`) | |
72 | } else { | |
73 | // file:///c:/folder/... ==> just remove the leading slash | |
74 | upth = strings.TrimPrefix(upth, `file:///`) | |
75 | } | |
76 | } else { | |
77 | upth = strings.TrimPrefix(upth, `file://`) | |
78 | } | |
79 | } | |
80 | ||
59 | 81 | return local(filepath.FromSlash(upth)) |
60 | 82 | } |
61 | 83 | } |