| 1 |
// Package resp implements an efficient decoder for the Redis Serialization Protocol (RESP). |
| 2 |
// |
| 3 |
// See http://redis.io/topics/protocol for the reference. |
| 4 |
package resp |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
// ErrInvalidPrefix is returned if the data contains an unrecognized prefix. |
| 15 |
ErrInvalidPrefix = errors.New("resp: invalid prefix") |
| 16 |
|
| 17 |
// ErrMissingCRLF is returned if a \r\n is missing in the data slice. |
| 18 |
ErrMissingCRLF = errors.New("resp: missing CRLF") |
| 19 |
|
| 20 |
// ErrInvalidInteger is returned if an invalid character is found while parsing an integer. |
| 21 |
ErrInvalidInteger = errors.New("resp: invalid integer character") |
| 22 |
|
| 23 |
// ErrInvalidBulkString is returned if the bulk string data cannot be decoded. |
| 24 |
ErrInvalidBulkString = errors.New("resp: invalid bulk string") |
| 25 |
|
| 26 |
// ErrInvalidArray is returned if the array data cannot be decoded. |
| 27 |
ErrInvalidArray = errors.New("resp: invalid array") |
| 28 |
|
| 29 |
// ErrNotAnArray is returned if the DecodeRequest function is called and |
| 30 |
// the decoded value is not an array. |
| 31 |
ErrNotAnArray = errors.New("resp: expected an array type") |
| 32 |
|
| 33 |
// ErrInvalidRequest is returned if the DecodeRequest function is called and |
| 34 |
// the decoded value is not an array containing only bulk strings, and at least 1 element. |
| 35 |
ErrInvalidRequest = errors.New("resp: invalid request, must be an array of bulk strings with at least one element") |
| 36 |
) |
| 37 |
|
| 38 |
// BytesReader defines the methods required for the Decode* family of methods. |
| 39 |
// Notably, a *bufio.Reader and a *bytes.Buffer both satisfy this interface. |
| 40 |
type BytesReader interface { |
| 41 |
io.Reader |
| 42 |
io.ByteReader |
| 43 |
ReadBytes(byte) ([]byte, error) |
| 44 |
} |
| 45 |
|
| 46 |
// Array represents an array of values, as defined by the RESP. |
| 47 |
type Array []interface{} |
| 48 |
|
| 49 |
// String is the Stringer implementation for the Array. |
| 50 |
func (a Array) String() string { |
| 51 |
var buf bytes.Buffer |
| 52 |
for i, v := range a { |
| 53 |
buf.WriteString(fmt.Sprintf("[%2d] %[2]v (%[2]T)\n", i, v)) |
| 54 |
} |
| 55 |
return buf.String() |
| 56 |
} |
| 57 |
|
| 58 |
// DecodeRequest decodes the provided byte slice and returns the array |
| 59 |
// representing the request. If the encoded value is not an array, it |
| 60 |
// returns ErrNotAnArray, and if it is not a valid request, it returns ErrInvalidRequest. |
| 61 |
func DecodeRequest(r BytesReader) ([]string, error) { |
| 62 |
// Decode the value |
| 63 |
val, err := Decode(r) |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
|
| 68 |
// Must be an array |
| 69 |
ar, ok := val.(Array) |
| 70 |
if !ok { |
| 71 |
return nil, ErrNotAnArray |
| 72 |
} |
| 73 |
|
| 74 |
// Must have at least one element |
| 75 |
if len(ar) < 1 { |
| 76 |
return nil, ErrInvalidRequest |
| 77 |
} |
| 78 |
|
| 79 |
// Must have only strings |
| 80 |
strs := make([]string, len(ar)) |
| 81 |
for i, v := range ar { |
| 82 |
v, ok := v.(string) |
| 83 |
if !ok { |
| 84 |
return nil, ErrInvalidRequest |
| 85 |
} |
| 86 |
strs[i] = v |
| 87 |
} |
| 88 |
return strs, nil |
| 89 |
} |
| 90 |
|
| 91 |
// Decode decodes the provided byte slice and returns the parsed value. |
| 92 |
func Decode(r BytesReader) (interface{}, error) { |
| 93 |
return decodeValue(r) |
| 94 |
} |
| 95 |
|
| 96 |
// decodeValue parses the byte slice and decodes the value based on its |
| 97 |
// prefix, as defined by the RESP protocol. |
| 98 |
func decodeValue(r BytesReader) (interface{}, error) { |
| 99 |
ch, err := r.ReadByte() |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
var val interface{} |
| 105 |
switch ch { |
| 106 |
case '+': |
| 107 |
// Simple string |
| 108 |
val, err = decodeSimpleString(r) |
| 109 |
case '-': |
| 110 |
// Error |
| 111 |
val, err = decodeError(r) |
| 112 |
case ':': |
| 113 |
// Integer |
| 114 |
val, err = decodeInteger(r) |
| 115 |
case '$': |
| 116 |
// Bulk string |
| 117 |
val, err = decodeBulkString(r) |
| 118 |
case '*': |
| 119 |
// Array |
| 120 |
val, err = decodeArray(r) |
| 121 |
default: |
| 122 |
err = ErrInvalidPrefix |
| 123 |
} |
| 124 |
|
| 125 |
return val, err |
| 126 |
} |
| 127 |
|
| 128 |
// decodeArray decodes the byte slice as an array. It assumes the |
| 129 |
// '*' prefix is already consumed. |
| 130 |
func decodeArray(r BytesReader) (Array, error) { |
| 131 |
// First comes the number of elements in the array |
| 132 |
cnt, err := decodeInteger(r) |
| 133 |
if err != nil { |
| 134 |
return nil, err |
| 135 |
} |
| 136 |
switch { |
| 137 |
case cnt == -1: |
| 138 |
// Nil array |
| 139 |
return nil, nil |
| 140 |
|
| 141 |
case cnt == 0: |
| 142 |
// Empty, but allocated, array |
| 143 |
return Array{}, nil |
| 144 |
|
| 145 |
case cnt < 0: |
| 146 |
// Invalid length |
| 147 |
return nil, ErrInvalidArray |
| 148 |
|
| 149 |
default: |
| 150 |
// Allocate the array |
| 151 |
ar := make(Array, cnt) |
| 152 |
|
| 153 |
// Decode each value |
| 154 |
for i := 0; i < int(cnt); i++ { |
| 155 |
val, err := decodeValue(r) |
| 156 |
if err != nil { |
| 157 |
return nil, err |
| 158 |
} |
| 159 |
ar[i] = val |
| 160 |
} |
| 161 |
return ar, nil |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// decodeBulkString decodes the byte slice as a binary-safe string. The |
| 166 |
// '$' prefix is assumed to be already consumed. |
| 167 |
func decodeBulkString(r BytesReader) (interface{}, error) { |
| 168 |
// First comes the length of the bulk string, an integer |
| 169 |
cnt, err := decodeInteger(r) |
| 170 |
if err != nil { |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
switch { |
| 174 |
case cnt == -1: |
| 175 |
// Special case to represent a nil bulk string |
| 176 |
return nil, nil |
| 177 |
|
| 178 |
case cnt < -1: |
| 179 |
return nil, ErrInvalidBulkString |
| 180 |
|
| 181 |
default: |
| 182 |
// Then the string is cnt long, and bytes read is cnt+n+2 (for ending CRLF) |
| 183 |
need := cnt + 2 |
| 184 |
got := 0 |
| 185 |
buf := make([]byte, need) |
| 186 |
for { |
| 187 |
nb, err := r.Read(buf[got:]) |
| 188 |
if err != nil { |
| 189 |
return nil, err |
| 190 |
} |
| 191 |
got += nb |
| 192 |
if int64(got) == need { |
| 193 |
break |
| 194 |
} |
| 195 |
} |
| 196 |
return string(buf[:got-2]), err |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// decodeInteger decodes the byte slice as a singed 64bit integer. The |
| 201 |
// ':' prefix is assumed to be already consumed. |
| 202 |
func decodeInteger(r BytesReader) (val int64, err error) { |
| 203 |
var cr bool |
| 204 |
var sign int64 = 1 |
| 205 |
var n int |
| 206 |
|
| 207 |
loop: |
| 208 |
for { |
| 209 |
ch, err := r.ReadByte() |
| 210 |
if err != nil { |
| 211 |
return 0, err |
| 212 |
} |
| 213 |
n++ |
| 214 |
|
| 215 |
switch ch { |
| 216 |
case '\r': |
| 217 |
cr = true |
| 218 |
break loop |
| 219 |
|
| 220 |
case '\n': |
| 221 |
break loop |
| 222 |
|
| 223 |
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 224 |
val = val*10 + int64(ch-'0') |
| 225 |
|
| 226 |
case '-': |
| 227 |
if n == 1 { |
| 228 |
sign = -1 |
| 229 |
continue |
| 230 |
} |
| 231 |
fallthrough |
| 232 |
default: |
| 233 |
return 0, ErrInvalidInteger |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if !cr { |
| 238 |
return 0, ErrMissingCRLF |
| 239 |
} |
| 240 |
// Presume next byte was \n |
| 241 |
_, err = r.ReadByte() |
| 242 |
if err != nil { |
| 243 |
return 0, err |
| 244 |
} |
| 245 |
return sign * val, nil |
| 246 |
} |
| 247 |
|
| 248 |
// decodeSimpleString decodes the byte slice as a SimpleString. The |
| 249 |
// '+' prefix is assumed to be already consumed. |
| 250 |
func decodeSimpleString(r BytesReader) (interface{}, error) { |
| 251 |
v, err := r.ReadBytes('\r') |
| 252 |
if err != nil { |
| 253 |
return nil, err |
| 254 |
} |
| 255 |
// Presume next byte was \n |
| 256 |
_, err = r.ReadByte() |
| 257 |
if err != nil { |
| 258 |
return nil, err |
| 259 |
} |
| 260 |
return string(v[:len(v)-1]), nil |
| 261 |
} |
| 262 |
|
| 263 |
// decodeError decodes the byte slice as an Error. The '-' prefix |
| 264 |
// is assumed to be already consumed. |
| 265 |
func decodeError(r BytesReader) (interface{}, error) { |
| 266 |
return decodeSimpleString(r) |
| 267 |
} |