Fully abstract route paths, move them to Routes.h.

This commit is contained in:
Jordan Bancino 2022-12-11 00:14:22 +00:00
parent 323dad1f8b
commit d56aaa921d
6 changed files with 36 additions and 22 deletions

View file

@ -50,7 +50,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
HashMap *response; HashMap *response;
char *requestPath; char *requestPath;
Array *pathParts; MATRIX_PATH *pathParts;
char *pathPart; char *pathPart;
RouteArgs routeArgs; RouteArgs routeArgs;
@ -94,14 +94,14 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
goto finish; goto finish;
} }
pathParts = ArrayCreate(); pathParts = MATRIX_PATH_CREATE();
key = requestPath; key = requestPath;
while ((pathPart = strtok_r(key, "/", &key))) while ((pathPart = strtok_r(key, "/", &key)))
{ {
char *decoded = HttpUrlDecode(pathPart); char *decoded = HttpUrlDecode(pathPart);
ArrayAdd(pathParts, decoded); MATRIX_PATH_APPEND(pathParts, decoded);
} }
routeArgs.matrixArgs = args; routeArgs.matrixArgs = args;
@ -148,7 +148,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
Free(pathPart); Free(pathPart);
} }
ArrayFree(pathParts); MATRIX_PATH_FREE(pathParts);
JsonFree(response); JsonFree(response);
finish: finish:

View file

@ -27,6 +27,7 @@
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
#include <Util.h>
ROUTE_IMPL(RouteLogin, args) ROUTE_IMPL(RouteLogin, args)
{ {

View file

@ -28,6 +28,7 @@
#include <Memory.h> #include <Memory.h>
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
#include <Util.h>
ROUTE_IMPL(RouteMatrix, args) ROUTE_IMPL(RouteMatrix, args)
{ {

View file

@ -28,6 +28,7 @@
#include <Memory.h> #include <Memory.h>
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
#include <Util.h>
ROUTE_IMPL(RouteWellKnown, args) ROUTE_IMPL(RouteWellKnown, args)
{ {

View file

@ -31,21 +31,6 @@
#include <TelodendriaConfig.h> #include <TelodendriaConfig.h>
#include <Db.h> #include <Db.h>
/*
* 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 typedef enum MatrixError
{ {
M_FORBIDDEN, M_FORBIDDEN,

View file

@ -24,18 +24,44 @@
#ifndef TELODENDRIA_ROUTES_H #ifndef TELODENDRIA_ROUTES_H
#define TELODENDRIA_ROUTES_H #define TELODENDRIA_ROUTES_H
#include <string.h>
#include <HashMap.h> #include <HashMap.h>
#include <Array.h> #include <Array.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <Util.h>
#include <Matrix.h> #include <Matrix.h>
/*
* 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 typedef struct RouteArgs
{ {
MatrixHttpHandlerArgs *matrixArgs; MatrixHttpHandlerArgs *matrixArgs;
HttpServerContext *context; HttpServerContext *context;
Array *path; MATRIX_PATH *path;
} RouteArgs; } RouteArgs;
#define ROUTE(name) \ #define ROUTE(name) \