blob: b2de2116135c4e4d63323de5f5d24a8b2de84ffa [file] [log] [blame]
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +05301// Copyright 2024 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package http2
6
7import (
8 "crypto/tls"
9 "errors"
10 "net"
11)
12
13const nextProtoUnencryptedHTTP2 = "unencrypted_http2"
14
15// unencryptedNetConnFromTLSConn retrieves a net.Conn wrapped in a *tls.Conn.
16//
17// TLSNextProto functions accept a *tls.Conn.
18//
19// When passing an unencrypted HTTP/2 connection to a TLSNextProto function,
20// we pass a *tls.Conn with an underlying net.Conn containing the unencrypted connection.
21// To be extra careful about mistakes (accidentally dropping TLS encryption in a place
22// where we want it), the tls.Conn contains a net.Conn with an UnencryptedNetConn method
23// that returns the actual connection we want to use.
24func unencryptedNetConnFromTLSConn(tc *tls.Conn) (net.Conn, error) {
25 conner, ok := tc.NetConn().(interface {
26 UnencryptedNetConn() net.Conn
27 })
28 if !ok {
29 return nil, errors.New("http2: TLS conn unexpectedly found in unencrypted handoff")
30 }
31 return conner.UnencryptedNetConn(), nil
32}