httpserver

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: MIT Imports: 26 Imported by: 1

Documentation

Overview

Package httpserver provides a flexible H1/H2C/H2/H3 server

Index

Constants

View Source
const (
	// AltSvcHeader is the header label used to advertise
	// Quic support
	AltSvcHeader = "Alt-Svc"

	// GrabQuicHeadersRetry indicates how long we wait to
	// grab the generated Alt-Svc header
	GrabQuicHeadersRetry = 10 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BindingConfig

type BindingConfig struct {
	Interfaces []string
	Addresses  []string

	Port         uint16
	PortStrict   bool
	PortAttempts int
	// PortInsecure specifies the port used to listen plain HTTP
	// if AllowInsecure is enabled
	PortInsecure uint16
	// AllowInsecure tells if plain HTTP 1.1 or H2C is allowed
	AllowInsecure bool

	// KeepAlive specifies the KeepAlive value to use on net.ListenConfig
	// when calling Listen()
	KeepAlive time.Duration
}

BindingConfig includes the information needed to listen TCP/UDP ports

type Config

type Config struct {
	// Logger is an optional slog.Logger used to debug the Server
	Logger slog.Logger
	// Context is the parent context.Context of the cancellable we use
	// with the workers
	Context context.Context

	// Bind defines the ports and addresses we listen
	Bind BindingConfig

	// ReadTimeout is the maximum duration for reading the entire
	// request, including the body. A zero or negative value means
	// there will be no timeout.
	//
	// Because ReadTimeout does not let Handlers make per-request
	// decisions on each request body's acceptable deadline or
	// upload rate, most users will prefer to use
	// ReadHeaderTimeout. It is valid to use them both.
	ReadTimeout time.Duration

	// ReadHeaderTimeout is the amount of time allowed to read
	// request headers. The connection's read deadline is reset
	// after reading the headers and the Handler can decide what
	// is considered too slow for the body. If ReadHeaderTimeout
	// is zero, the value of ReadTimeout is used. If both are
	// zero, there is no timeout.
	ReadHeaderTimeout time.Duration

	// WriteTimeout is the maximum duration before timing out
	// writes of the response. It is reset whenever a new
	// request's header is read. Like ReadTimeout, it does not
	// let Handlers make decisions on a per-request basis.
	// A zero or negative value means there will be no timeout.
	WriteTimeout time.Duration

	// IdleTimeout is the maximum amount of time to wait for the
	// next request when keep-alives are enabled. If IdleTimeout
	// is zero, the value of ReadTimeout is used. If both are
	// zero, there is no timeout.
	IdleTimeout time.Duration

	// MaxHeaderBytes controls the maximum number of bytes the
	// server will read parsing the request header's keys and
	// values, including the request line. It does not limit the
	// size of the request body.
	// If zero, http.DefaultMaxHeaderBytes is used.
	MaxHeaderBytes int

	// MaxRecvBufferSize is the buffer size we will attempt to set to
	// UDP listeners
	// If zero, bind.DefaultMaxRecvBufferSize is used.
	MaxRecvBufferSize int

	// TLSConfig optionally serves as starting point allowing the
	// use to specify different constraints
	TLSConfig *tls.Config

	// TLS Callbacks
	GetHandlerForClient func(*tls.ClientHelloInfo) sni.Handler
	GetConfigForClient  func(*tls.ClientHelloInfo) (*tls.Config, error)
	GetCertificate      func(*tls.ClientHelloInfo) (*tls.Certificate, error)
	GetRootCAs          func() *x509.CertPool
	GetClientCAs        func() *x509.CertPool

	// Optional resolver for the ACME-HTTP-01 challenge
	AcmeHTTP01 acme.HTTP01Resolver

	// Handler is the HTTPS application we serve on Bind.Port via H1/H2/H3 mapped
	// to `/` on our internal router. This internal router also takes
	// responsibility of handling the ACME-HTTP-01 challenge and other special cases
	// defined using the Handle() and HandleFunc() methods on the created Server
	Handler http.Handler
	// HandleInsecure tells us to use the same handler via H1/H2C on Bind.PortInsecure.
	// Otherwise the insecure port, if allowed, will only redirect to https and optionally
	// handle the ACME-HTTP-01 challenge using the resolver specified on the AcmeHTTP01
	// field
	HandleInsecure bool
}

Config describes how Server needs to be set up

func (*Config) New

func (cfg *Config) New() (*Server, error)

New creates a new Server from a Config

func (*Config) SetDefaults

func (cfg *Config) SetDefaults() error

SetDefaults attempts to fill any configuration gap

func (*Config) Update

func (cfg *Config) Update(hs *Server) error

Update alters the config based on what's found on the given server

type Server

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

Server is an instance of our H1/H2C/H2/H3 server

func (*Server) Cancel

func (srv *Server) Cancel()

Cancel initiates a cancellation if it wasn't cancelled already

func (*Server) Cancelled

func (srv *Server) Cancelled() bool

Cancelled tells if the server has been cancelled

func (*Server) Close

func (srv *Server) Close() error

Close tries to initiate a cancellation, and returns the reason if it was already cancelled

func (*Server) Err

func (srv *Server) Err() error

Err returns the error that caused the cancellation, if any

func (*Server) Fail

func (srv *Server) Fail(err error)

Fail initiates a cancellation with the given argument as reason

func (*Server) Handle

func (srv *Server) Handle(pattern string, handler http.Handler)

Handle registers the handler for the given pattern. If a handler already exists for pattern, Handle panics.

func (*Server) HandleFunc

func (srv *Server) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc registers the handler function for the given pattern. If a handler already exists for pattern, Handle panics.

func (*Server) Listen

func (srv *Server) Listen() error

Listen listens to the addresses specified on the Config

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(h http.Handler) error

ListenAndServe attempts to listen all the necessary port and then start the service as Serve does

func (*Server) ListenWithListener

func (srv *Server) ListenWithListener(lc bind.TCPUDPListener) error

ListenWithListener uses a given TCPUDPListener to listen to the addresses specified on the Config

func (*Server) NewH2CHandler

func (srv *Server) NewH2CHandler() http.Handler

NewH2CHandler returns the http.Handler to use on the H2C server

func (*Server) NewH2CServer

func (srv *Server) NewH2CServer(h http.Handler) *http.Server

NewH2CServer creates a new H2C capable http.Server

func (*Server) NewH2Handler

func (srv *Server) NewH2Handler() http.Handler

NewH2Handler returns the http.Handler to use on the H2 server

func (*Server) NewH2Server

func (srv *Server) NewH2Server(h http.Handler) (*http.Server, error)

NewH2Server creates a new HTTP/2 capable http.Server

func (*Server) NewHTTPSRedirectHandler

func (srv *Server) NewHTTPSRedirectHandler() http.Handler

NewHTTPSRedirectHandler creates a new handler that redirects everything to https, optionally handing ACME-HTTP-01

func (*Server) NewHTTPServer

func (srv *Server) NewHTTPServer() *http.Server

NewHTTPServer creates a new http.Server

func (*Server) NewQuicConfig

func (*Server) NewQuicConfig() *quic.Config

NewQuicConfig returns the quic.Config to be used on the HTTP/3 server

func (*Server) NewTLSConfig

func (srv *Server) NewTLSConfig() *tls.Config

NewTLSConfig returns the tls.Config to be used on the Server

func (*Server) OnCancel

func (srv *Server) OnCancel(fn func(error))

OnCancel specifies a function to be called when shutdown is initiated, caused by errors or by calling Cancel()/Close()

func (*Server) QuicHeadersMiddleware

func (srv *Server) QuicHeadersMiddleware(next http.Handler) http.Handler

QuicHeadersMiddleware creates is a middleware function that injects Alt-Svc on the http.Response headers

func (*Server) Serve

func (srv *Server) Serve(h http.Handler) error

Serve starts running the service. if a handler wasn't set on Server.Handler, you can provide one here. if you do it in both places the underlying http.ServeMux will panic

func (*Server) SetQuicHeaders

func (srv *Server) SetQuicHeaders(hdr http.Header) error

SetQuicHeaders appends Quic's Alt-Svc to the headers

func (*Server) Wait

func (srv *Server) Wait() error

Wait waits until all workers are done

func (*Server) WithListeners

func (srv *Server) WithListeners(sl *ServerListeners) error

WithListeners validates and attaches provided listeners

type ServerListeners

type ServerListeners struct {
	Insecure []*net.TCPListener
	Secure   []*net.TCPListener
	Quic     []*net.UDPConn
}

ServerListeners is the list of all listeners on a Server

func (*ServerListeners) Close

func (sl *ServerListeners) Close() error

Close closes all listeners. Errors are ignored

func (*ServerListeners) IPAddresses

func (sl *ServerListeners) IPAddresses() ([]net.IP, error)

IPAddresses validates the ServerListeners and provides the list of IP Addresses as string

func (*ServerListeners) Ports

func (sl *ServerListeners) Ports() (secure uint16, insecure uint16, ok bool)

Ports returns the port of the first secure listener and optionally the first insecure one

func (*ServerListeners) StringIPAddresses

func (sl *ServerListeners) StringIPAddresses() ([]string, error)

StringIPAddresses validates the ServerListeners and provides the list of IP Addresses as string

Jump to

Keyboard shortcuts

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