httputil

package
v0.24.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accepts added in v0.19.0

func Accepts(header string, mimeTypes ...string) string

Accepts returns the most highly desired mime type supported by the server. Ignores parameters like charsets.

func AssertStatusCode added in v0.16.0

func AssertStatusCode(r *http.Response, statusCode int) error

AssertStatusCode returns an error if the response does not match the given status code.

func InstrumentHandler added in v0.13.0

func InstrumentHandler(handler http.Handler) http.Handler

InstrumentHandler instruments a http.Handler. Instrumented handlers are required to set the http.route attribute to the span if available.

If CORS is in use, it's the wrapped handler's responsibility to allow the traceresponse header.

SEE: https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-server-semantic-conventions

func NewTransport added in v0.19.0

func NewTransport() *http.Transport

NewTransport returns a *http.Transport with sane defaults.

func ParseWWWAuthenticateHeader added in v0.19.0

func ParseWWWAuthenticateHeader(header string) (string, map[string]string, error)

ParseWWWAuthenticateHeader parses a Www-Authenticate header. Returns the scheme and parameters.

SEE: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate.

func ResolveRequestURL

func ResolveRequestURL(r *http.Request) (*url.URL, error)

ResolveRequestURL resolves the URL in request using common headers like X-Forwarded-Host and X-Forwarded-Proto.

func SpanFromRequest added in v0.13.0

func SpanFromRequest(r *http.Request) (context.Context, trace.Span)

SpanFromRequest returns the current span for a handler instrumented using InstrumentHandler. Panics if handler is not instrumented. A span received this way MUST NOT be manually closed. It is closed once the instrumented handler completed the request.

Types

type AuthHandler added in v0.16.0

type AuthHandler interface {
	// HandleAuth authenticates a request.
	HandleAuth(*http.Request) error
}

AuthHandler implements request authentication.

type AuthHandlerFunc added in v0.16.0

type AuthHandlerFunc func(*http.Request) error

func (AuthHandlerFunc) HandleAuth added in v0.16.0

func (f AuthHandlerFunc) HandleAuth(r *http.Request) error

type AuthMux added in v0.16.0

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

AuthMux is an HTTP auth multiplexer. It matches URLs of auth requests against a list of registered patterns and calls the handler for the pattern that most closely matches the request.

func NewAuthMux added in v0.16.0

func NewAuthMux() *AuthMux

func (*AuthMux) Copy added in v0.16.0

func (a *AuthMux) Copy(other *AuthMux)

Copy copies all patterns from another AuthMux to this one.

func (*AuthMux) Handle added in v0.16.0

func (a *AuthMux) Handle(pattern string, handler AuthHandler)

Handle registers handler for pattern.

func (*AuthMux) HandleAuth added in v0.16.0

func (a *AuthMux) HandleAuth(r *http.Request) error

HandleAuth implements [AuthHandler.HandleAuth].

func (*AuthMux) HandleFunc added in v0.16.0

func (a *AuthMux) HandleFunc(pattern string, handler func(*http.Request) error)

Handle registers handler for pattern.

func (*AuthMux) SetHeader added in v0.16.0

func (a *AuthMux) SetHeader(key string, value string)

SetHeader sets a header to set on all requests.

type BasicAuthHandler added in v0.16.0

type BasicAuthHandler struct {
	Username string
	Password string
}

BasicAuthHandler auths requests using a username/password via the Basic authorization scheme.

func (BasicAuthHandler) HandleAuth added in v0.16.0

func (h BasicAuthHandler) HandleAuth(r *http.Request) error

type Client

type Client struct {
	http.Client

	UserAgent string
	// contains filtered or unexported fields
}

func NewClient

func NewClient(cache cache.Cache, maxAge time.Duration) *Client

NewClient returns a Client with cache invalidated after maxAge.

func (*Client) CacheKey

func (c *Client) CacheKey(req *http.Request) string

CacheKey returns the key used when cacheing data for a request.

func (*Client) Collect

func (c *Client) Collect(ch chan<- prometheus.Metric)

Collect implements prometheus.Collector.

func (*Client) Describe

func (c *Client) Describe(descs chan<- *prometheus.Desc)

Describe implements prometheus.Collector.

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

See http.Client.Do.

func (*Client) DoCached

func (c *Client) DoCached(req *http.Request) (*http.Response, error)

DoCached returns a cached response for the request. If no cache entry exists, http.Client.Do is used. If the request succeeds, its response is cached if the response code is 2xx. It is the caller's responsibility to ensure that caching the request is sensible (i.e. only for GET requests). NOTE: Cached responses for URLs that were redirected will not have the correct request URL for the response - it will be the original request rather than the request to the final resource.

type Error added in v0.16.0

type Error struct {
	// Status is the HTTP status of the response.
	Status string
	// StatusCode is the HTTP status code of the response.
	StatusCode int
	// Message is a message communicated through other means, such as through an
	// Www-Authenticate header error parameter.
	Message string
}

Error is a base HTTP error useful for HTTP clients.

func (Error) Error added in v0.16.0

func (e Error) Error() string

type GzipWriter added in v0.20.0

type GzipWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

GzipWriter is an http.ResponseWriter that gzips the payload. Not safe for parallel use. Caller must set the correct content encoding header and close the writer.

func (*GzipWriter) Close added in v0.20.0

func (g *GzipWriter) Close() error

Close implements io.Closer.

func (*GzipWriter) Write added in v0.20.0

func (g *GzipWriter) Write(p []byte) (int, error)

Write implements http.ResponseWriter.

type Link struct {
	URL    *url.URL
	Params map[string]string
}

func ParseLinkHeader

func ParseLinkHeader(origin *url.URL, header string) ([]Link, error)

ParseLinkHeader parses a Link header.

SEE: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link.

type Requester added in v0.19.0

type Requester interface {
	Do(*http.Request) (*http.Response, error)
	DoCached(*http.Request) (*http.Response, error)
}

type StatusRecorder added in v0.13.0

type StatusRecorder struct {
	Writer http.ResponseWriter
	// contains filtered or unexported fields
}

StatusRecorder is a http.ResponseWriter and http.Flusher that records the status code.

func (*StatusRecorder) Flush added in v0.15.0

func (s *StatusRecorder) Flush()

Flush implements http.Flusher.

func (*StatusRecorder) Header added in v0.13.0

func (s *StatusRecorder) Header() http.Header

Header implements http.ResponseWriter.

func (*StatusRecorder) StatusCode added in v0.13.0

func (s *StatusRecorder) StatusCode() int

StatusCode returns the recorded status code.

func (*StatusRecorder) Write added in v0.13.0

func (s *StatusRecorder) Write(b []byte) (int, error)

Write implements http.ResponseWriter.

func (*StatusRecorder) WriteHeader added in v0.13.0

func (s *StatusRecorder) WriteHeader(statusCode int)

WriteHeader implements http.ResponseWriter.

Jump to

Keyboard shortcuts

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