From e199aa57da0c95cb127151ec2078f847f7cd41aa Mon Sep 17 00:00:00 2001 From: Tanguy Date: Mon, 20 Sep 2021 15:55:38 +0200 Subject: [PATCH] Handle hostname option properly (#92) --- websock/http/client.nim | 11 ++++++++--- websock/websock.nim | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/websock/http/client.nim b/websock/http/client.nim index 0c37f060fe..0811789f77 100644 --- a/websock/http/client.nim +++ b/websock/http/client.nim @@ -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: diff --git a/websock/websock.nim b/websock/websock.nim index 4b526d1837..612d05327f 100644 --- a/websock/websock.nim +++ b/websock/websock.nim @@ -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: