fixes required to allow autobahn client works
- send requestUrl.query in client.nim - fixes genMaskKey bug in utils.nim - using Base64Pad instead of Base64 in `connect` proc - add `host` field to request HTTP header in `connect` proc - add missing `secure` param in `connect` proc fixes #32
This commit is contained in:
parent
88e99ff028
commit
a60dbd5dc9
|
@ -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()
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue