package web import ( "net/http" "net/url" ) // RelativeEndpoint takes a path component of URL and constructs a new URL using // the host and port from the request combined the provided path. func RelativeEndpoint(request *http.Request, endpoint string) string { var result string proto := "http" host := request.Host // If the request was received via TLS, use `https://` for the protocol if request.TLS != nil { proto = "https" } // Allow upstream proxies to specify the forwarded protocol. Allow this value // to override our own guess. if specifiedProto := request.Header.Get("X-Forwarded-Proto"); specifiedProto != "" { proto = specifiedProto } // Default to "localhost" when no request.Host is provided. Otherwise requests // with an empty `Host` produce results like `http:///acme/new-authz` if request.Host == "" { host = "localhost" } resultUrl := url.URL{Scheme: proto, Host: host, Path: endpoint} result = resultUrl.String() return result }