diff --git a/examples/autobahn_client.nim b/examples/autobahn_client.nim index c7b6537754..341f216ed2 100644 --- a/examples/autobahn_client.nim +++ b/examples/autobahn_client.nim @@ -3,30 +3,35 @@ import pkg/[chronos, chronicles, stew/byteutils], ../ws/[ws, types, frame] -type - Arg = object - host: string - port: Port - path: string +const + clientFlags = {NoVerifyHost, NoVerifyServerName} -proc getCaseCount(arg: Arg): Future[int] {.async.} = - let path = arg.path & "/getCaseCount" +const agent = when defined tls: + "nim-ws-tls-client" + else: + "nim-ws-client" +const secure = defined tls +proc connectServer(path: string): Future[WSSession] {.async.} = + let ws = await WebSocket.connect( + host = "127.0.0.1", + port = Port(9001), + path = path, + secure=secure, + flags=clientFlags + ) + return ws + +proc getCaseCount(): Future[int] {.async.} = var caseCount = 0 block: try: - let ws = await WebSocket.connect(arg.host, arg.port, path) - + let ws = await connectServer("/getCaseCount") let buff = await ws.recv() - if buff.len <= 0: - break - let dataStr = string.fromBytes(buff) caseCount = parseInt(dataStr) - await ws.close() break - except WebSocketError as exc: error "WebSocket error", exception = exc.msg except ValueError as exc: @@ -34,43 +39,36 @@ proc getCaseCount(arg: Arg): Future[int] {.async.} = return caseCount -proc generateReport(arg: Arg) {.async.} = - let path = arg.path & "/updateReports?agent=nim-ws" +proc generateReport() {.async.} = try: - let ws = await WebSocket.connect(arg.host, arg.port, path) - + let ws = await connectServer("/updateReports?agent=" & agent) while true: let buff = await ws.recv() if buff.len <= 0: break - await ws.close() - except WebSocketError as exc: error "WebSocket error", exception = exc.msg proc main() {.async.} = - let arg = Arg(host: "127.0.0.1", port: Port(9001)) - let caseCount = await getCaseCount(arg) - notice "case count", count=caseCount + let caseCount = await getCaseCount() + trace "case count", count=caseCount for i in 1..caseCount: - let path = "$1/runCase?case=$2&agent=nim-ws" % [arg.path, $i] + let path = "/runCase?case=$1&agent=$2" % [$i, agent] try: - let ws = await WebSocket.connect(arg.host, arg.port, path) - + let ws = await connectServer(path) # echo back - while true: - let data = await ws.recv() - if data.len <= 0: - break - - await ws.send(data, if ws.binary: Opcode.Binary else: Opcode.Text) - + let data = await ws.recv() + let opCode = if ws.binary: + Opcode.Binary + else: + Opcode.Text + await ws.send(data, opCode) await ws.close() except WebSocketError as exc: error "WebSocket error", exception = exc.msg - await generateReport(arg) + await generateReport() waitFor main() diff --git a/ws/http/client.nim b/ws/http/client.nim index 7fcfc45467..be838ed0c6 100644 --- a/ws/http/client.nim +++ b/ws/http/client.nim @@ -59,8 +59,11 @@ proc generateHeaders( var headersData = toUpperAscii($httpMethod) headersData.add " " - if not requestUrl.path.startsWith("/"): headersData.add "/" - headersData.add(requestUrl.path & " ") + if not requestUrl.path.startsWith("/"): headersData.add "/" + headersData.add(requestUrl.path) + if requestUrl.query.len > 0: + headersData.add("?" & requestUrl.query) + headersData.add(" ") headersData.add($version & CRLF) for (key, val) in headers.stringItems(true): diff --git a/ws/utils.nim b/ws/utils.nim index d641eea470..865c9d46f6 100644 --- a/ws/utils.nim +++ b/ws/utils.nim @@ -40,5 +40,5 @@ proc genWebSecKey*(rng: Rng): seq[byte] = proc r(): byte = byte(rand(rng, 255)) ## Generates a random key of 16 random chars. for i in 0..15: - key.add(r()) + key[i] = r() return key diff --git a/ws/ws.nim b/ws/ws.nim index d239e0c3d7..a43ce4c924 100644 --- a/ws/ws.nim +++ b/ws/ws.nim @@ -61,7 +61,7 @@ proc connect*( ## var rng = if isNil(rng): newRng() else: rng - var key = Base64.encode(genWebSecKey(rng)) + var key = Base64Pad.encode(genWebSecKey(rng)) var uri = uri let client = case uri.scheme: of "wss": @@ -79,7 +79,8 @@ proc connect*( ("Upgrade", "websocket"), ("Cache-Control", "no-cache"), ("Sec-WebSocket-Version", $version), - ("Sec-WebSocket-Key", key)] + ("Sec-WebSocket-Key", key), + ("Host", uri.hostname & ":" & uri.port)] var headers = HttpTable.init(headerData) if protocols.len > 0: @@ -177,6 +178,7 @@ proc connect*( path = path, protocols = protocols, extensions = extensions, + secure = secure, flags = flags, version = version, frameSize = frameSize,