http

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2025 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body added in v0.7.0

type Body struct {
	Fetcher
	// contains filtered or unexported fields
}

func NewBody added in v0.7.0

func NewBody(src Fetcher) *Body

func (*Body) Bytes added in v0.10.5

func (b *Body) Bytes() ([]byte, error)

Bytes returns the whole body at once in a byte representation.

func (*Body) Callback added in v0.7.0

func (b *Body) Callback(cb func([]byte) error) error

Callback invokes the callback every time as there's a piece of body available for reading. If the callback returns an error, it'll be passed back to the caller. The callback is not notified when there's no more data or networking error has occurred.

Please note: this method can be used only once.

func (*Body) Discard added in v0.15.0

func (b *Body) Discard() error

Discard sinkholes the rest of the body. Should not be used unless you know what you're doing.

func (*Body) Form added in v0.17.0

func (b *Body) Form() (f form.Form, err error)

Form interprets the request's body as a mime.FormUrlencoded data and returns parsed key-value pairs. If the request's MIME type is defined and is different from mime.FormUrlencoded, status.ErrUnsupportedMediaType is returned

func (*Body) JSON added in v0.17.0

func (b *Body) JSON(model any) error

JSON convoys the request's body to a json unmarshaller automatically and behaves in a similar manner.

Please note: this method cannot be used on requests with Content-Type incompatible with mime.JSON (in this case, status.ErrUnsupportedMediaType is returned).

TODO: make possible to choose and use different from json-iterator json marshall/unmarshall

func (*Body) Len added in v0.17.3

func (b *Body) Len() int

func (*Body) Read added in v0.7.0

func (b *Body) Read(into []byte) (n int, err error)

Read implements the io.Reader interface.

func (*Body) Reset added in v0.7.0

func (b *Body) Reset(request *Request)

Reset resets the body state. Should never be used as serves internal purposes only.

func (*Body) String added in v0.10.5

func (b *Body) String() (string, error)

String returns the whole body at once in a string representation.

type Environment added in v0.13.0

type Environment struct {
	// Error contains an error, if occurred
	Error error
	// AllowedMethods is used to pass a string containing all the allowed methods for a
	// specific endpoint. Has non-zero-value only when 405 Method Not Allowed raises
	AllowedMethods string
	// Encryption represents the cryptographic protocol on top of the connection. They're
	// comparable against the tls.Version... enums. Zero value means no encryption.
	Encryption uint16
	// AliasFrom contains the original request path, in case it was replaced via alias
	// aka implicit redirect
	AliasFrom string
}

type Fetcher added in v0.17.0

type Fetcher interface {
	Fetch() ([]byte, error)
}

Fetcher abstracts the underlying protocol-dependant body source. Even though the signature is identical to transport.Client.Read(), it is named differently in order to highlight the difference between relatively low-level connection "raw data" and high-level body data streams.

type Header = kv.Pair

type Headers added in v0.17.0

type Headers = *kv.Storage

type Params added in v0.5.0

type Params = *kv.Storage

type Request

type Request struct {
	// Method is an enum representing the request method.
	Method method.Method
	// Path is a decoded and validated string, guaranteed to hold ASCII-printable characters only.
	Path string
	// Params are request URI parameters.
	Params Params
	// Vars are dynamic routing segments.
	Vars Vars
	// Proto is the enum of a protocol used for the request. Can be changed (mostly through upgrade).
	Protocol proto.Protocol
	// Headers holds non-normalized header pairs, even though lookup is case-insensitive. Header keys
	// and values aren't validated, therefore may contain ASCII-nonprintable and/or Unicode characters.
	Headers Headers

	// Remote holds the remote address. Please note that this is generally not a good parameter to identify
	// a user, because there might be proxies in the middle.
	Remote net.Addr
	// Ctx is user-managed context which lives as long as the connection does and is never automatically
	// cleared.
	Ctx context.Context
	// Env contains a fixed set of contextual values which are useful in specific cases. They aren't
	// passed via the Ctx due to performance considerations.
	Env Environment
	// Body is a dedicated entity providing access to the message body.
	Body *Body
	// contains filtered or unexported fields
}

