summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure')
-rwxr-xr-xlib/pure/json.nim29
-rw-r--r--lib/pure/redis.nim8
-rwxr-xr-xlib/pure/scgi.nim4
-rwxr-xr-xlib/pure/sockets.nim28
4 files changed, 53 insertions, 16 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index efadf030c..7b2707784 100755
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -606,6 +606,35 @@ proc `[]=`*(obj: PJsonNode, key: String, val: PJsonNode) =
       return
   obj.fields.add((key, val))
 
+proc delete*(obj: PJsonNode, key: string) =
+  assert(obj.kind == JObject)
+  for i in 0..obj.fields.len-1:
+    if obj.fields[i].key == key:
+      obj.fields.delete(i)
+      return
+  raise newException(EInvalidIndex, "key not in object")
+
+proc copy*(p: PJsonNode): PJsonNode =
+  case p.kind
+  of JString:
+    result = newJString(p.str)
+  of JInt:
+    result = newJInt(p.num)
+  of JFloat:
+    result = newJFloat(p.fnum)
+  of JBool:
+    result = newJBool(p.bval)
+  of JNull:
+    result = newJNull()
+  of JObject:
+    result = newJObject()
+    for key, field in items(p.fields):
+      result.fields.add((key, copy(field)))
+  of JArray:
+    result = newJArray()
+    for i in items(p.elems):
+      result.elems.add(copy(i))
+
 # ------------- pretty printing ----------------------------------------------
 
 proc indent(s: var string, i: int) = 
diff --git a/lib/pure/redis.nim b/lib/pure/redis.nim
index 1d25f1a5d..e91fdc832 100644
--- a/lib/pure/redis.nim
+++ b/lib/pure/redis.nim
@@ -338,15 +338,15 @@ proc hMSet*(r: TRedis, key: string,
   r.sendCommand("HMSET", args)
   raiseNoOK(r.parseStatus())
 
-proc hSet*(r: TRedis, key, field, value: string) =
+proc hSet*(r: TRedis, key, field, value: string): TRedisInteger =
   ## Set the string value of a hash field
   r.sendCommand("HSET", key, field, value)
-  raiseNoOK(r.parseStatus())
+  return r.parseInteger()
   
-proc hSetNX*(r: TRedis, key, field, value: string) =
+proc hSetNX*(r: TRedis, key, field, value: string): TRedisInteger =
   ## Set the value of a hash field, only if the field does **not** exist
   r.sendCommand("HSETNX", key, field, value)
-  raiseNoOK(r.parseStatus())
+  return r.parseInteger()
 
 proc hVals*(r: TRedis, key: string): TRedisList =
   ## Get all the values in a hash
diff --git a/lib/pure/scgi.nim b/lib/pure/scgi.nim
index 1326d9795..fa86cbaf2 100755
--- a/lib/pure/scgi.nim
+++ b/lib/pure/scgi.nim
@@ -73,7 +73,7 @@ proc recvBuffer(s: var TScgiState, L: int) =
     scgiError("could not read all data")
   setLen(s.input, L)
   
-proc open*(s: var TScgiState, port = TPort(4000)) = 
+proc open*(s: var TScgiState, port = TPort(4000), address = "127.0.0.1") = 
   ## opens a connection.
   s.bufLen = 4000
   s.input = newString(s.buflen) # will be reused
@@ -81,7 +81,7 @@ proc open*(s: var TScgiState, port = TPort(4000)) =
   s.server = socket()
   if s.server == InvalidSocket: scgiError("could not open socket")
   #s.server.connect(connectionName, port)
-  bindAddr(s.server, port)
+  bindAddr(s.server, port, address)
   listen(s.server)
   
 proc close*(s: var TScgiState) = 
diff --git a/lib/pure/sockets.nim b/lib/pure/sockets.nim
index 45e4da118..73f57395a 100755
--- a/lib/pure/sockets.nim
+++ b/lib/pure/sockets.nim
@@ -179,19 +179,27 @@ proc bindAddr*(socket: TSocket, port = TPort(0), address = "") =
   ## binds an address/port number to a socket.
   ## Use address string in dotted decimal form like "a.b.c.d"
   ## or leave "" for any address.
-  var name: Tsockaddr_in
-  when defined(Windows):
-    name.sin_family = int16(ord(AF_INET))
-  else:
-    name.sin_family = posix.AF_INET
-  name.sin_port = sockets.htons(int16(port))
+
   if address == "":
+    var name: Tsockaddr_in
+    when defined(Windows):
+      name.sin_family = int16(ord(AF_INET))
+    else:
+      name.sin_family = posix.AF_INET
+    name.sin_port = sockets.htons(int16(port))
     name.sin_addr.s_addr = sockets.htonl(INADDR_ANY)
+    if bindSocket(cint(socket), cast[ptr TSockAddr](addr(name)),
+                  sizeof(name)) < 0'i32:
+      OSError()
   else:
-    name.sin_addr.s_addr = parseIp4(address)
-  if bindSocket(cint(socket), cast[ptr TSockAddr](addr(name)),
-                sizeof(name)) < 0'i32:
-    OSError()
+    var hints: TAddrInfo
+    var aiList: ptr TAddrInfo = nil
+    hints.ai_family = toInt(AF_INET)
+    hints.ai_socktype = toInt(SOCK_STREAM)
+    hints.ai_protocol = toInt(IPPROTO_TCP)
+    if getAddrInfo(address, $port, addr(hints), aiList) != 0'i32: OSError()
+    if bindSocket(cint(socket), aiList.ai_addr, aiList.ai_addrLen) < 0'i32:
+      OSError()
 
 when false:
   proc bindAddr*(socket: TSocket, port = TPort(0)) =