From efeeece73543c5ec7d94a5eb942583b366775c2a Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 29 Oct 2015 10:13:30 -0600 Subject: [PATCH] caddyfile: http and https hosts should render in URL format --- caddy/caddyfile/json.go | 6 ++++++ caddy/caddyfile/json_test.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/caddy/caddyfile/json.go b/caddy/caddyfile/json.go index 4617ec035..42171e7a6 100644 --- a/caddy/caddyfile/json.go +++ b/caddy/caddyfile/json.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "net" "strconv" "strings" @@ -104,6 +105,11 @@ func FromJSON(jsonBytes []byte) ([]byte, error) { for _, sb := range j { for i, host := range sb.Hosts { + if hostname, port, err := net.SplitHostPort(host); err == nil { + if port == "http" || port == "https" { + host = port + "://" + hostname + } + } if i > 0 { result += ", " } diff --git a/caddy/caddyfile/json_test.go b/caddy/caddyfile/json_test.go index 2e5ed445b..44abf982f 100644 --- a/caddy/caddyfile/json_test.go +++ b/caddy/caddyfile/json_test.go @@ -59,6 +59,11 @@ baz" }`, json: `[{"hosts":["host"],"body":{"dir":["123","4.56","true"]}}]`, // NOTE: I guess we assume numbers and booleans should be encoded as strings...? }, + { // 8 + caddyfile: `http://host, https://host { +}`, + json: `[{"hosts":["host:http","host:https"],"body":{}}]`, // hosts in JSON are always host:port format (if port is specified) + }, } func TestToJSON(t *testing.T) {