Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix truncated command-line shown in the web interface #1168

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/api/variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ int escape_json_string(char *dest, int dl, char *src, int sl) {
return j;
}

int get_json_state(char *buf, int len) {
int get_json_state(char *buf, int len, char *sbuf, int slen) {
int ptr = 0, first = 1, i, j, off, string;
_symbols *p;
char escape[200]; // string variable max len

memset(escape, 0, sizeof(escape));
strlcatf(buf, len, ptr, "{\n");
for (i = 0; sym[i] != NULL; i++) {
for (j = 0; sym[i][j].name; j++) {
Expand All @@ -207,10 +205,10 @@ int get_json_state(char *buf, int len) {
if (p->type < VAR_ARRAY) {
if (string) {
int len2 =
snprintf_pointer(escape, sizeof(escape) - 1, p->type,
snprintf_pointer(sbuf, slen - 1, p->type,
p->addr, p->multiplier);
ptr +=
escape_json_string(buf + ptr, len - ptr, escape, len2);
escape_json_string(buf + ptr, len - ptr, sbuf, len2);
} else
ptr += snprintf_pointer(buf + ptr, len - ptr, p->type,
p->addr, p->multiplier);
Expand All @@ -221,9 +219,9 @@ int get_json_state(char *buf, int len) {
strlcatf(buf, len, ptr, ",");
if (string) {
int len2 = snprintf_pointer(
escape, sizeof(escape) - 1, p->type,
sbuf, slen - 1, p->type,
((char *)p->addr) + off + p->skip, p->multiplier);
ptr += escape_json_string(buf + ptr, len - ptr, escape,
ptr += escape_json_string(buf + ptr, len - ptr, sbuf,
len2);
} else
ptr += snprintf_pointer(
Expand All @@ -239,9 +237,9 @@ int get_json_state(char *buf, int len) {
strlcatf(buf, len, ptr, ",");
if (string) {
int len2 = snprintf_pointer(
escape, sizeof(escape) - 1, p->type,
sbuf, slen - 1, p->type,
p1[off] ? p1[off] + p->skip : zero, p->multiplier);
ptr += escape_json_string(buf + ptr, len - ptr, escape,
ptr += escape_json_string(buf + ptr, len - ptr, sbuf,
len2);
} else
ptr += snprintf_pointer(
Expand Down Expand Up @@ -275,12 +273,11 @@ int get_json_state(char *buf, int len) {
get_data_string funs = (get_data_string)p->addr;
strlcatf(buf, len, ptr, "[");
for (off = 0; off < p->len; off++) {
memset(escape, 0, sizeof(escape));
funs(off, escape, sizeof(escape) - 1);
memset(sbuf, 0, slen);
funs(off, sbuf, slen - 1);
if (off > 0)
strlcatf(buf, len, ptr, ",");
ptr += escape_json_string(buf + ptr, len - ptr, escape,
strlen(escape));
ptr += escape_json_string(buf + ptr, len - ptr, sbuf, slen);
}
strlcatf(buf, len, ptr, "]");
// LOG("func_str -> %s", buf);
Expand Down
6 changes: 5 additions & 1 deletion src/api/variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@

#define VAR_LENGTH 20

#define JSON_STATE_MAXLEN (256 * 1024)
#define JSON_BANDWIDTH_MAXLEN 1024
#define JSON_STRING_MAXLEN 1024

typedef int (*get_data_int)(int p);
typedef int64_t (*get_data_int64)(int p);
typedef char *(*get_data_string)(int p, char *dest, int max_len);
Expand All @@ -54,7 +58,7 @@ void *get_var_address(char *var, float *multiplier, int *type, void *storage,
int ls);

int escape_json_string(char *dest, int dl, char *src, int sl);
int get_json_state(char *buf, int len);
int get_json_state(char *buf, int len, char *sbuf, int slen);
int get_json_bandwidth(char *buf, int len);

#endif
13 changes: 8 additions & 5 deletions src/minisatip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1438,8 +1438,6 @@ int read_rtsp(sockets *s) {
return 0; \
}

#define JSON_STATE_MAXLEN (256 * 1024)

int read_http(sockets *s) {
char *arg[50];
char buf[2000]; // the XML should not be larger than 1400 as it will create
Expand Down Expand Up @@ -1591,24 +1589,29 @@ int read_http(sockets *s) {

if (strcmp(arg[1], "/state.json") == 0) {
char *buf = _malloc(JSON_STATE_MAXLEN);
int len = get_json_state(buf, JSON_STATE_MAXLEN);
char *sbuf = _malloc(JSON_STRING_MAXLEN);
memset(sbuf, 0, JSON_STRING_MAXLEN);
int len =
get_json_state(buf, JSON_STATE_MAXLEN, sbuf, JSON_STRING_MAXLEN);
http_response(s, 200,
"Content-Type: application/json\r\n"
"Connection: close\r\n"
"Access-Control-Allow-Origin: *",
buf, 0, len);
_free(buf);
_free(sbuf);
return 0;
}

if (strcmp(arg[1], "/bandwidth.json") == 0) {
char buf[1024];
int len = get_json_bandwidth(buf, sizeof(buf));
char *buf = _malloc(JSON_BANDWIDTH_MAXLEN);
int len = get_json_bandwidth(buf, JSON_BANDWIDTH_MAXLEN);
http_response(s, 200,
"Content-Type: application/json\r\n"
"Connection: close\r\n"
"Access-Control-Allow-Origin: *",
buf, 0, len);
_free(buf);
return 0;
}

Expand Down
Loading