Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding verifyMode param to safeRequest #74

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions nimutils/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,23 @@ proc safeRequest*(client: HttpClient,

# https://github.com/nim-lang/Nim/blob/a45f43da3407dbbf8ecd15ce8ecb361af677add7/lib/pure/httpclient.nim#L380-L386
# similar to stdlib but defaults to bundled CAs
proc getSSLContext(caFile: string = ""): SslContext =
proc getSSLContext(caFile: string = "", verifyMode = CVerifyPeer): SslContext =
ee7 marked this conversation as resolved.
Show resolved Hide resolved
if caFile != "":
# note when caFile is provided there is no try..except
# otherwise we would silently fail to bundled CA root store
# otherwise we would silently fallback to bundled CA root store
# if caFile is invalid/does not exist
return newContext(verifyMode = CVerifyPeer, caFile = caFile)
return newContext(verifyMode = verifyMode, caFile = caFile)
else:
try:
return newContext(verifyMode = CVerifyPeer)
return newContext(verifyMode = verifyMode)
except:
return newContext(verifyMode = CVerifyPeer, caFile = getCAStorePath())
return newContext(verifyMode = verifyMode, caFile = getCAStorePath())

proc createHttpClient*(uri: Uri = parseUri(""),
maxRedirects: int = 3,
timeout: int = 1000, # in ms - 1 second
pinnedCert: string = "",
verifyMode = CVerifyPeer,
disallowHttp: bool = false,
userAgent: string = defUserAgent,
): HttpClient =
Expand All @@ -207,7 +208,7 @@ proc createHttpClient*(uri: Uri = parseUri(""),
# always pass ssl context to client
# as otherwise if http server returns redirect to https
# nim segfaults vs throwing exception
context = getSSLContext(caFile = pinnedCert)
context = getSSLContext(caFile = pinnedCert, verifyMode = verifyMode)
client = newHttpClient(sslContext = context,
userAgent = userAgent,
timeout = timeout,
Expand All @@ -227,6 +228,7 @@ proc safeRequest*(url: Uri | string,
firstRetryDelayMs: int = 0,
timeout: int = 1000,
pinnedCert: string = "",
verifyMode = CVerifyPeer,
maxRedirects: int = 3,
disallowHttp: bool = false,
only2xx: bool = false,
Expand All @@ -240,6 +242,7 @@ proc safeRequest*(url: Uri | string,
maxRedirects = maxRedirects,
timeout = timeout,
pinnedCert = pinnedCert,
verifyMode = verifyMode,
disallowHttp = disallowHttp)
try:
return client.safeRequest(url = uri,
Expand Down