From 5ce69d6cf9a67355f42034b67872030cd2ac3bf5 Mon Sep 17 00:00:00 2001 From: James Yab Date: Sun, 14 Dec 2025 14:41:09 -0500 Subject: [PATCH 1/3] Comment out logging information --- src/HttpServer.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/HttpServer.cpp b/src/HttpServer.cpp index 274e349..d5d187b 100644 --- a/src/HttpServer.cpp +++ b/src/HttpServer.cpp @@ -36,7 +36,7 @@ HttpServer::~HttpServer() { this->stop_listening(); for (int i = 0; i < threads.size(); i++) { threads[i].join(); - std::cout << "thread removed: " << i << "\n"; + // std::cout << "thread removed: " << i << "\n"; } threads.clear(); } @@ -89,7 +89,6 @@ void HttpServer::stop_listening() { close(listener_fd); listener_fd = -1; } - std::cout << "stopped listening\n"; } @@ -131,7 +130,7 @@ void HttpServer::handle_client() { // check for valid method std::string_view method = path.substr(0, method_itr); - std::cout << "method: " << method << '\n'; + // std::cout << "method: " << method << '\n'; // get the route which is the second word size_t route_start = method_itr + 1; @@ -143,7 +142,7 @@ void HttpServer::handle_client() { } std::string_view route = path.substr(route_start, route_end - route_start); - std::cout << "route: " << route << '\n'; + // std::cout << "route: " << route << '\n'; // get body size_t req_body_start = path.find("\r\n\r\n") + 4; @@ -153,7 +152,7 @@ void HttpServer::handle_client() { } std::string_view req_body = path.substr(req_body_start, path.size() - req_body_start); - std::cout << "body: " << req_body << '\n'; + // std::cout << "body: " << req_body << '\n'; // TODO: create a map that has a key route and function pointer @@ -221,7 +220,7 @@ void HttpServer::handle_client() { "\r\n" + std::string(res.body); - std::cout << request_buffer << "\n"; + // std::cout << request_buffer << "\n"; } } int bytes_sent = send(conn_fd, response.c_str(), response.size(), 0); @@ -230,7 +229,7 @@ void HttpServer::handle_client() { std::cerr << "\n\n" << strerror(errno) << ": issue sending message to connection\n"; continue; } - std::cout << request_buffer << "\n"; + // std::cout << request_buffer << "\n"; close(conn_fd); } } From 49dc3f11699e246b98e0947bdfd0c3e490bef9a7 Mon Sep 17 00:00:00 2001 From: James Yab Date: Sun, 14 Dec 2025 14:43:10 -0500 Subject: [PATCH 2/3] Fix an issue where the program attempts to run a null function pointer --- src/HttpServer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/HttpServer.cpp b/src/HttpServer.cpp index d5d187b..736a1c5 100644 --- a/src/HttpServer.cpp +++ b/src/HttpServer.cpp @@ -190,10 +190,11 @@ void HttpServer::handle_client() { case compile_time_method_hash("PUT"): case compile_time_method_hash("PATCH"): { const Request req { path, std::string(req_body)}; - Handler route_fn = routes[method][route]; if (routes[method].find(route) != routes[method].end()) { Handler route_fn = routes[method][route]; - route_fn(req, res); + if (route_fn != nullptr) { + route_fn(req, res); + } response = "HTTP/1.1 200 OK\r\n" "Content-Length: " + std::to_string(res.body.size()) + "\r\n" From 8c8b9d4f00205935287a1fadb812e2fcfdc9f116 Mon Sep 17 00:00:00 2001 From: James Yab Date: Sun, 14 Dec 2025 14:50:03 -0500 Subject: [PATCH 3/3] Throw exceptions and propagate to main.cpp so that memory can be properly handled after errors A std::exit() terminates the whole application, which is bad for a library. It would prevent RAII, destructors, and stack unwinding. Throwing exceptions allows fixes this issue. --- src/HttpServer.cpp | 20 +++++++------------- src/main.cpp | 14 ++++++++------ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/HttpServer.cpp b/src/HttpServer.cpp index 736a1c5..069e8e9 100644 --- a/src/HttpServer.cpp +++ b/src/HttpServer.cpp @@ -44,8 +44,7 @@ HttpServer::~HttpServer() { void HttpServer::listen(int port) { listener_fd = get_listener_socket(port); if (listener_fd < 0) { - std::cerr << "unable to obtain listener socket, exiting\n"; - std::exit(EXIT_FAILURE); + throw std::runtime_error("unable to obtain listener socket, exiting\n"); } std::cout << "server listening now...\n"; @@ -65,8 +64,7 @@ void HttpServer::listen(int port) { if (errno == EBADF || errno == EINVAL || errno == EOPNOTSUPP) break; // Otherwise log and continue or break as appropriate. - std::cerr << strerror(errno) << ": issue trying to accept incoming connection\n"; - continue; + throw std::runtime_error("unable to obtain a valid connection file descriptor, exiting\n"); } this->store_conn_fd(conn_file_descriptor); } @@ -248,8 +246,7 @@ int HttpServer::get_listener_socket(int port) { int status = getaddrinfo(Constants::hostname, port_str.c_str(), &hints, &results); if (status != 0) { - std::cerr << stderr << " gai error: " << gai_strerror(status) << '\n'; - return 1; + throw std::runtime_error("gai error: " + std::string(gai_strerror(status))); } // find the first file descriptor that does not fail @@ -264,14 +261,13 @@ int HttpServer::get_listener_socket(int port) { int yes = 1; int sockopt_status = setsockopt(socket_file_descriptor, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int)); if (sockopt_status == -1) { - std::cerr << "\n\n" << strerror(errno) << ": issue setting socket options\n"; - return 1; + throw std::runtime_error(std::string(strerror(errno)) + ": issue setting socket options"); } // associate the socket descriptor with the port passed into getaddrinfo() int bind_status = bind(socket_file_descriptor, addrinfo_ptr->ai_addr, addrinfo_ptr->ai_addrlen); if (bind_status == -1) { - std::cerr << "\n\n" << strerror(errno) << ": issue binding the socket descriptor with a port\n"; + std::cerr << "\n\n" << strerror(errno) << ": issue binding the socket descriptor with a port"; continue; } @@ -281,14 +277,12 @@ int HttpServer::get_listener_socket(int port) { freeaddrinfo(results); if (addrinfo_ptr == nullptr) { - std::cerr << "\n\n" << strerror(errno) << ": failed to bind port to socket\n"; - return 1; + throw std::runtime_error(std::string(strerror(errno)) + ": failed to bind port to socket"); } int listen_status = ::listen(socket_file_descriptor, Constants::backlog); if (listen_status == -1) { - std::cerr << "\n\n" << strerror(errno) << ": issue trying to call listen()\n"; - return 1; + throw std::runtime_error(std::string(strerror(errno)) + ": issue trying to call listen()"); } return socket_file_descriptor; diff --git a/src/main.cpp b/src/main.cpp index 8c1fcd9..56589fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ +#include + #include "../include/HttpServer.h" int main() { - HttpServer server {}; server.get_mapping("/test", [](const HttpServer::Request&, HttpServer::Response& res){ @@ -12,9 +13,10 @@ int main() { res.body = "this is the other route"; }); - server.post_mapping("/post", [](const HttpServer::Request& req, HttpServer::Response& res){ - res.body = "post api route: " + req.body; - }); - - server.listen(3490); + try { + server.listen(3490); + } catch (const std::exception& err) { + std::cerr << err.what() << '\n'; + return EXIT_FAILURE; + } }