Files
aqhomecontrol/apps/aqhome-storage/http.c

193 lines
4.7 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
#include "./http.h"
#include "./aqhomestorage_p.h"
#include "aqhome/msg/endpoint_tty.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/mqtt/endpoint_mqttc.h"
#include "aqhome/http/endpoint_http.h"
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpd.h>
#include <gwenhywfar/endpoint_msgio.h>
#include <gwenhywfar/syncio.h>
#include <gwenhywfar/url.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
//#define I18N(msg) msg
#define I18S(msg) msg
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static GWEN_MSG *_handleUrl(AQHOME_STORAGE *aqh, const GWEN_MSG *msgReceived, const GWEN_URL *url);
static GWEN_MSG *_handleUrl_login(AQHOME_STORAGE *aqh, const GWEN_MSG *msgReceived, const GWEN_URL *url);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
int AqHomeStorage_AddFile(GWEN_UNUSED AQHOME_STORAGE *aqh, GWEN_UNUSED AQH_SESSION *session, const char *fname, GWEN_BUFFER *buf)
{
if (fname && *fname) {
int rv;
rv=GWEN_SyncIo_Helper_ReadFile(fname, buf);
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error reading file \"%s\": %d", fname, rv);
return rv;
}
}
return 0;
}
int AqHomeStorage_AddSiteHeader(AQHOME_STORAGE *aqh, AQH_SESSION *session, GWEN_BUFFER *buf)
{
return AqHomeStorage_AddFile(aqh, session, AQHOME_STORAGE_SITEHEADER, buf);
}
int AqHomeStorage_AddSiteFooter(AQHOME_STORAGE *aqh, AQH_SESSION *session, GWEN_BUFFER *buf)
{
return AqHomeStorage_AddFile(aqh, session, AQHOME_STORAGE_SITEFOOTER, buf);
}
GWEN_MSG *AqHomeStorage_HandleMessage(AQHOME_STORAGE *aqh, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msgReceived)
{
GWEN_DB_NODE *dbParsedMsgInfo;
GWEN_DB_NODE *dbCommand;
GWEN_DB_NODE *dbHeader;
const char *sCmd;
const char *sUrl;
GWEN_URL *gUrl;
const char *sPath;
GWEN_MSG *msgToSend;
dbParsedMsgInfo=GWEN_Msg_GetDbParsedInfo(msgReceived);
if (dbParsedMsgInfo==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "No parsed msg info in received message, SNH");
return NULL;
}
dbCommand=GWEN_DB_GetGroup(dbParsedMsgInfo, GWEN_PATH_FLAGS_PATHMUSTEXIST, "command");
dbHeader=GWEN_DB_GetGroup(dbParsedMsgInfo, GWEN_PATH_FLAGS_PATHMUSTEXIST, "header");
if (dbCommand==NULL || dbHeader==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "Either command group or headser group missing in parsed msg info, SNH");
return NULL;
}
sCmd=GWEN_DB_GetCharValue(dbCommand, "command", 0, NULL);
if (!(sCmd && *sCmd)) {
DBG_ERROR(NULL, "No command in message");
return NULL;
}
sUrl=GWEN_DB_GetCharValue(dbCommand, "url", 0, NULL);
if (!(sUrl && *sUrl)) {
DBG_ERROR(NULL, "No url in message");
return NULL;
}
gUrl=GWEN_Url_fromCommandString(sUrl);
if (gUrl==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "Invalid url [%s]", sUrl);
return NULL;
}
sPath=GWEN_Url_GetPath(gUrl);
if (!(sPath && *sPath)) {
DBG_ERROR(AQH_LOGDOMAIN, "Invalid URL (no path): [%s]", sUrl);
GWEN_Url_free(gUrl);
return NULL;
}
/* now we have all info from the incoming http message */
msgToSend=_handleUrl(aqh, msgReceived, gUrl);
if (msgToSend==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "Error handling url [%s]", sUrl);
}
GWEN_Url_free(gUrl);
return msgToSend;
}
GWEN_MSG *_handleUrl(AQHOME_STORAGE *aqh, const GWEN_MSG *msgReceived, const GWEN_URL *url)
{
const char *sPath;
sPath=GWEN_Url_GetPath(url);
if (strcasecmp(sPath, "/login")==0)
return _handleUrl_login(aqh, msgReceived, url);
else {
DBG_ERROR(NULL, "Invalid URL [%s]", sPath);
return NULL;
}
}
GWEN_MSG *_handleUrl_login(AQHOME_STORAGE *aqh, const GWEN_MSG *msgReceived, const GWEN_URL *url)
{
}