Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

frmdstryr/zhp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZHP

status

A (work in progress) Http server written in Zig.

If you have suggestions on improving the design please feel free to comment!

Features

  • A zero-copy parser and aims to compete with these parser_benchmarks while still rejecting nonsense requests. It currently runs around ~1000MB/s.
  • Regex url routing thanks to ctregex
  • Struct based handlers where the method maps to the function name
  • A builtin static file handler, error page handler, and not found page handler
  • Middleware support
  • Parses forms encoded with multipart/form-data
  • Streaming responses
  • Websockets

See how it compares in the http benchmarks done by kprotty (now very old).

It's a work in progress... feel free to contribute!

Demo

Try out the demo at https://zhp.codelv.com.

Note: If you try to benchmark the server it'll ban you, please run it locally or on your own server to do benchmarks.

To make and deploy your own app see:

Example

See the example folder for a more detailed example.

const std = @import("std");
const web = @import("zhp");

pub const io_mode = .evented;
pub const log_level = .info;

const MainHandler = struct {
    pub fn get(self: *MainHandler, request: *web.Request, response: *web.Response) !void {
        _ = self;
        _ = request;
        try response.headers.put("Content-Type", "text/plain");
        _ = try response.stream.write("Hello, World!");
    }

};

pub const routes = [_]web.Route{
    web.Route.create("home", "/", MainHandler),
};

pub const middleware = [_]web.Middleware{
    web.Middleware.create(web.middleware.LoggingMiddleware),
};

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(!gpa.deinit());
    const allocator = gpa.allocator();

    var app = web.Application.init(allocator, .{.debug=true});
    defer app.deinit();

    try app.listen("127.0.0.1", 9000);
    try app.start();
}