2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
2015-03-06 21:12:33 +02:00
|
|
|
"log"
|
2015-03-01 19:45:13 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
response struct {
|
|
|
|
http.ResponseWriter
|
2015-03-06 21:12:33 +02:00
|
|
|
status int
|
|
|
|
size int
|
|
|
|
committed bool
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-03-25 17:50:24 +02:00
|
|
|
func (r *response) WriteHeader(n int) {
|
|
|
|
// TODO: fix when halted.
|
2015-03-06 21:12:33 +02:00
|
|
|
if r.committed {
|
|
|
|
// TODO: Warning
|
2015-03-27 23:21:30 +02:00
|
|
|
log.Println("echo: response already committed")
|
2015-03-06 21:12:33 +02:00
|
|
|
return
|
|
|
|
}
|
2015-03-25 17:50:24 +02:00
|
|
|
r.status = n
|
|
|
|
r.ResponseWriter.WriteHeader(n)
|
2015-03-06 21:12:33 +02:00
|
|
|
r.committed = true
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *response) Write(b []byte) (n int, err error) {
|
|
|
|
n, err = r.ResponseWriter.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 {
|
2015-03-30 05:44:55 +02:00
|
|
|
return nil, nil, errors.New("bolt: hijacker interface not supported")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
return h.Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *response) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *response) Size() int {
|
|
|
|
return r.size
|
|
|
|
}
|
2015-03-06 21:12:33 +02:00
|
|
|
|
|
|
|
func (r *response) reset(rw http.ResponseWriter) {
|
|
|
|
r.ResponseWriter = rw
|
|
|
|
r.committed = false
|
|
|
|
}
|