From 897fdffcf9e4a22d2d26e3a40403e54969d3140a Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 24 Jul 2023 21:59:42 +0200 Subject: [PATCH] aqhome: cleanup AQH_HttpService_ParsePostBody(). --- aqhome/http/httpservice.c | 73 ++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/aqhome/http/httpservice.c b/aqhome/http/httpservice.c index 15a9e6a..30e9c9a 100644 --- a/aqhome/http/httpservice.c +++ b/aqhome/http/httpservice.c @@ -25,7 +25,7 @@ GWEN_INHERIT(AQH_SERVICE, AQH_HTTP_SERVICE) static void GWENHYWFAR_CB _freeData(void *bp, void *p); -static int _checkHeaderGetBodySize(const GWEN_MSG *msgReceived); +static int _parsePostBody(const char *s, int contentLength, GWEN_DB_NODE *dbBody); @@ -230,15 +230,47 @@ void AQH_HttpService_AddHeader(AQH_SERVICE *sv, GWEN_DB_NODE *dbHeader, GWEN_BUF int AQH_HttpService_ParsePostBody(AQH_SERVICE *sv, const GWEN_MSG *msgReceived, GWEN_DB_NODE *dbBody) { + GWEN_DB_NODE *dbParsedInfo; + GWEN_DB_NODE *dbHeader; + const char *contentType; int contentLength; const char *s; - contentLength=_checkHeaderGetBodySize(msgReceived); + dbParsedInfo=GWEN_Msg_GetDbParsedInfo(msgReceived); + if (dbParsedInfo==NULL) { + DBG_ERROR(AQH_LOGDOMAIN, "No parsed info in received message"); + return GWEN_ERROR_BAD_DATA; + } + dbHeader=GWEN_DB_GetGroup(dbParsedInfo, GWEN_PATH_FLAGS_PATHMUSTEXIST, "header"); + if (dbHeader==NULL) { + DBG_ERROR(AQH_LOGDOMAIN, "No http header group in received message"); + return GWEN_ERROR_BAD_DATA; + } + + contentType=GWEN_DB_GetCharValue(dbHeader, "content-type", 0, NULL); + if (!(contentType && *contentType && strcasecmp(contentType, "application/x-www-form-urlencoded")==0)) { + DBG_ERROR(NULL, "Invalid or missing content type [%s]", contentType?contentType:""); + return GWEN_ERROR_BAD_DATA; + } + + contentLength=GWEN_Msg_GetParsedPayloadSize(msgReceived); + if (contentLength<1) { + DBG_ERROR(NULL, "Empty message body"); + return 0; + } + if (contentLength<1) { DBG_ERROR(NULL, "Empty message body"); return 0; } s=(const char*)(GWEN_Msg_GetConstBuffer(msgReceived)+GWEN_Msg_GetParsedPayloadOffset(msgReceived)); + return _parsePostBody(s, contentLength, dbBody); +} + + + +int _parsePostBody(const char *s, int contentLength, GWEN_DB_NODE *dbBody) +{ while(contentLength>0) { const char *sNameStart; int nameLen; @@ -289,40 +321,3 @@ int AQH_HttpService_ParsePostBody(AQH_SERVICE *sv, const GWEN_MSG *msgReceived, -int _checkHeaderGetBodySize(const GWEN_MSG *msgReceived) -{ - GWEN_DB_NODE *dbParsedInfo; - GWEN_DB_NODE *dbHeader; - const char *contentType; - int contentLength; - - dbParsedInfo=GWEN_Msg_GetDbParsedInfo(msgReceived); - if (dbParsedInfo==NULL) { - DBG_ERROR(AQH_LOGDOMAIN, "No parsed info in received message"); - return GWEN_ERROR_BAD_DATA; - } - dbHeader=GWEN_DB_GetGroup(dbParsedInfo, GWEN_PATH_FLAGS_PATHMUSTEXIST, "header"); - if (dbHeader==NULL) { - DBG_ERROR(AQH_LOGDOMAIN, "No http header group in received message"); - return GWEN_ERROR_BAD_DATA; - } - - contentType=GWEN_DB_GetCharValue(dbHeader, "content-type", 0, NULL); - if (!(contentType && *contentType && strcasecmp(contentType, "application/x-www-form-urlencoded")==0)) { - DBG_ERROR(NULL, "Invalid or missing content type [%s]", contentType?contentType:""); - return GWEN_ERROR_BAD_DATA; - } - - contentLength=GWEN_Msg_GetParsedPayloadSize(msgReceived); - if (contentLength<1) { - DBG_ERROR(NULL, "Empty message body"); - return 0; - } - - return contentLength; -} - - - - -