Ditch JSON-RPC from mongoose

PUBLISHED_FROM=89b978c02be2f10eb930ff13673d45249fd67763
This commit is contained in:
Sergey Lyubka authored and Cesanta Bot committed 2016-07-01 11:35:18 +00:00
1 parent 421b420a0c
commit b7a0748312
30 files changed
+14 -2323

No files matched your search

-758
View File
@@ -610,603 +610,6 @@ double cs_time() {
return now;
}
#ifdef MG_MODULE_LINES
#line 1 "./src/../deps/frozen/frozen.c"
#endif
/*
* Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
* Copyright (c) 2013 Cesanta Software Limited
* All rights reserved
*
* This library is dual-licensed: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http: *www.gnu.org/licenses/>.
*
* You are free to use this library under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* Alternatively, you can license this library under a commercial
* license, as set out in <http://cesanta.com/products.html>.
*/
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005+ */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
/* Amalgamated: #include "frozen.h" */
#ifdef _WIN32
#define snprintf _snprintf
#endif
#ifndef FROZEN_REALLOC
#define FROZEN_REALLOC realloc
#endif
#ifndef FROZEN_FREE
#define FROZEN_FREE free
#endif
struct frozen {
const char *end;
const char *cur;
struct json_token *tokens;
int max_tokens;
int num_tokens;
int do_realloc;
};
static int parse_object(struct frozen *f);
static int parse_value(struct frozen *f);
#define EXPECT(cond, err_code) \
do { \
if (!(cond)) return (err_code); \
} while (0)
#define TRY(expr) \
do { \
int _n = expr; \
if (_n < 0) return _n; \
} while (0)
#define END_OF_STRING (-1)
static int left(const struct frozen *f) {
return f->end - f->cur;
}
static int is_space(int ch) {
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
}
static void skip_whitespaces(struct frozen *f) {
while (f->cur < f->end && is_space(*f->cur)) f->cur++;
}
static int cur(struct frozen *f) {
skip_whitespaces(f);
return f->cur >= f->end ? END_OF_STRING : *(unsigned char *) f->cur;
}
static int test_and_skip(struct frozen *f, int expected) {
int ch = cur(f);
if (ch == expected) {
f->cur++;
return 0;
}
return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
}
static int test_no_skip(struct frozen *f, int expected) {
int ch = cur(f);
if (ch == expected) {
return 0;
}
return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
}
static int is_alpha(int ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
static int is_digit(int ch) {
return ch >= '0' && ch <= '9';
}
static int is_hex_digit(int ch) {
return is_digit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
}
static int get_escape_len(const char *s, int len) {
switch (*s) {
case 'u':
return len < 6 ? JSON_STRING_INCOMPLETE
: is_hex_digit(s[1]) && is_hex_digit(s[2]) &&
is_hex_digit(s[3]) && is_hex_digit(s[4])
? 5
: JSON_STRING_INVALID;
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
return len < 2 ? JSON_STRING_INCOMPLETE : 1;
default:
return JSON_STRING_INVALID;
}
}
static int capture_ptr(struct frozen *f, const char *ptr, enum json_type type) {
if (f->do_realloc && f->num_tokens >= f->max_tokens) {
int new_size = f->max_tokens == 0 ? 100 : f->max_tokens * 2;
void *p = FROZEN_REALLOC(f->tokens, new_size * sizeof(f->tokens[0]));
if (p == NULL) return JSON_TOKEN_ARRAY_TOO_SMALL;
f->max_tokens = new_size;
f->tokens = (struct json_token *) p;
}
if (f->tokens == NULL || f->max_tokens == 0) return 0;
if (f->num_tokens >= f->max_tokens) return JSON_TOKEN_ARRAY_TOO_SMALL;
f->tokens[f->num_tokens].ptr = ptr;
f->tokens[f->num_tokens].type = type;
f->num_tokens++;
return 0;
}
static int capture_len(struct frozen *f, int token_index, const char *ptr) {
if (f->tokens == 0 || f->max_tokens == 0) return 0;
EXPECT(token_index >= 0 && token_index < f->max_tokens, JSON_STRING_INVALID);
f->tokens[token_index].len = ptr - f->tokens[token_index].ptr;
f->tokens[token_index].num_desc = (f->num_tokens - 1) - token_index;
return 0;
}
/* identifier = letter { letter | digit | '_' } */
static int parse_identifier(struct frozen *f) {
EXPECT(is_alpha(cur(f)), JSON_STRING_INVALID);
TRY(capture_ptr(f, f->cur, JSON_TYPE_STRING));
while (f->cur < f->end &&
(*f->cur == '_' || is_alpha(*f->cur) || is_digit(*f->cur))) {
f->cur++;
}
capture_len(f, f->num_tokens - 1, f->cur);
return 0;
}
static int get_utf8_char_len(unsigned char ch) {
if ((ch & 0x80) == 0) return 1;
switch (ch & 0xf0) {
case 0xf0:
return 4;
case 0xe0:
return 3;
default:
return 2;
}
}
/* string = '"' { quoted_printable_chars } '"' */
static int parse_string(struct frozen *f) {
int n, ch = 0, len = 0;
TRY(test_and_skip(f, '"'));
TRY(capture_ptr(f, f->cur, JSON_TYPE_STRING));
for (; f->cur < f->end; f->cur += len) {
ch = *(unsigned char *) f->cur;
len = get_utf8_char_len((unsigned char) ch);
EXPECT(ch >= 32 && len > 0, JSON_STRING_INVALID); /* No control chars */
EXPECT(len < left(f), JSON_STRING_INCOMPLETE);
if (ch == '\\') {
EXPECT((n = get_escape_len(f->cur + 1, left(f))) > 0, n);
len += n;
} else if (ch == '"') {
capture_len(f, f->num_tokens - 1, f->cur);
f->cur++;
break;
};
}
return ch == '"' ? 0 : JSON_STRING_INCOMPLETE;
}
/* number = [ '-' ] digit+ [ '.' digit+ ] [ ['e'|'E'] ['+'|'-'] digit+ ] */
static int parse_number(struct frozen *f) {
int ch = cur(f);
TRY(capture_ptr(f, f->cur, JSON_TYPE_NUMBER));
if (ch == '-') f->cur++;
EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID);
while (f->cur < f->end && is_digit(f->cur[0])) f->cur++;
if (f->cur < f->end && f->cur[0] == '.') {
f->cur++;
EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID);
while (f->cur < f->end && is_digit(f->cur[0])) f->cur++;
}
if (f->cur < f->end && (f->cur[0] == 'e' || f->cur[0] == 'E')) {
f->cur++;
EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
if ((f->cur[0] == '+' || f->cur[0] == '-')) f->cur++;
EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID);
while (f->cur < f->end && is_digit(f->cur[0])) f->cur++;
}
capture_len(f, f->num_tokens - 1, f->cur);
return 0;
}
/* array = '[' [ value { ',' value } ] ']' */
static int parse_array(struct frozen *f) {
int ind;
TRY(test_and_skip(f, '['));
TRY(capture_ptr(f, f->cur - 1, JSON_TYPE_ARRAY));
ind = f->num_tokens - 1;
while (cur(f) != ']') {
TRY(parse_value(f));
if (cur(f) == ',') f->cur++;
}
TRY(test_and_skip(f, ']'));
capture_len(f, ind, f->cur);
return 0;
}
static int compare(const char *s, const char *str, int len) {
int i = 0;
while (i < len && s[i] == str[i]) i++;
return i == len ? 1 : 0;
}
static int expect(struct frozen *f, const char *s, int len, enum json_type t) {
int i, n = left(f);
TRY(capture_ptr(f, f->cur, t));
for (i = 0; i < len; i++) {
if (i >= n) return JSON_STRING_INCOMPLETE;
if (f->cur[i] != s[i]) return JSON_STRING_INVALID;
}
f->cur += len;
TRY(capture_len(f, f->num_tokens - 1, f->cur));
return 0;
}
/* value = 'null' | 'true' | 'false' | number | string | array | object */
static int parse_value(struct frozen *f) {
int ch = cur(f);
switch (ch) {
case '"':
TRY(parse_string(f));
break;
case '{':
TRY(parse_object(f));
break;
case '[':
TRY(parse_array(f));
break;
case 'n':
TRY(expect(f, "null", 4, JSON_TYPE_NULL));
break;
case 't':
TRY(expect(f, "true", 4, JSON_TYPE_TRUE));
break;
case 'f':
TRY(expect(f, "false", 5, JSON_TYPE_FALSE));
break;
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
TRY(parse_number(f));
break;
default:
return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
}
return 0;
}
/* key = identifier | string */
static int parse_key(struct frozen *f) {
int ch = cur(f);
#if 0
printf("%s 1 [%.*s]\n", __func__, (int) (f->end - f->cur), f->cur);
#endif
if (is_alpha(ch)) {
TRY(parse_identifier(f));
} else if (ch == '"') {
TRY(parse_string(f));
} else {
return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
}
return 0;
}
/* pair = key ':' value */
static int parse_pair(struct frozen *f) {
TRY(parse_key(f));
TRY(test_and_skip(f, ':'));
TRY(parse_value(f));
return 0;
}
/* object = '{' pair { ',' pair } '}' */
static int parse_object(struct frozen *f) {
int ind;
TRY(test_and_skip(f, '{'));
TRY(capture_ptr(f, f->cur - 1, JSON_TYPE_OBJECT));
ind = f->num_tokens - 1;
while (cur(f) != '}') {
TRY(parse_pair(f));
if (cur(f) == ',') f->cur++;
}
TRY(test_and_skip(f, '}'));
capture_len(f, ind, f->cur);
return 0;
}
static int doit(struct frozen *f) {
int ret = 0;
if (f->cur == 0 || f->end < f->cur) return JSON_STRING_INVALID;
if (f->end == f->cur) return JSON_STRING_INCOMPLETE;
if (0 == (ret = test_no_skip(f, '{'))) {
TRY(parse_object(f));
} else if (0 == (ret = test_no_skip(f, '['))) {
TRY(parse_array(f));
} else {
return ret;
}
TRY(capture_ptr(f, f->cur, JSON_TYPE_EOF));
capture_len(f, f->num_tokens, f->cur);
return 0;
}
/* json = object */
int parse_json(const char *s, int s_len, struct json_token *arr, int arr_len) {
struct frozen frozen;
memset(&frozen, 0, sizeof(frozen));
frozen.end = s + s_len;
frozen.cur = s;
frozen.tokens = arr;
frozen.max_tokens = arr_len;
TRY(doit(&frozen));
return frozen.cur - s;
}
struct json_token *parse_json2(const char *s, int s_len) {
struct frozen frozen;
memset(&frozen, 0, sizeof(frozen));
frozen.end = s + s_len;
frozen.cur = s;
frozen.do_realloc = 1;
if (doit(&frozen) < 0) {
FROZEN_FREE((void *) frozen.tokens);
frozen.tokens = NULL;
}
return frozen.tokens;
}
static int path_part_len(const char *p) {
int i = 0;
while (p[i] != '\0' && p[i] != '[' && p[i] != '.') i++;
return i;
}
struct json_token *find_json_token(struct json_token *toks, const char *path) {
while (path != 0 && path[0] != '\0') {
int i, ind2 = 0, ind = -1, skip = 2, n = path_part_len(path);
if (path[0] == '[') {
if (toks->type != JSON_TYPE_ARRAY || !is_digit(path[1])) return 0;
for (ind = 0, n = 1; path[n] != ']' && path[n] != '\0'; n++) {
if (!is_digit(path[n])) return 0;
ind *= 10;
ind += path[n] - '0';
}
if (path[n++] != ']') return 0;
skip = 1; /* In objects, we skip 2 elems while iterating, in arrays 1. */
} else if (toks->type != JSON_TYPE_OBJECT)
return 0;
toks++;
for (i = 0; i < toks[-1].num_desc; i += skip, ind2++) {
/* ind == -1 indicated that we're iterating an array, not object */
if (ind == -1 && toks[i].type != JSON_TYPE_STRING) return 0;
if (ind2 == ind ||
(ind == -1 && toks[i].len == n && compare(path, toks[i].ptr, n))) {
i += skip - 1;
break;
};
if (toks[i - 1 + skip].type == JSON_TYPE_ARRAY ||
toks[i - 1 + skip].type == JSON_TYPE_OBJECT) {
i += toks[i - 1 + skip].num_desc;
}
}
if (i == toks[-1].num_desc) return 0;
path += n;
if (path[0] == '.') path++;
if (path[0] == '\0') return &toks[i];
toks += i;
}
return 0;
}
int json_emit_long(char *buf, int buf_len, long int value) {
char tmp[20];
int n = snprintf(tmp, sizeof(tmp), "%ld", value);
strncpy(buf, tmp, buf_len > 0 ? buf_len : 0);
return n;
}
int json_emit_double(char *buf, int buf_len, double value) {
char tmp[20];
int n = snprintf(tmp, sizeof(tmp), "%g", value);
strncpy(buf, tmp, buf_len > 0 ? buf_len : 0);
return n;
}
int json_emit_quoted_str(char *s, int s_len, const char *str, int len) {
const char *begin = s, *end = s + s_len, *str_end = str + len;
char ch;
#define EMIT(x) \
do { \
if (s < end) *s = x; \
s++; \
} while (0)
EMIT('"');
while (str < str_end) {
ch = *str++;
switch (ch) {
case '"':
EMIT('\\');
EMIT('"');
break;
case '\\':
EMIT('\\');
EMIT('\\');
break;
case '\b':
EMIT('\\');
EMIT('b');
break;
case '\f':
EMIT('\\');
EMIT('f');
break;
case '\n':
EMIT('\\');
EMIT('n');
break;
case '\r':
EMIT('\\');
EMIT('r');
break;
case '\t':
EMIT('\\');
EMIT('t');
break;
default:
EMIT(ch);
}
}
EMIT('"');
if (s < end) {
*s = '\0';
}
return s - begin;
}
int json_emit_unquoted_str(char *buf, int buf_len, const char *str, int len) {
if (buf_len > 0 && len > 0) {
int n = len < buf_len ? len : buf_len;
memcpy(buf, str, n);
if (n < buf_len) {
buf[n] = '\0';
}
}
return len;
}
int json_emit_va(char *s, int s_len, const char *fmt, va_list ap) {
const char *end = s + s_len, *str, *orig = s;
size_t len;
while (*fmt != '\0') {
switch (*fmt) {
case '[':
case ']':
case '{':
case '}':
case ',':
case ':':
case ' ':
case '\r':
case '\n':
case '\t':
if (s < end) {
*s = *fmt;
}
s++;
break;
case 'i':
s += json_emit_long(s, end - s, va_arg(ap, long) );
break;
case 'f':
s += json_emit_double(s, end - s, va_arg(ap, double) );
break;
case 'v':
str = va_arg(ap, char *);
len = va_arg(ap, size_t);
s += json_emit_quoted_str(s, end - s, str, len);
break;
case 'V':
str = va_arg(ap, char *);
len = va_arg(ap, size_t);
s += json_emit_unquoted_str(s, end - s, str, len);
break;
case 's':
str = va_arg(ap, char *);
s += json_emit_quoted_str(s, end - s, str, strlen(str));
break;
case 'S':
str = va_arg(ap, char *);
s += json_emit_unquoted_str(s, end - s, str, strlen(str));
break;
case 'T':
s += json_emit_unquoted_str(s, end - s, "true", 4);
break;
case 'F':
s += json_emit_unquoted_str(s, end - s, "false", 5);
break;
case 'N':
s += json_emit_unquoted_str(s, end - s, "null", 4);
break;
default:
return 0;
}
fmt++;
}
/* Best-effort to 0-terminate generated string */
if (s < end) {
*s = '\0';
}
return s - orig;
}
int json_emit(char *buf, int buf_len, const char *fmt, ...) {
int len;
va_list ap;
va_start(ap, fmt);
len = json_emit_va(buf, buf_len, fmt, ap);
va_end(ap);
return len;
}
#ifdef MG_MODULE_LINES
#line 1 "./src/../../common/md5.c"
#endif
/*
@@ -8245,167 +7648,6 @@ struct mg_str mg_mk_str(const char *s) {
return ret;
}
#ifdef MG_MODULE_LINES
#line 1 "./src/json-rpc.c"
#endif
/* Copyright (c) 2014 Cesanta Software Limited */
/* All rights reserved */
#ifndef MG_DISABLE_JSON_RPC
/* Amalgamated: #include "mongoose/src/internal.h" */
/* Amalgamated: #include "mongoose/src/json-rpc.h" */
/* Amalgamated: #include "mongoose/deps/frozen/frozen.h" */
int mg_rpc_create_reply(char *buf, int len, const struct mg_rpc_request *req,
const char *result_fmt, ...) {
static const struct json_token null_tok = {"null", 4, 0, JSON_TYPE_NULL};
const struct json_token *id = req->id == NULL ? &null_tok : req->id;
va_list ap;
int n = 0;
n += json_emit(buf + n, len - n, "{s:s,s:", "jsonrpc", "2.0", "id");
if (id->type == JSON_TYPE_STRING) {
n += json_emit_quoted_str(buf + n, len - n, id->ptr, id->len);
} else {
n += json_emit_unquoted_str(buf + n, len - n, id->ptr, id->len);
}
n += json_emit(buf + n, len - n, ",s:", "result");
va_start(ap, result_fmt);
n += json_emit_va(buf + n, len - n, result_fmt, ap);
va_end(ap);
n += json_emit(buf + n, len - n, "}");
return n;
}
int mg_rpc_create_request(char *buf, int len, const char *method,
const char *id, const char *params_fmt, ...) {
va_list ap;
int n = 0;
n += json_emit(buf + n, len - n, "{s:s,s:s,s:s,s:", "jsonrpc", "2.0", "id",
id, "method", method, "params");
va_start(ap, params_fmt);
n += json_emit_va(buf + n, len - n, params_fmt, ap);
va_end(ap);
n += json_emit(buf + n, len - n, "}");
return n;
}
int mg_rpc_create_error(char *buf, int len, struct mg_rpc_request *req,
int code, const char *message, const char *fmt, ...) {
va_list ap;
int n = 0;
n += json_emit(buf + n, len - n, "{s:s,s:V,s:{s:i,s:s,s:", "jsonrpc", "2.0",
"id", req->id == NULL ? "null" : req->id->ptr,
req->id == NULL ? 4 : req->id->len, "error", "code",
(long) code, "message", message, "data");
va_start(ap, fmt);
n += json_emit_va(buf + n, len - n, fmt, ap);
va_end(ap);
n += json_emit(buf + n, len - n, "}}");
return n;
}
int mg_rpc_create_std_error(char *buf, int len, struct mg_rpc_request *req,
int code) {
const char *message = NULL;
switch (code) {
case JSON_RPC_PARSE_ERROR:
message = "parse error";
break;
case JSON_RPC_INVALID_REQUEST_ERROR:
message = "invalid request";
break;
case JSON_RPC_METHOD_NOT_FOUND_ERROR:
message = "method not found";
break;
case JSON_RPC_INVALID_PARAMS_ERROR:
message = "invalid parameters";
break;
case JSON_RPC_SERVER_ERROR:
message = "server error";
break;
default:
message = "unspecified error";
break;
}
return mg_rpc_create_error(buf, len, req, code, message, "N");
}
int mg_rpc_dispatch(const char *buf, int len, char *dst, int dst_len,
const char **methods, mg_rpc_handler_t *handlers) {
struct json_token tokens[200];
struct mg_rpc_request req;
int i, n;
memset(&req, 0, sizeof(req));
n = parse_json(buf, len, tokens, sizeof(tokens) / sizeof(tokens[0]));
if (n <= 0) {
int err_code = (n == JSON_STRING_INVALID) ? JSON_RPC_PARSE_ERROR
: JSON_RPC_SERVER_ERROR;
return mg_rpc_create_std_error(dst, dst_len, &req, err_code);
}
req.message = tokens;
req.id = find_json_token(tokens, "id");
req.method = find_json_token(tokens, "method");
req.params = find_json_token(tokens, "params");
if (req.id == NULL || req.method == NULL) {
return mg_rpc_create_std_error(dst, dst_len, &req,
JSON_RPC_INVALID_REQUEST_ERROR);
}
for (i = 0; methods[i] != NULL; i++) {
int mlen = strlen(methods[i]);
if (mlen == req.method->len &&
memcmp(methods[i], req.method->ptr, mlen) == 0)
break;
}
if (methods[i] == NULL) {
return mg_rpc_create_std_error(dst, dst_len, &req,
JSON_RPC_METHOD_NOT_FOUND_ERROR);
}
return handlers[i](dst, dst_len, &req);
}
int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks,
int max_toks, struct mg_rpc_reply *rep,
struct mg_rpc_error *er) {
int n = parse_json(buf, len, toks, max_toks);
memset(rep, 0, sizeof(*rep));
memset(er, 0, sizeof(*er));
if (n > 0) {
if ((rep->result = find_json_token(toks, "result")) != NULL) {
rep->message = toks;
rep->id = find_json_token(toks, "id");
} else {
er->message = toks;
er->id = find_json_token(toks, "id");
er->error_code = find_json_token(toks, "error.code");
er->error_message = find_json_token(toks, "error.message");
er->error_data = find_json_token(toks, "error.data");
}
}
return n;
}
#endif /* MG_DISABLE_JSON_RPC */
#ifdef MG_MODULE_LINES
#line 1 "./src/mqtt.c"
#endif
/*