Handle hostname option properly (#92)

This commit is contained in:
Tanguy 2021-09-20 15:55:38 +02:00 committed by GitHub
parent f354dfebe9
commit e199aa57da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -168,10 +168,15 @@ proc connect*(
version = HttpVersion11,
tlsFlags: set[TLSFlags] = {},
tlsMinVersion = TLSVersion.TLS11,
tlsMaxVersion = TLSVersion.TLS12): Future[T]
tlsMaxVersion = TLSVersion.TLS12,
hostName = ""): Future[T]
{.async, raises: [Defect, HttpError].} =
let hostPort = host.split(":")
let wantedHostName = if hostName.len > 0:
hostName
else:
host.split(":")[0]
let addrs = resolveTAddress(host)
for a in addrs:
try:
@ -181,7 +186,7 @@ proc connect*(
tlsFlags,
tlsMinVersion,
tlsMaxVersion,
hostName = hostPort[0])
hostName = wantedHostName)
return conn
except TransportError as exc:

View File

@ -122,9 +122,10 @@ proc connect*(
let
rng = if isNil(rng): newRng() else: rng
key = Base64Pad.encode(genWebSecKey(rng))
hostname = if hostName.len > 0: hostName else: $host
let client = if secure:
await TlsHttpClient.connect(host, tlsFlags = flags)
await TlsHttpClient.connect(host, tlsFlags = flags, hostName = hostname)
else:
await HttpClient.connect(host)
@ -134,7 +135,7 @@ proc connect*(
("Cache-Control", "no-cache"),
("Sec-WebSocket-Version", $version),
("Sec-WebSocket-Key", key),
("Host", if hostName.len > 0: hostName else: $host)]
("Host", hostname)]
var headers = HttpTable.init(headerData)
if protocols.len > 0: