vhttp

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusSessionError   = 522
	StatusUpstreamError  = 1020
	StatusWSFBlocked     = 1021
	StatusMaintenance    = 1022
	StatusRestartLoop    = 1023
	StatusPanic          = 1024
	StatusSignatureError = 1025
	StatusPublishError   = 1026
)

Variables

View Source
var LocalClientSession = &ClientSession{
	IP:         netx.ParseIP("127.0.0.1"),
	IPHash:     0x7f000001,
	RemoteAddr: "127.0.0.1:0",
	IPInfo:     netx.LocalIPInfoHeaders,
	Local:      true,
}
View Source
var Raygen = ray.NewGenerator(config.Get().Host)

Functions

func CleanPath

func CleanPath(s string) string

func EnforceRateReq

func EnforceRateReq(r *http.Request, l rate.Limit) http.Handler

func Error

func Error(w http.ResponseWriter, r *http.Request, code int, explanation ...string)

func ForEachSession

func ForEachSession(f func(*ClientSession) bool)

func GetClientMetrics

func GetClientMetrics() (metrics map[string]ClientMetrics)

func NormalPath

func NormalPath(s string) string

func NumClients

func NumClients() int

func ParseErrorTemplates

func ParseErrorTemplates(fs fs.FS, prefix string) (*template.Template, error)

func RegisterSwitchVariable

func RegisterSwitchVariable(v SwitchVariable)

func RelativePath

func RelativePath(root, rel string) string

func ResolveNatsFromContext

func ResolveNatsFromContext(ctx context.Context) *enats.Client

func StartInternalRequest

func StartInternalRequest(r *http.Request) (rctx *http.Request)

func WithStateResolver

func WithStateResolver(ctx context.Context, r StateResolver) context.Context

Types

type BufferedResponse

type BufferedResponse struct {
	Status  int
	Headers http.Header
	Body    *bytes.Buffer
}

func NewBufferedResponse

func NewBufferedResponse(buf *bytes.Buffer) *BufferedResponse

func (*BufferedResponse) Header

func (bw *BufferedResponse) Header() http.Header

func (*BufferedResponse) Response

func (bw *BufferedResponse) Response() *http.Response

func (*BufferedResponse) ServeHTTP

func (bw *BufferedResponse) ServeHTTP(w http.ResponseWriter, r *http.Request)

Serves the buffered response to another writer.

func (*BufferedResponse) Write

func (bw *BufferedResponse) Write(b []byte) (int, error)

func (*BufferedResponse) WriteHeader

func (bw *BufferedResponse) WriteHeader(status int)

type CertProvider added in v0.3.5

type CertProvider struct {
	CertPath string `yaml:"cert,omitempty"` // Path to the TLS certificate.
	KeyPath  string `yaml:"key,omitempty"`  // Path to the TLS key.
	// contains filtered or unexported fields
}

func (*CertProvider) GetCertificate added in v0.3.5

func (cp *CertProvider) GetCertificate(chi *tls.ClientHelloInfo) (cert *tls.Certificate, err error)

type ClientMetrics

type ClientMetrics struct {
	AvgRps       float64   `json:"avg_rps"`
	NumReqs      int32     `json:"num_reqs"`
	FirstSeen    time.Time `json:"first_seen"`
	BlockedUntil time.Time `json:"blocked_until,omitempty"`
}

type ClientSession

type ClientSession struct {
	IP         netx.IP
	IPHash     uint32
	RemoteAddr string   // ip:0, for setting request.RemoteAddr
	Values     sync.Map // For handler's use.

	NumRequests    atomic.Int32
	BlockedUntilMs atomic.Int64
	IPInfo         http.Header
	Local          bool
	// contains filtered or unexported fields
}

func ClientSessionFromContext

func ClientSessionFromContext(ctx context.Context) (session *ClientSession)

func GetClientSession

func GetClientSession(ip netx.IP) (session *ClientSession)

func StartClientRequest

func StartClientRequest(r *http.Request, infoProvider netx.IPInfoProvider) (rctx *http.Request, session *ClientSession)

func (*ClientSession) BlockFor

func (s *ClientSession) BlockFor(d time.Duration) time.Time

func (*ClientSession) BlockUntil

func (s *ClientSession) BlockUntil(t time.Time) time.Time

func (*ClientSession) EnforceRateReq

func (s *ClientSession) EnforceRateReq(c context.Context, r *http.Request, l rate.Limit) http.Handler

func (*ClientSession) FirstRequest

func (s *ClientSession) FirstRequest() time.Time

func (*ClientSession) IsBlocked

func (s *ClientSession) IsBlocked() bool

func (*ClientSession) LastRequest

func (s *ClientSession) LastRequest() time.Time

func (*ClientSession) Metrics

func (s *ClientSession) Metrics() ClientMetrics

func (*ClientSession) SetOnContext

