Documentation ¶
Index ¶
- type Body
- type Encoding
- type Environment
- type OnBodyCallback
- type Params
- type Request
- type Response
- func Bytes(request *Request, b []byte) *Response
- func Code(request *Request, code status.Code) *Response
- func Error(request *Request, err error) *Response
- func File(request *Request, path string) *Response
- func JSON(request *Request, model any) *Response
- func NewResponse() *Response
- func Respond(request *Request) *Response
- func String(request *Request, str string) *Response
- func (r *Response) Attachment(reader io.Reader, size int) *Response
- func (r *Response) Bytes(body []byte) *Response
- func (r *Response) Clear() *Response
- func (r *Response) Code(code status.Code) *Response
- func (r *Response) ContentType(value string) *Response
- func (r *Response) Error(err error) *Response
- 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) Reveal() response.Fields
- func (r *Response) Status(status status.Status) *Response
- func (r *Response) String(body string) *Response
- func (r *Response) TransferEncoding(value 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 ResponseWriter
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 interface { // Init is used to initialize the Body instance. MUST NOT be used, as otherwise // deadlock may be encountered Init(*Request) // Retrieve reads request's body from a socket. If there's no data yet, the call will // be blocked. It's safe to call the method after whole body was read, as only io.EOF // will be returned Retrieve() ([]byte, error) // Read implements io.Reader interface Read([]byte) (n int, err error) // String returns the whole request's body at once as a string String() (string, error) // Bytes returns the whole request's body at once as a byte slice Bytes() ([]byte, error) // Callback takes a function that'll be called each time as at least 1 byte of request's // body is received. The call will be blocked until the whole body won't be processed. // When the body is completely processed, the method will silently exit without notifying // the passed function anyhow Callback(cb OnBodyCallback) error // Discard discards the rest of the body Discard() error }
type Encoding ¶ added in v0.15.0
type Encoding struct { // Transfer represents Transfer-Encoding header value, split by comma Transfer []string // Content represents Content-Encoding header value, split by comma Content []string // Chunked doesn't belong to any of encodings, as it is still must be processed individually Chunked, HasTrailer bool }
type Environment ¶ added in v0.13.0
type Environment struct { // Error is used mostly for error handlers Error error // AllowMethods 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 AllowMethods string Encryption encryption.Encryption // reserved for router AliasFrom string }
type OnBodyCallback ¶ added in v0.10.5
type Request ¶
type Request struct { // Method represents the request's method Method method.Method // Path represents decoded request URI Path string // Query are request's URI parameters Query *query.Query // Params are dynamic segment values Params Params // Proto is the protocol, which was used to make the request Proto proto.Proto // Headers are request headers. They are stored non-normalized, however lookup is // case-insensitive Headers headers.Headers // Encoding holds an information about encoding, that was used to make the request Encoding Encoding // ContentLength obtains the value from Content-Length header. It holds the value of 0 // if isn't presented. // // NOTE: if any of transfer-encodings were applied, you MUST NOT look at this value ContentLength int // ContentType obtains Content-Type header value ContentType string // Upgrade is the protocol token, which is set by default to proto.Unknown. In // case it is anything else, then Upgrade header was received Upgrade proto.Proto // Remote represents remote net.Addr. // WARNING: in order to use the value to represent a user, MAKE SURE there are no proxies // in the middle Remote net.Addr // Ctx is a request context. It may be filled with arbitrary data across middlewares // and handler by itself Ctx context.Context // Env is a set of fixed variables passed by core. They are passed separately from Request.Ctx // in order to not only distinguish user-defined values in ctx from those from core, but also // to gain performance, as accessing the struct is much faster than looking up in context.Context Env Environment // Body accesses the request's body Body Body // contains filtered or unexported fields }
Request represents HTTP request
func NewRequest ¶
func NewRequest( hdrs headers.Headers, query *query.Query, response *Response, conn net.Conn, body Body, params Params, ) *Request
NewRequest returns a new instance of request object and body gateway Must not be used externally, this function is for internal purposes only HTTP/1.1 as a protocol by default is set because if first request from user is invalid, we need to render a response using request method, but appears that default method is a null-value (proto.Unknown)
func (*Request) Clear ¶
Clear resets request headers and reads body into nowhere until completed. It is implemented to clear the request object between requests
func (*Request) Hijack ¶
Hijack the connection. Request body will be implicitly read (so if you need it you should read it before) all the body left. After handler exits, the connection will be closed, so the connection can be hijacked only once
func (*Request) JSON ¶ added in v0.7.4
JSON takes a model and returns an error if occurred. Model must be a pointer to a structure. If Content-Type header is given, but is not "application/json", then status.ErrUnsupportedMediaType will be returned. If JSON is malformed, or it doesn't match the model, then custom jsoniter error will be returned
func (*Request) Respond ¶ added in v0.8.1
Respond returns Response object.
WARNING: this method clears the response builder under the hood. As it is passed by reference, it'll be cleared EVERYWHERE along a handler
func (*Request) WasHijacked ¶
WasHijacked returns true or false, depending on whether was a connection hijacked
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
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 returns a response object of request.
NOTE: the behaviour differs from request.Respond() directly, as it doesn't clear the response builder each time
func (*Response) Attachment ¶ added in v0.5.0
Attachment sets a Response's attachment. In this case Response body will be ignored. If size <= 0, then Transfer-Encoding: chunked will be used
func (*Response) Bytes ¶ added in v0.12.0
Bytes sets the response's body to passed slice WITHOUT COPYING. Changing the passed slice later will affect the response by itself
func (*Response) Code ¶
Code sets a Response code and a corresponding status. In case of unknown code, "Unknown Status Code" will be set as a status code. In this case you should call Status explicitly
func (*Response) ContentType ¶ added in v0.5.0
ContentType sets a custom Content-Type header value.
func (*Response) Error ¶ added in v0.12.0
Error returns Response with corresponding HTTP error code, if passed error is status.HTTPError.fields. Otherwise, code is considered to be 500 Internal Server Error.fields. Note: revealing error text may be dangerous
func (*Response) File ¶
File opens a file for reading and returns a new Response with attachment, set to the file descriptor.fields. If error occurred, it'll be silently returned
func (*Response) Header ¶ added in v0.12.0
Header sets header values to a key. In case it already exists the value will be appended.
func (*Response) Headers ¶
Headers simply merges passed headers into Response. Also, it is the only way to specify a quality marker of value. In case headers were not initialized before, Response headers will be set to a passed map, so editing this map will affect Response
func (*Response) JSON ¶ added in v0.12.0
JSON does the same as TryJSON does, except returned error is being implicitly wrapped by Error
func (*Response) Reveal ¶ added in v0.12.0
Reveal returns a struct with values, filled by builder. Used mostly in internal purposes
func (*Response) Status ¶
Status sets a custom status text. This text does not matter at all, and usually totally ignored by client, so there is actually no reasons to use this except some rare cases when you need to represent a Response status text somewhere
func (*Response) TransferEncoding ¶ added in v0.5.0
TransferEncoding sets a custom Transfer-Encoding header value.
func (*Response) TryFile ¶ added in v0.15.1
TryFile tries to open a file for reading and returns a new Response with attachment.