Skip to content

Commit 5c3a6e9

Browse files
authored
fix: improve body reading (#127)
1 parent e3d6c7d commit 5c3a6e9

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

internals/proxy/middlewares/mapping.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ func mappingHandler(next http.Handler) http.Handler {
6161
if modifiedBody {
6262
body.Data = bodyData
6363

64-
log.Debug("Applied Data Aliasing: ", body.Data)
64+
err := body.Write(req)
65+
66+
if err != nil {
67+
http.Error(w, "Internal Error", http.StatusInternalServerError)
68+
return
69+
}
6570

66-
body.Write(req)
71+
log.Debug("Applied Data Aliasing: ", body.Data)
6772
}
6873

6974
next.ServeHTTP(w, req)

internals/proxy/middlewares/message.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ func messageHandler(next http.Handler) http.Handler {
6060
if modifiedBody {
6161
body.Data = bodyData
6262

63-
log.Debug("Applied Message Templating: ", body.Data)
63+
err := body.Write(req)
64+
65+
if err != nil {
66+
http.Error(w, "Internal Error", http.StatusInternalServerError)
67+
return
68+
}
6469

65-
body.Write(req)
70+
log.Debug("Applied Message Templating: ", body.Data)
6671
}
6772

6873
next.ServeHTTP(w, req)

internals/proxy/middlewares/template.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ func templateHandler(next http.Handler) http.Handler {
7272
if modifiedBody {
7373
body.Data = bodyData
7474

75-
log.Debug("Applied Body Templating: ", body.Data)
75+
err := body.Write(req)
7676

77-
body.Write(req)
77+
if err != nil {
78+
http.Error(w, "Internal Error", http.StatusInternalServerError)
79+
return
80+
}
81+
82+
log.Debug("Applied Body Templating: ", body.Data)
7883
}
7984

8085
if req.URL.Path != "" {

utils/request/request.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ func (body Body) ToString() string {
3030
return string(body.Raw)
3131
}
3232

33-
func (body Body) Write(req *http.Request) {
33+
func (body *Body) Write(req *http.Request) error {
34+
newBody, err := CreateBody(body.Data)
35+
36+
if err != nil {
37+
return err
38+
}
39+
40+
body = &newBody
41+
3442
bodyLength := len(body.Raw)
3543

3644
if req.ContentLength != int64(bodyLength) {
@@ -39,6 +47,8 @@ func (body Body) Write(req *http.Request) {
3947
}
4048

4149
req.Body = io.NopCloser(bytes.NewReader(body.Raw))
50+
51+
return nil
4252
}
4353

4454
func CreateBody(data map[string]any) (Body, error) {

0 commit comments

Comments
 (0)