Skip to content
This repository was archived by the owner on Sep 18, 2023. It is now read-only.

Repository files navigation

HTTP

HTTP core types for Zig inspired by Rust http.

Build StatusLicenseRequirements

⚠️ I'm currently renovating an old house which does not allow me to work on requestz, h11 and http anymore. Feel free to fork or borrow some ideas if there are any good ones :)

Installation

http is available on astrolabe.pm via gyro

gyro add ducdetronquito/http

Usage

Create an HTTP request

constRequest=@import("http").Request;
conststd=@import("std");
varrequest=tryRequest.builder(std.testing.allocator)
.get("https://ziglang.org/")
.header("GOTTA-GO", "FAST")
.body("");
deferrequest.deinit();

Create an HTTP response

constResponse=@import("http").Request;
constStatusCode=@import("http").StatusCode;
conststd=@import("std");
varresponse=tryResponse.builder(std.testing.allocator)
.status(.Ok)
.header("GOTTA-GO", "FAST")
.body("");
deferresponse.deinit();

API Reference

Structures

Headers

An HTTP header list
// The default constructorfninit(allocator: *Allocator) Headers
// Add a header name and valuefnappend(self: *Headers, name: []constu8, value: []constu8) !void
// Retrieve the first matching headerfnget(self: Headers, name: []constu8) ?Header
// Retrieve a list of matching headersfnlist(self: Headers, name: []constu8) ![]Header
// Retrieve the number of headersfnlen(self: Headers) usize
// Retrieve all headersfnitems(self: Headers) []Header

Header issues are tracked here: #2

Request

An HTTP request object produced by the request builder.
constRequest=struct {
method: Method,
uri: Uri,
version: Version,
headers: Headers,
body: []constu8,
};
// The default constructor to start building a requestfnbuilder(allocator: *Allocator) RequestBuilder
// Release the memory allocated by the headersfndeinit(self: *Request) void

RequestBuilder

The request builder.
// The default constructordefault(allocator: *Allocator) RequestBuilder
// Set the request's payload.// This function returns the final request objet or a potential error// collected during the build stepsfnbody(self: *RequestBuilder, value: []constu8) RequestError!Request
// Shortcut to define a CONNECT request to the provided URIfnconnect(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define a DELETE request to the provided URIfndelete(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define a GET request to the provided URIfnget(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define an HEAD request to the provided URIfnhead(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Set a request header name and valuefnheader(self: *RequestBuilder, name: []constu8, value: []constu8) *RequestBuilder
// Set the request's methodfnmethod(self: *RequestBuilder, value: Method) *RequestBuilder
// Shortcut to define an OPTIONS request to the provided URIfnoptions(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define a PATCH request to the provided URIfnpatch(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define a POST request to the provided URIfnpost(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define a PUT request to the provided URIfnput(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Shortcut to define a TRACE request to the provided URIfntrace(self: *RequestBuilder, _uri: []constu8) *RequestBuilder
// Set the request's URIfnuri(self: *RequestBuilder, value: []constu8) *RequestBuilder
// Set the request's protocol versionfnversion(self: *RequestBuilder, value: Version) *RequestBuilder

Response

An HTTP response object produced by the response builder.
constResponse=struct {
status: StatusCode,
version: Version,
headers: Headers,
body: []constu8,
};
// The default constructor to start building a responsefnbuilder(allocator: *Allocator) ResponseBuilder

ResponseBuilder

The response builder.
// The default constructordefault(allocator: *Allocator) ResponseBuilder
// Set the response's payload.// This function returns the final response objet or a potential error// collected during the build stepsfnbody(self: *ResponseBuilder, value: []constu8) ResponseError!Response
// Set a response header name and valuefnheader(self: *ResponseBuilder, name: []constu8, value: []constu8) *ResponseBuilder
// Set the response's status codefnstatus(self: *ResponseBuilder, value: StatusCode) *ResponseBuilder
// Set the response's protocol versionfnversion(self: *ResponseBuilder, value: Version) *ResponseBuilder

Uri

A valid URI object

Read more

Enumerations

Method

The available request methods.
  • Connect
  • Custom
  • Delete
  • Get
  • Head
  • Options
  • Patch
  • Post
  • Put
  • Trace

StatusCode

The available response status codes.

A lot; the list is available on MDN.

Version

The available protocol versions.
  • Http09
  • Http10
  • Http11
  • Http2
  • Http3

Errors

HeadersError
  • OutOfMemory
  • Invalid
RequestError
ResponseError
  • OutOfMemory

License

http is released under the BSD Zero clause license. 🎉🍻

The URI parser is a fork of Vexu's zuri under the MIT License.

About

HTTP core types for Zig 🦴

Topics

Resources

Stars

99 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages