From 4cd24a6e977f2fbd568954e2ba26d79916c22fa5 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sun, 28 Aug 2022 15:45:24 -0400 Subject: [PATCH] Finally getting into Matrix territory --- src/HttpServer.c | 103 +++++++++++++++++++++++++++++++++++++++ src/Telodendria.c | 21 +++++--- src/include/HttpServer.h | 7 ++- 3 files changed, 122 insertions(+), 9 deletions(-) diff --git a/src/HttpServer.c b/src/HttpServer.c index 6356d51..75f84a8 100644 --- a/src/HttpServer.c +++ b/src/HttpServer.c @@ -109,17 +109,120 @@ HttpServerContextCreate(HttpRequestMethod requestMethod, static void HttpServerContextFree(HttpServerContext * c) { + char *key; + void *val; + if (!c) { return; } + while (HashMapIterate(c->requestHeaders, &key, &val)) + { + free(key); + free(val); + } HashMapFree(c->requestHeaders); + + /* It is up to the handler to free its values */ HashMapFree(c->responseHeaders); + free(c->requestPath); fclose(c->stream); } +HashMap * +HttpRequestHeaders(HttpServerContext * c) +{ + if (!c) + { + return NULL; + } + + return c->requestHeaders; +} + +HttpRequestMethod +HttpRequestMethodGet(HttpServerContext * c) +{ + if (!c) + { + return HTTP_METHOD_UNKNOWN; + } + + return c->requestMethod; +} + +char * +HttpRequestPath(HttpServerContext * c) +{ + if (!c) + { + return NULL; + } + + return c->requestPath; +} + +HashMap * +HttpRequestParams(HttpServerContext * c) +{ + /* TODO: Implement param parsing */ + (void) c; + return NULL; +} + +char * +HttpResponseHeader(HttpServerContext * c, char *key, char *val) +{ + if (!c) + { + return NULL; + } + + return HashMapSet(c->responseHeaders, key, val); +} + +void +HttpResponseStatus(HttpServerContext * c, HttpStatus status) +{ + if (!c) + { + return; + } + + c->responseStatus = status; +} + +FILE * +HttpStream(HttpServerContext * c) +{ + if (!c) + { + return NULL; + } + + return c->stream; +} + +void +HttpSendHeaders(HttpServerContext * c) +{ + FILE *fp = c->stream; + + char *key; + char *val; + + fprintf(fp, "HTTP/1.0 %d %s\n", c->responseStatus, HttpStatusToString(c->responseStatus)); + + while (HashMapIterate(c->responseHeaders, &key, (void **) &val)) + { + fprintf(fp, "%s: %s\n", key, val); + } + + fprintf(fp, "\n"); +} + static int QueueConnection(HttpServer * server, int fd) { diff --git a/src/Telodendria.c b/src/Telodendria.c index 6b03a3e..3d9b767 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -38,15 +38,10 @@ #include #include #include +#include HttpServer *httpServer = NULL; -static void -TelodendriaHttpHandler(HttpServerContext *, void *args) -{ - -} - static void TelodendriaSignalHandler(int signalNo) { @@ -117,6 +112,8 @@ main(int argc, char **argv) /* Signal handling */ struct sigaction sigAction; + MatrixHttpHandlerArgs *matrixArgs; + lc = LogConfigCreate(); if (!lc) @@ -296,9 +293,19 @@ main(int argc, char **argv) Log(lc, LOG_DEBUG, "Found user/group information using getpwnam() and getgrnam()."); } + matrixArgs = malloc(sizeof(MatrixHttpHandlerArgs)); + if (!matrixArgs) + { + Log(lc, LOG_ERROR, "Unable to allocate memory for HTTP handler arguments."); + exit = EXIT_FAILURE; + goto finish; + } + + matrixArgs->lc = lc; + /* Bind the socket before possibly dropping permissions */ httpServer = HttpServerCreate(tConfig->listenPort, tConfig->threads, tConfig->maxConnections, - TelodendriaHttpHandler, NULL); + MatrixHttpHandler, matrixArgs); if (!httpServer) { Log(lc, LOG_ERROR, "Unable to create HTTP server on port %d: %s", diff --git a/src/include/HttpServer.h b/src/include/HttpServer.h index a5682c4..ecb5261 100644 --- a/src/include/HttpServer.h +++ b/src/include/HttpServer.h @@ -59,8 +59,11 @@ extern HttpRequestMethod extern char * HttpRequestPath(HttpServerContext *); -extern void - HttpResponseHeader(HttpServerContext *, const char *, const char *); +extern HashMap * + HttpRequestParams(HttpServerContext *); + +extern char * + HttpResponseHeader(HttpServerContext *, char *, char *); extern void HttpResponseStatus(HttpServerContext *, HttpStatus);