anthracite/lib/http/constants.hpp
Nicholas Orlowsky 71be773d49
split into src and lib and socket changes
* libanthracite files are now all in lib/
* anthracite-bin + anthracite-api-bin files are now all in src/
* socket split into header and source properly
2025-02-04 11:37:46 -05:00

245 lines
6.4 KiB
C++

#pragma once
#include <string>
#include <unordered_map>
namespace anthracite::http {
constexpr int HEADER_BYTES = 8190;
enum method {
GET,
POST,
DELETE,
PUT,
PATCH,
HEAD,
OPTIONS,
CONNECT,
TRACE,
COPY,
LINK,
UNLINK,
PURGE,
LOCK,
UNLOCK,
PROPFIND,
VIEW,
UNKNOWN
};
enum status_codes {
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
PROCESSING = 102,
EARLY_HINTS = 103,
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTHORITATIVE_INFORMATION = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,
MULTI_STATUS = 207,
ALREADY_REPORTED = 208,
IM_USED = 226,
MULTIPLE_CHOICES = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
USE_PROXY = 305,
TEMPORARY_REDIRECT = 307,
PERMANENT_REDIRECT = 308,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
PAYMENT_REQUIRED = 402,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTHENTICATION_REQUIRED = 407,
REQUEST_TIMEOUT = 408,
CONFLICT = 409,
GONE = 410,
LENGTH_REQUIRED = 411,
PRECONDITION_FAILED = 412,
PAYLOAD_TOO_LARGE = 413,
URI_TOO_LONG = 414,
UNSUPPORTED_MEDIA_TYPE = 415,
RANGE_NOT_SATISFIABLE = 416,
EXPECTATION_FAILED = 417,
I_AM_A_TEAPOT = 418,
ENHANCE_YOUR_CALM = 420,
MISDIRECTED_REQUEST = 421,
UNPROCESSABLE_ENTITY = 422,
LOCKED = 423,
FAILED_DEPENDENCY = 424,
TOO_EARLY = 425,
UPGRADE_REQUIRED = 426,
PRECONDITION_REQUIRED = 428,
TOO_MANY_REQUESTS = 429,
REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
UNAVAILABLE_FOR_LEGAL_REASONS = 451,
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
HTTP_VERSION_NOT_SUPPORTED = 505,
VARIANT_ALSO_NEGOTIATES = 506,
INSUFFICIENT_STORAGE = 507,
LOOP_DETECTED = 508,
NOT_EXTENDED = 510,
NETWORK_AUTHENTICATION_REQUIRED = 511
};
static std::unordered_map<std::string, method> const method_map = {
{ "GET", method::GET },
{ "POST", method::POST },
{ "DELETE", method::DELETE },
{ "PUT", method::PUT },
{ "PATCH", method::PATCH },
{ "HEAD", method::HEAD },
{ "OPTIONS", method::OPTIONS },
{ "CONNECT", method::CONNECT },
{ "TRACE", method::TRACE },
{ "COPY", method::COPY },
{ "LINK", method::LINK },
{ "UNLINK", method::UNLINK },
{ "PURGE", method::PURGE },
{ "LOCK", method::LOCK },
{ "UNLOCK", method::UNLOCK },
{ "PROPFIND", method::PROPFIND },
{ "VIEW", method::VIEW },
{ "UNKNOWN", method::UNKNOWN }
};
static std::unordered_map<method, std::string> const reverse_method_map = {
{ method::GET, "GET" },
{ method::POST, "POST" },
{ method::DELETE, "DELETE" },
{ method::PUT, "PUT" },
{ method::PATCH, "PATCH" },
{ method::HEAD, "HEAD" },
{ method::OPTIONS, "OPTIONS" },
{ method::CONNECT, "CONNECT" },
{ method::TRACE, "TRACE" },
{ method::COPY, "COPY" },
{ method::LINK, "LINK" },
{ method::UNLINK, "UNLINK" },
{ method::PURGE, "PURGE" },
{ method::LOCK, "LOCK" },
{ method::UNLOCK, "UNLOCK" },
{ method::PROPFIND, "PROPFIND" },
{ method::VIEW, "VIEW" },
{ method::UNKNOWN, "UNKNOWN" }
};
static std::unordered_map<int, std::string> const status_map = {
{ 100, "CONTINUE" },
{ 101, "SWITCHING PROTOCOLS" },
{ 200, "OK" },
{ 201, "CREATED" },
{ 202, "ACCEPTED" },
{ 203, "NON-AUTHORITATIVE INFORMATION" },
{ 204, "NO CONTENT" },
{ 205, "RESET CONTENT" },
{ 206, "PARTIAL CONTENT" },
{ 300, "MULTIPLE CHOICES" },
{ 301, "MOVED PERMANENTLY" },
{ 302, "FOUND" },
{ 303, "SEE OTHER" },
{ 304, "NOT MODIFIED" },
{ 305, "USE PROXY" },
{ 307, "TEMPORARY REDIRECT" },
{ 400, "BAD REQUEST" },
{ 401, "UNAUTHORIZED" },
{ 402, "PAYMENT REQUIRED" },
{ 403, "FORBIDDEN" },
{ 404, "NOT FOUND" },
{ 405, "METHOD NOT ALLOWED" },
{ 406, "NOT ACCEPTABLE" },
{ 407, "PROXY AUTHENTICATION REQUIRED" },
{ 408, "REQUEST TIMEOUT" },
{ 409, "CONFLICT" },
{ 410, "GONE" },
{ 411, "LENGTH REQUIRED" },
{ 412, "PRECONDITION FAILED" },
{ 413, "PAYLOAD TOO LARGE" },
{ 414, "URI TOO LONG" },
{ 415, "UNSUPPORTED MEDIA TYPE" },
{ 416, "RANGE NOT SATISFIABLE" },
{ 417, "EXPECTATION FAILED" },
{ 418, "I'M A TEAPOT" },
{ 421, "MISDIRECTED REQUEST" },
{ 422, "UNPROCESSABLE ENTITY" },
{ 423, "LOCKED" },
{ 424, "FAILED DEPENDENCY" },
{ 426, "UPGRADE REQUIRED" },
{ 428, "PRECONDITION REQUIRED" },
{ 429, "TOO MANY REQUESTS" },
{ 431, "REQUEST HEADER FIELDS TOO LARGE" },
{ 451, "UNAVAILABLE FOR LEGAL REASONS" },
{ 500, "INTERNAL SERVER ERROR" },
{ 501, "NOT IMPLEMENTED" },
{ 502, "BAD GATEWAY" },
{ 503, "SERVICE UNAVAILABLE" },
{ 504, "GATEWAY TIMEOUT" },
{ 505, "HTTP VERSION NOT SUPPORTED" },
{ 506, "VARIANT ALSO NEGOTIATES" },
{ 507, "INSUFFICIENT STORAGE" },
{ 508, "LOOP DETECTED" },
{ 510, "NOT EXTENDED" },
{ 511, "NETWORK AUTHENTICATION REQUIRED" },
{ 420, "ENHANCE YOUR CALM" }
};
static std::unordered_map<std::string, std::string> const mime_types = {
{ "html", "text/html" },
{ "css", "text/css" },
{ "js", "application/javascript" },
{ "json", "application/json" },
{ "pdf", "application/pdf" },
{ "ico", "image/x-icon" },
{ "jpg", "image/jpeg" },
{ "jpeg", "image/jpeg" },
{ "png", "image/png" },
{ "gif", "image/gif" },
{ "bmp", "image/bmp" },
{ "mp4", "video/mp4" },
{ "avi", "video/x-msvideo" },
{ "mkv", "video/x-matroska" },
{ "mov", "video/quicktime" },
{ "wmv", "video/x-ms-wmv" },
};
enum version { HTTP_0_9,
HTTP_1_0,
HTTP_1_1,
HTTP_2_0,
HTTP_3_0 };
static std::unordered_map<std::string, version> const version_map = {
// This is because HTTP 0.9 didn't specify version in the header
{ "", HTTP_0_9 },
{ "HTTP/0.9", HTTP_0_9 },
{ "HTTP/1.0", HTTP_1_0 },
{ "HTTP/1.1", HTTP_1_1 },
{ "HTTP/2.0", HTTP_2_0 },
{ "HTTP/3.0", HTTP_3_0 }
};
static std::unordered_map<version, std::string> const reverse_version_map = {
{ HTTP_0_9, "HTTP/0.9" },
{ HTTP_1_0, "HTTP/1.0" },
{ HTTP_1_1, "HTTP/1.1" },
{ HTTP_2_0, "HTTP/2.0" },
{ HTTP_3_0, "HTTP/3.0" }
};
};