Skip to content

Commit f0410c9

Browse files
AlinsRanronething
authored andcommitted
fix(conformance-test): HTTPRoutePathRewrite (#2597)
1 parent fdd4887 commit f0410c9

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
@@ -59,7 +59,7 @@ GO_LDFLAGS ?= "-X=$(VERSYM)=$(VERSION) -X=$(GITSHASYM)=$(GITSHA) -X=$(BUILDOSSYM
5959
# gateway-api
6060
GATEAY_API_VERSION ?= v1.3.0
6161
## https://github.com/kubernetes-sigs/gateway-api/blob/v1.3.0/pkg/features/httproute.go
62-
SUPPORTED_EXTENDED_FEATURES = "HTTPRouteDestinationPortMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteRequestMirror,HTTPRouteSchemeRedirect,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,GatewayPort8080,HTTPRouteHostRewrite,HTTPRouteQueryParamMatching"
62+
SUPPORTED_EXTENDED_FEATURES = "HTTPRouteDestinationPortMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteRequestMirror,HTTPRouteSchemeRedirect,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,GatewayPort8080,HTTPRouteHostRewrite,HTTPRouteQueryParamMatching,HTTPRoutePathRewrite"
6363
CONFORMANCE_TEST_REPORT_OUTPUT ?= $(DIR)/apisix-ingress-controller-conformance-report.yaml
6464
## https://github.com/kubernetes-sigs/gateway-api/blob/v1.3.0/conformance/utils/suite/profiles.go
6565
CONFORMANCE_PROFILES ?= GATEWAY-HTTP,GATEWAY-GRPC

internal/adc/translator/httproute.go

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

0 commit comments

Comments
 (0)