* fix 15215

* fix test

* end line

* Update tests/stdlib/tnetconnect.nim

Co-authored-by: flywind <xzsflywind@gmail.com>

* Update lib/pure/net.nim

Co-authored-by: flywind <xzsflywind@gmail.com>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
rockcavera
2021-02-22 13:40:57 -03:00
committed by GitHub
parent 2aba116bbc
commit d76d79336f
2 changed files with 34 additions and 5 deletions

View File

@@ -1986,10 +1986,6 @@ proc connect*(socket: Socket, address: string, port = Port(0),
##
## The ``timeout`` parameter specifies the time in milliseconds to allow for
## the connection to the server to be made.
##
## **Warning:** This procedure appears to be broken for SSL connections as of
## Nim v1.0.2. Consider using the other `connect` procedure. See
## https://github.com/nim-lang/Nim/issues/15215 for more info.
socket.fd.setBlocking(false)
socket.connectAsync(address, port, socket.domain)
@@ -2003,7 +1999,18 @@ proc connect*(socket: Socket, address: string, port = Port(0),
when defineSsl and not defined(nimdoc):
if socket.isSsl:
socket.fd.setBlocking(true)
doAssert socket.gotHandshake()
# RFC3546 for SNI specifies that IP addresses are not allowed.
if not isIpAddress(address):
# Discard result in case OpenSSL version doesn't support SNI, or we're
# not using TLSv1+
discard SSL_set_tlsext_host_name(socket.sslHandle, address)
ErrClearError()
let ret = SSL_connect(socket.sslHandle)
socketError(socket, ret)
when not defined(nimDisableCertificateValidation):
if not isIpAddress(address):
socket.checkCertName(address)
socket.fd.setBlocking(true)
proc getPrimaryIPAddr*(dest = parseIpAddress("8.8.8.8")): IpAddress =

View File

@@ -0,0 +1,22 @@
discard """
cmd: "nim c -r -d:ssl $file"
exitcode: 0
"""
import std/net
# Issue 15215 - https://github.com/nim-lang/Nim/issues/15215
proc test() =
var
ctx = newContext()
socket = newSocket()
wrapSocket(ctx, socket)
connect(socket, "www.nim-lang.org", Port(443), 5000)
send(socket, "GET / HTTP/1.0\nHost: www.nim-lang.org\nConnection: close\n\n")
close(socket)
test()