func (s *ClientSession) SetOnContext(ctx context.Context) context.Context

func (*ClientSession) Unblock

func (s *ClientSession) Unblock()

type ConditionalResponse

type ConditionalResponse struct {
	Touched bool
	// contains filtered or unexported fields
}

func NewConditionalResponse

func NewConditionalResponse(rw http.ResponseWriter) *ConditionalResponse

func (*ConditionalResponse) Header

func (cr *ConditionalResponse) Header() http.Header

func (*ConditionalResponse) Hijack

func (cr *ConditionalResponse) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*ConditionalResponse) Unwrap

func (*ConditionalResponse) Write

func (cr *ConditionalResponse) Write(b []byte) (int, error)

func (*ConditionalResponse) WriteHeader

func (cr *ConditionalResponse) WriteHeader(status int)

type ErrorPageParams

type ErrorPageParams struct {
	Template    string
	StatusSent  int           // Status code to send if non-zero
	Code        int           // Error code, or http status code
	Title       string        // Title of the error page
	Explanation string        // Explanation of the error
	Solution    string        // Solution to the error
	Server      string        // Server name
	Host        string        // Host name
	Date        string        // Date
	Ray         string        // Ray ID
	IP          string        // Client IP
	Type        ErrorPageType // Type of error page
}

func (*ErrorPageParams) WithRequest

func (params *ErrorPageParams) WithRequest(r *http.Request) *ErrorPageParams

func (*ErrorPageParams) WriteTo

func (params *ErrorPageParams) WriteTo(w http.ResponseWriter, sv *Server) (err error)

type ErrorPageType

type ErrorPageType uint8
const (
	ErrorPageAny ErrorPageType = iota
	ErrorPageHTML
	ErrorPageHead
	ErrorPagePlain
	ErrorPageJSON
)

type HandleMux

type HandleMux struct {
	Mux
}

func (*HandleMux) String

func (h *HandleMux) String() string

func (*HandleMux) UnmarshalYAML

func (h *HandleMux) UnmarshalYAML(node *yaml.Node) (e error)

type HandlePublish

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

func (HandlePublish) ServeHTTP

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

func (HandlePublish) String

func (h HandlePublish) String() string

func (*HandlePublish) UnmarshalInline

func (h *HandlePublish) UnmarshalInline(text string) error

func (*HandlePublish) UnmarshalText

func (h *HandlePublish) UnmarshalText(text []byte) error

type HandleService

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

func (HandleService) ServeHTTP

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

func (HandleService) String

func (h HandleService) String() string

func (*HandleService) UnmarshalInline

func (h *HandleService) UnmarshalInline(text string) error

func (*HandleService) UnmarshalText

func (h *HandleService) UnmarshalText(text []byte) error

type HandleSwitch

type HandleSwitch struct {
	Mux
	Variable SwitchVariable
}

func (*HandleSwitch) ServeHTTP

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

func (*HandleSwitch) String

func (h *HandleSwitch) String() string

func (*HandleSwitch) UnmarshalYAML

func (h *HandleSwitch) UnmarshalYAML(node *yaml.Node) (e error)

type Handler

type Handler interface {
	ServeHTTP(w http.ResponseWriter, r *http.Request) Result
	String() string
}

func ResolveServiceFromContext

func ResolveServiceFromContext(ctx context.Context, s string) Handler

type HandlerFunc added in v0.3.2

type HandlerFunc func(w http.ResponseWriter, r *http.Request) Result

func (HandlerFunc) ServeHTTP added in v0.3.2

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) Result

func (HandlerFunc) String added in v0.3.2

func (f HandlerFunc) String() string

type InternalHandler

type InternalHandler struct {
	Inner Subhandler
}

func (InternalHandler) ServeHTTP

func (InternalHandler) String

func (i InternalHandler) String() string

func (*InternalHandler) UnmarshalInline

func (i *InternalHandler) UnmarshalInline(text string) error

func (*InternalHandler) UnmarshalText

func (i *InternalHandler) UnmarshalText(text []byte) error

func (*InternalHandler) UnmarshalYAML

func (i *InternalHandler) UnmarshalYAML(node *yaml.Node) (err error)

type Mux

type Mux struct {
	Routes []*route
}

func (*Mux) ServeHTTP

func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) Result

func (*Mux) Then

func (mux *Mux) Then(mw Handler)

func (*Mux) Use

func (mux *Mux) Use(spattern string, mw Handler) error

func (*Mux) UsePattern

func (mux *Mux) UsePattern(p Pattern, mw Handler)

type Pattern

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

func NewExactPattern

func NewExactPattern(v string) (p Pattern)

func NewPattern

func NewPattern(v string) (p Pattern, e error)

func (Pattern) IsWildcard

func (p Pattern) IsWildcard() bool

func (Pattern) MarshalJSON

func (p Pattern) MarshalJSON() ([]byte, error)

func (Pattern) MarshalText

