Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions internals/proxy/middlewares/mapping.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package middlewares

import (
"bytes"
"io"
"net/http"
"strconv"

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

if modifiedBody {
modifiedBody, err := request.CreateBody(bodyData)
body.Data = bodyData

if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}

body = modifiedBody

strData := body.ToString()
log.Debug("Applied Data Aliasing: ", body.Data)

log.Debug("Applied Data Aliasing: ", strData)

req.ContentLength = int64(len(strData))
req.Header.Set("Content-Length", strconv.Itoa(len(strData)))
body.Write(req)
}

req.Body = io.NopCloser(bytes.NewReader(body.Raw))

next.ServeHTTP(w, req)
})
}
Expand Down
21 changes: 3 additions & 18 deletions internals/proxy/middlewares/message.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package middlewares

import (
"bytes"
"io"
"net/http"
"strconv"

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

if modifiedBody {
modifiedBody, err := request.CreateBody(bodyData)
body.Data = bodyData

if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}

body = modifiedBody

strData := body.ToString()
log.Debug("Applied Message Templating: ", body.Data)

log.Debug("Applied Message Templating: ", strData)

req.ContentLength = int64(len(strData))
req.Header.Set("Content-Length", strconv.Itoa(len(strData)))
body.Write(req)
}

req.Body = io.NopCloser(bytes.NewReader(body.Raw))

next.ServeHTTP(w, req)
})
}
Expand Down
21 changes: 3 additions & 18 deletions internals/proxy/middlewares/template.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package middlewares

import (
"bytes"
"io"
"maps"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"

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

if modifiedBody {
modifiedBody, err := request.CreateBody(bodyData)
body.Data = bodyData

if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}

body = modifiedBody
log.Debug("Applied Body Templating: ", body.Data)

strData := body.ToString()

log.Debug("Applied Body Templating: ", strData)

req.ContentLength = int64(len(strData))
req.Header.Set("Content-Length", strconv.Itoa(len(strData)))
body.Write(req)
}

req.Body = io.NopCloser(bytes.NewReader(body.Raw))

if req.URL.Path != "" {
var modified bool

Expand Down
16 changes: 14 additions & 2 deletions utils/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io"
"net/http"
"strconv"
"strings"

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

func (body Body) Write(req *http.Request) {
bodyLength := len(body.Raw)

if req.ContentLength != int64(bodyLength) {
req.ContentLength = int64(bodyLength)
req.Header.Set("Content-Length", strconv.Itoa(bodyLength))
}

req.Body = io.NopCloser(bytes.NewReader(body.Raw))
}

func CreateBody(data map[string]any) (Body, error) {
if len(data) <= 0 {
err := errors.New("empty data map")
Expand Down Expand Up @@ -84,7 +96,7 @@ func GetFormData(body []byte) (map[string]any, error) {
}

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

req.Body.Close()

Expand Down Expand Up @@ -175,4 +187,4 @@ func GetBodyType(req *http.Request) BodyType {
default:
return Unknown
}
}
}