-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to modify HTTP response body? #19
Comments
@yuval-k Hi man, may I ask for your help? 😂 |
Hi! |
Would be very interested in this. Could potentially enable something like https://github.com/inikulin/parse5/blob/master/packages/parse5-html-rewriting-stream/docs/index.md or https://github.com/cloudflare/lol-html at the proxy level (assuming there's also a way to get body chunks) |
My guess is that we are expected to return |
@esnible The appropriate APIs exists it looks like. See the Rust sdk for an example. https://github.com/proxy-wasm/proxy-wasm-rust-sdk/blob/master/examples/http_body.rs Specifically |
i would not use send_local_reply for this purpose; i believe send_local_reply also sends the headers, so not sure if it can be called when orResponseBody happens; what you probably want to do is manipulate the buffer as it goes through onResponseBody. this way you also minimize memory footprint |
@yuval-k I have a user who wants to use WASM to recreate his current NGINX configuration which uses I'm not an NGINX guy. We thought maybe an EnvoyFilter could detect HTTP status codes on text/html pages, and if it saw a problem do httpCall() and use the results of that call as the body to the original call, keeping the original headers. What do you think? |
Here is the current workaround that I have: @esnible and I are working on this together: function envoy_on_response(response_handle) -- This section of code is for redirecting to maintenance if a 4xx or 5xx status code is detected if string.match(headers:get(":status"), '[4-5].*') and requestData["accept"] ~= nil and string.find(string.lower(requestData["accept"]), "application/json") == nil then basically the goal is that the browser url doesn't change(redirect), it just displays a nice custom html page instead of the enovy test status page. the maintenance microservice is inside the istio mesh. |
Hi, any news on that ? All other sdks (cpp, rust, go, zig) are importing The goal is to be able to do the same thing as in this envoy sample https://github.com/envoyproxy/envoy/blob/6834081f835d609606e77ef84d734e30fc403d58/examples/wasm-cc/envoy_filter_http_wasm_updated_example.cc#L83 |
@cortex93 whatever you can do with other WASM envoy runtime, you should be able to do with this one as well. |
I may have missed something but https://github.com/solo-io/proxy-runtime/blob/master/assembly/imports.ts is not importing proxy_set_buffer_bytes and this sdk is not exporting some helper as others sdk. It seems clear to you but this thread is all about that and after more than a year, no one come with a working solution. Maybe you can share a sample ? |
Hi @yuval-k, I am trying out the proxy_set_buffer_bytes implementation, This basically overrides the bytes in existing buffer, What if there is a need to enrich response body and required additional bytes in buffer? Do you have any suggestion for this use case? |
Sharing below one of working approach. onResponseBody(body_buffer_length: usize, end_of_stream: bool): FilterDataStatusValues {
if (!end_of_stream) {
this.upstream_response_size = this.upstream_response_size + u32(body_buffer_length);
return FilterDataStatusValues.StopIterationAndBuffer;
}
// read the response
let original_response_body_buffer = get_buffer_bytes(BufferTypeValues.HttpResponseBody, 0,
this.upstream_response_size);
let response_body_buffer = ...;
// update the buffer
set_buffer_bytes(BufferTypeValues.HttpResponseBody, 0, response_body_buffer.byteLength, response_body_buffer);
return FilterDataStatusValues.Continue
}
onResponseHeaders(a: u32, end_of_stream: bool): FilterHeadersStatusValues {
// update the content length header
stream_context.headers.response.replace("content-length", new_response_content_length.toString());
}
```
|
I found that there are a lot of materials about modifying HTTP response headers. I want to inject some static codes into the HTTP response body, and how can I do it?
I want to inject some codes into HTML.
I would be very appreciative if somebody can help me.
The text was updated successfully, but these errors were encountered: