fixed memory leaks, added cleanup code, added valgrind scripts to test binaries

This commit is contained in:
Martin Preuss
2023-08-09 17:24:44 +02:00
parent 4701a71986
commit b5916acf79
41 changed files with 991 additions and 170 deletions

View File

@@ -36,14 +36,15 @@
*/
static int _handleUrl(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static int _handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static int _handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static GWEN_MSG *_handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static GWEN_MSG *_handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf);
static void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf);
static int _handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static GWEN_MSG *_handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *dbValues, GWEN_BUFFER *pageBuf);
static GWEN_MSG *_addRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
@@ -68,7 +69,7 @@ int _handleUrl(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
{
const char *protocol;
const char *cmd;
GWEN_MSG *msgOut=NULL;
GWEN_MSG *msgOut;
AQH_HttpService_SetupModuleAndPerms(AQH_HttpUrlHandler_GetHttpService(uh), rq, "aqhome");
AQH_HttpRequest_SetupUrlPathMembers(rq);
@@ -76,26 +77,14 @@ int _handleUrl(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
protocol=AQH_HttpRequest_GetProtocol(rq);
cmd=AQH_HttpRequest_GetCommand(rq);
if (cmd && *cmd) {
int rv;
if (strcasecmp(cmd, "GET")==0) {
rv=_handleGet(uh, rq);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
}
else if (strcasecmp(cmd, "POST")==0) {
rv=_handlePost(uh, rq);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
}
if (strcasecmp(cmd, "GET")==0)
msgOut=_handleGet(uh, rq);
else if (strcasecmp(cmd, "POST")==0)
msgOut=_handlePost(uh, rq);
else {
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 405, "Method not allowed", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
}
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
}
else {
@@ -106,7 +95,7 @@ int _handleUrl(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
int _handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
GWEN_MSG *_handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
{
GWEN_BUFFER *pageBuf;
int rv;
@@ -120,9 +109,7 @@ int _handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error adding headers");
GWEN_Buffer_free(pageBuf);
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 500, "Internal Error", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 500, "Internal Error", protocol, NULL);
}
/* handle middle part (header - middle - footer) */
@@ -140,38 +127,35 @@ int _handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
_handleGetAdd(uh, rq, pageBuf);
else {
DBG_ERROR(NULL, "Invalid url (2nd member is [%s])", s);
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 500, "Internal Error", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
GWEN_Buffer_free(pageBuf);
return 0;
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
}
}
else {
DBG_ERROR(NULL, "No list of url members");
GWEN_Buffer_free(pageBuf);
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 500, "Internal Error", protocol, NULL);
}
rv=AQH_HttpUrlHandler_AddContentFooters(uh, AQH_HTTP_CONTENT_MODE_DESKTOP, pageBuf);
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error adding footers");
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 500, "Internal Error", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
GWEN_Buffer_free(pageBuf);
return rv;
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 500, "Internal Error", protocol, NULL);
}
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 200, "OK", protocol, GWEN_Buffer_GetStart(pageBuf));
GWEN_Buffer_free(pageBuf);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return msgOut;
}
int _handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
GWEN_MSG *_handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
{
GWEN_DB_NODE *db;
const GWEN_STRINGLIST *sl;
const char *protocol;
GWEN_MSG *msgOut=NULL;
int rv;
DBG_ERROR(NULL, "POST:");
db=AQH_HttpRequest_GetDbPostBody(rq);
@@ -185,24 +169,21 @@ int _handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
s=GWEN_StringList_StringAt(sl, 1);
if (s && *s) {
if (strcasecmp(s, "add")==0)
rv=_handlePostAdd(uh, rq);
return _handlePostAdd(uh, rq);
else {
DBG_ERROR(NULL, "Invalid url (2nd member is [%s])", s);
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
}
return rv;
}
else {
DBG_ERROR(NULL, "Invalid url (2nd member missing)");
msgOut=AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
}
}
return 0;
else {
DBG_ERROR(NULL, "No list of url members");
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
}
}
@@ -316,65 +297,89 @@ int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *d
int _handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
GWEN_MSG *_handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
{
const char *protocol;
AQH_SERVICE *sv;
uint32_t perms;
GWEN_MSG *msgOut=NULL;
protocol=AQH_HttpRequest_GetProtocol(rq);
sv=AQH_HttpUrlHandler_GetHttpService(uh);
perms=AQH_HttpRequest_GetModulePerms(rq);
if (perms & AQHOME_HTTP_PERMS_ADD_ROOM) {
GWEN_DB_NODE *db;
const char *roomName;
const char *roomDescr;
AQH_STORAGE *sto;
db=AQH_HttpRequest_GetDbPostBody(rq);
roomName=GWEN_DB_GetCharValue(db, "name", 0, NULL);
roomDescr=GWEN_DB_GetCharValue(db, "description", 0, NULL);
if (!(roomName && *roomName)) {
msgOut=AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Missing room name"), 1, db, _writeAddPage);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
}
sto=AqHomeHttpService_GetStorage(sv);
if (sto) {
AQH_ROOM *r;
r=AQH_Room_new();
AQH_Room_SetName(r, roomName);
if (roomDescr && *roomDescr)
AQH_Room_SetDescription(r, roomDescr);
if (AQH_Storage_GetRoomByName(sto, roomName)!=NULL) {
msgOut=AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Room already exists"), 1, db, _writeAddPage);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
}
AQH_Storage_AddRoom(sto, r);
msgOut=AQH_HttpService_CreateRedirectingResponseMsg(sv, protocol, "/rooms/list");
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return _addRoomCreateResponse(uh, rq);
}
else {
DBG_ERROR(NULL, "No storage");
msgOut=AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
}
}
else {
msgOut=AQH_HttpService_CreateResponseMsg(sv, 403, "Forbidden", protocol, NULL);
AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0;
return AQH_HttpService_CreateResponseMsg(sv, 403, "Forbidden", protocol, NULL);
}
}
GWEN_MSG *_addRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
{
AQH_SERVICE *sv;
const char *protocol;
GWEN_DB_NODE *db;
const char *roomName;
const char *roomDescr;
AQH_STORAGE *sto;
sv=AQH_HttpUrlHandler_GetHttpService(uh);
protocol=AQH_HttpRequest_GetProtocol(rq);
db=AQH_HttpRequest_GetDbPostBody(rq);
roomName=GWEN_DB_GetCharValue(db, "name", 0, NULL);
roomDescr=GWEN_DB_GetCharValue(db, "description", 0, NULL);
if (!(roomName && *roomName)) {
DBG_INFO(NULL, "Missing room name");
return AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Missing room name"), 1, db, _writeAddPage);
}
sto=AqHomeHttpService_GetStorage(sv);
if (sto) {
AQH_ROOM *r;
int rv;
r=AQH_Room_new();
AQH_Room_SetName(r, roomName);
if (roomDescr && *roomDescr)
AQH_Room_SetDescription(r, roomDescr);
rv=AqHomeHttpService_LockStorage(sv);
if (rv<0) {
DBG_ERROR(NULL, "Error locking storage");
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
}
if (AQH_Storage_GetRoomByName(sto, roomName)!=NULL) {
DBG_INFO(NULL, "Room \"%s\" already exists", roomName);
AqHomeHttpService_UnlockStorage(sv);
return AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Room already exists"), 1, db, _writeAddPage);
}
AQH_Storage_AddRoom(sto, r);
AQH_Storage_AddRuntimeFlags(sto, AQH_STORAGE_RTFLAGS_MODIFIED);
rv=AqHomeHttpService_UnlockStorage(sv);
if (rv<0) {
DBG_ERROR(NULL, "Error unlocking storage");
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
}
return AQH_HttpService_CreateRedirectingResponseMsg(sv, protocol, "/rooms/list");
}
else
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
}