diff options
Diffstat (limited to '')
-rw-r--r-- | API-C.md | 12 | ||||
-rw-r--r-- | examples/c/luw-echo-request.c | 4 | ||||
-rw-r--r-- | examples/c/luw-upload-reflector.c | 4 | ||||
-rw-r--r-- | examples/rust/echo-request/src/lib.rs | 3 | ||||
-rw-r--r-- | examples/rust/upload-reflector/src/lib.rs | 3 | ||||
-rw-r--r-- | src/c/include/unit/unit-wasm.h | 5 | ||||
-rw-r--r-- | src/c/libunit-wasm.c | 13 | ||||
-rw-r--r-- | src/rust/unit-wasm-sys/rusty.rs | 3 |
8 files changed, 28 insertions, 19 deletions
@@ -212,6 +212,9 @@ typedef struct { /* points to the end of ctx->req_buf */ u8 *reqp; + + /* tracks the response header index number */ + s32 resp_hdr_idx; } luw_ctx_t; ``` @@ -864,14 +867,11 @@ luw_http_init_headers(ctx, 2, 0); ### luw_http_add_header ```C -void luw_http_add_header(luw_ctx_t *ctx, u16 idx, const char *name, - const char *value); +void luw_http_add_header(luw_ctx_t *ctx, const char *name, const char *value); ``` This function is used to add a header to the response. -_idx_ is the index (starting at 0) of the header we are adding. - _name_ is the name of the header. _value_ is the value of the header. @@ -882,8 +882,8 @@ Example char clen[32]; /* ... */ snprintf(clen, sizeof(clen), "%lu", luw_get_response_data_size(&ctx)); -luw_http_add_header(&ctx, 0, "Content-Type", "text/plain"); -luw_http_add_header(&ctx, 1, "Content-Length", clen); +luw_http_add_header(&ctx, "Content-Type", "text/plain"); +luw_http_add_header(&ctx, "Content-Length", clen); ``` ### luw_http_send_headers diff --git a/examples/c/luw-echo-request.c b/examples/c/luw-echo-request.c index 5655c65..66c3880 100644 --- a/examples/c/luw-echo-request.c +++ b/examples/c/luw-echo-request.c @@ -85,8 +85,8 @@ int luw_request_handler(u8 *addr) luw_http_init_headers(&ctx, 2, 0); snprintf(clen, sizeof(clen), "%lu", luw_get_response_data_size(&ctx)); - luw_http_add_header(&ctx, 0, "Content-Type", "text/plain"); - luw_http_add_header(&ctx, 1, "Content-Length", clen); + luw_http_add_header(&ctx, "Content-Type", "text/plain"); + luw_http_add_header(&ctx, "Content-Length", clen); luw_http_send_headers(&ctx); diff --git a/examples/c/luw-upload-reflector.c b/examples/c/luw-upload-reflector.c index 95bc514..5abce9f 100644 --- a/examples/c/luw-upload-reflector.c +++ b/examples/c/luw-upload-reflector.c @@ -57,8 +57,8 @@ static int upload_reflector(luw_ctx_t *ctx) luw_get_http_content_len(ctx)); luw_http_init_headers(ctx, 2, 0); - luw_http_add_header(ctx, 0, "Content-Type", ct ? ct : defct); - luw_http_add_header(ctx, 1, "Content-Length", clen); + luw_http_add_header(ctx, "Content-Type", ct ? ct : defct); + luw_http_add_header(ctx, "Content-Length", clen); luw_http_send_headers(ctx); } diff --git a/examples/rust/echo-request/src/lib.rs b/examples/rust/echo-request/src/lib.rs index 3fea8d5..235ad13 100644 --- a/examples/rust/echo-request/src/lib.rs +++ b/examples/rust/echo-request/src/lib.rs @@ -104,10 +104,9 @@ pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 { // storing the response headers at the beginning of our shared // memory at offset 0. uwr_http_init_headers(ctx, 2, 0); - uwr_http_add_header(ctx, 0, "Content-Type", "text/plain"); + uwr_http_add_header(ctx, "Content-Type", "text/plain"); uwr_http_add_header( ctx, - 1, "Content-Length", &format!("{}", uwr_get_response_data_size(ctx)), ); diff --git a/examples/rust/upload-reflector/src/lib.rs b/examples/rust/upload-reflector/src/lib.rs index 01138b0..43bd1c6 100644 --- a/examples/rust/upload-reflector/src/lib.rs +++ b/examples/rust/upload-reflector/src/lib.rs @@ -49,10 +49,9 @@ pub fn upload_reflector(ctx: *mut luw_ctx_t) -> i32 { } uwr_http_init_headers(ctx, 2, 0); - uwr_http_add_header(ctx, 0, "Content-Type", ct); + uwr_http_add_header(ctx, "Content-Type", ct); uwr_http_add_header( ctx, - 1, "Content-Length", &format!("{}", uwr_get_http_content_len(ctx)), ); diff --git a/src/c/include/unit/unit-wasm.h b/src/c/include/unit/unit-wasm.h index dedf15a..cfff906 100644 --- a/src/c/include/unit/unit-wasm.h +++ b/src/c/include/unit/unit-wasm.h @@ -118,6 +118,9 @@ typedef struct { /* points to the end of ctx->req_buf */ u8 *reqp; + + /* tracks the response header index number */ + s32 resp_hdr_idx; } luw_ctx_t; typedef enum { @@ -189,7 +192,7 @@ extern size_t luw_mem_fill_buf_from_req(luw_ctx_t *ctx, size_t from); extern void luw_mem_reset(luw_ctx_t *ctx); extern void luw_http_send_response(const luw_ctx_t *ctx); extern void luw_http_init_headers(luw_ctx_t *ctx, size_t nr, size_t offset); -extern void luw_http_add_header(luw_ctx_t *ctx, u16 idx, const char *name, +extern void luw_http_add_header(luw_ctx_t *ctx, const char *name, const char *value); extern void luw_http_send_headers(const luw_ctx_t *ctx); extern void luw_http_response_end(void); diff --git a/src/c/libunit-wasm.c b/src/c/libunit-wasm.c index 8cea78a..11272ed 100644 --- a/src/c/libunit-wasm.c +++ b/src/c/libunit-wasm.c @@ -83,6 +83,7 @@ void luw_init_ctx(luw_ctx_t *ctx, u8 *addr, size_t offset) ctx->resp_offset = offset; ctx->resp->size = 0; ctx->resp_hdr->nr_fields = 0; + ctx->resp_hdr_idx = -1; } /* @@ -326,6 +327,7 @@ void luw_mem_reset(luw_ctx_t *ctx) ctx->mem = ctx->resp->data; ctx->resp->size = 0; ctx->resp_hdr->nr_fields = 0; + ctx->resp_hdr_idx = -1; } void luw_http_send_response(const luw_ctx_t *ctx) @@ -342,9 +344,14 @@ void luw_http_init_headers(luw_ctx_t *ctx, size_t nr, size_t offset) ctx->resp_hdr->nr_fields = nr; } -void luw_http_add_header(luw_ctx_t *ctx, u16 idx, const char *name, - const char *value) +void luw_http_add_header(luw_ctx_t *ctx, const char *name, const char *value) { + s32 idx = ctx->resp_hdr_idx; + + idx++; + if ((u32)idx == ctx->resp_hdr->nr_fields) + return; + ctx->resp_hdr->fields[idx].name_off = ctx->hdrp - ctx->addr; ctx->resp_hdr->fields[idx].name_len = strlen(name); ctx->hdrp = mempcpy(ctx->hdrp, name, strlen(name)); @@ -352,6 +359,8 @@ void luw_http_add_header(luw_ctx_t *ctx, u16 idx, const char *name, ctx->resp_hdr->fields[idx].value_off = ctx->hdrp - ctx->addr; ctx->resp_hdr->fields[idx].value_len = strlen(value); ctx->hdrp = mempcpy(ctx->hdrp, value, strlen(value)); + + ctx->resp_hdr_idx = idx; } void luw_http_send_headers(const luw_ctx_t *ctx) diff --git a/src/rust/unit-wasm-sys/rusty.rs b/src/rust/unit-wasm-sys/rusty.rs index 79c38f3..fbfecd3 100644 --- a/src/rust/unit-wasm-sys/rusty.rs +++ b/src/rust/unit-wasm-sys/rusty.rs @@ -43,6 +43,7 @@ pub const fn UWR_CTX_INITIALIZER() -> luw_ctx_t { req_buf: null_mut(), hdrp: null_mut(), reqp: null_mut(), + resp_hdr_idx: -1, } } @@ -173,14 +174,12 @@ pub fn uwr_http_init_headers(ctx: *mut luw_ctx_t, nr: usize, offset: usize) { pub fn uwr_http_add_header( ctx: *mut luw_ctx_t, - idx: u16, name: &str, value: &str, ) { unsafe { luw_http_add_header( ctx, - idx, S2C!(name).as_ptr() as *const i8, S2C!(value).as_ptr() as *const i8, ); |