summaryrefslogtreecommitdiffhomepage
path: root/examples/c
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/c
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/c')
-rw-r--r--examples/c/Makefile8
-rw-r--r--examples/c/large-upload.c67
2 files changed, 74 insertions, 1 deletions
diff --git a/examples/c/Makefile b/examples/c/Makefile
index 1b10269..0b0fc31 100644
--- a/examples/c/Makefile
+++ b/examples/c/Makefile
@@ -12,7 +12,9 @@ luw_deps = $(LUW_SRCDIR)/libunit-wasm.a \
examples: examples-luw
-examples-luw: luw-echo-request.wasm luw-upload-reflector.wasm
+examples-luw: luw-echo-request.wasm \
+ luw-upload-reflector.wasm \
+ large-upload.wasm
examples-raw: echo-request-raw.wasm upload-reflector-raw.wasm
@@ -36,5 +38,9 @@ upload-reflector-raw.wasm: upload-reflector-raw.c unit-wasm-raw.o
$(PP_CCLNK) $(SDIR)/$@
$(v)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< unit-wasm-raw.o
+large-upload.wasm: large-upload.c $(luw_deps)
+ $(PP_CCLNK) $(SDIR)/$@
+ $(v)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS)
+
clean:
rm -f *.wasm *.o *.gch
diff --git a/examples/c/large-upload.c b/examples/c/large-upload.c
new file mode 100644
index 0000000..9d89298
--- /dev/null
+++ b/examples/c/large-upload.c
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+/* examples/c/large-upload.c - Example of handling request payload larger
+ * larger than the shared memory
+ *
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) F5, Inc.
+ */
+
+#define _XOPEN_SOURCE 500
+
+#define _FILE_OFFSET_BITS 64
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "unit/unit-wasm.h"
+
+static luw_ctx_t ctx;
+static u8 *request_buf;
+static unsigned long long total_bytes_wrote;
+static int fd;
+
+__luw_export_name("luw_module_end_handler")
+void luw_module_end_handler(void)
+{
+ free(request_buf);
+}
+
+__luw_export_name("luw_module_init_handler")
+void luw_module_init_handler(void)
+{
+ request_buf = malloc(luw_mem_get_init_size());
+}
+
+__luw_export_name("luw_response_end_handler")
+void luw_response_end_handler(void)
+{
+ close(fd);
+ total_bytes_wrote = 0;
+}
+
+__luw_export_name("luw_request_handler")
+int luw_request_handler(u8 *addr)
+{
+ ssize_t bytes_wrote;
+
+ if (total_bytes_wrote == 0) {
+ luw_init_ctx(&ctx, addr, 0);
+ luw_set_req_buf(&ctx, &request_buf, LUW_SRB_NONE);
+
+ fd = open("/var/tmp/large-file.dat", O_CREAT|O_TRUNC|O_WRONLY,
+ 0666);
+ }
+
+ bytes_wrote = luw_mem_splice_file(addr, fd);
+ if (bytes_wrote == -1)
+ return -1;
+
+ total_bytes_wrote += bytes_wrote;
+ if (total_bytes_wrote == luw_get_http_content_len(&ctx))
+ luw_http_response_end();
+
+ return 0;
+}