summary refs log tree commit diff stats
path: root/lib/pure/redis.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/redis.nim')
-rwxr-xr-xlib/pure/redis.nim50
1 files changed, 31 insertions, 19 deletions
diff --git a/lib/pure/redis.nim b/lib/pure/redis.nim
index 921f9a3d8..bb6ea6768 100755
--- a/lib/pure/redis.nim
+++ b/lib/pure/redis.nim
@@ -49,26 +49,35 @@ proc raiseNoOK(status: string) =
     raise newException(EInvalidReply, "Expected \"OK\" got \"$1\"" % status)
 
 proc parseStatus(r: TRedis): TRedisStatus =
-  var line = r.socket.recv.string
-  
-  if line[0] == '-':
-    raise newException(ERedis, strip(line))
-  if line[0] != '+':
-    raiseInvalidReply('+', line[0])
+  var line = ""
+  if r.socket.recvLine(line):
+    if line == "":
+      raise newException(ERedis, "Server closed connection prematurely")
   
-  return line.substr(1, line.len-3) # Strip '+' and \c\L.
+    if line[0] == '-':
+      raise newException(ERedis, strip(line))
+    if line[0] != '+':
+      raiseInvalidReply('+', line[0])
+    
+    return line.substr(1) # Strip '+'
+  else:
+    OSError()
   
 proc parseInteger(r: TRedis): TRedisInteger =
-  var line = r.socket.recv.string
-
-  if line[0] == '-':
-    raise newException(ERedis, strip(line))
-  if line[0] != ':':
-    raiseInvalidReply(':', line[0])
-  
-  # Strip ':' and \c\L.
-  if parseBiggestInt(line, result, 1) == 0:
-    raise newException(EInvalidReply, "Unable to parse integer.") 
+  var line = ""
+  if r.socket.recvLine(line):
+    if line == "":
+      raise newException(ERedis, "Server closed connection prematurely")
+
+    if line[0] == '-':
+      raise newException(ERedis, strip(line))
+    if line[0] != ':':
+      raiseInvalidReply(':', line[0])
+    
+    # Strip ':'
+    if parseBiggestInt(line, result, 1) == 0:
+      raise newException(EInvalidReply, "Unable to parse integer.") 
+  else: OSError()
 
 proc recv(sock: TSocket, size: int): TaintedString =
   result = newString(size).TaintedString
@@ -838,8 +847,11 @@ proc save*(r: TRedis) =
 proc shutdown*(r: TRedis) =
   ## Synchronously save the dataset to disk and then shut down the server
   r.sendCommand("SHUTDOWN")
-  var s = r.socket.recv()
-  if s.string.len != 0: raise newException(ERedis, s.string)
+  var s = "".TaintedString
+  if r.socket.recvLine(s):
+    if s.string.len != 0: raise newException(ERedis, s.string)
+  else:
+    OSError()
 
 proc slaveof*(r: TRedis, host: string, port: string) =
   ## Make the server a slave of another instance, or promote it as master