285 lines
6.4 KiB
C
285 lines
6.4 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2025 Martin Preuss, all rights reserved.
|
|
*
|
|
* The license for this file can be found in the file COPYING which you
|
|
* should have received along with this file.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
|
|
#include "./mservice_p.h"
|
|
|
|
#include "aqhome-cgi/service/module.h"
|
|
|
|
#include <gwenhywfar/debug.h>
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* defs and enums
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
#define AQH_MOD_SERVICE_HEADERFILE "header.html"
|
|
#define AQH_MOD_SERVICE_FOOTERFILE "footer.html"
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* global vars
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
GWEN_INHERIT(AQH_MODULE, AQH_MOD_SERVICE)
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* code
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
AQH_MODULE *AQH_ModService_new(AQH_SERVICE *sv, const char *baseFolder)
|
|
{
|
|
AQH_MODULE *m;
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
m=AQH_Module_new();
|
|
GWEN_NEW_OBJECT(AQH_MOD_SERVICE, xm);
|
|
GWEN_INHERIT_SETDATA(AQH_MODULE, AQH_MOD_SERVICE, m, xm, _freeData);
|
|
|
|
xm->service=sv;
|
|
xm->baseFolder=(baseFolder && *baseFolder)?strdup(baseFolder):NULL;
|
|
|
|
return m;
|
|
}
|
|
|
|
|
|
|
|
void _freeData(GWEN_UNUSED void *bp, void *p)
|
|
{
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=(AQH_MOD_SERVICE*) p;
|
|
free(xm->baseFolder);
|
|
GWEN_FREE_OBJECT(xm);
|
|
}
|
|
|
|
|
|
|
|
AQH_SERVICE *AQH_ModService_GetService(const AQH_MODULE *m)
|
|
{
|
|
if (m) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm) {
|
|
return xm->service;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
const char *AQH_ModService_GetBaseFolder(const AQH_MODULE *m)
|
|
{
|
|
if (m) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm) {
|
|
return xm->baseFolder;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
void AQH_ModService_SetHandleRequestFn(AQH_MODULE *m, AQH_MODSERVICE_HANDLEREQUEST_FN fn)
|
|
{
|
|
if (m) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm) {
|
|
xm->handleRequestFn=fn;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void AQH_ModService_SetLoadSubModuleFn(AQH_MODULE *m, AQH_MODSERVICE_LOADSUBMODULE_FN fn)
|
|
{
|
|
if (m) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm) {
|
|
xm->loadSubModuleFn=fn;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int AQH_ModService_AddHeader(AQH_MODULE *m, GWEN_BUFFER *dbuf)
|
|
{
|
|
if (m && dbuf) {
|
|
AQH_MODULE *mParent;
|
|
|
|
mParent=AQH_Module_Tree2_GetParent(m);
|
|
if (mParent) {
|
|
int rv;
|
|
|
|
rv=AQH_ModService_AddHeader(mParent, dbuf);
|
|
if (rv<0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
}
|
|
return AQH_ModService_ReadStaticFile(m, AQH_MOD_SERVICE_HEADERFILE, dbuf);
|
|
}
|
|
DBG_ERROR(NULL, "Argument missing");
|
|
return GWEN_ERROR_INVALID;
|
|
}
|
|
|
|
|
|
|
|
int AQH_ModService_AddFooter(AQH_MODULE *m, GWEN_BUFFER *dbuf)
|
|
{
|
|
if (m && dbuf) {
|
|
AQH_MODULE *mParent;
|
|
int rv;
|
|
|
|
rv=AQH_ModService_ReadStaticFile(m, AQH_MOD_SERVICE_FOOTERFILE, dbuf);
|
|
if (rv<0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
|
|
mParent=AQH_Module_Tree2_GetParent(m);
|
|
if (mParent) {
|
|
int rv;
|
|
|
|
rv=AQH_ModService_AddFooter(mParent, dbuf);
|
|
if (rv<0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
}
|
|
}
|
|
DBG_ERROR(NULL, "Argument missing");
|
|
return GWEN_ERROR_INVALID;
|
|
}
|
|
|
|
|
|
|
|
int AQH_ModService_RespondWithFile(AQH_MODULE *m, AQCGI_REQUEST *rq, const char *sFilename)
|
|
{
|
|
GWEN_BUFFER *buf;
|
|
int rv;
|
|
|
|
buf=GWEN_Buffer_new(0, 256, 0, 1);
|
|
rv=AQH_ModService_AddHeader(m, buf);
|
|
if (rv<0) {
|
|
AQCGI_SendResponseWithStatus(rq, 500, "Internal error");
|
|
GWEN_Buffer_free(buf);
|
|
return GWEN_ERROR_INTERNAL;
|
|
}
|
|
rv=AQH_ModService_ReadStaticFile(m, sFilename, buf);
|
|
if (rv<0) {
|
|
AQCGI_SendResponseWithStatus(rq, 500, "Internal error");
|
|
GWEN_Buffer_free(buf);
|
|
return GWEN_ERROR_INTERNAL;
|
|
}
|
|
rv=AQH_ModService_AddFooter(m, buf);
|
|
if (rv<0) {
|
|
AQCGI_SendResponseWithStatus(rq, 500, "Internal error");
|
|
GWEN_Buffer_free(buf);
|
|
return GWEN_ERROR_INTERNAL;
|
|
}
|
|
AQCGI_Request_SetBufferResponseBody(rq, buf);
|
|
AQCGI_Request_AddResponseHeaderData(rq, "Content-type: text/html");
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int AQH_ModService_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, const char *sLastPathElem)
|
|
{
|
|
if (m) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm && xm->handleRequestFn)
|
|
return xm->handleRequestFn(m, rq, sLastPathElem);
|
|
}
|
|
return GWEN_ERROR_NOT_IMPLEMENTED;
|
|
}
|
|
|
|
|
|
|
|
AQH_MODULE *AQH_ModService_LoadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, const char *sModuleName)
|
|
{
|
|
if (m) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm && xm->loadSubModuleFn)
|
|
return xm->loadSubModuleFn(m, rq, sModuleName);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
int AQH_ModService_ReadStaticFile(AQH_MODULE *m, const char *filename, GWEN_BUFFER *dbuf)
|
|
{
|
|
if (m && filename && dbuf) {
|
|
AQH_MOD_SERVICE *xm;
|
|
|
|
xm=GWEN_INHERIT_GETDATA(AQH_MODULE, AQH_MOD_SERVICE, m);
|
|
if (xm) {
|
|
GWEN_BUFFER *fbuf;
|
|
int rv;
|
|
|
|
fbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
|
GWEN_Buffer_AppendString(fbuf, xm->baseFolder);
|
|
GWEN_Buffer_AppendString(fbuf, GWEN_DIR_SEPARATOR_S);
|
|
GWEN_Buffer_AppendString(fbuf, filename);
|
|
DBG_ERROR(NULL, "Reading file \"%s\"", GWEN_Buffer_GetStart(fbuf));
|
|
rv=GWEN_SyncIo_Helper_ReadFile(GWEN_Buffer_GetStart(fbuf), dbuf);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Read(%s): %d", GWEN_Buffer_GetStart(fbuf), rv);
|
|
GWEN_Buffer_free(fbuf);
|
|
return rv;
|
|
}
|
|
GWEN_Buffer_free(fbuf);
|
|
return 0;
|
|
}
|
|
}
|
|
DBG_ERROR(NULL, "Any arg is missing (or is not a AQH_MOD_SERVICE object)");
|
|
return GWEN_ERROR_INTERNAL;
|
|
}
|
|
|
|
|
|
|
|
|
|
|