diff --git a/examples/http_body_chunk/README.md b/examples/http_body_chunk/README.md index db2e47d..1ce9528 100644 --- a/examples/http_body_chunk/README.md +++ b/examples/http_body_chunk/README.md @@ -1,16 +1,28 @@ ## http_body -this example demonstrates how to perform operation on a request body, chunk by chunk. +This example demonstrates how to perform operations on a request body, chunk by chunk. -# TODO! -To modify the request: +Reading the received body chunk by chunk, it looks for the string "pattern" inside the body. If it finds it, a 403 response is returned providing the number of the chunk where the pattern was found. Logs are printed every time a chunk is received providing also the size of the read chunk. + +Build and run the example: +```bash +$ make build.example name=http_body_chunk +$ make run name=http_body_chunk ``` -$ curl -XPUT localhost:18000 --data '[initial body]' -H "buffer-operation: prepend" -[this is prepended body][initial body] -$ curl -XPUT localhost:18000 --data '[initial body]' -H "buffer-operation: append" -[initial body][this is appended body] +Perform a request with a body containing the string "pattern": +```bash +$ head -c 700000 /dev/urandom | base64 > /tmp/file.txt && echo "pattern" >> /tmp/file.txt && curl 'localhost:18000/anything' -d @/tmp/file.txt +pattern found in chunk: 2 +``` -$ curl -XPUT localhost:18000 --data '[initial body]' -H "buffer-operation: replace" -[this is replaced body] +Logs generated: +``` +wasm log: OnHttpRequestBody called. BodySize: 114532, totalRequestBodyReadSize: 0, endOfStream: false +wasm log: read chunk size: 114532 +wasm log: OnHttpRequestBody called. BodySize: 114532, totalRequestBodyReadSize: 114532, endOfStream: false +wasm log: OnHttpRequestBody called. BodySize: 933343, totalRequestBodyReadSize: 114532, endOfStream: true +wasm log: read chunk size: 818811 +wasm log: pattern found in chunk: 2 +wasm log: local 403 response sent ``` diff --git a/proxywasm/proxytest/http_test.go b/proxywasm/proxytest/http_test.go index b0b34b9..74b9289 100644 --- a/proxywasm/proxytest/http_test.go +++ b/proxywasm/proxytest/http_test.go @@ -85,7 +85,8 @@ func TestBodyBuffering(t *testing.T) { name: "buffered", buffered: true, action: types.ActionPause, - logged: "1111122222", + // The first chunk has been buffered, therefore it will be retrieved when calling GetHttpRequestBody at the end of stream. + logged: "1111122222", }, { name: "unbuffered",