From 8f7c55e0ac94ac9dc5f0617ef607642a4afe5d3d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 9 Apr 2015 11:04:33 -0700 Subject: [PATCH] Refactored response Signed-off-by: Vishal Rana --- context.go | 2 +- context_test.go | 2 +- response.go | 38 ++++++++------------------------------ response_test.go | 8 -------- router.go | 2 +- 5 files changed, 11 insertions(+), 41 deletions(-) diff --git a/context.go b/context.go index f559e71f..38393e27 100644 --- a/context.go +++ b/context.go @@ -48,7 +48,7 @@ func (c *Context) Bind(v interface{}) error { func (c *Context) Render(code int, name string, data interface{}) error { c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8") c.Response.WriteHeader(code) - return c.echo.renderFunc(c.Response.ResponseWriter, name, data) + return c.echo.renderFunc(c.Response, name, data) } // JSON sends an application/json response with status code. diff --git a/context_test.go b/context_test.go index 4b32a58b..ba92f4b2 100644 --- a/context_test.go +++ b/context_test.go @@ -12,7 +12,7 @@ func TestContext(t *testing.T) { b, _ := json.Marshal(u1) r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b)) c := &Context{ - Response: &response{ResponseWriter: httptest.NewRecorder()}, + Response: &response{writer: httptest.NewRecorder()}, Request: r, params: make(Params, 5), store: make(store), diff --git a/response.go b/response.go index 38564613..92d06f5d 100644 --- a/response.go +++ b/response.go @@ -1,62 +1,40 @@ package echo import ( - "bufio" - "errors" "log" - "net" "net/http" ) type ( response struct { - http.ResponseWriter + writer http.ResponseWriter status int size int committed bool } ) +func (r *response) Header() http.Header { + return r.writer.Header() +} + func (r *response) WriteHeader(n int) { - // TODO: fix when halted. if r.committed { // TODO: Warning log.Println("echo: response already committed") return } r.status = n - r.ResponseWriter.WriteHeader(n) + r.writer.WriteHeader(n) r.committed = true } func (r *response) Write(b []byte) (n int, err error) { - n, err = r.ResponseWriter.Write(b) + n, err = r.writer.Write(b) r.size += n return n, err } -func (r *response) CloseNotify() <-chan bool { - cn, ok := r.ResponseWriter.(http.CloseNotifier) - if !ok { - return nil - } - return cn.CloseNotify() -} - -func (r *response) Flusher() { - if f, ok := r.ResponseWriter.(http.Flusher); ok { - f.Flush() - } -} - -func (r *response) Hijack() (net.Conn, *bufio.ReadWriter, error) { - h, ok := r.ResponseWriter.(http.Hijacker) - if !ok { - return nil, nil, errors.New("echo: hijacker interface not supported") - } - return h.Hijack() -} - func (r *response) Status() int { return r.status } @@ -66,6 +44,6 @@ func (r *response) Size() int { } func (r *response) reset(w http.ResponseWriter) { - r.ResponseWriter = w + r.writer = w r.committed = false } diff --git a/response_test.go b/response_test.go index 3bbae717..2d7a8831 100644 --- a/response_test.go +++ b/response_test.go @@ -20,14 +20,6 @@ func TestResponse(t *testing.T) { if c.Response.Status() != http.StatusOK { t.Error("size should be 5") } - - // TODO: fix us later - c.Response.CloseNotify() - c.Response.Flusher() - c.Response.Hijack() - - // Reset - c.Response.reset(c.Response.ResponseWriter) }) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/hello", nil) diff --git a/router.go b/router.go index 230ab166..e2839247 100644 --- a/router.go +++ b/router.go @@ -216,7 +216,7 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) { h, c, _ := r.Find(req.Method, req.URL.Path) - c.Response.ResponseWriter = w + c.Response.writer = w if h != nil { h(c) } else {