From a3389a3fee20b8186754bca73faac8ee8c4e7146 Mon Sep 17 00:00:00 2001 From: awwit Date: Thu, 31 May 2018 16:12:19 +0300 Subject: [PATCH 1/2] Added output of messages with exceptions. --- src/server/Server.cpp | 4 ++-- src/server/ServerSettings.cpp | 6 ++---- src/server/config/ConfigParser.cpp | 2 +- src/server/protocol/ServerProtocol.cpp | 12 ++++++++---- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/server/Server.cpp b/src/server/Server.cpp index df464eb..ca069e9 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -313,7 +313,7 @@ namespace HttpServer app->application_final(root.data() ); } } - catch (std::exception &exc) { + catch (const std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl; } @@ -460,7 +460,7 @@ namespace HttpServer app->application_init(root.data() ); } } - catch (std::exception &exc) { + catch (const std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << module_name << "' was initialized: " << exc.what() << std::endl; } } diff --git a/src/server/ServerSettings.cpp b/src/server/ServerSettings.cpp index 3677a62..4370d1e 100644 --- a/src/server/ServerSettings.cpp +++ b/src/server/ServerSettings.cpp @@ -34,15 +34,13 @@ namespace HttpServer for (auto &app : applications) { - try - { + try { if (app->application_final) { const std::string root = app->root_dir; app->application_final(root.data() ); } } - catch (std::exception &exc) - { + catch (const std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl; } diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index ab08166..c00ea41 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -283,7 +283,7 @@ namespace HttpServer success = app_init(root.data() ); } } - catch (std::exception &exc) { + catch (const std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << it_module->second << "' was initialized: " << exc.what() << std::endl; success = false; } diff --git a/src/server/protocol/ServerProtocol.cpp b/src/server/protocol/ServerProtocol.cpp index 42628eb..4f3010d 100644 --- a/src/server/protocol/ServerProtocol.cpp +++ b/src/server/protocol/ServerProtocol.cpp @@ -2,6 +2,8 @@ #include "ServerProtocol.h" #include "../../utils/Utils.h" +#include + namespace HttpServer { ServerProtocol::ServerProtocol( @@ -167,8 +169,9 @@ namespace HttpServer // Launch application req.app_exit_code = appSets.application_call(&request, &response); } - catch (std::exception &exc) { - // TODO: exception output + catch (const std::exception &exc) { + // Exception output + std::cout << "Exception when application_call: " << exc.what() << std::endl; } if (response.response_data && response.data_size) @@ -181,8 +184,9 @@ namespace HttpServer try { appSets.application_clear(response.response_data, response.data_size); } - catch (std::exception &exc) { - // TODO: exception output + catch (const std::exception &exc) { + // Exception output + std::cout << "Exception when application_clear: " << exc.what() << std::endl; } } } From d824fe72fe134e5abcddbb7879dd339a06142b92 Mon Sep 17 00:00:00 2001 From: awwit Date: Mon, 30 Jul 2018 15:31:31 +0300 Subject: [PATCH 2/2] Fixed issue #6 --- src/server/protocol/extensions/Sendfile.cpp | 6 +++-- src/socket/Socket.cpp | 26 ++++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/server/protocol/extensions/Sendfile.cpp b/src/server/protocol/extensions/Sendfile.cpp index 2799f57..dc46186 100644 --- a/src/server/protocol/extensions/Sendfile.cpp +++ b/src/server/protocol/extensions/Sendfile.cpp @@ -221,7 +221,8 @@ namespace HttpServer additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(fileTime, true) ); // Отправить заголовки (206 Partial Content) - if (prot.sendHeaders( + if ( + prot.sendHeaders( Http::StatusCode::PARTIAL_CONTENT, additionalHeaders, req.timeout, @@ -377,7 +378,8 @@ namespace HttpServer additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(file_time, true) ); // Отправить заголовки (200 OK) - if (prot.sendHeaders( + if ( + prot.sendHeaders( Http::StatusCode::OK, additionalHeaders, req.timeout, diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index 40b0f92..05263db 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -103,17 +103,21 @@ namespace Socket } bool Socket::bind(const int port) const noexcept { + // GCC bug with global namespace: https://bbs.archlinux.org/viewtopic.php?id=53751 + auto const net_port = htons(static_cast(port)); + auto const net_addr = ::htonl(INADDR_ANY); + const ::sockaddr_in sock_addr { AF_INET, - ::htons(port), - ::htonl(INADDR_ANY), - 0 + net_port, + { net_addr }, + { 0 } // sin_zero }; return 0 == ::bind( this->socket_handle, - reinterpret_cast(&sock_addr), - sizeof(sockaddr_in) + reinterpret_cast(&sock_addr), + sizeof(::sockaddr_in) ); } @@ -147,9 +151,9 @@ namespace Socket #ifdef WIN32 WSAPOLLFD event { this->socket_handle, - POLLRDNORM, - 0 - }; + POLLRDNORM, + 0 + }; if (1 == ::WSAPoll(&event, 1, ~0) && event.revents & POLLRDNORM) { client_socket = ::accept( @@ -161,9 +165,9 @@ namespace Socket #elif POSIX struct ::pollfd event { this->socket_handle, - POLLIN, - 0 - }; + POLLIN, + 0 + }; if (1 == ::poll(&event, 1, ~0) && event.revents & POLLIN) { client_socket = ::accept(