Finally getting into Matrix territory

This commit is contained in:
Jordan Bancino 2022-08-28 15:45:24 -04:00
parent 8ec6d6afb3
commit 4cd24a6e97
3 changed files with 122 additions and 9 deletions

View file

@ -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)
{

View file

@ -38,15 +38,10 @@
#include <HashMap.h>
#include <Config.h>
#include <HttpServer.h>
#include <Matrix.h>
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",

View file

@ -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);