[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

18
src/include/AS.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef PARSEE_AS_H
#define PARSEE_AS_H
#include <Cytoplasm/HttpClient.h>
#include <Cytoplasm/Json.h>
#include <Parsee.h>
#include <Routes.h>
/* Verifies a request from the homeserver to match the
* hs_token. */
extern HashMap * ASVerifyRequest(ParseeHttpArg *);
/* Authenticates a request with the correct as_token.
* It does not send the request, however. */
extern void ASAuthenticateRequest(ParseeConfig *, HttpClientContext *);
#endif

8
src/include/Matrix.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef PARSEE_MATRIX_H
#define PARSEE_MATRIX_H
#include <Cytoplasm/Json.h>
/* Creates an error message JSON, with the specified code and message. */
extern HashMap * MatrixCreateError(char *err, char *msg);
#endif

View file

@ -3,16 +3,21 @@
#include <Cytoplasm/HttpServer.h>
#include <Cytoplasm/HttpRouter.h>
#include <Cytoplasm/HttpClient.h>
#include <Cytoplasm/Stream.h>
typedef struct ParseeConfig {
char *as_token, *hs_token;
char *as_token, *hs_token;
/* id here is "Parsee XMPP". */
char *sender_localpart;
char *namespace_base;
char *sender_localpart;
char *namespace_base;
char *listen_as;
int port;
char *listen_as;
int port;
/* Homeserver port info */
char *homeserver_host;
int homeserver_port;
} ParseeConfig;
typedef struct ParseeData {
@ -42,4 +47,9 @@ extern void ParseeFreeData(ParseeData *);
/* HTTP server handler for Parsee, takes in a config. */
extern void ParseeRequest(HttpServerContext *, void *);
extern HttpClientContext * ParseeCreateRequest(ParseeConfig *, HttpRequestMethod, char *);
/* Sends headers, and writes the JSON object. */
extern HttpStatus ParseeSetRequestJSON(HttpClientContext *, HashMap *);
#endif

View file

@ -9,12 +9,29 @@ typedef struct ParseeHttpArg {
} ParseeHttpArg;
/* A list of all routes. */
#define ROUTES X_ROUTE("/", RouteRoot)
#define ROUTES X_ROUTE("/", RouteRoot) \
X_ROUTE("/_matrix/app/v1/transactions/(.*)", RouteTxns)
#define X_ROUTE(path, name) extern void * name(Array *, void *);
ROUTES
#undef X_ROUTE
#define RouteHead(name, pathargs, argp) void * \
name(Array * pathargs, void *argp)
#define RouteHead(name, pathargs, argp) void * \
name(Array * pathargs, void *argp)
#define RequestJSON() do \
{ \
Stream *s = args->stream;\
if (!(request = JsonDecode(s))) \
{ \
HttpResponseStatus( \
args->ctx, \
HTTP_BAD_REQUEST \
); \
response = MatrixCreateError(\
"M_NO_JSON", \
"Invalid JSON stream." \
); \
} \
} \
while (0)
#endif