context

package
v0.0.0-...-d032931 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2018 License: GPL-3.0 Imports: 24 Imported by: 1

Documentation

Overview

Usage:

import "github.com/CloudWise-OpenSource/GoCrab/Core/context"

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

Index

Constants

This section is empty.

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

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 Context

type Context struct {
	Input          *GoCrabInput
	Output         *GoCrabOutput
	Request        *http.Request
	ResponseWriter http.ResponseWriter
}

Http request context struct including GoCrabInput, GoCrabOutput, http.Request and http.ResponseWriter. GoCrabInput and GoCrabOutput provides some api to operate request and response more easily.

func (*Context) Abort

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

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

func (*Context) GetCookie

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

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

func (*Context) GetSecureCookie

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

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. It sends http response header directly.

func (*Context) SetCookie

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

Set cookie for response. It's alias of GoCrabOutput.Cookie.

func (*Context) SetSecureCookie

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

Set Secure cookie for response.

func (*Context) WriteString

func (ctx *Context) WriteString(content string)

Write string to response body. it sends response body.

type GoCrabInput

type GoCrabInput struct {
	Params        map[string]string
	Data          map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
	Request       *http.Request
	RequestBody   []byte
	RunController reflect.Type
	RunMethod     string
}

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

func NewInput

func NewInput(req *http.Request) *GoCrabInput

NewInput return GoCrabInput generated by http.Request.

func (*GoCrabInput) Bind

func (input *GoCrabInput) 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=Neeke var id int GoCrabInput.Bind(&id, "id") id ==123 var isok bool GoCrabInput.Bind(&isok, "isok") id ==true var ft float64 GoCrabInput.Bind(&ft, "ft") ft ==1.2 ol := make([]int, 0, 2) GoCrabInput.Bind(&ol, "ol") ol ==[1 2] ul := make([]string, 0, 2) GoCrabInput.Bind(&ul, "ul") ul ==[str array] user struct{Name} GoCrabInput.Bind(&user, "user") user == {Name:"Neeke"}

func (*GoCrabInput) Cookie

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

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

func (*GoCrabInput) CopyBody

func (input *GoCrabInput) CopyBody() []byte

CopyBody returns the raw request body data as bytes.

func (*GoCrabInput) Domain

func (input *GoCrabInput) Domain() string

Domain returns host name. Alias of Host method.

func (*GoCrabInput) GetData

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

GetData returns the stored data in this context.

func (*GoCrabInput) Header

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

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

func (*GoCrabInput) Host

func (input *GoCrabInput) Host() string

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

func (*GoCrabInput) IP

func (input *GoCrabInput) IP() string

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

func (*GoCrabInput) Is

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

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

func (*GoCrabInput) IsAjax

func (input *GoCrabInput) IsAjax() bool

IsAjax returns boolean of this request is generated by ajax.

func (*GoCrabInput) IsDelete

func (input *GoCrabInput) IsDelete() bool

Is this a DELETE method request?

func (*GoCrabInput) IsGet

func (input *GoCrabInput) IsGet() bool

Is this a GET method request?

func (*GoCrabInput) IsHead

func (input *GoCrabInput) IsHead() bool

Is this a Head method request?

func (*GoCrabInput) IsOptions

func (input *GoCrabInput) IsOptions() bool

Is this a OPTIONS method request?

func (*GoCrabInput) IsPatch

func (input *GoCrabInput) IsPatch() bool

Is this a PATCH method request?

func (*GoCrabInput) IsPost

func (input *GoCrabInput) IsPost() bool

Is this a POST method request?

func (*GoCrabInput) IsPut

func (input *GoCrabInput) IsPut() bool

Is this a PUT method request?

func (*GoCrabInput) IsSecure

func (input *GoCrabInput) IsSecure() bool

IsSecure returns boolean of this request is in https.

func (*GoCrabInput) IsUpload

func (input *GoCrabInput) IsUpload() bool

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

