diff --git a/CMakeLists.txt b/CMakeLists.txt index 23b8e8c..9eea334 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) \ No newline at end of file +install(DIRECTORY ${HEADERS} DESTINATION include COMPONENT Development) diff --git a/src/rum/http/method.cpp b/src/rum/http/method.cpp index decdeed..1699541 100644 --- a/src/rum/http/method.cpp +++ b/src/rum/http/method.cpp @@ -19,6 +19,8 @@ std::string to_string(Method method) { return "OPTIONS"; case TRACE: return "TRACE"; + default: + return ""; } } } // namespace Rum::HTTP diff --git a/src/rum/http/request.cpp b/src/rum/http/request.cpp index cb19e6a..5a5b861 100644 --- a/src/rum/http/request.cpp +++ b/src/rum/http/request.cpp @@ -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) { diff --git a/src/rum/http/response.cpp b/src/rum/http/response.cpp index 7cc56b9..cb72b99 100644 --- a/src/rum/http/response.cpp +++ b/src/rum/http/response.cpp @@ -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 \ No newline at end of file +} // namespace Rum::HTTP diff --git a/src/rum/http/server.cpp b/src/rum/http/server.cpp index 232cfa2..51eec9d 100644 --- a/src/rum/http/server.cpp +++ b/src/rum/http/server.cpp @@ -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 = "

404: Page not found :C

"; } - } catch (std::out_of_range) { - } catch (TCP::Error) { + } catch (std::out_of_range&) { + } catch (TCP::Error&) { } } } // namespace Rum::HTTP \ No newline at end of file diff --git a/src/rum/http/uri.cpp b/src/rum/http/uri.cpp index a73d4f5..022d811 100644 --- a/src/rum/http/uri.cpp +++ b/src/rum/http/uri.cpp @@ -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) { diff --git a/src/rum/tcp/error.cpp b/src/rum/tcp/error.cpp index d1c6325..29ebaef 100644 --- a/src/rum/tcp/error.cpp +++ b/src/rum/tcp/error.cpp @@ -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 \ No newline at end of file diff --git a/src/rum/tcp/server.cpp b/src/rum/tcp/server.cpp index df591a1..026180d 100644 --- a/src/rum/tcp/server.cpp +++ b/src/rum/tcp/server.cpp @@ -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);