httpx

package
v1.60.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 27 Imported by: 27

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAccessConfig = errors.New("request not permitted by access config")

ErrAccessConfig is returned when provided access config prevents request

View Source
var ErrResponseSize = errors.New("response body exceeds size limit")

ErrResponseSize is returned when response size exceeds provided limit

View Source
var MockConnectionError = &MockResponse{Status: 0, Headers: nil, Body: []byte{}, BodyIsString: true, BodyRepeat: 0}

MockConnectionError mocks a connection error

Functions

func BasicAuth added in v1.30.0

func BasicAuth(username, password string) string

BasicAuth returns the Authorization header value for HTTP Basic auth

func DefaultShouldRetry

func DefaultShouldRetry(request *http.Request, response *http.Response, withDelay time.Duration) bool

DefaultShouldRetry is the default function for determining if a response should be retried

func DetectContentType added in v1.18.0

func DetectContentType(d []byte) (string, string)

DetectContentType is a replacement for http.DetectContentType which leans on the github.com/gabriel-vasile/mimetype library to support more types, and additionally returns the extension (including leading period) associated with the detected type.

func Do

func Do(client *http.Client, request *http.Request, retries *RetryConfig, access *AccessConfig) (*http.Response, error)

Do makes the given HTTP request using the current requestor and retry config

func NewRequest

func NewRequest(method string, url string, body io.Reader, headers map[string]string) (*http.Request, error)

NewRequest is a convenience method to create a request with the given headers

func ParseNetworks added in v1.42.2

func ParseNetworks(addrs ...string) ([]net.IP, []*net.IPNet, error)

ParseNetworks parses a list of IPs and IP networks (written in CIDR notation)

func ParseRetryAfter

func ParseRetryAfter(value string) time.Duration

ParseRetryAfter parses value of Retry-After headers which can be date or delay in seconds see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After

func ReplaceEscapedNulls added in v1.26.0

func ReplaceEscapedNulls(data string, repl string) string

replaces any `\u0000` sequences with the given replacement sequence which may be empty. A sequence such as `\\u0000` is preserved as it is an escaped slash followed by the sequence `u0000`

func SetRequestor

func SetRequestor(requestor Requestor)

SetRequestor sets the requestor used by Request

Types

type AccessConfig

type AccessConfig struct {
	ResolveTimeout time.Duration
	DisallowedIPs  []net.IP
	DisallowedNets []*net.IPNet
}

AccessConfig configures what can be accessed

func NewAccessConfig

func NewAccessConfig(resolveTimeout time.Duration, disallowedIPs []net.IP, disallowedNets []*net.IPNet) *AccessConfig

NewAccessConfig creates a new access config

func (*AccessConfig) Allow

func (c *AccessConfig) Allow(request *http.Request) (bool, error)

Allow determines whether the given request should be allowed

type Log added in v1.26.0

type Log struct {
	*LogWithoutTime
	CreatedOn time.Time `json:"created_on" validate:"required"`
}

Log is a single HTTP trace that can be serialized/deserialized to/from JSON.

func NewLog added in v1.26.0

func NewLog(trace *Trace, trimURLTo, trimTracesTo int, redact stringsx.Redactor) *Log

NewLog creates a new HTTP log from a trace

type LogWithoutTime added in v1.26.0

type LogWithoutTime struct {
	URL        string `json:"url" validate:"required"`
	StatusCode int    `json:"status_code,omitempty"`
	Request    string `json:"request" validate:"required"`
	Response   string `json:"response,omitempty"`
	ElapsedMS  int    `json:"elapsed_ms"`
	Retries    int    `json:"retries"`
}

LogWithoutTime is a single HTTP trace that can be serialized/deserialized to/from JSON. Note that this struct has no time component because it's intended to be embedded in something that does.

func NewLogWithoutTime added in v1.26.0

func NewLogWithoutTime(trace *Trace, trimURLTo, trimTracesTo int, redact stringsx.Redactor) *LogWithoutTime

NewLogWithoutTime creates a new log

type MockRequestor

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

MockRequestor is a requestor which can be mocked with responses for given URLs

func NewMockRequestor

func NewMockRequestor(mocks map[string][]*MockResponse) *MockRequestor

NewMockRequestor creates a new mock requestor with the given mocks

func (*MockRequestor) Clone

func (r *MockRequestor) Clone() *MockRequestor

Clone returns a clone of this requestor

func (*MockRequestor) Do

func (r *MockRequestor) Do(client *http.Client, request *http.Request) (*http.Response, error)

Do returns the mocked reponse for the given request

func (*MockRequestor) HasUnused

func (r *MockRequestor) HasUnused() bool

HasUnused returns true if there are unused mocks leftover

func (*MockRequestor) MarshalJSON

func (r *MockRequestor) MarshalJSON() ([]byte, error)

func (*MockRequestor) Requests added in v1.30.2

func (r *MockRequestor) Requests() []*http.Request

