Core: Added all well known http status codes (#195)

This commit is contained in:
Zbigniew Rębacz 2024-04-08 21:59:37 +02:00 committed by GitHub
parent 7b38e463df
commit fd1ea9a945
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 98 additions and 7 deletions

View file

@ -9,7 +9,7 @@ void ProcessHttpRequest(TcpSocket& client)
HttpHeader header; HttpHeader header;
if(!header.Read(client)) { if(!header.Read(client)) {
Cerr() << "Failed to read HttpHeader.\n"; Cerr() << "Failed to read HttpHeader.\n";
HttpResponse(client, false, 400, "Invalid request"); HttpResponse(client, false, HttpStatus::BAD_REQUEST);
return; return;
} }
@ -21,12 +21,15 @@ void ProcessHttpRequest(TcpSocket& client)
<< "Indonesia" << "Indonesia"
<< "Brazil" << "Brazil"
<< "France"; << "France";
HttpResponse(client, false, 200, "OK", "application/json", ja.ToString()); auto code = HttpStatus::OK;
HttpResponse(client, false, code, HttpStatus::ToString(code), "application/json",
ja.ToString());
return;
} }
} }
HttpResponse(client, false, 404, "Not found"); HttpResponse(client, false, HttpStatus::NOT_FOUND);
} }
void RunServerLoop(TcpSocket& server) void RunServerLoop(TcpSocket& server)

View file

@ -183,6 +183,7 @@ file
xxHsh.cpp, xxHsh.cpp,
Web readonly separator, Web readonly separator,
Inet.h, Inet.h,
HttpStatusCode.i,
InetUtil.cpp, InetUtil.cpp,
MIME.cpp, MIME.cpp,
Socket.cpp, Socket.cpp,

View file

@ -922,4 +922,14 @@ String HttpRequest::GetPhaseName() const
return phase >= 0 && phase <= FAILED ? m[phase] : ""; return phase >= 0 && phase <= FAILED ? m[phase] : "";
} }
String HttpStatus::ToString(int status)
{
switch (status) {
#define CODE_(id, code, str) case id: return #str;
#include "HttpStatusCode.i"
#undef CODE_
default: return "";
}
}
} }

View file

