mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
5e4b63acb2
# FAQ ### Looking for free support? http://support.iris-go.com https://kataras.rocket.chat/channel/iris ### Looking for previous versions? https://github.com/kataras/iris#version ### Should I upgrade my Iris? Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready. > Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`. For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework). ### About our new home page http://iris-go.com Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome! [Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him. The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please! Read more at https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
261 lines
8.0 KiB
Go
261 lines
8.0 KiB
Go
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package context
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"sync"
|
|
)
|
|
|
|
// Recorder the middleware to enable response writer recording ( ResponseWriter -> ResponseRecorder)
|
|
var Recorder = func(ctx Context) {
|
|
ctx.Record()
|
|
ctx.Next()
|
|
}
|
|
|
|
var rrpool = sync.Pool{New: func() interface{} { return &ResponseRecorder{} }}
|
|
|
|
// AcquireResponseRecorder returns a new *AcquireResponseRecorder from the pool.
|
|
// Releasing is done automatically when request and response is done.
|
|
func AcquireResponseRecorder() *ResponseRecorder {
|
|
return rrpool.Get().(*ResponseRecorder)
|
|
}
|
|
|
|
func releaseResponseRecorder(w *ResponseRecorder) {
|
|
rrpool.Put(w)
|
|
}
|
|
|
|
type ResponseRecorder struct {
|
|
ResponseWriter
|
|
// keep track of the body in order to be
|
|
// resetable and useful inside custom transactions
|
|
chunks []byte
|
|
// the saved headers
|
|
headers http.Header
|
|
}
|
|
|
|
var _ ResponseWriter = &ResponseRecorder{}
|
|
|
|
func (w *ResponseRecorder) BeginRecord(underline ResponseWriter) {
|
|
w.ResponseWriter = underline
|
|
w.headers = underline.Header()
|
|
w.ResetBody()
|
|
}
|
|
|
|
func (w *ResponseRecorder) EndResponse() {
|
|
releaseResponseRecorder(w)
|
|
w.ResponseWriter.EndResponse()
|
|
}
|
|
|
|
// Write Adds the contents to the body reply, it writes the contents temporarily
|
|
// to a value in order to be flushed at the end of the request,
|
|
// this method give us the opportunity to reset the body if needed.
|
|
//
|
|
// If WriteHeader has not yet been called, Write calls
|
|
// WriteHeader(http.StatusOK) before writing the data. If the Header
|
|
// does not contain a Content-Type line, Write adds a Content-Type set
|
|
// to the result of passing the initial 512 bytes of written data to
|
|
// DetectContentType.
|
|
//
|
|
// Depending on the HTTP protocol version and the client, calling
|
|
// Write or WriteHeader may prevent future reads on the
|
|
// Request.Body. For HTTP/1.x requests, handlers should read any
|
|
// needed request body data before writing the response. Once the
|
|
// headers have been flushed (due to either an explicit Flusher.Flush
|
|
// call or writing enough data to trigger a flush), the request body
|
|
// may be unavailable. For HTTP/2 requests, the Go HTTP server permits
|
|
// handlers to continue to read the request body while concurrently
|
|
// writing the response. However, such behavior may not be supported
|
|
// by all HTTP/2 clients. Handlers should read before writing if
|
|
// possible to maximize compatibility.
|
|
func (w *ResponseRecorder) Write(contents []byte) (int, error) {
|
|
w.chunks = append(w.chunks, contents...)
|
|
return len(w.chunks), nil
|
|
}
|
|
|
|
// Writef formats according to a format specifier and writes to the response.
|
|
//
|
|
// Returns the number of bytes written and any write error encountered.
|
|
func (w *ResponseRecorder) Writef(format string, a ...interface{}) (n int, err error) {
|
|
return fmt.Fprintf(w, format, a...)
|
|
}
|
|
|
|
// WriteString writes a simple string to the response.
|
|
//
|
|
// Returns the number of bytes written and any write error encountered
|
|
func (w *ResponseRecorder) WriteString(s string) (n int, err error) {
|
|
return w.Write([]byte(s))
|
|
}
|
|
|
|
// SetBody overrides the body and sets it to a slice of bytes value.
|
|
func (w *ResponseRecorder) SetBody(b []byte) {
|
|
w.chunks = b
|
|
}
|
|
|
|
// SetBodyString overrides the body and sets it to a string value.
|
|
func (w *ResponseRecorder) SetBodyString(s string) {
|
|
w.SetBody([]byte(s))
|
|
}
|
|
|
|
// Body returns the body tracked from the writer so far
|
|
// do not use this for edit.
|
|
func (w *ResponseRecorder) Body() []byte {
|
|
return w.chunks
|
|
}
|
|
|
|
// ResetBody resets the response body.
|
|
func (w *ResponseRecorder) ResetBody() {
|
|
w.chunks = w.chunks[0:0]
|
|
}
|
|
|
|
// ResetHeaders sets the headers to the underline's response writer's headers, may empty.
|
|
func (w *ResponseRecorder) ResetHeaders() {
|
|
w.headers = w.ResponseWriter.Header()
|
|
}
|
|
|
|
// ClearHeaders clears all headers, both temp and underline's response writer.
|
|
func (w *ResponseRecorder) ClearHeaders() {
|
|
w.headers = http.Header{}
|
|
h := w.ResponseWriter.Header()
|
|
for k := range h {
|
|
h[k] = nil
|
|
}
|
|
}
|
|
|
|
// Reset resets the response body, headers and the status code header.
|
|
func (w *ResponseRecorder) Reset() {
|
|
w.ClearHeaders()
|
|
w.WriteHeader(defaultStatusCode)
|
|
w.ResetBody()
|
|
}
|
|
|
|
// FlushResponse the full body, headers and status code to the underline response writer
|
|
// called automatically at the end of each request.
|
|
func (w *ResponseRecorder) FlushResponse() {
|
|
// copy the headers to the underline response writer
|
|
if w.headers != nil {
|
|
h := w.ResponseWriter.Header()
|
|
|
|
for k, values := range w.headers {
|
|
h[k] = nil
|
|
for i := range values {
|
|
h.Add(k, values[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
// NOTE: before the ResponseWriter.Write in order to:
|
|
// set the given status code even if the body is empty.
|
|
w.ResponseWriter.FlushResponse()
|
|
|
|
if len(w.chunks) > 0 {
|
|
// ignore error
|
|
w.ResponseWriter.Write(w.chunks)
|
|
}
|
|
}
|
|
|
|
// Clone returns a clone of this response writer
|
|
// it copies the header, status code, headers and the beforeFlush finally returns a new ResponseRecorder
|
|
func (w *ResponseRecorder) Clone() ResponseWriter {
|
|
wc := &ResponseRecorder{}
|
|
wc.headers = w.headers
|
|
wc.chunks = w.chunks[0:]
|
|
if resW, ok := w.ResponseWriter.(*responseWriter); ok {
|
|
wc.ResponseWriter = &(*resW) // clone it
|
|
} else { // else just copy, may pointer, developer can change its behavior
|
|
wc.ResponseWriter = w.ResponseWriter
|
|
}
|
|
|
|
return wc
|
|
}
|
|
|
|
// WriteTo writes a response writer (temp: status code, headers and body) to another response writer
|
|
func (w *ResponseRecorder) WriteTo(res ResponseWriter) {
|
|
|
|
if to, ok := res.(*ResponseRecorder); ok {
|
|
|
|
// set the status code, to is first ( probably an error >=400)
|
|
if statusCode := w.ResponseWriter.StatusCode(); statusCode == defaultStatusCode {
|
|
to.WriteHeader(statusCode)
|
|
}
|
|
|
|
if beforeFlush := w.ResponseWriter.GetBeforeFlush(); beforeFlush != nil {
|
|
// if to had a before flush, lets combine them
|
|
if to.GetBeforeFlush() != nil {
|
|
nextBeforeFlush := beforeFlush
|
|
prevBeforeFlush := to.GetBeforeFlush()
|
|
to.SetBeforeFlush(func() {
|
|
prevBeforeFlush()
|
|
nextBeforeFlush()
|
|
})
|
|
} else {
|
|
to.SetBeforeFlush(w.ResponseWriter.GetBeforeFlush())
|
|
}
|
|
}
|
|
|
|
// if "to" is *responseWriter and it never written before (if -1),
|
|
// set the "w"'s written length.
|
|
if resW, ok := to.ResponseWriter.(*responseWriter); ok {
|
|
if resW.Written() != StatusCodeWritten {
|
|
resW.written = w.ResponseWriter.Written()
|
|
}
|
|
}
|
|
|
|
// append the headers
|
|
if w.headers != nil {
|
|
for k, values := range w.headers {
|
|
for _, v := range values {
|
|
if to.headers.Get(v) == "" {
|
|
to.headers.Add(k, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// append the body
|
|
if len(w.chunks) > 0 {
|
|
// ignore error
|
|
to.Write(w.chunks)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Flush sends any buffered data to the client.
|
|
func (w *ResponseRecorder) Flush() {
|
|
w.ResponseWriter.Flush()
|
|
w.ResetBody()
|
|
}
|
|
|
|
// Push initiates an HTTP/2 server push. This constructs a synthetic
|
|
// request using the given target and options, serializes that request
|
|
// into a PUSH_PROMISE frame, then dispatches that request using the
|
|
// server's request handler. If opts is nil, default options are used.
|
|
//
|
|
// The target must either be an absolute path (like "/path") or an absolute
|
|
// URL that contains a valid host and the same scheme as the parent request.
|
|
// If the target is a path, it will inherit the scheme and host of the
|
|
// parent request.
|
|
//
|
|
// The HTTP/2 spec disallows recursive pushes and cross-authority pushes.
|
|
// Push may or may not detect these invalid pushes; however, invalid
|
|
// pushes will be detected and canceled by conforming clients.
|
|
//
|
|
// Handlers that wish to push URL X should call Push before sending any
|
|
// data that may trigger a request for URL X. This avoids a race where the
|
|
// client issues requests for X before receiving the PUSH_PROMISE for X.
|
|
//
|
|
// Push returns ErrPushNotSupported if the client has disabled push or if push
|
|
// is not supported on the underlying connection.
|
|
func (w *ResponseRecorder) Push(target string, opts *http.PushOptions) error {
|
|
w.FlushResponse()
|
|
err := w.ResponseWriter.Push(target, opts)
|
|
// NOTE: we have to reset them even if the push failed.
|
|
w.ResetBody()
|
|
w.ResetHeaders()
|
|
|
|
return err
|
|
}
|