mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
improve cache handler, embracing #2210 too
This commit is contained in:
55
cache/entry/response.go
vendored
55
cache/entry/response.go
vendored
@@ -1,6 +1,9 @@
|
||||
package entry
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Response is the cached response will be send to the clients
|
||||
// its fields set at runtime on each of the non-cached executions
|
||||
@@ -15,11 +18,28 @@ type Response struct {
|
||||
headers http.Header
|
||||
}
|
||||
|
||||
// NewResponse returns a new cached Response.
|
||||
func NewResponse(statusCode int, headers http.Header, body []byte) *Response {
|
||||
r := new(Response)
|
||||
|
||||
r.SetStatusCode(statusCode)
|
||||
r.SetHeaders(headers)
|
||||
r.SetBody(body)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// SetStatusCode sets a valid status code.
|
||||
func (r *Response) SetStatusCode(statusCode int) {
|
||||
if statusCode <= 0 {
|
||||
statusCode = http.StatusOK
|
||||
}
|
||||
|
||||
r.statusCode = statusCode
|
||||
}
|
||||
|
||||
// StatusCode returns a valid status code.
|
||||
func (r *Response) StatusCode() int {
|
||||
if r.statusCode <= 0 {
|
||||
r.statusCode = 200
|
||||
}
|
||||
return r.statusCode
|
||||
}
|
||||
|
||||
@@ -31,12 +51,39 @@ func (r *Response) StatusCode() int {
|
||||
// return r.contentType
|
||||
// }
|
||||
|
||||
// SetHeaders sets a clone of headers of the cached response.
|
||||
func (r *Response) SetHeaders(h http.Header) {
|
||||
r.headers = h.Clone()
|
||||
}
|
||||
|
||||
// Headers returns the total headers of the cached response.
|
||||
func (r *Response) Headers() http.Header {
|
||||
return r.headers
|
||||
}
|
||||
|
||||
// SetBody consumes "b" and sets the body of the cached response.
|
||||
func (r *Response) SetBody(body []byte) {
|
||||
r.body = make([]byte, len(body))
|
||||
copy(r.body, body)
|
||||
}
|
||||
|
||||
// Body returns contents will be served by the cache handler.
|
||||
func (r *Response) Body() []byte {
|
||||
return r.body
|
||||
}
|
||||
|
||||
// Read implements the io.Reader interface.
|
||||
func (r *Response) Read(b []byte) (int, error) {
|
||||
if len(r.body) == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
n := copy(b, r.body)
|
||||
r.body = r.body[n:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Bytes returns a copy of the cached response body.
|
||||
func (r *Response) Bytes() []byte {
|
||||
return append([]byte(nil), r.body...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user