Skip to content

Commit 56a8f87

Browse files
committed
improved error handling and fixed modified flag
1 parent c5c2947 commit 56a8f87

File tree

3 files changed

+66
-35
lines changed

3 files changed

+66
-35
lines changed

internals/proxy/middlewares/body.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"strconv"
88

9+
log "github.com/codeshelldev/secured-signal-api/utils/logger"
910
request "github.com/codeshelldev/secured-signal-api/utils/request"
1011
)
1112

@@ -24,7 +25,11 @@ func (data BodyMiddleware) Use() http.Handler {
2425
messageAliases := data.MessageAliases
2526

2627
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
27-
body := request.GetReqBody(w, req)
28+
body, err := request.GetReqBody(w, req)
29+
30+
if err != nil {
31+
log.Error("Could not get Request Body: ", err.Error())
32+
}
2833

2934
var modifiedBody bool
3035
var bodyData map[string]interface{}

internals/proxy/middlewares/template.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

utils/request/request.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"strings"
99

10-
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1110
"github.com/codeshelldev/secured-signal-api/utils/query"
1211
)
1312

@@ -32,14 +31,14 @@ func (body Body) ToString() string {
3231
func CreateBody(data map[string]interface{}) (Body, error) {
3332
if len(data) <= 0 {
3433
err := errors.New("empty data map")
35-
log.Error("Could not encode Body: ", err.Error())
34+
3635
return Body{Empty: true}, err
3736
}
3837

3938
bytes, err := json.Marshal(data)
4039

4140
if err != nil {
42-
log.Error("Could not encode Body: ", err.Error())
41+
4342
return Body{Empty: true}, err
4443
}
4544

@@ -58,7 +57,7 @@ func GetJsonData(body []byte) (map[string]interface{}, error) {
5857
err := json.Unmarshal(body, &data)
5958

6059
if err != nil {
61-
log.Error("Could not decode Body: ", err.Error())
60+
6261
return nil, err
6362
}
6463

@@ -72,7 +71,7 @@ func GetFormData(body []byte) (map[string]interface{}, error) {
7271

7372
if len(queryData) <= 0 {
7473
err := errors.New("invalid form data")
75-
log.Error("Could not decode Body: ", err.Error())
74+
7675
return nil, err
7776
}
7877

@@ -87,8 +86,6 @@ func GetBody(req *http.Request) ([]byte, error) {
8786
bodyBytes, err := io.ReadAll(req.Body)
8887

8988
if err != nil {
90-
log.Error("Could not read Body: ", err.Error())
91-
9289
req.Body.Close()
9390

9491
return nil, err
@@ -98,23 +95,19 @@ func GetBody(req *http.Request) ([]byte, error) {
9895
return bodyBytes, nil
9996
}
10097

101-
func GetReqBody(w http.ResponseWriter, req *http.Request) Body {
98+
func GetReqBody(w http.ResponseWriter, req *http.Request) (Body, error) {
10299
bytes, err := GetBody(req)
103100

104101
var isEmpty bool
105102

106103
if err != nil {
107104
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
108105

109-
isEmpty = true
106+
return Body{Empty: true}, err
110107
}
111108

112109
if len(bytes) <= 0 {
113-
isEmpty = true
114-
}
115-
116-
if isEmpty {
117-
return Body{Empty: true}
110+
return Body{Empty: true}, errors.New("request body is empty")
118111
}
119112

120113
var data map[string]interface{}
@@ -125,12 +118,16 @@ func GetReqBody(w http.ResponseWriter, req *http.Request) Body {
125118

126119
if err != nil {
127120
http.Error(w, "Bad Request: invalid JSON", http.StatusBadRequest)
121+
122+
return Body{Empty: true}, err
128123
}
129124
case Form:
130125
data, err = GetFormData(bytes)
131126

132127
if err != nil {
133128
http.Error(w, "Bad Request: invalid Form", http.StatusBadRequest)
129+
130+
return Body{Empty: true}, err
134131
}
135132
}
136133

@@ -140,7 +137,7 @@ func GetReqBody(w http.ResponseWriter, req *http.Request) Body {
140137
Raw: bytes,
141138
Data: data,
142139
Empty: isEmpty,
143-
}
140+
}, nil
144141
}
145142

146143
func GetBodyType(req *http.Request) BodyType {

0 commit comments

Comments
 (0)