Implement handling http status codes for responses.

This commit is contained in:
Relintai 2022-07-21 12:26:39 +02:00
parent 7fa4125463
commit 9d07a6b271
5 changed files with 222 additions and 13 deletions

View File

@ -22,6 +22,209 @@ SOFTWARE.
#include "http_server_enums.h"
String HTTPServerEnums::get_status_code_header_string(const HTTPServerEnums::HTTPStatusCode code) {
switch (code) {
case HTTP_STATUS_CODE_100_CONTINUE:
return "100 Continue";
break;
case HTTP_STATUS_CODE_101_SWITCHING_PROTOCOLS:
return "101 Switching Protocols";
break;
case HTTP_STATUS_CODE_102_PROCESSING:
return "102 Processing";
break;
case HTTP_STATUS_CODE_103_EARLY_HINTS:
return "103 Early Hints";
break;
case HTTP_STATUS_CODE_200_OK:
return "200 OK";
break;
case HTTP_STATUS_CODE_201_CREATED:
return "201 Created";
break;
case HTTP_STATUS_CODE_202_ACCEPTED:
return "202 Accepted";
break;
case HTTP_STATUS_CODE_203_NON_AUTHORITATIVE_INFORMATION:
return "203 Non-Authoritative Information";
break;
case HTTP_STATUS_CODE_204_NO_CONTENT:
return "204 No Content";
break;
case HTTP_STATUS_CODE_205_RESET_CONTENT:
return "205 Reset Content";
break;
case HTTP_STATUS_CODE_206_PARTIAL_CONTENT:
return "206 Partial Content";
break;
case HTTP_STATUS_CODE_207_MULTI_STATUS:
return "207 Multi-Status";
break;
case HTTP_STATUS_CODE_208_ALREADY_REPORTED:
return "208 Already Reported";
break;
case HTTP_STATUS_CODE_226_IM_USED:
return "226 IM Used";
break;
case HTTP_STATUS_CODE_300_MULTIPLE_CHOICES:
return "300 Multiple Choices";
break;
case HTTP_STATUS_CODE_301_MOVED_PERMANENTLY:
return "301 Moved Permanently";
break;
case HTTP_STATUS_CODE_302_FOUND:
return "302 Found";
break;
case HTTP_STATUS_CODE_303_SEE_OTHER:
return "303 See Other";
break;
case HTTP_STATUS_CODE_304_NOT_MODIFIED:
return "304 Not Modified";
break;
case HTTP_STATUS_CODE_305_USE_PROXY:
return "305 Use Proxy";
break;
case HTTP_STATUS_CODE_306_UNUSED:
return "306 unused";
break;
case HTTP_STATUS_CODE_307_TEMPORARY_REDIRECT:
return "307 Temporary Redirect";
break;
case HTTP_STATUS_CODE_308_PERMANENT_REDIRECT:
return "308 Permanent Redirect";
break;
case HTTP_STATUS_CODE_400_BAD_REQUEST:
return "400 Bad Request";
break;
case HTTP_STATUS_CODE_401_UNAUTHORIZED:
return "401 Unauthorized";
break;
case HTTP_STATUS_CODE_402_PAYMENT_REQUIRED:
return "402 Payment Required";
break;
case HTTP_STATUS_CODE_403_FORBIDDEN:
return "403 Forbidden";
break;
case HTTP_STATUS_CODE_404_NOT_FOUND:
return "404 Not Found";
break;
case HTTP_STATUS_CODE_405_METHOD_NOT_ALLOWED:
return "405 Method Not Allowed";
break;
case HTTP_STATUS_CODE_406_NOT_ACCEPTABLE:
return "406 Not Acceptable";
break;
case HTTP_STATUS_CODE_407_PROXY_AUTHENTICATION_REQUIRED:
return "407 Proxy Authentication Required";
break;
case HTTP_STATUS_CODE_408_REQUEST_TIMEOUT:
return "408 Request Timeout";
break;
case HTTP_STATUS_CODE_409_CONFLICT:
return "409 Conflict";
break;
case HTTP_STATUS_CODE_410_GONE:
return "410 Gone";
break;
case HTTP_STATUS_CODE_411_LENGTH_REQUIRED:
return "411 Length Required";
break;
case HTTP_STATUS_CODE_412_PRECONDITION_FAILED:
return "412 Precondition Failed";
break;
case HTTP_STATUS_CODE_413_REQUEST_ENTITY_TOO_LARGE:
return "413 Payload Too Large";
break;
case HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LONG:
return "414 URI Too Long";
break;
case HTTP_STATUS_CODE_415_UNSUPPORTED_MEDIA_TYPE:
return "415 Unsupported Media Type";
break;
case HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE:
return "416 Range Not Satisfiable";
break;
case HTTP_STATUS_CODE_417_EXPECTATION_FAILED:
return "417 Expectation Failed";
break;
case HTTP_STATUS_CODE_418_IM_A_TEAPOT:
return "418 I'm a teapot";
break;
case HTTP_STATUS_CODE_421_MISDIRECTED_REQUEST:
return "421 Misdirected Request";
break;
case HTTP_STATUS_CODE_422_UNPROCESSABLE_ENTITY:
return "422 Unprocessable Entity";
break;
case HTTP_STATUS_CODE_423_LOCKED:
return "423 Locked";
break;
case HTTP_STATUS_CODE_424_FAILED_DEPENDENCY:
return "424 Failed Dependency";
break;
case HTTP_STATUS_CODE_425_TOO_EARLY:
return "425 Too Early";
break;
case HTTP_STATUS_CODE_426_UPGRADE_REQUIRED:
return "426 Upgrade Required";
break;
case HTTP_STATUS_CODE_428_PRECONDITION_REQUIRED:
return "428 Precondition Required";
break;
case HTTP_STATUS_CODE_429_TOO_MANY_REQUESTS:
return "429 Too Many Requests";
break;
case HTTP_STATUS_CODE_431_REQUEST_HEADER_FIELDS_TOO_LARGE:
return "431 Request Header Fields Too Large";
break;
case HTTP_STATUS_CODE_451_UNAVAILABLE_FOR_LEGAL_REASONS:
return "451 Unavailable For Legal Reasons";
break;
case HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR:
return "500 Internal Server Error";
break;
case HTTP_STATUS_CODE_501_NOT_IMPLEMENTED:
return "501 Not Implemented";
break;
case HTTP_STATUS_CODE_502_BAD_GATEWAY:
return "502 Bad Gateway";
break;
case HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE:
return "503 Service Unavailable";
break;
case HTTP_STATUS_CODE_504_GATEWAY_TIMEOUT:
return "504 Gateway Timeout";
break;
case HTTP_STATUS_CODE_505_HTTP_VERSION_NOT_SUPPORTED:
return "505 HTTP Version Not Supported";
break;
case HTTP_STATUS_CODE_506_VARIANT_ALSO_NEGOTIATES:
return "506 Variant Also Negotiates";
break;
case HTTP_STATUS_CODE_507_INSUFFICIENT_STORAGE:
return "507 Insufficient Storage";
break;
case HTTP_STATUS_CODE_508_LOOP_DETECTED:
return "508 Loop Detected";
break;
case HTTP_STATUS_CODE_510_NOT_EXTENDED:
return "510 Not Extended";
break;
case HTTP_STATUS_CODE_511_NETWORK_AUTHENTICATION_REQUIRED:
return "511 Network Authentication Required";
break;
case HTTP_STATUS_CODE_UNKNOWN:
default:
return "0 UNK";
}
return "0 UNK";
};
String HTTPServerEnums::get_status_code_header_stringi(const int code) {
return get_status_code_header_string(static_cast<HTTPStatusCode>(code));
}
void HTTPServerEnums::_bind_methods() {
BIND_ENUM_CONSTANT(HTTP_METHOD_GET);
BIND_ENUM_CONSTANT(HTTP_METHOD_POST);
@ -74,7 +277,7 @@ void HTTPServerEnums::_bind_methods() {
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_411_LENGTH_REQUIRED);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_412_PRECONDITION_FAILED);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_413_REQUEST_ENTITY_TOO_LARGE);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LARGE);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LONG);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_415_UNSUPPORTED_MEDIA_TYPE);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE);
BIND_ENUM_CONSTANT(HTTP_STATUS_CODE_417_EXPECTATION_FAILED);

