Skip to content

Commit

Permalink
Respond to POSTs as well as GETs in /cm handler (#900)
Browse files Browse the repository at this point in the history
* 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 <tconnors@rather.puzzling.org>
  • Loading branch information
spacelama and spacelama authored Aug 16, 2023
1 parent a5cf6b3 commit e130cfd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 13 additions & 7 deletions src/httpserver/new_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/httpserver/new_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e130cfd

Please sign in to comment.