api

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 6 Imported by: 41

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LogLevelTrace    = api.Trace
	LogLevelDebug    = api.Debug
	LogLevelInfo     = api.Info
	LogLevelWarn     = api.Warn
	LogLevelError    = api.Error
	LogLevelCritical = api.Critical
)
View Source
var (
	ErrInternalFailure      = api.ErrInternalFailure
	ErrSerializationFailure = api.ErrSerializationFailure
	ErrValueNotFound        = api.ErrValueNotFound
)
View Source
var (
	LogTrace     = api.LogTrace
	LogDebug     = api.LogDebug
	LogInfo      = api.LogInfo
	LogWarn      = api.LogWarn
	LogError     = api.LogError
	LogCritical  = api.LogCritical
	LogTracef    = api.LogTracef
	LogDebugf    = api.LogDebugf
	LogInfof     = api.LogInfof
	LogWarnf     = api.LogWarnf
	LogErrorf    = api.LogErrorf
	LogCriticalf = api.LogCriticalf

	GetLogLevel = api.GetLogLevel
)

Functions

func NewAllMethodsMap added in v0.5.0

func NewAllMethodsMap() map[string]bool

Types

type BufferInstance

type BufferInstance = api.BufferInstance

type ConfigCallbackHandler

type ConfigCallbackHandler interface {
}

ConfigCallbackHandler provides API that is used during initializing configuration

type Consumer

type Consumer interface {
	Name() string
	PluginConfig(name string) PluginConsumerConfig
}

type DataBufferBase

type DataBufferBase = api.DataBufferBase

type DecodeWholeRequestFilter

type DecodeWholeRequestFilter interface {
	// DecodeRequest processes the whole request once when WaitAllData is returned from DecodeHeaders
	// headers: the request headers
	// data: the whole request body, nil if the request doesn't have body
	// trailers: the request trailers, nil if the request doesn't have trailers
	DecodeRequest(headers RequestHeaderMap, data BufferInstance, trailers RequestTrailerMap) ResultAction
}

type DecoderFilterCallbacks added in v0.4.0

type DecoderFilterCallbacks interface {
	FilterProcessCallbacks
}

type DefaultJSONResponse added in v0.5.0

type DefaultJSONResponse struct {
	Msg string `json:"msg"`
}

DefaultJSONResponse is a default JSON response sent by LocalResponse. See the doc of LocalResponse's Msg field for more details.

type DynamicMetadata

type DynamicMetadata = api.DynamicMetadata

DynamicMetadata operates the Envoy's dynamic metadata

type EncodeWholeResponseFilter

type EncodeWholeResponseFilter interface {
	// EncodeResponse processes the whole response once when WaitAllData is returned from EncodeHeaders
	// headers: the response headers
	// data: the whole response body, nil if the response doesn't have body
	// trailers: the response trailers, current it's nil because of a bug in Envoy
	EncodeResponse(headers ResponseHeaderMap, data BufferInstance, trailers ResponseTrailerMap) ResultAction
}

type EncoderFilterCallbacks added in v0.4.0

type EncoderFilterCallbacks interface {
	FilterProcessCallbacks
}

type Filter

type Filter interface {

	// DecodeHeaders processes request headers. The endStream is true if the request doesn't have body
	DecodeHeaders(headers RequestHeaderMap, endStream bool) ResultAction
	// DecodeData might be called multiple times during handling the request body.
	// The endStream is true when handling the last piece of the body.
	DecodeData(data BufferInstance, endStream bool) ResultAction
	// DecodeTrailers processes request trailers. It doesn't fully work on Envoy < 1.31.
	DecodeTrailers(trailers RequestTrailerMap) ResultAction
	DecodeWholeRequestFilter

	// EncodeHeaders processes response headers. The endStream is true if the response doesn't have body
	EncodeHeaders(headers ResponseHeaderMap, endStream bool) ResultAction
	// EncodeData might be called multiple times during handling the response body.
	// The endStream is true when handling the last piece of the body.
	EncodeData(data BufferInstance, endStream bool) ResultAction
	// EncodeTrailers processes response trailers. It doesn't fully work on Envoy < 1.31.
	EncodeTrailers(trailers ResponseTrailerMap) ResultAction
	EncodeWholeResponseFilter

	// OnLog is called when the HTTP stream is ended on HTTP Connection Manager filter.
	// The trailers here are always nil on Envoy < 1.32.
	OnLog(reqHeaders RequestHeaderMap, reqTrailers RequestTrailerMap,
		respHeaders ResponseHeaderMap, respTrailers ResponseTrailerMap)
}

