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-06-01 05:59:52 +02:00
|
|
|
"github.com/labstack/echo/log"
|
2016-01-29 09:46:11 +02:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
*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-06-01 05:59:52 +02:00
|
|
|
logger log.Logger
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// NewResponse returns `Response` instance.
|
2016-06-01 05:59:52 +02:00
|
|
|
func NewResponse(c *fasthttp.RequestCtx, l log.Logger) *Response {
|
2016-04-16 18:15:37 +02:00
|
|
|
return &Response{
|
2016-04-17 00:53:27 +02:00
|
|
|
RequestCtx: c,
|
|
|
|
header: &ResponseHeader{ResponseHeader: &c.Response.Header},
|
|
|
|
writer: c,
|
|
|
|
logger: l,
|
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) {
|
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
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Write implements `engine.Response#Write` function.
|
2016-03-16 20:55:35 +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-16 20:55:35 +02:00
|
|
|
n, err = r.writer.Write(b)
|
|
|
|
r.size += int64(n)
|
|
|
|
return
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// SetCookie implements `engine.Response#SetCookie` function.
|
|
|
|
func (r *Response) SetCookie(c engine.Cookie) {
|
|
|
|
cookie := new(fasthttp.Cookie)
|
|
|
|
cookie.SetKey(c.Name())
|
|
|
|
cookie.SetValue(c.Value())
|
|
|
|
cookie.SetPath(c.Path())
|
|
|
|
cookie.SetDomain(c.Domain())
|
|
|
|
cookie.SetExpire(c.Expires())
|
|
|
|
cookie.SetSecure(c.Secure())
|
|
|
|
cookie.SetHTTPOnly(c.HTTPOnly())
|
|
|
|
r.Response.Header.SetCookie(cookie)
|
|
|
|
}
|
|
|
|
|
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-22 05:58:19 +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-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
|
|
|
|
}
|