chore: Use slices package where possible (#6585)

* chore: Use slices package where possible

* More, mostly using ContainsFunc

* Even more slice operations
This commit is contained in:
Francis Lavoie 2024-09-25 16:30:56 -04:00 committed by GitHub
parent 9dda8fbf84
commit 2faeac0a10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 142 additions and 268 deletions

View file

@ -25,6 +25,7 @@ import (
"net/netip"
"net/url"
"runtime"
"slices"
"strings"
"sync"
"time"
@ -543,12 +544,9 @@ func (s *Server) hasListenerAddress(fullAddr string) bool {
}
func (s *Server) hasTLSClientAuth() bool {
for _, cp := range s.TLSConnPolicies {
if cp.ClientAuthentication != nil && cp.ClientAuthentication.Active() {
return true
}
}
return false
return slices.ContainsFunc(s.TLSConnPolicies, func(cp *caddytls.ConnectionPolicy) bool {
return cp.ClientAuthentication != nil && cp.ClientAuthentication.Active()
})
}
// findLastRouteWithHostMatcher returns the index of the last route
@ -849,12 +847,7 @@ func (s *Server) logRequest(
// protocol returns true if the protocol proto is configured/enabled.
func (s *Server) protocol(proto string) bool {
for _, p := range s.Protocols {
if p == proto {
return true
}
}
return false
return slices.Contains(s.Protocols, proto)
}
// Listeners returns the server's listeners. These are active listeners,
@ -959,12 +952,9 @@ func determineTrustedProxy(r *http.Request, s *Server) (bool, string) {
// isTrustedClientIP returns true if the given IP address is
// in the list of trusted IP ranges.
func isTrustedClientIP(ipAddr netip.Addr, trusted []netip.Prefix) bool {
for _, ipRange := range trusted {
if ipRange.Contains(ipAddr) {
return true
}
}
return false
return slices.ContainsFunc(trusted, func(prefix netip.Prefix) bool {
return prefix.Contains(ipAddr)
})
}
// trustedRealClientIP finds the client IP from the request assuming it is