2016-01-28 23:46:11 -08:00
|
|
|
package standard
|
|
|
|
|
2016-02-04 14:40:08 -08:00
|
|
|
import (
|
2016-03-10 16:35:20 -08:00
|
|
|
"io"
|
2016-02-04 14:40:08 -08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/engine"
|
2016-03-06 09:52:32 -08:00
|
|
|
"github.com/labstack/gommon/log"
|
2016-02-04 14:40:08 -08:00
|
|
|
)
|
2016-01-28 23:46:11 -08:00
|
|
|
|
|
|
|
type (
|
2016-03-14 19:58:46 -07:00
|
|
|
// Response implements `engine.Response`.
|
2016-01-28 23:46:11 -08:00
|
|
|
Response struct {
|
2016-03-10 12:05:33 -08:00
|
|
|
http.ResponseWriter
|
2016-01-28 23:46:11 -08:00
|
|
|
header engine.Header
|
|
|
|
status int
|
|
|
|
size int64
|
|
|
|
committed bool
|
2016-03-10 16:35:20 -08:00
|
|
|
writer io.Writer
|
|
|
|
logger *log.Logger
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
2016-03-15 16:13:32 -07:00
|
|
|
|
|
|
|
responseAdapter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
writer io.Writer
|
|
|
|
}
|
2016-01-28 23:46:11 -08:00
|
|
|
)
|
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// Header implements `engine.Response#Header` method.
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) Header() engine.Header {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// WriteHeader implements `engine.Response#WriteHeader` method.
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) WriteHeader(code int) {
|
|
|
|
if r.committed {
|
2016-02-04 19:29:49 -08:00
|
|
|
r.logger.Warn("response already committed")
|
2016-01-28 23:46:11 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
r.status = code
|
2016-03-10 12:05:33 -08:00
|
|
|
r.ResponseWriter.WriteHeader(code)
|
2016-01-28 23:46:11 -08:00
|
|
|
r.committed = true
|
|
|
|
}
|
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// Write implements `engine.Response#Write` method.
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) Write(b []byte) (n int, err error) {
|
2016-03-10 16:35:20 -08:00
|
|
|
n, err = r.writer.Write(b)
|
2016-01-28 23:46:11 -08:00
|
|
|
r.size += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// Status implements `engine.Response#Status` method.
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// Size implements `engine.Response#Size` method.
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// Committed implements `engine.Response#Committed` method.
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) Committed() bool {
|
|
|
|
return r.committed
|
|
|
|
}
|
2016-02-04 14:40:08 -08:00
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// Writer implements `engine.Response#Writer` method.
|
2016-03-10 16:35:20 -08:00
|
|
|
func (r *Response) Writer() io.Writer {
|
|
|
|
return r.writer
|
|
|
|
}
|
2016-02-09 08:12:37 -08:00
|
|
|
|
2016-03-14 23:33:16 -07:00
|
|
|
// SetWriter implements `engine.Response#SetWriter` method.
|
2016-03-14 19:58:46 -07:00
|
|
|
func (r *Response) SetWriter(w io.Writer) {
|
|
|
|
r.writer = w
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:40:08 -08:00
|
|
|
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
2016-03-10 12:05:33 -08:00
|
|
|
r.ResponseWriter = w
|
2016-02-04 14:40:08 -08:00
|
|
|
r.header = h
|
|
|
|
r.status = http.StatusOK
|
|
|
|
r.size = 0
|
|
|
|
r.committed = false
|
2016-03-10 16:35:20 -08:00
|
|
|
r.writer = w
|
2016-02-04 14:40:08 -08:00
|
|
|
}
|
2016-03-15 16:13:32 -07:00
|
|
|
|
|
|
|
func (r *responseAdapter) Write(b []byte) (n int, err error) {
|
|
|
|
return r.writer.Write(b)
|
|
|
|
}
|