reqresp

package
v0.49.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: Apache-2.0 Imports: 21 Imported by: 1

Documentation

Overview

Package reqresp provides some http request and response functions.

Index

Constants

This section is empty.

Variables

View Source
var DefaultResponder func(*Context, result.Response) = RespondResultResponse

DefaultResponder is default result responder.

Functions

func ReleaseContext added in v0.42.0

func ReleaseContext(c *Context)

ReleaseContext releases the context to the pool.

func ReleaseResponseWriter added in v0.42.0

func ReleaseResponseWriter(w ResponseWriter)

ReleaseResponseWriter releases the ResponseWriter into the pool.

func RespondResultResponse added in v0.49.0

func RespondResultResponse(c *Context, response result.Response)

func SetContext

func SetContext(ctx context.Context, c *Context) context.Context

SetContext returns a new context.Context containing c.

func WroteHeader added in v0.42.0

func WroteHeader(w http.ResponseWriter) bool

WroteHeader reports whether the response writer has wrote header.

Types

type Context

type Context struct {
	ResponseWriter
	*http.Request

	// As a general rule, the data keys starting with "_" are private.
	Data map[string]interface{} // A set of any key-value pairs
	Reg1 interface{}            // The register to save the temporary context value.
	Reg2 interface{}            // The register to save the temporary context value.
	Reg3 interface{}            // The register to save the temporary context value.
	Err  error                  // Used to save the context error.

	// The extra context information, which may be used by other service,
	// such as the action router.
	Version string
	Action  string
	Route   any

	// Bind the value to the request body
	//
	// If nil, use binder.BodyDecoder instead.
	BodyDecoder binder.Decoder

	// Bind the value to the request query.
	//
	// If nil, use binder.QueryDecoder instead.
	QueryDecoder binder.Decoder

	// Bind the value to the request header.
	//
	// If nil, use binder.HeaderDecoder instead.
	HeaderDecoder binder.Decoder

	// Responder is the result responder used by the method Respond.
	//
	// If nil, use DefaultResponder instead.
	Responder func(*Context, result.Response)

	// Query and Cookies are used to cache the parsed request query and cookies.
	Cookies []*http.Cookie
	Query   url.Values
}

Context is used to represents the context information of the request.

func AcquireContext added in v0.42.0

func AcquireContext() *Context

AcquireContext acquires a context from the pool.

func GetContext

func GetContext(ctx context.Context) *Context

GetContext returns a *Context from context.Context.

If not exist, reutrn nil.

func NewContext added in v0.3.0

func NewContext(dataCapSize int) *Context

NewContext returns a new Context.

func (*Context) Accept added in v0.28.0

func (c *Context) Accept() []string

Accept returns the accepted Content-Type list from the request header "Accept", which are sorted by the q-factor weight from high to low.

If there is no the request header "Accept", return nil.

func (*Context) AppendError added in v0.42.0

func (c *Context) AppendError(err error)

AppendError appends the error err into c.Err.

func (*Context) Attachment added in v0.35.0

func (c *Context) Attachment(filename, filepath string)

Attachment sends a file as attachment.

If filename is "", it will use the base name of the filepath instead. And if the file does not exist, it returns os.ErrNotExist.

func (*Context) BindBody added in v0.5.0

func (c *Context) BindBody(v interface{}) (err error)

BindBody extracts the data from the request body and assigns it to v.

func (*Context) BindHeader added in v0.5.0

func (c *Context) BindHeader(v interface{}) (err error)

BindHeader extracts the data from the request header and assigns it to v.

func (*Context) BindQuery added in v0.5.0

func (c *Context) BindQuery(v interface{}) (err error)

BindQuery extracts the data from the request query and assigns it to v.

func (*Context) Blob

func (c *Context) Blob(code int, contentType string, data []byte)

Blob sends a blob response with the status code and the content type.

func (*Context) BlobText

func (c *Context) BlobText(code int, contentType string, format string, args ...interface{})

BlobText sends a string blob response with the status code and the content type.

func (*Context) Charset

func (c *Context) Charset() string

Charset returns the charset of the request content.

Return "" if there is no charset.

func (*Context) ContentType

func (c *Context) ContentType() string

ContentType returns the Content-Type of the request without the charset.

func (*Context) Error added in v0.10.0

func (c *Context) Error(err error)

Error sends the error as the response to the client

If err is nil, it is equal to c.WriteHeader(200).
If err implements http.Handler, it is equal to err.ServeHTTP(c.ResponseWriter, c.Request).
Or, it is equal to c.Text(500, err.Error()).

func (*Context) GetCookie

func (c *Context) GetCookie(name string) *http.Cookie

GetCookie returns the named cookie provided in the request.

Return nil if no the cookie named name.

func (*Context) GetCookies

func (c *Context) GetCookies() []*http.Cookie

GetCookies returns the HTTP cookies sent with the request.

func (*Context) GetData added in v0.10.0

func (c *Context) GetData(key string) interface{}

GetData returns the value by the key from the field Data.

If the key does not exist, return nil.

func (*Context) GetDataString added in v0.10.0

func (c *Context) GetDataString(key string) string

GetDataString returns the value as string by the key from the field Data.

If the key does not exist, return "".

func (*Context) GetQueries

func (c *Context) GetQueries() (query url.Values)

GetQueries is the same as Queries, but ingores the error.

