Documentation ¶
Overview ¶
Package reqresp provides some http request and response functions.
Index ¶
- Variables
- func ReleaseContext(c *Context)
- func ReleaseResponseWriter(w ResponseWriter)
- func RespondErrorWithContextAndStatusCode(c *Context, statuscode int, err error)
- func RespondErrorWithContextByCode(c *Context, xcode string, err error)
- func SetContext(ctx context.Context, c *Context) context.Context
- func WroteHeader(w http.ResponseWriter) bool
- type Context
- func (c *Context) Accept() []string
- func (c *Context) AppendError(err error)
- func (c *Context) Attachment(filename, filepath string)
- func (c *Context) BindBody(v interface{}) (err error)
- func (c *Context) BindHeader(v interface{}) (err error)
- func (c *Context) BindQuery(v interface{}) (err error)
- func (c *Context) Blob(code int, contentType string, data []byte)
- func (c *Context) BlobText(code int, contentType string, format string, args ...interface{})
- func (c *Context) Charset() string
- func (c *Context) ContentType() string
- func (c *Context) Error(err error)
- func (c *Context) GetCookie(name string) *http.Cookie
- func (c *Context) GetCookies() []*http.Cookie
- func (c *Context) GetData(key string) interface{}
- func (c *Context) GetDataString(key string) string
- func (c *Context) GetQueries() (query url.Values)
- func (c *Context) GetQuery(key string) (value string)
- func (c *Context) GetQueryInt64(key string, required bool) (value int64, err error)
- func (c *Context) GetQueryUint64(key string, required bool) (value uint64, err error)
- func (c *Context) GetRequest() *http.Request
- func (c *Context) GetResponse() http.ResponseWriter
- func (c *Context) HTML(code int, format string, args ...interface{})
- func (c *Context) Header() http.Header
- func (c *Context) Inline(filename, filepath string)
- func (c *Context) IsWebSocket() bool
- func (c *Context) JSON(code int, v interface{})
- func (c *Context) LocalAddr() net.Addr
- func (c *Context) MustGetDataString(key string) string
- func (c *Context) NoContent(code int)
- func (c *Context) ParseQuery() (query url.Values, err error)
- func (c *Context) Redirect(code int, toURL string)
- func (c *Context) RequestID() string
- func (c *Context) RequestId() string
- func (c *Context) Reset()
- func (c *Context) Respond(response result.Response)
- func (c *Context) Scheme() string
- func (c *Context) SetConnectionClose()
- func (c *Context) SetContentDisposition(dtype, filename string)
- func (c *Context) SetContentType(ct string)
- func (c *Context) SetData(key string, value interface{})
- func (c *Context) Stream(code int, contentType string, r io.Reader)
- func (c *Context) Text(code int, format string, args ...interface{})
- func (c *Context) Write(p []byte) (int, error)
- func (c *Context) XML(code int, v interface{})
- type Handler
- type HandlerWithError
- type ResponseWriter
- type StatusCoder
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultRespond is the default result responder. DefaultRespond func(http.ResponseWriter, *http.Request, result.Response) = defaultRespond // DefaultContextRespond is the default result responder based on Context. DefaultContextRespond func(*Context, result.Response) = defaultContextRespond // DefaultContextRespondByCode is the default result responder // based on Context and ResponseCode. DefaultContextRespondByCode func(*Context, string, result.Response) = defaultContextRespondByCode )
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 RespondErrorWithContextAndStatusCode ¶ added in v0.50.0
If statuscode is equal to 0, guess it from the error.
func RespondErrorWithContextByCode ¶ added in v0.50.0
RespondErrorWithContextByCode parses xcode as the status code, which supports
- ""
- "std"
- number
If xcode is empty, it is equal to "std". If xcode is a number and in [100, 599], parse it as status code. For other numbers or characters, but, they are equal to "std".
For "std", it will guess the status code from the error.
func SetContext ¶
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 DefaultContextRespond 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 NewContext ¶ added in v0.3.0
NewContext returns a new Context.
func (*Context) Accept ¶ added in v0.28.0
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
AppendError appends the error err into c.Err.
func (*Context) Attachment ¶ added in v0.35.0
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
BindBody extracts the data from the request body and assigns it to v.
func (*Context) BindHeader ¶ added in v0.5.0
BindHeader extracts the data from the request header and assigns it to v.
func (*Context) BindQuery ¶ added in v0.5.0
BindQuery extracts the data from the request query and assigns it to v.
func (*Context) BlobText ¶
BlobText sends a string blob response with the status code and the content type.
func (*Context) Charset ¶
Charset returns the charset of the request content.
Return "" if there is no charset.
func (*Context) ContentType ¶
ContentType returns the Content-Type of the request without the charset.
func (*Context) Error ¶ added in v0.10.0
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 ¶
GetCookie returns the named cookie provided in the request.
Return nil if no the cookie named name.
func (*Context) GetCookies ¶
GetCookies returns the HTTP cookies sent with the request.
func (*Context) GetData ¶ added in v0.10.0
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
GetDataString returns the value as string by the key from the field Data.
If the key does not exist, return "".
func (*Context) GetQueries ¶
GetQueries is the same as Queries, but ingores the error.
func (*Context) GetQuery ¶
GetQuery parses the query parameters and return the value of the parameter by the key.
func (*Context) GetQueryInt64 ¶ added in v0.33.0
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
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
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) Inline ¶ added in v0.35.0
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 ¶
IsWebSocket reports whether the request is websocket.
func (*Context) LocalAddr ¶ added in v0.42.0
LocalAddr returns the local address of the request connection.
func (*Context) MustGetDataString ¶ added in v0.46.0
MustGetDataString is the same as GetDataString, but panics if key does not found.
func (*Context) ParseQuery ¶
ParseQuery parses the query parameters, caches and returns the parsed query.
func (*Context) RequestID ¶ added in v0.30.0
RequestID returns the request header "X-Request-Id".
DEPRECATED!!! Please use the method RequestId.
func (*Context) Scheme ¶ added in v0.28.0
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
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 ¶
SetContentType sets the response header "Content-Type" to ct,
If ct is "", do nothing.
func (*Context) SetData ¶ added in v0.10.0
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 ¶
Stream sends a streaming response with the status code and the content type.
If contentType is empty, Content-Type is ignored.
type Handler ¶ added in v0.8.0
type Handler func(c *Context)
Handler is a handler based on Context to handle the http request.
type HandlerWithError ¶ added in v0.8.0
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
}