2016-09-23 07:53:44 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-03-09 10:50:47 +02:00
|
|
|
"errors"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Response wraps an http.ResponseWriter and implements its interface to be used
|
|
|
|
// by an HTTP handler to construct an HTTP response.
|
|
|
|
// See: https://golang.org/pkg/net/http/#ResponseWriter
|
|
|
|
Response struct {
|
2018-03-14 23:03:59 +02:00
|
|
|
echo *Echo
|
|
|
|
beforeFuncs []func()
|
|
|
|
afterFuncs []func()
|
|
|
|
Writer http.ResponseWriter
|
|
|
|
Status int
|
|
|
|
Size int64
|
|
|
|
Committed bool
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
2017-06-29 02:01:48 +02:00
|
|
|
)
|
2016-09-23 07:53:44 +02:00
|
|
|
|
2017-08-24 17:27:15 +02:00
|
|
|
// NewResponse creates a new instance of Response.
|
|
|
|
func NewResponse(w http.ResponseWriter, e *Echo) (r *Response) {
|
|
|
|
return &Response{Writer: w, echo: e}
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// Header returns the header map for the writer that will be sent by
|
|
|
|
// WriteHeader. Changing the header after a call to WriteHeader (or Write) has
|
|
|
|
// no effect unless the modified headers were declared as trailers by setting
|
|
|
|
// the "Trailer" header before the call to WriteHeader (see example)
|
|
|
|
// To suppress implicit response headers, set their value to nil.
|
|
|
|
// Example: https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
|
|
|
|
func (r *Response) Header() http.Header {
|
2016-12-22 04:56:35 +02:00
|
|
|
return r.Writer.Header()
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 02:01:48 +02:00
|
|
|
// Before registers a function which is called just before the response is written.
|
2017-06-29 19:59:21 +02:00
|
|
|
func (r *Response) Before(fn func()) {
|
2017-06-29 02:01:48 +02:00
|
|
|
r.beforeFuncs = append(r.beforeFuncs, fn)
|
|
|
|
}
|
|
|
|
|
2017-12-24 20:59:29 +02:00
|
|
|
// After registers a function which is called just after the response is written.
|
|
|
|
// If the `Content-Length` is unknown, none of the after function is executed.
|
|
|
|
func (r *Response) After(fn func()) {
|
|
|
|
r.afterFuncs = append(r.afterFuncs, fn)
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// WriteHeader sends an HTTP response header with status code. If WriteHeader is
|
|
|
|
// not called explicitly, the first call to Write will trigger an implicit
|
|
|
|
// WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly
|
|
|
|
// used to send error codes.
|
|
|
|
func (r *Response) WriteHeader(code int) {
|
|
|
|
if r.Committed {
|
2017-06-29 19:59:21 +02:00
|
|
|
r.echo.Logger.Warn("response already committed")
|
2016-09-23 07:53:44 +02:00
|
|
|
return
|
|
|
|
}
|
2020-12-02 14:13:42 +02:00
|
|
|
r.Status = code
|
2017-06-29 04:08:45 +02:00
|
|
|
for _, fn := range r.beforeFuncs {
|
2017-06-29 19:59:21 +02:00
|
|
|
fn()
|
2017-06-29 04:08:45 +02:00
|
|
|
}
|
2020-12-02 14:13:42 +02:00
|
|
|
r.Writer.WriteHeader(r.Status)
|
2016-09-23 07:53:44 +02:00
|
|
|
r.Committed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the data to the connection as part of an HTTP reply.
|
|
|
|
func (r *Response) Write(b []byte) (n int, err error) {
|
|
|
|
if !r.Committed {
|
2019-05-24 07:13:57 +02:00
|
|
|
if r.Status == 0 {
|
|
|
|
r.Status = http.StatusOK
|
|
|
|
}
|
|
|
|
r.WriteHeader(r.Status)
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
2016-12-22 04:56:35 +02:00
|
|
|
n, err = r.Writer.Write(b)
|
2016-09-23 07:53:44 +02:00
|
|
|
r.Size += int64(n)
|
2018-03-14 23:03:59 +02:00
|
|
|
for _, fn := range r.afterFuncs {
|
|
|
|
fn()
|
2017-12-24 20:59:29 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements the http.Flusher interface to allow an HTTP handler to flush
|
|
|
|
// buffered data to the client.
|
|
|
|
// See [http.Flusher](https://golang.org/pkg/net/http/#Flusher)
|
|
|
|
func (r *Response) Flush() {
|
2024-03-09 10:50:47 +02:00
|
|
|
err := responseControllerFlush(r.Writer)
|
|
|
|
if err != nil && errors.Is(err, http.ErrNotSupported) {
|
|
|
|
panic(errors.New("response writer flushing is not supported"))
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack implements the http.Hijacker interface to allow an HTTP handler to
|
|
|
|
// take over the connection.
|
|
|
|
// See [http.Hijacker](https://golang.org/pkg/net/http/#Hijacker)
|
|
|
|
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2024-03-09 10:50:47 +02:00
|
|
|
return responseControllerHijack(r.Writer)
|
2016-09-23 07:53:44 +02:00
|
|
|
}
|
|
|
|
|
2023-03-28 11:25:11 +02:00
|
|
|
// Unwrap returns the original http.ResponseWriter.
|
|
|
|
// ResponseController can be used to access the original http.ResponseWriter.
|
|
|
|
// See [https://go.dev/blog/go1.20]
|
|
|
|
func (r *Response) Unwrap() http.ResponseWriter {
|
|
|
|
return r.Writer
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func (r *Response) reset(w http.ResponseWriter) {
|
2017-12-24 20:59:29 +02:00
|
|
|
r.beforeFuncs = nil
|
|
|
|
r.afterFuncs = nil
|
2016-12-22 04:56:35 +02:00
|
|
|
r.Writer = w
|
2016-09-23 07:53:44 +02:00
|
|
|
r.Size = 0
|
|
|
|
r.Status = http.StatusOK
|
|
|
|
r.Committed = false
|
|
|
|
}
|