1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Refactored response

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-09 11:04:33 -07:00
parent 055200818a
commit 8f7c55e0ac
5 changed files with 11 additions and 41 deletions

View File

@ -48,7 +48,7 @@ func (c *Context) Bind(v interface{}) error {
func (c *Context) Render(code int, name string, data interface{}) error { func (c *Context) Render(code int, name string, data interface{}) error {
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8") c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
c.Response.WriteHeader(code) 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. // JSON sends an application/json response with status code.

View File

@ -12,7 +12,7 @@ func TestContext(t *testing.T) {
b, _ := json.Marshal(u1) b, _ := json.Marshal(u1)
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b)) r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
c := &Context{ c := &Context{
Response: &response{ResponseWriter: httptest.NewRecorder()}, Response: &response{writer: httptest.NewRecorder()},
Request: r, Request: r,
params: make(Params, 5), params: make(Params, 5),
store: make(store), store: make(store),

View File

@ -1,62 +1,40 @@
package echo package echo
import ( import (
"bufio"
"errors"
"log" "log"
"net"
"net/http" "net/http"
) )
type ( type (
response struct { response struct {
http.ResponseWriter writer http.ResponseWriter
status int status int
size int size int
committed bool committed bool
} }
) )
func (r *response) Header() http.Header {
return r.writer.Header()
}
func (r *response) WriteHeader(n int) { func (r *response) WriteHeader(n int) {
// TODO: fix when halted.
if r.committed { if r.committed {
// TODO: Warning // TODO: Warning
log.Println("echo: response already committed") log.Println("echo: response already committed")
return return
} }
r.status = n r.status = n
r.ResponseWriter.WriteHeader(n) r.writer.WriteHeader(n)
r.committed = true r.committed = true
} }
func (r *response) Write(b []byte) (n int, err error) { 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 r.size += n
return n, err 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 { func (r *response) Status() int {
return r.status return r.status
} }
@ -66,6 +44,6 @@ func (r *response) Size() int {
} }
func (r *response) reset(w http.ResponseWriter) { func (r *response) reset(w http.ResponseWriter) {
r.ResponseWriter = w r.writer = w
r.committed = false r.committed = false
} }

View File

@ -20,14 +20,6 @@ func TestResponse(t *testing.T) {
if c.Response.Status() != http.StatusOK { if c.Response.Status() != http.StatusOK {
t.Error("size should be 5") 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() w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/hello", nil) r, _ := http.NewRequest("GET", "/hello", nil)

View File

@ -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) { func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h, c, _ := r.Find(req.Method, req.URL.Path) h, c, _ := r.Find(req.Method, req.URL.Path)
c.Response.ResponseWriter = w c.Response.writer = w
if h != nil { if h != nil {
h(c) h(c)
} else { } else {