-
Notifications
You must be signed in to change notification settings - Fork 43
/
command.c
486 lines (434 loc) · 15.7 KB
/
command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright (c) 2015-2017, Ieshen Zheng <ieshen.zheng at 163 dot com>
* Copyright (c) 2020, Nick <heronr1 at gmail dot com>
* Copyright (c) 2020-2021, Bjorn Svensson <bjorn.a.svensson at est dot tech>
* 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.
*/
#include <ctype.h>
#include <errno.h>
#include <hiredis/alloc.h>
#ifndef _WIN32
#include <alloca.h>
#include <strings.h>
#else
#include <malloc.h>
#endif
#include <string.h>
#include "command.h"
#include "hiarray.h"
#include "hiutil.h"
#include "win32.h"
#define LF (uint8_t)10
#define CR (uint8_t)13
static uint64_t cmd_id = 0; /* command id counter */
typedef enum {
KEYPOS_NONE,
KEYPOS_UNKNOWN,
KEYPOS_INDEX,
KEYPOS_KEYNUM
} cmd_keypos;
typedef struct {
cmd_type_t type; /* A constant identifying the command. */
const char *name; /* Command name */
const char *subname; /* Subcommand name or NULL */
cmd_keypos firstkeymethod; /* First key none, unknown, pos or keynum */
int8_t firstkeypos; /* Position of first key or the arg */
int8_t arity; /* Arity, neg number means min num args */
} cmddef;
/* Populate the table with code in cmddef.h generated from Redis JSON files. */
static cmddef redis_commands[] = {
#define COMMAND(_type, _name, _subname, _arity, _keymethod, _keypos) \
{.type = CMD_REQ_REDIS_##_type, \
.name = _name, \
.subname = _subname, \
.firstkeymethod = KEYPOS_##_keymethod, \
.firstkeypos = _keypos, \
.arity = _arity},
#include "cmddef.h"
#undef COMMAND
};
static inline void to_upper(char *dst, const char *src, uint32_t len) {
uint32_t i;
for (i = 0; i < len; i++) {
if (src[i] >= 'a' && src[i] <= 'z')
dst[i] = src[i] - ('a' - 'A');
else
dst[i] = src[i];
}
}
/* Looks up a command or subcommand in the command table. Arg0 and arg1 are used
* to lookup the command. The function returns CMD_UNKNOWN on failure. On
* success, the command type is returned and *firstkey and *arity are
* populated. */
cmddef *redis_lookup_cmd(const char *arg0, uint32_t arg0_len, const char *arg1,
uint32_t arg1_len) {
int num_commands = sizeof(redis_commands) / sizeof(cmddef);
/* Compare command name in uppercase. */
char *cmd = alloca(arg0_len);
to_upper(cmd, arg0, arg0_len);
char *subcmd = NULL; /* Alloca later on demand. */
/* Find the command using binary search. */
int left = 0, right = num_commands - 1;
while (left <= right) {
int i = (left + right) / 2;
cmddef *c = &redis_commands[i];
int cmp = strncmp(c->name, cmd, arg0_len);
if (cmp == 0 && strlen(c->name) > arg0_len)
cmp = 1; /* "HGETALL" vs "HGET" */
/* If command name matches, compare subcommand if any */
if (cmp == 0 && c->subname != NULL) {
if (arg1 == NULL) {
/* Command has subcommands, but none given. */
return NULL;
}
if (subcmd == NULL) {
subcmd = alloca(arg1_len);
to_upper(subcmd, arg1, arg1_len);
}
cmp = strncmp(c->subname, subcmd, arg1_len);
if (cmp == 0 && strlen(c->subname) > arg1_len)
cmp = 1;
}
if (cmp < 0) {
left = i + 1;
} else if (cmp > 0) {
right = i - 1;
} else {
/* Found it. */
return c;
}
}
return NULL;
}
/*
* Return true, if the redis command is a vector command accepting one or
* more keys, otherwise return false
* Format: command key [ key ... ]
*/
static int redis_argx(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_EXISTS:
case CMD_REQ_REDIS_MGET:
case CMD_REQ_REDIS_DEL:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command is a vector command accepting one or
* more key-value pairs, otherwise return false
* Format: command key value [ key value ... ]
*/
static int redis_argkvx(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_MSET:
return 1;
default:
break;
}
return 0;
}
/* Parses a bulk string starting at 'p' and ending somewhere before 'end'.
* Returns the remaining of the input after consuming the bulk string. The
* pointers *str and *len are pointed to the parsed string and its length. On
* parse error, NULL is returned. */
char *redis_parse_bulk(char *p, char *end, char **str, uint32_t *len) {
uint32_t length = 0;
if (p >= end || *p++ != '$')
return NULL;
while (p < end && *p >= '0' && *p <= '9') {
length = length * 10 + (uint32_t)(*p++ - '0');
}
if (p >= end || *p++ != CR)
return NULL;
if (p >= end || *p++ != LF)
return NULL;
if (str)
*str = p;
if (len)
*len = length;
p += length;
if (p >= end || *p++ != CR)
return NULL;
if (p >= end || *p++ != LF)
return NULL;
return p;
}
static inline int push_keypos(struct cmd *r, char *arg, uint32_t arglen) {
struct keypos *kpos = hiarray_push(r->keys);
if (kpos == NULL)
return 0;
kpos->start = arg;
kpos->end = arg + arglen;
return 1;
}
/*
* Reference: http://redis.io/topics/protocol
*
* Redis >= 1.2 uses the unified protocol to send requests to the Redis
* server. In the unified protocol all the arguments sent to the server
* are binary safe and every request has the following general form:
*
* *<number of arguments> CR LF
* $<number of bytes of argument 1> CR LF
* <argument data> CR LF
* ...
* $<number of bytes of argument N> CR LF
* <argument data> CR LF
*
* Before the unified request protocol, redis protocol for requests supported
* the following commands
* 1). Inline commands: simple commands where arguments are just space
* separated strings. No binary safeness is possible.
* 2). Bulk commands: bulk commands are exactly like inline commands, but
* the last argument is handled in a special way in order to allow for
* a binary-safe last argument.
*
* only supports the Redis unified protocol for requests.
*/
void redis_parse_cmd(struct cmd *r) {
ASSERT(r->cmd != NULL && r->clen > 0);
char *p = r->cmd;
char *end = r->cmd + r->clen;
uint32_t rnarg = 0; /* Number of args including cmd name */
int argidx = -1; /* Index of last parsed arg */
char *arg; /* Last parsed arg */
uint32_t arglen; /* Length of arg */
char *arg0 = NULL, *arg1 = NULL; /* The first two args */
uint32_t arg0_len = 0, arg1_len = 0; /* Lengths of arg0 and arg1 */
cmddef *info = NULL; /* Command info, when found */
/* Check that the command line is multi-bulk. */
if (*p++ != '*')
goto error;
/* Parse multi-bulk size (rnarg). */
while (p < end && *p >= '0' && *p <= '9') {
rnarg = rnarg * 10 + (uint32_t)(*p++ - '0');
}
if (p == end || *p++ != CR)
goto error;
if (p == end || *p++ != LF)
goto error;
if (rnarg == 0)
goto error;
r->narg = rnarg;
/* Parse the first two args. */
if ((p = redis_parse_bulk(p, end, &arg0, &arg0_len)) == NULL)
goto error;
argidx++;
if (rnarg > 1) {
if ((p = redis_parse_bulk(p, end, &arg1, &arg1_len)) == NULL)
goto error;
argidx++;
}
/* Lookup command. */
if ((info = redis_lookup_cmd(arg0, arg0_len, arg1, arg1_len)) == NULL)
goto error; /* Command not found. */
r->type = info->type;
/* Arity check (negative arity means minimum num args) */
if ((info->arity >= 0 && (int)rnarg != info->arity) ||
(info->arity < 0 && (int)rnarg < -info->arity)) {
goto error;
}
if (info->firstkeymethod == KEYPOS_NONE)
goto done; /* Command takes no keys. */
if (arg1 == NULL)
goto error; /* Command takes keys, but no args given. Quick abort. */
/* Below we assume arg1 != NULL, */
/* Handle commands where firstkey depends on special logic. */
if (info->firstkeymethod == KEYPOS_UNKNOWN) {
/* Keyword-based first key position */
const char *keyword;
int startfrom;
if (r->type == CMD_REQ_REDIS_XREAD) {
keyword = "STREAMS";
startfrom = 1;
} else if (r->type == CMD_REQ_REDIS_XREADGROUP) {
keyword = "STREAMS";
startfrom = 4;
} else {
/* Not reached, but can be reached if Redis adds more commands. */
goto error;
}
/* Skip forward to the 'startfrom' arg index, then search for the keyword. */
arg = arg1;
arglen = arg1_len;
while (argidx < (int)rnarg - 1) {
if ((p = redis_parse_bulk(p, end, &arg, &arglen)) == NULL)
goto error; /* Keyword not provided, thus no keys. */
if (argidx++ < startfrom)
continue; /* Keyword can't appear in a position before 'startfrom' */
if (!strncasecmp(keyword, arg, arglen)) {
/* Keyword found. Now the first key is the next arg. */
if ((p = redis_parse_bulk(p, end, &arg, &arglen)) == NULL)
goto error;
if (!push_keypos(r, arg, arglen))
goto oom;
goto done;
}
}
/* Keyword not provided. */
goto error;
}
/* Find first key arg. */
arg = arg1;
arglen = arg1_len;
for (; argidx < info->firstkeypos; argidx++) {
if ((p = redis_parse_bulk(p, end, &arg, &arglen)) == NULL)
goto error;
}
if (info->firstkeymethod == KEYPOS_KEYNUM) {
/* The arg specifies the number of keys and the first key is the next
* arg. Example:
*
* EVAL script numkeys [key [key ...]] [arg [arg ...]] */
if (!strncmp("0", arg, arglen))
goto done; /* No args. */
/* One or more args. The first key is the arg after the 'numkeys' arg. */
if ((p = redis_parse_bulk(p, end, &arg, &arglen)) == NULL)
goto error;
argidx++;
}
/* Now arg is the first key and arglen is its length. */
if (info->type == CMD_REQ_REDIS_MIGRATE && arglen == 0 &&
info->firstkeymethod == KEYPOS_INDEX && info->firstkeypos == 3) {
/* MIGRATE host port <key | ""> destination-db timeout [COPY] [REPLACE]
* [[AUTH password] | [AUTH2 username password]] [KEYS key [key ...]]
*
* The key spec points out arg3 as the first key, but if it's an empty
* string, we would need to search for the KEYS keyword arg backwards
* from the end of the command line. This is not implemented. */
goto error;
}
if (!push_keypos(r, arg, arglen))
goto oom;
/* Special commands where we want all keys (not only the first key). */
if (redis_argx(r) || redis_argkvx(r)) {
/* argx: MGET key [ key ... ] */
/* argkvx: MSET key value [ key value ... ] */
if (redis_argkvx(r) && rnarg % 2 == 0)
goto error;
for (uint32_t i = 2; i < rnarg; i++) {
if ((p = redis_parse_bulk(p, end, &arg, &arglen)) == NULL)
goto error;
if (redis_argkvx(r) && i % 2 == 0)
continue; /* not a key */
if (!push_keypos(r, arg, arglen))
goto oom;
}
}
done:
ASSERT(r->type > CMD_UNKNOWN && r->type < CMD_SENTINEL);
r->result = CMD_PARSE_OK;
return;
error:
r->result = CMD_PARSE_ERROR;
errno = EINVAL;
size_t errmaxlen = 100; /* Enough for the error messages below. */
if (r->errstr == NULL) {
r->errstr = hi_malloc(errmaxlen);
if (r->errstr == NULL) {
goto oom;
}
}
if (info != NULL && info->subname != NULL)
snprintf(r->errstr, errmaxlen, "Failed to find keys of command %s %s",
info->name, info->subname);
else if (info != NULL)
snprintf(r->errstr, errmaxlen, "Failed to find keys of command %s",
info->name);
else if (r->type == CMD_UNKNOWN && arg0 != NULL && arg1 != NULL)
snprintf(r->errstr, errmaxlen, "Unknown command %.*s %.*s", arg0_len,
arg0, arg1_len, arg1);
else if (r->type == CMD_UNKNOWN && arg0 != NULL)
snprintf(r->errstr, errmaxlen, "Unknown command %.*s", arg0_len, arg0);
else
snprintf(r->errstr, errmaxlen, "Command parse error");
return;
oom:
r->result = CMD_PARSE_ENOMEM;
}
struct cmd *command_get(void) {
struct cmd *command;
command = hi_malloc(sizeof(struct cmd));
if (command == NULL) {
return NULL;
}
command->id = ++cmd_id;
command->result = CMD_PARSE_OK;
command->errstr = NULL;
command->type = CMD_UNKNOWN;
command->cmd = NULL;
command->clen = 0;
command->keys = NULL;
command->narg = 0;
command->quit = 0;
command->noforward = 0;
command->slot_num = -1;
command->frag_seq = NULL;
command->reply = NULL;
command->sub_commands = NULL;
command->node_addr = NULL;
command->keys = hiarray_create(1, sizeof(struct keypos));
if (command->keys == NULL) {
hi_free(command);
return NULL;
}
return command;
}
void command_destroy(struct cmd *command) {
if (command == NULL) {
return;
}
if (command->cmd != NULL) {
hi_free(command->cmd);
command->cmd = NULL;
}
if (command->errstr != NULL) {
hi_free(command->errstr);
command->errstr = NULL;
}
if (command->keys != NULL) {
command->keys->nelem = 0;
hiarray_destroy(command->keys);
command->keys = NULL;
}
if (command->frag_seq != NULL) {
hi_free(command->frag_seq);
command->frag_seq = NULL;
}
freeReplyObject(command->reply);
if (command->sub_commands != NULL) {
listRelease(command->sub_commands);
}
if (command->node_addr != NULL) {
sdsfree(command->node_addr);
command->node_addr = NULL;
}
hi_free(command);
}