http2/h2c: use ResponseController for hijacking connections

Fixes golang/go#71999

Change-Id: I38b236e47bc5893c5a84ef33abbeab0828125bd2
Reviewed-on: https://go-review.googlesource.com/c/net/+/655615
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/http2/h2c/h2c.go b/http2/h2c/h2c.go
index 2d6bf86..19e9479 100644
--- a/http2/h2c/h2c.go
+++ b/http2/h2c/h2c.go
@@ -132,11 +132,8 @@
 // of the body, and reforward the client preface on the net.Conn this function
 // creates.
 func initH2CWithPriorKnowledge(w http.ResponseWriter) (net.Conn, error) {
-	hijacker, ok := w.(http.Hijacker)
-	if !ok {
-		return nil, errors.New("h2c: connection does not support Hijack")
-	}
-	conn, rw, err := hijacker.Hijack()
+	rc := http.NewResponseController(w)
+	conn, rw, err := rc.Hijack()
 	if err != nil {
 		return nil, err
 	}
@@ -163,10 +160,6 @@
 	if err != nil {
 		return nil, nil, err
 	}
-	hijacker, ok := w.(http.Hijacker)
-	if !ok {
-		return nil, nil, errors.New("h2c: connection does not support Hijack")
-	}
 
 	body, err := io.ReadAll(r.Body)
 	if err != nil {
@@ -174,7 +167,8 @@
 	}
 	r.Body = io.NopCloser(bytes.NewBuffer(body))
 
-	conn, rw, err := hijacker.Hijack()
+	rc := http.NewResponseController(w)
+	conn, rw, err := rc.Hijack()
 	if err != nil {
 		return nil, nil, err
 	}