Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( // HeaderForwarded contains the key of the Forwarded request header. HeaderForwarded = "Forwarded" // SchemeHttp contains the URL scheme for HTTP. SchemeHttp = "http" // SchemeHttps contains the URL scheme for HTTPS. SchemeHttps = "https" )
const ( // HeaderXForwardedHost contains the key for the X-Forwarded-Proto request header. HeaderXForwardedProto = "X-Forwarded-Proto" // HeaderXForwardedHost contains the key for the X-Forwarded-Host request header. HeaderXForwardedHost = "X-Forwarded-Host" )
Variables ¶
This section is empty.
Functions ¶
func Forwarded ¶
Forwarded is a URLRewriter which updates URL fields based on Forwarded header as specified in RFC 7239 (https://datatracker.ietf.org/doc/html/rfc7239). Sepcificially, the following values are always set:
- r.URL.Scheme - the protocol being used (http or https) - r.URL.Host - the full host and port as specified by the client
func Middleware ¶
func Middleware(h http.Handler, rewriter ...URLRewriter) http.Handler
Middleware creates a http middleware by wrapping h in a new http.Handler that completes the request's URL field with protocol (scheme) and host, which are left empty by default. h may use r.URL to rebuild the full URL issued by the client when making the request. The list of URL rewriters may update the request's URL based on other request entities, such as a Forwarded-Header when running behind a reverse proxy.
Example (BehindProxy) ¶
package main
import (
"fmt"
"net/http"
"github.com/halimath/httputils/requesturi"
)
func main() {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprintf("%s://%s/some/path", r.URL.Scheme, r.URL.Host), http.StatusTemporaryRedirect)
})
http.ListenAndServe(":1234", requesturi.Middleware(h, requesturi.Forwarded, requesturi.XForwarded))
}
func XForwarded ¶
XForwarded is a URLRewriter that rewrites the r's URL based on the X-Forwarded-* headers. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded for technical details.
Types ¶
type URLRewriter ¶
URLRewriter is a function that updates a requests URL.
func RewritePath ¶
func RewritePath(mapping map[string]string) (URLRewriter, error)
RewritePath creates a URLRewriter func that rewrites URL paths based on mapping. The keys to mapping are compiled as patterns using github.com/halimath/glob. The values are the full paths to rewrite the path to. Patterns are compiled when RewritePath is invoked.