mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-19 07:43:17 +00:00
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>
This commit is contained in:
parent
b15ed9b084
commit
5125fbed41
11 changed files with 19 additions and 30 deletions
|
@ -207,7 +207,7 @@ func TestETags(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkLoad(b *testing.B) {
|
func BenchmarkLoad(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
Load(testCfg, true)
|
Load(testCfg, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -535,7 +535,7 @@ func BenchmarkMatchExpressionMatch(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
tc.expression.MatchWithError(req)
|
tc.expression.MatchWithError(req)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -452,8 +452,7 @@ func (rw *responseWriter) init() {
|
||||||
|
|
||||||
func hasVaryValue(hdr http.Header, target string) bool {
|
func hasVaryValue(hdr http.Header, target string) bool {
|
||||||
for _, vary := range hdr.Values("Vary") {
|
for _, vary := range hdr.Values("Vary") {
|
||||||
vals := strings.Split(vary, ",")
|
for val := range strings.SplitSeq(vary, ",") {
|
||||||
for _, val := range vals {
|
|
||||||
if strings.EqualFold(strings.TrimSpace(val), target) {
|
if strings.EqualFold(strings.TrimSpace(val), target) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -478,7 +477,7 @@ func AcceptedEncodings(r *http.Request, preferredOrder []string) []string {
|
||||||
|
|
||||||
prefs := []encodingPreference{}
|
prefs := []encodingPreference{}
|
||||||
|
|
||||||
for _, accepted := range strings.Split(acceptEncHeader, ",") {
|
for accepted := range strings.SplitSeq(acceptEncHeader, ",") {
|
||||||
parts := strings.Split(accepted, ";")
|
parts := strings.Split(accepted, ";")
|
||||||
encName := strings.ToLower(strings.TrimSpace(parts[0]))
|
encName := strings.ToLower(strings.TrimSpace(parts[0]))
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
func BenchmarkOpenResponseWriter(b *testing.B) {
|
func BenchmarkOpenResponseWriter(b *testing.B) {
|
||||||
enc := new(Encode)
|
enc := new(Encode)
|
||||||
for n := 0; n < b.N; n++ {
|
for b.Loop() {
|
||||||
enc.openResponseWriter("test", nil, false)
|
enc.openResponseWriter("test", nil, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -947,7 +947,7 @@ func BenchmarkHeaderREMatcher(b *testing.B) {
|
||||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
||||||
for run := 0; run < b.N; run++ {
|
for b.Loop() {
|
||||||
match.MatchWithError(req)
|
match.MatchWithError(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -992,7 +992,6 @@ func TestVarREMatcher(t *testing.T) {
|
||||||
expect: true,
|
expect: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
i := i // capture range value
|
|
||||||
tc := tc // capture range value
|
tc := tc // capture range value
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
@ -1180,8 +1179,7 @@ func BenchmarkLargeHostMatcher(b *testing.B) {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
matcher.MatchWithError(req)
|
matcher.MatchWithError(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1194,8 +1192,7 @@ func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
|
||||||
|
|
||||||
match := MatchHost{"localhost"}
|
match := MatchHost{"localhost"}
|
||||||
|
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
match.MatchWithError(req)
|
match.MatchWithError(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1212,8 +1209,7 @@ func BenchmarkHostMatcherWithPlaceholder(b *testing.B) {
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}
|
match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}
|
||||||
|
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
match.MatchWithError(req)
|
match.MatchWithError(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func parseLinkHeader(header string) []linkResource {
|
||||||
return resources
|
return resources
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, link := range strings.Split(header, comma) {
|
for link := range strings.SplitSeq(header, comma) {
|
||||||
l := linkResource{params: make(map[string]string)}
|
l := linkResource{params: make(map[string]string)}
|
||||||
|
|
||||||
li, ri := strings.Index(link, "<"), strings.Index(link, ">")
|
li, ri := strings.Index(link, "<"), strings.Index(link, ">")
|
||||||
|
@ -51,7 +51,7 @@ func parseLinkHeader(header string) []linkResource {
|
||||||
|
|
||||||
l.uri = strings.TrimSpace(link[li+1 : ri])
|
l.uri = strings.TrimSpace(link[li+1 : ri])
|
||||||
|
|
||||||
for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
|
for param := range strings.SplitSeq(strings.TrimSpace(link[ri+1:]), semicolon) {
|
||||||
before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
|
before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
|
||||||
key := strings.TrimSpace(before)
|
key := strings.TrimSpace(before)
|
||||||
if key == "" {
|
if key == "" {
|
||||||
|
|
|
@ -1356,7 +1356,7 @@ func upgradeType(h http.Header) string {
|
||||||
// See RFC 7230, section 6.1
|
// See RFC 7230, section 6.1
|
||||||
func removeConnectionHeaders(h http.Header) {
|
func removeConnectionHeaders(h http.Header) {
|
||||||
for _, f := range h["Connection"] {
|
for _, f := range h["Connection"] {
|
||||||
for _, sf := range strings.Split(f, ",") {
|
for sf := range strings.SplitSeq(f, ",") {
|
||||||
if sf = textproto.TrimString(sf); sf != "" {
|
if sf = textproto.TrimString(sf); sf != "" {
|
||||||
h.Del(sf)
|
h.Del(sf)
|
||||||
}
|
}
|
||||||
|
|
|
@ -985,10 +985,10 @@ func trustedRealClientIP(r *http.Request, headers []string, clientIP string) str
|
||||||
|
|
||||||
// Since there can be many header values, we need to
|
// Since there can be many header values, we need to
|
||||||
// join them together before splitting to get the full list
|
// join them together before splitting to get the full list
|
||||||
allValues := strings.Split(strings.Join(values, ","), ",")
|
allValues := strings.SplitSeq(strings.Join(values, ","), ",")
|
||||||
|
|
||||||
// Get first valid left-most IP address
|
// Get first valid left-most IP address
|
||||||
for _, part := range allValues {
|
for part := range allValues {
|
||||||
// Some proxies may retain the port number, so split if possible
|
// Some proxies may retain the port number, so split if possible
|
||||||
host, _, err := net.SplitHostPort(part)
|
host, _, err := net.SplitHostPort(part)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -116,9 +116,7 @@ func BenchmarkServer_LogRequest(b *testing.B) {
|
||||||
buf := io.Discard
|
buf := io.Discard
|
||||||
accLog := testLogger(buf.Write)
|
accLog := testLogger(buf.Write)
|
||||||
|
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
|
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,9 +137,7 @@ func BenchmarkServer_LogRequest_NopLogger(b *testing.B) {
|
||||||
|
|
||||||
accLog := zap.NewNop()
|
accLog := zap.NewNop()
|
||||||
|
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
|
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,9 +161,7 @@ func BenchmarkServer_LogRequest_WithTrace(b *testing.B) {
|
||||||
buf := io.Discard
|
buf := io.Discard
|
||||||
accLog := testLogger(buf.Write)
|
accLog := testLogger(buf.Write)
|
||||||
|
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
|
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -255,7 +255,7 @@ func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
|
||||||
|
|
||||||
func (m IPMaskFilter) mask(s string) string {
|
func (m IPMaskFilter) mask(s string) string {
|
||||||
output := ""
|
output := ""
|
||||||
for _, value := range strings.Split(s, ",") {
|
for value := range strings.SplitSeq(s, ",") {
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
host, port, err := net.SplitHostPort(value)
|
host, port, err := net.SplitHostPort(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -516,7 +516,7 @@ func BenchmarkReplacer(b *testing.B) {
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
b.Run(bm.name, func(b *testing.B) {
|
b.Run(bm.name, func(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
rep.ReplaceAll(bm.input, bm.empty)
|
rep.ReplaceAll(bm.input, bm.empty)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue