context

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: LGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package context provide the context utils Usage:

	import "github.com/W3-Engineers-Ltd/Radiant/server/web/context"

	ctx := context.Context{Request:req,ResponseWriter:rw}

 more docs http://beego.me/docs/module/context.md

Index

Constants

View Source
const (
	ApplicationJSON = context.ApplicationJSON
	ApplicationXML  = context.ApplicationXML
	ApplicationYAML = context.ApplicationYAML
	TextXML         = context.TextXML
)

commonly used mime-types

Variables

This section is empty.

Functions

func InitGzip

func InitGzip(minLength, compressLevel int, methods []string)

InitGzip init the gzipcompress

func ParseEncoding

func ParseEncoding(r *http.Request) string

ParseEncoding will extract the right encoding for response the Accept-Encoding's sec is here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3

func WriteBody

func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error)

WriteBody reads writes content to writer by the specific encoding(gzip/deflate)

func WriteFile

func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string, error)

WriteFile reads from file and writes to writer by the specific encoding(gzip/deflate)

Types

type BeegoInput added in v1.0.0

type BeegoInput context.BeegoInput

BeegoInput operates the http request header, data, cookie and body. it also contains router params and current session.

func NewInput

func NewInput() *BeegoInput

NewInput return BeegoInput generated by Context.

func (*BeegoInput) AcceptsHTML added in v1.0.0

func (input *BeegoInput) AcceptsHTML() bool

AcceptsHTML Checks if request accepts html response

func (*BeegoInput) AcceptsJSON added in v1.0.0

func (input *BeegoInput) AcceptsJSON() bool

AcceptsJSON Checks if request accepts json response

func (*BeegoInput) AcceptsXML added in v1.0.0

func (input *BeegoInput) AcceptsXML() bool

AcceptsXML Checks if request accepts xml response

func (*BeegoInput) AcceptsYAML added in v1.0.0

func (input *BeegoInput) AcceptsYAML() bool

AcceptsYAML Checks if request accepts json response

func (*BeegoInput) Bind added in v1.0.0

func (input *BeegoInput) Bind(dest interface{}, key string) error

Bind data from request.Form[key] to dest like /?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie var id int beegoInput.Bind(&id, "id") id ==123 var isok bool beegoInput.Bind(&isok, "isok") isok ==true var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2 ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2] ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str array] user struct{Name} beegoInput.Bind(&user, "user") user == {Name:"astaxie"}

func (*BeegoInput) Cookie added in v1.0.0

func (input *BeegoInput) Cookie(key string) string

Cookie returns request cookie item string by a given key. if non-existed, return empty string.

func (*BeegoInput) CopyBody added in v1.0.0

func (input *BeegoInput) CopyBody(MaxMemory int64) []byte

CopyBody returns the raw request body data as bytes.

func (*BeegoInput) Data added in v1.0.0

func (input *BeegoInput) Data() map[interface{}]interface{}

Data return the implicit data in the input

func (*BeegoInput) Domain added in v1.0.0

func (input *BeegoInput) Domain() string

Domain returns host name. Alias of Host method.

func (*BeegoInput) GetData added in v1.0.0

func (input *BeegoInput) GetData(key interface{}) interface{}

GetData returns the stored data in this context.

func (*BeegoInput) Header added in v1.0.0

func (input *BeegoInput) Header(key string) string

Header returns request header item string by a given string. if non-existed, return empty string.

func (*BeegoInput) Host added in v1.0.0

func (input *BeegoInput) Host() string

Host returns host name. if no host info in request, return localhost.

func (*BeegoInput) IP added in v1.0.0

func (input *BeegoInput) IP() string

IP returns request client ip. if in proxy, return first proxy id. if error, return RemoteAddr.

func (*BeegoInput) Is added in v1.0.0

func (input *BeegoInput) Is(method string) bool

Is returns boolean of this request is on given method, such as Is("POST").

func (*BeegoInput) IsAjax added in v1.0.0

func (input *BeegoInput) IsAjax() bool

