Documentation
¶
Index ¶
- Constants
- func Add(h http.Header, key, value string)
- func AddIfNotExists(h http.Header, key, value string)
- func Del(h http.Header, key string)
- func Exists(h http.Header, key string) bool
- func Get(h http.Header, key string) string
- func Set(h http.Header, key, value string)
- func SetShared(h http.Header, key string, vs []string)
Examples ¶
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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.
Types ¶
This section is empty.