{"modulePath":"github.com/gomodedev/gomode","version":"v0.5.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpserver\n\nPackage httpserver provides shared HTTP request infrastructure.\n\nCallers own routing, authentication and server lifetime. Forwarded client addresses never replace the request's transport peer.\n\n## Functions\n\n```go\nfunc CanonicalOrigin(value string) (string, error)\n```\nCanonicalOrigin validates a configured public origin without exposing raw credentials in errors. It accepts HTTP(S) origins with no credentials, query, fragment or non-root path, lowercases hosts, compresses IPv6, and removes default ports and the trailing slash. Hostnames must be ASCII (use punycode for internationalized names); IPv4 must use canonical dotted-decimal form. Mapped and scoped IPv6 addresses are unsupported. Empty returns \"\", nil so callers can represent an optional origin; requiredness and HTTPS policy belong to the host.\n\n```go\nfunc ClientIP(ctx context.Context) string\n```\nClientIP returns the trusted client address resolved by NewHandler, or empty when no shared request middleware populated the context.\n\n```go\nfunc NewHandler(next http.Handler, opts Options) (http.Handler, error)\n```\nNewHandler wraps next with per-request execution trace tasks and trusted metadata and access logging, then optional origin filtering and host checks. ClientIP, Origin and RequestOrigin expose the resolved metadata even for rejected requests. The request's RemoteAddr and Host are never rewritten. Routing, authentication, cross-origin policy, server lifetime and starting execution traces stay with the caller.\n\n```go\nfunc Origin(ctx context.Context) string\n```\nOrigin returns the country code or network label resolved by NewHandler. Empty means origin enrichment was disabled or the address was unknown.\n\n```go\nfunc ParseTrustedProxies(raw []string) ([]netip.Prefix, error)\n```\nParseTrustedProxies parses CIDR prefixes and masks off host bits. An empty list trusts no proxies. It returns no prefixes if any entry is invalid.\n\n```go\nfunc RequestOrigin(ctx context.Context) string\n```\nRequestOrigin returns the request's HTTP(S) scheme and authority, using forwarding headers only from TrustedProxies. It is not the Origin header or the configured external URL. Empty means invalid or unavailable metadata.\n\n```go\nfunc ResolveClientIP(r *http.Request, prefixes []netip.Prefix) string\n```\nResolveClientIP returns the client address for r without changing it. It accepts X-Forwarded-For and X-Real-IP only from a direct peer in prefixes. For X-Forwarded-For it validates the entire chain and returns the rightmost untrusted address. Malformed headers and all-trusted chains use the direct peer. X-Forwarded-For takes precedence over X-Real-IP, including on failure. IPv4-mapped addresses are returned and compared as IPv4. Prefixes should be parsed once at startup with ParseTrustedProxies. A bare IP peer is returned directly without honoring headers. An invalid peer yields \"\".\n\n```go\nfunc TrustsAddress(addr netip.Addr, trustedProxies []netip.Prefix) bool\n```\nTrustsAddress reports whether addr belongs to a configured trusted proxy.\n\n```go\nfunc TrustsPeer(remoteAddr string, trustedProxies []netip.Prefix) bool\n```\nTrustsPeer reports whether remoteAddr is the address of a configured trusted proxy. A trusted peer must use the host:port form supplied by net/http.\n\n## Types\n\n```go\ntype AccessLog struct {\n\thttp.Handler\n\n\tLogger *slog.Logger\n\t// SuccessLevel controls logging for responses below 300. Zero is Info.\n\tSuccessLevel slog.Level\n}\n```\nAccessLog wraps an HTTP handler and logs each request after it completes.\n\nIt records the response status, transmitted byte count, duration, and hijack state. The ip field uses ClientIP from request context; origin is included only when Origin is nonempty. NewHandler populates this metadata. Responses below 300 use SuccessLevel; all other responses are logged at info level.\n\n```go\nfunc (h AccessLog) ServeHTTP(w http.ResponseWriter, r *http.Request)\n```\nServeHTTP implements http.Handler.\n\n```go\ntype HostPolicy struct {\n\t// contains filtered or unexported fields\n}\n```\nHostPolicy enforces one public authority and publishes its external URL. Proxy trust belongs to Options.TrustedProxies, not the policy. Only the request middleware may discover an origin; ExternalURL is a read-only view. A policy is shared by one server and its OAuth/redirect consumers.\n\n```go\nfunc NewHostPolicy(opts HostPolicyOptions) (*HostPolicy, error)\n```\nNewHostPolicy validates a configured external URL or enables discovery. Authorities are compared with canonical host spelling and default ports interpreted using the configured public scheme. The configured scheme describes the public URL, not the backend transport: HTTPS termination may forward HTTP to the server.\n\n```go\nfunc (s *HostPolicy) ExternalURL() string\n```\nExternalURL returns the configured or discovered public origin, or empty before an automatic policy has accepted its first FQDN request. It never inspects a request or changes policy state.\n\n```go\ntype HostPolicyOptions struct {\n\t// ExternalURL is the configured HTTP(S) public origin. Empty discovers and\n\t// locks the origin from the first accepted FQDN request.\n\tExternalURL string\n\t// AllowLocal permits IP addresses and single-label hosts without changing\n\t// the external URL. Local requests never establish an automatic origin.\n\tAllowLocal bool\n}\n```\nHostPolicyOptions selects authority enforcement and external URL discovery.\n\n```go\ntype Options struct {\n\t// TrustedProxies authorizes forwarded client-IP and host/scheme headers.\n\t// It does not restrict which peers may connect. Empty trusts no proxies.\n\t// Trusted proxies must replace origin headers with authoritative values.\n\t// Forwarded or paired X-Forwarded-Host/X-Forwarded-Proto are accepted;\n\t// malformed or inconsistent trusted origin headers fail the host check.\n\t// Untrusted peers' forwarding headers are ignored.\n\tTrustedProxies []netip.Prefix\n\t// Hosts enables authority enforcement and external URL discovery. Nil allows\n\t// any host. Proxy trust always comes from TrustedProxies. Rejected\n\t// authorities return HTTP 403 and are access-logged.\n\tHosts *HostPolicy\n\tGeoIP OriginChecker\n\t// Logger is required for access logging. Use slog.DiscardHandler to discard logs.\n\tLogger *slog.Logger\n\t// SuccessLevel controls logging for responses below 300. Zero is Info.\n\tSuccessLevel slog.Level\n}\n```\nOptions selects host policies for NewHandler. Immutable after construction.\n\n```go\ntype OriginChecker interface {\n\tCheckOrigin(clientIP string) (origin string, allowed bool)\n}\n```\nOriginChecker resolves a client address and optionally restricts its origin. Implementations must support concurrent requests. The optional geoip package supplies MMDB enrichment and named-network allowlists.\n\n","path":"github.com/gomodedev/gomode/httpserver","name":"httpserver","synopsis":"Package httpserver provides shared HTTP request infrastructure.","isRedistributable":true}
