Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LightPanda Assessment (HTTP Client)

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();

Compilation

  • zig build will build the test executable named client.

  • zig build run will run the given example, which will GET the three httpbin.io URLs and print their body into stdout.

  • zig build test will run a set of unit tests.

Notes

  1. For TLS, it would likely be best to provide that as an opaque wrapper around the Socket type, 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.
  2. 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 the entry fn for each thread.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages