c++20 support

This commit is contained in:
BENEDEK László 2024-05-17 15:35:42 +02:00
parent 8dc707cf6b
commit 8da3921507
8 changed files with 38 additions and 16 deletions

View File

@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.25)
project(Rum VERSION 1.0)
# compiler setup
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
@ -44,4 +44,4 @@ install(TARGETS
ARCHIVE
COMPONENT Development
)
install(DIRECTORY ${HEADERS} DESTINATION include COMPONENT Development)
install(DIRECTORY ${HEADERS} DESTINATION include COMPONENT Development)

View File

@ -19,6 +19,8 @@ std::string to_string(Method method) {
return "OPTIONS";
case TRACE:
return "TRACE";
default:
return "";
}
}
} // namespace Rum::HTTP

View File

@ -16,6 +16,9 @@ Request::operator std::string() const {
for (auto cookie : cookies) {
cookie_string += "\t\t'" + cookie.first + "': '" + cookie.second + "'\n";
}
#if __cplusplus < 202302L
return "";
#else
return std::vformat(
"Request{{\n"
"\tmethod: {}\n"
@ -25,6 +28,7 @@ Request::operator std::string() const {
"\tbody: '{}'\n"
"}}",
std::make_format_args(to_string(method), (std::string)uri, headers_string, cookie_string, body));
#endif
}
std::ostream& operator<<(std::ostream& stream, const Request req) {

View File

@ -72,7 +72,7 @@ std::string to_string(int code) {
try {
return codes.at(code);
} catch (std::out_of_range) {
} catch (std::out_of_range&) {
return codes.at((code / 100) * 100);
}
}
@ -85,13 +85,19 @@ Response::~Response() {
for (Cookie& cookie : cookies) {
headers_string += "Set-cookie: " + (std::string)cookie + "\r\n";
}
#if __cplusplus < 202302L
std::string message =
"HTTP/1.1 "+std::to_string(code)+" "+to_string(code)+"\r\n"+
headers_string + "\r\n"+
body;
#else
std::string message = std::vformat(
"HTTP/1.1 {} {}\r\n"
"{}"
"\r\n"
"{}",
std::make_format_args(std::to_string(code), to_string(code), headers_string, body));
#endif
send(client_sock, message.c_str(), message.size(), 0);
}
} // namespace Rum::HTTP
} // namespace Rum::HTTP

View File

@ -119,7 +119,11 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
message += std::string(buffer, buffer + recieved);
#if __cplusplus < 202302L
if (stage == METHOD && message.find("\r\n") != message.npos) {
#else
if (stage == METHOD && message.contains("\r\n")) {
#endif
std::regex method_regex("(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE) (\\/.*) HTTP.*");
std::smatch match;
if (std::regex_match(message.cbegin(), message.cbegin() + message.find("\r\n"), match, method_regex)) {
@ -136,7 +140,11 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
}
}
#if __cplusplus < 202302L
if (stage == HEADER && message.find("\r\n\r\n") != message.npos) {
#else
if (stage == HEADER && message.contains("\r\n\r\n")) {
#endif
std::regex header_regex("(.*): (.*)");
for (std::sregex_iterator it = std::sregex_iterator(message.cbegin(), message.cbegin() + message.find("\r\n\r\n"), header_regex);
it != std::sregex_iterator(); it++) {
@ -176,15 +184,12 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
}
if (stage == BODY) {
std::cout << "here" << std::endl;
try {
size_t content_length = std::stoul(request.get_header("content-length"));
std::cout << message.size() << " " << content_length << std::endl;
if (message.size() < content_length)
continue;
} catch (std::out_of_range) {
} catch (std::invalid_argument e) {
} catch (std::out_of_range&) {
} catch (std::invalid_argument& e) {
std::cerr << "invlaid Content-Length header" << std::endl;
}
request.set_body(message);
@ -210,8 +215,8 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
resp.set_code(404);
resp.body = "<h1>404: Page not found :C</h1>";
}
} catch (std::out_of_range) {
} catch (TCP::Error) {
} catch (std::out_of_range&) {
} catch (TCP::Error&) {
}
}
} // namespace Rum::HTTP

View File

@ -12,7 +12,9 @@ URI::operator std::string() const {
for (auto param : parameters) {
query += "\t\t'" + param.first + "': '" + param.second + "'\n";
}
#if __cplusplus < 202302L
return "";
#else
return std::vformat(
"URI{{"
"\n\tscheme: '{}'"
@ -25,6 +27,7 @@ URI::operator std::string() const {
"\n\tfragment: '{}'"
"\n}}",
std::make_format_args(scheme, user, password, host, port, path, query, fragment));
#endif
}
URI::URI(const std::string& uri) {

View File

@ -8,6 +8,8 @@ const char* Error::what() const noexcept {
return "the connection is already closed";
case UNKNOWN:
return "an unknown error has occured";
default:
return "";
}
}
} // namespace Rum::TCP

View File

@ -19,7 +19,7 @@ Server::Server(unsigned short port) : sock(socket(AF_INET, SOCK_STREAM, 0)), por
std::signal(SIGPIPE, SIG_IGN);
sockaddr_in address = {.sin_family = AF_INET, .sin_port = htons(port), .sin_addr = {.s_addr = INADDR_ANY}};
sockaddr_in address = {.sin_family = AF_INET, .sin_port = htons(port), .sin_addr = {.s_addr = INADDR_ANY}, .sin_zero="0000000"};
if (bind(sock, (struct sockaddr*)&address, sizeof(address)) == -1)
throw Error(Error::UNKNOWN);