From 6eab1e7015e7149d4f0c5084872e3a110bb0052d Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Tue, 5 Apr 2022 16:24:07 -0400 Subject: [PATCH] feat: query params to wasi args --- applications/wasm_apps | 2 +- runtime/include/http.h | 3 ++ runtime/include/http_request.h | 17 ++++++++---- runtime/include/sandbox_receive_request.h | 8 ++++++ runtime/src/current_sandbox.c | 7 +++-- runtime/src/http_parser_settings.c | 34 +++++++++++++++++++++++ 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/applications/wasm_apps b/applications/wasm_apps index 1ae4a32..88e4c44 160000 --- a/applications/wasm_apps +++ b/applications/wasm_apps @@ -1 +1 @@ -Subproject commit 1ae4a32dea4abdb48db079c725862c649fc9b600 +Subproject commit 88e4c441dd3c51bc4035a94a0942495ece7f42a8 diff --git a/runtime/include/http.h b/runtime/include/http.h index 0fe2dcc..6ea6779 100644 --- a/runtime/include/http.h +++ b/runtime/include/http.h @@ -9,6 +9,9 @@ #define HTTP_MAX_HEADER_LENGTH 32 #define HTTP_MAX_HEADER_VALUE_LENGTH 256 +#define HTTP_MAX_QUERY_PARAM_COUNT 16 +#define HTTP_MAX_QUERY_PARAM_LENGTH 32 + #define HTTP_RESPONSE_200_TEMPLATE \ "HTTP/1.1 200 OK\r\n" \ "Server: SLEdge\r\n" \ diff --git a/runtime/include/http_request.h b/runtime/include/http_request.h index 4e9d936..0eef208 100644 --- a/runtime/include/http_request.h +++ b/runtime/include/http_request.h @@ -12,12 +12,19 @@ struct http_header { int value_length; }; +struct http_query_param { + char value[HTTP_MAX_QUERY_PARAM_LENGTH]; + int value_length; +}; + struct http_request { - struct http_header headers[HTTP_MAX_HEADER_COUNT]; - int header_count; - char *body; - int body_length; - int body_read_length; /* How far we've read */ + struct http_header headers[HTTP_MAX_HEADER_COUNT]; + int header_count; + struct http_query_param query_params[HTTP_MAX_QUERY_PARAM_COUNT]; + int query_params_count; + char *body; + int body_length; + int body_read_length; /* How far we've read */ /* additional members for http-parser */ bool last_was_value; /* http-parser flag used to help the http-parser callbacks differentiate between header diff --git a/runtime/include/sandbox_receive_request.h b/runtime/include/sandbox_receive_request.h index b77acbd..8b2cfb9 100644 --- a/runtime/include/sandbox_receive_request.h +++ b/runtime/include/sandbox_receive_request.h @@ -96,6 +96,14 @@ sandbox_receive_request(struct sandbox *sandbox) request->length += bytes_parsed; } +#ifdef LOG_HTTP_PARSER + for (int i = 0; i < sandbox->http_request.query_params_count; i++) { + debuglog("Argument %d, Len: %d, %.*s\n", i, sandbox->http_request.query_params[i].value_length, + sandbox->http_request.query_params[i].value_length, + sandbox->http_request.query_params[i].value); + } +#endif + rc = 0; done: return rc; diff --git a/runtime/src/current_sandbox.c b/runtime/src/current_sandbox.c index 82d7fa9..8a1e1bb 100644 --- a/runtime/src/current_sandbox.c +++ b/runtime/src/current_sandbox.c @@ -166,10 +166,11 @@ current_sandbox_init() wasi_options_init(&options); args[0] = sandbox->module->name; - for (int i = 0; i < dummy_argc; i++) args[i + 1] = (char *)dummy_argv[i]; + for (int i = 0; i < sandbox->http_request.query_params_count; i++) + args[i + 1] = (char *)sandbox->http_request.query_params[i].value; - options.argc = dummy_argc + 1; - options.argv = &args; + options.argc = sandbox->http_request.query_params_count + 1; + options.argv = (const char **)&args; sandbox->wasi_context = wasi_context_init(&options); sledge_abi__current_wasm_module_instance.wasi_context = sandbox->wasi_context; assert(sandbox->wasi_context != NULL); diff --git a/runtime/src/http_parser_settings.c b/runtime/src/http_parser_settings.c index e55c102..a55efb1 100644 --- a/runtime/src/http_parser_settings.c +++ b/runtime/src/http_parser_settings.c @@ -30,6 +30,40 @@ http_parser_settings_on_url(http_parser *parser, const char *at, size_t length) debuglog("sandbox: %lu, length: %zu, Content \"%.*s\"\n", sandbox->id, length, (int)length, at); #endif + char *query_params = memchr(at, '?', length); + + if (query_params != NULL) { + char *prev = query_params + 1; + char *cur = NULL; + while ((cur = strchr(prev, '&')) != NULL + && sandbox->http_request.query_params_count < HTTP_MAX_QUERY_PARAM_COUNT) { + cur++; + size_t len = cur - prev - 1; + sandbox->http_request.query_params[sandbox->http_request.query_params_count].value_length = + len < HTTP_MAX_QUERY_PARAM_LENGTH - 1 ? len : HTTP_MAX_QUERY_PARAM_LENGTH - 1; + + strncpy(sandbox->http_request.query_params[sandbox->http_request.query_params_count].value, + prev, + sandbox->http_request.query_params[sandbox->http_request.query_params_count] + .value_length); + + sandbox->http_request.query_params_count++; + prev = cur; + } + if (prev != NULL && sandbox->http_request.query_params_count < HTTP_MAX_QUERY_PARAM_COUNT) { + size_t len = &at[length] - prev; + sandbox->http_request.query_params[sandbox->http_request.query_params_count].value_length = + len < HTTP_MAX_QUERY_PARAM_LENGTH - 1 ? len : HTTP_MAX_QUERY_PARAM_LENGTH - 1; + + strncpy(sandbox->http_request.query_params[sandbox->http_request.query_params_count].value, + prev, + sandbox->http_request.query_params[sandbox->http_request.query_params_count] + .value_length); + + sandbox->http_request.query_params_count++; + } + } + return 0; }