Ensure default SSL context is always initialized
This commit is contained in:
parent
386efb464b
commit
ef508047bf
1 changed files with 10 additions and 2 deletions
|
|
@ -337,8 +337,16 @@ proc parseResponse(s: Socket, getBody: bool, timeout: int): Response =
|
||||||
when not defined(ssl):
|
when not defined(ssl):
|
||||||
type SSLContext = ref object
|
type SSLContext = ref object
|
||||||
var defaultSSLContext {.threadvar.}: SSLContext
|
var defaultSSLContext {.threadvar.}: SSLContext
|
||||||
|
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
||||||
|
template contextOrDefault(ctx: SSLContext): SSLContext =
|
||||||
|
var result = ctx
|
||||||
|
if ctx == nil:
|
||||||
|
if defaultSSLContext == nil:
|
||||||
|
defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
||||||
|
result = defaultSSLContext
|
||||||
|
result
|
||||||
|
|
||||||
proc newProxy*(url: string, auth = ""): Proxy =
|
proc newProxy*(url: string, auth = ""): Proxy =
|
||||||
## Constructs a new ``TProxy`` object.
|
## Constructs a new ``TProxy`` object.
|
||||||
|
|
@ -805,7 +813,7 @@ proc newHttpClient*(userAgent = defUserAgent,
|
||||||
result.bodyStream = newStringStream()
|
result.bodyStream = newStringStream()
|
||||||
result.getBody = true
|
result.getBody = true
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
result.sslContext = sslContext
|
result.sslContext = contextOrDefault(sslContext)
|
||||||
|
|
||||||
type
|
type
|
||||||
AsyncHttpClient* = HttpClientBase[AsyncSocket]
|
AsyncHttpClient* = HttpClientBase[AsyncSocket]
|
||||||
|
|
@ -837,7 +845,7 @@ proc newAsyncHttpClient*(userAgent = defUserAgent,
|
||||||
result.bodyStream = newFutureStream[string]("newAsyncHttpClient")
|
result.bodyStream = newFutureStream[string]("newAsyncHttpClient")
|
||||||
result.getBody = true
|
result.getBody = true
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
result.sslContext = sslContext
|
result.sslContext = contextOrDefault(sslContext)
|
||||||
|
|
||||||
proc close*(client: HttpClient | AsyncHttpClient) =
|
proc close*(client: HttpClient | AsyncHttpClient) =
|
||||||
## Closes any connections held by the HTTP client.
|
## Closes any connections held by the HTTP client.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue