[MOD] Use Parser API for user ID parsing

Only tested with POST /login though, but it should work
This commit is contained in:
lda 2023-11-24 21:19:54 +01:00
parent b378d443c0
commit 572d69c3f6

View file

@ -31,6 +31,10 @@
#include <Cytoplasm/Int64.h>
#include <Cytoplasm/UInt64.h>
#include <Parser.h>
#include <ctype.h>
#include <string.h>
struct User
@ -882,51 +886,88 @@ finish:
return arr;
}
static int
UserIdParseLocal(Parser *p, char **ret)
{
int c;
if (!p || !ret)
{
return 0;
}
ParserStartCopy(p);
/* Always assume the general case here: every ASCII char,
* minus ':'. */
while (isascii((c = ParserGetc(p))) && c != ':')
{
/* Do nothing. */
}
if (c != EOF)
{
ParserUndo(p);
}
*ret = ParserEndCopy(p);
return 1;
}
static int
UserIdParseServer(Parser *p, char **ret)
{
int c;
if (!p || !ret)
{
return 0;
}
ParserStartCopy(p);
/* Allow *every* ASCII character. */
while (isascii((c = ParserGetc(p))))
{
/* Do nothing. */
}
if (c != EOF)
{
ParserUndo(p);
}
*ret = ParserEndCopy(p);
return 1;
}
UserId *
UserIdParse(char *id, char *defaultServer)
{
UserId *userId;
Parser *p;
if (!id)
{
return NULL;
}
id = StrDuplicate(id);
if (!id)
{
return NULL;
}
p = ParserCreate(id);
userId = Malloc(sizeof(UserId));
userId->localpart = NULL;
userId->server = NULL;
if (!userId)
{
goto finish;
}
/* Fully-qualified user ID */
if (*id == '@')
/* Fully-qualified user ID.
* TODO: Generalise this for parsing namespaced IDs,
* like room aliases and IDs. */
if (ParserExcept(p, '@'))
{
char *localStart = id + 1;
char *serverStart = localStart;
while (*serverStart != ':' && *serverStart != '\0')
{
serverStart++;
}
if (*serverStart == '\0')
if (!UserIdParseLocal(p, &userId->localpart) ||
!ParserExcept(p, ':') ||
!UserIdParseServer(p, &userId->server))
{
if (userId->localpart)
{
Free(userId->localpart);
}
Free(userId);
userId = NULL;
goto finish;
}
*serverStart = '\0';
serverStart++;
userId->localpart = StrDuplicate(localStart);
userId->server = StrDuplicate(serverStart);
}
else
{
@ -942,7 +983,7 @@ UserIdParse(char *id, char *defaultServer)
}
finish:
Free(id);
ParserEnd(p);
return userId;
}