Request is the HTTP request representation.

func NewRequest

func NewRequest(
	cfg *config.Config,
	response *Response,
	client transport.Client,
	headers, params, vars *kv.Storage,
) *Request

func (*Request) Cookies added in v0.16.0

func (r *Request) Cookies() (cookie.Jar, error)

Cookies returns a cookie jar with parsed cookies key-value pairs, and an error if the syntax is malformed. The returned jar should be re-used, as this method doesn't cache the parsed result across calls and may be pretty expensive

func (*Request) Hijack

func (r *Request) Hijack() (transport.Client, error)

Hijack hijacks an underlying connection. The request body is implicitly discarded before exposing the transport. After the handler function terminates, the connection is closed automatically.

func (*Request) Hijacked added in v0.17.0

func (r *Request) Hijacked() bool

Hijacked tells whether the connection was hijacked.

func (*Request) PreferredEncoding added in v0.17.3

func (c *Request) PreferredEncoding() string

PreferredEncoding chooses a preferred encoding from AcceptEncoding, respecting quality markers.

func (*Request) Reset added in v0.17.0

func (r *Request) Reset()

Reset clears all the request fields to its zero values. This also includes resetting the context.

func (*Request) Respond added in v0.8.1

func (r *Request) Respond() *Response

Respond returns Response object.

WARNING: the Response is cleared before being returned. Considering it is stored by pointer, this action might affect your application if it was stored anywhere before.

type Response

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

func Bytes added in v0.15.0

func Bytes(request *Request, b []byte) *Response

Bytes sets the response body. Please note that the passed slice must not be modified after being passed.

func Code added in v0.12.0

func Code(request *Request, code status.Code) *Response

Code sets the response code. If the code is unrecognized, its default status string is "Nonstandard". Otherwise, it will be chosen automatically unless overridden.

func ContentType added in v0.17.3

func ContentType(request *Request, contentType mime.MIME, charset ...mime.Charset) *Response

ContentType is a shorthand for request.Respond().ContentType(...)

ContentType itself is a shorthand for Header("Content-Type", value) with an option of setting a charset, if at least one is specified. All others are ignored.

func Error added in v0.10.0

func Error(request *Request, err error, code ...status.Code) *Response

Error returns the response builder with an error set. The nil value for error is a no-op. If the error is an instance of status.HTTPError, its status code is used instead the default one. The default code is status.ErrInternalServerError, which can be overridden if at least one code is specified (all others are ignored).

func File added in v0.15.2

func File(request *Request, path string) *Response

File opens a file by the path and sets it as an upload stream if succeeded. Otherwise, the error is silently written instead.

func JSON added in v0.15.2

func JSON(request *Request, model any) *Response

JSON serializes the model into JSON and sets the Content-Type to application/json if succeeded. Otherwise, the error is silently written instead.

func NewResponse

func NewResponse() *Response

NewResponse returns a new instance of the Response object with status code set to 200 OK, pre-allocated space for response headers and text/html content-type. NOTE: it's recommended to use Request.Respond() method inside of handlers, if there's no clear reason otherwise

func Respond added in v0.8.1

func Respond(request *Request) *Response

Respond is a shorthand for request.Respond(). Can be used as a dummy handler.

func Stream added in v0.17.1

func Stream(request *Request, reader io.Reader, size ...int64) *Response

Stream sets a reader to be the source of the response's body. If no size is provided AND the reader doesn't have the Len() int method, the stream is considered unsized and therefore will be streamed using chunked transfer encoding. Otherwise, plain transfer is used, unless a compression is applied. Specifying the size of -1 forces the stream to be considered unsized.

func String added in v0.15.0

func String(request *Request, str string) *Response

String sets the response body.

func (*Response) Buffered added in v0.17.3

func (r *Response) Buffered(flag bool) *Response

