This is the HTTP client library (and an associated test executable) written in Zig according to the given spec.
This HTTP client is dependent on the Tardy runtime and is meant to be run on Zig 0.14.0
Client is the type that you interact with. You create a Client by providing it with an endpoint and it will both resolve the IP from the host-name and attempt to connect to it.
var client = try Client.init(rt, rt.allocator, .{ .name = "httpbin.io" });Clients also allow for global headers to be attached to them.
try client.header("X-Auth", "password");Request is what you pass into a Client when you call fetch() and begin an HTTP Request/Response. Requests come with a helper, RequestBuilder, that makes it slightly more ergonomic to create a Request.
var builder = try RequestBuilder.init(rt.allocator, 32);
var request = try builder.method(.GET).path(path).build();You are also able to provide headers that are scoped to a Request and these have precedence over the global Client headers.
var request = try builder.method(.GET).path(path).header("X-Auth", "password2").build();After a Request is sent, you get returned a Response. This Response provides a status, headers and an optional body. This response owns itself and you must call deinit() on it after you are done with it.
var response = try client.fetch(rt, request);
defer response.deinit();-
zig buildwill build the test executable namedclient. -
zig build runwill run the given example, which will GET the threehttpbin.ioURLs and print their body intostdout. -
zig build testwill run a set of unit tests.
- For TLS, it would likely be best to provide that as an opaque wrapper around the
Sockettype, allowing for the same logic to be used with or without TLS. There is an experimental opaque wrapper for Tardy called secsock that operates just like this (currently only with TLS server support) and is used in zzz. - Note about
Tardy, it operates as a 'thread-per-core' async runtime meaning that it doesn't share anything across threads. It is running in single threaded mode to prevent running theentryfn for each thread.