httpext

package
v0.55.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: AGPL-3.0 Imports: 35 Imported by: 21

Documentation

Overview

Package httpext provides extensions to the standard net/http package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeBatchRequests added in v0.26.0

func MakeBatchRequests(
	ctx context.Context, state *lib.State,
	requests []BatchParsedHTTPRequest,
	reqCount, globalLimit, perHostLimit int,
) <-chan error

MakeBatchRequests concurrently makes multiple requests. It spawns min(reqCount, globalLimit) goroutines that asynchronously process all requests coming from the requests channel. Responses are recorded in the pointers contained in each BatchParsedHTTPRequest object, so they need to be pre-initialized. In addition, each processed request would emit either a nil value, or an error, via the returned errors channel. The goroutines exit when the requests channel is closed.

func SetRequestCookies

func SetRequestCookies(req *http.Request, jar *cookiejar.Jar, reqCookies map[string]*HTTPRequestCookie)

SetRequestCookies sets the cookies of the requests getting those cookies both from the jar and from the reqCookies map. The Replace field of the HTTPRequestCookie will be taken into account

Types

type BatchParsedHTTPRequest added in v0.26.0

type BatchParsedHTTPRequest struct {
	*ParsedHTTPRequest
	Response *Response // this is modified by MakeBatchRequests()
}

BatchParsedHTTPRequest extends the normal parsed HTTP request with a pointer to a Response object, so that the batch goroutines can concurrently store the responses they receive, without any locking.

type CompressionType added in v0.25.0

type CompressionType uint

CompressionType is used to specify what compression is to be used to compress the body of a request The conversion and validation methods are auto-generated with https://github.com/alvaroloes/enumer:

const (
	// CompressionTypeGzip compresses through gzip
	CompressionTypeGzip CompressionType = iota
	// CompressionTypeDeflate compresses through flate
	CompressionTypeDeflate
	// CompressionTypeZstd compresses through zstd
	CompressionTypeZstd
	// CompressionTypeBr compresses through brotli
	CompressionTypeBr
)

func CompressionTypeString added in v0.25.0

func CompressionTypeString(s string) (CompressionType, error)

CompressionTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func CompressionTypeValues added in v0.25.0

func CompressionTypeValues() []CompressionType

CompressionTypeValues returns all values of the enum

func (CompressionType) IsACompressionType added in v0.25.0

func (i CompressionType) IsACompressionType() bool

IsACompressionType returns "true" if the value is listed in the enum definition. "false" otherwise

func (CompressionType) String added in v0.25.0

func (i CompressionType) String() string

type HTTPCookie

type HTTPCookie struct {
	Name, Value, Domain, Path string
	HTTPOnly, Secure          bool
	MaxAge                    int
	Expires                   int64
}

HTTPCookie is a representation of an http cookies used in the Response object

type HTTPRequestCookie

type HTTPRequestCookie struct {
	Name, Value string
	Replace     bool
}

HTTPRequestCookie is a representation of a cookie used for request objects

type K6Error added in v0.25.1

type K6Error struct {
	Code          errCode
	Message       string
	OriginalError error
}

K6Error is a helper struct that enhances Go errors with custom k6-specific error-codes and more user-readable error messages.

func NewK6Error added in v0.25.1

func NewK6Error(code errCode, msg string, originalErr error) K6Error

NewK6Error is the constructor for K6Error

func (K6Error) Error added in v0.25.1

func (k6Err K6Error) Error() string

Error implements the `error` interface, so K6Errors are normal Go errors.

func (K6Error) Unwrap added in v0.25.1

func (k6Err K6Error) Unwrap() error

Unwrap implements the `xerrors.Wrapper` interface, so K6Errors are a bit future-proof Go 2 errors.

type ParsedHTTPRequest

type ParsedHTTPRequest struct {
	URL              *URL
	Body             *bytes.Buffer
	Req              *http.Request
	Timeout          time.Duration
	Auth             string
	Throw            bool
	ResponseType     ResponseType
	ResponseCallback func(int) bool
	Compressions     []CompressionType
	Redirects        null.Int
	ActiveJar        *cookiejar.Jar
	Cookies          map[string]*HTTPRequestCookie
	TagsAndMeta      metrics.TagsAndMeta
}

ParsedHTTPRequest a represantion of a request after it has been parsed from a user script

type Request

type Request struct {
	Method  string                          `json:"method"`
	URL     string                          `json:"url"`
	Headers map[string][]string             `json:"headers"`
	Body    string                          `json:"body"`
	Cookies map[string][]*HTTPRequestCookie `json:"cookies"`
}

Request represent an http request

type Response

