From e428095e3ce2e8f15140d44496e1d2a2fc80d360 Mon Sep 17 00:00:00 2001 From: Jeroen van Dongen Date: Sun, 5 Aug 2018 14:38:51 +0200 Subject: [PATCH] fixed rewrites for paths not ending in / --- internal/ingress/controller/template/template.go | 6 +++--- .../ingress/controller/template/template_test.go | 15 ++++++++++++--- test/e2e/annotations/rewrite.go | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index dfcf1227b4..39cfa5ddbe 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -470,7 +470,6 @@ func buildProxyPass(host string, b interface{}, loc interface{}, dynamicConfigur var xForwardedPrefix string if location.Rewrite.AddBaseURL { - // path has a slash suffix, so that it can be connected with baseuri directly bPath := fmt.Sprintf("%s$escaped_base_uri", path) regex := `(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)` scheme := "$scheme" @@ -494,15 +493,16 @@ subs_filter '%v' '$1' ro; // ie /something to / return fmt.Sprintf(` rewrite (?i)%s(.*) /$1 break; -rewrite (?i)%s / break; +rewrite (?i)%s$ / break; %v%v %s%s; %v`, path, location.Path, xForwardedPrefix, proxyPass, proto, upstreamName, abu) } return fmt.Sprintf(` rewrite (?i)%s(.*) %s/$1 break; +rewrite (?i)%s$ %s/ break; %v%v %s%s; -%v`, path, location.Rewrite.Target, xForwardedPrefix, proxyPass, proto, upstreamName, abu) +%v`, path, location.Rewrite.Target, location.Path, location.Rewrite.Target, xForwardedPrefix, proxyPass, proto, upstreamName, abu) } // default proxy_pass diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index 3fc2ecf088..7068d295e5 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -123,6 +123,7 @@ var ( "~* /", ` rewrite (?i)/(.*) /jenkins/$1 break; +rewrite (?i)/$ /jenkins/ break; proxy_pass http://upstream-name; `, false, @@ -137,7 +138,7 @@ proxy_pass http://upstream-name; `~* ^/something\/?(?.*)`, ` rewrite (?i)/something/(.*) /$1 break; -rewrite (?i)/something / break; +rewrite (?i)/something$ / break; proxy_pass http://upstream-name; `, false, @@ -152,6 +153,7 @@ proxy_pass http://upstream-name; "~* ^/end-with-slash/(?.*)", ` rewrite (?i)/end-with-slash/(.*) /not-root/$1 break; +rewrite (?i)/end-with-slash/$ /not-root/ break; proxy_pass http://upstream-name; `, false, @@ -166,6 +168,7 @@ proxy_pass http://upstream-name; `~* ^/something-complex\/?(?.*)`, ` rewrite (?i)/something-complex/(.*) /not-root/$1 break; +rewrite (?i)/something-complex$ /not-root/ break; proxy_pass http://upstream-name; `, false, @@ -180,6 +183,7 @@ proxy_pass http://upstream-name; "~* /", ` rewrite (?i)/(.*) /jenkins/$1 break; +rewrite (?i)/$ /jenkins/ break; proxy_pass http://upstream-name; set_escape_uri $escaped_base_uri $baseuri; @@ -197,7 +201,7 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1