diff --git a/tests/extensions/base64ext.nim b/tests/extensions/base64ext.nim index 0ffd24f..fc03d1c 100644 --- a/tests/extensions/base64ext.nim +++ b/tests/extensions/base64ext.nim @@ -95,7 +95,7 @@ proc base64Factory*(padding: bool): ExtFactory = proc factory(isServer: bool, args: seq[ExtParam]): Result[Ext, string] {. - gcsafe, raises: [Defect].} = + gcsafe, raises: [].} = # you can capture configuration variables via closure # if you want diff --git a/tests/extensions/hexext.nim b/tests/extensions/hexext.nim index 9da8f37..9be5bb3 100644 --- a/tests/extensions/hexext.nim +++ b/tests/extensions/hexext.nim @@ -83,7 +83,7 @@ proc hexFactory*(): ExtFactory = proc factory(isServer: bool, args: seq[ExtParam]): Result[Ext, string] {. - gcsafe, raises: [Defect].} = + gcsafe, raises: [].} = # you can capture configuration variables via closure # if you want diff --git a/tests/helpers.nim b/tests/helpers.nim index 78d1ece..77484bc 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -19,13 +19,15 @@ import pkg/[ import ../websock/websock import ./keys +{.push gcsafe, raises: [].} + const WSPath* = when defined secure: "/wss" else: "/ws" -proc rndStr*(size: int): string = +proc rndStr*(size: int): string {.gcsafe, raises: [].} = for _ in 0..= 1.6.0" -requires "chronos >= 3.0.0" +requires "chronos#head" requires "httputils >= 0.2.0" requires "chronicles >= 0.10.2" requires "stew >= 0.1.0" diff --git a/websock/extensions/compression/deflate.nim b/websock/extensions/compression/deflate.nim index 73467c8..4f9f688 100644 --- a/websock/extensions/compression/deflate.nim +++ b/websock/extensions/compression/deflate.nim @@ -140,6 +140,7 @@ proc createParams(args: seq[ExtParam], ok(resp) +{.warning[HoleEnumConv]:off.} proc getWindowBits(opts: DeflateOpts, isServer: bool): ZWindowBits = if isServer: if opts.serverMaxWindowBits == 0: @@ -152,6 +153,8 @@ proc getWindowBits(opts: DeflateOpts, isServer: bool): ZWindowBits = else: ZWindowBits(-opts.clientMaxWindowBits) +{.warning[HoleEnumConv]:on.} + proc getContextTakeover(opts: DeflateOpts, isServer: bool): bool = if isServer: opts.serverNoContextTakeOver @@ -369,7 +372,7 @@ proc deflateFactory*( proc factory(isServer: bool, args: seq[ExtParam]): Result[Ext, string] {. - gcsafe, raises: [Defect].} = + gcsafe, raises: [].} = # capture user configuration via closure var opts = DeflateOpts( diff --git a/websock/frame.nim b/websock/frame.nim index e4d0342..3595342 100644 --- a/websock/frame.nim +++ b/websock/frame.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import pkg/[ chronos, diff --git a/websock/http/client.nim b/websock/http/client.nim index 88cb565..e4fc61a 100644 --- a/websock/http/client.nim +++ b/websock/http/client.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/[uri, strutils] import pkg/[ @@ -170,13 +170,17 @@ proc connect*( tlsMinVersion = TLSVersion.TLS12, tlsMaxVersion = TLSVersion.TLS12, hostName = ""): Future[T] - {.async, raises: [Defect, HttpError].} = + {.async: (raises: [CatchableError, HttpError]).} = let wantedHostName = if hostName.len > 0: hostName else: host.split(":")[0] + template used(x: typed) = + # silence unused warning + discard + let addrs = resolveTAddress(host) for a in addrs: try: @@ -190,6 +194,7 @@ proc connect*( return conn except TransportError as exc: + used(exc) trace "Error connecting to address", address = $a, exc = exc.msg raise newException(HttpError, diff --git a/websock/http/common.nim b/websock/http/common.nim index fbfcc99..ba21332 100644 --- a/websock/http/common.nim +++ b/websock/http/common.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/[uri] import pkg/[ diff --git a/websock/http/server.nim b/websock/http/server.nim index e4f7a63..1eee8ed 100644 --- a/websock/http/server.nim +++ b/websock/http/server.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/uri import pkg/[ @@ -43,6 +43,10 @@ type TlsHttpServer* = HttpServer +template used(x: typed) = + # silence unused warning + discard + proc validateRequest( stream: AsyncStreamWriter, header: HttpRequestHeader): Future[ReqStatus] {.async.} = @@ -69,7 +73,7 @@ proc parseRequest( ## var buffer = newSeq[byte](MaxHttpHeadersSize) - let remoteAddr = stream.reader.tsource.remoteAddress() + let remoteAddr {.used.} = stream.reader.tsource.remoteAddress() trace "Received connection", address = $remoteAddr try: let hlenfut = stream.reader.readUntil( @@ -115,11 +119,12 @@ proc parseRequest( # remote peer disconnected trace "Remote peer disconnected", address = $remoteAddr except TransportOsError as exc: + used(exc) trace "Problems with networking", address = $remoteAddr, error = exc.msg proc handleConnCb( server: StreamServer, - transp: StreamTransport) {.async.} = + transp: StreamTransport) {.async: (raises: []).} = var stream: AsyncStream try: stream = AsyncStream( @@ -131,10 +136,15 @@ proc handleConnCb( await httpServer.handler(request) except CatchableError as exc: + used(exc) debug "Exception in HttpHandler", exc = exc.msg finally: - await stream.closeWait() - + try: + await stream.closeWait() + except CatchableError as exc: + used(exc) + debug "Exception in HttpHandler closewait", exc = exc.msg + proc handleTlsConnCb( server: StreamServer, transp: StreamTransport) {.async.} = @@ -158,6 +168,7 @@ proc handleTlsConnCb( await httpServer.handler(request) except CatchableError as exc: + used(exc) debug "Exception in HttpHandler", exc = exc.msg finally: await stream.closeWait() @@ -208,7 +219,7 @@ proc create*( headersTimeout = HttpHeadersTimeout, handshakeTimeout = 0.seconds ): HttpServer - {.raises: [Defect, CatchableError].} = # TODO: remove CatchableError + {.raises: [CatchableError].} = # TODO: remove CatchableError ## Make a new HTTP Server ## diff --git a/websock/session.nim b/websock/session.nim index 30d82f9..e933812 100644 --- a/websock/session.nim +++ b/websock/session.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/strformat import pkg/[chronos, chronicles, stew/byteutils, stew/endians2] @@ -18,6 +18,10 @@ import pkg/chronos/streams/asyncstream logScope: topics = "websock ws-session" +template used(x: typed) = + # silence unused warning + discard + proc prepareCloseBody(code: StatusCodes, reason: string): seq[byte] = result = reason.toBytes if ord(code) > 999: @@ -178,8 +182,7 @@ proc send*( proc send*( ws: WSSession, - data: string): Future[void] - {.raises: [Defect, WSClosedError].} = + data: string): Future[void] = send(ws, data.toBytes(), Opcode.Text) proc handleClose*( @@ -214,7 +217,7 @@ proc handleClose*( else: try: code = StatusCodes(uint16.fromBytesBE(payload[0..<2])) - except RangeError: + except RangeDefect: raise newException(WSInvalidCloseCodeError, "Status code out of range!") @@ -241,6 +244,7 @@ proc handleClose*( try: (code, reason) = ws.onClose(code, reason) except CatchableError as exc: + used(exc) trace "Exception in Close callback, this is most likely a bug", exc = exc.msg else: code = StatusFulfilled @@ -253,6 +257,7 @@ proc handleClose*( try: await ws.send(prepareCloseBody(code, reason), Opcode.Close).wait(5.seconds) except CatchableError as exc: + used(exc) trace "Failed to send Close opcode", err=exc.msg ws.readyState = ReadyState.Closed @@ -303,6 +308,7 @@ proc handleControl*(ws: WSSession, frame: Frame) {.async.} = try: ws.onPing(payload) except CatchableError as exc: + used(exc) trace "Exception in Ping callback, this is most likely a bug", exc = exc.msg # send pong to remote @@ -312,12 +318,15 @@ proc handleControl*(ws: WSSession, frame: Frame) {.async.} = try: ws.onPong(payload) except CatchableError as exc: + used(exc) trace "Exception in Pong callback, this is most likely a bug", exc = exc.msg of Opcode.Close: await ws.handleClose(frame, payload) else: raise newException(WSInvalidOpcodeError, "Invalid control opcode!") +{.warning[HoleEnumConv]:off.} + proc readFrame*(ws: WSSession, extensions: seq[Ext] = @[]): Future[Frame] {.async.} = ## Gets a frame from the WebSocket. ## See https://tools.ietf.org/html/rfc6455#section-5.2 @@ -342,10 +351,11 @@ proc readFrame*(ws: WSSession, extensions: seq[Ext] = @[]): Future[Frame] {.asyn return frame +{.warning[HoleEnumConv]:on.} + proc ping*( ws: WSSession, - data: seq[byte] = @[]): Future[void] - {.raises: [Defect, WSClosedError].} = + data: seq[byte] = @[]): Future[void] = ws.send(data, opcode = Opcode.Ping) proc recv*( @@ -462,7 +472,7 @@ proc recvMsg*( var res: seq[byte] while ws.readyState != ReadyState.Closed: var buf = new(seq[byte]) - let read = await ws.recv(buf, min(size, ws.frameSize)) + let read {.used.} = await ws.recv(buf, min(size, ws.frameSize)) if res.len + buf[].len > size: raise newException(WSMaxMessageSizeError, "Max message size exceeded") @@ -522,7 +532,7 @@ proc close*( except CancelledError as exc: raise exc except CatchableError as exc: - discard # most likely EOF + discard exc # most likely EOF try: ws.readyState = ReadyState.Closing await gentleCloser(ws, prepareCloseBody(code, reason)).wait(10.seconds) @@ -530,6 +540,7 @@ proc close*( trace "Cancellation when closing!", exc = exc.msg raise exc except CatchableError as exc: + used(exc) trace "Exception closing", exc = exc.msg finally: await ws.stream.closeWait() diff --git a/websock/types.nim b/websock/types.nim index 41a752d..56187ac 100644 --- a/websock/types.nim +++ b/websock/types.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/deques import pkg/[chronos, @@ -73,14 +73,14 @@ type StatusCodes* = distinct range[0..4999] ControlCb* = proc(data: openArray[byte] = []) - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [].} CloseResult* = tuple code: StatusCodes reason: string CloseCb* = proc(code: StatusCodes, reason: string): - CloseResult {.gcsafe, raises: [Defect].} + CloseResult {.gcsafe, raises: [].} WebSocket* = ref object of RootObj extensions*: seq[Ext] @@ -122,7 +122,7 @@ type ExtFactoryProc* = proc( isServer: bool, args: seq[ExtParam]): Result[Ext, string] - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [].} ExtFactory* = object name*: string @@ -144,10 +144,10 @@ type Hook* = ref object of RootObj append*: proc(ctx: Hook, headers: var HttpTable): Result[void, string] - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [].} verify*: proc(ctx: Hook, headers: HttpTable): Future[Result[void, string]] - {.closure, gcsafe, raises: [Defect].} + {.gcsafe, async: (raises: []).} WebSocketError* = object of CatchableError WSMalformedHeaderError* = object of WebSocketError diff --git a/websock/websock.nim b/websock/websock.nim index b90477c..8bd4436 100644 --- a/websock/websock.nim +++ b/websock/websock.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +{.push gcsafe, raises: [].} import std/[tables, strutils, @@ -38,9 +38,6 @@ type protocols: seq[string] factories: seq[ExtFactory] -func toException(e: string): ref WebSocketError = - (ref WebSocketError)(msg: e) - func toException(e: cstring): ref WebSocketError = (ref WebSocketError)(msg: $e) @@ -57,7 +54,7 @@ proc getFactory(factories: openArray[ExtFactory], extName: string): ExtFactoryPr proc selectExt(isServer: bool, extensions: var seq[Ext], factories: openArray[ExtFactory], - exts: openArray[string]): string {.raises: [Defect, WSExtError].} = + exts: openArray[string]): string {.raises: [WSExtError].} = var extList: seq[AppExt] var response = "" @@ -216,7 +213,7 @@ proc connect*( onPong: ControlCb = nil, onClose: CloseCb = nil, rng = HmacDrbgContext.new()): Future[WSSession] - {.raises: [Defect, WSWrongUriSchemeError].} = + {.raises: [WSWrongUriSchemeError].} = ## Create a new websockets client ## using a Uri ## @@ -254,11 +251,12 @@ proc handleRequest*( version: uint = WSDefaultVersion, hooks: seq[Hook] = @[]): Future[WSSession] {. - async, - raises: [ - Defect, + async: + (raises: [ + CancelledError, + CatchableError, WSHandshakeError, - WSProtoMismatchError] + WSProtoMismatchError]) .} = ## Creates a new socket from a request. ##