Skip to content

Commit

Permalink
update response content-length when response body is updated by copro…
Browse files Browse the repository at this point in the history
…cess response hook
  • Loading branch information
jeffy-mathew committed Nov 28, 2024
1 parent 35f58ac commit 1928a9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions coprocess/grpc/coprocess_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")),
},
})
})

Expand Down
6 changes: 6 additions & 0 deletions gateway/coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 1928a9e

Please sign in to comment.