Documentation
¶
Overview ¶
Package engine implements the core HTTP client engine with request processing, response handling, connection pooling, retry logic, and middleware support.
Index ¶
- Variables
- func CloneHeader(src http.Header) http.Header
- func FormatQueryParam(v any) string
- func QueryEscape(s string) string
- func ReleaseRequest(req *Request)
- func ReleaseResponse(r *Response)
- type Client
- type ClientError
- type Config
- type ErrorType
- type Request
- func (r *Request) AllowPrivateIPs() *bool
- func (r *Request) Body() any
- func (r *Request) Context() context.Context
- func (r *Request) Cookies() []http.Cookie
- func (r *Request) EnsureQueryParams() map[string]any
- func (r *Request) FollowRedirects() *bool
- func (r *Request) Headers() map[string]string
- func (r *Request) MaxRedirects() *int
- func (r *Request) MaxRetries() int
- func (r *Request) Method() string
- func (r *Request) OnRequest() requestCallback
- func (r *Request) OnResponse() responseCallback
- func (r *Request) QueryParams() map[string]any
- func (r *Request) SanitizedURL() string
- func (r *Request) SetAllowPrivateIPs(v *bool)
- func (r *Request) SetBody(v any)
- func (r *Request) SetContext(v context.Context)
- func (r *Request) SetCookies(v []http.Cookie)
- func (r *Request) SetFollowRedirects(v *bool)
- func (r *Request) SetHeader(key, value string)
- func (r *Request) SetHeaders(v map[string]string)
- func (r *Request) SetMaxRedirects(v *int)
- func (r *Request) SetMaxRetries(v int)
- func (r *Request) SetMethod(v string)
- func (r *Request) SetOnRequest(cb requestCallback)
- func (r *Request) SetOnResponse(cb responseCallback)
- func (r *Request) SetQueryParams(v map[string]any)
- func (r *Request) SetSanitizedURL(v string)
- func (r *Request) SetStreamBody(v bool)
- func (r *Request) SetTimeout(v time.Duration)
- func (r *Request) SetURL(v string)
- func (r *Request) StreamBody() bool
- func (r *Request) Timeout() time.Duration
- func (r *Request) URL() string
- type RequestOption
- type Response
- func (r *Response) Attempts() int
- func (r *Response) Body() string
- func (r *Response) ContentLength() int64
- func (r *Response) Cookies() []*http.Cookie
- func (r *Response) Duration() time.Duration
- func (r *Response) Headers() http.Header
- func (r *Response) Proto() string
- func (r *Response) RawBody() []byte
- func (r *Response) RawBodyReader() io.ReadCloser
- func (r *Response) RedirectChain() []string
- func (r *Response) RedirectCount() int
- func (r *Response) RequestHeaders() http.Header
- func (r *Response) RequestMethod() string
- func (r *Response) RequestURL() string
- func (r *Response) SetAttempts(v int)
- func (r *Response) SetBody(v string)
- func (r *Response) SetContentLength(v int64)
- func (r *Response) SetCookies(v []*http.Cookie)
- func (r *Response) SetDuration(v time.Duration)
- func (r *Response) SetHeader(key string, values ...string)
- func (r *Response) SetHeaders(v http.Header)
- func (r *Response) SetProto(v string)
- func (r *Response) SetRawBody(v []byte)
- func (r *Response) SetRawBodyReader(rc io.ReadCloser)
- func (r *Response) SetRedirectChain(v []string)
- func (r *Response) SetRedirectCount(v int)
- func (r *Response) SetRequestHeaders(v http.Header)
- func (r *Response) SetRequestMethod(v string)
- func (r *Response) SetRequestURL(v string)
- func (r *Response) SetStatus(v string)
- func (r *Response) SetStatusCode(v int)
- func (r *Response) Status() string
- func (r *Response) StatusCode() int
- func (r *Response) TransferHeaders() http.Header
- func (r *Response) TransferRequestHeaders() http.Header
Constants ¶
This section is empty.
Variables ¶
var ErrClientClosed = errors.New("client is closed")
ErrClientClosed is returned when attempting to use a closed client.
Functions ¶
func CloneHeader ¶ added in v1.3.9
CloneHeader creates a deep copy of headers using batch allocation. Returns a newly allocated header map that can be safely modified. SECURITY: Always allocates new slices to prevent data leakage between requests. OPTIMIZATION: Uses batch allocation to reduce N allocations (one per header) to 1 allocation. For single-value headers (the common case), uses a shared backing array to reduce slice header overhead.
func FormatQueryParam ¶ added in v1.4.0
FormatQueryParam converts a value to string for query parameters. Optimized to avoid fmt.Sprintf allocations for common types.
func QueryEscape ¶ added in v1.5.0
QueryEscape performs URL query escaping with minimal allocations. Returns the original string if no escaping is needed (zero allocation).
func ReleaseRequest ¶ added in v1.5.0
func ReleaseRequest(req *Request)
ReleaseRequest returns a Request to the shared pool after clearing all fields. Resets maxRetries to maxRetriesUnset so recycled requests inherit the client's retry config.
func ReleaseResponse ¶ added in v1.4.0
func ReleaseResponse(r *Response)
ReleaseResponse returns a Response to the pool for reuse. Call this when the Response data has been consumed and copied elsewhere. After calling this, the Response must not be used.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the internal HTTP client that manages requests, responses, retries, and connection pooling.
func NewClient ¶
NewClient creates a new engine Client with the given configuration and optional client options.
type ClientError ¶
type ClientError struct {
Type ErrorType
Message string
Cause error
URL string
Method string
Attempts int
StatusCode int // HTTP status code if applicable
Host string // Host for circuit breaker errors
}
ClientError represents a classified HTTP client error with context about the failed request.
func (*ClientError) Code ¶
func (e *ClientError) Code() string
Code returns a short string code identifying the error type (e.g., "NETWORK_ERROR", "TIMEOUT").
func (*ClientError) Error ¶
func (e *ClientError) Error() string
func (*ClientError) IsRetryable ¶
func (e *ClientError) IsRetryable() bool
IsRetryable determines if the error is retryable based on its type and cause.
func (*ClientError) Unwrap ¶
func (e *ClientError) Unwrap() error
func (*ClientError) WithType ¶ added in v1.4.0
func (e *ClientError) WithType(t ErrorType) *ClientError
WithType returns a copy of the error with the specified type set.
type Config ¶
type Config struct {
Timeout time.Duration
DialTimeout time.Duration
KeepAlive time.Duration
TLSHandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
IdleConnTimeout time.Duration
MaxResponseHeaderBytes int64
MaxIdleConns int
MaxIdleConnsPerHost int
MaxConnsPerHost int
ProxyURL string
// System proxy configuration
EnableSystemProxy bool // Automatically detect and use system proxy settings
TLSConfig *tls.Config
MinTLSVersion uint16
MaxTLSVersion uint16
InsecureSkipVerify bool
MaxResponseBodySize int64
MaxRequestBodySize int64
MaxDecompressedBodySize int64
ValidateURL bool
ValidateHeaders bool
AllowPrivateIPs bool
ExemptNets []*net.IPNet
StrictContentLength bool
MaxRetries int
RetryDelay time.Duration
MaxRetryDelay time.Duration
BackoffFactor float64
Jitter bool
// CustomRetryPolicy allows providing a custom retry policy implementation.
// If set, it overrides the built-in retry logic.
CustomRetryPolicy types.RetryPolicy
UserAgent string
Headers map[string]string
FollowRedirects bool
MaxRedirects int
EnableHTTP2 bool
CookieJar http.CookieJar
EnableCookies bool
// DNS configuration
EnableDoH bool
DoHCacheTTL time.Duration
// Redirect whitelist configuration
RedirectWhitelist *security.DomainWhitelist
// Certificate pinning
CertificatePinner security.CertificatePinner
}
Config defines the HTTP client configuration. Config should be treated as immutable after creation.
type ErrorType ¶
type ErrorType int
ErrorType represents the classification of an HTTP client error.
const ( // ErrorTypeUnknown indicates an unknown or unclassified error. ErrorTypeUnknown ErrorType = iota // ErrorTypeNetwork indicates a network-level error. ErrorTypeNetwork // ErrorTypeTimeout indicates a timeout occurred during the request. ErrorTypeTimeout // ErrorTypeContextCanceled indicates the request context was canceled. ErrorTypeContextCanceled // ErrorTypeResponseRead indicates an error reading the response body. ErrorTypeResponseRead // ErrorTypeTransport indicates an HTTP transport-level error. ErrorTypeTransport // ErrorTypeRetryExhausted indicates all retry attempts have been exhausted. ErrorTypeRetryExhausted // ErrorTypeTLS indicates a TLS handshake or protocol error. ErrorTypeTLS // ErrorTypeCertificate indicates a certificate validation error. ErrorTypeCertificate // ErrorTypeDNS indicates a DNS resolution error. ErrorTypeDNS // ErrorTypeValidation indicates a request validation error. ErrorTypeValidation // ErrorTypeHTTP indicates an HTTP-level error (4xx, 5xx responses). ErrorTypeHTTP )
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request represents an HTTP request with method, URL, headers, body, and options.
func AcquireRequest ¶ added in v1.5.0
func AcquireRequest() *Request
AcquireRequest retrieves a Request from the shared pool. The caller must call ReleaseRequest when done.
func (*Request) AllowPrivateIPs ¶ added in v1.5.2
func (*Request) EnsureQueryParams ¶ added in v1.5.0
func (*Request) FollowRedirects ¶ added in v1.3.0
func (*Request) MaxRedirects ¶ added in v1.3.0
func (*Request) MaxRetries ¶
func (*Request) OnRequest ¶ added in v1.3.8
func (r *Request) OnRequest() requestCallback
Callback accessors
func (*Request) OnResponse ¶ added in v1.3.8
func (r *Request) OnResponse() responseCallback
func (*Request) QueryParams ¶
func (*Request) SanitizedURL ¶ added in v1.5.0
func (*Request) SetAllowPrivateIPs ¶ added in v1.5.2
func (*Request) SetContext ¶ added in v1.3.8
func (*Request) SetCookies ¶ added in v1.3.8
func (*Request) SetFollowRedirects ¶ added in v1.3.8
func (*Request) SetHeaders ¶ added in v1.3.8
func (*Request) SetMaxRedirects ¶ added in v1.3.8
func (*Request) SetMaxRetries ¶ added in v1.3.8
func (*Request) SetOnRequest ¶ added in v1.3.8
func (r *Request) SetOnRequest(cb requestCallback)
func (*Request) SetOnResponse ¶ added in v1.3.8
func (r *Request) SetOnResponse(cb responseCallback)
func (*Request) SetQueryParams ¶ added in v1.3.8
func (*Request) SetSanitizedURL ¶ added in v1.5.0
func (*Request) SetStreamBody ¶ added in v1.4.0
func (*Request) SetTimeout ¶ added in v1.3.8
func (*Request) StreamBody ¶ added in v1.4.0
type RequestOption ¶
RequestOption is a function that modifies a Request before it is sent.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response represents an HTTP response. Response objects are safe to read from multiple goroutines after they are returned.
func (*Response) ContentLength ¶
func (*Response) RawBodyReader ¶ added in v1.4.0
func (r *Response) RawBodyReader() io.ReadCloser
func (*Response) RedirectChain ¶ added in v1.3.0
func (*Response) RedirectCount ¶ added in v1.3.0
func (*Response) RequestHeaders ¶ added in v1.3.0
func (*Response) RequestMethod ¶ added in v1.3.8
func (*Response) RequestURL ¶ added in v1.3.8
func (*Response) SetAttempts ¶ added in v1.3.8
func (*Response) SetContentLength ¶ added in v1.3.8
func (*Response) SetCookies ¶ added in v1.3.8
func (*Response) SetDuration ¶ added in v1.3.8
func (*Response) SetHeader ¶ added in v1.3.8
SetHeader sets a header with multiple values (implements ResponseMutator)
func (*Response) SetHeaders ¶ added in v1.3.8
func (*Response) SetRawBody ¶ added in v1.3.8
func (*Response) SetRawBodyReader ¶ added in v1.4.1
func (r *Response) SetRawBodyReader(rc io.ReadCloser)
SetRawBodyReader replaces the raw body reader. Passing nil transfers ownership away from the response so that ReleaseResponse will not close it.
func (*Response) SetRedirectChain ¶ added in v1.3.8
func (*Response) SetRedirectCount ¶ added in v1.3.8
func (*Response) SetRequestHeaders ¶ added in v1.3.8
func (*Response) SetRequestMethod ¶ added in v1.3.8
func (*Response) SetRequestURL ¶ added in v1.3.8
func (*Response) SetStatusCode ¶ added in v1.3.8
Mutators (implement ResponseMutator)
func (*Response) StatusCode ¶
Accessors (implement ResponseAccessor)
func (*Response) TransferHeaders ¶ added in v1.5.0
TransferHeaders returns the response headers and clears the internal reference. The caller takes ownership of the returned map. Used by the public layer to avoid a redundant CloneHeader when converting engine.Response to Result.
func (*Response) TransferRequestHeaders ¶ added in v1.5.0
TransferRequestHeaders returns the request headers and clears the internal reference.