header

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 1 Imported by: 4

Documentation

Index

Examples

Constants

View Source
const (
	AcceptEncoding                = "Accept-Encoding"
	AccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	AccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	AccessControlAllowMethods     = "Access-Control-Allow-Methods"
	AccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	AccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	AccessControlMaxAge           = "Access-Control-Max-Age"
	AccessControlRequestHeaders   = "Access-Control-Request-Headers"
	AccessControlRequestMethod    = "Access-Control-Request-Method"
	Authorization                 = "Authorization"
	ContentEncoding               = "Content-Encoding"
	ContentLength                 = "Content-Length"
	ContentType                   = "Content-Type"
	Origin                        = "Origin"
	RetryAfter                    = "Retry-After"
	SecWebsocketKey               = "Sec-Websocket-Key"
	StrictTransportSecurity       = "Strict-Transport-Security"
	Upgrade                       = "Upgrade"
	Vary                          = "Vary"
	WWWAuthenticate               = "Www-Authenticate"
	XForwardedFor                 = "X-Forwarded-For"
	XForwardedHost                = "X-Forwarded-Host"
	XForwardedMethod              = "X-Forwarded-Method"
	XForwardedProto               = "X-Forwarded-Proto"
	XForwardedURI                 = "X-Forwarded-Uri"
	XRealIP                       = "X-Real-Ip"
	XRequestID                    = "X-Request-Id"
)

Headers in canonical format

Variables

This section is empty.

Functions

func Add

func Add(h http.Header, key, value string)
Example

Add appends a value, keeping any values already present — the way you build a multi-valued header such as Vary.

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	h := http.Header{}
	header.Add(h, header.Vary, "Origin")
	header.Add(h, header.Vary, "Accept-Encoding")

	fmt.Println(h[header.Vary])
}
Output:
[Origin Accept-Encoding]

func AddIfNotExists

func AddIfNotExists(h http.Header, key, value string)
Example

AddIfNotExists appends a value only when that exact value is not already present, so re-running it never duplicates an entry.

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	h := http.Header{}
	header.AddIfNotExists(h, header.Vary, "Origin")
	header.AddIfNotExists(h, header.Vary, "Origin") // no-op, already there

	fmt.Println(h[header.Vary])
}
Output:
[Origin]

func Del

func Del(h http.Header, key string)
Example

Del removes a key. It is nil-safe — deleting from a nil header is a no-op rather than a panic.

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	h := http.Header{}
	header.Set(h, header.Authorization, "Bearer t0ken")
	header.Del(h, header.Authorization)

	fmt.Println(header.Exists(h, header.Authorization))
}
Output:
false

func Exists

func Exists(h http.Header, key string) bool
Example

Exists reports whether a key is set to a non-empty value. A present-but-empty value counts as absent.

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	h := http.Header{}
	header.Set(h, header.Authorization, "Bearer t0ken")

	fmt.Println(header.Exists(h, header.Authorization), header.Exists(h, header.Origin))
}
Output:
true false

func Get

func Get(h http.Header, key string) string
Example

Get returns the first value for a key, or "" when the key is absent or the header map is nil. It reads the key verbatim — use the canonical constants.

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	h := http.Header{}
	header.Set(h, header.XRequestID, "abc-123")

	fmt.Printf("id=%q missing=%q\n", header.Get(h, header.XRequestID), header.Get(h, header.Origin))
}
Output:
id="abc-123" missing=""

func Set

func Set(h http.Header, key, value string)
Example

Set replaces any existing values for a key with a single value. Unlike http.Header.Set it does NOT canonicalize the key, so pair it with this package's canonical-name constants (header.ContentType, header.XRequestID, …).

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	h := http.Header{}
	header.Set(h, header.ContentType, "application/json")
	header.Set(h, header.ContentType, "text/plain") // replaces, not appends

	fmt.Println(h.Get("Content-Type"))
}
Output:
text/plain

func SetShared added in v0.15.0

func SetShared(h http.Header, key string, vs []string)

SetShared assigns a pre-built value slice directly into the header map, sharing its backing array across every call instead of allocating a fresh []string{value} per request the way Set does.

Use it only for values fixed at construction time, and only on response headers: the shared slice must be treated as immutable. parapet's own middleware never mutate response header value slices in place — MapResponse rebuilds the slice, and only MapRequest mutates in place (request headers only) — so sharing is safe response-side. The cors middleware shares its precomputed header slices the same way.

Example

SetShared assigns a pre-built value slice into the header map, sharing its backing array across every call instead of allocating a fresh []string per request. It is a hot-path optimization for response-header values that are fixed at construction time and treated as immutable.

package main

import (
	"fmt"
	"net/http"

	"github.com/moonrhythm/parapet/pkg/header"
)

func main() {
	// Built once, e.g. at middleware construction.
	hsts := []string{"max-age=63072000; includeSubDomains; preload"}

	// Reused on every response without re-allocating the slice.
	resp := http.Header{}
	header.SetShared(resp, header.StrictTransportSecurity, hsts)

	fmt.Println(resp.Get("Strict-Transport-Security"))
}
Output:
max-age=63072000; includeSubDomains; preload

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL