requests

package
v0.0.0-...-88bec2d Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMethodNotFound = errors.New("HTTP method not found")

ErrMethodNotFound is returned when an unsupported HTTP method is specified.

Functions

func HandleRequests

func HandleRequests(
	ctx context.Context,
	w io.Writer,
	cfg *RequestsMetaConfig,
) (map[string][]ResponseData, error)

HandleRequests iterates through all configured requests and processes them, returning a map of response data.

func RenderTLSData

func RenderTLSData(w io.Writer, r *http.Response, filter ...[]map[int][]string)

RenderTLSData prints TLS version, cipher suite, and peer certificates for an HTTP response. An optional filter can be provided to only print specific certificate indices and fields.

func TLSVersionName

func TLSVersionName(v uint16) string

TLSVersionName returns a human-readable name for the given TLS version constant.

Types

type Host

type Host struct {
	Name    string `mapstructure:"name"`
	URIList []URI  `mapstructure:"uriList"`
}

Host represents a target hostname and a list of URIs to request on that host.

type RequestConfig

type RequestConfig struct {
	// Name is a descriptive name for this request configuration.
	Name string `mapstructure:"name"`
	// ClientTimeout is the timeout in seconds for the HTTP client.
	ClientTimeout int `mapstructure:"clientTimeout"`
	// UserAgent is the custom User-Agent string to use for the request.
	UserAgent string `mapstructure:"userAgent"`
	// TransportOverrideURL is an optional address to connect to instead of the target host.
	TransportOverrideURL string `mapstructure:"transportOverrideUrl"`
	// EnableProxyProtocolV2 enables PROXY protocol v2 headers for the connection.
	EnableProxyProtocolV2 bool `mapstructure:"enableProxyProtocolV2"`
	// Insecure skips TLS certificate verification.
	Insecure bool `mapstructure:"insecure"`
	// RequestDebug enables dumping the outgoing HTTP request.
	RequestDebug bool `mapstructure:"requestDebug"`
	// RequestHeaders is a slice of custom HTTP headers to include in the request.
	RequestHeaders []RequestHeader `mapstructure:"requestHeaders"`
	// RequestMethod is the HTTP method to use (GET, POST, etc.).
	RequestMethod string `mapstructure:"requestMethod"`
	// RequestBody is the body of the HTTP request.
	RequestBody string `mapstructure:"requestBody"`
	// ResponseDebug enables dumping the incoming HTTP response.
	ResponseDebug bool `mapstructure:"responseDebug"`
	// ResponseHeadersFilter is a list of header keys to display in the output.
	ResponseHeadersFilter []string `mapstructure:"responseHeadersFilter"`
	// ResponseBodyMatchRegexp is a regular expression to match against the response body.
	ResponseBodyMatchRegexp string `mapstructure:"responseBodyMatchRegexp"`
	// PrintResponseBody indicates if the response body should be printed to the output.
	PrintResponseBody bool `mapstructure:"printResponseBody"`
	// PrintResponseHeaders indicates if the response headers should be printed to the output.
	PrintResponseHeaders bool `mapstructure:"printResponseHeaders"`
	// PrintResponseCertificates indicates if the response TLS certificates should be printed.
	PrintResponseCertificates bool `mapstructure:"printResponseCertificates"`
	// ResponseCertificatesFilter is a list of filters mapping certificate chain indices
	// (0 for leaf, 1, 2, etc. for intermediates/roots) to specific fields that should be printed.
	// Valid fields include: "Subject", "DNSNames", "Issuer", "NotBefore", "NotAfter",
	// "Expiration", "IsCA", "AuthorityKeyId", "SubjectKeyId", "PublicKeyAlgorithm",
	// "SignatureAlgorithm", "SerialNumber", and "Fingerprint SHA-256".
	ResponseCertificatesFilter []map[int][]string `mapstructure:"responseCertificatesFilter"`
	// Hosts is a list of target hosts and their URIs.
	Hosts []Host `mapstructure:"hosts"`
}

RequestConfig defines the configuration for a single HTTP request set.

func (*RequestConfig) PrintRequestDebug

func (r *RequestConfig) PrintRequestDebug(w io.Writer, req *http.Request) error

PrintRequestDebug dumps the HTTP request to the provided writer if request debug is enabled.

func (*RequestConfig) PrintResponseDebug

func (r *RequestConfig) PrintResponseDebug(w io.Writer, resp *http.Response)

PrintResponseDebug dumps the HTTP response and TLS information to the provided writer if response debug is enabled.

func (*RequestConfig) PrintTitle

func (r *RequestConfig) PrintTitle(w io.Writer, isVerbose bool)

PrintTitle prints the request name and transport override information if verbose mode is enabled.

type RequestHTTPClient

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

RequestHTTPClient wraps an http.Client with additional configuration for requests.

func NewHTTPClientFromRequestConfig

func NewHTTPClientFromRequestConfig(
	r RequestConfig,
	serverName string,
	caPool *x509.CertPool,
) (*RequestHTTPClient, error)

NewHTTPClientFromRequestConfig initializes a RequestHTTPClient using the provided RequestConfig.

func NewRequestHTTPClient

func NewRequestHTTPClient() *RequestHTTPClient

NewRequestHTTPClient creates a new RequestHTTPClient with default transport settings.

func (*RequestHTTPClient) SetCACertsPool

