-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
accept: Add unwrapping for hijack like http.ResponseController (#472)
Since we rely on the connection not being hijacked too early (i.e. detecting the presence of http.Hijacker) to set headers, we must manually implement the unwrapping of the http.ResponseController. By doing so, we also retain Go 1.19 compatibility without build tags. Closes #455
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build !js | ||
|
||
package websocket | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type rwUnwrapper interface { | ||
Unwrap() http.ResponseWriter | ||
} | ||
|
||
// hijacker returns the Hijacker interface of the http.ResponseWriter. | ||
// It follows the Unwrap method of the http.ResponseWriter if available, | ||
// matching the behavior of http.ResponseController. If the Hijacker | ||
// interface is not found, it returns false. | ||
// | ||
// Since the http.ResponseController is not available in Go 1.19, and | ||
// does not support checking the presence of the Hijacker interface, | ||
// this function is used to provide a consistent way to check for the | ||
// Hijacker interface across Go versions. | ||
func hijacker(rw http.ResponseWriter) (http.Hijacker, bool) { | ||
for { | ||
switch t := rw.(type) { | ||
case http.Hijacker: | ||
return t, true | ||
case rwUnwrapper: | ||
rw = t.Unwrap() | ||
default: | ||
return nil, false | ||
} | ||
} | ||
} |
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,38 @@ | ||
//go:build !js && go1.20 | ||
|
||
package websocket | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/coder/websocket/internal/test/assert" | ||
) | ||
|
||
func Test_hijackerHTTPResponseControllerCompatibility(t *testing.T) { | ||
t.Parallel() | ||
|
||
rr := httptest.NewRecorder() | ||
w := mockUnwrapper{ | ||
ResponseWriter: rr, | ||
unwrap: func() http.ResponseWriter { | ||
return mockHijacker{ | ||
ResponseWriter: rr, | ||
hijack: func() (conn net.Conn, writer *bufio.ReadWriter, err error) { | ||
return nil, nil, errors.New("haha") | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
_, _, err := http.NewResponseController(w).Hijack() | ||
assert.Contains(t, err, "haha") | ||
hj, ok := hijacker(w) | ||
assert.Equal(t, "hijacker found", ok, true) | ||
_, _, err = hj.Hijack() | ||
assert.Contains(t, err, "haha") | ||
} |