Requests returns the received requests

func (*MockRequestor) SetIgnoreLocal added in v1.31.0

func (r *MockRequestor) SetIgnoreLocal(ignore bool)

SetIgnoreLocal sets whether the requestor should ignore requests on localhost and delegate these the the default requestor.

func (*MockRequestor) UnmarshalJSON

func (r *MockRequestor) UnmarshalJSON(data []byte) error

type MockResponse

type MockResponse struct {
	Status       int
	Headers      map[string]string
	Body         []byte
	BodyIsString bool
	BodyRepeat   int
}

func NewMockResponse

func NewMockResponse(status int, headers map[string]string, body []byte) *MockResponse

NewMockResponse creates a new mock response

func (*MockResponse) Make

func (m *MockResponse) Make(request *http.Request) *http.Response

Make mocks making the given request and returning this as the response

func (*MockResponse) MarshalJSON

func (m *MockResponse) MarshalJSON() ([]byte, error)

func (*MockResponse) UnmarshalJSON

func (m *MockResponse) UnmarshalJSON(data []byte) error

type Recorder added in v1.5.0

type Recorder struct {
	Trace *Trace

	ResponseWriter http.ResponseWriter
	// contains filtered or unexported fields
}

Recorder is a utility for creating traces of HTTP requests being handled

func NewRecorder added in v1.5.0

func NewRecorder(r *http.Request, w http.ResponseWriter, reconstruct bool) (*Recorder, error)

NewRecorder creates a new recorder for an HTTP request. If `originalRequest` is true, it tries to reconstruct the original request object.

func (*Recorder) End added in v1.5.0

func (r *Recorder) End() error

End is called when the response has been written and generates the trace

type Requestor

type Requestor interface {
	Do(*http.Client, *http.Request) (*http.Response, error)
}

Requestor is anything that can make an HTTP request with a client

var DefaultRequestor Requestor = defaultRequestor{}

DefaultRequestor is the default HTTP requestor

type RetryConfig

type RetryConfig struct {
	Backoffs    []time.Duration
	Jitter      float64
	ShouldRetry func(*http.Request, *http.Response, time.Duration) bool
}

RetryConfig configures if and how retrying of requests happens

func NewExponentialRetries

func NewExponentialRetries(initialBackoff time.Duration, count int, jitter float64) *RetryConfig

NewExponentialRetries creates a new retry config with the given delays

func NewFixedRetries

func NewFixedRetries(backoffs ...time.Duration) *RetryConfig

NewFixedRetries creates a new retry config with the given backoffs

func (*RetryConfig) Backoff

func (r *RetryConfig) Backoff(n int) time.Duration

Backoff gets the backoff time for the nth retry

func (*RetryConfig) MaxRetries

func (r *RetryConfig) MaxRetries() int

MaxRetries gets the maximum number of retries allowed

type Trace

type Trace struct {
	Request       *http.Request
	RequestTrace  []byte
	Response      *http.Response
	ResponseTrace []byte
	ResponseBody  []byte // response body stored separately
	StartTime     time.Time
	EndTime       time.Time
	Retries       int
}

Trace holds the complete trace of an HTTP request/response

func DoTrace

func DoTrace(client *http.Client, request *http.Request, retries *RetryConfig, access *AccessConfig, maxBodyBytes int) (*Trace, error)

DoTrace makes the given request saving traces of the complete request and response.

  • If the request is successful, the trace will have a response and response body
  • If reading the body errors, the trace will have a response but no response body
  • If connection fails, the trace will have a request but no response or response body

func (*Trace) SanitizedRequest added in v1.29.0

func (t *Trace) SanitizedRequest(placeholder string) string

SanitizedRequest returns a valid UTF-8 string version of the request, substituting the body with a placeholder if it isn't valid UTF-8. It also strips any NULL characters as not all external dependencies can handle those.

func (*Trace) SanitizedResponse added in v1.12.0

func (t *Trace) SanitizedResponse(placeholder string) string

SanitizedResponse returns a valid UTF-8 string version of the response, substituting the body with a placeholder if it isn't valid UTF-8. It also strips any NULL characters as not all external dependencies can handle those.

func (*Trace) String

func (t *Trace) String() string

type WebSocket added in v1.51.0

type WebSocket interface {
	// Start begins reading and writing of messages on this socket
	Start()

	// Send sends the given message over the socket
	Send([]byte)

	// Close closes the socket connection
	Close(int)

	// OnMessage is called when the socket receives a message
	OnMessage(func([]byte))

	// OnClose is called when the socket is closed (even if we initiate the close)
	OnClose(func(int))
}

WebSocket provides a websocket interface similar to that of Javascript.

func NewWebSocket added in v1.51.0

func NewWebSocket(w http.ResponseWriter, r *http.Request, maxReadBytes int64, sendBuffer int) (WebSocket, error)

NewWebSocket creates a new web socket from a regular HTTP request

Jump to

Keyboard shortcuts

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