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, 22:15 UTC
use a more modern writing style to simplify code (#7182)
Signed-off-by: joemicky <joemickychang@outlook.com> Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
joemicky committed
Aug 21, 2025 at 02:41 UTC
5125fbed410f0b1d6bb9d8b9acde764ffc136d34
11 files changed
+19
-30
admin_test.go
+1
-1
@@ -207,7 +207,7 @@ func TestETags(t *testing.T) {
207
}
208
209
func BenchmarkLoad(b *testing.B) {
210
- for i := 0; i < b.N; i++ {
210
+ for b.Loop() {
211
Load(testCfg, true)
212
}
213
}
modules/caddyhttp/celmatcher_test.go
+1
-1
@@ -535,7 +535,7 @@ func BenchmarkMatchExpressionMatch(b *testing.B) {
535
}
536
}
537
b.ResetTimer()
538
- for i := 0; i < b.N; i++ {
538
+ for b.Loop() {
539
tc.expression.MatchWithError(req)
540
}
541
})
modules/caddyhttp/encode/encode.go
+2
-3
@@ -452,8 +452,7 @@ func (rw *responseWriter) init() {
452
453
func hasVaryValue(hdr http.Header, target string) bool {
454
for _, vary := range hdr.Values("Vary") {
455
- vals := strings.Split(vary, ",")
456
- for _, val := range vals {
455
+ for val := range strings.SplitSeq(vary, ",") {
456
if strings.EqualFold(strings.TrimSpace(val), target) {
457
return true
458
}
@@ -478,7 +477,7 @@ func AcceptedEncodings(r *http.Request, preferredOrder []string) []string {
477
478
prefs := []encodingPreference{}
479
481
- for _, accepted := range strings.Split(acceptEncHeader, ",") {
480
+ for accepted := range strings.SplitSeq(acceptEncHeader, ",") {
481
parts := strings.Split(accepted, ";")
482
encName := strings.ToLower(strings.TrimSpace(parts[0]))
483
modules/caddyhttp/encode/encode_test.go
+1
-1
@@ -9,7 +9,7 @@ import (
9
10
func BenchmarkOpenResponseWriter(b *testing.B) {
11
enc := new(Encode)
12
- for n := 0; n < b.N; n++ {
12
+ for b.Loop() {
13
enc.openResponseWriter("test", nil, false)
14
}
15
}
modules/caddyhttp/matchers_test.go
+4
-8
@@ -947,7 +947,7 @@ func BenchmarkHeaderREMatcher(b *testing.B) {
947
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
948
req = req.WithContext(ctx)
949
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
950
- for run := 0; run < b.N; run++ {
950
+ for b.Loop() {
951
match.MatchWithError(req)
952
}
953
}
@@ -992,7 +992,6 @@ func TestVarREMatcher(t *testing.T) {
992
expect: true,
993
},
994
} {
995
- i := i // capture range value
995
tc := tc // capture range value
996
t.Run(tc.desc, func(t *testing.T) {
997
t.Parallel()
@@ -1180,8 +1179,7 @@ func BenchmarkLargeHostMatcher(b *testing.B) {
1179
b.Fatal(err)
1180
}
1181
1183
- b.ResetTimer()
1184
- for i := 0; i < b.N; i++ {
1182
+ for b.Loop() {
1183
matcher.MatchWithError(req)
1184
}
1185
}
@@ -1194,8 +1192,7 @@ func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
1192
1193
match := MatchHost{"localhost"}
1194
1197
- b.ResetTimer()
1198
- for i := 0; i < b.N; i++ {
1195
+ for b.Loop() {
1196
match.MatchWithError(req)
1197
}
1198
}
@@ -1212,8 +1209,7 @@ func BenchmarkHostMatcherWithPlaceholder(b *testing.B) {
1209
req = req.WithContext(ctx)
1210
match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}
1211
1215
- b.ResetTimer()
1216
- for i := 0; i < b.N; i++ {
1212
+ for b.Loop() {
1213
match.MatchWithError(req)
1214
}
1215
}
modules/caddyhttp/push/link.go
+2
-2
@@ -41,7 +41,7 @@ func parseLinkHeader(header string) []linkResource {
41
return resources
42
}
43
44
- for _, link := range strings.Split(header, comma) {
44
+ for link := range strings.SplitSeq(header, comma) {
45
l := linkResource{params: make(map[string]string)}
46
47
li, ri := strings.Index(link, "<"), strings.Index(link, ">")
@@ -51,7 +51,7 @@ func parseLinkHeader(header string) []linkResource {
51
52
l.uri = strings.TrimSpace(link[li+1 : ri])
53
54
- for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
54
+ for param := range strings.SplitSeq(strings.TrimSpace(link[ri+1:]), semicolon) {
55
before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
56
key := strings.TrimSpace(before)
57
if key == "" {
modules/caddyhttp/reverseproxy/reverseproxy.go
+1
-1
@@ -1356,7 +1356,7 @@ func upgradeType(h http.Header) string {
1356
// See RFC 7230, section 6.1
1357
func removeConnectionHeaders(h http.Header) {
1358
for _, f := range h["Connection"] {
1359
- for _, sf := range strings.Split(f, ",") {
1359
+ for sf := range strings.SplitSeq(f, ",") {
1360
if sf = textproto.TrimString(sf); sf != "" {
1361
h.Del(sf)
1362
}
modules/caddyhttp/server.go
+2
-2
@@ -985,10 +985,10 @@ func trustedRealClientIP(r *http.Request, headers []string, clientIP string) str
985
986
// Since there can be many header values, we need to
987
// join them together before splitting to get the full list
988
- allValues := strings.Split(strings.Join(values, ","), ",")
988
+ allValues := strings.SplitSeq(strings.Join(values, ","), ",")
989
990
// Get first valid left-most IP address
991
- for _, part := range allValues {
991
+ for part := range allValues {
992
// Some proxies may retain the port number, so split if possible
993
host, _, err := net.SplitHostPort(part)
994
if err != nil {
modules/caddyhttp/server_test.go
+3
-9
@@ -116,9 +116,7 @@ func BenchmarkServer_LogRequest(b *testing.B) {
116
buf := io.Discard
117
accLog := testLogger(buf.Write)
118
119
- b.ResetTimer()
120
-
121
- for i := 0; i < b.N; i++ {
119
+ for b.Loop() {
120
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
121
}
122
}
@@ -139,9 +137,7 @@ func BenchmarkServer_LogRequest_NopLogger(b *testing.B) {
137
138
accLog := zap.NewNop()
139
142
- b.ResetTimer()
143
-
144
- for i := 0; i < b.N; i++ {
140
+ for b.Loop() {
141
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
142
}
143
}
@@ -165,9 +161,7 @@ func BenchmarkServer_LogRequest_WithTrace(b *testing.B) {
161
buf := io.Discard
162
accLog := testLogger(buf.Write)
163
168
- b.ResetTimer()
169
-
170
- for i := 0; i < b.N; i++ {
164
+ for b.Loop() {
165
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
166
}
167
}
modules/logging/filters.go
+1
-1
@@ -255,7 +255,7 @@ func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
255
256
func (m IPMaskFilter) mask(s string) string {
257
output := ""
258
- for _, value := range strings.Split(s, ",") {
258
+ for value := range strings.SplitSeq(s, ",") {
259
value = strings.TrimSpace(value)
260
host, port, err := net.SplitHostPort(value)
261
if err != nil {
replacer_test.go
+1
-1
@@ -516,7 +516,7 @@ func BenchmarkReplacer(b *testing.B) {
516
},
517
} {
518
b.Run(bm.name, func(b *testing.B) {
519
- for i := 0; i < b.N; i++ {
519
+ for b.Loop() {
520
rep.ReplaceAll(bm.input, bm.empty)
521
}
522
})