|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use proxy_wasm::traits::*; |
| 16 | +use proxy_wasm::types::*; |
| 17 | + |
| 18 | +#[no_mangle] |
| 19 | +pub fn _start() { |
| 20 | + proxy_wasm::set_log_level(LogLevel::Trace); |
| 21 | + proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpBody::new()) }); |
| 22 | +} |
| 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 | +} |
| 34 | + |
| 35 | +impl Context for HttpBody {} |
| 36 | + |
| 37 | +impl HttpContext for HttpBody { |
| 38 | + fn on_http_response_headers(&mut self, _: usize) -> Action { |
| 39 | + // If there is a Content-Length header and we change the length of |
| 40 | + // the body later, then clients will break. So remove it. |
| 41 | + // We must do this here, because once we exit this function we |
| 42 | + // can no longer modify the response headers. |
| 43 | + 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 |
| 47 | + } |
| 48 | + |
| 49 | + fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> Action { |
| 50 | + self.total_body_size += body_size; |
| 51 | + if !end_of_stream { |
| 52 | + // Wait -- we'll be called again when the complete body is buffered |
| 53 | + // at the host side. |
| 54 | + return Action::Pause; |
| 55 | + } |
| 56 | + |
| 57 | + // Replace the message body if it contains the text "secret". |
| 58 | + // 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) { |
| 61 | + let body_str = String::from_utf8(body_bytes).unwrap(); |
| 62 | + 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()); |
| 68 | + } |
| 69 | + } |
| 70 | + Action::Continue |
| 71 | + } |
| 72 | +} |
0 commit comments