2021-06-27 06:35:36 +00:00
|
|
|
## nim-websock
|
2021-06-20 04:33:20 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
2021-06-01 02:39:14 +00:00
|
|
|
|
|
|
|
import std/uri
|
2021-04-14 11:07:38 +00:00
|
|
|
import pkg/[chronos,
|
2021-04-05 21:01:10 +00:00
|
|
|
chronicles,
|
2021-04-14 11:07:38 +00:00
|
|
|
httputils]
|
2021-04-13 22:05:58 +00:00
|
|
|
|
2021-06-27 06:35:36 +00:00
|
|
|
import ../websock/[websock, extensions/compression/deflate]
|
2021-06-11 20:04:09 +00:00
|
|
|
import ../tests/keys
|
2021-03-11 03:34:14 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "Handling request:", uri = request.uri.path
|
2021-06-01 02:39:14 +00:00
|
|
|
|
|
|
|
try:
|
2021-06-20 03:19:08 +00:00
|
|
|
let deflateFactory = deflateFactory()
|
|
|
|
let server = WSServer.new(factories = [deflateFactory])
|
2021-06-01 02:39:14 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
if ws.readyState != Open:
|
|
|
|
error "Failed to open websocket connection"
|
|
|
|
return
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "Websocket handshake completed"
|
|
|
|
while ws.readyState != ReadyState.Closed:
|
2021-08-04 16:23:56 +00:00
|
|
|
let recvData = await ws.recvMsg()
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "Client Response: ", size = recvData.len, binary = ws.binary
|
2021-06-20 03:19:08 +00:00
|
|
|
|
|
|
|
if ws.readyState == ReadyState.Closed:
|
|
|
|
# if session already terminated by peer,
|
|
|
|
# no need to send response
|
|
|
|
break
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await ws.send(recvData,
|
|
|
|
if ws.binary: Opcode.Binary else: Opcode.Text)
|
2021-06-11 20:04:09 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
except WebSocketError as exc:
|
|
|
|
error "WebSocket error:", exception = exc.msg
|
2021-03-11 03:34:14 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2021-06-20 06:57:07 +00:00
|
|
|
# we want to run parallel tests in CI
|
|
|
|
# so we are using different port
|
2021-06-01 02:39:14 +00:00
|
|
|
proc main() {.async.} =
|
|
|
|
let
|
|
|
|
socketFlags = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}
|
2021-06-11 20:04:09 +00:00
|
|
|
server = when defined tls:
|
|
|
|
TlsHttpServer.create(
|
2021-07-15 20:17:55 +00:00
|
|
|
address = initTAddress("127.0.0.1:8889"),
|
2021-06-11 20:04:09 +00:00
|
|
|
tlsPrivateKey = TLSPrivateKey.init(SecureKey),
|
|
|
|
tlsCertificate = TLSCertificate.init(SecureCert),
|
|
|
|
flags = socketFlags)
|
|
|
|
else:
|
2021-07-15 20:17:55 +00:00
|
|
|
HttpServer.create(initTAddress("127.0.0.1:8888"), flags = socketFlags)
|
|
|
|
|
|
|
|
when defined accepts:
|
|
|
|
proc accepts() {.async, raises: [Defect].} =
|
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
let req = await server.accept()
|
|
|
|
await req.handle()
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Transport error", exc = exc.msg
|
|
|
|
|
|
|
|
asyncCheck accepts()
|
|
|
|
else:
|
|
|
|
server.handler = handle
|
|
|
|
server.start()
|
2021-06-01 02:39:14 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "Server listening on ", data = $server.localAddress()
|
2021-06-01 02:39:14 +00:00
|
|
|
await server.join()
|
|
|
|
|
|
|
|
waitFor(main())
|