forward

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package forward provides a reusable HTTP forward proxy handler for absolute URI requests.

The package owns request normalization, hop-by-hop header stripping, forwarding header policy, outbound transport execution, and neutral observation hooks. CONNECT tunneling, WebSocket upgrades, persistence, and app-specific monitoring remain outside the package.

Example
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s %s %s", r.Method, r.URL.Path, r.Header.Get("X-Proxy-Mode"))
}))
defer upstream.Close()

handler := New(Options{
	MutateRequest: func(_ context.Context, req *http.Request) error {
		req.Header.Set("X-Proxy-Mode", "forward")
		return nil
	},
})

req := httptest.NewRequest(http.MethodGet, upstream.URL+"/status", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)

fmt.Println(rec.Code)
fmt.Println(strings.TrimSpace(rec.Body.String()))
Output:
200
GET /status forward

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrAbsoluteURLRequired = errors.New("forward: absolute url required")
	ErrConnectNotSupported = errors.New("forward: CONNECT is not supported")
)

Functions

This section is empty.

Types

type ErrorWriter

type ErrorWriter func(http.ResponseWriter, *http.Request, int, error)

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func New

func New(opts Options) *Handler

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Options

type Options struct {
	RoundTripper            http.RoundTripper
	GenerateSessionID       func() string
	ObserveRequest          func(*http.Request) bool
	WriteError              ErrorWriter
	MutateRequest           RequestMutator
	MutateResponse          ResponseMutator
	Hooks                   observe.Hooks
	ForwardedHeaders        proxyhttp.ForwardedHeaderConfig
	SampleRequestBodyBytes  int
	SampleResponseBodyBytes int
}

type RequestMutator

type RequestMutator func(context.Context, *http.Request) error

type ResponseMutator

type ResponseMutator func(context.Context, *http.Request, *http.Response) error

Jump to

Keyboard shortcuts

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