refactor: http_router and session

master
Sean McBride 3 years ago
parent c559fc850d
commit 576f65d846

@ -113,7 +113,6 @@
"vec.h": "c", "vec.h": "c",
"module_database.h": "c", "module_database.h": "c",
"perf_window_t.h": "c", "perf_window_t.h": "c",
"typeinfo": "c",
"module_config.h": "c", "module_config.h": "c",
"tenant.h": "c", "tenant.h": "c",
"route_config.h": "c", "route_config.h": "c",

@ -5,11 +5,11 @@
#include "admissions_info.h" #include "admissions_info.h"
#include "http.h" #include "http.h"
#include "route_config.h"
#include "module_database.h" #include "module_database.h"
#include "route_config.h"
#include "tcp_server.h" #include "tcp_server.h"
#define HTTP_ROUTER_CAPACITY 32 #define HTTP_ROUTER_ROUTES_CAPACITY 32
/* Assumption: entrypoint is always _start. This should be enhanced later */ /* Assumption: entrypoint is always _start. This should be enhanced later */
struct route { struct route {
@ -23,16 +23,15 @@ struct route {
struct admissions_info admissions_info; struct admissions_info admissions_info;
}; };
struct http_router { struct http_router {
struct route routes[HTTP_ROUTER_CAPACITY]; struct route routes[HTTP_ROUTER_ROUTES_CAPACITY];
size_t route_length; size_t routes_length;
}; };
static inline void static inline void
http_router_init(struct http_router *router) http_router_init(struct http_router *router)
{ {
router->route_length = 0; router->routes_length = 0;
} }
static inline int static inline int
@ -43,9 +42,11 @@ http_router_add_route(struct http_router *router, struct route_config *config, s
assert(module != NULL); assert(module != NULL);
assert(config->route != NULL); assert(config->route != NULL);
assert(config->http_resp_content_type != NULL); assert(config->http_resp_content_type != NULL);
assert(router->routes_length <= HTTP_ROUTER_ROUTES_CAPACITY);
if (router->route_length < HTTP_ROUTER_CAPACITY) { if (unlikely(router->routes_length == HTTP_ROUTER_ROUTES_CAPACITY)) { return -1; }
router->routes[router->route_length] = (struct route){
router->routes[router->routes_length] = (struct route){
.route = config->route, .route = config->route,
.module = module, .module = module,
.relative_deadline_us = config->relative_deadline_us, .relative_deadline_us = config->relative_deadline_us,
@ -60,21 +61,18 @@ http_router_add_route(struct http_router *router, struct route_config *config, s
/* Admissions Control */ /* Admissions Control */
uint64_t expected_execution = (uint64_t)config->expected_execution_us * runtime_processor_speed_MHz; uint64_t expected_execution = (uint64_t)config->expected_execution_us * runtime_processor_speed_MHz;
admissions_info_initialize(&router->routes[router->route_length].admissions_info, admissions_info_initialize(&router->routes[router->routes_length].admissions_info,
config->admissions_percentile, expected_execution, config->admissions_percentile, expected_execution,
router->routes[router->route_length].relative_deadline); router->routes[router->routes_length].relative_deadline);
router->route_length++; router->routes_length++;
return 0; return 0;
}
return -1;
} }
static inline struct route * static inline struct route *
http_router_match_route(struct http_router *router, char *route) http_router_match_route(struct http_router *router, char *route)
{ {
for (int i = 0; i < router->route_length; i++) { for (int i = 0; i < router->routes_length; i++) {
if (strncmp(route, router->routes[i].route, strlen(router->routes[i].route)) == 0) { if (strncmp(route, router->routes[i].route, strlen(router->routes[i].route)) == 0) {
return &router->routes[i]; return &router->routes[i];
} }

@ -25,7 +25,7 @@ struct http_session {
/* HTTP State */ /* HTTP State */
struct sockaddr client_address; /* client requesting connection! */ struct sockaddr client_address; /* client requesting connection! */
int socket; int socket;
http_parser http_parser; struct http_parser http_parser;
struct http_request http_request; struct http_request http_request;
struct vec_u8 request; struct vec_u8 request;
struct vec_u8 response; struct vec_u8 response;

@ -18,9 +18,6 @@
#include "wasm_memory.h" #include "wasm_memory.h"
#include "wasm_table.h" #include "wasm_table.h"
#define MODULE_MAX_PATH_LENGTH 256
#define MODULE_MAX_ROUTE_LENGTH 256
#define MODULE_DATABASE_CAPACITY 128 #define MODULE_DATABASE_CAPACITY 128
extern thread_local int worker_thread_idx; extern thread_local int worker_thread_idx;

@ -192,22 +192,7 @@ http_parser_settings_on_header_end(http_parser *parser)
#endif #endif
http_request->header_end = true; http_request->header_end = true;
http_request->body_length = parser->content_length;
/* Search header for content length */
for (int i = 0; i < http_request->header_count; i++) {
if (strncasecmp(http_request->headers[i].key, "content-length", strlen("content-length")) == 0) {
intmax_t temp = strtoimax(http_request->headers[i].value, NULL, 10);
if (temp < 0 || temp > INT_MAX) {
/* TODO: Improve error handling to not crash runtime */
fprintf(stderr, "Unable to parse int for key %.*s\n",
http_request->headers[i].key_length, http_request->headers[i].key);
assert(0);
} else {
http_request->body_length = (int)temp;
}
}
}
return 0; return 0;
} }

Loading…
Cancel
Save