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