beego: github.com/astaxie/beego/server/web/context Index | Files | Directories

package context

import "github.com/astaxie/beego/server/web/context"

Package context provide the context utils Usage:

	import "github.com/astaxie/beego/context"

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

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

Index

Package Files

acceptencoder.go context.go input.go output.go renderer.go response.go

Constants

const (
    ApplicationJSON = "application/json"
    ApplicationXML  = "application/xml"
    ApplicationYAML = "application/x-yaml"
    TextXML         = "text/xml"
)

Commonly used mime-types

func InitGzip Uses

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

InitGzip initializes the gzipcompress

func ParseEncoding Uses

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 Uses

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 Uses

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)

type BeegoInput Uses

type BeegoInput struct {
    Context    *Context
    CruSession session.Store

    RequestBody   []byte
    RunMethod     string
    RunController reflect.Type
    // contains filtered or unexported fields
}

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

func NewInput Uses

func NewInput() *BeegoInput

NewInput returns the BeegoInput generated by context.

func (*BeegoInput) AcceptsHTML Uses

func (input *BeegoInput) AcceptsHTML() bool

AcceptsHTML Checks if request accepts html response

func (*BeegoInput) AcceptsJSON Uses

func (input *BeegoInput) AcceptsJSON() bool

AcceptsJSON Checks if request accepts json response

func (*BeegoInput) AcceptsXML Uses

func (input *BeegoInput) AcceptsXML() bool

AcceptsXML Checks if request accepts xml response

func (*BeegoInput) AcceptsYAML Uses

func (input *BeegoInput) AcceptsYAML() bool

AcceptsYAML Checks if request accepts json response

func (*BeegoInput) Bind Uses

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 Uses

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 Uses

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

CopyBody returns the raw request body data as bytes.

func (*BeegoInput) Data Uses

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

Data returns the implicit data in the input

func (*BeegoInput) Domain Uses

func (input *BeegoInput) Domain() string

Domain returns the host name (alias of host method)

func (*BeegoInput) GetData Uses

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

GetData returns the stored data in this context.

func (*BeegoInput) Header Uses

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 Uses

func (input *BeegoInput) Host() string

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

func (*BeegoInput) IP Uses

func (input *BeegoInput) IP() string

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

func (*BeegoInput) Is Uses

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

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

func (*BeegoInput) IsAjax Uses

func (input *BeegoInput) IsAjax() bool

IsAjax returns boolean of is this request generated by ajax.

func (*BeegoInput) IsDelete Uses

func (input *BeegoInput) IsDelete() bool

IsDelete Is this a DELETE method request?

func (*BeegoInput) IsGet Uses

func (input *BeegoInput) IsGet() bool

IsGet Is this a GET method request?

func (*BeegoInput) IsHead Uses

func (input *BeegoInput) IsHead() bool

IsHead Is this a Head method request?

func (*BeegoInput) IsOptions Uses

func (input *BeegoInput) IsOptions() bool

IsOptions Is this a OPTIONS method request?

func (*BeegoInput) IsPatch Uses

func (input *BeegoInput) IsPatch() bool

IsPatch Is this a PATCH method request?

func (*BeegoInput) IsPost Uses

func (input *BeegoInput) IsPost() bool

IsPost Is this a POST method request?

func (*BeegoInput) IsPut Uses

func (input *BeegoInput) IsPut() bool

IsPut Is this a PUT method request?

func (*BeegoInput) IsSecure Uses

func (input *BeegoInput) IsSecure() bool

IsSecure returns boolean of this request is in https.

func (*BeegoInput) IsUpload Uses

func (input *BeegoInput) IsUpload() bool

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

func (*BeegoInput) IsWebsocket Uses

func (input *BeegoInput) IsWebsocket() bool

IsWebsocket returns boolean of this request is in webSocket.

func (*BeegoInput) Method Uses

func (input *BeegoInput) Method() string

Method returns http request method.

func (*BeegoInput) Param Uses

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

Param returns router param by a given key.

func (*BeegoInput) Params Uses

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

Params returns the map[key]value.

func (*BeegoInput) ParamsLen Uses

func (input *BeegoInput) ParamsLen() int

ParamsLen return the length of the params

func (*BeegoInput) ParseFormOrMultiForm Uses

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

ParseFormOrMultiForm parseForm or parseMultiForm based on Content-type

func (*BeegoInput) Port Uses

func (input *BeegoInput) Port() int

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

func (*BeegoInput) Protocol Uses

func (input *BeegoInput) Protocol() string

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

func (*BeegoInput) Proxy Uses

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

Proxy returns proxy client ips slice.

func (*BeegoInput) Query Uses

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

Query returns input data item string by a given string.

func (*BeegoInput) Refer Uses

func (input *BeegoInput) Refer() string

Refer returns http referer header.

func (*BeegoInput) Referer Uses

func (input *BeegoInput) Referer() string

Referer returns http referer header.

func (*BeegoInput) Reset Uses

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

Reset initializes the BeegoInput

func (*BeegoInput) ResetParams Uses

func (input *BeegoInput) ResetParams()

ResetParams clears any of the input's params Used to clear parameters so they may be reset between filter passes.

func (*BeegoInput) Scheme Uses

func (input *BeegoInput) Scheme() string

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

func (*BeegoInput) Session Uses

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 Uses

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

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

func (*BeegoInput) SetParam Uses

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

SetParam sets the param with key and value

func (*BeegoInput) Site Uses

func (input *BeegoInput) Site() string

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

func (*BeegoInput) SubDomains Uses

func (input *BeegoInput) SubDomains() string

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

func (*BeegoInput) URI Uses

func (input *BeegoInput) URI() string

URI returns the full request url with query, string and fragment.

