net/http: expose "http: server gave HTTP response to HTTPS client" error

Expose "http: server gave HTTP response to HTTPS client" error as
ErrSchemeMismatch, so that it can be compared with errors.Is .

Fixes #44855

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2022-02-01 08:51:19 +09:00
parent 4f4a9c7fff
commit 22879fc883
No known key found for this signature in database
GPG key ID: 49524C6F9F638F1A
2 changed files with 5 additions and 1 deletions

1
api/next/44855.txt Normal file
View file

@ -0,0 +1 @@
pkg net/http, var ErrSchemeMismatch error #44855

View file

@ -204,6 +204,9 @@ func (c *Client) transport() RoundTripper {
return DefaultTransport
}
// ErrSchemeMismatch is returned when a server returns an HTTP response to an HTTPS client.
var ErrSchemeMismatch = errors.New("http: server gave HTTP response to HTTPS client")
// send issues an HTTP request.
// Caller should close resp.Body when done reading from it.
func send(ireq *Request, rt RoundTripper, deadline time.Time) (resp *Response, didTimeout func() bool, err error) {
@ -265,7 +268,7 @@ func send(ireq *Request, rt RoundTripper, deadline time.Time) (resp *Response, d
// response looks like HTTP and give a more helpful error.
// See golang.org/issue/11111.
if string(tlsErr.RecordHeader[:]) == "HTTP/" {
err = errors.New("http: server gave HTTP response to HTTPS client")
err = ErrSchemeMismatch
}
}
return nil, didTimeout, err