caddyhttp: refactor to use reflect.TypeFor (#7187)

This commit is contained in:
cui 2025-08-19 07:08:46 +08:00 committed by GitHub
parent 05acc5131e
commit b15ed9b084
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 12 deletions

View file

@ -20,7 +20,6 @@ import (
"net" "net"
"net/http" "net/http"
"net/netip" "net/netip"
"reflect"
"strings" "strings"
"github.com/google/cel-go/cel" "github.com/google/cel-go/cel"
@ -109,7 +108,7 @@ func (MatchRemoteIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
[]*cel.Type{cel.ListType(cel.StringType)}, []*cel.Type{cel.ListType(cel.StringType)},
// function to convert a constant list of strings to a MatchPath instance. // function to convert a constant list of strings to a MatchPath instance.
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList) strList, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -222,7 +221,7 @@ func (MatchClientIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
[]*cel.Type{cel.ListType(cel.StringType)}, []*cel.Type{cel.ListType(cel.StringType)},
// function to convert a constant list of strings to a MatchPath instance. // function to convert a constant list of strings to a MatchPath instance.
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList) strList, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -23,7 +23,6 @@ import (
"net/textproto" "net/textproto"
"net/url" "net/url"
"path" "path"
"reflect"
"regexp" "regexp"
"runtime" "runtime"
"slices" "slices"
@ -373,7 +372,7 @@ func (MatchHost) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"host_match_request_list", "host_match_request_list",
[]*cel.Type{cel.ListType(cel.StringType)}, []*cel.Type{cel.ListType(cel.StringType)},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList) strList, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -654,7 +653,7 @@ func (MatchPath) CELLibrary(ctx caddy.Context) (cel.Library, error) {
[]*cel.Type{cel.ListType(cel.StringType)}, []*cel.Type{cel.ListType(cel.StringType)},
// function to convert a constant list of strings to a MatchPath instance. // function to convert a constant list of strings to a MatchPath instance.
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList) strList, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -733,7 +732,7 @@ func (MatchPathRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"path_regexp_request_string_string", "path_regexp_request_string_string",
[]*cel.Type{cel.StringType, cel.StringType}, []*cel.Type{cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList) params, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -802,7 +801,7 @@ func (MatchMethod) CELLibrary(_ caddy.Context) (cel.Library, error) {
"method_request_list", "method_request_list",
[]*cel.Type{cel.ListType(cel.StringType)}, []*cel.Type{cel.ListType(cel.StringType)},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList) strList, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1173,7 +1172,7 @@ func (MatchHeaderRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"header_regexp_request_string_string", "header_regexp_request_string_string",
[]*cel.Type{cel.StringType, cel.StringType}, []*cel.Type{cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList) params, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1196,7 +1195,7 @@ func (MatchHeaderRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"header_regexp_request_string_string_string", "header_regexp_request_string_string_string",
[]*cel.Type{cel.StringType, cel.StringType, cel.StringType}, []*cel.Type{cel.StringType, cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList) params, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -28,6 +28,8 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
) )
var stringSliceType = reflect.TypeFor[[]string]()
func init() { func init() {
caddy.RegisterModule(VarsMiddleware{}) caddy.RegisterModule(VarsMiddleware{})
caddy.RegisterModule(VarsMatcher{}) caddy.RegisterModule(VarsMatcher{})
@ -353,7 +355,7 @@ func (MatchVarsRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"vars_regexp_request_string_string", "vars_regexp_request_string_string",
[]*cel.Type{cel.StringType, cel.StringType}, []*cel.Type{cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList) params, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err
@ -376,7 +378,7 @@ func (MatchVarsRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"vars_regexp_request_string_string_string", "vars_regexp_request_string_string_string",
[]*cel.Type{cel.StringType, cel.StringType, cel.StringType}, []*cel.Type{cel.StringType, cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) { func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{}) refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList) params, err := data.ConvertToNative(refStringList)
if err != nil { if err != nil {
return nil, err return nil, err