Documentation
¶
Overview ¶
Package proxyhead implements a middleware around HTTP proxies headers.
Index ¶
- func NewMiddleware(opts ...Option) func(http.Handler) http.Handler
- func WrapHandler(next http.Handler, opts ...Option) http.Handler
- type Option
- func WithHostExtractor(extractor func([]string) string) Option
- func WithHostHeadKey(headKey string) Option
- func WithProtoExtractor(extractor func([]string) string) Option
- func WithProtoHeadKey(headKey string) Option
- func WithRemoteAddrExtractor(extractor func([]string) *netip.Addr) Option
- func WithRemoteAddrHeadKey(headKey string) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMiddleware ¶
NewMiddleware returns a middleware that injects client information into r, overriding r's original values even if information from proxy can't be found, ensuring only proxy is trusted.
Example ¶
package main
import (
"fmt"
"net/http"
"codeberg.org/kema/kmicro/pkg/http/middleware/proxyhead"
"codeberg.org/kema/kmicro/pkg/http/router"
)
func main() {
r := router.New()
r.Use(proxyhead.NewMiddleware())
// curl -H 'X-Forwarded-For: 1.2.3.4' -H 'X-Forwarded-Proto: http' -H 'X-Forwarded-Host: www.example.com' localhost:8080/
r.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "remoteAddr: %s, proto: %s, host: %s\n", r.RemoteAddr, r.Proto, r.Host)
})
_ = http.ListenAndServe("[::]:8080", r.Compile())
}
Output:
Types ¶
type Option ¶
type Option func(*config)
Option configures http.TimeoutHandler.
func WithHostExtractor ¶
WithRemoteAddrExtractor sets the extractor to use to extract host.
func WithHostHeadKey ¶
WithHostHeadKey sets the header key used to extract host.
func WithProtoExtractor ¶
WithRemoteAddrExtractor sets the extractor to use to extract protocol.
func WithProtoHeadKey ¶
WithProtoHeadKey sets the header key used to extract protocol.
func WithRemoteAddrExtractor ¶
WithRemoteAddrExtractor sets the extractor to use to extract remote address.
func WithRemoteAddrHeadKey ¶
WithRemoteAddrHeadKey sets the header key used to extract remote address.