@@ -18,19 +18,10 @@ use proxy_wasm::types::*;
18
18
#[ no_mangle]
19
19
pub fn _start ( ) {
20
20
proxy_wasm:: set_log_level ( LogLevel :: Trace ) ;
21
- proxy_wasm:: set_http_context ( |_, _| -> Box < dyn HttpContext > { Box :: new ( HttpBody :: new ( ) ) } ) ;
21
+ proxy_wasm:: set_http_context ( |_, _| -> Box < dyn HttpContext > { Box :: new ( HttpBody ) } ) ;
22
22
}
23
23
24
- #[ derive( Default ) ]
25
- struct HttpBody {
26
- total_body_size : usize ,
27
- }
28
-
29
- impl HttpBody {
30
- fn new ( ) -> HttpBody {
31
- Default :: default ( )
32
- }
33
- }
24
+ struct HttpBody ;
34
25
35
26
impl Context for HttpBody { }
36
27
@@ -41,13 +32,10 @@ impl HttpContext for HttpBody {
41
32
// We must do this here, because once we exit this function we
42
33
// can no longer modify the response headers.
43
34
self . set_http_response_header ( "content-length" , None ) ;
44
- // Don't continue to the next callout in the chain because we might
45
- // modify the body.
46
- Action :: Pause
35
+ Action :: Continue
47
36
}
48
37
49
38
fn on_http_response_body ( & mut self , body_size : usize , end_of_stream : bool ) -> Action {
50
- self . total_body_size += body_size;
51
39
if !end_of_stream {
52
40
// Wait -- we'll be called again when the complete body is buffered
53
41
// at the host side.
@@ -56,15 +44,11 @@ impl HttpContext for HttpBody {
56
44
57
45
// Replace the message body if it contains the text "secret".
58
46
// Since we returned "Pause" previuously, this will return the whole body.
59
- // However, we have to calculate the size ourselves.
60
- if let Some ( body_bytes) = self . get_http_response_body ( 0 , self . total_body_size ) {
47
+ if let Some ( body_bytes) = self . get_http_response_body ( 0 , body_size) {
61
48
let body_str = String :: from_utf8 ( body_bytes) . unwrap ( ) ;
62
49
if body_str. find ( "secret" ) . is_some ( ) {
63
- let new_body = format ! (
64
- "Original message body ({} bytes) redacted." ,
65
- self . total_body_size
66
- ) ;
67
- self . set_http_response_body ( 0 , self . total_body_size , & new_body. into_bytes ( ) ) ;
50
+ let new_body = format ! ( "Original message body ({} bytes) redacted." , body_size) ;
51
+ self . set_http_response_body ( 0 , body_size, & new_body. into_bytes ( ) ) ;
68
52
}
69
53
}
70
54
Action :: Continue
0 commit comments