crispy-strawberry/http.zig
An HTTP/1.1 compliant client and server in Zig
Creating an HTTP/1.1 compliant server in Zig.
Let's look at how to use the http
package in your own project.
zig fetch git+https://github.com/crispy-strawberry/http.zig#main --save=http
build.zig
add the following lines -const http = b.dependency("http", .{
.target = target,
.optimize = optimize,
});
exe.addModule("http", http.module("http"));
const std = @import("std");
const http = @import("http");
const Server = http.Server;
pub fn main() !void {
var server = Server.init(allocator, .{
.reuse_port = true,
});
defer server.deinit();
try server.listen(std.net.Address.initIp4([_]u8{ 127, 0, 0, 1 }, 8000));
while (true) {
try server.accept();
}
}
The library provides two options to change the default settings.
REQ_SIZE
to choose the max size of the request stored
on the stack. Default is 4kb.GET
is 3 bytes and POST
is 4 bytes. The default
maximum size for methods is 24 bytes.To change these, you can do -
const http = b.dependency("http", .{
.target = target,
.optimize = optimize,
.REQ_SIZE = @as(usize, 2048),
.MAX_METHOD_SIZE = @as(u16, 32),
});
The library is dual licensed under MPL-2.0
or APACHE-2.0
.
Choose at your own discretion.
Please make pull requests to https://codeberg.org/crispy-strawberry/http.zig