IsAjax returns boolean of this request is generated by ajax.

func (*BeegoInput) IsDelete added in v1.0.0

func (input *BeegoInput) IsDelete() bool

IsDelete Is this a DELETE method request?

func (*BeegoInput) IsGet added in v1.0.0

func (input *BeegoInput) IsGet() bool

IsGet Is this a GET method request?

func (*BeegoInput) IsHead added in v1.0.0

func (input *BeegoInput) IsHead() bool

IsHead Is this a Head method request?

func (*BeegoInput) IsOptions added in v1.0.0

func (input *BeegoInput) IsOptions() bool

IsOptions Is this a OPTIONS method request?

func (*BeegoInput) IsPatch added in v1.0.0

func (input *BeegoInput) IsPatch() bool

IsPatch Is this a PATCH method request?

func (*BeegoInput) IsPost added in v1.0.0

func (input *BeegoInput) IsPost() bool

IsPost Is this a POST method request?

func (*BeegoInput) IsPut added in v1.0.0

func (input *BeegoInput) IsPut() bool

IsPut Is this a PUT method request?

func (*BeegoInput) IsSecure added in v1.0.0

func (input *BeegoInput) IsSecure() bool

IsSecure returns boolean of this request is in https.

func (*BeegoInput) IsUpload added in v1.0.0

func (input *BeegoInput) IsUpload() bool

IsUpload returns boolean of whether file uploads in this request or not..

func (*BeegoInput) IsWebsocket added in v1.0.0

func (input *BeegoInput) IsWebsocket() bool

IsWebsocket returns boolean of this request is in webSocket.

func (*BeegoInput) Method added in v1.0.0

func (input *BeegoInput) Method() string

Method returns http request method.

func (*BeegoInput) Param added in v1.0.0

func (input *BeegoInput) Param(key string) string

Param returns router param by a given key.

func (*BeegoInput) Params added in v1.0.0

func (input *BeegoInput) Params() map[string]string

Params returns the map[key]value.

func (*BeegoInput) ParamsLen added in v1.0.0

func (input *BeegoInput) ParamsLen() int

ParamsLen return the length of the params

func (*BeegoInput) ParseFormOrMulitForm added in v1.0.0

func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error

ParseFormOrMulitForm parseForm or parseMultiForm based on Content-type

func (*BeegoInput) Port added in v1.0.0

func (input *BeegoInput) Port() int

Port returns request client port. when error or empty, return 80.

func (*BeegoInput) Protocol added in v1.0.0

func (input *BeegoInput) Protocol() string

Protocol returns request protocol name, such as HTTP/1.1 .

func (*BeegoInput) Proxy added in v1.0.0

func (input *BeegoInput) Proxy() []string

Proxy returns proxy client ips slice.

func (*BeegoInput) Query added in v1.0.0

func (input *BeegoInput) Query(key string) string

Query returns input data item string by a given string.

func (*BeegoInput) Refer added in v1.0.0

func (input *BeegoInput) Refer() string

Refer returns http referer header.

func (*BeegoInput) Referer added in v1.0.0

func (input *BeegoInput) Referer() string

Referer returns http referer header.

func (*BeegoInput) Reset added in v1.0.0

func (input *BeegoInput) Reset(ctx *Context)

Reset init the BeegoInput

func (*BeegoInput) ResetParams added in v1.0.0

func (input *BeegoInput) ResetParams()

ResetParams clears any of the input's Params This function is used to clear parameters so they may be reset between filter passes.

func (*BeegoInput) Scheme added in v1.0.0

func (input *BeegoInput) Scheme() string

Scheme returns request scheme as "http" or "https".

func (*BeegoInput) Session added in v1.0.0

func (input *BeegoInput) Session(key interface{}) interface{}

Session returns current session item value by a given key. if non-existed, return nil.

func (*BeegoInput) SetData added in v1.0.0

func (input *BeegoInput) SetData(key, val interface{})

SetData stores data with given key in this context. This data are only available in this context.

func (*BeegoInput) SetParam added in v1.0.0

