Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:fasthttp server with tlsConfig #1595

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1677,14 +1677,16 @@ func (s *Server) ListenAndServeTLSEmbed(addr string, certData, keyData []byte) e
// the function will use previously added TLS configuration.
func (s *Server) ServeTLS(ln net.Listener, certFile, keyFile string) error {
s.mu.Lock()
err := s.AppendCert(certFile, keyFile)
if err != nil && err != errNoCertOrKeyProvided {
s.mu.Unlock()
return err
}
if s.TLSConfig == nil {
s.mu.Unlock()
return errNoCertOrKeyProvided
s.configTLS()
config := s.TLSConfig.Clone()
zxpdmw marked this conversation as resolved.
Show resolved Hide resolved
var err error
configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil
if !configHasCert || certFile != "" || keyFile != "" {
erikdubbelboer marked this conversation as resolved.
Show resolved Hide resolved
err = s.AppendCert(certFile, keyFile)
if err != nil {
zxpdmw marked this conversation as resolved.
Show resolved Hide resolved
s.mu.Unlock()
return err
}
}

// BuildNameToCertificate has been deprecated since 1.14.
Expand All @@ -1706,15 +1708,16 @@ func (s *Server) ServeTLS(ln net.Listener, certFile, keyFile string) error {
// the function will use previously added TLS configuration.
func (s *Server) ServeTLSEmbed(ln net.Listener, certData, keyData []byte) error {
s.mu.Lock()

err := s.AppendCertEmbed(certData, keyData)
if err != nil && err != errNoCertOrKeyProvided {
s.mu.Unlock()
return err
}
if s.TLSConfig == nil {
s.mu.Unlock()
return errNoCertOrKeyProvided
s.configTLS()
config := s.TLSConfig.Clone()
var err error
configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil
if !configHasCert || len(certData) != 0 || len(keyData) != 0 {
err = s.AppendCertEmbed(certData, keyData)
if err != nil {
s.mu.Unlock()
return err
}
}

// BuildNameToCertificate has been deprecated since 1.14.
Expand Down Expand Up @@ -1742,7 +1745,6 @@ func (s *Server) AppendCert(certFile, keyFile string) error {
return fmt.Errorf("cannot load TLS key pair from certFile=%q and keyFile=%q: %w", certFile, keyFile, err)
}

s.configTLS()
zxpdmw marked this conversation as resolved.
Show resolved Hide resolved
s.TLSConfig.Certificates = append(s.TLSConfig.Certificates, cert)

return nil
Expand All @@ -1760,7 +1762,6 @@ func (s *Server) AppendCertEmbed(certData, keyData []byte) error {
len(certData), len(keyData), err)
}

s.configTLS()
zxpdmw marked this conversation as resolved.
Show resolved Hide resolved
s.TLSConfig.Certificates = append(s.TLSConfig.Certificates, cert)

return nil
Expand Down