httpproxy

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const StatusClientClosedRequest = 499

StatusClientClosedRequest is a custom status code to indicate that the client closed the request.

View Source
const StatusClientClosedRequestText = "Client Closed Request"

Variables

This section is empty.

Functions

func AnswerErrorStatus

func AnswerErrorStatus(w http.ResponseWriter, r *http.Request, statusCode int)

func GetStatusCode

func GetStatusCode(err error) int

func StatusText

func StatusText(statusCode int) string

func StripHostPort

func StripHostPort(h string) string

Types

type FilterFunc

type FilterFunc[RCTX any] func(http.ResponseWriter, *http.Request, *RCTX) bool

type HTTPProxyService

type HTTPProxyService[RCTX any] interface {
	// Request context data
	NewRCTX() *RCTX

	// Get the upstream URL to proxy the request to
	// If *url.URL is nil then a 502 Bad Gateway is sent to downstream
	GetUpstream(*http.Request, *RCTX) *url.URL
}

type Options

type Options struct {
	FlushInterval time.Duration
	RoundTripper  http.RoundTripper
	BufferPool    httputil.BufferPool
	ErrorLog      *log.Logger
}

type Proxy

type Proxy[RCTX any] struct {
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy[RCTX any](service HTTPProxyService[RCTX], opts *Options) *Proxy[RCTX]

Create a new proxy from a service The service must implement HTTPProxyService[RCTX] opts may be nil The service can also implement the following interfaces: - WithErrorHandler[RCTX] - WithModifyResponse[RCTX] - WithRewrite[RCTX] - WithEarlyFilter[RCTX] - WithFilter[RCTX]

func (*Proxy[RCTX]) ServeHTTP

func (p *Proxy[RCTX]) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ProxyHandler

type ProxyHandler http.Handler

type ProxyRequest

type ProxyRequest struct {
	// In is the request received by the proxy.
	// The Rewrite function must not modify In.
	In *http.Request

	// Out is the request which will be sent by the proxy.
	// The Rewrite function may modify or replace this request.
	// Hop-by-hop headers are removed from this request
	// before Rewrite is called.
	Out *http.Request
}

type RewriteFunc

type RewriteFunc[RCTX any] func(*httputil.ProxyRequest, *RCTX)

type WithEarlyFilter

type WithEarlyFilter[RCTX any] interface {
	EarlyFilter(http.ResponseWriter, *http.Request, *RCTX) bool
}

Early filter to reject / accept the request before any processing Should return false to reject the request Default is to accept all requests

type WithErrorHandler

type WithErrorHandler[RCTX any] interface {
	ErrorHandler(http.ResponseWriter, *http.Request, *RCTX, error)
}

Error handler to handle errors returned by ModifyResponse or errors during request processing defaultErrorHandler is used if nil

type WithFilter

type WithFilter[RCTX any] interface {
	Filter(http.ResponseWriter, *http.Request, *RCTX) bool
}

Filter to reject / accept the request after the target is resolved Default is to accept all requests

type WithModifyResponse

type WithModifyResponse[RCTX any] interface {
	ModifyResponse(*http.Response) error
}

Modify the response before sending it to the client If returns an error, the Error handler is called Default is to do nothing

type WithRewrite

type WithRewrite[RCTX any] interface {
	Rewrite(*ProxyRequest, *RCTX)
}

Rewrite the request before sending it to the upstream useful for adding headers, etc. Default is to do nothing but rewrite the target and clean websocket headers

Jump to

Keyboard shortcuts

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