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 (
|
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-03-06 19:52:32 +02:00
|
|
|
logger *log.Logger
|
2016-01-29 09:46:11 +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) {
|
|
|
|
n, err = r.writer.Write(b)
|
|
|
|
r.size += int64(n)
|
|
|
|
return
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|