request

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidHost is returned when building/parsing a request
	// from plain text and the Host line is invalid.
	ErrInvalidHost = errors.New("invalid host line")
	// ErrInvalidPayload is returned when building/parsing a request
	// from plain text and the payload is invalid.
	ErrInvalidPayload = errors.New("invalid payload")
)

Functions

This section is empty.

Types

type Option

type Option func(Request) Request

Option defines a functional option type for Request, that facilitates the construction of a Default template, but with some of the default values modified, like the HTTP method.

It can be used in combination with WithOptions.

For instance: request.WithOptions("example.org", []request.Option{request.WithMethod("POST")}).

func WithBody

func WithBody(body []byte) Option

WithBody sets a body (not defined by Default).

func WithData

func WithData(data []byte) Option

WithData sets a body data as a form (i.e. as `application/x-www-form-urlencoded`).

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds a new header to the default ones (see Default).

func WithHeaders

func WithHeaders(headers map[string][]string) Option

WithHeaders modifies the default headers (see Default).

func WithMethod

func WithMethod(method string) Option

WithMethod modifies the default method (i.e. GET).

func WithPath

func WithPath(path string) Option

WithPath modifies the default path (i.e. /).

func WithProto

func WithProto(proto string) Option

WithProto modifies the default proto (i.e. HTTP/1.1).

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout modifies the default timeout (i.e. 20s).

type Request

type Request struct {
	UID               string // Used to detect interactions
	URL               string
	Method            string
	Path              string
	Proto             string
	Headers           map[string][]string
	Body              []byte
	Timeout           time.Duration
	RedirectType      profile.Redirect
	MaxRedirects      int
	FollowedRedirects int
	Modifications     map[string]string
}

Request is a representation of an HTTP request, complementary to the standard http.Request and used here and there for scans.

func Default

func Default(remote string) Request

Default is a named constructor to instantiate a new Request with the given remote as the Request's [Request.URL].

By default, it sets the `GET` method, some basic headers, and a timeout of 20s.

func ParseRequest

func ParseRequest(b []byte, hh ...string) (Request, error)

ParseRequest parses a request from a byte slice. If a host is given (variadic arg), it is used as the request URL.

func RequestFromJSON

func RequestFromJSON(data []byte) (Request, error)

RequestFromJSON creates a request from a JSON byte slice.

func WithOptions

func WithOptions(host string, options ...Option) Request

WithOptions can be used to construct a Default request, but with some of the default values modified, like the HTTP method.

It can be used in combination with Option.

For instance: request.WithOptions("example.org", []request.Option{request.WithMethod("POST")}).

func (*Request) Bytes

func (r *Request) Bytes() []byte

Bytes returns the request as a byte slice.

func (*Request) Clone

func (r *Request) Clone() Request

Clone returns a deep copy (e.g. headers' map, and body's byte slice are also copied) of the request.

func (*Request) ContentType

func (r *Request) ContentType() string

ContentType returns the value of the Content-Type header.

func (*Request) Cookies

func (r *Request) Cookies() []*http.Cookie

Cookies returns the cookies (i.e. *http.Cookie) from the request headers.

func (*Request) EscapedBytes

func (r *Request) EscapedBytes() []byte

EscapedBytes returns the request as a byte slice, with the body escaped (i.e. JSON encoded).

func (*Request) HasJSONBody

func (r *Request) HasJSONBody() bool

HasJSONBody returns whether the request body is a valid JSON.

func (*Request) HasMultipartBody

func (r *Request) HasMultipartBody() bool

HasMultipartBody returns whether the request body is a valid multipart form.

func (*Request) HasXMLBody

func (r *Request) HasXMLBody() bool

HasXMLBody returns whether the request body is a valid XML.

func (*Request) Header

func (r *Request) Header(header string) string

Header returns the value of the given header. If the header is not present, an empty string is returned. If the header has multiple values, the values are joined with a space.

func (*Request) HeaderBytes

func (r *Request) HeaderBytes() []byte

HeaderBytes returns the headers section as a byte slice.

func (*Request) IsEmpty

func (r *Request) IsEmpty() bool

IsEmpty returns whether the request is empty.

func (*Request) MultipartForm

func (r *Request) MultipartForm() (*multipart.Form, error)

MultipartForm returns the request body as a multipart form.

func (*Request) SetBody

func (r *Request) SetBody(body []byte)

SetBody sets the request body and updates the Content-Length header accordingly.

func (*Request) ToJSON

func (r *Request) ToJSON() ([]byte, error)

ToJSON returns the request as a JSON byte slice, with headers on its canonical form (i.e. textproto.CanonicalMIMEHeaderKey).

Jump to

Keyboard shortcuts

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