Skip to content

Commit 00f22a4

Browse files
authored
chore: improve body reading (#123)
1 parent 8b276c6 commit 00f22a4

File tree

4 files changed

+23
-56
lines changed

4 files changed

+23
-56
lines changed

internals/proxy/middlewares/mapping.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package middlewares
22

33
import (
4-
"bytes"
5-
"io"
64
"net/http"
7-
"strconv"
85

96
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
107
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
@@ -62,25 +59,13 @@ func mappingHandler(next http.Handler) http.Handler {
6259
}
6360

6461
if modifiedBody {
65-
modifiedBody, err := request.CreateBody(bodyData)
62+
body.Data = bodyData
6663

67-
if err != nil {
68-
http.Error(w, "Internal Error", http.StatusInternalServerError)
69-
return
70-
}
71-
72-
body = modifiedBody
73-
74-
strData := body.ToString()
64+
log.Debug("Applied Data Aliasing: ", body.Data)
7565

76-
log.Debug("Applied Data Aliasing: ", strData)
77-
78-
req.ContentLength = int64(len(strData))
79-
req.Header.Set("Content-Length", strconv.Itoa(len(strData)))
66+
body.Write(req)
8067
}
8168

82-
req.Body = io.NopCloser(bytes.NewReader(body.Raw))
83-
8469
next.ServeHTTP(w, req)
8570
})
8671
}

internals/proxy/middlewares/message.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package middlewares
22

33
import (
4-
"bytes"
5-
"io"
64
"net/http"
7-
"strconv"
85

96
log "github.com/codeshelldev/secured-signal-api/utils/logger"
107
request "github.com/codeshelldev/secured-signal-api/utils/request"
@@ -61,25 +58,13 @@ func messageHandler(next http.Handler) http.Handler {
6158
}
6259

6360
if modifiedBody {
64-
modifiedBody, err := request.CreateBody(bodyData)
61+
body.Data = bodyData
6562

66-
if err != nil {
67-
http.Error(w, "Internal Error", http.StatusInternalServerError)
68-
return
69-
}
70-
71-
body = modifiedBody
72-
73-
strData := body.ToString()
63+
log.Debug("Applied Message Templating: ", body.Data)
7464

75-
log.Debug("Applied Message Templating: ", strData)
76-
77-
req.ContentLength = int64(len(strData))
78-
req.Header.Set("Content-Length", strconv.Itoa(len(strData)))
65+
body.Write(req)
7966
}
8067

81-
req.Body = io.NopCloser(bytes.NewReader(body.Raw))
82-
8368
next.ServeHTTP(w, req)
8469
})
8570
}

internals/proxy/middlewares/template.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package middlewares
22

33
import (
4-
"bytes"
5-
"io"
64
"maps"
75
"net/http"
86
"net/url"
97
"regexp"
10-
"strconv"
118
"strings"
129

1310
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
@@ -73,25 +70,13 @@ func templateHandler(next http.Handler) http.Handler {
7370
}
7471

7572
if modifiedBody {
76-
modifiedBody, err := request.CreateBody(bodyData)
73+
body.Data = bodyData
7774

78-
if err != nil {
79-
http.Error(w, "Internal Error", http.StatusInternalServerError)
80-
return
81-
}
82-
83-
body = modifiedBody
75+
log.Debug("Applied Body Templating: ", body.Data)
8476

85-
strData := body.ToString()
86-
87-
log.Debug("Applied Body Templating: ", strData)
88-
89-
req.ContentLength = int64(len(strData))
90-
req.Header.Set("Content-Length", strconv.Itoa(len(strData)))
77+
body.Write(req)
9178
}
9279

93-
req.Body = io.NopCloser(bytes.NewReader(body.Raw))
94-
9580
if req.URL.Path != "" {
9681
var modified bool
9782

utils/request/request.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"io"
88
"net/http"
9+
"strconv"
910
"strings"
1011

1112
"github.com/codeshelldev/secured-signal-api/utils/query"
@@ -29,6 +30,17 @@ func (body Body) ToString() string {
2930
return string(body.Raw)
3031
}
3132

33+
func (body Body) Write(req *http.Request) {
34+
bodyLength := len(body.Raw)
35+
36+
if req.ContentLength != int64(bodyLength) {
37+
req.ContentLength = int64(bodyLength)
38+
req.Header.Set("Content-Length", strconv.Itoa(bodyLength))
39+
}
40+
41+
req.Body = io.NopCloser(bytes.NewReader(body.Raw))
42+
}
43+
3244
func CreateBody(data map[string]any) (Body, error) {
3345
if len(data) <= 0 {
3446
err := errors.New("empty data map")
@@ -84,7 +96,7 @@ func GetFormData(body []byte) (map[string]any, error) {
8496
}
8597

8698
func GetBody(req *http.Request) ([]byte, error) {
87-
bodyBytes, err := io.ReadAll(req.Body)
99+
bodyBytes, err := io.ReadAll(io.LimitReader(req.Body, 5<<20))
88100

89101
req.Body.Close()
90102

@@ -175,4 +187,4 @@ func GetBodyType(req *http.Request) BodyType {
175187
default:
176188
return Unknown
177189
}
178-
}
190+
}

0 commit comments

Comments
 (0)