Update examples to NOT use mg_*mprintf

This commit is contained in:
cpq committed 2022-08-13 20:09:51 +01:00
1 parent f122bd5797
commit 189aa80743
3 files changed
+11 -17

No files matched your search

+7 -10
View File
@@ -27,7 +27,7 @@ static long getparam(struct mg_http_message *hm, const char *json_path) {
return dv;
}
static size_t printdata(void (*out)(char, void *), void *ptr, va_list *ap) {
static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) {
unsigned start = va_arg(*ap, unsigned);
unsigned max = start + CHUNK_SIZE;
const char *comma = "";
@@ -45,23 +45,20 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data;
if (mg_http_match_uri(hm, "/api/data")) {
const char *headers = "content-type: text/json\r\n";
long start = getparam(hm, "$.start");
long version = getparam(hm, "$.version");
char *response = NULL;
MG_INFO(("%.*s", (int) hm->body.len, hm->body.ptr));
MG_DEBUG(("%.*s", (int) hm->body.len, hm->body.ptr));
if (version > 0 && version != s_version) {
// Version mismatch: s_data has changed while client fetches it
// Tell client to restart
response = mg_mprintf("{%Q:%Q, %Q:%ld}", "error", "wrong version",
"version", version);
mg_http_reply(c, 200, headers, "{%Q:%Q, %Q:%ld}", "error",
"wrong version", "version", version);
} else {
// Return data, up to CHUNK_SIZE elements
response = mg_mprintf("{%Q:%ld,%Q:%ld,%Q:[%M]}", "version", s_version,
"start", start, "data", printdata, start);
mg_http_reply(c, 200, headers, "{%Q:%ld,%Q:%ld,%Q:[%M]}", "version",
s_version, "start", start, "data", printdata, start);
}
mg_http_reply(c, 200, "Content-Type: text/json\r\n", "%s", response);
MG_INFO(("%s", response));
free(response);
} else {
struct mg_http_serve_opts opts = {0};
opts.root_dir = s_root_dir;