// Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package url provides functionality for converting file paths to file URLs. // // Code copied from // https://github.com/golang/go/blob/7c2b69080a0b9e35174cc9c93497b6e7176f8275/src/cmd/go/internal/web/url.go // TODO(golang.org/issue/32456): If accepted, move these functions into the // net/url package. package url import ( "errors" "net/url" "path/filepath" "strings" ) var errNotAbsolute = errors.New("path is not absolute") func FromFilePath(path string) (*url.URL, error) { if !filepath.IsAbs(path) { return nil, errNotAbsolute } // If path has a Windows volume name, convert the volume to a host and prefix // per https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/. if vol := filepath.VolumeName(path); vol != "" { if strings.HasPrefix(vol, `\\`) { path = filepath.ToSlash(path[2:]) i := strings.IndexByte(path, '/') if i < 0 { // A degenerate case. // \\host.example.com (without a share name) // becomes // file://host.example.com/ return &url.URL{ Scheme: "file", Host: path, Path: "/", }, nil } // \\host.example.com\Share\path\to\file // becomes // file://host.example.com/Share/path/to/file return &url.URL{ Scheme: "file", Host: path[:i], Path: filepath.ToSlash(path[i:]), }, nil } // C:\path\to\file // becomes // file:///C:/path/to/file return &url.URL{ Scheme: "file", Path: "/" + filepath.ToSlash(path), }, nil } // /path/to/file // becomes // file:///path/to/file return &url.URL{ Scheme: "file", Path: filepath.ToSlash(path), }, nil }