Parsee/src/HttParsee.c
LDA 692cb8aa6f [MOD] Verbosity levels, avatar cleanser
I have a weird bug where Parsee seems to hang on a Db request, been
tryign to figure that otu since a while but can't quite put my finger on
where.
You can have this commit, as a treat.
2024-08-24 19:36:37 +02:00

103 lines
2.5 KiB
C

#include <Parsee.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <Matrix.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;
Log(LOG_DEBUG, "%s %s",
HttpRequestMethodToString(HttpRequestMethodGet(ctx)),
path
);
if (!HttpRouterRoute(data->router, path, &arg, (void **) &response))
{
Log(LOG_DEBUG, "Couldn't route %s", path);
HttpResponseStatus(ctx, HTTP_NOT_FOUND);
JsonFree(response);
response = MatrixCreateError("M_NOT_FOUND", "Route not found.");
}
/* 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);
JsonFree(response);
}
Log(LOG_DEBUG, "%s %s (%d)",
HttpRequestMethodToString(HttpRequestMethodGet(ctx)),
path, HttpResponseStatusGet(ctx)
);
}
HttpClientContext *
ParseeCreateRequest(const ParseeConfig *conf, HttpRequestMethod meth, char *path)
{
HttpClientContext *ctx;
if (!conf || !path)
{
return NULL;
}
ctx = HttpRequest(
meth,
HTTP_FLAG_TLS,
conf->homeserver_port, conf->homeserver_host,
path
);
return ctx;
}
HttpStatus
ParseeSetRequestJSON(HttpClientContext *ctx, HashMap *json)
{
Stream *stream;
int size;
char *sizestr;
if (!ctx || !json)
{
return HTTP_STATUS_UNKNOWN;
}
size = JsonEncode(json, NULL, JSON_DEFAULT);
sizestr = StrInt(size);
HttpRequestHeader(ctx, "Content-Length", sizestr);
Free(sizestr);
stream = HttpClientStream(ctx);
HttpRequestSendHeaders(ctx);
JsonEncode(json, stream, JSON_DEFAULT);
return HttpRequestSend(ctx);
}