func (input *BeegoInput) SetParam(key, val string)

SetParam will set the param with key and value

func (*BeegoInput) Site added in v1.0.0

func (input *BeegoInput) Site() string

Site returns base site url as scheme://domain type.

func (*BeegoInput) SubDomains added in v1.0.0

func (input *BeegoInput) SubDomains() string

SubDomains returns sub domain string. if aa.bb.domain.com, returns aa.bb .

func (*BeegoInput) URI added in v1.0.0

func (input *BeegoInput) URI() string

URI returns full request url with query string, fragment.

func (*BeegoInput) URL added in v1.0.0

func (input *BeegoInput) URL() string

URL returns request url path (without query string, fragment).

func (*BeegoInput) UserAgent added in v1.0.0

func (input *BeegoInput) UserAgent() string

UserAgent returns request client user agent string.

type BeegoOutput added in v1.0.0

type BeegoOutput context.BeegoOutput

BeegoOutput does work for sending response header.

func NewOutput

func NewOutput() *BeegoOutput

NewOutput returns new BeegoOutput. it contains nothing now.

func (*BeegoOutput) Body added in v1.0.0

func (output *BeegoOutput) Body(content []byte) error

Body sets response body content. if EnableGzip, compress content string. it sends out response body directly.

func (*BeegoOutput) ContentType added in v1.0.0

func (output *BeegoOutput) ContentType(ext string)

ContentType sets the content type from ext string. MIME type is given in mime package.

func (*BeegoOutput) Cookie added in v1.0.0

func (output *BeegoOutput) Cookie(name string, value string, others ...interface{})

Cookie sets cookie value via given key. others are ordered as cookie's max age time, path,domain, secure and httponly.

func (*BeegoOutput) Download added in v1.0.0

func (output *BeegoOutput) Download(file string, filename ...string)

Download forces response for download file. it prepares the download response header automatically.

func (*BeegoOutput) Header added in v1.0.0

func (output *BeegoOutput) Header(key, val string)

Header sets response header item string via given key.

func (*BeegoOutput) IsCachable added in v1.0.0

func (output *BeegoOutput) IsCachable() bool

IsCachable returns boolean of this request is cached. HTTP 304 means cached.

func (*BeegoOutput) IsClientError added in v1.0.0

func (output *BeegoOutput) IsClientError() bool

IsClientError returns boolean of this request client sends error data. HTTP 4xx means client error.

func (*BeegoOutput) IsEmpty added in v1.0.0

func (output *BeegoOutput) IsEmpty() bool

IsEmpty returns boolean of this request is empty. HTTP 201,204 and 304 means empty.

func (*BeegoOutput) IsForbidden added in v1.0.0

func (output *BeegoOutput) IsForbidden() bool

IsForbidden returns boolean of this request is forbidden. HTTP 403 means forbidden.

func (*BeegoOutput) IsNotFound added in v1.0.0

func (output *BeegoOutput) IsNotFound() bool

IsNotFound returns boolean of this request is not found. HTTP 404 means not found.

func (*BeegoOutput) IsOk added in v1.0.0

func (output *BeegoOutput) IsOk() bool

IsOk returns boolean of this request runs well. HTTP 200 means ok.

func (*BeegoOutput) IsRedirect added in v1.0.0

func (output *BeegoOutput) IsRedirect() bool

IsRedirect returns boolean of this request is redirection header. HTTP 301,302,307 means redirection.

func (*BeegoOutput) IsServerError added in v1.0.0

func (output *BeegoOutput) IsServerError() bool

IsServerError returns boolean of this server handler errors. HTTP 5xx means server internal error.

func (*BeegoOutput) IsSuccessful added in v1.0.0

func (output *BeegoOutput) IsSuccessful() bool

IsSuccessful returns boolean of this request runs successfully. HTTP 2xx means ok.

func (*BeegoOutput) JSON added in v1.0.0

func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, encoding bool) error

JSON writes json to response body. if encoding is true, it converts utf-8 to \u0000 type.

