Skip to content

crispy-strawberry/http.zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http.zig

Creating an HTTP/1.1 compliant server in Zig.

Using

Let's look at how to use the http package in your own project.

  • Run the following command inside your project root -
    zig fetch git+https://github.com/crispy-strawberry/http.zig#main --save=http
    
  • Open build.zig add the following lines -
    const http = b.dependency("http", .{
      .target = target,
      .optimize = optimize,
    });
    
    exe.addModule("http", http.module("http"));  
  • Enjoy!
    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();
      }
    }

Options provided by the library

The library provides two options to change the default settings.

  1. Firstly, it provides REQ_SIZE to choose the max size of the request stored on the stack. Default is 4kb.
  2. It also provides an option to choose the max size in bytes of the HTTP method. For example, 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),
});

License

The library is dual licensed under MPL-2.0 or APACHE-2.0. Choose at your own discretion.

Contributing

Please make pull requests to https://codeberg.org/crispy-strawberry/http.zig

About

An HTTP/1.1 compliant client and server in Zig

Topics

Resources

License

Apache-2.0, MPL-2.0 licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MPL-2.0
LICENSE-MPL

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages