Skip to content

Commit

Permalink
fix incorrectly compute method and path when handling the two-word co…
Browse files Browse the repository at this point in the history
…mmand
  • Loading branch information
mazhechao committed Dec 10, 2019
1 parent 659fe8e commit c54e746
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 15 additions & 4 deletions packetbeat/protos/redis/redis_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,22 @@ func (p *parser) parseArray(depth int, buf *streambuf.Buffer) (common.NetString,
}

// handle top-level request command
if depth == 0 && (isRedisCommand(content[0]) || (count > 1 && isRedisCommand(bytes.Join(content[0:2], []byte(" "))))) {
var oneWordCommand, twoWordsCommand bool
oneWordCommand = isRedisCommand(content[0])
twoWordsCommand = count > 1 && isRedisCommand(bytes.Join(content[0:2], []byte(" ")))

if depth == 0 && (oneWordCommand || twoWordsCommand) {
p.message.isRequest = true
p.message.method = content[0]
if len(content) > 1 {
p.message.path = content[1]
if oneWordCommand {
p.message.method = content[0]
if len(content) > 1 {
p.message.path = content[1]
}
} else if twoWordsCommand {
p.message.method = bytes.Join(content[0:2], []byte(" "))
if len(content) > 2 {
p.message.path = content[2]
}
}

var value common.NetString
Expand Down
6 changes: 5 additions & 1 deletion packetbeat/protos/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ func TestRedisParser_ArrayRequest(t *testing.T) {
assert.True(t, complete)
assert.True(t, msg.isRequest)
assert.Equal(t, "SET key1 Hello", string(msg.message))
assert.Equal(t, "SET", string(msg.method))
assert.Equal(t, "key1", string(msg.path))
assert.Equal(t, len(arrayRequest), msg.size)
}

var arrayRequest2 = []byte("*3\r\n" +
"$6\r\n" +
"$6\r\n" +
"CONFIG\r\n" +
"$3\r\n" +
"GET\r\n" +
Expand All @@ -85,6 +87,8 @@ func TestRedisParser_ArrayRequest2(t *testing.T) {
assert.True(t, complete)
assert.True(t, msg.isRequest)
assert.Equal(t, "CONFIG GET *", string(msg.message))
assert.Equal(t, "CONFIG GET", string(msg.method))
assert.Equal(t, "*", string(msg.path))
assert.Equal(t, len(arrayRequest2), msg.size)
}

Expand Down

0 comments on commit c54e746

Please sign in to comment.