Skip to content

Commit

Permalink
Implement header handling
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Mar 19, 2024
1 parent b1e3a7c commit 3f7487b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
13 changes: 13 additions & 0 deletions http/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
void handler_fn(wasi_http_request_t *req, wasi_http_response_t *res) {
res->status_code = 200;
sprintf(res->body, "Hello world!\nPath is %s\nAuthority is %s\n", req->path_query, req->authority);

header_t headers [] = {
{
.name = "Content-type",
.value = "text/plain"
},
{
.name = "Server",
.value = "C/WASM"
}
};
res->headers.headers = (header_t*) &headers;
res->headers.len = 2;
}

int main() {
Expand Down
23 changes: 10 additions & 13 deletions http/wasi_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,26 @@ void exports_wasi_http_incoming_handler_handle(exports_wasi_http_incoming_handle
handler(&wasi_request, &wasi_response);
}

client_tuple2_field_key_field_value_t headers [] = {
{
.f0 = {
.ptr = (uint8_t*) "Content-type",
.len = strlen("Content-type"),
},
.f1 = {
.ptr = (uint8_t*) "text/plain",
.len = strlen("text/plain"),
},
},
};
client_tuple2_field_key_field_value_t *headers = malloc(sizeof(client_tuple2_field_key_field_value_t) * wasi_response.headers.len);
for (int i = 0; i < wasi_response.headers.len; i++) {
headers[i].f0.ptr = (uint8_t *)wasi_response.headers.headers[i].name;
headers[i].f0.len = strlen(wasi_response.headers.headers[i].name) - 1;

headers[i].f1.ptr = (uint8_t*)wasi_response.headers.headers[i].value;
headers[i].f1.len = strlen(wasi_response.headers.headers[i].value) - 1;
}
client_list_tuple2_field_key_field_value_t header_list = {
.ptr = headers,
.len = 1,
.len = wasi_response.headers.len,
};

wasi_http_types_own_headers_t f;
wasi_http_types_header_error_t err;

wasi_http_types_static_fields_from_list(&header_list, &f, &err);

free(headers);

wasi_http_types_own_outgoing_response_t res = wasi_http_types_constructor_outgoing_response(f);
wasi_http_types_method_outgoing_response_set_status_code(wasi_http_types_borrow_outgoing_response(res), wasi_response.status_code);

Expand Down

0 comments on commit 3f7487b

Please sign in to comment.