|
4 | 4 | package echo |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bufio" |
7 | 8 | "errors" |
| 9 | + "fmt" |
8 | 10 | "log/slog" |
| 11 | + "net" |
9 | 12 | "net/http" |
10 | 13 | ) |
11 | 14 |
|
@@ -72,6 +75,30 @@ func (r *Response) Write(b []byte) (n int, err error) { |
72 | 75 | return |
73 | 76 | } |
74 | 77 |
|
| 78 | +// Flush implements the http.Flusher interface to allow an HTTP handler to flush |
| 79 | +// buffered data to the client. |
| 80 | +// See [http.Flusher](https://golang.org/pkg/net/http/#Flusher) |
| 81 | +func (r *Response) Flush() { |
| 82 | + err := http.NewResponseController(r.ResponseWriter).Flush() |
| 83 | + if err != nil && errors.Is(err, http.ErrNotSupported) { |
| 84 | + panic(fmt.Errorf("echo: response writer %T does not support flushing (http.Flusher interface)", r.ResponseWriter)) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Hijack implements the http.Hijacker interface to allow an HTTP handler to |
| 89 | +// take over the connection. |
| 90 | +// This method is relevant to Websocket connection upgrades, proxis, and other advanced use cases. |
| 91 | +// See [http.Hijacker](https://golang.org/pkg/net/http/#Hijacker) |
| 92 | +func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 93 | + // newer code should do response hijacking like that |
| 94 | + // http.NewResponseController(responseWriter).Hijack() |
| 95 | + // |
| 96 | + // but there are older libraries that are not aware of `http.NewResponseController` and try to hijack directly |
| 97 | + // `hj, ok := resp.(http.Hijacker)` <-- which would fail without Response directly implementing Hijack method |
| 98 | + // so for that purpose we need to implement http.Hijacker interface |
| 99 | + return http.NewResponseController(r.ResponseWriter).Hijack() |
| 100 | +} |
| 101 | + |
75 | 102 | // Unwrap returns the original http.ResponseWriter. |
76 | 103 | // ResponseController can be used to access the original http.ResponseWriter. |
77 | 104 | // See [https://go.dev/blog/go1.20] |
|
0 commit comments