http

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment added in v0.5.0

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

Attachment is a wrapper for io.Reader, with the difference that there is the size attribute. If positive value (including 0) is set, then ordinary plain-text response will be rendered. Otherwise, chunked transfer encoding is used.

func NewAttachment added in v0.5.0

func NewAttachment(content io.Reader, size int) Attachment

NewAttachment returns a new Attachment instance

func (Attachment) Content added in v0.5.0

func (a Attachment) Content() io.Reader

func (Attachment) Size added in v0.5.0

func (a Attachment) Size() int

type BodyReader

type BodyReader interface {
	Init(*Request)
	Read() ([]byte, error)
}

type Fragment

type Fragment = string

type Params added in v0.5.0

type Params = map[string]string

type Path

type Path struct {
	String   string
	Params   Params
	Query    query.Query
	Fragment Fragment
}

type Request

type Request struct {
	Method method.Method
	Path   Path
	Proto  proto.Proto
	Remote net.Addr

	Headers headers.Headers

	Upgrade       proto.Proto
	ContentLength int
	IsChunked     bool

	Ctx context.Context
	// 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 query.Query, response Response, conn net.Conn, body BodyReader,
	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) 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) 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) HasBody

func (r *Request) HasBody() bool

HasBody returns not actual "whether request contains a body", but a possibility. So result only depends on whether content-length is more than 0, or chunked transfer encoding is enabled

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) OnBody

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

OnBody is a low-level interface accessing a request body. It takes onBody callback that is being called every time a piece of body is read (note: even a single byte can be passed). In case error returned, it'll be returned from OnBody method. In case onBody never did return an error, onComplete will be called when the body will be finished. This callback also can return an error that'll be returned from OnBody method - for example, in case body's hash sum is invalid

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) WasHijacked

func (r *Request) WasHijacked() bool

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

type Response

type Response struct {
	Code status.Code
	// Status is empty by default, in this case renderer must put a default one
	Status status.Status

	// ContentType, as a special for core header, should be treated individually
	ContentType string
	// The same is about TransferEncoding
	TransferEncoding string
	Body             []byte
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse() Response

func RespondTo

func RespondTo(request *Request) Response

RespondTo returns a response object of request

func (Response) Attachment added in v0.5.0

func (r Response) Attachment() Attachment

Attachment returns response's attachment.

WARNING: do NEVER use this method in your code. It serves internal purposes ONLY

func (Response) Clear

func (r Response) Clear() Response

Clear discards everything was done with response object before

func (Response) DiscardHeaders

func (r Response) DiscardHeaders() Response

DiscardHeaders returns response object with no any headers set.

Warning: this action is not pure. Appending new headers will cause overriding old ones

func (Response) Headers

func (r Response) Headers() []string

Headers returns an underlying response headers

func (Response) WithAttachment added in v0.5.0

func (r Response) WithAttachment(attachment Attachment) Response

WithAttachment sets a response's attachment. In this case response body will be ignored

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) WithBodyByte

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

WithBodyByte does all the same as Body does, but for 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 Status explicitly

func (Response) WithContentType added in v0.5.0

func (r Response) WithContentType(value string) Response

WithContentType sets a custom Content-Type header value.

func (Response) WithError

func (r Response) WithError(err error) Response

WithError tries to set a corresponding status code and response body equal to error text if error is known to server, otherwise setting status code to status.InternalServerError without setting a response body to the error text, because this possibly may reveal some sensitive internal infrastructure details

func (Response) WithFile

func (r Response) WithFile(path string) (Response, error)

WithFile opens a file for reading, and returns a new response with attachment corresponding to the file FD. In case not found or any other error, it'll be directly returned

func (Response) WithHeader

func (r Response) WithHeader(key string, values ...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 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) WithTransferEncoding added in v0.5.0

func (r Response) WithTransferEncoding(value string) Response

WithTransferEncoding sets a custom Transfer-Encoding header value.

func (Response) WithWriter

func (r Response) WithWriter(cb func(io.Writer) error) (Response, error)

WithWriter takes a function that takes an io.Writer, which allows us to stream data directly into the response body. Note: this method causes an allocation

TODO: This is not the best design solution. I would like to make this method just like

all others, so returning only Response object itself. The problem is that it is
impossible because io.Writer is a procedure-style thing that does not work with
our builder that pretends to be clear. Hope in future this issue will be solved

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