Use slices.Contains to simplify code (#7039)

Signed-off-by: tongjicoder <tongjicoder@icloud.com>
pull/6487/head
tongjicoder 2025-06-01 02:03:06 +08:00 committed by GitHub
parent a76d005a94
commit 5b2eb66418
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 11 additions and 40 deletions

View File

@ -17,6 +17,7 @@ package fastcgi
import (
"encoding/json"
"net/http"
"slices"
"strconv"
"strings"
@ -314,7 +315,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
// if the index is turned off, we skip the redirect and try_files
if indexFile != "off" {
dirRedir := false
var dirRedir bool
dirIndex := "{http.request.uri.path}/" + indexFile
tryPolicy := "first_exist_fallback"
@ -328,13 +329,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
tryPolicy = ""
}
for _, tf := range tryFiles {
if tf == dirIndex {
dirRedir = true
break
}
}
dirRedir = slices.Contains(tryFiles, dirIndex)
}
if dirRedir {

View File

@ -528,13 +528,7 @@ func (h *HTTPTransport) shouldUseTLS(req *http.Request) bool {
}
port := req.URL.Port()
for i := range h.TLS.ExceptPorts {
if h.TLS.ExceptPorts[i] == port {
return false
}
}
return true
return !slices.Contains(h.TLS.ExceptPorts, port)
}
// TLSEnabled returns true if TLS is enabled.

View File

@ -22,6 +22,7 @@ import (
"net/http"
"net/textproto"
"os"
"slices"
"strconv"
"strings"
"text/template"
@ -323,13 +324,7 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
// figure out if status code was explicitly specified; this lets
// us set a non-zero value as the default but is a little hacky
var statusCodeFlagSpecified bool
for _, fl := range os.Args {
if fl == "--status" {
statusCodeFlagSpecified = true
break
}
}
statusCodeFlagSpecified := slices.Contains(os.Args, "--status")
// try to determine what kind of parameter the unnamed argument is
if arg != "" {

View File

@ -87,13 +87,7 @@ nextChoice:
}
if len(p.AnyTag) > 0 {
var found bool
for _, tag := range p.AnyTag {
if cert.HasTag(tag) {
found = true
break
}
}
found := slices.ContainsFunc(p.AnyTag, cert.HasTag)
if !found {
continue
}

View File

@ -25,6 +25,7 @@ import (
"io"
"os"
"reflect"
"slices"
"strings"
"github.com/mholt/acmez/v3"
@ -369,13 +370,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
}
// ensure ALPN includes the ACME TLS-ALPN protocol
var alpnFound bool
for _, a := range p.ALPN {
if a == acmez.ACMETLS1Protocol {
alpnFound = true
break
}
}
alpnFound := slices.Contains(p.ALPN, acmez.ACMETLS1Protocol)
if !alpnFound && (cfg.NextProtos == nil || len(cfg.NextProtos) > 0) {
cfg.NextProtos = append(cfg.NextProtos, acmez.ACMETLS1Protocol)
}
@ -1004,10 +999,8 @@ func (l LeafCertClientAuth) VerifyClientCertificate(rawCerts [][]byte, _ [][]*x5
return fmt.Errorf("can't parse the given certificate: %s", err.Error())
}
for _, trustedLeafCert := range l.trustedLeafCerts {
if remoteLeafCert.Equal(trustedLeafCert) {
return nil
}
if slices.ContainsFunc(l.trustedLeafCerts, remoteLeafCert.Equal) {
return nil
}
return fmt.Errorf("client leaf certificate failed validation")