-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
7 changed files
with
255 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "rust-large-upload" | ||
version = "0.2.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
unit-wasm = { path = "../../../src/rust", version = "0.2.0" } | ||
|
||
[lib] | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters