httpclient

package
v1.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package httpclient provides a shared, rate-limited, concurrent HTTP client used across all SwaggerVu modules.

Index

Constants

This section is empty.

Variables

View Source
var ErrRequestBudget = errors.New("request budget exhausted")

Functions

func ForEach

func ForEach[T any](ctx context.Context, concurrency int, items []T, fn func(ctx context.Context, item T))

ForEach runs fn over items using up to `concurrency` workers. It stops early if ctx is cancelled.

func IsSensitiveHeader

func IsSensitiveHeader(name string) bool

IsSensitiveHeader reports whether a header must be bound to an exact origin.

func NormalizeAllowedHosts

func NormalizeAllowedHosts(hosts []string) ([]string, error)

NormalizeAllowedHosts validates exact hostnames/IP literals used as authority boundaries. URLs, ports, wildcards, userinfo, and path/query syntax are refused.

func NormalizeCredentialOrigins

func NormalizeCredentialOrigins(origins []string) ([]string, error)

NormalizeCredentialOrigins validates and canonicalizes scheme+host+port boundaries. Paths, queries, fragments, and userinfo are refused.

Types

type Client

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

Client is a thin wrapper around http.Client adding a global rate limiter.

func New

func New(opts Options) (*Client, error)

New builds a Client from Options.

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, url string, body io.Reader) (*Response, error)

Do performs a request honoring the rate limiter and default headers.

func (*Client) DoWithHeaders

func (c *Client) DoWithHeaders(ctx context.Context, method, url string, body io.Reader, headers map[string]string) (*Response, error)

DoWithHeaders performs a request, applying per-request headers on top of the client defaults (used to send the generated Content-Type and header params).

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*Response, error)

Get is a convenience wrapper for GET.

func (*Client) ValidateURL

func (c *Client) ValidateURL(ctx context.Context, raw string) error

ValidateURL applies scheme, scope, DNS, and private/special-address policy without sending a request. It is used before handing an initial URL to a component such as a browser that does not use Client's transport.

type Options

type Options struct {
	Timeout           time.Duration
	Insecure          bool
	Proxy             string // http(s):// proxy URL
	UserAgent         string
	RandomizeUA       bool
	RatePerSecond     float64 // 0 = unlimited
	Headers           map[string]string
	FollowRedirect    bool
	AllowedHosts      []string
	AllowPrivate      bool
	MaxRequests       int64 // 0 = unlimited
	MaxBodyBytes      int64
	CredentialOrigins []string
}

Options configures a Client.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults.

type Response

type Response struct {
	URL         string
	Status      int
	Body        []byte
	Truncated   bool
	ContentType string
	Header      http.Header
}

Response is a lightweight captured response.

func (*Response) BodyString

func (r *Response) BodyString() string

BodyString returns the response body as a string.

func (*Response) IsJSON

func (r *Response) IsJSON() bool

IsJSON reports whether the content type looks like JSON.

Jump to

Keyboard shortcuts

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