2016-01-29 09:46:11 +02:00
|
|
|
package fasthttp
|
|
|
|
|
|
|
|
import (
|
2016-02-05 00:40:08 +02:00
|
|
|
"io"
|
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
"github.com/labstack/echo/engine"
|
2016-02-09 08:17:20 +02:00
|
|
|
"github.com/labstack/echo/logger"
|
2016-01-29 09:46:11 +02:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Response struct {
|
2016-02-09 08:17:20 +02:00
|
|
|
context *fasthttp.RequestCtx
|
2016-01-29 09:46:11 +02:00
|
|
|
header engine.Header
|
|
|
|
status int
|
|
|
|
size int64
|
|
|
|
committed bool
|
2016-02-05 00:40:08 +02:00
|
|
|
writer io.Writer
|
2016-02-09 08:17:20 +02:00
|
|
|
logger logger.Logger
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-09 08:17:20 +02:00
|
|
|
func NewResponse(c *fasthttp.RequestCtx, l logger.Logger) *Response {
|
|
|
|
return &Response{
|
|
|
|
context: c,
|
|
|
|
header: &ResponseHeader{c.Response.Header},
|
|
|
|
writer: c,
|
|
|
|
logger: l,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 02:48:03 +02:00
|
|
|
func (r *Response) Object() interface{} {
|
2016-02-09 08:17:20 +02:00
|
|
|
return r.context
|
2016-02-09 02:48:03 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Response) Header() engine.Header {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) WriteHeader(code int) {
|
2016-02-09 08:17:20 +02:00
|
|
|
if r.committed {
|
|
|
|
r.logger.Warn("response already committed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.status = code
|
|
|
|
r.context.SetStatusCode(code)
|
|
|
|
r.committed = true
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Write(b []byte) (int, error) {
|
2016-02-09 08:17:20 +02:00
|
|
|
return r.context.Write(b)
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Committed() bool {
|
|
|
|
return r.committed
|
|
|
|
}
|
2016-02-05 00:40:08 +02:00
|
|
|
|
|
|
|
func (r *Response) SetWriter(w io.Writer) {
|
|
|
|
r.writer = w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Writer() io.Writer {
|
|
|
|
return r.writer
|
|
|
|
}
|