aqhome: added service definitions, started implementing HTTP service.

This commit is contained in:
Martin Preuss
2023-07-24 21:49:17 +02:00
parent db5d6cb980
commit 16ce958964
33 changed files with 2346 additions and 7 deletions

View File

@@ -38,6 +38,7 @@
aqhomestorage_p.h
aqhomestorage.h
init.h
http.h
</headers>
<sources>
@@ -45,6 +46,7 @@
aqhomestorage.c
init.c
http.c
main.c
</sources>

View File

@@ -11,6 +11,7 @@
#include "aqhome/data/storage.h"
#include "aqhome/service/session.h"
#include <gwenhywfar/endpoint.h>

View File

@@ -22,6 +22,9 @@
#define AQHOME_STORAGE_DEFAULT_MQTT_KEEPALIVE 600
#define AQHOME_STORAGE_DEFAULT_MQTT_PORT 1883
#define AQHOME_STORAGE_SITEHEADER "site-header.html"
#define AQHOME_STORAGE_SITEFOOTER "site-footer.html"
struct AQHOME_STORAGE {
@@ -36,7 +39,6 @@ struct AQHOME_STORAGE {
AQH_STORAGE *storage;
char *pidFile;
};
#endif

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!-- copyright (c) 2023 by martin -->
<meta name="generator" content="FTE 1.1" />
<meta name="revised" content="martin,2023-07-23" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="martin" />
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>AqHome Storage Service</title>
</head>
<body>
</body>
</html>

View File

@@ -0,0 +1,16 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!-- copyright (c) 2023 by martin -->
<meta name="generator" content="FTE 1.1" />
<meta name="revised" content="martin,2023-07-23" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="martin" />
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>AqHome Storage Service</title>
</head>
<body>

192
apps/aqhome-storage/http.c Normal file
View File

@@ -0,0 +1,192 @@
/****************************************************************************
* 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)
{
}

View File

@@ -0,0 +1,30 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQHOME_STORAGE_HTTP_H
#define AQHOME_STORAGE_HTTP_H
#include "./aqhomestorage.h"
int AqHomeStorage_AddFile(AQHOME_STORAGE *aqh, AQH_SESSION *session, const char *fname, GWEN_BUFFER *buf);
int AqHomeStorage_AddSiteHeader(AQHOME_STORAGE *aqh, AQH_SESSION *session, GWEN_BUFFER *buf);
int AqHomeStorage_AddSiteFooter(AQHOME_STORAGE *aqh, AQH_SESSION *session, GWEN_BUFFER *buf);
void AqHomeStorage_AddStatusLine(AQHOME_STORAGE *aqh, int code, const char *msg, const char *proto, GWEN_BUFFER *buf);
void AqHomeStorage_AddHeader(AQHOME_STORAGE *aqh, GWEN_DB_NODE *dbHeader, GWEN_BUFFER *buf);
int AqHomeStorage_ParsePostBody(AQHOME_STORAGE *aqh, const GWEN_MSG *msgReceived, GWEN_DB_NODE *dbBody);
#endif