commands: Allow schema-less baseURL on command line - hugo - [fork] hugo port for 9front
HTML git clone git@git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
DIR commit ca6b26fe65761e0a6f29d9f5480098de7b1eae68
DIR parent 5572e3496b97ace93fea88cdbcf4dfce93e8adc2
HTML Author: Cameron Moore <moorereason@gmail.com>
Date: Thu, 12 May 2016 18:06:56 -0500
commands: Allow schema-less baseURL on command line
Fixes #1632
Diffstat:
M commands/server.go | 39 ++++++++++++++++++-------------
M commands/server_test.go | 9 +++++----
2 files changed, 28 insertions(+), 20 deletions(-)
---
DIR diff --git a/commands/server.go b/commands/server.go
@@ -209,7 +209,6 @@ func serve(port int) {
http.Handle(u.Path, http.StripPrefix(u.Path, fileserver))
}
- u.Scheme = "http"
jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", u.String(), serverInterface)
fmt.Println("Press Ctrl+C to stop")
@@ -229,37 +228,45 @@ func fixURL(s string) (string, error) {
s = viper.GetString("BaseURL")
useLocalhost = true
}
- if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") {
- s = "http://" + s
- }
+
if !strings.HasSuffix(s, "/") {
s = s + "/"
}
+
+ // do an initial parse of the input string
u, err := url.Parse(s)
if err != nil {
return "", err
}
- if serverAppend {
- if useLocalhost {
- u.Host = fmt.Sprintf("localhost:%d", serverPort)
+ // if no Host is defined, then assume that no schema or double-slash were
+ // present in the url. Add a double-slash and make a best effort attempt.
+ if u.Host == "" && s != "/" {
+ s = "//" + s
+
+ u, err = url.Parse(s)
+ if err != nil {
+ return "", err
+ }
+ }
+
+ if useLocalhost {
+ if u.Scheme == "https" {
u.Scheme = "http"
- return u.String(), nil
}
- host := u.Host
- if strings.Contains(host, ":") {
- host, _, err = net.SplitHostPort(u.Host)
+ u.Host = "localhost"
+ }
+
+ if serverAppend {
+ if strings.Contains(u.Host, ":") {
+ u.Host, _, err = net.SplitHostPort(u.Host)
if err != nil {
return "", fmt.Errorf("Failed to split BaseURL hostpost: %s", err)
}
}
- u.Host = fmt.Sprintf("%s:%d", host, serverPort)
- return u.String(), nil
+ u.Host += fmt.Sprintf(":%d", serverPort)
}
- if useLocalhost {
- u.Host = "localhost"
- }
return u.String(), nil
}
DIR diff --git a/commands/server_test.go b/commands/server_test.go
@@ -36,10 +36,11 @@ func TestFixURL(t *testing.T) {
{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar/"},
{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com/"},
{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar/"},
- {"No http", "", "foo.com", true, 1313, "http://localhost:1313/"},
- {"Override configured port", "", "foo.com:2020", true, 1313, "http://localhost:1313/"},
- {"No http production", "foo.com", "foo.com", false, 80, "http://foo.com/"},
- {"No http production with port", "foo.com", "foo.com", true, 2020, "http://foo.com:2020/"},
+ {"No http", "", "foo.com", true, 1313, "//localhost:1313/"},
+ {"Override configured port", "", "foo.com:2020", true, 1313, "//localhost:1313/"},
+ {"No http production", "foo.com", "foo.com", false, 80, "//foo.com/"},
+ {"No http production with port", "foo.com", "foo.com", true, 2020, "//foo.com:2020/"},
+ {"No config", "", "", true, 1313, "//localhost:1313/"},
}
for i, test := range tests {