mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-24 02:59:01 +08:00
Introduce %Q format specifier, rework device dashboard
This commit is contained in:
parent
a83dd9569d
commit
cff208c5a9
@ -2509,6 +2509,8 @@ Print formatted string into a string buffer, just like `snprintf()`
|
||||
standard function does, but in a predictable way that does not depend on
|
||||
the C library or the build environment. The return value can be larger
|
||||
than the buffer length `len`, in which case the overflow bytes are not printed.
|
||||
Mongoose library is often used to exchange data in JSON format, therefore a
|
||||
non-standard `%Q` specifier for formatting JSON strings is also supported.
|
||||
|
||||
Parameters:
|
||||
- `buf` - Pointer to pointer to output buffer
|
||||
@ -2520,6 +2522,7 @@ Supported format specifiers:
|
||||
- `hhu`, `hu`, `u`, `lu`, `llu` - same but for unsigned variants
|
||||
- `hhx`, `hx`, `x`, `lx`, `llx` - same, unsigned and hex output
|
||||
- `s` - `for char *`
|
||||
- `Q` - `for char *`, outputs double-quoted JSON-escaped string (extension)
|
||||
- `c` - `for char`
|
||||
- `%` - `the `%` character itself
|
||||
- `p` - for any pointer, prints `0x.....` hex value
|
||||
|
@ -1,5 +1,5 @@
|
||||
PROG ?= example
|
||||
SOURCES ?= ../../mongoose.c main.c net.c mjson.c packed_fs.c
|
||||
SOURCES ?= ../../mongoose.c main.c net.c packed_fs.c
|
||||
CFLAGS ?= -I../.. -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_LINES=1 $(EXTRA)
|
||||
FILES_TO_EMBED ?= $(wildcard web_root/*)
|
||||
ROOT ?= $(realpath $(CURDIR)/../../..)
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,208 +0,0 @@
|
||||
// Copyright (c) 2018-2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#ifndef MJSON_H
|
||||
#define MJSON_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef MJSON_ENABLE_PRINT
|
||||
#define MJSON_ENABLE_PRINT 1
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_ENABLE_RPC
|
||||
#define MJSON_ENABLE_RPC 1
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_ENABLE_BASE64
|
||||
#define MJSON_ENABLE_BASE64 1
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_ENABLE_MERGE
|
||||
#define MJSON_ENABLE_MERGE 1
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_ENABLE_PRETTY
|
||||
#define MJSON_ENABLE_PRETTY 1
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_ENABLE_NEXT
|
||||
#define MJSON_ENABLE_NEXT 1
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_RPC_LIST_NAME
|
||||
#define MJSON_RPC_LIST_NAME "rpc.list"
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_DYNBUF_CHUNK
|
||||
#define MJSON_DYNBUF_CHUNK 256 // Allocation granularity for print_dynamic_buf
|
||||
#endif
|
||||
|
||||
#ifndef MJSON_REALLOC
|
||||
#define MJSON_REALLOC realloc
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MJSON_ERROR_INVALID_INPUT (-1)
|
||||
#define MJSON_ERROR_TOO_DEEP (-2)
|
||||
#define MJSON_TOK_INVALID 0
|
||||
#define MJSON_TOK_KEY 1
|
||||
#define MJSON_TOK_STRING 11
|
||||
#define MJSON_TOK_NUMBER 12
|
||||
#define MJSON_TOK_TRUE 13
|
||||
#define MJSON_TOK_FALSE 14
|
||||
#define MJSON_TOK_NULL 15
|
||||
#define MJSON_TOK_ARRAY 91
|
||||
#define MJSON_TOK_OBJECT 123
|
||||
#define MJSON_TOK_IS_VALUE(t) ((t) > 10 && (t) < 20)
|
||||
|
||||
typedef int (*mjson_cb_t)(int ev, const char *s, int off, int len, void *ud);
|
||||
|
||||
#ifndef MJSON_MAX_DEPTH
|
||||
#define MJSON_MAX_DEPTH 20
|
||||
#endif
|
||||
|
||||
int mjson(const char *s, int len, mjson_cb_t cb, void *ud);
|
||||
int mjson_find(const char *s, int len, const char *jp, const char **, int *);
|
||||
int mjson_get_number(const char *s, int len, const char *path, double *v);
|
||||
int mjson_get_bool(const char *s, int len, const char *path, int *v);
|
||||
int mjson_get_string(const char *s, int len, const char *path, char *to, int n);
|
||||
int mjson_get_hex(const char *s, int len, const char *path, char *to, int n);
|
||||
|
||||
#if MJSON_ENABLE_NEXT
|
||||
int mjson_next(const char *s, int n, int off, int *koff, int *klen, int *voff,
|
||||
int *vlen, int *vtype);
|
||||
#endif
|
||||
|
||||
#if MJSON_ENABLE_BASE64
|
||||
int mjson_get_base64(const char *s, int len, const char *path, char *to, int n);
|
||||
int mjson_base64_dec(const char *src, int n, char *dst, int dlen);
|
||||
#endif
|
||||
|
||||
#if MJSON_ENABLE_PRINT
|
||||
typedef int (*mjson_print_fn_t)(const char *buf, int len, void *userdata);
|
||||
typedef int (*mjson_vprint_fn_t)(mjson_print_fn_t, void *, va_list *);
|
||||
|
||||
struct mjson_fixedbuf {
|
||||
char *ptr;
|
||||
int size, len;
|
||||
};
|
||||
|
||||
int mjson_printf(mjson_print_fn_t, void *, const char *fmt, ...);
|
||||
int mjson_vprintf(mjson_print_fn_t, void *, const char *fmt, va_list *ap);
|
||||
int mjson_print_str(mjson_print_fn_t, void *, const char *s, int len);
|
||||
int mjson_print_int(mjson_print_fn_t, void *, int value, int is_signed);
|
||||
int mjson_print_long(mjson_print_fn_t, void *, long value, int is_signed);
|
||||
int mjson_print_buf(mjson_print_fn_t fn, void *, const char *buf, int len);
|
||||
int mjson_print_dbl(mjson_print_fn_t fn, void *, double, int width);
|
||||
|
||||
int mjson_print_null(const char *ptr, int len, void *userdata);
|
||||
int mjson_print_fixed_buf(const char *ptr, int len, void *userdata);
|
||||
int mjson_print_dynamic_buf(const char *ptr, int len, void *userdata);
|
||||
|
||||
int mjson_snprintf(char *buf, size_t len, const char *fmt, ...);
|
||||
char *mjson_aprintf(const char *fmt, ...);
|
||||
|
||||
#if MJSON_ENABLE_PRETTY
|
||||
int mjson_pretty(const char *, int, const char *, mjson_print_fn_t, void *);
|
||||
#endif
|
||||
|
||||
#if MJSON_ENABLE_MERGE
|
||||
int mjson_merge(const char *, int, const char *, int, mjson_print_fn_t, void *);
|
||||
#endif
|
||||
|
||||
#endif // MJSON_ENABLE_PRINT
|
||||
|
||||
#if MJSON_ENABLE_RPC
|
||||
|
||||
void jsonrpc_init(mjson_print_fn_t, void *userdata);
|
||||
int mjson_globmatch(const char *s1, int n1, const char *s2, int n2);
|
||||
|
||||
struct jsonrpc_request {
|
||||
struct jsonrpc_ctx *ctx;
|
||||
const char *frame; // Points to the whole frame
|
||||
int frame_len; // Frame length
|
||||
const char *params; // Points to the "params" in the request frame
|
||||
int params_len; // Length of the "params"
|
||||
const char *id; // Points to the "id" in the request frame
|
||||
int id_len; // Length of the "id"
|
||||
const char *method; // Points to the "method" in the request frame
|
||||
int method_len; // Length of the "method"
|
||||
mjson_print_fn_t fn; // Printer function
|
||||
void *fndata; // Printer function data
|
||||
void *userdata; // Callback's user data as specified at export time
|
||||
};
|
||||
|
||||
struct jsonrpc_method {
|
||||
const char *method;
|
||||
int method_sz;
|
||||
void (*cb)(struct jsonrpc_request *);
|
||||
struct jsonrpc_method *next;
|
||||
};
|
||||
|
||||
// Main RPC context, stores current request information and a list of
|
||||
// exported RPC methods.
|
||||
struct jsonrpc_ctx {
|
||||
struct jsonrpc_method *methods;
|
||||
mjson_print_fn_t response_cb;
|
||||
void *response_cb_data;
|
||||
};
|
||||
|
||||
// Registers function fn under the given name within the given RPC context
|
||||
#define jsonrpc_ctx_export(ctx, name, fn) \
|
||||
do { \
|
||||
static struct jsonrpc_method m = {(name), sizeof(name) - 1, (fn), 0}; \
|
||||
m.next = (ctx)->methods; \
|
||||
(ctx)->methods = &m; \
|
||||
} while (0)
|
||||
|
||||
void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t, void *);
|
||||
void jsonrpc_return_error(struct jsonrpc_request *r, int code,
|
||||
const char *message, const char *data_fmt, ...);
|
||||
void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
|
||||
...);
|
||||
void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
|
||||
mjson_print_fn_t fn, void *fndata, void *userdata);
|
||||
|
||||
extern struct jsonrpc_ctx jsonrpc_default_context;
|
||||
extern void jsonrpc_list(struct jsonrpc_request *r);
|
||||
|
||||
#define jsonrpc_export(name, fn) \
|
||||
jsonrpc_ctx_export(&jsonrpc_default_context, (name), (fn))
|
||||
|
||||
#define jsonrpc_process(buf, len, fn, fnd, ud) \
|
||||
jsonrpc_ctx_process(&jsonrpc_default_context, (buf), (len), (fn), (fnd), (ud))
|
||||
|
||||
#define JSONRPC_ERROR_INVALID -32700 /* Invalid JSON was received */
|
||||
#define JSONRPC_ERROR_NOT_FOUND -32601 /* The method does not exist */
|
||||
#define JSONRPC_ERROR_BAD_PARAMS -32602 /* Invalid params passed */
|
||||
#define JSONRPC_ERROR_INTERNAL -32603 /* Internal JSON-RPC error */
|
||||
|
||||
#endif // MJSON_ENABLE_RPC
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // MJSON_H
|
@ -1,7 +1,6 @@
|
||||
// Copyright (c) 2020-2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "mjson.h"
|
||||
#include "mongoose.h"
|
||||
|
||||
#define MQTT_SERVER "mqtt://broker.hivemq.com:1883"
|
||||
@ -67,20 +66,17 @@ static struct user *getuser(struct mg_http_message *hm) {
|
||||
static void send_notification(struct mg_mgr *mgr, const char *name,
|
||||
const char *data) {
|
||||
struct mg_connection *c;
|
||||
char *msg = mjson_aprintf("{%Q:%Q,%Q:%s}", "name", name, "data", data);
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] != 'W') continue;
|
||||
// c->is_hexdumping = 1;
|
||||
mg_ws_send(c, msg, strlen(msg), WEBSOCKET_OP_TEXT);
|
||||
if (strcmp(name, "metrics") != 0) MG_INFO(("%lu -> %s", c->id, msg));
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%Q:%Q,%Q:%s}", "name", name, "data",
|
||||
data);
|
||||
}
|
||||
free(msg);
|
||||
}
|
||||
|
||||
// Send simulated metrics data to the dashboard, for chart rendering
|
||||
static void timer_metrics_fn(void *param) {
|
||||
char buf[50];
|
||||
mg_snprintf(buf, sizeof(buf), "[ %lu, %d ]", (unsigned long) time(NULL),
|
||||
mg_snprintf(buf, sizeof(buf), "[%lu, %d]", (unsigned long) time(NULL),
|
||||
10 + (int) ((double) rand() * 10 / RAND_MAX));
|
||||
send_notification(param, "metrics", buf);
|
||||
}
|
||||
@ -94,11 +90,12 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
send_notification(c->mgr, "config", "null");
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
struct mg_mqtt_message *mm = ev_data;
|
||||
char *message = mjson_aprintf(
|
||||
"{%Q: %.*Q,%Q:%.*Q,%Q:%d}", "topic", (int) mm->topic.len, mm->topic.ptr,
|
||||
"data", (int) mm->data.len, mm->data.ptr, "qos", mm->qos);
|
||||
send_notification(c->mgr, "message", message);
|
||||
free(message);
|
||||
char buf[100], *p = buf;
|
||||
mg_asprintf(&p, sizeof(buf), "{%Q: %.*Q, %Q: %.*Q, %Q: %d}", "topic",
|
||||
(int) mm->topic.len, mm->topic.ptr, "data", (int) mm->data.len,
|
||||
mm->data.ptr, "qos", (int) mm->qos);
|
||||
send_notification(c->mgr, "message", p);
|
||||
if (p != buf) free(p);
|
||||
} else if (ev == MG_EV_MQTT_CMD) {
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_DEBUG(("cmd %d qos %d", mm->cmd, mm->qos));
|
||||
@ -141,11 +138,9 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
// All URIs starting with /api/ must be authenticated
|
||||
mg_printf(c, "%s", "HTTP/1.1 403 Denied\r\nContent-Length: 0\r\n\r\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||
char *response = mjson_aprintf("{%Q:%Q,%Q:%Q,%Q:%Q,%Q:%B}", "url",
|
||||
s_config.url, "pub", s_config.pub, "sub",
|
||||
s_config.sub, "connected", s_connected);
|
||||
mg_http_reply(c, 200, NULL, "%s\n", response);
|
||||
free(response);
|
||||
mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q,%Q:%Q,%Q:%s}\n", "url",
|
||||
s_config.url, "pub", s_config.pub, "sub", s_config.sub,
|
||||
"connected", s_connected ? "true" : "false");
|
||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||
// Admins only
|
||||
if (strcmp(u->name, "admin") == 0) {
|
||||
@ -170,10 +165,8 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
// mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||
char *response =
|
||||
mjson_aprintf("{%Q:%Q,%Q:%Q}", "user", u->name, "token", u->token);
|
||||
mg_http_reply(c, 200, NULL, "%s\n", response);
|
||||
free(response);
|
||||
mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q}\n", "user", u->name, "token",
|
||||
u->token);
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {0};
|
||||
#if 0
|
||||
|
@ -2786,7 +2786,7 @@ static const struct packed_file {
|
||||
time_t mtime;
|
||||
} packed_files[] = {
|
||||
{"/web_root/index.html", v1, sizeof(v1), 1654437619},
|
||||
{"/web_root/main.js", v2, sizeof(v2), 1654710571},
|
||||
{"/web_root/main.js", v2, sizeof(v2), 1654714271},
|
||||
{"/web_root/preact.min.js", v3, sizeof(v3), 1652374364},
|
||||
{"/web_root/style.css", v4, sizeof(v4), 1654709515},
|
||||
{"/web_root/user.png", v5, sizeof(v5), 1626172939},
|
||||
|
@ -2,7 +2,6 @@ idf_component_register(SRCS "main.c"
|
||||
"wifi.c"
|
||||
"net.c"
|
||||
"packed_fs.c"
|
||||
"mjson.c"
|
||||
"mongoose.c")
|
||||
component_compile_options(-DMG_ENABLE_LINES)
|
||||
component_compile_options(-DMG_ENABLE_PACKED_FS)
|
||||
|
@ -1 +0,0 @@
|
||||
../../../device-dashboard/mjson.c
|
@ -1 +0,0 @@
|
||||
../../../device-dashboard/mjson.h
|
44
mongoose.c
44
mongoose.c
@ -4823,10 +4823,10 @@ static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
|
||||
return ofs > n ? n : ofs;
|
||||
}
|
||||
|
||||
bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim) {
|
||||
bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char sep) {
|
||||
size_t koff = 0, klen = 0, voff = 0, vlen = 0, off = 0;
|
||||
if (s->ptr == NULL || s->len == 0) return 0;
|
||||
off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen, delim);
|
||||
off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen, sep);
|
||||
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
|
||||
if (v != NULL) *v = mg_str_n(s->ptr + voff, vlen);
|
||||
*s = mg_str_n(s->ptr + off, s->len - off);
|
||||
@ -4963,6 +4963,36 @@ static size_t mg_copys(char *buf, size_t len, size_t n, char *p, size_t k) {
|
||||
return j;
|
||||
}
|
||||
|
||||
static char mg_esc(int c, bool esc) {
|
||||
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
|
||||
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
|
||||
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char mg_escape(int c) {
|
||||
return mg_esc(c, true);
|
||||
}
|
||||
|
||||
static size_t mg_copyq(char *buf, size_t len, size_t n, char *p, size_t k) {
|
||||
size_t j = 0, extra = 2;
|
||||
if (n < len) buf[n++] = '"';
|
||||
for (j = 0; j < k && p[j]; j++) {
|
||||
char c = mg_escape(p[j]);
|
||||
if (c) {
|
||||
if (j + n < len) buf[n + j] = '\\';
|
||||
n++;
|
||||
extra++;
|
||||
if (j + n < len) buf[n + j] = c;
|
||||
} else {
|
||||
if (j + n < len) buf[n + j] = p[j];
|
||||
}
|
||||
}
|
||||
if (j + n < len) buf[n + j] = '"';
|
||||
return j + extra;
|
||||
}
|
||||
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
|
||||
size_t i = 0, n = 0;
|
||||
while (fmt[i] != '\0') {
|
||||
@ -5016,14 +5046,16 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
|
||||
int p = va_arg(ap, int);
|
||||
if (n < len) buf[n] = (char) p;
|
||||
n++;
|
||||
} else if (c == 's') {
|
||||
} else if (c == 's' || c == 'Q') {
|
||||
char *p = va_arg(ap, char *);
|
||||
size_t (*fn)(char *, size_t, size_t, char *, size_t) =
|
||||
c == 's' ? mg_copys : mg_copyq;
|
||||
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
||||
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
||||
n += mg_copys(buf, len, n, &pad, 1);
|
||||
n += mg_copys(buf, len, n, p, pr);
|
||||
n += fn(buf, len, n, &pad, 1);
|
||||
n += fn(buf, len, n, p, pr);
|
||||
for (j = 0; minus && pr < w && j + pr < w; j++)
|
||||
n += mg_copys(buf, len, n, &pad, 1);
|
||||
n += fn(buf, len, n, &pad, 1);
|
||||
} else if (c == '%') {
|
||||
if (n < len) buf[n] = '%';
|
||||
n++;
|
||||
|
44
src/str.c
44
src/str.c
@ -146,10 +146,10 @@ static size_t mg_nce(const char *s, size_t n, size_t ofs, size_t *koff,
|
||||
return ofs > n ? n : ofs;
|
||||
}
|
||||
|
||||
bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim) {
|
||||
bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char sep) {
|
||||
size_t koff = 0, klen = 0, voff = 0, vlen = 0, off = 0;
|
||||
if (s->ptr == NULL || s->len == 0) return 0;
|
||||
off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen, delim);
|
||||
off = mg_nce(s->ptr, s->len, 0, &koff, &klen, &voff, &vlen, sep);
|
||||
if (k != NULL) *k = mg_str_n(s->ptr + koff, klen);
|
||||
if (v != NULL) *v = mg_str_n(s->ptr + voff, vlen);
|
||||
*s = mg_str_n(s->ptr + off, s->len - off);
|
||||
@ -286,6 +286,36 @@ static size_t mg_copys(char *buf, size_t len, size_t n, char *p, size_t k) {
|
||||
return j;
|
||||
}
|
||||
|
||||
static char mg_esc(int c, bool esc) {
|
||||
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
|
||||
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
|
||||
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char mg_escape(int c) {
|
||||
return mg_esc(c, true);
|
||||
}
|
||||
|
||||
static size_t mg_copyq(char *buf, size_t len, size_t n, char *p, size_t k) {
|
||||
size_t j = 0, extra = 2;
|
||||
if (n < len) buf[n++] = '"';
|
||||
for (j = 0; j < k && p[j]; j++) {
|
||||
char c = mg_escape(p[j]);
|
||||
if (c) {
|
||||
if (j + n < len) buf[n + j] = '\\';
|
||||
n++;
|
||||
extra++;
|
||||
if (j + n < len) buf[n + j] = c;
|
||||
} else {
|
||||
if (j + n < len) buf[n + j] = p[j];
|
||||
}
|
||||
}
|
||||
if (j + n < len) buf[n + j] = '"';
|
||||
return j + extra;
|
||||
}
|
||||
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
|
||||
size_t i = 0, n = 0;
|
||||
while (fmt[i] != '\0') {
|
||||
@ -339,14 +369,16 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
|
||||
int p = va_arg(ap, int);
|
||||
if (n < len) buf[n] = (char) p;
|
||||
n++;
|
||||
} else if (c == 's') {
|
||||
} else if (c == 's' || c == 'Q') {
|
||||
char *p = va_arg(ap, char *);
|
||||
size_t (*fn)(char *, size_t, size_t, char *, size_t) =
|
||||
c == 's' ? mg_copys : mg_copyq;
|
||||
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
||||
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
||||
n += mg_copys(buf, len, n, &pad, 1);
|
||||
n += mg_copys(buf, len, n, p, pr);
|
||||
n += fn(buf, len, n, &pad, 1);
|
||||
n += fn(buf, len, n, p, pr);
|
||||
for (j = 0; minus && pr < w && j + pr < w; j++)
|
||||
n += mg_copys(buf, len, n, &pad, 1);
|
||||
n += fn(buf, len, n, &pad, 1);
|
||||
} else if (c == '%') {
|
||||
if (n < len) buf[n] = '%';
|
||||
n++;
|
||||
|
@ -1366,9 +1366,22 @@ static bool sn(const char *fmt, ...) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool sn2(const char *expect, const char *fmt, ...) {
|
||||
char buf[100];
|
||||
va_list ap;
|
||||
bool result;
|
||||
va_start(ap, fmt);
|
||||
mg_vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
va_start(ap, fmt);
|
||||
result = strcmp(expect, buf) == 0;
|
||||
if (!result) MG_ERROR(("[%s] != [%s]", expect, buf));
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool sccmp(const char *s1, const char *s2, int expected) {
|
||||
int n1 = mg_casecmp(s1, s2);
|
||||
MG_INFO(("[%s] [%s] %d %d", s1, s2, n1, expected));
|
||||
// MG_INFO(("[%s] [%s] %d %d", s1, s2, n1, expected));
|
||||
return n1 == expected;
|
||||
}
|
||||
|
||||
@ -1432,6 +1445,28 @@ static void test_str(void) {
|
||||
ASSERT(sn("%s ", "a"));
|
||||
ASSERT(sn("%s %s", "a", "b"));
|
||||
ASSERT(sn("%2s %s", "a", "b"));
|
||||
|
||||
// Non-standard formatting
|
||||
{
|
||||
char buf[100];
|
||||
const char *expected;
|
||||
|
||||
expected = "\"\"";
|
||||
mg_snprintf(buf, sizeof(buf), "%Q", "");
|
||||
ASSERT(strcmp(buf, expected) == 0);
|
||||
|
||||
expected = "\"a'b\"";
|
||||
mg_snprintf(buf, sizeof(buf), "%Q", "a'b");
|
||||
ASSERT(strcmp(buf, expected) == 0);
|
||||
|
||||
expected = "\"a\\b\\n\\f\\r\\t\\\"\"";
|
||||
mg_snprintf(buf, sizeof(buf), "%Q", "a\b\n\f\r\t\"");
|
||||
ASSERT(strcmp(buf, expected) == 0);
|
||||
|
||||
expected = "\"abc\"";
|
||||
mg_snprintf(buf, sizeof(buf), "%.*Q", 3, "abcdef");
|
||||
ASSERT(strcmp(buf, expected) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void fn1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
@ -1807,8 +1842,7 @@ struct stream_status {
|
||||
// Consume recv buffer after letting it reach MG_MAX_RECV_SIZE
|
||||
static void eh8(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct stream_status *status = (struct stream_status *) fn_data;
|
||||
if (c->is_listening)
|
||||
return;
|
||||
if (c->is_listening) return;
|
||||
|
||||
ASSERT(c->recv.len <= MG_MAX_RECV_SIZE);
|
||||
|
||||
@ -1832,29 +1866,29 @@ static void eh8(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
else
|
||||
consume = MG_MAX_RECV_SIZE / 3;
|
||||
status->received += consume;
|
||||
status->recv_crc = mg_crc32(status->recv_crc, (const char *) c->recv.buf, consume);
|
||||
status->recv_crc =
|
||||
mg_crc32(status->recv_crc, (const char *) c->recv.buf, consume);
|
||||
mg_iobuf_del(&c->recv, 0, consume);
|
||||
}
|
||||
|
||||
// count polls with full buffer to ensure c->is_full prevents reads
|
||||
if (ev == MG_EV_POLL && c->recv.len == MG_MAX_RECV_SIZE)
|
||||
status->polls += 1;
|
||||
if (ev == MG_EV_POLL && c->recv.len == MG_MAX_RECV_SIZE) status->polls += 1;
|
||||
(void) ev_data;
|
||||
}
|
||||
|
||||
// Toggle c->is_full to prevent max_recv_buf_size reached read errors
|
||||
static void eh10(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (c->recv.len >= MG_MAX_RECV_SIZE && ev == MG_EV_READ)
|
||||
c->is_full = true;
|
||||
static void eh10(struct mg_connection *c, int ev, void *ev_data,
|
||||
void *fn_data) {
|
||||
if (c->recv.len >= MG_MAX_RECV_SIZE && ev == MG_EV_READ) c->is_full = true;
|
||||
|
||||
eh8(c, ev, ev_data, fn_data);
|
||||
|
||||
if (c->recv.len < MG_MAX_RECV_SIZE && ev == MG_EV_POLL)
|
||||
c->is_full = false;
|
||||
if (c->recv.len < MG_MAX_RECV_SIZE && ev == MG_EV_POLL) c->is_full = false;
|
||||
}
|
||||
|
||||
// Send buffer larger than MG_MAX_RECV_SIZE to server
|
||||
static void eh11(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
static void eh11(struct mg_connection *c, int ev, void *ev_data,
|
||||
void *fn_data) {
|
||||
struct stream_status *status = (struct stream_status *) fn_data;
|
||||
if (ev == MG_EV_CONNECT) {
|
||||
size_t len = MG_MAX_RECV_SIZE * 2;
|
||||
|
Loading…
Reference in New Issue
Block a user