headers

package
v0.18.4 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	Key   string
	Value string
}

Header type

type RequestInterceptor

type RequestInterceptor struct {
	Intercept func(http.Header)
}

RequestInterceptor intercepts request's headers

func AddRequest

func AddRequest(headerpairs ...string) *RequestInterceptor

AddRequest creates new request interceptor for add headers.

Keys are canonicalized once at construction; the per-request path appends directly to the header map, skipping CanonicalMIMEHeaderKey in http.Header.Add.

Example

Add request headers, appending to any existing values rather than replacing them. Handy for flags a backend may set more than once.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/headers"
)

func main() {
	s := parapet.New()
	s.Use(headers.AddRequest("X-Forwarded-Proto", "https"))
}

func DeleteRequest

func DeleteRequest(headers ...string) *RequestInterceptor

DeleteRequest creates new request interceptor for delete headers.

Keys are canonicalized once at construction so the per-request path can use a direct map delete instead of http.Header.Del, which scans the key through CanonicalMIMEHeaderKey on every call.

func InterceptRequest

func InterceptRequest(f func(http.Header)) *RequestInterceptor

InterceptRequest creates new request interceptor

func MapRequest

func MapRequest(header string, mapper func(string) string) *RequestInterceptor

MapRequest creates new request interceptor for map a header

Example

Rewrite each value of a request header in place. Here the inbound Host is lower-cased before it is proxied; MapRequest only touches values that are already present.

package main

import (
	"strings"

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

func main() {
	s := parapet.New()
	s.Use(headers.MapRequest("Host", strings.ToLower))
}

func SetRequest

func SetRequest(headerpairs ...string) *RequestInterceptor

SetRequest creates new request interceptor for set headers.

Keys are canonicalized once at construction; the per-request path writes directly to the header map, skipping CanonicalMIMEHeaderKey in http.Header.Set. A fresh []string{value} is allocated per request — sharing a pre-built slice would let downstream in-place mutations (e.g. MapRequest) leak across requests.

func (RequestInterceptor) ServeHandler

func (m RequestInterceptor) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

type ResponseHeaderWriter added in v0.7.0

type ResponseHeaderWriter interface {
	StatusCode() int
	Header() http.Header
	WriteHeader(statusCode int)
}

ResponseHeaderWriter type

type ResponseInterceptFunc added in v0.7.0

type ResponseInterceptFunc func(w ResponseHeaderWriter)

ResponseInterceptFunc is the function for response's interceptor

type ResponseInterceptor

type ResponseInterceptor struct {
	Intercept ResponseInterceptFunc
}

ResponseInterceptor intercepts response's headers

func AddResponse

func AddResponse(headerpairs ...string) *ResponseInterceptor

AddResponse creates new response interceptor for add headers.

func DeleteResponse

func DeleteResponse(headers ...string) *ResponseInterceptor

DeleteResponse creates new response interceptor for delete headers.

Example

Strip hop-by-hop or sensitive headers from the upstream's response before it reaches the client.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/headers"
)

func main() {
	s := parapet.New()
	s.Use(headers.DeleteResponse("Server", "X-Powered-By"))
}

func InterceptResponse

func InterceptResponse(f ResponseInterceptFunc) *ResponseInterceptor

InterceptResponse creates new response interceptor

Example

InterceptResponse runs arbitrary logic against the response headers, with access to the final status code via the ResponseHeaderWriter. Here a long-lived Cache-Control is added only to successful responses.

package main

import (
	"net/http"

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

func main() {
	s := parapet.New()
	s.Use(headers.InterceptResponse(func(w headers.ResponseHeaderWriter) {
		if w.StatusCode() == http.StatusOK {
			w.Header().Set("Cache-Control", "public, max-age=3600")
		}
	}))
}

func MapResponse

func MapResponse(header string, mapper func(string) string) *ResponseInterceptor

MapResponse creates new response interceptor for map a header

func SetResponse

func SetResponse(headerpairs ...string) *ResponseInterceptor

SetResponse creates new response interceptor for set headers.

Example

Set response headers, overwriting any existing values. Pairs are given as key, value, key, value... Use SetRequest to rewrite headers on the way in to the upstream instead.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/headers"
)

func main() {
	s := parapet.New()
	s.Use(headers.SetResponse(
		"X-Frame-Options", "DENY",
		"X-Content-Type-Options", "nosniff",
	))
	// s.Use(upstream.SingleHost("10.0.0.1:8080")) — the proxied backend.
}

func (ResponseInterceptor) ServeHandler

func (m ResponseInterceptor) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

Jump to

Keyboard shortcuts

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