[ADD/WIP] Routerwerk!

This commit is contained in:
LDA 2024-06-12 23:27:36 +02:00
commit 47c98cbbe3
8 changed files with 280 additions and 1 deletions

52
src/HttParsee.c Normal file
View file

@ -0,0 +1,52 @@
#include <Parsee.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <Routes.h>
void
ParseeRequest(HttpServerContext *ctx, void *argp)
{
ParseeData *data = argp;
char *path = HttpRequestPath(ctx);
HashMap *response = NULL;
Stream *stream = HttpServerStream(ctx);
int encodedSize;
char *encodedStr;
ParseeHttpArg arg;
/* Basic headers */
HttpResponseStatus(ctx, HTTP_OK);
HttpResponseHeader(ctx, "Server", NAME "/v" VERSION);
HttpResponseHeader(ctx, "Connection", "close");
arg.data = data;
arg.ctx = ctx ;
arg.stream = stream;
if (!HttpRouterRoute(data->router, path, &arg, (void **) &response))
{
HttpResponseStatus(ctx, HTTP_NOT_FOUND);
JsonFree(response);
response = NULL;
/* TODO: Set a thing */
}
/* Whatever, we routed a thing. */
if (response)
{
encodedSize = JsonEncode(response, NULL, JSON_DEFAULT);
encodedStr = StrInt(encodedSize);
HttpResponseHeader(ctx, "Content-Length", encodedStr);
HttpResponseHeader(ctx, "Content-Type", "application/json");
Free(encodedStr);
HttpSendHeaders(ctx);
JsonEncode(response, stream, JSON_DEFAULT);
return;
}
}