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

View File

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

View File

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

View File

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

View File

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