Add RPC API

This commit is contained in:
Sergey Lyubka 2022-07-27 00:46:05 +01:00
parent 9cacf443e1
commit 0dabc45780
13 changed files with 332 additions and 75 deletions

View File

@ -156,7 +156,7 @@ mongoose.c: Makefile $(wildcard src/*) $(wildcard mip/*.c)
(cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c mip/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
mongoose.h: $(HDRS) Makefile
(cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h mip/mip.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
(cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h src/rpc.h mip/mip.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
clean:
rm -rf $(PROG) *.exe *.o *.dSYM unit_test* valgrind_unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack unpacked

View File

@ -2803,6 +2803,10 @@ static const struct packed_file {
{NULL, NULL, 0, 0}
};
static int scmp(const char *a, const char *b) {
while (*a && (*a == *b)) a++, b++;
return *(const unsigned char *) a - *(const unsigned char *) b;
}
const char *mg_unlist(size_t no);
const char *mg_unlist(size_t no) {
return packed_files[no].name;
@ -2811,7 +2815,7 @@ const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {
const struct packed_file *p;
for (p = packed_files; p->name != NULL; p++) {
if (strcmp(p->name, name) != 0) continue;
if (scmp(p->name, name) != 0) continue;
if (size != NULL) *size = p->size - 1;
if (mtime != NULL) *mtime = p->mtime;
return (const char *) p->data;

View File

@ -7,64 +7,20 @@
static const char *s_listen_on = "ws://localhost:8000";
static const char *s_web_root = "web_root";
static void *s_rpc_head = NULL;
// RPC functions. Take string params, return (allocated) string result
static char *sum(struct mg_str params) {
static void rpc_sum(struct mg_rpc_req *r) {
double a = 0.0, b = 0.0;
mg_json_get_num(params, "$[0]", &a);
mg_json_get_num(params, "$[1]", &b);
return mg_mprintf("%g", a + b);
mg_json_get_num(r->frame, "$.params[0]", &a);
mg_json_get_num(r->frame, "$.params[1]", &b);
mg_rpc_ok(r, "%g", a + b);
}
static char *multiply(struct mg_str params) {
static void rpc_mul(struct mg_rpc_req *r) {
double a = 0.0, b = 0.0;
mg_json_get_num(params, "$[0]", &a);
mg_json_get_num(params, "$[1]", &b);
return mg_mprintf("%g", a * b);
}
static void process_json_message(struct mg_connection *c, struct mg_str frame) {
struct mg_str params = mg_str(""), id = mg_str("");
int params_off = 0, params_len = 0, id_off = 0, id_len = 0;
char *response = NULL;
// Parse websocket message, which should be a JSON-RPC frame like this:
// { "id": 3, "method": "sum", "params": [1,2] }
char *method = mg_json_get_str(frame, "$.method");
id_off = mg_json_get(frame.ptr, (int) frame.len, "$.id", &id_len);
params_off = mg_json_get(frame.ptr, (int) frame.len, "$.params", &params_len);
params = mg_str_n(frame.ptr + params_off, params_len);
id = mg_str_n(frame.ptr + id_off, id_len);
if (method == NULL || id_off < 0 || params_off < 0) {
// Invalid frame. Return error and include this frame as error message
response = mg_mprintf("{%Q:{%Q:%d,%Q:%.*Q}", "error", "code", -32700,
"message", (int) frame.len, frame.ptr);
} else if (strcmp(method, "sum") == 0) {
char *result = sum(params);
response = mg_mprintf("{%Q:%.*s, %Q:%s}", "id", (int) id.len, id.ptr,
"result", result);
free(result);
} else if (strcmp(method, "mul") == 0) {
char *result = multiply(params);
response = mg_mprintf("{%Q:%.*s, %Q:%s}", "id", (int) id.len, id.ptr,
"result", result);
free(result);
} else {
response =
mg_mprintf("{%Q:%.*s, %Q:{%Q:%d,%Q:%Q}", "id", (int) id.len, id.ptr,
"error", "code", -32601, "message", "Method not found");
}
// Send the response back to the client
if (response) {
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "%s", response);
MG_INFO(("[%.*s] -> [%s]", (int) frame.len, frame.ptr, response));
}
free(method);
free(response);
mg_json_get_num(r->frame, "$.params[0]", &a);
mg_json_get_num(r->frame, "$.params[1]", &b);
mg_rpc_ok(r, "%g", a * b);
}
// This RESTful server implements the following endpoints:
@ -89,7 +45,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
process_json_message(c, wm->data);
char *resp = NULL;
mg_rpc_process(&s_rpc_head, wm->data, mg_pfn_realloc, &resp);
mg_ws_send(c, resp, strlen(resp), WEBSOCKET_OP_TEXT);
free(resp);
}
(void) fn_data;
}
@ -111,9 +70,16 @@ int main(void) {
mg_mgr_init(&mgr); // Init event manager
mg_log_set("3");
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT, timer_fn, &mgr); // Init timer
// Configure JSON-RPC functions we're going to handle
mg_rpc_add(&s_rpc_head, mg_str("sum"), rpc_sum, NULL);
mg_rpc_add(&s_rpc_head, mg_str("mul"), rpc_mul, NULL);
mg_rpc_add(&s_rpc_head, mg_str("rpc.list"), mg_rpc_list, &s_rpc_head);
printf("Starting WS listener on %s/websocket\n", s_listen_on);
mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
mg_mgr_free(&mgr); // Deallocate event manager
mg_rpc_free(&s_rpc_head); // Deallocate RPC handlers
return 0;
}

View File

@ -1814,6 +1814,10 @@ static const struct packed_file {
{NULL, NULL, 0, 0}
};
static int scmp(const char *a, const char *b) {
while (*a && (*a == *b)) a++, b++;
return *(const unsigned char *) a - *(const unsigned char *) b;
}
const char *mg_unlist(size_t no);
const char *mg_unlist(size_t no) {
return packed_files[no].name;
@ -1822,7 +1826,7 @@ const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {
const struct packed_file *p;
for (p = packed_files; p->name != NULL; p++) {
if (strcmp(p->name, name) != 0) continue;
if (scmp(p->name, name) != 0) continue;
if (size != NULL) *size = p->size - 1;
if (mtime != NULL) *mtime = p->mtime;
return (const char *) p->data;

View File

@ -615,7 +615,7 @@ static void mg_putchar_iobuf_static(char ch, void *param) {
}
}
void mg_putchar_iobuf(char ch, void *param) {
void mg_pfn_iobuf(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len + 2 > io->size) mg_iobuf_resize(io, io->size + 64);
if (io->len + 2 <= io->size) {
@ -626,7 +626,7 @@ void mg_putchar_iobuf(char ch, void *param) {
// We don't use realloc() in mongoose, so resort to inefficient calloc
// Every new character reallocates the whole string
void mg_putchar_realloc(char ch, void *param) {
void mg_pfn_realloc(char ch, void *param) {
char *s, *buf = *(char **) param;
size_t len = buf == NULL ? 0 : strlen(buf);
if ((s = (char *) calloc(1, len + 2)) != NULL) {
@ -2555,9 +2555,10 @@ int mg_json_get(const char *s, int len, const char *path, int *toklen) {
if (c == '"') {
int n = mg_pass_string(&s[i + 1], len - i - 1);
if (n < 0) return n;
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
// printf("K[%.*s] %d %d\n", n, &s[i + 1], depth, ed);
if (depth == ed && path[pos - 1] == '.' &&
memcmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
pos += n;
}
i += n + 1;
@ -3296,7 +3297,7 @@ size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) {
size_t old = c->send.len;
va_list tmp;
va_copy(tmp, ap);
mg_vrprintf(mg_putchar_iobuf, &c->send, fmt, &tmp);
mg_vrprintf(mg_pfn_iobuf, &c->send, fmt, &tmp);
return c->send.len - old;
}
@ -3546,6 +3547,102 @@ void mg_mgr_init(struct mg_mgr *mgr) {
mgr->dns6.url = "udp://[2001:4860:4860::8888]:53";
}
#ifdef MG_ENABLE_LINES
#line 1 "src/rpc.c"
#endif
struct mg_rpc {
struct mg_rpc *next;
struct mg_str method;
void (*fn)(struct mg_rpc_req *);
void *fn_data;
};
void mg_rpc_add(void **head, struct mg_str method,
void (*fn)(struct mg_rpc_req *), void *fn_data) {
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data;
rpc->next = (struct mg_rpc *) *head, *head = rpc;
}
void mg_rpc_free(void **head) {
while (head != NULL && *head != NULL) {
struct mg_rpc *rpc = *(struct mg_rpc **) head;
*head = rpc->next;
free((void *) rpc->method.ptr);
free(rpc);
}
}
void mg_rpc_process(void **head, struct mg_str s, mg_pfn_t pfn, void *pfnd) {
struct mg_rpc_req req = {{s.ptr, s.len}, pfn, pfnd, NULL};
int len, off = mg_json_get(s.ptr, (int) s.len, "$.method", &len);
if (off > 0 && s.ptr[off] == '"') {
struct mg_str m = mg_str_n(&s.ptr[off + 1], (size_t) len - 2);
struct mg_rpc *h = *(struct mg_rpc **) head;
while (h != NULL && !mg_match(m, h->method, NULL)) h = h->next;
if (h != NULL) {
req.fn_data = h->fn_data;
h->fn(&req);
} else {
mg_rpc_err(&req, -32601, "\"%.*s not found\"", (int) m.len, m.ptr);
}
} else {
mg_rpc_err(&req, -32700, "%.*Q", (int) s.len, s.ptr);
}
}
void mg_rpc_vok(struct mg_rpc_req *r, const char *fmt, va_list *ap) {
int len, off = mg_json_get(r->frame.ptr, (int) r->frame.len, "$.id", &len);
if (off > 0) {
mg_rprintf(r->pfn, r->pfn_data, "{%Q:%.*s,%Q:", "id", len,
&r->frame.ptr[off], "result");
mg_vrprintf(r->pfn, r->pfn_data, fmt == NULL ? "null" : fmt, ap);
mg_rprintf(r->pfn, r->pfn_data, "}");
}
}
void mg_rpc_ok(struct mg_rpc_req *r, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
mg_rpc_vok(r, fmt, &ap);
va_end(ap);
}
void mg_rpc_verr(struct mg_rpc_req *r, int code, const char *fmt, va_list *ap) {
int len, off = mg_json_get(r->frame.ptr, (int) r->frame.len, "$.id", &len);
mg_rprintf(r->pfn, r->pfn_data, "{");
if (off > 0) {
mg_rprintf(r->pfn, r->pfn_data, "%Q:%.*s,", "id", len, &r->frame.ptr[off]);
}
mg_rprintf(r->pfn, r->pfn_data, "%Q:{%Q:%d,%Q:", "error", "code", code,
"message");
mg_vrprintf(r->pfn, r->pfn_data, fmt == NULL ? "null" : fmt, ap);
mg_rprintf(r->pfn, r->pfn_data, "}}");
}
void mg_rpc_err(struct mg_rpc_req *r, int code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
mg_rpc_verr(r, code, fmt, &ap);
va_end(ap);
}
static size_t print_methods(mg_pfn_t pfn, void *pfn_data, va_list *ap) {
struct mg_rpc *h, **head = (struct mg_rpc **) va_arg(*ap, void **);
size_t len = 0;
for (h = *head; h != NULL; h = h->next) {
len += mg_rprintf(pfn, pfn_data, "%s%.*Q", h == *head ? "" : ",",
(int) h->method.len, h->method.ptr);
}
return len;
}
void mg_rpc_list(struct mg_rpc_req *r) {
mg_rpc_ok(r, "[%M]", print_methods, r->fn_data);
}
#ifdef MG_ENABLE_LINES
#line 1 "src/sha1.c"
#endif

View File

@ -749,10 +749,10 @@ double mg_atod(const char *buf, int len, int *numlen);
size_t mg_dtoa(char *buf, size_t len, double d, int width);
char *mg_remove_double_dots(char *s);
typedef void (*mg_pc_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer
void mg_putchar_realloc(char ch, void *param); // Print to malloced str
void mg_putchar_iobuf(char ch, void *param); // Print to iobuf
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
void mg_pfn_realloc(char ch, void *param); // Print to malloced str
void mg_pfn_iobuf(char ch, void *param); // Print to iobuf
size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
@ -1356,6 +1356,28 @@ char *mg_json_get_b64(struct mg_str json, const char *path, int *len);
// JSON-RPC request descriptor
struct mg_rpc_req {
struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
mg_pfn_t pfn; // Response printing function
void *pfn_data; // Response printing function data
void *fn_data; // Endpoint handler data
};
void mg_rpc_add(void **head, struct mg_str method_pattern,
void (*handler)(struct mg_rpc_req *), void *handler_data);
void mg_rpc_free(void **head);
void mg_rpc_process(void **head, struct mg_str json, mg_pfn_t pfn, void *pfnd);
// Helper functions to print result or error frame
void mg_rpc_ok(struct mg_rpc_req *, const char *fmt, ...);
void mg_rpc_vok(struct mg_rpc_req *, const char *fmt, va_list *ap);
void mg_rpc_err(struct mg_rpc_req *, int code, const char *fmt, ...);
void mg_rpc_verr(struct mg_rpc_req *, int code, const char *fmt, va_list *);
void mg_rpc_list(struct mg_rpc_req *r);
struct mip_driver {

View File

@ -64,7 +64,7 @@ static void mg_putchar_iobuf_static(char ch, void *param) {
}
}
void mg_putchar_iobuf(char ch, void *param) {
void mg_pfn_iobuf(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len + 2 > io->size) mg_iobuf_resize(io, io->size + 64);
if (io->len + 2 <= io->size) {
@ -75,7 +75,7 @@ void mg_putchar_iobuf(char ch, void *param) {
// We don't use realloc() in mongoose, so resort to inefficient calloc
// Every new character reallocates the whole string
void mg_putchar_realloc(char ch, void *param) {
void mg_pfn_realloc(char ch, void *param) {
char *s, *buf = *(char **) param;
size_t len = buf == NULL ? 0 : strlen(buf);
if ((s = (char *) calloc(1, len + 2)) != NULL) {

View File

@ -119,9 +119,10 @@ int mg_json_get(const char *s, int len, const char *path, int *toklen) {
if (c == '"') {
int n = mg_pass_string(&s[i + 1], len - i - 1);
if (n < 0) return n;
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
// printf("K[%.*s] %d %d\n", n, &s[i + 1], depth, ed);
if (depth == ed && path[pos - 1] == '.' &&
memcmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
strncmp(&s[i + 1], &path[pos], (size_t) n) == 0) {
pos += n;
}
i += n + 1;

View File

@ -9,7 +9,7 @@ size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) {
size_t old = c->send.len;
va_list tmp;
va_copy(tmp, ap);
mg_vrprintf(mg_putchar_iobuf, &c->send, fmt, &tmp);
mg_vrprintf(mg_pfn_iobuf, &c->send, fmt, &tmp);
return c->send.len - old;
}

92
src/rpc.c Normal file
View File

@ -0,0 +1,92 @@
#include "rpc.h"
struct mg_rpc {
struct mg_rpc *next;
struct mg_str method;
void (*fn)(struct mg_rpc_req *);
void *fn_data;
};
void mg_rpc_add(void **head, struct mg_str method,
void (*fn)(struct mg_rpc_req *), void *fn_data) {
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data;
rpc->next = (struct mg_rpc *) *head, *head = rpc;
}
void mg_rpc_free(void **head) {
while (head != NULL && *head != NULL) {
struct mg_rpc *rpc = *(struct mg_rpc **) head;
*head = rpc->next;
free((void *) rpc->method.ptr);
free(rpc);
}
}
void mg_rpc_process(void **head, struct mg_str s, mg_pfn_t pfn, void *pfnd) {
struct mg_rpc_req req = {{s.ptr, s.len}, pfn, pfnd, NULL};
int len, off = mg_json_get(s.ptr, (int) s.len, "$.method", &len);
if (off > 0 && s.ptr[off] == '"') {
struct mg_str m = mg_str_n(&s.ptr[off + 1], (size_t) len - 2);
struct mg_rpc *h = *(struct mg_rpc **) head;
while (h != NULL && !mg_match(m, h->method, NULL)) h = h->next;
if (h != NULL) {
req.fn_data = h->fn_data;
h->fn(&req);
} else {
mg_rpc_err(&req, -32601, "\"%.*s not found\"", (int) m.len, m.ptr);
}
} else {
mg_rpc_err(&req, -32700, "%.*Q", (int) s.len, s.ptr);
}
}
void mg_rpc_vok(struct mg_rpc_req *r, const char *fmt, va_list *ap) {
int len, off = mg_json_get(r->frame.ptr, (int) r->frame.len, "$.id", &len);
if (off > 0) {
mg_rprintf(r->pfn, r->pfn_data, "{%Q:%.*s,%Q:", "id", len,
&r->frame.ptr[off], "result");
mg_vrprintf(r->pfn, r->pfn_data, fmt == NULL ? "null" : fmt, ap);
mg_rprintf(r->pfn, r->pfn_data, "}");
}
}
void mg_rpc_ok(struct mg_rpc_req *r, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
mg_rpc_vok(r, fmt, &ap);
va_end(ap);
}
void mg_rpc_verr(struct mg_rpc_req *r, int code, const char *fmt, va_list *ap) {
int len, off = mg_json_get(r->frame.ptr, (int) r->frame.len, "$.id", &len);
mg_rprintf(r->pfn, r->pfn_data, "{");
if (off > 0) {
mg_rprintf(r->pfn, r->pfn_data, "%Q:%.*s,", "id", len, &r->frame.ptr[off]);
}
mg_rprintf(r->pfn, r->pfn_data, "%Q:{%Q:%d,%Q:", "error", "code", code,
"message");
mg_vrprintf(r->pfn, r->pfn_data, fmt == NULL ? "null" : fmt, ap);
mg_rprintf(r->pfn, r->pfn_data, "}}");
}
void mg_rpc_err(struct mg_rpc_req *r, int code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
mg_rpc_verr(r, code, fmt, &ap);
va_end(ap);
}
static size_t print_methods(mg_pfn_t pfn, void *pfn_data, va_list *ap) {
struct mg_rpc *h, **head = (struct mg_rpc **) va_arg(*ap, void **);
size_t len = 0;
for (h = *head; h != NULL; h = h->next) {
len += mg_rprintf(pfn, pfn_data, "%s%.*Q", h == *head ? "" : ",",
(int) h->method.len, h->method.ptr);
}
return len;
}
void mg_rpc_list(struct mg_rpc_req *r) {
mg_rpc_ok(r, "[%M]", print_methods, r->fn_data);
}

22
src/rpc.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include "json.h"
// JSON-RPC request descriptor
struct mg_rpc_req {
struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
mg_pfn_t pfn; // Response printing function
void *pfn_data; // Response printing function data
void *fn_data; // Endpoint handler data
};
void mg_rpc_add(void **head, struct mg_str method_pattern,
void (*handler)(struct mg_rpc_req *), void *handler_data);
void mg_rpc_free(void **head);
void mg_rpc_process(void **head, struct mg_str json, mg_pfn_t pfn, void *pfnd);
// Helper functions to print result or error frame
void mg_rpc_ok(struct mg_rpc_req *, const char *fmt, ...);
void mg_rpc_vok(struct mg_rpc_req *, const char *fmt, va_list *ap);
void mg_rpc_err(struct mg_rpc_req *, int code, const char *fmt, ...);
void mg_rpc_verr(struct mg_rpc_req *, int code, const char *fmt, va_list *);
void mg_rpc_list(struct mg_rpc_req *r);

View File

@ -42,10 +42,10 @@ double mg_atod(const char *buf, int len, int *numlen);
size_t mg_dtoa(char *buf, size_t len, double d, int width);
char *mg_remove_double_dots(char *s);
typedef void (*mg_pc_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer
void mg_putchar_realloc(char ch, void *param); // Print to malloced str
void mg_putchar_iobuf(char ch, void *param); // Print to iobuf
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
void mg_pfn_realloc(char ch, void *param); // Print to malloced str
void mg_pfn_iobuf(char ch, void *param); // Print to iobuf
size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);

View File

@ -1504,8 +1504,8 @@ static void test_str(void) {
free(p);
p = mg_mprintf("[%M", pf2, 10);
mg_rprintf(mg_putchar_realloc, &p, ",");
mg_rprintf(mg_putchar_realloc, &p, "%d]", 7);
mg_rprintf(mg_pfn_realloc, &p, ",");
mg_rprintf(mg_pfn_realloc, &p, "%d]", 7);
printf("-> %s\n", p);
ASSERT(strcmp(p, "[9876543210,7]") == 0);
free(p);
@ -2422,12 +2422,61 @@ static void test_json(void) {
}
}
static void test_rpc(void) {
void *head = NULL;
mg_rpc_add(&head, mg_str("rpc.list"), mg_rpc_list, &head);
{
char *s = NULL;
const char *req = "{\"method\":\"rpc.list\"}";
mg_rpc_process(&head, mg_str(req), mg_pfn_realloc, &s);
ASSERT(s == NULL);
}
{
char *s = NULL;
const char *req = "{\"id\": 1,\"method\":\"rpc.list\"}";
const char *resp = "{\"id\":1,\"result\":[\"rpc.list\"]}";
mg_rpc_process(&head, mg_str(req), mg_pfn_realloc, &s);
MG_INFO(("-> %s", s));
ASSERT(strcmp(s, resp) == 0);
free(s);
}
{
char *s = NULL;
const char *req = "{\"id\": true,\"method\":\"foo\"}";
const char *resp =
"{\"id\":true,\"error\":{\"code\":-32601,\"message\":\"foo not "
"found\"}}";
mg_rpc_process(&head, mg_str(req), mg_pfn_realloc, &s);
MG_INFO(("-> %s", s));
ASSERT(strcmp(s, resp) == 0);
free(s);
}
{
char *s = NULL;
const char *req = "haha";
const char *resp = "{\"error\":{\"code\":-32700,\"message\":\"haha\"}}";
mg_rpc_process(&head, mg_str(req), mg_pfn_realloc, &s);
ASSERT(s != NULL);
MG_INFO(("-> %s", s));
ASSERT(strcmp(s, resp) == 0);
free(s);
}
mg_rpc_free(&head);
ASSERT(head == NULL);
}
int main(void) {
const char *debug_level = getenv("V");
if (debug_level == NULL) debug_level = "3";
mg_log_set(debug_level);
test_json();
test_rpc();
test_str();
test_globmatch();
test_get_header_var();