types

package
v1.1.2-beta Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRead = errors.New("body has been already read")
View Source
var WithResponse = Response{
	Code:   status.OK,
	Status: status.Text(status.OK),
}

WithResponse is just a nil-filled default pre-created response. Because of clear methods, it is anyway copied every time it is used as constructor so please, DO NOT modify fields of this variable

Functions

func Hijacker

func Hijacker(request *Request, hijacker hijackConn) func() (net.Conn, error)

Types

type ConnectionHijacker

type ConnectionHijacker func() (net.Conn, error)

ConnectionHijacker is for user. It returns error because it has to read full request body to stop the server in defined state. And, as we know, reading body may return an error

type FileErrHandler

type FileErrHandler func(err error) Response

type Render

type Render func(response Response) error

type Request

type Request struct {
	Method   methods.Method
	Path     string
	Query    url.Query
	Fragment string
	Proto    proto.Proto
	Remote   net.Addr

	Headers headers.Headers

	ContentLength uint

	Hijack ConnectionHijacker
	// contains filtered or unexported fields
}

Request struct represents http request About headers manager see at http/headers/headers.go:Manager Headers attribute references at that one that lays in manager

func NewRequest

func NewRequest(hdrs headers.Headers, query url.Query, remote net.Addr) (*Request, *body.Gateway)

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) Also url.Query is being constructed right here instead of passing from outside because it has only optional purposes and buff will be nil anyway But maybe it's better to implement DI all the way we go? I don't know, maybe someone will contribute and fix this

func (*Request) Body

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

Body is a high-level function that wraps OnBody, and the only it does is reading pieces of body into the buffer that is a nil by default, but may grow and will stay as big as it grew until the disconnect

func (*Request) OnBody

func (r *Request) OnBody(onBody onBodyCallback, onComplete onCompleteCallback) error

OnBody is a proxy-function for r.body.Read. This method reads body in streaming processing mode by calling onBody on each body piece, and onComplete when body is over (onComplete is guaranteed to be called except situation when body is already read)

func (*Request) Reader

func (r *Request) Reader() io.Reader

Reader returns io.Reader for request body. This method may be called multiple times, but reading from multiple readers leads to Undefined Behaviour

func (*Request) Reset

func (r *Request) Reset() error

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

type Response

type Response struct {
	Code   status.Code
	Status status.Status

	// Body is a mutable object. But it's guaranteed that in WithResponse it will not
	// be modified because it's nil. This means that any data will be appended will
	// allocate a new underlying array
	Body     []byte
	Filename string
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse() Response

func OK

func OK() Response

OK returns a 200 OK response

func WithBody

func WithBody(body string) Response

WithBody sets a string as a response body. This will override already-existing body if it was set

func WithBodyAppend

func WithBodyAppend(body string) Response

WithBodyAppend appends a string to already-existing body

func WithBodyByte

func WithBodyByte(body []byte) Response

WithBodyByte does all the same as WithBody does, but for byte slices

func WithBodyByteAppend

func WithBodyByteAppend(body []byte) Response

WithBodyByteAppend does all the same as WithBodyAppend does, but with byte slices

func WithCode

func WithCode(code status.Code) Response

WithCode 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 WithStatus explicitly

func WithError

func WithError(err error) Response

WithError simply sets a code status.InternalServerError and response body as an error text

func WithFile

func WithFile(path string, handler FileErrHandler) Response

WithFile sets a file path as a file that is supposed to be uploaded as a response. WithFile replaces a response body, so in case last one is specified, it'll be ignored. In case any error occurred (file not found, or error occurred during reading, etc.), handler will be called with a raised error

func WithHeader

func WithHeader(key string, values ...string) Response

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

func WithHeaders

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

WithHeaders 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 WithStatus

func WithStatus(status status.Status) Response

WithStatus sets a status text. Not compulsory, because http does not force us strictly checking a response code, and Unknown Status Code is still a valid response code, but you are better to do this. Be bro

func (Response) File

func (r Response) File() (string, FileErrHandler)

File returns response filename and error handler

func (Response) Headers

func (r Response) Headers() headers.Headers

Headers returns response headers map

func (Response) WithBody

func (r Response) WithBody(body string) Response

WithBody sets a string as a response body. This will override already-existing body if it was set

func (Response) WithBodyAppend

func (r Response) WithBodyAppend(body string) Response

WithBodyAppend appends a string to already-existing body

func (Response) WithBodyByte

func (r Response) WithBodyByte(body []byte) Response

WithBodyByte does all the same as WithBody does, but for byte slices

func (Response) WithBodyByteAppend

func (r Response) WithBodyByteAppend(body []byte) Response

WithBodyByteAppend does all the same as WithBodyAppend does, but with byte slices

func (Response) WithCode

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

WithCode 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 WithStatus explicitly

func (Response) WithError

func (r Response) WithError(err error) Response

WithError simply sets a code status.InternalServerError and response body as an error text

func (Response) WithFile

func (r Response) WithFile(path string, handler FileErrHandler) Response

WithFile sets a file path as a file that is supposed to be uploaded as a response. WithFile replaces a response body, so in case last one is specified, it'll be ignored. In case any error occurred (file not found, or error occurred during reading, etc.), handler will be called with a raised error

func (Response) WithHeader

func (r Response) WithHeader(key string, headerValues ...string) Response

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

func (Response) WithHeaders

func (r Response) WithHeaders(headers map[string][]string) Response

WithHeaders 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) WithStatus

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

WithStatus sets a status text. Not compulsory, because http does not force us strictly checking a response code, and Unknown Status Code is still a valid response code, but you are better to do this. Be a bro

type ResponseWriter

type ResponseWriter func(b []byte) error

Jump to

Keyboard shortcuts

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