Skip to content

Commit

Permalink
Merge pull request #29 from delta10/fix/content-length-for-body-params
Browse files Browse the repository at this point in the history
fix: correctly set content length for body parameters
  • Loading branch information
bartjkdp authored Dec 31, 2023
2 parents ab93a86 + 76a19a4 commit c56f13c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions cmd/filter-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {

utils.DelHopHeaders(r.Header)

var filterParams map[string]interface{}
var bodyFilterParams map[string]interface{}
if path.RequestRewrite != "" {
body, _ := io.ReadAll(r.Body)

Expand All @@ -74,11 +74,11 @@ func main() {
continue
}

filterParams = v.(map[string]interface{})
bodyFilterParams = v.(map[string]interface{})
}
}

authorizationStatusCode, authorizationResponse := authorizeRequestWithService(config, backend, path, r, filterParams)
authorizationStatusCode, authorizationResponse := authorizeRequestWithService(config, backend, path, r, bodyFilterParams)
if authorizationStatusCode != http.StatusOK {
writeError(w, authorizationStatusCode, "unauthorized request")
return
Expand Down Expand Up @@ -118,22 +118,27 @@ func main() {
// Copy query parameters to backend
fullBackendURL.RawQuery = r.URL.Query().Encode()

backendRequest, err := http.NewRequest(r.Method, fullBackendURL.String(), nil)
if err != nil {
writeError(w, http.StatusInternalServerError, "could not construct backend request")
return
}

if len(filterParams) > 0 {
backendRequestBody, err := json.MarshalIndent(filterParams, "", " ")
var backendRequest *http.Request
if len(bodyFilterParams) > 0 {
backendRequestBody, err := json.MarshalIndent(bodyFilterParams, "", " ")
if err != nil {
writeError(w, http.StatusInternalServerError, "could not marshal json")
return
}

buffer := bytes.NewBuffer(backendRequestBody)
backendRequest, err = http.NewRequest(r.Method, fullBackendURL.String(), bytes.NewReader(backendRequestBody))
if err != nil {
writeError(w, http.StatusInternalServerError, "could not construct backend request")
return
}

backendRequest.Header.Set("Content-Type", "application/json")
backendRequest.Body = io.NopCloser(buffer)
} else {
backendRequest, err = http.NewRequest(r.Method, fullBackendURL.String(), nil)
if err != nil {
writeError(w, http.StatusInternalServerError, "could not construct backend request")
return
}
}

tlsConfig := &tls.Config{}
Expand Down

0 comments on commit c56f13c

Please sign in to comment.