-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow ResponseWriters to unwrap writers when flushing/hijacking (#2595)
* Allow ResponseWriters to unwrap writers when flushing/hijacking
- Loading branch information
Showing
11 changed files
with
289 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//go:build !go1.20 | ||
|
||
package middleware | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
// TODO: remove when Go 1.23 is released and we do not support 1.19 anymore | ||
func responseControllerFlush(rw http.ResponseWriter) error { | ||
for { | ||
switch t := rw.(type) { | ||
case interface{ FlushError() error }: | ||
return t.FlushError() | ||
case http.Flusher: | ||
t.Flush() | ||
return nil | ||
case interface{ Unwrap() http.ResponseWriter }: | ||
rw = t.Unwrap() | ||
default: | ||
return fmt.Errorf("%w", http.ErrNotSupported) | ||
} | ||
} | ||
} | ||
|
||
// TODO: remove when Go 1.23 is released and we do not support 1.19 anymore | ||
func responseControllerHijack(rw http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) { | ||
for { | ||
switch t := rw.(type) { | ||
case http.Hijacker: | ||
return t.Hijack() | ||
case interface{ Unwrap() http.ResponseWriter }: | ||
rw = t.Unwrap() | ||
default: | ||
return nil, nil, fmt.Errorf("%w", http.ErrNotSupported) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build go1.20 | ||
|
||
package middleware | ||
|
||
import ( | ||
"bufio" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
func responseControllerFlush(rw http.ResponseWriter) error { | ||
return http.NewResponseController(rw).Flush() | ||
} | ||
|
||
func responseControllerHijack(rw http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) { | ||
return http.NewResponseController(rw).Hijack() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//go:build !go1.20 | ||
|
||
package echo | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
// TODO: remove when Go 1.23 is released and we do not support 1.19 anymore | ||
func responseControllerFlush(rw http.ResponseWriter) error { | ||
for { | ||
switch t := rw.(type) { | ||
case interface{ FlushError() error }: | ||
return t.FlushError() | ||
case http.Flusher: | ||
t.Flush() | ||
return nil | ||
case interface{ Unwrap() http.ResponseWriter }: | ||
rw = t.Unwrap() | ||
default: | ||
return fmt.Errorf("%w", http.ErrNotSupported) | ||
} | ||
} | ||
} | ||
|
||
// TODO: remove when Go 1.23 is released and we do not support 1.19 anymore | ||
func responseControllerHijack(rw http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) { | ||
for { | ||
switch t := rw.(type) { | ||
case http.Hijacker: | ||
return t.Hijack() | ||
case interface{ Unwrap() http.ResponseWriter }: | ||
rw = t.Unwrap() | ||
default: | ||
return nil, nil, fmt.Errorf("%w", http.ErrNotSupported) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build go1.20 | ||
|
||
package echo | ||
|
||
import ( | ||
"bufio" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
func responseControllerFlush(rw http.ResponseWriter) error { | ||
return http.NewResponseController(rw).Flush() | ||
} | ||
|
||
func responseControllerHijack(rw http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) { | ||
return http.NewResponseController(rw).Hijack() | ||
} |