func (p Pattern) MarshalText() ([]byte, error)

func (Pattern) MarshalYAML

func (p Pattern) MarshalYAML() (any, error)

func (*Pattern) Match

func (p *Pattern) Match(host, path string) bool

func (Pattern) String

func (p Pattern) String() string

func (*Pattern) UnmarshalJSON

func (p *Pattern) UnmarshalJSON(data []byte) (err error)

func (*Pattern) UnmarshalText

func (p *Pattern) UnmarshalText(data []byte) (e error)

func (*Pattern) UnmarshalYAML

func (p *Pattern) UnmarshalYAML(node *yaml.Node) (err error)

type RateLimitHandler

type RateLimitHandler struct {
	rate.Limit
}

func (RateLimitHandler) ServeHTTP

func (rh RateLimitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) Result

func (RateLimitHandler) String

func (rh RateLimitHandler) String() string

func (*RateLimitHandler) UnmarshalInline

func (rh *RateLimitHandler) UnmarshalInline(text string) error

type Result

type Result uint8
const (
	Done     Result = iota // Finished handling the request, return all the way up.
	Continue               // Continue to the next handler.
	Drop                   // Drop from this mux, but continue to the next.
)

type RewriteHandler

type RewriteHandler struct {
	Match   *regexp.Regexp
	Replace string
}

func (RewriteHandler) ServeHTTP

func (rh RewriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) Result

func (RewriteHandler) String

func (rh RewriteHandler) String() string

func (*RewriteHandler) UnmarshalInline

func (rh *RewriteHandler) UnmarshalInline(text string) error

func (*RewriteHandler) UnmarshalText

func (rh *RewriteHandler) UnmarshalText(text []byte) error

func (*RewriteHandler) UnmarshalYAML

func (rh *RewriteHandler) UnmarshalYAML(node *yaml.Node) (err error)

type Server

type Server struct {
	context.Context

	Server http.Server

	TopLevelMux

	Signer *urlsigner.Signer
	// contains filtered or unexported fields
}

func GetServerFromContext

func GetServerFromContext(ctx context.Context) *Server

func NewServer

func NewServer(ctx context.Context) (s *Server)

NewServer returns a new Server.

func (*Server) Close

func (s *Server) Close() error

func (*Server) GetIPInfoProvider

func (s *Server) GetIPInfoProvider() netx.IPInfoProvider

func (*Server) Listen

func (s *Server) Listen() (err error)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) ServeHTTPSession

func (s *Server) ServeHTTPSession(w http.ResponseWriter, r *http.Request, session *ClientSession)

ServeHTTP implements http.Handler.

func (*Server) SetErrorTemplates

func (s *Server) SetErrorTemplates(t *template.Template)

func (*Server) SetHosts

func (s *Server) SetHosts(vhosts ...*VirtualHost)

func (*Server) SetIPInfoProvider

func (s *Server) SetIPInfoProvider(provider netx.IPInfoProvider)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

func (*Server) Value

func (s *Server) Value(key any) any

func (*Server) Wait

func (s *Server) Wait()

type StateResolver

type StateResolver interface {
	ResolveService(s string) Handler
	ResolveNats() *enats.Client
}

func StateResolverFromContext

func StateResolverFromContext(ctx context.Context) StateResolver

type Subhandler

type Subhandler struct {
	Handler
}

func (*Subhandler) UnmarshalYAML

func (t *Subhandler) UnmarshalYAML(node *yaml.Node) (e error)

type SwitchVariable

type SwitchVariable interface {
	Name() string
	Value(r *http.Request) string
}

func RegisterSwitchVariableFunc

func RegisterSwitchVariableFunc(name string, f func(r *http.Request) string) SwitchVariable

type TopLevelMux

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

func (*TopLevelMux) GetCertificate

func (mux *TopLevelMux) GetCertificate(chi *tls.ClientHelloInfo) (cert *tls.Certificate, err error)

GetCertificate implements tls.Config.GetCertificate.

func (*TopLevelMux) Hostnames

func (mux *TopLevelMux) Hostnames() (hostnames []string)

Hostnames returns a list of all hostnames in the mux.

func (*TopLevelMux) SetHosts

func (mux *TopLevelMux) SetHosts(vhosts ...*VirtualHost)

Replaces virtual hosts in bulk.

type VirtualHost

type VirtualHost struct {
	VirtualHostOptions
	Mux
}

func NewVirtualHost

func NewVirtualHost(opt VirtualHostOptions) (vh *VirtualHost)

type VirtualHostOptions

type VirtualHostOptions struct {
	Hostnames []string                 `yaml:"-"`
	NoUpgrade bool                     `yaml:"no_upgrade,omitempty"` // Do not upgrade HTTP to HTTPS.
	Certs     map[string]*CertProvider `yaml:"certs,omitempty"`      // TLS certificates.
}

Jump to

Keyboard shortcuts

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