context

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + CharsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + CharsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationYAML                  = "application/x-yaml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + CharsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + CharsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + CharsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEOctetStream                      = "application/octet-stream"
)

MIME types

View Source
const (
	HeaderAccept                        = "Accept"
	HeaderAcceptEncoding                = "Accept-Encoding"
	HeaderAllow                         = "Allow"
	HeaderAuthorization                 = "Authorization"
	HeaderContentDisposition            = "Content-Disposition"
	HeaderContentEncoding               = "Content-Encoding"
	HeaderContentLength                 = "Content-Length"
	HeaderContentType                   = "Content-Type"
	HeaderCookie                        = "Cookie"
	HeaderSetCookie                     = "Set-Cookie"
	HeaderIfModifiedSince               = "If-Modified-Since"
	HeaderLastModified                  = "Last-Modified"
	HeaderLocation                      = "Location"
	HeaderUpgrade                       = "Upgrade"
	HeaderVary                          = "Vary"
	HeaderWWWAuthenticate               = "WWW-Authenticate"
	HeaderXRequestedWith                = "X-Requested-With"
	HeaderXForwardedProto               = "X-Forwarded-Proto"
	HeaderXHTTPMethodOverride           = "X-HTTP-Method-Override"
	HeaderXForwardedFor                 = "X-Forwarded-For"
	HeaderXRealIP                       = "X-Real-IP"
	HeaderServer                        = "Server"
	HeaderOrigin                        = "Origin"
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"
	HeaderP3P                           = "P3P"
	HeaderCacheControl                  = "Cache-control"

	// Security
	HeaderStrictTransportSecurity = "Strict-Transport-Security"
	HeaderXContentTypeOptions     = "X-Content-Type-Options"
	HeaderXXSSProtection          = "X-XSS-Protection"
	HeaderXFrameOptions           = "X-Frame-Options"
	HeaderContentSecurityPolicy   = "Content-Security-Policy"
	HeaderXCSRFToken              = "X-CSRF-Token"
)

Headers

View Source
const (
	CONNECT = http.MethodConnect
	DELETE  = http.MethodDelete
	GET     = http.MethodGet
	HEAD    = http.MethodHead
	OPTIONS = http.MethodOptions
	PATCH   = http.MethodPatch
	POST    = http.MethodPost
	PUT     = http.MethodPut
	TRACE   = http.MethodTrace
)
View Source
const (
	CharsetUTF8 = "charset=utf-8"
)

Http define

Variables

Functions

func ConvertMapToStruct

func ConvertMapToStruct(tagName string, ptr interface{}, form map[string][]string) error

convert map to struct

func Query2Number added in v1.8.0

func Query2Number[N cast.Number](ctx *HttpContext, key string, defaultVal string) N

Query2Number Query String to number with default value

Types

type CResponseWriter

type CResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*CResponseWriter) Before

func (rw *CResponseWriter) Before(before func(IResponseWriter))

func (*CResponseWriter) Flush

func (rw *CResponseWriter) Flush()

func (*CResponseWriter) Hijack

func (rw *CResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*CResponseWriter) SetStatus

func (rw *CResponseWriter) SetStatus(code int)

func (*CResponseWriter) Size

func (rw *CResponseWriter) Size() int

func (*CResponseWriter) Status

func (rw *CResponseWriter) Status() int

func (*CResponseWriter) Write

func (rw *CResponseWriter) Write(b []byte) (int, error)

func (*CResponseWriter) WriteHeader

func (rw *CResponseWriter) WriteHeader(s int)

func (*CResponseWriter) WriteHeaderNow

func (w *CResponseWriter) WriteHeaderNow()

func (*CResponseWriter) Written

func (rw *CResponseWriter) Written() bool

type H

type H = map[string]interface{}

type HttpContext

type HttpContext struct {
	Input            *Input
	Output           Output
	RequiredServices dependencyinjection.IServiceProvider

	Result interface{}
	// contains filtered or unexported fields
}

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request, maxRequestSizeMemory int64, sp dependencyinjection.IServiceProvider) *HttpContext

func (*HttpContext) AsciiJSON

func (ctx *HttpContext) AsciiJSON(code int, obj interface{})

AsciiJSON serializes the given struct as JSON into the response body with unicode to ASCII string. It also sets the Content-Type as "application/json".

