httputil

package
v1.12.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: MIT Imports: 10 Imported by: 26

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimeouts = HTTPTimeouts{
	ReadTimeout:       30 * time.Second,
	ReadHeaderTimeout: 30 * time.Second,
	WriteTimeout:      30 * time.Second,
	IdleTimeout:       120 * time.Second,
}

DefaultTimeouts for HTTP server, based on the RPC timeouts that geth uses.

Functions

This section is empty.

Types

type HTTPOption added in v1.4.2

type HTTPOption func(config *http.Server) error

HTTPOption applies a change to an HTTP server, just before standup. HTTPOption options are be re-executed on server shutdown/startup cycles, for each new underlying Go *http.Server instance.

func WithMaxHeaderBytes added in v1.4.2

func WithMaxHeaderBytes(max int) HTTPOption

func WithTimeouts added in v1.4.2

func WithTimeouts(timeouts HTTPTimeouts) HTTPOption

type HTTPServer added in v1.4.2

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

HTTPServer wraps a http.Server, while providing conveniences like exposing the running state and address.

It can be started with HTTPServer.Start and closed with HTTPServer.Stop, HTTPServer.Close and HTTPServer.Shutdown (convenience functions for different gracefulness).

The addr contains both host and port. A 0 port may be used to make the system bind to an available one. The resulting address can be retrieved with HTTPServer.Addr or HTTPServer.HTTPEndpoint.

The server may be started, stopped and started back up.

func NewHTTPServer added in v1.12.0

func NewHTTPServer(addr string, handler http.Handler, opts ...Option) *HTTPServer

NewHTTPServer creates an HTTPServer that serves the given HTTP handler. The server is inactive and has to be started explicitly.

func StartHTTPServer added in v1.4.2

func StartHTTPServer(addr string, handler http.Handler, opts ...Option) (*HTTPServer, error)

func (*HTTPServer) Addr added in v1.4.2

func (s *HTTPServer) Addr() net.Addr

Addr returns the address that the server is listening on. It returns nil if the server is not online.

func (*HTTPServer) Close added in v1.4.2

func (s *HTTPServer) Close() error

Close force-closes the HTTPServer, its listener, and all its active connections.

func (*HTTPServer) Closed added in v1.4.2

func (s *HTTPServer) Closed() bool

func (*HTTPServer) HTTPEndpoint added in v1.12.0

func (s *HTTPServer) HTTPEndpoint() string

HTTPEndpoint returns the http(s) endpoint the server is serving. It returns an empty string if the server is not online.

func (*HTTPServer) Shutdown added in v1.4.2

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

Shutdown shuts down the HTTP server and its listener, but allows active connections to close gracefully. If the function exits due to a ctx cancellation the listener is closed but active connections may remain, a call to Close() can force-close any remaining active connections.

func (*HTTPServer) Start added in v1.12.0

func (s *HTTPServer) Start() error

Start starts the server, and checks if it comes online fully.

func (*HTTPServer) Stop added in v1.4.2

func (s *HTTPServer) Stop(ctx context.Context) error

Stop is a convenience method to gracefully shut down the server, but force-close if the ctx is cancelled. The ctx error is not returned when the force-close is successful.

type HTTPTimeouts added in v1.4.2

type HTTPTimeouts struct {
	// 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
}

HTTPTimeouts represents the configuration params for the HTTP RPC server.

type Option added in v1.12.0

type Option func(cfg *config)

Option is a general config option.

func WithHTTPOptions added in v1.12.0

func WithHTTPOptions(options ...HTTPOption) Option

func WithServerTLS added in v1.12.0

func WithServerTLS(tlsCfg *ServerTLSConfig) Option

type ServerTLSConfig added in v1.12.0

type ServerTLSConfig struct {
	Config    *tls.Config
	CLIConfig *optls.CLIConfig // paths to certificate and key files
}

type WrappedResponseWriter

type WrappedResponseWriter struct {
	StatusCode  int
	ResponseLen int

	UpgradeAttempt bool
	// contains filtered or unexported fields
}

func NewWrappedResponseWriter

func NewWrappedResponseWriter(w http.ResponseWriter) *WrappedResponseWriter

func (*WrappedResponseWriter) Header

func (w *WrappedResponseWriter) Header() http.Header

func (*WrappedResponseWriter) Hijack added in v1.11.0

Hijack implements http.Hijacker, so the WrappedResponseWriter is compatible as middleware for websocket-upgrades that take over the connection.

func (*WrappedResponseWriter) Write

func (w *WrappedResponseWriter) Write(bytes []byte) (int, error)

func (*WrappedResponseWriter) WriteHeader

func (w *WrappedResponseWriter) WriteHeader(statusCode int)

Jump to

Keyboard shortcuts

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