Filter represents a collection of callbacks in which Envoy will call your Go code. Every filter method (except the OnLog) is run in goroutine so it's non-blocking. To know how do we run the Filter during request processing, please refer to https://github.com/mosn/htnn/blob/main/site/content/en/docs/developer-guide/plugin_development.md TODO: change the link to the official website once we have it.

type FilterCallbackHandler

type FilterCallbackHandler interface {
	StreamFilterCallbacks
	// DecoderFilterCallbacks could only be used in DecodeXXX phases.
	DecoderFilterCallbacks() DecoderFilterCallbacks
	// EncoderFilterCallbacks could only be used in EncodeXXX phases.
	EncoderFilterCallbacks() EncoderFilterCallbacks
}

type FilterFactory

type FilterFactory func(config interface{}, callbacks FilterCallbackHandler) Filter

FilterFactory returns a per-request Filter which has configuration bound to it. This function should be a pure builder and should not have any side effect.

type FilterProcessCallbacks added in v0.4.0

type FilterProcessCallbacks interface {
	SendLocalReply(responseCode int, bodyText string, headers map[string][]string, grpcStatus int64, details string)
	// RecoverPanic recover panic in defer and terminate the request by SendLocalReply with 500 status code.
	RecoverPanic()
	// AddData add extra data when processing headers/trailers.
	// For example, turn a headers only request into a request with a body, add more body when processing trailers, and so on.
	// The second argument isStreaming supplies if this caller streams data or buffers the full body.
	AddData(data []byte, isStreaming bool)
}

FilterProcessCallbacks is the interface for filter to process request/response in decode/encode phase.

type FilterState

type FilterState = api.FilterState

FilterState operates the Envoy's filter state

type HeaderMap

type HeaderMap = api.HeaderMap

type IPAddress

type IPAddress struct {
	Address string
	IP      string
	Port    int
}

type LocalResponse

type LocalResponse struct {
	Code int
	// If the Msg is not empty, we will set the reply's body according to the Msg.
	// The rule to generate body is:
	// 1. If Content-Type is specified in the Header, the Msg will be sent directly.
	// 2. If the response header is received, and the Content-Type is "application/json", the Msg is wrapped into a JSON like `{"msg": $MSG}`.
	//    See the struct DefaultJSONResponse for more details.
	// 3. If the request doesn't have Content-Type or the Content-Type is "application/json", the Msg is wrapped into a JSON.
	// 4. Otherwise, the Msg will be sent directly.
	Msg    string
	Header http.Header

	// Details allow user to specify a custom response code details.
	// See https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_conn_man/response_code_details.
	Details string
	// contains filtered or unexported fields
}

LocalResponse represents the reply sent directly to the client instead of using the upstream response. Return `&LocalResponse{Code: 4xx, ...}` in the method if you want to send such a reply.

func (*LocalResponse) OK

func (i *LocalResponse) OK()

type LogType

type LogType = api.LogType

type PassThroughFilter

type PassThroughFilter struct{}

func (*PassThroughFilter) DecodeData

func (f *PassThroughFilter) DecodeData(data BufferInstance, endStream bool) ResultAction

func (*PassThroughFilter) DecodeHeaders

func (f *PassThroughFilter) DecodeHeaders(headers RequestHeaderMap, endStream bool) ResultAction

func (*PassThroughFilter) DecodeRequest

func (f *PassThroughFilter) DecodeRequest(headers RequestHeaderMap, data BufferInstance, trailers RequestTrailerMap) ResultAction

