xhttp

package
v1.113.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MPL-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package xhttp provides HTTP-related utilities.

Index

Constants

View Source
const (
	ProtocolHTTP  = "http"
	ProtocolHTTPS = "https"
)

Constants for protocols the server can provide.

Variables

This section is empty.

Functions

func DisableCaching added in v1.104.0

func DisableCaching(w http.ResponseWriter)

DisableCaching disables caching for the given response writer. To be effective, should be called before any data is written.

func ErrorStatus added in v1.103.0

func ErrorStatus(w http.ResponseWriter, statusCode int)

ErrorStatus sends an HTTP response header with 'statusCode' and follows it with the standard text for that code as the body.

Types

type BasicAuth

type BasicAuth struct {
	Realm string
	// Lookup provides a way to map a user in a realm to a password. The returned password should have already been
	// passed through the Hasher function.
	Lookup func(user, realm string) ([]byte, bool)
	Hasher func(input string) []byte
}

BasicAuth provides basic HTTP authentication.

func (*BasicAuth) Wrap

func (ba *BasicAuth) Wrap(handler http.Handler) http.Handler

Wrap an http.Handler, requiring Basic Authentication.

type Metadata added in v1.103.0

type Metadata struct {
	Logger *slog.Logger
	User   string
}

Metadata holds auxiliary information for a request.

func MetadataFromRequest added in v1.103.0

func MetadataFromRequest(req *http.Request) *Metadata

MetadataFromRequest returns the Metadata from the request.

type Server added in v1.103.0

type Server struct {
	WebServer           *http.Server
	Logger              *slog.Logger
	CertFile            string
	KeyFile             string
	StartedChan         chan any // If not nil, will be closed once the server is ready to accept connections
	ShutdownGracePeriod time.Duration
	ShutdownCallback    func(*slog.Logger)
	// contains filtered or unexported fields
}

Server holds the data necessary for the server.

func (*Server) Addresses added in v1.103.0

func (s *Server) Addresses() []string

Addresses returns the host addresses being listened to.

func (*Server) LocalBaseURL added in v1.103.0

func (s *Server) LocalBaseURL() string

LocalBaseURL returns the local base URL that will reach the server.

func (*Server) Port added in v1.103.0

func (s *Server) Port() int

Port returns the port being listened to.

func (*Server) Protocol added in v1.103.0

func (s *Server) Protocol() string

Protocol returns the protocol this server is handling.

func (*Server) Run added in v1.103.0

func (s *Server) Run() error

Run the server. Does not return until the server is shutdown.

func (*Server) ServeHTTP added in v1.103.0

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (*Server) Shutdown added in v1.103.0

func (s *Server) Shutdown()

Shutdown the server gracefully.

func (*Server) String added in v1.103.0

func (s *Server) String() string

type StatusResponseWriter

type StatusResponseWriter struct {
	Original http.ResponseWriter
	Head     bool
	// contains filtered or unexported fields
}

StatusResponseWriter wraps an http.ResponseWriter and provides methods to retrieve the status code and number of bytes written.

func (*StatusResponseWriter) BytesWritten

func (w *StatusResponseWriter) BytesWritten() int

BytesWritten returns the number of bytes written.

func (*StatusResponseWriter) Flush

func (w *StatusResponseWriter) Flush()

Flush implements http.Flusher.

func (*StatusResponseWriter) Header

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

Header implements http.ResponseWriter.

func (*StatusResponseWriter) Status

func (w *StatusResponseWriter) Status() int

Status returns the status that was set, or http.StatusOK if no call to WriteHeader() was made.

func (*StatusResponseWriter) Write

func (w *StatusResponseWriter) Write(data []byte) (int, error)

Write implements http.ResponseWriter.

func (*StatusResponseWriter) WriteHeader

func (w *StatusResponseWriter) WriteHeader(status int)

WriteHeader implements http.ResponseWriter.

Jump to

Keyboard shortcuts

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