diff --git a/server/handle_map_style_test.go b/server/handle_map_style_test.go index 3c75d5ac..abb9997a 100644 --- a/server/handle_map_style_test.go +++ b/server/handle_map_style_test.go @@ -14,22 +14,26 @@ import ( func TestHandleMapStyle(t *testing.T) { type tcase struct { - handler http.Handler - uriPrefix string - uri string - uriPattern string - expected style.Root - } - - // config params this test relies on - server.HostName = &url.URL{ - Host: serverHostName, + handler http.Handler + uriPrefix string + uri string + uriPattern string + serverHostName string + expected style.Root } fn := func(tc tcase) func(t *testing.T) { return func(t *testing.T) { var err error + // config params this test relies on + server.HostName = nil + if tc.serverHostName != "" { + server.HostName = &url.URL{ + Host: tc.serverHostName, + } + } + if tc.uriPrefix != "" { server.URIPrefix = tc.uriPrefix } else { @@ -58,9 +62,10 @@ func TestHandleMapStyle(t *testing.T) { tests := map[string]tcase{ "default": { - handler: server.HandleMapStyle{}, - uri: path.Join("/maps", testMapName, "style.json"), - uriPattern: "/maps/:map_name/style.json", + handler: server.HandleMapStyle{}, + uri: path.Join("/maps", testMapName, "style.json"), + uriPattern: "/maps/:map_name/style.json", + serverHostName: serverHostName, expected: style.Root{ Name: testMapName, Version: style.Version, @@ -106,10 +111,11 @@ func TestHandleMapStyle(t *testing.T) { }, }, "uri prefix set": { - handler: server.HandleMapStyle{}, - uriPrefix: "/tegola", - uri: path.Join("/tegola", "maps", testMapName, "style.json"), - uriPattern: "/tegola/maps/:map_name/style.json", + handler: server.HandleMapStyle{}, + uriPrefix: "/tegola", + uri: path.Join("/tegola", "maps", testMapName, "style.json"), + uriPattern: "/tegola/maps/:map_name/style.json", + serverHostName: serverHostName, expected: style.Root{ Name: testMapName, Version: style.Version, diff --git a/server/server.go b/server/server.go index 3cba13fb..90afea91 100644 --- a/server/server.go +++ b/server/server.go @@ -143,18 +143,33 @@ func hostName(r *http.Request) *url.URL { return HostName } - return r.URL + // favor the r.URL.Host attribute in case tegola is behind a proxy + // https://stackoverflow.com/questions/42921567/what-is-the-difference-between-host-and-url-host-for-golang-http-request + if r.URL != nil && r.URL.Host != "" { + return r.URL + } + + return &url.URL{ + Host: r.Host, + } } +const ( + HeaderXForwardedProto = "X-Forwarded-Proto" +) + // various checks to determine if the request is http or https. the scheme is needed for the TileURLs // r.URL.Scheme can be empty if a relative request is issued from the client. (i.e. GET /foo.html) func scheme(r *http.Request) string { if ProxyProtocol != "" { return ProxyProtocol } - if r.Header.Get("X-Forwarded-Proto") != "" { - return r.Header.Get("X-Forwarded-Proto") - } else if r.TLS != nil { + + if r.Header.Get(HeaderXForwardedProto) != "" { + return r.Header.Get(HeaderXForwardedProto) + } + + if r.TLS != nil { return "https" } diff --git a/server/server_internal_test.go b/server/server_internal_test.go index ef64dc7e..3b4b5527 100644 --- a/server/server_internal_test.go +++ b/server/server_internal_test.go @@ -83,6 +83,11 @@ func TestHostName(t *testing.T) { url: "http://localhost/capabilities", expected: "localhost", }, + "no host on url": { + // With hostname & port unset in config, expect host:port matching URL + url: "/capabilities", + expected: "", + }, } for name, tc := range tests {