diff --git a/context.go b/context.go index 6f2625c0..ea4bc4cf 100644 --- a/context.go +++ b/context.go @@ -16,20 +16,21 @@ import ( "bytes" - netContext "golang.org/x/net/context" + "golang.org/x/net/context" ) type ( // Context represents the context of the current HTTP request. It holds request and // response objects, path, path parameters, data and registered handler. Context interface { - netContext.Context + context.Context - // NetContext returns `http://blog.golang.org/context.Context` interface. - NetContext() netContext.Context + // StdContext returns `net/context.Context`. By default it is set the background + // context. To change the context, use SetStdContext(). + StdContext() context.Context - // SetNetContext sets `http://blog.golang.org/context.Context` interface. - SetNetContext(netContext.Context) + // SetStdContext sets `net/context.Context`. + SetStdContext(context.Context) // Request returns `engine.Request` interface. Request() engine.Request @@ -178,16 +179,16 @@ type ( Reset(engine.Request, engine.Response) } - context struct { - netContext netContext.Context - request engine.Request - response engine.Response - path string - pnames []string - pvalues []string - store store - handler HandlerFunc - echo *Echo + echoContext struct { + context context.Context + request engine.Request + response engine.Response + path string + pnames []string + pvalues []string + store store + handler HandlerFunc + echo *Echo } store map[string]interface{} @@ -197,47 +198,47 @@ const ( indexPage = "index.html" ) -func (c *context) NetContext() netContext.Context { - return c.netContext +func (c *echoContext) StdContext() context.Context { + return c.context } -func (c *context) SetNetContext(ctx netContext.Context) { - c.netContext = ctx +func (c *echoContext) SetStdContext(ctx context.Context) { + c.context = ctx } -func (c *context) Deadline() (deadline time.Time, ok bool) { - return c.netContext.Deadline() +func (c *echoContext) Deadline() (deadline time.Time, ok bool) { + return c.context.Deadline() } -func (c *context) Done() <-chan struct{} { - return c.netContext.Done() +func (c *echoContext) Done() <-chan struct{} { + return c.context.Done() } -func (c *context) Err() error { - return c.netContext.Err() +func (c *echoContext) Err() error { + return c.context.Err() } -func (c *context) Value(key interface{}) interface{} { - return c.netContext.Value(key) +func (c *echoContext) Value(key interface{}) interface{} { + return c.context.Value(key) } -func (c *context) Request() engine.Request { +func (c *echoContext) Request() engine.Request { return c.request } -func (c *context) Response() engine.Response { +func (c *echoContext) Response() engine.Response { return c.response } -func (c *context) Path() string { +func (c *echoContext) Path() string { return c.path } -func (c *context) SetPath(p string) { +func (c *echoContext) SetPath(p string) { c.path = p } -func (c *context) P(i int) (value string) { +func (c *echoContext) P(i int) (value string) { l := len(c.pnames) if i < l { value = c.pvalues[i] @@ -245,7 +246,7 @@ func (c *context) P(i int) (value string) { return } -func (c *context) Param(name string) (value string) { +func (c *echoContext) Param(name string) (value string) { l := len(c.pnames) for i, n := range c.pnames { if n == name && i < l { @@ -256,83 +257,83 @@ func (c *context) Param(name string) (value string) { return } -func (c *context) ParamNames() []string { +func (c *echoContext) ParamNames() []string { return c.pnames } -func (c *context) SetParamNames(names ...string) { +func (c *echoContext) SetParamNames(names ...string) { c.pnames = names } -func (c *context) ParamValues() []string { +func (c *echoContext) ParamValues() []string { return c.pvalues } -func (c *context) SetParamValues(values ...string) { +func (c *echoContext) SetParamValues(values ...string) { c.pvalues = values } -func (c *context) QueryParam(name string) string { +func (c *echoContext) QueryParam(name string) string { return c.request.URL().QueryParam(name) } -func (c *context) QueryParams() map[string][]string { +func (c *echoContext) QueryParams() map[string][]string { return c.request.URL().QueryParams() } -func (c *context) FormValue(name string) string { +func (c *echoContext) FormValue(name string) string { return c.request.FormValue(name) } -func (c *context) FormParams() map[string][]string { +func (c *echoContext) FormParams() map[string][]string { return c.request.FormParams() } -func (c *context) FormFile(name string) (*multipart.FileHeader, error) { +func (c *echoContext) FormFile(name string) (*multipart.FileHeader, error) { return c.request.FormFile(name) } -func (c *context) MultipartForm() (*multipart.Form, error) { +func (c *echoContext) MultipartForm() (*multipart.Form, error) { return c.request.MultipartForm() } -func (c *context) Cookie(name string) (engine.Cookie, error) { +func (c *echoContext) Cookie(name string) (engine.Cookie, error) { return c.request.Cookie(name) } -func (c *context) SetCookie(cookie engine.Cookie) { +func (c *echoContext) SetCookie(cookie engine.Cookie) { c.response.SetCookie(cookie) } -func (c *context) Cookies() []engine.Cookie { +func (c *echoContext) Cookies() []engine.Cookie { return c.request.Cookies() } -func (c *context) Set(key string, val interface{}) { +func (c *echoContext) Set(key string, val interface{}) { if c.store == nil { c.store = make(store) } c.store[key] = val } -func (c *context) Get(key string) interface{} { +func (c *echoContext) Get(key string) interface{} { return c.store[key] } -func (c *context) Del(key string) { +func (c *echoContext) Del(key string) { delete(c.store, key) } -func (c *context) Contains(key string) bool { +func (c *echoContext) Contains(key string) bool { _, ok := c.store[key] return ok } -func (c *context) Bind(i interface{}) error { +func (c *echoContext) Bind(i interface{}) error { return c.echo.binder.Bind(i, c) } -func (c *context) Render(code int, name string, data interface{}) (err error) { +func (c *echoContext) Render(code int, name string, data interface{}) (err error) { if c.echo.renderer == nil { return ErrRendererNotRegistered } @@ -346,21 +347,21 @@ func (c *context) Render(code int, name string, data interface{}) (err error) { return } -func (c *context) HTML(code int, html string) (err error) { +func (c *echoContext) HTML(code int, html string) (err error) { c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8) c.response.WriteHeader(code) _, err = c.response.Write([]byte(html)) return } -func (c *context) String(code int, s string) (err error) { +func (c *echoContext) String(code int, s string) (err error) { c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8) c.response.WriteHeader(code) _, err = c.response.Write([]byte(s)) return } -func (c *context) JSON(code int, i interface{}) (err error) { +func (c *echoContext) JSON(code int, i interface{}) (err error) { b, err := json.Marshal(i) if c.echo.Debug() { b, err = json.MarshalIndent(i, "", " ") @@ -371,14 +372,14 @@ func (c *context) JSON(code int, i interface{}) (err error) { return c.JSONBlob(code, b) } -func (c *context) JSONBlob(code int, b []byte) (err error) { +func (c *echoContext) JSONBlob(code int, b []byte) (err error) { c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8) c.response.WriteHeader(code) _, err = c.response.Write(b) return } -func (c *context) JSONP(code int, callback string, i interface{}) (err error) { +func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error) { b, err := json.Marshal(i) if err != nil { return err @@ -395,7 +396,7 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) { return } -func (c *context) XML(code int, i interface{}) (err error) { +func (c *echoContext) XML(code int, i interface{}) (err error) { b, err := xml.Marshal(i) if c.echo.Debug() { b, err = xml.MarshalIndent(i, "", " ") @@ -406,7 +407,7 @@ func (c *context) XML(code int, i interface{}) (err error) { return c.XMLBlob(code, b) } -func (c *context) XMLBlob(code int, b []byte) (err error) { +func (c *echoContext) XMLBlob(code int, b []byte) (err error) { c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8) c.response.WriteHeader(code) if _, err = c.response.Write([]byte(xml.Header)); err != nil { @@ -416,7 +417,7 @@ func (c *context) XMLBlob(code int, b []byte) (err error) { return } -func (c *context) File(file string) error { +func (c *echoContext) File(file string) error { f, err := os.Open(file) if err != nil { return ErrNotFound @@ -437,7 +438,7 @@ func (c *context) File(file string) error { return c.ServeContent(f, fi.Name(), fi.ModTime()) } -func (c *context) Attachment(r io.ReadSeeker, name string) (err error) { +func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) { c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name)) c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name) c.response.WriteHeader(http.StatusOK) @@ -445,12 +446,12 @@ func (c *context) Attachment(r io.ReadSeeker, name string) (err error) { return } -func (c *context) NoContent(code int) error { +func (c *echoContext) NoContent(code int) error { c.response.WriteHeader(code) return nil } -func (c *context) Redirect(code int, url string) error { +func (c *echoContext) Redirect(code int, url string) error { if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect { return ErrInvalidRedirectCode } @@ -459,27 +460,27 @@ func (c *context) Redirect(code int, url string) error { return nil } -func (c *context) Error(err error) { +func (c *echoContext) Error(err error) { c.echo.httpErrorHandler(err, c) } -func (c *context) Echo() *Echo { +func (c *echoContext) Echo() *Echo { return c.echo } -func (c *context) Handler() HandlerFunc { +func (c *echoContext) Handler() HandlerFunc { return c.handler } -func (c *context) SetHandler(h HandlerFunc) { +func (c *echoContext) SetHandler(h HandlerFunc) { c.handler = h } -func (c *context) Logger() log.Logger { +func (c *echoContext) Logger() log.Logger { return c.echo.logger } -func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error { +func (c *echoContext) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error { req := c.Request() res := c.Response() @@ -506,8 +507,8 @@ func ContentTypeByExtension(name string) (t string) { return } -func (c *context) Reset(req engine.Request, res engine.Response) { - c.netContext = nil +func (c *echoContext) Reset(req engine.Request, res engine.Response) { + c.context = nil c.request = req c.response = res c.store = nil diff --git a/context_test.go b/context_test.go index 964a6f7e..3c8c5926 100644 --- a/context_test.go +++ b/context_test.go @@ -34,7 +34,7 @@ func TestContext(t *testing.T) { e := New() req := test.NewRequest(POST, "/", strings.NewReader(userJSON)) rec := test.NewResponseRecorder() - c := e.NewContext(req, rec).(*context) + c := e.NewContext(req, rec).(*echoContext) // Request assert.NotNil(t, c.Request()) @@ -78,7 +78,7 @@ func TestContext(t *testing.T) { // JSON rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) err = c.JSON(http.StatusOK, user{1, "Jon Snow"}) if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Status()) @@ -88,13 +88,13 @@ func TestContext(t *testing.T) { // JSON (error) rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) err = c.JSON(http.StatusOK, make(chan bool)) assert.Error(t, err) // JSONP rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) callback := "callback" err = c.JSONP(http.StatusOK, callback, user{1, "Jon Snow"}) if assert.NoError(t, err) { @@ -105,7 +105,7 @@ func TestContext(t *testing.T) { // XML rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) err = c.XML(http.StatusOK, user{1, "Jon Snow"}) if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Status()) @@ -115,13 +115,13 @@ func TestContext(t *testing.T) { // XML (error) rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) err = c.XML(http.StatusOK, make(chan bool)) assert.Error(t, err) // String rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) err = c.String(http.StatusOK, "Hello, World!") if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Status()) @@ -131,7 +131,7 @@ func TestContext(t *testing.T) { // HTML rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) err = c.HTML(http.StatusOK, "Hello, World!") if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Status()) @@ -141,7 +141,7 @@ func TestContext(t *testing.T) { // Attachment rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) file, err := os.Open("_fixture/images/walle.png") if assert.NoError(t, err) { err = c.Attachment(file, "walle.png") @@ -154,20 +154,20 @@ func TestContext(t *testing.T) { // NoContent rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) c.NoContent(http.StatusOK) assert.Equal(t, http.StatusOK, rec.Status()) // Redirect rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo")) assert.Equal(t, http.StatusMovedPermanently, rec.Status()) assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation)) // Error rec = test.NewResponseRecorder() - c = e.NewContext(req, rec).(*context) + c = e.NewContext(req, rec).(*echoContext) c.Error(errors.New("error")) assert.Equal(t, http.StatusInternalServerError, rec.Status()) @@ -183,7 +183,7 @@ func TestContextCookie(t *testing.T) { req.Header().Add(HeaderCookie, theme) req.Header().Add(HeaderCookie, user) rec := test.NewResponseRecorder() - c := e.NewContext(req, rec).(*context) + c := e.NewContext(req, rec).(*echoContext) // Read single cookie, err := c.Cookie("theme") diff --git a/echo.go b/echo.go index 738d1e96..07f58b76 100644 --- a/echo.go +++ b/echo.go @@ -48,6 +48,8 @@ import ( "runtime" "sync" + ncontext "golang.org/x/net/context" + "github.com/labstack/echo/engine" "github.com/labstack/echo/log" glog "github.com/labstack/gommon/log" @@ -235,7 +237,8 @@ func New() (e *Echo) { // NewContext returns a Context instance. func (e *Echo) NewContext(req engine.Request, res engine.Response) Context { - return &context{ + return &echoContext{ + context: ncontext.Background(), request: req, response: res, echo: e, @@ -546,7 +549,7 @@ func (e *Echo) PutContext(c Context) { } func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) { - c := e.pool.Get().(*context) + c := e.pool.Get().(*echoContext) c.Reset(req, res) // Middleware diff --git a/router_test.go b/router_test.go index b9f1884c..b099d5e9 100644 --- a/router_test.go +++ b/router_test.go @@ -281,7 +281,7 @@ func TestRouterStatic(t *testing.T) { c.Set("path", path) return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) r.Find(GET, path, c) c.handler(c) assert.Equal(t, path, c.Get("path")) @@ -293,7 +293,7 @@ func TestRouterParam(t *testing.T) { r.Add(GET, "/users/:id", func(c Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/users/1", c) assert.Equal(t, "1", c.P(0)) } @@ -304,7 +304,7 @@ func TestRouterTwoParam(t *testing.T) { r.Add(GET, "/users/:uid/files/:fid", func(Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/users/1/files/1", c) assert.Equal(t, "1", c.P(0)) @@ -324,7 +324,7 @@ func TestRouterParamWithSlash(t *testing.T) { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) assert.NotPanics(t, func() { r.Find(GET, "/a/1/c/d/2/3", c) }) @@ -344,7 +344,7 @@ func TestRouterMatchAny(t *testing.T) { r.Add(GET, "/users/*", func(Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/", c) assert.Equal(t, "", c.P(0)) @@ -362,7 +362,7 @@ func TestRouterMicroParam(t *testing.T) { r.Add(GET, "/:a/:b/:c", func(c Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/1/2/3", c) assert.Equal(t, "1", c.P(0)) assert.Equal(t, "2", c.P(1)) @@ -377,7 +377,7 @@ func TestRouterMixParamMatchAny(t *testing.T) { r.Add(GET, "/users/:id/*", func(c Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/users/joe/comments", c) c.handler(c) @@ -396,7 +396,7 @@ func TestRouterMultiRoute(t *testing.T) { r.Add(GET, "/users/:id", func(c Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) // Route > /users r.Find(GET, "/users", c) @@ -408,7 +408,7 @@ func TestRouterMultiRoute(t *testing.T) { assert.Equal(t, "1", c.P(0)) // Route > /user - c = e.NewContext(nil, nil).(*context) + c = e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/user", c) he := c.handler(c).(*HTTPError) assert.Equal(t, http.StatusNotFound, he.Code) @@ -447,7 +447,7 @@ func TestRouterPriority(t *testing.T) { c.Set("g", 7) return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) // Route > /users r.Find(GET, "/users", c) @@ -490,7 +490,7 @@ func TestRouterPriority(t *testing.T) { func TestRouterPriorityNotFound(t *testing.T) { e := New() r := e.router - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) // Add r.Add(GET, "/a/foo", func(c Context) error { @@ -511,7 +511,7 @@ func TestRouterPriorityNotFound(t *testing.T) { c.handler(c) assert.Equal(t, 2, c.Get("b")) - c = e.NewContext(nil, nil).(*context) + c = e.NewContext(nil, nil).(*echoContext) r.Find(GET, "/abc/def", c) he := c.handler(c).(*HTTPError) assert.Equal(t, http.StatusNotFound, he.Code) @@ -532,7 +532,7 @@ func TestRouterParamNames(t *testing.T) { r.Add(GET, "/users/:uid/files/:fid", func(c Context) error { return nil }, e) - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) // Route > /users r.Find(GET, "/users", c) @@ -561,7 +561,7 @@ func TestRouterAPI(t *testing.T) { return nil }, e) } - c := e.NewContext(nil, nil).(*context) + c := e.NewContext(nil, nil).(*echoContext) for _, route := range api { r.Find(route.Method, route.Path, c) for i, n := range c.pnames {