func (*BeegoOutput) JSONP added in v1.0.0

func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error

JSONP writes jsonp to response body.

func (*BeegoOutput) Reset added in v1.0.0

func (output *BeegoOutput) Reset(ctx *Context)

Reset init BeegoOutput

func (*BeegoOutput) ServeFormatted added in v1.0.0

func (output *BeegoOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool)

ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header

func (*BeegoOutput) Session added in v1.0.0

func (output *BeegoOutput) Session(name interface{}, value interface{})

Session sets session item value with given key.

func (*BeegoOutput) SetStatus added in v1.0.0

func (output *BeegoOutput) SetStatus(status int)

SetStatus sets response status code. It writes response header directly.

func (*BeegoOutput) XML added in v1.0.0

func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error

XML writes xml string to response body.

func (*BeegoOutput) YAML added in v1.0.0

func (output *BeegoOutput) YAML(data interface{}) error

YAML writes yaml to response body.

type Context

type Context context.Context

Context Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter. BeegoInput and BeegoOutput provides some api to operate request and response more easily.

func NewContext

func NewContext() *Context

NewContext return the Context with Input and Output

func (*Context) Abort

func (ctx *Context) Abort(status int, body string)

Abort stops this request. if beego.ErrorMaps exists, panic body.

func (*Context) CheckXSRFCookie

func (ctx *Context) CheckXSRFCookie() bool

CheckXSRFCookie checks xsrf token in this request is valid or not. the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" or in form field value named as "_xsrf".

func (*Context) GetCookie

func (ctx *Context) GetCookie(key string) string

GetCookie Get cookie from request by a given key. It's alias of BeegoInput.Cookie.

func (*Context) GetSecureCookie

func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool)

GetSecureCookie Get secure cookie from request by a given key.

func (*Context) Redirect

func (ctx *Context) Redirect(status int, localurl string)

Redirect does redirection to localurl with http header status code.

func (*Context) RenderMethodResult

func (ctx *Context) RenderMethodResult(result interface{})

RenderMethodResult renders the return value of a controller method to the output

func (*Context) Reset

func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request)

Reset init Context, BeegoInput and BeegoOutput

func (*Context) SetCookie

func (ctx *Context) SetCookie(name string, value string, others ...interface{})

SetCookie Set cookie for response. It's alias of BeegoOutput.Cookie.

func (*Context) SetSecureCookie

func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{})

SetSecureCookie Set Secure cookie for response.

func (*Context) WriteString

func (ctx *Context) WriteString(content string)

WriteString Write string to response body. it sends response body.

func (*Context) XSRFToken

func (ctx *Context) XSRFToken(key string, expire int64) string

XSRFToken creates a xsrf token string and returns.

type Renderer

type Renderer context.Renderer

Renderer defines an http response renderer

type Response

type Response context.Response

Response is a wrapper for the http.ResponseWriter started set to true if response was written to then don't execute other handler

func (*Response) CloseNotify

func (r *Response) CloseNotify() <-chan bool

CloseNotify http.CloseNotifier

func (*Response) Flush

func (r *Response) Flush()

Flush http.Flusher

func (*Response) Hijack

func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack hijacker for http

func (*Response) Pusher

func (r *Response) Pusher() (pusher http.Pusher)

Pusher http.Pusher

func (*Response) Write

func (r *Response) Write(p []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply, and sets `started` to true. started means the response has sent out.

func (*Response) WriteHeader

func (r *Response) WriteHeader(code int)

WriteHeader sends an HTTP response header with status code, and sets `started` to true.

type StatusCode

type StatusCode int

StatusCode sets the http response status code

const (
	// BadRequest indicates http error 400
	BadRequest StatusCode = http.StatusBadRequest

	// NotFound indicates http error 404
	NotFound StatusCode = http.StatusNotFound
)

func (StatusCode) Error

func (s StatusCode) Error() string

func (StatusCode) Render

func (s StatusCode) Render(ctx *Context)

Render sets the http status code

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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