Skip to content

Commit

Permalink
add proxy_set_buffer_bytes (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yskopets authored Aug 28, 2020
1 parent 48e0435 commit 40a50e3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<B>(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,
Expand Down

0 comments on commit 40a50e3

Please sign in to comment.