From d56aaa921d528247bf3c9f18a04561a0d92c7cba Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sun, 11 Dec 2022 00:14:22 +0000 Subject: [PATCH] Fully abstract route paths, move them to Routes.h. --- src/Matrix.c | 8 ++++---- src/Routes/RouteLogin.c | 1 + src/Routes/RouteMatrix.c | 1 + src/Routes/RouteWellKnown.c | 1 + src/include/Matrix.h | 15 --------------- src/include/Routes.h | 32 +++++++++++++++++++++++++++++--- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/Matrix.c b/src/Matrix.c index 72e1567..f3cf5ab 100644 --- a/src/Matrix.c +++ b/src/Matrix.c @@ -50,7 +50,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) HashMap *response; char *requestPath; - Array *pathParts; + MATRIX_PATH *pathParts; char *pathPart; RouteArgs routeArgs; @@ -94,14 +94,14 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) goto finish; } - pathParts = ArrayCreate(); + pathParts = MATRIX_PATH_CREATE(); key = requestPath; while ((pathPart = strtok_r(key, "/", &key))) { char *decoded = HttpUrlDecode(pathPart); - ArrayAdd(pathParts, decoded); + MATRIX_PATH_APPEND(pathParts, decoded); } routeArgs.matrixArgs = args; @@ -148,7 +148,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) Free(pathPart); } - ArrayFree(pathParts); + MATRIX_PATH_FREE(pathParts); JsonFree(response); finish: diff --git a/src/Routes/RouteLogin.c b/src/Routes/RouteLogin.c index ca5313c..b7843da 100644 --- a/src/Routes/RouteLogin.c +++ b/src/Routes/RouteLogin.c @@ -27,6 +27,7 @@ #include #include +#include ROUTE_IMPL(RouteLogin, args) { diff --git a/src/Routes/RouteMatrix.c b/src/Routes/RouteMatrix.c index c25fe90..8adf921 100644 --- a/src/Routes/RouteMatrix.c +++ b/src/Routes/RouteMatrix.c @@ -28,6 +28,7 @@ #include #include #include +#include ROUTE_IMPL(RouteMatrix, args) { diff --git a/src/Routes/RouteWellKnown.c b/src/Routes/RouteWellKnown.c index 24b8362..879b902 100644 --- a/src/Routes/RouteWellKnown.c +++ b/src/Routes/RouteWellKnown.c @@ -28,6 +28,7 @@ #include #include #include +#include ROUTE_IMPL(RouteWellKnown, args) { diff --git a/src/include/Matrix.h b/src/include/Matrix.h index 0041b80..990c7d7 100644 --- a/src/include/Matrix.h +++ b/src/include/Matrix.h @@ -31,21 +31,6 @@ #include #include -/* - * Abstract away the underlying data structure of the path so that - * routes don't have to care what it is. - * - * This will be helpful, for instance, if we decide to switch to a - * queue (which can be easily done with the current implementation if - * we just add a function that computes how many elements are in a - * queue.) An array isn't the most efficient data structure for this - * purpose; a queue would be much better. This allows us to change that - * down the road without having to rewrite all the routes. - */ -#define MATRIX_PATH_POP(path) ArrayDelete(path, 0) -#define MATRIX_PATH_PARTS(path) ArraySize(path) -#define MATRIX_PATH_EQUALS(pathPart, str) ((pathPart != NULL) && (strcmp(pathPart, str) == 0)) - typedef enum MatrixError { M_FORBIDDEN, diff --git a/src/include/Routes.h b/src/include/Routes.h index c84eee0..8beebf8 100644 --- a/src/include/Routes.h +++ b/src/include/Routes.h @@ -24,18 +24,44 @@ #ifndef TELODENDRIA_ROUTES_H #define TELODENDRIA_ROUTES_H +#include + #include #include #include -#include - #include +/* + * Abstract away the underlying data structure of the path so that + * routes don't have to care what it is. + * + * This will be helpful, for instance, if we decide to switch to a + * queue (which can easily be done with the current implementation if + * we just add a function that computes how many elements are in a + * queue.) An array isn't the most efficient data structure for this + * purpose; a queue would be much better. This allows us to change that + * down the road without having to rewrite all the routes. + * + * One tricky thing about the current Queue implementation is that it + * is a fixed-size queue, so we'd either need to make it large enough + * to accomodate large paths, or rewrite it to be dynamically-sized. + */ +#define MATRIX_PATH Array +#define MATRIX_PATH_CREATE() ArrayCreate() +#define MATRIX_PATH_APPEND(path, part) ArrayAdd(path, part) +#define MATRIX_PATH_FREE(path) ArrayFree(path) + +#define MATRIX_PATH_POP(path) ArrayDelete(path, 0) +#define MATRIX_PATH_PARTS(path) ArraySize(path) + +#define MATRIX_PATH_EQUALS(pathPart, str) \ + ((pathPart != NULL) && (strcmp(pathPart, str) == 0)) + typedef struct RouteArgs { MatrixHttpHandlerArgs *matrixArgs; HttpServerContext *context; - Array *path; + MATRIX_PATH *path; } RouteArgs; #define ROUTE(name) \