@@ -414,7 +414,7 @@ func initializeExternalLocations(
414
414
}
415
415
if ! exactPathExists {
416
416
externalLocExact := http.Location {
417
- Path : exactPath (externalLocPath ),
417
+ Path : exactPath (rule . Path ),
418
418
Type : locType ,
419
419
}
420
420
extLocations = append (extLocations , externalLocExact )
@@ -458,23 +458,22 @@ func updateLocation(
458
458
mirrorPercentage * float64 ,
459
459
) http.Location {
460
460
filters := matchRule .Filters
461
- path := pathRule .Path
462
461
grpc := pathRule .GRPC
463
462
464
463
if filters .InvalidFilter != nil {
465
464
location .Return = & http.Return {Code : http .StatusInternalServerError }
466
465
return location
467
466
}
468
467
469
- location = updateLocationMirrorRoute (location , path , grpc )
468
+ location = updateLocationMirrorRoute (location , pathRule . Path , grpc )
470
469
location .Includes = append (location .Includes , createIncludesFromLocationSnippetsFilters (filters .SnippetsFilters )... )
471
470
472
471
if filters .RequestRedirect != nil {
473
- return updateLocationRedirectFilter (location , filters .RequestRedirect , listenerPort , path )
472
+ return updateLocationRedirectFilter (location , filters .RequestRedirect , listenerPort , pathRule )
474
473
}
475
474
476
- location = updateLocationRewriteFilter (location , filters .RequestURLRewrite , path )
477
- location = updateLocationMirrorFilters (location , filters .RequestMirrors , path , mirrorPercentage )
475
+ location = updateLocationRewriteFilter (location , filters .RequestURLRewrite , pathRule )
476
+ location = updateLocationMirrorFilters (location , filters .RequestMirrors , pathRule . Path , mirrorPercentage )
478
477
location = updateLocationProxySettings (location , matchRule , grpc , keepAliveCheck )
479
478
480
479
return location
@@ -495,9 +494,9 @@ func updateLocationRedirectFilter(
495
494
location http.Location ,
496
495
redirectFilter * dataplane.HTTPRequestRedirectFilter ,
497
496
listenerPort int32 ,
498
- path string ,
497
+ pathRule dataplane. PathRule ,
499
498
) http.Location {
500
- ret , rewrite := createReturnAndRewriteConfigForRedirectFilter (redirectFilter , listenerPort , path )
499
+ ret , rewrite := createReturnAndRewriteConfigForRedirectFilter (redirectFilter , listenerPort , pathRule )
501
500
if rewrite .MainRewrite != "" {
502
501
location .Rewrites = append (location .Rewrites , rewrite .MainRewrite )
503
502
}
@@ -509,9 +508,9 @@ func updateLocationRedirectFilter(
509
508
func updateLocationRewriteFilter (
510
509
location http.Location ,
511
510
rewriteFilter * dataplane.HTTPURLRewriteFilter ,
512
- path string ,
511
+ pathRule dataplane. PathRule ,
513
512
) http.Location {
514
- rewrites := createRewritesValForRewriteFilter (rewriteFilter , path )
513
+ rewrites := createRewritesValForRewriteFilter (rewriteFilter , pathRule )
515
514
if rewrites != nil {
516
515
if location .Type == http .InternalLocationType && rewrites .InternalRewrite != "" {
517
516
location .Rewrites = append (location .Rewrites , rewrites .InternalRewrite )
@@ -658,7 +657,7 @@ func createProxySSLVerify(v *dataplane.VerifyTLS) *http.ProxySSLVerify {
658
657
func createReturnAndRewriteConfigForRedirectFilter (
659
658
filter * dataplane.HTTPRequestRedirectFilter ,
660
659
listenerPort int32 ,
661
- path string ,
660
+ pathRule dataplane. PathRule ,
662
661
) (* http.Return , * rewriteConfig ) {
663
662
if filter == nil {
664
663
return nil , nil
@@ -702,7 +701,12 @@ func createReturnAndRewriteConfigForRedirectFilter(
702
701
703
702
rewrites := & rewriteConfig {}
704
703
if filter .Path != nil {
705
- rewrites .MainRewrite = createMainRewriteForFilters (filter .Path , path )
704
+ mainRewrite := createMainRewriteForFilters (filter .Path , pathRule )
705
+ if mainRewrite == "" {
706
+ // Invalid configuration for the rewrite filter
707
+ return nil , nil
708
+ }
709
+ rewrites .MainRewrite = mainRewrite
706
710
body = fmt .Sprintf ("%s://%s$uri$is_args$args" , scheme , hostnamePort )
707
711
}
708
712
@@ -712,19 +716,26 @@ func createReturnAndRewriteConfigForRedirectFilter(
712
716
}, rewrites
713
717
}
714
718
715
- func createMainRewriteForFilters (pathModifier * dataplane.HTTPPathModifier , path string ) string {
719
+ func createMainRewriteForFilters (pathModifier * dataplane.HTTPPathModifier , pathRule dataplane. PathRule ) string {
716
720
var mainRewrite string
717
721
switch pathModifier .Type {
718
722
case dataplane .ReplaceFullPath :
719
723
mainRewrite = fmt .Sprintf ("^ %s" , pathModifier .Replacement )
720
724
case dataplane .ReplacePrefixMatch :
725
+ // ReplacePrefixMatch is only compatible with a PathPrefix HTTPRouteMatch.
726
+ // ReplaceFullPath is compatible with PathTypeExact/PathTypePrefix/PathTypeRegularExpression HTTPRouteMatch.
727
+ // see https://gateway-api.sigs.k8s.io/reference/spec/?h=replaceprefixmatch#httppathmodifier
728
+ if pathRule .PathType != dataplane .PathTypePrefix {
729
+ return ""
730
+ }
731
+
721
732
filterPrefix := pathModifier .Replacement
722
733
if filterPrefix == "" {
723
734
filterPrefix = "/"
724
735
}
725
736
726
737
// capture everything following the configured prefix up to the first ?, if present.
727
- regex := fmt .Sprintf ("^%s([^?]*)?" , path )
738
+ regex := fmt .Sprintf ("^%s([^?]*)?" , pathRule . Path )
728
739
// replace the configured prefix with the filter prefix, append the captured segment,
729
740
// and include the request arguments stored in nginx variable $args.
730
741
// https://nginx.org/en/docs/http/ngx_http_core_module.html#var_args
@@ -733,13 +744,13 @@ func createMainRewriteForFilters(pathModifier *dataplane.HTTPPathModifier, path
733
744
// if configured prefix does not end in /, but replacement prefix does end in /,
734
745
// then make sure that we *require* but *don't capture* a trailing slash in the request,
735
746
// otherwise we'll get duplicate slashes in the full replacement
736
- if strings .HasSuffix (filterPrefix , "/" ) && ! strings .HasSuffix (path , "/" ) {
737
- regex = fmt .Sprintf ("^%s(?:/([^?]*))?" , path )
747
+ if strings .HasSuffix (filterPrefix , "/" ) && ! strings .HasSuffix (pathRule . Path , "/" ) {
748
+ regex = fmt .Sprintf ("^%s(?:/([^?]*))?" , pathRule . Path )
738
749
}
739
750
740
751
// if configured prefix ends in / we won't capture it for a request (since it's not in the regex),
741
752
// so append it to the replacement prefix if the replacement prefix doesn't already end in /
742
- if strings .HasSuffix (path , "/" ) && ! strings .HasSuffix (filterPrefix , "/" ) {
753
+ if strings .HasSuffix (pathRule . Path , "/" ) && ! strings .HasSuffix (filterPrefix , "/" ) {
743
754
replacement = fmt .Sprintf ("%s/1ドル?$args?" , filterPrefix )
744
755
}
745
756
@@ -749,7 +760,10 @@ func createMainRewriteForFilters(pathModifier *dataplane.HTTPPathModifier, path
749
760
return mainRewrite
750
761
}
751
762
752
- func createRewritesValForRewriteFilter (filter * dataplane.HTTPURLRewriteFilter , path string ) * rewriteConfig {
763
+ func createRewritesValForRewriteFilter (
764
+ filter * dataplane.HTTPURLRewriteFilter ,
765
+ pathRule dataplane.PathRule ,
766
+ ) * rewriteConfig {
753
767
if filter == nil {
754
768
return nil
755
769
}
@@ -758,8 +772,13 @@ func createRewritesValForRewriteFilter(filter *dataplane.HTTPURLRewriteFilter, p
758
772
if filter .Path != nil {
759
773
rewrites .InternalRewrite = "^ $request_uri"
760
774
761
- // for URLRewriteFilter, we add a break to the rewrite to prevent further processing of the request.
762
- rewrites .MainRewrite = fmt .Sprintf ("%s break" , createMainRewriteForFilters (filter .Path , path ))
775
+ mainRewrite := createMainRewriteForFilters (filter .Path , pathRule )
776
+ if mainRewrite == "" {
777
+ // Invalid configuration for the rewrite filter
778
+ return nil
779
+ }
780
+ // For URLRewriteFilter, add "break" to prevent further processing of the request.
781
+ rewrites .MainRewrite = fmt .Sprintf ("%s break" , mainRewrite )
763
782
}
764
783
765
784
return rewrites
@@ -982,14 +1001,18 @@ func createPath(rule dataplane.PathRule) string {
982
1001
switch rule .PathType {
983
1002
case dataplane .PathTypeExact :
984
1003
return exactPath (rule .Path )
1004
+ case dataplane .PathTypePrefix :
1005
+ return fmt .Sprintf ("^~ %s" , rule .Path )
1006
+ case dataplane .PathTypeRegularExpression :
1007
+ return fmt .Sprintf ("~ %s" , rule .Path )
985
1008
default :
986
- return rule . Path
1009
+ return "" // should never happen because path type is validated earlier
987
1010
}
988
1011
}
989
1012
990
1013
func createDefaultRootLocation () http.Location {
991
1014
return http.Location {
992
- Path : "/" ,
1015
+ Path : "= /" ,
993
1016
Return : & http.Return {Code : http .StatusNotFound },
994
1017
}
995
1018
}
0 commit comments