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"
|
2015-03-06 11:12:33 -08:00
|
|
|
"log"
|
2015-05-18 17:23:44 -07:00
|
|
|
"net"
|
2015-03-01 09:45:13 -08:00
|
|
|
"net/http"
|
2015-04-18 21:46:00 -07:00
|
|
|
|
2015-04-18 22:00:44 -07:00
|
|
|
"github.com/labstack/gommon/color"
|
2015-03-01 09:45:13 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
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-03-01 09:45:13 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-20 14:38:51 -07:00
|
|
|
func NewResponse(w http.ResponseWriter) *Response {
|
|
|
|
return &Response{writer: w}
|
|
|
|
}
|
|
|
|
|
2015-05-08 11:21:50 -07:00
|
|
|
func (r *Response) Header() http.Header {
|
2015-05-20 14:38:51 -07:00
|
|
|
return r.writer.Header()
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:40:01 -07:00
|
|
|
func (r *Response) SetWriter(w http.ResponseWriter) {
|
|
|
|
r.writer = w
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// TODO: Warning
|
2015-05-18 13:38:35 -07:00
|
|
|
log.Printf("echo => %s", color.Yellow("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-05-08 11:21:50 -07:00
|
|
|
func (r *Response) reset(w http.ResponseWriter) {
|
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
|
|
|
|
}
|