Skip to content

Commit

Permalink
Use httpclient.getDefaultSsl if visible
Browse files Browse the repository at this point in the history
  • Loading branch information
hlaaftana authored Jul 19, 2019
1 parent 79c5f4b commit 58fece6
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions websocket/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ import private/hex

const WebsocketUserAgent* = "websocket.nim (https://github.com/niv/websocket.nim)"

when not defined(ssl):
type SslContext = ref object
var defaultSsl {.threadvar.}: SslContext

proc defaultSslContext: SslContext =
result = defaultSsl
when defined(ssl):
if result.isNil:
result = newContext(protTLSv1, verifyMode = CVerifyNone)
doAssert(not result.isNil, "failure to initialize SSL context")
defaultSsl = result
when not declared(httpclient.getDefaultSsl):
when not defined(ssl):
type SslContext = ref object
var defaultSsl {.threadvar.}: SslContext

proc getDefaultSsl: SslContext =
result = defaultSsl
when defined(ssl):
if result.isNil:
result = newContext(protTLSv1, verifyMode = CVerifyNone)
doAssert(not result.isNil, "failure to initialize SSL context")
defaultSsl = result

proc newAsyncWebsocketClient*(uri: Uri, client: AsyncHttpClient,
protocols: seq[string] = @[]): Future[AsyncWebSocket] {.async.} =
Expand Down Expand Up @@ -101,7 +102,7 @@ proc newAsyncWebsocketClient*(uri: Uri,
additionalHeaders: seq[(string, string)] = @[],
protocols: seq[string] = @[],
userAgent: string = WebsocketUserAgent,
sslContext: SslContext = defaultSslContext()
sslContext: SslContext = getDefaultSsl()
): Future[AsyncWebSocket] =
var client =
when defined(ssl):
Expand All @@ -115,7 +116,7 @@ proc newAsyncWebsocketClient*(uri: string,
additionalHeaders: seq[(string, string)] = @[],
protocols: seq[string] = @[],
userAgent: string = WebsocketUserAgent,
sslContext: SslContext = defaultSslContext()
sslContext: SslContext = getDefaultSsl()
): Future[AsyncWebSocket] =
result = newAsyncWebsocketClient(parseUri(uri),
additionalHeaders, protocols, userAgent, sslContext)
Expand All @@ -124,7 +125,7 @@ proc newAsyncWebsocketClient*(host: string, port: Port, path: string,
ssl = false, additionalHeaders: seq[(string, string)] = @[],
protocols: seq[string] = @[],
userAgent: string = WebsocketUserAgent,
sslContext: SslContext = defaultSslContext()
sslContext: SslContext = getDefaultSsl()
): Future[AsyncWebSocket] =
result = newAsyncWebsocketClient(
(if ssl: "wss" else: "ws") & "://" & host & ":" & $port & "/" & path,
Expand Down

0 comments on commit 58fece6

Please sign in to comment.