func (*PassThroughFilter) DecodeTrailers

func (f *PassThroughFilter) DecodeTrailers(trailers RequestTrailerMap) ResultAction

func (*PassThroughFilter) EncodeData

func (f *PassThroughFilter) EncodeData(data BufferInstance, endStream bool) ResultAction

func (*PassThroughFilter) EncodeHeaders

func (f *PassThroughFilter) EncodeHeaders(headers ResponseHeaderMap, endStream bool) ResultAction

func (*PassThroughFilter) EncodeResponse

func (f *PassThroughFilter) EncodeResponse(headers ResponseHeaderMap, data BufferInstance, trailers ResponseTrailerMap) ResultAction

func (*PassThroughFilter) EncodeTrailers

func (f *PassThroughFilter) EncodeTrailers(trailers ResponseTrailerMap) ResultAction

func (*PassThroughFilter) OnLog

func (f *PassThroughFilter) OnLog(reqHeaders RequestHeaderMap, reqTrailers RequestTrailerMap,
	respHeaders ResponseHeaderMap, respTrailers ResponseTrailerMap)

type Phase added in v0.5.0

type Phase int
const (
	PhaseDecodeHeaders  Phase = 0x01
	PhaseDecodeData     Phase = 0x02
	PhaseDecodeTrailers Phase = 0x04
	PhaseDecodeRequest  Phase = 0x08
	PhaseEncodeHeaders  Phase = 0x10
	PhaseEncodeData     Phase = 0x20
	PhaseEncodeTrailers Phase = 0x40
	PhaseEncodeResponse Phase = 0x80
	PhaseOnLog          Phase = 0x100
)

func MethodToPhase added in v0.5.0

func MethodToPhase(meth string) Phase

func (Phase) Contains added in v0.5.0

func (p Phase) Contains(phases Phase) bool

func (Phase) String added in v0.5.0

func (p Phase) String() string

type PluginConfig

type PluginConfig interface {
	ProtoReflect() protoreflect.Message
	Validate() error
}

type PluginConsumerConfig

type PluginConsumerConfig interface {
	PluginConfig
	Index() string
}

type PluginState

type PluginState interface {
	// Get the value. Returns nil if the value doesn't exist.
	Get(namespace string, key string) any
	// Set the value.
	Set(namespace string, key string, value any)
}

PluginState stores the plugin level state shared between Go plugins. Unlike DynamicMetadata, it doesn't do any serialization/deserialization. So: 1. modifying the returned state can affect the internal structure. 2. fields can't be marshalled can be kept in the state. 3. one can't access the state outside the current Envoy Go filter.

type RequestHeaderMap

type RequestHeaderMap interface {
	api.RequestHeaderMap

	// URL returns the parsed `url.URL`. Changing the field in the returned `url.URL` will not affect the original path.
	// Please use `Set(":path", ...)` to change it.
	URL() *url.URL
	// Cookie returns the HTTP Cookie. If there is no cookie with the given name, nil will be returned.
	// If multiple cookies match the given name, only one cookie will be returned.
	// Changing the field in the returned `http.Cookie` will not affect the cookies sent to the upstream.
	// Please use `Cookies` to get all cookies, change the target cookie,
	// then call `String()` to each cookie and join them as a list with `;`,
	// finally use `Set("cookie", $all-cookies-merged-as-list)` to set the previously fetched cookies back.
	Cookie(name string) *http.Cookie
	// Cookies returns all HTTP cookies. Changing the returned cookies will not affect the cookies sent to the upstream.
	// Please see the comment in `Cookie` for how to change the cookies.
	Cookies() []*http.Cookie
}

type RequestTrailerMap

type RequestTrailerMap = api.RequestTrailerMap

type ResponseHeaderMap

type ResponseHeaderMap = api.ResponseHeaderMap

type ResponseTrailerMap

type ResponseTrailerMap = api.ResponseTrailerMap

type ResultAction

type ResultAction interface {
	OK()
}

ResultAction is the result returned by each Filter method

