Debian Codegraph @

dependencies/pkg/mod/github.com/redis/go-redis/[email protected]/internal/proto/reader.go

raw
1 package proto
2
3 import (
4 "bufio"
5 "errors"
6 "fmt"
7 "io"
8 "math"
9 "math/big"
10 "strconv"
11
12 "github.com/redis/go-redis/v9/internal/util"
13 )
14
15 // DefaultBufferSize is the default size for read/write buffers (32 KiB).
16 const DefaultBufferSize = 32 * 1024
17
18 // redis resp protocol data type.
19 const (
20 RespStatus = '+' // +<string>\r\n
21 RespError = '-' // -<string>\r\n
22 RespString = '$' // $<length>\r\n<bytes>\r\n
23 RespInt = ':' // :<number>\r\n
24 RespNil = '_' // _\r\n
25 RespFloat = ',' // ,<floating-point-number>\r\n (golang float)
26 RespBool = '#' // true: #t\r\n false: #f\r\n
27 RespBlobError = '!' // !<length>\r\n<bytes>\r\n
28 RespVerbatim = '=' // =<length>\r\nFORMAT:<bytes>\r\n
29 RespBigInt = '(' // (<big number>\r\n
30 RespArray = '*' // *<len>\r\n... (same as resp2)
31 RespMap = '%' // %<len>\r\n(key)\r\n(value)\r\n... (golang map)
32 RespSet = '~' // ~<len>\r\n... (same as Array)
33 RespAttr = '|' // |<len>\r\n(key)\r\n(value)\r\n... + command reply
34 RespPush = '>' // ><len>\r\n... (same as Array)
35 )
36
37 // Not used temporarily.
38 // Redis has not used these two data types for the time being, and will implement them later.
39 // Streamed = "EOF:"
40 // StreamedAggregated = '?'
41
42 //------------------------------------------------------------------------------
43
44 const Nil = RedisError("redis: nil") // nolint:errname
45
46 type RedisError string
47
48 func (e RedisError) Error() string { return string(e) }
49
50 func (RedisError) RedisError() {}
51
52 func ParseErrorReply(line []byte) error {
53 msg := string(line[1:])
54 return parseTypedRedisError(msg)
55 }
56
57 //------------------------------------------------------------------------------
58
59 type Reader struct {
60 rd *bufio.Reader
61 }
62
63 func NewReader(rd io.Reader) *Reader {
64 return &Reader{
65 rd: bufio.NewReaderSize(rd, DefaultBufferSize),
66 }
67 }
68
69 func NewReaderSize(rd io.Reader, size int) *Reader {
70 return &Reader{
71 rd: bufio.NewReaderSize(rd, size),
72 }
73 }
74
75 func (r *Reader) Buffered() int {
76 return r.rd.Buffered()
77 }
78
79 func (r *Reader) Peek(n int) ([]byte, error) {
80 return r.rd.Peek(n)
81 }
82
83 func (r *Reader) Reset(rd io.Reader) {
84 r.rd.Reset(rd)
85 }
86
87 // PeekReplyType returns the data type of the next response without advancing the Reader,
88 // and discard the attribute type.
89 func (r *Reader) PeekReplyType() (byte, error) {
90 b, err := r.rd.Peek(1)
91 if err != nil {
92 return 0, err
93 }
94 if b[0] == RespAttr {
95 if err = r.DiscardNext(); err != nil {
96 return 0, err
97 }
98 return r.PeekReplyType()
99 }
100 return b[0], nil
101 }
102
103 func (r *Reader) PeekPushNotificationName() (string, error) {
104 // "prime" the buffer by peeking at the next byte
105 c, err := r.Peek(1)
106 if err != nil {
107 return "", err
108 }
109 if c[0] != RespPush {
110 return "", fmt.Errorf("redis: can't peek push notification name, next reply is not a push notification")
111 }
112
113 // peek 36 bytes at most, should be enough to read the push notification name
114 toPeek := 36
115 buffered := r.Buffered()
116 if buffered == 0 {
117 return "", fmt.Errorf("redis: can't peek push notification name, no data available")
118 }
119 if buffered < toPeek {
120 toPeek = buffered
121 }
122 buf, err := r.rd.Peek(toPeek)
123 if err != nil {
124 return "", err
125 }
126 if buf[0] != RespPush {
127 return "", fmt.Errorf("redis: can't parse push notification: %q", buf)
128 }
129
130 if len(buf) < 3 {
131 return "", fmt.Errorf("redis: can't parse push notification: %q", buf)
132 }
133
134 // remove push notification type
135 buf = buf[1:]
136 // remove first line - e.g. >2\r\n
137 for i := 0; i < len(buf)-1; i++ {
138 if buf[i] == '\r' && buf[i+1] == '\n' {
139 buf = buf[i+2:]
140 break
141 } else {
142 if buf[i] < '0' || buf[i] > '9' {
143 return "", fmt.Errorf("redis: can't parse push notification: %q", buf)
144 }
145 }
146 }
147 if len(buf) < 2 {
148 return "", fmt.Errorf("redis: can't parse push notification: %q", buf)
149 }
150 // next line should be $<length><string>\r\n or +<length><string>\r\n
151 // should have the type of the push notification name and it's length
152 if buf[0] != RespString && buf[0] != RespStatus {
153 return "", fmt.Errorf("redis: can't parse push notification name: %q", buf)
154 }
155 typeOfName := buf[0]
156 // remove the type of the push notification name
157 buf = buf[1:]
158 if typeOfName == RespString {
159 // remove the length of the string
160 if len(buf) < 2 {
161 return "", fmt.Errorf("redis: can't parse push notification name: %q", buf)
162 }
163 for i := 0; i < len(buf)-1; i++ {
164 if buf[i] == '\r' && buf[i+1] == '\n' {
165 buf = buf[i+2:]
166 break
167 } else {
168 if buf[i] < '0' || buf[i] > '9' {
169 return "", fmt.Errorf("redis: can't parse push notification name: %q", buf)
170 }
171 }
172 }
173 }
174
175 if len(buf) < 2 {
176 return "", fmt.Errorf("redis: can't parse push notification name: %q", buf)
177 }
178 // keep only the notification name
179 for i := 0; i < len(buf)-1; i++ {
180 if buf[i] == '\r' && buf[i+1] == '\n' {
181 buf = buf[:i]
182 break
183 }
184 }
185
186 return util.BytesToString(buf), nil
187 }
188
189 // ReadLine Return a valid reply, it will check the protocol or redis error,
190 // and discard the attribute type.
191 func (r *Reader) ReadLine() ([]byte, error) {
192 line, err := r.readLine()
193 if err != nil {
194 return nil, err
195 }
196 switch line[0] {
197 case RespError:
198 return nil, ParseErrorReply(line)
199 case RespNil:
200 return nil, Nil
201 case RespBlobError:
202 var blobErr string
203 blobErr, err = r.readStringReply(line)
204 if err == nil {
205 err = parseTypedRedisError(blobErr)
206 }
207 return nil, err
208 case RespAttr:
209 if err = r.Discard(line); err != nil {
210 return nil, err
211 }
212 return r.ReadLine()
213 }
214
215 // Compatible with RESP2
216 if IsNilReply(line) {
217 return nil, Nil
218 }
219
220 return line, nil
221 }
222
223 // readLine returns an error if:
224 // - there is a pending read error;
225 // - or line does not end with \r\n.
226 func (r *Reader) readLine() ([]byte, error) {
227 b, err := r.rd.ReadSlice('\n')
228 if err != nil {
229 if err != bufio.ErrBufferFull {
230 return nil, err
231 }
232
233 full := make([]byte, len(b))
234 copy(full, b)
235
236 b, err = r.rd.ReadBytes('\n')
237 if err != nil {
238 return nil, err
239 }
240
241 full = append(full, b...) //nolint:makezero
242 b = full
243 }
244 if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
245 return nil, fmt.Errorf("redis: invalid reply: %q", b)
246 }
247 return b[:len(b)-2], nil
248 }
249
250 func (r *Reader) ReadReply() (interface{}, error) {
251 line, err := r.ReadLine()
252 if err != nil {
253 return nil, err
254 }
255
256 switch line[0] {
257 case RespStatus:
258 return string(line[1:]), nil
259 case RespInt:
260 return util.ParseInt(line[1:], 10, 64)
261 case RespFloat:
262 return r.readFloat(line)
263 case RespBool:
264 return r.readBool(line)
265 case RespBigInt:
266 return r.readBigInt(line)
267
268 case RespString:
269 return r.readStringReply(line)
270 case RespVerbatim:
271 return r.readVerb(line)
272
273 case RespArray, RespSet, RespPush:
274 return r.readSlice(line)
275 case RespMap:
276 return r.readMap(line)
277 }
278 return nil, fmt.Errorf("redis: can't parse %.100q", line)
279 }
280
281 func (r *Reader) readFloat(line []byte) (float64, error) {
282 v := string(line[1:])
283 switch string(line[1:]) {
284 case "inf":
285 return math.Inf(1), nil
286 case "-inf":
287 return math.Inf(-1), nil
288 case "nan", "-nan":
289 return math.NaN(), nil
290 }
291 return strconv.ParseFloat(v, 64)
292 }
293
294 func (r *Reader) readBool(line []byte) (bool, error) {
295 switch string(line[1:]) {
296 case "t":
297 return true, nil
298 case "f":
299 return false, nil
300 }
301 return false, fmt.Errorf("redis: can't parse bool reply: %q", line)
302 }
303
304 func (r *Reader) readBigInt(line []byte) (*big.Int, error) {
305 i := new(big.Int)
306 if i, ok := i.SetString(string(line[1:]), 10); ok {
307 return i, nil
308 }
309 return nil, fmt.Errorf("redis: can't parse bigInt reply: %q", line)
310 }
311
312 func (r *Reader) readStringReply(line []byte) (string, error) {
313 n, err := replyLen(line)
314 if err != nil {
315 return "", err
316 }
317
318 b := make([]byte, n+2)
319 _, err = io.ReadFull(r.rd, b)
320 if err != nil {
321 return "", err
322 }
323
324 return util.BytesToString(b[:n]), nil
325 }
326
327 func (r *Reader) readVerb(line []byte) (string, error) {
328 s, err := r.readStringReply(line)
329 if err != nil {
330 return "", err
331 }
332 if len(s) < 4 || s[3] != ':' {
333 return "", fmt.Errorf("redis: can't parse verbatim string reply: %q", line)
334 }
335 return s[4:], nil
336 }
337
338 func (r *Reader) readSlice(line []byte) ([]interface{}, error) {
339 n, err := replyLen(line)
340 if err != nil {
341 return nil, err
342 }
343
344 val := make([]interface{}, n)
345 for i := 0; i < len(val); i++ {
346 v, err := r.ReadReply()
347 if err != nil {
348 if err == Nil {
349 val[i] = nil
350 continue
351 }
352 if err, ok := err.(RedisError); ok {
353 val[i] = err
354 continue
355 }
356 return nil, err
357 }
358 val[i] = v
359 }
360 return val, nil
361 }
362
363 func (r *Reader) readMap(line []byte) (map[interface{}]interface{}, error) {
364 n, err := replyLen(line)
365 if err != nil {
366 return nil, err
367 }
368 m := make(map[interface{}]interface{}, n)
369 for i := 0; i < n; i++ {
370 k, err := r.ReadReply()
371 if err != nil {
372 return nil, err
373 }
374 v, err := r.ReadReply()
375 if err != nil {
376 if err == Nil {
377 m[k] = nil
378 continue
379 }
380 if err, ok := err.(RedisError); ok {
381 m[k] = err
382 continue
383 }
384 return nil, err
385 }
386 m[k] = v
387 }
388 return m, nil
389 }
390
391 // -------------------------------
392
393 func (r *Reader) ReadInt() (int64, error) {
394 line, err := r.ReadLine()
395 if err != nil {
396 return 0, err
397 }
398 switch line[0] {
399 case RespInt, RespStatus:
400 return util.ParseInt(line[1:], 10, 64)
401 case RespString:
402 s, err := r.readStringReply(line)
403 if err != nil {
404 return 0, err
405 }
406 return util.ParseInt([]byte(s), 10, 64)
407 case RespBigInt:
408 b, err := r.readBigInt(line)
409 if err != nil {
410 return 0, err
411 }
412 if !b.IsInt64() {
413 return 0, fmt.Errorf("bigInt(%s) value out of range", b.String())
414 }
415 return b.Int64(), nil
416 }
417 return 0, fmt.Errorf("redis: can't parse int reply: %.100q", line)
418 }
419
420 func (r *Reader) ReadUint() (uint64, error) {
421 line, err := r.ReadLine()
422 if err != nil {
423 return 0, err
424 }
425 switch line[0] {
426 case RespInt, RespStatus:
427 return util.ParseUint(line[1:], 10, 64)
428 case RespString:
429 s, err := r.readStringReply(line)
430 if err != nil {
431 return 0, err
432 }
433 return util.ParseUint([]byte(s), 10, 64)
434 case RespBigInt:
435 b, err := r.readBigInt(line)
436 if err != nil {
437 return 0, err
438 }
439 if !b.IsUint64() {
440 return 0, fmt.Errorf("bigInt(%s) value out of range", b.String())
441 }
442 return b.Uint64(), nil
443 }
444 return 0, fmt.Errorf("redis: can't parse uint reply: %.100q", line)
445 }
446
447 func (r *Reader) ReadFloat() (float64, error) {
448 line, err := r.ReadLine()
449 if err != nil {
450 return 0, err
451 }
452 switch line[0] {
453 case RespFloat:
454 return r.readFloat(line)
455 case RespStatus:
456 return strconv.ParseFloat(string(line[1:]), 64)
457 case RespString:
458 s, err := r.readStringReply(line)
459 if err != nil {
460 return 0, err
461 }
462 return strconv.ParseFloat(s, 64)
463 }
464 return 0, fmt.Errorf("redis: can't parse float reply: %.100q", line)
465 }
466
467 func (r *Reader) ReadString() (string, error) {
468 line, err := r.ReadLine()
469 if err != nil {
470 return "", err
471 }
472
473 switch line[0] {
474 case RespStatus, RespInt, RespFloat:
475 return string(line[1:]), nil
476 case RespString:
477 return r.readStringReply(line)
478 case RespBool:
479 b, err := r.readBool(line)
480 return strconv.FormatBool(b), err
481 case RespVerbatim:
482 return r.readVerb(line)
483 case RespBigInt:
484 b, err := r.readBigInt(line)
485 if err != nil {
486 return "", err
487 }
488 return b.String(), nil
489 }
490 return "", fmt.Errorf("redis: can't parse reply=%.100q reading string", line)
491 }
492
493 func (r *Reader) ReadBool() (bool, error) {
494 s, err := r.ReadString()
495 if err != nil {
496 return false, err
497 }
498 return s == "OK" || s == "1" || s == "true", nil
499 }
500
501 func (r *Reader) ReadSlice() ([]interface{}, error) {
502 line, err := r.ReadLine()
503 if err != nil {
504 return nil, err
505 }
506 return r.readSlice(line)
507 }
508
509 // ReadFixedArrayLen read fixed array length.
510 func (r *Reader) ReadFixedArrayLen(fixedLen int) error {
511 n, err := r.ReadArrayLen()
512 if err != nil {
513 return err
514 }
515 if n != fixedLen {
516 return fmt.Errorf("redis: got %d elements in the array, wanted %d", n, fixedLen)
517 }
518 return nil
519 }
520
521 // ReadArrayLen Read and return the length of the array.
522 func (r *Reader) ReadArrayLen() (int, error) {
523 line, err := r.ReadLine()
524 if err != nil {
525 return 0, err
526 }
527 switch line[0] {
528 case RespArray, RespSet, RespPush:
529 return replyLen(line)
530 default:
531 return 0, fmt.Errorf("redis: can't parse array/set/push reply: %.100q", line)
532 }
533 }
534
535 // ReadFixedMapLen reads fixed map length.
536 func (r *Reader) ReadFixedMapLen(fixedLen int) error {
537 n, err := r.ReadMapLen()
538 if err != nil {
539 return err
540 }
541 if n != fixedLen {
542 return fmt.Errorf("redis: got %d elements in the map, wanted %d", n, fixedLen)
543 }
544 return nil
545 }
546
547 // ReadMapLen reads the length of the map type.
548 // If responding to the array type (RespArray/RespSet/RespPush),
549 // it must be a multiple of 2 and return n/2.
550 // Other types will return an error.
551 func (r *Reader) ReadMapLen() (int, error) {
552 line, err := r.ReadLine()
553 if err != nil {
554 return 0, err
555 }
556 switch line[0] {
557 case RespMap:
558 return replyLen(line)
559 case RespArray, RespSet, RespPush:
560 // Some commands and RESP2 protocol may respond to array types.
561 n, err := replyLen(line)
562 if err != nil {
563 return 0, err
564 }
565 if n%2 != 0 {
566 return 0, fmt.Errorf("redis: the length of the array must be a multiple of 2, got: %d", n)
567 }
568 return n / 2, nil
569 default:
570 return 0, fmt.Errorf("redis: can't parse map reply: %.100q", line)
571 }
572 }
573
574 // DiscardNext read and discard the data represented by the next line.
575 func (r *Reader) DiscardNext() error {
576 line, err := r.readLine()
577 if err != nil {
578 return err
579 }
580 return r.Discard(line)
581 }
582
583 // Discard the data represented by line.
584 func (r *Reader) Discard(line []byte) (err error) {
585 if len(line) == 0 {
586 return errors.New("redis: invalid line")
587 }
588 switch line[0] {
589 case RespStatus, RespError, RespInt, RespNil, RespFloat, RespBool, RespBigInt:
590 return nil
591 }
592
593 n, err := replyLen(line)
594 if err != nil && err != Nil {
595 return err
596 }
597
598 switch line[0] {
599 case RespBlobError, RespString, RespVerbatim:
600 // +\r\n
601 _, err = r.rd.Discard(n + 2)
602 return err
603 case RespArray, RespSet, RespPush:
604 for i := 0; i < n; i++ {
605 if err = r.DiscardNext(); err != nil {
606 return err
607 }
608 }
609 return nil
610 case RespMap, RespAttr:
611 // Read key & value.
612 for i := 0; i < n*2; i++ {
613 if err = r.DiscardNext(); err != nil {
614 return err
615 }
616 }
617 return nil
618 }
619
620 return fmt.Errorf("redis: can't parse %.100q", line)
621 }
622
623 func replyLen(line []byte) (n int, err error) {
624 n, err = util.Atoi(line[1:])
625 if err != nil {
626 return 0, err
627 }
628
629 if n < -1 {
630 return 0, fmt.Errorf("redis: invalid reply: %q", line)
631 }
632
633 switch line[0] {
634 case RespString, RespVerbatim, RespBlobError,
635 RespArray, RespSet, RespPush, RespMap, RespAttr:
636 if n == -1 {
637 return 0, Nil
638 }
639 }
640 return n, nil
641 }
642
643 // IsNilReply detects redis.Nil of RESP2.
644 func IsNilReply(line []byte) bool {
645 return len(line) == 3 &&
646 (line[0] == RespString || line[0] == RespArray) &&
647 line[1] == '-' && line[2] == '1'
648 }