@@ -26,23 +26,34 @@ func (data TemplateMiddleware) Use() http.Handler {
2626 VARIABLES := data .Variables
2727
2828 return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
29- var body request.Body
30- body = request .GetReqBody (w , req )
29+ body , err := request .GetReqBody (w , req )
30+
31+ if err != nil {
32+ log .Error ("Could not get Request Body: " , err .Error ())
33+ }
3134
3235 bodyData := map [string ]interface {}{}
3336
3437 var modifiedBody bool
3538
3639 if ! body .Empty {
37- bodyData = templateJSON ( body . Data , VARIABLES )
40+ var modified bool
3841
39- modifiedBody = true
42+ bodyData , modified = templateJSON (body .Data , VARIABLES )
43+
44+ if modified {
45+ modifiedBody = true
46+ }
4047 }
4148
4249 if req .URL .RawQuery != "" {
43- req . URL . RawQuery , bodyData = templateQuery ( req . URL , VARIABLES )
50+ var modified bool
4451
45- modifiedBody = true
52+ req .URL .RawQuery , bodyData , modified = templateQuery (req .URL , VARIABLES )
53+
54+ if modified {
55+ modifiedBody = true
56+ }
4657 }
4758
4859 if modifiedBody {
@@ -65,7 +76,7 @@ func (data TemplateMiddleware) Use() http.Handler {
6576
6677 req .Body = io .NopCloser (bytes .NewReader (body .Raw ))
6778
68- req .URL .Path = templatePath (req .URL , VARIABLES )
79+ req .URL .Path , _ = templatePath (req .URL , VARIABLES )
6980
7081 next .ServeHTTP (w , req )
7182 })
@@ -87,15 +98,17 @@ func renderTemplate(name string, tmplStr string, data any) (string, error) {
8798 return buf .String (), nil
8899}
89100
90- func templateJSON (data map [string ]interface {}, variables map [string ]interface {}) map [string ]interface {} {
101+ func templateJSON (data map [string ]interface {}, variables map [string ]interface {}) (map [string ]interface {}, bool ) {
102+ var modified bool
103+
91104 for k , v := range data {
92105 str , ok := v .(string )
93106
94107 if ok {
95108 re , err := regexp .Compile (`{{\s*\.([A-Za-z_][A-Za-z0-9_]*)\s*}}` )
96109
97110 if err != nil {
98- log .Error ("Encountered Error while Compiling Regex: " , err .Error ())
111+ log .Error ("Error while Compiling Regex: " , err .Error ())
99112 }
100113
101114 matches := re .FindAllStringSubmatch (str , - 1 )
@@ -114,38 +127,50 @@ func templateJSON(data map[string]interface{}, variables map[string]interface{})
114127
115128 data [k ] = strings .ReplaceAll (str , string (variable ), tmplStr [0 ])
116129 }
130+
131+ modified = true
117132 } else if len (matches ) == 1 {
118133 tmplKey := matches [0 ][1 ]
119134
120135 data [k ] = variables [tmplKey ]
136+
137+ modified = true
121138 }
122139 }
123140 }
124141
125- return data
142+ return data , modified
126143}
127144
128- func templatePath (reqUrl * url.URL , VARIABLES interface {}) string {
145+ func templatePath (reqUrl * url.URL , VARIABLES interface {}) (string , bool ) {
146+ var modified bool
147+
129148 reqPath , err := url .PathUnescape (reqUrl .Path )
130149
131150 if err != nil {
132151 log .Error ("Error while Escaping Path: " , err .Error ())
133- return reqUrl .Path
152+ return reqUrl .Path , modified
134153 }
135154
136155 reqPath , err = renderTemplate ("path" , reqPath , VARIABLES )
137156
138157 if err != nil {
139158 log .Error ("Could not Template Path: " , err .Error ())
140- return reqUrl .Path
159+ return reqUrl .Path , modified
141160 }
142161
143- log .Debug ("Applied Path Templating: " , reqPath )
162+ if reqUrl .Path != reqPath {
163+ log .Debug ("Applied Path Templating: " , reqPath )
164+
165+ modified = true
166+ }
144167
145- return reqPath
168+ return reqPath , modified
146169}
147170
148- func templateQuery (reqUrl * url.URL , VARIABLES interface {}) (string , map [string ]interface {}) {
171+ func templateQuery (reqUrl * url.URL , VARIABLES interface {}) (string , map [string ]interface {}, bool ) {
172+ var modified bool
173+
149174 data := map [string ]interface {}{}
150175
151176 decodedQuery , _ := url .QueryUnescape (reqUrl .RawQuery )
@@ -168,9 +193,13 @@ func templateQuery(reqUrl *url.URL, VARIABLES interface{}) (string, map[string]i
168193 }
169194 }
170195
171- reqUrl .RawQuery = modifiedQuery .Encode ()
196+ reqRawQuery := modifiedQuery .Encode ()
197+
198+ if reqUrl .RawQuery != reqRawQuery {
199+ log .Debug ("Applied Query Templating: " , templatedQuery )
172200
173- log .Debug ("Applied Query Templating: " , templatedQuery )
201+ modified = true
202+ }
174203
175- return reqUrl . RawQuery , data
204+ return reqRawQuery , data , modified
176205}
0 commit comments