Documentation
¶
Index ¶
- type ResponseRecorder
- func (r *ResponseRecorder) Body() []byte
- func (r *ResponseRecorder) Buffered() bool
- func (r *ResponseRecorder) Commit() error
- func (r *ResponseRecorder) Discard()
- func (r *ResponseRecorder) Flush()
- func (r *ResponseRecorder) FlushError() error
- func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *ResponseRecorder) Hijacked() bool
- func (r *ResponseRecorder) Overflowed() bool
- func (r *ResponseRecorder) Passthrough() bool
- func (r *ResponseRecorder) Push(target string, opts *http.PushOptions) error
- func (r *ResponseRecorder) Status() int
- func (r *ResponseRecorder) Unwrap() http.ResponseWriter
- func (r *ResponseRecorder) Write(p []byte) (int, error)
- func (r *ResponseRecorder) WriteHeader(code int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ResponseRecorder ¶
type ResponseRecorder struct {
http.ResponseWriter
// BeforeWriteHeader, when set, is called once just before the status line
// and headers are written to the real writer. It is the last chance to add
// headers such as ETag or X-Cache.
BeforeWriteHeader func(http.Header)
// contains filtered or unexported fields
}
ResponseRecorder buffers a response so a middleware can inspect and modify it before anything is sent to the client.
The recorder holds at most Limit bytes. If the handler writes more than that, the recorder flushes what it has and switches to pass-through mode; the response is streamed normally and Buffered reports false. Calling Flush or Hijack also disables buffering, which keeps SSE, websockets, and other streaming handlers working.
A middleware must call Commit to send a fully buffered response, or Discard to throw it away (for example when the handler returns an error and Echo should render its own error response).
ResponseRecorder implements Unwrap so it plays well with http.ResponseController and Echo's response-writer contract.
func NewResponseRecorder ¶
func NewResponseRecorder(w http.ResponseWriter, limit int) *ResponseRecorder
NewResponseRecorder wraps w and buffers up to limit bytes. A limit <= 0 disables buffering entirely: the first write goes straight to w.
func (*ResponseRecorder) Body ¶
func (r *ResponseRecorder) Body() []byte
Body returns a copy of the buffered body.
func (*ResponseRecorder) Buffered ¶
func (r *ResponseRecorder) Buffered() bool
Buffered reports whether the response is still being buffered.
func (*ResponseRecorder) Commit ¶
func (r *ResponseRecorder) Commit() error
Commit flushes a buffered response to the client. It is a no-op when the response was streamed, hijacked, or discarded.
func (*ResponseRecorder) Discard ¶
func (r *ResponseRecorder) Discard()
Discard drops the buffered response and prevents it from being committed. Headers already set on the underlying writer are left alone.
func (*ResponseRecorder) Flush ¶
func (r *ResponseRecorder) Flush()
Flush sends the status, headers, and buffered body, then flushes the underlying writer. It permanently disables buffering for this response.
func (*ResponseRecorder) FlushError ¶
func (r *ResponseRecorder) FlushError() error
FlushError is the http.ResponseController-compatible variant of Flush.
func (*ResponseRecorder) Hijack ¶
func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack lets the handler take over the connection (websockets). Buffered data is discarded and no HTTP response is written.
func (*ResponseRecorder) Hijacked ¶
func (r *ResponseRecorder) Hijacked() bool
Hijacked reports whether the handler took over the connection.
func (*ResponseRecorder) Overflowed ¶
func (r *ResponseRecorder) Overflowed() bool
Overflowed reports whether the buffer limit was exceeded.
func (*ResponseRecorder) Passthrough ¶
func (r *ResponseRecorder) Passthrough() bool
Passthrough reports whether the response was streamed because it exceeded the buffer limit or the handler flushed it.
func (*ResponseRecorder) Push ¶
func (r *ResponseRecorder) Push(target string, opts *http.PushOptions) error
Push implements http.Pusher when the underlying writer supports HTTP/2 server push.
func (*ResponseRecorder) Status ¶
func (r *ResponseRecorder) Status() int
Status returns the recorded status code.
func (*ResponseRecorder) Unwrap ¶
func (r *ResponseRecorder) Unwrap() http.ResponseWriter
Unwrap returns the wrapped writer. Echo v5 uses this to reach the original *echo.Response and http.ResponseController uses it for optional interfaces.
func (*ResponseRecorder) Write ¶
func (r *ResponseRecorder) Write(p []byte) (int, error)
Write stores the body in the buffer, or streams it once the limit is exceeded.
func (*ResponseRecorder) WriteHeader ¶
func (r *ResponseRecorder) WriteHeader(code int)
WriteHeader records the status code. It is not sent to the client until Commit, a buffer overflow, or Flush.