diff --git a/middleware_modify_headers.go b/middleware_modify_headers.go index 240316ab3f6..ff40a965464 100644 --- a/middleware_modify_headers.go +++ b/middleware_modify_headers.go @@ -2,8 +2,6 @@ package main import ( "net/http" - "net/url" - "reflect" "strings" "github.com/TykTechnologies/tyk/apidef" @@ -71,30 +69,9 @@ func (t *TransformHeaders) iterateAddHeaders(kv map[string]string, r *http.Reque // Using context key if contextData != nil { metaKey := strings.Replace(nVal, contextLabel, "", 1) - tempVal, ok := contextData[metaKey] + val, ok := contextData[metaKey] if ok { - switch x := tempVal.(type) { - case string: - nVal = x - case []string: - nVal = strings.Join(x, ",") - // Remove empty start - nVal = strings.TrimPrefix(nVal, ",") - case url.Values: - i := 0 - nVal = "" - for key, val := range x { - nVal += key + ":" + strings.Join(val, ",") - if i < len(x)-1 { - nVal += ";" - } - i++ - } - default: - log.Error("Context variable type is not supported: ", reflect.TypeOf(x)) - } - - r.Header.Add(nKey, nVal) + r.Header.Add(nKey, valToStr(val)) } else { log.Warning("Context Data not found for key in map: ", metaKey) } diff --git a/middleware_url_rewrite.go b/middleware_url_rewrite.go index 377f549702f..f2061638983 100644 --- a/middleware_url_rewrite.go +++ b/middleware_url_rewrite.go @@ -66,7 +66,8 @@ func (u URLRewriter) Rewrite(meta *apidef.URLRewriteMeta, path string, useContex log.Debug("Replacing: ", v[0]) if val, ok := contextData[contextKey]; ok { - newpath = strings.Replace(newpath, v[0], valToStr(val), -1) + newpath = strings.Replace(newpath, v[0], + url.QueryEscape(valToStr(val)), -1) } } } @@ -82,7 +83,8 @@ func (u URLRewriter) Rewrite(meta *apidef.URLRewriteMeta, path string, useContex val, ok := session.MetaData[contextKey] if ok { - newpath = strings.Replace(newpath, v[0], valToStr(val), -1) + newpath = strings.Replace(newpath, v[0], + url.QueryEscape(valToStr(val)), -1) } } @@ -112,7 +114,7 @@ func valToStr(v interface{}) string { default: log.Error("Context variable type is not supported: ", reflect.TypeOf(v)) } - return url.QueryEscape(s) + return s } // URLRewriteMiddleware Will rewrite an inbund URL to a matching outbound one, it can also handle dynamic variable substitution