1/*
2 * Copyright (c) 2021, Redis Labs Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef SRC_RESP_PARSER_H_
31#define SRC_RESP_PARSER_H_
32
33#include <stddef.h>
34
35typedef struct ReplyParser ReplyParser;
36
37typedef struct ReplyParserCallbacks {
38 /* Called when the parser reaches an empty mbulk ('*-1') */
39 void (*null_array_callback)(void *ctx, const char *proto, size_t proto_len);
40
41 /* Called when the parser reaches an empty bulk ('$-1') (bulk len is -1) */
42 void (*null_bulk_string_callback)(void *ctx, const char *proto, size_t proto_len);
43
44 /* Called when the parser reaches a bulk ('$'), which is passed as 'str' along with its length 'len' */
45 void (*bulk_string_callback)(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len);
46
47 /* Called when the parser reaches an error ('-'), which is passed as 'str' along with its length 'len' */
48 void (*error_callback)(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len);
49
50 /* Called when the parser reaches a simple string ('+'), which is passed as 'str' along with its length 'len' */
51 void (*simple_str_callback)(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len);
52
53 /* Called when the parser reaches a long long value (':'), which is passed as an argument 'val' */
54 void (*long_callback)(void *ctx, long long val, const char *proto, size_t proto_len);
55
56 /* Called when the parser reaches an array ('*'). The array length is passed as an argument 'len' */
57 void (*array_callback)(struct ReplyParser *parser, void *ctx, size_t len, const char *proto);
58
59 /* Called when the parser reaches a set ('~'). The set length is passed as an argument 'len' */
60 void (*set_callback)(struct ReplyParser *parser, void *ctx, size_t len, const char *proto);
61
62 /* Called when the parser reaches a map ('%'). The map length is passed as an argument 'len' */
63 void (*map_callback)(struct ReplyParser *parser, void *ctx, size_t len, const char *proto);
64
65 /* Called when the parser reaches a bool ('#'), which is passed as an argument 'val' */
66 void (*bool_callback)(void *ctx, int val, const char *proto, size_t proto_len);
67
68 /* Called when the parser reaches a double (','), which is passed as an argument 'val' */
69 void (*double_callback)(void *ctx, double val, const char *proto, size_t proto_len);
70
71 /* Called when the parser reaches a big number ('('), which is passed as 'str' along with its length 'len' */
72 void (*big_number_callback)(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len);
73
74 /* Called when the parser reaches a string ('='), which is passed as 'str' along with its 'format' and length 'len' */
75 void (*verbatim_string_callback)(void *ctx, const char *format, const char *str, size_t len, const char *proto, size_t proto_len);
76
77 /* Called when the parser reaches an attribute ('|'). The attribute length is passed as an argument 'len' */
78 void (*attribute_callback)(struct ReplyParser *parser, void *ctx, size_t len, const char *proto);
79
80 /* Called when the parser reaches a null ('_') */
81 void (*null_callback)(void *ctx, const char *proto, size_t proto_len);
82
83 void (*error)(void *ctx);
84} ReplyParserCallbacks;
85
86struct ReplyParser {
87 /* The current location in the reply buffer, needs to be set to the beginning of the reply */
88 const char *curr_location;
89 ReplyParserCallbacks callbacks;
90};
91
92int parseReply(ReplyParser *parser, void *p_ctx);
93
94#endif /* SRC_RESP_PARSER_H_ */
95