From 6965b2906d05c0adbb36446982c19c93d3dca657 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 20 Apr 2022 21:27:35 -0400 Subject: [PATCH] feat: Preliminary routes --- runtime/include/http.h | 12 ++++++++++++ runtime/include/json.h | 7 +++++++ runtime/include/module.h | 6 ++++-- runtime/include/module_config.h | 1 + runtime/src/listener_thread.c | 7 +++++++ runtime/src/module.c | 4 ++++ tests/fibonacci/bimodal/spec.json | 6 ++++-- tests/html/Makefile | 8 ++++---- tests/html/spec.json | 1 + 9 files changed, 44 insertions(+), 8 deletions(-) diff --git a/runtime/include/http.h b/runtime/include/http.h index d87220f..9ed8f5e 100644 --- a/runtime/include/http.h +++ b/runtime/include/http.h @@ -30,6 +30,12 @@ "Connection: close\r\n" \ "\r\n" +#define HTTP_RESPONSE_404_NOT_FOUND \ + "HTTP/1.1 404 Not Found\r\n" \ + "Server: SLEdge\r\n" \ + "Connection: close\r\n" \ + "\r\n" + #define HTTP_RESPONSE_413_PAYLOAD_TOO_LARGE \ "HTTP/1.1 413 Payload Too Large\r\n" \ "Server: SLEdge\r\n" \ @@ -70,6 +76,10 @@ http_header_build(int status_code) response = HTTP_RESPONSE_400_BAD_REQUEST; http_total_increment_4XX(); break; + case 404: + response = HTTP_RESPONSE_404_NOT_FOUND; + http_total_increment_4XX(); + break; case 413: response = HTTP_RESPONSE_413_PAYLOAD_TOO_LARGE; http_total_increment_4XX(); @@ -99,6 +109,8 @@ http_header_len(int status_code) switch (status_code) { case 400: return strlen(HTTP_RESPONSE_400_BAD_REQUEST); + case 404: + return strlen(HTTP_RESPONSE_404_NOT_FOUND); case 413: return strlen(HTTP_RESPONSE_413_PAYLOAD_TOO_LARGE); case 429: diff --git a/runtime/include/json.h b/runtime/include/json.h index 064c883..5fb3a77 100644 --- a/runtime/include/json.h +++ b/runtime/include/json.h @@ -21,6 +21,7 @@ enum module_name, module_path, module_port, + module_route, module_expected_execution_us, module_admissions_percentile, module_relative_deadline_us, @@ -33,6 +34,7 @@ enum static const char *module_keys[module_keys_len] = { "name", "path", "port", + "route", "expected-execution-us", "admissions-percentile", "relative-deadline-us", @@ -248,6 +250,11 @@ parse_json(const char *json_buf, ssize_t json_buf_size, struct module_config **m int rc = parse_uint16_t(tokens[i], json_buf, module_keys[module_port], &(*module_config_vec)[module_idx].port); if (rc < 0) goto json_parse_err; + } else if (strcmp(key, module_keys[module_route]) == 0) { + if (!is_nonempty_string(tokens[i], key)) goto json_parse_err; + + (*module_config_vec)[module_idx].route = strndup(json_buf + tokens[i].start, + tokens[i].end - tokens[i].start); } else if (strcmp(key, module_keys[module_expected_execution_us]) == 0) { if (!has_valid_type(tokens[i], key, JSMN_PRIMITIVE)) goto json_parse_err; diff --git a/runtime/include/module.h b/runtime/include/module.h index 8fb91c5..288090f 100644 --- a/runtime/include/module.h +++ b/runtime/include/module.h @@ -21,8 +21,9 @@ #define MODULE_DEFAULT_REQUEST_RESPONSE_SIZE (PAGE_SIZE) -#define MODULE_MAX_NAME_LENGTH 32 -#define MODULE_MAX_PATH_LENGTH 256 +#define MODULE_MAX_NAME_LENGTH 32 +#define MODULE_MAX_PATH_LENGTH 256 +#define MODULE_MAX_ROUTE_LENGTH 256 extern thread_local int worker_thread_idx; @@ -56,6 +57,7 @@ struct module { /* Metadata from JSON Config */ char name[MODULE_MAX_NAME_LENGTH]; char path[MODULE_MAX_PATH_LENGTH]; + char route[MODULE_MAX_ROUTE_LENGTH]; uint32_t stack_size; /* a specification? */ uint32_t relative_deadline_us; uint16_t port; diff --git a/runtime/include/module_config.h b/runtime/include/module_config.h index 0adab05..d59c84d 100644 --- a/runtime/include/module_config.h +++ b/runtime/include/module_config.h @@ -7,6 +7,7 @@ struct module_config { char *name; char *path; + char *route; uint16_t port; uint8_t admissions_percentile; uint32_t expected_execution_us; diff --git a/runtime/src/listener_thread.c b/runtime/src/listener_thread.c index 097ca5e..c09cb65 100644 --- a/runtime/src/listener_thread.c +++ b/runtime/src/listener_thread.c @@ -184,6 +184,13 @@ listener_thread_main(void *dummy) continue; } + if (strncmp(session->http_request.full_url, module->route, strlen(module->route)) + != 0) { + http_session_send_err_oneshot(session, 404); + http_session_close(session); + continue; + } + http_request_print(&session->http_request); /* diff --git a/runtime/src/module.c b/runtime/src/module.c index 201f36d..21f4f28 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -44,6 +44,9 @@ module_init(struct module *module, struct module_config *config) panic("response-size must be between 0 and %u, was %u\n", (uint32_t)RUNTIME_HTTP_RESPONSE_SIZE_MAX, config->http_resp_size); + /* If a route is not specified, default to root */ + if (config->route == NULL) config->route = "/"; + struct module *existing_module = module_database_find_by_name(config->name); if (existing_module != NULL) panic("Module %s is already initialized\n", existing_module->name); @@ -86,6 +89,7 @@ module_init(struct module *module, struct module_config *config) /* Set fields in the module struct */ strncpy(module->name, config->name, MODULE_MAX_NAME_LENGTH); strncpy(module->path, config->path, MODULE_MAX_PATH_LENGTH); + strncpy(module->route, config->route, MODULE_MAX_ROUTE_LENGTH); strncpy(module->response_content_type, config->http_resp_content_type, HTTP_MAX_HEADER_VALUE_LENGTH); module->stack_size = ((uint32_t)(round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size))); diff --git a/tests/fibonacci/bimodal/spec.json b/tests/fibonacci/bimodal/spec.json index 733eaa9..b8135d5 100644 --- a/tests/fibonacci/bimodal/spec.json +++ b/tests/fibonacci/bimodal/spec.json @@ -1,8 +1,9 @@ [ { - "name": "fibonacci_10", + "name": "fibonacci", "path": "fibonacci.wasm.so", "port": 10010, + "route": "/fib", "expected-execution-us": 6000, "admissions-percentile": 70, "relative-deadline-us": 20000, @@ -14,6 +15,7 @@ "name": "fibonacci_40", "path": "fibonacci.wasm.so", "port": 10040, + "route": "/fib", "expected-execution-us": 10000000, "admissions-percentile": 70, "relative-deadline-us": 20000000, @@ -21,4 +23,4 @@ "http-resp-size": 1024, "http-resp-content-type": "text/plain" } -] \ No newline at end of file +] diff --git a/tests/html/Makefile b/tests/html/Makefile index d61e746..388f666 100644 --- a/tests/html/Makefile +++ b/tests/html/Makefile @@ -37,16 +37,16 @@ client: http :1337 browser-args: - xdg-open "http://localhost:1337" + xdg-open "http://localhost:1337/index.html" client-stdin: echo "Example STDIN" | http :1337 client-args: - http ":1337?firstArg&secondArg&thirdArg" + http ":1337/index.html?firstArg&secondArg&thirdArg" browser-args: - xdg-open "http://localhost:1337?firstArg&secondArg&thirdArg" + xdg-open "http://localhost:1337/index.html?firstArg&secondArg&thirdArg" client-stdin-args: - echo "Example STDIN" | http ":1337?firstArg&secondArg&thirdArg" + echo "Example STDIN" | http ":1337/index.html?firstArg&secondArg&thirdArg" diff --git a/tests/html/spec.json b/tests/html/spec.json index d5e8b30..741d8bd 100644 --- a/tests/html/spec.json +++ b/tests/html/spec.json @@ -3,6 +3,7 @@ "name": "html", "path": "html.wasm.so", "port": 1337, + "route": "/index.html", "expected-execution-us": 10000000, "admissions-percentile": 70, "relative-deadline-us": 20000000,