proxy-wasm-cpp-sdk icon indicating copy to clipboard operation
proxy-wasm-cpp-sdk copied to clipboard

modify response headers values based on response body contents

Open webngt opened this issue 5 years ago • 6 comments

Is it possible to modify response header values based on response body contents? For example, if I match a certain pattern in onResponseBody I want to replace :status header value from 200 to 401 or 403 value.

According to documentation, Header API is only effective from onRequestHeader/onResponseHeader. So my task seems to be impossible to implement. Or maybe you can point to some kind of workaround?

Cheers, Alexander.

webngt avatar Oct 21 '20 22:10 webngt

I have a similar use case. We need to update crc-32 header after we modify response body. However, according to the docs, header modification is not allowed when we process body. Is there a workaround available?

aysee avatar Apr 01 '21 17:04 aysee

@webngt buffering complete responses is not supported, sorry (see: https://github.com/proxy-wasm/proxy-wasm-cpp-host/issues/143#issuecomment-812287392).

@aysee if the consumer supports it, then you should be sending updated CRC-32 value in HTTP trailers instead of headers.

PiotrSikora avatar Apr 02 '21 02:04 PiotrSikora

@PiotrSikora thank you for your suggestion of using HTTP trailers! I tried to use them in my sandbox project, but onResponseTrailers and onRequestTrailers are not being called. Do you have any example of how to use them? Here is what I have in my Context:

  FilterTrailersStatus onRequestTrailers(uint32_t) override;
  FilterTrailersStatus onResponseTrailers(uint32_t) override;

Here is the implementation itself (just logs for debug purposes):

FilterTrailersStatus ExampleContext::onRequestTrailers(uint32_t) {
    LOG_INFO(std::string("onRequestTrailers ") + std::to_string(id()));
    return FilterTrailersStatus::Continue;
}

FilterTrailersStatus ExampleContext::onResponseTrailers(uint32_t) {
    LOG_INFO(std::string("onResponseTrailers ") + std::to_string(id()));
    return FilterTrailersStatus::Continue;
}

on*Body methods also return FilterDataStatus::Continue

aysee avatar Apr 03 '21 00:04 aysee

@PiotrSikora thank you for your suggestion of using HTTP trailers! I tried to use them in my sandbox project, but onResponseTrailers and onRequestTrailers are not being called. Do you have any example of how to use them?

Those callbacks are called only if trailers already exist (i.e. if they were received from upstream)... and I've now realized that we don't support setting trailers if they don't already exist in Envoy, see: https://github.com/envoyproxy/envoy/pull/15831.

PiotrSikora avatar Apr 03 '21 10:04 PiotrSikora

@aysee that PR was merged and it's included in the Envoy v1.18.0 release. There are examples for how to use it in that PR.

PiotrSikora avatar Apr 16 '21 07:04 PiotrSikora

@PiotrSikora thank you so much for such a quick implementation! I appreciate it!

aysee avatar Apr 19 '21 23:04 aysee