2016-02-09 11:43:00 -08:00
|
|
|
// +build !appengine
|
|
|
|
|
2016-01-28 23:46:11 -08:00
|
|
|
package fasthttp
|
|
|
|
|
|
|
|
import (
|
2016-02-04 14:40:08 -08:00
|
|
|
"io"
|
2016-02-21 19:58:19 -08:00
|
|
|
"net/http"
|
2016-02-04 14:40:08 -08:00
|
|
|
|
2016-01-28 23:46:11 -08:00
|
|
|
"github.com/labstack/echo/engine"
|
2016-02-09 17:16:46 -08:00
|
|
|
"github.com/labstack/gommon/log"
|
2016-01-28 23:46:11 -08:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Response struct {
|
2016-03-10 12:05:33 -08:00
|
|
|
*fasthttp.RequestCtx
|
2016-01-28 23:46:11 -08:00
|
|
|
header engine.Header
|
|
|
|
status int
|
|
|
|
size int64
|
|
|
|
committed bool
|
2016-02-04 14:40:08 -08:00
|
|
|
writer io.Writer
|
2016-03-06 09:52:32 -08:00
|
|
|
logger *log.Logger
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-10 16:51:43 -08:00
|
|
|
func NewResponse(c *fasthttp.RequestCtx) *Response {
|
2016-02-08 22:17:20 -08:00
|
|
|
return &Response{
|
2016-03-10 12:05:33 -08:00
|
|
|
RequestCtx: c,
|
2016-03-13 10:03:39 -07:00
|
|
|
header: &ResponseHeader{&c.Response.Header},
|
2016-03-10 12:05:33 -08:00
|
|
|
writer: c,
|
|
|
|
logger: log.New("test"),
|
2016-02-08 22:17:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 23:46:11 -08:00
|
|
|
func (r *Response) Header() engine.Header {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) WriteHeader(code int) {
|
2016-02-08 22:17:20 -08:00
|
|
|
if r.committed {
|
|
|
|
r.logger.Warn("response already committed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.status = code
|
2016-03-10 12:05:33 -08:00
|
|
|
r.SetStatusCode(code)
|
2016-02-08 22:17:20 -08:00
|
|
|
r.committed = true
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Write(b []byte) (int, error) {
|
2016-03-10 12:05:33 -08:00
|
|
|
return r.RequestCtx.Write(b)
|
2016-01-28 23:46:11 -08: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-04 14:40:08 -08:00
|
|
|
|
2016-03-10 16:35:20 -08:00
|
|
|
func (r *Response) SetWriter(w io.Writer) {
|
|
|
|
r.writer = w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Writer() io.Writer {
|
|
|
|
return r.writer
|
|
|
|
}
|
2016-02-21 19:58:19 -08:00
|
|
|
|
|
|
|
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
|
2016-03-10 12:05:33 -08:00
|
|
|
r.RequestCtx = c
|
2016-02-21 19:58:19 -08:00
|
|
|
r.header = h
|
|
|
|
r.status = http.StatusOK
|
|
|
|
r.size = 0
|
|
|
|
r.committed = false
|
|
|
|
r.writer = c
|
|
|
|
}
|