Git Product home page Git Product logo

Comments (10)

mathetake avatar mathetake commented on July 3, 2024 2

Hi all, I implemented the API by @pwoelfle as in #117 (comment), and believe fixed this issue.

Please take a look at this example https://github.com/tetratelabs/proxy-wasm-go-sdk/tree/main/examples/http_body for detail. Thanks!

from proxy-wasm-go-sdk.

pwoelfle avatar pwoelfle commented on July 3, 2024 1

@FrimIdan No there is no limitation in Envoy. The buffer can be replaced - even with a shorter length - but the length argument must be equal or greater than the current buffer size (see here).

From my perspective, the Proxy WASM SetHttpRequestBody could be changed towards something similar which I posted above to fit the Envoy logic.

from proxy-wasm-go-sdk.

FalkFiedler avatar FalkFiedler commented on July 3, 2024

image: istio/proxyv2:1.8.2
GolangVersion:"go1.15.5"
I get the same response from the SetHttpResponseBody function.

b := []byte(mynewBody)
		err = SetHTTPResponseBody(b)
		if err != nil {
			proxywasm.LogErrorf("failed to set response body: %v", err)
			return types.ActionContinue
		}

log output

[2021-02-05 13:38:53.257][19][error][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1154] wasm log: failed to set response body: error status returned by host: bad argument

from proxy-wasm-go-sdk.

cookieY avatar cookieY commented on July 3, 2024

@FalkFiedler
maybe that`s the reason https://github.com/proxy-wasm/proxy-wasm-cpp-host/blob/49ed20e895b728aae6b811950a2939ecbaf76f7c/include/proxy-wasm/context.h#L82-L86

from proxy-wasm-go-sdk.

pwoelfle avatar pwoelfle commented on July 3, 2024

I got the same issue on Envoy v1.17.0. The root cause (at least for my problem) was found here: https://github.com/envoyproxy/envoy/blob/v1.17.0/source/extensions/common/wasm/context.cc#L122-L146

Envoy supports the following buffer manipulations:

  • prepend: start and length needs to be zero
  • append: start needs to be greater or equal to the origin buffer size
  • replace: start needs to be zero and length must be greater or equal to the origin buffer size <- here the whole buffer is drained and then the new data are set

The current Proxy WASM Go SDK SetHttpRequestBody implementation assumes that the new body is always equal or greater than the origin body. If it is not the case the envoy returns a bad argument error.

According to this I tested the following successfully:

body is the data which should prepend, append or replace
bodySize is the origin body size given by the OnHttpRequestBody hook.

func PrependToHttpRequestBody(body []byte, bodySize int) (int, error) {
	var bufferData *byte
	if len(body) != 0 {
		bufferData = &body[0]
	}

	st := rawhostcall.ProxySetBufferBytes(types.BufferTypeHttpRequestBody, 0, 0, bufferData, len(body))
	if st != types.StatusOK {
		return -1, types.StatusToError(st)
	}
	return bodySize + len(body), nil
}

func AppendToHttpRequestBody(body []byte, bodySize int) (int, error) {
	var bufferData *byte
	if len(body) != 0 {
		bufferData = &body[0]
	}

	st := rawhostcall.ProxySetBufferBytes(types.BufferTypeHttpRequestBody, bodySize, 0, bufferData, len(body))
	if st != types.StatusOK {
		return -1, types.StatusToError(st)
	}
	return bodySize + len(body), nil
}

func ReplaceHttpRequestBody(body []byte, bodySize int) (int, error) {
	var bufferData *byte
	if len(body) != 0 {
		bufferData = &body[0]
	}

	st := rawhostcall.ProxySetBufferBytes(types.BufferTypeHttpRequestBody, 0, bodySize, bufferData, len(body))
	if st != types.StatusOK {
		return -1, types.StatusToError(st)
	}
	return len(body), nil
}

from proxy-wasm-go-sdk.

djannot avatar djannot commented on July 3, 2024

@pwoelfle do you have a working example with the body manipulation ?
I'm trying to implement a modification of the response body and I get some issues with the fact that I need to update the content-length and can't find a way to do it properly (using ActionPause, ...).

from proxy-wasm-go-sdk.

pwoelfle avatar pwoelfle commented on July 3, 2024

@djannot I faced the same problem. It's not possible to adjust the Content-Length HTTP header. You have to remove this header.

See also in the Rust SDK: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/blob/v0.1.3/examples/http_body.rs#L48

from proxy-wasm-go-sdk.

abhide avatar abhide commented on July 3, 2024

@djannot You will have to remove Content-Length header from HTTP response headers.
Using wasm go sdk; check this: https://github.com/abhide/envoy-wasm-experiments/blob/main/main.go#L72
The issue happens on the http client side; where the only a part of the output (equal to Content-length) gets read and output looks truncated.

I haven't tried setting Content-Length to be equal to sum of original content-length + length introduced by changes from within the wasm filter.

from proxy-wasm-go-sdk.

djannot avatar djannot commented on July 3, 2024

@abhide that's what I've done at the end, because I wasn't able to update the value

from proxy-wasm-go-sdk.

FrimIdan avatar FrimIdan commented on July 3, 2024

@pwoelfle So there is a limitation in Envoy and set buffer can't replace the current buffer with a shorter one?
Did you find/opened an issue in Envoy for it?

from proxy-wasm-go-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.