Skip to content

Commit

Permalink
Use http.ResponseController instead of http.Hijacker assertion
Browse files Browse the repository at this point in the history
Fixes #455
  • Loading branch information
mafredri committed Aug 15, 2024
1 parent b366270 commit 75c16e2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
17 changes: 7 additions & 10 deletions accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
}
}

hj, ok := w.(http.Hijacker)
if !ok {
err = errors.New("http.ResponseWriter does not implement http.Hijacker")
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
return nil, err
}

w.Header().Set("Upgrade", "websocket")
w.Header().Set("Connection", "Upgrade")

Expand All @@ -136,10 +129,14 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
ginWriter.WriteHeaderNow()
}

netConn, brw, err := hj.Hijack()
netConn, brw, err := hijack(w)
if err != nil {
err = fmt.Errorf("failed to hijack connection: %w", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
if errors.Is(err, errHTTPHijackNotSupported) {
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
} else {
err = fmt.Errorf("failed to hijack connection: %w", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return nil, err
}

Expand Down
20 changes: 20 additions & 0 deletions hijack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build go1.20

package websocket

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

var errHTTPHijackNotSupported = errors.New("http.ResponseWriter does not implement http.Hijacker")

Check failure on line 12 in hijack.go

View workflow job for this annotation

GitHub Actions / lint

var errHTTPHijackNotSupported is unused (U1000)

Check failure on line 12 in hijack.go

View workflow job for this annotation

GitHub Actions / lint

var errHTTPHijackNotSupported is unused (U1000)

func hijack(w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) {

Check failure on line 14 in hijack.go

View workflow job for this annotation

GitHub Actions / lint

func hijack is unused (U1000)

Check failure on line 14 in hijack.go

View workflow job for this annotation

GitHub Actions / lint

func hijack is unused (U1000)
conn, rw, err := http.NewResponseController(w).Hijack()
if errors.Is(err, http.ErrNotSupported) {
return nil, nil, errHTTPHijackNotSupported
}
return conn, rw, err
}
20 changes: 20 additions & 0 deletions hijack_119.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build !go1.20

package websocket

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

var errHTTPHijackNotSupported = errors.New("http.ResponseWriter does not implement http.Hijacker")

func hijack(w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) {
hj, ok := w.(http.Hijacker)
if !ok {
return nil, nil, errHTTPHijackNotSupported
}
return hj.Hijack()
}

0 comments on commit 75c16e2

Please sign in to comment.