func (*HttpContext) Bind

func (ctx *HttpContext) Bind(i interface{}) (err error)

Bind BootStrap Binding

func (*HttpContext) BindWith added in v1.6.9

func (ctx *HttpContext) BindWith(i interface{}, bindEnum binding.Binding) (err error)

BindWith Use Binding By Name

func (*HttpContext) BindWithRouteData added in v1.8.0

func (ctx *HttpContext) BindWithRouteData(i interface{}) (err error)

func (*HttpContext) BindWithUri added in v1.7.8

func (ctx *HttpContext) BindWithUri(i interface{}) (err error)

BindWithUri is a special bind

func (*HttpContext) File

func (ctx *HttpContext) File(filepath string)

func (*HttpContext) FileStream

func (ctx *HttpContext) FileStream(code int, bytes []byte)

func (*HttpContext) GetItem

func (ctx *HttpContext) GetItem(key string) interface{}

GetItem Get data in context.

func (*HttpContext) GetSession added in v1.6.2

func (ctx *HttpContext) GetSession() *Session

func (*HttpContext) GetUser

func (ctx *HttpContext) GetUser() map[string]interface{}

Get JWT UserInfo

func (*HttpContext) HTML

func (ctx *HttpContext) HTML(code int, name string, obj interface{})

func (*HttpContext) IndentedJSON

func (ctx *HttpContext) IndentedJSON(code int, obj interface{})

func (*HttpContext) JSON

func (ctx *HttpContext) JSON(code int, obj interface{})

func (*HttpContext) JSONP

func (ctx *HttpContext) JSONP(code int, obj interface{})

func (*HttpContext) ProtoBuf

func (ctx *HttpContext) ProtoBuf(code int, obj interface{})

ProtoBuf serializes the given struct as ProtoBuf into the response body.

func (*HttpContext) PureJSON

func (ctx *HttpContext) PureJSON(code int, obj interface{})

PureJSON serializes the given struct as JSON into the response body. PureJSON, unlike JSON, does not replace special html characters with their unicode entities.

func (*HttpContext) Redirect

func (ctx *HttpContext) Redirect(code int, url string)

Redirect redirects the request

func (*HttpContext) Render

func (ctx *HttpContext) Render(code int, r actionresult.IActionResult)

Render actionresult writes the response headers and calls render.actionresult to render data.

func (*HttpContext) SecureJSON

func (ctx *HttpContext) SecureJSON(code int, obj interface{})

func (*HttpContext) SetItem

func (ctx *HttpContext) SetItem(key string, val interface{})

SetItem Set data in context.

func (*HttpContext) Text

func (ctx *HttpContext) Text(code int, format string, values ...interface{})

String writes the given string into the response body.

func (*HttpContext) XML

func (ctx *HttpContext) XML(code int, obj interface{})

XML serializes the given struct as XML into the response body. It also sets the Content-Type as "application/xml".

func (*HttpContext) YAML

func (ctx *HttpContext) YAML(code int, obj interface{})

YAML serializes the given struct as YAML into the response body.

type IResponseWriter

type IResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 0 if the response has
	// not been written
	Status() int
	// Written returns whether or not the IResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the IResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(IResponseWriter))
}

IResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) IResponseWriter

NewResponseWriter creates a IResponseWriter that wraps an http.ResponseWriter

type ISessionManager added in v1.6.2

type ISessionManager interface {
	GetIDList() []string
	Clear(interface{})
	Load(interface{}) string
	NewSession(sessionId string) string
	Remove(sessionId string)
	SetValue(sessionID string, key string, value interface{})
	GetValue(sessionID string, key string) (interface{}, bool)
	GC()
}

ISessionManager session manager of interface

type Input

type Input struct {
	Request    *http.Request
	RouterData url.Values

	RequestMaxSize int64
	// contains filtered or unexported fields
}

func NewInput

func NewInput(request *http.Request, maxMemory int64) *Input

NewInput return OrangeInput.

func (*Input) Domain

func (input *Input) Domain() string

Domain returns host name. Alias of Host method.

func (*Input) FormFile

func (input *Input) FormFile(key string) (multipart.File, *multipart.FileHeader, error)

FormFile gets file from request.

