Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SSL support so that jennet can now listen on SSL directly. #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,61 @@ actor Main

if server is None then env.out.print("bad routes!") end
```

### Serving over SSL

Refer to the [SSLContext](https://ponylang.github.io/net_ssl/net_ssl-SSLContext/) documentation in [net_ssl](https://ponylang.github.io/net_ssl/) for SSL / TLS configuration.

```pony
use "net"
use "files"
use "net_ssl"
use "http_server"
use "jennet"

actor Main
new create(env: Env) =>
let tcplauth: TCPListenAuth = TCPListenAuth(env.root)
let fileauth: FileAuth = FileAuth(env.root)

try
let sslctx: SSLContext =
try
recover
SSLContext
.>set_cert(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.>set_cert(
.> set_cert(

FilePath(fileauth, "cert.pem"),
FilePath(fileauth, "key.pem")
)?
end
else
env.err.print("Unable to configure SSL")
error
end

let server =
Jennet(tcplauth, env.out)
.> get("/", H)
.> sslctx(sslctx)
.serve(ServerConfig(where port' = "8443"))

if server is None then
env.err.print("bad routes!")
error
end
else
env.err.print("Server unable to start")
end

primitive H is RequestHandler
fun apply(ctx: Context): Context iso^ =>
let body = "Hello".array()
ctx.respond(
StatusResponse(
StatusOK,
[("Content-Length", body.size().string())]
),
body
)
consume ctx
```
20 changes: 20 additions & 0 deletions examples/ssl/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDLzCCAhegAwIBAgIJAI/o78XcM9NqM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BZD
BccR
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions examples/ssl/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAzMtF7msdRI9YgjnpmHYi8GifwfFLAh9s7SVTx8sOh0scGljW
q4if3duEC7hfWBce16tqBCSu1ifs0H55/o55UFinwwRUmoK3kpjiCkWGJCsmtycK
pbirF3KsV8//9D5C+oH0TawrGz1Fbo0z9z5XuXkStYpTsXNCmicLUXWFRD1nFK79
Ar7lO6gyjMwYdi7d7TwvCelTaryuW7CwXYXMRVPjoy+uVRBoyHN96RzBpEIrZTKb
BaJal2LzLfyTzXX97azxIPHhh3ivfuwMZmkaOFi2hbGMleRLcGvKMfqd4Bi/yDqR
NjwwtUqidh3Zy4FytSPyxkZazULLH3sP636nWQIDAQABAoIBADvxuAt8gPmjd8XO
i8ibs8ho53JOXaVGa4zSoz5U+nCxlI1FubhF6n13FqSTmBzhz55TR1nlHuQClbfF
fZH8RBg3iwtzRgxf/LnFVEyrYwNNOizcGaq+bh4T68xcTtBANTy8MzVVEt0LRzp/
zFkvf2ADx20qCyti0HjsusxiONrrFBS6O7DTss9GFRN5lfsBP49FSl3/r0e+fUaO
/6ImRVJvmAU92+dQ4VOi4c+laeFDmhw1MQSpvHOz3Vrslin9NSR6TMQ7rH9I/QKL
+YQULz7fYINRwt8FBa/p94qjGIqlNz6UdTPGvaEMlVyYo1ziTDZlCHnAYWk3wiFW
s13Jct0CgYEA7PxGYpZCyzntd1wvOradxh7EbUrF68lhfDt6BgOBV8Ur3/4SvcwB
blc4nEf5/p/x9w8ICe4uyA0bsGwqGppIhptrPe0uLApidUrxNeqY5zsebriR/gE6
mq5/cmScFTnywfAb/93qK5SMzeWKfocPozb5YqO8oJpPHPhVZYAj16MCgYEA3TnH
14szOoQSq8ul2tFdu7s9S+sfi1l7ejqiHjZVcMST+GyEk3ox5Ns24pwkRq80cQhp
1UYfapeeiYroW7nkVTqJUfZbilFO6eKCBfpzC7NGHMs86er3pLUrfHQKq6S5RSZr
W9CB7+Kd9OXMOIJ2yXcF+OyEzQH5ESwxoHvrJNMCgYEAz/o6Hw01wzqsP2Mkg8d+
QUABLNVBS0NpjWh5F0v+ODPu5F4KvoyJ+PcM1IKKUD64UBTd/jEM1z5BvZu/e6dI
3PEKtccwDTBz5fIGLEYdOFj2xT6vyRX1A4j+ijrni+1WMYNyXyO2/dYZmBzJZW9M
dvWo+TsvMFcb9RWvhCtnyTcCgYAyK9t9r60TlUZivdHEeX0HiWRSZmWGBeoyA0An
F+1yoLJqQbojdDAClhCxffXgLfX3uI+/9aJEW1RyHxWpT9RP2/Guq++AxAXglyUj
0/PpcGPzPch3yHkXWpsdI3gUC0yVOSxZ60S8salfFAqnujbUY/Dvzjwj/lGNKneq
zM+8TwKBgHjQforDq00DhDAqzqZDDVd8OZ9PLp3wXL+TaidTMmU7Fob5spyQe0gn
LqVIyGFYoRbW57pLgSEXRh+Zqw1AAKmMYoc3/pKM60rgOQxMXEemFAJ8zuDQ5L6y
1dR3ak4ueRcSFpa6x8/STsgIuL38HMNXhStsJB9whHAFB3U6Wo2/
-----END RSA PRIVATE KEY-----
51 changes: 51 additions & 0 deletions examples/ssl/main.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use "net"
use "files"
use "net_ssl"
use "http_server"
use "../../jennet"

actor Main
new create(env: Env) =>
let tcplauth: TCPListenAuth = TCPListenAuth(env.root)
let fileauth: FileAuth = FileAuth(env.root)

try
let sslctx: SSLContext =
try
recover
SSLContext
.>set_cert(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.>set_cert(
.> set_cert(

FilePath(fileauth, "cert.pem"),
FilePath(fileauth, "key.pem")
)?
end
else
env.err.print("Unable to configure SSL")
error
end

let server =
Jennet(tcplauth, env.out)
.> get("/", H)
.> sslctx(sslctx)
.serve(ServerConfig(where port' = "8443"))

if server is None then
env.err.print("bad routes!")
error
end
else
env.err.print("Server unable to start")
end

primitive H is RequestHandler
fun apply(ctx: Context): Context iso^ =>
let body = "Hello".array()
ctx.respond(
StatusResponse(
StatusOK,
[("Content-Length", body.size().string())]
),
body
)
consume ctx
15 changes: 14 additions & 1 deletion jennet/jennet.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use "collections"
use "files"
use "http_server"
use "net"
use "net_ssl"
use "valbytes"

class iso Jennet
Expand All @@ -12,6 +13,7 @@ class iso Jennet
let _routes: Array[_Route] iso = recover Array[_Route] end
var _notfound: _HandlerGroup = _HandlerGroup(_DefaultNotFound)
var _host: String = "Jennet" // TODO get host from server
var _sslctx: (SSLContext | None) = None

new iso create(
auth: TCPListenAuth val,
Expand All @@ -26,6 +28,12 @@ class iso Jennet
_out = out
_auth = auth

fun ref sslctx(sslctx': SSLContext) =>
_sslctx = sslctx'
"""
Provide a valid SSLContext to enable SSL.
"""

fun ref get(
path: String,
handler: RequestHandler,
Expand Down Expand Up @@ -127,7 +135,12 @@ class iso Jennet
let mux = try _Mux(_routes)? else return None end
if dump_routes then _out.print(mux.debug()) end
let router_factory = _RouterFactory(consume mux, _responder, _notfound)
Server(_auth, _ServerInfo(_out, _responder), router_factory, config)
match _sslctx
| None =>
Server(_auth, _ServerInfo(_out, _responder), router_factory, config)
| let sslcontext: SSLContext =>
Server(_auth, _ServerInfo(_out, _responder), router_factory, config, sslcontext)
end

fun ref _add_route(method: String, path: String,
handler: RequestHandler, middlewares: Array[Middleware] val)
Expand Down
Loading