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

Commit

Permalink
Fix panic in dispatched callback on getting empty http trailers. (#218)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Oct 27, 2021
1 parent 1775575 commit 2041438
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Test_dispatch_call_on_tick(t *testing.T) {
if checkMessage(stdErr.String(), []string{
fmt.Sprintf("called %d for contextID=1", count),
fmt.Sprintf("called %d for contextID=2", count),
":status: 200", ":status: 503",
}, nil) {
count++
}
Expand Down
7 changes: 6 additions & 1 deletion examples/dispatch_call_on_tick/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ static_resources:
- "*"
routes:
- match:
prefix: "/"
prefix: "/ok"
direct_response:
status: 200
body:
inline_string: "example body\n"
- match:
prefix: "/fail"
direct_response:
status: 503

http_filters:
- name: envoy.filters.http.router
typed_config: {}
Expand Down
30 changes: 27 additions & 3 deletions examples/dispatch_call_on_tick/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package main

import (
"crypto/rand"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
Expand Down Expand Up @@ -55,16 +57,38 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
ctx.callBack = func(numHeaders, bodySize, numTrailers int) {
ctx.cnt++
proxywasm.LogInfof("called %d for contextID=%d", ctx.cnt, ctx.contextID)
headers, err := proxywasm.GetHttpCallResponseHeaders()
if err != nil && err != types.ErrorStatusNotFound {
panic(err)
}
for _, h := range headers {
proxywasm.LogInfof("response header for the dispatched call: %s: %s", h[0], h[1])
}
headers, err = proxywasm.GetHttpCallResponseTrailers()
if err != nil && err != types.ErrorStatusNotFound {
panic(err)
}
for _, h := range headers {
proxywasm.LogInfof("response trailer for the dispatched call: %s: %s", h[0], h[1])
}
}
return types.OnPluginStartStatusOK
}

// Override types.DefaultPluginContext.
func (ctx *pluginContext) OnTick() {
hs := [][2]string{
{":method", "GET"}, {":authority", "some_authority"}, {":path", "/path/to/service"}, {"accept", "*/*"},
headers := [][2]string{
{":method", "GET"}, {":authority", "some_authority"}, {"accept", "*/*"},
}
// Pick random value to select the request path.
buf := make([]byte, 1)
_, _ = rand.Read(buf)
if buf[0]%2 == 0 {
headers = append(headers, [2]string{":path", "/ok"})
} else {
headers = append(headers, [2]string{":path", "/fail"})
}
if _, err := proxywasm.DispatchHttpCall("web_service", hs, nil, nil, 5000, ctx.callBack); err != nil {
if _, err := proxywasm.DispatchHttpCall("web_service", headers, nil, nil, 5000, ctx.callBack); err != nil {
proxywasm.LogCriticalf("dispatch httpcall failed: %v", err)
}
}
2 changes: 2 additions & 0 deletions proxywasm/hostcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ func getMap(mapType internal.MapType) ([][2]string, error) {
st := internal.ProxyGetHeaderMapPairs(mapType, &raw, &rvs)
if st != internal.StatusOK {
return nil, internal.StatusToError(st)
} else if raw == nil {
return nil, types.ErrorStatusNotFound
}

bs := internal.RawBytePtrToByteSlice(raw, rvs)
Expand Down
8 changes: 8 additions & 0 deletions proxywasm/proxytest/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ func (h *httpHostEmulator) httpHostEmulatorProxyGetHeaderMapPairs(mapType intern
panic("unreachable: maybe a bug in this host emulation or SDK")
}

if len(m) == 0 {
// The host might reutrn OK without setting the data pointer,
// if there's nothing to pass to Wasm VM.
*returnValueData = nil
*returnValueSize = 0
return internal.StatusOK
}

*returnValueData = &m[0]
*returnValueSize = len(m)
return internal.StatusOK
Expand Down

0 comments on commit 2041438

Please sign in to comment.