func (*Input) FullRemoteIP

func (input *Input) FullRemoteIP() string

FullRemoteIP RemoteAddr to an "IP:port" address

func (*Input) GetAllParam

func (input *Input) GetAllParam() url.Values

func (*Input) GetBody added in v1.6.9

func (input *Input) GetBody() []byte

func (*Input) GetCookie

func (input *Input) GetCookie(name string) string

Get Cookie by Name

func (*Input) GetReader

func (input *Input) GetReader() *http.Request

func (*Input) Header

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

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

func (*Input) Host

func (input *Input) Host() string

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

func (*Input) IsAJAX

func (input *Input) IsAJAX() bool

IsAJAX returns if it is a ajax request

func (*Input) IsUpload

func (input *Input) IsUpload() bool

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

func (*Input) IsWebsocket

func (input *Input) IsWebsocket() bool

func (*Input) Method

func (input *Input) Method() string

Get Http Method.

func (*Input) Param

func (input *Input) Param(name string) string

Get Post Param

func (*Input) ParseFormOrMultipartForm

func (input *Input) ParseFormOrMultipartForm(maxMemory int64) error

ParseFormOrMultipartForm parseForm or parseMultiForm based on Content-type

func (*Input) Path

func (input *Input) Path() string

Path returns URL Path string.

func (*Input) Query

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

Query Get Query String By Key GET /path?id=1234&name=Manu&value= c.Query("id") == "1234" c.Query("name") == "Manu" c.Query("value") == "" c.Query("wtf") == ""

func (*Input) QueryDefault

func (input *Input) QueryDefault(key string, defaultval string) string

func (*Input) QueryStrings

func (input *Input) QueryStrings() url.Values

QueryStrings Get queryString

func (*Input) RealIP

func (input *Input) RealIP() string

RealIP returns the first ip from 'X-Forwarded-For' or 'X-Real-IP' header key if not exists data, returns request.RemoteAddr fixed for #164

func (*Input) Referer

func (input *Input) Referer() string

Referer returns request referer.

func (*Input) RemoteIP

func (input *Input) RemoteIP() string

RemoteIP RemoteAddr to an "IP" address

func (*Input) Reset

func (input *Input) Reset(request *http.Request)

Reset init the

func (*Input) SaveFile

func (input *Input) SaveFile(name, saveDir string) (string, error)

SaveFile saves the form file and returns the filename.

func (*Input) Scheme

func (input *Input) Scheme() string

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

func (*Input) Url

func (input *Input) Url() string

Url get request url

func (*Input) UserAgent

func (input *Input) UserAgent() string

UserAgent returns http request UserAgent

type Output

type Output struct {
	Response IResponseWriter
}

func (Output) Error

func (output Output) Error(code int, error string)

Write Error Response.

func (Output) GetWriter

func (output Output) GetWriter() IResponseWriter

func (Output) Header

func (output Output) Header(key, value string)

func (Output) SetCookie

func (output Output) SetCookie(name, value string)

Set Cookie value

func (Output) SetStatus

func (output Output) SetStatus(status int)

func (Output) SetStatusCode

func (output Output) SetStatusCode(status int)

func (Output) SetStatusCodeNow

func (output Output) SetStatusCodeNow()

func (Output) Status

func (output Output) Status() int

func (Output) Write

func (output Output) Write(data []byte) (n int, err error)

Write Byte[] Response.

type Session added in v1.6.2

type Session struct {
	// contains filtered or unexported fields
}

Session current session by http context

func NewSession added in v1.6.2

func NewSession(sessionId string, mgr ISessionManager) *Session

func (Session) GetFloat added in v1.6.2

func (session Session) GetFloat(key string) float32

func (Session) GetFloat64 added in v1.6.2

func (session Session) GetFloat64(key string) float64

func (Session) GetInt added in v1.6.2

func (session Session) GetInt(key string) int

func (Session) GetInt64 added in v1.6.2

func (session Session) GetInt64(key string) int64

func (Session) GetString added in v1.6.2

func (session Session) GetString(key string) string

func (Session) GetValue added in v1.6.2

func (session Session) GetValue(key string) (interface{}, bool)

func (Session) SetValue added in v1.6.2

func (session Session) SetValue(key string, value interface{})

Jump to

Keyboard shortcuts

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