Skip to content

Commit

Permalink
make ssl optional
Browse files Browse the repository at this point in the history
mainly for grpc insecure connections. It's not supported by web-browsers
  • Loading branch information
nitely committed Aug 11, 2024
1 parent b1fe100 commit 2b874bb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 80 deletions.
63 changes: 0 additions & 63 deletions src/hyperx/bin.nim

This file was deleted.

11 changes: 6 additions & 5 deletions src/hyperx/client.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## HTTP/2 client
## WIP

when not defined(ssl):
{.error: "this lib needs -d:ssl".}
Expand Down Expand Up @@ -49,20 +48,22 @@ proc defaultSslContext(): SslContext {.raises: [HyperxConnError].} =
return sslContext

when not defined(hyperxTest):
proc newMySocket(): MyAsyncSocket {.raises: [HyperxConnError].} =
proc newMySocket(ssl: bool): MyAsyncSocket {.raises: [HyperxConnError].} =
try:
result = newAsyncSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, buffered = true)
wrapSocket(defaultSslContext(), result)
if ssl:
wrapSocket(defaultSslContext(), result)
except CatchableError as err:
debugInfo err.getStackTrace()
debugInfo err.msg
raise newHyperxConnError(err.msg)

proc newClient*(
hostname: string,
port = Port 443
port = Port 443,
ssl = true
): ClientContext {.raises: [HyperxConnError].} =
newClient(ctClient, newMySocket(), hostname, port)
newClient(ctClient, newMySocket(ssl), hostname, port)

type
HttpMethod* = enum
Expand Down
2 changes: 1 addition & 1 deletion src/hyperx/clientserver.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Functionality shared beetwen client and server
## Functionality shared between client and server

when not defined(ssl):
{.error: "this lib needs -d:ssl".}
Expand Down
20 changes: 12 additions & 8 deletions src/hyperx/server.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## HTTP/2 server
## WIP

when not defined(ssl):
{.error: "this lib needs -d:ssl".}
Expand Down Expand Up @@ -53,12 +52,14 @@ proc defaultSslContext(

when not defined(hyperxTest):
proc newMySocket(
ssl: bool,
certFile = "",
keyFile = ""
): MyAsyncSocket {.raises: [HyperxConnError].} =
try:
result = newAsyncSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, buffered = true)
wrapSocket(defaultSslContext(certFile, keyFile), result)
if ssl:
wrapSocket(defaultSslContext(certFile, keyFile), result)
except CatchableError as err:
debugInfo err.getStackTrace()
debugInfo err.msg
Expand All @@ -75,10 +76,12 @@ proc newServer*(
hostname: string,
port: Port,
sslCertFile = "",
sslKeyFile = ""
sslKeyFile = "",
ssl = true
): ServerContext =
ServerContext(
sock: newMySocket(
ssl,
certFile = sslCertFile,
keyFile = sslKeyFile
),
Expand Down Expand Up @@ -118,11 +121,12 @@ proc recvClient*(server: ServerContext): Future[ClientContext] {.async.} =
try:
# note OptNoDelay is inherited from server.sock
let sock = await server.sock.accept()
when not defined(hyperxTest):
doAssert not sslContext.isNil
wrapConnectedSocket(
sslContext, sock, handshakeAsServer, server.hostname
)
if server.sock.isSsl:
when not defined(hyperxTest):
doAssert not sslContext.isNil
wrapConnectedSocket(
sslContext, sock, handshakeAsServer, server.hostname
)
result = newClient(ctServer, sock, server.hostname)
except CatchableError as err:
debugInfo err.getStackTrace()
Expand Down
8 changes: 5 additions & 3 deletions src/hyperx/testsocket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type
isConnected*: bool
hostname*: string
port*: Port
isSsl*: bool

proc newMySocket*(certFile = "", keyFile = ""): TestSocket =
proc newMySocket*(ssl: bool, certFile = "", keyFile = ""): TestSocket =
TestSocket(
recvData: newQueue[seq[byte]](1000),
sentData: newQueue[seq[byte]](1000),
Expand All @@ -28,7 +29,8 @@ proc newMySocket*(certFile = "", keyFile = ""): TestSocket =
sentIdx: 0,
isConnected: false,
hostname: "",
port: Port 0
port: Port 0,
isSsl: ssl
)

proc putRecvData*(s: TestSocket, data: seq[byte]) {.async.} =
Expand Down Expand Up @@ -108,7 +110,7 @@ proc listen*(s: TestSocket) =
s.isConnected = true

proc accept*(s: TestSocket): Future[TestSocket] {.async.} =
result = newMySocket()
result = newMySocket(s.isSsl)
result.isConnected = true

proc wrapConnectedSocket*(
Expand Down

0 comments on commit 2b874bb

Please sign in to comment.