summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2014-09-06 15:29:38 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2014-09-06 15:29:38 +0100
commitbd542ebea3ec01eb269fe6fa118a8648d63f7097 (patch)
treef0b963181ba9400d74bf26a2d1632c625ed94951 /lib/pure
parentcda457c865aaab854c7d59bd1117250f73c944fe (diff)
downloadNim-bd542ebea3ec01eb269fe6fa118a8648d63f7097.tar.gz
Fixes httpclient SSL issue. Implements unbuffered SSL recv. Ref #1487.
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/asyncdispatch.nim4
-rw-r--r--lib/pure/asyncnet.nim54
-rw-r--r--lib/pure/httpclient.nim3
3 files changed, 39 insertions, 22 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 052de6f3a..e521b8e64 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -449,6 +449,8 @@ when defined(windows) or defined(nimdoc):
     ## complete once all the data requested is read, a part of the data has been
     ## read, or the socket has disconnected in which case the future will
     ## complete with a value of ``""``.
+    ##
+    ## **Warning**: The ``Peek`` socket flag is not supported on Windows.
 
 
     # Things to note:
@@ -458,6 +460,8 @@ when defined(windows) or defined(nimdoc):
     #     '\0' in the message currently signifies a socket disconnect. Who
     #     knows what will happen when someone sends that to our socket.
     verifyPresence(socket)
+    assert SocketFlag.Peek notin flags, "Peek not supported on Windows."
+
     var retFuture = newFuture[string]("recv")
     var dataBuf: TWSABuf
     dataBuf.buf = cast[cstring](alloc0(size))
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim
index ea7432404..4539dfdcb 100644
--- a/lib/pure/asyncnet.nim
+++ b/lib/pure/asyncnet.nim
@@ -157,34 +157,43 @@ proc connect*(socket: PAsyncSocket, address: string, port: TPort,
       sslSetConnectState(socket.sslHandle)
       sslLoop(socket, flags, sslDoHandshake(socket.sslHandle))
 
-proc readIntoBuf(socket: PAsyncSocket,
-    flags: set[TSocketFlags]): Future[int] {.async.} =
-  var data = await recv(socket.fd.TAsyncFD, BufferSize, flags)
-  if data.len != 0:
-    copyMem(addr socket.buffer[0], addr data[0], data.len)
+proc readInto(buf: cstring, size: int, socket: PAsyncSocket,
+              flags: set[TSocketFlags]): Future[int] {.async.} =
   if socket.isSsl:
     when defined(ssl):
       # SSL mode.
-      let ret = bioWrite(socket.bioIn, addr socket.buffer[0], data.len.cint)
-      if ret < 0:
-        raiseSSLError()
       sslLoop(socket, flags,
-        sslRead(socket.sslHandle, addr socket.buffer[0], BufferSize.cint))
-      socket.currPos = 0
-      socket.bufLen = opResult # Injected from sslLoop template.
+        sslRead(socket.sslHandle, buf, size.cint))
       result = opResult
   else:
+    var data = await recv(socket.fd.TAsyncFD, size, flags)
+    if data.len != 0:
+      copyMem(buf, addr data[0], data.len)
     # Not in SSL mode.
-    socket.bufLen = data.len
-    socket.currPos = 0
     result = data.len
 
+proc readIntoBuf(socket: PAsyncSocket,
+    flags: set[TSocketFlags]): Future[int] {.async.} =
+  result = await readInto(addr socket.buffer[0], BufferSize, socket, flags)
+  socket.currPos = 0
+  socket.bufLen = result
+
 proc recv*(socket: PAsyncSocket, size: int,
            flags = {TSocketFlags.SafeDisconn}): Future[string] {.async.} =
-  ## Reads ``size`` bytes from ``socket``. Returned future will complete once
-  ## all of the requested data is read. If socket is disconnected during the
+  ## Reads **up to** ``size`` bytes from ``socket``.
+  ##
+  ## For buffered sockets this function will attempt to read all the requested
+  ## data. It will read this data in ``BufferSize`` chunks.
+  ##
+  ## For unbuffered sockets this function makes no effort to read
+  ## all the data requested. It will return as much data as the operating system
+  ## gives it.
+  ##
+  ## If socket is disconnected during the
   ## recv operation then the future may complete with only a part of the
-  ## requested data read. If socket is disconnected and no data is available
+  ## requested data.
+  ##
+  ## If socket is disconnected and no data is available
   ## to be read then the future will complete with a value of ``""``.
   if socket.isBuffered:
     result = newString(size)
@@ -216,7 +225,9 @@ proc recv*(socket: PAsyncSocket, size: int,
       socket.currPos = originalBufPos
     result.setLen(read)
   else:
-    result = await recv(socket.fd.TAsyncFD, size, flags)
+    result = newString(size)
+    let read = await readInto(addr result[0], size, socket, flags)
+    result.setLen(read)
 
 proc send*(socket: PAsyncSocket, data: string,
            flags = {TSocketFlags.SafeDisconn}) {.async.} =
@@ -282,6 +293,9 @@ proc recvLine*(socket: PAsyncSocket,
   ## The partial line **will be lost**.
   ##
   ## **Warning**: The ``Peek`` flag is not yet implemented.
+  ## 
+  ## **Warning**: ``recvLine`` on unbuffered sockets assumes that the protocol
+  ## uses ``\r\L`` to delimit a new line.
   template addNLIfEmpty(): stmt =
     if result.len == 0:
       result.add("\c\L")
@@ -324,10 +338,8 @@ proc recvLine*(socket: PAsyncSocket,
       if c.len == 0:
         return ""
       if c == "\r":
-        c = await recv(socket, 1, flags + {TSocketFlags.Peek})
-        if c.len > 0 and c == "\L":
-          let dummy = await recv(socket, 1, flags)
-          assert dummy == "\L"
+        c = await recv(socket, 1, flags) # Skip \L
+        assert c == "\L"
         addNLIfEmpty()
         return
       elif c == "\L":
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index 8b19c58f8..1548cf231 100644
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -466,7 +466,8 @@ proc newAsyncHttpClient*(userAgent = defUserAgent,
   result.headers = newStringTable(modeCaseInsensitive)
   result.userAgent = defUserAgent
   result.maxRedirects = maxRedirects
-  result.sslContext = net.SslContext(sslContext)
+  when defined(ssl):
+    result.sslContext = net.SslContext(sslContext)
 
 proc close*(client: AsyncHttpClient) =
   ## Closes any connections held by the HTTP client.