From 1928a9e048999422428d891061fc3ce577678b0d Mon Sep 17 00:00:00 2001 From: Jeffy Mathew Date: Sat, 23 Nov 2024 16:28:16 +0100 Subject: [PATCH 1/2] update response content-length when response body is updated by coprocess response hook --- coprocess/grpc/coprocess_grpc_test.go | 5 +++++ gateway/coprocess.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/coprocess/grpc/coprocess_grpc_test.go b/coprocess/grpc/coprocess_grpc_test.go index 9a670dae777..eeee6983b98 100644 --- a/coprocess/grpc/coprocess_grpc_test.go +++ b/coprocess/grpc/coprocess_grpc_test.go @@ -5,12 +5,14 @@ import ( "context" "encoding/json" "fmt" + "github.com/TykTechnologies/tyk/header" "io/ioutil" "math/rand" "mime/multipart" "net" "net/http" "os" + "strconv" "strings" "testing" "time" @@ -567,6 +569,9 @@ func TestGRPCDispatch(t *testing.T) { Code: http.StatusOK, Headers: headers, BodyMatch: "newbody", + HeadersMatch: map[string]string{ + header.ContentLength: strconv.Itoa(len("newbody")), + }, }) }) diff --git a/gateway/coprocess.go b/gateway/coprocess.go index 1e547f8ca17..0d6c30a5074 100644 --- a/gateway/coprocess.go +++ b/gateway/coprocess.go @@ -598,6 +598,12 @@ func (h *CustomMiddlewareResponseHook) HandleResponse(rw http.ResponseWriter, re bodyBuf := bytes.NewBuffer(retObject.Response.RawBody) res.Body = ioutil.NopCloser(bodyBuf) + //set response body length with the size of response body returned from the hook + //so that it is updated accordingly in the response object + responseBodyLen := len(retObject.Response.RawBody) + res.ContentLength = int64(responseBodyLen) + res.Header.Set("Content-Length", fmt.Sprintf("%d", responseBodyLen)) + res.StatusCode = int(retObject.Response.StatusCode) return nil } From 18a82c0db2bcb757a954a6627620332c6a19c7b5 Mon Sep 17 00:00:00 2001 From: Jeffy Mathew Date: Thu, 28 Nov 2024 17:36:21 +0100 Subject: [PATCH 2/2] lint --- coprocess/grpc/coprocess_grpc_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coprocess/grpc/coprocess_grpc_test.go b/coprocess/grpc/coprocess_grpc_test.go index eeee6983b98..a97fb7c41b8 100644 --- a/coprocess/grpc/coprocess_grpc_test.go +++ b/coprocess/grpc/coprocess_grpc_test.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/TykTechnologies/tyk/header" "io/ioutil" "math/rand" "mime/multipart" @@ -17,6 +16,8 @@ import ( "testing" "time" + "github.com/TykTechnologies/tyk/header" + "github.com/stretchr/testify/assert" "google.golang.org/grpc"