hotfix: fixes SSL initialization for httpclient with --threads:on (#7863)

* hotfix: fixes SSL initialization for httpclient with --threads:on
This commit is contained in:
Andreas Rumpf 2018-05-22 23:07:02 +02:00 • committed by GitHub
commit c6a9a9c30b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -362,15 +362,13 @@ when not defined(ssl):
type SSLContext = ref object type SSLContext = ref object
var defaultSSLContext {.threadvar.}: SSLContext var defaultSSLContext {.threadvar.}: SSLContext
when defined(ssl): proc getDefaultSSL(): SSLContext =
defaultSSLContext = newContext(verifyMode = CVerifyNone) result = defaultSslContext
template contextOrDefault(ctx: SSLContext): SSLContext = when defined(ssl):
var result = ctx if result == nil:
if ctx == nil: defaultSSLContext = newContext(verifyMode = CVerifyNone)
if defaultSSLContext == nil:
defaultSSLContext = newContext(verifyMode = CVerifyNone)
result = defaultSSLContext result = defaultSSLContext
result doAssert result != nil, "failure to initialize the SSL context"
proc newProxy*(url: string, auth = ""): Proxy = proc newProxy*(url: string, auth = ""): Proxy =
## Constructs a new ``TProxy`` object. ## Constructs a new ``TProxy`` object.
@ -480,9 +478,9 @@ proc format(p: MultipartData): tuple[contentType, body: string] =
result.body.add("--" & bound & "--\c\L") result.body.add("--" & bound & "--\c\L")
proc request*(url: string, httpMethod: string, extraHeaders = "", proc request*(url: string, httpMethod: string, extraHeaders = "",
body = "", sslContext = defaultSSLContext, timeout = -1, body = "", sslContext = getDefaultSSL(), timeout = -1,
userAgent = defUserAgent, proxy: Proxy = nil): Response userAgent = defUserAgent, proxy: Proxy = nil): Response
{.deprecated.} = {.deprecated: "use HttpClient.request instead".} =
## | Requests ``url`` with the custom method string specified by the ## | Requests ``url`` with the custom method string specified by the
## | ``httpMethod`` parameter. ## | ``httpMethod`` parameter.
## | Extra headers can be specified and must be separated by ``\c\L`` ## | Extra headers can be specified and must be separated by ``\c\L``
@ -580,7 +578,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
result = parseResponse(s, httpMethod != "HEAD", timeout) result = parseResponse(s, httpMethod != "HEAD", timeout)
proc request*(url: string, httpMethod = HttpGET, extraHeaders = "", proc request*(url: string, httpMethod = HttpGET, extraHeaders = "",
body = "", sslContext = defaultSSLContext, timeout = -1, body = "", sslContext = getDefaultSSL(), timeout = -1,
userAgent = defUserAgent, proxy: Proxy = nil): Response userAgent = defUserAgent, proxy: Proxy = nil): Response
{.deprecated.} = {.deprecated.} =
## | Requests ``url`` with the specified ``httpMethod``. ## | Requests ``url`` with the specified ``httpMethod``.
@ -611,7 +609,7 @@ proc getNewLocation(lastURL: string, headers: HttpHeaders): string =
result = $parsed result = $parsed
proc get*(url: string, extraHeaders = "", maxRedirects = 5, proc get*(url: string, extraHeaders = "", maxRedirects = 5,
sslContext: SSLContext = defaultSSLContext, sslContext: SSLContext = getDefaultSSL(),
timeout = -1, userAgent = defUserAgent, timeout = -1, userAgent = defUserAgent,
proxy: Proxy = nil): Response {.deprecated.} = proxy: Proxy = nil): Response {.deprecated.} =
## | GETs the ``url`` and returns a ``Response`` object ## | GETs the ``url`` and returns a ``Response`` object
@ -632,7 +630,7 @@ proc get*(url: string, extraHeaders = "", maxRedirects = 5,
lastURL = redirectTo lastURL = redirectTo
proc getContent*(url: string, extraHeaders = "", maxRedirects = 5, proc getContent*(url: string, extraHeaders = "", maxRedirects = 5,
sslContext: SSLContext = defaultSSLContext, sslContext: SSLContext = getDefaultSSL(),
timeout = -1, userAgent = defUserAgent, timeout = -1, userAgent = defUserAgent,
proxy: Proxy = nil): string {.deprecated.} = proxy: Proxy = nil): string {.deprecated.} =
## | GETs the body and returns it as a string. ## | GETs the body and returns it as a string.
@ -651,7 +649,7 @@ proc getContent*(url: string, extraHeaders = "", maxRedirects = 5,
proc post*(url: string, extraHeaders = "", body = "", proc post*(url: string, extraHeaders = "", body = "",
maxRedirects = 5, maxRedirects = 5,
sslContext: SSLContext = defaultSSLContext, sslContext: SSLContext = getDefaultSSL(),
timeout = -1, userAgent = defUserAgent, timeout = -1, userAgent = defUserAgent,
proxy: Proxy = nil, proxy: Proxy = nil,
multipart: MultipartData = nil): Response {.deprecated.} = multipart: MultipartData = nil): Response {.deprecated.} =
@ -694,7 +692,7 @@ proc post*(url: string, extraHeaders = "", body = "",
proc postContent*(url: string, extraHeaders = "", body = "", proc postContent*(url: string, extraHeaders = "", body = "",
maxRedirects = 5, maxRedirects = 5,
sslContext: SSLContext = defaultSSLContext, sslContext: SSLContext = getDefaultSSL(),
timeout = -1, userAgent = defUserAgent, timeout = -1, userAgent = defUserAgent,
proxy: Proxy = nil, proxy: Proxy = nil,
multipart: MultipartData = nil): string multipart: MultipartData = nil): string
@ -717,7 +715,7 @@ proc postContent*(url: string, extraHeaders = "", body = "",
return r.body return r.body
proc downloadFile*(url: string, outputFilename: string, proc downloadFile*(url: string, outputFilename: string,
sslContext: SSLContext = defaultSSLContext, sslContext: SSLContext = getDefaultSSL(),
timeout = -1, userAgent = defUserAgent, timeout = -1, userAgent = defUserAgent,
proxy: Proxy = nil) {.deprecated.} = proxy: Proxy = nil) {.deprecated.} =
## | Downloads ``url`` and saves it to ``outputFilename`` ## | Downloads ``url`` and saves it to ``outputFilename``
@ -817,7 +815,7 @@ type
HttpClient* = HttpClientBase[Socket] HttpClient* = HttpClientBase[Socket]
proc newHttpClient*(userAgent = defUserAgent, proc newHttpClient*(userAgent = defUserAgent,
maxRedirects = 5, sslContext = defaultSslContext, proxy: Proxy = nil, maxRedirects = 5, sslContext = getDefaultSSL(), proxy: Proxy = nil,
timeout = -1): HttpClient = timeout = -1): HttpClient =
## Creates a new HttpClient instance. ## Creates a new HttpClient instance.
## ##
@ -844,7 +842,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 = contextOrDefault(sslContext) result.sslContext = sslContext
type type
AsyncHttpClient* = HttpClientBase[AsyncSocket] AsyncHttpClient* = HttpClientBase[AsyncSocket]
@ -852,7 +850,7 @@ type
{.deprecated: [PAsyncHttpClient: AsyncHttpClient].} {.deprecated: [PAsyncHttpClient: AsyncHttpClient].}
proc newAsyncHttpClient*(userAgent = defUserAgent, proc newAsyncHttpClient*(userAgent = defUserAgent,
maxRedirects = 5, sslContext = defaultSslContext, maxRedirects = 5, sslContext = getDefaultSSL(),
proxy: Proxy = nil): AsyncHttpClient = proxy: Proxy = nil): AsyncHttpClient =
## Creates a new AsyncHttpClient instance. ## Creates a new AsyncHttpClient instance.
## ##
@ -876,7 +874,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 = contextOrDefault(sslContext) result.sslContext = 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.