Files
aqhomecontrol/aqhome/http/content_files.c
Martin Preuss b9a54b8ffb added #define DISABLE_DEBUGLOG to source files in http and service folders
will alter be enabled to disable verbose debug logging.
2023-08-09 17:45:59 +02:00

144 lines
3.9 KiB
C

/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 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
//#define DISABLE_DEBUGLOG
#include "aqhome/http/content_files_p.h"
#include <gwenhywfar/debug.h>
#include <gwenhywfar/syncio.h>
GWEN_INHERIT(AQH_HTTP_CONTENT, AQH_HTTP_CONTENT_FILES)
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _freeData(void *bp, void *p);
static int _addOpeningContent(AQH_HTTP_CONTENT *cp, int mode, GWEN_BUFFER *buffer);
static int _addClosingContent(AQH_HTTP_CONTENT *cp, int mode, GWEN_BUFFER *buffer);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
AQH_HTTP_CONTENT *AQH_HttpContentFiles_new(const char *name, const char *headerFilename, const char *footerFilename)
{
AQH_HTTP_CONTENT *cp;
AQH_HTTP_CONTENT_FILES *xcp;
cp=AQH_HttpContent_new(name);
GWEN_NEW_OBJECT(AQH_HTTP_CONTENT_FILES, xcp);
GWEN_INHERIT_SETDATA(AQH_HTTP_CONTENT, AQH_HTTP_CONTENT_FILES, cp, xcp, _freeData);
xcp->headerFilename=(headerFilename && *headerFilename)?strdup(headerFilename):NULL;
xcp->footerFilename=(footerFilename && *footerFilename)?strdup(footerFilename):NULL;
AQH_HttpContent_SetAddOpeningContentFn(cp, _addOpeningContent);
AQH_HttpContent_SetAddClosingContentFn(cp, _addClosingContent);
return cp;
}
void _freeData(void *bp, void *p)
{
AQH_HTTP_CONTENT_FILES *xcp;
xcp=(AQH_HTTP_CONTENT_FILES*) p;
free(xcp->footerData);
free(xcp->headerData);
free(xcp->footerFilename);
free(xcp->headerFilename);
GWEN_FREE_OBJECT(xcp);
}
int _addOpeningContent(AQH_HTTP_CONTENT *cp, int mode, GWEN_BUFFER *buffer)
{
if (cp) {
AQH_HTTP_CONTENT_FILES *xcp;
xcp=GWEN_INHERIT_GETDATA(AQH_HTTP_CONTENT, AQH_HTTP_CONTENT_FILES, cp);
if (xcp) {
if (xcp->headerData==NULL) {
if (xcp->headerFilename) {
int rv;
GWEN_BUFFER *fileBuffer;
fileBuffer=GWEN_Buffer_new(0, 256, 0, 1);
rv=GWEN_SyncIo_Helper_ReadFile(xcp->headerFilename, fileBuffer);
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error reading header file \"%s\": %d", xcp->headerFilename, rv);
GWEN_Buffer_free(fileBuffer);
return rv;
}
xcp->headerData=strdup(GWEN_Buffer_GetStart(fileBuffer));
GWEN_Buffer_free(fileBuffer);
}
}
if (xcp->headerData)
GWEN_Buffer_AppendString(buffer, xcp->headerData);
}
}
return 0;
}
int _addClosingContent(AQH_HTTP_CONTENT *cp, int mode, GWEN_BUFFER *buffer)
{
if (cp) {
AQH_HTTP_CONTENT_FILES *xcp;
xcp=GWEN_INHERIT_GETDATA(AQH_HTTP_CONTENT, AQH_HTTP_CONTENT_FILES, cp);
if (xcp) {
if (xcp->footerData==NULL) {
if (xcp->footerFilename) {
int rv;
GWEN_BUFFER *fileBuffer;
fileBuffer=GWEN_Buffer_new(0, 256, 0, 1);
rv=GWEN_SyncIo_Helper_ReadFile(xcp->footerFilename, fileBuffer);
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error reading footer file \"%s\": %d", xcp->footerFilename, rv);
GWEN_Buffer_free(fileBuffer);
return rv;
}
xcp->footerData=strdup(GWEN_Buffer_GetStart(fileBuffer));
GWEN_Buffer_free(fileBuffer);
}
if (xcp->footerData)
GWEN_Buffer_AppendString(buffer, xcp->footerData);
}
}
}
return 0;
}