@ -0,0 +1,67 @@
CODE_(CONTINUE, 100, "Continue")
CODE_(SWITCHING_PROTOCOLS, 101, "Switching Protocols")
CODE_(PROCESSING, 102, "Processing")
CODE_(EARLY_HINTS, 103, "Early Hints")
CODE_(OK, 200, "OK")
CODE_(CREATED, 201, "Created")
CODE_(ACCEPTED, 202, "Accepted")
CODE_(NON_AUTHORITATIVE_INFO, 203, "Non Authoritative Info")
CODE_(NO_CONTENT, 204, "No Content")
CODE_(RESET_CONTENT, 205, "Reset Content")
CODE_(PARTIAL_CONTENT, 206, "Partial Content")
CODE_(MULTI_STATUS, 207, "Multi Status")
CODE_(ALREADY_REPORTED, 208, "Already Reported")
CODE_(IM_USED, 226, "IM Used")
CODE_(MULTIPLE_CHOICES, 300, "Multiple Choices")
CODE_(MOVED_PERMANENTLY, 301, "Moved Permanently")
CODE_(FOUND, 302, "Found")
CODE_(SEE_OTHER, 303, "See Other")
CODE_(NOT_MODIFIED, 304, "Not Modified")
CODE_(USE_PROXY, 305, "Use Proxy")
CODE_(_, 306, "_")
CODE_(TEMPORARY_REDIRECT, 307, "Temporary Redirect")
CODE_(PERMANENT_REDIRECT, 308, "Permanent Redirect")
CODE_(BAD_REQUEST, 400, "Bad Request")
CODE_(UNAUTHORIZED, 401, "Unauthorized")
CODE_(PAYMENT_REQUIRED, 402, "Payment Required")
CODE_(FORBIDDEN, 403, "Forbidden")
CODE_(NOT_FOUND, 404, "Not Found")
CODE_(METHOD_NOT_ALLOWED, 405, "Method Not Allowed")
CODE_(NOT_ACCEPTABLE, 406, "Not Acceptable")
CODE_(PROXY_AUTH_REQUIRED, 407, "Proxy Auth Required")
CODE_(REQUEST_TIEMOUT, 408, "Request Timeout")
CODE_(CONFLICT, 409, "Conflict")
CODE_(GONE, 410, "Gone")
CODE_(LENGTH_REQUIRED, 411, "Length Required")
CODE_(PRECONDITION_FAILED, 412, "Precondition Failed")
CODE_(REQUEST_ENTITY_TOO_LARGE, 413, "Request Entity Too Large")
CODE_(REQUEST_URI_TOO_LONG, 414, "Request URI Too Long")
CODE_(UNSUPPORTED_MEDIA_TYPE, 415, "Unsupported Media Type")
CODE_(REQUEST_RANGE_NOT_SATISFIABLE, 416, "Request Range Not Satisfiable")
CODE_(EXPECTATION_FAILED, 417, "Expectation Failed")
CODE_(TEAPOT, 418, "Teapot")
CODE_(MISDIRECTED_REQUEST, 421, "Misdirected Request")
CODE_(UNPROCESSABLE_ENTITY, 422, "Unprocessable Entity")
CODE_(LOCKED, 423, "Locked")
CODE_(FAILED_DEPENDENCY, 424, "Failed Dependency")
CODE_(TOO_EARLY, 425, "Too Early")
CODE_(UPGRADE_REQUIRED, 426, "Upgrade Required")
CODE_(PRECONDITION_REQUIRED, 428, "Precondition Required")
CODE_(TOO_MANY_REQUESTS, 429, "Too Many Requests")
CODE_(REQUEST_HEADER_FIELDS_TOO_LARGE, 431, "Request Header Fields Too Large")
CODE_(UNAVAILABLE_FOR_LEGAL_REASONS, 451, "Unavailable For Legal Reasons")
CODE_(INTERNAL_SERVER_ERROR, 500, "Internal Server Error")
CODE_(NOT_IMPLEMENTED, 501, "Not Implemented")
CODE_(BAD_GATEWAY, 502, "Bad Gateway")
CODE_(SERVICE_UNAVAILABLE, 503, "Service Unavailable")
CODE_(GATEWAY_TIMEOUT, 504, "Gateway Timeout")
CODE_(HTTP_VERSION_NOT_SUPPORTED, 505, "HTTP Version Not Supported")
CODE_(VARIANT_ALSO_NEGOTIATES, 506, "Variant Also Negotiates")
CODE_(INSUFFICIENT_STORAGE, 507, "Insufficient Storage")
CODE_(LOOP_DETECTED, 508, "Loop Detected")
CODE_(NOT_EXTENDED, 510, "Not Extended")
CODE_(NETWORK_AUTHENTICATION_REQUIRED, 511, "Network Authentication Required")

View file

@ -297,6 +297,15 @@ struct UrlInfo {
UrlInfo(const String& url) { Parse(url); } UrlInfo(const String& url) { Parse(url); }
}; };
namespace HttpStatus
{
#define CODE_(id, code, str) constexpr int id = code;
#include "HttpStatusCode.i"
#undef CODE_
String ToString(int code);
}
struct HttpCookie : Moveable<HttpCookie> { struct HttpCookie : Moveable<HttpCookie> {
String id; String id;
String value; String value;
@ -596,7 +605,7 @@ public:
static void TraceShort(bool b = true); static void TraceShort(bool b = true);
}; };
bool HttpResponse(TcpSocket& socket, bool scgi, int code, const char *phrase, bool HttpResponse(TcpSocket& socket, bool scgi, int code, const char *phrase = NULL,
const char *content_type = NULL, const String& data = Null, const char *content_type = NULL, const String& data = Null,
const char *server = NULL, bool gzip = false); const char *server = NULL, bool gzip = false);

View file

@ -632,7 +632,8 @@ bool HttpResponse(TcpSocket& socket, bool scgi, int code, const char *phrase,
bool gzip) bool gzip)
{ {
String r; String r;
r << (scgi ? "Status: " : "HTTP/1.1 ") << code << ' ' << phrase << "\r\n" r << (scgi ? "Status: " : "HTTP/1.1 ") <<
code << ' ' << (phrase ? String(phrase) : HttpStatus::ToString(code)) << "\r\n"
"Date: " << WwwFormat(GetUtcTime()) << "\r\n" "Date: " << WwwFormat(GetUtcTime()) << "\r\n"
"Server: " << (server ? server : "U++ based server") << "\r\n" "Server: " << (server ? server : "U++ based server") << "\r\n"
"Connection: close\r\n"; "Connection: close\r\n";