Allow user-defined data when receiving weblogs from callback (#770)

* Adding userData to getweblogs callback.
Allows user-specific data to be available in the callback that returns weblog HTML data.

* Adding userData to getweblogs callback.
Allows user-specific data to be available in the callback that returns weblog HTML data.

Return HTTP response code and content type only when no weblog callback defined
This commit is contained in:
frankonski 2024-02-11 17:34:19 -05:00 committed by GitHub
parent 5011733449
commit 48793eff12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 13 deletions

View File

@ -256,7 +256,7 @@ void HAPClient::processRequest(){
} }
if(homeSpan.webLog.isEnabled && !strncmp(body,homeSpan.webLog.statusURL.c_str(),homeSpan.webLog.statusURL.length())){ // GET STATUS - AN OPTIONAL, NON-HAP-R2 FEATURE if(homeSpan.webLog.isEnabled && !strncmp(body,homeSpan.webLog.statusURL.c_str(),homeSpan.webLog.statusURL.length())){ // GET STATUS - AN OPTIONAL, NON-HAP-R2 FEATURE
getStatusURL(this,NULL); getStatusURL(this,NULL,NULL);
return; return;
} }
@ -1095,7 +1095,7 @@ int HAPClient::putPrepareURL(char *json){
////////////////////////////////////// //////////////////////////////////////
void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *)){ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *, void *), void *user_data){
char clocktime[33]; char clocktime[33];
@ -1119,9 +1119,9 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *
if(hapClient) if(hapClient)
LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",hapClient->client.remoteIP().toString().c_str()); LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",hapClient->client.remoteIP().toString().c_str());
hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(callBack); hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(callBack).setCallbackUserData(user_data);
hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n"; if(!callBack) hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n";
hapOut << "<html><head><title>" << homeSpan.displayName << "</title>\n"; hapOut << "<html><head><title>" << homeSpan.displayName << "</title>\n";
hapOut << "<style>body {background-color:lightblue;} th, td {padding-right: 10px; padding-left: 10px; border:1px solid black;}" << homeSpan.webLog.css.c_str() << "</style></head>\n"; hapOut << "<style>body {background-color:lightblue;} th, td {padding-right: 10px; padding-left: 10px; border:1px solid black;}" << homeSpan.webLog.css.c_str() << "</style></head>\n";
hapOut << "<body class=bod1><h2>" << homeSpan.displayName << "</h2>\n"; hapOut << "<body class=bod1><h2>" << homeSpan.displayName << "</h2>\n";
@ -1191,7 +1191,7 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *
if(homeSpan.weblogCallback){ if(homeSpan.weblogCallback){
String usrString; String usrString;
homeSpan.weblogCallback(usrString); homeSpan.weblogCallback(usrString); // Callback to add user-defined html in top table.
hapOut << usrString.c_str(); hapOut << usrString.c_str();
} }
@ -1593,7 +1593,7 @@ void HapOut::HapStreamBuffer::flushBuffer(){
buffer[num]='\0'; // add null terminator but DO NOT increment num (we don't want terminator considered as part of buffer) buffer[num]='\0'; // add null terminator but DO NOT increment num (we don't want terminator considered as part of buffer)
if(callBack) if(callBack)
callBack(buffer); callBack(buffer,callBackUserData);
if(logLevel<=homeSpan.getLogLevel()){ if(logLevel<=homeSpan.getLogLevel()){
if(enablePrettyPrint) // if pretty print needed, use formatted method if(enablePrettyPrint) // if pretty print needed, use formatted method
@ -1649,8 +1649,9 @@ int HapOut::HapStreamBuffer::sync(){
indent=0; indent=0;
if(callBack){ if(callBack){
callBack(NULL); callBack(NULL,callBackUserData);
callBack=NULL; callBack=NULL;
callBackUserData=NULL;
} }
mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash

View File

@ -170,7 +170,7 @@ struct HAPClient {
static void checkTimedWrites(); // checks for expired Timed Write PIDs, and clears any found (HAP Section 6.7.2.4) static void checkTimedWrites(); // checks for expired Timed Write PIDs, and clears any found (HAP Section 6.7.2.4)
static void eventNotify(SpanBuf *pObj, int nObj, int ignoreClient=-1); // transmits EVENT Notifications for nObj SpanBuf objects, pObj, with optional flag to ignore a specific client static void eventNotify(SpanBuf *pObj, int nObj, int ignoreClient=-1); // transmits EVENT Notifications for nObj SpanBuf objects, pObj, with optional flag to ignore a specific client
static void getStatusURL(HAPClient *, void (*)(const char *)); // GET / status (an optional, non-HAP feature) static void getStatusURL(HAPClient *, void (*)(const char *, void *), void *); // GET / status (an optional, non-HAP feature)
class HAPTLV : public TLV8 { // dedicated class for HAP TLV8 records class HAPTLV : public TLV8 { // dedicated class for HAP TLV8 records
public: public:
@ -198,7 +198,8 @@ class HapOut : public std::ostream {
size_t indent=0; size_t indent=0;
uint8_t *hash; uint8_t *hash;
mbedtls_sha512_context *ctx; mbedtls_sha512_context *ctx;
void (*callBack)(const char *)=NULL; void (*callBack)(const char *, void *)=NULL;
void *callBackUserData = NULL;
void flushBuffer(); void flushBuffer();
int_type overflow(int_type c) override; int_type overflow(int_type c) override;
@ -220,7 +221,8 @@ class HapOut : public std::ostream {
HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);} HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);}
HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);} HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);}
HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;hapBuffer.logLevel=0;return(*this);} HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;hapBuffer.logLevel=0;return(*this);}
HapOut& setCallback(void(*f)(const char *)){hapBuffer.callBack=f;return(*this);} HapOut& setCallback(void(*f)(const char *, void *)){hapBuffer.callBack=f;return(*this);}
HapOut& setCallbackUserData(void *userData){hapBuffer.callBackUserData=userData;return(*this);}
uint8_t *getHash(){return(hapBuffer.hash);} uint8_t *getHash(){return(hapBuffer.hash);}
size_t getSize(){return(hapBuffer.getSize());} size_t getSize(){return(hapBuffer.getSize());}

View File

@ -1148,8 +1148,8 @@ void Span::processSerialCommand(const char *c){
/////////////////////////////// ///////////////////////////////
void Span::getWebLog(void (*f)(const char *)){ void Span::getWebLog(void (*f)(const char *, void *), void *user_data){
HAPClient::getStatusURL(NULL,f); HAPClient::getStatusURL(NULL,f,user_data);
} }
/////////////////////////////// ///////////////////////////////

View File

@ -362,7 +362,7 @@ class Span{
Span& setWebLogCSS(const char *css){webLog.css="\n" + String(css) + "\n";return(*this);} Span& setWebLogCSS(const char *css){webLog.css="\n" + String(css) + "\n";return(*this);}
Span& setWebLogCallback(void (*f)(String &)){weblogCallback=f;return(*this);} Span& setWebLogCallback(void (*f)(String &)){weblogCallback=f;return(*this);}
void getWebLog(void (*f)(const char *)); void getWebLog(void (*f)(const char *, void *), void *);
Span& setVerboseWifiReconnect(bool verbose=true){verboseWifiReconnect=verbose;return(*this);} Span& setVerboseWifiReconnect(bool verbose=true){verboseWifiReconnect=verbose;return(*this);}