var (
	// Continue indicates the process can continue without steering
	Continue ResultAction = &isResultAction{typeid: 0}
	// WaitAllData controls if the request/response body needs to be fully buffered during processing by Go plugin.
	// If this action is returned, DecodeData/EncodeData will be called by DecodeRequest/EncodeResponse.
	WaitAllData ResultAction = &isResultAction{typeid: 1}
)

type StreamFilterCallbacks added in v0.4.0

type StreamFilterCallbacks interface {
	// StreamInfo provides API to get/set current stream's context.
	StreamInfo() StreamInfo
	// GetProperty fetch Envoy attribute and return the value as a string.
	// The list of attributes can be found in https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
	// If the fetch succeeded, a string will be returned.
	// If the value is a timestamp, it is returned as a timestamp string like "2023-07-31T07:21:40.695646+00:00".
	// If the fetch failed (including the value is not found), an error will be returned.
	//
	// The error can be one of:
	// * ErrInternalFailure
	// * ErrSerializationFailure (Currently, fetching attributes in List/Map type are unsupported)
	// * ErrValueNotFound
	GetProperty(key string) (string, error)
	// ClearRouteCache clears the route cache for the current request, and filtermanager will re-fetch the route in the next filter.
	// Please be careful to invoke it, since filtermanager will raise an 404 route_not_found response when failed to re-fetch a route.
	ClearRouteCache()
	// RefreshRouteCache works like ClearRouteCache, but it will re-fetch the route immediately.
	RefreshRouteCache()

	// LookupConsumer is used in the Authn plugins to fetch the corresponding consumer, with
	// the plugin name and plugin specific key. We return a 'fat' Consumer so that additional
	// info like `Name` can be retrieved.
	LookupConsumer(pluginName, key string) (Consumer, bool)
	// SetConsumer is used in the Authn plugins to set the corresponding consumer after authentication.
	SetConsumer(c Consumer)
	GetConsumer() Consumer

	// PluginState returns the PluginState associated to this request.
	PluginState() PluginState

	// WithLogArg injectes `key: value` as the suffix of application log created by this
	// callback's Log* methods. The injected log arguments are only valid in the current request.
	// This method can be used to inject IDs or other context information into the logs.
	WithLogArg(key string, value any) StreamFilterCallbacks
	LogTracef(format string, v ...any)
	LogTrace(message string)
	LogDebugf(format string, v ...any)
	LogDebug(message string)
	LogInfof(format string, v ...any)
	LogInfo(message string)
	LogWarnf(format string, v ...any)
	LogWarn(message string)
	LogErrorf(format string, v ...any)
	LogError(message string)
}

StreamFilterCallbacks provides API that is used during request processing

type StreamInfo

type StreamInfo interface {
	GetRouteName() string
	FilterChainName() string
	// Protocol return the request's protocol.
	Protocol() (string, bool)
	// ResponseCode return the response code.
	ResponseCode() (uint32, bool)
	// ResponseCodeDetails return the response code details.
	ResponseCodeDetails() (string, bool)
	// AttemptCount return the number of times the request was attempted upstream.
	AttemptCount() uint32
	// Get the dynamic metadata of the request
	DynamicMetadata() DynamicMetadata
	// DownstreamLocalAddress return the downstream local address.
	DownstreamLocalAddress() string
	// DownstreamRemoteAddress return the downstream remote address.
	DownstreamRemoteAddress() string
	// UpstreamLocalAddress return the upstream local address.
	UpstreamLocalAddress() (string, bool)
	// UpstreamRemoteAddress return the upstream remote address.
	UpstreamRemoteAddress() (string, bool)
	// UpstreamClusterName return the upstream host cluster.
	UpstreamClusterName() (string, bool)
	// FilterState return the filter state interface.
	FilterState() FilterState
	// VirtualClusterName returns the name of the virtual cluster which got matched
	VirtualClusterName() (string, bool)
	// WorkerID returns the ID of the Envoy worker thread
	WorkerID() uint32

	// DownstreamRemoteParsedAddress returns the downstream remote address, in the IPAddress struct
	DownstreamRemoteParsedAddress() *IPAddress
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