Skip to content

Commit 8f7c55e

Browse files
committed
Refactored response
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 0552008 commit 8f7c55e

File tree

5 files changed

+11
-41
lines changed

5 files changed

+11
-41
lines changed

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *Context) Bind(v interface{}) error {
4848
func (c *Context) Render(code int, name string, data interface{}) error {
4949
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
5050
c.Response.WriteHeader(code)
51-
return c.echo.renderFunc(c.Response.ResponseWriter, name, data)
51+
return c.echo.renderFunc(c.Response, name, data)
5252
}
5353

5454
// JSON sends an application/json response with status code.

context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestContext(t *testing.T) {
1212
b, _ := json.Marshal(u1)
1313
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
1414
c := &Context{
15-
Response: &response{ResponseWriter: httptest.NewRecorder()},
15+
Response: &response{writer: httptest.NewRecorder()},
1616
Request: r,
1717
params: make(Params, 5),
1818
store: make(store),

response.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,40 @@
11
package echo
22

33
import (
4-
"bufio"
5-
"errors"
64
"log"
7-
"net"
85
"net/http"
96
)
107

118
type (
129
response struct {
13-
http.ResponseWriter
10+
writer http.ResponseWriter
1411
status int
1512
size int
1613
committed bool
1714
}
1815
)
1916

17+
func (r *response) Header() http.Header {
18+
return r.writer.Header()
19+
}
20+
2021
func (r *response) WriteHeader(n int) {
21-
// TODO: fix when halted.
2222
if r.committed {
2323
// TODO: Warning
2424
log.Println("echo: response already committed")
2525
return
2626
}
2727
r.status = n
28-
r.ResponseWriter.WriteHeader(n)
28+
r.writer.WriteHeader(n)
2929
r.committed = true
3030
}
3131

3232
func (r *response) Write(b []byte) (n int, err error) {
33-
n, err = r.ResponseWriter.Write(b)
33+
n, err = r.writer.Write(b)
3434
r.size += n
3535
return n, err
3636
}
3737

38-
func (r *response) CloseNotify() <-chan bool {
39-
cn, ok := r.ResponseWriter.(http.CloseNotifier)
40-
if !ok {
41-
return nil
42-
}
43-
return cn.CloseNotify()
44-
}
45-
46-
func (r *response) Flusher() {
47-
if f, ok := r.ResponseWriter.(http.Flusher); ok {
48-
f.Flush()
49-
}
50-
}
51-
52-
func (r *response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
53-
h, ok := r.ResponseWriter.(http.Hijacker)
54-
if !ok {
55-
return nil, nil, errors.New("echo: hijacker interface not supported")
56-
}
57-
return h.Hijack()
58-
}
59-
6038
func (r *response) Status() int {
6139
return r.status
6240
}
@@ -66,6 +44,6 @@ func (r *response) Size() int {
6644
}
6745

6846
func (r *response) reset(w http.ResponseWriter) {
69-
r.ResponseWriter = w
47+
r.writer = w
7048
r.committed = false
7149
}

response_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ func TestResponse(t *testing.T) {
2020
if c.Response.Status() != http.StatusOK {
2121
t.Error("size should be 5")
2222
}
23-
24-
// TODO: fix us later
25-
c.Response.CloseNotify()
26-
c.Response.Flusher()
27-
c.Response.Hijack()
28-
29-
// Reset
30-
c.Response.reset(c.Response.ResponseWriter)
3123
})
3224
w := httptest.NewRecorder()
3325
r, _ := http.NewRequest("GET", "/hello", nil)

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
216216

217217
func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
218218
h, c, _ := r.Find(req.Method, req.URL.Path)
219-
c.Response.ResponseWriter = w
219+
c.Response.writer = w
220220
if h != nil {
221221
h(c)
222222
} else {

0 commit comments

Comments
 (0)