Commit 7191c1f2 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

common: Fix URL parsing issues on Windows

parent 159587da
...@@ -28,6 +28,7 @@ IMPROVEMENTS: ...@@ -28,6 +28,7 @@ IMPROVEMENTS:
BUG FIXES: BUG FIXES:
* core: Errors are properly shown when adding bad floppy files. [GH-1043] * core: Errors are properly shown when adding bad floppy files. [GH-1043]
* core: Fix some URL parsing issues on Windows.
* builder/amazon-instance: Use S3Endpoint for ec2-upload-bundle arg, * builder/amazon-instance: Use S3Endpoint for ec2-upload-bundle arg,
which works for every region. [GH-904] which works for every region. [GH-904]
* builder/digitalocean: updated default image_id [GH-1032] * builder/digitalocean: updated default image_id [GH-1032]
......
...@@ -101,15 +101,20 @@ func DownloadableURL(original string) (string, error) { ...@@ -101,15 +101,20 @@ func DownloadableURL(original string) (string, error) {
} }
if url.Scheme == "file" { if url.Scheme == "file" {
// For Windows absolute file paths, remove leading / prior to processing // Windows file handling is all sorts of tricky...
// since net/url turns "C:/" into "/C:/" if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" && url.Path[0] == '/' { // If the path is using Windows-style slashes, URL parses
url.Path = url.Path[1:len(url.Path)] // it into the host field.
if url.Path == "" && strings.Contains(url.Host, `\`) {
url.Path = url.Host
url.Host = ""
}
// Also replace all backslashes with forwardslashes since Windows // For Windows absolute file paths, remove leading / prior to processing
// users are likely to do this but the URL should actually only // since net/url turns "C:/" into "/C:/"
// contain forward slashes. if len(url.Path) > 0 && url.Path[0] == '/' {
url.Path = strings.Replace(url.Path, `\`, `/`, -1) url.Path = url.Path[1:len(url.Path)]
}
} }
// Only do the filepath transformations if the file appears // Only do the filepath transformations if the file appears
...@@ -127,6 +132,13 @@ func DownloadableURL(original string) (string, error) { ...@@ -127,6 +132,13 @@ func DownloadableURL(original string) (string, error) {
url.Path = filepath.Clean(url.Path) url.Path = filepath.Clean(url.Path)
} }
if runtime.GOOS == "windows" {
// Also replace all backslashes with forwardslashes since Windows
// users are likely to do this but the URL should actually only
// contain forward slashes.
url.Path = strings.Replace(url.Path, `\`, `/`, -1)
}
} }
// Make sure it is lowercased // Make sure it is lowercased
...@@ -195,11 +207,13 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) { ...@@ -195,11 +207,13 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
}, nil }, nil
} }
func CoalesceVals(vals ...string) string { // ChooseString returns the first non-empty value.
func ChooseString(vals ...string) string {
for _, el := range vals { for _, el := range vals {
if el != "" { if el != "" {
return el return el
} }
} }
return "" return ""
} }
...@@ -7,6 +7,8 @@ import ( ...@@ -7,6 +7,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime"
"strings"
"testing" "testing"
) )
...@@ -141,6 +143,11 @@ func TestDownloadableURL_FilePaths(t *testing.T) { ...@@ -141,6 +143,11 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
tfPath = filepath.Clean(tfPath) tfPath = filepath.Clean(tfPath)
filePrefix := "file://"
if runtime.GOOS == "windows" {
filePrefix += "/"
}
// Relative filepath. We run this test in a func so that // Relative filepath. We run this test in a func so that
// the defers run right away. // the defers run right away.
func() { func() {
...@@ -161,8 +168,11 @@ func TestDownloadableURL_FilePaths(t *testing.T) { ...@@ -161,8 +168,11 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if u != fmt.Sprintf("file://%s", tfPath) { expected := fmt.Sprintf("%s%s",
t.Fatalf("unexpected: %s", u) filePrefix,
strings.Replace(tfPath, `\`, `/`, -1))
if u != expected {
t.Fatalf("unexpected: %#v != %#v", u, expected)
} }
}() }()
...@@ -180,8 +190,11 @@ func TestDownloadableURL_FilePaths(t *testing.T) { ...@@ -180,8 +190,11 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if u != fmt.Sprintf("file://%s", tfPath) { expected := fmt.Sprintf("%s%s",
t.Fatalf("unexpected: %s", u) filePrefix,
strings.Replace(tfPath, `\`, `/`, -1))
if u != expected {
t.Fatalf("unexpected: %s != %s", u, expected)
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment