Documentation
¶
Index ¶
- type Body
- func (b *Body) Bytes() ([]byte, error)
- func (b *Body) Callback(cb func([]byte) error) error
- func (b *Body) Discard() error
- func (b *Body) Form() (f form.Form, err error)
- func (b *Body) JSON(model any) error
- func (b *Body) Len() int
- func (b *Body) Read(into []byte) (n int, err error)
- func (b *Body) Reset(request *Request)
- func (b *Body) String() (string, error)
- type Environment
- type Fetcher
- type Header
- type Headers
- type Params
- type Request
- type Response
- func Bytes(request *Request, b []byte) *Response
- func Code(request *Request, code status.Code) *Response
- func ContentType(request *Request, contentType mime.MIME, charset ...mime.Charset) *Response
- func Error(request *Request, err error, code ...status.Code) *Response
- func File(request *Request, path string) *Response
- func JSON(request *Request, model any) *Response
- func NewResponse() *Response
- func Respond(request *Request) *Response
- func Stream(request *Request, reader io.Reader, size ...int64) *Response
- func String(request *Request, str string) *Response
- func (r *Response) Buffered(flag bool) *Response
- func (r *Response) Bytes(body []byte) *Response
- func (r *Response) Clear() *Response
- func (r *Response) Code(code status.Code) *Response
- func (r *Response) Compress() *Response
- func (r *Response) Compression(token string) *Response
- func (r *Response) ContentType(value mime.MIME, charset ...mime.Charset) *Response
- func (r *Response) Cookie(cookies ...cookie.Cookie) *Response
- func (r *Response) Error(err error, code ...status.Code) *Response
- func (r *Response) Expose() *response.Fields
- func (r *Response) File(path string) *Response
- func (r *Response) Header(key string, values ...string) *Response
- func (r *Response) Headers(headers map[string][]string) *Response
- func (r *Response) JSON(model any) *Response
- func (r *Response) Status(status status.Status) *Response
- func (r *Response) Stream(reader io.Reader, size ...int64) *Response
- func (r *Response) String(body string) *Response
- func (r *Response) TryFile(path string) (*Response, error)
- func (r *Response) TryJSON(model any) (*Response, error)
- func (r *Response) Write(b []byte) (n int, err error)
- type Vars
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 (*Body) Bytes ¶ added in v0.10.5
Bytes returns the whole body at once in a byte representation.
func (*Body) Callback ¶ added in v0.7.0
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
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
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
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
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
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 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 (*Request) Cookies ¶ added in v0.16.0
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 ¶
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) PreferredEncoding ¶ added in v0.17.3
func (c *Request) PreferredEncoding() string
PreferredEncoding chooses a preferred encoding from AcceptEncoding, respecting quality markers.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func Bytes ¶ added in v0.15.0
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
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
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
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
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
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
Respond is a shorthand for request.Respond(). Can be used as a dummy handler.
func Stream ¶ added in v0.17.1
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) Buffered ¶ added in v0.17.3
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
Bytes sets the response body. Please note that the passed slice must not be modified after being passed.
func (*Response) Code ¶
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
Compress chooses and sets the best suiting compression based on client preferences.
func (*Response) Compression ¶ added in v0.17.3
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
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
Cookie adds cookies. They'll be later rendered as a set of Set-Cookie headers
func (*Response) Error ¶ added in v0.12.0
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) File ¶
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
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) JSON ¶ added in v0.12.0
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) Stream ¶ added in v0.17.1
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) TryFile ¶ added in v0.15.1
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.