feat: query params to wasi args

master
Sean McBride 3 years ago
parent d05a58af75
commit 6eab1e7015

@ -1 +1 @@
Subproject commit 1ae4a32dea4abdb48db079c725862c649fc9b600
Subproject commit 88e4c441dd3c51bc4035a94a0942495ece7f42a8

@ -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" \

@ -12,9 +12,16 @@ 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;
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 */

@ -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;

@ -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);

@ -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;
}

Loading…
Cancel
Save