diff options
Diffstat (limited to 'lib/pure/asyncftpclient.nim')
-rw-r--r-- | lib/pure/asyncftpclient.nim | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/pure/asyncftpclient.nim b/lib/pure/asyncftpclient.nim index 96f54b49e..fe4577ed9 100644 --- a/lib/pure/asyncftpclient.nim +++ b/lib/pure/asyncftpclient.nim @@ -37,12 +37,24 @@ type proc (total, progress: BiggestInt, speed: float): Future[void] {.closure, gcsafe.} -proc expectReply(ftp: AsyncFtpClient): Future[TaintedString] = - result = ftp.csock.recvLine() +const multiLineLimit = 10000 + +proc expectReply(ftp: AsyncFtpClient): Future[TaintedString] {.async.} = + result = await ftp.csock.recvLine() + var count = 0 + while result[3] == '-': + ## Multi-line reply. + let line = await ftp.csock.recvLine() + result.add("\n" & line) + count.inc() + if count >= multiLineLimit: + raise newException(ReplyError, "Reached maximum multi-line reply count.") proc send*(ftp: AsyncFtpClient, m: string): Future[TaintedString] {.async.} = ## Send a message to the server, and wait for a primary reply. ## ``\c\L`` is added for you. + ## + ## **Note:** The server may return multiple lines of coded replies. await ftp.csock.send(m & "\c\L") return await ftp.expectReply() @@ -79,6 +91,8 @@ proc connect*(ftp: AsyncFtpClient) {.async.} = # 120 Service ready in nnn minutes. # We wait until we receive 220. reply = await ftp.expectReply() + + # Handle 220 messages from the server assertReply(reply, "220") if ftp.user != "": @@ -300,7 +314,7 @@ proc newAsyncFtpClient*(address: string, port = Port(21), result.dsockConnected = false result.csock = newAsyncSocket() -when isMainModule: +when not defined(testing) and isMainModule: var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test") proc main(ftp: AsyncFtpClient) {.async.} = await ftp.connect() |