Skip to content

Commit

Permalink
fix: server response hijack
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Mar 3, 2024
1 parent 6fecc0c commit 338c34e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package websocket
import "errors"

var (
ErrNilConn = errors.New("nil *Conn")
ErrNilNetConn = errors.New("nil net.Conn")
ErrNilConn = errors.New("nil *Conn")
ErrNilNetConn = errors.New("nil net.Conn")
ErrResponseHijackUnsupported = errors.New("websocket: response does not implement http.Hijacker")
)
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
}
}

netConn, brw, err := http.NewResponseController(w).Hijack()
netConn, brw, err := HijackResponse(r, w)
if err != nil {
return u.returnError(w, r, http.StatusInternalServerError, err.Error())
}
Expand Down
13 changes: 13 additions & 0 deletions server_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !go1.20

package websocket

import (
"bufio"
"net"
"net/http"
)

func HijackResponse(_ *http.Request, w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) {
return http.NewResponseController(w).Hijack()

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / build (1.18.x)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / build (1.19.x)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, macos-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, windows-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, macos-latest)

undefined: http.NewResponseController

Check failure on line 12 in server_utils.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, windows-latest)

undefined: http.NewResponseController
}
24 changes: 24 additions & 0 deletions server_utils_120.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build go1.20

package websocket

import (
"bufio"
"net"
"net/http"
)

func HijackResponse(r *http.Request, w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) {
h, ok := w.(http.Hijacker)
if !ok {
return nil, nil, ErrResponseHijackUnsupported
}

var brw *bufio.ReadWriter
netConn, brw, err := h.Hijack()
if err != nil {
return nil, nil, err
}

return netConn, brw, nil
}

0 comments on commit 338c34e

Please sign in to comment.