diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2023-08-29 01:33:23 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2023-08-29 16:39:07 +0100 |
commit | bf968c99ce109c63fded62ec573c4bc247dc9c0a (patch) | |
tree | ea116e4f38e77ab057b2b3c7287971dd1526b8a0 | |
parent | d7acf9a053c876bfedc829e2d02558881bf26cc1 (diff) | |
download | unit-wasm-bf968c99ce109c63fded62ec573c4bc247dc9c0a.tar.gz unit-wasm-bf968c99ce109c63fded62ec573c4bc247dc9c0a.tar.bz2 |
Rust/rusty: Add uwr_get_http_content_str()
This function is like uwr_get_http_content() except that it returns a
Rust str.
This is more convenient if you want to operate on the body content
within Rust.
It's worth noting that uwr_get_http_content() returns a non null
terminated buffer which makes it tricky to work with in Rust.
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
-rw-r--r-- | API-Rust.md | 9 | ||||
-rw-r--r-- | src/rust/unit-wasm-sys/rusty.rs | 12 |
2 files changed, 21 insertions, 0 deletions
diff --git a/API-Rust.md b/API-Rust.md index 7520384..8e2b6a9 100644 --- a/API-Rust.md +++ b/API-Rust.md @@ -64,6 +64,7 @@ and there isn't a real need to create wrappers specifically for them. * [uwr_get_http_local_port](#uwr_get_http_local_port) * [uwr_get_http_server_name](#uwr_get_http_server_name) * [uwr_get_http_content](#uwr_get_http_content) + * [uwr_get_http_content_str](#uwr_get_http_content_str) * [uwr_get_http_content_len](#uwr_get_http_content_len) * [uwr_get_http_content_sent](#uwr_get_http_content_sent) * [uwr_http_is_tls](#uwr_http_is_tls) @@ -592,6 +593,14 @@ pub fn uwr_get_http_content(ctx: *const luw_ctx_t) -> *const u8; This function returns a pointer to the start of the request body. +### uwr_get_http_content_str + +```Rsut +pub fn uwr_get_http_content_str(ctx: *const luw_ctx_t) -> &'static str; +``` + +Same as above but returns a Rust str. + ### uwr_get_http_content_len ```Rust diff --git a/src/rust/unit-wasm-sys/rusty.rs b/src/rust/unit-wasm-sys/rusty.rs index 3c96d02..3bff4df 100644 --- a/src/rust/unit-wasm-sys/rusty.rs +++ b/src/rust/unit-wasm-sys/rusty.rs @@ -9,6 +9,8 @@ use std::ffi::c_char; use std::ffi::c_void; use std::ffi::CStr; use std::ptr::null_mut; +use std::slice; +use std::str; #[macro_export] macro_rules! C2S { @@ -101,6 +103,16 @@ pub fn uwr_get_http_content_len(ctx: *const luw_ctx_t) -> usize { unsafe { luw_get_http_content_len(ctx) } } +pub fn uwr_get_http_content_str(ctx: *const luw_ctx_t) -> &'static str { + unsafe { + let slice = slice::from_raw_parts( + uwr_get_http_content(ctx), + uwr_get_http_content_len(ctx), + ); + str::from_utf8(slice).unwrap() + } +} + pub fn uwr_get_http_content_sent(ctx: *const luw_ctx_t) -> usize { unsafe { luw_get_http_content_sent(ctx) } } |