Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

fix: body callbacks return the whole buffered body size #418

Merged
merged 1 commit into from
Dec 23, 2023
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
23 changes: 9 additions & 14 deletions examples/http_body/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,14 @@ type setBodyContext struct {
// Embed the default root http context here,
// so that we don't need to reimplement all the methods.
types.DefaultHttpContext
modifyResponse bool
totalRequestBodySize int
totalResponseBodySize int
bufferOperation string
modifyResponse bool
bufferOperation string
}

// Override types.DefaultHttpContext.
func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
mode, err := proxywasm.GetHttpRequestHeader("buffer-replace-at")
if mode == "response" {
if err == nil && mode == "response" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious what's the reason for this check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err is retrieved but not used. I was in doubt about whether not even retrieve it, or to ensure it was nil before relying on mode

ctx.modifyResponse = true
}

Expand Down Expand Up @@ -112,13 +110,12 @@ func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) typ
return types.ActionContinue
}

ctx.totalRequestBodySize += bodySize
M4tteoP marked this conversation as resolved.
Show resolved Hide resolved
if !endOfStream {
// Wait until we see the entire body to replace.
return types.ActionPause
}

originalBody, err := proxywasm.GetHttpRequestBody(0, ctx.totalRequestBodySize)
// Being the body never been sent upstream so far, bodySize is the total size of the body received.
originalBody, err := proxywasm.GetHttpRequestBody(0, bodySize)
if err != nil {
proxywasm.LogErrorf("failed to get request body: %v", err)
return types.ActionContinue
Expand Down Expand Up @@ -160,13 +157,12 @@ func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) ty
return types.ActionContinue
}

ctx.totalResponseBodySize += bodySize
if !endOfStream {
// Wait until we see the entire body to replace.
return types.ActionPause
}

originalBody, err := proxywasm.GetHttpResponseBody(0, ctx.totalResponseBodySize)
originalBody, err := proxywasm.GetHttpResponseBody(0, bodySize)
if err != nil {
proxywasm.LogErrorf("failed to get response body: %v", err)
return types.ActionContinue
Expand All @@ -189,22 +185,21 @@ func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) ty
}

type echoBodyContext struct {
// mbed the default plugin context
// Embed the default plugin context
// so that you don't need to reimplement all the methods by yourself.
types.DefaultHttpContext
totalRequestBodySize int
}

// Override types.DefaultHttpContext.
func (ctx *echoBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
ctx.totalRequestBodySize += bodySize
ctx.totalRequestBodySize = bodySize
if !endOfStream {
// Wait until we see the entire body to replace.
return types.ActionPause
}

// Send the request body as the response body.
body, _ := proxywasm.GetHttpRequestBody(0, ctx.totalRequestBodySize)
body, _ := proxywasm.GetHttpRequestBody(0, bodySize)
if err := proxywasm.SendHttpResponse(200, nil, body, -1); err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/http_body/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestEchoBodyContext_OnHttpRequestBody(t *testing.T) {
// Create http context.
id := host.InitializeHttpContext()

for _, frame := range []string{"frame1...", "frame2..."} {
for _, frame := range []string{"frame1...", "frame2...", "frame3..."} {
// Call OnRequestHeaders without "content-length"
action := host.CallOnRequestBody(id, []byte(frame), false /* end of stream */)

Expand All @@ -282,7 +282,7 @@ func TestEchoBodyContext_OnHttpRequestBody(t *testing.T) {
localResponse := host.GetSentLocalResponse(id)
require.NotNil(t, localResponse)
require.Equal(t, uint32(200), localResponse.StatusCode)
require.Equal(t, "frame1...frame2...", string(localResponse.Data))
require.Equal(t, "frame1...frame2...frame3...", string(localResponse.Data))
})
})
}
Expand Down
4 changes: 2 additions & 2 deletions proxywasm/proxytest/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (h *httpHostEmulator) CallOnRequestBody(contextID uint32, body []byte, endO

cs.requestBody = append(cs.requestBodyBuffer, body...)
cs.action = internal.ProxyOnRequestBody(contextID,
len(body), endOfStream)
len(cs.requestBody), endOfStream)
if cs.action == types.ActionPause {
// Buffering requested
cs.requestBodyBuffer = cs.requestBody
Expand All @@ -435,7 +435,7 @@ func (h *httpHostEmulator) CallOnResponseBody(contextID uint32, body []byte, end

cs.responseBody = append(cs.responseBodyBuffer, body...)
cs.action = internal.ProxyOnResponseBody(contextID,
len(body), endOfStream)
len(cs.responseBody), endOfStream)
if cs.action == types.ActionPause {
// Buffering requested
cs.responseBodyBuffer = cs.responseBody
Expand Down
6 changes: 4 additions & 2 deletions proxywasm/proxytest/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ func TestBodyBuffering(t *testing.T) {
name: "buffered",
buffered: true,
action: types.ActionPause,
logged: "11111",
// The first chunk has been buffered, therefore it will be retrieved when calling GetHttpRequestBody at the end of stream.
logged: "1111122222",
},
{
name: "unbuffered",
buffered: false,
action: types.ActionContinue,
logged: "22222",
// The first chunk has not been buffered, therefore it will not be retrieved when calling GetHttpRequestBody at the end of stream.
logged: "22222",
},
}

Expand Down