diff --git a/client.go b/client.go index 9e7242691a..5e05e131d4 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ package fasthttp import ( "bufio" - "bytes" "crypto/tls" "errors" "fmt" @@ -464,11 +463,10 @@ func (c *Client) Do(req *Request, resp *Response) error { host := uri.Host() isTLS := false - scheme := uri.Scheme() - if bytes.Equal(scheme, strHTTPS) { + if uri.isHttps() { isTLS = true - } else if !bytes.Equal(scheme, strHTTP) { - return fmt.Errorf("unsupported protocol %q. http and https are supported", scheme) + } else if !uri.isHttp() { + return fmt.Errorf("unsupported protocol %q. http and https are supported", uri.Scheme()) } startCleaner := false @@ -1363,7 +1361,7 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error) req.secureErrorLogMessage = c.SecureErrorLogMessage req.Header.secureErrorLogMessage = c.SecureErrorLogMessage - if c.IsTLS != bytes.Equal(req.uri.Scheme(), strHTTPS) { + if c.IsTLS != req.uri.isHttps() { return false, ErrHostClientRedirectToDifferentScheme } diff --git a/server_test.go b/server_test.go index 1ffe3964fa..4a2951562f 100644 --- a/server_test.go +++ b/server_test.go @@ -1158,9 +1158,8 @@ func TestServerServeTLSEmbed(t *testing.T) { ctx.Error("expecting tls", StatusBadRequest) return } - scheme := ctx.URI().Scheme() - if string(scheme) != "https" { - ctx.Error(fmt.Sprintf("unexpected scheme=%q. Expecting %q", scheme, "https"), StatusBadRequest) + if !ctx.URI().isHttps() { + ctx.Error(fmt.Sprintf("unexpected scheme=%q. Expecting %q", ctx.URI().Scheme(), "https"), StatusBadRequest) return } ctx.WriteString("success") //nolint:errcheck diff --git a/uri.go b/uri.go index 2285f45d0d..f6bb8f9eea 100644 --- a/uri.go +++ b/uri.go @@ -216,6 +216,14 @@ func (u *URI) SetSchemeBytes(scheme []byte) { lowercaseBytes(u.scheme) } +func (u *URI) isHttps() bool { + return bytes.Equal(u.scheme, strHTTPS) +} + +func (u *URI) isHttp() bool { + return bytes.Equal(u.scheme, strHTTP) +} + // Reset clears uri. func (u *URI) Reset() { u.pathOriginal = u.pathOriginal[:0]