func (*GoCrabInput) IsWebsocket

func (input *GoCrabInput) IsWebsocket() bool

IsWebsocket returns boolean of this request is in webSocket.

func (*GoCrabInput) Method

func (input *GoCrabInput) Method() string

Method returns http request method.

func (*GoCrabInput) Param

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

Param returns router param by a given key.

func (*GoCrabInput) ParseFormOrMulitForm

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

parseForm or parseMultiForm based on Content-type

func (*GoCrabInput) Port

func (input *GoCrabInput) Port() int

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

func (*GoCrabInput) Protocol

func (input *GoCrabInput) Protocol() string

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

func (*GoCrabInput) Proxy

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

Proxy returns proxy client ips slice.

func (*GoCrabInput) Query

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

Query returns input data item string by a given string.

func (*GoCrabInput) Refer

func (input *GoCrabInput) Refer() string

Refer returns http referer header.

func (*GoCrabInput) Referer

func (input *GoCrabInput) Referer() string

Referer returns http referer header.

func (*GoCrabInput) Scheme

func (input *GoCrabInput) Scheme() string

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

func (*GoCrabInput) SetData

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

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

func (*GoCrabInput) Site

func (input *GoCrabInput) Site() string

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

func (*GoCrabInput) SubDomains

func (input *GoCrabInput) SubDomains() string

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

func (*GoCrabInput) Uri

func (input *GoCrabInput) Uri() string

Uri returns full request url with query string, fragment.

func (*GoCrabInput) Url

func (input *GoCrabInput) Url() string

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

func (*GoCrabInput) UserAgent

func (input *GoCrabInput) UserAgent() string

UserAgent returns request client user agent string.

type GoCrabOutput

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

GoCrabOutput does work for sending response header.

func NewOutput

func NewOutput() *GoCrabOutput

NewOutput returns new GoCrabOutput. it contains nothing now.

func (*GoCrabOutput) Body

func (output *GoCrabOutput) Body(content []byte)

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

func (*GoCrabOutput) ContentType

func (output *GoCrabOutput) ContentType(ext string)

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

func (*GoCrabOutput) Cookie

func (output *GoCrabOutput) 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 (*GoCrabOutput) Download

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

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

func (*GoCrabOutput) Header

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

Header sets response header item string via given key.

func (*GoCrabOutput) IsCachable

func (output *GoCrabOutput) IsCachable(status int) bool

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

func (*GoCrabOutput) IsClientError

func (output *GoCrabOutput) IsClientError(status int) bool

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

func (*GoCrabOutput) IsEmpty

func (output *GoCrabOutput) IsEmpty(status int) bool

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

func (*GoCrabOutput) IsForbidden

func (output *GoCrabOutput) IsForbidden(status int) bool

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

func (*GoCrabOutput) IsNotFound

func (output *GoCrabOutput) IsNotFound(status int) bool

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

func (*GoCrabOutput) IsOk

func (output *GoCrabOutput) IsOk(status int) bool

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

func (*GoCrabOutput) IsRedirect

func (output *GoCrabOutput) IsRedirect(status int) bool

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

func (*GoCrabOutput) IsServerError

func (output *GoCrabOutput) IsServerError(status int) bool

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

func (*GoCrabOutput) IsSuccessful

func (output *GoCrabOutput) IsSuccessful(status int) bool

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

func (*GoCrabOutput) Json

func (output *GoCrabOutput) Json(data interface{}, hasIndent bool, coding bool) error

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

func (*GoCrabOutput) Jsonp

func (output *GoCrabOutput) Jsonp(data interface{}, hasIndent bool) error

Jsonp writes jsonp to response body.

func (*GoCrabOutput) SetStatus

func (output *GoCrabOutput) SetStatus(status int)

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

func (*GoCrabOutput) Xml

func (output *GoCrabOutput) Xml(data interface{}, hasIndent bool) error

Xml writes xml string to response body.

Jump to

Keyboard shortcuts

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