From 40a50e3997db0203b79d327679a79e6d1c8a6642 Mon Sep 17 00:00:00 2001 From: Yaroslav Skopets Date: Fri, 28 Aug 2020 20:00:29 +0200 Subject: [PATCH] add `proxy_set_buffer_bytes` (#17) --- src/hostcalls.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/hostcalls.rs b/src/hostcalls.rs index 7394cb02..b4c05c9d 100644 --- a/src/hostcalls.rs +++ b/src/hostcalls.rs @@ -34,6 +34,7 @@ mod abi { pub const PROXY_SET_TICK_PERIOD_MILLISECONDS: &str = "proxy_set_tick_period_milliseconds"; pub const PROXY_GET_CONFIGURATION: &str = "proxy_get_configuration"; pub const PROXY_GET_BUFFER_BYTES: &str = "proxy_get_buffer_bytes"; + pub const PROXY_SET_BUFFER_BYTES: &str = "proxy_set_buffer_bytes"; pub const PROXY_GET_HEADER_MAP_PAIRS: &str = "proxy_get_header_map_pairs"; pub const PROXY_SET_HEADER_MAP_PAIRS: &str = "proxy_set_header_map_pairs"; pub const PROXY_GET_HEADER_MAP_VALUE: &str = "proxy_get_header_map_value"; @@ -172,6 +173,47 @@ pub fn get_buffer( } } +extern "C" { + fn proxy_set_buffer_bytes( + buffer_type: BufferType, + start: usize, + size: usize, + buffer_data: *const u8, + buffer_size: usize, + ) -> Status; +} + +/// Mutates content in a given buffer. +/// +/// # Examples +/// +/// ```no_run +/// # use proxy_wasm_experimental as proxy_wasm; +/// use proxy_wasm::hostcalls; +/// use proxy_wasm::types::BufferType; +/// +/// # fn action() -> proxy_wasm::error::Result<()> { +/// hostcalls::set_buffer(BufferType::HttpRequestBody, 0, usize::MAX, "replacement text")?; +/// # Ok(()) +/// # } +pub fn set_buffer(buffer_type: BufferType, start: usize, size: usize, value: B) -> Result<()> +where + B: AsRef<[u8]>, +{ + unsafe { + match proxy_set_buffer_bytes( + buffer_type, + start, + size, + value.as_ref().as_ptr(), + value.as_ref().len(), + ) { + Status::Ok => Ok(()), + status => Err(HostCallError::new(abi::PROXY_SET_BUFFER_BYTES, status).into()), + } + } +} + extern "C" { fn proxy_get_header_map_pairs( map_type: MapType,