2016-01-29 09:46:11 +02:00
|
|
|
package standard
|
|
|
|
|
2016-02-05 00:40:08 +02:00
|
|
|
import (
|
2016-03-19 17:38:35 +02:00
|
|
|
"bufio"
|
2016-03-11 02:35:20 +02:00
|
|
|
"io"
|
2016-03-19 17:38:35 +02:00
|
|
|
"net"
|
2016-02-05 00:40:08 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/engine"
|
2016-06-01 05:59:52 +02:00
|
|
|
"github.com/labstack/echo/log"
|
2016-02-05 00:40:08 +02:00
|
|
|
)
|
2016-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
type (
|
2016-03-15 04:58:46 +02:00
|
|
|
// Response implements `engine.Response`.
|
2016-01-29 09:46:11 +02:00
|
|
|
Response struct {
|
2016-03-10 22:05:33 +02:00
|
|
|
http.ResponseWriter
|
2016-06-07 07:27:36 +02:00
|
|
|
adapter *responseAdapter
|
2016-01-29 09:46:11 +02:00
|
|
|
header engine.Header
|
|
|
|
status int
|
|
|
|
size int64
|
|
|
|
committed bool
|
2016-03-11 02:35:20 +02:00
|
|
|
writer io.Writer
|
2016-06-01 05:59:52 +02:00
|
|
|
logger log.Logger
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-03-16 01:13:32 +02:00
|
|
|
|
|
|
|
responseAdapter struct {
|
2016-06-07 07:27:36 +02:00
|
|
|
*Response
|
2016-03-16 01:13:32 +02:00
|
|
|
}
|
2016-01-29 09:46:11 +02:00
|
|
|
)
|
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// NewResponse returns `Response` instance.
|
2016-06-07 07:27:36 +02:00
|
|
|
func NewResponse(w http.ResponseWriter, l log.Logger) (r *Response) {
|
|
|
|
r = &Response{
|
2016-04-17 00:53:27 +02:00
|
|
|
ResponseWriter: w,
|
|
|
|
header: &Header{Header: w.Header()},
|
|
|
|
writer: w,
|
|
|
|
logger: l,
|
2016-04-16 18:15:37 +02:00
|
|
|
}
|
2016-06-07 07:27:36 +02:00
|
|
|
r.adapter = &responseAdapter{Response: r}
|
|
|
|
return
|
2016-04-16 18:15:37 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Header implements `engine.Response#Header` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) Header() engine.Header {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// WriteHeader implements `engine.Response#WriteHeader` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) WriteHeader(code int) {
|
|
|
|
if r.committed {
|
2016-02-05 05:29:49 +02:00
|
|
|
r.logger.Warn("response already committed")
|
2016-01-29 09:46:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
r.status = code
|
2016-03-10 22:05:33 +02:00
|
|
|
r.ResponseWriter.WriteHeader(code)
|
2016-01-29 09:46:11 +02:00
|
|
|
r.committed = true
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Write implements `engine.Response#Write` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) Write(b []byte) (n int, err error) {
|
2016-06-07 07:27:36 +02:00
|
|
|
if !r.committed {
|
2016-06-03 16:56:16 +02:00
|
|
|
r.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2016-03-11 02:35:20 +02:00
|
|
|
n, err = r.writer.Write(b)
|
2016-01-29 09:46:11 +02:00
|
|
|
r.size += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// SetCookie implements `engine.Response#SetCookie` function.
|
|
|
|
func (r *Response) SetCookie(c engine.Cookie) {
|
|
|
|
http.SetCookie(r.ResponseWriter, &http.Cookie{
|
|
|
|
Name: c.Name(),
|
|
|
|
Value: c.Value(),
|
|
|
|
Path: c.Path(),
|
|
|
|
Domain: c.Domain(),
|
|
|
|
Expires: c.Expires(),
|
|
|
|
Secure: c.Secure(),
|
|
|
|
HttpOnly: c.HTTPOnly(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Status implements `engine.Response#Status` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Size implements `engine.Response#Size` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Committed implements `engine.Response#Committed` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) Committed() bool {
|
|
|
|
return r.committed
|
|
|
|
}
|
2016-02-05 00:40:08 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Writer implements `engine.Response#Writer` function.
|
2016-03-11 02:35:20 +02:00
|
|
|
func (r *Response) Writer() io.Writer {
|
|
|
|
return r.writer
|
|
|
|
}
|
2016-02-09 18:12:37 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// SetWriter implements `engine.Response#SetWriter` function.
|
2016-03-15 04:58:46 +02:00
|
|
|
func (r *Response) SetWriter(w io.Writer) {
|
|
|
|
r.writer = w
|
|
|
|
}
|
|
|
|
|
2016-03-19 17:38:35 +02:00
|
|
|
// Flush implements the http.Flusher interface to allow an HTTP handler to flush
|
|
|
|
// buffered data to the client.
|
2016-03-20 00:47:20 +02:00
|
|
|
// See https://golang.org/pkg/net/http/#Flusher
|
2016-03-19 17:38:35 +02:00
|
|
|
func (r *Response) Flush() {
|
|
|
|
r.ResponseWriter.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack implements the http.Hijacker interface to allow an HTTP handler to
|
|
|
|
// take over the connection.
|
2016-03-20 00:47:20 +02:00
|
|
|
// See https://golang.org/pkg/net/http/#Hijacker
|
2016-03-19 17:38:35 +02:00
|
|
|
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return r.ResponseWriter.(http.Hijacker).Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify implements the http.CloseNotifier interface to allow detecting
|
|
|
|
// when the underlying connection has gone away.
|
|
|
|
// This mechanism can be used to cancel long operations on the server if the
|
|
|
|
// client has disconnected before the response is ready.
|
2016-03-20 00:47:20 +02:00
|
|
|
// See https://golang.org/pkg/net/http/#CloseNotifier
|
2016-03-19 17:38:35 +02:00
|
|
|
func (r *Response) CloseNotify() <-chan bool {
|
|
|
|
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
|
|
|
}
|
|
|
|
|
2016-04-01 17:51:18 +02:00
|
|
|
func (r *Response) reset(w http.ResponseWriter, a *responseAdapter, h engine.Header) {
|
2016-06-07 07:27:36 +02:00
|
|
|
r.ResponseWriter = w
|
|
|
|
r.adapter = a
|
2016-02-05 00:40:08 +02:00
|
|
|
r.header = h
|
|
|
|
r.status = http.StatusOK
|
|
|
|
r.size = 0
|
|
|
|
r.committed = false
|
2016-03-11 02:35:20 +02:00
|
|
|
r.writer = w
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
2016-03-16 01:13:32 +02:00
|
|
|
|
2016-06-07 07:27:36 +02:00
|
|
|
func (r *responseAdapter) Header() http.Header {
|
|
|
|
return r.ResponseWriter.Header()
|
2016-03-19 17:38:35 +02:00
|
|
|
}
|
2016-04-01 17:51:18 +02:00
|
|
|
|
2016-06-07 07:27:36 +02:00
|
|
|
func (r *responseAdapter) reset(res *Response) {
|
|
|
|
r.Response = res
|
2016-04-01 17:51:18 +02:00
|
|
|
}
|