本文整理汇总了Golang中github.com/cosiner/zerver.Response.Send方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.Send方法的具体用法?Golang Response.Send怎么用?Golang Response.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cosiner/zerver.Response
的用法示例。
在下文中一共展示了Response.Send方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Filter
func (ri *RequestId) Filter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) {
if req.Method() == "GET" {
chain(req, resp)
return
}
reqId := req.Header(ri.HeaderName)
if reqId == "" {
if ri.PassingOnNoId {
chain(req, resp)
} else {
resp.ReportBadRequest()
resp.Send("error", ri.Error)
}
} else {
id := req.RemoteIP() + ":" + reqId
if err := ri.Store.Save(id); err == ErrRequestIDExist {
resp.ReportForbidden()
resp.Send("error", ri.ErrorOverlap)
} else if err != nil {
ri.logger.Warnln(err)
} else {
chain(req, resp)
ri.Store.Remove(id)
}
}
}
示例2: Send
func Send(resp zerver.Response, key string, value interface{}, err error) {
if err != nil {
SendErr(resp, err)
} else {
resp.Send(key, value)
}
}
示例3: Verify
// Verify xsrf token, used as zerver.FilterFunc
//
// The reason not use "Filter" as function name is to prevent the Xsrf from used as both Component and Filter
func (x *Xsrf) Verify(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) {
if x.VerifyFor(req) {
chain(req, resp)
} else {
resp.ReportBadRequest()
resp.Send("error", x.Error)
}
}
示例4: Create
// Create xsrf token, used as zerver.HandleFunc
func (x *Xsrf) Create(req zerver.Request, resp zerver.Response) {
tokBytes, err := x.CreateFor(req)
if err == nil {
resp.ReportServiceUnavailable()
return
}
if req.Method() == "POST" {
resp.ReportCreated()
}
defer x.Pool.Put(tokBytes)
x.logger.Warnln(resp.Send("tokBytes", tokBytes))
}
示例5: SendErr
func SendErr(resp zerver.Response, err error) {
switch err := err.(type) {
case httperrs.Error:
resp.ReportStatus(err.Code())
if err.Code() < int(httperrs.Server) {
OnErrLog(resp.Send(KeyError, err.Error()))
return
}
default:
resp.ReportInternalServerError()
}
Logger.Errorln(err.Error())
}
示例6: Handle
func (h *Handler) Handle(req zerver.Request, resp zerver.Response) {
req.Wrap(func(req *http.Request, shouldClose bool) (r *http.Request, c bool) {
r, c = req, shouldClose
err := req.ParseMultipartForm(h.MaxMemory)
if err != nil {
handle.SendErr(resp, handle.BadRequest(err))
return
}
if req.MultipartForm == nil || req.MultipartForm.File == nil {
handle.SendErr(resp, h.ErrNoFile)
return
}
files, has := req.MultipartForm.File[h.FileKey]
if !has || len(files) == 0 {
handle.SendErr(resp, h.ErrNoFile)
return
}
file := convertFileHandler(files[0])
if !h.isSuffixSupported(filepath.Ext(file.Filename)) {
handle.SendErr(resp, h.ErrNonImage)
return
}
if file.Size() > h.MaxSize {
handle.SendErr(resp, h.ErrTooLarge)
return
}
fd, err := file.Open()
if err != nil {
handle.SendErr(resp, handle.BadRequest(err))
return
}
defer fd.Close()
path, err := h.SaveImage(fd)
if err != nil {
handle.SendErr(resp, err)
return
}
resp.Send(h.PathKey, path)
return
})
}
示例7: SendErr
func SendErr(resp zerver.Response, err error) {
switch err := errors.Unwrap(err).(type) {
case httperrs.Error:
resp.ReportStatus(err.Code())
if err.Code() < int(httperrs.Server) {
if err := resp.Send(KeyError, err.Error()); err != nil {
Logger.Errorln(err.Error())
}
return
}
default:
resp.ReportInternalServerError()
}
Logger.Errorln(err.Error())
}
示例8: SendErr
func SendErr(resp zerver.Response, err error) {
if err == nil {
panic("there is no error occurred")
}
switch e := errors.Unwrap(err).(type) {
case httperrs.Error:
resp.StatusCode(e.Code())
if e.Code() < int(httperrs.Server) {
resp.Send(Error{e.Error()})
return
}
default:
resp.Logger().Error(log.M{"msg": "internal server error", "error": err.Error()})
resp.StatusCode(http.StatusInternalServerError)
}
}
示例9: Create
// Create xsrf token, used as zerver.HandleFunc
func (x *Xsrf) Create(req zerver.Request, resp zerver.Response) {
tokBytes, err := x.CreateFor(req)
if err == nil {
resp.StatusCode(http.StatusServiceUnavailable)
return
}
if req.ReqMethod() == "POST" {
resp.StatusCode(http.StatusCreated)
}
defer x.Pool.Put(tokBytes)
err = resp.Send(Token{string(tokBytes)})
if err != nil {
x.log.Error(log.M{"msg": "send xsrf token", "err": err.Error()})
}
}
示例10: Filter
func (j JSONP) Filter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) {
if req.Method() != "GET" {
chain(req, resp)
return
}
res, _ := req.ResourceMaster().Resource(resource.RES_JSON)
if res == nil {
resp.ReportNotAcceptable()
return
}
callback := req.Param(string(j))
if callback == "" {
resp.ReportBadRequest()
resp.Send("error", "no callback function")
return
}
resp.SetContentType(resource.RES_JSON, res)
_, err := resp.WriteString(callback)
if err != nil {
goto ERROR
}
_, err = resp.WriteString("(")
if err != nil {
goto ERROR
}
chain(req, resp)
_, err = resp.WriteString(")")
if err == nil {
return
}
ERROR:
req.Logger().Warnln(err)
}
示例11: Handle
func (h *Handler) Handle(req zerver.Request, resp zerver.Response) {
req.Wrap(func(requ *http.Request, shouldClose bool) (r *http.Request, c bool) {
r, c = requ, shouldClose
err := requ.ParseMultipartForm(h.MaxMemory)
if err != nil {
handle.SendBadRequest(resp, err)
return
}
if requ.MultipartForm == nil || requ.MultipartForm.File == nil {
handle.SendErr(resp, h.ErrNoFile)
return
}
files, has := requ.MultipartForm.File[h.FileKey]
if !has || len(files) == 0 {
handle.SendErr(resp, h.ErrNoFile)
return
}
if len(h.Params) > 0 {
for _, param := range h.Params {
if vals := requ.MultipartForm.Value[param]; len(vals) != 0 {
req.SetAttr(param, vals[0])
}
}
}
if h.PreChecker != nil {
if err = h.PreChecker(req); err != nil {
handle.SendErr(resp, err)
return
}
}
file := convertFileHandler(files[0])
if !h.isSuffixSupported(filepath.Ext(file.Filename)) {
handle.SendErr(resp, h.ErrNonImage)
return
}
if file.Size() > h.MaxSize {
handle.SendErr(resp, h.ErrTooLarge)
return
}
fd, err := file.Open()
if err != nil {
handle.SendBadRequest(resp, err)
return
}
defer fd.Close()
h.logger.Debugf("upload file: %s, size: %s\n", fd.Filename(), bytesize.ToHuman(uint64(fd.Size())))
path, err := h.SaveImage(fd, req)
if err != nil {
handle.SendErr(resp, err)
return
}
if h.PostDo != nil {
err := h.PostDo(req)
if err != nil {
h.logger.Warnln("PostDo", err)
}
}
resp.SetContentType(resource.RES_JSON, nil)
resp.Send(h.PathKey, path)
return
})
}
示例12: Handle
func (h *Handler) Handle(req zerver.Request, resp zerver.Response) {
req.Wrap(func(requ *http.Request, shouldClose bool) (r *http.Request, c bool) {
r, c = requ, shouldClose
err := requ.ParseMultipartForm(h.MaxMemory)
if err != nil {
handle.SendBadRequest(resp, err)
return
}
if requ.MultipartForm == nil || requ.MultipartForm.File == nil {
handle.SendErr(resp, h.ErrNoFile)
return
}
files, has := requ.MultipartForm.File[h.FileKey]
if !has || len(files) == 0 {
handle.SendErr(resp, h.ErrNoFile)
return
}
if len(h.Params) > 0 {
for _, param := range h.Params {
if vals := requ.MultipartForm.Value[param]; len(vals) != 0 {
req.SetAttr(param, vals[0])
}
}
}
if h.PreChecker != nil {
if err = h.PreChecker(req); err != nil {
handle.SendErr(resp, err)
return
}
}
file := convertFileHandler(files[0])
if !h.isSuffixSupported(filepath.Ext(file.Filename)) {
handle.SendErr(resp, h.ErrNonImage)
return
}
if file.Size() > h.MaxSize {
handle.SendErr(resp, h.ErrTooLarge)
return
}
fd, err := file.Open()
if err != nil {
handle.SendBadRequest(resp, err)
return
}
defer fd.Close()
if h.log.IsDebugEnable() {
h.log.Debug(log.M{"msg": "file upload", "filename": fd.Filename(), "bytes": fd.Size()})
}
path, err := h.SaveImage(fd, req)
if err != nil {
handle.SendErr(resp, err)
return
}
if h.PostDo != nil {
err := h.PostDo(req)
if err != nil {
h.log.Warn(log.M{"msg": "call post do failed", "err": err.Error()})
}
}
resp.Send(Path{path})
return
})
}