func (*Context) GetQuery

func (c *Context) GetQuery(key string) (value string)

GetQuery parses the query parameters and return the value of the parameter by the key.

func (*Context) GetQueryInt64 added in v0.33.0

func (c *Context) GetQueryInt64(key string, required bool) (value int64, err error)

GetQueryInt64 returns the value as int64 by the key from the field Data.

If the key does not exist and required is false, return (0, nil).

func (*Context) GetQueryUint64 added in v0.33.0

func (c *Context) GetQueryUint64(key string, required bool) (value uint64, err error)

GetQueryUint64 returns the value as uint64 by the key from the field Data.

If the key does not exist and required is false, return (0, nil).

func (*Context) GetRequest added in v0.30.0

func (c *Context) GetRequest() *http.Request

GetRequest returns the wrapped http.Request.

func (*Context) GetResponse added in v0.30.0

func (c *Context) GetResponse() http.ResponseWriter

GetResponse returns the wrapped http.ResponseWriter.

func (*Context) HTML

func (c *Context) HTML(code int, format string, args ...interface{})

HTML sends a HTML response with the status code.

func (*Context) Header

func (c *Context) Header() http.Header

Header implements the interface http.ResponseWriter#Header.

func (*Context) Inline added in v0.35.0

func (c *Context) Inline(filename, filepath string)

Inline sends a file as inline.

If filename is "", it will use the base name of the filepath instead. And if the file does not exist, it returns os.ErrNotExist.

func (*Context) IsWebSocket

func (c *Context) IsWebSocket() bool

IsWebSocket reports whether the request is websocket.

func (*Context) JSON

func (c *Context) JSON(code int, v interface{})

JSON sends a JSON response with the status code.

func (*Context) LocalAddr added in v0.42.0

func (c *Context) LocalAddr() net.Addr

LocalAddr returns the local address of the request connection.

func (*Context) MustGetDataString added in v0.46.0

func (c *Context) MustGetDataString(key string) string

MustGetDataString is the same as GetDataString, but panics if key does not found.

func (*Context) NoContent added in v0.28.0

func (c *Context) NoContent(code int)

NoContent is the alias of WriteHeader.

func (*Context) ParseQuery

func (c *Context) ParseQuery() (query url.Values, err error)

ParseQuery parses the query parameters, caches and returns the parsed query.

func (*Context) Redirect

func (c *Context) Redirect(code int, toURL string)

Redirect redirects the request to a provided URL with status code.

func (*Context) RequestID added in v0.30.0

func (c *Context) RequestID() string

RequestID returns the request header "X-Request-Id".

DEPRECATED!!! Please use the method RequestId.

func (*Context) RequestId added in v0.49.0

func (c *Context) RequestId() string

RequestId returns the request header "X-Request-Id".

func (*Context) Reset

func (c *Context) Reset()

Reset resets the context itself.

func (*Context) Respond added in v0.26.0

func (c *Context) Respond(response result.Response)

Respond implements the interface result.Responder.

func (*Context) Scheme added in v0.28.0

func (c *Context) Scheme() string

Scheme returns the HTTP protocol scheme, `http` or `https`.

func (*Context) SetConnectionClose added in v0.28.0

func (c *Context) SetConnectionClose()

SetConnectionClose sets the response header "Connection: close" to tell the server to close the connection.

func (*Context) SetContentDisposition added in v0.42.0

func (c *Context) SetContentDisposition(dtype, filename string)

SetConnectionClose sets the response header "Content-Disposition". For example,

Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="filename.jpg"

dtype must be either "inline" or "attachment". But, filename is optional.

func (*Context) SetContentType

func (c *Context) SetContentType(ct string)

SetContentType sets the response header "Content-Type" to ct,

If ct is "", do nothing.

func (*Context) SetData added in v0.10.0

func (c *Context) SetData(key string, value interface{})

SetData sets the value with the key into the field Data.

If value is nil, delete the key from the field Data.

func (*Context) Stream

func (c *Context) Stream(code int, contentType string, r io.Reader)

Stream sends a streaming response with the status code and the content type.

If contentType is empty, Content-Type is ignored.

func (*Context) Text

func (c *Context) Text(code int, format string, args ...interface{})

Text sends a string response with the status code.

func (*Context) Write

func (c *Context) Write(p []byte) (int, error)

Write implements the interface http.ResponseWriter#Write.

func (*Context) XML

func (c *Context) XML(code int, v interface{})

XML sends a XML response with the status code.

type Handler added in v0.8.0

type Handler func(c *Context)

Handler is a handler based on Context to handle the http request.

func (Handler) ServeHTTP added in v0.8.0

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the interface http.Handler.

type HandlerWithError added in v0.8.0

type HandlerWithError func(c *Context) error

HandlerWithError is a handler to handle the http request with the error.

func (HandlerWithError) ServeHTTP added in v0.8.0

func (h HandlerWithError) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the interface http.Handler.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	WroteHeader() bool
	StatusCode() int
}

ResponseWriter is an extended http.ResponseWriter.

func AcquireResponseWriter added in v0.42.0

func AcquireResponseWriter(w http.ResponseWriter) ResponseWriter

AcquireResponseWriter acquires a ResponseWriter with w from the pool.

type StatusCoder added in v0.49.0

type StatusCoder interface {
	StatusCode() int
}

Jump to

Keyboard shortcuts

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