diff --git a/src/EspBootstrapDict.h b/src/EspBootstrapDict.h index 17c273f..5e4e517 100644 --- a/src/EspBootstrapDict.h +++ b/src/EspBootstrapDict.h @@ -41,7 +41,7 @@ class EspBootstrapDict : public EspBootstrapBase { EspBootstrapDict(); virtual ~EspBootstrapDict(); - int8_t run(Dictionary &aDict, uint8_t aNum = 0, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE); + int8_t run(Dictionary &aDict, uint8_t aNum = 0, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE, bool aSecPass = true); void handleRoot (); void handleSubmit (); inline void cancel() { iCancelAP = true; } ; @@ -51,6 +51,7 @@ class EspBootstrapDict : public EspBootstrapBase { int8_t doRun(); bool iCancelAP; + bool iSecurePassword; Dictionary* iDict; }; @@ -75,7 +76,7 @@ void __espbootstrap_handlesubmit() { } -int8_t EspBootstrapDict::run(Dictionary &aDict, uint8_t aNum, uint32_t aTimeout) { +int8_t EspBootstrapDict::run(Dictionary &aDict, uint8_t aNum, uint32_t aTimeout, bool aSecPass) { if (aNum == 0) { iNum = aDict.count() - 1; } @@ -87,6 +88,7 @@ int8_t EspBootstrapDict::run(Dictionary &aDict, uint8_t aNum, uint32_t aTimeout) iDict = &aDict; iTimeout = aTimeout; + iSecurePassword = aSecPass; iCancelAP = false; return doRun(); @@ -165,7 +167,7 @@ void EspBootstrapDict::handleRoot() { for (int i = 1; i <= iNum; i++) { String s = d(i); s.toUpperCase(); - if ( s.indexOf("PASSWORD") >= 0 || s.indexOf("PWD") >= 0 ) { + if ( iSecurePassword && (s.indexOf("PASSWORD") >= 0 || s.indexOf("PWD") >= 0) ) { snprintf(buf, BUFLEN, "

", i, d(i).c_str(), i, i, d[i].c_str() ); } else { diff --git a/src/EspBootstrapMap.h b/src/EspBootstrapMap.h index c123f84..8e0fac4 100644 --- a/src/EspBootstrapMap.h +++ b/src/EspBootstrapMap.h @@ -41,7 +41,7 @@ class EspBootstrapMap : public EspBootstrapBase { EspBootstrapMap(); virtual ~EspBootstrapMap(); - int8_t run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE); + int8_t run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE, bool aSecPass = true); void handleRoot (); void handleSubmit (); inline void cancel() { iCancelAP = true; } ; @@ -51,6 +51,7 @@ class EspBootstrapMap : public EspBootstrapBase { int8_t doRun(); bool iCancelAP; + bool iSecurePassword; const char** iTitles; char** iMap; }; @@ -76,14 +77,15 @@ void __espbootstrap_handlesubmit() { } -int8_t EspBootstrapMap::run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout) { +int8_t EspBootstrapMap::run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout, bool aSecPass) { iNum = aNum; iTitles = aTitles; iMap = aMap; iTimeout = aTimeout; iCancelAP = false; - + iSecurePassword = aSecPass; + return doRun(); } @@ -157,7 +159,12 @@ void EspBootstrapMap::handleRoot() { iServer->sendContent(buf); for (int i = 1; i <= iNum; i++) { - snprintf(buf, BUFLEN, "

", i, iTitles[i], i, i, iMap[i - 1] ); + if ( iSecurePassword && false ) { // fr future use + snprintf(buf, BUFLEN, "

", i, iTitles[i], i, i, iMap[i - 1] ); + } + else { + snprintf(buf, BUFLEN, "

", i, iTitles[i], i, i, iMap[i - 1] ); + } iServer->sendContent(buf); } iServer->sendContent("
"); diff --git a/src/JsonConfigBase.h b/src/JsonConfigBase.h index 4ebe057..8dc7a8b 100644 --- a/src/JsonConfigBase.h +++ b/src/JsonConfigBase.h @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #include - #define JSON_OK 0 #define JSON_ERR (-1) #define JSON_COMMA (-20) @@ -51,15 +50,127 @@ class JsonConfigBase { virtual ~JsonConfigBase(); protected: - virtual int8_t _doParse(size_t aLen, uint16_t aNum); - virtual int16_t _nextChar() { return JSON_EOF; }; + virtual int8_t _doParse(Stream& aJson, uint16_t aNum); virtual int8_t _storeKeyValue(const char* aKey, const char* aValue) { return JSON_MEM; }; }; JsonConfigBase::JsonConfigBase() {} JsonConfigBase::~JsonConfigBase() {} -int8_t JsonConfigBase::_doParse(size_t aLen, uint16_t aNum) { +int8_t JsonConfigBase::_doParse(Stream& aJson, uint16_t aNum) { + bool insideQoute = false; + bool nextVerbatim = false; + bool isValue = false; + bool isComment = false; + int p = 0; + int8_t rc; + String currentKey; + String currentValue; + + while ( aJson.peek() >= 0 ) { + char c = aJson.read(); + +//#ifdef _LIBDEBUG_ +//Serial.print((uint8_t)c); +//Serial.print(" ("); +//Serial.print(c); +//Serial.println(")"); +//#endif + + if ( isComment ) { + if ( c == '\n' ) { + isComment = false; + isValue = false; + } + continue; + } + if (nextVerbatim) { + nextVerbatim = false; + } + + // not a comment and not a verbatim char + else { + // process all special cases: '\', '"', ':', and ',' + if (c == '\\' ) { + nextVerbatim = true; + continue; + } + + if ( c == '\"' ) { + if (!insideQoute) { + if ( isValue ) { + if ( currentValue.length() > 0 ) return JSON_FMT; + } + else { + if ( currentKey.length() > 0 ) return JSON_FMT; + } + insideQoute = true; + continue; + } + else { + insideQoute = false; + continue; + } + } + + if (c == '\n') { + if ( insideQoute ) { + return JSON_QUOTE; + } + if ( nextVerbatim ) { + return JSON_BCKSL; + } + } + +#ifdef _JSON_ASCII_ONLY + if ( c > 127 ) continue; // ignore non-ascii characters +#endif + + if (!insideQoute) { + if ( c == '#' ) { + isComment = true; + continue; + } + + if (c == ':') { + if ( isValue ) { + return JSON_COMMA; //missing comma probably + } + isValue = true; + continue; + } + + if ( c == '{' || c == ' ' || c == '\t' || c == '\r' ) continue; + + if ( c == ',' || c == '\n' || c == '}') { + if ( isValue ) { + if ( currentValue.length() == 0 ) return JSON_FMT; + isValue = false; + rc = _storeKeyValue( currentKey.c_str(), currentValue.c_str() ); + if (rc) return JSON_MEM; // if error - exit with an error code + currentValue = String(); + currentKey = String(); + p++; + if (aNum > 0 && p >= aNum) break; + } + else { + if ( c == ',' ) return JSON_FMT; + } + continue; + } + } + } + if (isValue) currentValue.concat(c); + else currentKey.concat(c); + } + if (insideQoute || nextVerbatim || (aNum > 0 && p < aNum )) return JSON_EOF; + #ifdef _LIBDEBUG_ + Serial.printf("Dictionary::jload: DICTIONARY_OK\n"); + #endif + return JSON_OK; +} + +/* int8_t JsonConfigBase::_doParse(size_t aLen, uint16_t aNum) { bool insideQoute = false; bool nextVerbatim = false; @@ -143,7 +254,11 @@ Serial.print(c); Serial.print("("); Serial.print((int)c); Serial.print(")"); isValue = false; continue; } - if ( c == '{' || c == '}' || c == ' ' || c == '\t' || c == '\r' ) continue; + if ( c == '{' || c == ' ' || c == '\t' || c == '\n' || c == '\r' ) continue; +#ifdef _JSON_ASCII_ONLY + if ( c > 127 ) continue; // ignore non-ascii characters +#endif + if ( c == '}' ) break; return JSON_FMT; } } @@ -157,7 +272,7 @@ Serial.print(c); Serial.print("("); Serial.print((int)c); Serial.print(")"); Serial.printf("JsonConfigBase::_doParse: JSON_OK\n"); #endif return JSON_OK; -} +} */ diff --git a/src/JsonConfigHttp.h b/src/JsonConfigHttp.h index adf7843..02bf8ba 100644 --- a/src/JsonConfigHttp.h +++ b/src/JsonConfigHttp.h @@ -59,9 +59,8 @@ class JsonConfigHttp : public JsonConfigBase { int8_t parse(const String aHost, uint16_t aPort, String aUrl, Dictionary& aDict, int aNum = 0); protected: - virtual int16_t _nextChar(); virtual int8_t _storeKeyValue(const char* aKey, const char* aValue); - virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); }; + virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); }; private: int8_t parseCommon(int aHttpResult, int aNum); @@ -69,8 +68,8 @@ class JsonConfigHttp : public JsonConfigBase { Dictionary* iDict; WiFiClient iClient; HTTPClient iHttp; - String iPayload; - size_t iIndex; +// String iPayload; +// size_t iIndex; }; #ifndef _JSONCONFIG_NOSTATIC @@ -122,9 +121,9 @@ int8_t JsonConfigHttp::parseCommon(int aHttpResult, int aNum) { #endif if (httpCode > 0) { if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { - iPayload = iHttp.getString(); - iIndex = 0; - rc = _doParse(iPayload.length(), aNum); +// iPayload = iHttp.getString(); +// iIndex = 0; + rc = _doParse(iHttp.getStream(), aNum); return rc; } } @@ -140,14 +139,14 @@ int8_t JsonConfigHttp::parseCommon(int aHttpResult, int aNum) { } -int16_t JsonConfigHttp::_nextChar() { +/* int16_t JsonConfigHttp::_nextChar() { if (iIndex < iPayload.length() ) { return (int16_t) iPayload[iIndex++]; } else { return JSON_EOF; } -} +} */ int8_t JsonConfigHttp::_storeKeyValue(const char* aKey, const char* aValue){ diff --git a/src/JsonConfigHttpMap.h b/src/JsonConfigHttpMap.h index b9ff88f..fa25351 100644 --- a/src/JsonConfigHttpMap.h +++ b/src/JsonConfigHttpMap.h @@ -58,17 +58,16 @@ class JsonConfigHttpMap : public JsonConfigBase { int8_t parse(const String aHost, uint16_t aPort, String aUrl, char** aMap, int aNum); protected: - virtual int16_t _nextChar(); virtual int8_t _storeKeyValue(const char* aKey, const char* aValue); - virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); }; + virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); }; private: int8_t parseCommon(int aHttpResult, int aNum); char** iMap; HTTPClient iHttp; - String iPayload; - size_t iIndex; + // String iPayload; + // size_t iIndex; size_t iParamIndex; }; @@ -124,10 +123,10 @@ int8_t JsonConfigHttpMap::parseCommon(int aHttpResult, int aNum) { #endif if (httpCode > 0) { if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { - iPayload = iHttp.getString(); - iIndex = 0; + // iPayload = iHttp.getString(); + // iIndex = 0; iParamIndex = 0; - rc = _doParse(iPayload.length(), aNum); + rc = _doParse(iHttp.getStream(), aNum); #ifdef _LIBDEBUG_ Serial.printf("JsonConfigHttpMap::parseCommon rc %d\n", rc ); #endif @@ -145,7 +144,7 @@ int8_t JsonConfigHttpMap::parseCommon(int aHttpResult, int aNum) { return JSON_ERR; // should never get here anyway - but stupid compiler complains. } - +/* int16_t JsonConfigHttpMap::_nextChar() { if (iIndex < iPayload.length() ) { return (int16_t) iPayload[iIndex++]; @@ -154,7 +153,7 @@ int16_t JsonConfigHttpMap::_nextChar() { return JSON_EOF; } } - + */ int8_t JsonConfigHttpMap::_storeKeyValue(const char* aKey, const char* aValue){ #ifdef _LIBDEBUG_ diff --git a/src/JsonConfigSPIFFS.h b/src/JsonConfigSPIFFS.h index dbf07bc..b0467ea 100644 --- a/src/JsonConfigSPIFFS.h +++ b/src/JsonConfigSPIFFS.h @@ -55,9 +55,8 @@ class JsonConfigSPIFFS : public JsonConfigBase { int8_t parse(const String aUrl, Dictionary& aDict, int aNum = 0); protected: - virtual int16_t _nextChar(); virtual int8_t _storeKeyValue(const char* aKey, const char* aValue); - virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); }; + virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); }; private: Dictionary* iDict; @@ -89,16 +88,16 @@ int8_t JsonConfigSPIFFS::parse(const String aUrl, Dictionary& aDict, int aNum) { } iDict = &aDict; - rc = _doParse ( iF.size(), aNum ); + rc = _doParse ( iF, aNum ); iF.close(); return rc; } -int16_t JsonConfigSPIFFS::_nextChar() { - return (int16_t) iF.read(); -} +// int16_t JsonConfigSPIFFS::_nextChar() { + // return (int16_t) iF.read(); +// } int8_t JsonConfigSPIFFS::_storeKeyValue(const char* aKey, const char* aValue){ diff --git a/src/JsonConfigSPIFFSMap.h b/src/JsonConfigSPIFFSMap.h index 9d1a12d..f0f2d5a 100644 --- a/src/JsonConfigSPIFFSMap.h +++ b/src/JsonConfigSPIFFSMap.h @@ -56,9 +56,8 @@ class JsonConfigSPIFFSMap : public JsonConfigBase { int8_t parse(const String aUrl, char** aMap, int aNum); protected: - virtual int16_t _nextChar(); virtual int8_t _storeKeyValue(const char* aKey, const char* aValue); - virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); }; + virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); }; private: char** iMap; @@ -87,16 +86,16 @@ int8_t JsonConfigSPIFFSMap::parse(const String aUrl, char** aMap, int aNum) { iMap = aMap; iParamIndex = 0; - rc = _doParse ( iF.size(), aNum ); + rc = _doParse ( iF, aNum ); iF.close(); return rc; } -char JsonConfigSPIFFSMap::_nextChar() { - return (int16_t) iF.read(); -} +// char JsonConfigSPIFFSMap::_nextChar() { + // return (int16_t) iF.read(); +// } int8_t JsonConfigSPIFFSMap::_storeKeyValue(const char* aKey, const char* aValue){