github.com/letsencrypt/boulder@v0.20251208.0/web/relative.go (about) 1 package web 2 3 import ( 4 "net/http" 5 "net/url" 6 ) 7 8 // RelativeEndpoint takes a path component of URL and constructs a new URL using 9 // the host and port from the request combined the provided path. 10 func RelativeEndpoint(request *http.Request, endpoint string) string { 11 var result string 12 proto := "http" 13 host := request.Host 14 15 // If the request was received via TLS, use `https://` for the protocol 16 if request.TLS != nil { 17 proto = "https" 18 } 19 20 // Allow upstream proxies to specify the forwarded protocol. Allow this value 21 // to override our own guess. 22 if specifiedProto := request.Header.Get("X-Forwarded-Proto"); specifiedProto != "" { 23 proto = specifiedProto 24 } 25 26 // Default to "localhost" when no request.Host is provided. Otherwise requests 27 // with an empty `Host` produce results like `http:///acme/new-authz` 28 if request.Host == "" { 29 host = "localhost" 30 } 31 32 resultUrl := url.URL{Scheme: proto, Host: host, Path: endpoint} 33 result = resultUrl.String() 34 35 return result 36 }