removed some waffling in bulkstring

master
Jordan Orelli 11 years ago
parent fa895b37d6
commit 5904986e1b

@ -42,7 +42,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if !isOK(v) { if !isOK(v) {
fmt.Printf("not OK: %v\n", v) fmt.Printf("auth not OK: %q\n", v)
os.Exit(1) os.Exit(1)
} }
} }

@ -42,7 +42,7 @@ func isOK(v value) bool {
if !ok { if !ok {
return false return false
} }
return string(vv) == "OK" return string(vv) == "+OK\r\n"
} }
func getBytes(v value) []byte { func getBytes(v value) []byte {
@ -110,8 +110,7 @@ func readValue(r io.Reader) (value, error) {
case start_integer: case start_integer:
return IntVal(line), nil return IntVal(line), nil
case start_bulkstring: case start_bulkstring:
line = line[:len(line)-2] return readBulkString(line, br)
return readBulkString(line[1:], br)
case start_array: case start_array:
line = line[:len(line)-2] line = line[:len(line)-2]
return readArray(line[1:], br) return readArray(line[1:], br)
@ -174,10 +173,23 @@ func Int(i int) value {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
type BulkString []byte type BulkStringVal []byte
func BulkString(s string) value {
l := strconv.Itoa(len(s))
b := make(BulkStringVal, len(l)+len(s)+5)
b[0] = '$'
copy(b[1:], l)
b[len(l)+1] = '\r'
b[len(l)+2] = '\n'
copy(b[len(l)+3:], s)
b[len(b)-2] = '\r'
b[len(b)-1] = '\n'
return b
}
func readBulkString(prefix []byte, r io.Reader) (value, error) { func readBulkString(prefix []byte, r io.Reader) (value, error) {
n, err := strconv.ParseInt(string(prefix), 10, 64) n, err := strconv.Atoi(string(prefix[1 : len(prefix)-2]))
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to read bulkstring in redis protocol: bad prefix: %v", err) return nil, fmt.Errorf("unable to read bulkstring in redis protocol: bad prefix: %v", err)
} }
@ -189,9 +201,9 @@ func readBulkString(prefix []byte, r io.Reader) (value, error) {
return nil, fmt.Errorf("redis protocol error: illegal bulk string of negative length %d", n) return nil, fmt.Errorf("redis protocol error: illegal bulk string of negative length %d", n)
} }
n += 2 b := make(BulkStringVal, len(prefix)+n+2)
b := make(BulkString, n) copy(b, prefix)
n_read, err := io.ReadFull(r, b) n_read, err := io.ReadFull(r, b[len(prefix):])
switch err { switch err {
case io.EOF: case io.EOF:
fmt.Printf("saw eof after %d bytes looking for %d bytes in bulkstring\n", n_read, n) fmt.Printf("saw eof after %d bytes looking for %d bytes in bulkstring\n", n_read, n)
@ -202,16 +214,11 @@ func readBulkString(prefix []byte, r io.Reader) (value, error) {
return nil, fmt.Errorf("unable to read bulkstring in redis protocol: error on read: %v", err) return nil, fmt.Errorf("unable to read bulkstring in redis protocol: error on read: %v", err)
} }
return b[:len(b)-2], nil return b, nil
} }
func (s BulkString) Write(w io.Writer) (int, error) { func (s BulkStringVal) Write(w io.Writer) (int, error) {
w.Write([]byte{'$'}) return w.Write(s)
w.Write([]byte(strconv.Itoa(len(s))))
w.Write([]byte{'\r', '\n'})
w.Write(s)
w.Write([]byte{'\r', '\n'})
return 0, nil
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------

Loading…
Cancel
Save