From 1dc4261f6fb38677a54c69502098ac47a59b90ef Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Fri, 26 Aug 2022 11:11:15 -0400 Subject: [PATCH] Add Http.c --- src/Http.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 src/Http.c diff --git a/src/Http.c b/src/Http.c new file mode 100644 index 0000000..aede3fb --- /dev/null +++ b/src/Http.c @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net> + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include + +const char * +HttpRequestMethodToString(const HttpStatus status) +{ + switch (status) + { + case HTTP_GET: + return "GET"; + case HTTP_HEAD: + return "HEAD"; + case HTTP_POST: + return "POST"; + case HTTP_PUT: + return "PUT"; + case HTTP_DELETE: + return "DELETE"; + case HTTP_CONNECT: + return "CONNECT"; + case HTTP_OPTIONS: + return "OPTIONS"; + case HTTP_TRACE: + return "TRACE"; + case HTTP_PATCH: + return "PATCH"; + default: + return NULL; + } +} + +HttpRequestMethod +HttpRequestMethodFromString(const char *str) +{ + if (strcmp(str, "GET") == 0) + { + return HTTP_GET; + } + + if (strcmp(str, "HEAD") == 0) + { + return HTTP_HEAD; + } + + if (strcmp(str, "POST") == 0) + { + return HTTP_POST; + } + + if (strcmp(str, "PUT") == 0) + { + return HTTP_PUT; + } + + if (strcmp(str, "DELETE") == 0) + { + return HTTP_DELETE; + } + + if (strcmp(str, "CONNECT") == 0) + { + return HTTP_CONNECT; + } + + if (strcmp(str, "OPTIONS") == 0) + { + return HTTP_OPTIONS; + } + + if (strcmp(str, "TRACE") == 0) + { + return HTTP_TRACE; + } + + if (strcmp(str, "PATCH") == 0) + { + return HTTP_PATCH; + } + + return -1; +} + +const char * +HttpStatusToString(const HttpStatus status) +{ + switch (status) + { + case HTTP_CONTINUE: + return "Continue"; + case HTTP_SWITCHING_PROTOCOLS: + return "Switching Protocols"; + case HTTP_EARLY_HINTS: + return "Early Hints"; + case HTTP_OK: + return "Ok"; + case HTTP_CREATED: + return "Created"; + case HTTP_ACCEPTED: + return "Accepted"; + case HTTP_NON_AUTHORITATIVE_INFORMATION: + return "Non-Authoritative Information"; + case HTTP_NO_CONTENT: + return "No Content"; + case HTTP_RESET_CONTENT: + return "Reset Content"; + case HTTP_PARTIAL_CONTENT: + return "Partial Content"; + case HTTP_MULTIPLE_CHOICES: + return "Multiple Choices"; + case HTTP_MOVED_PERMANENTLY: + return "Moved Permanently"; + case HTTP_FOUND: + return "Found"; + case HTTP_SEE_OTHER: + return "See Other"; + case HTTP_NOT_MODIFIED: + return "Not Modified"; + case HTTP_TEMPORARY_REDIRECT: + return "Temporary Redirect"; + case HTTP_PERMANENT_REDIRECT: + return "Permanent Redirect"; + case HTTP_BAD_REQUEST: + return "Bad Request"; + case HTTP_UNAUTHORIZED: + return "Unauthorized"; + case HTTP_FORBIDDEN: + return "Forbidden"; + case HTTP_NOT_FOUND: + return "Not Found"; + case HTTP_METHOD_NOT_ALLOWED: + return "Method Not Allowed"; + case HTTP_NOT_ACCEPTABLE: + return "Not Acceptable"; + case HTTP_PROXY_AUTH_REQUIRED: + return "Proxy Authentication Required"; + case HTTP_REQUEST_TIMEOUT: + return "Request Timeout"; + case HTTP_CONFLICT: + return "Conflict"; + case HTTP_GONE: + return "Gone"; + case HTTP_LENGTH_REQUIRED: + return "Length Required"; + case HTTP_PRECONDITION_FAILED: + return "Precondition Failed"; + case HTTP_PAYLOAD_TOO_LARGE: + return "Payload Too Large"; + case HTTP_URI_TOO_LONG: + return "URI Too Long"; + case HTTP_UNSUPPORTED_MEDIA_TYPE: + return "Unsupported Media Type"; + case HTTP_RANGE_NOT_SATISFIABLE: + return "Range Not Satisfiable"; + case HTTP_EXPECTATION_FAILED: + return "Expectation Failed"; + case HTTP_TEAPOT: + return "I'm a Teapot": + case HTTP_UPGRADE_REQUIRED: + return "Upgrade Required"; + case HTTP_PRECONDITION_REQUIRED: + return "Precondition Required"; + case HTTP_TOO_MANY_REQUESTS: + return "Too Many Requests"; + case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE: + return "Request Header Fields Too Large"; + case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: + return "Unavailable For Legal Reasons"; + case HTTP_INTERNAL_SERVER_ERROR: + return "Internal Server Error"; + case HTTP_NOT_IMPLEMENTED: + return "Not Implemented"; + case HTTP_BAD_GATEWAY: + return "Bad Gateway"; + case HTTP_SERVICE_UNAVAILABLE: + return "Service Unavailable"; + case HTTP_GATEWAY_TIMEOUT: + return "Gateway Timeout"; + case HTTP_VERSION_NOT_SUPPORTED: + return "Version Not Supported"; + case HTTP_VARIANT_ALSO_NEGOTIATES: + return "Variant Also Negotiates"; + case HTTP_NOT_EXTENDED: + return "Not Extended"; + case HTTP_NETWORK_AUTH_REQUIRED: + return "Network Authentication Required"; + default: + return NULL; + } +}