Buffered allows to enable or disable writes deferring. When enabled, data from body stream is read until there is enough space available in an underlying buffer. If the data must be flushed soon possible (e.g. polling or proxying), the option should be disabled.

By default, the option is enabled.

func (*Response) Bytes added in v0.12.0

func (r *Response) Bytes(body []byte) *Response

Bytes sets the response body. Please note that the passed slice must not be modified after being passed.

func (*Response) Clear

func (r *Response) Clear() *Response

Clear discards all changes.

func (*Response) Code

func (r *Response) Code(code status.Code) *Response

Code sets the response code. If the code is unrecognized, its default status string is "Nonstandard". Otherwise, it will be chosen automatically unless overridden.

func (*Response) Compress added in v0.17.2

func (r *Response) Compress() *Response

Compress chooses and sets the best suiting compression based on client preferences.

func (*Response) Compression added in v0.17.3

func (r *Response) Compression(token string) *Response

Compression enforces a specific codec to be used, even if it isn't in Accept-Encoding. The method is no-op if the token is not recognized.

func (*Response) ContentType added in v0.5.0

func (r *Response) ContentType(value mime.MIME, charset ...mime.Charset) *Response

ContentType is a shorthand for Header("Content-Type", value) with an option of setting a charset if at least one is specified. All others are ignored.

func (*Response) Cookie added in v0.16.0

func (r *Response) Cookie(cookies ...cookie.Cookie) *Response

Cookie adds cookies. They'll be later rendered as a set of Set-Cookie headers

func (*Response) Error added in v0.12.0

func (r *Response) Error(err error, code ...status.Code) *Response

Error returns the response builder with an error set. The nil value for error is a no-op. If the error is an instance of status.HTTPError, its status code is used instead the default one. The default code is status.ErrInternalServerError, which can be overridden if at least one code is specified (all others are ignored).

func (*Response) Expose added in v0.17.2

func (r *Response) Expose() *response.Fields

Expose gives direct access to internal builder fields.

func (*Response) File

func (r *Response) File(path string) *Response

File opens a file by the path and sets it as an upload stream if succeeded. Otherwise, the error is silently written instead.

func (*Response) Header added in v0.12.0

func (r *Response) Header(key string, values ...string) *Response

Header appends a key-values pair into the list of headers to be sent in the response. Passing Content-Encoding isn't equivalent to calling Compress() and ultimately results in no encodings being automatically applied. Can be used in order to use own compressors.

func (*Response) Headers

func (r *Response) Headers(headers map[string][]string) *Response

Headers merges the map into the response headers.

func (*Response) JSON added in v0.12.0

func (r *Response) JSON(model any) *Response

JSON serializes the model into JSON and sets the Content-Type to application/json if succeeded. Otherwise, the error is silently written instead.

func (*Response) Status

func (r *Response) Status(status status.Status) *Response

Status sets a custom status text.

func (*Response) Stream added in v0.17.1

func (r *Response) Stream(reader io.Reader, size ...int64) *Response

Stream sets a reader to be the source of the response's body. If no size is provided AND the reader doesn't have the Len() int method, the stream is considered unsized and therefore will be streamed using chunked transfer encoding. Otherwise, plain transfer is used, unless a compression is applied. Specifying the size of -1 forces the stream to be considered unsized.

func (*Response) String added in v0.12.0

func (r *Response) String(body string) *Response

String sets the response body.

func (*Response) TryFile added in v0.15.1

func (r *Response) TryFile(path string) (*Response, error)

TryFile tries to open a file by the path for reading and sets it as an upload stream if succeeded. Otherwise, the error is returned.

func (*Response) TryJSON added in v0.15.1

func (r *Response) TryJSON(model any) (*Response, error)

TryJSON tries to serialize the model into JSON.

func (*Response) Write added in v0.10.0

func (r *Response) Write(b []byte) (n int, err error)

Write implements io.Reader interface. It always returns n=len(b) and err=nil

type Vars added in v0.17.0

type Vars = *kv.Storage

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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