Skip to content

Commit 5b2eb66

Browse files
authored
Use slices.Contains to simplify code (#7039)
Signed-off-by: tongjicoder <[email protected]>
1 parent a76d005 commit 5b2eb66

File tree

5 files changed

+11
-40
lines changed

5 files changed

+11
-40
lines changed

modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package fastcgi
1717
import (
1818
"encoding/json"
1919
"net/http"
20+
"slices"
2021
"strconv"
2122
"strings"
2223

@@ -314,7 +315,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
314315

315316
// if the index is turned off, we skip the redirect and try_files
316317
if indexFile != "off" {
317-
dirRedir := false
318+
var dirRedir bool
318319
dirIndex := "{http.request.uri.path}/" + indexFile
319320
tryPolicy := "first_exist_fallback"
320321

@@ -328,13 +329,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
328329
tryPolicy = ""
329330
}
330331

331-
for _, tf := range tryFiles {
332-
if tf == dirIndex {
333-
dirRedir = true
334-
335-
break
336-
}
337-
}
332+
dirRedir = slices.Contains(tryFiles, dirIndex)
338333
}
339334

340335
if dirRedir {

modules/caddyhttp/reverseproxy/httptransport.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,7 @@ func (h *HTTPTransport) shouldUseTLS(req *http.Request) bool {
528528
}
529529

530530
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)
538532
}
539533

540534
// TLSEnabled returns true if TLS is enabled.

modules/caddyhttp/staticresp.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"net/textproto"
2424
"os"
25+
"slices"
2526
"strconv"
2627
"strings"
2728
"text/template"
@@ -323,13 +324,7 @@ func cmdRespond(fl caddycmd.Flags) (int, error) {
323324

324325
// figure out if status code was explicitly specified; this lets
325326
// 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")
333328

334329
// try to determine what kind of parameter the unnamed argument is
335330
if arg != "" {

modules/caddytls/certselection.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,7 @@ nextChoice:
8787
}
8888

8989
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)
9791
if !found {
9892
continue
9993
}

modules/caddytls/connpolicy.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io"
2626
"os"
2727
"reflect"
28+
"slices"
2829
"strings"
2930

3031
"github.com/mholt/acmez/v3"
@@ -369,13 +370,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
369370
}
370371

371372
// 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)
379374
if !alpnFound && (cfg.NextProtos == nil || len(cfg.NextProtos) > 0) {
380375
cfg.NextProtos = append(cfg.NextProtos, acmez.ACMETLS1Protocol)
381376
}
@@ -1004,10 +999,8 @@ func (l LeafCertClientAuth) VerifyClientCertificate(rawCerts [][]byte, _ [][]*x5
1004999
return fmt.Errorf("can't parse the given certificate: %s", err.Error())
10051000
}
10061001

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
10111004
}
10121005

10131006
return fmt.Errorf("client leaf certificate failed validation")

0 commit comments

Comments
 (0)