View File

@ -80,7 +80,7 @@ public:
HTTP_STATUS_CODE_411_LENGTH_REQUIRED = 411,
HTTP_STATUS_CODE_412_PRECONDITION_FAILED = 412,
HTTP_STATUS_CODE_413_REQUEST_ENTITY_TOO_LARGE = 413,
HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LARGE = 414,
HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LONG = 414,
HTTP_STATUS_CODE_415_UNSUPPORTED_MEDIA_TYPE = 415,
HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
HTTP_STATUS_CODE_417_EXPECTATION_FAILED = 417,
@ -108,6 +108,9 @@ public:
HTTP_STATUS_CODE_511_NETWORK_AUTHENTICATION_REQUIRED = 511
};
static String get_status_code_header_string(const HTTPStatusCode code);
static String get_status_code_header_stringi(const int code);
HTTPServerEnums();
~HTTPServerEnums();

View File

@ -213,17 +213,19 @@ void HTTPServerSimple::poll() {
}
}
void HTTPServerSimple::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
void HTTPServerSimple::send_redirect(Ref<WebServerRequest> request, const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
//String s = "HTTP/1.1 " + itos(static_cast<int>(status_code)) + " Found\r\n";
String s = "HTTP/1.1 302 Found\r\n";
String s = "HTTP/1.1 " + HTTPServerEnums::get_status_code_header_string(status_code) + "\r\n";
s += "Location: " + location + "\r\n";
s += "Connection: Close\r\n";
s += "\r\n";
CharString cs = s.utf8();
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
}
void HTTPServerSimple::send(const String &body) {
String s = "HTTP/1.1 200 OK\r\n";
void HTTPServerSimple::send(Ref<WebServerRequest> request) {
String body = request->get_compiled_body();
String s = "HTTP/1.1 " + HTTPServerEnums::get_status_code_header_string(request->get_status_code()) + "\r\n";
s += "Content-Length: " + itos(body.size()) + "\r\n";
s += "Content-type: text/html\r\n";
s += "Connection: Close\r\n";
@ -233,7 +235,7 @@ void HTTPServerSimple::send(const String &body) {
CharString cs = s.utf8();
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
}
void HTTPServerSimple::send_file(const String &p_file_path) {
void HTTPServerSimple::send_file(Ref<WebServerRequest> request, const String &p_file_path) {
if (!FileAccess::exists(p_file_path)) {
String s = "HTTP/1.1 404 Not Found\r\n";
s += "Connection: Close\r\n";

View File

@ -41,6 +41,7 @@
class HTTPParser;
class WebServerSimple;
class WebServerRequest;
class HTTPServerSimple : public Reference {
public:
@ -51,9 +52,9 @@ public:
void _send_response();
void poll();
void send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code);
void send(const String &body);
void send_file(const String &p_file_path);
void send_redirect(Ref<WebServerRequest> request, const String &location, const HTTPServerEnums::HTTPStatusCode status_code);
void send(Ref<WebServerRequest> request);
void send_file(Ref<WebServerRequest> request, const String &p_file_path);
HTTPServerSimple();
~HTTPServerSimple();

View File

@ -74,13 +74,13 @@ String SimpleWebServerRequest::get_parameter(const String &key) const {
void SimpleWebServerRequest::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
ERR_FAIL_COND(!_server);
_server->send_redirect(location, status_code);
_server->send_redirect(Ref<WebServerRequest>(this), location, status_code);
}
void SimpleWebServerRequest::send() {
ERR_FAIL_COND(!_server);
_server->send(compiled_body);
_server->send(Ref<WebServerRequest>(this));
// if (connection_closed) {
// SimpleWebServerRequestPool::return_request(this);
@ -93,7 +93,7 @@ void SimpleWebServerRequest::send() {
void SimpleWebServerRequest::send_file(const String &p_file_path) {
ERR_FAIL_COND(!_server);
_server->send_file(p_file_path);
_server->send_file(Ref<WebServerRequest>(this), p_file_path);
// SimpleWebServerRequestPool::return_request(this);
}