hendriknielaender/http2.zig
🌐 HTTP/2 server for zig
main.tar.gz
WARNING
Still work in progress.
Add http2.zig to your build.zig.zon
:
.{
.name = "my-project",
.version = "1.0.0",
.dependencies = .{
.http2 = .{
.url = "https://github.com/hendriknielaender/http2.zig/archive/main.tar.gz",
.hash = "1220...", // Use `zig fetch` to get the hash
},
},
}
Import in your build.zig
:
const http2_dep = b.dependency("http2", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("http2", http2_dep.module("http2"));
const std = @import("std");
const http2 = @import("http2");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Initialize the HTTP/2 system
try http2.init(allocator);
defer http2.deinit();
// Configure and create server
const config = http2.Server.Config{
.address = try std.net.Address.resolveIp("127.0.0.1", 3000),
.max_connections = 1000,
};
var server = try http2.Server.init(allocator, config);
defer server.deinit();
std.log.info("HTTP/2 server listening on {}", .{config.address});
// Run the server
try server.run();
}
TBD
pub const Server.Config = struct {
/// Address to bind to
address: std.net.Address,
/// Maximum concurrent connections (default: 1000)
max_connections: u32 = 1000,
/// Buffer size per connection (default: 32KB)
buffer_size: u32 = 32 * 1024,
};
// Create a new server
pub fn init(allocator: Allocator, config: Config) !Server
// Clean up server resources
pub fn deinit(self: *Server) void
// Run the server event loop (blocks)
pub fn run(self: *Server) !void
// Stop the server
pub fn stop(self: *Server) void
// Get server statistics
pub fn getStats(self: *Server) ServerStats
pub const ServerStats = struct {
total_connections: u64,
active_connections: u32,
requests_processed: u64,
};
# Build the library
zig build
# Run tests
zig build test
# Build with optimizations
zig build -Doptimize=ReleaseFast
# Run the hello world example
zig build run-hello
# Run the benchmark server
cd benchmarks && zig build run
cd benchmarks
./bench.sh
http2.zig implements core HTTP/2 features:
Contributions are welcome! Please ensure:
Areas for contribution:
MIT License - see LICENSE for details.