summaryrefslogtreecommitdiffhomepage
path: root/examples/rust/large-upload/src
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-09-25 13:19:31 +0100
committerAndrew Clayton <a.clayton@nginx.com>2023-09-25 17:39:51 +0100
commite4a868078ab43772e36cd8ffc59fd995353fb402 (patch)
tree1ab9f9f2430822154e39259eaf07e76879a1178a /examples/rust/large-upload/src
parentc3ea7bbe122c87abd7114a770144e114e2ce927e (diff)
downloadunit-wasm-e4a868078ab43772e36cd8ffc59fd995353fb402.tar.gz
unit-wasm-e4a868078ab43772e36cd8ffc59fd995353fb402.tar.bz2
examples: Add C and Rust examples of handling large uploads
The programs demonstrate handling requests with payloads larger than 4GiB which means they need to be written out to disk and so also demonstrates the use of the file-system access mechanism. Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'examples/rust/large-upload/src')
-rw-r--r--examples/rust/large-upload/src/lib.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/examples/rust/large-upload/src/lib.rs b/examples/rust/large-upload/src/lib.rs
new file mode 100644
index 0000000..a59bdb3
--- /dev/null
+++ b/examples/rust/large-upload/src/lib.rs
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+/*
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) Timo Stark
+ * Copyright (C) F5, Inc.
+ */
+
+use unit_wasm::rusty::*;
+
+use std::fs::File;
+use std::ptr::null_mut;
+
+static mut CTX: luw_ctx_t = UWR_CTX_INITIALIZER();
+static mut REQUEST_BUF: *mut u8 = null_mut();
+static mut TOTAL_BYTES_WROTE: u64 = 0;
+
+#[no_mangle]
+pub extern "C" fn uwr_module_end_handler() {
+ unsafe { uwr_free(REQUEST_BUF); }
+}
+
+#[no_mangle]
+pub extern "C" fn uwr_module_init_handler() {
+ unsafe { REQUEST_BUF = uwr_malloc(uwr_mem_get_init_size()); }
+}
+
+#[no_mangle]
+pub extern "C" fn uwr_response_end_handler() {
+ unsafe { TOTAL_BYTES_WROTE = 0; }
+}
+
+#[no_mangle]
+pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
+ let ctx: *mut luw_ctx_t = unsafe { &mut CTX };
+ let mut f;
+ let bytes_wrote: isize;
+ let mut total = unsafe { TOTAL_BYTES_WROTE };
+
+ if total == 0 {
+ uwr_init_ctx(ctx, addr, 0);
+ uwr_set_req_buf(ctx, unsafe { &mut REQUEST_BUF }, LUW_SRB_NONE);
+
+ f = File::create("/var/tmp/large-file.dat").unwrap();
+ } else {
+ f = File::options()
+ .append(true)
+ .open("/var/tmp/large-file.dat")
+ .unwrap();
+ }
+
+ bytes_wrote = uwr_mem_splice_file(addr, &mut f);
+ if bytes_wrote == -1 {
+ return -1;
+ }
+
+ total += bytes_wrote as u64;
+ if total == uwr_get_http_content_len(ctx) {
+ uwr_http_response_end();
+ } else {
+ unsafe { TOTAL_BYTES_WROTE = total };
+ }
+
+ return 0;
+}