@marcel91200 / caddy Mirror · read-only / commits / 5b2eb664
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 23:30 UTC

Use slices.Contains to simplify code (#7039)

Signed-off-by: tongjicoder <tongjicoder@icloud.com>

tongjicoder committed Jun 1, 2025 at 02:03 UTC 5b2eb664188775973cb8254d4e21544572866a47
5 files changed +11 -40
modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
+3 -8
@@ -17,6 +17,7 @@ package fastcgi
17 import (
18 "encoding/json"
19 "net/http"
20 + "slices"
21 "strconv"
22 "strings"
23
@@ -314,7 +315,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
315
316 // if the index is turned off, we skip the redirect and try_files
317 if indexFile != "off" {
317 - dirRedir := false
318 + var dirRedir bool
319 dirIndex := "{http.request.uri.path}/" + indexFile
320 tryPolicy := "first_exist_fallback"
321
@@ -328,13 +329,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
329 tryPolicy = ""
330 }
331
331 - for _, tf := range tryFiles {
332 - if tf == dirIndex {
333 - dirRedir = true
334 -
335 - break
336 - }
337 - }
332 + dirRedir = slices.Contains(tryFiles, dirIndex)
333 }
334
335 if dirRedir {
modules/caddyhttp/reverseproxy/httptransport.go
+1 -7
@@ -528,13 +528,7 @@ func (h *HTTPTransport) shouldUseTLS(req *http.Request) bool {
528 }
529
530 port := req.URL.Port()
531 - for i := range h.TLS.ExceptPorts {
532 - if h.TLS.ExceptPorts[i] == port {
533 - return false
534 - }
535 - }
536 -
537 - return true
531 + return !slices.Contains(h.TLS.ExceptPorts, port)
532 }
533
534 // TLSEnabled returns true if TLS is enabled.
modules/caddyhttp/staticresp.go
+2 -7
@@ -22,6 +22,7 @@ import (
22 "net/http"
23 "net/textproto"
24 "os"
25 + "slices"
26 "strconv"
27 "strings"
28 "text/template"
@@ -323,13 +324,7 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
324
325 // figure out if status code was explicitly specified; this lets
326 // us set a non-zero value as the default but is a little hacky
326 - var statusCodeFlagSpecified bool
327 - for _, fl := range os.Args {
328 - if fl == "--status" {
329 - statusCodeFlagSpecified = true
330 - break
331 - }
332 - }
327 + statusCodeFlagSpecified := slices.Contains(os.Args, "--status")
328
329 // try to determine what kind of parameter the unnamed argument is
330 if arg != "" {
modules/caddytls/certselection.go
+1 -7
@@ -87,13 +87,7 @@ nextChoice:
87 }
88
89 if len(p.AnyTag) > 0 {
90 - var found bool
91 - for _, tag := range p.AnyTag {
92 - if cert.HasTag(tag) {
93 - found = true
94 - break
95 - }
96 - }
90 + found := slices.ContainsFunc(p.AnyTag, cert.HasTag)
91 if !found {
92 continue
93 }
modules/caddytls/connpolicy.go
+4 -11
@@ -25,6 +25,7 @@ import (
25 "io"
26 "os"
27 "reflect"
28 + "slices"
29 "strings"
30
31 "github.com/mholt/acmez/v3"
@@ -369,13 +370,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
370 }
371
372 // ensure ALPN includes the ACME TLS-ALPN protocol
372 - var alpnFound bool
373 - for _, a := range p.ALPN {
374 - if a == acmez.ACMETLS1Protocol {
375 - alpnFound = true
376 - break
377 - }
378 - }
373 + alpnFound := slices.Contains(p.ALPN, acmez.ACMETLS1Protocol)
374 if !alpnFound && (cfg.NextProtos == nil || len(cfg.NextProtos) > 0) {
375 cfg.NextProtos = append(cfg.NextProtos, acmez.ACMETLS1Protocol)
376 }
@@ -1004,10 +999,8 @@ func (l LeafCertClientAuth) VerifyClientCertificate(rawCerts [][]byte, _ [][]*x5
999 return fmt.Errorf("can't parse the given certificate: %s", err.Error())
1000 }
1001
1007 - for _, trustedLeafCert := range l.trustedLeafCerts {
1008 - if remoteLeafCert.Equal(trustedLeafCert) {
1009 - return nil
1010 - }
1002 + if slices.ContainsFunc(l.trustedLeafCerts, remoteLeafCert.Equal) {
1003 + return nil
1004 }
1005
1006 return fmt.Errorf("client leaf certificate failed validation")