Skip to content

Commit 1513202

Browse files
authored
fix(conformance-test): HTTPRoutePathRewrite (#2597)
1 parent 1afb9ac commit 1513202

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ GO_LDFLAGS ?= "-X=$(VERSYM)=$(VERSION) -X=$(GITSHASYM)=$(GITSHA) -X=$(BUILDOSSYM
5252
# gateway-api
5353
GATEAY_API_VERSION ?= v1.3.0
5454
## https://github.com/kubernetes-sigs/gateway-api/blob/v1.3.0/pkg/features/httproute.go
55-
SUPPORTED_EXTENDED_FEATURES = "HTTPRouteDestinationPortMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteRequestMirror,HTTPRouteSchemeRedirect,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,GatewayPort8080,HTTPRouteHostRewrite,HTTPRouteQueryParamMatching"
55+
SUPPORTED_EXTENDED_FEATURES = "HTTPRouteDestinationPortMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteRequestMirror,HTTPRouteSchemeRedirect,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,GatewayPort8080,HTTPRouteHostRewrite,HTTPRouteQueryParamMatching,HTTPRoutePathRewrite"
5656
CONFORMANCE_TEST_REPORT_OUTPUT ?= $(DIR)/apisix-ingress-controller-conformance-report.yaml
5757
## https://github.com/kubernetes-sigs/gateway-api/blob/v1.3.0/conformance/utils/suite/profiles.go
5858
CONFORMANCE_PROFILES ?= GATEWAY-HTTP,GATEWAY-GRPC,GATEWAY-TLS

internal/adc/translator/httproute.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,33 @@ func (t *Translator) fillPluginFromURLRewriteFilter(plugins adctypes.Plugins, ur
118118
}
119119
prefixPaths = append(prefixPaths, *match.Path.Value)
120120
}
121-
regexPattern := "^(" + strings.Join(prefixPaths, "|") + ")" + "/(.*)"
121+
if len(prefixPaths) == 0 || urlRewrite.Path.ReplacePrefixMatch == nil {
122+
break
123+
}
124+
prefixGroup := "(" + strings.Join(prefixPaths, "|") + ")"
122125
replaceTarget := *urlRewrite.Path.ReplacePrefixMatch
123-
regexTarget := replaceTarget + "/$2"
124-
125-
plugin.RewriteTargetRegex = []string{
126-
regexPattern,
127-
regexTarget,
126+
// Handle ReplacePrefixMatch path rewrite
127+
// If replaceTarget == "/", special handling is required to avoid
128+
// producing double slashes or empty paths.
129+
var regexPattern, regexTarget string
130+
if replaceTarget == "/" {
131+
// Match either "/prefix" or "/prefix/<remainder>"
132+
// Pattern captures the remainder (if any) without a leading slash.
133+
// Template reconstructs "/" + remainder, resulting in:
134+
// /prefix/three → /three
135+
// /prefix → /
136+
regexPattern = "^" + prefixGroup + "(?:/(.*))?$"
137+
regexTarget = "/" + "$2"
138+
} else {
139+
// Match either "/prefix" or "/prefix/<remainder>"
140+
// Pattern captures the remainder (including leading slash) as $2.
141+
// Template appends it to replaceTarget:
142+
// /prefix/one/two → /one/two
143+
// /prefix/one → /one
144+
regexPattern = "^" + prefixGroup + "(/.*)?$"
145+
regexTarget = replaceTarget + "$2"
128146
}
147+
plugin.RewriteTargetRegex = []string{regexPattern, regexTarget}
129148
}
130149
}
131150
}

0 commit comments

Comments
 (0)