diff --git a/json_rpc/clients/httpclient.nim b/json_rpc/clients/httpclient.nim index b0296d8..e3d8f7e 100644 --- a/json_rpc/clients/httpclient.nim +++ b/json_rpc/clients/httpclient.nim @@ -24,17 +24,18 @@ const MaxHttpRequestSize = 128 * 1024 * 1024 # maximum size of HTTP body in octets proc new(T: type RpcHttpClient, maxBodySize = MaxHttpRequestSize, secure = false): T = - if secure: - T( - maxBodySize: maxBodySize, - httpSession: HttpSessionRef.new(flags={HttpClientFlag.NoVerifyHost, - HttpClientFlag.NoVerifyServerName}), - ) + let httpSessionFlags = if secure: + { + HttpClientFlag.NoVerifyHost, + HttpClientFlag.NoVerifyServerName + } else: - T( - maxBodySize: maxBodySize, - httpSession: HttpSessionRef.new(), - ) + {} + + T( + maxBodySize: maxBodySize, + httpSession: HttpSessionRef.new(flags = httpSessionFlags) + ) proc newRpcHttpClient*(maxBodySize = MaxHttpRequestSize, secure = false): RpcHttpClient = RpcHttpClient.new(maxBodySize, secure) @@ -55,8 +56,10 @@ method call*(client: RpcHttpClient, name: string, res = try: await req.send() - except CatchableError as exc: - raise (ref RpcPostError)(msg: "Failed to send POST Request with JSON-RPC.", parent: exc) + except CancelledError as e: + raise e + except CatchableError as e: + raise (ref RpcPostError)(msg: "Failed to send POST Request with JSON-RPC.", parent: e) if res.status < 200 or res.status >= 300: # res.status is not 2xx (success) raise newException(ErrorResponse, "POST Response: " & $res.status) @@ -68,6 +71,8 @@ method call*(client: RpcHttpClient, name: string, let resBytes = try: await res.getBodyBytes(client.maxBodySize) + except CancelledError as e: + raise e except CatchableError as exc: raise (ref FailedHttpResponse)(msg: "Failed to read POST Response for JSON-RPC.", parent: exc) @@ -101,18 +106,15 @@ proc connect*(client: RpcHttpClient, url: string) if client.httpAddress.isErr: raise newException(RpcAddressUnresolvableError, client.httpAddress.error) -proc connect*(client: RpcHttpClient, address: string, port: Port, secure=false) {.async.} = - var uri = initUri() - if secure: - uri.scheme = "https" - else: - uri.scheme = "http" - uri.hostname = address - uri.port = $port +proc connect*(client: RpcHttpClient, address: string, port: Port, secure: bool) {.async.} = + var uri = Uri( + scheme: if secure: "https" else: "http", + hostname: address, + port: $port) let res = getAddress(client.httpSession, uri) if res.isOk: client.httpAddress = res else: raise newException(RpcAddressUnresolvableError, res.error) - \ No newline at end of file + diff --git a/tests/testhttp.nim b/tests/testhttp.nim index cb42643..4aabe8d 100644 --- a/tests/testhttp.nim +++ b/tests/testhttp.nim @@ -6,7 +6,7 @@ const TestsCount = 100 proc simpleTest(address: string, port: Port): Future[bool] {.async.} = var client = newRpcHttpClient() - await client.connect(address, port) + await client.connect(address, port, secure = false) var r = await client.call("noParamsProc", %[]) if r.getStr == "Hello world": result = true @@ -15,7 +15,7 @@ proc continuousTest(address: string, port: Port): Future[int] {.async.} = var client = newRpcHttpClient() result = 0 for i in 0..