From 25c8e2bd3b1227c3a5a6b2b38f7ec711388c5288 Mon Sep 17 00:00:00 2001 From: longxboy Date: Thu, 17 Jun 2021 10:50:33 +0800 Subject: [PATCH] fix wrapper seq (#1073) --- transport/http/context.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/transport/http/context.go b/transport/http/context.go index 4225483f1..c101070b3 100644 --- a/transport/http/context.go +++ b/transport/http/context.go @@ -105,6 +105,7 @@ func (c *wrapper) Returns(v interface{}, err error) error { } return nil } + func (c *wrapper) Result(code int, v interface{}) error { c.w.WriteHeader(code) if err := c.route.srv.enc(&c.w, c.req, v); err != nil { @@ -112,57 +113,67 @@ func (c *wrapper) Result(code int, v interface{}) error { } return nil } + func (c *wrapper) JSON(code int, v interface{}) error { - c.res.WriteHeader(code) c.res.Header().Set("Content-Type", "application/json") + c.res.WriteHeader(code) return json.NewEncoder(c.res).Encode(v) } + func (c *wrapper) XML(code int, v interface{}) error { - c.res.WriteHeader(code) c.res.Header().Set("Content-Type", "application/xml") + c.res.WriteHeader(code) return xml.NewEncoder(c.res).Encode(v) } + func (c *wrapper) String(code int, text string) error { - c.res.WriteHeader(code) c.res.Header().Set("Content-Type", "text/plain") + c.res.WriteHeader(code) c.res.Write([]byte(text)) return nil } + func (c *wrapper) Blob(code int, contentType string, data []byte) error { - c.res.WriteHeader(code) c.res.Header().Set("Content-Type", contentType) + c.res.WriteHeader(code) c.res.Write(data) return nil } + func (c *wrapper) Stream(code int, contentType string, rd io.Reader) error { - c.res.WriteHeader(code) c.res.Header().Set("Content-Type", contentType) + c.res.WriteHeader(code) _, err := io.Copy(c.res, rd) return err } + func (c *wrapper) Reset(res http.ResponseWriter, req *http.Request) { c.w.rest(res) c.res = res c.req = req } + func (c *wrapper) Deadline() (time.Time, bool) { if c.req == nil { return time.Time{}, false } return c.req.Context().Deadline() } + func (c *wrapper) Done() <-chan struct{} { if c.req == nil { return nil } return c.req.Context().Done() } + func (c *wrapper) Err() error { if c.req == nil { return context.Canceled } return c.req.Context().Err() } + func (c *wrapper) Value(key interface{}) interface{} { if c.req == nil { return nil