From 91e0a6874b6bb4d0f19a840cdd67ac24be34189c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 11 Feb 2023 11:16:15 +0100 Subject: [PATCH] net::http: add status code utilities --- net/http/constants.ha | 72 +++++++++++++-------------- net/http/status.ha | 111 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 36 deletions(-) create mode 100644 net/http/status.ha diff --git a/net/http/constants.ha b/net/http/constants.ha index 09862ff..13d84e9 100644 --- a/net/http/constants.ha +++ b/net/http/constants.ha @@ -12,95 +12,95 @@ export def DELETE: str = "DELETE"; // HTTP "Continue" response status (100). export def STATUS_CONTINUE: uint = 100; // HTTP "Switching Protocols" response status (101). -export def STATUS_SWITCHINGPROTOCOLS: uint = 101; +export def STATUS_SWITCHING_PROTOCOLS: uint = 101; -// HTTP "Ok" response status (200). +// HTTP "OK" response status (200). export def STATUS_OK: uint = 200; // HTTP "Created" response status (201). export def STATUS_CREATED: uint = 201; // HTTP "Accepted" response status (202). export def STATUS_ACCEPTED: uint = 202; // HTTP "Non-authoritative Info" response status (203). -export def STATUS_NONAUTHORITATIVEINFO: uint = 203; +export def STATUS_NONAUTHORITATIVE_INFO: uint = 203; // HTTP "No Content" response status (204). -export def STATUS_NOCONTENT: uint = 204; +export def STATUS_NO_CONTENT: uint = 204; // HTTP "Reset Content" response status (205). -export def STATUS_RESETCONTENT: uint = 205; +export def STATUS_RESET_CONTENT: uint = 205; // HTTP "Partial Content" response status (206). -export def STATUS_PARTIALCONTENT: uint = 206; +export def STATUS_PARTIAL_CONTENT: uint = 206; // HTTP "Multiple Choices" response status (300). -export def STATUS_MULTIPLECHOICES: uint = 300; +export def STATUS_MULTIPLE_CHOICES: uint = 300; // HTTP "Moved Permanently" response status (301). -export def STATUS_MOVEDPERMANENTLY: uint = 301; +export def STATUS_MOVED_PERMANENTLY: uint = 301; // HTTP "Found" response status (302). export def STATUS_FOUND: uint = 302; // HTTP "See Other" response status (303). -export def STATUS_SEEOTHER: uint = 303; +export def STATUS_SEE_OTHER: uint = 303; // HTTP "Not Modified" response status (304). -export def STATUS_NOTMODIFIED: uint = 304; +export def STATUS_NOT_MODIFIED: uint = 304; // HTTP "Use Proxy" response status (305). -export def STATUS_USEPROXY: uint = 305; +export def STATUS_USE_PROXY: uint = 305; // HTTP "Temporary Redirect" response status (307). -export def STATUS_TEMPORARYREDIRECT: uint = 307; +export def STATUS_TEMPORARY_REDIRECT: uint = 307; // HTTP "Permanent Redirect" response status (308). -export def STATUS_PERMANENTREDIRECT: uint = 308; +export def STATUS_PERMANENT_REDIRECT: uint = 308; // HTTP "Bad Request" response status (400). -export def STATUS_BADREQUEST: uint = 400; +export def STATUS_BAD_REQUEST: uint = 400; // HTTP "Unauthorized" response status (401). export def STATUS_UNAUTHORIZED: uint = 401; // HTTP "Payment Required" response status (402). -export def STATUS_PAYMENTREQUIRED: uint = 402; +export def STATUS_PAYMENT_REQUIRED: uint = 402; // HTTP "Forbidden" response status (403). export def STATUS_FORBIDDEN: uint = 403; // HTTP "Not Found" response status (404). -export def STATUS_NOTFOUND: uint = 404; +export def STATUS_NOT_FOUND: uint = 404; // HTTP "Method Not Allowed" response status (405). -export def STATUS_METHODNOTALLOWED: uint = 405; +export def STATUS_METHOD_NOT_ALLOWED: uint = 405; // HTTP "Not Acceptable" response status (406). -export def STATUS_NOTACCEPTABLE: uint = 406; +export def STATUS_NOT_ACCEPTABLE: uint = 406; // HTTP "Proxy Authentication Required" response status (407). -export def STATUS_PROXYAUTHREQUIRED: uint = 407; +export def STATUS_PROXY_AUTH_REQUIRED: uint = 407; // HTTP "Request Timeout" response status (408). -export def STATUS_REQUESTTIMEOUT: uint = 408; +export def STATUS_REQUEST_TIMEOUT: uint = 408; // HTTP "Conflict" response status (409). export def STATUS_CONFLICT: uint = 409; // HTTP "Gone" response status (410). export def STATUS_GONE: uint = 410; // HTTP "Length Required" response status (411). -export def STATUS_LENGTHREQUIRED: uint = 411; +export def STATUS_LENGTH_REQUIRED: uint = 411; // HTTP "Precondition Failed" response status (412). -export def STATUS_PRECONDITIONFAILED: uint = 412; +export def STATUS_PRECONDITION_FAILED: uint = 412; // HTTP "Request Entity Too Large" response status (413). -export def STATUS_REQUESTENTITYTOOLARGE: uint = 413; +export def STATUS_REQUEST_ENTITY_TOO_LARGE: uint = 413; // HTTP "Request URI Too Long" response status (414). -export def STATUS_REQUESTURITOOLONG: uint = 414; +export def STATUS_REQUEST_URI_TOO_LONG: uint = 414; // HTTP "Unsupported Media Type" response status (415). -export def STATUS_UNSUPPORTEDMEDIATYPE: uint = 415; +export def STATUS_UNSUPPORTED_MEDIA_TYPE: uint = 415; // HTTP "Requested Range Not Satisfiable" response status (416). -export def STATUS_REQUESTEDRANGENOTSATISFIABLE: uint = 416; +export def STATUS_REQUESTED_RANGE_NOT_SATISFIABLE: uint = 416; // HTTP "Expectation Failed" response status (417). -export def STATUS_EXPECTATIONFAILED: uint = 417; +export def STATUS_EXPECTATION_FAILED: uint = 417; // HTTP "I'm a Teapot" response status (418). export def STATUS_TEAPOT: uint = 418; // HTTP "Misdirected Request" response status (421). -export def STATUS_MISDIRECTEDREQUEST: uint = 421; +export def STATUS_MISDIRECTED_REQUEST: uint = 421; // HTTP "Unprocessable Entity" response status (422). -export def STATUS_UNPROCESSABLEENTITY: uint = 422; +export def STATUS_UNPROCESSABLE_ENTITY: uint = 422; // HTTP "Upgrade Required" response status (426). -export def STATUS_UPGRADEREQUIRED: uint = 426; +export def STATUS_UPGRADE_REQUIRED: uint = 426; // HTTP "Internal Server Error" response status (500). -export def STATUS_INTERNALSERVERERROR: uint = 500; +export def STATUS_INTERNAL_SERVER_ERROR: uint = 500; // HTTP "Not Implemented" response status (501). -export def STATUS_NOTIMPLEMENTED: uint = 501; +export def STATUS_NOT_IMPLEMENTED: uint = 501; // HTTP "Bad Gateway" response status (502). -export def STATUS_BADGATEWAY: uint = 502; +export def STATUS_BAD_GATEWAY: uint = 502; // HTTP "Service Unavailable" response status (503). -export def STATUS_SERVICEUNAVAILABLE: uint = 503; +export def STATUS_SERVICE_UNAVAILABLE: uint = 503; // HTTP "Gateway Timeout" response status (504). -export def STATUS_GATEWAYTIMEOUT: uint = 504; +export def STATUS_GATEWAY_TIMEOUT: uint = 504; // HTTP "HTTP Version Not Supported" response status (505). -export def STATUS_HTTPVERSIONNOTSUPPORTED: uint = 505; +export def STATUS_HTTP_VERSION_NOT_SUPPORTED: uint = 505; diff --git a/net/http/status.ha b/net/http/status.ha new file mode 100644 index 0000000..bbb418a --- /dev/null +++ b/net/http/status.ha @@ -0,0 +1,111 @@ +// A semantic HTTP error and its status code. +export type httperror = !uint; + +// Checks if an HTTP status code is semantically considered an error, returning +// [[httperror]] if so, or otherwise returning the original status code. +export fn check(status: uint) (uint | httperror) = { + if (status >= 400 && status < 600) { + return status: httperror; + }; + return status; +}; + +// Converts a standard HTTP status code into the reason text typically +// associated with this status code (or "Unknown Status" if the status code is +// not known to net::http). +export fn status_reason(status: uint) const str = { + switch (status) { + case STATUS_CONTINUE => + return "Continue"; + case STATUS_SWITCHING_PROTOCOLS => + return "Switching Protocols"; + case STATUS_OK => + return "Continue"; + case STATUS_CREATED => + return "Continue"; + case STATUS_ACCEPTED => + return "Accepted"; + case STATUS_NONAUTHORITATIVE_INFO => + return "Non-Authoritative Information"; + case STATUS_NO_CONTENT => + return "No Content"; + case STATUS_RESET_CONTENT => + return "Reset Content"; + case STATUS_PARTIAL_CONTENT => + return "Partial Content"; + case STATUS_MULTIPLE_CHOICES => + return "Multiple Choices"; + case STATUS_MOVED_PERMANENTLY => + return "Moved Permanently"; + case STATUS_FOUND => + return "Found"; + case STATUS_SEE_OTHER => + return "See Other"; + case STATUS_NOT_MODIFIED => + return "Not Modified"; + case STATUS_USE_PROXY => + return "Use Proxy"; + case STATUS_TEMPORARY_REDIRECT => + return "Temporary Redirect"; + case STATUS_PERMANENT_REDIRECT => + return "Permanent Redirect"; + case STATUS_BAD_REQUEST => + return "Bad Request"; + case STATUS_UNAUTHORIZED => + return "Unauthorized"; + case STATUS_PAYMENT_REQUIRED => + return "Payment Required"; + case STATUS_FORBIDDEN => + return "Forbidden"; + case STATUS_NOT_FOUND => + return "Not Found"; + case STATUS_METHOD_NOT_ALLOWED => + return "Method Not Allowed"; + case STATUS_NOT_ACCEPTABLE => + return "Not Acceptable"; + case STATUS_PROXY_AUTH_REQUIRED => + return "Proxy Authentication Required"; + case STATUS_REQUEST_TIMEOUT => + return "Request Timeout"; + case STATUS_CONFLICT => + return "Conflict"; + case STATUS_GONE => + return "Gone"; + case STATUS_LENGTH_REQUIRED => + return "Length Required"; + case STATUS_PRECONDITION_FAILED => + return "Precondition Failed"; + case STATUS_REQUEST_ENTITY_TOO_LARGE => + return "Request Entity Too Large"; + case STATUS_REQUEST_URI_TOO_LONG => + return "Request URI Too Long"; + case STATUS_UNSUPPORTED_MEDIA_TYPE => + return "Unsupported Media Type"; + case STATUS_REQUESTED_RANGE_NOT_SATISFIABLE => + return "Requested Range Not Satisfiable"; + case STATUS_EXPECTATION_FAILED => + return "Expectation Failed"; + case STATUS_TEAPOT => + return "I'm A Teapot"; + case STATUS_MISDIRECTED_REQUEST => + return "Misdirected Request"; + case STATUS_UNPROCESSABLE_ENTITY => + return "Unprocessable Entity"; + case STATUS_UPGRADE_REQUIRED => + return "Upgrade Required"; + case STATUS_INTERNAL_SERVER_ERROR => + return "Internal Server Error"; + case STATUS_NOT_IMPLEMENTED => + return "Not Implemented"; + case STATUS_BAD_GATEWAY => + return "Bad Gateway"; + case STATUS_SERVICE_UNAVAILABLE => + return "Service Unavailable"; + case STATUS_GATEWAY_TIMEOUT => + return "Gateway Timeout"; + case STATUS_HTTP_VERSION_NOT_SUPPORTED => + return "HTTP Version Not Supported"; + case => + return "Unknown status"; + }; +};