v0.4.0 Latest Latest

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 25 Imported by: 0



Package httpserver implements a HTTP/HTTPS/HTTP3 server for darvaza sidecars.



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
View Source
const (
	// DefaultSecurePort represents the default port for secure HTTP (TCP and UDP)
	DefaultSecurePort = 443
	// DefaultInsecurePort represents the default port for plain HTTP (TCP)
	DefaultInsecurePort = 80


View Source
var AcmeHTTP01Pattern = glob.MustCompile("/.well-known/acme-challenge{/(*),/**,}", '/')

AcmeHTTP01Pattern matches ACME-HTTP-01 tokens


func AcmeHTTP01Middleware added in v0.3.1

func AcmeHTTP01Middleware(next, acme http.Handler) http.Handler

AcmeHTTP01Middleware adds middleware to handle HTTP-01 challenges. if called without ACME handler or to a path without a valid token 404 will be returned automatically instead of passing the request to the next handler.


type BindingConfig

type BindingConfig struct {
	Addrs        []netip.Addr
	Port         uint16
	PortInsecure uint16

	PortStrict   bool
	PortAttempts int

	// EnableInsecure makes us listen the plain HTTP port
	EnableInsecure bool
	// AllowInsecure makes us handle plain HTTP requests
	// instead of simply redirecting to the HTTPS port.
	AllowInsecure bool

BindingConfig describes what the Server will listen.

type Config

type Config struct {
	Context context.Context
	Logger  slog.Logger

	Bind      BindingConfig
	TLSConfig *tls.Config

	// AcmeHTTP01 is an optional [http.Handler] that will
	// receive requests for /.well-known/acme-challenge
	// with a valid token.
	// If no handler is specified, the server will
	// automatically emit a 404 error for requests
	// against this well-known path.
	AcmeHTTP01 http.Handler

	ReadTimeout       time.Duration
	ReadHeaderTimeout time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration

	GracefulTimeout time.Duration

Config describes how the Server will be assembled and operate.

func (*Config) New

func (sc *Config) New(eg *core.ErrGroup) (*Server, error)

New creates a new Server from the Config, optionally taking a shared core.ErrGroup for cancellations.

func (*Config) SetDefaults

func (sc *Config) SetDefaults() error

SetDefaults fills gaps in the Config.

type Listeners

type Listeners struct {
	Secure   []net.Listener
	Insecure []*net.TCPListener
	Quic     []*quic.EarlyListener
	// contains filtered or unexported fields

Listeners contains the listeners to be used by this HTTP server.

func (*Listeners) Close

func (sl *Listeners) Close() error

Close closes all listeners.

type Server

type Server struct {
	// contains filtered or unexported fields

Server is an HTTP/1, HTTP/2, HTTP/3 server built around a shared core.ErrGroup.

func (*Server) Cancel

func (srv *Server) Cancel(cause error)

Cancel initiates a cancellation with the given reason.

func (*Server) Close

func (srv *Server) Close() error

Close tries to initiate a cancellation if the server is running, and closes all listeners. Errors are ignored.

func (*Server) HasInsecure

func (srv *Server) HasInsecure() bool

HasInsecure tells if the Server will handle plain HTTP requests.

func (*Server) HasSecure

func (srv *Server) HasSecure() bool

HasSecure tells if the Server will handle HTTPS and HTTP/3 requests.

func (*Server) ListenWithListener

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

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

func (*Server) NewH2CHandler

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

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

func (*Server) NewH2CServer

func (srv *Server) NewH2CServer(h http.Handler, addr net.Addr) *http.Server

NewH2CServer creates a new H2C capable http.Server.

func (*Server) NewH2Handler

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

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

func (*Server) NewH2Server

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

NewH2Server creates a new HTTP/2 capable http.Server.

func (*Server) NewH3Handler added in v0.3.1

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

NewH3Handler returns the http.Handler to use on the H3 server.

func (*Server) NewH3Server

func (*Server) NewH3Server(h http.Handler, addr net.Addr) *http3.Server

NewH3Server creates a new http3.Server.

func (*Server) NewHTTPSRedirectHandler

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

NewHTTPSRedirectHandler creates a new handler that redirects everything to https.

func (*Server) NewHTTPServer

func (srv *Server) NewHTTPServer(proto string, addr net.Addr) *http.Server

NewHTTPServer creates a new http.Server.

func (*Server) NewHTTPServerErrorLogger added in v0.3.1

func (srv *Server) NewHTTPServerErrorLogger(proto string, addr net.Addr) *log.Logger

NewHTTPServerErrorLogger produces a standard log.Logger use the Server's slog.Logger to be used by http.Server to log errors.

func (*Server) NewQuicConfig

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

NewQuicConfig returns the quic.Config to be used on the http3.Server.

func (*Server) NewTLSConfig

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

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

func (*Server) QuicHeadersMiddleware

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

QuicHeadersMiddleware creates 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 all workers and waits until they have finished.

func (*Server) SetQuicHeaders

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

SetQuicHeaders appends Quic's Alt-Svc to the http.Response headers.

func (*Server) Spawn

func (srv *Server) Spawn(h http.Handler, wait time.Duration) error

Spawn starts all workers and optionally waits a given amount to make sure they didn't fail.

func (*Server) Wait

func (srv *Server) Wait() error

Wait waits until all workers have finished.

Jump to

Keyboard shortcuts

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