diff --git a/TODO.txt b/TODO.txt index 9e997bd..44a0020 100644 --- a/TODO.txt +++ b/TODO.txt @@ -22,7 +22,7 @@ Milestone: v0.2.0 [x] Logout [x] Delete refresh token if present [ ] Logout all - [ ] Login fallback (static HTML page) + [~] Login fallback (static HTML page) [x] User Interactive [x] Passwords [x] Caller builds flows diff --git a/src/Routes/RouteMainPage.c b/src/Html.c similarity index 68% rename from src/Routes/RouteMainPage.c rename to src/Html.c index df14443..fcca083 100644 --- a/src/Routes/RouteMainPage.c +++ b/src/Html.c @@ -21,17 +21,21 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include +#include + +#include #include -ROUTE_IMPL(RouteMainPage, args) +void +HtmlBegin(FILE * stream, char *title) { - FILE *stream = HttpStream(args->context); size_t i; - HttpResponseHeader(args->context, "Content-Type", "text/html"); - HttpSendHeaders(args->context); + if (!stream) + { + return; + } fprintf(stream, "" @@ -39,27 +43,35 @@ ROUTE_IMPL(RouteMainPage, args) "" "" "" - "It Works! | Telodendria" + "%s | Telodendria" "" "" "" - "
"
-            );
+            "
"
+            ,title);
 
     for (i = 0; i < TELODENDRIA_LOGO_HEIGHT; i++)
     {
@@ -68,24 +80,14 @@ ROUTE_IMPL(RouteMainPage, args)
 
     fprintf(stream,
             "
" - "

It works! Telodendria is running

" - "

" - "Your Telodendria server is listening on this port and is ready " - "for messages." - "

" - "

" - "To use this server, you'll need " - "a Matrix client." - "

" - "

" - "Welcome to the Matrix universe :)" - "

" - ); + "

%s

" + ,title); +} +void +HtmlEnd(FILE * stream) +{ fprintf(stream, "" - "" - ); - - return NULL; + ""); } diff --git a/src/Matrix.c b/src/Matrix.c index d4378ff..13d4b12 100644 --- a/src/Matrix.c +++ b/src/Matrix.c @@ -100,11 +100,7 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) pathPart = MATRIX_PATH_POP(pathParts); - if (!pathPart) - { - response = RouteMainPage(&routeArgs); - } - else if (MATRIX_PATH_EQUALS(pathPart, ".well-known")) + if (MATRIX_PATH_EQUALS(pathPart, ".well-known")) { response = RouteWellKnown(&routeArgs); } diff --git a/src/Routes/RouteMatrix.c b/src/Routes/RouteMatrix.c index 1d30c42..45ce8a6 100644 --- a/src/Routes/RouteMatrix.c +++ b/src/Routes/RouteMatrix.c @@ -35,6 +35,12 @@ ROUTE_IMPL(RouteMatrix, args) HashMap *response = NULL; char *pathPart = MATRIX_PATH_POP(args->path); + if (MATRIX_PATH_EQUALS(pathPart, "static")) + { + Free(pathPart); + return RouteStatic(args); + } + if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(args->path) < 1) { Free(pathPart); diff --git a/src/Routes/RouteStatic.c b/src/Routes/RouteStatic.c new file mode 100644 index 0000000..bc7d031 --- /dev/null +++ b/src/Routes/RouteStatic.c @@ -0,0 +1,37 @@ +/* + * 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 +#include + +ROUTE_IMPL(RouteStatic, args) +{ + FILE *stream = HttpStream(args->context); + + HttpResponseHeader(args->context, "Content-Type", "text/html"); + HttpSendHeaders(args->context); + + StaticItWorks(stream); + + return NULL; +} diff --git a/src/Static/StaticItWorks.c b/src/Static/StaticItWorks.c new file mode 100644 index 0000000..6f87e08 --- /dev/null +++ b/src/Static/StaticItWorks.c @@ -0,0 +1,55 @@ +/* + * 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 +#include + +void +StaticItWorks(FILE * stream) +{ + HtmlBegin(stream, "It works! Telodendria is running."); + + fprintf(stream, + "" + ); + + fprintf(stream, + "

" + "Your Telodendria server is listening on this port and is ready " + "for messages." + "

" + "

" + "To use this server, you'll need " + "a Matrix client." + "

" + "

" + "Welcome to the Matrix universe :)" + "

" + ); + + HtmlEnd(stream); +} diff --git a/src/include/Html.h b/src/include/Html.h new file mode 100644 index 0000000..2bacf2d --- /dev/null +++ b/src/include/Html.h @@ -0,0 +1,35 @@ +/* + * 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. + */ +#ifndef TELODENDRIA_HTML_H +#define TELODENDRIA_HTML_H + +#include + +extern void + HtmlBegin(FILE *, char *); + +extern void + HtmlEnd(FILE *); + +#endif /* TELODENDRIA_HTML_H */ diff --git a/src/include/Routes.h b/src/include/Routes.h index 0d4fc8e..dca2e7c 100644 --- a/src/include/Routes.h +++ b/src/include/Routes.h @@ -57,9 +57,9 @@ typedef struct RouteArgs HashMap * \ name(RouteArgs * argsName) -ROUTE(RouteMainPage); /* / */ ROUTE(RouteWellKnown); /* /.well-known */ ROUTE(RouteMatrix); /* /_matrix */ +ROUTE(RouteStatic); /* /_matrix/static */ ROUTE(RouteLogin); /* /_matrix/client/(r0|v3)/login */ ROUTE(RouteLogout); /* /_matrix/client/(r0|v3)/logout */ diff --git a/src/include/Static.h b/src/include/Static.h new file mode 100644 index 0000000..1ad13a5 --- /dev/null +++ b/src/include/Static.h @@ -0,0 +1,35 @@ +/* + * 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. + */ +#ifndef TELODENDRIA_STATIC_H +#define TELODENDRIA_STATIC_H + +#include + +extern void + StaticItWorks(FILE *); + +extern void + StaticLogin(FILE *); + +#endif /* TELODENDRIA_STATIC_H */