From e130cfddbed670c26f45bbbf8d8bb94f0e114ea3 Mon Sep 17 00:00:00 2001 From: Tim Connors <1707595+spacelama@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:15:48 +1000 Subject: [PATCH] Respond to POSTs as well as GETs in /cm handler (#900) * add method to parse args from body instead of just url * Allow /cm to POST and PUT as well as GET * forgot to ensure didn't misparse non GET/PUT/POST in /cm requests --------- Co-authored-by: Tim Connors --- src/httpserver/http_fns.c | 16 +++++++++++++--- src/httpserver/new_http.c | 20 +++++++++++++------- src/httpserver/new_http.h | 1 + 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 222b07618..5b0a3d841 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2249,17 +2249,27 @@ int http_fn_ha_cfg(http_request_t* request) { int http_fn_cm(http_request_t* request) { char tmpA[128]; char* long_str_alloced = 0; - int commandLen; + int commandLen = 0; http_setup(request, httpMimeTypeJson); // exec command - commandLen = http_getArg(request->url, "cmnd", tmpA, sizeof(tmpA)); + if (request->method == HTTP_GET) { + commandLen = http_getArg(request->url, "cmnd", tmpA, sizeof(tmpA)); + //ADDLOG_INFO(LOG_FEATURE_HTTP, "Got here (GET) %s;%s;%d\n", request->url, tmpA, commandLen); + } else if (request->method == HTTP_POST || request->method == HTTP_PUT) { + commandLen = http_getRawArg(request->bodystart, "cmnd", tmpA, sizeof(tmpA)); + //ADDLOG_INFO(LOG_FEATURE_HTTP, "Got here (POST) %s;%s;%d\n", request->bodystart, tmpA, commandLen); + } 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); + if (request->method == HTTP_GET) { + http_getArg(request->url, "cmnd", long_str_alloced, commandLen); + } else if (request->method == HTTP_POST || request->method == HTTP_PUT) { + http_getRawArg(request->bodystart, "cmnd", long_str_alloced, commandLen); + } CMD_ExecuteCommand(long_str_alloced, COMMAND_FLAG_SOURCE_HTTP); JSON_ProcessCommandReply(long_str_alloced, skipToNextWord(long_str_alloced), request, (jsonCb_t)hprintf255, COMMAND_FLAG_SOURCE_HTTP); free(long_str_alloced); diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index f936a72fe..9f9c46e3f 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -351,14 +351,9 @@ int http_copyCarg(const char* atin, char* to, int maxSize) { return realSize; } -int http_getArg(const char* base, const char* name, char* o, int maxSize) { +int http_getRawArg(const char* base, const char* name, char* o, int maxSize) { *o = '\0'; - while (*base != '?') { - if (*base == 0) - return 0; - base++; - } - base++; + while (*base) { const char* at = http_checkArg(base, name); if (at) { @@ -375,6 +370,17 @@ int http_getArg(const char* base, const char* name, char* o, int maxSize) { } return 0; } +int http_getArg(const char* base, const char* name, char* o, int maxSize) { + *o = '\0'; + while (*base != '?') { + if (*base == '\0') + return 0; + base++; + } + base++; + + return http_getRawArg(base, name, o, maxSize); +} int http_getArgInteger(const char* base, const char* name) { char tmp[16]; if (http_getArg(base, name, tmp, sizeof(tmp)) == 0) diff --git a/src/httpserver/new_http.h b/src/httpserver/new_http.h index 451b0dc57..0f5fff17d 100644 --- a/src/httpserver/new_http.h +++ b/src/httpserver/new_http.h @@ -67,6 +67,7 @@ int postany(http_request_t* request, const char* str, int len); void misc_formatUpTimeString(int totalSeconds, char* o); // void HTTP_AddBuildFooter(http_request_t *request); // void HTTP_AddHeader(http_request_t *request); +int http_getRawArg(const char* base, const char* name, char* o, int maxSize); int http_getArg(const char* base, const char* name, char* o, int maxSize); int http_getArgInteger(const char* base, const char* name);