forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasm: Add API to get buffered body data. (envoyproxy#437) (envoyproxy…
…#177) Now, if a callback previously returned StopIterationAndBuffer, the various "getBuffer" methods will return the buffered data. But if the callback previously returned anything else, then the methods will only return the current chunk. Signed-off-by: Gregory Brail <gregbrail@google.com> Signed-off-by: Piotr Sikora <piotrsikora@google.com> Co-authored-by: Greg Brail <gbrail@users.noreply.github.com>
- Loading branch information
1 parent
6fc4752
commit 80d0be0
Showing
6 changed files
with
223 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
all: headers_cpp.wasm async_call_cpp.wasm metadata_cpp.wasm grpc_call_cpp.wasm shared_cpp.wasm queue_cpp.wasm http_callout_cpp.wasm grpc_callout_cpp.wasm | ||
all: headers_cpp.wasm async_call_cpp.wasm metadata_cpp.wasm grpc_call_cpp.wasm shared_cpp.wasm queue_cpp.wasm body_cpp.wasm http_callout_cpp.wasm grpc_callout_cpp.wasm | ||
|
||
include ../../../../../../api/wasm/cpp/Makefile.base_lite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// NOLINT(namespace-envoy) | ||
#include <cassert> | ||
#include <cstdlib> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include "proxy_wasm_intrinsics.h" | ||
|
||
class ExampleContext : public Context { | ||
public: | ||
explicit ExampleContext(uint32_t id, RootContext* root) : Context(id, root) {} | ||
|
||
FilterHeadersStatus onRequestHeaders(uint32_t) override; | ||
FilterHeadersStatus onResponseHeaders(uint32_t) override; | ||
FilterDataStatus onRequestBody(size_t body_buffer_length, bool end_of_stream) override; | ||
FilterDataStatus onResponseBody(size_t body_buffer_length, bool end_of_stream) override; | ||
|
||
private: | ||
FilterDataStatus onBody(BufferType bt, size_t bufLen, bool end); | ||
static void logBody(BufferType bt); | ||
|
||
std::string test_op_; | ||
int num_chunks_ = 0; | ||
}; | ||
static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleContext)); | ||
|
||
FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t) { | ||
test_op_ = getRequestHeader("x-test-operation")->toString(); | ||
return FilterHeadersStatus::Continue; | ||
} | ||
|
||
FilterHeadersStatus ExampleContext::onResponseHeaders(uint32_t) { | ||
test_op_ = getResponseHeader("x-test-operation")->toString(); | ||
return FilterHeadersStatus::Continue; | ||
} | ||
|
||
void ExampleContext::logBody(BufferType bt) { | ||
size_t bufferedSize; | ||
uint32_t flags; | ||
getBufferStatus(bt, &bufferedSize, &flags); | ||
auto body = getBufferBytes(bt, 0, bufferedSize); | ||
logError(std::string("onRequestBody ") + std::string(body->view())); | ||
} | ||
|
||
FilterDataStatus ExampleContext::onBody(BufferType bt, size_t bufLen, bool end) { | ||
if (test_op_ == "ReadBody") { | ||
auto body = getBufferBytes(bt, 0, bufLen); | ||
logError("onRequestBody " + std::string(body->view())); | ||
|
||
} else if (test_op_ == "BufferBody") { | ||
logBody(bt); | ||
return end ? FilterDataStatus::Continue : FilterDataStatus::StopIterationAndBuffer; | ||
|
||
} else if (test_op_ == "BufferTwoBodies") { | ||
logBody(bt); | ||
num_chunks_++; | ||
if (end || num_chunks_ > 2) { | ||
return FilterDataStatus::Continue; | ||
} | ||
return FilterDataStatus::StopIterationAndBuffer; | ||
|
||
} else { | ||
// This is a test and the test was configured incorrectly. | ||
logError("Invalid test op " + test_op_); | ||
abort(); | ||
} | ||
return FilterDataStatus::Continue; | ||
} | ||
|
||
FilterDataStatus ExampleContext::onRequestBody(size_t body_buffer_length, bool end_of_stream) { | ||
return onBody(BufferType::HttpRequestBody, body_buffer_length, end_of_stream); | ||
} | ||
|
||
FilterDataStatus ExampleContext::onResponseBody(size_t body_buffer_length, bool end_of_stream) { | ||
return onBody(BufferType::HttpResponseBody, body_buffer_length, end_of_stream); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters