-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
245 lines (237 loc) · 5.13 KB
/
command.go
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
package resp
import (
"bytes"
"io"
)
// CommandReader is a reader you can use to read from a socket in a way
// similar to what the Redis server does when listening for commands.
type CommandReader struct {
r io.Reader
buf *bytes.Buffer
}
// NewCommandReader creates a new CommandReader from any io.Reader.
func NewCommandReader(r io.Reader) *CommandReader {
return &CommandReader{
r: r,
buf: new(bytes.Buffer),
}
}
// Read reads a single command, and returns it in parts. The slices in return
// values data and parts are only valid until the next call to Read.
func (r *CommandReader) Read() (data []byte, parts [][]byte, err error) {
var length int
for err == nil {
length, parts, err = parseCommand(r.buf.Bytes())
if err == errIncomplete {
err = readmore(r.r, r.buf)
} else if err == nil {
break
}
}
data = r.buf.Next(length)
return
}
// parseCommand parses a Redis Serialisation Protocol Command, returning the
// length of the first Command that is present in data, and a slice parts of
// the interpreted command. The slices in slice parts can be subslices of
// slice data.
func parseCommand(data []byte) (length int, parts [][]byte, err error) {
if len(data) < 1 {
err = errIncomplete
return
}
if data[0] != '*' {
var pos int
for pos < len(data) {
newline := bytes.IndexByte(data[pos:], '\n')
if newline == -1 {
err = errIncomplete
return
}
blank := true
for _, c := range data[pos : pos+newline] {
if !isspace(c) {
blank = false
break
}
}
if !blank {
length = pos + newline + 1
break
}
pos += newline + 1
if pos >= len(data) {
err = errIncomplete
return
}
}
if pos != 0 {
length, parts, err = parseCommand(data[pos:])
length += pos
return
}
parts, err = parseInlineCommand(data[:length])
return
}
length++
var (
narg int
arglen int
)
arglen, narg, err = parseInteger(data[length:])
if err == errInvalidInt || narg > 1024*1024 {
err = errInvalidMultibulkLength
}
if err != nil {
return
}
length += arglen
for n := 0; n < narg; n++ {
var part []byte
arglen, part, err = parseCommandPart(data[length:])
if err != nil {
return
}
length += arglen
parts = append(parts, part)
}
return
}
func parseCommandPart(data []byte) (length int, part []byte, err error) {
if len(data) < 1 {
err = errIncomplete
return
}
if data[0] != '$' {
c := data[0]
if c == '\r' || c == '\n' {
// prevent newlines in error; invalid in protocol
c = ' '
}
err = Error("ERR Protocol error: expected '$', got '" + string(c) + "'")
return
}
length, part, err = parseBulkString(data[1:])
length++ // the '$' in front
if err == nil && part == nil {
err = errInvalidBulkLength
}
return
}
// parseInlineCommand takes exactly one line (with the final byte in data
// '\n'), and interprets it as an inline command.
func parseInlineCommand(data []byte) (parts [][]byte, err error) {
var pos int
// skip leading blanks
for pos < len(data)-1 && data[pos] != 0 && isspace(data[pos]) {
pos++
}
for pos < len(data)-1 {
var (
partLength int
part []byte
)
partLength, part, err = parseInlineCommandPart(data[pos:])
if err != nil {
return
}
pos += partLength
parts = append(parts, part)
// skip trailing blanks
for pos < len(data) && data[pos] != 0 && isspace(data[pos]) {
pos++
}
}
return
}
// parseInlineCommandPart parses a single Inline Command part from data,
// returning the length of the part in data and a parsed part.
func parseInlineCommandPart(data []byte) (length int, part []byte, err error) {
var (
pos int
done bool
inq bool // inside "quotes"
insq bool // inside 'single quotes'
)
for pos < len(data) && !done {
switch {
case inq:
switch data[pos] {
case '\\':
if len(data) > pos+3 &&
data[pos+1] == 'x' &&
ishexdigit(data[pos+2]) &&
ishexdigit(data[pos+3]) {
part = append(part, hextobyte(data[pos+2])<<4|
hextobyte(data[pos+3]))
pos += 3
} else if len(data) > pos+1 {
pos++
var c byte
switch data[pos] {
case 'n':
c = '\n'
case 'r':
c = '\r'
case 't':
c = '\t'
case 'b':
c = '\b'
case 'a':
c = '\a'
default:
c = data[pos]
}
part = append(part, c)
}
case '"':
if len(data) > pos+1 && !isspace(data[pos+1]) {
err = errUnbalancedQuotes
return
}
inq = false
case 0:
err = errUnbalancedQuotes
return
default:
part = append(part, data[pos])
}
case insq:
switch data[pos] {
case '\\':
if len(data) > pos+1 && data[pos+1] == '\'' {
pos++
part = append(part, '\'')
} else {
part = append(part, data[pos])
}
case '\'':
if len(data) > pos+1 && !isspace(data[pos+1]) {
err = errUnbalancedQuotes
return
}
insq = false
default:
part = append(part, data[pos])
}
default:
switch data[pos] {
case ' ', '\n', '\r', '\t', 0:
done = true
case '"':
inq = true
case '\'':
insq = true
default:
part = append(part, data[pos])
}
}
pos++
}
if inq || insq {
err = errUnbalancedQuotes
return
}
length = pos
return
}