adding many things, adding docs

main
flopetautschnig 1 year ago committed by GitHub
parent 0e41b40852
commit e7340bb3f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,4 @@
const std = @import("std");
const status = @import("status.zig");
const allocator = std.heap.page_allocator;
pub fn main() !void {

@ -1,3 +1,4 @@
/// Representing the HTTP status
pub const Status = enum(u32) {
// INFORMATION RESPONSES
@ -18,7 +19,7 @@ pub const Status = enum(u32) {
ALREADY_REPORTED = 208,
IM_USED = 226,
//REDIRECTION MESSAGES
// REDIRECTION MESSAGES
MULTIPLE_CHOICES = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
@ -60,7 +61,7 @@ pub const Status = enum(u32) {
REQUESTS_HEADER_FIELDS_TOO_LARGE = 431,
UNAVAILABLE_FOR_LEGAL_REASONS = 451,
//SERVER RESPONSES
// SERVER RESPONSES
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMETED = 501,
BAD_GATEWAY = 502,
@ -73,6 +74,8 @@ pub const Status = enum(u32) {
NOT_EXTENDED = 510,
NETWORK_AUTHENTICATION_REQUIRED = 511,
/// Returns a stringified version of a HTTP status.
/// E.g. `Status.OK.stringify()` will be "200 OK".
pub fn stringify(self: Status) []const u8 {
switch (self) {
Status.CONTINUE => return "100 Continue",
@ -140,4 +143,11 @@ pub const Status = enum(u32) {
Status.NETWORK_AUTHENTICATION_REQUIRED => return "511 Network Authentication Required",
}
}
/// Parses a given u32 code and returns the corresponding `Status`.
/// E.g. `Status.code(200)` will return `Status.OK`.
/// The program will panic if the passed code does not exist.
pub fn code(n: u32) Status {
return @intToEnum(Status, n);
}
};

@ -1,20 +1,42 @@
const std = @import("std");
const tuple = std.meta.Tuple;
const stat = @import("./status.zig");
pub const Route = tuple(&.{ []const u8, *const fn () Response });
/// Route is a touple that consists of the path and the function that shall handle it.
/// e.g. `const rt = Route{"/home", home};`
/// It it usual that a webapp handles more than one path so you can declare an array of `Route`
/// e.g. `const rt =[_]Route{.{"/index", index}, .{"/home", home}};`
pub const Route = tuple(&.{ []const u8, *const fn (Request) Response });
pub const Header = tuple(&.{ []const u8, *const fn () Response });
/// A header of a `Request` or a `Response`.
/// It is usual that more than one is sent, so you can declare an array.
pub const Header = tuple(&.{ []const u8, []const u8 });
pub const HTTP_Version = enum([]const u8) { HTTP1_1 = "HTTP/1.1", HTTP2 = "HTTP/" };
/// The HTTP Version.
pub const HTTP_Version = enum { HTTP1_1, HTTP2 };
/// Represents the Method of a request or a response.
pub const Method = enum { GET, POST, PUT, HEAD, DELETE, CONNECT, OPTIONS, TRACE, PATCH };
/// Represents a standard http-Request sent by the client.
pub const Request = struct {
/// The Request Method, e.g. "GET"
method: Method,
/// HTTP-Version of the Request sent by the client
httpVersion: HTTP_Version,
headers: std.ArrayList,
body: std.ArrayList,
/// Represents the request headers sent by the client
headers: []Header,
/// Represents the request body sent by the client
body: []u8,
};
/// Represents a standard http-Response sent by the webapp (server).
/// It is the return type of every handling function.
pub const Response = struct {
httpVersion: HTTP_Version = HTTP_Version.HTTP1_1,
headers: std.ArrayList,
body: std.ArrayList,
/// Response status, default is "200 OK"
status: stat.Status = stat.Status.OK,
/// Response eaders sent by the server
headers: []Header,
/// Response body sent by the server
body: []u8,
};

Loading…
Cancel
Save