fix matching if only h1 is enabled

This commit is contained in:
WeidiDeng 2025-05-23 10:28:58 +08:00
parent c28bab9cb3
commit 4158f9e8d5
No known key found for this signature in database
GPG Key ID: 25F87CE1741EC7CD

View File

@ -86,11 +86,17 @@ func (c *http2Conn) Read(p []byte) (int, error) {
} }
n, err := c.Conn.Read(p) n, err := c.Conn.Read(p)
for i := range n { for i := range n {
// first mismatch, close the connection if h2 is expected // first mismatch
if p[i] != http2.ClientPreface[c.idx] && c.h2Expected { if p[i] != http2.ClientPreface[c.idx] {
c.logger.Debug("h1 connection detected, but h1 is not enabled") // close the connection if h2 is expected
_ = c.Conn.Close() if c.h2Expected {
return 0, io.EOF c.logger.Debug("h1 connection detected, but h1 is not enabled")
_ = c.Conn.Close()
return 0, io.EOF
}
// no need to continue matching anymore
c.idx = len(http2.ClientPreface)
return n, err
} }
c.idx++ c.idx++
// matching complete // matching complete