Skip to content

Commit

Permalink
server: Add additional checks to hostName func
Browse files Browse the repository at this point in the history
The hostName func is used to dynamically determine
the hostName value in various API endpoints. The previous
logic favored the Host value of request URL, but this value
is often not set (i.e. when not operating behind a proxy).
The default handling has been changed to return the
request Host value if the request URL Host is not set.

closes #1022
  • Loading branch information
ARolek committed Dec 23, 2024
1 parent 6d0f098 commit 60841ec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
40 changes: 23 additions & 17 deletions server/handle_map_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 19 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
5 changes: 5 additions & 0 deletions server/server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 60841ec

Please sign in to comment.