Documentation
¶
Overview ¶
Package proxy provides a reusable dynamic reverse proxy handler.
The package is independent of application-level request formats. Callers supply a Resolver that converts an incoming request into a Plan, and the handler applies that plan to a standard reverse proxy.
Example:
handler := proxy.NewHandler(resolver, transport, proxy.HandlerOptions{})
_ = handler
Index ¶
- Variables
- func BuildRoundTripRequest(template *RequestTemplate, plan *Plan, ctx context.Context, body io.ReadCloser) *http.Request
- func IsResponseHeaderProtected(name string) bool
- func NewProxyAwareTransport(base *http.Transport) *http.Transport
- func NewProxyAwareTransportWithOptions(base *http.Transport, opts ProxyTransportOptions) *http.Transport
- func WithRequestProxy(req *http.Request, proxyURL *url.URL) *http.Request
- func WriteProxyErrorJSON(w http.ResponseWriter, status int, code, message string)
- type BodyPolicy
- type DirectiveSource
- type Handler
- type HandlerOptions
- type Plan
- type PreparedDirective
- func (prepared *PreparedDirective) BodyPolicy() *BodyPolicy
- func (prepared *PreparedDirective) Metadata() metadata.Set
- func (prepared *PreparedDirective) Plan() *Plan
- func (prepared *PreparedDirective) Program() *program.Executable
- func (prepared *PreparedDirective) Recovery() *recovery.Policy
- func (prepared *PreparedDirective) Source() DirectiveSource
- type ProxyTransportOptions
- type RecoveryTransport
- type RecoveryTransportOptions
- type RequestMetrics
- type RequestTemplate
- type Resolver
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrResolverFailed = errors.New("proxy directive resolver failed") ErrModuleFailed = errors.New("proxy module failed") ErrRecoveryFailed = errors.New("proxy recovery failed") )
var ( ErrNoMatch = errors.New("proxy resolver did not match request") ErrInvalidDirective = errors.New("invalid proxy directive") ErrDirectiveNotFound = errors.New("directive reference not found") ErrDirectiveTokenTooLarge = errors.New("directive token is too large") ErrRemoteDirectiveInvalid = errors.New("remote directive is invalid") )
Functions ¶
func BuildRoundTripRequest ¶ added in v0.67.260718
func BuildRoundTripRequest(template *RequestTemplate, plan *Plan, ctx context.Context, body io.ReadCloser) *http.Request
func NewProxyAwareTransportWithOptions ¶
func NewProxyAwareTransportWithOptions(base *http.Transport, opts ProxyTransportOptions) *http.Transport
func WriteProxyErrorJSON ¶
func WriteProxyErrorJSON(w http.ResponseWriter, status int, code, message string)
Types ¶
type BodyPolicy ¶ added in v0.71.260719
type DirectiveSource ¶ added in v0.67.260718
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
func NewHandler(resolver Resolver, transport http.RoundTripper, opts HandlerOptions) *Handler
Example ¶
package main
import (
"net/http"
"net/url"
"github.com/lwmacct/260628-directive-proxy/internal/core/httpheader"
)
type staticResolver struct{}
func (staticResolver) Prepare(*http.Request) (*PreparedDirective, error) {
target, _ := url.Parse("https://api.example.com/v1")
return NewPreparedDirective(DirectiveSource{Mode: "inline"}, &Plan{
Target: target,
Headers: httpheader.Plan{Request: httpheader.RequestPlan{Ops: []httpheader.Op{{
Action: httpheader.ActionSet,
Selector: httpheader.Selector{
Kind: httpheader.SelectorExact,
Pattern: "Authorization",
},
Values: []string{"Bearer upstream-token"},
}}}},
}, nil, nil, proxyTestMetadata())
}
func main() {
handler := NewHandler(staticResolver{}, http.DefaultTransport, HandlerOptions{})
_ = handler
}
Output:
type HandlerOptions ¶
type HandlerOptions struct {
ExchangeFactory exchangeStarter
TrackBeforeResolve bool
BodyStore *bodystore.Controller
BodyReadTimeout time.Duration
BodyMaxBytes int64
BodyQueueWait time.Duration
BodyChunkBytes int
RequestMetrics RequestMetrics
// Next receives requests for which Resolver returns ErrNoMatch.
Next http.Handler
}
type PreparedDirective ¶
type PreparedDirective struct {
// contains filtered or unexported fields
}
PreparedDirective is one immutable compilation result. Remote dereference, Payload validation and Program compilation are complete before this value is constructed; every RoundTrip consumes the same Plan and Recovery policy.
func NewPreparedDirective ¶ added in v0.67.260718
func NewPreparedDirective(source DirectiveSource, plan *Plan, executable *program.Executable, policy *recovery.Policy, fields metadata.Set, bodyPolicy ...*BodyPolicy) (*PreparedDirective, error)
func (*PreparedDirective) BodyPolicy ¶ added in v0.71.260719
func (prepared *PreparedDirective) BodyPolicy() *BodyPolicy
func (*PreparedDirective) Metadata ¶ added in v0.67.260718
func (prepared *PreparedDirective) Metadata() metadata.Set
func (*PreparedDirective) Plan ¶ added in v0.67.260718
func (prepared *PreparedDirective) Plan() *Plan
func (*PreparedDirective) Program ¶ added in v0.67.260718
func (prepared *PreparedDirective) Program() *program.Executable
func (*PreparedDirective) Recovery ¶ added in v0.67.260718
func (prepared *PreparedDirective) Recovery() *recovery.Policy
func (*PreparedDirective) Source ¶
func (prepared *PreparedDirective) Source() DirectiveSource
type ProxyTransportOptions ¶
type RecoveryTransport ¶ added in v0.63.260717
type RecoveryTransport struct {
// contains filtered or unexported fields
}
func NewRecoveryTransport ¶ added in v0.63.260717
func NewRecoveryTransport(base http.RoundTripper, options RecoveryTransportOptions) (*RecoveryTransport, error)
type RecoveryTransportOptions ¶ added in v0.63.260717
type RequestMetrics ¶ added in v0.71.260719
type RequestTemplate ¶
type RequestTemplate struct {
Method string
URL *url.URL
Host string
Header http.Header
Trailer http.Header
TransferEncoding []string
Proto string
ProtoMajor int
ProtoMinor int
ContentLength int64
Close bool
IdempotencyKey string
}
RequestTemplate is an immutable snapshot of the original inbound request metadata. Bodies are replayed separately; every round trip is rebuilt from this snapshot so routing and header mutations cannot leak between round trips.
func NewRequestTemplate ¶
func NewRequestTemplate(req *http.Request) *RequestTemplate