Documentation ¶
Overview ¶
Package mux is a library to multiplex network connections based on their payload. Using mux, you can serve different protocols from the same listener.
Index ¶
- Variables
- func ConnStateSliceContains(enums []ConnState, sunEnums ...ConnState) bool
- func ConnStateSliceContainsAny(enums []ConnState, sunEnums ...ConnState) bool
- func Handle(pattern Matcher, handler HandlerConn)
- func HandleFunc(pattern Matcher, handler func(net.Conn))
- func HandleListener(pattern Matcher) net.Listener
- func ListenAndServe(addr string, handler HandlerConn) error
- func ListenAndServeTLS(addr string, handler HandlerConn, tlsConfig *tls.Config, ...) error
- func NotFound(c net.Conn)
- func SetReadTimeout(t time.Duration)
- type ConnState
- func (i ConnState) MarshalBinary() (data []byte, err error)
- func (i ConnState) MarshalJSON() ([]byte, error)
- func (i ConnState) MarshalText() ([]byte, error)
- func (i ConnState) MarshalYAML() (any, error)
- func (i ConnState) Registered() bool
- func (i *ConnState) Scan(value any) error
- func (i ConnState) String() string
- func (i *ConnState) UnmarshalBinary(data []byte) error
- func (i *ConnState) UnmarshalJSON(data []byte) error
- func (i *ConnState) UnmarshalText(text []byte) error
- func (i *ConnState) UnmarshalYAML(unmarshal func(any) error) error
- func (i ConnState) Value() (driver.Value, error)
- type EmptyServerOption
- type ErrorHandler
- type ErrorHandlerFunc
- type HandlerConn
- type HandlerConnFunc
- type Matcher
- type MatcherFunc
- func Any() MatcherFunc
- func AnyPrefixByteMatcher(list ...[]byte) MatcherFunc
- func AnyPrefixMatcher(strs ...string) MatcherFunc
- func GRPC() MatcherFunc
- func HTTP() MatcherFunc
- func HTTP1() MatcherFunc
- func HTTP1Fast(extMethods ...string) MatcherFunc
- func HTTP1Header(match func(actual, expect http.Header) bool, expect http.Header) MatcherFunc
- func HTTP1HeaderEqual(header http.Header) MatcherFunc
- func HTTP1HeaderPrefix(header http.Header) MatcherFunc
- func HTTP1HeaderValue(match func(actualVal, expectVal string) bool, expect http.Header) MatcherFunc
- func HTTP2() MatcherFunc
- func HTTP2HeaderField(sendSetting bool, match func(actual, expect map[string]hpack.HeaderField) bool, ...) MatcherFunc
- func HTTP2HeaderFieldEqual(sendSetting bool, headers ...hpack.HeaderField) MatcherFunc
- func HTTP2HeaderFieldPrefix(sendSetting bool, headers ...hpack.HeaderField) MatcherFunc
- func HTTP2HeaderFieldValue(sendSetting bool, match func(actualVal, expectVal string) bool, ...) MatcherFunc
- func TLS(versions ...int) MatcherFunc
- type ServeMux
- func (mux *ServeMux) Close() error
- func (mux *ServeMux) Handle(pattern Matcher, handler HandlerConn)
- func (mux *ServeMux) HandleFunc(pattern Matcher, handler func(net.Conn))
- func (mux *ServeMux) HandleListener(pattern Matcher) net.Listener
- func (mux *ServeMux) Handler(c *sniffConn) (h HandlerConn)
- func (mux *ServeMux) Serve(c net.Conn)
- func (mux *ServeMux) SetReadTimeout(t time.Duration)
- type Server
- func (srv *Server) ApplyOptions(options ...ServerOption) *Server
- func (srv *Server) Close() error
- func (srv *Server) Context() context.Context
- func (srv *Server) HandleError(h ErrorHandler)
- func (srv *Server) ListenAndServe(addr string) error
- func (srv *Server) ListenAndServeTLS(addr string, tlsConfig *tls.Config, certFile, keyFile string) error
- func (srv *Server) RegisterOnShutdown(f func())
- func (srv *Server) Serve(l net.Listener) error
- func (srv *Server) ServeTLS(l net.Listener, tLSConfig *tls.Config, certFile, keyFile string) error
- func (srv *Server) Shutdown(ctx context.Context) error
- type ServerOption
- type ServerOptionFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ServerContextKey is a context key. It can be used in cmux // handlers with context.WithValue to access the server that // started the handler. The associated value will be of // type CMuxer. ServerContextKey = &contextKey{"cmux-server"} // LocalAddrContextKey is a context key. It can be used in // cmux handlers with context.WithValue to access the local // address the connection arrived on. // The associated value will be of type net.Addr. LocalAddrContextKey = &contextKey{"local-addr"} )
var DefaultServeMux = &defaultServeMux
DefaultServeMux is the default ServeMux used by Serve.
var ErrAbortHandler = errors.New("net/mux: abort HandlerConn")
ErrAbortHandler is a sentinel panic value to abort a handler. While any panic from ServeHTTP aborts the response to the client, panicking with ErrAbortHandler also suppresses logging of a stack trace to the server's error log.
var ( // ErrHijacked is returned by ResponseWriter.Write calls when // the underlying connection has been hijacked using the // Hijacker interface. A zero-byte write on a hijacked // connection will return ErrHijacked without any other side // effects. ErrHijacked = errors.New("cmux: connection has been hijacked") )
var ErrListenerClosed = net_.ErrListenerClosed
ErrListenerClosed is returned from MuxListener.Accept when the underlying listener is closed.
var ErrServerClosed = errors.New("net/mux: Server closed")
ErrServerClosed is returned by the Server's Serve, ServeTLS, ListenAndServe, and ListenAndServeTLS methods after a call to Shutdown or Close.
Functions ¶
func ConnStateSliceContains ¶
ConnStateSliceContains reports whether sunEnums is within enums.
func ConnStateSliceContainsAny ¶
ConnStateSliceContainsAny reports whether any sunEnum is within enums.
func Handle ¶
func Handle(pattern Matcher, handler HandlerConn)
func HandleFunc ¶
HandleFunc registers the handler function for the given pattern.
func HandleListener ¶
func ListenAndServe ¶
func ListenAndServe(addr string, handler HandlerConn) error
ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
The handler is typically nil, in which case the DefaultServeMux is used.
ListenAndServe always returns a non-nil error.
func ListenAndServeTLS ¶
func ListenAndServeTLS(addr string, handler HandlerConn, tlsConfig *tls.Config, certFile, keyFile string) error
ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.
func SetReadTimeout ¶
Types ¶
type ConnState ¶
type ConnState int
A ConnState represents the state of a client connection to a server. It's used by the optional Server.ConnStateHook hook.
const ( // ConnStateNew represents a new connection that is expected to // send a request immediately. Connections begin at this // state and then transition to either ConnStateActive or // ConnStateClosed. ConnStateNew ConnState = iota // ConnStateActive represents a connection that has read 1 or more // bytes of a request. The Server.ConnStateHook hook for // ConnStateActive fires before the request has entered a handler // and doesn't fire again until the request has been // handled. After the request is handled, the state // transitions to ConnStateClosed, ConnStateHijacked, or ConnStateIdle. // For HTTP/2, ConnStateActive fires on the transition from zero // to one active request, and only transitions away once all // active requests are complete. That means that ConnStateHook // cannot be used to do per-request work; ConnStateHook only notes // the overall state of the connection. ConnStateActive // ConnStateIdle represents a connection that has finished // handling a request and is in the keep-alive state, waiting // for a new request. Connections transition from ConnStateIdle // to either ConnStateActive or ConnStateClosed. ConnStateIdle // ConnStateHijacked represents a hijacked connection. // This is a terminal state. It does not transition to ConnStateClosed. ConnStateHijacked // ConnStateClosed represents a closed connection. // This is a terminal state. Hijacked connections do not // transition to ConnStateClosed. ConnStateClosed )
func ConnStateValues ¶
func ConnStateValues() []ConnState
ConnStateValues returns all values of the enum
func ParseConnStateString ¶
ParseConnStateString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func (ConnState) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface for ConnState
func (ConnState) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for ConnState
func (ConnState) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for ConnState
func (ConnState) MarshalYAML ¶
MarshalYAML implements a YAML Marshaler for ConnState
func (ConnState) Registered ¶
IsAConnState returns "true" if the value is listed in the enum definition. "false" otherwise
func (*ConnState) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for ConnState
func (*ConnState) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for ConnState
func (*ConnState) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for ConnState
func (*ConnState) UnmarshalYAML ¶
UnmarshalYAML implements a YAML Unmarshaler for ConnState
type EmptyServerOption ¶
type EmptyServerOption struct{}
EmptyServerOption does not alter the configuration. It can be embedded in another structure to build custom options.
This API is EXPERIMENTAL.
type ErrorHandler ¶
type ErrorHandlerFunc ¶
ErrorHandler handles an error and returns whether the mux should continue serving the listener.
func (ErrorHandlerFunc) Continue ¶
func (f ErrorHandlerFunc) Continue(err error) bool
type HandlerConn ¶
func NotFoundHandler ¶
func NotFoundHandler() HandlerConn
NotFoundHandler returns a simple request handler that replies to each request with a “404 page not found” reply.
type HandlerConnFunc ¶
HandlerConnFunc is a match that can also write response (say to do handshake).
func (HandlerConnFunc) Serve ¶
func (f HandlerConnFunc) Serve(c net.Conn)
type Matcher ¶
func MatcherAny ¶
type MatcherFunc ¶
MatcherFunc is a match that can also write response (say to do handshake).
func AnyPrefixByteMatcher ¶
func AnyPrefixByteMatcher(list ...[]byte) MatcherFunc
func AnyPrefixMatcher ¶
func AnyPrefixMatcher(strs ...string) MatcherFunc
AnyPrefixMatcher returns a matcher that matches a connection if it starts with any of the strings in strs.
func GRPC ¶
func GRPC() MatcherFunc
GRPC parses the frame header of the first frame to detect whether the connection is an HTTP2 connection.
func HTTP ¶
func HTTP() MatcherFunc
PRI * HTTP/2.0\r\n\r\n HTTP parses the first line or upto 4096 bytes of the request to see if the connection contains an HTTP request.
func HTTP1 ¶
func HTTP1() MatcherFunc
HTTP1 parses the first line or upto 4096 bytes of the request to see if the conection contains an HTTP request.
func HTTP1Fast ¶
func HTTP1Fast(extMethods ...string) MatcherFunc
HTTP1Fast only matches the methods in the HTTP request.
This matcher is very optimistic: if it returns true, it does not mean that the request is a valid HTTP response. If you want a correct but slower HTTP1 matcher, use HTTP1 instead.
func HTTP1Header ¶
HTTP1Header returns true if all headers are expected
func HTTP1HeaderEqual ¶
func HTTP1HeaderEqual(header http.Header) MatcherFunc
HTTP1HeaderEqual returns a matcher matching the header fields of the first request of an HTTP 1 connection. strings.Equal for all value in expects
func HTTP1HeaderPrefix ¶
func HTTP1HeaderPrefix(header http.Header) MatcherFunc
HTTP1HeaderPrefix returns a matcher matching the header fields of the first request of an HTTP 1 connection. If the header with key name has a value prefixed with valuePrefix, this will match. strings.HasPrefix for all value in expects
func HTTP1HeaderValue ¶
func HTTP1HeaderValue(match func(actualVal, expectVal string) bool, expect http.Header) MatcherFunc
HTTP1HeaderValue returns true if all headers are expected, shorthand for HTTP1Header strings.Match for all value in expects
func HTTP2 ¶
func HTTP2() MatcherFunc
HTTP2 parses the frame header of the first frame to detect whether the connection is an HTTP2 connection.
func HTTP2HeaderField ¶
func HTTP2HeaderField(sendSetting bool, match func(actual, expect map[string]hpack.HeaderField) bool, expects ...hpack.HeaderField) MatcherFunc
HTTP2HeaderField returns a matcher matching the header fields of the first headers frame. writes the settings to the server if sendSetting is true. Prefer HTTP2HeaderField over this one, if the client does not block on receiving a SETTING frame.
func HTTP2HeaderFieldEqual ¶
func HTTP2HeaderFieldEqual(sendSetting bool, headers ...hpack.HeaderField) MatcherFunc
HTTP2HeaderFieldEqual returns a matcher matching the header fields.
func HTTP2HeaderFieldPrefix ¶
func HTTP2HeaderFieldPrefix(sendSetting bool, headers ...hpack.HeaderField) MatcherFunc
HTTP2HeaderFieldPrefix returns a matcher matching the header fields. If the header with key name has a value prefixed with valuePrefix, this will match.
func HTTP2HeaderFieldValue ¶
func HTTP2HeaderFieldValue(sendSetting bool, match func(actualVal, expectVal string) bool, expects ...hpack.HeaderField) MatcherFunc
HTTP2HeaderFieldValue returns a matcher matching the header fields, registered with the match handler.
func TLS ¶
func TLS(versions ...int) MatcherFunc
TLS matches HTTPS requests.
By default, any TLS handshake packet is matched. An optional whitelist of versions can be passed in to restrict the matcher, for example:
TLS(tls.VersionTLS11, tls.VersionTLS12)
reverse of crypto/tls/conn.go func (c *Conn) readRecordOrCCS(expectChangeCipherSpec bool) error { HandlerShake of TLS type byte // recordTypeHandshake versions [2]byte
type ServeMux ¶
type ServeMux struct { // NotFound replies to the listener with a not found error. NotFoundHandler HandlerConn // contains filtered or unexported fields }
ServeMux is a net.ServeMux that accepts only the connections that matched. goroutine unsafe
func (*ServeMux) Handle ¶
func (mux *ServeMux) Handle(pattern Matcher, handler HandlerConn)
func (*ServeMux) HandleFunc ¶
HandleFunc registers the handler function for the given pattern.
func (*ServeMux) Handler ¶
func (mux *ServeMux) Handler(c *sniffConn) (h HandlerConn)
Handler is the main implementation of HandlerConn.
func (*ServeMux) SetReadTimeout ¶
SetReadTimeout sets a timeout for the read of matchers
type Server ¶
type Server struct { Handler HandlerConn // handler to invoke, mux.DefaultServeMux if nil // ConnStateHook specifies an optional callback function that is // called when a client connection changes state. See the // ConnStateHook type and associated constants for details. ConnStateHook func(net.Conn, ConnState) // contains filtered or unexported fields }
Server is a multiplexer for network connections.
func NewServer ¶
func NewServer() *Server
NewServer wraps NewServerWithContext using the background context.
func NewServerWithContext ¶
NewServerWithContext returns a new Server.
func (*Server) ApplyOptions ¶
func (srv *Server) ApplyOptions(options ...ServerOption) *Server
func (*Server) Close ¶
Close immediately closes all active net.Listeners and any connections in state StateNew, StateActive, or StateIdle. For a graceful shutdown, use Shutdown.
Close does not attempt to close (and does not even know about) any hijacked connections, such as WebSockets.
Close returns any error returned from closing the ServeMux's underlying ServeMux(s).
func (*Server) Context ¶
Context returns the request's context. To change the context, use WithContext.
The returned context is always non-nil; it defaults to the background context.
func (*Server) HandleError ¶
func (srv *Server) HandleError(h ErrorHandler)
HandleError registers an error handler that handles listener errors.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
If srv.Addr is blank, ":http" is used.
ListenAndServe always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.
func (*Server) ListenAndServeTLS ¶
func (srv *Server) ListenAndServeTLS(addr string, tlsConfig *tls.Config, certFile, keyFile string) error
ListenAndServeTLS listens on the TCP network address srv.Addr and then calls ServeTLS to handle requests on incoming TLS connections. Accepted connections are configured to enable TCP keep-alives.
Filenames containing a certificate and matching private key for the server must be provided if neither the ServeMux's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.
If srv.Addr is blank, ":https" is used.
ListenAndServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.
func (*Server) RegisterOnShutdown ¶
func (srv *Server) RegisterOnShutdown(f func())
RegisterOnShutdown registers a function to call on Shutdown. This can be used to gracefully shutdown connections that have undergone NPN/ALPN protocol upgrade or that have been hijacked. This function should start protocol-specific graceful shutdown, but should not wait for shutdown to complete.
func (*Server) Serve ¶
Serve starts multiplexing the listener. Serve blocks and perhaps should be invoked concurrently within a go routine. Serve accepts incoming connections on the ServeMux l, creating a new service goroutine for each. The service goroutines read requests and then call srv.HandlerConn to reply to them.
func (*Server) ServeTLS ¶
ServeTLS accepts incoming connections on the ServeMux l, creating a new service goroutine for each. The service goroutines perform TLS setup and then read requests, calling srv.HandlerConn to reply to them.
Files containing a certificate and matching private key for the server must be provided if neither the ServeMux's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.
ServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the ServeMux's underlying ServeMux(s).
When Shutdown is called, Serve, ListenAndServe, and ListenAndServeTLS immediately return ErrServerClosed. Make sure the program doesn't exit and waits instead for Shutdown to return.
Shutdown does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Shutdown should separately notify such long-lived connections of shutdown and wait for them to close, if desired. See RegisterOnShutdown for a way to register shutdown notification functions.
Once Shutdown has been called on a server, it may not be reused; future calls to methods such as Serve will return ErrServerClosed.
type ServerOption ¶
type ServerOption interface {
// contains filtered or unexported methods
}
A ServerOption sets options.
func WithErrorLog ¶
func WithErrorLog(errorLog *log.Logger) ServerOption
WithErrorLog specifies an optional logger for errors
func WithMaxIdleConns ¶
func WithMaxIdleConns(maxIdleConns int) ServerOption
WithMaxIdleConns controls the maximum number of idle (keep-alive) connections across all hosts. Zero means no limit.
type ServerOptionFunc ¶
type ServerOptionFunc func(*Server)
ServerOptionFunc wraps a function that modifies Server into an implementation of the ServerOption interface.