Case sensitivity fixes for httpclient and sockets module.
This commit is contained in:
parent
eac71d38d7
commit
d26d42b88e
2 changed files with 62 additions and 62 deletions
|
|
@ -181,7 +181,7 @@ proc parseBody(s: TSocket, headers: PStringTable, timeout: int): string =
|
||||||
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.5
|
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.5
|
||||||
if headers["Connection"] == "close":
|
if headers["Connection"] == "close":
|
||||||
var buf = ""
|
var buf = ""
|
||||||
while True:
|
while true:
|
||||||
buf = newString(4000)
|
buf = newString(4000)
|
||||||
let r = s.recv(addr(buf[0]), 4000, timeout)
|
let r = s.recv(addr(buf[0]), 4000, timeout)
|
||||||
if r == 0: break
|
if r == 0: break
|
||||||
|
|
@ -194,7 +194,7 @@ proc parseResponse(s: TSocket, getBody: bool, timeout: int): TResponse =
|
||||||
var fullyRead = false
|
var fullyRead = false
|
||||||
var line = ""
|
var line = ""
|
||||||
result.headers = newStringTable(modeCaseInsensitive)
|
result.headers = newStringTable(modeCaseInsensitive)
|
||||||
while True:
|
while true:
|
||||||
line = ""
|
line = ""
|
||||||
linei = 0
|
linei = 0
|
||||||
s.readLine(line, timeout)
|
s.readLine(line, timeout)
|
||||||
|
|
@ -294,7 +294,7 @@ proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
|
||||||
add(headers, "\c\L")
|
add(headers, "\c\L")
|
||||||
|
|
||||||
var s = socket()
|
var s = socket()
|
||||||
if s == InvalidSocket: raiseOSError(osLastError())
|
if s == invalidSocket: raiseOSError(osLastError())
|
||||||
var port = sockets.TPort(80)
|
var port = sockets.TPort(80)
|
||||||
if r.scheme == "https":
|
if r.scheme == "https":
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
|
|
@ -321,7 +321,7 @@ proc redirection(status: string): bool =
|
||||||
const redirectionNRs = ["301", "302", "303", "307"]
|
const redirectionNRs = ["301", "302", "303", "307"]
|
||||||
for i in items(redirectionNRs):
|
for i in items(redirectionNRs):
|
||||||
if status.startsWith(i):
|
if status.startsWith(i):
|
||||||
return True
|
return true
|
||||||
|
|
||||||
proc getNewLocation(lastUrl: string, headers: PStringTable): string =
|
proc getNewLocation(lastUrl: string, headers: PStringTable): string =
|
||||||
result = headers["Location"]
|
result = headers["Location"]
|
||||||
|
|
@ -467,7 +467,7 @@ proc close*(client: AsyncHttpClient) =
|
||||||
client.socket.close()
|
client.socket.close()
|
||||||
client.connected = false
|
client.connected = false
|
||||||
|
|
||||||
proc recvFull(socket: PAsyncSocket, size: int): PFuture[string] {.async.} =
|
proc recvFull(socket: PAsyncSocket, size: int): Future[string] {.async.} =
|
||||||
## Ensures that all the data requested is read and returned.
|
## Ensures that all the data requested is read and returned.
|
||||||
result = ""
|
result = ""
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -476,7 +476,7 @@ proc recvFull(socket: PAsyncSocket, size: int): PFuture[string] {.async.} =
|
||||||
if data == "": break # We've been disconnected.
|
if data == "": break # We've been disconnected.
|
||||||
result.add data
|
result.add data
|
||||||
|
|
||||||
proc parseChunks(client: PAsyncHttpClient): PFuture[string] {.async.} =
|
proc parseChunks(client: PAsyncHttpClient): Future[string] {.async.} =
|
||||||
result = ""
|
result = ""
|
||||||
var ri = 0
|
var ri = 0
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -509,7 +509,7 @@ proc parseChunks(client: PAsyncHttpClient): PFuture[string] {.async.} =
|
||||||
# them: http://tools.ietf.org/html/rfc2616#section-3.6.1
|
# them: http://tools.ietf.org/html/rfc2616#section-3.6.1
|
||||||
|
|
||||||
proc parseBody(client: PAsyncHttpClient,
|
proc parseBody(client: PAsyncHttpClient,
|
||||||
headers: PStringTable): PFuture[string] {.async.} =
|
headers: PStringTable): Future[string] {.async.} =
|
||||||
result = ""
|
result = ""
|
||||||
if headers["Transfer-Encoding"] == "chunked":
|
if headers["Transfer-Encoding"] == "chunked":
|
||||||
result = await parseChunks(client)
|
result = await parseChunks(client)
|
||||||
|
|
@ -532,19 +532,19 @@ proc parseBody(client: PAsyncHttpClient,
|
||||||
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.5
|
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.5
|
||||||
if headers["Connection"] == "close":
|
if headers["Connection"] == "close":
|
||||||
var buf = ""
|
var buf = ""
|
||||||
while True:
|
while true:
|
||||||
buf = await client.socket.recvFull(4000)
|
buf = await client.socket.recvFull(4000)
|
||||||
if buf == "": break
|
if buf == "": break
|
||||||
result.add(buf)
|
result.add(buf)
|
||||||
|
|
||||||
proc parseResponse(client: PAsyncHttpClient,
|
proc parseResponse(client: PAsyncHttpClient,
|
||||||
getBody: bool): PFuture[TResponse] {.async.} =
|
getBody: bool): Future[TResponse] {.async.} =
|
||||||
var parsedStatus = false
|
var parsedStatus = false
|
||||||
var linei = 0
|
var linei = 0
|
||||||
var fullyRead = false
|
var fullyRead = false
|
||||||
var line = ""
|
var line = ""
|
||||||
result.headers = newStringTable(modeCaseInsensitive)
|
result.headers = newStringTable(modeCaseInsensitive)
|
||||||
while True:
|
while true:
|
||||||
linei = 0
|
linei = 0
|
||||||
line = await client.socket.recvLine()
|
line = await client.socket.recvLine()
|
||||||
if line == "": break # We've been disconnected.
|
if line == "": break # We've been disconnected.
|
||||||
|
|
@ -603,7 +603,7 @@ proc newConnection(client: PAsyncHttpClient, url: TURL) {.async.} =
|
||||||
client.connected = true
|
client.connected = true
|
||||||
|
|
||||||
proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET,
|
proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET,
|
||||||
body = ""): PFuture[TResponse] {.async.} =
|
body = ""): Future[TResponse] {.async.} =
|
||||||
## Connects to the hostname specified by the URL and performs a request
|
## Connects to the hostname specified by the URL and performs a request
|
||||||
## using the method specified.
|
## using the method specified.
|
||||||
##
|
##
|
||||||
|
|
@ -626,7 +626,7 @@ proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET,
|
||||||
|
|
||||||
result = await parseResponse(client, httpMethod != httpHEAD)
|
result = await parseResponse(client, httpMethod != httpHEAD)
|
||||||
|
|
||||||
proc get*(client: PAsyncHttpClient, url: string): PFuture[TResponse] {.async.} =
|
proc get*(client: PAsyncHttpClient, url: string): Future[TResponse] {.async.} =
|
||||||
## Connects to the hostname specified by the URL and performs a GET request.
|
## Connects to the hostname specified by the URL and performs a GET request.
|
||||||
##
|
##
|
||||||
## This procedure will follow redirects up to a maximum number of redirects
|
## This procedure will follow redirects up to a maximum number of redirects
|
||||||
|
|
|
||||||
|
|
@ -251,14 +251,14 @@ when defined(ssl):
|
||||||
ErrLoadBioStrings()
|
ErrLoadBioStrings()
|
||||||
OpenSSL_add_all_algorithms()
|
OpenSSL_add_all_algorithms()
|
||||||
|
|
||||||
proc SSLError(s = "") =
|
proc raiseSSLError(s = "") =
|
||||||
if s != "":
|
if s != "":
|
||||||
raise newException(ESSL, s)
|
raise newException(ESSL, s)
|
||||||
let err = ErrPeekLastError()
|
let err = ErrPeekLastError()
|
||||||
if err == 0:
|
if err == 0:
|
||||||
raise newException(ESSL, "No error reported.")
|
raise newException(ESSL, "No error reported.")
|
||||||
if err == -1:
|
if err == -1:
|
||||||
OSError(OSLastError())
|
raiseOSError(osLastError())
|
||||||
var errStr = ErrErrorString(err, nil)
|
var errStr = ErrErrorString(err, nil)
|
||||||
raise newException(ESSL, $errStr)
|
raise newException(ESSL, $errStr)
|
||||||
|
|
||||||
|
|
@ -272,18 +272,18 @@ when defined(ssl):
|
||||||
if certFile != "":
|
if certFile != "":
|
||||||
var ret = SSLCTXUseCertificateChainFile(ctx, certFile)
|
var ret = SSLCTXUseCertificateChainFile(ctx, certFile)
|
||||||
if ret != 1:
|
if ret != 1:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
|
|
||||||
# TODO: Password? www.rtfm.com/openssl-examples/part1.pdf
|
# TODO: Password? www.rtfm.com/openssl-examples/part1.pdf
|
||||||
if keyFile != "":
|
if keyFile != "":
|
||||||
if SSL_CTX_use_PrivateKey_file(ctx, keyFile,
|
if SSL_CTX_use_PrivateKey_file(ctx, keyFile,
|
||||||
SSL_FILETYPE_PEM) != 1:
|
SSL_FILETYPE_PEM) != 1:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
|
|
||||||
if SSL_CTX_check_private_key(ctx) != 1:
|
if SSL_CTX_check_private_key(ctx) != 1:
|
||||||
SSLError("Verification of private key file failed.")
|
raiseSslError("Verification of private key file failed.")
|
||||||
|
|
||||||
proc newContext*(protVersion = ProtSSLv23, verifyMode = CVerifyPeer,
|
proc newContext*(protVersion = protSSLv23, verifyMode = CVerifyPeer,
|
||||||
certFile = "", keyFile = ""): PSSLContext =
|
certFile = "", keyFile = ""): PSSLContext =
|
||||||
## Creates an SSL context.
|
## Creates an SSL context.
|
||||||
##
|
##
|
||||||
|
|
@ -308,21 +308,21 @@ when defined(ssl):
|
||||||
when not defined(linux) and not defined(OpenBSD):
|
when not defined(linux) and not defined(OpenBSD):
|
||||||
newCTX = SSL_CTX_new(SSLv2_method())
|
newCTX = SSL_CTX_new(SSLv2_method())
|
||||||
else:
|
else:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
of protSSLv3:
|
of protSSLv3:
|
||||||
newCTX = SSL_CTX_new(SSLv3_method())
|
newCTX = SSL_CTX_new(SSLv3_method())
|
||||||
of protTLSv1:
|
of protTLSv1:
|
||||||
newCTX = SSL_CTX_new(TLSv1_method())
|
newCTX = SSL_CTX_new(TLSv1_method())
|
||||||
|
|
||||||
if newCTX.SSLCTXSetCipherList("ALL") != 1:
|
if newCTX.SSLCTXSetCipherList("ALL") != 1:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
case verifyMode
|
case verifyMode
|
||||||
of CVerifyPeer:
|
of CVerifyPeer:
|
||||||
newCTX.SSLCTXSetVerify(SSLVerifyPeer, nil)
|
newCTX.SSLCTXSetVerify(SSLVerifyPeer, nil)
|
||||||
of CVerifyNone:
|
of CVerifyNone:
|
||||||
newCTX.SSLCTXSetVerify(SSLVerifyNone, nil)
|
newCTX.SSLCTXSetVerify(SSLVerifyNone, nil)
|
||||||
if newCTX == nil:
|
if newCTX == nil:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
|
|
||||||
discard newCTX.SSLCTXSetMode(SSL_MODE_AUTO_RETRY)
|
discard newCTX.SSLCTXSetMode(SSL_MODE_AUTO_RETRY)
|
||||||
newCTX.loadCertificates(certFile, keyFile)
|
newCTX.loadCertificates(certFile, keyFile)
|
||||||
|
|
@ -341,10 +341,10 @@ when defined(ssl):
|
||||||
socket.sslNoHandshake = false
|
socket.sslNoHandshake = false
|
||||||
socket.sslHasPeekChar = false
|
socket.sslHasPeekChar = false
|
||||||
if socket.sslHandle == nil:
|
if socket.sslHandle == nil:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
|
|
||||||
if SSLSetFd(socket.sslHandle, socket.fd) != 1:
|
if SSLSetFd(socket.sslHandle, socket.fd) != 1:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
|
|
||||||
proc raiseSocketError*(socket: Socket, err: int = -1, async = false) =
|
proc raiseSocketError*(socket: Socket, err: int = -1, async = false) =
|
||||||
## Raises proper errors based on return values of ``recv`` functions.
|
## Raises proper errors based on return values of ``recv`` functions.
|
||||||
|
|
@ -359,20 +359,20 @@ proc raiseSocketError*(socket: Socket, err: int = -1, async = false) =
|
||||||
var ret = SSLGetError(socket.sslHandle, err.cint)
|
var ret = SSLGetError(socket.sslHandle, err.cint)
|
||||||
case ret
|
case ret
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
||||||
if async:
|
if async:
|
||||||
return
|
return
|
||||||
else: SSLError("Not enough data on socket.")
|
else: raiseSslError("Not enough data on socket.")
|
||||||
of SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_READ:
|
of SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_READ:
|
||||||
if async:
|
if async:
|
||||||
return
|
return
|
||||||
else: SSLError("Not enough data on socket.")
|
else: raiseSslError("Not enough data on socket.")
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else: SSLError("Unknown Error")
|
else: raiseSslError("Unknown Error")
|
||||||
|
|
||||||
if err == -1 and not (when defined(ssl): socket.isSSL else: false):
|
if err == -1 and not (when defined(ssl): socket.isSSL else: false):
|
||||||
let lastError = osLastError()
|
let lastError = osLastError()
|
||||||
|
|
@ -545,16 +545,16 @@ proc acceptAddr*(server: Socket, client: var Socket, address: var string) {.
|
||||||
if err != SSL_ERROR_WANT_ACCEPT:
|
if err != SSL_ERROR_WANT_ACCEPT:
|
||||||
case err
|
case err
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE,
|
of SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE,
|
||||||
SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
||||||
SSLError("acceptAddrSSL should be used for non-blocking SSL sockets.")
|
raiseSslError("acceptAddrSSL should be used for non-blocking SSL sockets.")
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else:
|
else:
|
||||||
SSLError("Unknown error")
|
raiseSslError("Unknown error")
|
||||||
|
|
||||||
proc setBlocking*(s: Socket, blocking: bool) {.tags: [], gcsafe.}
|
proc setBlocking*(s: Socket, blocking: bool) {.tags: [], gcsafe.}
|
||||||
## Sets blocking mode on socket
|
## Sets blocking mode on socket
|
||||||
|
|
@ -591,17 +591,17 @@ when defined(ssl):
|
||||||
if err != SSL_ERROR_WANT_ACCEPT:
|
if err != SSL_ERROR_WANT_ACCEPT:
|
||||||
case err
|
case err
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE,
|
of SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE,
|
||||||
SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
||||||
client.sslNoHandshake = true
|
client.sslNoHandshake = true
|
||||||
return AcceptNoHandshake
|
return AcceptNoHandshake
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else:
|
else:
|
||||||
SSLError("Unknown error")
|
raiseSslError("Unknown error")
|
||||||
client.sslNoHandshake = false
|
client.sslNoHandshake = false
|
||||||
|
|
||||||
if client.isSSL and client.sslNoHandshake:
|
if client.isSSL and client.sslNoHandshake:
|
||||||
|
|
@ -813,16 +813,16 @@ proc connect*(socket: Socket, address: string, port = Port(0),
|
||||||
let err = SSLGetError(socket.sslHandle, ret)
|
let err = SSLGetError(socket.sslHandle, ret)
|
||||||
case err
|
case err
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_CONNECT,
|
of SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_CONNECT,
|
||||||
SSL_ERROR_WANT_ACCEPT:
|
SSL_ERROR_WANT_ACCEPT:
|
||||||
SSLError("The operation did not complete. Perhaps you should use connectAsync?")
|
raiseSslError("The operation did not complete. Perhaps you should use connectAsync?")
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else:
|
else:
|
||||||
SSLError("Unknown error")
|
raiseSslError("Unknown error")
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
var s: TSockAddrIn
|
var s: TSockAddrIn
|
||||||
|
|
@ -901,19 +901,19 @@ when defined(ssl):
|
||||||
var errret = SSLGetError(socket.sslHandle, ret)
|
var errret = SSLGetError(socket.sslHandle, ret)
|
||||||
case errret
|
case errret
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT,
|
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT,
|
||||||
SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE:
|
SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE:
|
||||||
return false
|
return false
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else:
|
else:
|
||||||
SSLError("Unknown Error")
|
raiseSslError("Unknown Error")
|
||||||
socket.sslNoHandshake = false
|
socket.sslNoHandshake = false
|
||||||
else:
|
else:
|
||||||
SSLError("Socket is not an SSL socket.")
|
raiseSslError("Socket is not an SSL socket.")
|
||||||
|
|
||||||
proc gotHandshake*(socket: TSocket): bool =
|
proc gotHandshake*(socket: TSocket): bool =
|
||||||
## Determines whether a handshake has occurred between a client (``socket``)
|
## Determines whether a handshake has occurred between a client (``socket``)
|
||||||
|
|
@ -923,7 +923,7 @@ when defined(ssl):
|
||||||
if socket.isSSL:
|
if socket.isSSL:
|
||||||
return not socket.sslNoHandshake
|
return not socket.sslNoHandshake
|
||||||
else:
|
else:
|
||||||
SSLError("Socket is not an SSL socket.")
|
raiseSslError("Socket is not an SSL socket.")
|
||||||
|
|
||||||
proc timeValFromMilliseconds(timeout = 500): Timeval =
|
proc timeValFromMilliseconds(timeout = 500): Timeval =
|
||||||
if timeout != -1:
|
if timeout != -1:
|
||||||
|
|
@ -1412,7 +1412,7 @@ proc recv*(socket: Socket): TaintedString {.tags: [ReadIOEffect], deprecated.} =
|
||||||
while true:
|
while true:
|
||||||
var bytesRead = recv(socket, cstring(buf), bufSize-1)
|
var bytesRead = recv(socket, cstring(buf), bufSize-1)
|
||||||
# Error
|
# Error
|
||||||
if bytesRead == -1: OSError(OSLastError())
|
if bytesRead == -1: OSError(osLastError())
|
||||||
|
|
||||||
buf[bytesRead] = '\0' # might not be necessary
|
buf[bytesRead] = '\0' # might not be necessary
|
||||||
setLen(buf, bytesRead)
|
setLen(buf, bytesRead)
|
||||||
|
|
@ -1457,16 +1457,16 @@ proc recvAsync*(socket: Socket, s: var TaintedString): bool {.
|
||||||
var ret = SSLGetError(socket.sslHandle, bytesRead.cint)
|
var ret = SSLGetError(socket.sslHandle, bytesRead.cint)
|
||||||
case ret
|
case ret
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
||||||
SSLError("Unexpected error occured.") # This should just not happen.
|
raiseSslError("Unexpected error occured.") # This should just not happen.
|
||||||
of SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_READ:
|
of SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_READ:
|
||||||
return false
|
return false
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else: SSLError("Unknown Error")
|
else: raiseSslError("Unknown Error")
|
||||||
|
|
||||||
if bytesRead == -1 and not (when defined(ssl): socket.isSSL else: false):
|
if bytesRead == -1 and not (when defined(ssl): socket.isSSL else: false):
|
||||||
let err = osLastError()
|
let err = osLastError()
|
||||||
|
|
@ -1578,7 +1578,7 @@ proc send*(socket: Socket, data: string) {.tags: [WriteIOEffect].} =
|
||||||
if sent < 0:
|
if sent < 0:
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
if socket.isSSL:
|
if socket.isSSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
|
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
|
|
||||||
|
|
@ -1600,16 +1600,16 @@ proc sendAsync*(socket: Socket, data: string): int {.tags: [WriteIOEffect].} =
|
||||||
let ret = SSLGetError(socket.sslHandle, result.cint)
|
let ret = SSLGetError(socket.sslHandle, result.cint)
|
||||||
case ret
|
case ret
|
||||||
of SSL_ERROR_ZERO_RETURN:
|
of SSL_ERROR_ZERO_RETURN:
|
||||||
SSLError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
raiseSslError("TLS/SSL connection failed to initiate, socket closed prematurely.")
|
||||||
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
of SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT:
|
||||||
SSLError("Unexpected error occured.") # This should just not happen.
|
raiseSslError("Unexpected error occured.") # This should just not happen.
|
||||||
of SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_READ:
|
of SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_READ:
|
||||||
return 0
|
return 0
|
||||||
of SSL_ERROR_WANT_X509_LOOKUP:
|
of SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
SSLError("Function for x509 lookup has been called.")
|
raiseSslError("Function for x509 lookup has been called.")
|
||||||
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
of SSL_ERROR_SYSCALL, SSL_ERROR_SSL:
|
||||||
SSLError()
|
raiseSslError()
|
||||||
else: SSLError("Unknown Error")
|
else: raiseSslError("Unknown Error")
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
if result == -1:
|
if result == -1:
|
||||||
|
|
@ -1692,7 +1692,7 @@ discard """ proc setReuseAddr*(s: TSocket) =
|
||||||
var blah: int = 1
|
var blah: int = 1
|
||||||
var mode = SO_REUSEADDR
|
var mode = SO_REUSEADDR
|
||||||
if setsockopt(s.fd, SOL_SOCKET, mode, addr blah, TSOcklen(sizeof(int))) == -1:
|
if setsockopt(s.fd, SOL_SOCKET, mode, addr blah, TSOcklen(sizeof(int))) == -1:
|
||||||
OSError(OSLastError()) """
|
raiseOSError(osLastError()) """
|
||||||
|
|
||||||
proc connect*(socket: Socket, address: string, port = Port(0), timeout: int,
|
proc connect*(socket: Socket, address: string, port = Port(0), timeout: int,
|
||||||
af: Domain = AF_INET) {.tags: [ReadIOEffect, WriteIOEffect].} =
|
af: Domain = AF_INET) {.tags: [ReadIOEffect, WriteIOEffect].} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue