mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 23:21:04 +08:00
Update to use MG_ESC()
This commit is contained in:
parent
f4cf40d38f
commit
1908d3200e
@ -115,8 +115,8 @@ static void send_notification(struct mg_mgr *mgr, const char *fmt, ...) {
|
||||
|
||||
// Send simulated metrics data to the dashboard, for chart rendering
|
||||
static void timer_metrics_fn(void *param) {
|
||||
send_notification(param, "{%m:%m,%m:[%lu, %d]}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "metrics", mg_print_esc, 0, "data",
|
||||
send_notification(param, "{%m:%m,%m:[%lu, %d]}", MG_ESC("name"),
|
||||
MG_ESC("metrics"), MG_ESC("data"),
|
||||
(unsigned long) ourtime(NULL),
|
||||
10 + (int) ((double) rand() * 10 / RAND_MAX));
|
||||
}
|
||||
@ -136,19 +136,18 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
memset(&sub_opts, 0, sizeof(sub_opts));
|
||||
sub_opts.topic = mg_str(s_config.sub);
|
||||
sub_opts.qos = 2;
|
||||
|
||||
|
||||
mg_mqtt_sub(s_mqtt, &sub_opts);
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "config", mg_print_esc, 0, "data");
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", MG_ESC("name"),
|
||||
MG_ESC("config"), MG_ESC("data"));
|
||||
MG_INFO(("MQTT connected, server %s", MQTT_SERVER));
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
struct mg_mqtt_message *mm = ev_data;
|
||||
send_notification(c->mgr, "{%m:%m,%m:{%m: %m, %m: %m, %m: %d}}",
|
||||
mg_print_esc, 0, "name", mg_print_esc, 0, "message",
|
||||
mg_print_esc, 0, "data", mg_print_esc, 0, "topic",
|
||||
mg_print_esc, (int) mm->topic.len, mm->topic.ptr,
|
||||
mg_print_esc, 0, "data", mg_print_esc, (int) mm->data.len,
|
||||
mm->data.ptr, mg_print_esc, 0, "qos", (int) mm->qos);
|
||||
send_notification(
|
||||
c->mgr, "{%m:%m,%m:{%m: %m, %m: %m, %m: %d}}", MG_ESC("name"),
|
||||
MG_ESC("message"), MG_ESC("data"), MG_ESC("topic"), mg_print_esc,
|
||||
(int) mm->topic.len, mm->topic.ptr, MG_ESC("data"), mg_print_esc,
|
||||
(int) mm->data.len, mm->data.ptr, MG_ESC("qos"), (int) mm->qos);
|
||||
} else if (ev == MG_EV_MQTT_CMD) {
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_DEBUG(("%lu cmd %d qos %d", c->id, mm->cmd, mm->qos));
|
||||
@ -156,8 +155,8 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
s_mqtt = NULL;
|
||||
if (s_connected) {
|
||||
s_connected = false;
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "config", mg_print_esc, 0, "data");
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", MG_ESC("name"),
|
||||
MG_ESC("config"), MG_ESC("data"));
|
||||
}
|
||||
}
|
||||
(void) fnd;
|
||||
@ -226,16 +225,14 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
mg_http_reply(c, 403, "", "Denied\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||
#ifdef DISABLE_ROUTING
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m}\n", mg_print_esc, 0,
|
||||
"url", mg_print_esc, 0, s_config.url, mg_print_esc, 0,
|
||||
"pub", mg_print_esc, 0, s_config.pub, mg_print_esc, 0,
|
||||
"sub", mg_print_esc, 0, s_config.sub);
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m}\n", MG_ESC("url"),
|
||||
MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub),
|
||||
MG_ESC("sub"), MG_ESC(s_config.sub));
|
||||
#else
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m,%m:%s}\n", mg_print_esc,
|
||||
0, "url", mg_print_esc, 0, s_config.url, mg_print_esc, 0,
|
||||
"pub", mg_print_esc, 0, s_config.pub, mg_print_esc, 0,
|
||||
"sub", mg_print_esc, 0, s_config.sub, mg_print_esc, 0,
|
||||
"connected", s_connected ? "true" : "false");
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m,%m:%m,%m:%s}\n", MG_ESC("url"),
|
||||
MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub),
|
||||
MG_ESC("sub"), MG_ESC(s_config.sub), MG_ESC("connected"),
|
||||
s_connected ? "true" : "false");
|
||||
#endif
|
||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||
// Admins only
|
||||
@ -244,8 +241,8 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
update_config(&hm->body, "pub", &s_config.pub);
|
||||
update_config(&hm->body, "sub", &s_config.sub);
|
||||
if (s_mqtt) s_mqtt->is_closing = 1; // Ask to disconnect from MQTT
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, "config", mg_print_esc, 0, "data");
|
||||
send_notification(c->mgr, "{%m:%m,%m:null}", MG_ESC("name"),
|
||||
MG_ESC("config"), MG_ESC("data"));
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else {
|
||||
mg_http_reply(c, 403, "", "Denied\n");
|
||||
@ -267,9 +264,8 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
c->data[0] = 'W'; // Mark ourselves as a event listener
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m}\n", mg_print_esc, 0, "user",
|
||||
mg_print_esc, 0, u->name, mg_print_esc, 0, "token",
|
||||
mg_print_esc, 0, u->token);
|
||||
mg_http_reply(c, 200, NULL, "{%m:%m,%m:%m}\n", MG_ESC("user"),
|
||||
MG_ESC(u->name), MG_ESC("token"), MG_ESC(u->token));
|
||||
} else {
|
||||
struct mg_http_serve_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
|
@ -52,14 +52,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (version > 0 && version != s_version) {
|
||||
// Version mismatch: s_data has changed while client fetches it
|
||||
// Tell client to restart
|
||||
mg_http_reply(c, 200, headers, "{%m:%m, %m:%ld}", mg_print_esc, 0,
|
||||
"error", mg_print_esc, 0, "wrong version", mg_print_esc,
|
||||
0, "version", version);
|
||||
mg_http_reply(c, 200, headers, "{%m:%m, %m:%ld}", MG_ESC("error"),
|
||||
MG_ESC("wrong version"), MG_ESC("version"), version);
|
||||
} else {
|
||||
// Return data, up to CHUNK_SIZE elements
|
||||
mg_http_reply(c, 200, headers, "{%m:%ld,%m:%ld,%m:[%M]}", mg_print_esc,
|
||||
0, "version", s_version, mg_print_esc, 0, "start", start,
|
||||
mg_print_esc, 0, "data", printdata, start);
|
||||
mg_http_reply(c, 200, headers, "{%m:%ld,%m:%ld,%m:[%M]}",
|
||||
MG_ESC("version"), s_version, MG_ESC("start"), start,
|
||||
MG_ESC("data"), printdata, start);
|
||||
}
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {0};
|
||||
|
@ -59,9 +59,9 @@ static void timer_fn(void *arg) {
|
||||
// Broadcast message to all connected websocket clients.
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->data[0] != 'W') continue;
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%m:%m,%m:[%d,%d,%d]}", mg_print_esc, 0,
|
||||
"method", mg_print_esc, 0, "notification1", mg_print_esc, 0,
|
||||
"params", 1, 2, 3);
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%m:%m,%m:[%d,%d,%d]}",
|
||||
MG_ESC("method"), MG_ESC("notification1"), MG_ESC("params"), 1,
|
||||
2, 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,14 +52,12 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
mg_http_reply(c, 403, "", "Denied\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/data")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m,%m:%m}\n", mg_print_esc, 0, "text", mg_print_esc, 0,
|
||||
"Hello!", mg_print_esc, 0, "data", mg_print_esc, 0,
|
||||
"somedata");
|
||||
"{%m:%m,%m:%m}\n", MG_ESC("text"), MG_ESC("Hello!"),
|
||||
MG_ESC("data"), MG_ESC("somedata"));
|
||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m,%m:%m}\n", mg_print_esc, 0, "user", mg_print_esc, 0,
|
||||
u->name, mg_print_esc, 0, "token", mg_print_esc, 0,
|
||||
u->token);
|
||||
"{%m:%m,%m:%m}\n", MG_ESC("user"), MG_ESC(u->name),
|
||||
MG_ESC("token"), MG_ESC(u->token));
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
mg_http_serve_dir(c, ev_data, &opts);
|
||||
|
@ -38,10 +38,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m,%m:%m,%m:%m}\n", mg_print_esc, 0, "url",
|
||||
mg_print_esc, 0, s_config.url, mg_print_esc, 0, "pub",
|
||||
mg_print_esc, 0, s_config.pub, mg_print_esc, 0, "sub",
|
||||
mg_print_esc, 0, s_config.sub);
|
||||
"{%m:%m,%m:%m,%m:%m}\n", MG_ESC("url"),
|
||||
MG_ESC(s_config.url), MG_ESC("pub"), MG_ESC(s_config.pub),
|
||||
MG_ESC("sub"), MG_ESC(s_config.sub));
|
||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||
struct mg_str json = hm->body;
|
||||
update_config(json, "$.url", &s_config.url);
|
||||
|
@ -45,14 +45,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (version > 0 && version == s_version) {
|
||||
// Version match: no changes
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m,%m:%ld}\n", mg_print_esc, 0, "status",
|
||||
mg_print_esc, 0, "no change", mg_print_esc, 0, "version",
|
||||
version);
|
||||
"{%m:%m,%m:%ld}\n", MG_ESC("status"), MG_ESC("no change"),
|
||||
MG_ESC("version"), version);
|
||||
} else {
|
||||
// Version mismatch, return data
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%ld,%m:[%M]}\n", mg_print_esc, 0, "version",
|
||||
s_version, mg_print_esc, 0, "data", printdata);
|
||||
"{%m:%ld,%m:[%M]}\n", MG_ESC("version"), s_version,
|
||||
MG_ESC("data"), printdata);
|
||||
}
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
|
||||
|
@ -29,9 +29,8 @@ static void push(struct mg_mgr *mgr, const char *name, const void *data) {
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->data[0] != 'W') continue;
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%m:%m,%m:%m}", mg_print_esc, 0, "name",
|
||||
mg_print_esc, 0, name, mg_print_esc, 0, "data", mg_print_esc,
|
||||
0, data);
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%m:%m,%m:%m}", MG_ESC("name"),
|
||||
MG_ESC(name), MG_ESC("data"), MG_ESC(data));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_http_match_uri(hm, "/api/f1")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%d}\n",
|
||||
mg_print_esc, 0, "result", 123);
|
||||
MG_ESC("result"), 123);
|
||||
} else if (mg_http_match_uri(hm, "/api/sum")) {
|
||||
// Attempt to fetch a JSON array from the body, hm->body
|
||||
struct mg_str json = hm->body;
|
||||
@ -27,7 +27,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
mg_json_get_num(json, "$[1]", &num2)) {
|
||||
// Success! create a JSON response
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%g}\n",
|
||||
mg_print_esc, 0, "result", num1 + num2);
|
||||
MG_ESC("result"), num1 + num2);
|
||||
}
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
|
||||
|
Loading…
x
Reference in New Issue
Block a user