add fn to status, changed Request Response structs

This commit is contained in:
flopetautschnig 2023-04-16 17:31:53 +02:00 committed by GitHub
parent 1f171b59c0
commit bc0f05feda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 16 deletions

View file

@ -1,5 +1,5 @@
const std = @import("std");
const testing = std.testing;
const status = @import("status.zig");
const allocator = std.heap.page_allocator;
@ -28,7 +28,7 @@ pub fn main() !void {
defer buffer.deinit();
var chunk_buf: [4096]u8 = undefined;
// Collect max 4096 byte of data from the stream into the chunk_buf. Then add it
// Collect max 4096 bytes of data from the stream into the chunk_buf. Then add it
// to the ArrayList. Repeat this until request stream ends by counting the appearence
// of "\r\n"
while (true) {

View file

@ -2,7 +2,7 @@ pub const Status = enum(u32) {
// INFORMATION RESPONSES
CONTINUE = 100,
SWICTHING_PROTOCOLS = 101,
SWITCHING_PROTOCOLS = 101,
PROCESSING = 102,
EARLY_HINTS = 103,
@ -10,7 +10,7 @@ pub const Status = enum(u32) {
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTH_INFORMATION = 203,
NON_AUTHORATIVE_INFORMATION = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,
@ -22,7 +22,6 @@ pub const Status = enum(u32) {
MULTIPLE_CHOICES = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
MOVED_TEMPORARILY = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
USE_PROXY = 305,
@ -38,6 +37,7 @@ pub const Status = enum(u32) {
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTHENTICATION_REQUIRED = 407,
REQUEST_TIMEOUT = 408,
CONFLICT = 409,
GONE = 410,
@ -48,7 +48,7 @@ pub const Status = enum(u32) {
UNSUPPORTED_MEDIA_TYPE = 415,
RANGE_NOT_SATISIFIABLE = 416,
EXPECTATION_FAILED = 417,
I_AM_A_TEAPOT = 421,
I_AM_A_TEAPOT = 418,
MISDIRECTED_REQUEST = 421,
UNPROCESSABLE_CONTENT = 422,
LOCKED = 423,
@ -72,4 +72,72 @@ pub const Status = enum(u32) {
LOOP_DETECTED = 508,
NOT_EXTENDED = 510,
NETWORK_AUTHENTICATION_REQUIRED = 511,
pub fn stringify(self: Status) []const u8 {
switch (self) {
Status.CONTINUE => return "100 Continue",
Status.SWITCHING_PROTOCOLS => return "101 Switching Protocols",
Status.PROCESSING => return "102 Processing",
Status.EARLY_HINTS => return "103 Early Hints",
Status.OK => return "200 OK",
Status.CREATED => return "201 Created",
Status.ACCEPTED => return "202 Accepted",
Status.NON_AUTHORATIVE_INFORMATION => return "203 Non-Authorative Information",
Status.NO_CONTENT => return "204 No Content",
Status.RESET_CONTENT => return "205 Reset Content",
Status.PARTIAL_CONTENT => return "206 Partial Content",
Status.MULTI_STATUS => return "207 Multi-Status",
Status.ALREADY_REPORTED => return "208 Already Reported",
Status.IM_USED => return "226 IM Used",
Status.MULTIPLE_CHOICES => return "300 Multiple Choices",
Status.MOVED_PERMANENTLY => return "301 Moved Permanently",
Status.FOUND => return "302 Found",
Status.SEE_OTHER => return "303 See Other",
Status.NOT_MODIFIED => return "304 Not Modified",
Status.USE_PROXY => return "305 Use Proxy",
Status.SWITCH_PROXY => return "306 Switch Proxy",
Status.TEMPORARY_REDIRECT => return "307 Temporary Redirect",
Status.PERMANENT_REDIRECT => return "308 Permanent Redirect",
Status.BAD_REQUEST => return "400 Bad Request",
Status.UNAUTHORIZED => return "401 Unauthorized",
Status.PAYMENT_REQUIRED => return "402 Payment Required",
Status.FORBIDDEN => return "403 Forbidden",
Status.NOT_FOUND => return "404 Not Found",
Status.METHOD_NOT_ALLOWED => return "405 Method Not Allowed",
Status.NOT_ACCEPTABLE => return "406 Not Acceptable",
Status.PROXY_AUTHENTICATION_REQUIRED => return "407 Proxy Authentication Required",
Status.REQUEST_TIMEOUT => return "408 Request Timeout",
Status.CONFLICT => return "409 Conflict",
Status.GONE => return "410 Gone",
Status.LENGTH_REQUIRED => return "411 Length Required",
Status.PRECONDITION_FAILED => return "412 Precondition Failed",
Status.PAYLOAD_TOO_LARGE => return "413 Payload Too Large",
Status.URI_TOO_LONG => return "414 URI Too Long",
Status.UNSUPPORTED_MEDIA_TYPE => return "415 Unsupported Media Type",
Status.RANGE_NOT_SATISIFIABLE => return "416 Range Not Satisfiable",
Status.EXPECTATION_FAILED => return "417 Expectation Failed",
Status.I_AM_A_TEAPOT => return "418 I'm a teapot",
Status.MISDIRECTED_REQUEST => return "421 Misdirected Request",
Status.UNPROCESSABLE_CONTENT => return "422 Unprocessable Content",
Status.LOCKED => return "423 Locked",
Status.FAILED_DEPENDENCY => return "424 Failed Dependency",
Status.TOO_EARLY => return "425 Too Early",
Status.UPGRADE_REQUIRED => return "426 Upgrade Required",
Status.PRECONDITION_REQUIRED => return "428 Precondition Required",
Status.TOO_MANY_REQUESTS => return "429 Too Many Requests",
Status.REQUESTS_HEADER_FIELDS_TOO_LARGE => return "431 Request Header Fields Too Large",
Status.UNAVAILABLE_FOR_LEGAL_REASONS => return "451 Unavailable For Legal Reasons",
Status.INTERNAL_SERVER_ERROR => return "500 Internal Server Error",
Status.NOT_IMPLEMETED => return "501 Not Implemented",
Status.BAD_GATEWAY => return "502 Bad Gateway",
Status.SERVUCE_UNAVAILABLE => return "503 Service Unavailable",
Status.GATEWAY_TIMEOUT => return "504 Gateway Timeout",
Status.HTTP_VERSION_NOT_SUPPORTED => return "505 HTTP Version Not Supported",
Status.VARIANT_ALSO_NEGOTIATES => return "506 Variant Also Negotiates",
Status.INSUFFICIENT_STORAGE => return "507 Insufficient Storage",
Status.LOOP_DETECTED => return "508 Loop Detected",
Status.NOT_EXTENDED => return "510 Not Extended",
Status.NETWORK_AUTHENTICATION_REQUIRED => return "511 Network Authentication Required",
}
}
};

View file

@ -1,22 +1,20 @@
const tuple = @import("std").meta.Tuple;
const std = @import("std");
const tuple = std.meta.Tuple;
pub const Route = tuple(&.{ []const u8, *const fn () Response });
pub const Header = tuple(&.{ []const u8, *const fn () Response });
pub const HTTP_Version = enum {
HTTP1_1,
HTTP2,
};
pub const HTTP_Version = enum([]const u8) { HTTP1_1 = "HTTP/1.1", HTTP2 = "HTTP/" };
pub const Request = struct {
httpVersion: HTTP_Version,
headers: []Header,
body: []u8,
headers: std.ArrayList,
body: std.ArrayList,
};
pub const Response = struct {
httpVersion: HTTP_Version,
headers: []Header,
body: []u8,
httpVersion: HTTP_Version = HTTP_Version.HTTP1_1,
headers: std.ArrayList,
body: std.ArrayList,
};