Skip to content

Commit

Permalink
closes #669
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Oct 8, 2016
1 parent 9c0e9d6 commit 4dc7716
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 137 deletions.
155 changes: 72 additions & 83 deletions context.go
Expand Up @@ -17,20 +17,18 @@ import (

"bytes"

"github.com/labstack/echo/context"
gcontext "github.com/labstack/echo/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 {
context.Context

// StdContext returns `context.Context`.
StdContext() context.Context
StdContext() gcontext.Context

// SetStdContext sets `context.Context`.
SetStdContext(context.Context)
SetStdContext(gcontext.Context)

// Request returns `engine.Request` interface.
Request() engine.Request
Expand Down Expand Up @@ -187,71 +185,58 @@ type (
Reset(engine.Request, engine.Response)
}

echoContext struct {
context context.Context
request engine.Request
response engine.Response
path string
pnames []string
pvalues []string
handler HandlerFunc
echo *Echo
context struct {
stdContext gcontext.Context
request engine.Request
response engine.Response
path string
pnames []string
pvalues []string
handler HandlerFunc
store store
echo *Echo
}

store map[string]interface{}
)

const (
indexPage = "index.html"
)

func (c *echoContext) StdContext() context.Context {
return c.context
}

func (c *echoContext) SetStdContext(ctx context.Context) {
c.context = ctx
}

func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
return c.context.Deadline()
func (c *context) StdContext() gcontext.Context {
return c.stdContext
}

func (c *echoContext) Done() <-chan struct{} {
return c.context.Done()
func (c *context) SetStdContext(ctx gcontext.Context) {
c.stdContext = ctx
}

func (c *echoContext) Err() error {
return c.context.Err()
}

func (c *echoContext) Value(key interface{}) interface{} {
return c.context.Value(key)
}

func (c *echoContext) Request() engine.Request {
func (c *context) Request() engine.Request {
return c.request
}

func (c *echoContext) Response() engine.Response {
func (c *context) Response() engine.Response {
return c.response
}

func (c *echoContext) Path() string {
func (c *context) Path() string {
return c.path
}

func (c *echoContext) SetPath(p string) {
func (c *context) SetPath(p string) {
c.path = p
}

func (c *echoContext) P(i int) (value string) {
func (c *context) P(i int) (value string) {
l := len(c.pnames)
if i < l {
value = c.pvalues[i]
}
return
}

func (c *echoContext) Param(name string) (value string) {
func (c *context) Param(name string) (value string) {
l := len(c.pnames)
for i, n := range c.pnames {
if n == name && i < l {
Expand All @@ -262,71 +247,74 @@ func (c *echoContext) Param(name string) (value string) {
return
}

func (c *echoContext) ParamNames() []string {
func (c *context) ParamNames() []string {
return c.pnames
}

func (c *echoContext) SetParamNames(names ...string) {
func (c *context) SetParamNames(names ...string) {
c.pnames = names
}

func (c *echoContext) ParamValues() []string {
func (c *context) ParamValues() []string {
return c.pvalues
}

func (c *echoContext) SetParamValues(values ...string) {
func (c *context) SetParamValues(values ...string) {
c.pvalues = values
}

func (c *echoContext) QueryParam(name string) string {
func (c *context) QueryParam(name string) string {
return c.request.URL().QueryParam(name)
}

func (c *echoContext) QueryParams() map[string][]string {
func (c *context) QueryParams() map[string][]string {
return c.request.URL().QueryParams()
}

func (c *echoContext) FormValue(name string) string {
func (c *context) FormValue(name string) string {
return c.request.FormValue(name)
}

func (c *echoContext) FormParams() map[string][]string {
func (c *context) FormParams() map[string][]string {
return c.request.FormParams()
}

func (c *echoContext) FormFile(name string) (*multipart.FileHeader, error) {
func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
return c.request.FormFile(name)
}

func (c *echoContext) MultipartForm() (*multipart.Form, error) {
func (c *context) MultipartForm() (*multipart.Form, error) {
return c.request.MultipartForm()
}

func (c *echoContext) Cookie(name string) (engine.Cookie, error) {
func (c *context) Cookie(name string) (engine.Cookie, error) {
return c.request.Cookie(name)
}

func (c *echoContext) SetCookie(cookie engine.Cookie) {
func (c *context) SetCookie(cookie engine.Cookie) {
c.response.SetCookie(cookie)
}

func (c *echoContext) Cookies() []engine.Cookie {
func (c *context) Cookies() []engine.Cookie {
return c.request.Cookies()
}

func (c *echoContext) Set(key string, val interface{}) {
c.context = context.WithValue(c.context, key, val)
func (c *context) Set(key string, val interface{}) {
if c.store == nil {
c.store = make(store)
}
c.store[key] = val
}

func (c *echoContext) Get(key string) interface{} {
return c.context.Value(key)
func (c *context) Get(key string) interface{} {
return c.store[key]
}

func (c *echoContext) Bind(i interface{}) error {
func (c *context) Bind(i interface{}) error {
return c.echo.binder.Bind(i, c)
}

func (c *echoContext) Render(code int, name string, data interface{}) (err error) {
func (c *context) Render(code int, name string, data interface{}) (err error) {
if c.echo.renderer == nil {
return ErrRendererNotRegistered
}
Expand All @@ -340,21 +328,21 @@ func (c *echoContext) Render(code int, name string, data interface{}) (err error
return
}

func (c *echoContext) HTML(code int, html string) (err error) {
func (c *context) 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 *echoContext) String(code int, s string) (err error) {
func (c *context) 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 *echoContext) JSON(code int, i interface{}) (err error) {
func (c *context) JSON(code int, i interface{}) (err error) {
b, err := json.Marshal(i)
if c.echo.Debug() {
b, err = json.MarshalIndent(i, "", " ")
Expand All @@ -365,19 +353,19 @@ func (c *echoContext) JSON(code int, i interface{}) (err error) {
return c.JSONBlob(code, b)
}

func (c *echoContext) JSONBlob(code int, b []byte) (err error) {
func (c *context) JSONBlob(code int, b []byte) (err error) {
return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b)
}

func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error) {
func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
b, err := json.Marshal(i)
if err != nil {
return err
}
return c.JSONPBlob(code, callback, b)
}

func (c *echoContext) JSONPBlob(code int, callback string, b []byte) (err error) {
func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
c.response.WriteHeader(code)
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
Expand All @@ -390,7 +378,7 @@ func (c *echoContext) JSONPBlob(code int, callback string, b []byte) (err error)
return
}

func (c *echoContext) XML(code int, i interface{}) (err error) {
func (c *context) XML(code int, i interface{}) (err error) {
b, err := xml.Marshal(i)
if c.echo.Debug() {
b, err = xml.MarshalIndent(i, "", " ")
Expand All @@ -401,28 +389,28 @@ func (c *echoContext) XML(code int, i interface{}) (err error) {
return c.XMLBlob(code, b)
}

func (c *echoContext) XMLBlob(code int, b []byte) (err error) {
func (c *context) XMLBlob(code int, b []byte) (err error) {
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
return
}
return c.Blob(code, MIMEApplicationXMLCharsetUTF8, b)
}

func (c *echoContext) Blob(code int, contentType string, b []byte) (err error) {
func (c *context) Blob(code int, contentType string, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, contentType)
c.response.WriteHeader(code)
_, err = c.response.Write(b)
return
}

func (c *echoContext) Stream(code int, contentType string, r io.Reader) (err error) {
func (c *context) Stream(code int, contentType string, r io.Reader) (err error) {
c.response.Header().Set(HeaderContentType, contentType)
c.response.WriteHeader(code)
_, err = io.Copy(c.response, r)
return
}

func (c *echoContext) File(file string) error {
func (c *context) File(file string) error {
f, err := os.Open(file)
if err != nil {
return ErrNotFound
Expand All @@ -443,28 +431,28 @@ func (c *echoContext) File(file string) error {
return c.ServeContent(f, fi.Name(), fi.ModTime())
}

func (c *echoContext) Attachment(r io.ReadSeeker, name string) (err error) {
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
return c.contentDisposition(r, name, "attachment")
}

func (c *echoContext) Inline(r io.ReadSeeker, name string) (err error) {
func (c *context) Inline(r io.ReadSeeker, name string) (err error) {
return c.contentDisposition(r, name, "inline")
}

func (c *echoContext) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) {
func (c *context) contentDisposition(r io.ReadSeeker, name, dispositionType string) (err error) {
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
c.response.Header().Set(HeaderContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, name))
c.response.WriteHeader(http.StatusOK)
_, err = io.Copy(c.response, r)
return
}

func (c *echoContext) NoContent(code int) error {
func (c *context) NoContent(code int) error {
c.response.WriteHeader(code)
return nil
}

func (c *echoContext) Redirect(code int, url string) error {
func (c *context) Redirect(code int, url string) error {
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
return ErrInvalidRedirectCode
}
Expand All @@ -473,27 +461,27 @@ func (c *echoContext) Redirect(code int, url string) error {
return nil
}

func (c *echoContext) Error(err error) {
func (c *context) Error(err error) {
c.echo.httpErrorHandler(err, c)
}

func (c *echoContext) Echo() *Echo {
func (c *context) Echo() *Echo {
return c.echo
}

func (c *echoContext) Handler() HandlerFunc {
func (c *context) Handler() HandlerFunc {
return c.handler
}

func (c *echoContext) SetHandler(h HandlerFunc) {
func (c *context) SetHandler(h HandlerFunc) {
c.handler = h
}

func (c *echoContext) Logger() log.Logger {
func (c *context) Logger() log.Logger {
return c.echo.logger
}

func (c *echoContext) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.Time) error {
req := c.Request()
res := c.Response()

Expand All @@ -520,9 +508,10 @@ func ContentTypeByExtension(name string) (t string) {
return
}

func (c *echoContext) Reset(req engine.Request, res engine.Response) {
c.context = context.Background()
func (c *context) Reset(req engine.Request, res engine.Response) {
c.stdContext = gcontext.Background()
c.request = req
c.response = res
c.store = nil
c.handler = NotFoundHandler
}

0 comments on commit 4dc7716

Please sign in to comment.