type Response struct {
	RemoteIP       string                   `json:"remote_ip"`
	RemotePort     int                      `json:"remote_port"`
	URL            string                   `json:"url"`
	Status         int                      `json:"status"`
	StatusText     string                   `json:"status_text"`
	Proto          string                   `json:"proto"`
	Headers        map[string]string        `json:"headers"`
	Cookies        map[string][]*HTTPCookie `json:"cookies"`
	Body           interface{}              `json:"body"`
	Timings        ResponseTimings          `json:"timings"`
	TLSVersion     string                   `json:"tls_version"`
	TLSCipherSuite string                   `json:"tls_cipher_suite"`
	OCSP           netext.OCSP              `json:"ocsp"`
	Error          string                   `json:"error"`
	ErrorCode      int                      `json:"error_code"`
	Request        *Request                 `json:"request"`
}

Response is a representation of an HTTP response

func MakeRequest

func MakeRequest(ctx context.Context, state *lib.State, preq *ParsedHTTPRequest) (*Response, error)

MakeRequest makes http request for tor the provided ParsedHTTPRequest.

TODO: split apart...

func NewResponse added in v0.33.0

func NewResponse() *Response

NewResponse returns an empty Response instance.

type ResponseTimings

type ResponseTimings struct {
	Duration       float64 `json:"duration"`
	Blocked        float64 `json:"blocked"`
	LookingUp      float64 `json:"looking_up"`
	Connecting     float64 `json:"connecting"`
	TLSHandshaking float64 `json:"tls_handshaking"`
	Sending        float64 `json:"sending"`
	Waiting        float64 `json:"waiting"`
	Receiving      float64 `json:"receiving"`
}

ResponseTimings is a struct to put all timings for a given HTTP response/request

type ResponseType

type ResponseType uint

ResponseType is used in the request to specify how the response body should be treated The conversion and validation methods are auto-generated with https://github.com/alvaroloes/enumer:

const (
	// ResponseTypeText causes k6 to return the response body as a string. It works
	// well for web pages and JSON documents, but it can cause issues with
	// binary files since their data could be lost when they're converted in the
	// UTF-16 strings JavaScript uses.
	// This is the default value for backwards-compatibility, unless the global
	// discardResponseBodies option is enabled.
	ResponseTypeText ResponseType = iota
	// ResponseTypeBinary causes k6 to return the response body as a []byte, suitable
	// for working with binary files without lost data and needless string conversions.
	ResponseTypeBinary
	// ResponseTypeNone causes k6 to fully read the response body while immediately
	// discarding the actual data - k6 would set the body of the returned HTTPResponse
	// to null. This saves CPU and memory and is suitable for HTTP requests that we just
	// want to  measure, but we don't care about their responses' contents. This is the
	// default value for all requests if the global discardResponseBodies is enablled.
	ResponseTypeNone
)

func ResponseTypeString

func ResponseTypeString(s string) (ResponseType, error)

ResponseTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func ResponseTypeValues

func ResponseTypeValues() []ResponseType

ResponseTypeValues returns all values of the enum

func (ResponseType) IsAResponseType

func (i ResponseType) IsAResponseType() bool

IsAResponseType returns "true" if the value is listed in the enum definition. "false" otherwise

func (ResponseType) MarshalJSON

func (i ResponseType) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for ResponseType

func (ResponseType) MarshalText

func (i ResponseType) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for ResponseType

func (ResponseType) String

func (i ResponseType) String() string

func (*ResponseType) UnmarshalJSON

func (i *ResponseType) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for ResponseType

func (*ResponseType) UnmarshalText

func (i *ResponseType) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for ResponseType

type Tracer

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

