http

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 16 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 interface {
	Init(*Request)
	Retrieve() ([]byte, error)
	Read([]byte) (n int, err error)
	String() (string, error)
	Bytes() ([]byte, error)
	Callback(cb OnBodyCallback) error
	Reset() error
}

type OnBodyCallback added in v0.10.5

type OnBodyCallback func([]byte) error

type Params added in v0.5.0

type Params = map[string]string

type Request

type Request struct {
	Method method.Method
	Path   string
	// Query is a key-value part of the Path
	Query query.Query
	// Params are dynamic segments, in case dynamic routing is enabled
	Params        Params
	Proto         proto.Proto
	Headers       *headers.Headers
	Encoding      headers.Encoding
	ContentLength int
	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
	Remote net.Addr
	// Ctx is a request context. It may be filled with arbitrary data across middlewares
	// and handler by itself.
	Ctx context.Context
	// IsTLS describes, whether request was made via https
	IsTLS bool

	Body Body
	// contains filtered or unexported fields
}

Request represents HTTP request

func NewRequest

func NewRequest(
	ctx context.Context, hdrs *headers.Headers, query query.Query, response *Response,
	conn net.Conn, body Body, paramsMap Params, disableParamsMapClearing bool,
) *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

func (r *Request) Clear() (err error)

Clear resets request headers and reads body into nowhere until completed. It is implemented to clear the request object between requests

func (*Request) Hijack

func (r *Request) Hijack() (net.Conn, error)

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

func (r *Request) JSON(model any) error

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

func (r *Request) Respond() *Response

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

func (r *Request) WasHijacked() bool

WasHijacked returns true or false, depending on whether was a connection hijacked

type Response

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

func Code added in v0.12.0

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

func Error added in v0.10.0

func Error(request *Request, err error) *Response

func NewResponse

func NewResponse() *Response

func Respond added in v0.8.1

func Respond(request *Request) *Response

Respond returns a response object of request

func (*Response) Attachment added in v0.5.0

func (r *Response) Attachment(reader io.Reader, size int) *Response

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

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

Bytes sets the response's body to passed slice WITHOUT COPYING. Changing the passed slice later will affect the response by itself

func (*Response) Clear

func (r *Response) Clear() *Response

Clear discards everything was done with Response object before

func (*Response) Code

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

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

func (r *Response) ContentType(value string) *Response

ContentType sets a custom Content-Type header value.

func (*Response) Error added in v0.12.0

func (r *Response) Error(err error) *Response

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

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

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

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

Header sets header values to a key. In case it already exists the value will be appended.

func (*Response) Headers

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

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

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

JSON receives a model (must be a pointer to the structure) and returns a new Response object and an error

func (*Response) RetrieveFile added in v0.12.0

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

RetrieveFile opens a file for reading and returns a new Response with attachment, set to the file descriptor.fields. If error occurred during opening a file, it'll be returned

func (*Response) Reveal added in v0.12.0

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

Reveal returns a struct with values, filled by builder. Used mostly in internal purposes

func (*Response) Status

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

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) String added in v0.12.0

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

String sets the response's body to the passed string

func (*Response) TransferEncoding added in v0.5.0

func (r *Response) TransferEncoding(value string) *Response

TransferEncoding sets a custom Transfer-Encoding header value.

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 ResponseWriter

type ResponseWriter func(b []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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