httputil

package
v1.16.13 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 13 Imported by: 29

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 Downloader

type Downloader struct {
	Client     *http.Client
	Progressor ioutil.Progressor
	MaxSize    int64
}

func (*Downloader) Download

func (d *Downloader) Download(ctx context.Context, url string, out io.Writer) error

type HTTPOption

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

func WithMaxHeaderBytes(max int) HTTPOption

func WithTimeouts

func WithTimeouts(timeouts HTTPTimeouts) HTTPOption

type HTTPServer

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

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

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

func (*HTTPServer) Addr

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

func (s *HTTPServer) Close() error

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

func (*HTTPServer) Closed

func (s *HTTPServer) Closed() bool

func (*HTTPServer) HTTPEndpoint

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) Port

func (s *HTTPServer) Port() (int, error)

Port returns the port that the server is listening on.

func (*HTTPServer) Shutdown

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

func (s *HTTPServer) Start() error

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

func (*HTTPServer) Stop

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

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

type Option func(cfg *config)

Option is a general config option.

func WithHTTPOptions

func WithHTTPOptions(options ...HTTPOption) Option

func WithServerTLS

func WithServerTLS(tlsCfg *ServerTLSConfig) Option

type ServerTLSConfig

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

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