httpz

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package httpz provides functionality supplemental to stdlib http. Indeed, some of the functions are copied verbatim from stdlib. The jumping-off point is httpz.NewClient.

Design note: this package contains generally fairly straightforward HTTP functionality, but the Opt / TripFunc config mechanism is a bit experimental. And probably tries to be a bit too clever. It may change.

And one last thing: remember kids, ALWAYS close your response bodies.

Index

Constants

This section is empty.

Variables

View Source
var DefaultHeaderTimeout = OptRequestTimeout(time.Second * 5)

DefaultHeaderTimeout is the default header timeout as used by NewDefaultClient.

View Source
var DefaultTLSVersion = minTLSVersion(tls.VersionTLS10)

DefaultTLSVersion is the default minimum TLS version, as used by NewDefaultClient.

View Source
var DefaultUserAgent = OptUserAgent(buildinfo.Get().UserAgent())

DefaultUserAgent is the default User-Agent header value, as used by NewDefaultClient.

Functions

func Filename

func Filename(resp *http.Response) string

Filename returns the filename to use for a download. It first checks the Content-Disposition header, and if that's not present, it uses the last path segment of the URL. The filename is sanitized. It's possible that the returned value will be empty string; the caller should handle that situation themselves.

func NewClient

func NewClient(opts ...Opt) *http.Client

NewClient returns a new HTTP client configured with opts.

func NewDefaultClient

func NewDefaultClient() *http.Client

NewDefaultClient invokes NewClient with default settings.

func NopTripFunc

func NopTripFunc(next http.RoundTripper, req *http.Request) (*http.Response, error)

NopTripFunc is a TripFunc that does nothing.

func ReadResponseHeader

func ReadResponseHeader(r *bufio.Reader, req *http.Request) (resp *http.Response, err error)

ReadResponseHeader is a fork of http.ReadResponse that reads only the header from req and not the body. Note that resp.Body will be nil, and that the resp object is borked for general use.

func RequestLogValue

func RequestLogValue(req *http.Request) slog.Value

RequestLogValue implements slog.LogValuer for http.Request.

func ResponseLogValue

func ResponseLogValue(resp *http.Response) slog.Value

ResponseLogValue implements slog.LogValuer for http.Response.

func RoundTrip

func RoundTrip(next http.RoundTripper, fn TripFunc) http.RoundTripper

RoundTrip adapts a TripFunc to http.RoundTripper.

func StatusText

func StatusText(code int) string

StatusText is like http.StatusText, but also includes the code, e.g. "200 OK".

Types

type Opt

type Opt interface {
	// contains filtered or unexported methods
}

Opt is an option that can be passed to NewClient to configure the client.

type OptInsecureSkipVerify

type OptInsecureSkipVerify bool

OptInsecureSkipVerify is an Opt that can be passed to NewClient that, when true, disables TLS verification.

type TripFunc

type TripFunc func(next http.RoundTripper, req *http.Request) (*http.Response, error)

TripFunc is a function that implements http.RoundTripper. It is commonly used with RoundTrip to decorate an existing http.RoundTripper.

func OptRequestDelay

func OptRequestDelay(delay time.Duration) TripFunc

OptRequestDelay is passed to NewClient to delay the request by the specified duration. This is useful for testing.

func OptRequestTimeout

func OptRequestTimeout(timeout time.Duration) TripFunc

OptRequestTimeout is passed to NewClient to set a timeout for just getting the initial response headers. This is useful if you expect a response within, say, 2 seconds, but you expect the body to take longer to read.

Contrast with OptResponseTimeout.

func OptResponseTimeout

func OptResponseTimeout(timeout time.Duration) TripFunc

OptResponseTimeout is passed to NewClient to set the total request timeout, including reading the body. This is basically the same as a traditional request timeout via context.WithTimeout. If timeout is zero, this is no-op.

Contrast with OptRequestTimeout.

func OptUserAgent

func OptUserAgent(ua string) TripFunc

OptUserAgent is passed to NewClient to set the User-Agent header.

Jump to

Keyboard shortcuts

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