Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

How to use the response from DispatchHttpCall as the final result #390

Closed
TC-wang opened this issue Jul 27, 2023 · 1 comment
Closed

How to use the response from DispatchHttpCall as the final result #390

TC-wang opened this issue Jul 27, 2023 · 1 comment

Comments

@TC-wang
Copy link

TC-wang commented Jul 27, 2023

Describe the bug / error

I am trying to use DispatchHttpCall to call a remote service and get the response, then return the response as the final result. But it didn't work.

  1. Looks like the types.ActionPause in OnHttpRequestBody didn't work.

The original request was still being sent to upstream, the response received by the client was also the result returned by upstream.

The code is as follows:

func (ctx *testContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
	proxywasm.LogInfof("OnHttpRequestHeaders...")
	return types.ActionContinue
}

func (ctx *testContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
	var requestBodySize int
	requestBodySize += bodySize
	if !endOfStream {
		return types.ActionPause
	}
	body, err := proxywasm.GetHttpRequestBody(0, requestBodySize)
	if err != nil {
		proxywasm.LogErrorf("get request body failed: %v", err)
		return types.ActionContinue
	}
	proxywasm.LogErrorf("get request body success: %v", string(body))

	headers := [][2]string{
		{":method", "GET"}, {":authority", "some_authority"}, {"accept", "*/*"}, {":path", "/GET/hello"},
	}
	if _, err := proxywasm.DispatchHttpCall("cluster1", headers, nil, nil,
		5000, httpCallResponseCallback); err != nil {
		proxywasm.LogCriticalf("dispatch httpcall failed: %v", err)
		return types.ActionContinue
	}

	return types.ActionPause
}

func (ctx *testContext) OnHttpResponseHeaders(bodySize int, endOfStream bool) types.Action {
	proxywasm.LogInfof("onHttpResponseHeaders...")
	return types.ActionContinue
}

func (ctx *testContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {
	var responseBodySize int
	responseBodySize += bodySize
	if !endOfStream {
		return types.ActionPause
	}
	body, err := proxywasm.GetHttpResponseBody(0, responseBodySize)
	if err != nil {
		proxywasm.LogErrorf("get response body failed: %v", err)
		return types.ActionContinue
	}

	proxywasm.LogInfof("onHttpResponseBody...: %s", string(body))
	return types.ActionContinue
}

func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
	if err := proxywasm.SendHttpResponse(200, [][2]string{
		{"powered-by", "proxy-wasm-go-sdk!!"},
	}, []byte("success"), -1); err != nil {
		proxywasm.LogErrorf("failed to send local response: %v", err)
		proxywasm.ResumeHttpRequest()
	}
	return
}

What is your Envoy/Istio version?

1.22

What is the SDK version?

proxy-wasm-go-sdk@v0.19.1-0.20220822060051-f9d179a57f8c

What is your TinyGo version?

tinygo version 0.25.0 linux/amd64 (using go version go1.19.9 and LLVM version 14.0.0)

@mathetake
Copy link
Member

This has been asked really really frequently, and it's impossible to do due to the limitation of Proxy-Wasm. See #364 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants