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

proxywasm.SendHttpResponse in OnHttpResponseBody results resetting the connection. #364

Closed
sunil220552 opened this issue Mar 8, 2023 · 1 comment

Comments

@sunil220552
Copy link

Based on the content of the response body, I want to either

  • Stream the response body as it is
  • Send HTTP/1.1 403 Forbidden response by using proxywasm.SendHttpResponse.
func (ctx *httpFTC) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {

	if !endOfStream {
		// Wait until we see the entire body to replace.
		return types.ActionPause
	}
	originalBody, err := proxywasm.GetHttpResponseBody(0, bodySize)
	if err != nil {
		proxywasm.LogErrorf("failed to get response body: %v", err)
		return types.ActionContinue
	}
         
        localReplay := false 
        /// Evaluate the content of the originalBody and set localReplay

	if localReplay == true {
		if err := proxywasm.SendHttpResponse(403, [][2]string{
			{"powered-by", "proxy-wasm-go-sdk!!"}, {"content-type", "text/html"},
		}, []byte(blcokedPageBody), -1); err != nil {
			proxywasm.LogErrorf("failed to send local response: %v", err)
			return types.ActionContinue
		}
		proxywasm.LogInfof("Blocked")
		return types.ActionPause
	}

	return types.ActionContinue
}

Describe the bug / error

By the time I evaluate the response body in the filter, the envoy would already have completed streaming response headers. So upon proxywasm.SendHttpResponse envoy resets the connection with the below log. This is expected as per the documentation.

[2023-03-07 21:07:21.978][2341335][debug][http] [source/common/http/filter_manager.cc:936] [C0][S6738853039800735951] Resetting stream due to . Prior headers have already been sent
[2023-03-07 21:07:21.978][2341335][debug][http] [source/common/http/conn_manager_impl.cc:205] [C0][S6738853039800735951] doEndStream() resetting stream

Before evaluating the body, is there any way to delay sending response headers. I tried to see if I could achieve this by cascading two WASM filters, but no luck.

I think it's possible by cascading the native filter (using FilterHeadersStatus::StopIteration in encodeHeaders) and the WASM filter. Is there any way to achieve this without using the native filter?

What is your Envoy/Istio version?

1.25.2

What is the SDK version?

v0.20.0

What is your TinyGo version?

> tinygo version
tinygo version 0.27.0 darwin/amd64 (using go version go1.18.1 and LLVM version 15.0.0)

URL or snippet of your code including Envoy configuration

Additional context (Optional)

@mathetake
Copy link
Member

at the point when executing OnHttpResponseBody, the headers have already been sent to the client, and that's the limitation due to the design of Proxy-Wasm. proxy-wasm/proxy-wasm-cpp-host#143

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