unit test for long command and accept long commands

This commit is contained in:
openshwprojects
2022-12-17 18:07:35 +01:00
parent 4284daa293
commit 87f2ec0b13
6 changed files with 118 additions and 15 deletions

View File

@ -1340,6 +1340,8 @@ int http_fn_cmd_tool(http_request_t* request) {
commandResult_t res;
const char *resStr;
char tmpA[128];
char *long_str_alloced = 0;
int commandLen;
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Command tool");
@ -1348,11 +1350,23 @@ int http_fn_cmd_tool(http_request_t* request) {
poststr(request, "Please consider using 'Web Application' console with more options and real time log view. <br>");
poststr(request, "Remember that some commands are added after a restart when a driver is activated... <br>");
if (http_getArg(request->url, "cmd", tmpA, sizeof(tmpA))) {
commandLen = http_getArg(request->url, "cmd", tmpA, sizeof(tmpA));
if (commandLen) {
poststr(request, "<br>");
// all log printfs made by command will be sent also to request
LOG_SetCommandHTTPRedirectReply(request);
res = CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_CONSOLE);
if (commandLen > (sizeof(tmpA) - 5)) {
commandLen += 8;
long_str_alloced = (char*)malloc(commandLen);
if (long_str_alloced) {
http_getArg(request->url, "cmd", long_str_alloced, commandLen);
CMD_ExecuteCommand(long_str_alloced, COMMAND_FLAG_SOURCE_CONSOLE);
free(long_str_alloced);
}
}
else {
CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_CONSOLE);
}
LOG_SetCommandHTTPRedirectReply(0);
resStr = CMD_GetResultString(res);
hprintf255(request, "<h3>%s</h3>", resStr);
@ -2268,11 +2282,25 @@ int http_tasmota_json_status_generic(http_request_t* request) {
}
int http_fn_cm(http_request_t* request) {
char tmpA[128];
char *long_str_alloced = 0;
int commandLen;
http_setup(request, httpMimeTypeJson);
// exec command
if (http_getArg(request->url, "cmnd", tmpA, sizeof(tmpA))) {
CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_HTTP);
commandLen = http_getArg(request->url, "cmnd", tmpA, sizeof(tmpA));
if (commandLen) {
if (commandLen > (sizeof(tmpA) - 5)) {
commandLen += 8;
long_str_alloced = (char*)malloc(commandLen);
if (long_str_alloced) {
http_getArg(request->url, "cmnd", long_str_alloced, commandLen);
CMD_ExecuteCommand(long_str_alloced, COMMAND_FLAG_SOURCE_HTTP);
free(long_str_alloced);
}
}
else {
CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_HTTP);
}
if (!wal_strnicmp(tmpA, "POWER", 5)) {

View File

@ -219,9 +219,9 @@ void http_setup(http_request_t* request, const char* type) {
poststr(request, "Accept-Ranges: none");
poststr(request, "\r\n");
poststr(request, "Transfer-Encoding: chunked");
#endif
poststr(request, "\r\n");
poststr(request, "Connection: close");
#endif
poststr(request, "\r\n"); // end headers with double CRLF
poststr(request, "\r\n");
}
@ -277,11 +277,14 @@ const char* http_checkArg(const char* p, const char* n) {
return p;
}
void http_copyCarg(const char* atin, char* to, int maxSize) {
int http_copyCarg(const char* atin, char* to, int maxSize) {
int a, b;
int realSize;
const unsigned char* at = (unsigned char*)atin;
while (*at != 0 && *at != '&' && *at != ' ' && maxSize > 1) {
realSize = 0;
while (*at != 0 && *at != '&' && *at != ' ') {
#if 0
* to = *at;
to++;
@ -303,20 +306,36 @@ void http_copyCarg(const char* atin, char* to, int maxSize) {
b -= ('A' - 10);
else
b -= '0';
*to++ = 16 * a + b;
// can we afford to place this char in the target?
if (maxSize > 1) {
maxSize--;
*to++ = 16 * a + b;
}
realSize++;
at += 3;
}
else if (*at == '+') {
*to++ = ' ';
// can we afford to place this char in the target?
if (maxSize > 1) {
maxSize--;
*to++ = ' ';
}
realSize++;
at++;
}
else {
*to++ = *at++;
// can we afford to place this char in the target?
if (maxSize > 1) {
maxSize--;
*to++ = *at;
}
realSize++;
at++;
}
maxSize--;
#endif
}
*to = 0;
return realSize;
}
int http_getArg(const char* base, const char* name, char* o, int maxSize) {
@ -331,8 +350,7 @@ int http_getArg(const char* base, const char* name, char* o, int maxSize) {
const char* at = http_checkArg(base, name);
if (at) {
at++;
http_copyCarg(at, o, maxSize);
return 1;
return http_copyCarg(at, o, maxSize);
}
while (*base != '&') {
if (*base == 0) {