From b21d018daaec5d380f84c6764d31ab61108af4a7 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Fri, 14 Apr 2023 21:37:00 +0000 Subject: [PATCH] Move router building function into a more sensible location. --- TODO.txt | 1 + src/Main.c | 3 +- src/Routes.c | 74 +++++++++++++++++++++++++++++++++++++++ src/Telodendria.c | 50 -------------------------- src/include/Routes.h | 4 +++ src/include/Telodendria.h | 3 -- 6 files changed, 81 insertions(+), 54 deletions(-) create mode 100644 src/Routes.c diff --git a/TODO.txt b/TODO.txt index ca70f61..6f2fab1 100644 --- a/TODO.txt +++ b/TODO.txt @@ -48,6 +48,7 @@ Milestone: v0.3.0 [x] Support regex matching [x] Replace current routing system [ ] Add route for requestToken endpoints + [x] Move TelodendriaBuildRouter() to Routes [ ] Token permissions [ ] Move configuration to database diff --git a/src/Main.c b/src/Main.c index 4e7aa2c..94f3ea9 100644 --- a/src/Main.c +++ b/src/Main.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -179,7 +180,7 @@ main(int argc, char **argv) } Log(LOG_NOTICE, "Building routing tree..."); - matrixArgs.router = TelodendriaBuildRouter(); + matrixArgs.router = RouterBuild(); if (!matrixArgs.router) { Log(LOG_ERR, "Unable to build routing tree."); diff --git a/src/Routes.c b/src/Routes.c new file mode 100644 index 0000000..0c76add --- /dev/null +++ b/src/Routes.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net> + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include + +HttpRouter * +RouterBuild(void) +{ + HttpRouter *router = HttpRouterCreate(); + + if (!router) + { + return NULL; + } + +#define R(path, func) \ + if (!HttpRouterAdd(router, path, func)) \ + { \ + Log(LOG_ERR, "Unable to add route: %s", path); \ + HttpRouterFree(router); \ + return NULL; \ + } + + R("/.well-known/matrix/(client|server)", RouteWellKnown); + + R("/_matrix/client/versions", RouteVersions); + + R("/_matrix/static", RouteStaticDefault); + R("/_matrix/static/client/login", RouteStaticLogin); + + R("/_matrix/client/v3/login", RouteLogin); + R("/_matrix/client/v3/logout", RouteLogout); + R("/_matrix/client/v3/logout/(all)", RouteLogout); + R("/_matrix/client/v3/register", RouteRegister); + R("/_matrix/client/v3/register/(available)", RouteRegister); + R("/_matrix/client/v3/refresh", RouteRefresh); + + R("/_matrix/client/v3/account/whoami", RouteWhoami); + R("/_matrix/client/v3/account/password", RouteChangePwd); + + R("/_matrix/client/v1/register/m.login.registration_token/validity", RouteTokenValid); + +#if 0 + R("/_matrix/client/v3/account/password/(email|msisdn)/requestToken", RouteRequestToken); + R("/_matrix/client/v3/register/(email|msisdn)/requestToken", RouteRequestToken); +#endif + + R("/_matrix/client/v3/profile/(.*)", RouteUserProfile); + R("/_matrix/client/v3/profile/(.*)/(avatar_url|displayname)", RouteUserProfile); + +#undef R + + return router; +} diff --git a/src/Telodendria.c b/src/Telodendria.c index 33c8ac8..d504af6 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -197,53 +197,3 @@ TelodendriaPrintHeader(void) "Documentation/Support: https://telodendria.io"); Log(LOG_INFO, ""); } - -HttpRouter * -TelodendriaBuildRouter(void) -{ - HttpRouter *router = HttpRouterCreate(); - - if (!router) - { - return NULL; - } - -#define R(path, func) \ - if (!HttpRouterAdd(router, path, func)) \ - { \ - Log(LOG_ERR, "Unable to add route: %s", path); \ - HttpRouterFree(router); \ - return NULL; \ - } - - R("/.well-known/matrix/(client|server)", RouteWellKnown); - - R("/_matrix/client/versions", RouteVersions); - - R("/_matrix/static", RouteStaticDefault); - R("/_matrix/static/client/login", RouteStaticLogin); - - R("/_matrix/client/v3/login", RouteLogin); - R("/_matrix/client/v3/logout", RouteLogout); - R("/_matrix/client/v3/logout/(all)", RouteLogout); - R("/_matrix/client/v3/register", RouteRegister); - R("/_matrix/client/v3/register/(available)", RouteRegister); - R("/_matrix/client/v3/refresh", RouteRefresh); - - R("/_matrix/client/v3/account/whoami", RouteWhoami); - R("/_matrix/client/v3/account/password", RouteChangePwd); - - R("/_matrix/client/v1/register/m.login.registration_token/validity", RouteTokenValid); - -#if 0 - R("/_matrix/client/v3/account/password/(email|msisdn)/requestToken", RouteRequestToken); - R("/_matrix/client/v3/register/(email|msisdn)/requestToken", RouteRequestToken); -#endif - - R("/_matrix/client/v3/profile/(.*)", RouteUserProfile); - R("/_matrix/client/v3/profile/(.*)/(avatar_url|displayname)", RouteUserProfile); - -#undef R - - return router; -} diff --git a/src/include/Routes.h b/src/include/Routes.h index 150b977..8c83c92 100644 --- a/src/include/Routes.h +++ b/src/include/Routes.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #define MATRIX_PATH_EQUALS(pathPart, str) \ @@ -40,6 +41,9 @@ typedef struct RouteArgs HttpServerContext *context; } RouteArgs; +HttpRouter * + RouterBuild(void); + #define ROUTE(name) \ extern void * \ name(Array *, void *) diff --git a/src/include/Telodendria.h b/src/include/Telodendria.h index d01313c..3480270 100644 --- a/src/include/Telodendria.h +++ b/src/include/Telodendria.h @@ -49,7 +49,4 @@ extern void extern void TelodendriaPrintHeader(void); -extern HttpRouter * - TelodendriaBuildRouter(void); - #endif