Permalink
Browse files

Fixes against keys containing special characters

Thanks to Enric Lafont for a bug report
  • Loading branch information...
kklis committed Oct 5, 2011
1 parent 0acf676 commit dd5e8579574ee749da77eedb201e67d516893dea
Showing with 12 additions and 11 deletions.
  1. +7 −6 memcache.go
  2. +5 −5 memcache_test.go
View
@@ -34,7 +34,6 @@ import (
"bufio"
"net"
"os"
- "regexp"
"strconv"
"strings"
)
@@ -81,18 +80,20 @@ func (memc *Memcache) Get(key string) (value []byte, flags int, err os.Error) {
}
reader := bufio.NewReader(memc.conn)
line, err := reader.ReadString('\n')
- re, _ := regexp.Compile("VALUE " + key + " ([0-9]+) ([0-9]+)")
- a := re.FindStringSubmatch(line)
- if len(a) != 3 {
+ if err != nil {
+ return
+ }
+ a := strings.Split(strings.TrimSpace(line), " ")
+ if len(a) != 4 || a[0] != "VALUE" || a[1] != key {
if line == "END\r\n" {
err = NotFoundError
} else {
err = ReadError
}
return
}
- flags, _ = strconv.Atoi(a[1])
- l, _ := strconv.Atoi(a[2])
+ flags, _ = strconv.Atoi(a[2])
+ l, _ := strconv.Atoi(a[3])
value = make([]byte, l)
n := 0
for {
View
@@ -1,5 +1,5 @@
package memcache
-
+
import (
"strconv"
"testing"
@@ -15,7 +15,7 @@ func TestClient(t *testing.T) {
t.Error(err.String())
}
// clean
- memc.Delete("foo")
+ memc.Delete(key)
// test add
err = memc.Add(key, []uint8(value), flags, 0)
if err != nil {
@@ -41,11 +41,11 @@ func TestClient(t *testing.T) {
}
testGet(t, memc, value + value + value)
// test delete
- err = memc.Delete("foo")
+ err = memc.Delete(key)
if err != nil {
t.Error(err.String())
}
- _, _, err = memc.Get("foo")
+ _, _, err = memc.Get(key)
if err == nil {
t.Error("Data not removed from memcache")
}
@@ -72,7 +72,7 @@ func TestClient(t *testing.T) {
}
func testGet(t *testing.T, memc *Memcache, s string) {
- val, fl, err := memc.Get("foo")
+ val, fl, err := memc.Get(key)
if err != nil {
t.Error(err.String())
}

0 comments on commit dd5e857

Please sign in to comment.