@@ -36,16 +36,15 @@ HttpServer::~HttpServer() {
3636 this ->stop_listening ();
3737 for (int i = 0 ; i < threads.size (); i++) {
3838 threads[i].join ();
39- std::cout << " thread removed: " << i << " \n " ;
39+ // std::cout << "thread removed: " << i << "\n";
4040 }
4141 threads.clear ();
4242}
4343
4444void HttpServer::listen (int port) {
4545 listener_fd = get_listener_socket (port);
4646 if (listener_fd < 0 ) {
47- std::cerr << " unable to obtain listener socket, exiting\n " ;
48- std::exit (EXIT_FAILURE);
47+ throw std::runtime_error (" unable to obtain listener socket, exiting\n " );
4948 }
5049
5150 std::cout << " server listening now...\n " ;
@@ -65,8 +64,7 @@ void HttpServer::listen(int port) {
6564 if (errno == EBADF || errno == EINVAL || errno == EOPNOTSUPP) break ;
6665
6766 // Otherwise log and continue or break as appropriate.
68- std::cerr << strerror (errno) << " : issue trying to accept incoming connection\n " ;
69- continue ;
67+ throw std::runtime_error (" unable to obtain a valid connection file descriptor, exiting\n " );
7068 }
7169 this ->store_conn_fd (conn_file_descriptor);
7270 }
@@ -89,7 +87,6 @@ void HttpServer::stop_listening() {
8987 close (listener_fd);
9088 listener_fd = -1 ;
9189 }
92- std::cout << " stopped listening\n " ;
9390}
9491
9592
@@ -131,7 +128,7 @@ void HttpServer::handle_client() {
131128
132129 // check for valid method
133130 std::string_view method = path.substr (0 , method_itr);
134- std::cout << " method: " << method << ' \n ' ;
131+ // std::cout << "method: " << method << '\n';
135132
136133 // get the route which is the second word
137134 size_t route_start = method_itr + 1 ;
@@ -143,7 +140,7 @@ void HttpServer::handle_client() {
143140 }
144141
145142 std::string_view route = path.substr (route_start, route_end - route_start);
146- std::cout << " route: " << route << ' \n ' ;
143+ // std::cout << "route: " << route << '\n';
147144
148145 // get body
149146 size_t req_body_start = path.find (" \r\n\r\n " ) + 4 ;
@@ -153,7 +150,7 @@ void HttpServer::handle_client() {
153150 }
154151
155152 std::string_view req_body = path.substr (req_body_start, path.size () - req_body_start);
156- std::cout << " body: " << req_body << ' \n ' ;
153+ // std::cout << "body: " << req_body << '\n';
157154
158155 // TODO: create a map that has a key route and function pointer
159156
@@ -191,10 +188,11 @@ void HttpServer::handle_client() {
191188 case compile_time_method_hash (" PUT" ):
192189 case compile_time_method_hash (" PATCH" ): {
193190 const Request req { path, std::string (req_body)};
194- Handler route_fn = routes[method][route];
195191 if (routes[method].find (route) != routes[method].end ()) {
196192 Handler route_fn = routes[method][route];
197- route_fn (req, res);
193+ if (route_fn != nullptr ) {
194+ route_fn (req, res);
195+ }
198196 response =
199197 " HTTP/1.1 200 OK\r\n "
200198 " Content-Length: " + std::to_string (res.body .size ()) + " \r\n "
@@ -221,7 +219,7 @@ void HttpServer::handle_client() {
221219 " \r\n " +
222220 std::string (res.body );
223221
224- std::cout << request_buffer << " \n " ;
222+ // std::cout << request_buffer << "\n";
225223 }
226224 }
227225 int bytes_sent = send (conn_fd, response.c_str (), response.size (), 0 );
@@ -230,7 +228,7 @@ void HttpServer::handle_client() {
230228 std::cerr << " \n\n " << strerror (errno) << " : issue sending message to connection\n " ;
231229 continue ;
232230 }
233- std::cout << request_buffer << " \n " ;
231+ // std::cout << request_buffer << "\n";
234232 close (conn_fd);
235233 }
236234}
@@ -248,8 +246,7 @@ int HttpServer::get_listener_socket(int port) {
248246
249247 int status = getaddrinfo (Constants::hostname, port_str.c_str (), &hints, &results);
250248 if (status != 0 ) {
251- std::cerr << stderr << " gai error: " << gai_strerror (status) << ' \n ' ;
252- return 1 ;
249+ throw std::runtime_error (" gai error: " + std::string (gai_strerror (status)));
253250 }
254251
255252 // find the first file descriptor that does not fail
@@ -264,14 +261,13 @@ int HttpServer::get_listener_socket(int port) {
264261 int yes = 1 ;
265262 int sockopt_status = setsockopt (socket_file_descriptor, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof (int ));
266263 if (sockopt_status == -1 ) {
267- std::cerr << " \n\n " << strerror (errno) << " : issue setting socket options\n " ;
268- return 1 ;
264+ throw std::runtime_error (std::string (strerror (errno)) + " : issue setting socket options" );
269265 }
270266
271267 // associate the socket descriptor with the port passed into getaddrinfo()
272268 int bind_status = bind (socket_file_descriptor, addrinfo_ptr->ai_addr , addrinfo_ptr->ai_addrlen );
273269 if (bind_status == -1 ) {
274- std::cerr << " \n\n " << strerror (errno) << " : issue binding the socket descriptor with a port\n " ;
270+ std::cerr << " \n\n " << strerror (errno) << " : issue binding the socket descriptor with a port" ;
275271 continue ;
276272 }
277273
@@ -281,14 +277,12 @@ int HttpServer::get_listener_socket(int port) {
281277 freeaddrinfo (results);
282278
283279 if (addrinfo_ptr == nullptr ) {
284- std::cerr << " \n\n " << strerror (errno) << " : failed to bind port to socket\n " ;
285- return 1 ;
280+ throw std::runtime_error (std::string (strerror (errno)) + " : failed to bind port to socket" );
286281 }
287282
288283 int listen_status = ::listen (socket_file_descriptor, Constants::backlog);
289284 if (listen_status == -1 ) {
290- std::cerr << " \n\n " << strerror (errno) << " : issue trying to call listen()\n " ;
291- return 1 ;
285+ throw std::runtime_error (std::string (strerror (errno)) + " : issue trying to call listen()" );
292286 }
293287
294288 return socket_file_descriptor;
0 commit comments