2015-03-27 14:21:30 -07:00
|
|
|
package echo
|
2015-03-01 09:45:13 -08:00
|
|
|
|
|
|
|
import (
|
2015-05-18 17:23:44 -07:00
|
|
|
"bufio"
|
|
|
|
"net"
|
2015-03-01 09:45:13 -08:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2015-05-08 11:21:50 -07:00
|
|
|
Response struct {
|
2015-05-20 14:38:51 -07:00
|
|
|
writer http.ResponseWriter
|
2015-05-15 15:24:47 -07:00
|
|
|
status int
|
|
|
|
size int64
|
2015-03-06 11:12:33 -08:00
|
|
|
committed bool
|
2015-12-01 11:22:45 -08:00
|
|
|
echo *Echo
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-12-01 11:22:45 -08:00
|
|
|
func NewResponse(w http.ResponseWriter, e *Echo) *Response {
|
|
|
|
return &Response{writer: w, echo: e}
|
2015-05-20 14:38:51 -07:00
|
|
|
}
|
|
|
|
|
2015-05-22 04:40:01 -07:00
|
|
|
func (r *Response) SetWriter(w http.ResponseWriter) {
|
|
|
|
r.writer = w
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:50:46 -07:00
|
|
|
func (r *Response) Header() http.Header {
|
|
|
|
return r.writer.Header()
|
|
|
|
}
|
|
|
|
|
2015-05-20 14:38:51 -07:00
|
|
|
func (r *Response) Writer() http.ResponseWriter {
|
|
|
|
return r.writer
|
2015-04-09 11:04:33 -07:00
|
|
|
}
|
|
|
|
|
2015-05-15 12:29:14 -07:00
|
|
|
func (r *Response) WriteHeader(code int) {
|
2015-03-06 11:12:33 -08:00
|
|
|
if r.committed {
|
2015-12-01 11:22:45 -08:00
|
|
|
r.echo.Logger().Warn("response already committed")
|
2015-03-06 11:12:33 -08:00
|
|
|
return
|
|
|
|
}
|
2015-05-15 12:29:14 -07:00
|
|
|
r.status = code
|
2015-05-20 14:38:51 -07:00
|
|
|
r.writer.WriteHeader(code)
|
2015-03-06 11:12:33 -08:00
|
|
|
r.committed = true
|
2015-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
|
2015-05-08 11:21:50 -07:00
|
|
|
func (r *Response) Write(b []byte) (n int, err error) {
|
2015-05-20 14:38:51 -07:00
|
|
|
n, err = r.writer.Write(b)
|
2015-05-15 15:24:47 -07:00
|
|
|
r.size += int64(n)
|
2015-03-01 09:45:13 -08:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:23:44 -07:00
|
|
|
// Flush wraps response writer's Flush function.
|
|
|
|
func (r *Response) Flush() {
|
2015-05-20 14:38:51 -07:00
|
|
|
r.writer.(http.Flusher).Flush()
|
2015-05-18 17:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack wraps response writer's Hijack function.
|
|
|
|
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2015-05-20 14:38:51 -07:00
|
|
|
return r.writer.(http.Hijacker).Hijack()
|
2015-05-18 17:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify wraps response writer's CloseNotify function.
|
|
|
|
func (r *Response) CloseNotify() <-chan bool {
|
2015-05-20 14:38:51 -07:00
|
|
|
return r.writer.(http.CloseNotifier).CloseNotify()
|
2015-05-18 17:23:44 -07:00
|
|
|
}
|
|
|
|
|
2015-05-08 11:21:50 -07:00
|
|
|
func (r *Response) Status() int {
|
2015-03-01 09:45:13 -08:00
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2015-05-15 15:24:47 -07:00
|
|
|
func (r *Response) Size() int64 {
|
2015-03-01 09:45:13 -08:00
|
|
|
return r.size
|
|
|
|
}
|
2015-03-06 11:12:33 -08:00
|
|
|
|
2015-09-13 01:22:19 -07:00
|
|
|
func (r *Response) Committed() bool {
|
|
|
|
return r.committed
|
|
|
|
}
|
|
|
|
|
2015-12-02 08:07:22 -08:00
|
|
|
func (r *Response) reset(w http.ResponseWriter, e *Echo) {
|
2015-05-20 14:38:51 -07:00
|
|
|
r.writer = w
|
2015-05-27 21:11:57 -06:00
|
|
|
r.size = 0
|
2015-05-18 17:23:44 -07:00
|
|
|
r.status = http.StatusOK
|
2015-03-06 11:12:33 -08:00
|
|
|
r.committed = false
|
2015-12-02 08:07:22 -08:00
|
|
|
r.echo = e
|
2015-03-06 11:12:33 -08:00
|
|
|
}
|