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