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

Commit

Permalink
fix: host emulator returns body size not chunk size, clarifies http_b…
Browse files Browse the repository at this point in the history
…ody example
  • Loading branch information
M4tteoP committed Dec 23, 2023
1 parent 1b9daaf commit 0306f95
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
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" {
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
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

0 comments on commit 0306f95

Please sign in to comment.