A Tracer wraps "net/http/httptrace" to collect granular timings for HTTP requests. Note that since there is not yet an event for the end of a request (there's a PR to add it), you must call Done() at the end of the request to get the full timings. It's NOT safe to reuse Tracers between requests. Cheers, love, the cavalry's here.

func (*Tracer) ConnectDone

func (t *Tracer) ConnectDone(_, _ string, err error)

ConnectDone is called when a new connection's Dial completes. The provided err indicates whether the connection completed successfully. If net.Dialer.DualStack ("Happy Eyeballs") support is enabled (default), this may be called multiple times.

If the connection is reused, this won't be called. Otherwise, it will be called after ConnectStart() and before either TLSHandshakeStart() (for TLS connections) or GotConn().

func (*Tracer) ConnectStart

func (t *Tracer) ConnectStart(_, _ string)

ConnectStart is called when a new connection's Dial begins. If net.Dialer.DualStack (IPv6 "Happy Eyeballs") support is enabled (default), this may be called multiple times.

If the connection is reused, this won't be called. Otherwise, it will be called after GetConn() and before ConnectDone().

func (*Tracer) Done

func (t *Tracer) Done() *Trail

Done calculates all metrics and should be called when the request is finished.

func (*Tracer) GetConn

func (t *Tracer) GetConn(_ string)

GetConn is called before a connection is created or retrieved from an idle pool. The hostPort is the "host:port" of the target or proxy. GetConn is called even if there's already an idle cached connection available.

Keep in mind that GetConn won't be called if a connection is reused though, for example when there's a redirect. If it's called, it will be called before all other hooks.

func (*Tracer) GotConn

func (t *Tracer) GotConn(info httptrace.GotConnInfo)

GotConn is called after a successful connection is obtained. There is no hook for failure to obtain a connection; instead, use the error from Transport.RoundTrip.

This is the fist hook called for reused connections. For new connections, it's called either after TLSHandshakeDone() (for TLS connections) or after ConnectDone()

func (*Tracer) GotFirstResponseByte

func (t *Tracer) GotFirstResponseByte()

GotFirstResponseByte is called when the first byte of the response headers is available. If the request was cancelled, this could be called after the RoundTrip() method has returned.

func (*Tracer) TLSHandshakeDone

func (t *Tracer) TLSHandshakeDone(_ tls.ConnectionState, err error)

TLSHandshakeDone is called after the TLS handshake with either the successful handshake's connection state, or a non-nil error on handshake failure.

If the connection is reused, this won't be called. Otherwise, it will be called after TLSHandshakeStart() and before GotConn(). If the request was cancelled, this could be called after the RoundTrip() method has returned.

func (*Tracer) TLSHandshakeStart

func (t *Tracer) TLSHandshakeStart()

TLSHandshakeStart is called when the TLS handshake is started. When connecting to a HTTPS site via a HTTP proxy, the handshake happens after the CONNECT request is processed by the proxy.

If the connection is reused, this won't be called. Otherwise, it will be called after ConnectDone() and before TLSHandshakeDone().

func (*Tracer) Trace

func (t *Tracer) Trace() *httptrace.ClientTrace

Trace returns a premade ClientTrace that calls all of the Tracer's hooks.

func (*Tracer) WroteRequest

func (t *Tracer) WroteRequest(info httptrace.WroteRequestInfo)

WroteRequest is called with the result of writing the request and any body. It may be called multiple times in the case of retried requests.

type Trail

type Trail struct {
	EndTime time.Time

	// Total connect time (Connecting + TLSHandshaking)
	ConnDuration time.Duration

	// Total request duration, excluding DNS lookup and connect time.
	Duration time.Duration

	Blocked        time.Duration // Waiting to acquire a connection.
	Connecting     time.Duration // Connecting to remote host.
	TLSHandshaking time.Duration // Executing TLS handshake.
	Sending        time.Duration // Writing request.
	Waiting        time.Duration // Waiting for first byte.
	Receiving      time.Duration // Receiving response.

	// Detailed connection information.
	ConnReused     bool
	ConnRemoteAddr net.Addr

	Failed null.Bool
	// Populated by SaveSamples()
	Tags     *metrics.TagSet
	Metadata map[string]string
	Samples  []metrics.Sample
}

A Trail represents detailed information about an HTTP request. You'd typically get one from a Tracer.

func (*Trail) GetSamples

func (tr *Trail) GetSamples() []metrics.Sample

GetSamples implements the metrics.SampleContainer interface.

func (*Trail) GetTags

func (tr *Trail) GetTags() *metrics.TagSet

GetTags implements the metrics.ConnectedSampleContainer interface.

func (*Trail) GetTime

func (tr *Trail) GetTime() time.Time

GetTime implements the metrics.ConnectedSampleContainer interface.

func (*Trail) SaveSamples

func (tr *Trail) SaveSamples(builtinMetrics *metrics.BuiltinMetrics, ctm *metrics.TagsAndMeta)

SaveSamples populates the Trail's sample slice so they're accessible via GetSamples()

type URL

type URL struct {
	Name     string // http://example.com/thing/${}/
	URL      string // http://example.com/thing/1234/
	CleanURL string // URL with masked user credentials, used for output
	// contains filtered or unexported fields
}

A URL wraps net.URL, and preserves the template (if any) the URL was constructed from.

func NewURL

func NewURL(urlString, name string) (URL, error)

NewURL returns a new URL for the provided url and name. The error is returned if the url provided can't be parsed

func ToURL added in v0.33.0

func ToURL(u interface{}) (URL, error)

ToURL tries to convert anything passed to it to a k6 URL struct

func (URL) Clean added in v0.26.0

func (u URL) Clean() string

Clean returns an output-safe representation of URL

func (URL) GetURL

func (u URL) GetURL() *url.URL

GetURL returns the internal url.URL

Jump to

Keyboard shortcuts

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