Skip to content

Commit

Permalink
optionally dont strip target url trailing slash in reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblakeley committed Nov 9, 2018
1 parent be59f76 commit eae8675
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ type APIDefinition struct {
PreserveHostHeader bool `bson:"preserve_host_header" json:"preserve_host_header"`
ListenPath string `bson:"listen_path" json:"listen_path"`
TargetURL string `bson:"target_url" json:"target_url"`
TargetURLDontStripSlash bool `bson:"target_url_dont_strip_slash" json:"target_url_dont_strip_slash"`
StripListenPath bool `bson:"strip_listen_path" json:"strip_listen_path"`
EnableLoadBalancing bool `bson:"enable_load_balancing" json:"enable_load_balancing"`
Targets []string `bson:"target_list" json:"target_list"`
Expand Down
9 changes: 6 additions & 3 deletions reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func TykNewSingleHostReverseProxy(target *url.URL, spec *APISpec) *ReverseProxy
if targetToUse == target {
req.URL.Scheme = targetToUse.Scheme
req.URL.Host = targetToUse.Host
req.URL.Path = singleJoiningSlash(targetToUse.Path, req.URL.Path)
req.URL.Path = singleJoiningSlash(targetToUse.Path, req.URL.Path, spec.Proxy.TargetURLDontStripSlash)
if req.URL.RawPath != "" {
req.URL.RawPath = singleJoiningSlash(targetToUse.Path, req.URL.RawPath)
req.URL.RawPath = singleJoiningSlash(targetToUse.Path, req.URL.RawPath, spec.Proxy.TargetURLDontStripSlash)
}
}
if !spec.Proxy.PreserveHostHeader {
Expand Down Expand Up @@ -317,7 +317,10 @@ func defaultTransport() *http.Transport {
}
}

func singleJoiningSlash(a, b string) string {
func singleJoiningSlash(a, b string, dontStripTargetSlash bool) string {
if dontStripTargetSlash && len(b) == 0 {
return a
}
a = strings.TrimRight(a, "/")
b = strings.TrimLeft(b, "/")
if len(b) > 0 {
Expand Down
20 changes: 17 additions & 3 deletions reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestWrappedServeHTTP(t *testing.T) {
}

func TestSingleJoiningSlash(t *testing.T) {
tests := []struct {
testsFalse := []struct {
a, b, want string
}{
{"foo", "", "foo"},
Expand All @@ -119,9 +119,23 @@ func TestSingleJoiningSlash(t *testing.T) {
{"foo/", "/bar", "foo/bar"},
{"foo//", "//bar", "foo/bar"},
}
for _, tc := range tests {
for _, tc := range testsFalse {
t.Run(fmt.Sprintf("%s+%s", tc.a, tc.b), func(t *testing.T) {
got := singleJoiningSlash(tc.a, tc.b, false)
if got != tc.want {
t.Fatalf("want %s, got %s", tc.want, got)
}
})
}
testsTrue := []struct {
a, b, want string
}{
{"foo/", "", "foo/"},
{"foo", "", "foo"},
}
for _, tc := range testsTrue {
t.Run(fmt.Sprintf("%s+%s", tc.a, tc.b), func(t *testing.T) {
got := singleJoiningSlash(tc.a, tc.b)
got := singleJoiningSlash(tc.a, tc.b, true)
if got != tc.want {
t.Fatalf("want %s, got %s", tc.want, got)
}
Expand Down

0 comments on commit eae8675

Please sign in to comment.