func (*BeegoInput) URL Uses

func (input *BeegoInput) URL() string

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

func (*BeegoInput) UserAgent Uses

func (input *BeegoInput) UserAgent() string

UserAgent returns request client user agent string.

type BeegoOutput Uses

type BeegoOutput struct {
    Context    *Context
    Status     int
    EnableGzip bool
}

BeegoOutput does work for sending response header.

func NewOutput Uses

func NewOutput() *BeegoOutput

NewOutput returns new BeegoOutput. Empty when initialized

func (*BeegoOutput) Body Uses

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

Body sets the response body content. if EnableGzip, content is compressed. Sends out response body directly.

func (*BeegoOutput) ContentType Uses

func (output *BeegoOutput) ContentType(ext string)

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

func (*BeegoOutput) Cookie Uses

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

Cookie sets a cookie value via given key. others: used to set a cookie's max age time, path,domain, secure and httponly.

func (*BeegoOutput) Download Uses

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

Download forces response for download file. Prepares the download response header automatically.

func (*BeegoOutput) Header Uses

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

Header sets response header item string via given key.

func (*BeegoOutput) IsCachable Uses

func (output *BeegoOutput) IsCachable() bool

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

func (*BeegoOutput) IsClientError Uses

func (output *BeegoOutput) IsClientError() bool

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

func (*BeegoOutput) IsEmpty Uses

func (output *BeegoOutput) IsEmpty() bool

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

func (*BeegoOutput) IsForbidden Uses

func (output *BeegoOutput) IsForbidden() bool

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

func (*BeegoOutput) IsNotFound Uses

func (output *BeegoOutput) IsNotFound() bool

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

func (*BeegoOutput) IsOk Uses

func (output *BeegoOutput) IsOk() bool

IsOk returns boolean of if this request was ok. HTTP 200 means ok.

func (*BeegoOutput) IsRedirect Uses

func (output *BeegoOutput) IsRedirect() bool

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

func (*BeegoOutput) IsServerError Uses

func (output *BeegoOutput) IsServerError() bool

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

func (*BeegoOutput) IsSuccessful Uses

func (output *BeegoOutput) IsSuccessful() bool

IsSuccessful returns boolean of this request was successful. HTTP 2xx means ok.

func (*BeegoOutput) JSON Uses

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

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

func (*BeegoOutput) JSONP Uses

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

JSONP writes jsonp to the response body.

func (*BeegoOutput) Reset Uses

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

Reset initializes BeegoOutput

func (*BeegoOutput) ServeFormatted Uses

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

ServeFormatted serves YAML, XML or JSON, depending on the value of the Accept header

func (*BeegoOutput) Session Uses

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

Session sets session item value with given key.

func (*BeegoOutput) SetStatus Uses

func (output *BeegoOutput) SetStatus(status int)

SetStatus sets the response status code. Writes response header directly.

func (*BeegoOutput) XML Uses

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

XML writes xml string to the response body.

func (*BeegoOutput) YAML Uses

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

YAML writes yaml to the response body.

type Context Uses

type Context struct {
    Input          *BeegoInput
    Output         *BeegoOutput
    Request        *http.Request
    ResponseWriter *Response
    // contains filtered or unexported fields
}

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

func NewContext Uses

func NewContext() *Context

NewContext return the Context with Input and Output

func (*Context) Abort Uses

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

Abort stops the request. If beego.ErrorMaps exists, panic body.

func (*Context) CheckXSRFCookie Uses

func (ctx *Context) CheckXSRFCookie() bool

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

func (*Context) GetCookie Uses

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

GetCookie gets a cookie from a request for a given key. (Alias of BeegoInput.Cookie)

func (*Context) GetSecureCookie Uses

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

GetSecureCookie gets a secure cookie from a request for a given key.

func (*Context) Redirect Uses

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

Redirect redirects to localurl with http header status code.

func (*Context) RenderMethodResult Uses

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

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

func (*Context) Reset Uses

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

Reset initializes Context, BeegoInput and BeegoOutput

func (*Context) SetCookie Uses

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

SetCookie sets a cookie for a response. (Alias of BeegoOutput.Cookie)

func (*Context) SetSecureCookie Uses

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

SetSecureCookie sets a secure cookie for a response.

func (*Context) WriteString Uses

func (ctx *Context) WriteString(content string)

WriteString writes a string to response body.

func (*Context) XSRFToken Uses

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

XSRFToken creates and returns an xsrf token string

type Renderer Uses

type Renderer interface {
    Render(ctx *Context)
}

Renderer defines a http response renderer

type Response Uses

type Response struct {
    http.ResponseWriter
    Started bool
    Status  int
    Elapsed time.Duration
}

Response is a wrapper for the http.ResponseWriter Started: if true, response was already written to so the other handler will not be executed

func (*Response) CloseNotify Uses

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

CloseNotify http.CloseNotifier

func (*Response) Flush Uses

func (r *Response) Flush()

Flush http.Flusher

func (*Response) Hijack Uses

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

Hijack hijacker for http

func (*Response) Pusher Uses

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

Pusher http.Pusher

func (*Response) Write Uses

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

Write writes the data to the connection as part of a HTTP reply, and sets `Started` to true. Started: if true, the response was already sent

func (*Response) WriteHeader Uses

func (r *Response) WriteHeader(code int)

WriteHeader sends a HTTP response header with status code, and sets `Started` to true.

type StatusCode Uses

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 Uses

func (s StatusCode) Error() string

func (StatusCode) Render Uses

func (s StatusCode) Render(ctx *Context)

Render sets the HTTP status code

Directories

PathSynopsis
param

Package context imports 30 packages (graph) and is imported by 8 packages. Updated 2020-11-30. Refresh now. Tools for package owners.