Skip to content

Commit

Permalink
mw: dedup another valToStr copy-paste
Browse files Browse the repository at this point in the history
This one is a tiny bit different, because it doesn't use url.QueryEscape
on the result. So move that out of the helper func.
  • Loading branch information
mvdan authored and buger committed May 29, 2017
1 parent d0a8439 commit 578b66d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
27 changes: 2 additions & 25 deletions middleware_modify_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"net/http"
"net/url"
"reflect"
"strings"

"github.com/TykTechnologies/tyk/apidef"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions middleware_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}

}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 578b66d

Please sign in to comment.