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
caddyhttp: refactor to use reflect.TypeFor (#7187)
cui committed
Aug 19, 2025 at 07:08 UTC
b15ed9b0844dd7b73977f4c6dacfb3348579ce4a
3 files changed
+12
-12
modules/caddyhttp/ip_matchers.go
+2
-3
@@ -20,7 +20,6 @@ import (
20
"net"
21
"net/http"
22
"net/netip"
23
- "reflect"
23
"strings"
24
25
"github.com/google/cel-go/cel"
@@ -109,7 +108,7 @@ func (MatchRemoteIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
108
[]*cel.Type{cel.ListType(cel.StringType)},
109
// function to convert a constant list of strings to a MatchPath instance.
110
func(data ref.Val) (RequestMatcherWithError, error) {
112
- refStringList := reflect.TypeOf([]string{})
111
+ refStringList := stringSliceType
112
strList, err := data.ConvertToNative(refStringList)
113
if err != nil {
114
return nil, err
@@ -222,7 +221,7 @@ func (MatchClientIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
221
[]*cel.Type{cel.ListType(cel.StringType)},
222
// function to convert a constant list of strings to a MatchPath instance.
223
func(data ref.Val) (RequestMatcherWithError, error) {
225
- refStringList := reflect.TypeOf([]string{})
224
+ refStringList := stringSliceType
225
strList, err := data.ConvertToNative(refStringList)
226
if err != nil {
227
return nil, err
modules/caddyhttp/matchers.go
+6
-7
@@ -23,7 +23,6 @@ import (
23
"net/textproto"
24
"net/url"
25
"path"
26
- "reflect"
26
"regexp"
27
"runtime"
28
"slices"
@@ -373,7 +372,7 @@ func (MatchHost) CELLibrary(ctx caddy.Context) (cel.Library, error) {
372
"host_match_request_list",
373
[]*cel.Type{cel.ListType(cel.StringType)},
374
func(data ref.Val) (RequestMatcherWithError, error) {
376
- refStringList := reflect.TypeOf([]string{})
375
+ refStringList := stringSliceType
376
strList, err := data.ConvertToNative(refStringList)
377
if err != nil {
378
return nil, err
@@ -654,7 +653,7 @@ func (MatchPath) CELLibrary(ctx caddy.Context) (cel.Library, error) {
653
[]*cel.Type{cel.ListType(cel.StringType)},
654
// function to convert a constant list of strings to a MatchPath instance.
655
func(data ref.Val) (RequestMatcherWithError, error) {
657
- refStringList := reflect.TypeOf([]string{})
656
+ refStringList := stringSliceType
657
strList, err := data.ConvertToNative(refStringList)
658
if err != nil {
659
return nil, err
@@ -733,7 +732,7 @@ func (MatchPathRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
732
"path_regexp_request_string_string",
733
[]*cel.Type{cel.StringType, cel.StringType},
734
func(data ref.Val) (RequestMatcherWithError, error) {
736
- refStringList := reflect.TypeOf([]string{})
735
+ refStringList := stringSliceType
736
params, err := data.ConvertToNative(refStringList)
737
if err != nil {
738
return nil, err
@@ -802,7 +801,7 @@ func (MatchMethod) CELLibrary(_ caddy.Context) (cel.Library, error) {
801
"method_request_list",
802
[]*cel.Type{cel.ListType(cel.StringType)},
803
func(data ref.Val) (RequestMatcherWithError, error) {
805
- refStringList := reflect.TypeOf([]string{})
804
+ refStringList := stringSliceType
805
strList, err := data.ConvertToNative(refStringList)
806
if err != nil {
807
return nil, err
@@ -1173,7 +1172,7 @@ func (MatchHeaderRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
1172
"header_regexp_request_string_string",
1173
[]*cel.Type{cel.StringType, cel.StringType},
1174
func(data ref.Val) (RequestMatcherWithError, error) {
1176
- refStringList := reflect.TypeOf([]string{})
1175
+ refStringList := stringSliceType
1176
params, err := data.ConvertToNative(refStringList)
1177
if err != nil {
1178
return nil, err
@@ -1196,7 +1195,7 @@ func (MatchHeaderRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
1195
"header_regexp_request_string_string_string",
1196
[]*cel.Type{cel.StringType, cel.StringType, cel.StringType},
1197
func(data ref.Val) (RequestMatcherWithError, error) {
1199
- refStringList := reflect.TypeOf([]string{})
1198
+ refStringList := stringSliceType
1199
params, err := data.ConvertToNative(refStringList)
1200
if err != nil {
1201
return nil, err
modules/caddyhttp/vars.go
+4
-2
@@ -28,6 +28,8 @@ import (
28
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
29
)
30
31
+var stringSliceType = reflect.TypeFor[[]string]()
32
+
33
func init() {
34
caddy.RegisterModule(VarsMiddleware{})
35
caddy.RegisterModule(VarsMatcher{})
@@ -353,7 +355,7 @@ func (MatchVarsRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
355
"vars_regexp_request_string_string",
356
[]*cel.Type{cel.StringType, cel.StringType},
357
func(data ref.Val) (RequestMatcherWithError, error) {
356
- refStringList := reflect.TypeOf([]string{})
358
+ refStringList := stringSliceType
359
params, err := data.ConvertToNative(refStringList)
360
if err != nil {
361
return nil, err
@@ -376,7 +378,7 @@ func (MatchVarsRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
378
"vars_regexp_request_string_string_string",
379
[]*cel.Type{cel.StringType, cel.StringType, cel.StringType},
380
func(data ref.Val) (RequestMatcherWithError, error) {
379
- refStringList := reflect.TypeOf([]string{})
381
+ refStringList := stringSliceType
382
params, err := data.ConvertToNative(refStringList)
383
if err != nil {
384
return nil, err