summaryrefslogtreecommitdiff
path: root/src/resp_parser.c
diff options
context:
space:
mode:
authorMeir Shpilraien (Spielrein) <meir@redislabs.com>2021-08-04 16:28:07 +0300
committerGitHub <noreply@github.com>2021-08-04 16:28:07 +0300
commit2237131e15c84689f2cd990455111e222f5164f6 (patch)
tree1667772ffece65bc68936fb0d9ea7630243ecd45 /src/resp_parser.c
parentb4eda14257841f7fdda3b70a8d352c0100a988b7 (diff)
downloadredis-2237131e15c84689f2cd990455111e222f5164f6.tar.gz
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state 1. Lua has its own parser that handles parsing `reds.call` replies and translates them to Lua objects that can be used by the user Lua code. The parser partially handles resp3 (missing big number, verbatim, attribute, ...) 2. Modules have their own parser that handles parsing `RM_Call` replies and translates them to RedisModuleCallReply objects. The parser does not support resp3. In addition, in the future, we want to add Redis Function (#8693) that will probably support more languages. At some point maintaining so many parsers will stop scaling (bug fixes and protocol changes will need to be applied on all of them). We will probably end up with different parsers that support different parts of the resp protocol (like we already have today with Lua and modules) ## PR Changes This PR attempt to unified the reply parsing of Lua and modules (and in the future Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser handles parsing the reply and calls different callbacks to allow the users (another unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply. ### Lua API Additions The code that handles reply parsing on `scripting.c` was removed. Instead, it uses the resp_parser to parse and create a Lua object out of the reply. As mentioned above the Lua parser did not handle parsing big numbers, verbatim, and attribute. The new parser can handle those and so Lua also gets it for free. Those are translated to Lua objects in the following way: 1. Big Number - Lua table `{'big_number':'<str representation for big number>'}` 2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}` 3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it. Tests were added to check resp3 reply parsing on Lua ### Modules API Additions The reply parsing code on `module.c` was also removed and the new resp_parser is used instead. In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c` (in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis will automatically chose the reply protocol base on the current client set on the RedisModuleCtx (this mode will mostly be used when the module want to pass the reply to the client as is). In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies: * New RedisModuleCallReply types: * `REDISMODULE_REPLY_MAP` * `REDISMODULE_REPLY_SET` * `REDISMODULE_REPLY_BOOL` * `REDISMODULE_REPLY_DOUBLE` * `REDISMODULE_REPLY_BIG_NUMBER` * `REDISMODULE_REPLY_VERBATIM_STRING` * `REDISMODULE_REPLY_ATTRIBUTE` * New RedisModuleAPI: * `RedisModule_CallReplyDouble` - getting double value from resp3 double reply * `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply * `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply * `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply * `RedisModule_CallReplySetElement` - getting element from resp3 set reply * `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply * `RedisModule_CallReplyAttribute` - getting a reply attribute * `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply * New context flags: * `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3 Tests were added to check the new RedisModuleAPI ### Modules API Changes * RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3 but the client expects resp2. This is not a breaking change because in order to get a resp3 CallReply one needs to specifically specify `3` as a parameter to the fmt argument of `RM_Call` (as mentioned above). Tests were added to check this change ### More small Additions * Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol` and check the resp3 parsing code. Co-authored-by: Oran Agra <oran@redislabs.com> Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
Diffstat (limited to 'src/resp_parser.c')
-rw-r--r--src/resp_parser.c228
1 files changed, 228 insertions, 0 deletions
diff --git a/src/resp_parser.c b/src/resp_parser.c
new file mode 100644
index 000000000..c9f5d0b00
--- /dev/null
+++ b/src/resp_parser.c
@@ -0,0 +1,228 @@
+/*
+ * Copyright (c) 2009-2021, Redis Labs Ltd.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Redis nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* ----------------------------------------------------------------------------------------
+ * A RESP parser for parsing replies returned by RM_Call or Lua's
+ * 'redis.call()'.
+ *
+ * The parser introduces callbacks that need to be set by the user. Each
+ * callback represents a different reply type. Each callback gets a p_ctx that
+ * was given to the parseReply function. The callbacks also give the protocol
+ * (underlying blob) of the current reply and the size.
+ *
+ * Some callbacks also get the parser object itself:
+ * - array_callback
+ * - set_callback
+ * - map_callback
+ *
+ * These callbacks need to continue parsing by calling parseReply a number of
+ * times, according to the supplied length. Subsequent parseReply calls may use
+ * a different p_ctx, which will be used for nested CallReply objects.
+ *
+ * These callbacks also do not receive a proto_len, which is not known at the
+ * time of parsing. Callers may calculate it themselves after parsing the
+ * entire collection.
+ *
+ * NOTE: This parser is designed to only handle replies generated by Redis
+ * itself. It does not perform many required validations and thus NOT SAFE FOR
+ * PARSING USER INPUT.
+ * ----------------------------------------------------------------------------------------
+ */
+
+#include "resp_parser.h"
+#include "server.h"
+
+static int parseBulk(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ long long bulklen;
+ parser->curr_location = p + 2; /* for \r\n */
+
+ string2ll(proto+1,p-proto-1,&bulklen);
+ if (bulklen == -1) {
+ parser->callbacks.null_bulk_string_callback(p_ctx, proto, parser->curr_location - proto);
+ } else {
+ const char *str = parser->curr_location;
+ parser->curr_location += bulklen;
+ parser->curr_location += 2; /* for \r\n */
+ parser->callbacks.bulk_string_callback(p_ctx, str, bulklen, proto, parser->curr_location - proto);
+ }
+
+ return C_OK;
+}
+
+static int parseSimpleString(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; /* for \r\n */
+ parser->callbacks.simple_str_callback(p_ctx, proto+1, p-proto-1, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseError(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; // for \r\n
+ parser->callbacks.error_callback(p_ctx, proto+1, p-proto-1, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseLong(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; /* for \r\n */
+ long long val;
+ string2ll(proto+1,p-proto-1,&val);
+ parser->callbacks.long_callback(p_ctx, val, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseAttributes(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ long long len;
+ string2ll(proto+1,p-proto-1,&len);
+ p += 2;
+ parser->curr_location = p;
+ parser->callbacks.attribute_callback(parser, p_ctx, len, proto);
+ return C_OK;
+}
+
+static int parseVerbatimString(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ long long bulklen;
+ parser->curr_location = p + 2; /* for \r\n */
+ string2ll(proto+1,p-proto-1,&bulklen);
+ const char *format = parser->curr_location;
+ parser->curr_location += bulklen;
+ parser->curr_location += 2; /* for \r\n */
+ parser->callbacks.verbatim_string_callback(p_ctx, format, format + 4, bulklen - 4, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseBigNumber(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; /* for \r\n */
+ parser->callbacks.big_number_callback(p_ctx, proto+1, p-proto-1, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseNull(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; /* for \r\n */
+ parser->callbacks.null_callback(p_ctx, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseDouble(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; /* for \r\n */
+ char buf[MAX_LONG_DOUBLE_CHARS+1];
+ size_t len = p-proto-1;
+ double d;
+ if (len <= MAX_LONG_DOUBLE_CHARS) {
+ memcpy(buf,proto+1,len);
+ buf[len] = '\0';
+ d = strtod(buf,NULL); /* We expect a valid representation. */
+ } else {
+ d = 0;
+ }
+ parser->callbacks.double_callback(p_ctx, d, proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseBool(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ parser->curr_location = p + 2; /* for \r\n */
+ parser->callbacks.bool_callback(p_ctx, proto[1] == 't', proto, parser->curr_location - proto);
+ return C_OK;
+}
+
+static int parseArray(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ long long len;
+ string2ll(proto+1,p-proto-1,&len);
+ p += 2;
+ parser->curr_location = p;
+ if (len == -1) {
+ parser->callbacks.null_array_callback(p_ctx, proto, parser->curr_location - proto);
+ } else {
+ parser->callbacks.array_callback(parser, p_ctx, len, proto);
+ }
+ return C_OK;
+}
+
+static int parseSet(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ long long len;
+ string2ll(proto+1,p-proto-1,&len);
+ p += 2;
+ parser->curr_location = p;
+ parser->callbacks.set_callback(parser, p_ctx, len, proto);
+ return C_OK;
+}
+
+static int parseMap(ReplyParser *parser, void *p_ctx) {
+ const char *proto = parser->curr_location;
+ char *p = strchr(proto+1,'\r');
+ long long len;
+ string2ll(proto+1,p-proto-1,&len);
+ p += 2;
+ parser->curr_location = p;
+ parser->callbacks.map_callback(parser, p_ctx, len, proto);
+ return C_OK;
+}
+
+/* Parse a reply pointed to by parser->curr_location. */
+int parseReply(ReplyParser *parser, void *p_ctx) {
+ switch (parser->curr_location[0]) {
+ case '$': return parseBulk(parser, p_ctx);
+ case '+': return parseSimpleString(parser, p_ctx);
+ case '-': return parseError(parser, p_ctx);
+ case ':': return parseLong(parser, p_ctx);
+ case '*': return parseArray(parser, p_ctx);
+ case '~': return parseSet(parser, p_ctx);
+ case '%': return parseMap(parser, p_ctx);
+ case '#': return parseBool(parser, p_ctx);
+ case ',': return parseDouble(parser, p_ctx);
+ case '_': return parseNull(parser, p_ctx);
+ case '(': return parseBigNumber(parser, p_ctx);
+ case '=': return parseVerbatimString(parser, p_ctx);
+ case '|': return parseAttributes(parser, p_ctx);
+ default: if (parser->callbacks.error) parser->callbacks.error(p_ctx);
+ }
+ return C_ERR;
+}