func (rc *RequestHTTPClient) SetCACertsPool(caPool *x509.CertPool) (*RequestHTTPClient, error)

SetCACertsPool sets the CA certificate pool for the HTTP transport.

func (*RequestHTTPClient) SetClientTimeout

func (rc *RequestHTTPClient) SetClientTimeout(timeout int) (*RequestHTTPClient, error)

SetClientTimeout sets the timeout for the HTTP client in seconds.

func (*RequestHTTPClient) SetInsecureSkipVerify

func (rc *RequestHTTPClient) SetInsecureSkipVerify(isInsecure bool) (*RequestHTTPClient, error)

SetInsecureSkipVerify sets whether to skip TLS certificate verification.

func (*RequestHTTPClient) SetMethod

func (rc *RequestHTTPClient) SetMethod(method string) (*RequestHTTPClient, error)

SetMethod sets the HTTP method for the client.

func (*RequestHTTPClient) SetProxyProtocolHeader

func (rc *RequestHTTPClient) SetProxyProtocolHeader(header proxyproto.Header) (*RequestHTTPClient, error)

SetProxyProtocolHeader sets a custom PROXY protocol header for the client's dialer.

func (*RequestHTTPClient) SetProxyProtocolV2

func (rc *RequestHTTPClient) SetProxyProtocolV2(enable bool) *RequestHTTPClient

SetProxyProtocolV2 enables or disables PROXY protocol v2 support.

func (*RequestHTTPClient) SetServerName

func (rc *RequestHTTPClient) SetServerName(serverName string) (*RequestHTTPClient, error)

SetServerName sets the ServerName for SNI in the TLS configuration.

func (*RequestHTTPClient) SetTransportOverride

func (rc *RequestHTTPClient) SetTransportOverride(transportURL string) (*RequestHTTPClient, error)

SetTransportOverride sets a dialer override to connect to a specific transport address.

type RequestHeader

type RequestHeader struct {
	Key   string `mapstructure:"key"`
	Value string `mapstructure:"value"`
}

RequestHeader represents a single HTTP header key-value pair.

type RequestsMetaConfig

type RequestsMetaConfig struct {
	// RequestDebug enables global request debugging.
	RequestDebug bool
	// RequestVerbose enables global verbose output.
	RequestVerbose bool
	// CACertsPool is the certificate pool used for validating server certificates.
	CACertsPool *x509.CertPool
	// Requests is the list of request configurations to execute.
	Requests []RequestConfig `mapstructure:"requests"`
}

RequestsMetaConfig holds the global configuration and the list of requests to execute.

func NewRequestsMetaConfig

func NewRequestsMetaConfig() (*RequestsMetaConfig, error)

NewRequestsMetaConfig creates a new RequestsMetaConfig with the system's certificate pool.

func (*RequestsMetaConfig) PrintCmd

func (r *RequestsMetaConfig) PrintCmd(w io.Writer)

PrintCmd prints a header for the requests execution if verbose mode is enabled.

func (*RequestsMetaConfig) SetCaPoolFromFile

func (r *RequestsMetaConfig) SetCaPoolFromFile(filePath string, fileReader certinfo.Reader) error

SetCaPoolFromFile loads a CA certificate pool from a PEM file.

func (*RequestsMetaConfig) SetCaPoolFromYAML

func (r *RequestsMetaConfig) SetCaPoolFromYAML(s string) error

SetCaPoolFromYAML loads a CA certificate pool from a PEM-encoded string.

func (*RequestsMetaConfig) SetDebug

func (r *RequestsMetaConfig) SetDebug(b bool) *RequestsMetaConfig

SetDebug sets the debug level for the requests.

func (*RequestsMetaConfig) SetRequests

func (r *RequestsMetaConfig) SetRequests(requests []RequestConfig) *RequestsMetaConfig

SetRequests sets the list of request configurations.

func (*RequestsMetaConfig) SetVerbose

func (r *RequestsMetaConfig) SetVerbose(b bool) *RequestsMetaConfig

SetVerbose sets the verbosity level for the requests.

type ResponseData

type ResponseData struct {
	// Request is the original configuration for the executed request.
	Request RequestConfig
	// TransportAddress is the network address the request was sent to.
	TransportAddress string
	// URL is the full URL requested.
	URL string
	// ResponseBody is the content of the HTTP response.
	ResponseBody string
	// ResponseBodyRegexpMatched indicates if the response body matched the configured regexp.
	ResponseBodyRegexpMatched bool
	// Response is the raw HTTP response object.
	Response *http.Response
	// Error is any error encountered during the request.
	Error error
}

ResponseData holds the results and metadata of an executed HTTP request.

func (*ResponseData) ImportResponseBody

func (rd *ResponseData) ImportResponseBody()

ImportResponseBody reads the response body, handles regex matching, and applies syntax highlighting if applicable.

func (ResponseData) PrintResponseData

func (rd ResponseData) PrintResponseData(w io.Writer, isVerbose bool)

PrintResponseData prints the collected response data (status, headers, body) if verbose mode is enabled.

type ResponseHeader

type ResponseHeader string

ResponseHeader represents a formatted string of HTTP response headers.

func (ResponseHeader) String

func (h ResponseHeader) String() string

String returns the response header as a string.

type URI

type URI string

URI represents a partial URL path (e.g. /index.html).

func (URI) Parse

func (u URI) Parse() bool

Parse validates that the URI starts with a slash.

Jump to

Keyboard shortcuts

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