mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Refactored response
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
055200818a
commit
8f7c55e0ac
@ -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.
|
||||
|
@ -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),
|
||||
|
38
response.go
38
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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user