Hello,
I’m still learning Zig and I’m quite sure that I’m somewhat blindfold and I don’t got it.
But for now I need some help.
I’m using the Zap library for providing an embedded web server (Zig Zap ).
I’m just going to enable the tls functionality, which is working so far, when I’m providing the path to my certificate file und key file by creating a string literal and assigning that string literal.
...
const CERT_FILE = "/workspaces/oauth2_02/certificates/srv-git2.crt";
const KEY_FILE = "/workspaces/oauth2_02/certificates/srv-git2.key";
...
const tls = try zap.Tls.init(.{
.server_name = "localhost:3000",
.public_certificate_file = CERT_FILE,
.private_key_file = KEY_FILE,
});
...
That’s working.
Now I’m trying to read the file path from an .env file:
.env
CLIENT_CERTIFICATE_FILE="/workspace/oauth2_02/certificates/srv-git2.crt"
CLIENT_CERTIFICATE_KEY="/workspace/oauth2_02/certificates/srv-it2.key"
.zig
...
somehandler.clientCertificate = try std.process.getEnvVarOwned(allocator, "CLIENT_CERTIFICATE_FILE"),
...
const tls = try zap.Tls.init(.{
.server_name = "localhost:3000",
.public_certificate_file = somehandler.clientCertificate;
.private_key_file = KEY_FILE,
});
That is NOT working!
tls.public_certificate_file
is type of [*:0]const u8; a C string.
somehandler.clientCertificate
is type of const 8; a Zig string.
How can I convert that Zig string into a C string?
I’ve tried all this bufPrintZ, allocPrintZ, dupeZ stuff.
But I don’t got it working.
Maybe someone can just show me the right conversion.
Any help is realy appriciated!
BR SMF
What type of somehandler.clientCertificate
is?
Probably, You can use dupeZ
to achive it.
const tls = try zap.Tls.init(.{
.server_name = "localhost:3000",
.public_certificate_file = try allocator.dupe(u8, somehandler.clientCertificate);
.private_key_file = KEY_FILE,
});
Hi ktz_alias,
thank you for your help.
somehandler.clientCertificate
is type of []const u8
.
Trying
.public_certificate_file = try allocator.dupe(u8, somehandler.clientCertificate);
results in the following compile error:
src/main.zig:63:10: error: expected type '?[*:0]const u8', found '[]u8'
.public_certificate_file = try allocator.dupe(u8, oauth2Handler.clientCertificate),
~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Trying
.public_certificate_file = try allocator.dupe([]const u8, oauth2Handler.clientCertificate),
leads to the following compile error:
src/main.zig:63:80: error: expected type '[]const []const u8', found '[]const u8'
.public_certificate_file = try allocator.dupe([]const u8, oauth2Handler.clientCertificate),
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
I don’t understand this error.
But maybe there is another idea I haven’t seen until now.
BR SMF
Sze
February 25, 2025, 5:53pm
4
mf-in-mun:
results in the following compile error:
src/main.zig:63:10: error: expected type '?[*:0]const u8', found '[]u8'
.public_certificate_file = try allocator.dupe(u8, oauth2Handler.clientCertificate),
~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const str = try allocator.dupeZ(u8, somehandler.clientCertificate);
...
.public_certificate_file = str.ptr,
dupeZ
to get a copy of the string that is zero terminated, .ptr
to access the multi-item-pointer [*:0]u8
, which can be automatically coerced to [*:0]const u8
and ?[*:0]const u8
.
mf-in-mun:
Trying
.public_certificate_file = try allocator.dupe([]const u8, oauth2Handler.clientCertificate),
leads to the following compile error:
src/main.zig:63:80: error: expected type '[]const []const u8', found '[]const u8'
.public_certificate_file = try allocator.dupe([]const u8, oauth2Handler.clientCertificate),
~~~~~~~~
dupe
expects the type of the slice element as first parameter not the type of the resulting slice. So if you pass it T
dupe returns a []T
and dupeZ returns a [:0]T
.
3 Likes
Hi Sze,
yepp, that does the trick
Thank you very much for your help and your explanation.
I realy appriciated that.
BR SMF
1 Like