Release v0.2.2
Pre-releaseZAP Release v0.2.2
Updates
TLS / HTTPS / openssl support!!!
Thanks to @ColaNova's (GitHub) work, Zap can now utilize facil.io's openssl support.
In the README, at the bottom of the list of examples, there is now a super simple https
example.
It's super simple:
const tls = zap.fio_tls_new(
"localhost:4443",
CERT_FILE,
KEY_FILE,
null, // key file is not password-protected
);
That tls
data is then passed to the SimpleHttpListener
.
The example requires -Dopenssl=true
on the command line. This is so that zap doesn't depend on openssl unconditionally.
Here's what happened:
openssl in facil.io
fio patches from https://github.com/CoalNova/facil.io 0.7.6-zapped branch:
* b0a7b00b - (HEAD -> 0.7.6-zapdate, coalnova/0.7.6-zapdate) Update to description (7 weeks ago) <coalnova>
* 115d07d6 - Update to delocalize OpenSSL lines (7 weeks ago) <coalnova>
* 73619a8b - Update to organize build options for TLS (7 weeks ago) <coalnova>
* 16024c0d - Update to build settings (7 weeks ago) <coalnova>
* c487bb00 - Update to fix to library name (7 weeks ago) <coalnova>
* a87818fc - Update to flag HAVE_OPENSSL as build feature (8 weeks ago) <coalnova>
* 968993b4 - Update to co/remove TODO: delete me! (8 weeks ago) <coalnova>
* 094ac99d - Update to differentiate exitcodes in TLS functions (8 weeks ago) <coalnova>
* 6d22411a - Update to expose both sets of tls functions (8 weeks ago) <coalnova>
* 007f7b19 - Update switched fio_tls_missing to fio_tls_openssl (9 weeks ago) <coalnova>
* 026c92fc - Update to expose TLS C and Header files. (9 weeks ago) <coalnova>
Changes to CoalNova's approach
!Important!: @CoalNova commented out one #if HAVE_OPENSSL
in the C code. I will not do that as the build.zig
will set this flag anyway. If the flag isn't specified, facil.io will compile like it did before the introduction of zap's OpenSSL support.
Addition: I had to replace the #if HAVE_OPENSSL
in question to #ifdef HAVE_OPENSSL
or else clang would complain. Maybe this error was the motivation for @CoalNova to remove the #if
.
Also, since we don't install headers of facil.io anymore (as zap is the only consumer), I skipped installing facil.io's tls headers.
Changes to zap
Basically 2 patches:
- [Update to include basic TLS functions](https://github.com/CoalNova/zap/commit/124a3134ca71bf41635320c0bc3c098a65aba931.patch)
- [Update to expose further functions for TLS/SSL](https://github.com/CoalNova/zap/commit/14b05451989ec66e84feae9b8efc52e4f8bed1e8.patch)
Used those but skipped the build.zig.zon stuff.
OpenSSL needs to be hidden behind a custom build option orelse all projects that depend on zap will also depend on a working openssl + libcrypto dev setup: having headers and lib installed on the system.
- make openssl a build option: -Dopenssl=true
The Example
Trying out openssl from the commandline first
Trying stuff from the command line first.
Creating the certificate and key file:
$ openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout mykey.pem -out mycert.pem
Serving the current dir:
$ openssl s_server -accept 4443 -cert mycert.pem -key mykey.pem -WWW
Issuing an HTTPS request:
$ curl -v -k https://localhost:4443/build.zig
The zap https example
$ zig build -Dopenssl=true https
$ zig build -Dopenssl=true run-https
It's super simple:
const tls = zap.fio_tls_new(
"localhost:4443",
CERT_FILE,
KEY_FILE,
null, // key file is not password-protected
);
That tls
data is then passed to the SimpleHttpListener
.
Using it
To use in your own projects, put this dependency into your build.zig.zon
:
// zap v0.2.2
.zap = .{
.url = "https://github.com/zigzap/zap/archive/refs/tags/v0.2.2.tar.gz",
.hash = "1220124603a1d655a772197781547c8c3d1ee58f263be29a6cd8a4b93a26407ae68b",
}
Here is a complete build.zig.zon
example:
.{
.name = "My example project",
.version = "0.0.1",
.dependencies = .{
// zap v0.2.2
.zap = .{
.url = "https://github.com/zigzap/zap/archive/refs/tags/v0.2.2.tar.gz",
.hash = "1220124603a1d655a772197781547c8c3d1ee58f263be29a6cd8a4b93a26407ae68b",
}
}
}
Then, in your build.zig
's build
function, add the following before exe.install()
:
const zap = b.dependency("zap", .{
.target = target,
.optimize = optimize,
});
exe.addModule("zap", zap.module("zap"));
exe.linkLibrary(zap.artifact("facil.io"));