cors

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResponseListenerPriority = -100

	/* RequestListenerPriority places the preflight answer ahead of the security chain: token resolution listens at 50 and access control at 20, and a preflight carries neither cookie nor Authorization by specification, so behind them it could only be refused. */
	RequestListenerPriority = 100
)

Variables

This section is empty.

Functions

func DefaultMiddleware

func DefaultMiddleware() httpcontract.Middleware

func Middleware

func Middleware(service *Service) httpcontract.Middleware

Middleware decorates the handler path: it answers a preflight and applies the response headers for responses produced inside the middleware chain. A response produced by an event listener — a security refusal, an error page — never enters the chain, so this door never sees it, and a preflight to a path access control protects is refused before the chain is built. An application that needs those covered registers the listener doors through RegisterListeners.

func RegisterListeners added in v2.13.0

func RegisterListeners(eventDispatcher eventcontract.EventDispatcher, service *Service)

RegisterListeners wires both cors doors at once: the request listener that answers a preflight before the security chain can refuse it, and the response listener that decorates every response — the security refusals and the error pages the middleware chain never sees included. An application serving cross-origin traffic registers these; the Middleware remains for decorating the handler path inside the chain.

func RegisterRequestListener added in v2.13.0

func RegisterRequestListener(eventDispatcher eventcontract.EventDispatcher, service *Service)

RegisterRequestListener answers a well-formed preflight from an allowed origin before the security chain runs. A disallowed or absent origin and an OPTIONS request that is not a preflight fall through untouched, so the listener can only answer what the cors middleware would have answered had the request reached it, never widen what security refuses. A preflight from an allowed origin that a listener ahead of this one already answered is the one case that is neither answered nor left alone: its STATUS is untouched, and it is decorated with the cross-origin headers for the reason spelled out at that branch.

func RegisterResponseListener

func RegisterResponseListener(eventDispatcher eventcontract.EventDispatcher, service *Service)

func Restrictive

func Restrictive(allowedOrigins ...string) httpcontract.Middleware

Types

type Config

type Config struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string
	AllowCredentials bool
	MaxAge           int
	AllowOriginFunc  func(origin string) bool
}

type Service

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

func DefaultService

func DefaultService() *Service

func NewService

func NewService(config Config) *Service

func RestrictiveService

func RestrictiveService(allowedOrigins []string) *Service

func (*Service) AllowCredentials

func (instance *Service) AllowCredentials() bool

func (*Service) AllowHeaders

func (instance *Service) AllowHeaders() []string

func (*Service) AllowHeadersString

func (instance *Service) AllowHeadersString() string

func (*Service) AllowMethods

func (instance *Service) AllowMethods() []string

func (*Service) AllowMethodsString

func (instance *Service) AllowMethodsString() string

func (*Service) AllowOrigins

func (instance *Service) AllowOrigins() []string

func (*Service) ApplyPreflightHeaders

func (instance *Service) ApplyPreflightHeaders(origin string, headers nethttp.Header)

func (*Service) ApplyResponseHeaders

func (instance *Service) ApplyResponseHeaders(origin string, headers nethttp.Header)

func (*Service) ExposeHeaders

func (instance *Service) ExposeHeaders() []string

func (*Service) ExposeHeadersString

func (instance *Service) ExposeHeadersString() string

func (*Service) IsPreflight

func (instance *Service) IsPreflight(request httpcontract.Request) bool

func (*Service) MaxAge

func (instance *Service) MaxAge() int

func (*Service) OriginAllowed

func (instance *Service) OriginAllowed(origin string) bool

func (*Service) RequestOrigin

func (instance *Service) RequestOrigin(request httpcontract.Request) string

Jump to

Keyboard shortcuts

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