diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index e661b54fb..cc698a386 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -3,6 +3,7 @@ * Fix Sonoff Pow R2 and Sonoff S31 Serial interface hang caused by Sonoff Basic R2 driver delay implementation (and possibly core bug) * Change command WebSend Host header field from IP address to hostname (#4331) * Add to command WebSend option to send a direct path when command starts with a slash (#4329) + * Consolidate LTrim into Trim * * 6.3.0.6 20181110 * Change GUI Configure Module by using AJAX for data fetch to cut page size (and memory use) by 40% diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index e92536a1b..0c7b16beb 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -531,7 +531,7 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) char *blcommand = strtok(dataBuf, ";"); while ((blcommand != NULL) && (backlog_index != bl_pointer)) { while(true) { - blcommand = LTrim(blcommand); + blcommand = Trim(blcommand); if (!strncasecmp_P(blcommand, PSTR(D_CMND_BACKLOG), strlen(D_CMND_BACKLOG))) { blcommand += strlen(D_CMND_BACKLOG); // Skip unnecessary command Backlog } else { @@ -1224,9 +1224,9 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) if (5 == tpos) { Settings.tflag[ts].hour = (value < 0) ? 0 : (value > 23) ? 23 : value; } if (6 == tpos) { Settings.toffset[ts] = (value < -900) ? -900 : (value > 900) ? 900 : value; } } - p = LTrim(p); // Skip spaces + p = Trim(p); // Skip spaces if (tpos && (*p == ',')) { p++; } // Skip separator - p = LTrim(p); // Skip spaces + p = Trim(p); // Skip spaces q = p; // Reset any value entered flag value = strtol(p, &p, 10); tpos++; // Next parameter diff --git a/sonoff/support.ino b/sonoff/support.ino index 0c5203d69..7df675b13 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -290,6 +290,7 @@ char* UpperCase_P(char* dest, const char* source) return dest; } +/* char* LTrim(char* p) { while ((*p != '\0') && (isblank(*p))) { @@ -308,14 +309,13 @@ char* RTrim(char* p) *q = '\0'; return p; } +*/ char* Trim(char* p) { - if (*p == '\0') { return p; } - while (isspace(*p)) { p++; } // Trim leading spaces - if (*p == '\0') { return p; } + while ((*p != '\0') && isblank(*p)) { p++; } // Trim leading spaces char* q = p + strlen(p) -1; - while (isspace(*q) && q >= p) { q--; } // Trim trailing spaces + while ((q >= p) && isblank(*q)) { q--; } // Trim trailing spaces q++; *q = '\0'; return p; diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index ed14472cd..25c4c013b 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -1909,23 +1909,26 @@ int WebSend(char *buffer) uint16_t nport = 80; int status = 1; // Wrong parameters - host = strtok_r(buffer, "]", &command); // buffer = [192.168.178.86:80,admin:joker] POWER1 ON + // buffer = | [ 192.168.178.86 : 80 , admin : joker ] POWER1 ON | + host = strtok_r(buffer, "]", &command); // host = | [ 192.168.178.86 : 80 , admin : joker |, command = | POWER1 ON | if (host && command) { - host = LTrim(host); - host++; // Skip [ - host = strtok_r(host, ",", &user); // host = 192.168.178.86:80,admin:joker > 192.168.178.86:80 - host = strtok_r(host, ":", &port); // host = 192.168.178.86:80 > 192.168.178.86 - if (user) { - user = strtok_r(user, ":", &password); // user = admin:joker > admin + host = Trim(host); // host = |[ 192.168.178.86 : 80 , admin : joker| + host++; // host = | 192.168.178.86 : 80 , admin : joker| - Skip [ + host = strtok_r(host, ",", &user); // host = | 192.168.178.86 : 80 |, user = | admin : joker| + host = strtok_r(host, ":", &port); // host = | 192.168.178.86 |, port = | 80 | + host = Trim(host); // host = |192.168.178.86| + if (port) { + port = Trim(port); // port = |80| + nport = atoi(port); } - -//snprintf_P(log_data, sizeof(log_data), PSTR("DBG: Buffer |%X|, Host |%X|, Port |%X|, User |%X|, Password |%X|, Command |%X|"), buffer, host, port, user, password, command); -//AddLog(LOG_LEVEL_DEBUG); - - if (port) { nport = atoi(port); } + if (user) { + user = strtok_r(user, ":", &password); // user = | admin |, password = | joker| + user = Trim(user); // user = |admin| + if (password) { password = Trim(password); } // password = |joker| + } + command = Trim(command); // command = |POWER1 ON| or |/any/link/starting/with/a/slash.php?log=123| String nuri = ""; - command = LTrim(command); if (command[0] != '/') { nuri = "/cm?"; if (user && password) { @@ -1937,7 +1940,7 @@ int WebSend(char *buffer) } nuri += F("cmnd="); } - nuri += command; // command = POWER1 ON or /any/link/starting/with/a/slash.php?log=123 + nuri += command; String uri = UrlEncode(nuri); IPAddress host_ip;