Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly set content length for body parameters #29

Merged
merged 1 commit into from
Dec 31, 2023
Merged
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
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