forked from Nordix/hiredis-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
1334 lines (1160 loc) · 38.6 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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>
#include <strings.h>
#include "command.h"
#include "hiarray.h"
#include "hiutil.h"
static uint64_t cmd_id = 0; /* command id counter */
/*
* Return true, if the redis command take no key, otherwise
* return false
* Format: command
*/
static int redis_argz(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_PING:
case CMD_REQ_REDIS_QUIT:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts no arguments, otherwise
* return false
* Format: command key
*/
static int redis_arg0(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_PERSIST:
case CMD_REQ_REDIS_PTTL:
case CMD_REQ_REDIS_SORT:
case CMD_REQ_REDIS_TTL:
case CMD_REQ_REDIS_TYPE:
case CMD_REQ_REDIS_DUMP:
case CMD_REQ_REDIS_DECR:
case CMD_REQ_REDIS_GET:
case CMD_REQ_REDIS_INCR:
case CMD_REQ_REDIS_STRLEN:
case CMD_REQ_REDIS_HGETALL:
case CMD_REQ_REDIS_HKEYS:
case CMD_REQ_REDIS_HLEN:
case CMD_REQ_REDIS_HVALS:
case CMD_REQ_REDIS_LLEN:
case CMD_REQ_REDIS_LPOP:
case CMD_REQ_REDIS_RPOP:
case CMD_REQ_REDIS_SCARD:
case CMD_REQ_REDIS_SMEMBERS:
case CMD_REQ_REDIS_SPOP:
case CMD_REQ_REDIS_XLEN:
case CMD_REQ_REDIS_ZCARD:
case CMD_REQ_REDIS_PFCOUNT:
case CMD_REQ_REDIS_AUTH:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts exactly 1 argument, otherwise
* return false
* Format: command key arg
*/
static int redis_arg1(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_EXPIRE:
case CMD_REQ_REDIS_EXPIREAT:
case CMD_REQ_REDIS_PEXPIRE:
case CMD_REQ_REDIS_PEXPIREAT:
case CMD_REQ_REDIS_APPEND:
case CMD_REQ_REDIS_DECRBY:
case CMD_REQ_REDIS_GETBIT:
case CMD_REQ_REDIS_GETSET:
case CMD_REQ_REDIS_INCRBY:
case CMD_REQ_REDIS_INCRBYFLOAT:
case CMD_REQ_REDIS_SETNX:
case CMD_REQ_REDIS_HEXISTS:
case CMD_REQ_REDIS_HGET:
case CMD_REQ_REDIS_LINDEX:
case CMD_REQ_REDIS_LPUSHX:
case CMD_REQ_REDIS_RPOPLPUSH:
case CMD_REQ_REDIS_RPUSHX:
case CMD_REQ_REDIS_SISMEMBER:
case CMD_REQ_REDIS_ZRANK:
case CMD_REQ_REDIS_ZREVRANK:
case CMD_REQ_REDIS_ZSCORE:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts exactly 2 arguments, otherwise
* return false
* Format: command key arg1 arg2
*/
static int redis_arg2(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_GETRANGE:
case CMD_REQ_REDIS_PSETEX:
case CMD_REQ_REDIS_SETBIT:
case CMD_REQ_REDIS_SETEX:
case CMD_REQ_REDIS_SETRANGE:
case CMD_REQ_REDIS_HINCRBY:
case CMD_REQ_REDIS_HINCRBYFLOAT:
case CMD_REQ_REDIS_HSET:
case CMD_REQ_REDIS_HSETNX:
case CMD_REQ_REDIS_LRANGE:
case CMD_REQ_REDIS_LREM:
case CMD_REQ_REDIS_LSET:
case CMD_REQ_REDIS_LTRIM:
case CMD_REQ_REDIS_SMOVE:
case CMD_REQ_REDIS_ZCOUNT:
case CMD_REQ_REDIS_ZLEXCOUNT:
case CMD_REQ_REDIS_ZINCRBY:
case CMD_REQ_REDIS_ZREMRANGEBYLEX:
case CMD_REQ_REDIS_ZREMRANGEBYRANK:
case CMD_REQ_REDIS_ZREMRANGEBYSCORE:
case CMD_REQ_REDIS_RESTORE:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts exactly 3 arguments, otherwise
* return false
* Format: command key arg1 arg2 arg3
*/
static int redis_arg3(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_LINSERT:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts 0 or more arguments, otherwise
* return false
* Format: command key [ arg ... ]
*/
static int redis_argn(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_BITCOUNT:
case CMD_REQ_REDIS_SET:
case CMD_REQ_REDIS_HDEL:
case CMD_REQ_REDIS_HMGET:
case CMD_REQ_REDIS_HMSET:
case CMD_REQ_REDIS_HSCAN:
case CMD_REQ_REDIS_LPUSH:
case CMD_REQ_REDIS_RPUSH:
case CMD_REQ_REDIS_SADD:
case CMD_REQ_REDIS_SDIFF:
case CMD_REQ_REDIS_SDIFFSTORE:
case CMD_REQ_REDIS_SINTER:
case CMD_REQ_REDIS_SINTERSTORE:
case CMD_REQ_REDIS_SREM:
case CMD_REQ_REDIS_SUNION:
case CMD_REQ_REDIS_SUNIONSTORE:
case CMD_REQ_REDIS_SRANDMEMBER:
case CMD_REQ_REDIS_SSCAN:
case CMD_REQ_REDIS_PFADD:
case CMD_REQ_REDIS_PFMERGE:
case CMD_REQ_REDIS_XACK:
case CMD_REQ_REDIS_XADD:
case CMD_REQ_REDIS_XAUTOCLAIM:
case CMD_REQ_REDIS_XCLAIM:
case CMD_REQ_REDIS_XDEL:
case CMD_REQ_REDIS_XPENDING:
case CMD_REQ_REDIS_XRANGE:
case CMD_REQ_REDIS_XREVRANGE:
case CMD_REQ_REDIS_XTRIM:
case CMD_REQ_REDIS_ZADD:
case CMD_REQ_REDIS_ZINTERSTORE:
case CMD_REQ_REDIS_ZRANGE:
case CMD_REQ_REDIS_ZRANGEBYSCORE:
case CMD_REQ_REDIS_ZREM:
case CMD_REQ_REDIS_ZREVRANGE:
case CMD_REQ_REDIS_ZRANGEBYLEX:
case CMD_REQ_REDIS_ZREVRANGEBYSCORE:
case CMD_REQ_REDIS_ZUNIONSTORE:
case CMD_REQ_REDIS_ZSCAN:
return 1;
default:
break;
}
return 0;
}
/*
* 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;
}
/*
* Check if command type expects a sub-command before the key
* Format: command subcommand key [ arg ... ]
*/
static int redis_argsub(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_XGROUP:
case CMD_REQ_REDIS_XINFO:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command is either EVAL or EVALSHA. These commands
* have a special format with exactly 2 arguments, followed by one or more keys,
* followed by zero or more arguments (the documentation online seems to suggest
* that at least one argument is required, but that shouldn't be the case).
*/
static int redis_argeval(struct cmd *r) {
switch (r->type) {
case CMD_REQ_REDIS_EVAL:
case CMD_REQ_REDIS_EVALSHA:
return 1;
default:
break;
}
return 0;
}
static inline cmd_type_t redis_parse_cmd_verb(const char *m, int len) {
// clang-format off
switch (len) {
case 3:
return !strncasecmp(m, "get", 3) ? CMD_REQ_REDIS_GET :
!strncasecmp(m, "set", 3) ? CMD_REQ_REDIS_SET :
!strncasecmp(m, "ttl", 3) ? CMD_REQ_REDIS_TTL :
!strncasecmp(m, "del", 3) ? CMD_REQ_REDIS_DEL :
CMD_UNKNOWN;
case 4:
return !strncasecmp(m, "pttl", 4) ? CMD_REQ_REDIS_PTTL :
!strncasecmp(m, "decr", 4) ? CMD_REQ_REDIS_DECR :
!strncasecmp(m, "dump", 4) ? CMD_REQ_REDIS_DUMP :
!strncasecmp(m, "hdel", 4) ? CMD_REQ_REDIS_HDEL :
!strncasecmp(m, "hget", 4) ? CMD_REQ_REDIS_HGET :
!strncasecmp(m, "hlen", 4) ? CMD_REQ_REDIS_HLEN :
!strncasecmp(m, "hset", 4) ? CMD_REQ_REDIS_HSET :
!strncasecmp(m, "incr", 4) ? CMD_REQ_REDIS_INCR :
!strncasecmp(m, "llen", 4) ? CMD_REQ_REDIS_LLEN :
!strncasecmp(m, "lpop", 4) ? CMD_REQ_REDIS_LPOP :
!strncasecmp(m, "lrem", 4) ? CMD_REQ_REDIS_LREM :
!strncasecmp(m, "lset", 4) ? CMD_REQ_REDIS_LSET :
!strncasecmp(m, "rpop", 4) ? CMD_REQ_REDIS_RPOP :
!strncasecmp(m, "sadd", 4) ? CMD_REQ_REDIS_SADD :
!strncasecmp(m, "spop", 4) ? CMD_REQ_REDIS_SPOP :
!strncasecmp(m, "srem", 4) ? CMD_REQ_REDIS_SREM :
!strncasecmp(m, "type", 4) ? CMD_REQ_REDIS_TYPE :
!strncasecmp(m, "mget", 4) ? CMD_REQ_REDIS_MGET :
!strncasecmp(m, "mset", 4) ? CMD_REQ_REDIS_MSET :
!strncasecmp(m, "xack", 4) ? CMD_REQ_REDIS_XACK :
!strncasecmp(m, "xadd", 4) ? CMD_REQ_REDIS_XADD :
!strncasecmp(m, "xdel", 4) ? CMD_REQ_REDIS_XDEL :
!strncasecmp(m, "xlen", 4) ? CMD_REQ_REDIS_XLEN :
!strncasecmp(m, "zadd", 4) ? CMD_REQ_REDIS_ZADD :
!strncasecmp(m, "zrem", 4) ? CMD_REQ_REDIS_ZREM :
!strncasecmp(m, "eval", 4) ? CMD_REQ_REDIS_EVAL :
!strncasecmp(m, "sort", 4) ? CMD_REQ_REDIS_SORT :
!strncasecmp(m, "ping", 4) ? CMD_REQ_REDIS_PING :
!strncasecmp(m, "quit", 4) ? CMD_REQ_REDIS_QUIT :
!strncasecmp(m, "auth", 4) ? CMD_REQ_REDIS_AUTH :
CMD_UNKNOWN;
case 5:
return !strncasecmp(m, "hkeys", 5) ? CMD_REQ_REDIS_HKEYS :
!strncasecmp(m, "hmget", 5) ? CMD_REQ_REDIS_HMGET :
!strncasecmp(m, "hmset", 5) ? CMD_REQ_REDIS_HMSET :
!strncasecmp(m, "hvals", 5) ? CMD_REQ_REDIS_HVALS :
!strncasecmp(m, "hscan", 5) ? CMD_REQ_REDIS_HSCAN :
!strncasecmp(m, "lpush", 5) ? CMD_REQ_REDIS_LPUSH :
!strncasecmp(m, "ltrim", 5) ? CMD_REQ_REDIS_LTRIM :
!strncasecmp(m, "rpush", 5) ? CMD_REQ_REDIS_RPUSH :
!strncasecmp(m, "scard", 5) ? CMD_REQ_REDIS_SCARD :
!strncasecmp(m, "sdiff", 5) ? CMD_REQ_REDIS_SDIFF :
!strncasecmp(m, "setex", 5) ? CMD_REQ_REDIS_SETEX :
!strncasecmp(m, "setnx", 5) ? CMD_REQ_REDIS_SETNX :
!strncasecmp(m, "smove", 5) ? CMD_REQ_REDIS_SMOVE :
!strncasecmp(m, "sscan", 5) ? CMD_REQ_REDIS_SSCAN :
!strncasecmp(m, "xinfo", 5) ? CMD_REQ_REDIS_XINFO :
!strncasecmp(m, "xtrim", 5) ? CMD_REQ_REDIS_XTRIM :
!strncasecmp(m, "zcard", 5) ? CMD_REQ_REDIS_ZCARD :
!strncasecmp(m, "zrank", 5) ? CMD_REQ_REDIS_ZRANK :
!strncasecmp(m, "zscan", 5) ? CMD_REQ_REDIS_ZSCAN :
!strncasecmp(m, "pfadd", 5) ? CMD_REQ_REDIS_PFADD :
CMD_UNKNOWN;
case 6:
return !strncasecmp(m, "append", 6) ? CMD_REQ_REDIS_APPEND :
!strncasecmp(m, "decrby", 6) ? CMD_REQ_REDIS_DECRBY :
!strncasecmp(m, "exists", 6) ? CMD_REQ_REDIS_EXISTS :
!strncasecmp(m, "expire", 6) ? CMD_REQ_REDIS_EXPIRE :
!strncasecmp(m, "getbit", 6) ? CMD_REQ_REDIS_GETBIT :
!strncasecmp(m, "getset", 6) ? CMD_REQ_REDIS_GETSET :
!strncasecmp(m, "psetex", 6) ? CMD_REQ_REDIS_PSETEX :
!strncasecmp(m, "hsetnx", 6) ? CMD_REQ_REDIS_HSETNX :
!strncasecmp(m, "incrby", 6) ? CMD_REQ_REDIS_INCRBY :
!strncasecmp(m, "lindex", 6) ? CMD_REQ_REDIS_LINDEX :
!strncasecmp(m, "lpushx", 6) ? CMD_REQ_REDIS_LPUSHX :
!strncasecmp(m, "lrange", 6) ? CMD_REQ_REDIS_LRANGE :
!strncasecmp(m, "rpushx", 6) ? CMD_REQ_REDIS_RPUSHX :
!strncasecmp(m, "setbit", 6) ? CMD_REQ_REDIS_SETBIT :
!strncasecmp(m, "sinter", 6) ? CMD_REQ_REDIS_SINTER :
!strncasecmp(m, "strlen", 6) ? CMD_REQ_REDIS_STRLEN :
!strncasecmp(m, "sunion", 6) ? CMD_REQ_REDIS_SUNION :
!strncasecmp(m, "xclaim", 6) ? CMD_REQ_REDIS_XCLAIM :
!strncasecmp(m, "xgroup", 6) ? CMD_REQ_REDIS_XGROUP :
!strncasecmp(m, "xrange", 6) ? CMD_REQ_REDIS_XRANGE :
!strncasecmp(m, "zcount", 6) ? CMD_REQ_REDIS_ZCOUNT :
!strncasecmp(m, "zrange", 6) ? CMD_REQ_REDIS_ZRANGE :
!strncasecmp(m, "zscore", 6) ? CMD_REQ_REDIS_ZSCORE :
CMD_UNKNOWN;
case 7:
return !strncasecmp(m, "persist", 7) ? CMD_REQ_REDIS_PERSIST :
!strncasecmp(m, "pexpire", 7) ? CMD_REQ_REDIS_PEXPIRE :
!strncasecmp(m, "hexists", 7) ? CMD_REQ_REDIS_HEXISTS :
!strncasecmp(m, "hgetall", 7) ? CMD_REQ_REDIS_HGETALL :
!strncasecmp(m, "hincrby", 7) ? CMD_REQ_REDIS_HINCRBY :
!strncasecmp(m, "linsert", 7) ? CMD_REQ_REDIS_LINSERT :
!strncasecmp(m, "zincrby", 7) ? CMD_REQ_REDIS_ZINCRBY :
!strncasecmp(m, "evalsha", 7) ? CMD_REQ_REDIS_EVALSHA :
!strncasecmp(m, "restore", 7) ? CMD_REQ_REDIS_RESTORE :
!strncasecmp(m, "pfcount", 7) ? CMD_REQ_REDIS_PFCOUNT :
!strncasecmp(m, "pfmerge", 7) ? CMD_REQ_REDIS_PFMERGE :
CMD_UNKNOWN;
case 8:
return !strncasecmp(m, "expireat", 8) ? CMD_REQ_REDIS_EXPIREAT :
!strncasecmp(m, "bitcount", 8) ? CMD_REQ_REDIS_BITCOUNT :
!strncasecmp(m, "getrange", 8) ? CMD_REQ_REDIS_GETRANGE :
!strncasecmp(m, "setrange", 8) ? CMD_REQ_REDIS_SETRANGE :
!strncasecmp(m, "smembers", 8) ? CMD_REQ_REDIS_SMEMBERS :
!strncasecmp(m, "xpending", 8) ? CMD_REQ_REDIS_XPENDING :
!strncasecmp(m, "zrevrank", 8) ? CMD_REQ_REDIS_ZREVRANK :
CMD_UNKNOWN;
case 9:
return !strncasecmp(m, "pexpireat", 9) ? CMD_REQ_REDIS_PEXPIREAT :
!strncasecmp(m, "rpoplpush", 9) ? CMD_REQ_REDIS_RPOPLPUSH :
!strncasecmp(m, "sismember", 9) ? CMD_REQ_REDIS_SISMEMBER :
!strncasecmp(m, "xrevrange", 9) ? CMD_REQ_REDIS_XREVRANGE :
!strncasecmp(m, "zrevrange", 9) ? CMD_REQ_REDIS_ZREVRANGE :
!strncasecmp(m, "zlexcount", 9) ? CMD_REQ_REDIS_ZLEXCOUNT :
CMD_UNKNOWN;
case 10:
return !strncasecmp(m, "sdiffstore", 10) ? CMD_REQ_REDIS_SDIFFSTORE :
!strncasecmp(m, "xautoclaim", 10) ? CMD_REQ_REDIS_XAUTOCLAIM :
CMD_UNKNOWN;
case 11:
return !strncasecmp(m, "incrbyfloat", 11) ? CMD_REQ_REDIS_INCRBYFLOAT :
!strncasecmp(m, "sinterstore", 11) ? CMD_REQ_REDIS_SINTERSTORE :
!strncasecmp(m, "srandmember", 11) ? CMD_REQ_REDIS_SRANDMEMBER :
!strncasecmp(m, "sunionstore", 11) ? CMD_REQ_REDIS_SUNIONSTORE :
!strncasecmp(m, "zinterstore", 11) ? CMD_REQ_REDIS_ZINTERSTORE :
!strncasecmp(m, "zunionstore", 11) ? CMD_REQ_REDIS_ZUNIONSTORE :
!strncasecmp(m, "zrangebylex", 11) ? CMD_REQ_REDIS_ZRANGEBYLEX :
CMD_UNKNOWN;
case 12:
return !strncasecmp(m, "hincrbyfloat", 12) ?
CMD_REQ_REDIS_HINCRBYFLOAT :
CMD_UNKNOWN;
case 13:
return !strncasecmp(m, "zrangebyscore", 13) ?
CMD_REQ_REDIS_ZRANGEBYSCORE :
CMD_UNKNOWN;
case 14:
return !strncasecmp(m, "zremrangebylex", 14) ?
CMD_REQ_REDIS_ZREMRANGEBYLEX :
CMD_UNKNOWN;
case 15:
return !strncasecmp(m, "zremrangebyrank", 15) ?
CMD_REQ_REDIS_ZREMRANGEBYRANK :
CMD_UNKNOWN;
case 16:
return !strncasecmp(m, "zremrangebyscore", 16) ?
CMD_REQ_REDIS_ZREMRANGEBYSCORE :
!strncasecmp(m, "zrevrangebyscore", 16) ?
CMD_REQ_REDIS_ZREVRANGEBYSCORE :
CMD_UNKNOWN;
default:
return CMD_UNKNOWN;
}
// clang-format on
}
/*
* 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) {
int len;
char *p, *m, *token = NULL;
char *cmd_end;
char ch;
uint32_t rlen = 0; /* running length in parsing fsa */
uint32_t rnarg = 0; /* running # arg used by parsing fsa */
enum {
SW_START,
SW_NARG,
SW_NARG_LF,
SW_CMD_TYPE_LEN,
SW_CMD_TYPE_LEN_LF,
SW_CMD_TYPE,
SW_CMD_TYPE_LF,
SW_KEY_LEN,
SW_KEY_LEN_LF,
SW_KEY,
SW_KEY_LF,
SW_ARG1_LEN,
SW_ARG1_LEN_LF,
SW_ARG1,
SW_ARG1_LF,
SW_ARG2_LEN,
SW_ARG2_LEN_LF,
SW_ARG2,
SW_ARG2_LF,
SW_ARG3_LEN,
SW_ARG3_LEN_LF,
SW_ARG3,
SW_ARG3_LF,
SW_ARGN_LEN,
SW_ARGN_LEN_LF,
SW_ARGN,
SW_ARGN_LF,
SW_SENTINEL
} state;
state = SW_START;
cmd_end = r->cmd + r->clen;
ASSERT(state >= SW_START && state < SW_SENTINEL);
ASSERT(r->cmd != NULL && r->clen > 0);
for (p = r->cmd; p < cmd_end; p++) {
ch = *p;
switch (state) {
case SW_START:
case SW_NARG:
if (token == NULL) {
if (ch != '*') {
goto error;
}
token = p;
r->narg_start = p;
rnarg = 0;
state = SW_NARG;
} else if (isdigit(ch)) {
rnarg = rnarg * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if (rnarg == 0) {
goto error;
}
r->narg = rnarg;
r->narg_end = p;
token = NULL;
state = SW_NARG_LF;
} else {
goto error;
}
break;
case SW_NARG_LF:
switch (ch) {
case LF:
state = SW_CMD_TYPE_LEN;
break;
default:
goto error;
}
break;
case SW_CMD_TYPE_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
token = p;
rlen = 0;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if (rlen == 0 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_CMD_TYPE_LEN_LF;
} else {
goto error;
}
break;
case SW_CMD_TYPE_LEN_LF:
switch (ch) {
case LF:
state = SW_CMD_TYPE;
break;
default:
goto error;
}
break;
case SW_CMD_TYPE:
if (token == NULL) {
token = p;
}
m = token + rlen;
if (m >= cmd_end) {
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
m = token;
token = NULL;
r->type = redis_parse_cmd_verb(m, p - m);
if (r->type == CMD_UNKNOWN) {
goto error;
}
state = SW_CMD_TYPE_LF;
break;
case SW_CMD_TYPE_LF:
switch (ch) {
case LF:
if (redis_argz(r)) {
goto done;
} else if (redis_argeval(r) || redis_argsub(r)) {
state = SW_ARG1_LEN;
} else {
state = SW_KEY_LEN;
}
break;
default:
goto error;
}
break;
case SW_KEY_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
token = p;
rlen = 0;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if (rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_KEY_LEN_LF;
} else {
goto error;
}
break;
case SW_KEY_LEN_LF:
switch (ch) {
case LF:
state = SW_KEY;
break;
default:
goto error;
}
break;
case SW_KEY:
if (token == NULL) {
token = p;
}
m = token + rlen;
if (m >= cmd_end) {
goto error;
}
if (*m != CR) {
goto error;
} else { /* got a key */
struct keypos *kpos;
p = m; /* move forward by rlen bytes */
rlen = 0;
m = token;
token = NULL;
kpos = hiarray_push(r->keys);
if (kpos == NULL) {
goto oom;
}
kpos->start = m;
kpos->end = p;
state = SW_KEY_LF;
}
break;
case SW_KEY_LF:
switch (ch) {
case LF:
if (redis_arg0(r)) {
if (rnarg != 0) {
goto error;
}
goto done;
} else if (redis_arg1(r)) {
if (rnarg != 1) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_arg2(r)) {
if (rnarg != 2) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_arg3(r)) {
if (rnarg != 3) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_argn(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARG1_LEN;
} else if (redis_argx(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_KEY_LEN;
} else if (redis_argkvx(r)) {
if (rnarg == 0) {
goto done;
}
if (r->narg % 2 == 0) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_argeval(r) || redis_argsub(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_ARG1_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
rlen = 0;
token = p;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if ((p - token) <= 1 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
/*
//for mset value length
if(redis_argkvx(r))
{
struct keypos *kpos;
uint32_t array_len = array_n(r->keys);
if(array_len == 0)
{
goto error;
}
kpos = array_n(r->keys, array_len-1);
if (kpos == NULL || kpos->v_len != 0) {
goto error;
}
kpos->v_len = rlen;
}
*/
state = SW_ARG1_LEN_LF;
} else {
goto error;
}
break;
case SW_ARG1_LEN_LF:
switch (ch) {
case LF:
state = SW_ARG1;
break;
default:
goto error;
}
break;
case SW_ARG1:
m = p + rlen;
if (m >= cmd_end) {
// Moving past the end, not good..
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
state = SW_ARG1_LF;
break;
case SW_ARG1_LF:
// Check that the command parser has enough
// arguments left to be acceptable
// rnarg is the number of arguments after the first argument
switch (ch) {
case LF:
if (redis_arg1(r)) {
if (rnarg != 0) {
goto error;
}
goto done;
} else if (redis_arg2(r)) {
if (rnarg != 1) {
goto error;
}
state = SW_ARG2_LEN;
} else if (redis_arg3(r)) {
if (rnarg != 2) {
goto error;
}
state = SW_ARG2_LEN;
} else if (redis_argn(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else if (redis_argeval(r)) {
// EVAL command layout:
// eval <script> <no of keys> <keys..> <args..>
// ^
// The Redis docs specifies that EVAL dont require a key
// (i.e rnarg < 1) but also that a key would be
// required for it to work in Redis Cluster.
// Hiredis-cluster requires at least one key in eval to know
// which instance to use.
if (rnarg < 2) {
goto error;
}
state = SW_ARG2_LEN;
} else if (redis_argsub(r)) {
// Command layout: cmd <sub-cmd> <key> <args..>
// Current position: ^
if (rnarg < 1) {
goto error;
}
state = SW_KEY_LEN;
} else if (redis_argkvx(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_KEY_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_ARG2_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
rlen = 0;
token = p;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if ((p - token) <= 1 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_ARG2_LEN_LF;
} else {
goto error;
}
break;
case SW_ARG2_LEN_LF:
switch (ch) {
case LF:
state = SW_ARG2;
break;
default:
goto error;
}
break;
case SW_ARG2:
if (token == NULL && redis_argeval(r)) {
/*
* For EVAL/EVALSHA, ARG2 represents the # key/arg pairs which
* must be tokenized and stored in contiguous memory.
*/
token = p;
}
m = p + rlen;
if (m >= cmd_end) {
// rlen -= (uint32_t)(b->last - p);
// m = b->last - 1;
// p = m;
// break;
goto error;
}
if (*m != CR) {
goto error;
}