[ADD/WIP] Add some HTTP request code

We can now *register* users!
This commit is contained in:
LDA 2024-06-13 09:35:57 +02:00
commit 0fa95c2d14
12 changed files with 320 additions and 11 deletions

View file

@ -5,6 +5,7 @@
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <Matrix.h>
#include <Routes.h>
void
@ -27,12 +28,19 @@ ParseeRequest(HttpServerContext *ctx, void *argp)
arg.ctx = ctx ;
arg.stream = stream;
Log(LOG_NOTICE, "%s %s",
HttpRequestMethodToString(HttpRequestMethodGet(ctx)),
path
);
if (!HttpRouterRoute(data->router, path, &arg, (void **) &response))
{
Log(LOG_NOTICE, "Couldn't route %s", path);
HttpResponseStatus(ctx, HTTP_NOT_FOUND);
JsonFree(response);
response = NULL;
response = MatrixCreateError("M_NOT_FOUND", "Route not found.");
/* TODO: Set a thing */
}
@ -50,3 +58,43 @@ ParseeRequest(HttpServerContext *ctx, void *argp)
return;
}
}
HttpClientContext *
ParseeCreateRequest(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);
}