From 0e20e1b78dc4387181ddfc32ba9f805051336179 Mon Sep 17 00:00:00 2001 From: awwit Date: Thu, 30 Mar 2017 22:28:45 +0300 Subject: [PATCH 01/22] Refactor code, categorize source code files. Add HTTP/2 protocol support --- README.md | 24 +- projects/qt-creator/httpserver.qbs | 158 +- src/DataVariantAbstract.h | 40 - src/DataVariantFormUrlencoded.cpp | 62 - src/DataVariantFormUrlencoded.h | 22 - src/DataVariantMultipartFormData.cpp | 426 -- src/DataVariantMultipartFormData.h | 35 - src/DataVariantTextPlain.cpp | 60 - src/DataVariantTextPlain.h | 22 - src/FileIncoming.cpp | 36 - src/FileIncoming.h | 41 - src/Main.cpp | 4 +- src/RawData.h | 20 - src/RequestParameters.cpp | 21 - src/RequestParameters.h | 31 - src/Server.cpp | 2036 ------ src/Server.h | 148 - src/ServerApplicationDefaultSettings.h | 12 - src/ServerRequest.h | 53 - src/ServerResponse.h | 23 - src/SignalHandlers.cpp | 36 +- src/SignalHandlers.h | 6 +- src/Socket.h | 76 - src/SocketAdapter.cpp | 15 - src/SocketAdapter.h | 32 - src/SocketAdapterDefault.cpp | 49 - src/SocketAdapterDefault.h | 28 - src/SocketAdapterTls.cpp | 122 - src/SocketAdapterTls.h | 32 - src/Utils.cpp | 459 -- src/Utils.h | 74 - src/server/Request.cpp | 20 + src/server/Request.h | 44 + src/server/Server.cpp | 1109 +++ src/server/Server.h | 62 + src/{ => server}/ServerApplicationSettings.h | 11 +- src/{ => server}/ServerApplicationsTree.cpp | 47 +- src/{ => server}/ServerApplicationsTree.h | 10 +- src/server/ServerControls.cpp | 75 + src/server/ServerControls.h | 37 + src/server/ServerSettings.cpp | 61 + src/server/ServerSettings.h | 25 + src/{ => server}/ServerStructuresArguments.h | 2 +- src/server/SocketsQueue.h | 20 + src/{ => server/config}/ConfigParser.cpp | 46 +- src/{ => server/config}/ConfigParser.h | 24 +- src/server/data-variant/Abstract.cpp | 20 + src/server/data-variant/Abstract.h | 40 + src/server/data-variant/FormUrlencoded.cpp | 63 + src/server/data-variant/FormUrlencoded.h | 15 + src/server/data-variant/MultipartFormData.cpp | 411 ++ src/server/data-variant/MultipartFormData.h | 19 + src/server/data-variant/TextPlain.cpp | 61 + src/server/data-variant/TextPlain.h | 15 + src/server/protocol/ServerHttp1.cpp | 563 ++ src/server/protocol/ServerHttp1.h | 29 + src/server/protocol/ServerHttp2.cpp | 728 ++ src/server/protocol/ServerHttp2.h | 21 + src/server/protocol/ServerHttp2Protocol.cpp | 178 + src/server/protocol/ServerHttp2Protocol.h | 25 + src/server/protocol/ServerHttp2Stream.cpp | 110 + src/server/protocol/ServerHttp2Stream.h | 16 + src/server/protocol/ServerProtocol.cpp | 166 + src/server/protocol/ServerProtocol.h | 39 + src/server/protocol/ServerWebSocket.cpp | 47 + src/server/protocol/ServerWebSocket.h | 22 + src/server/protocol/extensions/Sendfile.cpp | 375 + src/server/protocol/extensions/Sendfile.h | 41 + src/socket/Adapter.cpp | 25 + src/socket/Adapter.h | 33 + src/socket/AdapterDefault.cpp | 44 + src/socket/AdapterDefault.h | 26 + src/socket/AdapterTls.cpp | 136 + src/socket/AdapterTls.h | 30 + src/{SocketList.cpp => socket/List.cpp} | 59 +- src/{SocketList.h => socket/List.h} | 24 +- src/{ => socket}/Socket.cpp | 114 +- src/socket/Socket.h | 79 + src/system/Cache.h | 24 + src/{ => system}/GlobalMutex.cpp | 18 +- src/{ => system}/GlobalMutex.h | 18 +- src/{ => system}/Module.cpp | 26 +- src/{ => system}/Module.h | 28 +- src/{ => system}/SharedMemory.cpp | 16 +- src/{ => system}/SharedMemory.h | 16 +- src/{ => system}/System.cpp | 10 +- src/{ => system}/System.h | 12 +- src/transfer/AppRequest.h | 23 + src/transfer/AppResponse.h | 13 + src/transfer/FileIncoming.cpp | 108 + src/transfer/FileIncoming.h | 40 + src/transfer/HttpStatusCode.h | 52 + src/transfer/ProtocolVariant.h | 11 + src/transfer/http2/HPack.cpp | 6038 +++++++++++++++++ src/transfer/http2/HPack.h | 15 + src/transfer/http2/Http2.cpp | 182 + src/transfer/http2/Http2.h | 191 + src/{ => utils}/Event.cpp | 15 +- src/{ => utils}/Event.h | 16 +- src/utils/Utils.cpp | 841 +++ src/utils/Utils.h | 137 + 101 files changed, 12901 insertions(+), 4319 deletions(-) delete mode 100644 src/DataVariantAbstract.h delete mode 100644 src/DataVariantFormUrlencoded.cpp delete mode 100644 src/DataVariantFormUrlencoded.h delete mode 100644 src/DataVariantMultipartFormData.cpp delete mode 100644 src/DataVariantMultipartFormData.h delete mode 100644 src/DataVariantTextPlain.cpp delete mode 100644 src/DataVariantTextPlain.h delete mode 100644 src/FileIncoming.cpp delete mode 100644 src/FileIncoming.h delete mode 100644 src/RawData.h delete mode 100644 src/RequestParameters.cpp delete mode 100644 src/RequestParameters.h delete mode 100644 src/Server.cpp delete mode 100644 src/Server.h delete mode 100644 src/ServerApplicationDefaultSettings.h delete mode 100644 src/ServerRequest.h delete mode 100644 src/ServerResponse.h delete mode 100644 src/Socket.h delete mode 100644 src/SocketAdapter.cpp delete mode 100644 src/SocketAdapter.h delete mode 100644 src/SocketAdapterDefault.cpp delete mode 100644 src/SocketAdapterDefault.h delete mode 100644 src/SocketAdapterTls.cpp delete mode 100644 src/SocketAdapterTls.h delete mode 100644 src/Utils.cpp delete mode 100644 src/Utils.h create mode 100644 src/server/Request.cpp create mode 100644 src/server/Request.h create mode 100644 src/server/Server.cpp create mode 100644 src/server/Server.h rename src/{ => server}/ServerApplicationSettings.h (73%) rename src/{ => server}/ServerApplicationsTree.cpp (75%) rename src/{ => server}/ServerApplicationsTree.h (84%) create mode 100644 src/server/ServerControls.cpp create mode 100644 src/server/ServerControls.h create mode 100644 src/server/ServerSettings.cpp create mode 100644 src/server/ServerSettings.h rename src/{ => server}/ServerStructuresArguments.h (98%) create mode 100644 src/server/SocketsQueue.h rename src/{ => server/config}/ConfigParser.cpp (90%) rename src/{ => server/config}/ConfigParser.h (56%) create mode 100644 src/server/data-variant/Abstract.cpp create mode 100644 src/server/data-variant/Abstract.h create mode 100644 src/server/data-variant/FormUrlencoded.cpp create mode 100644 src/server/data-variant/FormUrlencoded.h create mode 100644 src/server/data-variant/MultipartFormData.cpp create mode 100644 src/server/data-variant/MultipartFormData.h create mode 100644 src/server/data-variant/TextPlain.cpp create mode 100644 src/server/data-variant/TextPlain.h create mode 100644 src/server/protocol/ServerHttp1.cpp create mode 100644 src/server/protocol/ServerHttp1.h create mode 100644 src/server/protocol/ServerHttp2.cpp create mode 100644 src/server/protocol/ServerHttp2.h create mode 100644 src/server/protocol/ServerHttp2Protocol.cpp create mode 100644 src/server/protocol/ServerHttp2Protocol.h create mode 100644 src/server/protocol/ServerHttp2Stream.cpp create mode 100644 src/server/protocol/ServerHttp2Stream.h create mode 100644 src/server/protocol/ServerProtocol.cpp create mode 100644 src/server/protocol/ServerProtocol.h create mode 100644 src/server/protocol/ServerWebSocket.cpp create mode 100644 src/server/protocol/ServerWebSocket.h create mode 100644 src/server/protocol/extensions/Sendfile.cpp create mode 100644 src/server/protocol/extensions/Sendfile.h create mode 100644 src/socket/Adapter.cpp create mode 100644 src/socket/Adapter.h create mode 100644 src/socket/AdapterDefault.cpp create mode 100644 src/socket/AdapterDefault.h create mode 100644 src/socket/AdapterTls.cpp create mode 100644 src/socket/AdapterTls.h rename src/{SocketList.cpp => socket/List.cpp} (83%) rename src/{SocketList.h => socket/List.h} (60%) rename src/{ => socket}/Socket.cpp (73%) create mode 100644 src/socket/Socket.h create mode 100644 src/system/Cache.h rename src/{ => system}/GlobalMutex.cpp (92%) rename src/{ => system}/GlobalMutex.h (67%) rename src/{ => system}/Module.cpp (84%) rename src/{ => system}/Module.h (51%) rename src/{ => system}/SharedMemory.cpp (95%) rename src/{ => system}/SharedMemory.h (76%) rename src/{ => system}/System.cpp (97%) rename src/{ => system}/System.h (88%) create mode 100644 src/transfer/AppRequest.h create mode 100644 src/transfer/AppResponse.h create mode 100644 src/transfer/FileIncoming.cpp create mode 100644 src/transfer/FileIncoming.h create mode 100644 src/transfer/HttpStatusCode.h create mode 100644 src/transfer/ProtocolVariant.h create mode 100644 src/transfer/http2/HPack.cpp create mode 100644 src/transfer/http2/HPack.h create mode 100644 src/transfer/http2/Http2.cpp create mode 100644 src/transfer/http2/Http2.h rename src/{ => utils}/Event.cpp (83%) rename src/{ => utils}/Event.h (59%) create mode 100644 src/utils/Utils.cpp create mode 100644 src/utils/Utils.h diff --git a/README.md b/README.md index 0e1b53b..75bcf02 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -httpserver -========== +# httpserver Http server is written on C++14 language. @@ -11,20 +10,19 @@ by using the parameter `server_module`. Sample application code: https://github.com/awwit/httpserverapp -Features --------- +## Features This http server support: * HTTP v1.1 * HTTPS (TLS) +* HTTP v2 (*need optimize cleanup of streams*) * Keep-Alive * WebSocket -* Get-Parted requests * X-Sendfile (header) +* Get-Parted requests -Dependencies ------------- +## Dependencies Common: @@ -34,8 +32,7 @@ Linux: `dl`, `pthread`, `rt`, `gnutls` Windows: `ws2_32.lib`, `libgnutls.dll.a` -Build ------ +## Build Linux: @@ -57,8 +54,7 @@ cd build devenv ./../projects/msvs/httpserver.sln /build ``` -Server start ------------- +## Server start ```sh ./httpserver --start @@ -70,14 +66,12 @@ Or input a parameter `--config-path=` to set the directory with configurat Use the parameter `--server-name=` to define the name of web-server's instance. Instances can be used to run web-servers with different settings. -Server configuration --------------------- +## Server configuration Server (and its applications) setting is made using config-files. Examples of settings are located in the folder [samples](samples/). -License -======= +# License The source codes are licensed under the [AGPL](http://www.gnu.org/licenses/agpl.html), diff --git a/projects/qt-creator/httpserver.qbs b/projects/qt-creator/httpserver.qbs index 67eab75..464d956 100644 --- a/projects/qt-creator/httpserver.qbs +++ b/projects/qt-creator/httpserver.qbs @@ -1,76 +1,100 @@ import qbs Project { - CppApplication { - name: "httpserver" + CppApplication { + name: "httpserver" - cpp.cxxLanguageVersion: "c++14" + cpp.cxxLanguageVersion: "c++14" - cpp.defines: qbs.buildVariant == "debug" ? base : base.concat(["DEBUG"]) + cpp.defines: qbs.buildVariant == "debug" ? ["DEBUG"] : original - cpp.dynamicLibraries: base.concat(["gnutls"]) + cpp.dynamicLibraries: ["gnutls"] - Properties { - condition: qbs.targetOS.contains("linux") - cpp.defines: outer.concat(["POSIX"]) - cpp.dynamicLibraries: outer.concat(["dl", "pthread", "rt"]) - } + Properties { + condition: qbs.targetOS.contains("linux") + cpp.platformDefines: outer.concat(["POSIX"]) + cpp.dynamicLibraries: outer.concat(["dl", "pthread", "rt"]) + } + Properties { + condition: qbs.targetOS.contains("windows") + cpp.platformDefines: outer.concat(["WIN32", "NOMINMAX"]) + } - Properties { - condition: qbs.targetOS.contains("windows") - cpp.defines: outer.concat(["WIN32", "NOMINMAX"]) - } - - files: [ - "../../src/ConfigParser.cpp", - "../../src/ConfigParser.h", - "../../src/DataVariantAbstract.h", - "../../src/DataVariantFormUrlencoded.cpp", - "../../src/DataVariantFormUrlencoded.h", - "../../src/DataVariantMultipartFormData.cpp", - "../../src/DataVariantMultipartFormData.h", - "../../src/DataVariantTextPlain.cpp", - "../../src/DataVariantTextPlain.h", - "../../src/Event.cpp", - "../../src/Event.h", - "../../src/FileIncoming.cpp", - "../../src/FileIncoming.h", - "../../src/GlobalMutex.cpp", - "../../src/GlobalMutex.h", - "../../src/Main.cpp", - "../../src/Main.h", - "../../src/Module.cpp", - "../../src/Module.h", - "../../src/RawData.h", - "../../src/RequestParameters.cpp", - "../../src/RequestParameters.h", - "../../src/Server.cpp", - "../../src/Server.h", - "../../src/ServerApplicationDefaultSettings.h", - "../../src/ServerApplicationSettings.h", - "../../src/ServerApplicationsTree.cpp", - "../../src/ServerApplicationsTree.h", - "../../src/ServerRequest.h", - "../../src/ServerResponse.h", - "../../src/ServerStructuresArguments.h", - "../../src/SharedMemory.cpp", - "../../src/SharedMemory.h", - "../../src/SignalHandlers.cpp", - "../../src/SignalHandlers.h", - "../../src/Socket.cpp", - "../../src/Socket.h", - "../../src/SocketAdapter.cpp", - "../../src/SocketAdapter.h", - "../../src/SocketAdapterDefault.cpp", - "../../src/SocketAdapterDefault.h", - "../../src/SocketAdapterTls.cpp", - "../../src/SocketAdapterTls.h", - "../../src/SocketList.cpp", - "../../src/SocketList.h", - "../../src/System.cpp", - "../../src/System.h", - "../../src/Utils.cpp", - "../../src/Utils.h", - ] - } + files: [ + "../../src/transfer/AppRequest.h", + "../../src/transfer/AppResponse.h", + "../../src/server/ServerControls.cpp", + "../../src/server/ServerControls.h", + "../../src/server/ServerSettings.cpp", + "../../src/server/ServerSettings.h", + "../../src/server/SocketsQueue.h", + "../../src/server/config/ConfigParser.cpp", + "../../src/server/config/ConfigParser.h", + "../../src/server/data-variant/Abstract.cpp", + "../../src/server/data-variant/Abstract.h", + "../../src/server/data-variant/FormUrlencoded.cpp", + "../../src/server/data-variant/FormUrlencoded.h", + "../../src/server/data-variant/MultipartFormData.cpp", + "../../src/server/data-variant/MultipartFormData.h", + "../../src/server/data-variant/TextPlain.cpp", + "../../src/server/data-variant/TextPlain.h", + "../../src/server/protocol/ServerHttp2Protocol.cpp", + "../../src/server/protocol/ServerHttp2Protocol.h", + "../../src/server/protocol/ServerHttp2Stream.cpp", + "../../src/server/protocol/ServerHttp2Stream.h", + "../../src/server/protocol/ServerWebSocket.cpp", + "../../src/server/protocol/ServerWebSocket.h", + "../../src/socket/Adapter.cpp", + "../../src/socket/Adapter.h", + "../../src/socket/AdapterDefault.cpp", + "../../src/socket/AdapterDefault.h", + "../../src/socket/AdapterTls.cpp", + "../../src/socket/AdapterTls.h", + "../../src/socket/List.cpp", + "../../src/socket/List.h", + "../../src/system/Cache.h", + "../../src/utils/Event.cpp", + "../../src/utils/Event.h", + "../../src/transfer/FileIncoming.cpp", + "../../src/transfer/FileIncoming.h", + "../../src/system/GlobalMutex.cpp", + "../../src/system/GlobalMutex.h", + "../../src/transfer/http2/HPack.cpp", + "../../src/transfer/http2/HPack.h", + "../../src/transfer/http2/Http2.cpp", + "../../src/transfer/http2/Http2.h", + "../../src/transfer/HttpStatusCode.h", + "../../src/Main.cpp", + "../../src/Main.h", + "../../src/system/Module.cpp", + "../../src/system/Module.h", + "../../src/transfer/ProtocolVariant.h", + "../../src/server/Request.cpp", + "../../src/server/Request.h", + "../../src/server/Server.cpp", + "../../src/server/Server.h", + "../../src/server/ServerApplicationSettings.h", + "../../src/server/ServerApplicationsTree.cpp", + "../../src/server/ServerApplicationsTree.h", + "../../src/server/protocol/ServerHttp1.cpp", + "../../src/server/protocol/ServerHttp1.h", + "../../src/server/protocol/ServerHttp2.cpp", + "../../src/server/protocol/ServerHttp2.h", + "../../src/server/protocol/ServerProtocol.cpp", + "../../src/server/protocol/ServerProtocol.h", + "../../src/server/protocol/extensions/Sendfile.cpp", + "../../src/server/protocol/extensions/Sendfile.h", + "../../src/server/ServerStructuresArguments.h", + "../../src/system/SharedMemory.cpp", + "../../src/system/SharedMemory.h", + "../../src/SignalHandlers.cpp", + "../../src/SignalHandlers.h", + "../../src/socket/Socket.cpp", + "../../src/socket/Socket.h", + "../../src/system/System.cpp", + "../../src/system/System.h", + "../../src/utils/Utils.cpp", + "../../src/utils/Utils.h", + ] + } } diff --git a/src/DataVariantAbstract.h b/src/DataVariantAbstract.h deleted file mode 100644 index 9058696..0000000 --- a/src/DataVariantAbstract.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include "SocketAdapter.h" -#include "RequestParameters.h" - -namespace HttpServer -{ - class DataVariantAbstract - { - protected: - std::string data_variant_name; - - public: - inline std::string getName() const { return data_variant_name; } - - public: - /** - * virtual destructor - */ - virtual ~DataVariantAbstract() = default; - - /** - * @param const Socket &sock - сокет, откуда можно достать остальные данные - * @param std::string &str - первая часть полученных данных - * @param const size_t leftBytes - сколько осталось данных (в байтах) получить из сокета - * @param const std::unordered_map &contentParams - дополнительные параметры, описывающие формат данных - * @param request_data &rp - данные текущего запроса (заголовки, параметры, коллекции для хранения данных) - * - * @return bool - (true|false) - удачно ли были разобраны данные - */ - virtual bool parse - ( - const SocketAdapter &sock, - std::string &str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) = 0; - }; -}; \ No newline at end of file diff --git a/src/DataVariantFormUrlencoded.cpp b/src/DataVariantFormUrlencoded.cpp deleted file mode 100644 index 6dc674b..0000000 --- a/src/DataVariantFormUrlencoded.cpp +++ /dev/null @@ -1,62 +0,0 @@ - -#include "DataVariantFormUrlencoded.h" - -#include "Utils.h" - -namespace HttpServer -{ - DataVariantFormUrlencoded::DataVariantFormUrlencoded() - { - data_variant_name = "application/x-www-form-urlencoded"; - } - - bool DataVariantFormUrlencoded::parse - ( - const SocketAdapter &sock, - std::string &str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) - { - if (str.empty() ) - { - return true; - } - - for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) - { - // Поиск следующего параметра - var_end = str.find('&', var_pos); - - // Поиск значения параметра - size_t delimiter = str.find('=', var_pos); - - if (delimiter >= var_end) - { - // Получить имя параметра - std::string var_name = Utils::urlDecode(str.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) ); - - // Сохранить параметр с пустым значением - rp.incoming_data.emplace(std::move(var_name), ""); - } - else - { - // Получить имя параметра - std::string var_name = Utils::urlDecode(str.substr(var_pos, delimiter - var_pos) ); - - ++delimiter; - - // Получить значение параметра - std::string var_value = Utils::urlDecode(str.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) ); - - // Сохранить параметр и значение - rp.incoming_data.emplace(std::move(var_name), std::move(var_value) ); - } - } - - str.clear(); - - return true; - } -}; \ No newline at end of file diff --git a/src/DataVariantFormUrlencoded.h b/src/DataVariantFormUrlencoded.h deleted file mode 100644 index 0c848c1..0000000 --- a/src/DataVariantFormUrlencoded.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "DataVariantAbstract.h" - -namespace HttpServer -{ - class DataVariantFormUrlencoded: public DataVariantAbstract - { - public: - DataVariantFormUrlencoded(); - - public: - virtual bool parse - ( - const SocketAdapter &sock, - std::string & str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) override; - }; -}; \ No newline at end of file diff --git a/src/DataVariantMultipartFormData.cpp b/src/DataVariantMultipartFormData.cpp deleted file mode 100644 index c79b309..0000000 --- a/src/DataVariantMultipartFormData.cpp +++ /dev/null @@ -1,426 +0,0 @@ - -#include "Utils.h" -#include "DataVariantMultipartFormData.h" - -#include - -namespace HttpServer -{ - DataVariantMultipartFormData::DataVariantMultipartFormData() - { - data_variant_name = "multipart/form-data"; - } - - bool DataVariantMultipartFormData::append - ( - const SocketAdapter &sock, - const std::chrono::milliseconds &timeout, - std::vector &buf, - std::string &str_buf, - const std::string &data_end, - const size_t &leftBytes, - long &recv_len, - size_t &recv_total_len - ) - { - // Завершаем работу, если уже получено байт сколько нужно - if (recv_total_len >= leftBytes) - { - return false; - } - - // Получаем данные из сокета - recv_len = sock.nonblock_recv(buf, timeout); - - // Завершаем работу, если ошибка получения данных через сокет - if (recv_len <= 0) - { - return false; - } - - // Обновляем общее количество полученных данных - recv_total_len += recv_len; - - // Добавляем полученные данные к рабочему буферу - str_buf.append(buf.cbegin(), buf.cbegin() + recv_len); - - // Завершаем работу, если в буфере меньше чем окончание блока данных - if (str_buf.length() <= data_end.length() ) - { - return false; - } - - return true; - } - - bool DataVariantMultipartFormData::parse - ( - const SocketAdapter &sock, - std::string &str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) - { - // Проверить есть ли в параметрах разделитель блоков данных - auto const it = contentParams.find("boundary"); - - if (contentParams.cend() == it) - { - return false; - } - - // Определить признак начала блока данных - std::string block_delimiter("--" + it->second); - // Определить признак окончания всех данных - std::string data_end("--" + it->second + "--\r\n"); - - if (0 == str.find(data_end) ) - { - return true; - } - - data_end = "\r\n" + data_end; - - // Установить размер буфера данных - const size_t buf_len = (leftBytes >= 512 * 1024) ? 512 * 1024 : leftBytes; - - // Создание буферов - std::vector buf(buf_len); - - long recv_len; // Прочитано байт при последнем извлечении данных из сокета - size_t recv_total_len = 0; // Получено байт из сокета всего - - // Поиск разделителя блока данных - // str_cur — текущая позиция в буфере - size_t str_cur = str.find(block_delimiter); - - if (std::string::npos == str_cur) - { - // Получить следующий кусок данных - if (false == append(sock, rp.timeout, buf, str, data_end, leftBytes, recv_len, recv_total_len) ) - { - return false; - } - - // Поиск разделителя блока данных - str_cur = str.find(block_delimiter); - - if (std::string::npos == str_cur) - { - return false; - } - } - - str_cur += block_delimiter.length() + 2; - - block_delimiter = "\r\n" + block_delimiter; - - // Если найден конец данных - bool is_find_data_end = false; - - do - { - // Правильно ли был передан блок данных - bool is_block_valid = true; - - // Поиск конца заголовков блока данных - size_t headers_end = str.find("\r\n\r\n", str_cur); - - // Если конец не был найден, то - if (std::string::npos == headers_end) - { - // Получить следующий кусок данных - if (false == append(sock, rp.timeout, buf, str, data_end, leftBytes, recv_len, recv_total_len) ) - { - return false; - } - - // Провести повторный поиск - headers_end = str.find("\r\n\r\n", str_cur); - - // Если снова не найдено, то данные некорректны - if (std::string::npos == headers_end) - { - is_block_valid = false; - } - } - - if (is_block_valid) - { - // Разобрать заголовки блока данных - std::unordered_map headers; - - for (size_t line_end = str.find("\r\n", str_cur); str_cur < headers_end; line_end = str.find("\r\n", str_cur) ) - { - size_t delimiter = str.find(':', str_cur); - - if (std::string::npos == delimiter || delimiter > line_end) - { - std::string header_name = str.substr(str_cur, line_end - str_cur); - Utils::trim(header_name); - headers.emplace(std::move(header_name), ""); - } - else - { - std::string header_name = str.substr(str_cur, delimiter - str_cur); - Utils::trim(header_name); - - ++delimiter; - - std::string header_value = str.substr(delimiter, line_end - delimiter); - Utils::trim(header_value); - - headers.emplace(std::move(header_name), std::move(header_value) ); - } - - // Перейти к следующему заголовку - str_cur = line_end + 2; - } - - // Перейти к данным - str_cur += 2; - - // Определить источник блока данных - auto it = headers.find("Content-Disposition"); - - // Если заголовок определён - if (headers.cend() != it) - { - // Разобрать значение заголовка данных на параметры - std::unordered_map header_params; - - const std::string &header_value = it->second; - - size_t delimiter = header_value.find(';'); - - std::string content_disposition(header_value.substr(0, delimiter) ); - Utils::trim(content_disposition); - - // Проверить соответствие указанного формата - if ("form-data" == content_disposition && std::string::npos != delimiter) - { - // Получить параметры блока данных - for (size_t str_param_cur = delimiter + 1, str_param_end; std::string::npos != str_param_cur; str_param_cur = str_param_end) - { - str_param_end = header_value.find(';', str_param_cur); - delimiter = header_value.find('=', str_param_cur); - - if (std::string::npos == delimiter || delimiter > str_param_end) - { - std::string param_name = (std::string::npos == str_param_end) ? header_value.substr(str_param_cur) : header_value.substr(str_param_cur, str_param_end - str_param_cur); - Utils::trim(param_name); - header_params.emplace(std::move(param_name), ""); - } - else - { - std::string param_name = header_value.substr(str_param_cur, delimiter - str_param_cur); - Utils::trim(param_name); - - ++delimiter; - - delimiter = header_value.find('"', delimiter); - - if (std::string::npos == delimiter) - { - str_param_end = header_value.find(';', str_param_cur); - - std::string param_value = (std::string::npos == str_param_end) ? header_value.substr(delimiter) : header_value.substr(delimiter, str_param_end - delimiter); - Utils::trim(param_value); - - header_params.emplace(std::move(param_name), std::move(param_value) ); - } - else - { - ++delimiter; - - str_param_cur = header_value.find('"', delimiter); - str_param_end = header_value.find(';', str_param_cur); - - std::string param_value = (std::string::npos == str_param_cur) ? header_value.substr(delimiter) : header_value.substr(delimiter, str_param_cur - delimiter); - - header_params.emplace(std::move(param_name), std::move(param_value) ); - } - } - - if (std::string::npos != str_param_end) - { - ++str_param_end; - } - } - - // Поиск имени блока данных - auto const it_name = header_params.find("name"); - - if (header_params.cend() != it_name) - { - // Если данные пришли из файла - auto const it_filename = header_params.find("filename"); - - if (header_params.cend() != it_filename) - { - // Найти тип файла - auto const it_filetype = headers.find("Content-Type"); - - if (headers.cend() != it_filetype) - { - // Сгенерировать уникальное имя - std::string tmp_name = System::getTempDir() + Utils::getUniqueName(); - - // Создать файл - std::ofstream file(tmp_name, std::ofstream::trunc | std::ofstream::binary); - - // Если файл был создан и готов для работы - if (file.is_open() ) - { - // Смещение данных в буфере в начало - // str.assign(str.cbegin() + str_cur, str.cend() ); - str.erase(str.begin(), str.begin() + str_cur); - - // Поиск конца блока данных - size_t delimiter = str.find(block_delimiter); - - // Пока конец блока данных не найден - while (std::string::npos == delimiter) - { - // Добавить данные к значению - file.write(str.data(), str.length() - data_end.length() ); - - // str.assign(str.cend() - data_end.length(), str.cend() ); - str.erase(str.begin(), str.end() - data_end.length() ); - - // Получить следующий кусок данных - if (false == append(sock, rp.timeout, buf, str, data_end, leftBytes, recv_len, recv_total_len) ) - { - return false; - } - - // Поиск конца блока данных - delimiter = str.find(block_delimiter); - } - - // Добавить последнюю часть данных к значению - file.write(str.data(), delimiter); - - // Добавить данные в список - rp.incoming_files.emplace(it_name->second, FileIncoming(std::move(tmp_name), it_filetype->second, file.tellp() ) ); - - file.close(); - - // Если найден конец данных - if (str.find(data_end, delimiter) == delimiter) - { - is_find_data_end = true; - } - - str_cur = delimiter + block_delimiter.length() + 2; - } - else // Файл не смог быть открыт/создан - { - is_block_valid = false; - } - } - else // Тип файла не определён - { - is_block_valid = false; - } - } - else // Если данные пришли из формы - { - std::string value; - - // Смещение данных в буфере в начало - // str.assign(str.cbegin() + str_cur, str.cend() ); - str.erase(str.begin(), str.begin() + str_cur); - - // Поиск конца блока данных - size_t delimiter = str.find(block_delimiter); - - // Пока конец блока данных не найден - while (std::string::npos == delimiter) - { - // Добавить данные к значению - value.append(str.cbegin(), str.cend() - data_end.length() ); - - // str.assign(str.cend() - data_end.length(), str.cend() ); - str.erase(str.begin(), str.end() - data_end.length() ); - - // Получить следующий кусок данных - if (false == append(sock, rp.timeout, buf, str, data_end, leftBytes, recv_len, recv_total_len) ) - { - return false; - } - - // Поиск конца блока данных - delimiter = str.find(block_delimiter); - } - - // Добавить последнюю часть данных к значению - value.append(str.cbegin(), str.cbegin() + delimiter); - - // Добавить данные в список - rp.incoming_data.emplace(it_name->second, std::move(value) ); - - // Если найден конец данных - if (str.find(data_end, delimiter) == delimiter) - { - is_find_data_end = true; - } - - str_cur = delimiter + block_delimiter.length() + 2; - } - } - else // Имя блока данных не определено - { - is_block_valid = false; - } - } - else // Формат не соответствует - { - is_block_valid = false; - } - } - else // Если источник не определён - { - is_block_valid = false; - } - } - - // Если данные некорректны - if (false == is_block_valid) - { - // то блок данных пропускаем (ищем следующий блок) - str_cur = str.find(block_delimiter, str_cur); - - while (std::string::npos == str_cur) - { - // str.assign(str.cend() - data_end.length(), str.cend() ); - str.erase(str.begin(), str.end() - data_end.length() ); - - // Получить следующий кусок данных - if (false == append(sock, rp.timeout, buf, str, data_end, leftBytes, recv_len, recv_total_len) ) - { - return false; - } - - str_cur = str.find(block_delimiter); - } - - // Если найден конец данных - if (str.find(data_end, str_cur) == str_cur) - { - is_find_data_end = true; - } - - str_cur += block_delimiter.length() + 2; - } - - str.erase(str.begin(), str.begin() + str_cur); - - str_cur = 0; - } - while (false == is_find_data_end); - - return true; - } -}; \ No newline at end of file diff --git a/src/DataVariantMultipartFormData.h b/src/DataVariantMultipartFormData.h deleted file mode 100644 index 0a72b68..0000000 --- a/src/DataVariantMultipartFormData.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "DataVariantAbstract.h" - -namespace HttpServer -{ - class DataVariantMultipartFormData: public DataVariantAbstract - { - public: - DataVariantMultipartFormData(); - - protected: - static bool append - ( - const SocketAdapter &sock, - const std::chrono::milliseconds &timeout, - std::vector &buf, - std::string &str_buf, - const std::string &data_end, - const size_t &leftBytes, - long &recv_len, - size_t &recv_total_len - ); - - public: - virtual bool parse - ( - const SocketAdapter &sock, - std::string &str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) override; - }; -}; \ No newline at end of file diff --git a/src/DataVariantTextPlain.cpp b/src/DataVariantTextPlain.cpp deleted file mode 100644 index 2d5a6b1..0000000 --- a/src/DataVariantTextPlain.cpp +++ /dev/null @@ -1,60 +0,0 @@ - -#include "DataVariantTextPlain.h" - -namespace HttpServer -{ - DataVariantTextPlain::DataVariantTextPlain() - { - data_variant_name = "text/plain"; - } - - bool DataVariantTextPlain::parse - ( - const SocketAdapter &sock, - std::string &str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) - { - if (str.empty() ) - { - return true; - } - - for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) - { - // Поиск следующего параметра - var_end = str.find('&', var_pos); - - // Поиск значения параметра - size_t delimiter = str.find('=', var_pos); - - if (delimiter >= var_end) - { - // Получить имя параметра - std::string var_name = str.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos); - - // Сохранить параметр с пустым значением - rp.incoming_data.emplace(std::move(var_name), ""); - } - else - { - // Получить имя параметра - std::string var_name = str.substr(var_pos, delimiter - var_pos); - - ++delimiter; - - // Получить значение параметра - std::string var_value = str.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos); - - // Сохранить параметр и значение - rp.incoming_data.emplace(std::move(var_name), std::move(var_value) ); - } - } - - str.clear(); - - return true; - } -}; \ No newline at end of file diff --git a/src/DataVariantTextPlain.h b/src/DataVariantTextPlain.h deleted file mode 100644 index d3fb2ac..0000000 --- a/src/DataVariantTextPlain.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "DataVariantAbstract.h" - -namespace HttpServer -{ - class DataVariantTextPlain: public DataVariantAbstract - { - public: - DataVariantTextPlain(); - - public: - virtual bool parse - ( - const SocketAdapter &sock, - std::string &str, - const size_t leftBytes, - std::unordered_map &contentParams, - struct request_parameters &rp - ) override; - }; -}; \ No newline at end of file diff --git a/src/FileIncoming.cpp b/src/FileIncoming.cpp deleted file mode 100644 index 1526735..0000000 --- a/src/FileIncoming.cpp +++ /dev/null @@ -1,36 +0,0 @@ - -#include "FileIncoming.h" - -#include - -namespace HttpServer -{ - FileIncoming::FileIncoming(const std::string &fileName, const std::string &fileType, const size_t fileSize) - : file_name(fileName), file_type(fileType), file_size(fileSize) - { - - } - - FileIncoming::FileIncoming(const FileIncoming &obj) - : file_name(obj.file_name), file_type(obj.file_type), file_size(obj.file_size) - { - - } - - FileIncoming::FileIncoming(FileIncoming &&obj) - : file_name(std::move(obj.file_name) ), file_type(std::move(obj.file_type) ), file_size(obj.file_size) - { - obj.file_size = 0; - } - - bool FileIncoming::isExists() const - { - std::ifstream file(file_name, std::ifstream::binary); - - const bool is_exists = file.good(); - - file.close(); - - return is_exists; - } -}; \ No newline at end of file diff --git a/src/FileIncoming.h b/src/FileIncoming.h deleted file mode 100644 index 6c6fa97..0000000 --- a/src/FileIncoming.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include - -namespace HttpServer -{ - class FileIncoming - { - protected: - std::string file_name; - std::string file_type; - size_t file_size; - - private: - FileIncoming() = delete; - - public: - FileIncoming(const std::string &fileName, const std::string &fileType, const size_t fileSize); - FileIncoming(const FileIncoming &obj); - FileIncoming(FileIncoming &&obj); - - ~FileIncoming() = default; - - inline std::string getName() const - { - return file_name; - } - - inline std::string getType() const - { - return file_type; - } - - inline size_t getSize() const - { - return file_size; - } - - bool isExists() const; - }; -}; \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index c7739e1..d3d341d 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,6 +1,6 @@ #include "Main.h" -#include "Server.h" +#include "server/Server.h" #include "SignalHandlers.h" #include @@ -47,4 +47,4 @@ int main(const int argc, const char *argv[]) } return exitcode; -} \ No newline at end of file +} diff --git a/src/RawData.h b/src/RawData.h deleted file mode 100644 index a493382..0000000 --- a/src/RawData.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -namespace Utils -{ - struct raw_pair - { - char *key; - char *value; - }; - - struct raw_fileinfo - { - char *key; - char *file_name; - char *file_type; - size_t file_size; - }; -}; \ No newline at end of file diff --git a/src/RequestParameters.cpp b/src/RequestParameters.cpp deleted file mode 100644 index d3e1c39..0000000 --- a/src/RequestParameters.cpp +++ /dev/null @@ -1,21 +0,0 @@ - -#include "RequestParameters.h" - -namespace HttpServer -{ - void request_parameters::clear() - { - incoming_headers.clear(); - incoming_params.clear(); - incoming_data.clear(); - incoming_files.clear(); - - outgoing_headers.clear(); - - method.clear(); - version.clear(); - uri_reference.clear(); - - timeout = std::chrono::milliseconds::zero(); - } -}; \ No newline at end of file diff --git a/src/RequestParameters.h b/src/RequestParameters.h deleted file mode 100644 index 0d3e209..0000000 --- a/src/RequestParameters.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "FileIncoming.h" - -#include -#include - -namespace HttpServer -{ - struct request_parameters - { - std::unordered_map incoming_headers; - std::unordered_multimap incoming_params; - std::unordered_multimap incoming_data; - std::unordered_multimap incoming_files; - - std::unordered_map outgoing_headers; - - std::string method; - std::string version; - std::string uri_reference; - - std::chrono::milliseconds timeout; - - size_t keep_alive_count; - int connection_params; - int app_exit_code; - - void clear(); - }; -}; \ No newline at end of file diff --git a/src/Server.cpp b/src/Server.cpp deleted file mode 100644 index 6c53dea..0000000 --- a/src/Server.cpp +++ /dev/null @@ -1,2036 +0,0 @@ - -#include "Server.h" -#include "Utils.h" -#include "System.h" - -#include "DataVariantFormUrlencoded.h" -#include "DataVariantMultipartFormData.h" -#include "DataVariantTextPlain.h" -#include "FileIncoming.h" -#include "ServerRequest.h" -#include "ServerResponse.h" -#include "ConfigParser.h" -#include "SocketAdapterDefault.h" -#include "SocketAdapterTls.h" -#include "GlobalMutex.h" -#include "SharedMemory.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace HttpServer -{ - std::string Server::getMimeTypeByFileName(const std::string &fileName) const - { - const size_t ext_pos = fileName.rfind('.'); - std::string file_ext = std::string::npos != ext_pos ? fileName.substr(ext_pos + 1) : ""; - - const std::locale loc; - Utils::toLower(file_ext, loc); - - auto const it_mime = this->mimes_types.find(file_ext); - - return this->mimes_types.cend() != it_mime ? it_mime->second : "application/octet-stream"; - } - - std::vector > Server::getRanges( - const std::string &rangeHeader, - const size_t posSymEqual, - const size_t fileSize, - std::string &resultRangeHeader, - size_t &contentLength - ) const - { - std::vector > ranges; - - contentLength = 0; - - size_t delimiter = posSymEqual; // rangeHeader.find('='); - - const std::string range_unit_name(rangeHeader.cbegin(), rangeHeader.cbegin() + delimiter); - - static const std::unordered_map ranges_units { - {"bytes", 1} - }; - - auto const it_unit = ranges_units.find(range_unit_name); - - if (ranges_units.cend() == it_unit) - { - return ranges; - } - - const size_t range_unit = it_unit->second; - - for (size_t str_pos; std::string::npos != delimiter; ) - { - str_pos = delimiter + 1; - - delimiter = rangeHeader.find(',', str_pos); - - const size_t range_pos = rangeHeader.find('-', str_pos); - - if (range_pos < delimiter) - { - const std::string range_begin_str(rangeHeader.cbegin() + str_pos, rangeHeader.cbegin() + range_pos); - const std::string range_end_str(rangeHeader.cbegin() + range_pos + 1, std::string::npos == delimiter ? rangeHeader.cend() : rangeHeader.cbegin() + delimiter); - - if (false == range_begin_str.empty() ) - { - const size_t range_begin = std::strtoull(range_begin_str.c_str(), nullptr, 10) * range_unit; - - if (range_begin < fileSize) - { - if (false == range_end_str.empty() ) - { - size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit; - - if (range_end >= range_begin) - { - if (range_end > fileSize) - { - range_end = fileSize; - } - - const size_t length = range_end - range_begin + 1; - - contentLength += length; - - resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ','; - - ranges.emplace_back(std::tuple {range_begin, length}); - } - } - else // if range_end_str empty - { - const size_t length = fileSize - range_begin; - - contentLength += length; - - resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(fileSize - 1) + ','; - - ranges.emplace_back(std::tuple {range_begin, length}); - } - } - } - else if (false == range_end_str.empty() ) // if range_begin_str empty - { - size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit; - - const size_t length = range_end < fileSize ? fileSize - range_end : fileSize; - - const size_t range_begin = fileSize - length; - - range_end = fileSize - range_begin - 1; - - contentLength += length; - - resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ','; - - ranges.emplace_back(std::tuple {range_begin, length}); - } - } - } - - if (false == ranges.empty() ) - { - resultRangeHeader.back() = '/'; - - resultRangeHeader = "bytes " + resultRangeHeader + std::to_string(fileSize); - } - - return ranges; - } - - int Server::transferFilePart - ( - const SocketAdapter &clientSocket, - const std::chrono::milliseconds &timeout, - const std::string &fileName, - const time_t fileTime, - const size_t fileSize, - const std::string &rangeHeader, - const std::string &connectionHeader, - const std::string &dateHeader, - const bool headersOnly - ) const - { - const size_t pos_sym_equal = rangeHeader.find('='); - - if (std::string::npos == pos_sym_equal) - { - // HTTP 400 - std::string headers("HTTP/1.1 400 Bad Request\r\n"); - headers += connectionHeader + dateHeader + "\r\n"; - - clientSocket.nonblock_send(headers, timeout); - - return 0; - } - - std::string content_range_header; - - size_t content_length; - - const std::vector > ranges = this->getRanges(rangeHeader, pos_sym_equal, fileSize, content_range_header, content_length); - - if (0 == content_length) - { - // HTTP 416 - std::string headers("HTTP/1.1 416 Requested Range Not Satisfiable\r\n"); - headers += connectionHeader + dateHeader + "\r\n"; - - clientSocket.nonblock_send(headers, timeout); - - return 0; - } - - // Ranges transfer - std::ifstream file(fileName, std::ifstream::binary); - - if ( ! file) - { - file.close(); - - // HTTP 500 - std::string headers("HTTP/1.1 500 Internal Server Error\r\n"); - headers += connectionHeader + dateHeader + "\r\n"; - - clientSocket.nonblock_send(headers, timeout); - - return 0; - } - - const std::string file_mime_type = this->getMimeTypeByFileName(fileName); - - std::string headers("HTTP/1.1 206 Partial Content\r\n"); - headers += "Content-Type: " + file_mime_type + "\r\n" - + "Content-Length: " + std::to_string(content_length) + "\r\n" - + "Accept-Ranges: bytes\r\n" - + "Content-Range: " + content_range_header + "\r\n" - + "Last-Modified: " + Utils::getDatetimeAsString(fileTime, true) + "\r\n" - + connectionHeader + dateHeader + "\r\n"; - - // Отправить заголовки - if (clientSocket.nonblock_send(headers, timeout) < 0) - { - file.close(); - - return 0; - } - - if (false == headersOnly) - { - size_t position, length; - - for (auto const &range : ranges) - { - std::tie(position, length) = range; - - std::vector buf(length < 512 * 1024 ? length : 512 * 1024); - - file.seekg(position, file.beg); - - size_t send_size_left = length; - - long send_size; - - do - { - if (send_size_left < 512 * 1024) - { - buf.resize(send_size_left); - } - - file.read(buf.data(), buf.size() ); - send_size = clientSocket.nonblock_send(buf, file.gcount(), timeout); - - send_size_left -= send_size; - } - while (false == file.eof() && false == file.fail() && send_size > 0 && send_size_left); - } - } - - file.close(); - - return 1; - } - - /** - * Передача файла (или его части) - */ - int Server::transferFile - ( - const SocketAdapter &clientSocket, - const std::string &fileName, - const std::string &connectionHeader, - const bool headersOnly, - struct request_parameters &rp - ) const - { - // Get current time in GMT - const std::string date_header = "Date: " + Utils::getDatetimeAsString() + "\r\n"; - - time_t file_time; - size_t file_size; - - // Получить размер файла и дату последнего изменения - if (false == System::getFileSizeAndTimeGmt(fileName, &file_size, &file_time) ) - { - // HTTP 404 - std::string headers("HTTP/1.1 404 Not Found\r\n"); - headers += connectionHeader + date_header + "\r\n"; - - clientSocket.nonblock_send(headers, rp.timeout); - - return 0; - } - - // Check for If-Modified header - auto const it_modified = rp.incoming_headers.find("If-Modified-Since"); - - // Если найден заголовок проверки изменения файла (проверить, изменялся ли файл) - if (rp.incoming_headers.cend() != it_modified) - { - const time_t time_in_request = Utils::stringTimeToTimestamp(it_modified->second); - - if (file_time == time_in_request) - { - // HTTP 304 - std::string headers("HTTP/1.1 304 Not Modified\r\n"); - headers += connectionHeader + date_header + "\r\n"; - - clientSocket.nonblock_send(headers, rp.timeout); - - return 0; - } - } - - auto const it_range = rp.incoming_headers.find("Range"); - - // Range transfer - if (rp.incoming_headers.cend() != it_range) - { - return this->transferFilePart(clientSocket, rp.timeout, fileName, file_time, file_size, it_range->second, connectionHeader, date_header, headersOnly); - } - - // File transfer - std::ifstream file(fileName, std::ifstream::binary); - - if ( ! file) - { - file.close(); - - // HTTP 500 - std::string headers("HTTP/1.1 500 Internal Server Error\r\n"); - headers += connectionHeader + date_header + "\r\n"; - - clientSocket.nonblock_send(headers, rp.timeout); - - return 0; - } - - const std::string file_mime_type = this->getMimeTypeByFileName(fileName); - - std::string headers("HTTP/1.1 200 OK\r\n"); - headers += "Content-Type: " + file_mime_type + "\r\n" - + "Content-Length: " + std::to_string(file_size) + "\r\n" - + "Accept-Ranges: bytes\r\n" - + "Last-Modified: " + Utils::getDatetimeAsString(file_time, true) + "\r\n" - + connectionHeader + date_header + "\r\n"; - - // Отправить заголовки - if (clientSocket.nonblock_send(headers, rp.timeout) < 0) - { - file.close(); - - return 0; - } - - // Отправить файл - if (false == headersOnly && file_size) - { - std::vector buf(file_size < 512 * 1024 ? file_size : 512 * 1024); - - long send_size; - - do - { - file.read(reinterpret_cast(buf.data() ), buf.size() ); - send_size = clientSocket.nonblock_send(buf, file.gcount(), rp.timeout); - } - while (false == file.eof() && false == file.fail() && send_size > 0); - } - - file.close(); - - return 1; - } - - /** - * Парсинг переданных параметров (URI) - */ - bool Server::parseIncomingVars(std::unordered_multimap ¶ms, const std::string &uriParams) - { - if (uriParams.length() ) - { - for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) - { - // Поиск следующего параметра - var_end = uriParams.find('&', var_pos); - - // Поиск значения параметра - size_t delimiter = uriParams.find('=', var_pos); - - if (delimiter >= var_end) - { - // Получить имя параметра - std::string var_name = Utils::urlDecode(uriParams.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) ); - - // Сохранить параметр с пустым значением - params.emplace(std::move(var_name), ""); - } - else - { - // Получить имя параметра - std::string var_name = Utils::urlDecode(uriParams.substr(var_pos, delimiter - var_pos) ); - - ++delimiter; - - // Получить значение параметра - std::string var_value = Utils::urlDecode(uriParams.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) ); - - // Сохранить параметр и значение - params.emplace(std::move(var_name), std::move(var_value) ); - } - } - - return true; - } - - return false; - } - - void Server::sendStatus(const SocketAdapter &clientSocket, const std::chrono::milliseconds &timeout, const size_t statusCode) - { - static const std::unordered_map statuses { - {400, "Bad Request"}, - {404, "Not Found"}, - {413, "Request Entity Too Large"} - }; - - auto const it = statuses.find(statusCode); - - if (statuses.cend() != it) - { - const std::string &status = it->second; - - std::string headers("HTTP/1.1 " + std::to_string(statusCode) + ' ' + status + "\r\n\r\n"); - - clientSocket.nonblock_send(headers, timeout); - } - } - - /** - * Метод для обработки запроса - */ - int Server::threadRequestProc(SocketAdapter &clientSocket, const struct sockaddr_in &clientAddr) const - { - struct request_parameters rp; - - rp.keep_alive_count = 100; - - const size_t buf_len = 4096; - std::vector buf(buf_len); - - std::string str_buf; - - do - { - rp.app_exit_code = EXIT_FAILURE; - - // Подготовить параметры для получения данных - rp.timeout = std::chrono::milliseconds(5000); - - if (false == getRequest(clientSocket, buf, str_buf, rp) ) - { - break; - } - - if (int error_code = getRequestHeaders(str_buf, rp) ) - { - this->sendStatus(clientSocket, rp.timeout, error_code); - - break; - } - - const ServerApplicationSettings *app_sets = getApplicationSettings(rp); - - // Если приложение не найдено - if (nullptr == app_sets) - { - // HTTP 404 Not Found - this->sendStatus(clientSocket, rp.timeout, 404); - - break; - } - - if (int error_code = getRequestData(clientSocket, str_buf, *app_sets, rp) ) - { - this->sendStatus(clientSocket, rp.timeout, error_code); - - break; - } - - runApplication(clientSocket, *app_sets, rp); - - for (auto const &it : rp.incoming_files) - { - remove(it.second.getName().c_str() ); - } - - if (EXIT_SUCCESS == rp.app_exit_code) - { - this->getConnectionParams(rp); - - this->xSendfile(clientSocket, rp); - } - else - { - rp.connection_params = CONNECTION_CLOSED; - } - - rp.clear(); - } - while (isConnectionKeepAlive(rp) ); - - if (false == isConnectionUpgrade(rp) ) - { - clientSocket.close(); - } - - return rp.app_exit_code; - } - - bool Server::getRequest(const SocketAdapter &clientSocket, std::vector &buf, std::string &str_buf, struct request_parameters &rp) - { - // Получить данные запроса от клиента - const long recv_size = clientSocket.nonblock_recv(buf, rp.timeout); - - if (recv_size < 0 && str_buf.empty() ) - { - return false; - } - - if (recv_size > 0) // Если данные были получены - { - str_buf.append(buf.cbegin(), buf.cbegin() + recv_size); - } - - return true; - } - - int Server::getRequestHeaders(std::string &str_buf, struct request_parameters &rp) const - { - // Если запрос пустой - if (str_buf.empty() ) - { - // HTTP 400 Bad Request - return 400; - } - - // Поиск конца заголовков (пустая строка) - size_t headers_end = str_buf.find("\r\n\r\n"); - - // Если найден конец заголовков - if (std::string::npos == headers_end) - { - // HTTP 400 Bad Request - return 400; - } - - headers_end += 2; - - size_t str_cur = 0; - // Поиск конца первого заголовка - size_t str_end = str_buf.find("\r\n"); - - // Если не найден конец заголовка - if (std::string::npos == str_end) - { - // HTTP 400 Bad Request - return 400; - } - - // Установка конца строки (для поиска) - str_buf[str_end] = '\0'; - - // Разделить метод запроса и параметры запроса - size_t delimiter = str_buf.find(' ', str_cur); - - // Получить метод запроса (GET, POST, PUT, DELETE, ...) - rp.method = str_buf.substr(str_cur, delimiter - str_cur); - // Сохранить метод и параметры запроса - rp.incoming_headers[rp.method] = str_buf.substr(delimiter + 1, str_end - delimiter - 1); - - delimiter += 1; - // Найти окончание URI - size_t uri_end = str_buf.find(' ', delimiter); - - // Если окончание не найдено - if (std::string::npos == uri_end) - { - uri_end = str_end; - // то версия протокола HTTP - 0.9 - rp.version = "0.9"; - } - else // Если окончание найдено - { - str_buf[uri_end] = '\0'; - const size_t ver_beg = uri_end + 6; // Пропустить "HTTP/" - - if (ver_beg < str_end) - { - // Получить версию протокола HTTP - rp.version = str_buf.substr(ver_beg, str_end - ver_beg); - } - } - - // Поиск именованных параметров запросов (переменных ?) - const size_t params_pos = str_buf.find('?', delimiter); - - // Сохранить полную ссылку URI (без параметров) - rp.uri_reference = (std::string::npos == params_pos) ? str_buf.substr(delimiter) : str_buf.substr(delimiter, params_pos - delimiter); - - if (std::string::npos != params_pos) - { - // Извлекаем параметры запроса из URI - if (false == parseIncomingVars(rp.incoming_params, str_buf.substr(params_pos + 1, uri_end) ) ) - { - // HTTP 400 Bad Request - return 400; - } - } - - // Переход к обработке следующего заголовка - str_cur = str_end + 2; - // Поиск конца заголовка - str_end = str_buf.find("\r\n", str_cur); - // Установка конца заголовка - str_buf[str_end] = '\0'; - - // Цикл извлечения заголовков запроса - for (; str_cur != headers_end; str_end = str_buf.find("\r\n", str_cur), str_buf[str_end] = '\0') - { - // Поиск разделителя названия заголовка и его значения - delimiter = str_buf.find(':', str_cur); - - // Если разделитель найден в текущей строке - if (delimiter < str_end) - { - std::string header_name = str_buf.substr(str_cur, delimiter - str_cur); - std::string header_value = str_buf.substr(delimiter + 1, str_end - delimiter - 1); - - // Удалить лишние пробелы в начале и в конце строки - Utils::trim(header_value); - - // Сохранить заголовок и его значение - rp.incoming_headers.emplace(std::move(header_name), std::move(header_value) ); - } - - // Перейти к следующей строке - str_cur = str_end + 2; - } - - str_buf.erase(str_buf.begin(), str_buf.begin() + headers_end + 2); - - return 0; - } - - void Server::runApplication(const SocketAdapter &clientSocket, const ServerApplicationSettings &appSets, struct request_parameters &rp) - { - Utils::raw_pair *raw_pair_params = nullptr; - Utils::raw_pair *raw_pair_headers = nullptr; - Utils::raw_pair *raw_pair_data = nullptr; - Utils::raw_fileinfo *raw_fileinfo_files = nullptr; - - Utils::stlToRawPairs(&raw_pair_params, rp.incoming_params); - Utils::stlToRawPairs(&raw_pair_headers, rp.incoming_headers); - Utils::stlToRawPairs(&raw_pair_data, rp.incoming_data); - Utils::filesIncomingToRawFilesInfo(&raw_fileinfo_files, rp.incoming_files); - - server_request request { - clientSocket.get_handle(), - clientSocket.get_tls_session(), - rp.method.c_str(), - rp.uri_reference.c_str(), - appSets.root_dir.c_str(), - rp.incoming_params.size(), - raw_pair_params, - rp.incoming_headers.size(), - raw_pair_headers, - rp.incoming_data.size(), - raw_pair_data, - rp.incoming_files.size(), - raw_fileinfo_files - }; - - server_response response { - clientSocket.get_handle(), 0, nullptr - }; - - try - { - // Launch application - rp.app_exit_code = appSets.application_call(&request, &response); - } - catch (...) - { - rp.app_exit_code = EXIT_FAILURE; - } - - if (EXIT_SUCCESS == rp.app_exit_code) - { - Utils::rawPairsToStl(rp.outgoing_headers, response.headers, response.headers_count); - } - - // Очистить заголовки сформированные приложением - try - { - appSets.application_clear(response.headers, response.headers_count); - } - catch (...) {} - - Utils::destroyRawPairs(raw_pair_params, rp.incoming_params.size() ); - Utils::destroyRawPairs(raw_pair_headers, rp.incoming_headers.size() ); - Utils::destroyRawPairs(raw_pair_data, rp.incoming_data.size() ); - Utils::destroyRawFilesInfo(raw_fileinfo_files, rp.incoming_files.size() ); - } - - int Server::getRequestData(const SocketAdapter &clientSocket, std::string &str_buf, const ServerApplicationSettings &appSets, struct request_parameters &rp) const - { - // Определить вариант данных запроса (заодно проверить, есть ли данные) - auto const it = rp.incoming_headers.find("Content-Type"); - - if (rp.incoming_headers.cend() == it) - { - return 0; - } - - // Параметры - std::unordered_map content_params; - - // Получить значение заголовка - const std::string &header_value = it->second; - - // Определить, содержит ли тип данных запроса дополнительные параметры - size_t delimiter = header_value.find(';'); - - std::string data_variant_name; // Название варианта данных запроса - - // Если есть дополнительные параметры - извлекаем их - if (std::string::npos != delimiter) - { - data_variant_name = header_value.substr(0, delimiter); - Utils::trim(data_variant_name); - - for (size_t str_param_cur = delimiter + 1, str_param_end = 0; std::string::npos != str_param_end; str_param_cur = str_param_end + 1) - { - str_param_end = header_value.find(';', str_param_cur); - delimiter = header_value.find('=', str_param_cur); - - if (delimiter >= str_param_end) - { - std::string param_name = header_value.substr(str_param_cur, std::string::npos != str_param_end ? str_param_end - str_param_cur : std::string::npos); - Utils::trim(param_name); - content_params.emplace(std::move(param_name), ""); - } - else - { - std::string param_name = header_value.substr(str_param_cur, delimiter - str_param_cur); - Utils::trim(param_name); - - ++delimiter; - - std::string param_value = header_value.substr(delimiter, std::string::npos != str_param_end ? str_param_end - delimiter : std::string::npos); - Utils::trim(param_value); - - content_params.emplace(std::move(param_name), std::move(param_value) ); - } - } - } - else - { - data_variant_name = header_value; - } - - // Поиск варианта данных по имени типа - auto variant = this->variants.find(data_variant_name); - - // Если сервер не поддерживает формат полученных данных - if (this->variants.cend() == variant) - { - // HTTP 400 Bad Request - return 400; - } - - DataVariantAbstract *data_variant = variant->second; - - // Получить длину запроса в байтах - size_t data_length = 0; - - auto const it_len = rp.incoming_headers.find("Content-Length"); - - if (rp.incoming_headers.cend() != it_len) - { - data_length = std::strtoull(it_len->second.c_str(), nullptr, 10); - } - - // Если размер запроса превышает лимит (если лимит был установлен) - if (data_length > appSets.request_max_size && 0 != appSets.request_max_size) - { - // HTTP 413 Request Entity Too Large - return 413; - } - - // Сколько осталось получить данных - size_t left_bytes = 0; - - std::string data_buf; - - if (data_length >= str_buf.length() ) - { - left_bytes = data_length - str_buf.length(); - - data_buf.swap(str_buf); - } - else - { - data_buf.assign(str_buf.cbegin(), str_buf.cbegin() + data_length); - str_buf.erase(str_buf.begin(), str_buf.begin() + data_length); - } - - // Разобрать данные на составляющие - if (false == data_variant->parse(clientSocket, data_buf, left_bytes, content_params, rp) ) - { - for (auto const &it : rp.incoming_files) - { - remove(it.second.getName().c_str() ); - } - - // HTTP 400 Bad Request - return 400; - } - - if (false == data_buf.empty() ) - { - str_buf.swap(data_buf); - } - - return 0; - } - - const ServerApplicationSettings *Server::getApplicationSettings(const struct request_parameters &rp) const - { - // Получить доменное имя (или адрес) назначения запроса - auto const it_host = rp.incoming_headers.find("Host"); - - // Если имя задано - продолжить обработку запроса - if (rp.incoming_headers.cend() != it_host) - { - const std::string &host_header = it_host->second; - - // Поиск разделителя, за которым помещается номер порта, если указан - const size_t delimiter = host_header.find(':'); - - // Получить имя (или адрес) - const std::string host = host_header.substr(0, delimiter); - - // Получить номер порта - const int port = (std::string::npos != delimiter) ? std::strtol(host_header.substr(delimiter + 1).c_str(), nullptr, 10) : 80; - - // Поиск настроек приложения по имени - const ServerApplicationSettings *app_sets = this->apps_tree.find(host); - - // Если приложение найдено - if (app_sets && (app_sets->ports.cend() != app_sets->ports.find(port) || app_sets->tls_ports.cend() != app_sets->tls_ports.find(port) ) ) - { - return app_sets; - } - } - - return nullptr; - } - - void Server::xSendfile(const SocketAdapter &clientSocket, struct request_parameters &rp) const - { - auto const it_x_sendfile = rp.outgoing_headers.find("X-Sendfile"); - - if (rp.outgoing_headers.cend() != it_x_sendfile) - { - const std::string connection_header = isConnectionKeepAlive(rp) ? "Connection: Keep-Alive\r\nKeep-Alive: timeout=5; max=" + std::to_string(rp.keep_alive_count) + "\r\n" : "Connection: Close\r\n"; - - const bool headers_only = ("head" == rp.method); - - this->transferFile(clientSocket, it_x_sendfile->second, connection_header, headers_only, rp); - } - } - - void Server::getConnectionParams(struct request_parameters &rp) - { - rp.connection_params = CONNECTION_CLOSED; - - auto const it_in_connection = rp.incoming_headers.find("Connection"); - auto const it_out_connection = rp.outgoing_headers.find("Connection"); - - if (rp.incoming_headers.cend() != it_in_connection && rp.outgoing_headers.cend() != it_out_connection) - { - const std::locale loc; - - std::string connection_in = it_in_connection->second; - Utils::toLower(connection_in, loc); - - std::string connection_out = it_out_connection->second; - Utils::toLower(connection_out, loc); - - auto const incoming_params = Utils::explode(connection_in, ','); - - auto const it = std::find(incoming_params.cbegin(), incoming_params.cend(), connection_out); - - if (incoming_params.cend() != it) - { - const std::string &inc = *it; - - if ("keep-alive" == inc) - { - --rp.keep_alive_count; - - if (0 < rp.keep_alive_count) - { - rp.connection_params |= CONNECTION_KEEP_ALIVE; - } - } - else if ("upgrade" == inc) - { - rp.connection_params |= CONNECTION_UPGRADE; - } - } - } - } - - bool Server::isConnectionKeepAlive(const struct request_parameters &rp) - { - return (rp.connection_params & CONNECTION_KEEP_ALIVE) == CONNECTION_KEEP_ALIVE; - } - - bool Server::isConnectionUpgrade(const struct request_parameters &rp) - { - return (rp.connection_params & CONNECTION_UPGRADE) == CONNECTION_UPGRADE; - } - - /** - * Метод для обработки запросов (запускается в отдельном потоке) - * извлекает сокет клиенты из очереди и передаёт его на обслуживание - */ - void Server::threadRequestCycle(std::queue > &sockets, Event &eventThreadCycle) const - { - while (true) - { - Socket sock; - struct sockaddr_in addr; - - eventThreadCycle.wait(); - - if (false == this->process_flag) - { - break; - } - - this->sockets_queue_mtx.lock(); - - if (sockets.size() ) - { - std::tie(sock, addr) = sockets.front(); - - sockets.pop(); - } - - if (sockets.empty() ) - { - eventThreadCycle.reset(); - - this->eventNotFullQueue->notify(); - } - - this->sockets_queue_mtx.unlock(); - - if (sock.is_open() ) - { - ++this->threads_working_count; - - struct ::sockaddr_in sock_addr; - ::socklen_t sock_addr_len = sizeof(sock_addr); - - ::getsockname(sock.get_handle(), reinterpret_cast(&sock_addr), &sock_addr_len); - - const int port = ntohs(sock_addr.sin_port); - - auto const it = this->tls_data.find(port); - - if (this->tls_data.cend() != it) // if TLS connection - { - const std::tuple &data = it->second; - - SocketAdapterTls socket_adapter( - sock, - std::get(data), - std::get(data) - ); - - if (socket_adapter.handshake() ) - { - this->threadRequestProc(socket_adapter, addr); - } - } - else - { - SocketAdapterDefault socket_adapter(sock); - - this->threadRequestProc(socket_adapter, addr); - } - - --this->threads_working_count; - } - } - } - - /** - * Цикл управления количеством рабочих потоков - */ - int Server::cycleQueue(std::queue > &sockets) - { - auto const it_option = this->settings.find("threads_max_count"); - - size_t threads_max_count = 0; - - if (this->settings.cend() != it_option) - { - const std::string &option = it_option->second; - - threads_max_count = std::strtoull(option.c_str(), nullptr, 10); - } - - if (0 == threads_max_count) - { - threads_max_count = std::thread::hardware_concurrency(); - - if (0 == threads_max_count) - { - threads_max_count = 1; - } - - threads_max_count *= 2; - } - - this->threads_working_count = 0; - - Event eventThreadCycle(false, true); - - std::function > &, Event &)> serverThreadRequestCycle = std::mem_fn(&Server::threadRequestCycle); - - std::vector active_threads; - active_threads.reserve(threads_max_count); - - // For update applications modules - do - { - if (this->eventUpdateModule->notifed() ) - { - updateModules(); - } - - // Cycle creation threads applications requests - do - { - while (this->threads_working_count == active_threads.size() && active_threads.size() < threads_max_count && sockets.empty() == false) - { - active_threads.emplace_back(serverThreadRequestCycle, this, std::ref(sockets), std::ref(eventThreadCycle) ); - } - - size_t notify_count = active_threads.size() - this->threads_working_count; - - if (notify_count > sockets.size() ) - { - notify_count = sockets.size(); - } - - eventThreadCycle.notify(notify_count); - - this->eventProcessQueue->wait(); - } - while (this->process_flag); - - // Data clear - - eventThreadCycle.notify(); - - if (false == active_threads.empty() ) - { - // Join threads (wait completion) - for (auto &th : active_threads) - { - th.join(); - } - - active_threads.clear(); - } - - this->eventNotFullQueue->notify(); - } - while (this->eventUpdateModule->notifed() ); - - if (false == this->server_sockets.empty() ) - { - for (Socket &s : this->server_sockets) - { - s.close(); - } - - this->server_sockets.clear(); - } - - return 0; - } - - Server::Server(): eventNotFullQueue(nullptr), eventProcessQueue(nullptr), eventUpdateModule(nullptr) - { - - } - - void Server::addDataVariant(DataVariantAbstract *dataVariant) - { - this->variants.emplace(dataVariant->getName(), dataVariant); - } - - bool Server::updateModule(Module &module, std::unordered_set &applications, const size_t moduleIndex) - { - std::unordered_set same; - - for (auto &app : applications) - { - if (app->module_index == moduleIndex) - { - same.emplace(app); - - try - { - if (app->application_final) - { - app->application_final(); - } - } - catch (std::exception &exc) - { - std::cout << "Warning: the error of the application's finalize '" << app->server_module << "':" << exc.what() << std::endl; - } - - app->application_call = std::function(); - app->application_clear = std::function(); - app->application_init = std::function(); - app->application_final = std::function(); - } - } - - module.close(); - - auto const app = *(same.cbegin() ); - - const std::string &module_name = app->server_module; - - #ifdef WIN32 - std::ifstream src(app->server_module_update, std::ifstream::binary); - - if ( ! src) - { - std::cout << "Error: the file '" << app->server_module_update << "' cannot be open;" << std::endl; - return false; - } - - std::ofstream dst(module_name, std::ofstream::binary | std::ofstream::trunc); - - if ( ! dst) - { - std::cout << "Error: the file '" << module_name << "' cannot be open;" << std::endl; - return false; - } - - // Copy (rewrite) file - dst << src.rdbuf(); - - src.close(); - dst.close(); - - // Open updated module - module.open(module_name); - - #elif POSIX - // HACK: for posix system — load new version shared library - - const size_t dir_pos = module_name.rfind('/'); - const size_t ext_pos = module_name.rfind('.'); - - std::string module_name_temp; - - if (std::string::npos != ext_pos && (std::string::npos == dir_pos || dir_pos < ext_pos) ) - { - module_name_temp = module_name.substr(0, ext_pos) + '-' + Utils::getUniqueName() + module_name.substr(ext_pos); - } - else - { - module_name_temp = module_name + '-' + Utils::getUniqueName(); - } - - std::ifstream src(app->server_module_update, std::ifstream::binary); - - if ( ! src) - { - std::cout << "Error: the file '" << app->server_module_update << "' cannot be open;" << std::endl; - return false; - } - - std::ofstream dst(module_name_temp, std::ofstream::binary | std::ofstream::trunc); - - if ( ! dst) - { - std::cout << "Error: the file '" << module_name << "' cannot be open;" << std::endl; - return false; - } - - // Copy (rewrite) file - dst << src.rdbuf(); - - src.close(); - dst.close(); - - // Open updated module - module.open(module_name_temp); - - if (0 != remove(module_name.c_str() ) ) - { - std::cout << "Error: the file '" << module_name << "' could not be removed;" << std::endl; - return false; - } - - if (0 != rename(module_name_temp.c_str(), module_name.c_str() ) ) - { - std::cout << "Error: the file '" << module_name_temp << "' could not be renamed;" << std::endl; - return false; - } - #else - #error "Undefine platform" - #endif - - if (false == module.is_open() ) - { - std::cout << "Error: application's module '" << module_name << "' cloud not be opened;" << std::endl; - return false; - } - - void *(*addr)(void *) = nullptr; - - if (false == module.find("application_call", &addr) ) - { - std::cout << "Error: the function 'application_call' was not found in the module '" << module_name << "';" << std::endl; - return false; - } - - std::function app_call = reinterpret_cast(addr); - - if ( ! app_call) - { - std::cout << "Error: invalid function 'application_call' is in the module '" << module_name << "';" << std::endl; - return false; - } - - if (false == module.find("application_clear", &addr) ) - { - std::cout << "Error: the function 'application_clear' was not found in the module '" << module_name << "';" << std::endl; - return false; - } - - std::function app_clear = reinterpret_cast(addr); - - std::function app_init = std::function(); - - if (module.find("application_init", &addr) ) - { - app_init = reinterpret_cast(addr); - } - - std::function app_final = std::function(); - - if (module.find("application_final", &addr) ) - { - app_final = reinterpret_cast(addr); - } - - for (auto &app : same) - { - app->application_call = app_call; - app->application_clear = app_clear; - app->application_init = app_init; - app->application_final = app_final; - - try - { - if (app->application_init) - { - app->application_init(); - } - } - catch (std::exception &exc) - { - std::cout << "Warning: the error of the application's initializing '" << module_name << "':" << exc.what() << std::endl; - } - } - - return true; - } - - void Server::updateModules() - { - // Applications settings list - std::unordered_set applications; - // Get full applications settings list - this->apps_tree.collectApplicationSettings(applications); - - std::unordered_set updated; - - for (auto const &app : applications) - { - const size_t module_index = app->module_index; - - // If module is not updated (not checked) - if (updated.cend() == updated.find(module_index) ) - { - if (false == app->server_module_update.empty() && app->server_module_update != app->server_module) - { - size_t module_size_new = 0; - time_t module_time_new = 0; - - if (System::getFileSizeAndTimeGmt(app->server_module_update, &module_size_new, &module_time_new) ) - { - size_t module_size_cur = 0; - time_t module_time_cur = 0; - - Module &module = this->modules[module_index]; - - if (System::getFileSizeAndTimeGmt(app->server_module, &module_size_cur, &module_time_cur) ) - { - if (module_size_cur != module_size_new || module_time_cur < module_time_new) - { - this->updateModule(module, applications, module_index); - } - } - } - } - - updated.emplace(module_index); - } - } - - std::cout << "Notice: applications' modules have been updated;" << std::endl; - - this->process_flag = true; - this->eventUpdateModule->reset(); - } - - bool Server::init() - { - ConfigParser conf_parser; - - if (Socket::Startup() && conf_parser.loadConfig("main.conf", this->settings, this->mimes_types, this->modules, this->apps_tree) ) - { - this->addDataVariant(new DataVariantFormUrlencoded() ); - this->addDataVariant(new DataVariantMultipartFormData() ); - this->addDataVariant(new DataVariantTextPlain() ); - - ::gnutls_global_init(); - - return true; - } - - return false; - } - - void Server::clear() - { - Socket::Cleanup(); - - if (this->eventNotFullQueue) - { - delete this->eventNotFullQueue; - this->eventNotFullQueue = nullptr; - } - - if (this->eventProcessQueue) - { - delete this->eventProcessQueue; - this->eventProcessQueue = nullptr; - } - - if (this->eventUpdateModule) - { - delete this->eventUpdateModule; - this->eventUpdateModule = nullptr; - } - - if (false == this->variants.empty() ) - { - for (auto &variant : this->variants) - { - delete variant.second; - } - - this->variants.clear(); - } - - if (false == this->tls_data.empty() ) - { - for (auto &pair : this->tls_data) - { - std::tuple &data = pair.second; - - ::gnutls_certificate_free_credentials(std::get<0>(data) ); - ::gnutls_priority_deinit(std::get<1>(data) ); - } - } - - if (false == this->apps_tree.empty() ) - { - std::unordered_set applications; - this->apps_tree.collectApplicationSettings(applications); - - for (auto &app : applications) - { - try - { - if (app->application_final) - { - app->application_final(); - } - } - catch (std::exception &exc) - { - std::cout << "Warning: the error of the application's finalize '" << app->server_module << "':" << exc.what() << std::endl; - } - - delete app; - } - - applications.clear(); - this->apps_tree.clear(); - } - - if (false == this->modules.empty() ) - { - for (auto &module : this->modules) - { - module.close(); - } - - this->modules.empty(); - } - - if (false == this->settings.empty() ) - { - this->settings.clear(); - } - - ::gnutls_global_deinit(); - } - - bool Server::tlsInit(const ServerApplicationSettings &app, std::tuple &data) - { - ::gnutls_certificate_credentials_t x509_cred; - - int ret = ::gnutls_certificate_allocate_credentials(&x509_cred); - - if (ret < 0) - { - std::cout << "Error [tls]: certificate credentials has not been allocated;" << std::endl; - - return false; - } - - if (false == app.chain_file.empty() ) - { - ret = ::gnutls_certificate_set_x509_trust_file(x509_cred, app.chain_file.c_str(), GNUTLS_X509_FMT_PEM); - - if (ret < 0) - { - std::cout << "Warning [tls]: (CA) chain file has not been accepted;" << std::endl; - } - } - - if (false == app.crl_file.empty() ) - { - ret = ::gnutls_certificate_set_x509_crl_file(x509_cred, app.crl_file.c_str(), GNUTLS_X509_FMT_PEM); - - if (ret < 0) - { - std::cout << "Warning [tls]: (CLR) clr file has not been accepted;" << std::endl; - } - } - - ret = ::gnutls_certificate_set_x509_key_file(x509_cred, app.cert_file.c_str(), app.key_file.c_str(), GNUTLS_X509_FMT_PEM); - - if (ret < 0) - { - std::cout << "Error [tls]: (CERT) cert file or/and (KEY) key file has not been accepted;" << std::endl; - - return false; - } - - if (false == app.stapling_file.empty() ) - { - ret = ::gnutls_certificate_set_ocsp_status_request_file(x509_cred, app.stapling_file.c_str(), 0); - - if (ret < 0) - { - std::cout << "Warning [tls]: (OCSP) stapling file has not been accepted;" << std::endl; - } - } - - ::gnutls_dh_params_t dh_params; - - ::gnutls_dh_params_init(&dh_params); - - if (app.dh_file.empty() ) - { - const unsigned int bits = ::gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_HIGH); - - ret = ::gnutls_dh_params_generate2(dh_params, bits); - } - else - { - std::ifstream dh_file(app.dh_file); - - if (dh_file) - { - const size_t max_file_size = 1024 * 1024; - - std::vector buf(max_file_size); - - dh_file.read(buf.data(), buf.size() ); - - gnutls_datum_t datum { - reinterpret_cast(buf.data() ), - static_cast(dh_file.gcount() ) - }; - - ret = ::gnutls_dh_params_import_pkcs3(dh_params, &datum, GNUTLS_X509_FMT_PEM); - } - else - { - ret = -1; - - std::cout << "Error [tls]: DH params file has not been opened;" << std::endl;; - } - - dh_file.close(); - } - - if (ret < 0) - { - ::gnutls_certificate_free_credentials(x509_cred); - - std::cout << "Error [tls]: DH params were not loaded;" << std::endl; - - return false; - } - - ::gnutls_certificate_set_dh_params(x509_cred, dh_params); - - ::gnutls_priority_t priority_cache; - - ret = ::gnutls_priority_init(&priority_cache, "NORMAL", nullptr); - - if (ret < 0) - { - ::gnutls_certificate_free_credentials(x509_cred); - - std::cout << "Error [tls]: priority cache cannot be init;" << std::endl; - - return false; - } - - data = std::tuple{x509_cred, priority_cache}; - - return true; - } - - bool Server::tryBindPort(const int port, std::unordered_set &ports) - { - // Only unique ports - if (ports.cend() != ports.find(port) ) - { - return false; - } - - Socket sock; - - if (false == sock.open() ) - { - std::cout << "Error: the socket cannot be open; errno " << Socket::getLastError() << ";" << std::endl; - return false; - } - - if (false == sock.bind(port) ) - { - std::cout << "Error: the socket " << port << " cannot be bind; errno " << Socket::getLastError() << ";" << std::endl; - return false; - } - - if (false == sock.listen() ) - { - std::cout << "Error: the socket " << port << " cannot be listen; errno " << Socket::getLastError() << ";" << std::endl; - return false; - } - - sock.nonblock(true); - - this->server_sockets.emplace_back(std::move(sock) ); - - ports.emplace(port); - - return true; - } - - void Server::initAppsPorts() - { - // Applications settings list - std::unordered_set applications; - // Get full applications settings list - this->apps_tree.collectApplicationSettings(applications); - - // Bind ports set - std::unordered_set ports; - - // Open applications sockets - for (auto const &app : applications) - { - const std::unordered_set &tls = app->tls_ports; - - if (false == tls.empty() ) - { - std::tuple data; - - if (Server::tlsInit(*app, data) ) - { - for (const int port : tls) - { - if (this->tryBindPort(port, ports) ) - { - this->tls_data.emplace(port, data); - } - } - } - } - - const std::unordered_set &list = app->ports; - - for (const int port : list) - { - this->tryBindPort(port, ports); - } - } - } - - int Server::run() - { - if (false == this->init() ) - { - return 0x10; - } - - this->initAppsPorts(); - - if (this->server_sockets.empty() ) - { - std::cout << "Error: any socket was not open;" << std::endl; - this->clear(); - return 0x20; - } - - SocketList sockets_list; - - sockets_list.create(this->server_sockets.size() ); - - for (auto const &sock : this->server_sockets) - { - sockets_list.addSocket(sock); - } - - std::cout << "Log: launch server's cycle;" << std::endl << std::endl; - - const size_t queue_max_length = 1024; - this->eventNotFullQueue = new Event(true, true); - this->eventProcessQueue = new Event(); - this->eventUpdateModule = new Event(false, true); - - std::queue > sockets; - - this->process_flag = true; - - std::function > &)> serverCycleQueue = std::mem_fn(&Server::cycleQueue); - std::thread threadQueue(serverCycleQueue, this, std::ref(sockets) ); - - std::vector accept_sockets; - std::vector accept_sockets_address; - - // Cycle for receiving new connections - do - { - if (sockets_list.accept(accept_sockets, accept_sockets_address) ) - { - this->sockets_queue_mtx.lock(); - - for (size_t i = 0; i < accept_sockets.size(); ++i) - { - const Socket &sock = accept_sockets[i]; - - if (sock.is_open() ) - { - sock.nonblock(true); - sock.tcp_nodelay(true); - - sockets.emplace( - std::tuple { - sock, - accept_sockets_address[i] - } - ); - } - } - - this->sockets_queue_mtx.unlock(); - - this->eventProcessQueue->notify(); - - if (sockets.size() >= queue_max_length) - { - this->eventNotFullQueue->reset(); - } - - accept_sockets.clear(); - accept_sockets_address.clear(); - - this->eventNotFullQueue->wait(); - } - } - while (this->process_flag || this->eventUpdateModule->notifed() ); - - this->eventProcessQueue->notify(); - - threadQueue.join(); - - sockets_list.destroy(); - - this->clear(); - - std::cout << "Log: complete server's cycle;" << std::endl; - - return EXIT_SUCCESS; - } - - void Server::stopProcess() - { - this->process_flag = false; - - if (this->eventNotFullQueue) - { - this->eventNotFullQueue->notify(); - } - - this->setProcessQueue(); - } - - void Server::unsetProcess() - { - this->process_flag = false; - } - - void Server::setRestart() - { - this->restart_flag = true; - } - - void Server::setUpdateModule() - { - if (this->eventUpdateModule) - { - this->eventUpdateModule->notify(); - } - } - - void Server::setProcessQueue() - { - if (this->eventProcessQueue) - { - this->eventProcessQueue->notify(); - } - } - - bool Server::get_start_args(const int argc, const char *argv[], struct server_start_args *st) - { - for (int i = 1; i < argc; ++i) - { - if (0 == ::strcmp(argv[i], "--start") ) - { - - } - else if (0 == ::strcmp(argv[i], "--force") ) - { - st->force = true; - } - else if (argv[i] == ::strstr(argv[i], "--config-path=") ) - { - st->config_path = std::string(argv[i] + sizeof("--config-path=") - 1); - } - else if (argv[i] == ::strstr(argv[i], "--server-name=") ) - { - st->server_name = std::string(argv[i] + sizeof("--server-name=") - 1); - } - else - { - std::cout << "The argument '" << argv[i] << "' can't be applied with --start;" << std::endl; - - return false; - } - } - - if (st->server_name.empty() ) - { - st->server_name = argv[0]; - } - - System::filterSharedMemoryName(st->server_name); - - return true; - } - - int Server::command_start(const int argc, const char *argv[]) - { - struct server_start_args st = {}; - - if (false == Server::get_start_args(argc, argv, &st) ) - { - return 0x1; - } - - if (false == st.config_path.empty() ) - { - if (false == System::changeCurrentDirectory(st.config_path) ) - { - std::cout << "Configuration path '" << st.config_path << "' has not been found;" << std::endl; - - return 0x2; - } - } - - if (st.force) - { - SharedMemory::destroy(st.server_name); - GlobalMutex::destory(st.server_name); - } - - GlobalMutex glob_mtx; - SharedMemory glob_mem; - - bool is_exists = false; - - if (glob_mtx.open(st.server_name) ) - { - glob_mtx.lock(); - - if (glob_mem.open(st.server_name) ) - { - System::native_processid_type pid = 0; - - if (glob_mem.read(&pid, sizeof(pid) ) ) - { - is_exists = System::isProcessExists(pid); - } - } - - glob_mtx.unlock(); - } - - if (is_exists) - { - std::cout << "The server instance with the name '" << st.server_name << "' is already running;" << std::endl; - - return 0x3; - } - - if (false == glob_mtx.open(st.server_name) ) - { - if (false == glob_mtx.create(st.server_name) ) - { - std::cout << "The global mutex could not been created;" << std::endl; - - return 0x4; - } - } - - glob_mtx.lock(); - - if (false == glob_mem.open(st.server_name) ) - { - if (false == glob_mem.create(st.server_name, sizeof(System::native_processid_type) ) ) - { - glob_mtx.unlock(); - - std::cout << "The shared memory could not been allocated;" << std::endl; - - return 0x5; - } - } - - System::native_processid_type pid = System::getProcessId(); - - if (false == glob_mem.write(&pid, sizeof(pid) ) ) - { - glob_mem.destroy(); - glob_mtx.unlock(); - - std::cout << "Writing data to the shared memory has failed;" << std::endl; - - return 0x6; - } - - glob_mtx.unlock(); - - int code = EXIT_FAILURE; - - do - { - this->process_flag = false; - this->restart_flag = false; - - code = this->run(); - } - while (this->process_flag || this->restart_flag); - - glob_mem.destroy(); - glob_mtx.destory(); - - return code; - } - - System::native_processid_type Server::getServerProcessId(const std::string &serverName) - { - System::native_processid_type pid = 0; - - GlobalMutex glob_mtx; - - if (glob_mtx.open(serverName) ) - { - SharedMemory glob_mem; - - glob_mtx.lock(); - - if (glob_mem.open(serverName) ) - { - glob_mem.read(&pid, sizeof(pid) ); - } - - glob_mtx.unlock(); - } - - return pid; - } - - int Server::command_help(const int argc, const char *argv[]) const - { - std::cout << std::left << "Available arguments:" << std::endl - << std::setw(4) << ' ' << std::setw(26) << "--start" << "Start the http server" << std::endl - << std::setw(8) << ' ' << std::setw(22) << "[options]" << std::endl - << std::setw(8) << ' ' << std::setw(22) << "--force" << "Forcibly start the http server (ignore the existing instance)" << std::endl - << std::setw(8) << ' ' << std::setw(22) << "--config-path=" << "The path to the directory with configuration files" << std::endl - << std::endl - << std::setw(4) << ' ' << std::setw(26) << "--restart" << "Restart the http server" << std::endl - << std::setw(4) << ' ' << std::setw(26) << "--update-module" << "Update the applications modules" << std::endl - << std::setw(4) << ' ' << std::setw(26) << "--kill" << "Shutdown the http server" << std::endl - << std::setw(4) << ' ' << std::setw(26) << "--help" << "This help" << std::endl - << std::endl<< "Optional arguments:" << std::endl - << std::setw(4) << ' ' << std::setw(26) << "--server-name=" << "The name of the server instance" << std::endl; - - return EXIT_SUCCESS; - } - - static std::string get_server_name(const int argc, const char *argv[]) - { - std::string server_name; - - for (int i = 1; i < argc; ++i) - { - if (argv[i] == ::strstr(argv[i], "--server-name=") ) - { - server_name = std::string(argv[i] + sizeof("--server-name=") - 1); - break; - } - } - - if (server_name.empty() ) - { - server_name = argv[0]; - } - - System::filterSharedMemoryName(server_name); - - return server_name; - } - - int Server::command_restart(const int argc, const char *argv[]) const - { - const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); - - if (1 < pid && System::sendSignal(pid, SIGUSR1) ) - { - return EXIT_SUCCESS; - } - - return EXIT_FAILURE; - } - - int Server::command_terminate(const int argc, const char *argv[]) const - { - const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); - - if (1 < pid && System::sendSignal(pid, SIGTERM) ) - { - return EXIT_SUCCESS; - } - - return EXIT_FAILURE; - } - - int Server::command_update_module(const int argc, const char *argv[]) const - { - const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); - - if (1 < pid && System::sendSignal(pid, SIGUSR2) ) - { - return EXIT_SUCCESS; - } - - return EXIT_FAILURE; - } -}; \ No newline at end of file diff --git a/src/Server.h b/src/Server.h deleted file mode 100644 index 54f1d2d..0000000 --- a/src/Server.h +++ /dev/null @@ -1,148 +0,0 @@ -#pragma once - -#include "SocketList.h" -#include "DataVariantAbstract.h" -#include "ServerApplicationsTree.h" -#include "ServerApplicationDefaultSettings.h" -#include "Module.h" -#include "Event.h" -#include "SocketAdapter.h" -#include "ServerStructuresArguments.h" - -#include -#include -#include -#include -#include -#include - -#include - -namespace HttpServer -{ - class Server - { - protected: - std::unordered_map variants; - - std::unordered_map settings; - std::unordered_map mimes_types; - - std::unordered_map > tls_data; - - std::vector modules; - - ServerApplicationsTree apps_tree; - - std::vector server_sockets; - - Event *eventNotFullQueue; - Event *eventProcessQueue; - Event *eventUpdateModule; - - mutable std::atomic_size_t threads_working_count; - mutable std::mutex sockets_queue_mtx; - - // Флаг, означающий - активированы ли главные циклы сервера - // (с помощью этого флага можно деактивировать циклы, чтобы завершить работу сервера) - sig_atomic_t process_flag; - sig_atomic_t restart_flag; - - protected: - static const int CONNECTION_CLOSED = 0; - static const int CONNECTION_KEEP_ALIVE = 1; - static const int CONNECTION_UPGRADE = 2; - - protected: - int cycleQueue(std::queue > &sockets); - - int threadRequestProc(SocketAdapter &clientSocket, const struct sockaddr_in &clientAddr) const; - - static bool getRequest(const SocketAdapter &clientSocket, std::vector &buf, std::string &str_buf, struct request_parameters &rp); - - int getRequestHeaders(std::string &str_buf, struct request_parameters &rp) const; - - static void runApplication(const SocketAdapter &clientSocket, const ServerApplicationSettings &appSets, struct request_parameters &rp); - - int getRequestData(const SocketAdapter &clientSocket, std::string &str_buf, const ServerApplicationSettings &appSets, struct request_parameters &rp) const; - - const ServerApplicationSettings *getApplicationSettings(const struct request_parameters &rp) const; - - static void getConnectionParams(struct request_parameters &rp); - - void xSendfile(const SocketAdapter &clientSocket, struct request_parameters &rp) const; - - static bool isConnectionKeepAlive(const struct request_parameters &rp); - static bool isConnectionUpgrade(const struct request_parameters &rp); - - void threadRequestCycle(std::queue > &sockets, Event &eventThreadCycle) const; - - std::string getMimeTypeByFileName(const std::string &fileName) const; - - std::vector > getRanges( - const std::string &rangeHeader, - const size_t posSymEqual, - const size_t fileSize, - std::string &resultRangeHeader, - size_t &contentLength - ) const; - - int transferFilePart( - const SocketAdapter &clientSocket, - const std::chrono::milliseconds &timeout, - const std::string &fileName, - const time_t fileTime, - const size_t fileSize, - const std::string &rangeHeader, - const std::string &connectionHeader, - const std::string &dateHeader, - const bool headersOnly - ) const; - - int transferFile( - const SocketAdapter &clientSocket, - const std::string &fileName, - const std::string &connectionHeader, - const bool headersOnly, - struct request_parameters &rp - ) const; - - static bool parseIncomingVars(std::unordered_multimap ¶ms, const std::string &uriParams); - static void sendStatus(const SocketAdapter &clientSocket, const std::chrono::milliseconds &timeout, const size_t statusCode); - - static bool tlsInit(const ServerApplicationSettings &app, std::tuple &data); - - bool tryBindPort(const int port, std::unordered_set &ports); - void initAppsPorts(); - - bool init(); - int run(); - void clear(); - - static System::native_processid_type getServerProcessId(const std::string &serverName); - - void updateModules(); - bool updateModule(Module &module, std::unordered_set &applications, const size_t moduleIndex); - - private: - void addDataVariant(DataVariantAbstract *dataVariant); - - static bool get_start_args(const int argc, const char *argv[], struct server_start_args *st); - - public: - Server(); - ~Server() = default; - - void stopProcess(); - void unsetProcess(); - void setRestart(); - void setUpdateModule(); - void setProcessQueue(); - - int command_help(const int argc, const char *argv[]) const; - int command_start(const int argc, const char *argv[]); - int command_restart(const int argc, const char *argv[]) const; - int command_terminate(const int argc, const char *argv[]) const; - int command_update_module(const int argc, const char *argv[]) const; - }; -}; \ No newline at end of file diff --git a/src/ServerApplicationDefaultSettings.h b/src/ServerApplicationDefaultSettings.h deleted file mode 100644 index c0d59d1..0000000 --- a/src/ServerApplicationDefaultSettings.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -namespace HttpServer -{ - struct ServerApplicationDefaultSettings - { - std::string temp_dir; - size_t request_max_size; - }; -}; \ No newline at end of file diff --git a/src/ServerRequest.h b/src/ServerRequest.h deleted file mode 100644 index dc80893..0000000 --- a/src/ServerRequest.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include "RawData.h" -#include "SocketAdapterTls.h" -#include "FileIncoming.h" - -#include - -namespace HttpServer -{ - struct server_request - { - const System::native_socket_type socket; - const ::gnutls_session_t tls_session; - const char *method; - const char *uri_reference; - const char *document_root; - const size_t params_count; - const Utils::raw_pair *params; - const size_t headers_count; - const Utils::raw_pair *headers; - const size_t data_count; - const Utils::raw_pair *data; - const size_t files_count; - const Utils::raw_fileinfo *files; - }; - - /** - * Структура запроса (входные данные) - * - * @member const SocketAdapter &socket - сокет клиента - * @member const std::string method - метод применяемый к ресурсу - * @member const std::string uri_reference - ссылка на ресурс - * @member const std::string document_root - корневая директория приложения - * @member const std::unordered_multimap params - параметры ресурса - * @member const std::unordered_map headers - заголовки запроса - * @member const std::unordered_multimap data - входящие данные запроса - * @member const std::unordered_multimap files - входящие файлы запроса - * @member const std::unordered_multimap cookies - входящие куки запроса - */ - struct ServerRequest - { - const SocketAdapter &socket; - const std::string method; - const std::string uri_reference; - const std::string document_root; - const std::unordered_multimap params; - const std::unordered_map headers; - const std::unordered_multimap data; - const std::unordered_multimap files; - const std::unordered_multimap cookies; - }; -}; diff --git a/src/ServerResponse.h b/src/ServerResponse.h deleted file mode 100644 index 7573e3a..0000000 --- a/src/ServerResponse.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "RawData.h" -#include "SocketAdapter.h" - -#include -#include - -namespace HttpServer -{ - struct server_response - { - System::native_socket_type socket; - size_t headers_count; - Utils::raw_pair *headers; - }; - - struct ServerResponse - { - SocketAdapter &socket; - std::map headers; - }; -}; diff --git a/src/SignalHandlers.cpp b/src/SignalHandlers.cpp index cdc4bd4..a187c36 100644 --- a/src/SignalHandlers.cpp +++ b/src/SignalHandlers.cpp @@ -1,6 +1,6 @@  #include "SignalHandlers.h" -#include "System.h" +#include "system/System.h" #ifdef WIN32 #include @@ -17,47 +17,47 @@ static HttpServer::Server *globalServerPtr = nullptr; /** * Terminate signal */ -static void handlerSigTerm(const int) +static void handlerSigTerm(const int) noexcept { if (globalServerPtr) { - globalServerPtr->stopProcess(); + globalServerPtr->controls.stopProcess(); } } /** * Interrupt signal */ -static void handlerSigInt(const int) +static void handlerSigInt(const int) noexcept { if (globalServerPtr) { - globalServerPtr->stopProcess(); + globalServerPtr->controls.stopProcess(); } } /** * Signal to restart */ -static void handlerSigUsr1(const int) +static void handlerSigUsr1(const int) noexcept { if (globalServerPtr) { - globalServerPtr->setRestart(); - globalServerPtr->stopProcess(); + globalServerPtr->controls.setRestart(); + globalServerPtr->controls.stopProcess(); } } /** * Signal to update modules */ -static void handlerSigUsr2(const int) +static void handlerSigUsr2(const int) noexcept { if (globalServerPtr) { - globalServerPtr->setUpdateModule(); - globalServerPtr->unsetProcess(); - globalServerPtr->setProcessQueue(); + globalServerPtr->controls.setUpdateModule(); + globalServerPtr->controls.setProcess(false); + globalServerPtr->controls.setProcessQueue(); } } @@ -67,7 +67,7 @@ static void handlerSigUsr2(const int) * It doesn't work in case the program was launched and was * attempted to finish under different remote sessions. */ -static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const ::WPARAM wParam, const ::LPARAM lParam) +static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const ::WPARAM wParam, const ::LPARAM lParam) noexcept { switch (message) { @@ -123,7 +123,7 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const return 0; } -static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event * const eventWindowCreation) +static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event * const eventWindowCreation) noexcept { const ::HWND hWnd = ::CreateWindow(myWndClassName, nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, hInstance, nullptr); @@ -146,7 +146,7 @@ static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event * } #ifdef _CONSOLE -static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) +static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept { switch (ctrlType) { @@ -175,7 +175,7 @@ static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) #endif // _CONSOLE #endif // WIN32 -bool bindSignalHandlers(HttpServer::Server *server) +bool bindSignalHandlers(HttpServer::Server *server) noexcept { globalServerPtr = server; @@ -234,10 +234,10 @@ bool bindSignalHandlers(HttpServer::Server *server) return true; } -void stopSignalHandlers() +void stopSignalHandlers() noexcept { #ifdef WIN32 System::sendSignal(::GetCurrentProcessId(), SIGINT); gThreadMessageLoop.join(); #endif -} \ No newline at end of file +} diff --git a/src/SignalHandlers.h b/src/SignalHandlers.h index 17bcc1e..3db4da3 100644 --- a/src/SignalHandlers.h +++ b/src/SignalHandlers.h @@ -1,7 +1,7 @@ #pragma once -#include "Server.h" +#include "server/Server.h" -bool bindSignalHandlers(HttpServer::Server *server); +bool bindSignalHandlers(HttpServer::Server *server) noexcept; -void stopSignalHandlers(); \ No newline at end of file +void stopSignalHandlers() noexcept; diff --git a/src/Socket.h b/src/Socket.h deleted file mode 100644 index 738abc8..0000000 --- a/src/Socket.h +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once - -#include "System.h" - -#include -#include -#include - -namespace HttpServer -{ - class Socket - { - protected: - System::native_socket_type socket_handle; - - public: - bool static Startup(); - bool static Cleanup(); - int static getLastError(); - - public: - Socket(); - Socket(const System::native_socket_type fd); - Socket(const Socket &obj); - Socket(Socket &&obj); - - ~Socket() = default; - - bool open(); - bool close(); - - bool is_open() const; - System::native_socket_type get_handle() const; - - bool bind(const int port) const; - bool listen() const; - - Socket accept() const; - Socket nonblock_accept() const; - Socket nonblock_accept(const std::chrono::milliseconds &timeout) const; - - bool shutdown() const; - - bool nonblock(const bool isNonBlock = true) const; - // bool is_nonblock() const; - bool tcp_nodelay(const bool nodelay = true) const; - - long recv(std::vector &buf) const; - long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const; - - long send(const std::string &buf) const; - long send(const std::vector &buf, const size_t length) const; - - long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const; - long nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const; - - void nonblock_send_sync() const; - - Socket &operator =(const Socket &obj); - - bool operator ==(const Socket &obj) const; - bool operator !=(const Socket &obj) const; - }; -}; - -namespace std -{ - // Hash for Socket - template<> struct hash - { - std::size_t operator()(const HttpServer::Socket &obj) const - { - return std::hash{}(obj.get_handle() ); - } - }; -}; \ No newline at end of file diff --git a/src/SocketAdapter.cpp b/src/SocketAdapter.cpp deleted file mode 100644 index 5354cb0..0000000 --- a/src/SocketAdapter.cpp +++ /dev/null @@ -1,15 +0,0 @@ - -#include "SocketAdapter.h" - -namespace HttpServer -{ - bool SocketAdapter::operator ==(const SocketAdapter &obj) const - { - return this->get_handle() == obj.get_handle(); - } - - bool SocketAdapter::operator !=(const SocketAdapter &obj) const - { - return this->get_handle() != obj.get_handle(); - } -}; \ No newline at end of file diff --git a/src/SocketAdapter.h b/src/SocketAdapter.h deleted file mode 100644 index 690e668..0000000 --- a/src/SocketAdapter.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include "System.h" - -#include -#include -#include - -#include - -namespace HttpServer -{ - class SocketAdapter - { - public: - virtual ~SocketAdapter() = default; - - virtual System::native_socket_type get_handle() const = 0; - virtual ::gnutls_session_t get_tls_session() const = 0; - virtual SocketAdapter *copy() const = 0; - - virtual long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const = 0; - - virtual long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const = 0; - virtual long nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const = 0; - - virtual void close() = 0; - - bool operator ==(const SocketAdapter &obj) const; - bool operator !=(const SocketAdapter &obj) const; - }; -}; \ No newline at end of file diff --git a/src/SocketAdapterDefault.cpp b/src/SocketAdapterDefault.cpp deleted file mode 100644 index 12fffd4..0000000 --- a/src/SocketAdapterDefault.cpp +++ /dev/null @@ -1,49 +0,0 @@ - -#include "SocketAdapterDefault.h" - -namespace HttpServer -{ - SocketAdapterDefault::SocketAdapterDefault(const Socket &_sock) : sock(_sock) - { - - } - - System::native_socket_type SocketAdapterDefault::get_handle() const - { - return sock.get_handle(); - } - - ::gnutls_session_t SocketAdapterDefault::get_tls_session() const - { - return 0; - } - - SocketAdapter *SocketAdapterDefault::copy() const - { - return new SocketAdapterDefault(this->sock); - } - - long SocketAdapterDefault::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const - { - return sock.nonblock_recv(buf, timeout); - } - - long SocketAdapterDefault::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const - { - return sock.nonblock_send(buf, timeout); - } - - long SocketAdapterDefault::nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const - { - return sock.nonblock_send(buf, length, timeout); - } - - void SocketAdapterDefault::close() - { - // Wait for send all data to client - sock.nonblock_send_sync(); - - sock.shutdown(); - sock.close(); - } -}; \ No newline at end of file diff --git a/src/SocketAdapterDefault.h b/src/SocketAdapterDefault.h deleted file mode 100644 index e3272be..0000000 --- a/src/SocketAdapterDefault.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include "SocketAdapter.h" -#include "Socket.h" - -namespace HttpServer -{ - class SocketAdapterDefault : public SocketAdapter - { - private: - Socket sock; - - public: - SocketAdapterDefault() = delete; - SocketAdapterDefault(const Socket &_sock); - - virtual System::native_socket_type get_handle() const override; - virtual ::gnutls_session_t get_tls_session() const override; - virtual SocketAdapter *copy() const override; - - virtual long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const override; - - virtual long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const override; - virtual long nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const override; - - virtual void close() override; - }; -}; \ No newline at end of file diff --git a/src/SocketAdapterTls.cpp b/src/SocketAdapterTls.cpp deleted file mode 100644 index 2860db7..0000000 --- a/src/SocketAdapterTls.cpp +++ /dev/null @@ -1,122 +0,0 @@ - -#include "SocketAdapterTls.h" - -namespace HttpServer -{ - SocketAdapterTls::SocketAdapterTls(const Socket &sock, ::gnutls_priority_t priority_cache, ::gnutls_certificate_credentials_t x509_cred) - { - ::gnutls_init(&this->session, GNUTLS_SERVER); - ::gnutls_priority_set(this->session, priority_cache); - ::gnutls_credentials_set(this->session, GNUTLS_CRD_CERTIFICATE, x509_cred); - - ::gnutls_certificate_server_set_request(this->session, GNUTLS_CERT_IGNORE); - - ::gnutls_transport_set_int2(this->session, sock.get_handle(), sock.get_handle() ); - } - - SocketAdapterTls::SocketAdapterTls(const ::gnutls_session_t _session) : session(_session) - { - - } - - bool SocketAdapterTls::handshake() - { - int ret; - - do - { - ret = ::gnutls_handshake(this->session); - } - while (ret < 0 && ::gnutls_error_is_fatal(ret) == 0); - - if (ret < 0) - { - Socket sock(this->get_handle() ); - - sock.close(); - ::gnutls_deinit(this->session); - - return false; - } - - return true; - } - - long SocketAdapterTls::nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const - { - size_t record_size = ::gnutls_record_get_max_size(this->session); - - if (0 == record_size) - { - return -1; - } - - size_t total = 0; - - while (total < length) - { - ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); - - if (record_size > length - total) - { - record_size = length - total; - } - - const long send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); - - if (send_size < 0) - { - return send_size; - } - - total += send_size; - } - - return static_cast(total); - } - - System::native_socket_type SocketAdapterTls::get_handle() const - { - return static_cast(::gnutls_transport_get_int(this->session) ); - } - - ::gnutls_session_t SocketAdapterTls::get_tls_session() const - { - return this->session; - } - - SocketAdapter *SocketAdapterTls::copy() const - { - return new SocketAdapterTls(this->session); - } - - long SocketAdapterTls::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const - { - ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); - return ::gnutls_record_recv(this->session, buf.data(), buf.size() ); - } - - long SocketAdapterTls::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const - { - return this->nonblock_send_all(buf.data(), buf.length(), timeout); - } - - long SocketAdapterTls::nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const - { - return this->nonblock_send_all(buf.data(), length, timeout); - } - - void SocketAdapterTls::close() - { - Socket sock(this->get_handle() ); - - // Wait for send all data to client - sock.nonblock_send_sync(); - - ::gnutls_bye(this->session, GNUTLS_SHUT_RDWR); - - sock.close(); - - ::gnutls_deinit(this->session); - } -}; \ No newline at end of file diff --git a/src/SocketAdapterTls.h b/src/SocketAdapterTls.h deleted file mode 100644 index de57147..0000000 --- a/src/SocketAdapterTls.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include "SocketAdapter.h" -#include "Socket.h" - -namespace HttpServer -{ - class SocketAdapterTls : public SocketAdapter - { - private: - ::gnutls_session_t session; - - public: - SocketAdapterTls() = delete; - SocketAdapterTls(const Socket &sock, ::gnutls_priority_t priority_cache, ::gnutls_certificate_credentials_t x509_cred); - SocketAdapterTls(const ::gnutls_session_t _session); - - bool handshake(); - long nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const; - - virtual System::native_socket_type get_handle() const override; - virtual ::gnutls_session_t get_tls_session() const override; - virtual SocketAdapter *copy() const override; - - virtual long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const override; - - virtual long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const override; - virtual long nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const override; - - virtual void close() override; - }; -}; \ No newline at end of file diff --git a/src/Utils.cpp b/src/Utils.cpp deleted file mode 100644 index 525a216..0000000 --- a/src/Utils.cpp +++ /dev/null @@ -1,459 +0,0 @@ - -#include "Utils.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace Utils -{ - void toLower(std::string &str, const std::locale &loc) - { - for (auto &c : str) - { - c = std::tolower(c, loc); - } - } - - void trim(std::string &str) - { - static const std::array whitespace { " \t\n\v\f\r" }; - - const size_t last = str.find_last_not_of(whitespace.data() ); - - if (std::string::npos == last) - { - return str.clear(); - } - - str.assign(str.cbegin() + str.find_first_not_of(whitespace.data() ), str.cbegin() + last + 1); - } - - std::vector explode(const std::string &str, const char sep) - { - std::vector values; - - for (size_t pos = 0; std::string::npos != pos;) - { - size_t delimiter = str.find(sep, pos); - - std::string value = str.substr(pos, delimiter); - trim(value); - - values.emplace_back(std::move(value) ); - - pos = delimiter; - - if (std::string::npos != pos) - { - ++pos; - } - } - - return values; - } - - std::string encodeHtmlSymbols(const std::string &str) - { - std::string buf; - buf.reserve(str.length() ); - - for (size_t pos = 0; pos < str.length(); ++pos) - { - switch (str[pos]) - { - case '&': buf.append("&"); break; - case '\"': buf.append("""); break; - case '\'': buf.append("'"); break; - case '<': buf.append("<"); break; - case '>': buf.append(">"); break; - default: buf.push_back(str[pos]); break; - } - } - - return buf; - } - - std::string binToHexString(const void *binData, const size_t dataSize) - { - std::string str(dataSize * 2, 0); - - const uint8_t *bin = reinterpret_cast(binData); - - static const std::array hexDigits { "0123456789abcdef" }; - - for (size_t i = dataSize - 1; std::numeric_limits::max() != i; --i) - { - str[i * 2 + 0] = hexDigits[bin[i] >> 4]; - str[i * 2 + 1] = hexDigits[bin[i] & 0x0F]; - } - - return str; - } - - static unsigned char hexStringToBinEncodeSymbol(const char c) - { - if (c >= '0' && c <= '9') - { - return c - 0x30; - } - else if (c >= 'a' && c <= 'f') - { - return c - 0x57; - } - else if (c >= 'A' && c <= 'F') - { - return c - 0x37; - } - - return 0; - } - - std::string hexStringToBin(const std::string &hexStr) - { - std::string bin(hexStr.length() / 2, 0); - - for (size_t i = 0; i < bin.length(); ++i) - { - const char a = hexStr[i * 2 + 0]; - const char b = hexStr[i * 2 + 1]; - - bin[i] = ( - (hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b) - ); - } - - return bin; - } - - unsigned long long htonll(const unsigned long long src) - { - enum Endianness { - INIT = 0, - LITE = 1, - BIGE = 2 - }; - - static int endian = Endianness::INIT; - - union { - unsigned long long ull; - unsigned char c[8]; - } x; - - if (endian == Endianness::INIT) - { - x.ull = 0x01; - endian = (x.c[7] == 0x01ULL) ? Endianness::BIGE : Endianness::LITE; - } - - if (endian == Endianness::BIGE) - { - return src; - } - - x.ull = src; - - unsigned char c; - - c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c; - c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c; - c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c; - c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c; - - return x.ull; - } - - std::string getUniqueName() - { - size_t time = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - - time = htonll(time); - - return binToHexString(&time, sizeof(time) ); - } - - char *stlStringToPChar(const std::string &str) - { - const size_t length = str.length(); - char *s = nullptr; - - if (length) - { - s = new char[length + 1]; - #ifdef WIN32 - ::strcpy_s(s, length + 1, str.c_str() ); - #elif POSIX - ::strcpy(s, str.c_str() ); - s[length] = '\0'; - #else - #error "Undefine platform" - #endif - } - - return s; - } - - void filesIncomingToRawFilesInfo(Utils::raw_fileinfo *raw[], const std::unordered_multimap &map) - { - if (raw && map.size() ) - { - raw_fileinfo *arr = new raw_fileinfo[map.size()]; - - *raw = arr; - - size_t i = 0; - - for (auto it = map.cbegin(); map.cend() != it; ++it, ++i) - { - arr[i].key = stlStringToPChar(it->first); - - const HttpServer::FileIncoming &file = it->second; - - arr[i].file_name = stlStringToPChar(file.getName() ); - arr[i].file_type = stlStringToPChar(file.getType() ); - arr[i].file_size = file.getSize(); - } - } - } - - void rawFilesInfoToFilesIncoming(std::unordered_multimap &map, const Utils::raw_fileinfo raw[], const size_t count) - { - for (size_t i = 0; i < count; ++i) - { - map.emplace(raw[i].key ? raw[i].key : "", HttpServer::FileIncoming(raw[i].file_name, raw[i].file_type, raw[i].file_size) ); - } - } - - void destroyRawPairs(Utils::raw_pair raw[], const size_t count) - { - if (raw) - { - for (size_t i = 0; i < count; ++i) - { - raw_pair &cur = raw[i]; - - delete[] cur.key; - delete[] cur.value; - } - - delete[] raw; - } - } - - void destroyRawFilesInfo(Utils::raw_fileinfo raw[], const size_t count) - { - if (raw) - { - for (size_t i = 0; i < count; ++i) - { - raw_fileinfo &cur = raw[i]; - - delete[] cur.key; - delete[] cur.file_name; - delete[] cur.file_type; - } - - delete[] raw; - } - } - - time_t stringTimeToTimestamp(const std::string &strTime) - { - static const std::unordered_map map_months { - {"Jan", 0}, {"Feb", 1}, {"Mar", 2}, {"Apr", 3}, {"May", 4}, {"Jun", 5}, {"Jul", 6}, {"Aug", 7}, {"Sep", 8}, {"Oct", 9}, {"Nov", 10}, {"Dec", 11} - }; - - if (strTime.length() > 64) - { - return ~0; - } - - const size_t str_mon_length = 64; - std::vector s_mon(str_mon_length); - - struct ::tm tc = {}; - - // Parse RFC 822 - #ifdef WIN32 - if (~0 != ::sscanf_s(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), static_cast(s_mon.size() ), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) ) - #else - if (~0 != ::sscanf(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) ) - #endif - { - tc.tm_year -= 1900; - - auto const it_mon = map_months.find(s_mon.data() ); - - if (map_months.cend() != it_mon) - { - tc.tm_mon = it_mon->second; - } - } - - tc.tm_isdst = -1; - - return ::mktime(&tc); - } - - std::string getDatetimeAsString(const ::time_t tTime, const bool isGmtTime) - { - std::array buf; - - ::time_t cur_time = tTime; - - if (tTime == ~0) - { - ::time(&cur_time); - } - - #ifdef WIN32 - struct ::tm stm = {}; - - isGmtTime ? - ::localtime_s(&stm, &cur_time) : - ::gmtime_s(&stm, &cur_time); - - // RFC 822 - ::strftime(buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm); - #else - struct ::tm stm = {}; - - isGmtTime ? - ::localtime_r(&cur_time, &stm) : - ::gmtime_r(&cur_time, &stm); - - // RFC 822 - ::strftime(buf.data(), buf.size(), "%a, %d %b %G %H:%M:%S GMT", &stm); - #endif - - return std::string(buf.data() ); - } - - size_t getNumberLength(const size_t number) - { - size_t length = 0; - - size_t n = number; - - do - { - ++length; - n /= 10; - } - while (n); - - return length; - } - - bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies) - { - if (cookieHeader.empty() ) - { - return true; - } - - for (size_t cur_pos = 0, next_value; std::string::npos != cur_pos; cur_pos = next_value) - { - next_value = cookieHeader.find(';', cur_pos); - - size_t delimiter = cookieHeader.find('=', cur_pos); - - if (std::string::npos == delimiter || delimiter > next_value) - { - return false; - } - - std::string key = cookieHeader.substr(cur_pos, delimiter - cur_pos); - trim(key); - key = urlDecode(key); - - ++delimiter; - - std::string value = cookieHeader.substr(delimiter, std::string::npos != next_value ? next_value - delimiter : next_value); - trim(value); - value = urlDecode(value); - - cookies.emplace(std::move(key), std::move(value) ); - - if (std::string::npos != next_value) - { - ++next_value; - } - } - - return true; - } - - static inline bool isCharUrlAllowed(const char c) - { - return c == '-' || c == '_' || c == '.' || c == '~'; - } - - std::string urlEncode(const std::string &str) - { - std::string encoded; - - static const std::array hexDigits { "0123456789ABCDEF" }; - - for (size_t i = 0; i < str.length(); ++i) - { - const unsigned char c = str[i]; - - if (std::isalnum(c) || isCharUrlAllowed(c) ) - { - encoded.push_back(c); - } - else if (' ' == c) - { - encoded.push_back('+'); - } - else - { - const uint8_t a = c >> 4; - const uint8_t b = c & 0x0F; - - encoded.push_back('%'); - encoded.push_back(hexDigits[a]); - encoded.push_back(hexDigits[b]); - } - } - - return encoded; - } - - std::string urlDecode(const std::string &str) - { - std::string decoded; - - for (size_t i = 0; i < str.length(); ++i) - { - unsigned char c = str[i]; - - if ('%' == c) - { - if (i + 2 < str.length() ) - { - const char a = str[++i]; - const char b = str[++i]; - - c = ( - (hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b) - ); - } - } - else if ('+' == c) - { - c = ' '; - } - - decoded.push_back(c); - } - - return decoded; - } -}; \ No newline at end of file diff --git a/src/Utils.h b/src/Utils.h deleted file mode 100644 index b2e15c1..0000000 --- a/src/Utils.h +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once - -#include "RawData.h" -#include "FileIncoming.h" - -#include -#include -#include - -namespace Utils -{ - void toLower(std::string &str, const std::locale &loc); - - void trim(std::string &str); - - std::vector explode(const std::string &str, const char sep); - - std::string encodeHtmlSymbols(const std::string &str); - - std::string binToHexString(const void *bin, const size_t size); - - std::string hexStringToBin(const std::string &hexStr); - - unsigned long long htonll(const unsigned long long src); - - std::string getUniqueName(); - - char *stlStringToPChar(const std::string &str); - - template - void stlToRawPairs(Utils::raw_pair *raw[], const T &stl) - { - if (raw && stl.size() ) - { - raw_pair *arr = new raw_pair[stl.size()]; - - *raw = arr; - - size_t i = 0; - - for (auto it = stl.cbegin(); stl.cend() != it; ++it, ++i) - { - arr[i].key = stlStringToPChar(it->first); - arr[i].value = stlStringToPChar(it->second); - } - } - } - - template - void rawPairsToStl(T &stl, const Utils::raw_pair raw[], const size_t count) - { - for (size_t i = 0; i < count; ++i) - { - stl.emplace(raw[i].key ? raw[i].key : "", raw[i].value ? raw[i].value : ""); - } - } - - void filesIncomingToRawFilesInfo(Utils::raw_fileinfo *raw[], const std::unordered_multimap &map); - void rawFilesInfoToFilesIncoming(std::unordered_multimap &map, const Utils::raw_fileinfo raw[], const size_t count); - - void destroyRawPairs(Utils::raw_pair raw[], const size_t count); - void destroyRawFilesInfo(Utils::raw_fileinfo raw[], const size_t count); - - time_t stringTimeToTimestamp(const std::string &strTime); - - std::string getDatetimeAsString(const ::time_t tTime = ~0, const bool isGmtTime = false); - - size_t getNumberLength(const size_t number); - - bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies); - - std::string urlEncode(const std::string &str); - std::string urlDecode(const std::string &str); -}; \ No newline at end of file diff --git a/src/server/Request.cpp b/src/server/Request.cpp new file mode 100644 index 0000000..f8bfcea --- /dev/null +++ b/src/server/Request.cpp @@ -0,0 +1,20 @@ + +#include "Request.h" + +namespace HttpServer +{ + void Request::clear() noexcept + { + incoming_headers.clear(); + incoming_data.clear(); + incoming_files.clear(); + + outgoing_headers.clear(); + + host.clear(); + path.clear(); + method.clear(); + + // timeout = std::chrono::milliseconds::zero(); + } +}; diff --git a/src/server/Request.h b/src/server/Request.h new file mode 100644 index 0000000..dd2b5d5 --- /dev/null +++ b/src/server/Request.h @@ -0,0 +1,44 @@ +#pragma once + +#include "../transfer/AppRequest.h" +#include "../transfer/ProtocolVariant.h" + +#include +#include + +namespace HttpServer +{ + enum ConnectionParams + { + CONNECTION_CLOSE = 0, + CONNECTION_REUSE = 1, + CONNECTION_LEAVE_OPEN = 2 + }; + + struct Request : public Transfer::request_data + { + std::unordered_map outgoing_headers; + + std::string host; + std::string path; + std::string method; + + std::chrono::milliseconds timeout; + + size_t keep_alive_count; + int connection_params; + int app_exit_code; + + Transfer::ProtocolVariant protocol_variant; + // void *protocol_data; + + public: + void clear() noexcept; + }; + + struct DataTransfer + { + size_t full_size; + size_t send_total; + }; +}; diff --git a/src/server/Server.cpp b/src/server/Server.cpp new file mode 100644 index 0000000..d46caf0 --- /dev/null +++ b/src/server/Server.cpp @@ -0,0 +1,1109 @@ + +#include "Server.h" + +#include "config/ConfigParser.h" +#include "data-variant/FormUrlencoded.h" +#include "data-variant/MultipartFormData.h" +#include "data-variant/TextPlain.h" +#include "protocol/ServerHttp1.h" +#include "protocol/ServerHttp2.h" +#include "protocol/ServerHttp2Stream.h" + +#include "ServerStructuresArguments.h" + +#include "../socket/List.h" +#include "../socket/AdapterDefault.h" +#include "../socket/AdapterTls.h" +#include "../system/GlobalMutex.h" +#include "../system/SharedMemory.h" +#include "../system/System.h" +#include "../utils/Utils.h" + +#include +#include +#include +#include +#include + +namespace HttpServer +{ + static std::unique_ptr getProtocolVariant( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, + SocketsQueue &sockets, + Http2::IncStream *stream + ) + { + std::unique_ptr prot; + + // If request is HTTP/2 Stream + if (stream) + { + prot.reset(new ServerHttp2Stream(sock, settings, controls, stream) ); + + return prot; + } + + if (sock.get_tls_session() ) + { + ::gnutls_datum_t datum; + + if (0 == ::gnutls_alpn_get_selected_protocol(sock.get_tls_session(), &datum) ) + { + const std::string protocol(reinterpret_cast(datum.data), datum.size); + + if ("h2" == protocol) + { + prot.reset(new ServerHttp2(sock, settings, controls, sockets) ); + } + else if ("http/1.1" == protocol) + { + prot.reset(new ServerHttp1(sock, settings, controls) ); + } + } + + return prot; + } + + prot.reset(new ServerHttp1(sock, settings, controls) ); + + return prot; + } + + /** + * Метод для обработки запроса + */ + void Server::threadRequestProc(Socket::Adapter &sock, SocketsQueue &sockets, Http2::IncStream *stream) const + { + std::unique_ptr prot = getProtocolVariant(sock, this->settings, this->controls, sockets, stream); + + if (prot) + { + // Check if switching protocol + for (ServerProtocol *ret = nullptr; ; ) + { + ret = prot->process(); + + if (prot.get() == ret) + { + break; + } + + prot.reset(ret); + } + + prot->close(); + } + } + + /** + * Метод для обработки запросов (запускается в отдельном потоке) + * извлекает сокет клиенты из очереди и передаёт его на обслуживание + */ + void Server::threadRequestCycle(SocketsQueue &sockets, Utils::Event &eventThreadCycle) const + { + while (true) + { + Socket::Socket sock; + Http2::IncStream *stream = nullptr; + + eventThreadCycle.wait(); + + if (false == this->controls.process_flag) + { + break; + } + + sockets.lock(); + + if (sockets.size() ) + { + std::tie(sock, stream) = sockets.front(); +/* + sockaddr_in addr {}; + socklen_t addr_size = sizeof(sockaddr_in); + ::getpeername(sock.get_handle(), reinterpret_cast(&addr), &addr_size); +*/ + sockets.pop(); + } + + if (sockets.empty() ) + { + eventThreadCycle.reset(); + + this->controls.eventNotFullQueue->notify(); + } + + sockets.unlock(); + + if (sock.is_open() ) + { + ++this->threads_working_count; + + ::sockaddr_in sock_addr {}; + ::socklen_t sock_addr_len = sizeof(sock_addr); + + ::getsockname(sock.get_handle(), reinterpret_cast(&sock_addr), &sock_addr_len); + + const int port = ntohs(sock_addr.sin_port); + + auto const it = this->tls_data.find(port); + + if (this->tls_data.cend() != it) // if TLS connection + { + if (stream) + { + Socket::AdapterTls socket_adapter(reinterpret_cast(stream->reserved) ); + + this->threadRequestProc(socket_adapter, sockets, stream); + } + else + { + const std::tuple &data = it->second; + + Socket::AdapterTls socket_adapter( + sock, + std::get(data), + std::get(data) + ); + + if (socket_adapter.handshake() ) + { + this->threadRequestProc(socket_adapter, sockets, nullptr); + } + } + } + else + { + Socket::AdapterDefault socket_adapter(sock); + + this->threadRequestProc(socket_adapter, sockets, stream); + } + + --this->threads_working_count; + } + } + } + + /** + * Цикл управления количеством рабочих потоков + */ + int Server::cycleQueue(SocketsQueue &sockets) + { + auto const it_option = this->settings.global.find("threads_max_count"); + + size_t threads_max_count = 0; + + if (this->settings.global.cend() != it_option) + { + const std::string &option = it_option->second; + + threads_max_count = std::strtoull(option.c_str(), nullptr, 10); + } + + if (0 == threads_max_count) + { + threads_max_count = std::thread::hardware_concurrency(); + + if (0 == threads_max_count) + { + threads_max_count = 1; + } + + threads_max_count *= 2; + } + + this->threads_working_count = 0; + + Utils::Event eventThreadCycle(false, true); + + std::function serverThreadRequestCycle + = std::mem_fn(&Server::threadRequestCycle); + + std::vector active_threads; + active_threads.reserve(threads_max_count); + + // For update applications modules + do + { + if (this->controls.eventUpdateModule->notifed() ) + { + updateModules(); + } + + // Cycle creation threads applications requests + do + { + while (this->threads_working_count == active_threads.size() && active_threads.size() < threads_max_count && sockets.empty() == false) + { + active_threads.emplace_back(serverThreadRequestCycle, this, std::ref(sockets), std::ref(eventThreadCycle) ); + } + + size_t notify_count = active_threads.size() - this->threads_working_count; + + if (notify_count > sockets.size() ) + { + notify_count = sockets.size(); + } + + eventThreadCycle.notify(notify_count); + + this->controls.eventProcessQueue->wait(); + } + while (this->controls.process_flag); + + // Data clear + + eventThreadCycle.notify(); + + if (false == active_threads.empty() ) + { + // Join threads (wait completion) + for (auto &th : active_threads) + { + th.join(); + } + + active_threads.clear(); + } + + this->controls.eventNotFullQueue->notify(); + } + while (this->controls.eventUpdateModule->notifed() ); + + return 0; + } + + bool Server::updateModule(System::Module &module, std::unordered_set &applications, const size_t moduleIndex) + { + std::unordered_set same; + + for (auto &app : applications) + { + if (app->module_index == moduleIndex) + { + same.emplace(app); + + try + { + if (app->application_final) + { + app->application_final(); + } + } + catch (std::exception &exc) + { + std::cout << "Warning: error when the application finishes '" << app->server_module << "':" << exc.what() << std::endl; + } + + app->application_call = std::function(); + app->application_clear = std::function(); + app->application_init = std::function(); + app->application_final = std::function(); + } + } + + module.close(); + + auto const app = *(same.cbegin() ); + + const std::string &module_name = app->server_module; + + #ifdef WIN32 + std::ifstream src(app->server_module_update, std::ifstream::binary); + + if ( ! src) + { + std::cout << "Error: file '" << app->server_module_update << "' cannot be open;" << std::endl; + return false; + } + + std::ofstream dst(module_name, std::ofstream::binary | std::ofstream::trunc); + + if ( ! dst) + { + std::cout << "Error: file '" << module_name << "' cannot be open;" << std::endl; + return false; + } + + // Copy (rewrite) file + dst << src.rdbuf(); + + src.close(); + dst.close(); + + // Open updated module + module.open(module_name); + + #elif POSIX + // HACK: for posix system — load new version shared library + + const size_t dir_pos = module_name.rfind('/'); + const size_t ext_pos = module_name.rfind('.'); + + std::string module_name_temp; + + if (std::string::npos != ext_pos && (std::string::npos == dir_pos || dir_pos < ext_pos) ) + { + module_name_temp = module_name.substr(0, ext_pos) + '-' + Utils::getUniqueName() + module_name.substr(ext_pos); + } + else + { + module_name_temp = module_name + '-' + Utils::getUniqueName(); + } + + std::ifstream src(app->server_module_update, std::ifstream::binary); + + if ( ! src) + { + std::cout << "Error: file '" << app->server_module_update << "' cannot be open;" << std::endl; + return false; + } + + std::ofstream dst(module_name_temp, std::ofstream::binary | std::ofstream::trunc); + + if ( ! dst) + { + std::cout << "Error: file '" << module_name << "' cannot be open;" << std::endl; + return false; + } + + // Copy (rewrite) file + dst << src.rdbuf(); + + src.close(); + dst.close(); + + // Open updated module + module.open(module_name_temp); + + if (0 != std::remove(module_name.c_str() ) ) + { + std::cout << "Error: file '" << module_name << "' could not be removed;" << std::endl; + return false; + } + + if (0 != std::rename(module_name_temp.c_str(), module_name.c_str() ) ) + { + std::cout << "Error: file '" << module_name_temp << "' could not be renamed;" << std::endl; + return false; + } + #else + #error "Undefined platform" + #endif + + if (false == module.is_open() ) + { + std::cout << "Error: the application module '" << module_name << "' can not be opened;" << std::endl; + return false; + } + + void *(*addr)(void *) = nullptr; + + if (false == module.find("application_call", &addr) ) + { + std::cout << "Error: function 'application_call' was not found in the module '" << module_name << "';" << std::endl; + return false; + } + + std::function app_call = reinterpret_cast(addr); + + if ( ! app_call) + { + std::cout << "Error: invalid function 'application_call' is in the module '" << module_name << "';" << std::endl; + return false; + } + + if (false == module.find("application_clear", &addr) ) + { + std::cout << "Error: function 'application_clear' was not found in the module '" << module_name << "';" << std::endl; + return false; + } + + std::function app_clear = reinterpret_cast(addr); + + std::function app_init = std::function(); + + if (module.find("application_init", &addr) ) + { + app_init = reinterpret_cast(addr); + } + + std::function app_final = std::function(); + + if (module.find("application_final", &addr) ) + { + app_final = reinterpret_cast(addr); + } + + for (auto &app : same) + { + app->application_call = app_call; + app->application_clear = app_clear; + app->application_init = app_init; + app->application_final = app_final; + + try + { + if (app->application_init) + { + app->application_init(); + } + } + catch (std::exception &exc) + { + std::cout << "Warning: error when initializing the application '" << module_name << "':" << exc.what() << std::endl; + } + } + + return true; + } + + void Server::updateModules() + { + // Applications settings list + std::unordered_set applications; + // Get full applications settings list + this->settings.apps_tree.collectApplicationSettings(applications); + + std::unordered_set updated; + + for (auto const &app : applications) + { + const size_t module_index = app->module_index; + + // If module is not updated (not checked) + if (updated.cend() == updated.find(module_index) ) + { + if (false == app->server_module_update.empty() && app->server_module_update != app->server_module) + { + size_t module_size_new = 0; + time_t module_time_new = 0; + + if (System::getFileSizeAndTimeGmt(app->server_module_update, &module_size_new, &module_time_new) ) + { + size_t module_size_cur = 0; + time_t module_time_cur = 0; + + System::Module &module = this->modules[module_index]; + + if (System::getFileSizeAndTimeGmt(app->server_module, &module_size_cur, &module_time_cur) ) + { + if (module_size_cur != module_size_new || module_time_cur < module_time_new) + { + this->updateModule(module, applications, module_index); + } + } + } + } + + updated.emplace(module_index); + } + } + + std::cout << "Notice: applications' modules have been updated;" << std::endl; + + this->controls.setProcess(); + this->controls.eventUpdateModule->reset(); + } + + bool Server::init() + { + ConfigParser conf_parser; + + if (Socket::Socket::Startup() && conf_parser.loadConfig("main.conf", this->settings, this->modules) ) + { + this->settings.addDataVariant(new DataVariant::FormUrlencoded() ); + this->settings.addDataVariant(new DataVariant::MultipartFormData() ); + this->settings.addDataVariant(new DataVariant::TextPlain() ); + + ::gnutls_global_init(); + + return true; + } + + return false; + } + + void Server::clear() + { + this->controls.clear(); + + if (false == this->tls_data.empty() ) + { + for (auto &pair : this->tls_data) + { + std::tuple &data = pair.second; + + ::gnutls_certificate_free_credentials(std::get(data) ); + ::gnutls_priority_deinit(std::get(data) ); + } + } + + this->settings.clear(); + + if (false == this->modules.empty() ) + { + for (auto &module : this->modules) + { + module.close(); + } + + this->modules.empty(); + } + + ::gnutls_global_deinit(); + + Socket::Socket::Cleanup(); + } + + static bool tlsInit(const ServerApplicationSettings &app, std::tuple &data) + { + ::gnutls_certificate_credentials_t x509_cred; + + int ret = ::gnutls_certificate_allocate_credentials(&x509_cred); + + if (ret < 0) + { + std::cout << "Error [tls]: certificate credentials has not been allocated;" << std::endl; + + return false; + } + + if (false == app.chain_file.empty() ) + { + ret = ::gnutls_certificate_set_x509_trust_file(x509_cred, app.chain_file.c_str(), GNUTLS_X509_FMT_PEM); + + if (ret < 0) + { + std::cout << "Warning [tls]: (CA) chain file has not been accepted;" << std::endl; + } + } + + if (false == app.crl_file.empty() ) + { + ret = ::gnutls_certificate_set_x509_crl_file(x509_cred, app.crl_file.c_str(), GNUTLS_X509_FMT_PEM); + + if (ret < 0) + { + std::cout << "Warning [tls]: (CLR) clr file has not been accepted;" << std::endl; + } + } + + ret = ::gnutls_certificate_set_x509_key_file(x509_cred, app.cert_file.c_str(), app.key_file.c_str(), GNUTLS_X509_FMT_PEM); + + if (ret < 0) + { + std::cout << "Error [tls]: (CERT) cert file or/and (KEY) key file has not been accepted;" << std::endl; + + return false; + } + + if (false == app.stapling_file.empty() ) + { + ret = ::gnutls_certificate_set_ocsp_status_request_file(x509_cred, app.stapling_file.c_str(), 0); + + if (ret < 0) + { + std::cout << "Warning [tls]: (OCSP) stapling file has not been accepted;" << std::endl; + } + } + + ::gnutls_dh_params_t dh_params; + + ::gnutls_dh_params_init(&dh_params); + + if (app.dh_file.empty() ) + { + const unsigned int bits = ::gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_HIGH); + + ret = ::gnutls_dh_params_generate2(dh_params, bits); + } + else + { + std::ifstream dh_file(app.dh_file); + + if (dh_file) + { + const size_t max_file_size = 1024 * 1024; + + std::vector buf(max_file_size); + + dh_file.read(buf.data(), buf.size() ); + + gnutls_datum_t datum { + reinterpret_cast(buf.data() ), + static_cast(dh_file.gcount() ) + }; + + ret = ::gnutls_dh_params_import_pkcs3(dh_params, &datum, GNUTLS_X509_FMT_PEM); + } + else + { + ret = -1; + + std::cout << "Error [tls]: DH params file has not been opened;" << std::endl;; + } + + dh_file.close(); + } + + if (ret < 0) + { + ::gnutls_certificate_free_credentials(x509_cred); + + std::cout << "Error [tls]: DH params were not loaded;" << std::endl; + + return false; + } + + ::gnutls_certificate_set_dh_params(x509_cred, dh_params); + + ::gnutls_priority_t priority_cache; + + ret = ::gnutls_priority_init(&priority_cache, "NORMAL", nullptr); + + if (ret < 0) + { + ::gnutls_certificate_free_credentials(x509_cred); + + std::cout << "Error [tls]: priority cache cannot be init;" << std::endl; + + return false; + } + + data = std::tuple { x509_cred, priority_cache }; + + return true; + } + + bool Server::tryBindPort(const int port, std::unordered_set &ports) + { + // Only unique ports + if (ports.cend() != ports.find(port) ) + { + return false; + } + + Socket::Socket sock; + + if (false == sock.open() ) + { + std::cout << "Error: socket cannot be open; errno " << Socket::Socket::getLastError() << ";" << std::endl; + return false; + } + + if (false == sock.bind(port) ) + { + std::cout << "Error: port " << port << " cannot be bind; errno " << Socket::Socket::getLastError() << ";" << std::endl; + return false; + } + + if (false == sock.listen() ) + { + std::cout << "Error: socket " << port << " cannot be listen; errno " << Socket::Socket::getLastError() << ";" << std::endl; + return false; + } + + sock.nonblock(true); + + this->liseners.emplace_back(std::move(sock) ); + + ports.emplace(port); + + return true; + } + + void Server::initAppsPorts() + { + // Applications settings list + std::unordered_set applications; + // Get full applications settings list + this->settings.apps_tree.collectApplicationSettings(applications); + + // Bind ports set + std::unordered_set ports; + + // Open applications sockets + for (auto const &app : applications) + { + const std::unordered_set &tls = app->tls_ports; + + if (false == tls.empty() ) + { + std::tuple data; + + if (tlsInit(*app, data) ) + { + for (const int port : tls) + { + if (this->tryBindPort(port, ports) ) + { + this->tls_data.emplace(port, data); + } + } + } + } + + const std::unordered_set &list = app->ports; + + for (const int port : list) + { + this->tryBindPort(port, ports); + } + } + } + + int Server::run() + { + if (false == this->init() ) + { + return 0x10; + } + + this->initAppsPorts(); + + if (this->liseners.empty() ) + { + std::cout << "Error: any socket was not open;" << std::endl; + this->clear(); + return 0x20; + } + + Socket::List sockets_list; + + sockets_list.create(this->liseners.size() ); + + for (auto const &sock : this->liseners) + { + sockets_list.addSocket(sock); + } + + std::cout << "Log: launch server's cycle;" << std::endl << std::endl; + + constexpr size_t queue_max_length = 1024; + this->controls.eventNotFullQueue = new Utils::Event(true, true); + this->controls.eventProcessQueue = new Utils::Event(); + this->controls.eventUpdateModule = new Utils::Event(false, true); + + SocketsQueue sockets; + + this->controls.setProcess(); + + std::function serverCycleQueue = std::mem_fn(&Server::cycleQueue); + std::thread threadQueue(serverCycleQueue, this, std::ref(sockets) ); + + std::vector accept_sockets; + + // Cycle for receiving new connections + do + { + if (sockets_list.accept(accept_sockets) ) + { + sockets.lock(); + + for (size_t i = 0; i < accept_sockets.size(); ++i) + { + const Socket::Socket &sock = accept_sockets[i]; + + if (sock.is_open() ) + { + sock.nonblock(true); + sock.tcp_nodelay(true); + + sockets.emplace( + std::tuple { + sock, + nullptr + } + ); + } + } + + sockets.unlock(); + + this->controls.eventProcessQueue->notify(); + + if (sockets.size() >= queue_max_length) + { + this->controls.eventNotFullQueue->reset(); + } + + accept_sockets.clear(); + + this->controls.eventNotFullQueue->wait(); + } + } + while (this->controls.process_flag || this->controls.eventUpdateModule->notifed() ); + + this->controls.eventProcessQueue->notify(); + + threadQueue.join(); + + sockets_list.destroy(); + + if (false == this->liseners.empty() ) + { + for (Socket::Socket &sock : this->liseners) + { + sock.close(); + } + + this->liseners.clear(); + } + + this->clear(); + + std::cout << "Log: complete server's cycle;" << std::endl; + + return EXIT_SUCCESS; + } + + bool Server::get_start_args(const int argc, const char *argv[], struct server_start_args *st) + { + for (int i = 1; i < argc; ++i) + { + if (0 == ::strcmp(argv[i], "--start") ) + { + + } + else if (0 == ::strcmp(argv[i], "--force") ) + { + st->force = true; + } + else if (argv[i] == ::strstr(argv[i], "--config-path=") ) + { + st->config_path = std::string(argv[i] + sizeof("--config-path=") - 1); + } + else if (argv[i] == ::strstr(argv[i], "--server-name=") ) + { + st->server_name = std::string(argv[i] + sizeof("--server-name=") - 1); + } + else + { + std::cout << "Argument '" << argv[i] << "' can't be applied with --start;" << std::endl; + + return false; + } + } + + if (st->server_name.empty() ) + { + st->server_name = argv[0]; + } + + System::filterSharedMemoryName(st->server_name); + + return true; + } + + int Server::command_start(const int argc, const char *argv[]) + { + struct server_start_args st = {}; + + if (false == Server::get_start_args(argc, argv, &st) ) + { + return 0x1; + } + + if (false == st.config_path.empty() ) + { + if (false == System::changeCurrentDirectory(st.config_path) ) + { + std::cout << "Configuration path '" << st.config_path << "' has not been found;" << std::endl; + + return 0x2; + } + } + + if (st.force) + { + System::SharedMemory::destroy(st.server_name); + System::GlobalMutex::destory(st.server_name); + } + + System::GlobalMutex glob_mtx; + System::SharedMemory glob_mem; + + bool is_exists = false; + + if (glob_mtx.open(st.server_name) ) + { + glob_mtx.lock(); + + if (glob_mem.open(st.server_name) ) + { + System::native_processid_type pid = 0; + + if (glob_mem.read(&pid, sizeof(pid) ) ) + { + is_exists = System::isProcessExists(pid); + } + } + + glob_mtx.unlock(); + } + + if (is_exists) + { + std::cout << "Server instance with the name '" << st.server_name << "' is already running;" << std::endl; + + return 0x3; + } + + if (false == glob_mtx.open(st.server_name) ) + { + if (false == glob_mtx.create(st.server_name) ) + { + std::cout << "Global mutex could not been created;" << std::endl; + + return 0x4; + } + } + + glob_mtx.lock(); + + if (false == glob_mem.open(st.server_name) ) + { + if (false == glob_mem.create(st.server_name, sizeof(System::native_processid_type) ) ) + { + glob_mtx.unlock(); + + std::cout << "Shared memory could not been allocated;" << std::endl; + + return 0x5; + } + } + + System::native_processid_type pid = System::getProcessId(); + + if (false == glob_mem.write(&pid, sizeof(pid) ) ) + { + glob_mem.destroy(); + glob_mtx.unlock(); + + std::cout << "Writing data to shared memory has failed;" << std::endl; + + return 0x6; + } + + glob_mtx.unlock(); + + int code = EXIT_FAILURE; + + do + { + this->controls.setProcess(false); + this->controls.setRestart(false); + + code = this->run(); + } + while (this->controls.process_flag || this->controls.restart_flag); + + glob_mem.destroy(); + glob_mtx.destory(); + + return code; + } + + System::native_processid_type Server::getServerProcessId(const std::string &serverName) + { + System::native_processid_type pid = 0; + + System::GlobalMutex glob_mtx; + + if (glob_mtx.open(serverName) ) + { + System::SharedMemory glob_mem; + + glob_mtx.lock(); + + if (glob_mem.open(serverName) ) + { + glob_mem.read(&pid, sizeof(pid) ); + } + + glob_mtx.unlock(); + } + + return pid; + } + + int Server::command_help(const int argc, const char *argv[]) const + { + std::cout << std::left << "Available arguments:" << std::endl + << std::setw(4) << ' ' << std::setw(26) << "--start" << "Start http server" << std::endl + << std::setw(8) << ' ' << std::setw(22) << "[options]" << std::endl + << std::setw(8) << ' ' << std::setw(22) << "--force" << "Forcibly start http server (ignore existing instance)" << std::endl + << std::setw(8) << ' ' << std::setw(22) << "--config-path=" << "Path to directory with configuration files" << std::endl + << std::endl + << std::setw(4) << ' ' << std::setw(26) << "--restart" << "Restart http server" << std::endl + << std::setw(4) << ' ' << std::setw(26) << "--update-module" << "Update applications modules" << std::endl + << std::setw(4) << ' ' << std::setw(26) << "--kill" << "Shutdown http server" << std::endl + << std::setw(4) << ' ' << std::setw(26) << "--help" << "This help" << std::endl + << std::endl<< "Optional arguments:" << std::endl + << std::setw(4) << ' ' << std::setw(26) << "--server-name=" << "Name of server instance" << std::endl; + + return EXIT_SUCCESS; + } + + static std::string get_server_name(const int argc, const char *argv[]) + { + std::string server_name; + + for (int i = 1; i < argc; ++i) + { + if (argv[i] == ::strstr(argv[i], "--server-name=") ) + { + server_name = std::string(argv[i] + sizeof("--server-name=") - 1); + break; + } + } + + if (server_name.empty() ) + { + server_name = argv[0]; + } + + System::filterSharedMemoryName(server_name); + + return server_name; + } + + int Server::command_restart(const int argc, const char *argv[]) const + { + const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); + + if (1 < pid && System::sendSignal(pid, SIGUSR1) ) + { + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; + } + + int Server::command_terminate(const int argc, const char *argv[]) const + { + const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); + + if (1 < pid && System::sendSignal(pid, SIGTERM) ) + { + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; + } + + int Server::command_update_module(const int argc, const char *argv[]) const + { + const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); + + if (1 < pid && System::sendSignal(pid, SIGUSR2) ) + { + return EXIT_SUCCESS; + } + + return EXIT_FAILURE; + } +}; diff --git a/src/server/Server.h b/src/server/Server.h new file mode 100644 index 0000000..4218ad8 --- /dev/null +++ b/src/server/Server.h @@ -0,0 +1,62 @@ +#pragma once + +#include "../socket/Adapter.h" +#include "../system/Module.h" + +#include "ServerControls.h" +#include "ServerSettings.h" +#include "SocketsQueue.h" + +#include + +namespace HttpServer +{ + class Server + { + protected: + ServerSettings settings; + + std::unordered_map > tls_data; + // std::unordered_map > > http_streams; + + std::vector modules; + std::vector liseners; + + mutable std::atomic_size_t threads_working_count; + System::CachePadding padding_1; + + public: + mutable ServerControls controls; + + protected: + int cycleQueue(SocketsQueue &sockets); + + void threadRequestProc(Socket::Adapter &sock, SocketsQueue &sockets, Http2::IncStream *stream) const; + + void threadRequestCycle(SocketsQueue &sockets, Utils::Event &eventThreadCycle) const; + + bool tryBindPort(const int port, std::unordered_set &ports); + void initAppsPorts(); + + bool init(); + int run(); + void clear(); + + static System::native_processid_type getServerProcessId(const std::string &serverName); + + void updateModules(); + bool updateModule(System::Module &module, std::unordered_set &applications, const size_t moduleIndex); + + private: + static bool get_start_args(const int argc, const char *argv[], struct server_start_args *st); + + public: + Server() = default; + + int command_help(const int argc, const char *argv[]) const; + int command_start(const int argc, const char *argv[]); + int command_restart(const int argc, const char *argv[]) const; + int command_terminate(const int argc, const char *argv[]) const; + int command_update_module(const int argc, const char *argv[]) const; + }; +}; diff --git a/src/ServerApplicationSettings.h b/src/server/ServerApplicationSettings.h similarity index 73% rename from src/ServerApplicationSettings.h rename to src/server/ServerApplicationSettings.h index 7d61acc..7d2cb38 100644 --- a/src/ServerApplicationSettings.h +++ b/src/server/ServerApplicationSettings.h @@ -1,8 +1,7 @@ #pragma once -#include "ServerRequest.h" -#include "ServerResponse.h" -#include "RawData.h" +#include "../transfer/AppRequest.h" +#include "../transfer/AppResponse.h" #include #include @@ -30,9 +29,9 @@ namespace HttpServer std::string stapling_file; std::string dh_file; - std::function application_call; - std::function application_clear; + std::function application_call; + std::function application_clear; std::function application_init; std::function application_final; }; -}; \ No newline at end of file +}; diff --git a/src/ServerApplicationsTree.cpp b/src/server/ServerApplicationsTree.cpp similarity index 75% rename from src/ServerApplicationsTree.cpp rename to src/server/ServerApplicationsTree.cpp index e548925..250e432 100644 --- a/src/ServerApplicationsTree.cpp +++ b/src/server/ServerApplicationsTree.cpp @@ -3,14 +3,19 @@ namespace HttpServer { - ServerApplicationsTree::ServerApplicationsTree(): app_sets(nullptr) + ServerApplicationsTree::ServerApplicationsTree() noexcept : app_sets(nullptr) { } - ServerApplicationsTree::~ServerApplicationsTree() + ServerApplicationsTree::~ServerApplicationsTree() noexcept { - clear(); + this->clear(); + } + + bool ServerApplicationsTree::empty() const noexcept + { + return this->list.empty(); } void ServerApplicationsTree::addApplication(const std::string &name, ServerApplicationSettings *sets) @@ -46,31 +51,31 @@ namespace HttpServer name_parts.emplace_back(name); } - addApplication(name_parts, sets); + this->addApplication(name_parts, sets); } void ServerApplicationsTree::addApplication(std::vector &nameParts, ServerApplicationSettings *sets) { if (nameParts.empty() ) { - app_sets = sets; + this->app_sets = sets; } else { std::string &part = nameParts.back(); - auto it = list.find(part); + auto it = this->list.find(part); ServerApplicationsTree *sub; - if (list.cend() != it) + if (this->list.cend() != it) { sub = it->second; } else { sub = new ServerApplicationsTree(); - list.emplace(std::move(part), sub); + this->list.emplace(std::move(part), sub); } nameParts.pop_back(); @@ -105,14 +110,14 @@ namespace HttpServer name_parts.emplace_back(name); } - return find(name_parts); + return this->find(name_parts); } const ServerApplicationSettings *ServerApplicationsTree::find(std::vector &nameParts) const { if (nameParts.empty() ) { - return app_sets; + return this->app_sets; } else { @@ -120,15 +125,15 @@ namespace HttpServer nameParts.pop_back(); - auto it = list.find(part); + auto it = this->list.find(part); - if (list.cend() == it) + if (this->list.cend() == it) { - it = list.find("*"); + it = this->list.find("*"); - if (list.end() != it) + if (this->list.end() != it) { - return app_sets; + return this->app_sets; } } else @@ -142,7 +147,7 @@ namespace HttpServer void ServerApplicationsTree::collectApplicationSettings(std::unordered_set &set) const { - for (auto &node : list) + for (auto &node : this->list) { const ServerApplicationsTree *tree = node.second; @@ -155,16 +160,16 @@ namespace HttpServer } } - void ServerApplicationsTree::clear() + void ServerApplicationsTree::clear() noexcept { - if (false == list.empty() ) + if (false == this->list.empty() ) { - for (auto &it : list) + for (auto &it : this->list) { delete it.second; } - list.clear(); + this->list.clear(); } } -}; \ No newline at end of file +}; diff --git a/src/ServerApplicationsTree.h b/src/server/ServerApplicationsTree.h similarity index 84% rename from src/ServerApplicationsTree.h rename to src/server/ServerApplicationsTree.h index 84ce430..6fe2b24 100644 --- a/src/ServerApplicationsTree.h +++ b/src/server/ServerApplicationsTree.h @@ -16,10 +16,10 @@ namespace HttpServer ServerApplicationSettings *app_sets; public: - ServerApplicationsTree(); - ~ServerApplicationsTree(); + ServerApplicationsTree() noexcept; + ~ServerApplicationsTree() noexcept; - inline bool empty() const { return list.empty(); } + bool empty() const noexcept; void addApplication(const std::string &name, ServerApplicationSettings *sets); void addApplication(std::vector &nameParts, ServerApplicationSettings *sets); @@ -29,6 +29,6 @@ namespace HttpServer void collectApplicationSettings(std::unordered_set &set) const; - void clear(); + void clear() noexcept; }; -}; \ No newline at end of file +}; diff --git a/src/server/ServerControls.cpp b/src/server/ServerControls.cpp new file mode 100644 index 0000000..6bc57ed --- /dev/null +++ b/src/server/ServerControls.cpp @@ -0,0 +1,75 @@ + +#include "ServerControls.h" + +namespace HttpServer +{ + ServerControls::ServerControls() + : eventProcessQueue(nullptr), eventNotFullQueue(nullptr), eventUpdateModule(nullptr) + { + + } + + ServerControls::~ServerControls() + { + this->clear(); + } + + void ServerControls::clear() + { + if (this->eventNotFullQueue) + { + delete this->eventNotFullQueue; + this->eventNotFullQueue = nullptr; + } + + if (this->eventProcessQueue) + { + delete this->eventProcessQueue; + this->eventProcessQueue = nullptr; + } + + if (this->eventUpdateModule) + { + delete this->eventUpdateModule; + this->eventUpdateModule = nullptr; + } + } + + void ServerControls::setProcess(const bool flag) + { + this->process_flag = flag; + } + + void ServerControls::stopProcess() + { + this->process_flag = false; + + if (this->eventNotFullQueue) + { + this->eventNotFullQueue->notify(); + } + + this->setProcessQueue(); + } + + void ServerControls::setRestart(const bool flag) + { + this->restart_flag = flag; + } + + void ServerControls::setUpdateModule() + { + if (this->eventUpdateModule) + { + this->eventUpdateModule->notify(); + } + } + + void ServerControls::setProcessQueue() + { + if (this->eventProcessQueue) + { + this->eventProcessQueue->notify(); + } + } +}; diff --git a/src/server/ServerControls.h b/src/server/ServerControls.h new file mode 100644 index 0000000..ab811f0 --- /dev/null +++ b/src/server/ServerControls.h @@ -0,0 +1,37 @@ +#pragma once + +#include "../utils/Event.h" +#include "../system/Cache.h" + +#include +#include + +namespace HttpServer +{ + class ServerControls + { + public: + Utils::Event *eventProcessQueue; + Utils::Event *eventNotFullQueue; + Utils::Event *eventUpdateModule; + + System::CachePaddingSize padding_1; + + // Флаг, означающий - активированы ли главные циклы сервера + // (с помощью этого флага можно деактивировать циклы, чтобы завершить работу сервера) + sig_atomic_t process_flag; + sig_atomic_t restart_flag; + + public: + ServerControls(); + ~ServerControls(); + + void clear(); + + void setProcess(const bool flag = true); + void stopProcess(); + void setRestart(const bool flag = true); + void setUpdateModule(); + void setProcessQueue(); + }; +}; diff --git a/src/server/ServerSettings.cpp b/src/server/ServerSettings.cpp new file mode 100644 index 0000000..7890fb6 --- /dev/null +++ b/src/server/ServerSettings.cpp @@ -0,0 +1,61 @@ + +#include "ServerSettings.h" + +#include + +namespace HttpServer +{ + ServerSettings::~ServerSettings() + { + this->clear(); + } + + void ServerSettings::addDataVariant(DataVariant::Abstract *dataVariant) + { + this->variants.emplace(dataVariant->getName(), dataVariant); + } + + void ServerSettings::clear() + { + if (false == this->variants.empty() ) + { + for (auto &variant : this->variants) + { + delete variant.second; + } + + this->variants.clear(); + } + + if (false == this->apps_tree.empty() ) + { + std::unordered_set applications; + this->apps_tree.collectApplicationSettings(applications); + + for (auto &app : applications) + { + try + { + if (app->application_final) + { + app->application_final(); + } + } + catch (std::exception &exc) + { + std::cout << "Warning: the error of the application's finalize '" << app->server_module << "':" << exc.what() << std::endl; + } + + delete app; + } + + applications.clear(); + this->apps_tree.clear(); + } + + if (false == this->global.empty() ) + { + this->global.clear(); + } + } +}; diff --git a/src/server/ServerSettings.h b/src/server/ServerSettings.h new file mode 100644 index 0000000..2ef64a2 --- /dev/null +++ b/src/server/ServerSettings.h @@ -0,0 +1,25 @@ +#pragma once + +#include "data-variant/Abstract.h" + +#include "ServerApplicationsTree.h" + +namespace HttpServer +{ + class ServerSettings + { + public: + std::unordered_map global; + std::unordered_map variants; + std::unordered_map mimes_types; + ServerApplicationsTree apps_tree; + + public: + ServerSettings() = default; + ~ServerSettings(); + + void addDataVariant(DataVariant::Abstract *dataVariant); + + void clear(); + }; +}; diff --git a/src/ServerStructuresArguments.h b/src/server/ServerStructuresArguments.h similarity index 98% rename from src/ServerStructuresArguments.h rename to src/server/ServerStructuresArguments.h index 6877ab0..0e10b8b 100644 --- a/src/ServerStructuresArguments.h +++ b/src/server/ServerStructuresArguments.h @@ -10,4 +10,4 @@ namespace HttpServer std::string config_path; bool force; }; -}; \ No newline at end of file +}; diff --git a/src/server/SocketsQueue.h b/src/server/SocketsQueue.h new file mode 100644 index 0000000..ff6ec83 --- /dev/null +++ b/src/server/SocketsQueue.h @@ -0,0 +1,20 @@ +#pragma once + +#include "../socket/Socket.h" +#include "../system/Cache.h" + +#include "../transfer/http2/Http2.h" + +#include +#include + +namespace HttpServer +{ + class SocketsQueue : + public std::queue >, + System::CachePadding > >, + public std::mutex + { + + }; +}; diff --git a/src/ConfigParser.cpp b/src/server/config/ConfigParser.cpp similarity index 90% rename from src/ConfigParser.cpp rename to src/server/config/ConfigParser.cpp index 7f65321..0767cd8 100644 --- a/src/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -1,8 +1,8 @@ #include "ConfigParser.h" -#include "ServerApplicationSettings.h" -#include "Utils.h" +#include "../ServerApplicationSettings.h" +#include "../../utils/Utils.h" #include #include @@ -20,7 +20,7 @@ namespace HttpServer { file.close(); - std::cout << "Error: " << fileName << " - cannot be open;" << std::endl; + std::cout << "Error: file " << fileName << " cannot be open;" << std::endl; return false; } @@ -29,13 +29,13 @@ namespace HttpServer std::streamsize file_size = file.tellg(); file.seekg(0, std::ifstream::beg); - const std::streamsize file_size_max = 2 * 1024 * 1024; + constexpr std::streamsize file_size_max = 2 * 1024 * 1024; if (file_size_max < file_size) { file.close(); - std::cout << "Error: " << fileName << " - is too large; max include file size = " << file_size_max << " bytes;" << std::endl; + std::cout << "Error: file " << fileName << " is too large; max include file size = " << file_size_max << " bytes;" << std::endl; return false; } @@ -59,7 +59,7 @@ namespace HttpServer bool ConfigParser::addApplication( const std::unordered_multimap &app, const ServerApplicationDefaultSettings &defaults, - std::vector &modules, + std::vector &modules, ServerApplicationsTree &apps_tree ) { @@ -212,7 +212,7 @@ namespace HttpServer // TODO: get module realpath - Module module(it_module->second); + System::Module module(it_module->second); if (false == module.is_open() ) { @@ -228,7 +228,7 @@ namespace HttpServer return false; } - std::function app_call = reinterpret_cast(addr); + std::function app_call = reinterpret_cast(addr); if ( ! app_call) { @@ -242,7 +242,7 @@ namespace HttpServer return false; } - std::function app_clear = reinterpret_cast(addr); + std::function app_clear = reinterpret_cast(addr); std::function app_init = std::function(); @@ -494,13 +494,7 @@ namespace HttpServer /** * Config - parse */ - bool ConfigParser::loadConfig( - const std::string &conf_file_name, - std::unordered_map &settings, - std::unordered_map &mimes_types, - std::vector &modules, - ServerApplicationsTree &apps_tree - ) + bool ConfigParser::loadConfig(const std::string &conf_file_name, ServerSettings &settings, std::vector &modules) { std::string str_buf; @@ -509,6 +503,10 @@ namespace HttpServer return false; } + std::unordered_map &global = settings.global; + std::unordered_map &mimes_types = settings.mimes_types; + ServerApplicationsTree &apps_tree = settings.apps_tree; + std::vector > applications; const std::string whitespace(" \t\n\v\f\r"); @@ -543,7 +541,7 @@ namespace HttpServer } else { - settings.emplace(std::move(param_name), std::move(param_value) ); + global.emplace(std::move(param_name), std::move(param_value) ); } } else // if comment line @@ -659,9 +657,9 @@ namespace HttpServer end_pos = str_buf.find(';', cur_pos); } - auto const it_mimes = settings.find("mimes"); + auto const it_mimes = global.find("mimes"); - if (settings.cend() != it_mimes) + if (global.cend() != it_mimes) { this->parseMimes(it_mimes->second, mimes_types); } @@ -672,13 +670,13 @@ namespace HttpServer if (false == applications.empty() ) { - auto const it_default_temp_dir = settings.find("default_temp_dir"); + auto const it_default_temp_dir = global.find("default_temp_dir"); - const std::string default_temp_dir = settings.cend() != it_default_temp_dir ? it_default_temp_dir->second : System::getTempDir(); + const std::string default_temp_dir = global.cend() != it_default_temp_dir ? it_default_temp_dir->second : System::getTempDir(); - auto const it_default_request_max_size = settings.find("request_max_size"); + auto const it_default_request_max_size = global.find("request_max_size"); - const size_t default_request_max_size = settings.cend() != it_default_request_max_size ? std::strtoull(it_default_request_max_size->second.c_str(), nullptr, 10) : 0; + const size_t default_request_max_size = global.cend() != it_default_request_max_size ? std::strtoull(it_default_request_max_size->second.c_str(), nullptr, 10) : 0; ServerApplicationDefaultSettings defaults { default_temp_dir, @@ -698,4 +696,4 @@ namespace HttpServer return true; } -}; \ No newline at end of file +}; diff --git a/src/ConfigParser.h b/src/server/config/ConfigParser.h similarity index 56% rename from src/ConfigParser.h rename to src/server/config/ConfigParser.h index 07af9c9..5614237 100644 --- a/src/ConfigParser.h +++ b/src/server/config/ConfigParser.h @@ -1,10 +1,8 @@ #pragma once -#include "ServerApplicationDefaultSettings.h" -#include "ServerApplicationsTree.h" -#include "Module.h" +#include "../ServerSettings.h" +#include "../../system/Module.h" -#include #include namespace HttpServer @@ -12,24 +10,24 @@ namespace HttpServer class ConfigParser { private: + struct ServerApplicationDefaultSettings + { + std::string temp_dir; + size_t request_max_size; + }; + static bool includeConfigFile(const std::string &fileName, std::string &strBuf, const size_t offset = 0); static bool addApplication( const std::unordered_multimap &app, const ServerApplicationDefaultSettings &defaults, - std::vector &modules, + std::vector &modules, ServerApplicationsTree &apps_tree ); static bool parseMimes(const std::string &fileName, std::unordered_map &mimes_types); public: - bool loadConfig( - const std::string &conf, - std::unordered_map &settings, - std::unordered_map &mimes_types, - std::vector &modules, - ServerApplicationsTree &apps_tree - ); + bool loadConfig(const std::string &conf, ServerSettings &settings, std::vector &modules); }; -}; \ No newline at end of file +}; diff --git a/src/server/data-variant/Abstract.cpp b/src/server/data-variant/Abstract.cpp new file mode 100644 index 0000000..5a13e4a --- /dev/null +++ b/src/server/data-variant/Abstract.cpp @@ -0,0 +1,20 @@ + +#include "Abstract.h" + +namespace DataVariant +{ + const std::string &Abstract::getName() const noexcept + { + return this->data_variant_name; + } + + void *Abstract::createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const + { + return nullptr; + } + + void Abstract::destroyStateStruct(void *st) const noexcept + { + + } +}; diff --git a/src/server/data-variant/Abstract.h b/src/server/data-variant/Abstract.h new file mode 100644 index 0000000..0ec2e0a --- /dev/null +++ b/src/server/data-variant/Abstract.h @@ -0,0 +1,40 @@ +#pragma once + +#include "../../socket/Adapter.h" +#include "../Request.h" + +namespace DataVariant +{ + struct DataReceiver; + + class Abstract + { + protected: + std::string data_variant_name; + + public: + const std::string &getName() const noexcept; + + public: + /** + * virtual destructor + */ + virtual ~Abstract() noexcept = default; + + virtual void *createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const; + + virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const = 0; + + virtual void destroyStateStruct(void *st) const noexcept; + }; + + struct DataReceiver + { + const Abstract *data_variant; + void *ss; + size_t full_size; + size_t recv_total; + size_t left; + void *reserved; + }; +}; diff --git a/src/server/data-variant/FormUrlencoded.cpp b/src/server/data-variant/FormUrlencoded.cpp new file mode 100644 index 0000000..5535b14 --- /dev/null +++ b/src/server/data-variant/FormUrlencoded.cpp @@ -0,0 +1,63 @@ + +#include "FormUrlencoded.h" + +#include "../../utils/Utils.h" + +namespace DataVariant +{ + FormUrlencoded::FormUrlencoded() noexcept + { + this->data_variant_name = "application/x-www-form-urlencoded"; + } + + bool FormUrlencoded::parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const + { + if (buf.empty() ) + { + return 0 == dr->full_size || dr->full_size != dr->recv_total; + } + + for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) + { + // Поиск следующего параметра + var_end = buf.find('&', var_pos); + + if (std::string::npos == var_end) + { + if (dr->full_size != dr->recv_total) + { + dr->left = buf.size() - var_pos; + + return true; + } + } + + // Поиск значения параметра + size_t delimiter = buf.find('=', var_pos); + + if (delimiter >= var_end) + { + // Получить имя параметра + std::string var_name = Utils::urlDecode(buf.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) ); + + // Сохранить параметр с пустым значением + rd->incoming_data.emplace(std::move(var_name), std::string() ); + } + else + { + // Получить имя параметра + std::string var_name = Utils::urlDecode(buf.substr(var_pos, delimiter - var_pos) ); + + ++delimiter; + + // Получить значение параметра + std::string var_value = Utils::urlDecode(buf.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) ); + + // Сохранить параметр и значение + rd->incoming_data.emplace(std::move(var_name), std::move(var_value) ); + } + } + + return true; + } +}; diff --git a/src/server/data-variant/FormUrlencoded.h b/src/server/data-variant/FormUrlencoded.h new file mode 100644 index 0000000..c52b575 --- /dev/null +++ b/src/server/data-variant/FormUrlencoded.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Abstract.h" + +namespace DataVariant +{ + class FormUrlencoded: public Abstract + { + public: + FormUrlencoded() noexcept; + + public: + virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const override; + }; +}; diff --git a/src/server/data-variant/MultipartFormData.cpp b/src/server/data-variant/MultipartFormData.cpp new file mode 100644 index 0000000..2c24504 --- /dev/null +++ b/src/server/data-variant/MultipartFormData.cpp @@ -0,0 +1,411 @@ + +#include "../../utils/Utils.h" +#include "MultipartFormData.h" + +#include + +namespace DataVariant +{ + MultipartFormData::MultipartFormData() noexcept + { + this->data_variant_name = "multipart/form-data"; + } + + enum class ParsingState : uint8_t + { + INITIALIZATION = 0, + FIND_DATA_BLOCK, + GET_DATA_BLOCK_TYPE, + COPY_DATA_BLOCK, + SAVE_DATA_BLOCK, + }; + + enum class BlockType : uint8_t + { + UNKNOWN = 0, + DATA, + FILE, + }; + + struct StateMultipartFormData + { + std::string block_name; + std::string block_value; + std::string file_tmp_name; + std::string file_name; + std::string file_type; + std::ofstream file; + std::string boundary; + ParsingState state; + BlockType block_type; + }; + + void *MultipartFormData::createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const + { + std::string boundary; + + auto const it = contentParams.find("boundary"); + + if (contentParams.cend() != it) + { + boundary = it->second; + } + + return new StateMultipartFormData { + std::string(), std::string(), std::string(), std::string(), std::string(), std::ofstream(), + std::move(boundary), ParsingState::INITIALIZATION, BlockType::UNKNOWN + }; + } + + void MultipartFormData::destroyStateStruct(void *st) const noexcept + { + delete reinterpret_cast(st); + } + + static std::unordered_map parseHeader(const std::string &buf, size_t cur, const size_t end) + { + const std::string nl("\r\n"); + + std::unordered_map headers; + + for (size_t line_end = buf.find(nl.data(), cur); cur < end; line_end = buf.find(nl.data(), cur) ) + { + size_t delimiter = buf.find(':', cur); + + if (std::string::npos == delimiter || delimiter > line_end) + { + std::string header_name = buf.substr(cur, line_end - cur); + Utils::trim(header_name); + Utils::toLower(header_name); + headers.emplace(std::move(header_name), std::string() ); + } + else + { + std::string header_name = buf.substr(cur, delimiter - cur); + Utils::trim(header_name); + Utils::toLower(header_name); + + ++delimiter; + + std::string header_value = buf.substr(delimiter, line_end - delimiter); + Utils::trim(header_value); + + headers.emplace(std::move(header_name), std::move(header_value) ); + } + + // Перейти к следующему заголовку + cur = line_end + nl.length(); + } + + return headers; + } + + static std::unordered_map parseMainHeaderParams(const std::string &header) + { + // Разобрать значение заголовка данных на параметры + std::unordered_map header_params; + + size_t delimiter = header.find(';'); + + if (std::string::npos == delimiter) + { + return header_params; + } + + std::string content_disposition(header.substr(0, delimiter) ); + Utils::trim(content_disposition); + + // Проверить соответствие указанного формата + if ("form-data" != content_disposition) + { + return header_params; + } + + // Получить параметры блока данных + for (size_t cur = delimiter + 1, end; std::string::npos != cur; cur = end) + { + end = header.find(';', cur); + delimiter = header.find('=', cur); + + if (std::string::npos == delimiter || delimiter > end) + { + std::string param_name = header.substr(cur, (std::string::npos != end) ? end - cur: std::string::npos); + Utils::trim(param_name); + Utils::toLower(param_name); + header_params.emplace(std::move(param_name), std::string() ); + } + else + { + std::string param_name = header.substr(cur, delimiter - cur); + Utils::trim(param_name); + Utils::toLower(param_name); + + ++delimiter; + + delimiter = header.find('"', delimiter); + + if (std::string::npos == delimiter) + { + end = header.find(';', cur); + + std::string param_value = header.substr(delimiter, (std::string::npos != end) ? end - delimiter : std::string::npos); + Utils::trim(param_value); + + header_params.emplace(std::move(param_name), std::move(param_value) ); + } + else + { + ++delimiter; + + cur = header.find('"', delimiter); + end = header.find(';', cur); + + std::string param_value = header.substr(delimiter, (std::string::npos != cur) ? cur - delimiter : std::string::npos); + + header_params.emplace(std::move(param_name), std::move(param_value) ); + } + } + + if (std::string::npos != end) + { + ++end; + } + } + + return header_params; + } + + bool MultipartFormData::parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const + { + StateMultipartFormData *ss = reinterpret_cast(dr->ss); + + size_t cur = 0; + + while (cur < buf.size() ) + { + switch (ss->state) + { + case ParsingState::INITIALIZATION: + { + if (ss->boundary.empty() ) + { + return false; + } + + const std::string data_end("--" + ss->boundary + "--\r\n"); + + if (buf.size() < data_end.length() ) + { + dr->left = buf.size(); + + return dr->full_size != dr->recv_total; + } + + if (0 == buf.find(data_end) ) + { + return dr->full_size == data_end.length() && dr->full_size == dr->recv_total; + } + + const std::string first_block("--" + ss->boundary); + + cur = buf.find(first_block); + + if (0 != cur) + { + return false; + } + + cur += first_block.length() + 2; + + ss->state = ParsingState::GET_DATA_BLOCK_TYPE; + + break; + } + + case ParsingState::FIND_DATA_BLOCK: + { + dr->left = buf.size() - cur; + + const std::string data_end("\r\n--" + ss->boundary + "--\r\n"); + + if (data_end.length() > dr->left) + { + return dr->full_size != dr->recv_total; + } + + const size_t end = buf.find(data_end, cur); + + if (end == cur) + { + dr->left -= data_end.length(); + + return dr->full_size == dr->recv_total; + } + + const std::string block_delimiter("\r\n--" + ss->boundary); + + cur = buf.find(block_delimiter, cur); + + if (std::string::npos == cur) + { + return dr->full_size != dr->recv_total; + } + + cur += block_delimiter.length() + 2; + + ss->state = ParsingState::GET_DATA_BLOCK_TYPE; + + // break; + } + + case ParsingState::GET_DATA_BLOCK_TYPE: + { + const size_t end = buf.find("\r\n\r\n", cur); + + if (std::string::npos == end) + { + dr->left = buf.size() - cur; + + return dr->full_size != dr->recv_total; + } + + // Разобрать заголовки блока данных + const std::unordered_map headers = parseHeader(buf, cur, end); + + // Определить параметры блока данных + auto const it = headers.find("content-disposition"); + + // Если заголовок не определён + if (headers.cend() == it) + { + return false; + } + + const std::unordered_map header_params = parseMainHeaderParams(it->second); + + // Поиск имени блока данных + auto const it_name = header_params.find("name"); + + if (header_params.cend() == it_name) + { + return false; + } + + ss->block_name = it_name->second; + + auto const it_filename = header_params.find("filename"); + + ss->block_type = header_params.cend() == it_filename ? BlockType::DATA : BlockType::FILE; + + if (BlockType::FILE == ss->block_type) + { + ss->file_name = it_filename->second; + + // Найти тип файла + auto const it_filetype = headers.find("content-type"); + + if (headers.cend() != it_filetype) + { + ss->file_type = it_filetype->second; + } + + // Сгенерировать уникальное имя + ss->file_tmp_name = System::getTempDir() + Utils::getUniqueName(); + + // Создать файл + ss->file.open(ss->file_tmp_name, std::ofstream::trunc | std::ofstream::binary); + + if (false == ss->file.is_open() ) + { + return false; + } + } + + // Перейти к данным + cur = end + 4; + + ss->state = ParsingState::COPY_DATA_BLOCK; + + // break; + } + + case ParsingState::COPY_DATA_BLOCK: + { + const std::string block_delimiter("\r\n--" + ss->boundary); + + // Поиск конца блока данных (возможное начало следующего блока) + const size_t block_end = buf.find(block_delimiter, cur); + + const size_t end = (std::string::npos == block_end) ? buf.size() - block_delimiter.length() : block_end; + + switch (ss->block_type) + { + case BlockType::DATA: + { + ss->block_value.append(buf.cbegin() + cur, buf.cbegin() + end); + + break; + } + + case BlockType::FILE: + { + ss->file.write(&buf[cur], end - cur); + + break; + } + + default: + return false; + } + + if (std::string::npos == block_end) + { + dr->left = buf.size() - end; + + return dr->full_size != dr->recv_total; + } + + cur = end; + + ss->state = ParsingState::SAVE_DATA_BLOCK; + + // break; + } + + case ParsingState::SAVE_DATA_BLOCK: + { + switch (ss->block_type) + { + case BlockType::DATA: + { + rd->incoming_data.emplace(std::move(ss->block_name), std::move(ss->block_value) ); + + break; + } + + case BlockType::FILE: + { + rd->incoming_files.emplace(std::move(ss->block_name), Transfer::FileIncoming(std::move(ss->file_tmp_name), std::move(ss->file_name), std::move(ss->file_type), ss->file.tellp() ) ); + + ss->file.close(); + + break; + } + + default: + return false; + } + + ss->state = ParsingState::FIND_DATA_BLOCK; + + break; + } + + default: + return false; + } + } + + return dr->full_size != dr->recv_total; + } +}; diff --git a/src/server/data-variant/MultipartFormData.h b/src/server/data-variant/MultipartFormData.h new file mode 100644 index 0000000..65e9071 --- /dev/null +++ b/src/server/data-variant/MultipartFormData.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Abstract.h" + +namespace DataVariant +{ + class MultipartFormData: public Abstract + { + public: + MultipartFormData() noexcept; + + public: + virtual void *createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const override; + + virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const override; + + virtual void destroyStateStruct(void *st) const noexcept override; + }; +}; diff --git a/src/server/data-variant/TextPlain.cpp b/src/server/data-variant/TextPlain.cpp new file mode 100644 index 0000000..1f4333c --- /dev/null +++ b/src/server/data-variant/TextPlain.cpp @@ -0,0 +1,61 @@ + +#include "TextPlain.h" + +namespace DataVariant +{ + TextPlain::TextPlain() noexcept + { + this->data_variant_name = "text/plain"; + } + + bool TextPlain::parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const + { + if (buf.empty() ) + { + return 0 == dr->full_size || dr->full_size != dr->recv_total; + } + + for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) + { + // Поиск следующего параметра + var_end = buf.find('&', var_pos); + + if (std::string::npos == var_end) + { + if (dr->full_size != dr->recv_total) + { + dr->left = buf.size() - var_pos; + + return true; + } + } + + // Поиск значения параметра + size_t delimiter = buf.find('=', var_pos); + + if (delimiter >= var_end) + { + // Получить имя параметра + std::string var_name = buf.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos); + + // Сохранить параметр с пустым значением + rd->incoming_data.emplace(std::move(var_name), std::string() ); + } + else + { + // Получить имя параметра + std::string var_name = buf.substr(var_pos, delimiter - var_pos); + + ++delimiter; + + // Получить значение параметра + std::string var_value = buf.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos); + + // Сохранить параметр и значение + rd->incoming_data.emplace(std::move(var_name), std::move(var_value) ); + } + } + + return true; + } +}; diff --git a/src/server/data-variant/TextPlain.h b/src/server/data-variant/TextPlain.h new file mode 100644 index 0000000..8d98766 --- /dev/null +++ b/src/server/data-variant/TextPlain.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Abstract.h" + +namespace DataVariant +{ + class TextPlain: public Abstract + { + public: + TextPlain() noexcept; + + public: + virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const override; + }; +}; diff --git a/src/server/protocol/ServerHttp1.cpp b/src/server/protocol/ServerHttp1.cpp new file mode 100644 index 0000000..c8c24c1 --- /dev/null +++ b/src/server/protocol/ServerHttp1.cpp @@ -0,0 +1,563 @@ + +#include "ServerHttp1.h" + +#include "extensions/Sendfile.h" +#include "ServerWebSocket.h" + +#include "../../utils/Utils.h" + +#include + +namespace HttpServer +{ + ServerHttp1::ServerHttp1(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept + : ServerProtocol(sock, settings, controls) + { + + } + + bool ServerHttp1::sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const + { + static const std::unordered_map status_list { + { 200, "OK" }, + { 206, "Partial Content" }, + { 304, "Not Modified" }, + { 400, "Bad Request" }, + { 404, "Not Found" }, + { 413, "Request Entity Too Large" }, + { 416, "Requested Range Not Satisfiable" }, + { 500, "Internal Server Error" }, + }; + + std::string str = "HTTP/1.1 " + std::to_string(static_cast(status) ); + + auto const it = status_list.find(static_cast(status) ); + + if (status_list.cend() != it) + { + const std::string &status = it->second; + + str += ' ' + status; + } + + str += "\r\n"; + + for (auto const &header : headers) + { + str += header.first + ": " + header.second + "\r\n"; + } + + str += "\r\n"; + + return this->sock.nonblock_send(str, timeout) > 0; // >= 0 + } + + long ServerHttp1::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const + { + long send_size = this->sock.nonblock_send(src, size, timeout); + + dt->send_total += send_size; + + return send_size; + } + + void ServerHttp1::close() + { + this->sock.close(); + } + + bool ServerHttp1::packRequestParameters(std::vector &buf, const struct Request &req, const std::string &rootDir) const + { + Utils::packNumber(buf, static_cast(Transfer::ProtocolVariant::HTTP_1) ); + Utils::packString(buf, rootDir); + Utils::packString(buf, req.host); + Utils::packString(buf, req.path); + Utils::packString(buf, req.method); + Utils::packContainer(buf, req.incoming_headers); + Utils::packContainer(buf, req.incoming_data); + Utils::packFilesIncoming(buf, req.incoming_files); + + return true; + } + + void ServerHttp1::unpackResponseParameters(struct Request &req, const void *src) const + { + Utils::unpackContainer(req.outgoing_headers, reinterpret_cast(src) ); + } + + static bool getRequest(const Socket::Adapter &sock, struct Request &req, std::vector &buf, std::string &str_buf) + { + // Получить данные запроса от клиента + const long recv_size = sock.nonblock_recv(buf, req.timeout); + + if (recv_size < 0 && str_buf.empty() ) + { + return false; + } + + if (recv_size > 0) // Если данные были получены + { + str_buf.append(buf.cbegin(), buf.cbegin() + recv_size); + } + + return true; + } + + static Http::StatusCode getRequestHeaders(struct Request &req, std::string &str_buf) + { + // Если запрос пустой + if (str_buf.empty() ) + { + return Http::StatusCode::BAD_REQUEST; + } + + // Поиск конца заголовков (пустая строка) + size_t headers_end = str_buf.find("\r\n\r\n"); + + // Если найден конец заголовков + if (std::string::npos == headers_end) + { + return Http::StatusCode::BAD_REQUEST; + } + + headers_end += 2; + + size_t str_cur = 0; + // Поиск конца первого заголовка + size_t str_end = str_buf.find("\r\n"); + + // Если не найден конец заголовка + if (std::string::npos == str_end) + { + return Http::StatusCode::BAD_REQUEST; + } + + // Установка конца строки (для поиска) + str_buf[str_end] = '\0'; + + // Разделить метод запроса и параметры запроса + size_t delimiter = str_buf.find(' ', str_cur); + + // Получить метод запроса (GET, POST, PUT, DELETE, ...) + req.method = str_buf.substr(str_cur, delimiter - str_cur); + Utils::toLower(req.method); + + // Сохранить метод и параметры запроса + // rp.incoming_headers[rp.method] = str_buf.substr(delimiter + 1, str_end - delimiter - 1); + + delimiter += 1; + // Найти окончание URI + size_t uri_end = str_buf.find(' ', delimiter); + + // Если окончание не найдено + if (std::string::npos == uri_end) + { + uri_end = str_end; + // то версия протокола HTTP - 0.9 + // const std::string version = "0.9"; + } + else // Если окончание найдено + { + str_buf[uri_end] = '\0'; + const size_t ver_beg = uri_end + 6; // Пропустить "HTTP/" + + if (ver_beg < str_end) + { + // Получить версию протокола HTTP + // const std::string version = str_buf.substr(ver_beg, str_end - ver_beg); + } + } + + // Сохранить полную ссылку URI + req.path = str_buf.substr(delimiter, uri_end - delimiter); + + // Переход к обработке следующего заголовка + str_cur = str_end + 2; + // Поиск конца заголовка + str_end = str_buf.find("\r\n", str_cur); + // Установка конца заголовка + str_buf[str_end] = '\0'; + + // Цикл извлечения заголовков запроса + for (; str_cur != headers_end; str_end = str_buf.find("\r\n", str_cur), str_buf[str_end] = '\0') + { + // Поиск разделителя названия заголовка и его значения + delimiter = str_buf.find(':', str_cur); + + // Если разделитель найден в текущей строке + if (delimiter < str_end) + { + std::string header_name = str_buf.substr(str_cur, delimiter - str_cur); + Utils::toLower(header_name); + + std::string header_value = str_buf.substr(delimiter + 1, str_end - delimiter - 1); + // Удалить лишние пробелы в начале и в конце строки + Utils::trim(header_value); + + // Сохранить заголовок и его значение + req.incoming_headers.emplace(std::move(header_name), std::move(header_value) ); + } + + // Перейти к следующей строке + str_cur = str_end + 2; + } + + str_buf.erase(str_buf.begin(), str_buf.begin() + headers_end + 2); + + return Http::StatusCode::EMPTY; + } + + const ServerApplicationSettings *ServerHttp1::getApplicationSettings(struct Request &req, const bool isSecureConnection) const + { + // Получить доменное имя (или адрес) назначения запроса + auto const it_host = req.incoming_headers.find("host"); + + // Если имя задано - продолжить обработку запроса + if (req.incoming_headers.cend() != it_host) + { + const std::string &host_header = it_host->second; + + // Поиск разделителя, за которым помещается номер порта, если указан + const size_t delimiter = host_header.find(':'); + + // Получить имя (или адрес) + req.host = host_header.substr(0, delimiter); + + const int default_port = isSecureConnection ? 443 : 80; + + // Получить номер порта + const int port = (std::string::npos != delimiter) ? std::strtol(host_header.substr(delimiter + 1).c_str(), nullptr, 10) : default_port; + + // Поиск настроек приложения по имени + const ServerApplicationSettings *app_sets = this->settings.apps_tree.find(req.host); + + // Если приложение найдено + if (app_sets && (app_sets->ports.cend() != app_sets->ports.find(port) || app_sets->tls_ports.cend() != app_sets->tls_ports.find(port) ) ) + { + return app_sets; + } + } + + return nullptr; + } + + Http::StatusCode ServerHttp1::getRequestData(struct Request &req, std::string &str_buf, const ServerApplicationSettings &appSets) const + { + // Определить вариант данных запроса (заодно проверить, есть ли данные) + auto const it = req.incoming_headers.find("content-type"); + + if (req.incoming_headers.cend() == it) + { + return Http::StatusCode::EMPTY; + } + + // Получить значение заголовка + const std::string &header_value = it->second; + + std::string data_variant_name; // Название варианта данных запроса + + std::unordered_map content_params; + + // Определить, содержит ли тип данных запроса дополнительные параметры + size_t delimiter = header_value.find(';'); + + // Если есть дополнительные параметры - извлекаем их + if (std::string::npos != delimiter) + { + data_variant_name = header_value.substr(0, delimiter); + Utils::trim(data_variant_name); + + for (size_t str_param_cur = delimiter + 1, str_param_end = 0; std::string::npos != str_param_end; str_param_cur = str_param_end + 1) + { + str_param_end = header_value.find(';', str_param_cur); + delimiter = header_value.find('=', str_param_cur); + + if (delimiter >= str_param_end) + { + std::string param_name = header_value.substr(str_param_cur, std::string::npos != str_param_end ? str_param_end - str_param_cur : std::string::npos); + Utils::trim(param_name); + content_params.emplace(std::move(param_name), std::string() ); + } + else + { + std::string param_name = header_value.substr(str_param_cur, delimiter - str_param_cur); + Utils::trim(param_name); + + ++delimiter; + + std::string param_value = header_value.substr(delimiter, std::string::npos != str_param_end ? str_param_end - delimiter : std::string::npos); + Utils::trim(param_value); + + content_params.emplace(std::move(param_name), std::move(param_value) ); + } + } + } + else + { + data_variant_name = header_value; + } + + // Поиск варианта данных по имени типа + auto const variant = this->settings.variants.find(data_variant_name); + + // Если сервер не поддерживает формат полученных данных + if (this->settings.variants.cend() == variant) + { + return Http::StatusCode::BAD_REQUEST; + } + + const DataVariant::Abstract *data_variant = variant->second; + + // Получить длину запроса в байтах + size_t data_length = 0; + + auto const it_len = req.incoming_headers.find("content-length"); + + if (req.incoming_headers.cend() != it_len) + { + data_length = std::strtoull(it_len->second.c_str(), nullptr, 10); + } + + // Если размер запроса превышает лимит (если лимит был установлен) + if (data_length > appSets.request_max_size && 0 != appSets.request_max_size) + { + return Http::StatusCode::REQUEST_ENTITY_TOO_LARGE; + } + + Transfer::request_data *rd = static_cast(&req); + + DataVariant::DataReceiver dr { + data_variant, + data_variant->createStateStruct(rd, content_params), + data_length, + 0, 0, nullptr, + }; + + std::string data_buf; + + if (data_length >= str_buf.length() ) + { + dr.recv_total = str_buf.length(); + + data_buf.swap(str_buf); + } + else + { + data_buf.assign(str_buf.cbegin(), str_buf.cbegin() + data_length); + str_buf.erase(str_buf.begin(), str_buf.begin() + data_length); + + dr.recv_total = data_buf.size(); + } + + bool result = data_variant->parse(data_buf, rd, &dr); + + while (result && dr.full_size > dr.recv_total) + { + std::vector buf(dr.full_size - dr.recv_total >= 512 * 1024 ? 512 * 1024 : dr.full_size - dr.recv_total); + + long recv_size = this->sock.nonblock_recv(buf.data(), buf.size(), req.timeout); + + if (recv_size <= 0) + { + result = false; + + break; + } + + dr.recv_total += recv_size; + + data_buf.erase(data_buf.begin(), data_buf.end() - dr.left); + data_buf.append(buf.cbegin(), buf.cbegin() + recv_size); + + dr.left = 0; + + result = data_variant->parse(data_buf, rd, &dr); + } + + data_variant->destroyStateStruct(dr.ss); + + // Разобрать данные на составляющие + if (false == result) + { + for (auto const &it : req.incoming_files) + { + std::remove(it.second.getTmpName().c_str() ); + } + + return Http::StatusCode::BAD_REQUEST; + } + + if (dr.left) + { + str_buf.assign(data_buf.cend() - dr.left, data_buf.cend() ); + } + + return Http::StatusCode::EMPTY; + } + + static void sendStatus(const Socket::Adapter &sock, const struct Request &req, const Http::StatusCode statusCode) + { + static const std::unordered_map status_list { + { 400, "Bad Request" }, + { 404, "Not Found" }, + { 413, "Request Entity Too Large" }, + }; + + auto const it = status_list.find(static_cast(statusCode) ); + + if (status_list.cend() != it) + { + const std::string &status = it->second; + + std::string headers("HTTP/1.1 " + std::to_string(static_cast(statusCode) ) + ' ' + status + "\r\n\r\n"); + + sock.nonblock_send(headers, req.timeout); + } + } + + static void getConnectionParams(struct Request &req, const bool isSecureConnection) + { + auto const it_in_connection = req.incoming_headers.find("connection"); + auto const it_out_connection = req.outgoing_headers.find("connection"); + + if (req.incoming_headers.cend() != it_in_connection && req.outgoing_headers.cend() != it_out_connection) + { + const std::string connection_in = Utils::getLowerString(it_in_connection->second); + const std::string connection_out = Utils::getLowerString(it_out_connection->second); + + auto const incoming_params = Utils::explode(connection_in, ','); + + auto const it = std::find(incoming_params.cbegin(), incoming_params.cend(), connection_out); + + if (incoming_params.cend() != it) + { + const std::string &inc = *it; + + if ("keep-alive" == inc) + { + --req.keep_alive_count; + + if (0 < req.keep_alive_count) + { + req.connection_params |= ConnectionParams::CONNECTION_REUSE; + } + } + else if ("upgrade" == inc) + { + auto const it_out_upgrade = req.outgoing_headers.find("upgrade"); + + if (req.outgoing_headers.cend() != it_out_upgrade) + { + const std::string upgrade = Utils::getLowerString(it_out_upgrade->second); + + if ("h2" == upgrade) + { + if (isSecureConnection) + { + req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; + req.connection_params |= ConnectionParams::CONNECTION_REUSE; + } + } + else if ("h2c" == upgrade) + { + if (false == isSecureConnection) + { + req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; + req.connection_params |= ConnectionParams::CONNECTION_REUSE; + } + } + else if ("websocket" == upgrade) + { + req.connection_params |= ConnectionParams::CONNECTION_LEAVE_OPEN; + } + } + } + } + } + } + + void ServerHttp1::useHttp1Protocol(struct Request &req, std::vector &buf, std::string &str_buf) const + { + if (false == getRequest(this->sock, req, buf, str_buf) ) + { + return; + } + + Http::StatusCode error_code = getRequestHeaders(req, str_buf); + + if (error_code != Http::StatusCode::EMPTY) + { + sendStatus(this->sock, req, error_code); + + return; + } + + const ServerApplicationSettings *app_sets = this->getApplicationSettings(req, this->sock.get_tls_session() != 0); + + // Если приложение не найдено + if (nullptr == app_sets) + { + sendStatus(this->sock, req, Http::StatusCode::NOT_FOUND); + + return; + } + + error_code = this->getRequestData(req, str_buf, *app_sets); + + if (error_code != Http::StatusCode::EMPTY) + { + sendStatus(this->sock, req, error_code); + + return; + } + + this->runApplication(req, *app_sets); + + for (auto const &it : req.incoming_files) + { + std::remove(it.second.getTmpName().c_str() ); + } + + if (EXIT_SUCCESS == req.app_exit_code) + { + getConnectionParams(req, this->sock.get_tls_session() != 0); + + Sendfile::xSendfile(std::ref(*this), req, this->settings.mimes_types); + } + } + + static bool isConnectionLeaveOpen(const struct Request &req) + { + return (req.connection_params & ConnectionParams::CONNECTION_LEAVE_OPEN) == ConnectionParams::CONNECTION_LEAVE_OPEN; + } + + ServerProtocol *ServerHttp1::process() + { + struct Request req; + req.timeout = std::chrono::milliseconds(5000); + req.protocol_variant = Transfer::ProtocolVariant::HTTP_1; + req.keep_alive_count = 100; + + std::vector buf(4096); + std::string str_buf; + + do + { + // Подготовить параметры для получения данных + req.connection_params = ConnectionParams::CONNECTION_CLOSE; + req.app_exit_code = EXIT_FAILURE; + + this->useHttp1Protocol(req, buf, str_buf); + + req.clear(); + } + while (Sendfile::isConnectionReuse(req) ); + + if (isConnectionLeaveOpen(req) ) + { + return new ServerWebSocket(*this); + } + + return this; + } +}; diff --git a/src/server/protocol/ServerHttp1.h b/src/server/protocol/ServerHttp1.h new file mode 100644 index 0000000..cc7f2c3 --- /dev/null +++ b/src/server/protocol/ServerHttp1.h @@ -0,0 +1,29 @@ +#pragma once + +#include "ServerProtocol.h" +#include "../../transfer/HttpStatusCode.h" + +namespace HttpServer +{ + class ServerHttp1 : public ServerProtocol + { + private: + const ServerApplicationSettings *getApplicationSettings(struct Request &rp, const bool isSecureConnection) const; + Http::StatusCode getRequestData(struct Request &rp, std::string &str_buf, const ServerApplicationSettings &appSets) const; + + protected: + void useHttp1Protocol(struct Request &rp, std::vector &buf, std::string &str_buf) const; + + public: + ServerHttp1(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept; + + virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const override; + virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const override; + + virtual bool packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const override; + virtual void unpackResponseParameters(struct Request &req, const void *src) const override; + + virtual ServerProtocol *process() override; + virtual void close() override; + }; +}; diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp new file mode 100644 index 0000000..b04fceb --- /dev/null +++ b/src/server/protocol/ServerHttp2.cpp @@ -0,0 +1,728 @@ + +#include "ServerHttp2.h" +#include "../../utils/Utils.h" +#include "../../transfer/http2/HPack.h" + +namespace HttpServer +{ + ServerHttp2::ServerHttp2(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, SocketsQueue &sockets) noexcept + : ServerHttp2Protocol(sock, settings, controls, nullptr), sockets(sockets) + { + + } + + void ServerHttp2::close() + { + this->sock.close(); + } + + static uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags, const uint32_t streamId) noexcept + { + Utils::hton24(addr, frameSize); + *(addr + 3) = static_cast(frameType); + *(addr + 4) = static_cast(frameFlags); + *reinterpret_cast(addr + 5) = ::htonl(streamId); + + return (addr + Http2::FRAME_HEADER_SIZE); + } + + static Http2::IncStream &getStreamData(std::unordered_map &streams, const uint32_t streamId, Http2::ConnectionData &conn) noexcept + { + auto it = streams.find(streamId); + + if (streams.end() != it) + { + return it->second; + } + + return streams.emplace(streamId, Http2::IncStream(streamId, conn) ).first->second; + } + + static void sendWindowUpdate(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::IncStream &stream, const uint32_t size) noexcept + { + std::array buf; + uint8_t *addr = buf.data(); + + addr = setHttp2FrameHeader(addr, sizeof(uint32_t), Http2::FrameType::WINDOW_UPDATE, Http2::FrameFlag::EMPTY, stream.stream_id); + + *reinterpret_cast(addr) = ::htonl(size); + + sock.nonblock_send(buf.data(), buf.size(), timeout); + } + + static Http2::ErrorCode parseHttp2Data(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) + { + if (0 == meta.stream_id) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + if (Http2::StreamState::OPEN != stream.state) + { + return Http2::ErrorCode::STREAM_CLOSED; + } + + if (stream.window_size_inc <= 0) + { + return Http2::ErrorCode::FLOW_CONTROL_ERROR; + } + + uint8_t padding = 0; + + if (meta.flags & Http2::FrameFlag::PADDED) + { + padding = *src; + + if (padding >= meta.length) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + src += sizeof(uint8_t); + } + + Http2::ErrorCode error_code = Http2::ErrorCode::NO_ERROR; + + if (stream.reserved) + { + Transfer::request_data *rd = static_cast(&stream); + + DataVariant::DataReceiver *dr = reinterpret_cast(stream.reserved); + + std::string &buf = *reinterpret_cast(dr->reserved); + + buf.append(src, end - padding); + + dr->recv_total += end - padding - src; + + if (dr->data_variant->parse(buf, rd, dr) ) + { + buf.erase(buf.begin(), buf.end() - dr->left); + } + else + { + error_code = Http2::ErrorCode::PROTOCOL_ERROR; + } + } + else + { + error_code = Http2::ErrorCode::PROTOCOL_ERROR; + } + + if (meta.flags & Http2::FrameFlag::END_STREAM) + { + stream.state = Http2::StreamState::HALF_CLOSED; + + ServerProtocol::destroyDataReceiver(stream.reserved); + stream.reserved = nullptr; + } + + return error_code; + } + + static Http2::ErrorCode parseHttp2Headers(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) + { + stream.state = (meta.flags & Http2::FrameFlag::END_STREAM) ? Http2::StreamState::HALF_CLOSED : Http2::StreamState::OPEN; + + uint8_t padding = 0; + + if (meta.flags & Http2::FrameFlag::PADDED) + { + padding = *src; + + if (padding >= meta.length) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + src += sizeof(uint8_t); + } + + if (meta.flags & Http2::FrameFlag::PRIORITY) + { + // Stream id + const uint32_t depend_stream_id = ::ntohl(*reinterpret_cast(src) ) & ~(1 << 31); + + src += sizeof(uint32_t); + + // Priority weight + stream.priority = *src; + + src += sizeof(uint8_t); + } + + if (false == HPack::unpack(src, end - src - padding, stream) ) + { + return Http2::ErrorCode::COMPRESSION_ERROR; + } + + return Http2::ErrorCode::NO_ERROR; + } + + static Http2::ErrorCode parseHttp2rstStream(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) + { + if (Http2::StreamState::IDLE == stream.state) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + stream.state = Http2::StreamState::CLOSED; + + if (0 == meta.stream_id) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + if (sizeof(uint32_t) != meta.length) + { + return Http2::ErrorCode::FRAME_SIZE_ERROR; + } + + const Http2::ErrorCode error_code = static_cast(::ntohl(*reinterpret_cast(src) ) ); + + if (Http2::ErrorCode::NO_ERROR != error_code) + { + // DEBUG + } + + return Http2::ErrorCode::NO_ERROR; + } + + static Http2::ErrorCode parseHttp2Settings(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) + { + if (0 != meta.stream_id) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + if (meta.length % (sizeof(uint16_t) + sizeof(uint32_t) ) ) + { + return Http2::ErrorCode::FRAME_SIZE_ERROR; + } + + if (Http2::StreamState::OPEN != stream.state) + { + stream.state = Http2::StreamState::OPEN; + } + + Http2::ConnectionSettings &settings = stream.conn.client_settings; + + while (src != end) + { + const Http2::ConnectionSetting setting = static_cast(ntohs(*reinterpret_cast(src) ) ); + + src += sizeof(uint16_t); + + const uint32_t value = ::ntohl(*reinterpret_cast(src) ); + + src += sizeof(uint32_t); + + switch (setting) + { + case Http2::ConnectionSetting::SETTINGS_HEADER_TABLE_SIZE: + settings.header_table_size = value; + break; + + case Http2::ConnectionSetting::SETTINGS_ENABLE_PUSH: + { + if (value > 1) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + settings.enable_push = value; + + break; + } + + case Http2::ConnectionSetting::SETTINGS_MAX_CONCURRENT_STREAMS: + settings.max_concurrent_streams = value; + break; + + case Http2::ConnectionSetting::SETTINGS_INITIAL_WINDOW_SIZE: + { + if (value >= uint32_t(1 << 31) ) + { + return Http2::ErrorCode::FLOW_CONTROL_ERROR; + } + + settings.initial_window_size = value; + + break; + } + + case Http2::ConnectionSetting::SETTINGS_MAX_FRAME_SIZE: + { + if (value < (1 << 14) || value >= (1 << 24) ) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + settings.max_frame_size = value; + + break; + } + + case Http2::ConnectionSetting::SETTINGS_MAX_HEADER_LIST_SIZE: + settings.max_header_list_size = value; + break; + + default: + break; + } + } + + return Http2::ErrorCode::NO_ERROR; + } + + static Http2::ErrorCode parseHttp2GoAway(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) + { + if (0 != meta.stream_id) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + stream.state = Http2::StreamState::CLOSED; + + const uint32_t last_stream_id = ::ntohl(*reinterpret_cast(src) ); + + if (last_stream_id > 0) + { + + } + + src += sizeof(uint32_t); + + const Http2::ErrorCode error_code = static_cast(::ntohl(*reinterpret_cast(src) ) ); + + if (Http2::ErrorCode::NO_ERROR != error_code) + { + + } + + return Http2::ErrorCode::NO_ERROR; + } + + static void ping(const Socket::Adapter &sock, const struct Request &req, const uint64_t pingData) + { + constexpr uint32_t frame_size = sizeof(uint64_t); + + std::array buf; + uint8_t *addr = buf.data(); + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::PING, Http2::FrameFlag::ACK, 0); + + *reinterpret_cast(addr) = pingData; + + sock.nonblock_send(buf.data(), buf.size(), req.timeout); + } + + static Http2::ErrorCode parseHttp2Ping(Http2::FrameMeta &meta) + { + if (0 != meta.stream_id) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + + if (sizeof(uint64_t) != meta.length) + { + return Http2::ErrorCode::FRAME_SIZE_ERROR; + } + + return Http2::ErrorCode::NO_ERROR; + } + + static Http2::ErrorCode parseHttp2WindowUpdate(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) + { + if (Http2::StreamState::RESERVED == stream.state) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + else if (Http2::StreamState::OPEN != stream.state) + { + return Http2::ErrorCode::NO_ERROR; + } + + if (sizeof(uint32_t) != meta.length) + { + return Http2::ErrorCode::FRAME_SIZE_ERROR; + } + + const uint32_t window_size_increment = ::ntohl(*reinterpret_cast(src) ); + + if (0 == window_size_increment) + { + return Http2::ErrorCode::PROTOCOL_ERROR; + } + else if (window_size_increment >= uint32_t(1 << 31) ) + { + return Http2::ErrorCode::FLOW_CONTROL_ERROR; + } + + if (0 == meta.stream_id) + { + // TODO: update all streams + stream.window_size_out += window_size_increment; + } + else + { + stream.window_size_out += window_size_increment; + } + + return Http2::ErrorCode::NO_ERROR; + } + + static void rstStream(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const uint32_t streamId, const Http2::ErrorCode errorCode) + { + constexpr uint32_t frame_size = sizeof(uint32_t); + + std::array buf; + uint8_t *addr = buf.data(); + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::RST_STREAM, Http2::FrameFlag::EMPTY, streamId); + + *reinterpret_cast(addr) = ::htonl(static_cast(errorCode) ); + + sock.nonblock_send(buf.data(), buf.size(), timeout); + } + + static void sendSettings(const Socket::Adapter &sock, const struct Request &req, const uint8_t *src, const uint8_t *end) + { + const uint32_t frame_size = end - src; + + std::vector buf(Http2::FRAME_HEADER_SIZE + frame_size); + + uint8_t *addr = buf.data(); + + constexpr uint32_t stream_id = 0; + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::SETTINGS, Http2::FrameFlag::EMPTY, stream_id); + + std::copy(src, end, addr); + + sock.nonblock_send(buf.data(), buf.size(), req.timeout); + } + + static void goAway(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const uint32_t lastStreamId, const Http2::ErrorCode errorCode) + { + constexpr uint32_t frame_size = sizeof(uint32_t) * 2; + + std::array buf; + + uint8_t *addr = buf.data(); + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::RST_STREAM, Http2::FrameFlag::EMPTY, 0); + + *reinterpret_cast(addr) = ::htonl(static_cast(lastStreamId) ); + *reinterpret_cast(addr + sizeof(uint32_t) ) = ::htonl(static_cast(errorCode) ); + + sock.nonblock_send(buf.data(), buf.size(), timeout); + } + + static bool getClientPreface(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout) + { + std::array buf; + + const long read_size = sock.nonblock_recv(buf.data(), buf.size(), timeout); + + if (read_size != buf.size() ) + { + return false; + } + + static constexpr char client_preface_data[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; + + const uint64_t *left = reinterpret_cast(client_preface_data); + const uint64_t *right = reinterpret_cast(buf.data() ); + + uint64_t compare = 0; + + compare |= left[0] ^ right[0]; + compare |= left[1] ^ right[1]; + compare |= left[2] ^ right[2]; + + return 0 == compare; + } + + static void sendEmptySettings(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const Http2::FrameFlag flags) + { + constexpr uint32_t frame_size = 0; + + std::array buf; + uint8_t *addr = buf.data(); + + constexpr uint32_t stream_id = 0; + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::SETTINGS, flags, stream_id); + + sock.nonblock_send(buf.data(), buf.size(), timeout); + } + + static void windowUpdate(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout) + { + constexpr uint32_t frame_size = 4; + + std::array buf; + uint8_t *addr = buf.data(); + + constexpr uint32_t stream_id = 0; + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::WINDOW_UPDATE, Http2::FrameFlag::EMPTY, stream_id); + + *reinterpret_cast(addr) = ::htonl(static_cast(1 << 16) - 1); + + sock.nonblock_send(buf.data(), buf.size(), timeout); + } + + static bool getNextHttp2FrameMeta(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, std::vector &buf, Http2::FrameMeta &meta, long &read_size) + { + if (read_size <= meta.length + Http2::FRAME_HEADER_SIZE) + { + if (read_size == meta.length + Http2::FRAME_HEADER_SIZE) + { + read_size = 0; + } + + read_size = sock.nonblock_recv(buf.data() + read_size, buf.size() - read_size, timeout); + + if (read_size < Http2::FRAME_HEADER_SIZE) + { + return false; + } + } + else + { + std::copy(buf.cbegin() + meta.length + Http2::FRAME_HEADER_SIZE, buf.cbegin() + read_size, buf.begin() ); + + read_size -= meta.length + Http2::FRAME_HEADER_SIZE; + } + + const uint8_t *addr = reinterpret_cast(buf.data() ); + + meta.length = Utils::ntoh24(addr); + meta.type = static_cast(*(addr + 3) ); + meta.flags = static_cast(*(addr + 4) ); + meta.stream_id = ::ntohl(*reinterpret_cast(addr + 5) ); + + return true; + } + + ServerProtocol *ServerHttp2::process() + { + struct Request req; + req.timeout = std::chrono::milliseconds(5000); + req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; + + sendEmptySettings(this->sock, req.timeout, Http2::FrameFlag::EMPTY); + + if (false == getClientPreface(this->sock, req.timeout) ) + { + constexpr uint32_t last_stream_id = 0; + + goAway(this->sock, req.timeout, last_stream_id, Http2::ErrorCode::PROTOCOL_ERROR); + + return this; + } + + Http2::ConnectionData conn; + + conn.client_settings = Http2::ConnectionSettings::defaultSettings(); + conn.server_settings = Http2::ConnectionSettings::defaultSettings(); + + std::vector buf(conn.server_settings.max_frame_size); + + std::unordered_map streams { + { 0, Http2::IncStream(0, conn) } + }; + + Http2::IncStream &primary = streams.find(0)->second; + this->stream = &primary; + + size_t streams_process_count = 0; + uint32_t last_stream_id = 0; + + Http2::FrameMeta meta {}; + long read_size = 0; + + do + { + if (false == getNextHttp2FrameMeta(this->sock, req.timeout, buf, meta, read_size) ) + { + break; + } + + const uint8_t *addr = reinterpret_cast(buf.data() ) + Http2::FRAME_HEADER_SIZE; + const uint8_t *end = addr + meta.length; + + if (meta.stream_id > last_stream_id) + { + last_stream_id = meta.stream_id; + } + + Http2::IncStream &stream = getStreamData(streams, meta.stream_id, conn); + + if (Http2::StreamState::CLOSED == stream.state) + { + rstStream(this->sock, req.timeout, stream.stream_id, Http2::ErrorCode::STREAM_CLOSED); + + continue; + } + + if (meta.type != Http2::FrameType::CONTINUATION) + { + stream.frame_type = meta.type; + } + + Http2::ErrorCode result = Http2::ErrorCode::NO_ERROR; + + switch (stream.frame_type) + { + case Http2::FrameType::DATA: + { + result = parseHttp2Data(meta, stream, addr, end); + + stream.window_size_inc -= meta.length; + + if (stream.reserved) + { + DataVariant::DataReceiver *dr = reinterpret_cast(stream.reserved); + + if (static_cast(stream.window_size_inc - conn.server_settings.max_frame_size) <= 0) + { + size_t update_size = conn.server_settings.initial_window_size + (dr->full_size - dr->recv_total) - stream.window_size_inc; + + if (update_size > Http2::MAX_WINDOW_UPDATE) + { + update_size = Http2::MAX_WINDOW_UPDATE; + } + + sendWindowUpdate(this->sock, req.timeout, stream, static_cast(update_size) ); + sendWindowUpdate(this->sock, req.timeout, primary, static_cast(update_size) ); + + stream.window_size_inc += update_size; + } + } + + break; + } + + case Http2::FrameType::HEADERS: + { + result = parseHttp2Headers(meta, stream, addr, end); + + if (meta.flags & Http2::FrameFlag::END_HEADERS) + { + Transfer::request_data *rd = static_cast(&stream); + + stream.reserved = createDataReceiver(rd, this->settings.variants); + + if (stream.reserved) + { + DataVariant::DataReceiver *dr = reinterpret_cast(stream.reserved); + + dr->reserved = new std::string(); + } + } + + break; + } + + case Http2::FrameType::PRIORITY: + result = Http2::ErrorCode::NO_ERROR; + break; + + case Http2::FrameType::RST_STREAM: + result = parseHttp2rstStream(meta, stream, addr, end); + break; + + case Http2::FrameType::SETTINGS: + { + result = parseHttp2Settings(meta, stream, addr, end); + + if (Http2::ErrorCode::NO_ERROR == result && false == (meta.flags & Http2::FrameFlag::ACK) ) + { + conn.decoding_dynamic_table.changeHeaderTableSize(conn.client_settings.header_table_size); + conn.decoding_dynamic_table.changeMaxHeaderListSize(conn.client_settings.max_header_list_size); + + sendEmptySettings(this->sock, req.timeout, Http2::FrameFlag::ACK); + } + + break; + } + + case Http2::FrameType::PUSH_PROMISE: + result = Http2::ErrorCode::NO_ERROR; + break; + + case Http2::FrameType::PING: + { + result = parseHttp2Ping(meta); + + if (Http2::ErrorCode::NO_ERROR == result && false == (meta.flags & Http2::FrameFlag::ACK) ) + { + const uint64_t ping_data = *reinterpret_cast(addr); + + ping(this->sock, req, ping_data); + } + + break; + } + + case Http2::FrameType::GOAWAY: + result = parseHttp2GoAway(meta, stream, addr, end); + break; + + case Http2::FrameType::WINDOW_UPDATE: + result = parseHttp2WindowUpdate(meta, stream, addr, end); + break; + + default: + result = Http2::ErrorCode::PROTOCOL_ERROR; + break; + } + + if (result != Http2::ErrorCode::NO_ERROR) + { + stream.state = Http2::StreamState::CLOSED; + + rstStream(this->sock, req.timeout, meta.stream_id, result); + + // TODO: remove closed stream(s) from unordered map + } + else if ( (meta.flags & Http2::FrameFlag::END_STREAM) && meta.stream_id != 0) + { + stream.reserved = this->sock.get_tls_session(); + + sockets.lock(); + + sockets.emplace( + std::tuple { + Socket::Socket(this->sock.get_handle() ), + &stream + } + ); + + sockets.unlock(); + + this->controls.eventProcessQueue->notify(); + + ++streams_process_count; + } + } + while (Http2::StreamState::CLOSED != primary.state); + + while (conn.sync.completed.load() < streams_process_count) + { + conn.sync.event.wait(); + } + + goAway(this->sock, req.timeout, last_stream_id, Http2::ErrorCode::NO_ERROR); + + for (auto &pair : streams) + { + destroyDataReceiver(pair.second.reserved); + } + + return this; + } +}; diff --git a/src/server/protocol/ServerHttp2.h b/src/server/protocol/ServerHttp2.h new file mode 100644 index 0000000..54658d4 --- /dev/null +++ b/src/server/protocol/ServerHttp2.h @@ -0,0 +1,21 @@ +#pragma once + +#include "../SocketsQueue.h" + +#include "ServerHttp2Protocol.h" +#include "../../transfer/HttpStatusCode.h" + +namespace HttpServer +{ + class ServerHttp2 : public ServerHttp2Protocol + { + protected: + SocketsQueue &sockets; + + public: + ServerHttp2(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, SocketsQueue &sockets) noexcept; + + virtual ServerProtocol *process() override; + virtual void close() override; + }; +}; diff --git a/src/server/protocol/ServerHttp2Protocol.cpp b/src/server/protocol/ServerHttp2Protocol.cpp new file mode 100644 index 0000000..00f305c --- /dev/null +++ b/src/server/protocol/ServerHttp2Protocol.cpp @@ -0,0 +1,178 @@ + +#include "ServerHttp2Protocol.h" +#include "../../utils/Utils.h" +#include "../../transfer/http2/HPack.h" + +#include + +namespace HttpServer +{ + ServerHttp2Protocol::ServerHttp2Protocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept + : ServerProtocol(sock, settings, controls), stream(stream) + { + + } + + uint8_t ServerHttp2Protocol::getPaddingSize(const size_t dataSize) + { + if (0 == dataSize) + { + return 0; + } + + std::random_device rd; + + uint8_t padding = rd(); + + while (dataSize <= padding) + { + padding /= 2; + } + + return padding; + } + + bool ServerHttp2Protocol::sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const + { + headers.emplace(headers.begin(), ":status", std::to_string(static_cast(status) ) ); + + std::vector buf; + buf.reserve(4096); + + HPack::pack(buf, headers, this->stream->conn.encoding_dynamic_table); + + const uint32_t frame_size = buf.size(); + + buf.insert(buf.begin(), Http2::FRAME_HEADER_SIZE, 0); + + Http2::FrameFlag flags = Http2::FrameFlag::END_HEADERS; + + if (endStream) + { + flags |= Http2::FrameFlag::END_STREAM; + } + + this->stream->setHttp2FrameHeader(reinterpret_cast(buf.data() ), frame_size, Http2::FrameType::HEADERS, flags); + + return this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; // >= 0 + } + + long ServerHttp2Protocol::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const + { + const uint8_t *data = reinterpret_cast(src); + + const Http2::ConnectionSettings &setting = this->stream->conn.client_settings; + + std::vector buf; + buf.reserve(setting.max_frame_size + Http2::FRAME_HEADER_SIZE); + + long send_size = 0; + + while (size != 0) + { + buf.resize(0); + + // TODO: test with data_size == 1 (padding length == 0) + size_t data_size = setting.max_frame_size < size ? setting.max_frame_size : size; + + const uint8_t padding = getPaddingSize(data_size); + const uint16_t padding_size = padding + sizeof(uint8_t); + + if (padding_size) + { + if (data_size + padding_size > setting.max_frame_size) + { + data_size = setting.max_frame_size - padding_size; + } + } + + const size_t frame_size = data_size + padding_size; + + /* if (this->stream->window_size_out - frame_size <= 0) + { + size_t update_size = (dt->full_size - dt->send_total) - this->stream->window_size_out; + + if (update_size > Http2::MAX_WINDOW_UPDATE) + { + update_size = Http2::MAX_WINDOW_UPDATE; + } + + sendWindowUpdate(this->sock, rp, static_cast(update_size) ); + + this->stream->window_size_out += update_size; + }*/ + + Http2::FrameFlag flags = Http2::FrameFlag::EMPTY; + + if (dt->send_total + data_size >= dt->full_size) + { + flags |= Http2::FrameFlag::END_STREAM; + } + + if (padding_size) + { + flags |= Http2::FrameFlag::PADDED; + + buf.insert(buf.begin(), sizeof(uint8_t), padding); + } + + buf.insert(buf.begin(), Http2::FRAME_HEADER_SIZE, 0); + + const Http2::FrameType frame_type = Http2::FrameType::DATA; + + this->stream->setHttp2FrameHeader(buf.data(), frame_size, frame_type, flags); + + std::copy(data, data + data_size, std::back_inserter(buf) ); + + if (padding) + { + buf.insert(buf.end(), padding, 0); + } + + long sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout); + + if (sended <= 0) + { + break; + } + + data += data_size; + send_size += data_size; + dt->send_total += data_size; + // stream->window_size_out -= frame_size; + + size -= data_size; + } + + return send_size; + } + + bool ServerHttp2Protocol::packRequestParameters(std::vector &buf, const struct Request &req, const std::string &rootDir) const + { + Utils::packNumber(buf, static_cast(Transfer::ProtocolVariant::HTTP_2) ); + Utils::packString(buf, rootDir); + Utils::packString(buf, req.host); + Utils::packString(buf, req.path); + Utils::packString(buf, req.method); + + Utils::packNumber(buf, this->stream->stream_id); + Utils::packNumber(buf, this->stream->conn.client_settings.header_table_size); + Utils::packNumber(buf, this->stream->conn.client_settings.enable_push); + Utils::packNumber(buf, this->stream->conn.client_settings.max_concurrent_streams); + Utils::packNumber(buf, this->stream->conn.client_settings.initial_window_size); + Utils::packNumber(buf, this->stream->conn.client_settings.max_frame_size); + Utils::packNumber(buf, this->stream->conn.client_settings.max_header_list_size); + Utils::packContainer(buf, this->stream->conn.encoding_dynamic_table.getList() ); + Utils::packContainer(buf, req.incoming_headers); + Utils::packContainer(buf, req.incoming_data); + Utils::packFilesIncoming(buf, req.incoming_files); + + return true; + } + + void ServerHttp2Protocol::unpackResponseParameters(struct Request &req, const void *src) const + { + Utils::unpackContainer(req.outgoing_headers, reinterpret_cast(src) ); + } +}; + diff --git a/src/server/protocol/ServerHttp2Protocol.h b/src/server/protocol/ServerHttp2Protocol.h new file mode 100644 index 0000000..7c84ba4 --- /dev/null +++ b/src/server/protocol/ServerHttp2Protocol.h @@ -0,0 +1,25 @@ +#pragma once + +#include "ServerProtocol.h" +#include "../../transfer/http2/Http2.h" + +namespace HttpServer +{ + class ServerHttp2Protocol : public ServerProtocol + { + protected: + Http2::IncStream *stream; + + protected: + static uint8_t getPaddingSize(const size_t dataSize); + + public: + ServerHttp2Protocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept; + + virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const override; + virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const override; + + virtual bool packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const override; + virtual void unpackResponseParameters(struct Request &rp, const void *src) const override; + }; +}; diff --git a/src/server/protocol/ServerHttp2Stream.cpp b/src/server/protocol/ServerHttp2Stream.cpp new file mode 100644 index 0000000..8a20c8a --- /dev/null +++ b/src/server/protocol/ServerHttp2Stream.cpp @@ -0,0 +1,110 @@ + +#include "ServerHttp2Stream.h" + +#include "extensions/Sendfile.h" + +namespace HttpServer +{ + ServerHttp2Stream::ServerHttp2Stream(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept + : ServerHttp2Protocol(sock, settings, controls, stream) + { + + } + + void ServerHttp2Stream::close() + { + this->stream->close(); + } + + ServerProtocol *ServerHttp2Stream::process() + { + struct Request req; + + req.timeout = std::chrono::milliseconds(5000); + req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; + + req.incoming_headers = std::move(this->stream->incoming_headers); + req.incoming_data = std::move(this->stream->incoming_data); + req.incoming_files = std::move(this->stream->incoming_files); + + auto const &headers = req.incoming_headers; + + auto const it_scheme = headers.find(":scheme"); + + if (headers.cend() == it_scheme) + { + return this; + } + + const std::string &scheme = it_scheme->second; + + const int default_port = (scheme == "https") ? 443 : (scheme == "http") ? 80 : 0; + + auto const it_host = headers.find(":authority"); + + if (headers.cend() == it_host) + { + return this; + } + + const std::string &host_header = it_host->second; + + // Поиск разделителя, за которым помещается номер порта, если указан + const size_t delimiter = host_header.find(':'); + + // Получить имя (или адрес) + req.host = host_header.substr(0, delimiter); + + // Получить номер порта + const int port = (std::string::npos != delimiter) ? std::strtol(host_header.substr(delimiter + 1).c_str(), nullptr, 10) : default_port; + + const ServerApplicationSettings *app_sets = this->settings.apps_tree.find(req.host); + + // Если приложение найдено + if (nullptr == app_sets || (app_sets->ports.cend() == app_sets->ports.find(port) && app_sets->tls_ports.cend() == app_sets->tls_ports.find(port) ) ) + { + return this; + } + + auto const it_method = headers.find(":method"); + + if (headers.cend() == it_method) + { + return this; + } + + req.method = it_method->second; + + auto const it_path = headers.find(":path"); + + if (headers.cend() == it_path) + { + return this; + } + + req.path = it_path->second; + + req.app_exit_code = EXIT_FAILURE; + + this->runApplication(req, *app_sets); + + for (auto const &it : req.incoming_files) + { + std::remove(it.second.getTmpName().c_str() ); + } + + if (EXIT_SUCCESS == req.app_exit_code) + { + // Http2::OutStream out(*stream); + + // auto tmp = req.protocol_data; + // req.protocol_data = &out; + + Sendfile::xSendfile(std::ref(*this), req, this->settings.mimes_types); + + // req.protocol_data = tmp; + } + + return this; + } +}; diff --git a/src/server/protocol/ServerHttp2Stream.h b/src/server/protocol/ServerHttp2Stream.h new file mode 100644 index 0000000..a4c18c1 --- /dev/null +++ b/src/server/protocol/ServerHttp2Stream.h @@ -0,0 +1,16 @@ +#pragma once + +#include "ServerHttp2Protocol.h" +#include "../../transfer/http2/Http2.h" + +namespace HttpServer +{ + class ServerHttp2Stream : public ServerHttp2Protocol + { + public: + ServerHttp2Stream(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept; + + virtual ServerProtocol *process() override; + virtual void close() override; + }; +}; diff --git a/src/server/protocol/ServerProtocol.cpp b/src/server/protocol/ServerProtocol.cpp new file mode 100644 index 0000000..9adc151 --- /dev/null +++ b/src/server/protocol/ServerProtocol.cpp @@ -0,0 +1,166 @@ + +#include "ServerProtocol.h" +#include "../../utils/Utils.h" + +namespace HttpServer +{ + ServerProtocol::ServerProtocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept + : sock(sock), settings(settings), controls(controls) + { + + } + + ServerProtocol::ServerProtocol(const ServerProtocol &prot) noexcept + : sock(prot.sock), settings(prot.settings), controls(prot.controls) + { + + } + + DataVariant::DataReceiver *ServerProtocol::createDataReceiver(const Transfer::request_data *rd, const std::unordered_map &variants) + { + auto const it = rd->incoming_headers.find("content-type"); + + if (rd->incoming_headers.cend() == it) + { + return nullptr; + } + + // Получить значение заголовка + const std::string &header_value = it->second; + + std::string data_variant_name; // Название варианта данных запроса + + std::unordered_map content_params; + + // Определить, содержит ли тип данных запроса дополнительные параметры + size_t delimiter = header_value.find(';'); + + // Если есть дополнительные параметры - извлекаем их + if (std::string::npos != delimiter) + { + data_variant_name = header_value.substr(0, delimiter); + Utils::trim(data_variant_name); + + for (size_t str_param_cur = delimiter + 1, str_param_end = 0; std::string::npos != str_param_end; str_param_cur = str_param_end + 1) + { + str_param_end = header_value.find(';', str_param_cur); + delimiter = header_value.find('=', str_param_cur); + + if (delimiter >= str_param_end) + { + std::string param_name = header_value.substr(str_param_cur, std::string::npos != str_param_end ? str_param_end - str_param_cur : std::string::npos); + Utils::trim(param_name); + content_params.emplace(std::move(param_name), std::string() ); + } + else + { + std::string param_name = header_value.substr(str_param_cur, delimiter - str_param_cur); + Utils::trim(param_name); + + ++delimiter; + + std::string param_value = header_value.substr(delimiter, std::string::npos != str_param_end ? str_param_end - delimiter : std::string::npos); + Utils::trim(param_value); + + content_params.emplace(std::move(param_name), std::move(param_value) ); + } + } + } + else + { + data_variant_name = header_value; + } + + auto const variant = variants.find(data_variant_name); + + if (variants.cend() == variant) + { + return nullptr; + } + + const DataVariant::Abstract *data_variant = variant->second; + + size_t data_length = 0; + + auto const it_len = rd->incoming_headers.find("content-length"); + + if (rd->incoming_headers.cend() != it_len) + { + data_length = std::strtoull(it_len->second.c_str(), nullptr, 10); + } + + return new DataVariant::DataReceiver { + data_variant, + data_variant->createStateStruct(rd, content_params), + data_length, + 0, 0, nullptr, + }; + } + + void ServerProtocol::destroyDataReceiver(void *src) + { + DataVariant::DataReceiver *dr = reinterpret_cast(src); + + if (dr) + { + dr->data_variant->destroyStateStruct(dr->ss); + + if (dr->reserved) + { + delete reinterpret_cast(dr->reserved); + dr->reserved = nullptr; + } + } + + delete dr; + } + + void ServerProtocol::runApplication(struct Request &req, const ServerApplicationSettings &appSets) const + { + std::vector buf; + buf.reserve(4096); + + if (false == this->packRequestParameters(buf, req, appSets.root_dir) ) + { + return; + } + + Transfer::app_request request { + this->sock.get_handle(), + this->sock.get_tls_session(), + buf.data() + }; + + Transfer::app_response response { + nullptr, 0 + }; + + try + { + // Launch application + req.app_exit_code = appSets.application_call(&request, &response); + } + catch (std::exception &exc) + { + // TODO: exception output + } + + if (response.response_data && response.data_size) + { + if (EXIT_SUCCESS == req.app_exit_code) + { + this->unpackResponseParameters(req, response.response_data); + } + + // Clear outgoing data of application + try + { + appSets.application_clear(response.response_data, response.data_size); + } + catch (std::exception &exc) + { + // TODO: exception output + } + } + } +}; diff --git a/src/server/protocol/ServerProtocol.h b/src/server/protocol/ServerProtocol.h new file mode 100644 index 0000000..91f5b3a --- /dev/null +++ b/src/server/protocol/ServerProtocol.h @@ -0,0 +1,39 @@ +#pragma once + +#include "../Request.h" +#include "../ServerControls.h" +#include "../ServerSettings.h" + +#include "../../transfer/HttpStatusCode.h" + +namespace HttpServer +{ + class ServerProtocol + { + protected: + Socket::Adapter &sock; + const ServerSettings &settings; + ServerControls &controls; + + public: + ServerProtocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept; + ServerProtocol(const ServerProtocol &prot) noexcept; + virtual ~ServerProtocol() noexcept = default; + + static DataVariant::DataReceiver *createDataReceiver(const Transfer::request_data *rd, const std::unordered_map &variants); + static void destroyDataReceiver(void *src); + + protected: + void runApplication(struct Request &req, const ServerApplicationSettings &appSets) const; + + public: + virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream = true) const = 0; + virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const = 0; + + virtual bool packRequestParameters(std::vector &buf, const struct Request &req, const std::string &rootDir) const = 0; + virtual void unpackResponseParameters(struct Request &req, const void *src) const = 0; + + virtual ServerProtocol *process() = 0; + virtual void close() = 0; + }; +}; diff --git a/src/server/protocol/ServerWebSocket.cpp b/src/server/protocol/ServerWebSocket.cpp new file mode 100644 index 0000000..5c03f1a --- /dev/null +++ b/src/server/protocol/ServerWebSocket.cpp @@ -0,0 +1,47 @@ + +#include "ServerWebSocket.h" + +namespace HttpServer +{ + ServerWebSocket::ServerWebSocket(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept + : ServerProtocol(sock, settings, controls) + { + + } + + ServerWebSocket::ServerWebSocket(const ServerProtocol &prot) noexcept + : ServerProtocol(prot) + { + + } + + bool ServerWebSocket::sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const + { + return false; + } + + long ServerWebSocket::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const + { + return 0; + } + + bool ServerWebSocket::packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const + { + return false; + } + + void ServerWebSocket::unpackResponseParameters(struct Request &req, const void *src) const + { + + } + + ServerProtocol *ServerWebSocket::process() + { + return this; + } + + void ServerWebSocket::close() + { + + } +}; diff --git a/src/server/protocol/ServerWebSocket.h b/src/server/protocol/ServerWebSocket.h new file mode 100644 index 0000000..094293f --- /dev/null +++ b/src/server/protocol/ServerWebSocket.h @@ -0,0 +1,22 @@ +#pragma once + +#include "ServerProtocol.h" + +namespace HttpServer +{ + class ServerWebSocket : public ServerProtocol + { + public: + ServerWebSocket(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept; + ServerWebSocket(const ServerProtocol &prot) noexcept; + + virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const override; + virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const override; + + virtual bool packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const override; + virtual void unpackResponseParameters(struct Request &req, const void *src) const override; + + virtual ServerProtocol *process() override; + virtual void close() override; + }; +}; diff --git a/src/server/protocol/extensions/Sendfile.cpp b/src/server/protocol/extensions/Sendfile.cpp new file mode 100644 index 0000000..9c557ba --- /dev/null +++ b/src/server/protocol/extensions/Sendfile.cpp @@ -0,0 +1,375 @@ + +#include "Sendfile.h" +#include "../../../utils/Utils.h" + +#include + +namespace HttpServer +{ + static std::string getMimeTypeByFileName(const std::string &fileName, const std::unordered_map &mimesTypes) noexcept + { + const size_t ext_pos = fileName.rfind('.'); + + const std::string file_ext = Utils::getLowerString(std::string::npos != ext_pos ? fileName.substr(ext_pos + 1) : std::string() ); + + auto const it_mime = mimesTypes.find(file_ext); + + return mimesTypes.cend() != it_mime ? it_mime->second : std::string("application/octet-stream"); + } + + static std::vector > getRanges( + const std::string &rangeHeader, + const size_t posSymEqual, + const size_t fileSize, + std::string *resultRangeHeader, + size_t *contentLength + ) noexcept + { + std::vector > ranges; + + *contentLength = 0; + + size_t delimiter = posSymEqual; // rangeHeader.find('='); + + const std::string range_unit_name(rangeHeader.cbegin(), rangeHeader.cbegin() + delimiter); + + static const std::unordered_map ranges_units { + { "bytes", 1 } + }; + + auto const it_unit = ranges_units.find(range_unit_name); + + if (ranges_units.cend() == it_unit) + { + return ranges; + } + + const size_t range_unit = it_unit->second; + + for (size_t str_pos; std::string::npos != delimiter; ) + { + str_pos = delimiter + 1; + + delimiter = rangeHeader.find(',', str_pos); + + const size_t range_pos = rangeHeader.find('-', str_pos); + + if (range_pos < delimiter) + { + const std::string range_begin_str(rangeHeader.cbegin() + str_pos, rangeHeader.cbegin() + range_pos); + const std::string range_end_str(rangeHeader.cbegin() + range_pos + 1, std::string::npos == delimiter ? rangeHeader.cend() : rangeHeader.cbegin() + delimiter); + + if (false == range_begin_str.empty() ) + { + const size_t range_begin = std::strtoull(range_begin_str.c_str(), nullptr, 10) * range_unit; + + if (range_begin < fileSize) + { + if (false == range_end_str.empty() ) + { + size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit; + + if (range_end >= range_begin) + { + if (range_end > fileSize) + { + range_end = fileSize; + } + + const size_t length = range_end - range_begin + 1; + + *contentLength += length; + + *resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ','; + + ranges.emplace_back(std::tuple {range_begin, length}); + } + } + else // if range_end_str empty + { + const size_t length = fileSize - range_begin; + + *contentLength += length; + + *resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(fileSize - 1) + ','; + + ranges.emplace_back(std::tuple {range_begin, length}); + } + } + } + else if (false == range_end_str.empty() ) // if range_begin_str empty + { + size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit; + + const size_t length = range_end < fileSize ? fileSize - range_end : fileSize; + + const size_t range_begin = fileSize - length; + + range_end = fileSize - range_begin - 1; + + *contentLength += length; + + *resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ','; + + ranges.emplace_back(std::tuple {range_begin, length}); + } + } + } + + if (false == ranges.empty() ) + { + (*resultRangeHeader).back() = '/'; + + *resultRangeHeader = "bytes " + *resultRangeHeader + std::to_string(fileSize); + } + + return ranges; + } + + int Sendfile::transferFilePart( + const ServerProtocol &prot, + const struct Request &req, + const std::string &fileName, + const std::unordered_map &mimesTypes, + const time_t fileTime, + const size_t fileSize, + const std::string &rangeHeader, + std::vector > &additionalHeaders, + const bool headersOnly + ) noexcept + { + const size_t pos_sym_equal = rangeHeader.find('='); + + if (std::string::npos == pos_sym_equal) + { + prot.sendHeaders(Http::StatusCode::BAD_REQUEST, additionalHeaders, req.timeout); + + return 1; + } + + std::string content_range_header; + + size_t content_length; + + const std::vector > ranges = getRanges(rangeHeader, pos_sym_equal, fileSize, &content_range_header, &content_length); + + if (0 == content_length) + { + prot.sendHeaders(Http::StatusCode::REQUESTED_RANGE_NOT_SATISFIABLE, additionalHeaders, req.timeout); + + return 2; + } + + // Ranges transfer + std::ifstream file(fileName, std::ifstream::binary); + + if ( ! file) + { + file.close(); + + prot.sendHeaders(Http::StatusCode::INTERNAL_SERVER_ERROR, additionalHeaders, req.timeout); + + return 3; + } + + const std::string file_mime_type = getMimeTypeByFileName(fileName, mimesTypes); + + additionalHeaders.emplace_back("content-type", file_mime_type); + additionalHeaders.emplace_back("content-length", std::to_string(content_length) ); + additionalHeaders.emplace_back("accept-ranges", "bytes"); + additionalHeaders.emplace_back("content-range", content_range_header); + additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(fileTime, true) ); + + // Отправить заголовки (206 Partial Content) + if (false == prot.sendHeaders(Http::StatusCode::PARTIAL_CONTENT, additionalHeaders, req.timeout, headersOnly) ) + { + file.close(); + + return 4; + } + + if (false == headersOnly) + { + size_t position, length; + + std::vector buf; + + DataTransfer dt { + content_length, + 0 + }; + + for (auto const &range : ranges) + { + std::tie(position, length) = range; + + buf.resize(length < 512 * 1024 ? length : 512 * 1024); + + file.seekg(position, file.beg); + + size_t send_size_left = length; + + long send_size; + + do + { + if (send_size_left < 512 * 1024) + { + buf.resize(send_size_left); + } + + file.read(buf.data(), buf.size() ); + + auto const read_size = file.gcount(); + + send_size = prot.sendData(buf.data(), read_size, req.timeout, &dt); + + send_size_left -= send_size; + } + while (false == file.eof() && false == file.fail() && send_size > 0 && send_size_left); + } + } + + file.close(); + + return 0; + } + + /** + * Передача файла (или его части) + */ + int Sendfile::transferFile( + const ServerProtocol &prot, + const struct Request &req, + const std::string &fileName, + const std::unordered_map &mimesTypes, + std::vector > &additionalHeaders, + const bool headersOnly + ) noexcept + { + // Get current time in GMT + additionalHeaders.emplace_back("date", Utils::getDatetimeAsString() ); + + time_t file_time; + size_t file_size; + + // Получить размер файла и дату последнего изменения + if (false == System::getFileSizeAndTimeGmt(fileName, &file_size, &file_time) ) + { + prot.sendHeaders(Http::StatusCode::NOT_FOUND, additionalHeaders, req.timeout); + + return 1; + } + + // Check for If-Modified header + auto const it_modified = req.incoming_headers.find("if-modified-since"); + + // Если найден заголовок проверки изменения файла (проверить, изменялся ли файл) + if (req.incoming_headers.cend() != it_modified) + { + const time_t time_in_request = Utils::rfc822DatetimeToTimestamp(it_modified->second); + + if (file_time == time_in_request) + { + prot.sendHeaders(Http::StatusCode::NOT_MODIFIED, additionalHeaders, req.timeout); + + return 2; + } + } + + auto const it_range = req.incoming_headers.find("range"); + + // Range transfer + if (req.incoming_headers.cend() != it_range) + { + return transferFilePart(prot, req, fileName, mimesTypes, file_time, file_size, it_range->second, additionalHeaders, headersOnly); + } + + // File transfer + std::ifstream file(fileName, std::ifstream::binary); + + if ( ! file) + { + file.close(); + + prot.sendHeaders(Http::StatusCode::INTERNAL_SERVER_ERROR, additionalHeaders, req.timeout); + + return 3; + } + + const std::string file_mime_type = getMimeTypeByFileName(fileName, mimesTypes); + + additionalHeaders.emplace_back("content-type", file_mime_type); + additionalHeaders.emplace_back("content-length", std::to_string(file_size) ); + additionalHeaders.emplace_back("accept-ranges", "bytes"); + additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(file_time, true) ); + + // Отправить заголовки (200 OK) + if (false == prot.sendHeaders(Http::StatusCode::OK, additionalHeaders, req.timeout, headersOnly || 0 == file_size) ) + { + file.close(); + + return 4; + } + + // Отправить файл + if (false == headersOnly && file_size) + { + std::vector buf(file_size < 512 * 1024 ? file_size : 512 * 1024); + + long send_size; + + DataTransfer dt { + file_size, + 0 + }; + + do + { + file.read(buf.data(), buf.size() ); + send_size = prot.sendData(buf.data(), file.gcount(), req.timeout, &dt); + } + while (false == file.eof() && false == file.fail() && send_size > 0 && (dt.full_size - dt.send_total) ); + } + + file.close(); + + return 0; + } + + bool Sendfile::isConnectionReuse(const struct Request &req) noexcept + { + return (req.connection_params & ConnectionParams::CONNECTION_REUSE) == ConnectionParams::CONNECTION_REUSE; + } + + void Sendfile::xSendfile( + const ServerProtocol &prot, + struct Request &req, + const std::unordered_map &mimesTypes + ) noexcept + { + auto const it_x_sendfile = req.outgoing_headers.find("x-sendfile"); + + if (req.outgoing_headers.cend() != it_x_sendfile) + { + std::vector > additional_headers; + + if (Transfer::ProtocolVariant::HTTP_1 == req.protocol_variant) + { + if (isConnectionReuse(req) ) + { + additional_headers.emplace_back("connection", "keep-alive"); + additional_headers.emplace_back("keep-alive", "timeout=5; max=" + std::to_string(req.keep_alive_count) ); + } + else + { + additional_headers.emplace_back("connection", "close"); + } + } + + const bool headers_only = ("head" == req.method); + + transferFile(prot, req, it_x_sendfile->second, mimesTypes, additional_headers, headers_only); + } + } +}; diff --git a/src/server/protocol/extensions/Sendfile.h b/src/server/protocol/extensions/Sendfile.h new file mode 100644 index 0000000..eb2823b --- /dev/null +++ b/src/server/protocol/extensions/Sendfile.h @@ -0,0 +1,41 @@ +#pragma once + +#include "../ServerProtocol.h" +#include "../../Request.h" + +namespace HttpServer +{ + class Sendfile + { + private: + static int transferFilePart( + const ServerProtocol &prot, + const struct Request &rp, + const std::string &fileName, + const std::unordered_map &mimesTypes, + const time_t fileTime, + const size_t fileSize, + const std::string &rangeHeader, + std::vector > &additionalHeaders, + const bool headersOnly + ) noexcept; + + static int transferFile( + const ServerProtocol &prot, + const struct Request &req, + const std::string &fileName, + const std::unordered_map &mimesTypes, + std::vector > &additionalHeaders, + const bool headersOnly + ) noexcept; + + public: + static bool isConnectionReuse(const struct Request &req) noexcept; + + static void xSendfile( + const ServerProtocol &prot, + struct Request &req, + const std::unordered_map &mimesTypes + ) noexcept; + }; +}; diff --git a/src/socket/Adapter.cpp b/src/socket/Adapter.cpp new file mode 100644 index 0000000..d0524b0 --- /dev/null +++ b/src/socket/Adapter.cpp @@ -0,0 +1,25 @@ + +#include "Adapter.h" + +namespace Socket +{ + long Adapter::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept + { + return this->nonblock_recv(buf.data(), buf.size(), timeout); + } + + long Adapter::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept + { + return this->nonblock_send(buf.data(), buf.length(), timeout); + } + + bool Adapter::operator ==(const Adapter &obj) const noexcept + { + return this->get_handle() == obj.get_handle(); + } + + bool Adapter::operator !=(const Adapter &obj) const noexcept + { + return this->get_handle() != obj.get_handle(); + } +}; diff --git a/src/socket/Adapter.h b/src/socket/Adapter.h new file mode 100644 index 0000000..554714f --- /dev/null +++ b/src/socket/Adapter.h @@ -0,0 +1,33 @@ +#pragma once + +#include "../system/System.h" + +#include +#include +#include + +#include + +namespace Socket +{ + class Adapter + { + public: + virtual ~Adapter() noexcept = default; + + virtual System::native_socket_type get_handle() const noexcept = 0; + virtual ::gnutls_session_t get_tls_session() const noexcept = 0; + virtual Adapter *copy() const noexcept = 0; + + long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; + virtual long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept = 0; + + long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept; + virtual long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept = 0; + + virtual void close() noexcept = 0; + + bool operator ==(const Adapter &obj) const noexcept; + bool operator !=(const Adapter &obj) const noexcept; + }; +}; diff --git a/src/socket/AdapterDefault.cpp b/src/socket/AdapterDefault.cpp new file mode 100644 index 0000000..d71d6a2 --- /dev/null +++ b/src/socket/AdapterDefault.cpp @@ -0,0 +1,44 @@ + +#include "AdapterDefault.h" + +namespace Socket +{ + AdapterDefault::AdapterDefault(const Socket &_sock) noexcept : sock(_sock) + { + + } + + System::native_socket_type AdapterDefault::get_handle() const noexcept + { + return sock.get_handle(); + } + + ::gnutls_session_t AdapterDefault::get_tls_session() const noexcept + { + return 0; + } + + Adapter *AdapterDefault::copy() const noexcept + { + return new AdapterDefault(this->sock); + } + + long AdapterDefault::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept + { + return sock.nonblock_recv(buf, length, timeout); + } + + long AdapterDefault::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept + { + return sock.nonblock_send(buf, length, timeout); + } + + void AdapterDefault::close() noexcept + { + // Wait for send all data to client + sock.nonblock_send_sync(); + + sock.shutdown(); + sock.close(); + } +}; diff --git a/src/socket/AdapterDefault.h b/src/socket/AdapterDefault.h new file mode 100644 index 0000000..0008fa9 --- /dev/null +++ b/src/socket/AdapterDefault.h @@ -0,0 +1,26 @@ +#pragma once + +#include "Adapter.h" +#include "Socket.h" + +namespace Socket +{ + class AdapterDefault : public Adapter + { + private: + Socket sock; + + public: + AdapterDefault() = delete; + AdapterDefault(const Socket &_sock) noexcept; + + virtual System::native_socket_type get_handle() const noexcept override; + virtual ::gnutls_session_t get_tls_session() const noexcept override; + virtual Adapter *copy() const noexcept override; + + virtual long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; + virtual long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; + + virtual void close() noexcept override; + }; +}; diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp new file mode 100644 index 0000000..2c88aec --- /dev/null +++ b/src/socket/AdapterTls.cpp @@ -0,0 +1,136 @@ + +#include "AdapterTls.h" + +namespace Socket +{ + AdapterTls::AdapterTls(const Socket &sock, ::gnutls_priority_t priority_cache, ::gnutls_certificate_credentials_t x509_cred) noexcept + { + // https://leandromoreira.com.br/2015/10/12/how-to-optimize-nginx-configuration-for-http2-tls-ssl/ + // https://www.gnutls.org/manual/html_node/False-Start.html + + #ifdef GNUTLS_ENABLE_FALSE_START + constexpr int flags = GNUTLS_SERVER | GNUTLS_ENABLE_FALSE_START; + #else + constexpr int flags = GNUTLS_SERVER; + #endif + + ::gnutls_init(&this->session, flags); + ::gnutls_priority_set(this->session, priority_cache); + ::gnutls_credentials_set(this->session, GNUTLS_CRD_CERTIFICATE, x509_cred); + + ::gnutls_certificate_server_set_request(this->session, GNUTLS_CERT_IGNORE); + + ::gnutls_transport_set_int2(this->session, sock.get_handle(), sock.get_handle() ); + + char h2[] = "h2"; + char http11[] = "http/1.1"; + + const ::gnutls_datum_t protocols[] { + { reinterpret_cast(h2), sizeof(h2) - 1 }, + { reinterpret_cast(http11), sizeof(http11) - 1 }, + }; + + ::gnutls_alpn_set_protocols(this->session, protocols, sizeof(protocols) / sizeof(::gnutls_datum_t), 0); + } + + AdapterTls::AdapterTls(const ::gnutls_session_t session) noexcept : session(session) + { + + } + + bool AdapterTls::handshake() noexcept + { + int ret; + + do + { + ret = ::gnutls_handshake(this->session); + } + while (ret < 0 && ::gnutls_error_is_fatal(ret) == 0); + + if (ret < 0) + { + Socket sock(this->get_handle() ); + + sock.close(); + ::gnutls_deinit(this->session); + + return false; + } + + return true; + } + + long AdapterTls::nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept + { + size_t record_size = ::gnutls_record_get_max_size(this->session); + + if (0 == record_size) + { + return -1; + } + + size_t total = 0; + + while (total < length) + { + ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); + + if (record_size > length - total) + { + record_size = length - total; + } + + const long send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); + + if (send_size < 0) + { + return send_size; + } + + total += send_size; + } + + return static_cast(total); + } + + System::native_socket_type AdapterTls::get_handle() const noexcept + { + return static_cast(::gnutls_transport_get_int(this->session) ); + } + + ::gnutls_session_t AdapterTls::get_tls_session() const noexcept + { + return this->session; + } + + Adapter *AdapterTls::copy() const noexcept + { + return new AdapterTls(this->session); + } + + long AdapterTls::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept + { + ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); + return ::gnutls_record_recv(this->session, buf, length); + } + + long AdapterTls::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept + { + return this->nonblock_send_all(buf, length, timeout); + } + + void AdapterTls::close() noexcept + { + Socket sock(this->get_handle() ); + + // Wait for send all data to client + sock.nonblock_send_sync(); + + ::gnutls_bye(this->session, GNUTLS_SHUT_RDWR); + + sock.close(); + + ::gnutls_deinit(this->session); + } +}; diff --git a/src/socket/AdapterTls.h b/src/socket/AdapterTls.h new file mode 100644 index 0000000..5751b7e --- /dev/null +++ b/src/socket/AdapterTls.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Adapter.h" +#include "Socket.h" + +namespace Socket +{ + class AdapterTls : public Adapter + { + private: + ::gnutls_session_t session; + + public: + AdapterTls() = delete; + AdapterTls(const Socket &sock, ::gnutls_priority_t priority_cache, ::gnutls_certificate_credentials_t x509_cred) noexcept; + AdapterTls(const ::gnutls_session_t session) noexcept; + + bool handshake() noexcept; + long nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + + virtual System::native_socket_type get_handle() const noexcept override; + virtual ::gnutls_session_t get_tls_session() const noexcept override; + virtual Adapter *copy() const noexcept override; + + virtual long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; + virtual long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; + + virtual void close() noexcept override; + }; +}; diff --git a/src/SocketList.cpp b/src/socket/List.cpp similarity index 83% rename from src/SocketList.cpp rename to src/socket/List.cpp index e016ed8..2d07b23 100644 --- a/src/SocketList.cpp +++ b/src/socket/List.cpp @@ -1,5 +1,5 @@  -#include "SocketList.h" +#include "List.h" #ifdef POSIX #include @@ -7,9 +7,9 @@ #include #endif -namespace HttpServer +namespace Socket { - SocketList::SocketList() + List::List() noexcept { #ifdef WIN32 this->obj_list = INVALID_HANDLE_VALUE; @@ -20,9 +20,28 @@ namespace HttpServer #endif } - bool SocketList::create(const size_t startListSize) + List::List(List &&obj) noexcept + : obj_list(obj.obj_list) { - destroy(); + #ifdef WIN32 + obj.obj_list = INVALID_HANDLE_VALUE; + obj.poll_events.swap(this->poll_events); + #elif POSIX + obj.obj_list = ~0; + obj.epoll_events.swap(this->epoll_events); + #else + #error "Undefine platform" + #endif + } + + List::~List() noexcept + { + this->destroy(); + } + + bool List::create(const size_t startListSize) + { + this->destroy(); #ifdef WIN32 this->obj_list = (HANDLE) 1; @@ -52,9 +71,9 @@ namespace HttpServer #endif } - void SocketList::destroy() + void List::destroy() noexcept { - if (is_created() ) + if (this->is_created() ) { #ifdef WIN32 this->obj_list = INVALID_HANDLE_VALUE; @@ -69,7 +88,7 @@ namespace HttpServer } } - bool SocketList::is_created() const + bool List::is_created() const noexcept { #ifdef WIN32 return this->obj_list != INVALID_HANDLE_VALUE; @@ -80,9 +99,9 @@ namespace HttpServer #endif } - bool SocketList::addSocket(const Socket &sock) + bool List::addSocket(const Socket &sock) noexcept { - if (false == is_created() ) + if (false == this->is_created() ) { return false; } @@ -118,9 +137,9 @@ namespace HttpServer #endif } - bool SocketList::removeSocket(const Socket &sock) + bool List::removeSocket(const Socket &sock) noexcept { - if (false == is_created() ) + if (false == this->is_created() ) { return false; } @@ -152,9 +171,9 @@ namespace HttpServer #endif } - bool SocketList::accept(std::vector &sockets) const + bool List::accept(std::vector &sockets) const noexcept { - if (is_created() ) + if (this->is_created() ) { #ifdef WIN32 const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), ~0); @@ -224,9 +243,9 @@ namespace HttpServer return false; } - bool SocketList::accept(std::vector &sockets, std::vector &socketsAddress) const + bool List::accept(std::vector &sockets, std::vector &socketsAddress) const noexcept { - if (is_created() ) + if (this->is_created() ) { #ifdef WIN32 const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), ~0); @@ -249,7 +268,7 @@ namespace HttpServer struct ::sockaddr_in client_addr = {}; socklen_t client_addr_len = sizeof(client_addr); - client_socket = ::accept(event.fd, reinterpret_cast(&client_addr), &client_addr_len); + client_socket = ::accept(event.fd, static_cast(&client_addr), &client_addr_len); if (~0 != client_socket) { @@ -304,9 +323,9 @@ namespace HttpServer return false; } - bool SocketList::recv(std::vector &sockets, std::vector &disconnected, std::chrono::milliseconds timeout) const + bool List::recv(std::vector &sockets, std::vector &disconnected, std::chrono::milliseconds timeout) const noexcept { - if (false == is_created() ) + if (false == this->is_created() ) { return false; } @@ -361,4 +380,4 @@ namespace HttpServer #error "Undefine platform" #endif } -}; \ No newline at end of file +}; diff --git a/src/SocketList.h b/src/socket/List.h similarity index 60% rename from src/SocketList.h rename to src/socket/List.h index 06c8d56..650447f 100644 --- a/src/SocketList.h +++ b/src/socket/List.h @@ -7,9 +7,9 @@ #include #endif -namespace HttpServer +namespace Socket { - class SocketList + class List { protected: #ifdef WIN32 @@ -23,19 +23,21 @@ namespace HttpServer #endif public: - SocketList(); + List() noexcept; + List(List &&obj) noexcept; + ~List() noexcept; bool create(const size_t startListSize = 1); - void destroy(); + void destroy() noexcept; - bool is_created() const; + bool is_created() const noexcept; - bool addSocket(const Socket &sock); - bool removeSocket(const Socket &sock); + bool addSocket(const Socket &sock) noexcept; + bool removeSocket(const Socket &sock) noexcept; - bool accept(std::vector &sockets) const; - bool accept(std::vector &sockets, std::vector &socketsAddress) const; + bool accept(std::vector &sockets) const noexcept; + bool accept(std::vector &sockets, std::vector &socketsAddress) const noexcept; - bool recv(std::vector &sockets, std::vector &errors, std::chrono::milliseconds timeout = std::chrono::milliseconds(~0) ) const; + bool recv(std::vector &sockets, std::vector &errors, std::chrono::milliseconds timeout = std::chrono::milliseconds(~0) ) const noexcept; }; -}; \ No newline at end of file +}; diff --git a/src/Socket.cpp b/src/socket/Socket.cpp similarity index 73% rename from src/Socket.cpp rename to src/socket/Socket.cpp index 158e2d0..d9b9043 100644 --- a/src/Socket.cpp +++ b/src/socket/Socket.cpp @@ -11,9 +11,9 @@ #include #endif -namespace HttpServer +namespace Socket { - bool Socket::Startup() + bool Socket::Startup() noexcept { #ifdef WIN32 unsigned short version = MAKEWORD(2, 2); @@ -26,7 +26,7 @@ namespace HttpServer #endif } - bool Socket::Cleanup() + bool Socket::Cleanup() noexcept { #ifdef WIN32 return 0 == ::WSACleanup(); @@ -37,7 +37,7 @@ namespace HttpServer #endif } - int Socket::getLastError() + int Socket::getLastError() noexcept { #ifdef WIN32 return ::WSAGetLastError(); @@ -48,27 +48,27 @@ namespace HttpServer #endif } - Socket::Socket(): socket_handle(~0) + Socket::Socket() noexcept : socket_handle(~0) { } - Socket::Socket(const System::native_socket_type fd) : socket_handle(fd) + Socket::Socket(const System::native_socket_type fd) noexcept : socket_handle(fd) { } - Socket::Socket(const Socket &obj) : socket_handle(obj.socket_handle) + Socket::Socket(const Socket &obj) noexcept : socket_handle(obj.socket_handle) { } - Socket::Socket(Socket &&obj) : socket_handle(obj.socket_handle) + Socket::Socket(Socket &&obj) noexcept : socket_handle(obj.socket_handle) { obj.socket_handle = ~0; } - bool Socket::open() + bool Socket::open() noexcept { this->close(); @@ -77,7 +77,7 @@ namespace HttpServer return this->is_open(); } - bool Socket::close() + bool Socket::close() noexcept { if (this->is_open() ) { @@ -100,7 +100,7 @@ namespace HttpServer return false; } - bool Socket::is_open() const + bool Socket::is_open() const noexcept { #ifdef WIN32 return INVALID_SOCKET != this->socket_handle; @@ -111,12 +111,12 @@ namespace HttpServer #endif } - System::native_socket_type Socket::get_handle() const + System::native_socket_type Socket::get_handle() const noexcept { return this->socket_handle; } - bool Socket::bind(const int port) const + bool Socket::bind(const int port) const noexcept { const ::sockaddr_in sock_addr = { AF_INET, @@ -128,12 +128,12 @@ namespace HttpServer return 0 == ::bind(this->socket_handle, reinterpret_cast(&sock_addr), sizeof(sockaddr_in) ); } - bool Socket::listen() const + bool Socket::listen() const noexcept { return 0 == ::listen(this->socket_handle, SOMAXCONN); } - Socket Socket::accept() const + Socket Socket::accept() const noexcept { #ifdef WIN32 System::native_socket_type client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); @@ -145,7 +145,7 @@ namespace HttpServer return Socket(client_socket); } - Socket Socket::nonblock_accept() const + Socket Socket::nonblock_accept() const noexcept { System::native_socket_type client_socket = ~0; #ifdef WIN32 @@ -176,7 +176,7 @@ namespace HttpServer return Socket(client_socket); } - Socket Socket::nonblock_accept(const std::chrono::milliseconds &timeout) const + Socket Socket::nonblock_accept(const std::chrono::milliseconds &timeout) const noexcept { System::native_socket_type client_socket = ~0; #ifdef WIN32 @@ -207,7 +207,7 @@ namespace HttpServer return Socket(client_socket); } - bool Socket::shutdown() const + bool Socket::shutdown() const noexcept { if (is_open() ) { @@ -223,7 +223,7 @@ namespace HttpServer return false; } - bool Socket::nonblock(const bool isNonBlock) const + bool Socket::nonblock(const bool isNonBlock) const noexcept { #ifdef WIN32 unsigned long value = isNonBlock; @@ -236,7 +236,7 @@ namespace HttpServer } /* - bool Socket::is_nonblock() const + bool Socket::is_nonblock() const noexcept { #ifdef WIN32 @@ -249,7 +249,7 @@ namespace HttpServer } */ - bool Socket::tcp_nodelay(const bool nodelay) const + bool Socket::tcp_nodelay(const bool nodelay) const noexcept { #ifdef WIN32 int flags = nodelay ? 1 : 0; @@ -262,41 +262,51 @@ namespace HttpServer #endif } - long Socket::recv(std::vector &buf) const + long Socket::recv(std::vector &buf) const noexcept + { + return this->recv(buf.data(), buf.size() ); + } + + long Socket::recv(void *buf, const size_t length) const noexcept { #ifdef WIN32 - return ::recv(this->socket_handle, buf.data(), static_cast(buf.size() ), 0); + return ::recv(this->socket_handle, buf, static_cast(length), 0); #elif POSIX - return ::recv(this->socket_handle, buf.data(), buf.size(), 0); + return ::recv(this->socket_handle, buf, length, 0); #else #error "Undefine platform" #endif } - long Socket::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const + long Socket::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept + { + return this->nonblock_recv(buf.data(), buf.size(), timeout); + } + + long Socket::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { long recv_len = ~0; #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event = { this->socket_handle, - POLLRDNORM, - 0 - }; + POLLRDNORM, + 0 + }; if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) - { - recv_len = ::recv(this->socket_handle, buf.data(), static_cast(buf.size() ), 0); + { + recv_len = this->recv(this->socket_handle, buf, static_cast(length), 0); } #elif POSIX - struct ::pollfd event = { + struct ::pollfd event = { this->socket_handle, - POLLIN, - 0 - }; + POLLIN, + 0 + }; if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) { - recv_len = ::recv(this->socket_handle, buf.data(), buf.size(), 0); + recv_len = ::recv(this->socket_handle, buf, length, 0); } #else #error "Undefine platform" @@ -304,7 +314,7 @@ namespace HttpServer return recv_len; } - static long send_all(const System::native_socket_type socket_handle, const void *data, const size_t length) + static long send_all(const System::native_socket_type socket_handle, const void *data, const size_t length) noexcept { size_t total = 0; @@ -323,17 +333,17 @@ namespace HttpServer return static_cast(total); } - long Socket::send(const std::string &buf) const + long Socket::send(const std::string &buf) const noexcept { - return send_all(this->socket_handle, buf.data(), buf.length() ); + return this->send(buf.data(), buf.length() ); } - long Socket::send(const std::vector &buf, const size_t length) const + long Socket::send(const void *buf, const size_t length) const noexcept { - return send_all(this->socket_handle, buf.data(), length); + return send_all(this->socket_handle, buf, length); } - static long nonblock_send_all(const System::native_socket_type socket_handle, const void *data, const size_t length, const std::chrono::milliseconds &timeout) + static long nonblock_send_all(const System::native_socket_type socket_handle, const void *data, const size_t length, const std::chrono::milliseconds &timeout) noexcept { size_t total = 0; @@ -348,7 +358,7 @@ namespace HttpServer { if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLWRNORM) { - const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, static_cast(length - total), 0); + const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, static_cast(length - total), 0); if (send_size < 0) { @@ -395,17 +405,17 @@ namespace HttpServer return static_cast(total); } - long Socket::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const + long Socket::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept { - return nonblock_send_all(this->socket_handle, buf.data(), buf.length(), timeout); + return this->nonblock_send(buf.data(), buf.length(), timeout); } - long Socket::nonblock_send(const std::vector &buf, const size_t length, const std::chrono::milliseconds &timeout) const + long Socket::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { - return nonblock_send_all(this->socket_handle, buf.data(), length, timeout); + return nonblock_send_all(this->socket_handle, buf, length, timeout); } - void Socket::nonblock_send_sync() const + void Socket::nonblock_send_sync() const noexcept { #ifdef WIN32 WSAPOLLFD event = { @@ -428,19 +438,19 @@ namespace HttpServer #endif } - Socket &Socket::operator=(const Socket &obj) + Socket &Socket::operator=(const Socket &obj) noexcept { this->socket_handle = obj.socket_handle; return *this; } - bool Socket::operator ==(const Socket &obj) const + bool Socket::operator ==(const Socket &obj) const noexcept { return this->socket_handle == obj.socket_handle; } - bool Socket::operator !=(const Socket &obj) const + bool Socket::operator !=(const Socket &obj) const noexcept { return this->socket_handle != obj.socket_handle; } -}; \ No newline at end of file +}; diff --git a/src/socket/Socket.h b/src/socket/Socket.h new file mode 100644 index 0000000..26f40a5 --- /dev/null +++ b/src/socket/Socket.h @@ -0,0 +1,79 @@ +#pragma once + +#include "../system/System.h" + +#include +#include +#include + +namespace Socket +{ + class Socket + { + protected: + System::native_socket_type socket_handle; + + public: + bool static Startup() noexcept; + bool static Cleanup() noexcept; + int static getLastError() noexcept; + + public: + Socket() noexcept; + Socket(const System::native_socket_type fd) noexcept; + Socket(const Socket &obj) noexcept; + Socket(Socket &&obj) noexcept; + + ~Socket() noexcept = default; + + bool open() noexcept; + bool close() noexcept; + + bool is_open() const noexcept; + System::native_socket_type get_handle() const noexcept; + + bool bind(const int port) const noexcept; + bool listen() const noexcept; + + Socket accept() const noexcept; + Socket nonblock_accept() const noexcept; + Socket nonblock_accept(const std::chrono::milliseconds &timeout) const noexcept; + + bool shutdown() const noexcept; + + bool nonblock(const bool isNonBlock = true) const noexcept; + // bool is_nonblock() const noexcept; + bool tcp_nodelay(const bool nodelay = true) const noexcept; + + long recv(std::vector &buf) const noexcept; + long recv(void *buf, const size_t length) const noexcept; + + long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; + long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + + long send(const std::string &buf) const noexcept; + long send(const void *buf, const size_t length) const noexcept; + + long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept; + long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + + void nonblock_send_sync() const noexcept; + + Socket &operator =(const Socket &obj) noexcept; + + bool operator ==(const Socket &obj) const noexcept; + bool operator !=(const Socket &obj) const noexcept; + }; +}; + +namespace std +{ + // Hash for Socket + template<> struct hash + { + std::size_t operator()(const Socket::Socket &obj) const noexcept + { + return std::hash{}(obj.get_handle() ); + } + }; +}; diff --git a/src/system/Cache.h b/src/system/Cache.h new file mode 100644 index 0000000..f8998e2 --- /dev/null +++ b/src/system/Cache.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace System +{ + constexpr int cacheLine = 64; + + template + struct CachePadding + { + private: + static constexpr int padding_size = cacheLine - sizeof(T) % cacheLine; + + uint8_t padding[padding_size > 0 ? padding_size : sizeof(void *)]; + }; + + template + struct CachePaddingSize + { + private: + uint8_t padding[cacheLine - T % cacheLine]; + }; +} diff --git a/src/GlobalMutex.cpp b/src/system/GlobalMutex.cpp similarity index 92% rename from src/GlobalMutex.cpp rename to src/system/GlobalMutex.cpp index 68a0c3a..2079b4e 100644 --- a/src/GlobalMutex.cpp +++ b/src/system/GlobalMutex.cpp @@ -11,14 +11,14 @@ #include #endif -namespace HttpServer +namespace System { - GlobalMutex::GlobalMutex() : mtx_desc(nullptr) + GlobalMutex::GlobalMutex() noexcept : mtx_desc(nullptr) { } - GlobalMutex::~GlobalMutex() + GlobalMutex::~GlobalMutex() noexcept { this->close(); } @@ -150,7 +150,7 @@ namespace HttpServer return true; } - bool GlobalMutex::close() + bool GlobalMutex::close() noexcept { if (this->mtx_desc) { @@ -172,7 +172,7 @@ namespace HttpServer return false; } - bool GlobalMutex::is_open() const + bool GlobalMutex::is_open() const noexcept { #ifdef WIN32 return nullptr != this->mtx_desc; @@ -183,7 +183,7 @@ namespace HttpServer #endif } - bool GlobalMutex::lock() const + bool GlobalMutex::lock() const noexcept { #ifdef WIN32 return WAIT_OBJECT_0 == ::WaitForSingleObject(this->mtx_desc, INFINITE); @@ -194,7 +194,7 @@ namespace HttpServer #endif } - bool GlobalMutex::try_lock() const + bool GlobalMutex::try_lock() const noexcept { #ifdef WIN32 return WAIT_OBJECT_0 == ::WaitForSingleObject(this->mtx_desc, 0); @@ -205,7 +205,7 @@ namespace HttpServer #endif } - bool GlobalMutex::unlock() const + bool GlobalMutex::unlock() const noexcept { #ifdef WIN32 return 0 != ::ReleaseMutex(this->mtx_desc); @@ -215,4 +215,4 @@ namespace HttpServer #error "Undefine platform" #endif } -}; \ No newline at end of file +}; diff --git a/src/GlobalMutex.h b/src/system/GlobalMutex.h similarity index 67% rename from src/GlobalMutex.h rename to src/system/GlobalMutex.h index 42cee55..5250d78 100644 --- a/src/GlobalMutex.h +++ b/src/system/GlobalMutex.h @@ -9,7 +9,7 @@ #include -namespace HttpServer +namespace System { class GlobalMutex { @@ -25,8 +25,8 @@ namespace HttpServer std::string mtx_name; public: - GlobalMutex(); - ~GlobalMutex(); + GlobalMutex() noexcept; + ~GlobalMutex() noexcept; bool create(const std::string &mutexName); bool destory(); @@ -34,12 +34,12 @@ namespace HttpServer static bool destory(const std::string &mutexName); bool open(const std::string &mutexName); - bool close(); + bool close() noexcept; - bool is_open() const; + bool is_open() const noexcept; - bool lock() const; - bool try_lock() const; - bool unlock() const; + bool lock() const noexcept; + bool try_lock() const noexcept; + bool unlock() const noexcept; }; -}; \ No newline at end of file +}; diff --git a/src/Module.cpp b/src/system/Module.cpp similarity index 84% rename from src/Module.cpp rename to src/system/Module.cpp index 50aa0b5..8cbddb1 100644 --- a/src/Module.cpp +++ b/src/system/Module.cpp @@ -12,9 +12,9 @@ #include #endif -namespace HttpServer +namespace System { - Module::Module(): lib_handle(nullptr) + Module::Module() noexcept : lib_handle(nullptr) { } @@ -24,17 +24,17 @@ namespace HttpServer open(libPath); } - Module::Module(const Module &obj) : lib_handle(obj.lib_handle) + Module::Module(const Module &obj) noexcept : lib_handle(obj.lib_handle) { } - Module::Module(Module &&obj) : lib_handle(obj.lib_handle) + Module::Module(Module &&obj) noexcept : lib_handle(obj.lib_handle) { obj.lib_handle = nullptr; } - bool Module::is_open() const + bool Module::is_open() const noexcept { return nullptr != this->lib_handle; } @@ -100,7 +100,7 @@ namespace HttpServer return true; } - void Module::close() + void Module::close() noexcept { if (lib_handle) { @@ -116,7 +116,7 @@ namespace HttpServer } } - bool Module::find(const std::string &symbolName, void *(**addr)(void *) ) const + bool Module::find(const std::string &symbolName, void *(**addr)(void *) ) const noexcept { if (lib_handle) { @@ -140,7 +140,7 @@ namespace HttpServer return false; } - bool Module::find(const char *symbolName, void *(**addr)(void *) ) const + bool Module::find(const char *symbolName, void *(**addr)(void *) ) const noexcept { if (lib_handle) { @@ -164,17 +164,17 @@ namespace HttpServer return false; } - bool Module::operator ==(const Module &obj) const + bool Module::operator ==(const Module &obj) const noexcept { return this->lib_handle == obj.lib_handle; } - bool Module::operator !=(const Module &obj) const + bool Module::operator !=(const Module &obj) const noexcept { return this->lib_handle != obj.lib_handle; } - Module &Module::operator =(const Module &obj) + Module &Module::operator =(const Module &obj) noexcept { if (*this != obj) { @@ -186,7 +186,7 @@ namespace HttpServer return *this; } - Module &Module::operator =(Module &&obj) + Module &Module::operator =(Module &&obj) noexcept { if (*this != obj) { @@ -199,4 +199,4 @@ namespace HttpServer return *this; } -}; \ No newline at end of file +}; diff --git a/src/Module.h b/src/system/Module.h similarity index 51% rename from src/Module.h rename to src/system/Module.h index a1dc301..49e2e34 100644 --- a/src/Module.h +++ b/src/system/Module.h @@ -7,7 +7,7 @@ #include -namespace HttpServer +namespace System { class Module { @@ -21,25 +21,25 @@ namespace HttpServer #endif public: - Module(); + Module() noexcept; Module(const std::string &libPath); - Module(const Module &obj); - Module(Module &&obj); + Module(const Module &obj) noexcept; + Module(Module &&obj) noexcept; - ~Module() = default; + ~Module() noexcept = default; - bool is_open() const; + bool is_open() const noexcept; bool open(const std::string &libPath); - void close(); + void close() noexcept; - bool find(const std::string &symbolName, void *(**addr)(void *) ) const; - bool find(const char *symbolName, void *(**addr)(void *) ) const; + bool find(const std::string &symbolName, void *(**addr)(void *) ) const noexcept; + bool find(const char *symbolName, void *(**addr)(void *) ) const noexcept; - bool operator ==(const Module &obj) const; - bool operator !=(const Module &obj) const; + bool operator ==(const Module &obj) const noexcept; + bool operator !=(const Module &obj) const noexcept; - Module &operator =(const Module &obj); - Module &operator =(Module &&obj); + Module &operator =(const Module &obj) noexcept; + Module &operator =(Module &&obj) noexcept; }; -}; \ No newline at end of file +}; diff --git a/src/SharedMemory.cpp b/src/system/SharedMemory.cpp similarity index 95% rename from src/SharedMemory.cpp rename to src/system/SharedMemory.cpp index 878d3d4..1c0560f 100644 --- a/src/SharedMemory.cpp +++ b/src/system/SharedMemory.cpp @@ -15,9 +15,9 @@ #include -namespace HttpServer +namespace System { - SharedMemory::SharedMemory() + SharedMemory::SharedMemory() noexcept { #ifdef WIN32 this->shm_desc = nullptr; @@ -28,7 +28,7 @@ namespace HttpServer #endif } - SharedMemory::~SharedMemory() + SharedMemory::~SharedMemory() noexcept { this->close(); } @@ -129,7 +129,7 @@ namespace HttpServer return true; } - bool SharedMemory::is_open() const + bool SharedMemory::is_open() const noexcept { #ifdef WIN32 return nullptr != this->shm_desc; @@ -140,7 +140,7 @@ namespace HttpServer #endif } - bool SharedMemory::write(const void *src, const size_t size, const size_t offset) const + bool SharedMemory::write(const void *src, const size_t size, const size_t offset) const noexcept { #ifdef WIN32 @@ -175,7 +175,7 @@ namespace HttpServer return true; } - bool SharedMemory::read(void *dest, const size_t size, const size_t offset) const + bool SharedMemory::read(void *dest, const size_t size, const size_t offset) const noexcept { #ifdef WIN32 @@ -210,7 +210,7 @@ namespace HttpServer return true; } - bool SharedMemory::close() + bool SharedMemory::close() noexcept { if (this->is_open() ) { @@ -281,4 +281,4 @@ namespace HttpServer #error "Undefine platform" #endif } -}; \ No newline at end of file +}; diff --git a/src/SharedMemory.h b/src/system/SharedMemory.h similarity index 76% rename from src/SharedMemory.h rename to src/system/SharedMemory.h index 8ea5b24..d76608f 100644 --- a/src/SharedMemory.h +++ b/src/system/SharedMemory.h @@ -7,7 +7,7 @@ #include -namespace HttpServer +namespace System { class SharedMemory { @@ -23,20 +23,20 @@ namespace HttpServer std::string shm_name; public: - SharedMemory(); - ~SharedMemory(); + SharedMemory() noexcept; + ~SharedMemory() noexcept; bool create(const std::string &memName, const size_t memSize); bool open(const std::string &memName); - bool is_open() const; + bool is_open() const noexcept; - bool write(const void *data, const size_t size, const size_t offset = 0) const; - bool read(void *dest, const size_t size, const size_t offset = 0) const; + bool write(const void *data, const size_t size, const size_t offset = 0) const noexcept; + bool read(void *dest, const size_t size, const size_t offset = 0) const noexcept; - bool close(); + bool close() noexcept; bool destroy(); static bool destroy(const std::string &memName); }; -}; \ No newline at end of file +}; diff --git a/src/System.cpp b/src/system/System.cpp similarity index 97% rename from src/System.cpp rename to src/system/System.cpp index c903837..b32f51c 100644 --- a/src/System.cpp +++ b/src/system/System.cpp @@ -52,7 +52,7 @@ namespace System } #endif - native_processid_type getProcessId() + native_processid_type getProcessId() noexcept { #ifdef WIN32 return ::GetCurrentProcessId(); @@ -80,7 +80,7 @@ namespace System #endif } - bool isProcessExists(const native_processid_type pid) + bool isProcessExists(const native_processid_type pid) noexcept { #ifdef WIN32 HANDLE hProcess = ::OpenProcess(SYNCHRONIZE, false, pid); @@ -93,7 +93,7 @@ namespace System #endif } - bool sendSignal(const native_processid_type pid, const int signal) + bool sendSignal(const native_processid_type pid, const int signal) noexcept { #ifdef WIN32 EnumData ed = {pid, 0}; @@ -113,7 +113,7 @@ namespace System #endif } - bool isDoneThread(const std::thread::native_handle_type handle) + bool isDoneThread(const std::thread::native_handle_type handle) noexcept { #ifdef WIN32 return WAIT_OBJECT_0 == ::WaitForSingleObject(handle, 0); @@ -317,4 +317,4 @@ namespace System #error "Undefine platform" #endif } -}; \ No newline at end of file +}; diff --git a/src/System.h b/src/system/System.h similarity index 88% rename from src/System.h rename to src/system/System.h index 6d5e109..1ce46d2 100644 --- a/src/System.h +++ b/src/system/System.h @@ -3,6 +3,8 @@ #ifdef WIN32 #include + typedef long ssize_t; + ::TCHAR myWndClassName[]; #ifdef SIGTERM @@ -50,15 +52,15 @@ namespace System #error "Undefine platform" #endif - native_processid_type getProcessId(); + native_processid_type getProcessId() noexcept; bool changeCurrentDirectory(const std::string &dir); - bool isProcessExists(const native_processid_type pid); + bool isProcessExists(const native_processid_type pid) noexcept; - bool sendSignal(const native_processid_type pid, const int signal); + bool sendSignal(const native_processid_type pid, const int signal) noexcept; - bool isDoneThread(const std::thread::native_handle_type handle); + bool isDoneThread(const std::thread::native_handle_type handle) noexcept; std::string getTempDir(); @@ -67,4 +69,4 @@ namespace System bool getFileSizeAndTimeGmt(const std::string &filePath, size_t *fileSize, time_t *fileTime); void filterSharedMemoryName(std::string &memName); -}; \ No newline at end of file +}; diff --git a/src/transfer/AppRequest.h b/src/transfer/AppRequest.h new file mode 100644 index 0000000..5676b34 --- /dev/null +++ b/src/transfer/AppRequest.h @@ -0,0 +1,23 @@ +#pragma once + +#include "../socket/AdapterTls.h" +#include "FileIncoming.h" + +#include + +namespace Transfer +{ + struct request_data + { + std::unordered_multimap incoming_headers; + std::unordered_multimap incoming_data; + std::unordered_multimap incoming_files; + }; + + struct app_request + { + const System::native_socket_type socket; + const ::gnutls_session_t tls_session; + const void * const request_data; + }; +}; diff --git a/src/transfer/AppResponse.h b/src/transfer/AppResponse.h new file mode 100644 index 0000000..80937dd --- /dev/null +++ b/src/transfer/AppResponse.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +namespace Transfer +{ + struct app_response + { + void *response_data; + size_t data_size; + }; +}; diff --git a/src/transfer/FileIncoming.cpp b/src/transfer/FileIncoming.cpp new file mode 100644 index 0000000..58188fc --- /dev/null +++ b/src/transfer/FileIncoming.cpp @@ -0,0 +1,108 @@ + +#include "FileIncoming.h" +#include "../utils/Utils.h" + +#include + +namespace Transfer +{ + FileIncoming::FileIncoming(std::string &&fileTmpName, std::string &&fileName, std::string &&fileType, const size_t fileSize) noexcept + : file_tmp_name(std::move(fileTmpName) ), file_name(std::move(fileName) ), + file_type(std::move(fileType) ), file_size(fileSize) + { + + } + + FileIncoming::FileIncoming(const FileIncoming &obj) + : file_tmp_name(obj.file_tmp_name), file_name(obj.file_name), + file_type(obj.file_type), file_size(obj.file_size) + { + + } + + FileIncoming::FileIncoming(FileIncoming &&obj) noexcept + : file_tmp_name(std::move(obj.file_tmp_name) ), file_name(std::move(obj.file_name) ), + file_type(std::move(obj.file_type) ), file_size(obj.file_size) + { + obj.file_size = 0; + } + + const std::string &FileIncoming::getTmpName() const noexcept + { + return this->file_tmp_name; + } + + const std::string &FileIncoming::getName() const noexcept + { + return this->file_name; + } + + const std::string &FileIncoming::getType() const noexcept + { + return this->file_type; + } + + size_t FileIncoming::getSize() const noexcept + { + return this->file_size; + } + + bool FileIncoming::isExists() const noexcept + { + std::ifstream file(this->file_tmp_name, std::ifstream::binary); + + const bool is_exists = file.good(); + + file.close(); + + return is_exists; + } +}; + +namespace Utils +{ + void packFilesIncoming(std::vector &buf, const std::unordered_multimap &map) + { + packNumber(buf, map.size() ); + + for (auto it = map.cbegin(); map.cend() != it; ++it) + { + packString(buf, it->first); + + const Transfer::FileIncoming &file = it->second; + + packString(buf, file.getTmpName() ); + packString(buf, file.getName() ); + packString(buf, file.getType() ); + packNumber(buf, file.getSize() ); + } + } + + const uint8_t *unpackFilesIncoming(std::unordered_multimap &map, const uint8_t *src) + { + size_t count; + src = unpackNumber(&count, src); + + for (size_t i = 0; i < count; ++i) + { + std::string key; + src = unpackString(key, src); + + std::string file_tmp_name; + src = unpackString(file_tmp_name, src); + + std::string file_name; + src = unpackString(file_name, src); + + std::string file_type; + src = unpackString(file_type, src); + + size_t file_size; + src = unpackNumber(&file_size, src); + + map.emplace(std::move(key), Transfer::FileIncoming(std::move(file_tmp_name), std::move(file_name), std::move(file_type), file_size) ); + } + + return src; + } +}; diff --git a/src/transfer/FileIncoming.h b/src/transfer/FileIncoming.h new file mode 100644 index 0000000..0d853eb --- /dev/null +++ b/src/transfer/FileIncoming.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include + +namespace Transfer +{ + class FileIncoming + { + protected: + std::string file_tmp_name; + std::string file_name; + std::string file_type; + size_t file_size; + + private: + FileIncoming() = delete; + + public: + FileIncoming(std::string &&fileTmpName, std::string &&fileName, std::string &&fileType, const size_t fileSize) noexcept; + FileIncoming(const FileIncoming &obj); + FileIncoming(FileIncoming &&obj) noexcept; + + ~FileIncoming() noexcept = default; + + const std::string &getTmpName() const noexcept; + const std::string &getName() const noexcept; + const std::string &getType() const noexcept; + size_t getSize() const noexcept; + + bool isExists() const noexcept; + }; +}; + +namespace Utils +{ + void packFilesIncoming(std::vector &buf, const std::unordered_multimap &map); + const uint8_t *unpackFilesIncoming(std::unordered_multimap &map, const uint8_t *src); +}; diff --git a/src/transfer/HttpStatusCode.h b/src/transfer/HttpStatusCode.h new file mode 100644 index 0000000..169326a --- /dev/null +++ b/src/transfer/HttpStatusCode.h @@ -0,0 +1,52 @@ +#pragma once + +namespace Http +{ + enum class StatusCode + { + EMPTY = 0, + + CONTINUE = 100, + SWITCHING_PROTOCOLS = 101, + PROCESSING = 102, + + OK = 200, + CREATED = 201, + ACCEPTED = 202, + NON_AUTHORITATIVE_INFORMATION = 203, + NO_CONTENT = 204, + RESET_CONTENT = 205, + PARTIAL_CONTENT = 206, + MULTI_STATUS = 207, + + MULTIPLE_CHOISES = 300, + MOVED_PERMANENTLY = 301, + MOVED_TEMPORARILY = 302, + FOUND = 302, + SEE_OTHER = 303, + NOT_MODIFIED = 304, + USE_PROXY = 305, + TEMPORARY_REDIRECT = 307, + + BAD_REQUEST = 400, + UNAUTHORIZED = 401, + PAYMENT_REQUIRED = 402, + FORBIDDEN = 403, + NOT_FOUND = 404, + METHOD_NOT_ALLOWED = 405, + NOT_ACCEPTABLE = 406, + PROXY_AUTHENTICATION_REQUIRED = 407, + REQUEST_TIMEOUT = 408, + CONFLICT = 409, + GONE = 410, + REQUEST_ENTITY_TOO_LARGE = 413, + REQUESTED_RANGE_NOT_SATISFIABLE = 416, + + INTERNAL_SERVER_ERROR = 500, + NOT_IMPLEMENTED = 501, + BAD_GATEWAY = 502, + SERVICE_UNAVAILABLE = 503, + GATEWAY_TIMEOUT = 504, + HTTP_VERSION_NOT_SUPPORTED = 505, + }; +}; diff --git a/src/transfer/ProtocolVariant.h b/src/transfer/ProtocolVariant.h new file mode 100644 index 0000000..11954bf --- /dev/null +++ b/src/transfer/ProtocolVariant.h @@ -0,0 +1,11 @@ +#pragma once + +namespace Transfer +{ + enum class ProtocolVariant + { + UNKNOWN = 0, + HTTP_1, + HTTP_2 + }; +}; diff --git a/src/transfer/http2/HPack.cpp b/src/transfer/http2/HPack.cpp new file mode 100644 index 0000000..60889d2 --- /dev/null +++ b/src/transfer/http2/HPack.cpp @@ -0,0 +1,6038 @@ +/** + * This code getting from https://github.com/tatsuhiro-t/go-http2-hpack + */ + +#include "HPack.h" + +#include +#include +#include +#include + +namespace HPack +{ + struct HuffmanSymbol + { + int nbits; + uint32_t code; + }; + + static const HuffmanSymbol huffmanSymbolTable[] { + {13, 0x1ff8}, + {23, 0x7fffd8}, + {28, 0xfffffe2}, + {28, 0xfffffe3}, + {28, 0xfffffe4}, + {28, 0xfffffe5}, + {28, 0xfffffe6}, + {28, 0xfffffe7}, + {28, 0xfffffe8}, + {24, 0xffffea}, + {30, 0x3ffffffc}, + {28, 0xfffffe9}, + {28, 0xfffffea}, + {30, 0x3ffffffd}, + {28, 0xfffffeb}, + {28, 0xfffffec}, + {28, 0xfffffed}, + {28, 0xfffffee}, + {28, 0xfffffef}, + {28, 0xffffff0}, + {28, 0xffffff1}, + {28, 0xffffff2}, + {30, 0x3ffffffe}, + {28, 0xffffff3}, + {28, 0xffffff4}, + {28, 0xffffff5}, + {28, 0xffffff6}, + {28, 0xffffff7}, + {28, 0xffffff8}, + {28, 0xffffff9}, + {28, 0xffffffa}, + {28, 0xffffffb}, + {6, 0x14}, + {10, 0x3f8}, + {10, 0x3f9}, + {12, 0xffa}, + {13, 0x1ff9}, + {6, 0x15}, + {8, 0xf8}, + {11, 0x7fa}, + {10, 0x3fa}, + {10, 0x3fb}, + {8, 0xf9}, + {11, 0x7fb}, + {8, 0xfa}, + {6, 0x16}, + {6, 0x17}, + {6, 0x18}, + {5, 0x0}, + {5, 0x1}, + {5, 0x2}, + {6, 0x19}, + {6, 0x1a}, + {6, 0x1b}, + {6, 0x1c}, + {6, 0x1d}, + {6, 0x1e}, + {6, 0x1f}, + {7, 0x5c}, + {8, 0xfb}, + {15, 0x7ffc}, + {6, 0x20}, + {12, 0xffb}, + {10, 0x3fc}, + {13, 0x1ffa}, + {6, 0x21}, + {7, 0x5d}, + {7, 0x5e}, + {7, 0x5f}, + {7, 0x60}, + {7, 0x61}, + {7, 0x62}, + {7, 0x63}, + {7, 0x64}, + {7, 0x65}, + {7, 0x66}, + {7, 0x67}, + {7, 0x68}, + {7, 0x69}, + {7, 0x6a}, + {7, 0x6b}, + {7, 0x6c}, + {7, 0x6d}, + {7, 0x6e}, + {7, 0x6f}, + {7, 0x70}, + {7, 0x71}, + {7, 0x72}, + {8, 0xfc}, + {7, 0x73}, + {8, 0xfd}, + {13, 0x1ffb}, + {19, 0x7fff0}, + {13, 0x1ffc}, + {14, 0x3ffc}, + {6, 0x22}, + {15, 0x7ffd}, + {5, 0x3}, + {6, 0x23}, + {5, 0x4}, + {6, 0x24}, + {5, 0x5}, + {6, 0x25}, + {6, 0x26}, + {6, 0x27}, + {5, 0x6}, + {7, 0x74}, + {7, 0x75}, + {6, 0x28}, + {6, 0x29}, + {6, 0x2a}, + {5, 0x7}, + {6, 0x2b}, + {7, 0x76}, + {6, 0x2c}, + {5, 0x8}, + {5, 0x9}, + {6, 0x2d}, + {7, 0x77}, + {7, 0x78}, + {7, 0x79}, + {7, 0x7a}, + {7, 0x7b}, + {15, 0x7ffe}, + {11, 0x7fc}, + {14, 0x3ffd}, + {13, 0x1ffd}, + {28, 0xffffffc}, + {20, 0xfffe6}, + {22, 0x3fffd2}, + {20, 0xfffe7}, + {20, 0xfffe8}, + {22, 0x3fffd3}, + {22, 0x3fffd4}, + {22, 0x3fffd5}, + {23, 0x7fffd9}, + {22, 0x3fffd6}, + {23, 0x7fffda}, + {23, 0x7fffdb}, + {23, 0x7fffdc}, + {23, 0x7fffdd}, + {23, 0x7fffde}, + {24, 0xffffeb}, + {23, 0x7fffdf}, + {24, 0xffffec}, + {24, 0xffffed}, + {22, 0x3fffd7}, + {23, 0x7fffe0}, + {24, 0xffffee}, + {23, 0x7fffe1}, + {23, 0x7fffe2}, + {23, 0x7fffe3}, + {23, 0x7fffe4}, + {21, 0x1fffdc}, + {22, 0x3fffd8}, + {23, 0x7fffe5}, + {22, 0x3fffd9}, + {23, 0x7fffe6}, + {23, 0x7fffe7}, + {24, 0xffffef}, + {22, 0x3fffda}, + {21, 0x1fffdd}, + {20, 0xfffe9}, + {22, 0x3fffdb}, + {22, 0x3fffdc}, + {23, 0x7fffe8}, + {23, 0x7fffe9}, + {21, 0x1fffde}, + {23, 0x7fffea}, + {22, 0x3fffdd}, + {22, 0x3fffde}, + {24, 0xfffff0}, + {21, 0x1fffdf}, + {22, 0x3fffdf}, + {23, 0x7fffeb}, + {23, 0x7fffec}, + {21, 0x1fffe0}, + {21, 0x1fffe1}, + {22, 0x3fffe0}, + {21, 0x1fffe2}, + {23, 0x7fffed}, + {22, 0x3fffe1}, + {23, 0x7fffee}, + {23, 0x7fffef}, + {20, 0xfffea}, + {22, 0x3fffe2}, + {22, 0x3fffe3}, + {22, 0x3fffe4}, + {23, 0x7ffff0}, + {22, 0x3fffe5}, + {22, 0x3fffe6}, + {23, 0x7ffff1}, + {26, 0x3ffffe0}, + {26, 0x3ffffe1}, + {20, 0xfffeb}, + {19, 0x7fff1}, + {22, 0x3fffe7}, + {23, 0x7ffff2}, + {22, 0x3fffe8}, + {25, 0x1ffffec}, + {26, 0x3ffffe2}, + {26, 0x3ffffe3}, + {26, 0x3ffffe4}, + {27, 0x7ffffde}, + {27, 0x7ffffdf}, + {26, 0x3ffffe5}, + {24, 0xfffff1}, + {25, 0x1ffffed}, + {19, 0x7fff2}, + {21, 0x1fffe3}, + {26, 0x3ffffe6}, + {27, 0x7ffffe0}, + {27, 0x7ffffe1}, + {26, 0x3ffffe7}, + {27, 0x7ffffe2}, + {24, 0xfffff2}, + {21, 0x1fffe4}, + {21, 0x1fffe5}, + {26, 0x3ffffe8}, + {26, 0x3ffffe9}, + {28, 0xffffffd}, + {27, 0x7ffffe3}, + {27, 0x7ffffe4}, + {27, 0x7ffffe5}, + {20, 0xfffec}, + {24, 0xfffff3}, + {20, 0xfffed}, + {21, 0x1fffe6}, + {22, 0x3fffe9}, + {21, 0x1fffe7}, + {21, 0x1fffe8}, + {23, 0x7ffff3}, + {22, 0x3fffea}, + {22, 0x3fffeb}, + {25, 0x1ffffee}, + {25, 0x1ffffef}, + {24, 0xfffff4}, + {24, 0xfffff5}, + {26, 0x3ffffea}, + {23, 0x7ffff4}, + {26, 0x3ffffeb}, + {27, 0x7ffffe6}, + {26, 0x3ffffec}, + {26, 0x3ffffed}, + {27, 0x7ffffe7}, + {27, 0x7ffffe8}, + {27, 0x7ffffe9}, + {27, 0x7ffffea}, + {27, 0x7ffffeb}, + {28, 0xffffffe}, + {27, 0x7ffffec}, + {27, 0x7ffffed}, + {27, 0x7ffffee}, + {27, 0x7ffffef}, + {27, 0x7fffff0}, + {26, 0x3ffffee}, + {30, 0x3fffffff}, + }; + + struct HuffmanDecodeNode + { + uint8_t state; + uint8_t flags; + uint8_t symbol; + }; + + static const HuffmanDecodeNode huffmanDecodeTable[][16] { + { + {4, 0x00, 0}, + {5, 0x00, 0}, + {7, 0x00, 0}, + {8, 0x00, 0}, + {11, 0x00, 0}, + {12, 0x00, 0}, + {16, 0x00, 0}, + {19, 0x00, 0}, + {25, 0x00, 0}, + {28, 0x00, 0}, + {32, 0x00, 0}, + {35, 0x00, 0}, + {42, 0x00, 0}, + {49, 0x00, 0}, + {57, 0x00, 0}, + {64, 0x01, 0}, + }, + + { + {0, 0x03, 48}, + {0, 0x03, 49}, + {0, 0x03, 50}, + {0, 0x03, 97}, + {0, 0x03, 99}, + {0, 0x03, 101}, + {0, 0x03, 105}, + {0, 0x03, 111}, + {0, 0x03, 115}, + {0, 0x03, 116}, + {13, 0x00, 0}, + {14, 0x00, 0}, + {17, 0x00, 0}, + {18, 0x00, 0}, + {20, 0x00, 0}, + {21, 0x00, 0}, + }, + + { + {1, 0x02, 48}, + {22, 0x03, 48}, + {1, 0x02, 49}, + {22, 0x03, 49}, + {1, 0x02, 50}, + {22, 0x03, 50}, + {1, 0x02, 97}, + {22, 0x03, 97}, + {1, 0x02, 99}, + {22, 0x03, 99}, + {1, 0x02, 101}, + {22, 0x03, 101}, + {1, 0x02, 105}, + {22, 0x03, 105}, + {1, 0x02, 111}, + {22, 0x03, 111}, + }, + + { + {2, 0x02, 48}, + {9, 0x02, 48}, + {23, 0x02, 48}, + {40, 0x03, 48}, + {2, 0x02, 49}, + {9, 0x02, 49}, + {23, 0x02, 49}, + {40, 0x03, 49}, + {2, 0x02, 50}, + {9, 0x02, 50}, + {23, 0x02, 50}, + {40, 0x03, 50}, + {2, 0x02, 97}, + {9, 0x02, 97}, + {23, 0x02, 97}, + {40, 0x03, 97}, + }, + + { + {3, 0x02, 48}, + {6, 0x02, 48}, + {10, 0x02, 48}, + {15, 0x02, 48}, + {24, 0x02, 48}, + {31, 0x02, 48}, + {41, 0x02, 48}, + {56, 0x03, 48}, + {3, 0x02, 49}, + {6, 0x02, 49}, + {10, 0x02, 49}, + {15, 0x02, 49}, + {24, 0x02, 49}, + {31, 0x02, 49}, + {41, 0x02, 49}, + {56, 0x03, 49}, + }, + + { + {3, 0x02, 50}, + {6, 0x02, 50}, + {10, 0x02, 50}, + {15, 0x02, 50}, + {24, 0x02, 50}, + {31, 0x02, 50}, + {41, 0x02, 50}, + {56, 0x03, 50}, + {3, 0x02, 97}, + {6, 0x02, 97}, + {10, 0x02, 97}, + {15, 0x02, 97}, + {24, 0x02, 97}, + {31, 0x02, 97}, + {41, 0x02, 97}, + {56, 0x03, 97}, + }, + + { + {2, 0x02, 99}, + {9, 0x02, 99}, + {23, 0x02, 99}, + {40, 0x03, 99}, + {2, 0x02, 101}, + {9, 0x02, 101}, + {23, 0x02, 101}, + {40, 0x03, 101}, + {2, 0x02, 105}, + {9, 0x02, 105}, + {23, 0x02, 105}, + {40, 0x03, 105}, + {2, 0x02, 111}, + {9, 0x02, 111}, + {23, 0x02, 111}, + {40, 0x03, 111}, + }, + + { + {3, 0x02, 99}, + {6, 0x02, 99}, + {10, 0x02, 99}, + {15, 0x02, 99}, + {24, 0x02, 99}, + {31, 0x02, 99}, + {41, 0x02, 99}, + {56, 0x03, 99}, + {3, 0x02, 101}, + {6, 0x02, 101}, + {10, 0x02, 101}, + {15, 0x02, 101}, + {24, 0x02, 101}, + {31, 0x02, 101}, + {41, 0x02, 101}, + {56, 0x03, 101}, + }, + + { + {3, 0x02, 105}, + {6, 0x02, 105}, + {10, 0x02, 105}, + {15, 0x02, 105}, + {24, 0x02, 105}, + {31, 0x02, 105}, + {41, 0x02, 105}, + {56, 0x03, 105}, + {3, 0x02, 111}, + {6, 0x02, 111}, + {10, 0x02, 111}, + {15, 0x02, 111}, + {24, 0x02, 111}, + {31, 0x02, 111}, + {41, 0x02, 111}, + {56, 0x03, 111}, + }, + + { + {1, 0x02, 115}, + {22, 0x03, 115}, + {1, 0x02, 116}, + {22, 0x03, 116}, + {0, 0x03, 32}, + {0, 0x03, 37}, + {0, 0x03, 45}, + {0, 0x03, 46}, + {0, 0x03, 47}, + {0, 0x03, 51}, + {0, 0x03, 52}, + {0, 0x03, 53}, + {0, 0x03, 54}, + {0, 0x03, 55}, + {0, 0x03, 56}, + {0, 0x03, 57}, + }, + + { + {2, 0x02, 115}, + {9, 0x02, 115}, + {23, 0x02, 115}, + {40, 0x03, 115}, + {2, 0x02, 116}, + {9, 0x02, 116}, + {23, 0x02, 116}, + {40, 0x03, 116}, + {1, 0x02, 32}, + {22, 0x03, 32}, + {1, 0x02, 37}, + {22, 0x03, 37}, + {1, 0x02, 45}, + {22, 0x03, 45}, + {1, 0x02, 46}, + {22, 0x03, 46}, + }, + + { + {3, 0x02, 115}, + {6, 0x02, 115}, + {10, 0x02, 115}, + {15, 0x02, 115}, + {24, 0x02, 115}, + {31, 0x02, 115}, + {41, 0x02, 115}, + {56, 0x03, 115}, + {3, 0x02, 116}, + {6, 0x02, 116}, + {10, 0x02, 116}, + {15, 0x02, 116}, + {24, 0x02, 116}, + {31, 0x02, 116}, + {41, 0x02, 116}, + {56, 0x03, 116}, + }, + + { + {2, 0x02, 32}, + {9, 0x02, 32}, + {23, 0x02, 32}, + {40, 0x03, 32}, + {2, 0x02, 37}, + {9, 0x02, 37}, + {23, 0x02, 37}, + {40, 0x03, 37}, + {2, 0x02, 45}, + {9, 0x02, 45}, + {23, 0x02, 45}, + {40, 0x03, 45}, + {2, 0x02, 46}, + {9, 0x02, 46}, + {23, 0x02, 46}, + {40, 0x03, 46}, + }, + + { + {3, 0x02, 32}, + {6, 0x02, 32}, + {10, 0x02, 32}, + {15, 0x02, 32}, + {24, 0x02, 32}, + {31, 0x02, 32}, + {41, 0x02, 32}, + {56, 0x03, 32}, + {3, 0x02, 37}, + {6, 0x02, 37}, + {10, 0x02, 37}, + {15, 0x02, 37}, + {24, 0x02, 37}, + {31, 0x02, 37}, + {41, 0x02, 37}, + {56, 0x03, 37}, + }, + + { + {3, 0x02, 45}, + {6, 0x02, 45}, + {10, 0x02, 45}, + {15, 0x02, 45}, + {24, 0x02, 45}, + {31, 0x02, 45}, + {41, 0x02, 45}, + {56, 0x03, 45}, + {3, 0x02, 46}, + {6, 0x02, 46}, + {10, 0x02, 46}, + {15, 0x02, 46}, + {24, 0x02, 46}, + {31, 0x02, 46}, + {41, 0x02, 46}, + {56, 0x03, 46}, + }, + + { + {1, 0x02, 47}, + {22, 0x03, 47}, + {1, 0x02, 51}, + {22, 0x03, 51}, + {1, 0x02, 52}, + {22, 0x03, 52}, + {1, 0x02, 53}, + {22, 0x03, 53}, + {1, 0x02, 54}, + {22, 0x03, 54}, + {1, 0x02, 55}, + {22, 0x03, 55}, + {1, 0x02, 56}, + {22, 0x03, 56}, + {1, 0x02, 57}, + {22, 0x03, 57}, + }, + + { + {2, 0x02, 47}, + {9, 0x02, 47}, + {23, 0x02, 47}, + {40, 0x03, 47}, + {2, 0x02, 51}, + {9, 0x02, 51}, + {23, 0x02, 51}, + {40, 0x03, 51}, + {2, 0x02, 52}, + {9, 0x02, 52}, + {23, 0x02, 52}, + {40, 0x03, 52}, + {2, 0x02, 53}, + {9, 0x02, 53}, + {23, 0x02, 53}, + {40, 0x03, 53}, + }, + + { + {3, 0x02, 47}, + {6, 0x02, 47}, + {10, 0x02, 47}, + {15, 0x02, 47}, + {24, 0x02, 47}, + {31, 0x02, 47}, + {41, 0x02, 47}, + {56, 0x03, 47}, + {3, 0x02, 51}, + {6, 0x02, 51}, + {10, 0x02, 51}, + {15, 0x02, 51}, + {24, 0x02, 51}, + {31, 0x02, 51}, + {41, 0x02, 51}, + {56, 0x03, 51}, + }, + + { + {3, 0x02, 52}, + {6, 0x02, 52}, + {10, 0x02, 52}, + {15, 0x02, 52}, + {24, 0x02, 52}, + {31, 0x02, 52}, + {41, 0x02, 52}, + {56, 0x03, 52}, + {3, 0x02, 53}, + {6, 0x02, 53}, + {10, 0x02, 53}, + {15, 0x02, 53}, + {24, 0x02, 53}, + {31, 0x02, 53}, + {41, 0x02, 53}, + {56, 0x03, 53}, + }, + + { + {2, 0x02, 54}, + {9, 0x02, 54}, + {23, 0x02, 54}, + {40, 0x03, 54}, + {2, 0x02, 55}, + {9, 0x02, 55}, + {23, 0x02, 55}, + {40, 0x03, 55}, + {2, 0x02, 56}, + {9, 0x02, 56}, + {23, 0x02, 56}, + {40, 0x03, 56}, + {2, 0x02, 57}, + {9, 0x02, 57}, + {23, 0x02, 57}, + {40, 0x03, 57}, + }, + + { + {3, 0x02, 54}, + {6, 0x02, 54}, + {10, 0x02, 54}, + {15, 0x02, 54}, + {24, 0x02, 54}, + {31, 0x02, 54}, + {41, 0x02, 54}, + {56, 0x03, 54}, + {3, 0x02, 55}, + {6, 0x02, 55}, + {10, 0x02, 55}, + {15, 0x02, 55}, + {24, 0x02, 55}, + {31, 0x02, 55}, + {41, 0x02, 55}, + {56, 0x03, 55}, + }, + + { + {3, 0x02, 56}, + {6, 0x02, 56}, + {10, 0x02, 56}, + {15, 0x02, 56}, + {24, 0x02, 56}, + {31, 0x02, 56}, + {41, 0x02, 56}, + {56, 0x03, 56}, + {3, 0x02, 57}, + {6, 0x02, 57}, + {10, 0x02, 57}, + {15, 0x02, 57}, + {24, 0x02, 57}, + {31, 0x02, 57}, + {41, 0x02, 57}, + {56, 0x03, 57}, + }, + + { + {26, 0x00, 0}, + {27, 0x00, 0}, + {29, 0x00, 0}, + {30, 0x00, 0}, + {33, 0x00, 0}, + {34, 0x00, 0}, + {36, 0x00, 0}, + {37, 0x00, 0}, + {43, 0x00, 0}, + {46, 0x00, 0}, + {50, 0x00, 0}, + {53, 0x00, 0}, + {58, 0x00, 0}, + {61, 0x00, 0}, + {65, 0x00, 0}, + {68, 0x01, 0}, + }, + + { + {0, 0x03, 61}, + {0, 0x03, 65}, + {0, 0x03, 95}, + {0, 0x03, 98}, + {0, 0x03, 100}, + {0, 0x03, 102}, + {0, 0x03, 103}, + {0, 0x03, 104}, + {0, 0x03, 108}, + {0, 0x03, 109}, + {0, 0x03, 110}, + {0, 0x03, 112}, + {0, 0x03, 114}, + {0, 0x03, 117}, + {38, 0x00, 0}, + {39, 0x00, 0}, + }, + + { + {1, 0x02, 61}, + {22, 0x03, 61}, + {1, 0x02, 65}, + {22, 0x03, 65}, + {1, 0x02, 95}, + {22, 0x03, 95}, + {1, 0x02, 98}, + {22, 0x03, 98}, + {1, 0x02, 100}, + {22, 0x03, 100}, + {1, 0x02, 102}, + {22, 0x03, 102}, + {1, 0x02, 103}, + {22, 0x03, 103}, + {1, 0x02, 104}, + {22, 0x03, 104}, + }, + + { + {2, 0x02, 61}, + {9, 0x02, 61}, + {23, 0x02, 61}, + {40, 0x03, 61}, + {2, 0x02, 65}, + {9, 0x02, 65}, + {23, 0x02, 65}, + {40, 0x03, 65}, + {2, 0x02, 95}, + {9, 0x02, 95}, + {23, 0x02, 95}, + {40, 0x03, 95}, + {2, 0x02, 98}, + {9, 0x02, 98}, + {23, 0x02, 98}, + {40, 0x03, 98}, + }, + + { + {3, 0x02, 61}, + {6, 0x02, 61}, + {10, 0x02, 61}, + {15, 0x02, 61}, + {24, 0x02, 61}, + {31, 0x02, 61}, + {41, 0x02, 61}, + {56, 0x03, 61}, + {3, 0x02, 65}, + {6, 0x02, 65}, + {10, 0x02, 65}, + {15, 0x02, 65}, + {24, 0x02, 65}, + {31, 0x02, 65}, + {41, 0x02, 65}, + {56, 0x03, 65}, + }, + + { + {3, 0x02, 95}, + {6, 0x02, 95}, + {10, 0x02, 95}, + {15, 0x02, 95}, + {24, 0x02, 95}, + {31, 0x02, 95}, + {41, 0x02, 95}, + {56, 0x03, 95}, + {3, 0x02, 98}, + {6, 0x02, 98}, + {10, 0x02, 98}, + {15, 0x02, 98}, + {24, 0x02, 98}, + {31, 0x02, 98}, + {41, 0x02, 98}, + {56, 0x03, 98}, + }, + + { + {2, 0x02, 100}, + {9, 0x02, 100}, + {23, 0x02, 100}, + {40, 0x03, 100}, + {2, 0x02, 102}, + {9, 0x02, 102}, + {23, 0x02, 102}, + {40, 0x03, 102}, + {2, 0x02, 103}, + {9, 0x02, 103}, + {23, 0x02, 103}, + {40, 0x03, 103}, + {2, 0x02, 104}, + {9, 0x02, 104}, + {23, 0x02, 104}, + {40, 0x03, 104}, + }, + + { + {3, 0x02, 100}, + {6, 0x02, 100}, + {10, 0x02, 100}, + {15, 0x02, 100}, + {24, 0x02, 100}, + {31, 0x02, 100}, + {41, 0x02, 100}, + {56, 0x03, 100}, + {3, 0x02, 102}, + {6, 0x02, 102}, + {10, 0x02, 102}, + {15, 0x02, 102}, + {24, 0x02, 102}, + {31, 0x02, 102}, + {41, 0x02, 102}, + {56, 0x03, 102}, + }, + + { + {3, 0x02, 103}, + {6, 0x02, 103}, + {10, 0x02, 103}, + {15, 0x02, 103}, + {24, 0x02, 103}, + {31, 0x02, 103}, + {41, 0x02, 103}, + {56, 0x03, 103}, + {3, 0x02, 104}, + {6, 0x02, 104}, + {10, 0x02, 104}, + {15, 0x02, 104}, + {24, 0x02, 104}, + {31, 0x02, 104}, + {41, 0x02, 104}, + {56, 0x03, 104}, + }, + + { + {1, 0x02, 108}, + {22, 0x03, 108}, + {1, 0x02, 109}, + {22, 0x03, 109}, + {1, 0x02, 110}, + {22, 0x03, 110}, + {1, 0x02, 112}, + {22, 0x03, 112}, + {1, 0x02, 114}, + {22, 0x03, 114}, + {1, 0x02, 117}, + {22, 0x03, 117}, + {0, 0x03, 58}, + {0, 0x03, 66}, + {0, 0x03, 67}, + {0, 0x03, 68}, + }, + + { + {2, 0x02, 108}, + {9, 0x02, 108}, + {23, 0x02, 108}, + {40, 0x03, 108}, + {2, 0x02, 109}, + {9, 0x02, 109}, + {23, 0x02, 109}, + {40, 0x03, 109}, + {2, 0x02, 110}, + {9, 0x02, 110}, + {23, 0x02, 110}, + {40, 0x03, 110}, + {2, 0x02, 112}, + {9, 0x02, 112}, + {23, 0x02, 112}, + {40, 0x03, 112}, + }, + + { + {3, 0x02, 108}, + {6, 0x02, 108}, + {10, 0x02, 108}, + {15, 0x02, 108}, + {24, 0x02, 108}, + {31, 0x02, 108}, + {41, 0x02, 108}, + {56, 0x03, 108}, + {3, 0x02, 109}, + {6, 0x02, 109}, + {10, 0x02, 109}, + {15, 0x02, 109}, + {24, 0x02, 109}, + {31, 0x02, 109}, + {41, 0x02, 109}, + {56, 0x03, 109}, + }, + + { + {3, 0x02, 110}, + {6, 0x02, 110}, + {10, 0x02, 110}, + {15, 0x02, 110}, + {24, 0x02, 110}, + {31, 0x02, 110}, + {41, 0x02, 110}, + {56, 0x03, 110}, + {3, 0x02, 112}, + {6, 0x02, 112}, + {10, 0x02, 112}, + {15, 0x02, 112}, + {24, 0x02, 112}, + {31, 0x02, 112}, + {41, 0x02, 112}, + {56, 0x03, 112}, + }, + + { + {2, 0x02, 114}, + {9, 0x02, 114}, + {23, 0x02, 114}, + {40, 0x03, 114}, + {2, 0x02, 117}, + {9, 0x02, 117}, + {23, 0x02, 117}, + {40, 0x03, 117}, + {1, 0x02, 58}, + {22, 0x03, 58}, + {1, 0x02, 66}, + {22, 0x03, 66}, + {1, 0x02, 67}, + {22, 0x03, 67}, + {1, 0x02, 68}, + {22, 0x03, 68}, + }, + + { + {3, 0x02, 114}, + {6, 0x02, 114}, + {10, 0x02, 114}, + {15, 0x02, 114}, + {24, 0x02, 114}, + {31, 0x02, 114}, + {41, 0x02, 114}, + {56, 0x03, 114}, + {3, 0x02, 117}, + {6, 0x02, 117}, + {10, 0x02, 117}, + {15, 0x02, 117}, + {24, 0x02, 117}, + {31, 0x02, 117}, + {41, 0x02, 117}, + {56, 0x03, 117}, + }, + + { + {2, 0x02, 58}, + {9, 0x02, 58}, + {23, 0x02, 58}, + {40, 0x03, 58}, + {2, 0x02, 66}, + {9, 0x02, 66}, + {23, 0x02, 66}, + {40, 0x03, 66}, + {2, 0x02, 67}, + {9, 0x02, 67}, + {23, 0x02, 67}, + {40, 0x03, 67}, + {2, 0x02, 68}, + {9, 0x02, 68}, + {23, 0x02, 68}, + {40, 0x03, 68}, + }, + + { + {3, 0x02, 58}, + {6, 0x02, 58}, + {10, 0x02, 58}, + {15, 0x02, 58}, + {24, 0x02, 58}, + {31, 0x02, 58}, + {41, 0x02, 58}, + {56, 0x03, 58}, + {3, 0x02, 66}, + {6, 0x02, 66}, + {10, 0x02, 66}, + {15, 0x02, 66}, + {24, 0x02, 66}, + {31, 0x02, 66}, + {41, 0x02, 66}, + {56, 0x03, 66}, + }, + + { + {3, 0x02, 67}, + {6, 0x02, 67}, + {10, 0x02, 67}, + {15, 0x02, 67}, + {24, 0x02, 67}, + {31, 0x02, 67}, + {41, 0x02, 67}, + {56, 0x03, 67}, + {3, 0x02, 68}, + {6, 0x02, 68}, + {10, 0x02, 68}, + {15, 0x02, 68}, + {24, 0x02, 68}, + {31, 0x02, 68}, + {41, 0x02, 68}, + {56, 0x03, 68}, + }, + + { + {44, 0x00, 0}, + {45, 0x00, 0}, + {47, 0x00, 0}, + {48, 0x00, 0}, + {51, 0x00, 0}, + {52, 0x00, 0}, + {54, 0x00, 0}, + {55, 0x00, 0}, + {59, 0x00, 0}, + {60, 0x00, 0}, + {62, 0x00, 0}, + {63, 0x00, 0}, + {66, 0x00, 0}, + {67, 0x00, 0}, + {69, 0x00, 0}, + {72, 0x01, 0}, + }, + + { + {0, 0x03, 69}, + {0, 0x03, 70}, + {0, 0x03, 71}, + {0, 0x03, 72}, + {0, 0x03, 73}, + {0, 0x03, 74}, + {0, 0x03, 75}, + {0, 0x03, 76}, + {0, 0x03, 77}, + {0, 0x03, 78}, + {0, 0x03, 79}, + {0, 0x03, 80}, + {0, 0x03, 81}, + {0, 0x03, 82}, + {0, 0x03, 83}, + {0, 0x03, 84}, + }, + + { + {1, 0x02, 69}, + {22, 0x03, 69}, + {1, 0x02, 70}, + {22, 0x03, 70}, + {1, 0x02, 71}, + {22, 0x03, 71}, + {1, 0x02, 72}, + {22, 0x03, 72}, + {1, 0x02, 73}, + {22, 0x03, 73}, + {1, 0x02, 74}, + {22, 0x03, 74}, + {1, 0x02, 75}, + {22, 0x03, 75}, + {1, 0x02, 76}, + {22, 0x03, 76}, + }, + + { + {2, 0x02, 69}, + {9, 0x02, 69}, + {23, 0x02, 69}, + {40, 0x03, 69}, + {2, 0x02, 70}, + {9, 0x02, 70}, + {23, 0x02, 70}, + {40, 0x03, 70}, + {2, 0x02, 71}, + {9, 0x02, 71}, + {23, 0x02, 71}, + {40, 0x03, 71}, + {2, 0x02, 72}, + {9, 0x02, 72}, + {23, 0x02, 72}, + {40, 0x03, 72}, + }, + + { + {3, 0x02, 69}, + {6, 0x02, 69}, + {10, 0x02, 69}, + {15, 0x02, 69}, + {24, 0x02, 69}, + {31, 0x02, 69}, + {41, 0x02, 69}, + {56, 0x03, 69}, + {3, 0x02, 70}, + {6, 0x02, 70}, + {10, 0x02, 70}, + {15, 0x02, 70}, + {24, 0x02, 70}, + {31, 0x02, 70}, + {41, 0x02, 70}, + {56, 0x03, 70}, + }, + + { + {3, 0x02, 71}, + {6, 0x02, 71}, + {10, 0x02, 71}, + {15, 0x02, 71}, + {24, 0x02, 71}, + {31, 0x02, 71}, + {41, 0x02, 71}, + {56, 0x03, 71}, + {3, 0x02, 72}, + {6, 0x02, 72}, + {10, 0x02, 72}, + {15, 0x02, 72}, + {24, 0x02, 72}, + {31, 0x02, 72}, + {41, 0x02, 72}, + {56, 0x03, 72}, + }, + + { + {2, 0x02, 73}, + {9, 0x02, 73}, + {23, 0x02, 73}, + {40, 0x03, 73}, + {2, 0x02, 74}, + {9, 0x02, 74}, + {23, 0x02, 74}, + {40, 0x03, 74}, + {2, 0x02, 75}, + {9, 0x02, 75}, + {23, 0x02, 75}, + {40, 0x03, 75}, + {2, 0x02, 76}, + {9, 0x02, 76}, + {23, 0x02, 76}, + {40, 0x03, 76}, + }, + + { + {3, 0x02, 73}, + {6, 0x02, 73}, + {10, 0x02, 73}, + {15, 0x02, 73}, + {24, 0x02, 73}, + {31, 0x02, 73}, + {41, 0x02, 73}, + {56, 0x03, 73}, + {3, 0x02, 74}, + {6, 0x02, 74}, + {10, 0x02, 74}, + {15, 0x02, 74}, + {24, 0x02, 74}, + {31, 0x02, 74}, + {41, 0x02, 74}, + {56, 0x03, 74}, + }, + + { + {3, 0x02, 75}, + {6, 0x02, 75}, + {10, 0x02, 75}, + {15, 0x02, 75}, + {24, 0x02, 75}, + {31, 0x02, 75}, + {41, 0x02, 75}, + {56, 0x03, 75}, + {3, 0x02, 76}, + {6, 0x02, 76}, + {10, 0x02, 76}, + {15, 0x02, 76}, + {24, 0x02, 76}, + {31, 0x02, 76}, + {41, 0x02, 76}, + {56, 0x03, 76}, + }, + + { + {1, 0x02, 77}, + {22, 0x03, 77}, + {1, 0x02, 78}, + {22, 0x03, 78}, + {1, 0x02, 79}, + {22, 0x03, 79}, + {1, 0x02, 80}, + {22, 0x03, 80}, + {1, 0x02, 81}, + {22, 0x03, 81}, + {1, 0x02, 82}, + {22, 0x03, 82}, + {1, 0x02, 83}, + {22, 0x03, 83}, + {1, 0x02, 84}, + {22, 0x03, 84}, + }, + + { + {2, 0x02, 77}, + {9, 0x02, 77}, + {23, 0x02, 77}, + {40, 0x03, 77}, + {2, 0x02, 78}, + {9, 0x02, 78}, + {23, 0x02, 78}, + {40, 0x03, 78}, + {2, 0x02, 79}, + {9, 0x02, 79}, + {23, 0x02, 79}, + {40, 0x03, 79}, + {2, 0x02, 80}, + {9, 0x02, 80}, + {23, 0x02, 80}, + {40, 0x03, 80}, + }, + + { + {3, 0x02, 77}, + {6, 0x02, 77}, + {10, 0x02, 77}, + {15, 0x02, 77}, + {24, 0x02, 77}, + {31, 0x02, 77}, + {41, 0x02, 77}, + {56, 0x03, 77}, + {3, 0x02, 78}, + {6, 0x02, 78}, + {10, 0x02, 78}, + {15, 0x02, 78}, + {24, 0x02, 78}, + {31, 0x02, 78}, + {41, 0x02, 78}, + {56, 0x03, 78}, + }, + + { + {3, 0x02, 79}, + {6, 0x02, 79}, + {10, 0x02, 79}, + {15, 0x02, 79}, + {24, 0x02, 79}, + {31, 0x02, 79}, + {41, 0x02, 79}, + {56, 0x03, 79}, + {3, 0x02, 80}, + {6, 0x02, 80}, + {10, 0x02, 80}, + {15, 0x02, 80}, + {24, 0x02, 80}, + {31, 0x02, 80}, + {41, 0x02, 80}, + {56, 0x03, 80}, + }, + + { + {2, 0x02, 81}, + {9, 0x02, 81}, + {23, 0x02, 81}, + {40, 0x03, 81}, + {2, 0x02, 82}, + {9, 0x02, 82}, + {23, 0x02, 82}, + {40, 0x03, 82}, + {2, 0x02, 83}, + {9, 0x02, 83}, + {23, 0x02, 83}, + {40, 0x03, 83}, + {2, 0x02, 84}, + {9, 0x02, 84}, + {23, 0x02, 84}, + {40, 0x03, 84}, + }, + + { + {3, 0x02, 81}, + {6, 0x02, 81}, + {10, 0x02, 81}, + {15, 0x02, 81}, + {24, 0x02, 81}, + {31, 0x02, 81}, + {41, 0x02, 81}, + {56, 0x03, 81}, + {3, 0x02, 82}, + {6, 0x02, 82}, + {10, 0x02, 82}, + {15, 0x02, 82}, + {24, 0x02, 82}, + {31, 0x02, 82}, + {41, 0x02, 82}, + {56, 0x03, 82}, + }, + + { + {3, 0x02, 83}, + {6, 0x02, 83}, + {10, 0x02, 83}, + {15, 0x02, 83}, + {24, 0x02, 83}, + {31, 0x02, 83}, + {41, 0x02, 83}, + {56, 0x03, 83}, + {3, 0x02, 84}, + {6, 0x02, 84}, + {10, 0x02, 84}, + {15, 0x02, 84}, + {24, 0x02, 84}, + {31, 0x02, 84}, + {41, 0x02, 84}, + {56, 0x03, 84}, + }, + + { + {0, 0x03, 85}, + {0, 0x03, 86}, + {0, 0x03, 87}, + {0, 0x03, 89}, + {0, 0x03, 106}, + {0, 0x03, 107}, + {0, 0x03, 113}, + {0, 0x03, 118}, + {0, 0x03, 119}, + {0, 0x03, 120}, + {0, 0x03, 121}, + {0, 0x03, 122}, + {70, 0x00, 0}, + {71, 0x00, 0}, + {73, 0x00, 0}, + {74, 0x01, 0}, + }, + + { + {1, 0x02, 85}, + {22, 0x03, 85}, + {1, 0x02, 86}, + {22, 0x03, 86}, + {1, 0x02, 87}, + {22, 0x03, 87}, + {1, 0x02, 89}, + {22, 0x03, 89}, + {1, 0x02, 106}, + {22, 0x03, 106}, + {1, 0x02, 107}, + {22, 0x03, 107}, + {1, 0x02, 113}, + {22, 0x03, 113}, + {1, 0x02, 118}, + {22, 0x03, 118}, + }, + + { + {2, 0x02, 85}, + {9, 0x02, 85}, + {23, 0x02, 85}, + {40, 0x03, 85}, + {2, 0x02, 86}, + {9, 0x02, 86}, + {23, 0x02, 86}, + {40, 0x03, 86}, + {2, 0x02, 87}, + {9, 0x02, 87}, + {23, 0x02, 87}, + {40, 0x03, 87}, + {2, 0x02, 89}, + {9, 0x02, 89}, + {23, 0x02, 89}, + {40, 0x03, 89}, + }, + + { + {3, 0x02, 85}, + {6, 0x02, 85}, + {10, 0x02, 85}, + {15, 0x02, 85}, + {24, 0x02, 85}, + {31, 0x02, 85}, + {41, 0x02, 85}, + {56, 0x03, 85}, + {3, 0x02, 86}, + {6, 0x02, 86}, + {10, 0x02, 86}, + {15, 0x02, 86}, + {24, 0x02, 86}, + {31, 0x02, 86}, + {41, 0x02, 86}, + {56, 0x03, 86}, + }, + + { + {3, 0x02, 87}, + {6, 0x02, 87}, + {10, 0x02, 87}, + {15, 0x02, 87}, + {24, 0x02, 87}, + {31, 0x02, 87}, + {41, 0x02, 87}, + {56, 0x03, 87}, + {3, 0x02, 89}, + {6, 0x02, 89}, + {10, 0x02, 89}, + {15, 0x02, 89}, + {24, 0x02, 89}, + {31, 0x02, 89}, + {41, 0x02, 89}, + {56, 0x03, 89}, + }, + + { + {2, 0x02, 106}, + {9, 0x02, 106}, + {23, 0x02, 106}, + {40, 0x03, 106}, + {2, 0x02, 107}, + {9, 0x02, 107}, + {23, 0x02, 107}, + {40, 0x03, 107}, + {2, 0x02, 113}, + {9, 0x02, 113}, + {23, 0x02, 113}, + {40, 0x03, 113}, + {2, 0x02, 118}, + {9, 0x02, 118}, + {23, 0x02, 118}, + {40, 0x03, 118}, + }, + + { + {3, 0x02, 106}, + {6, 0x02, 106}, + {10, 0x02, 106}, + {15, 0x02, 106}, + {24, 0x02, 106}, + {31, 0x02, 106}, + {41, 0x02, 106}, + {56, 0x03, 106}, + {3, 0x02, 107}, + {6, 0x02, 107}, + {10, 0x02, 107}, + {15, 0x02, 107}, + {24, 0x02, 107}, + {31, 0x02, 107}, + {41, 0x02, 107}, + {56, 0x03, 107}, + }, + + { + {3, 0x02, 113}, + {6, 0x02, 113}, + {10, 0x02, 113}, + {15, 0x02, 113}, + {24, 0x02, 113}, + {31, 0x02, 113}, + {41, 0x02, 113}, + {56, 0x03, 113}, + {3, 0x02, 118}, + {6, 0x02, 118}, + {10, 0x02, 118}, + {15, 0x02, 118}, + {24, 0x02, 118}, + {31, 0x02, 118}, + {41, 0x02, 118}, + {56, 0x03, 118}, + }, + + { + {1, 0x02, 119}, + {22, 0x03, 119}, + {1, 0x02, 120}, + {22, 0x03, 120}, + {1, 0x02, 121}, + {22, 0x03, 121}, + {1, 0x02, 122}, + {22, 0x03, 122}, + {0, 0x03, 38}, + {0, 0x03, 42}, + {0, 0x03, 44}, + {0, 0x03, 59}, + {0, 0x03, 88}, + {0, 0x03, 90}, + {75, 0x00, 0}, + {78, 0x00, 0}, + }, + + { + {2, 0x02, 119}, + {9, 0x02, 119}, + {23, 0x02, 119}, + {40, 0x03, 119}, + {2, 0x02, 120}, + {9, 0x02, 120}, + {23, 0x02, 120}, + {40, 0x03, 120}, + {2, 0x02, 121}, + {9, 0x02, 121}, + {23, 0x02, 121}, + {40, 0x03, 121}, + {2, 0x02, 122}, + {9, 0x02, 122}, + {23, 0x02, 122}, + {40, 0x03, 122}, + }, + + { + {3, 0x02, 119}, + {6, 0x02, 119}, + {10, 0x02, 119}, + {15, 0x02, 119}, + {24, 0x02, 119}, + {31, 0x02, 119}, + {41, 0x02, 119}, + {56, 0x03, 119}, + {3, 0x02, 120}, + {6, 0x02, 120}, + {10, 0x02, 120}, + {15, 0x02, 120}, + {24, 0x02, 120}, + {31, 0x02, 120}, + {41, 0x02, 120}, + {56, 0x03, 120}, + }, + + { + {3, 0x02, 121}, + {6, 0x02, 121}, + {10, 0x02, 121}, + {15, 0x02, 121}, + {24, 0x02, 121}, + {31, 0x02, 121}, + {41, 0x02, 121}, + {56, 0x03, 121}, + {3, 0x02, 122}, + {6, 0x02, 122}, + {10, 0x02, 122}, + {15, 0x02, 122}, + {24, 0x02, 122}, + {31, 0x02, 122}, + {41, 0x02, 122}, + {56, 0x03, 122}, + }, + + { + {1, 0x02, 38}, + {22, 0x03, 38}, + {1, 0x02, 42}, + {22, 0x03, 42}, + {1, 0x02, 44}, + {22, 0x03, 44}, + {1, 0x02, 59}, + {22, 0x03, 59}, + {1, 0x02, 88}, + {22, 0x03, 88}, + {1, 0x02, 90}, + {22, 0x03, 90}, + {76, 0x00, 0}, + {77, 0x00, 0}, + {79, 0x00, 0}, + {81, 0x00, 0}, + }, + + { + {2, 0x02, 38}, + {9, 0x02, 38}, + {23, 0x02, 38}, + {40, 0x03, 38}, + {2, 0x02, 42}, + {9, 0x02, 42}, + {23, 0x02, 42}, + {40, 0x03, 42}, + {2, 0x02, 44}, + {9, 0x02, 44}, + {23, 0x02, 44}, + {40, 0x03, 44}, + {2, 0x02, 59}, + {9, 0x02, 59}, + {23, 0x02, 59}, + {40, 0x03, 59}, + }, + + { + {3, 0x02, 38}, + {6, 0x02, 38}, + {10, 0x02, 38}, + {15, 0x02, 38}, + {24, 0x02, 38}, + {31, 0x02, 38}, + {41, 0x02, 38}, + {56, 0x03, 38}, + {3, 0x02, 42}, + {6, 0x02, 42}, + {10, 0x02, 42}, + {15, 0x02, 42}, + {24, 0x02, 42}, + {31, 0x02, 42}, + {41, 0x02, 42}, + {56, 0x03, 42}, + }, + + { + {3, 0x02, 44}, + {6, 0x02, 44}, + {10, 0x02, 44}, + {15, 0x02, 44}, + {24, 0x02, 44}, + {31, 0x02, 44}, + {41, 0x02, 44}, + {56, 0x03, 44}, + {3, 0x02, 59}, + {6, 0x02, 59}, + {10, 0x02, 59}, + {15, 0x02, 59}, + {24, 0x02, 59}, + {31, 0x02, 59}, + {41, 0x02, 59}, + {56, 0x03, 59}, + }, + + { + {2, 0x02, 88}, + {9, 0x02, 88}, + {23, 0x02, 88}, + {40, 0x03, 88}, + {2, 0x02, 90}, + {9, 0x02, 90}, + {23, 0x02, 90}, + {40, 0x03, 90}, + {0, 0x03, 33}, + {0, 0x03, 34}, + {0, 0x03, 40}, + {0, 0x03, 41}, + {0, 0x03, 63}, + {80, 0x00, 0}, + {82, 0x00, 0}, + {84, 0x00, 0}, + }, + + { + {3, 0x02, 88}, + {6, 0x02, 88}, + {10, 0x02, 88}, + {15, 0x02, 88}, + {24, 0x02, 88}, + {31, 0x02, 88}, + {41, 0x02, 88}, + {56, 0x03, 88}, + {3, 0x02, 90}, + {6, 0x02, 90}, + {10, 0x02, 90}, + {15, 0x02, 90}, + {24, 0x02, 90}, + {31, 0x02, 90}, + {41, 0x02, 90}, + {56, 0x03, 90}, + }, + + { + {1, 0x02, 33}, + {22, 0x03, 33}, + {1, 0x02, 34}, + {22, 0x03, 34}, + {1, 0x02, 40}, + {22, 0x03, 40}, + {1, 0x02, 41}, + {22, 0x03, 41}, + {1, 0x02, 63}, + {22, 0x03, 63}, + {0, 0x03, 39}, + {0, 0x03, 43}, + {0, 0x03, 124}, + {83, 0x00, 0}, + {85, 0x00, 0}, + {88, 0x00, 0}, + }, + + { + {2, 0x02, 33}, + {9, 0x02, 33}, + {23, 0x02, 33}, + {40, 0x03, 33}, + {2, 0x02, 34}, + {9, 0x02, 34}, + {23, 0x02, 34}, + {40, 0x03, 34}, + {2, 0x02, 40}, + {9, 0x02, 40}, + {23, 0x02, 40}, + {40, 0x03, 40}, + {2, 0x02, 41}, + {9, 0x02, 41}, + {23, 0x02, 41}, + {40, 0x03, 41}, + }, + + { + {3, 0x02, 33}, + {6, 0x02, 33}, + {10, 0x02, 33}, + {15, 0x02, 33}, + {24, 0x02, 33}, + {31, 0x02, 33}, + {41, 0x02, 33}, + {56, 0x03, 33}, + {3, 0x02, 34}, + {6, 0x02, 34}, + {10, 0x02, 34}, + {15, 0x02, 34}, + {24, 0x02, 34}, + {31, 0x02, 34}, + {41, 0x02, 34}, + {56, 0x03, 34}, + }, + + { + {3, 0x02, 40}, + {6, 0x02, 40}, + {10, 0x02, 40}, + {15, 0x02, 40}, + {24, 0x02, 40}, + {31, 0x02, 40}, + {41, 0x02, 40}, + {56, 0x03, 40}, + {3, 0x02, 41}, + {6, 0x02, 41}, + {10, 0x02, 41}, + {15, 0x02, 41}, + {24, 0x02, 41}, + {31, 0x02, 41}, + {41, 0x02, 41}, + {56, 0x03, 41}, + }, + + { + {2, 0x02, 63}, + {9, 0x02, 63}, + {23, 0x02, 63}, + {40, 0x03, 63}, + {1, 0x02, 39}, + {22, 0x03, 39}, + {1, 0x02, 43}, + {22, 0x03, 43}, + {1, 0x02, 124}, + {22, 0x03, 124}, + {0, 0x03, 35}, + {0, 0x03, 62}, + {86, 0x00, 0}, + {87, 0x00, 0}, + {89, 0x00, 0}, + {90, 0x00, 0}, + }, + + { + {3, 0x02, 63}, + {6, 0x02, 63}, + {10, 0x02, 63}, + {15, 0x02, 63}, + {24, 0x02, 63}, + {31, 0x02, 63}, + {41, 0x02, 63}, + {56, 0x03, 63}, + {2, 0x02, 39}, + {9, 0x02, 39}, + {23, 0x02, 39}, + {40, 0x03, 39}, + {2, 0x02, 43}, + {9, 0x02, 43}, + {23, 0x02, 43}, + {40, 0x03, 43}, + }, + + { + {3, 0x02, 39}, + {6, 0x02, 39}, + {10, 0x02, 39}, + {15, 0x02, 39}, + {24, 0x02, 39}, + {31, 0x02, 39}, + {41, 0x02, 39}, + {56, 0x03, 39}, + {3, 0x02, 43}, + {6, 0x02, 43}, + {10, 0x02, 43}, + {15, 0x02, 43}, + {24, 0x02, 43}, + {31, 0x02, 43}, + {41, 0x02, 43}, + {56, 0x03, 43}, + }, + + { + {2, 0x02, 124}, + {9, 0x02, 124}, + {23, 0x02, 124}, + {40, 0x03, 124}, + {1, 0x02, 35}, + {22, 0x03, 35}, + {1, 0x02, 62}, + {22, 0x03, 62}, + {0, 0x03, 0}, + {0, 0x03, 36}, + {0, 0x03, 64}, + {0, 0x03, 91}, + {0, 0x03, 93}, + {0, 0x03, 126}, + {91, 0x00, 0}, + {92, 0x00, 0}, + }, + + { + {3, 0x02, 124}, + {6, 0x02, 124}, + {10, 0x02, 124}, + {15, 0x02, 124}, + {24, 0x02, 124}, + {31, 0x02, 124}, + {41, 0x02, 124}, + {56, 0x03, 124}, + {2, 0x02, 35}, + {9, 0x02, 35}, + {23, 0x02, 35}, + {40, 0x03, 35}, + {2, 0x02, 62}, + {9, 0x02, 62}, + {23, 0x02, 62}, + {40, 0x03, 62}, + }, + + { + {3, 0x02, 35}, + {6, 0x02, 35}, + {10, 0x02, 35}, + {15, 0x02, 35}, + {24, 0x02, 35}, + {31, 0x02, 35}, + {41, 0x02, 35}, + {56, 0x03, 35}, + {3, 0x02, 62}, + {6, 0x02, 62}, + {10, 0x02, 62}, + {15, 0x02, 62}, + {24, 0x02, 62}, + {31, 0x02, 62}, + {41, 0x02, 62}, + {56, 0x03, 62}, + }, + + { + {1, 0x02, 0}, + {22, 0x03, 0}, + {1, 0x02, 36}, + {22, 0x03, 36}, + {1, 0x02, 64}, + {22, 0x03, 64}, + {1, 0x02, 91}, + {22, 0x03, 91}, + {1, 0x02, 93}, + {22, 0x03, 93}, + {1, 0x02, 126}, + {22, 0x03, 126}, + {0, 0x03, 94}, + {0, 0x03, 125}, + {93, 0x00, 0}, + {94, 0x00, 0}, + }, + + { + {2, 0x02, 0}, + {9, 0x02, 0}, + {23, 0x02, 0}, + {40, 0x03, 0}, + {2, 0x02, 36}, + {9, 0x02, 36}, + {23, 0x02, 36}, + {40, 0x03, 36}, + {2, 0x02, 64}, + {9, 0x02, 64}, + {23, 0x02, 64}, + {40, 0x03, 64}, + {2, 0x02, 91}, + {9, 0x02, 91}, + {23, 0x02, 91}, + {40, 0x03, 91}, + }, + + { + {3, 0x02, 0}, + {6, 0x02, 0}, + {10, 0x02, 0}, + {15, 0x02, 0}, + {24, 0x02, 0}, + {31, 0x02, 0}, + {41, 0x02, 0}, + {56, 0x03, 0}, + {3, 0x02, 36}, + {6, 0x02, 36}, + {10, 0x02, 36}, + {15, 0x02, 36}, + {24, 0x02, 36}, + {31, 0x02, 36}, + {41, 0x02, 36}, + {56, 0x03, 36}, + }, + + { + {3, 0x02, 64}, + {6, 0x02, 64}, + {10, 0x02, 64}, + {15, 0x02, 64}, + {24, 0x02, 64}, + {31, 0x02, 64}, + {41, 0x02, 64}, + {56, 0x03, 64}, + {3, 0x02, 91}, + {6, 0x02, 91}, + {10, 0x02, 91}, + {15, 0x02, 91}, + {24, 0x02, 91}, + {31, 0x02, 91}, + {41, 0x02, 91}, + {56, 0x03, 91}, + }, + + { + {2, 0x02, 93}, + {9, 0x02, 93}, + {23, 0x02, 93}, + {40, 0x03, 93}, + {2, 0x02, 126}, + {9, 0x02, 126}, + {23, 0x02, 126}, + {40, 0x03, 126}, + {1, 0x02, 94}, + {22, 0x03, 94}, + {1, 0x02, 125}, + {22, 0x03, 125}, + {0, 0x03, 60}, + {0, 0x03, 96}, + {0, 0x03, 123}, + {95, 0x00, 0}, + }, + + { + {3, 0x02, 93}, + {6, 0x02, 93}, + {10, 0x02, 93}, + {15, 0x02, 93}, + {24, 0x02, 93}, + {31, 0x02, 93}, + {41, 0x02, 93}, + {56, 0x03, 93}, + {3, 0x02, 126}, + {6, 0x02, 126}, + {10, 0x02, 126}, + {15, 0x02, 126}, + {24, 0x02, 126}, + {31, 0x02, 126}, + {41, 0x02, 126}, + {56, 0x03, 126}, + }, + + { + {2, 0x02, 94}, + {9, 0x02, 94}, + {23, 0x02, 94}, + {40, 0x03, 94}, + {2, 0x02, 125}, + {9, 0x02, 125}, + {23, 0x02, 125}, + {40, 0x03, 125}, + {1, 0x02, 60}, + {22, 0x03, 60}, + {1, 0x02, 96}, + {22, 0x03, 96}, + {1, 0x02, 123}, + {22, 0x03, 123}, + {96, 0x00, 0}, + {110, 0x00, 0}, + }, + + { + {3, 0x02, 94}, + {6, 0x02, 94}, + {10, 0x02, 94}, + {15, 0x02, 94}, + {24, 0x02, 94}, + {31, 0x02, 94}, + {41, 0x02, 94}, + {56, 0x03, 94}, + {3, 0x02, 125}, + {6, 0x02, 125}, + {10, 0x02, 125}, + {15, 0x02, 125}, + {24, 0x02, 125}, + {31, 0x02, 125}, + {41, 0x02, 125}, + {56, 0x03, 125}, + }, + + { + {2, 0x02, 60}, + {9, 0x02, 60}, + {23, 0x02, 60}, + {40, 0x03, 60}, + {2, 0x02, 96}, + {9, 0x02, 96}, + {23, 0x02, 96}, + {40, 0x03, 96}, + {2, 0x02, 123}, + {9, 0x02, 123}, + {23, 0x02, 123}, + {40, 0x03, 123}, + {97, 0x00, 0}, + {101, 0x00, 0}, + {111, 0x00, 0}, + {133, 0x00, 0}, + }, + + { + {3, 0x02, 60}, + {6, 0x02, 60}, + {10, 0x02, 60}, + {15, 0x02, 60}, + {24, 0x02, 60}, + {31, 0x02, 60}, + {41, 0x02, 60}, + {56, 0x03, 60}, + {3, 0x02, 96}, + {6, 0x02, 96}, + {10, 0x02, 96}, + {15, 0x02, 96}, + {24, 0x02, 96}, + {31, 0x02, 96}, + {41, 0x02, 96}, + {56, 0x03, 96}, + }, + + { + {3, 0x02, 123}, + {6, 0x02, 123}, + {10, 0x02, 123}, + {15, 0x02, 123}, + {24, 0x02, 123}, + {31, 0x02, 123}, + {41, 0x02, 123}, + {56, 0x03, 123}, + {98, 0x00, 0}, + {99, 0x00, 0}, + {102, 0x00, 0}, + {105, 0x00, 0}, + {112, 0x00, 0}, + {119, 0x00, 0}, + {134, 0x00, 0}, + {153, 0x00, 0}, + }, + + { + {0, 0x03, 92}, + {0, 0x03, 195}, + {0, 0x03, 208}, + {100, 0x00, 0}, + {103, 0x00, 0}, + {104, 0x00, 0}, + {106, 0x00, 0}, + {107, 0x00, 0}, + {113, 0x00, 0}, + {116, 0x00, 0}, + {120, 0x00, 0}, + {126, 0x00, 0}, + {135, 0x00, 0}, + {142, 0x00, 0}, + {154, 0x00, 0}, + {169, 0x00, 0}, + }, + + { + {1, 0x02, 92}, + {22, 0x03, 92}, + {1, 0x02, 195}, + {22, 0x03, 195}, + {1, 0x02, 208}, + {22, 0x03, 208}, + {0, 0x03, 128}, + {0, 0x03, 130}, + {0, 0x03, 131}, + {0, 0x03, 162}, + {0, 0x03, 184}, + {0, 0x03, 194}, + {0, 0x03, 224}, + {0, 0x03, 226}, + {108, 0x00, 0}, + {109, 0x00, 0}, + }, + + { + {2, 0x02, 92}, + {9, 0x02, 92}, + {23, 0x02, 92}, + {40, 0x03, 92}, + {2, 0x02, 195}, + {9, 0x02, 195}, + {23, 0x02, 195}, + {40, 0x03, 195}, + {2, 0x02, 208}, + {9, 0x02, 208}, + {23, 0x02, 208}, + {40, 0x03, 208}, + {1, 0x02, 128}, + {22, 0x03, 128}, + {1, 0x02, 130}, + {22, 0x03, 130}, + }, + + { + {3, 0x02, 92}, + {6, 0x02, 92}, + {10, 0x02, 92}, + {15, 0x02, 92}, + {24, 0x02, 92}, + {31, 0x02, 92}, + {41, 0x02, 92}, + {56, 0x03, 92}, + {3, 0x02, 195}, + {6, 0x02, 195}, + {10, 0x02, 195}, + {15, 0x02, 195}, + {24, 0x02, 195}, + {31, 0x02, 195}, + {41, 0x02, 195}, + {56, 0x03, 195}, + }, + + { + {3, 0x02, 208}, + {6, 0x02, 208}, + {10, 0x02, 208}, + {15, 0x02, 208}, + {24, 0x02, 208}, + {31, 0x02, 208}, + {41, 0x02, 208}, + {56, 0x03, 208}, + {2, 0x02, 128}, + {9, 0x02, 128}, + {23, 0x02, 128}, + {40, 0x03, 128}, + {2, 0x02, 130}, + {9, 0x02, 130}, + {23, 0x02, 130}, + {40, 0x03, 130}, + }, + + { + {3, 0x02, 128}, + {6, 0x02, 128}, + {10, 0x02, 128}, + {15, 0x02, 128}, + {24, 0x02, 128}, + {31, 0x02, 128}, + {41, 0x02, 128}, + {56, 0x03, 128}, + {3, 0x02, 130}, + {6, 0x02, 130}, + {10, 0x02, 130}, + {15, 0x02, 130}, + {24, 0x02, 130}, + {31, 0x02, 130}, + {41, 0x02, 130}, + {56, 0x03, 130}, + }, + + { + {1, 0x02, 131}, + {22, 0x03, 131}, + {1, 0x02, 162}, + {22, 0x03, 162}, + {1, 0x02, 184}, + {22, 0x03, 184}, + {1, 0x02, 194}, + {22, 0x03, 194}, + {1, 0x02, 224}, + {22, 0x03, 224}, + {1, 0x02, 226}, + {22, 0x03, 226}, + {0, 0x03, 153}, + {0, 0x03, 161}, + {0, 0x03, 167}, + {0, 0x03, 172}, + }, + + { + {2, 0x02, 131}, + {9, 0x02, 131}, + {23, 0x02, 131}, + {40, 0x03, 131}, + {2, 0x02, 162}, + {9, 0x02, 162}, + {23, 0x02, 162}, + {40, 0x03, 162}, + {2, 0x02, 184}, + {9, 0x02, 184}, + {23, 0x02, 184}, + {40, 0x03, 184}, + {2, 0x02, 194}, + {9, 0x02, 194}, + {23, 0x02, 194}, + {40, 0x03, 194}, + }, + + { + {3, 0x02, 131}, + {6, 0x02, 131}, + {10, 0x02, 131}, + {15, 0x02, 131}, + {24, 0x02, 131}, + {31, 0x02, 131}, + {41, 0x02, 131}, + {56, 0x03, 131}, + {3, 0x02, 162}, + {6, 0x02, 162}, + {10, 0x02, 162}, + {15, 0x02, 162}, + {24, 0x02, 162}, + {31, 0x02, 162}, + {41, 0x02, 162}, + {56, 0x03, 162}, + }, + + { + {3, 0x02, 184}, + {6, 0x02, 184}, + {10, 0x02, 184}, + {15, 0x02, 184}, + {24, 0x02, 184}, + {31, 0x02, 184}, + {41, 0x02, 184}, + {56, 0x03, 184}, + {3, 0x02, 194}, + {6, 0x02, 194}, + {10, 0x02, 194}, + {15, 0x02, 194}, + {24, 0x02, 194}, + {31, 0x02, 194}, + {41, 0x02, 194}, + {56, 0x03, 194}, + }, + + { + {2, 0x02, 224}, + {9, 0x02, 224}, + {23, 0x02, 224}, + {40, 0x03, 224}, + {2, 0x02, 226}, + {9, 0x02, 226}, + {23, 0x02, 226}, + {40, 0x03, 226}, + {1, 0x02, 153}, + {22, 0x03, 153}, + {1, 0x02, 161}, + {22, 0x03, 161}, + {1, 0x02, 167}, + {22, 0x03, 167}, + {1, 0x02, 172}, + {22, 0x03, 172}, + }, + + { + {3, 0x02, 224}, + {6, 0x02, 224}, + {10, 0x02, 224}, + {15, 0x02, 224}, + {24, 0x02, 224}, + {31, 0x02, 224}, + {41, 0x02, 224}, + {56, 0x03, 224}, + {3, 0x02, 226}, + {6, 0x02, 226}, + {10, 0x02, 226}, + {15, 0x02, 226}, + {24, 0x02, 226}, + {31, 0x02, 226}, + {41, 0x02, 226}, + {56, 0x03, 226}, + }, + + { + {2, 0x02, 153}, + {9, 0x02, 153}, + {23, 0x02, 153}, + {40, 0x03, 153}, + {2, 0x02, 161}, + {9, 0x02, 161}, + {23, 0x02, 161}, + {40, 0x03, 161}, + {2, 0x02, 167}, + {9, 0x02, 167}, + {23, 0x02, 167}, + {40, 0x03, 167}, + {2, 0x02, 172}, + {9, 0x02, 172}, + {23, 0x02, 172}, + {40, 0x03, 172}, + }, + + { + {3, 0x02, 153}, + {6, 0x02, 153}, + {10, 0x02, 153}, + {15, 0x02, 153}, + {24, 0x02, 153}, + {31, 0x02, 153}, + {41, 0x02, 153}, + {56, 0x03, 153}, + {3, 0x02, 161}, + {6, 0x02, 161}, + {10, 0x02, 161}, + {15, 0x02, 161}, + {24, 0x02, 161}, + {31, 0x02, 161}, + {41, 0x02, 161}, + {56, 0x03, 161}, + }, + + { + {3, 0x02, 167}, + {6, 0x02, 167}, + {10, 0x02, 167}, + {15, 0x02, 167}, + {24, 0x02, 167}, + {31, 0x02, 167}, + {41, 0x02, 167}, + {56, 0x03, 167}, + {3, 0x02, 172}, + {6, 0x02, 172}, + {10, 0x02, 172}, + {15, 0x02, 172}, + {24, 0x02, 172}, + {31, 0x02, 172}, + {41, 0x02, 172}, + {56, 0x03, 172}, + }, + + { + {114, 0x00, 0}, + {115, 0x00, 0}, + {117, 0x00, 0}, + {118, 0x00, 0}, + {121, 0x00, 0}, + {123, 0x00, 0}, + {127, 0x00, 0}, + {130, 0x00, 0}, + {136, 0x00, 0}, + {139, 0x00, 0}, + {143, 0x00, 0}, + {146, 0x00, 0}, + {155, 0x00, 0}, + {162, 0x00, 0}, + {170, 0x00, 0}, + {180, 0x00, 0}, + }, + + { + {0, 0x03, 176}, + {0, 0x03, 177}, + {0, 0x03, 179}, + {0, 0x03, 209}, + {0, 0x03, 216}, + {0, 0x03, 217}, + {0, 0x03, 227}, + {0, 0x03, 229}, + {0, 0x03, 230}, + {122, 0x00, 0}, + {124, 0x00, 0}, + {125, 0x00, 0}, + {128, 0x00, 0}, + {129, 0x00, 0}, + {131, 0x00, 0}, + {132, 0x00, 0}, + }, + + { + {1, 0x02, 176}, + {22, 0x03, 176}, + {1, 0x02, 177}, + {22, 0x03, 177}, + {1, 0x02, 179}, + {22, 0x03, 179}, + {1, 0x02, 209}, + {22, 0x03, 209}, + {1, 0x02, 216}, + {22, 0x03, 216}, + {1, 0x02, 217}, + {22, 0x03, 217}, + {1, 0x02, 227}, + {22, 0x03, 227}, + {1, 0x02, 229}, + {22, 0x03, 229}, + }, + + { + {2, 0x02, 176}, + {9, 0x02, 176}, + {23, 0x02, 176}, + {40, 0x03, 176}, + {2, 0x02, 177}, + {9, 0x02, 177}, + {23, 0x02, 177}, + {40, 0x03, 177}, + {2, 0x02, 179}, + {9, 0x02, 179}, + {23, 0x02, 179}, + {40, 0x03, 179}, + {2, 0x02, 209}, + {9, 0x02, 209}, + {23, 0x02, 209}, + {40, 0x03, 209}, + }, + + { + {3, 0x02, 176}, + {6, 0x02, 176}, + {10, 0x02, 176}, + {15, 0x02, 176}, + {24, 0x02, 176}, + {31, 0x02, 176}, + {41, 0x02, 176}, + {56, 0x03, 176}, + {3, 0x02, 177}, + {6, 0x02, 177}, + {10, 0x02, 177}, + {15, 0x02, 177}, + {24, 0x02, 177}, + {31, 0x02, 177}, + {41, 0x02, 177}, + {56, 0x03, 177}, + }, + + { + {3, 0x02, 179}, + {6, 0x02, 179}, + {10, 0x02, 179}, + {15, 0x02, 179}, + {24, 0x02, 179}, + {31, 0x02, 179}, + {41, 0x02, 179}, + {56, 0x03, 179}, + {3, 0x02, 209}, + {6, 0x02, 209}, + {10, 0x02, 209}, + {15, 0x02, 209}, + {24, 0x02, 209}, + {31, 0x02, 209}, + {41, 0x02, 209}, + {56, 0x03, 209}, + }, + + { + {2, 0x02, 216}, + {9, 0x02, 216}, + {23, 0x02, 216}, + {40, 0x03, 216}, + {2, 0x02, 217}, + {9, 0x02, 217}, + {23, 0x02, 217}, + {40, 0x03, 217}, + {2, 0x02, 227}, + {9, 0x02, 227}, + {23, 0x02, 227}, + {40, 0x03, 227}, + {2, 0x02, 229}, + {9, 0x02, 229}, + {23, 0x02, 229}, + {40, 0x03, 229}, + }, + + { + {3, 0x02, 216}, + {6, 0x02, 216}, + {10, 0x02, 216}, + {15, 0x02, 216}, + {24, 0x02, 216}, + {31, 0x02, 216}, + {41, 0x02, 216}, + {56, 0x03, 216}, + {3, 0x02, 217}, + {6, 0x02, 217}, + {10, 0x02, 217}, + {15, 0x02, 217}, + {24, 0x02, 217}, + {31, 0x02, 217}, + {41, 0x02, 217}, + {56, 0x03, 217}, + }, + + { + {3, 0x02, 227}, + {6, 0x02, 227}, + {10, 0x02, 227}, + {15, 0x02, 227}, + {24, 0x02, 227}, + {31, 0x02, 227}, + {41, 0x02, 227}, + {56, 0x03, 227}, + {3, 0x02, 229}, + {6, 0x02, 229}, + {10, 0x02, 229}, + {15, 0x02, 229}, + {24, 0x02, 229}, + {31, 0x02, 229}, + {41, 0x02, 229}, + {56, 0x03, 229}, + }, + + { + {1, 0x02, 230}, + {22, 0x03, 230}, + {0, 0x03, 129}, + {0, 0x03, 132}, + {0, 0x03, 133}, + {0, 0x03, 134}, + {0, 0x03, 136}, + {0, 0x03, 146}, + {0, 0x03, 154}, + {0, 0x03, 156}, + {0, 0x03, 160}, + {0, 0x03, 163}, + {0, 0x03, 164}, + {0, 0x03, 169}, + {0, 0x03, 170}, + {0, 0x03, 173}, + }, + + { + {2, 0x02, 230}, + {9, 0x02, 230}, + {23, 0x02, 230}, + {40, 0x03, 230}, + {1, 0x02, 129}, + {22, 0x03, 129}, + {1, 0x02, 132}, + {22, 0x03, 132}, + {1, 0x02, 133}, + {22, 0x03, 133}, + {1, 0x02, 134}, + {22, 0x03, 134}, + {1, 0x02, 136}, + {22, 0x03, 136}, + {1, 0x02, 146}, + {22, 0x03, 146}, + }, + + { + {3, 0x02, 230}, + {6, 0x02, 230}, + {10, 0x02, 230}, + {15, 0x02, 230}, + {24, 0x02, 230}, + {31, 0x02, 230}, + {41, 0x02, 230}, + {56, 0x03, 230}, + {2, 0x02, 129}, + {9, 0x02, 129}, + {23, 0x02, 129}, + {40, 0x03, 129}, + {2, 0x02, 132}, + {9, 0x02, 132}, + {23, 0x02, 132}, + {40, 0x03, 132}, + }, + + { + {3, 0x02, 129}, + {6, 0x02, 129}, + {10, 0x02, 129}, + {15, 0x02, 129}, + {24, 0x02, 129}, + {31, 0x02, 129}, + {41, 0x02, 129}, + {56, 0x03, 129}, + {3, 0x02, 132}, + {6, 0x02, 132}, + {10, 0x02, 132}, + {15, 0x02, 132}, + {24, 0x02, 132}, + {31, 0x02, 132}, + {41, 0x02, 132}, + {56, 0x03, 132}, + }, + + { + {2, 0x02, 133}, + {9, 0x02, 133}, + {23, 0x02, 133}, + {40, 0x03, 133}, + {2, 0x02, 134}, + {9, 0x02, 134}, + {23, 0x02, 134}, + {40, 0x03, 134}, + {2, 0x02, 136}, + {9, 0x02, 136}, + {23, 0x02, 136}, + {40, 0x03, 136}, + {2, 0x02, 146}, + {9, 0x02, 146}, + {23, 0x02, 146}, + {40, 0x03, 146}, + }, + + { + {3, 0x02, 133}, + {6, 0x02, 133}, + {10, 0x02, 133}, + {15, 0x02, 133}, + {24, 0x02, 133}, + {31, 0x02, 133}, + {41, 0x02, 133}, + {56, 0x03, 133}, + {3, 0x02, 134}, + {6, 0x02, 134}, + {10, 0x02, 134}, + {15, 0x02, 134}, + {24, 0x02, 134}, + {31, 0x02, 134}, + {41, 0x02, 134}, + {56, 0x03, 134}, + }, + + { + {3, 0x02, 136}, + {6, 0x02, 136}, + {10, 0x02, 136}, + {15, 0x02, 136}, + {24, 0x02, 136}, + {31, 0x02, 136}, + {41, 0x02, 136}, + {56, 0x03, 136}, + {3, 0x02, 146}, + {6, 0x02, 146}, + {10, 0x02, 146}, + {15, 0x02, 146}, + {24, 0x02, 146}, + {31, 0x02, 146}, + {41, 0x02, 146}, + {56, 0x03, 146}, + }, + + { + {1, 0x02, 154}, + {22, 0x03, 154}, + {1, 0x02, 156}, + {22, 0x03, 156}, + {1, 0x02, 160}, + {22, 0x03, 160}, + {1, 0x02, 163}, + {22, 0x03, 163}, + {1, 0x02, 164}, + {22, 0x03, 164}, + {1, 0x02, 169}, + {22, 0x03, 169}, + {1, 0x02, 170}, + {22, 0x03, 170}, + {1, 0x02, 173}, + {22, 0x03, 173}, + }, + + { + {2, 0x02, 154}, + {9, 0x02, 154}, + {23, 0x02, 154}, + {40, 0x03, 154}, + {2, 0x02, 156}, + {9, 0x02, 156}, + {23, 0x02, 156}, + {40, 0x03, 156}, + {2, 0x02, 160}, + {9, 0x02, 160}, + {23, 0x02, 160}, + {40, 0x03, 160}, + {2, 0x02, 163}, + {9, 0x02, 163}, + {23, 0x02, 163}, + {40, 0x03, 163}, + }, + + { + {3, 0x02, 154}, + {6, 0x02, 154}, + {10, 0x02, 154}, + {15, 0x02, 154}, + {24, 0x02, 154}, + {31, 0x02, 154}, + {41, 0x02, 154}, + {56, 0x03, 154}, + {3, 0x02, 156}, + {6, 0x02, 156}, + {10, 0x02, 156}, + {15, 0x02, 156}, + {24, 0x02, 156}, + {31, 0x02, 156}, + {41, 0x02, 156}, + {56, 0x03, 156}, + }, + + { + {3, 0x02, 160}, + {6, 0x02, 160}, + {10, 0x02, 160}, + {15, 0x02, 160}, + {24, 0x02, 160}, + {31, 0x02, 160}, + {41, 0x02, 160}, + {56, 0x03, 160}, + {3, 0x02, 163}, + {6, 0x02, 163}, + {10, 0x02, 163}, + {15, 0x02, 163}, + {24, 0x02, 163}, + {31, 0x02, 163}, + {41, 0x02, 163}, + {56, 0x03, 163}, + }, + + { + {2, 0x02, 164}, + {9, 0x02, 164}, + {23, 0x02, 164}, + {40, 0x03, 164}, + {2, 0x02, 169}, + {9, 0x02, 169}, + {23, 0x02, 169}, + {40, 0x03, 169}, + {2, 0x02, 170}, + {9, 0x02, 170}, + {23, 0x02, 170}, + {40, 0x03, 170}, + {2, 0x02, 173}, + {9, 0x02, 173}, + {23, 0x02, 173}, + {40, 0x03, 173}, + }, + + { + {3, 0x02, 164}, + {6, 0x02, 164}, + {10, 0x02, 164}, + {15, 0x02, 164}, + {24, 0x02, 164}, + {31, 0x02, 164}, + {41, 0x02, 164}, + {56, 0x03, 164}, + {3, 0x02, 169}, + {6, 0x02, 169}, + {10, 0x02, 169}, + {15, 0x02, 169}, + {24, 0x02, 169}, + {31, 0x02, 169}, + {41, 0x02, 169}, + {56, 0x03, 169}, + }, + + { + {3, 0x02, 170}, + {6, 0x02, 170}, + {10, 0x02, 170}, + {15, 0x02, 170}, + {24, 0x02, 170}, + {31, 0x02, 170}, + {41, 0x02, 170}, + {56, 0x03, 170}, + {3, 0x02, 173}, + {6, 0x02, 173}, + {10, 0x02, 173}, + {15, 0x02, 173}, + {24, 0x02, 173}, + {31, 0x02, 173}, + {41, 0x02, 173}, + {56, 0x03, 173}, + }, + + { + {137, 0x00, 0}, + {138, 0x00, 0}, + {140, 0x00, 0}, + {141, 0x00, 0}, + {144, 0x00, 0}, + {145, 0x00, 0}, + {147, 0x00, 0}, + {150, 0x00, 0}, + {156, 0x00, 0}, + {159, 0x00, 0}, + {163, 0x00, 0}, + {166, 0x00, 0}, + {171, 0x00, 0}, + {174, 0x00, 0}, + {181, 0x00, 0}, + {190, 0x00, 0}, + }, + + { + {0, 0x03, 178}, + {0, 0x03, 181}, + {0, 0x03, 185}, + {0, 0x03, 186}, + {0, 0x03, 187}, + {0, 0x03, 189}, + {0, 0x03, 190}, + {0, 0x03, 196}, + {0, 0x03, 198}, + {0, 0x03, 228}, + {0, 0x03, 232}, + {0, 0x03, 233}, + {148, 0x00, 0}, + {149, 0x00, 0}, + {151, 0x00, 0}, + {152, 0x00, 0}, + }, + + { + {1, 0x02, 178}, + {22, 0x03, 178}, + {1, 0x02, 181}, + {22, 0x03, 181}, + {1, 0x02, 185}, + {22, 0x03, 185}, + {1, 0x02, 186}, + {22, 0x03, 186}, + {1, 0x02, 187}, + {22, 0x03, 187}, + {1, 0x02, 189}, + {22, 0x03, 189}, + {1, 0x02, 190}, + {22, 0x03, 190}, + {1, 0x02, 196}, + {22, 0x03, 196}, + }, + + { + {2, 0x02, 178}, + {9, 0x02, 178}, + {23, 0x02, 178}, + {40, 0x03, 178}, + {2, 0x02, 181}, + {9, 0x02, 181}, + {23, 0x02, 181}, + {40, 0x03, 181}, + {2, 0x02, 185}, + {9, 0x02, 185}, + {23, 0x02, 185}, + {40, 0x03, 185}, + {2, 0x02, 186}, + {9, 0x02, 186}, + {23, 0x02, 186}, + {40, 0x03, 186}, + }, + + { + {3, 0x02, 178}, + {6, 0x02, 178}, + {10, 0x02, 178}, + {15, 0x02, 178}, + {24, 0x02, 178}, + {31, 0x02, 178}, + {41, 0x02, 178}, + {56, 0x03, 178}, + {3, 0x02, 181}, + {6, 0x02, 181}, + {10, 0x02, 181}, + {15, 0x02, 181}, + {24, 0x02, 181}, + {31, 0x02, 181}, + {41, 0x02, 181}, + {56, 0x03, 181}, + }, + + { + {3, 0x02, 185}, + {6, 0x02, 185}, + {10, 0x02, 185}, + {15, 0x02, 185}, + {24, 0x02, 185}, + {31, 0x02, 185}, + {41, 0x02, 185}, + {56, 0x03, 185}, + {3, 0x02, 186}, + {6, 0x02, 186}, + {10, 0x02, 186}, + {15, 0x02, 186}, + {24, 0x02, 186}, + {31, 0x02, 186}, + {41, 0x02, 186}, + {56, 0x03, 186}, + }, + + { + {2, 0x02, 187}, + {9, 0x02, 187}, + {23, 0x02, 187}, + {40, 0x03, 187}, + {2, 0x02, 189}, + {9, 0x02, 189}, + {23, 0x02, 189}, + {40, 0x03, 189}, + {2, 0x02, 190}, + {9, 0x02, 190}, + {23, 0x02, 190}, + {40, 0x03, 190}, + {2, 0x02, 196}, + {9, 0x02, 196}, + {23, 0x02, 196}, + {40, 0x03, 196}, + }, + + { + {3, 0x02, 187}, + {6, 0x02, 187}, + {10, 0x02, 187}, + {15, 0x02, 187}, + {24, 0x02, 187}, + {31, 0x02, 187}, + {41, 0x02, 187}, + {56, 0x03, 187}, + {3, 0x02, 189}, + {6, 0x02, 189}, + {10, 0x02, 189}, + {15, 0x02, 189}, + {24, 0x02, 189}, + {31, 0x02, 189}, + {41, 0x02, 189}, + {56, 0x03, 189}, + }, + + { + {3, 0x02, 190}, + {6, 0x02, 190}, + {10, 0x02, 190}, + {15, 0x02, 190}, + {24, 0x02, 190}, + {31, 0x02, 190}, + {41, 0x02, 190}, + {56, 0x03, 190}, + {3, 0x02, 196}, + {6, 0x02, 196}, + {10, 0x02, 196}, + {15, 0x02, 196}, + {24, 0x02, 196}, + {31, 0x02, 196}, + {41, 0x02, 196}, + {56, 0x03, 196}, + }, + + { + {1, 0x02, 198}, + {22, 0x03, 198}, + {1, 0x02, 228}, + {22, 0x03, 228}, + {1, 0x02, 232}, + {22, 0x03, 232}, + {1, 0x02, 233}, + {22, 0x03, 233}, + {0, 0x03, 1}, + {0, 0x03, 135}, + {0, 0x03, 137}, + {0, 0x03, 138}, + {0, 0x03, 139}, + {0, 0x03, 140}, + {0, 0x03, 141}, + {0, 0x03, 143}, + }, + + { + {2, 0x02, 198}, + {9, 0x02, 198}, + {23, 0x02, 198}, + {40, 0x03, 198}, + {2, 0x02, 228}, + {9, 0x02, 228}, + {23, 0x02, 228}, + {40, 0x03, 228}, + {2, 0x02, 232}, + {9, 0x02, 232}, + {23, 0x02, 232}, + {40, 0x03, 232}, + {2, 0x02, 233}, + {9, 0x02, 233}, + {23, 0x02, 233}, + {40, 0x03, 233}, + }, + + { + {3, 0x02, 198}, + {6, 0x02, 198}, + {10, 0x02, 198}, + {15, 0x02, 198}, + {24, 0x02, 198}, + {31, 0x02, 198}, + {41, 0x02, 198}, + {56, 0x03, 198}, + {3, 0x02, 228}, + {6, 0x02, 228}, + {10, 0x02, 228}, + {15, 0x02, 228}, + {24, 0x02, 228}, + {31, 0x02, 228}, + {41, 0x02, 228}, + {56, 0x03, 228}, + }, + + { + {3, 0x02, 232}, + {6, 0x02, 232}, + {10, 0x02, 232}, + {15, 0x02, 232}, + {24, 0x02, 232}, + {31, 0x02, 232}, + {41, 0x02, 232}, + {56, 0x03, 232}, + {3, 0x02, 233}, + {6, 0x02, 233}, + {10, 0x02, 233}, + {15, 0x02, 233}, + {24, 0x02, 233}, + {31, 0x02, 233}, + {41, 0x02, 233}, + {56, 0x03, 233}, + }, + + { + {1, 0x02, 1}, + {22, 0x03, 1}, + {1, 0x02, 135}, + {22, 0x03, 135}, + {1, 0x02, 137}, + {22, 0x03, 137}, + {1, 0x02, 138}, + {22, 0x03, 138}, + {1, 0x02, 139}, + {22, 0x03, 139}, + {1, 0x02, 140}, + {22, 0x03, 140}, + {1, 0x02, 141}, + {22, 0x03, 141}, + {1, 0x02, 143}, + {22, 0x03, 143}, + }, + + { + {2, 0x02, 1}, + {9, 0x02, 1}, + {23, 0x02, 1}, + {40, 0x03, 1}, + {2, 0x02, 135}, + {9, 0x02, 135}, + {23, 0x02, 135}, + {40, 0x03, 135}, + {2, 0x02, 137}, + {9, 0x02, 137}, + {23, 0x02, 137}, + {40, 0x03, 137}, + {2, 0x02, 138}, + {9, 0x02, 138}, + {23, 0x02, 138}, + {40, 0x03, 138}, + }, + + { + {3, 0x02, 1}, + {6, 0x02, 1}, + {10, 0x02, 1}, + {15, 0x02, 1}, + {24, 0x02, 1}, + {31, 0x02, 1}, + {41, 0x02, 1}, + {56, 0x03, 1}, + {3, 0x02, 135}, + {6, 0x02, 135}, + {10, 0x02, 135}, + {15, 0x02, 135}, + {24, 0x02, 135}, + {31, 0x02, 135}, + {41, 0x02, 135}, + {56, 0x03, 135}, + }, + + { + {3, 0x02, 137}, + {6, 0x02, 137}, + {10, 0x02, 137}, + {15, 0x02, 137}, + {24, 0x02, 137}, + {31, 0x02, 137}, + {41, 0x02, 137}, + {56, 0x03, 137}, + {3, 0x02, 138}, + {6, 0x02, 138}, + {10, 0x02, 138}, + {15, 0x02, 138}, + {24, 0x02, 138}, + {31, 0x02, 138}, + {41, 0x02, 138}, + {56, 0x03, 138}, + }, + + { + {2, 0x02, 139}, + {9, 0x02, 139}, + {23, 0x02, 139}, + {40, 0x03, 139}, + {2, 0x02, 140}, + {9, 0x02, 140}, + {23, 0x02, 140}, + {40, 0x03, 140}, + {2, 0x02, 141}, + {9, 0x02, 141}, + {23, 0x02, 141}, + {40, 0x03, 141}, + {2, 0x02, 143}, + {9, 0x02, 143}, + {23, 0x02, 143}, + {40, 0x03, 143}, + }, + + { + {3, 0x02, 139}, + {6, 0x02, 139}, + {10, 0x02, 139}, + {15, 0x02, 139}, + {24, 0x02, 139}, + {31, 0x02, 139}, + {41, 0x02, 139}, + {56, 0x03, 139}, + {3, 0x02, 140}, + {6, 0x02, 140}, + {10, 0x02, 140}, + {15, 0x02, 140}, + {24, 0x02, 140}, + {31, 0x02, 140}, + {41, 0x02, 140}, + {56, 0x03, 140}, + }, + + { + {3, 0x02, 141}, + {6, 0x02, 141}, + {10, 0x02, 141}, + {15, 0x02, 141}, + {24, 0x02, 141}, + {31, 0x02, 141}, + {41, 0x02, 141}, + {56, 0x03, 141}, + {3, 0x02, 143}, + {6, 0x02, 143}, + {10, 0x02, 143}, + {15, 0x02, 143}, + {24, 0x02, 143}, + {31, 0x02, 143}, + {41, 0x02, 143}, + {56, 0x03, 143}, + }, + + { + {157, 0x00, 0}, + {158, 0x00, 0}, + {160, 0x00, 0}, + {161, 0x00, 0}, + {164, 0x00, 0}, + {165, 0x00, 0}, + {167, 0x00, 0}, + {168, 0x00, 0}, + {172, 0x00, 0}, + {173, 0x00, 0}, + {175, 0x00, 0}, + {177, 0x00, 0}, + {182, 0x00, 0}, + {185, 0x00, 0}, + {191, 0x00, 0}, + {207, 0x00, 0}, + }, + + { + {0, 0x03, 147}, + {0, 0x03, 149}, + {0, 0x03, 150}, + {0, 0x03, 151}, + {0, 0x03, 152}, + {0, 0x03, 155}, + {0, 0x03, 157}, + {0, 0x03, 158}, + {0, 0x03, 165}, + {0, 0x03, 166}, + {0, 0x03, 168}, + {0, 0x03, 174}, + {0, 0x03, 175}, + {0, 0x03, 180}, + {0, 0x03, 182}, + {0, 0x03, 183}, + }, + + { + {1, 0x02, 147}, + {22, 0x03, 147}, + {1, 0x02, 149}, + {22, 0x03, 149}, + {1, 0x02, 150}, + {22, 0x03, 150}, + {1, 0x02, 151}, + {22, 0x03, 151}, + {1, 0x02, 152}, + {22, 0x03, 152}, + {1, 0x02, 155}, + {22, 0x03, 155}, + {1, 0x02, 157}, + {22, 0x03, 157}, + {1, 0x02, 158}, + {22, 0x03, 158}, + }, + + { + {2, 0x02, 147}, + {9, 0x02, 147}, + {23, 0x02, 147}, + {40, 0x03, 147}, + {2, 0x02, 149}, + {9, 0x02, 149}, + {23, 0x02, 149}, + {40, 0x03, 149}, + {2, 0x02, 150}, + {9, 0x02, 150}, + {23, 0x02, 150}, + {40, 0x03, 150}, + {2, 0x02, 151}, + {9, 0x02, 151}, + {23, 0x02, 151}, + {40, 0x03, 151}, + }, + + { + {3, 0x02, 147}, + {6, 0x02, 147}, + {10, 0x02, 147}, + {15, 0x02, 147}, + {24, 0x02, 147}, + {31, 0x02, 147}, + {41, 0x02, 147}, + {56, 0x03, 147}, + {3, 0x02, 149}, + {6, 0x02, 149}, + {10, 0x02, 149}, + {15, 0x02, 149}, + {24, 0x02, 149}, + {31, 0x02, 149}, + {41, 0x02, 149}, + {56, 0x03, 149}, + }, + + { + {3, 0x02, 150}, + {6, 0x02, 150}, + {10, 0x02, 150}, + {15, 0x02, 150}, + {24, 0x02, 150}, + {31, 0x02, 150}, + {41, 0x02, 150}, + {56, 0x03, 150}, + {3, 0x02, 151}, + {6, 0x02, 151}, + {10, 0x02, 151}, + {15, 0x02, 151}, + {24, 0x02, 151}, + {31, 0x02, 151}, + {41, 0x02, 151}, + {56, 0x03, 151}, + }, + + { + {2, 0x02, 152}, + {9, 0x02, 152}, + {23, 0x02, 152}, + {40, 0x03, 152}, + {2, 0x02, 155}, + {9, 0x02, 155}, + {23, 0x02, 155}, + {40, 0x03, 155}, + {2, 0x02, 157}, + {9, 0x02, 157}, + {23, 0x02, 157}, + {40, 0x03, 157}, + {2, 0x02, 158}, + {9, 0x02, 158}, + {23, 0x02, 158}, + {40, 0x03, 158}, + }, + + { + {3, 0x02, 152}, + {6, 0x02, 152}, + {10, 0x02, 152}, + {15, 0x02, 152}, + {24, 0x02, 152}, + {31, 0x02, 152}, + {41, 0x02, 152}, + {56, 0x03, 152}, + {3, 0x02, 155}, + {6, 0x02, 155}, + {10, 0x02, 155}, + {15, 0x02, 155}, + {24, 0x02, 155}, + {31, 0x02, 155}, + {41, 0x02, 155}, + {56, 0x03, 155}, + }, + + { + {3, 0x02, 157}, + {6, 0x02, 157}, + {10, 0x02, 157}, + {15, 0x02, 157}, + {24, 0x02, 157}, + {31, 0x02, 157}, + {41, 0x02, 157}, + {56, 0x03, 157}, + {3, 0x02, 158}, + {6, 0x02, 158}, + {10, 0x02, 158}, + {15, 0x02, 158}, + {24, 0x02, 158}, + {31, 0x02, 158}, + {41, 0x02, 158}, + {56, 0x03, 158}, + }, + + { + {1, 0x02, 165}, + {22, 0x03, 165}, + {1, 0x02, 166}, + {22, 0x03, 166}, + {1, 0x02, 168}, + {22, 0x03, 168}, + {1, 0x02, 174}, + {22, 0x03, 174}, + {1, 0x02, 175}, + {22, 0x03, 175}, + {1, 0x02, 180}, + {22, 0x03, 180}, + {1, 0x02, 182}, + {22, 0x03, 182}, + {1, 0x02, 183}, + {22, 0x03, 183}, + }, + + { + {2, 0x02, 165}, + {9, 0x02, 165}, + {23, 0x02, 165}, + {40, 0x03, 165}, + {2, 0x02, 166}, + {9, 0x02, 166}, + {23, 0x02, 166}, + {40, 0x03, 166}, + {2, 0x02, 168}, + {9, 0x02, 168}, + {23, 0x02, 168}, + {40, 0x03, 168}, + {2, 0x02, 174}, + {9, 0x02, 174}, + {23, 0x02, 174}, + {40, 0x03, 174}, + }, + + { + {3, 0x02, 165}, + {6, 0x02, 165}, + {10, 0x02, 165}, + {15, 0x02, 165}, + {24, 0x02, 165}, + {31, 0x02, 165}, + {41, 0x02, 165}, + {56, 0x03, 165}, + {3, 0x02, 166}, + {6, 0x02, 166}, + {10, 0x02, 166}, + {15, 0x02, 166}, + {24, 0x02, 166}, + {31, 0x02, 166}, + {41, 0x02, 166}, + {56, 0x03, 166}, + }, + + { + {3, 0x02, 168}, + {6, 0x02, 168}, + {10, 0x02, 168}, + {15, 0x02, 168}, + {24, 0x02, 168}, + {31, 0x02, 168}, + {41, 0x02, 168}, + {56, 0x03, 168}, + {3, 0x02, 174}, + {6, 0x02, 174}, + {10, 0x02, 174}, + {15, 0x02, 174}, + {24, 0x02, 174}, + {31, 0x02, 174}, + {41, 0x02, 174}, + {56, 0x03, 174}, + }, + + { + {2, 0x02, 175}, + {9, 0x02, 175}, + {23, 0x02, 175}, + {40, 0x03, 175}, + {2, 0x02, 180}, + {9, 0x02, 180}, + {23, 0x02, 180}, + {40, 0x03, 180}, + {2, 0x02, 182}, + {9, 0x02, 182}, + {23, 0x02, 182}, + {40, 0x03, 182}, + {2, 0x02, 183}, + {9, 0x02, 183}, + {23, 0x02, 183}, + {40, 0x03, 183}, + }, + + { + {3, 0x02, 175}, + {6, 0x02, 175}, + {10, 0x02, 175}, + {15, 0x02, 175}, + {24, 0x02, 175}, + {31, 0x02, 175}, + {41, 0x02, 175}, + {56, 0x03, 175}, + {3, 0x02, 180}, + {6, 0x02, 180}, + {10, 0x02, 180}, + {15, 0x02, 180}, + {24, 0x02, 180}, + {31, 0x02, 180}, + {41, 0x02, 180}, + {56, 0x03, 180}, + }, + + { + {3, 0x02, 182}, + {6, 0x02, 182}, + {10, 0x02, 182}, + {15, 0x02, 182}, + {24, 0x02, 182}, + {31, 0x02, 182}, + {41, 0x02, 182}, + {56, 0x03, 182}, + {3, 0x02, 183}, + {6, 0x02, 183}, + {10, 0x02, 183}, + {15, 0x02, 183}, + {24, 0x02, 183}, + {31, 0x02, 183}, + {41, 0x02, 183}, + {56, 0x03, 183}, + }, + + { + {0, 0x03, 188}, + {0, 0x03, 191}, + {0, 0x03, 197}, + {0, 0x03, 231}, + {0, 0x03, 239}, + {176, 0x00, 0}, + {178, 0x00, 0}, + {179, 0x00, 0}, + {183, 0x00, 0}, + {184, 0x00, 0}, + {186, 0x00, 0}, + {187, 0x00, 0}, + {192, 0x00, 0}, + {199, 0x00, 0}, + {208, 0x00, 0}, + {223, 0x00, 0}, + }, + + { + {1, 0x02, 188}, + {22, 0x03, 188}, + {1, 0x02, 191}, + {22, 0x03, 191}, + {1, 0x02, 197}, + {22, 0x03, 197}, + {1, 0x02, 231}, + {22, 0x03, 231}, + {1, 0x02, 239}, + {22, 0x03, 239}, + {0, 0x03, 9}, + {0, 0x03, 142}, + {0, 0x03, 144}, + {0, 0x03, 145}, + {0, 0x03, 148}, + {0, 0x03, 159}, + }, + + { + {2, 0x02, 188}, + {9, 0x02, 188}, + {23, 0x02, 188}, + {40, 0x03, 188}, + {2, 0x02, 191}, + {9, 0x02, 191}, + {23, 0x02, 191}, + {40, 0x03, 191}, + {2, 0x02, 197}, + {9, 0x02, 197}, + {23, 0x02, 197}, + {40, 0x03, 197}, + {2, 0x02, 231}, + {9, 0x02, 231}, + {23, 0x02, 231}, + {40, 0x03, 231}, + }, + + { + {3, 0x02, 188}, + {6, 0x02, 188}, + {10, 0x02, 188}, + {15, 0x02, 188}, + {24, 0x02, 188}, + {31, 0x02, 188}, + {41, 0x02, 188}, + {56, 0x03, 188}, + {3, 0x02, 191}, + {6, 0x02, 191}, + {10, 0x02, 191}, + {15, 0x02, 191}, + {24, 0x02, 191}, + {31, 0x02, 191}, + {41, 0x02, 191}, + {56, 0x03, 191}, + }, + + { + {3, 0x02, 197}, + {6, 0x02, 197}, + {10, 0x02, 197}, + {15, 0x02, 197}, + {24, 0x02, 197}, + {31, 0x02, 197}, + {41, 0x02, 197}, + {56, 0x03, 197}, + {3, 0x02, 231}, + {6, 0x02, 231}, + {10, 0x02, 231}, + {15, 0x02, 231}, + {24, 0x02, 231}, + {31, 0x02, 231}, + {41, 0x02, 231}, + {56, 0x03, 231}, + }, + + { + {2, 0x02, 239}, + {9, 0x02, 239}, + {23, 0x02, 239}, + {40, 0x03, 239}, + {1, 0x02, 9}, + {22, 0x03, 9}, + {1, 0x02, 142}, + {22, 0x03, 142}, + {1, 0x02, 144}, + {22, 0x03, 144}, + {1, 0x02, 145}, + {22, 0x03, 145}, + {1, 0x02, 148}, + {22, 0x03, 148}, + {1, 0x02, 159}, + {22, 0x03, 159}, + }, + + { + {3, 0x02, 239}, + {6, 0x02, 239}, + {10, 0x02, 239}, + {15, 0x02, 239}, + {24, 0x02, 239}, + {31, 0x02, 239}, + {41, 0x02, 239}, + {56, 0x03, 239}, + {2, 0x02, 9}, + {9, 0x02, 9}, + {23, 0x02, 9}, + {40, 0x03, 9}, + {2, 0x02, 142}, + {9, 0x02, 142}, + {23, 0x02, 142}, + {40, 0x03, 142}, + }, + + { + {3, 0x02, 9}, + {6, 0x02, 9}, + {10, 0x02, 9}, + {15, 0x02, 9}, + {24, 0x02, 9}, + {31, 0x02, 9}, + {41, 0x02, 9}, + {56, 0x03, 9}, + {3, 0x02, 142}, + {6, 0x02, 142}, + {10, 0x02, 142}, + {15, 0x02, 142}, + {24, 0x02, 142}, + {31, 0x02, 142}, + {41, 0x02, 142}, + {56, 0x03, 142}, + }, + + { + {2, 0x02, 144}, + {9, 0x02, 144}, + {23, 0x02, 144}, + {40, 0x03, 144}, + {2, 0x02, 145}, + {9, 0x02, 145}, + {23, 0x02, 145}, + {40, 0x03, 145}, + {2, 0x02, 148}, + {9, 0x02, 148}, + {23, 0x02, 148}, + {40, 0x03, 148}, + {2, 0x02, 159}, + {9, 0x02, 159}, + {23, 0x02, 159}, + {40, 0x03, 159}, + }, + + { + {3, 0x02, 144}, + {6, 0x02, 144}, + {10, 0x02, 144}, + {15, 0x02, 144}, + {24, 0x02, 144}, + {31, 0x02, 144}, + {41, 0x02, 144}, + {56, 0x03, 144}, + {3, 0x02, 145}, + {6, 0x02, 145}, + {10, 0x02, 145}, + {15, 0x02, 145}, + {24, 0x02, 145}, + {31, 0x02, 145}, + {41, 0x02, 145}, + {56, 0x03, 145}, + }, + + { + {3, 0x02, 148}, + {6, 0x02, 148}, + {10, 0x02, 148}, + {15, 0x02, 148}, + {24, 0x02, 148}, + {31, 0x02, 148}, + {41, 0x02, 148}, + {56, 0x03, 148}, + {3, 0x02, 159}, + {6, 0x02, 159}, + {10, 0x02, 159}, + {15, 0x02, 159}, + {24, 0x02, 159}, + {31, 0x02, 159}, + {41, 0x02, 159}, + {56, 0x03, 159}, + }, + + { + {0, 0x03, 171}, + {0, 0x03, 206}, + {0, 0x03, 215}, + {0, 0x03, 225}, + {0, 0x03, 236}, + {0, 0x03, 237}, + {188, 0x00, 0}, + {189, 0x00, 0}, + {193, 0x00, 0}, + {196, 0x00, 0}, + {200, 0x00, 0}, + {203, 0x00, 0}, + {209, 0x00, 0}, + {216, 0x00, 0}, + {224, 0x00, 0}, + {238, 0x00, 0}, + }, + + { + {1, 0x02, 171}, + {22, 0x03, 171}, + {1, 0x02, 206}, + {22, 0x03, 206}, + {1, 0x02, 215}, + {22, 0x03, 215}, + {1, 0x02, 225}, + {22, 0x03, 225}, + {1, 0x02, 236}, + {22, 0x03, 236}, + {1, 0x02, 237}, + {22, 0x03, 237}, + {0, 0x03, 199}, + {0, 0x03, 207}, + {0, 0x03, 234}, + {0, 0x03, 235}, + }, + + { + {2, 0x02, 171}, + {9, 0x02, 171}, + {23, 0x02, 171}, + {40, 0x03, 171}, + {2, 0x02, 206}, + {9, 0x02, 206}, + {23, 0x02, 206}, + {40, 0x03, 206}, + {2, 0x02, 215}, + {9, 0x02, 215}, + {23, 0x02, 215}, + {40, 0x03, 215}, + {2, 0x02, 225}, + {9, 0x02, 225}, + {23, 0x02, 225}, + {40, 0x03, 225}, + }, + + { + {3, 0x02, 171}, + {6, 0x02, 171}, + {10, 0x02, 171}, + {15, 0x02, 171}, + {24, 0x02, 171}, + {31, 0x02, 171}, + {41, 0x02, 171}, + {56, 0x03, 171}, + {3, 0x02, 206}, + {6, 0x02, 206}, + {10, 0x02, 206}, + {15, 0x02, 206}, + {24, 0x02, 206}, + {31, 0x02, 206}, + {41, 0x02, 206}, + {56, 0x03, 206}, + }, + + { + {3, 0x02, 215}, + {6, 0x02, 215}, + {10, 0x02, 215}, + {15, 0x02, 215}, + {24, 0x02, 215}, + {31, 0x02, 215}, + {41, 0x02, 215}, + {56, 0x03, 215}, + {3, 0x02, 225}, + {6, 0x02, 225}, + {10, 0x02, 225}, + {15, 0x02, 225}, + {24, 0x02, 225}, + {31, 0x02, 225}, + {41, 0x02, 225}, + {56, 0x03, 225}, + }, + + { + {2, 0x02, 236}, + {9, 0x02, 236}, + {23, 0x02, 236}, + {40, 0x03, 236}, + {2, 0x02, 237}, + {9, 0x02, 237}, + {23, 0x02, 237}, + {40, 0x03, 237}, + {1, 0x02, 199}, + {22, 0x03, 199}, + {1, 0x02, 207}, + {22, 0x03, 207}, + {1, 0x02, 234}, + {22, 0x03, 234}, + {1, 0x02, 235}, + {22, 0x03, 235}, + }, + + { + {3, 0x02, 236}, + {6, 0x02, 236}, + {10, 0x02, 236}, + {15, 0x02, 236}, + {24, 0x02, 236}, + {31, 0x02, 236}, + {41, 0x02, 236}, + {56, 0x03, 236}, + {3, 0x02, 237}, + {6, 0x02, 237}, + {10, 0x02, 237}, + {15, 0x02, 237}, + {24, 0x02, 237}, + {31, 0x02, 237}, + {41, 0x02, 237}, + {56, 0x03, 237}, + }, + + { + {2, 0x02, 199}, + {9, 0x02, 199}, + {23, 0x02, 199}, + {40, 0x03, 199}, + {2, 0x02, 207}, + {9, 0x02, 207}, + {23, 0x02, 207}, + {40, 0x03, 207}, + {2, 0x02, 234}, + {9, 0x02, 234}, + {23, 0x02, 234}, + {40, 0x03, 234}, + {2, 0x02, 235}, + {9, 0x02, 235}, + {23, 0x02, 235}, + {40, 0x03, 235}, + }, + + { + {3, 0x02, 199}, + {6, 0x02, 199}, + {10, 0x02, 199}, + {15, 0x02, 199}, + {24, 0x02, 199}, + {31, 0x02, 199}, + {41, 0x02, 199}, + {56, 0x03, 199}, + {3, 0x02, 207}, + {6, 0x02, 207}, + {10, 0x02, 207}, + {15, 0x02, 207}, + {24, 0x02, 207}, + {31, 0x02, 207}, + {41, 0x02, 207}, + {56, 0x03, 207}, + }, + + { + {3, 0x02, 234}, + {6, 0x02, 234}, + {10, 0x02, 234}, + {15, 0x02, 234}, + {24, 0x02, 234}, + {31, 0x02, 234}, + {41, 0x02, 234}, + {56, 0x03, 234}, + {3, 0x02, 235}, + {6, 0x02, 235}, + {10, 0x02, 235}, + {15, 0x02, 235}, + {24, 0x02, 235}, + {31, 0x02, 235}, + {41, 0x02, 235}, + {56, 0x03, 235}, + }, + + { + {194, 0x00, 0}, + {195, 0x00, 0}, + {197, 0x00, 0}, + {198, 0x00, 0}, + {201, 0x00, 0}, + {202, 0x00, 0}, + {204, 0x00, 0}, + {205, 0x00, 0}, + {210, 0x00, 0}, + {213, 0x00, 0}, + {217, 0x00, 0}, + {220, 0x00, 0}, + {225, 0x00, 0}, + {231, 0x00, 0}, + {239, 0x00, 0}, + {246, 0x00, 0}, + }, + + { + {0, 0x03, 192}, + {0, 0x03, 193}, + {0, 0x03, 200}, + {0, 0x03, 201}, + {0, 0x03, 202}, + {0, 0x03, 205}, + {0, 0x03, 210}, + {0, 0x03, 213}, + {0, 0x03, 218}, + {0, 0x03, 219}, + {0, 0x03, 238}, + {0, 0x03, 240}, + {0, 0x03, 242}, + {0, 0x03, 243}, + {0, 0x03, 255}, + {206, 0x00, 0}, + }, + + { + {1, 0x02, 192}, + {22, 0x03, 192}, + {1, 0x02, 193}, + {22, 0x03, 193}, + {1, 0x02, 200}, + {22, 0x03, 200}, + {1, 0x02, 201}, + {22, 0x03, 201}, + {1, 0x02, 202}, + {22, 0x03, 202}, + {1, 0x02, 205}, + {22, 0x03, 205}, + {1, 0x02, 210}, + {22, 0x03, 210}, + {1, 0x02, 213}, + {22, 0x03, 213}, + }, + + { + {2, 0x02, 192}, + {9, 0x02, 192}, + {23, 0x02, 192}, + {40, 0x03, 192}, + {2, 0x02, 193}, + {9, 0x02, 193}, + {23, 0x02, 193}, + {40, 0x03, 193}, + {2, 0x02, 200}, + {9, 0x02, 200}, + {23, 0x02, 200}, + {40, 0x03, 200}, + {2, 0x02, 201}, + {9, 0x02, 201}, + {23, 0x02, 201}, + {40, 0x03, 201}, + }, + + { + {3, 0x02, 192}, + {6, 0x02, 192}, + {10, 0x02, 192}, + {15, 0x02, 192}, + {24, 0x02, 192}, + {31, 0x02, 192}, + {41, 0x02, 192}, + {56, 0x03, 192}, + {3, 0x02, 193}, + {6, 0x02, 193}, + {10, 0x02, 193}, + {15, 0x02, 193}, + {24, 0x02, 193}, + {31, 0x02, 193}, + {41, 0x02, 193}, + {56, 0x03, 193}, + }, + + { + {3, 0x02, 200}, + {6, 0x02, 200}, + {10, 0x02, 200}, + {15, 0x02, 200}, + {24, 0x02, 200}, + {31, 0x02, 200}, + {41, 0x02, 200}, + {56, 0x03, 200}, + {3, 0x02, 201}, + {6, 0x02, 201}, + {10, 0x02, 201}, + {15, 0x02, 201}, + {24, 0x02, 201}, + {31, 0x02, 201}, + {41, 0x02, 201}, + {56, 0x03, 201}, + }, + + { + {2, 0x02, 202}, + {9, 0x02, 202}, + {23, 0x02, 202}, + {40, 0x03, 202}, + {2, 0x02, 205}, + {9, 0x02, 205}, + {23, 0x02, 205}, + {40, 0x03, 205}, + {2, 0x02, 210}, + {9, 0x02, 210}, + {23, 0x02, 210}, + {40, 0x03, 210}, + {2, 0x02, 213}, + {9, 0x02, 213}, + {23, 0x02, 213}, + {40, 0x03, 213}, + }, + + { + {3, 0x02, 202}, + {6, 0x02, 202}, + {10, 0x02, 202}, + {15, 0x02, 202}, + {24, 0x02, 202}, + {31, 0x02, 202}, + {41, 0x02, 202}, + {56, 0x03, 202}, + {3, 0x02, 205}, + {6, 0x02, 205}, + {10, 0x02, 205}, + {15, 0x02, 205}, + {24, 0x02, 205}, + {31, 0x02, 205}, + {41, 0x02, 205}, + {56, 0x03, 205}, + }, + + { + {3, 0x02, 210}, + {6, 0x02, 210}, + {10, 0x02, 210}, + {15, 0x02, 210}, + {24, 0x02, 210}, + {31, 0x02, 210}, + {41, 0x02, 210}, + {56, 0x03, 210}, + {3, 0x02, 213}, + {6, 0x02, 213}, + {10, 0x02, 213}, + {15, 0x02, 213}, + {24, 0x02, 213}, + {31, 0x02, 213}, + {41, 0x02, 213}, + {56, 0x03, 213}, + }, + + { + {1, 0x02, 218}, + {22, 0x03, 218}, + {1, 0x02, 219}, + {22, 0x03, 219}, + {1, 0x02, 238}, + {22, 0x03, 238}, + {1, 0x02, 240}, + {22, 0x03, 240}, + {1, 0x02, 242}, + {22, 0x03, 242}, + {1, 0x02, 243}, + {22, 0x03, 243}, + {1, 0x02, 255}, + {22, 0x03, 255}, + {0, 0x03, 203}, + {0, 0x03, 204}, + }, + + { + {2, 0x02, 218}, + {9, 0x02, 218}, + {23, 0x02, 218}, + {40, 0x03, 218}, + {2, 0x02, 219}, + {9, 0x02, 219}, + {23, 0x02, 219}, + {40, 0x03, 219}, + {2, 0x02, 238}, + {9, 0x02, 238}, + {23, 0x02, 238}, + {40, 0x03, 238}, + {2, 0x02, 240}, + {9, 0x02, 240}, + {23, 0x02, 240}, + {40, 0x03, 240}, + }, + + { + {3, 0x02, 218}, + {6, 0x02, 218}, + {10, 0x02, 218}, + {15, 0x02, 218}, + {24, 0x02, 218}, + {31, 0x02, 218}, + {41, 0x02, 218}, + {56, 0x03, 218}, + {3, 0x02, 219}, + {6, 0x02, 219}, + {10, 0x02, 219}, + {15, 0x02, 219}, + {24, 0x02, 219}, + {31, 0x02, 219}, + {41, 0x02, 219}, + {56, 0x03, 219}, + }, + + { + {3, 0x02, 238}, + {6, 0x02, 238}, + {10, 0x02, 238}, + {15, 0x02, 238}, + {24, 0x02, 238}, + {31, 0x02, 238}, + {41, 0x02, 238}, + {56, 0x03, 238}, + {3, 0x02, 240}, + {6, 0x02, 240}, + {10, 0x02, 240}, + {15, 0x02, 240}, + {24, 0x02, 240}, + {31, 0x02, 240}, + {41, 0x02, 240}, + {56, 0x03, 240}, + }, + + { + {2, 0x02, 242}, + {9, 0x02, 242}, + {23, 0x02, 242}, + {40, 0x03, 242}, + {2, 0x02, 243}, + {9, 0x02, 243}, + {23, 0x02, 243}, + {40, 0x03, 243}, + {2, 0x02, 255}, + {9, 0x02, 255}, + {23, 0x02, 255}, + {40, 0x03, 255}, + {1, 0x02, 203}, + {22, 0x03, 203}, + {1, 0x02, 204}, + {22, 0x03, 204}, + }, + + { + {3, 0x02, 242}, + {6, 0x02, 242}, + {10, 0x02, 242}, + {15, 0x02, 242}, + {24, 0x02, 242}, + {31, 0x02, 242}, + {41, 0x02, 242}, + {56, 0x03, 242}, + {3, 0x02, 243}, + {6, 0x02, 243}, + {10, 0x02, 243}, + {15, 0x02, 243}, + {24, 0x02, 243}, + {31, 0x02, 243}, + {41, 0x02, 243}, + {56, 0x03, 243}, + }, + + { + {3, 0x02, 255}, + {6, 0x02, 255}, + {10, 0x02, 255}, + {15, 0x02, 255}, + {24, 0x02, 255}, + {31, 0x02, 255}, + {41, 0x02, 255}, + {56, 0x03, 255}, + {2, 0x02, 203}, + {9, 0x02, 203}, + {23, 0x02, 203}, + {40, 0x03, 203}, + {2, 0x02, 204}, + {9, 0x02, 204}, + {23, 0x02, 204}, + {40, 0x03, 204}, + }, + + { + {3, 0x02, 203}, + {6, 0x02, 203}, + {10, 0x02, 203}, + {15, 0x02, 203}, + {24, 0x02, 203}, + {31, 0x02, 203}, + {41, 0x02, 203}, + {56, 0x03, 203}, + {3, 0x02, 204}, + {6, 0x02, 204}, + {10, 0x02, 204}, + {15, 0x02, 204}, + {24, 0x02, 204}, + {31, 0x02, 204}, + {41, 0x02, 204}, + {56, 0x03, 204}, + }, + + { + {211, 0x00, 0}, + {212, 0x00, 0}, + {214, 0x00, 0}, + {215, 0x00, 0}, + {218, 0x00, 0}, + {219, 0x00, 0}, + {221, 0x00, 0}, + {222, 0x00, 0}, + {226, 0x00, 0}, + {228, 0x00, 0}, + {232, 0x00, 0}, + {235, 0x00, 0}, + {240, 0x00, 0}, + {243, 0x00, 0}, + {247, 0x00, 0}, + {250, 0x00, 0}, + }, + + { + {0, 0x03, 211}, + {0, 0x03, 212}, + {0, 0x03, 214}, + {0, 0x03, 221}, + {0, 0x03, 222}, + {0, 0x03, 223}, + {0, 0x03, 241}, + {0, 0x03, 244}, + {0, 0x03, 245}, + {0, 0x03, 246}, + {0, 0x03, 247}, + {0, 0x03, 248}, + {0, 0x03, 250}, + {0, 0x03, 251}, + {0, 0x03, 252}, + {0, 0x03, 253}, + }, + + { + {1, 0x02, 211}, + {22, 0x03, 211}, + {1, 0x02, 212}, + {22, 0x03, 212}, + {1, 0x02, 214}, + {22, 0x03, 214}, + {1, 0x02, 221}, + {22, 0x03, 221}, + {1, 0x02, 222}, + {22, 0x03, 222}, + {1, 0x02, 223}, + {22, 0x03, 223}, + {1, 0x02, 241}, + {22, 0x03, 241}, + {1, 0x02, 244}, + {22, 0x03, 244}, + }, + + { + {2, 0x02, 211}, + {9, 0x02, 211}, + {23, 0x02, 211}, + {40, 0x03, 211}, + {2, 0x02, 212}, + {9, 0x02, 212}, + {23, 0x02, 212}, + {40, 0x03, 212}, + {2, 0x02, 214}, + {9, 0x02, 214}, + {23, 0x02, 214}, + {40, 0x03, 214}, + {2, 0x02, 221}, + {9, 0x02, 221}, + {23, 0x02, 221}, + {40, 0x03, 221}, + }, + + { + {3, 0x02, 211}, + {6, 0x02, 211}, + {10, 0x02, 211}, + {15, 0x02, 211}, + {24, 0x02, 211}, + {31, 0x02, 211}, + {41, 0x02, 211}, + {56, 0x03, 211}, + {3, 0x02, 212}, + {6, 0x02, 212}, + {10, 0x02, 212}, + {15, 0x02, 212}, + {24, 0x02, 212}, + {31, 0x02, 212}, + {41, 0x02, 212}, + {56, 0x03, 212}, + }, + + { + {3, 0x02, 214}, + {6, 0x02, 214}, + {10, 0x02, 214}, + {15, 0x02, 214}, + {24, 0x02, 214}, + {31, 0x02, 214}, + {41, 0x02, 214}, + {56, 0x03, 214}, + {3, 0x02, 221}, + {6, 0x02, 221}, + {10, 0x02, 221}, + {15, 0x02, 221}, + {24, 0x02, 221}, + {31, 0x02, 221}, + {41, 0x02, 221}, + {56, 0x03, 221}, + }, + + { + {2, 0x02, 222}, + {9, 0x02, 222}, + {23, 0x02, 222}, + {40, 0x03, 222}, + {2, 0x02, 223}, + {9, 0x02, 223}, + {23, 0x02, 223}, + {40, 0x03, 223}, + {2, 0x02, 241}, + {9, 0x02, 241}, + {23, 0x02, 241}, + {40, 0x03, 241}, + {2, 0x02, 244}, + {9, 0x02, 244}, + {23, 0x02, 244}, + {40, 0x03, 244}, + }, + + { + {3, 0x02, 222}, + {6, 0x02, 222}, + {10, 0x02, 222}, + {15, 0x02, 222}, + {24, 0x02, 222}, + {31, 0x02, 222}, + {41, 0x02, 222}, + {56, 0x03, 222}, + {3, 0x02, 223}, + {6, 0x02, 223}, + {10, 0x02, 223}, + {15, 0x02, 223}, + {24, 0x02, 223}, + {31, 0x02, 223}, + {41, 0x02, 223}, + {56, 0x03, 223}, + }, + + { + {3, 0x02, 241}, + {6, 0x02, 241}, + {10, 0x02, 241}, + {15, 0x02, 241}, + {24, 0x02, 241}, + {31, 0x02, 241}, + {41, 0x02, 241}, + {56, 0x03, 241}, + {3, 0x02, 244}, + {6, 0x02, 244}, + {10, 0x02, 244}, + {15, 0x02, 244}, + {24, 0x02, 244}, + {31, 0x02, 244}, + {41, 0x02, 244}, + {56, 0x03, 244}, + }, + + { + {1, 0x02, 245}, + {22, 0x03, 245}, + {1, 0x02, 246}, + {22, 0x03, 246}, + {1, 0x02, 247}, + {22, 0x03, 247}, + {1, 0x02, 248}, + {22, 0x03, 248}, + {1, 0x02, 250}, + {22, 0x03, 250}, + {1, 0x02, 251}, + {22, 0x03, 251}, + {1, 0x02, 252}, + {22, 0x03, 252}, + {1, 0x02, 253}, + {22, 0x03, 253}, + }, + + { + {2, 0x02, 245}, + {9, 0x02, 245}, + {23, 0x02, 245}, + {40, 0x03, 245}, + {2, 0x02, 246}, + {9, 0x02, 246}, + {23, 0x02, 246}, + {40, 0x03, 246}, + {2, 0x02, 247}, + {9, 0x02, 247}, + {23, 0x02, 247}, + {40, 0x03, 247}, + {2, 0x02, 248}, + {9, 0x02, 248}, + {23, 0x02, 248}, + {40, 0x03, 248}, + }, + + { + {3, 0x02, 245}, + {6, 0x02, 245}, + {10, 0x02, 245}, + {15, 0x02, 245}, + {24, 0x02, 245}, + {31, 0x02, 245}, + {41, 0x02, 245}, + {56, 0x03, 245}, + {3, 0x02, 246}, + {6, 0x02, 246}, + {10, 0x02, 246}, + {15, 0x02, 246}, + {24, 0x02, 246}, + {31, 0x02, 246}, + {41, 0x02, 246}, + {56, 0x03, 246}, + }, + + { + {3, 0x02, 247}, + {6, 0x02, 247}, + {10, 0x02, 247}, + {15, 0x02, 247}, + {24, 0x02, 247}, + {31, 0x02, 247}, + {41, 0x02, 247}, + {56, 0x03, 247}, + {3, 0x02, 248}, + {6, 0x02, 248}, + {10, 0x02, 248}, + {15, 0x02, 248}, + {24, 0x02, 248}, + {31, 0x02, 248}, + {41, 0x02, 248}, + {56, 0x03, 248}, + }, + + { + {2, 0x02, 250}, + {9, 0x02, 250}, + {23, 0x02, 250}, + {40, 0x03, 250}, + {2, 0x02, 251}, + {9, 0x02, 251}, + {23, 0x02, 251}, + {40, 0x03, 251}, + {2, 0x02, 252}, + {9, 0x02, 252}, + {23, 0x02, 252}, + {40, 0x03, 252}, + {2, 0x02, 253}, + {9, 0x02, 253}, + {23, 0x02, 253}, + {40, 0x03, 253}, + }, + + { + {3, 0x02, 250}, + {6, 0x02, 250}, + {10, 0x02, 250}, + {15, 0x02, 250}, + {24, 0x02, 250}, + {31, 0x02, 250}, + {41, 0x02, 250}, + {56, 0x03, 250}, + {3, 0x02, 251}, + {6, 0x02, 251}, + {10, 0x02, 251}, + {15, 0x02, 251}, + {24, 0x02, 251}, + {31, 0x02, 251}, + {41, 0x02, 251}, + {56, 0x03, 251}, + }, + + { + {3, 0x02, 252}, + {6, 0x02, 252}, + {10, 0x02, 252}, + {15, 0x02, 252}, + {24, 0x02, 252}, + {31, 0x02, 252}, + {41, 0x02, 252}, + {56, 0x03, 252}, + {3, 0x02, 253}, + {6, 0x02, 253}, + {10, 0x02, 253}, + {15, 0x02, 253}, + {24, 0x02, 253}, + {31, 0x02, 253}, + {41, 0x02, 253}, + {56, 0x03, 253}, + }, + + { + {0, 0x03, 254}, + {227, 0x00, 0}, + {229, 0x00, 0}, + {230, 0x00, 0}, + {233, 0x00, 0}, + {234, 0x00, 0}, + {236, 0x00, 0}, + {237, 0x00, 0}, + {241, 0x00, 0}, + {242, 0x00, 0}, + {244, 0x00, 0}, + {245, 0x00, 0}, + {248, 0x00, 0}, + {249, 0x00, 0}, + {251, 0x00, 0}, + {252, 0x00, 0}, + }, + + { + {1, 0x02, 254}, + {22, 0x03, 254}, + {0, 0x03, 2}, + {0, 0x03, 3}, + {0, 0x03, 4}, + {0, 0x03, 5}, + {0, 0x03, 6}, + {0, 0x03, 7}, + {0, 0x03, 8}, + {0, 0x03, 11}, + {0, 0x03, 12}, + {0, 0x03, 14}, + {0, 0x03, 15}, + {0, 0x03, 16}, + {0, 0x03, 17}, + {0, 0x03, 18}, + }, + + { + {2, 0x02, 254}, + {9, 0x02, 254}, + {23, 0x02, 254}, + {40, 0x03, 254}, + {1, 0x02, 2}, + {22, 0x03, 2}, + {1, 0x02, 3}, + {22, 0x03, 3}, + {1, 0x02, 4}, + {22, 0x03, 4}, + {1, 0x02, 5}, + {22, 0x03, 5}, + {1, 0x02, 6}, + {22, 0x03, 6}, + {1, 0x02, 7}, + {22, 0x03, 7}, + }, + + { + {3, 0x02, 254}, + {6, 0x02, 254}, + {10, 0x02, 254}, + {15, 0x02, 254}, + {24, 0x02, 254}, + {31, 0x02, 254}, + {41, 0x02, 254}, + {56, 0x03, 254}, + {2, 0x02, 2}, + {9, 0x02, 2}, + {23, 0x02, 2}, + {40, 0x03, 2}, + {2, 0x02, 3}, + {9, 0x02, 3}, + {23, 0x02, 3}, + {40, 0x03, 3}, + }, + + { + {3, 0x02, 2}, + {6, 0x02, 2}, + {10, 0x02, 2}, + {15, 0x02, 2}, + {24, 0x02, 2}, + {31, 0x02, 2}, + {41, 0x02, 2}, + {56, 0x03, 2}, + {3, 0x02, 3}, + {6, 0x02, 3}, + {10, 0x02, 3}, + {15, 0x02, 3}, + {24, 0x02, 3}, + {31, 0x02, 3}, + {41, 0x02, 3}, + {56, 0x03, 3}, + }, + + { + {2, 0x02, 4}, + {9, 0x02, 4}, + {23, 0x02, 4}, + {40, 0x03, 4}, + {2, 0x02, 5}, + {9, 0x02, 5}, + {23, 0x02, 5}, + {40, 0x03, 5}, + {2, 0x02, 6}, + {9, 0x02, 6}, + {23, 0x02, 6}, + {40, 0x03, 6}, + {2, 0x02, 7}, + {9, 0x02, 7}, + {23, 0x02, 7}, + {40, 0x03, 7}, + }, + + { + {3, 0x02, 4}, + {6, 0x02, 4}, + {10, 0x02, 4}, + {15, 0x02, 4}, + {24, 0x02, 4}, + {31, 0x02, 4}, + {41, 0x02, 4}, + {56, 0x03, 4}, + {3, 0x02, 5}, + {6, 0x02, 5}, + {10, 0x02, 5}, + {15, 0x02, 5}, + {24, 0x02, 5}, + {31, 0x02, 5}, + {41, 0x02, 5}, + {56, 0x03, 5}, + }, + + { + {3, 0x02, 6}, + {6, 0x02, 6}, + {10, 0x02, 6}, + {15, 0x02, 6}, + {24, 0x02, 6}, + {31, 0x02, 6}, + {41, 0x02, 6}, + {56, 0x03, 6}, + {3, 0x02, 7}, + {6, 0x02, 7}, + {10, 0x02, 7}, + {15, 0x02, 7}, + {24, 0x02, 7}, + {31, 0x02, 7}, + {41, 0x02, 7}, + {56, 0x03, 7}, + }, + + { + {1, 0x02, 8}, + {22, 0x03, 8}, + {1, 0x02, 11}, + {22, 0x03, 11}, + {1, 0x02, 12}, + {22, 0x03, 12}, + {1, 0x02, 14}, + {22, 0x03, 14}, + {1, 0x02, 15}, + {22, 0x03, 15}, + {1, 0x02, 16}, + {22, 0x03, 16}, + {1, 0x02, 17}, + {22, 0x03, 17}, + {1, 0x02, 18}, + {22, 0x03, 18}, + }, + + { + {2, 0x02, 8}, + {9, 0x02, 8}, + {23, 0x02, 8}, + {40, 0x03, 8}, + {2, 0x02, 11}, + {9, 0x02, 11}, + {23, 0x02, 11}, + {40, 0x03, 11}, + {2, 0x02, 12}, + {9, 0x02, 12}, + {23, 0x02, 12}, + {40, 0x03, 12}, + {2, 0x02, 14}, + {9, 0x02, 14}, + {23, 0x02, 14}, + {40, 0x03, 14}, + }, + + { + {3, 0x02, 8}, + {6, 0x02, 8}, + {10, 0x02, 8}, + {15, 0x02, 8}, + {24, 0x02, 8}, + {31, 0x02, 8}, + {41, 0x02, 8}, + {56, 0x03, 8}, + {3, 0x02, 11}, + {6, 0x02, 11}, + {10, 0x02, 11}, + {15, 0x02, 11}, + {24, 0x02, 11}, + {31, 0x02, 11}, + {41, 0x02, 11}, + {56, 0x03, 11}, + }, + + { + {3, 0x02, 12}, + {6, 0x02, 12}, + {10, 0x02, 12}, + {15, 0x02, 12}, + {24, 0x02, 12}, + {31, 0x02, 12}, + {41, 0x02, 12}, + {56, 0x03, 12}, + {3, 0x02, 14}, + {6, 0x02, 14}, + {10, 0x02, 14}, + {15, 0x02, 14}, + {24, 0x02, 14}, + {31, 0x02, 14}, + {41, 0x02, 14}, + {56, 0x03, 14}, + }, + + { + {2, 0x02, 15}, + {9, 0x02, 15}, + {23, 0x02, 15}, + {40, 0x03, 15}, + {2, 0x02, 16}, + {9, 0x02, 16}, + {23, 0x02, 16}, + {40, 0x03, 16}, + {2, 0x02, 17}, + {9, 0x02, 17}, + {23, 0x02, 17}, + {40, 0x03, 17}, + {2, 0x02, 18}, + {9, 0x02, 18}, + {23, 0x02, 18}, + {40, 0x03, 18}, + }, + + { + {3, 0x02, 15}, + {6, 0x02, 15}, + {10, 0x02, 15}, + {15, 0x02, 15}, + {24, 0x02, 15}, + {31, 0x02, 15}, + {41, 0x02, 15}, + {56, 0x03, 15}, + {3, 0x02, 16}, + {6, 0x02, 16}, + {10, 0x02, 16}, + {15, 0x02, 16}, + {24, 0x02, 16}, + {31, 0x02, 16}, + {41, 0x02, 16}, + {56, 0x03, 16}, + }, + + { + {3, 0x02, 17}, + {6, 0x02, 17}, + {10, 0x02, 17}, + {15, 0x02, 17}, + {24, 0x02, 17}, + {31, 0x02, 17}, + {41, 0x02, 17}, + {56, 0x03, 17}, + {3, 0x02, 18}, + {6, 0x02, 18}, + {10, 0x02, 18}, + {15, 0x02, 18}, + {24, 0x02, 18}, + {31, 0x02, 18}, + {41, 0x02, 18}, + {56, 0x03, 18}, + }, + + { + {0, 0x03, 19}, + {0, 0x03, 20}, + {0, 0x03, 21}, + {0, 0x03, 23}, + {0, 0x03, 24}, + {0, 0x03, 25}, + {0, 0x03, 26}, + {0, 0x03, 27}, + {0, 0x03, 28}, + {0, 0x03, 29}, + {0, 0x03, 30}, + {0, 0x03, 31}, + {0, 0x03, 127}, + {0, 0x03, 220}, + {0, 0x03, 249}, + {253, 0x00, 0}, + }, + + { + {1, 0x02, 19}, + {22, 0x03, 19}, + {1, 0x02, 20}, + {22, 0x03, 20}, + {1, 0x02, 21}, + {22, 0x03, 21}, + {1, 0x02, 23}, + {22, 0x03, 23}, + {1, 0x02, 24}, + {22, 0x03, 24}, + {1, 0x02, 25}, + {22, 0x03, 25}, + {1, 0x02, 26}, + {22, 0x03, 26}, + {1, 0x02, 27}, + {22, 0x03, 27}, + }, + + { + {2, 0x02, 19}, + {9, 0x02, 19}, + {23, 0x02, 19}, + {40, 0x03, 19}, + {2, 0x02, 20}, + {9, 0x02, 20}, + {23, 0x02, 20}, + {40, 0x03, 20}, + {2, 0x02, 21}, + {9, 0x02, 21}, + {23, 0x02, 21}, + {40, 0x03, 21}, + {2, 0x02, 23}, + {9, 0x02, 23}, + {23, 0x02, 23}, + {40, 0x03, 23}, + }, + + { + {3, 0x02, 19}, + {6, 0x02, 19}, + {10, 0x02, 19}, + {15, 0x02, 19}, + {24, 0x02, 19}, + {31, 0x02, 19}, + {41, 0x02, 19}, + {56, 0x03, 19}, + {3, 0x02, 20}, + {6, 0x02, 20}, + {10, 0x02, 20}, + {15, 0x02, 20}, + {24, 0x02, 20}, + {31, 0x02, 20}, + {41, 0x02, 20}, + {56, 0x03, 20}, + }, + + { + {3, 0x02, 21}, + {6, 0x02, 21}, + {10, 0x02, 21}, + {15, 0x02, 21}, + {24, 0x02, 21}, + {31, 0x02, 21}, + {41, 0x02, 21}, + {56, 0x03, 21}, + {3, 0x02, 23}, + {6, 0x02, 23}, + {10, 0x02, 23}, + {15, 0x02, 23}, + {24, 0x02, 23}, + {31, 0x02, 23}, + {41, 0x02, 23}, + {56, 0x03, 23}, + }, + + { + {2, 0x02, 24}, + {9, 0x02, 24}, + {23, 0x02, 24}, + {40, 0x03, 24}, + {2, 0x02, 25}, + {9, 0x02, 25}, + {23, 0x02, 25}, + {40, 0x03, 25}, + {2, 0x02, 26}, + {9, 0x02, 26}, + {23, 0x02, 26}, + {40, 0x03, 26}, + {2, 0x02, 27}, + {9, 0x02, 27}, + {23, 0x02, 27}, + {40, 0x03, 27}, + }, + + { + {3, 0x02, 24}, + {6, 0x02, 24}, + {10, 0x02, 24}, + {15, 0x02, 24}, + {24, 0x02, 24}, + {31, 0x02, 24}, + {41, 0x02, 24}, + {56, 0x03, 24}, + {3, 0x02, 25}, + {6, 0x02, 25}, + {10, 0x02, 25}, + {15, 0x02, 25}, + {24, 0x02, 25}, + {31, 0x02, 25}, + {41, 0x02, 25}, + {56, 0x03, 25}, + }, + + { + {3, 0x02, 26}, + {6, 0x02, 26}, + {10, 0x02, 26}, + {15, 0x02, 26}, + {24, 0x02, 26}, + {31, 0x02, 26}, + {41, 0x02, 26}, + {56, 0x03, 26}, + {3, 0x02, 27}, + {6, 0x02, 27}, + {10, 0x02, 27}, + {15, 0x02, 27}, + {24, 0x02, 27}, + {31, 0x02, 27}, + {41, 0x02, 27}, + {56, 0x03, 27}, + }, + + { + {1, 0x02, 28}, + {22, 0x03, 28}, + {1, 0x02, 29}, + {22, 0x03, 29}, + {1, 0x02, 30}, + {22, 0x03, 30}, + {1, 0x02, 31}, + {22, 0x03, 31}, + {1, 0x02, 127}, + {22, 0x03, 127}, + {1, 0x02, 220}, + {22, 0x03, 220}, + {1, 0x02, 249}, + {22, 0x03, 249}, + {254, 0x00, 0}, + {255, 0x00, 0}, + }, + + { + {2, 0x02, 28}, + {9, 0x02, 28}, + {23, 0x02, 28}, + {40, 0x03, 28}, + {2, 0x02, 29}, + {9, 0x02, 29}, + {23, 0x02, 29}, + {40, 0x03, 29}, + {2, 0x02, 30}, + {9, 0x02, 30}, + {23, 0x02, 30}, + {40, 0x03, 30}, + {2, 0x02, 31}, + {9, 0x02, 31}, + {23, 0x02, 31}, + {40, 0x03, 31}, + }, + + { + {3, 0x02, 28}, + {6, 0x02, 28}, + {10, 0x02, 28}, + {15, 0x02, 28}, + {24, 0x02, 28}, + {31, 0x02, 28}, + {41, 0x02, 28}, + {56, 0x03, 28}, + {3, 0x02, 29}, + {6, 0x02, 29}, + {10, 0x02, 29}, + {15, 0x02, 29}, + {24, 0x02, 29}, + {31, 0x02, 29}, + {41, 0x02, 29}, + {56, 0x03, 29}, + }, + + { + {3, 0x02, 30}, + {6, 0x02, 30}, + {10, 0x02, 30}, + {15, 0x02, 30}, + {24, 0x02, 30}, + {31, 0x02, 30}, + {41, 0x02, 30}, + {56, 0x03, 30}, + {3, 0x02, 31}, + {6, 0x02, 31}, + {10, 0x02, 31}, + {15, 0x02, 31}, + {24, 0x02, 31}, + {31, 0x02, 31}, + {41, 0x02, 31}, + {56, 0x03, 31}, + }, + + { + {2, 0x02, 127}, + {9, 0x02, 127}, + {23, 0x02, 127}, + {40, 0x03, 127}, + {2, 0x02, 220}, + {9, 0x02, 220}, + {23, 0x02, 220}, + {40, 0x03, 220}, + {2, 0x02, 249}, + {9, 0x02, 249}, + {23, 0x02, 249}, + {40, 0x03, 249}, + {0, 0x03, 10}, + {0, 0x03, 13}, + {0, 0x03, 22}, + {0, 0x04, 0}, + }, + + { + {3, 0x02, 127}, + {6, 0x02, 127}, + {10, 0x02, 127}, + {15, 0x02, 127}, + {24, 0x02, 127}, + {31, 0x02, 127}, + {41, 0x02, 127}, + {56, 0x03, 127}, + {3, 0x02, 220}, + {6, 0x02, 220}, + {10, 0x02, 220}, + {15, 0x02, 220}, + {24, 0x02, 220}, + {31, 0x02, 220}, + {41, 0x02, 220}, + {56, 0x03, 220}, + }, + + { + {3, 0x02, 249}, + {6, 0x02, 249}, + {10, 0x02, 249}, + {15, 0x02, 249}, + {24, 0x02, 249}, + {31, 0x02, 249}, + {41, 0x02, 249}, + {56, 0x03, 249}, + {1, 0x02, 10}, + {22, 0x03, 10}, + {1, 0x02, 13}, + {22, 0x03, 13}, + {1, 0x02, 22}, + {22, 0x03, 22}, + {0, 0x04, 0}, + {0, 0x04, 0}, + }, + + { + {2, 0x02, 10}, + {9, 0x02, 10}, + {23, 0x02, 10}, + {40, 0x03, 10}, + {2, 0x02, 13}, + {9, 0x02, 13}, + {23, 0x02, 13}, + {40, 0x03, 13}, + {2, 0x02, 22}, + {9, 0x02, 22}, + {23, 0x02, 22}, + {40, 0x03, 22}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + }, + + { + {3, 0x02, 10}, + {6, 0x02, 10}, + {10, 0x02, 10}, + {15, 0x02, 10}, + {24, 0x02, 10}, + {31, 0x02, 10}, + {41, 0x02, 10}, + {56, 0x03, 10}, + {3, 0x02, 13}, + {6, 0x02, 13}, + {10, 0x02, 13}, + {15, 0x02, 13}, + {24, 0x02, 13}, + {31, 0x02, 13}, + {41, 0x02, 13}, + {56, 0x03, 13}, + }, + + { + {3, 0x02, 22}, + {6, 0x02, 22}, + {10, 0x02, 22}, + {15, 0x02, 22}, + {24, 0x02, 22}, + {31, 0x02, 22}, + {41, 0x02, 22}, + {56, 0x03, 22}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + {0, 0x04, 0}, + }, + }; + + static const std::pair staticTable[] { + {":authority", ""}, + {":method", "GET"}, + {":method", "POST"}, + {":path", "/"}, + {":path", "/index.html"}, + {":scheme", "http"}, + {":scheme", "https"}, + {":status", "200"}, + {":status", "204"}, + {":status", "206"}, + {":status", "304"}, + {":status", "400"}, + {":status", "404"}, + {":status", "500"}, + {"accept-charset", ""}, + {"accept-encoding", "gzip, deflate"}, + {"accept-language", ""}, + {"accept-ranges", ""}, + {"accept", ""}, + {"access-control-allow-origin", ""}, + {"age", ""}, + {"allow", ""}, + {"authorization", ""}, + {"cache-control", ""}, + {"content-disposition", ""}, + {"content-encoding", ""}, + {"content-language", ""}, + {"content-length", ""}, + {"content-location", ""}, + {"content-range", ""}, + {"content-type", ""}, + {"cookie", ""}, + {"date", ""}, + {"etag", ""}, + {"expect", ""}, + {"expires", ""}, + {"from", ""}, + {"host", ""}, + {"if-match", ""}, + {"if-modified-since", ""}, + {"if-none-match", ""}, + {"if-range", ""}, + {"if-unmodified-since", ""}, + {"last-modified", ""}, + {"link", ""}, + {"location", ""}, + {"max-forwards", ""}, + {"proxy-authenticate", ""}, + {"proxy-authorization", ""}, + {"range", ""}, + {"referer", ""}, + {"refresh", ""}, + {"retry-after", ""}, + {"server", ""}, + {"set-cookie", ""}, + {"strict-transport-security", ""}, + {"transfer-encoding", ""}, + {"user-agent", ""}, + {"vary", ""}, + {"via", ""}, + {"www-authenticate", ""}, + }; + + static constexpr size_t getStaticTableSize() noexcept + { + return sizeof(staticTable) / sizeof(*staticTable); + } + + static size_t huffmanEncodeLength(const void *dest, const size_t length) noexcept + { + const uint8_t *data = reinterpret_cast(dest); + + size_t n = 0; + + for (size_t i = 0; i < length; ++i) + { + n += huffmanSymbolTable[data[i] ].nbits; + } + + return (n + 7) / 8; + } + + static uint8_t huffmanEncodeSymbol(std::vector &dest, uint8_t rembits, const HuffmanSymbol &sym) + { + uint8_t nbits = sym.nbits; + + for (;;) + { + if (rembits > nbits) + { + dest.back() |= uint8_t(sym.code << (rembits - nbits) ); + rembits -= nbits; + break; + } + + dest.back() |= uint8_t(sym.code >> (nbits - rembits) ); + + nbits -= rembits; + rembits = 8; + + if (0 == nbits) + { + break; + } + + dest.emplace_back(0); + } + + return rembits; + } + + static void encode(std::vector &dest, const void* src, const size_t srcSize) + { + const uint8_t *data = reinterpret_cast(src); + + uint8_t rembits = 8; + + for (size_t i = 0; i < srcSize; ++i) + { + const HuffmanSymbol &sym = huffmanSymbolTable[data[i] ]; + + if (8 == rembits) + { + dest.emplace_back(0); + } + + rembits = huffmanEncodeSymbol(dest, rembits, sym); + } + + if (rembits < 8) + { + const HuffmanSymbol &sym = huffmanSymbolTable[256]; + + dest.back() |= uint8_t(sym.code >> (sym.nbits - rembits) ); + } + } + + enum HuffmanDecode + { + ACCEPT = 0x1, + SYMBOL = 0x2, + FAIL = 0x4 + }; + + static bool decode(std::vector &dest, const void* src, const size_t srcSize) + { + const uint8_t *data = reinterpret_cast(src); + const uint8_t *end = data + srcSize; + + uint8_t state = 0; + bool accept = true; + + for (; data < end; ++data) + { + uint8_t c = *data; + uint8_t x = c >> 4; + + for (auto i = 0; i < 2; ++i) + { + const HuffmanDecodeNode &node = huffmanDecodeTable[state][x]; + + if (node.flags & HuffmanDecode::FAIL) + { + return false; + } + + if (node.flags & HuffmanDecode::SYMBOL) + { + dest.emplace_back(node.symbol); + } + + state = node.state; + accept = (node.flags & HuffmanDecode::ACCEPT); + + x = c & 0xf; + } + } + + return accept; + } + + static void packInteger(std::vector &dest, uint64_t num, const uint8_t prefix) + { + uint64_t k = (1 << prefix) - 1; + + if (num < k) + { + dest.emplace_back(num); + return; + } + + dest.emplace_back(k); + + num -= k; + + for (;;) + { + if (num < 128) + { + dest.emplace_back(num); + break; + } + + dest.emplace_back(0x80 | (num & 0x7f) ); + + num >>= 7; + + if (0 == num) + { + break; + } + } + } + + static void packIndex(std::vector &dest, const size_t index) + { + const size_t head = dest.size(); + + packInteger(dest, index, 7); + + dest[head] |= 0x80; + } + + static std::tuple findHeaderInTable(const std::pair &header, const Http2::DynamicTable &dynamicTable) + { + std::tuple result { + 0, false + }; + + size_t &index = std::get(result); + bool &is_full_match = std::get(result); + + for (size_t i = 0; i < getStaticTableSize(); ++i) + { + auto const &pair = staticTable[i]; + + if (pair.first == header.first) + { + index = i + 1; + + if (pair.second == header.second) + { + is_full_match = true; + + return result; + } + } + else if (0 != index) + { + break; + } + } + + for (size_t i = 0; i < dynamicTable.size(); ++i) + { + auto const &pair = dynamicTable[i]; + + if (pair.first == header.first) + { + index = i + getStaticTableSize() + 1; + + if (pair.second == header.second) + { + is_full_match = true; + + break; + } + } + } + + return result; + } + + static uint8_t packFirstByte(const bool indexing) noexcept + { + if (indexing) + { + return 0x40; + } + + /* if (never_indexing) + { + return 0x10; + }*/ + + return 0; + } + + static void packString(std::vector &dest, const std::string &str) + { + const size_t huffman_length = huffmanEncodeLength(str.data(), str.length() ); + + if (huffman_length < str.length() ) + { + const size_t head = dest.size(); + + packInteger(dest, huffman_length, 7); + encode(dest, str.data(), str.length() ); + + dest[head] |= 0x80; + } + else + { + packInteger(dest, str.length(), 7); + std::copy(str.cbegin(), str.cend(), std::back_inserter(dest) ); + } + } + + static void packFullHeader(std::vector &dest, const std::pair &header, const bool indexing) + { + dest.emplace_back(packFirstByte(indexing) ); + packString(dest, header.first); + packString(dest, header.second); + } + + static void packHeaderValue(std::vector &dest, const size_t keyIndex, const std::pair &header, const bool indexing) + { + const size_t head = dest.size(); + + const uint8_t prefix = indexing ? 6 : 4; + + packInteger(dest, keyIndex, prefix); + + dest[head] |= packFirstByte(indexing); + + packString(dest, header.second); + } + + static bool shouldIndexing(const std::pair &header) + { + /* const std::string &key = header.first; + + if ("content-length" == key || "set-cookie" == key) + { + return true; + }*/ + + return false; + } + + static void packHeader(std::vector &dest, const std::pair &header, Http2::DynamicTable &dynamicTable) + { + size_t index; + bool is_full_match; + + std::tie(index, is_full_match) = findHeaderInTable(header, dynamicTable); + + if (is_full_match) + { + packIndex(dest, index); + + return; + } + + const bool indexing = shouldIndexing(header); + + if (indexing) + { + dynamicTable.addHeader(header); + } + + if (0 == index) + { + packFullHeader(dest, header, indexing); + } + else + { + packHeaderValue(dest, index, header, indexing); + } + } +/* + static void packTableSize(std::vector &dest, const size_t tableSize) + { + const size_t head = dest.size(); + + packInteger(dest, tableSize, 5); + + dest[head] |= 0x20; + } +*/ + void pack(std::vector &dest, const std::vector > &headers, Http2::DynamicTable &dynamicTable) + { + for (auto const &header : headers) + { + packHeader(dest, header, dynamicTable); + } + } + + enum class HuffmanDecodeOpcode + { + NONE, + INDEXED, + NOT_INDEXED, + INDEXED_KEY, + }; + + enum class HuffmanDecodeState + { + OPCODE, + READ_TABLE_SIZE, + READ_INDEX, + CHECK_KEY_LENGTH, + READ_KEY_LENGTH, + READ_KEY_HUFFMAN, + READ_KEY, + CHECK_VALUE_LENGTH, + READ_VALUE_LENGTH, + READ_VALUE_HUFFMAN, + READ_VALUE, + }; + + static size_t getMaxTableIndex(const Http2::IncStream &stream) noexcept + { + return getStaticTableSize() + stream.conn.decoding_dynamic_table.size(); + } + + static const std::pair *getHeaderFromTable(const size_t index, const Http2::IncStream &stream) noexcept + { + if (getStaticTableSize() >= index) + { + return &staticTable[index - 1]; + } + else if (stream.conn.decoding_dynamic_table.size() + getStaticTableSize() >= index) + { + return &stream.conn.decoding_dynamic_table[index - getStaticTableSize() - 1]; + } + + return nullptr; + } + + static const std::pair *unpackIndexed(const size_t index, const Http2::IncStream &stream) noexcept + { + return getHeaderFromTable(index, stream); + } + + static bool checkHuffmanEncoded(const uint8_t c) noexcept + { + return c & (1 << 7); + } + + static std::tuple unpackHuffman(std::vector &dest, const uint8_t *data, const size_t dataSize, const uint64_t left) + { + std::tuple result { + left, false + }; + + if (dataSize < left) + { + std::get(result) = dataSize; + } + + std::get(result) = decode(dest, data, std::get(result) ); + + return result; + } + + static uint64_t unpackString(std::vector &dest, const uint8_t *data, const size_t dataSize, uint64_t left) + { + if (dataSize < left) + { + left = dataSize; + } + + std::copy(data, data + left, std::back_inserter(dest) ); + + return left; + } + + static std::tuple unpackInteger(const uint8_t *data, const size_t dataSize, const uint64_t initial, const uint8_t initialShift, const uint8_t prefix) + { + std::tuple result { + initial, 0, initialShift, true + }; + + uint64_t &num = std::get<0>(result); + uint64_t &nread = std::get<1>(result); + uint8_t &shift = std::get(result); + + uint8_t k = (1 << prefix) - 1; + + if (0 == num) + { + const uint8_t c = data[0]; + + ++nread; + + if (k != (c & k) ) + { + num = c & k; + + return result; + } + + num = k; + + if (nread == dataSize) + { + return result; + } + } + + for (; nread < dataSize; ++nread) + { + const uint8_t c = data[nread]; + + uint32_t add = c & 0x7f; + + if (add > (std::numeric_limits::max() >> shift) ) + { + std::get(result) = false; + + return result; + } + + add <<= shift; + + if (std::numeric_limits::max() - add < num) + { + std::get(result) = false; + + return result; + } + + num += add; + + if (0 == (c & (1 << 7) ) ) + { + break; + } + + shift += 7; + } + + if (nread != dataSize) + { + ++nread; + } + + return result; + } + + class HuffmanDecoder + { + public: + std::vector buf; + + size_t key_index = 0; + size_t key_length = 0; + + HuffmanDecodeOpcode opcode = HuffmanDecodeOpcode::NONE; + HuffmanDecodeState state = HuffmanDecodeState::OPCODE; + + uint64_t left = 0; + uint8_t shift = 0; + + bool index_required = false; + bool never_indexed = false; + bool huffman_encoded = false; + + public: + std::pair unpackFullHeader(Http2::IncStream &stream) + { + std::pair header { + std::string(this->buf.cbegin(), this->buf.cbegin() + this->key_length), + std::string(this->buf.cbegin() + this->key_length, this->buf.cend() ) + }; + + this->buf.clear(); + + if (this->index_required) + { + stream.conn.decoding_dynamic_table.addHeader(header); + } + + return header; + } + + std::pair unpackHeaderValue(Http2::IncStream &stream) + { + auto entry = getHeaderFromTable(this->key_index, stream); + + if (nullptr == entry) + { + return std::pair(); + } + + std::pair header { + entry->first, + std::string(this->buf.cbegin(), this->buf.cend() ) + }; + + this->key_index = 0; + this->buf.clear(); + + if (this->index_required) + { + stream.conn.decoding_dynamic_table.addHeader(header); + } + + return header; + } + }; + + bool unpack(const void *src, const size_t srcSize, Http2::IncStream &stream) + { + const uint8_t *data = reinterpret_cast(src); + + size_t cur = 0; + + HuffmanDecoder dec; + + while (cur < srcSize) + { + switch (dec.state) + { + case HuffmanDecodeState::OPCODE: + { + const uint8_t c = data[cur]; + + if (0x20 == (c & 0xe0) ) + { + dec.opcode = HuffmanDecodeOpcode::INDEXED; + dec.state = HuffmanDecodeState::READ_TABLE_SIZE; + } + else if (c & 0x80) + { + dec.opcode = HuffmanDecodeOpcode::INDEXED; + dec.state = HuffmanDecodeState::READ_INDEX; + } + else + { + if (0 == c || 0x40 == c || 0x10 == c) + { + dec.opcode = HuffmanDecodeOpcode::NOT_INDEXED; + dec.state = HuffmanDecodeState::CHECK_KEY_LENGTH; + + ++cur; + } + else + { + dec.opcode = HuffmanDecodeOpcode::INDEXED_KEY; + dec.state = HuffmanDecodeState::READ_INDEX; + } + + dec.index_required = (c & 0x40); + dec.never_indexed = (c & 0xf0) == 0x10; + } + + dec.left = dec.shift = 0; + + break; + } + + case HuffmanDecodeState::READ_TABLE_SIZE: + { + uint64_t nread; + bool success; + + std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, 5); + + cur += nread; + + if (false == success) + { + return false; + } + + if (dec.left > stream.conn.client_settings.header_table_size) + { + // TODO: invalid max table size error code + return false; + } + + stream.conn.decoding_dynamic_table.changeHeaderTableSize(dec.left); + + dec.state = HuffmanDecodeState::OPCODE; + + break; + } + + case HuffmanDecodeState::READ_INDEX: + { + uint8_t prefixlen; + + if (HuffmanDecodeOpcode::INDEXED == dec.opcode) + { + prefixlen = 7; + } + else if (dec.index_required) + { + prefixlen = 6; + } + else + { + prefixlen = 4; + } + + uint64_t nread; + bool success; + + std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, prefixlen); + + cur += nread; + + if (false == success || 0 == dec.left || dec.left > getMaxTableIndex(stream) ) + { + return false; + } + + if (HuffmanDecodeOpcode::INDEXED == dec.opcode) + { + stream.incoming_headers.emplace(*unpackIndexed(dec.left, stream) ); + + dec.state = HuffmanDecodeState::OPCODE; + } + else + { + dec.key_index = dec.left; + + dec.state = HuffmanDecodeState::CHECK_VALUE_LENGTH; + } + + break; + } + + case HuffmanDecodeState::CHECK_KEY_LENGTH: + { + dec.huffman_encoded = checkHuffmanEncoded(data[cur]); + dec.state = HuffmanDecodeState::READ_KEY_LENGTH; + dec.left = dec.shift = 0; + break; + } + + case HuffmanDecodeState::READ_KEY_LENGTH: + { + uint64_t nread; + bool success; + + std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, 7); + + cur += nread; + + if (false == success) + { + return false; + } + + dec.state = dec.huffman_encoded ? + HuffmanDecodeState::READ_KEY_HUFFMAN : + HuffmanDecodeState::READ_KEY; + + break; + } + + case HuffmanDecodeState::READ_KEY_HUFFMAN: + { + uint64_t nread; + bool success; + + std::tie(nread, success) = unpackHuffman(dec.buf, data + cur, srcSize - cur, dec.left); + + cur += nread; + dec.left -= nread; + + if (false == success || dec.left > 0) + { + return false; + } + + dec.key_length = dec.buf.size(); + + dec.state = HuffmanDecodeState::CHECK_VALUE_LENGTH; + + break; + } + + case HuffmanDecodeState::READ_KEY: + { + const uint64_t nread = unpackString(dec.buf, data + cur, srcSize - cur, dec.left); + + cur += nread; + dec.left -= nread; + + if (dec.left > 0) + { + return false; + } + + dec.key_length = dec.buf.size(); + + dec.state = HuffmanDecodeState::CHECK_VALUE_LENGTH; + + break; + } + + case HuffmanDecodeState::CHECK_VALUE_LENGTH: + { + dec.huffman_encoded = checkHuffmanEncoded(data[cur]); + dec.state = HuffmanDecodeState::READ_VALUE_LENGTH; + dec.left = dec.shift = 0; + break; + } + + case HuffmanDecodeState::READ_VALUE_LENGTH: + { + uint64_t nread; + bool success; + + std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, 7); + + cur += nread; + + if (false == success) + { + return false; + } + + if (0 == dec.left) + { + HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode ? + stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) : + stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); + + dec.state = HuffmanDecodeState::OPCODE; + + break; + } + + dec.state = dec.huffman_encoded ? + HuffmanDecodeState::READ_VALUE_HUFFMAN : + HuffmanDecodeState::READ_VALUE; + + break; + } + + case HuffmanDecodeState::READ_VALUE_HUFFMAN: + { + uint64_t nread; + bool success; + + std::tie(nread, success) = unpackHuffman(dec.buf, data + cur, srcSize - cur, dec.left); + + cur += nread; + dec.left -= nread; + + if (false == success || dec.left > 0) + { + return false; + } + + HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode ? + stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) : + stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); + + dec.state = HuffmanDecodeState::OPCODE; + + break; + } + + case HuffmanDecodeState::READ_VALUE: + { + const uint64_t nread = unpackString(dec.buf, data + cur, srcSize - cur, dec.left); + + cur += nread; + dec.left -= nread; + + if (dec.left > 0) + { + return false; + } + + HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode ? + stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) : + stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); + + dec.state = HuffmanDecodeState::OPCODE; + + break; + } + } + } + + return true; + } +}; diff --git a/src/transfer/http2/HPack.h b/src/transfer/http2/HPack.h new file mode 100644 index 0000000..d71375c --- /dev/null +++ b/src/transfer/http2/HPack.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Http2.h" + +#include +#include +#include + +namespace HPack +{ + void pack(std::vector &dest, const std::vector > &headers, Http2::DynamicTable &dynamicTable); + + // TODO: replace IncStream to DynamicTable if possible + bool unpack(const void *src, const size_t srcSize, Http2::IncStream &stream); +}; diff --git a/src/transfer/http2/Http2.cpp b/src/transfer/http2/Http2.cpp new file mode 100644 index 0000000..208eb79 --- /dev/null +++ b/src/transfer/http2/Http2.cpp @@ -0,0 +1,182 @@ + +#include "Http2.h" +#include "../../utils/Utils.h" + +namespace Http2 +{ + bool operator &(const FrameFlag left, const FrameFlag right) noexcept + { + return static_cast(left) & static_cast(right); + } + + FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept + { + return static_cast(static_cast(left) | static_cast(right) ); + } + + FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept + { + return static_cast(*reinterpret_cast(&left) |= static_cast(right) ); + } + + ConnectionSettings ConnectionSettings::defaultSettings() noexcept + { + return ConnectionSettings { + 4096, + 1, + 0, + (1 << 16) - 1, // 65535 + 1 << 14, // 16384 + 0 + }; + } + + DynamicTable::DynamicTable() noexcept + : header_table_size(0), max_header_list_size(0), cur_header_list_size(0) + { + + } + + size_t DynamicTable::size() const noexcept + { + return this->list.size(); + } + + DynamicTable::DynamicTable(const uint32_t headerTableSize, const uint32_t maxHeaderListSize, std::deque > &&list) noexcept + : list(std::move(list) ), header_table_size(headerTableSize), max_header_list_size(maxHeaderListSize), cur_header_list_size(0) + { + for (auto const &pair : list) + { + this->cur_header_list_size += pair.first.length() + pair.second.length(); + } + } + + void DynamicTable::addHeader(const std::pair &header) + { + this->cur_header_list_size += header.first.length() + header.second.length(); + + this->list.emplace_front(header); + + while (this->list.size() > this->header_table_size || (0 != this->max_header_list_size && this->cur_header_list_size > this->max_header_list_size) ) + { + auto const &pair = this->list.back(); + + this->cur_header_list_size -= pair.first.length() + pair.second.length(); + + this->list.pop_back(); + } + } + + void DynamicTable::addHeader(std::pair &&header) + { + this->cur_header_list_size += header.first.length() + header.second.length(); + + this->list.emplace_front(std::move(header) ); + + while (this->list.size() > this->header_table_size || (0 != this->max_header_list_size && this->cur_header_list_size > this->max_header_list_size) ) + { + auto const &pair = this->list.back(); + + this->cur_header_list_size -= pair.first.length() + pair.second.length(); + + this->list.pop_back(); + } + } + + void DynamicTable::changeHeaderTableSize(const uint32_t headerTableSize) + { + this->header_table_size = headerTableSize; + + while (this->list.size() > this->header_table_size) + { + auto const &pair = this->list.back(); + + this->cur_header_list_size -= pair.first.length() + pair.second.length(); + + this->list.pop_back(); + } + } + + void DynamicTable::changeMaxHeaderListSize(const uint32_t maxHeaderListSize) + { + this->max_header_list_size = maxHeaderListSize; + + while (0 != this->max_header_list_size && this->cur_header_list_size > this->max_header_list_size) + { + auto const &pair = this->list.back(); + + this->cur_header_list_size -= pair.first.length() + pair.second.length(); + + this->list.pop_back(); + } + } + + const std::pair &DynamicTable::operator[](const size_t index) const noexcept + { + return this->list[index]; + } + + std::pair &DynamicTable::operator[](const size_t index) noexcept + { + return this->list[index]; + } + + const std::deque > &DynamicTable::getList() const noexcept + { + return this->list; + } + + IncStream::IncStream(const uint32_t streamId, ConnectionData &conn) noexcept + : conn(conn), window_size_inc(conn.server_settings.initial_window_size), + window_size_out(conn.client_settings.initial_window_size), stream_id(streamId), + state(StreamState::IDLE), priority(0), reserved(nullptr) + { + + } + + uint8_t *IncStream::setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept + { + Utils::hton24(addr, frameSize); + *(addr + 3) = static_cast(frameType); + *(addr + 4) = static_cast(frameFlags); + *reinterpret_cast(addr + 5) = ::htonl(this->stream_id); + + return (addr + Http2::FRAME_HEADER_SIZE); + } + + void IncStream::close() noexcept + { + this->incoming_headers.clear(); + this->incoming_data.clear(); + this->incoming_files.clear(); + this->reserved = nullptr; + + ++this->conn.sync.completed; + this->conn.sync.event.notify(); + + // this->state = StreamState::CLOSED; + } + + OutStream::OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table) noexcept + : stream_id(streamId), settings(settings), window_size_out(settings.initial_window_size), dynamic_table(std::move(dynamic_table) ) + { + + } + + OutStream::OutStream(const IncStream &stream) noexcept + : stream_id(stream.stream_id), settings(stream.conn.client_settings), + window_size_out(stream.window_size_out), dynamic_table(stream.conn.encoding_dynamic_table) + { + + } + + uint8_t *OutStream::setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept + { + Utils::hton24(addr, frameSize); + *(addr + 3) = static_cast(frameType); + *(addr + 4) = static_cast(frameFlags); + *reinterpret_cast(addr + 5) = ::htonl(this->stream_id); + + return (addr + Http2::FRAME_HEADER_SIZE); + } +}; diff --git a/src/transfer/http2/Http2.h b/src/transfer/http2/Http2.h new file mode 100644 index 0000000..a754b38 --- /dev/null +++ b/src/transfer/http2/Http2.h @@ -0,0 +1,191 @@ +#pragma once + +#include "../../utils/Event.h" +#include "../AppRequest.h" + +#include +#include +#include +#include +#include + +#include + +namespace Http2 +{ + enum class ErrorCode : uint32_t + { + NO_ERROR = 0x0, + PROTOCOL_ERROR, + INTERNAL_ERROR, + FLOW_CONTROL_ERROR, + SETTINGS_TIMEOUT, + STREAM_CLOSED, + FRAME_SIZE_ERROR, + REFUSED_STREAM, + CANCEL, + COMPRESSION_ERROR, + CONNECT_ERROR, + ENHANCE_YOUR_CALM, + INADEQUATE_SECURITY, + HTTP_1_1_REQUIRED + }; + + enum class FrameType : uint8_t + { + DATA = 0x0, + HEADERS, + PRIORITY, + RST_STREAM, + SETTINGS, + PUSH_PROMISE, + PING, + GOAWAY, + WINDOW_UPDATE, + CONTINUATION + }; + + enum class FrameFlag : uint8_t + { + EMPTY = 0x0, + ACK = 0x1, + END_STREAM = 0x1, + END_HEADERS = 0x4, + PADDED = 0x8, + PRIORITY = 0x20 + }; + + bool operator &(const FrameFlag left, const FrameFlag right) noexcept; + FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept; + FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept; + + struct FrameMeta + { + uint32_t stream_id; + uint32_t length; + FrameType type; + FrameFlag flags; + }; + + constexpr uint32_t FRAME_HEADER_SIZE = 9; + constexpr uint32_t MAX_WINDOW_UPDATE = uint32_t(1 << 31) - 1; + + enum class ConnectionSetting : uint16_t + { + SETTINGS_HEADER_TABLE_SIZE = 0x1, + SETTINGS_ENABLE_PUSH = 0x2, + SETTINGS_MAX_CONCURRENT_STREAMS = 0x3, + SETTINGS_INITIAL_WINDOW_SIZE = 0x4, + SETTINGS_MAX_FRAME_SIZE = 0x5, + SETTINGS_MAX_HEADER_LIST_SIZE = 0x6 + }; + + struct ConnectionSettings + { + uint32_t header_table_size; + uint32_t enable_push; + uint32_t max_concurrent_streams; + uint32_t initial_window_size; + uint32_t max_frame_size; + uint32_t max_header_list_size; + + static ConnectionSettings defaultSettings() noexcept; + }; + + enum class StreamState : uint8_t + { + IDLE, + RESERVED, + OPEN, + HALF_CLOSED, + CLOSED + }; + + struct ConnectionSync + { + Utils::Event event; + std::mutex mtx; + std::atomic completed {}; + }; + + class DynamicTable + { + private: + std::deque > list; + + uint32_t header_table_size; + uint32_t max_header_list_size; + + uint32_t cur_header_list_size; + + public: + DynamicTable() noexcept; + DynamicTable(const uint32_t headerTableSize, const uint32_t maxHeaderListSize, std::deque > &&list) noexcept; + + size_t size() const noexcept; + + void addHeader(const std::pair &header); + void addHeader(std::pair &&header); + + void changeHeaderTableSize(const uint32_t headerTableSize); + void changeMaxHeaderListSize(const uint32_t maxHeaderListSize); + + const std::pair &operator[](const size_t index) const noexcept; + std::pair &operator[](const size_t index) noexcept; + + const std::deque > &getList() const noexcept; + }; + + struct ConnectionData + { + DynamicTable decoding_dynamic_table; + DynamicTable encoding_dynamic_table; + + ConnectionSettings client_settings; + ConnectionSettings server_settings; + + ConnectionSync sync; + }; + + class IncStream : public Transfer::request_data + { + public: + ConnectionData &conn; + + int32_t window_size_inc; + int32_t window_size_out; + + uint32_t stream_id; + + StreamState state; + uint8_t priority; + + FrameType frame_type; + + void *reserved; + + public: + IncStream(const uint32_t streamId, ConnectionData &conn) noexcept; + + uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept; + + void close() noexcept; + }; + + struct OutStream + { + uint32_t stream_id; + + ConnectionSettings settings; + + int32_t window_size_out; + + DynamicTable dynamic_table; + + public: + OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table) noexcept; + OutStream(const IncStream &stream) noexcept; + + uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept; + }; +}; diff --git a/src/Event.cpp b/src/utils/Event.cpp similarity index 83% rename from src/Event.cpp rename to src/utils/Event.cpp index 77eeb40..6b23628 100644 --- a/src/Event.cpp +++ b/src/utils/Event.cpp @@ -1,9 +1,10 @@ #include "Event.h" -namespace HttpServer +namespace Utils { - Event::Event(const bool _signaled, const bool _manually): signaled(_signaled), manually(_manually) + Event::Event(const bool signaled, const bool manually) noexcept + : signaled(signaled), manually(manually) { } @@ -73,13 +74,13 @@ namespace HttpServer return is_timeout; } - void Event::notify() + void Event::notify() noexcept { this->signaled.store(true); this->cv.notify_all(); } - void Event::notify(const size_t threadsCount) + void Event::notify(const size_t threadsCount) noexcept { this->signaled.store(true); @@ -89,13 +90,13 @@ namespace HttpServer } } - void Event::reset() + void Event::reset() noexcept { this->signaled.store(false); } - bool Event::notifed() const + bool Event::notifed() const noexcept { return this->signaled.load(); } -}; \ No newline at end of file +}; diff --git a/src/Event.h b/src/utils/Event.h similarity index 59% rename from src/Event.h rename to src/utils/Event.h index c9f1174..ddacbe4 100644 --- a/src/Event.h +++ b/src/utils/Event.h @@ -4,7 +4,7 @@ #include #include -namespace HttpServer +namespace Utils { class Event { @@ -15,19 +15,19 @@ namespace HttpServer bool manually; public: - Event(const bool _signaled = false, const bool _manualy = false); - ~Event() = default; + Event(const bool signaled = false, const bool manualy = false) noexcept; + ~Event() noexcept = default; public: void wait(); bool wait_for(const std::chrono::milliseconds &ms); bool wait_until(const std::chrono::high_resolution_clock::time_point &tp); - void notify(); - void notify(const size_t threadsCount); + void notify() noexcept; + void notify(const size_t threadsCount) noexcept; - void reset(); + void reset() noexcept; - bool notifed() const; + bool notifed() const noexcept; }; -}; \ No newline at end of file +}; diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp new file mode 100644 index 0000000..dc06e23 --- /dev/null +++ b/src/utils/Utils.cpp @@ -0,0 +1,841 @@ + +#include "Utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Utils +{ + void toLower(std::string &str) noexcept + { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + } + + std::string getLowerString(const std::string &str) + { + std::string copy = str; + + toLower(copy); + + return copy; + } + + void trim(std::string &str) + { + static const std::array whitespace { " \t\n\v\f\r" }; + + const size_t last = str.find_last_not_of(whitespace.data() ); + + if (std::string::npos == last) + { + return str.clear(); + } + + str.assign(str.cbegin() + str.find_first_not_of(whitespace.data() ), str.cbegin() + last + 1); + } + + std::string getTrimmedString(const std::string &str) + { + std::string copy = str; + + trim(copy); + + return copy; + } + + std::vector explode(const std::string &str, const char sep) + { + std::vector values; + + for (size_t pos = 0; std::string::npos != pos;) + { + size_t delimiter = str.find(sep, pos); + + std::string value = str.substr(pos, delimiter); + trim(value); + + values.emplace_back(std::move(value) ); + + pos = delimiter; + + if (std::string::npos != pos) + { + ++pos; + } + } + + return values; + } + + std::string encodeHtmlSymbols(const std::string &str) + { + std::string buf; + buf.reserve(str.length() ); + + for (size_t pos = 0; pos < str.length(); ++pos) + { + switch (str[pos]) + { + case '&': buf.append("&"); break; + case '\"': buf.append("""); break; + case '\'': buf.append("'"); break; + case '<': buf.append("<"); break; + case '>': buf.append(">"); break; + default: buf.push_back(str[pos]); break; + } + } + + return buf; + } + + std::string binToHexString(const void *binData, const size_t dataSize) + { + std::string str(dataSize * 2, 0); + + const uint8_t *bin = reinterpret_cast(binData); + + static const std::array hexDigits { "0123456789abcdef" }; + + for (size_t i = dataSize - 1; std::numeric_limits::max() != i; --i) + { + str[i * 2 + 0] = hexDigits[bin[i] >> 4]; + str[i * 2 + 1] = hexDigits[bin[i] & 0x0F]; + } + + return str; + } + + static unsigned char hexStringToBinEncodeSymbol(const char c) noexcept + { + if (c >= '0' && c <= '9') + { + return c - 0x30; + } + else if (c >= 'a' && c <= 'f') + { + return c - 0x57; + } + else if (c >= 'A' && c <= 'F') + { + return c - 0x37; + } + + return 0; + } + + std::string hexStringToBin(const std::string &hexStr) + { + std::string bin(hexStr.length() / 2, 0); + + for (size_t i = 0; i < bin.length(); ++i) + { + const char a = hexStr[i * 2 + 0]; + const char b = hexStr[i * 2 + 1]; + + bin[i] = ( + (hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b) + ); + } + + return bin; + } + + enum Endianness { + INIT = 0, + LITE = 1, + BIGE = 2 + }; + + uint64_t hton64(const uint64_t host64) noexcept + { + static Endianness endian = Endianness::INIT; + + union { + uint64_t ull; + unsigned char c[sizeof(uint64_t)]; + } x; + + if (endian == Endianness::INIT) + { + x.ull = 0x01; + endian = (x.c[7] == 0x01ULL) ? Endianness::BIGE : Endianness::LITE; + } + + if (endian == Endianness::BIGE) + { + return host64; + } + + x.ull = host64; + + unsigned char c; + + c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c; + c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c; + c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c; + c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c; + + return x.ull; + } + + uint64_t ntoh64(const uint64_t net64) noexcept + { + return hton64(net64); + } + + void hton24(void *dest, const uint32_t src) noexcept + { + static Endianness endian = Endianness::INIT; + + union { + uint32_t ui; + uint8_t c[sizeof(uint32_t)]; + } x; + + if (endian == Endianness::INIT) + { + x.ui = 0x01; + endian = (x.c[3] == 0x01) ? Endianness::BIGE : Endianness::LITE; + } + + x.ui = src; + + if (endian == Endianness::BIGE) + { + x.ui <<= 8; + } + else + { + uint8_t c = x.c[0]; + x.c[0] = x.c[2]; + x.c[2] = c; + } + + std::copy(x.c, x.c + 3, reinterpret_cast(dest) ); + } + + uint32_t ntoh24(const void *src24) noexcept + { + static Endianness endian = Endianness::INIT; + + union { + uint32_t ui; + uint8_t c[sizeof(uint32_t)]; + } x; + + if (endian == Endianness::INIT) + { + x.ui = 0x01; + endian = (x.c[3] == 0x01) ? Endianness::BIGE : Endianness::LITE; + } + + if (endian == Endianness::BIGE) + { + return *reinterpret_cast(src24) >> 8; + } + + const uint8_t *addr = reinterpret_cast(src24); + + x.ui = 0; + + x.c[0] = addr[2]; + x.c[1] = addr[1]; + x.c[2] = addr[0]; + + return x.ui;// *reinterpret_cast(x.c); + } + + std::string getUniqueName() + { + size_t time = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + + time = hton64(time); + + return binToHexString(&time, sizeof(time) ); + } + + size_t getPackNumberSize(const size_t number) noexcept + { + if (number <= 253) + { + return sizeof(uint8_t); + } + else if (number <= std::numeric_limits::max() ) + { + return sizeof(uint8_t) + sizeof(uint16_t); + } + else if (number <= std::numeric_limits::max() ) + { + return sizeof(uint8_t) + sizeof(uint32_t); + } + + return sizeof(uint8_t) + sizeof(size_t); + } + + size_t getPackStringSize(const std::string &str) noexcept + { + return getPackNumberSize(str.length() ) + str.length(); + } + + uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept + { + if (number <= 252) + { + *dest = number; + + dest += sizeof(uint8_t); + } + else if (number <= std::numeric_limits::max() ) + { + *dest = 253; + + dest += sizeof(uint8_t); + + *reinterpret_cast(dest) = static_cast(number); + + dest += sizeof(uint16_t); + } + else if (number <= std::numeric_limits::max() ) + { + *dest = 254; + + dest += sizeof(uint8_t); + + *reinterpret_cast(dest) = static_cast(number); + + dest += sizeof(uint32_t); + } + else + { + *dest = 255; + + dest += sizeof(uint8_t); + + *reinterpret_cast(dest) = number; + + dest += sizeof(size_t); + } + + return dest; + } + + uint8_t *packString(uint8_t *dest, const std::string &str) noexcept + { + dest = packNumber(dest, str.length() ); + + std::memcpy(dest, str.data(), str.length() ); + + return dest + str.length(); + } + + void packNumber(std::vector &buf, const size_t number) + { + if (number <= 252) + { + buf.emplace_back(number); + } + else if (number <= std::numeric_limits::max() ) + { + buf.emplace_back(253); + + buf.resize(buf.size() + sizeof(uint16_t) ); + + *reinterpret_cast(buf.data() + buf.size() - sizeof(uint16_t) ) = static_cast(number); + } + else if (number <= std::numeric_limits::max() ) + { + buf.emplace_back(254); + + buf.resize(buf.size() + sizeof(uint32_t) ); + + *reinterpret_cast(buf.data() + buf.size() - sizeof(uint32_t) ) = static_cast(number); + } + else + { + buf.emplace_back(255); + + buf.resize(buf.size() + sizeof(size_t) ); + + *reinterpret_cast(buf.data() + buf.size() - sizeof(size_t) ) = number; + } + } + + void packString(std::vector &buf, const std::string &str) + { + packNumber(buf, str.length() ); + + if (str.length() ) + { + std::copy(str.cbegin(), str.cend(), std::back_inserter(buf) ); + } + } + + const uint8_t *unpackNumber(size_t *number, const uint8_t *src) noexcept + { + *number = *src; + + src += sizeof(uint8_t); + + if (*number <= 252) + { + + } + else if (*number == 253) + { + *number = *reinterpret_cast(src); + + src += sizeof(uint16_t); + } + else if (*number == 254) + { + *number = *reinterpret_cast(src); + + src += sizeof(uint32_t); + } + else + { + *number = *reinterpret_cast(src); + + src += sizeof(size_t); + } + + return src; + } + + const uint8_t *unpackString(std::string &str, const uint8_t *src) + { + size_t length; + src = unpackNumber(&length, src); + + str.assign(src, src + length); + + return src + length; + } + + static const std::unordered_map map_days { + {"Mon", 0}, {"Tue", 1}, {"Wed", 2}, {"Thu", 3}, {"Fri", 4}, {"Sat", 5}, {"Sun", 6} + }; + + static const std::unordered_map map_months { + {"Jan", 0}, {"Feb", 1}, {"Mar", 2}, {"Apr", 3}, {"May", 4}, {"Jun", 5}, {"Jul", 6}, {"Aug", 7}, {"Sep", 8}, {"Oct", 9}, {"Nov", 10}, {"Dec", 11} + }; + + static const std::unordered_map map_zones { + {"GMT", 0}, {"UT", 0}, + {"EST", -5 * 3600}, {"EDT", -4 * 3600}, {"CST", -6 * 3600}, {"CDT", -5 * 3600}, {"MST", -7 * 3600}, {"MDT", -6 * 3600}, {"PST", -8 * 3600}, {"PDT", -7 * 3600}, + {"Z", 0}, {"A", -1 * 3600}, {"M", -12 * 3600}, {"N", 1 * 3600}, {"Y", 12 * 3600} + }; + + /** + * Parse RFC 882 (ddd, dd MMM yyyy HH:mm:ss K) + */ + time_t rfc822DatetimeToTimestamp(const std::string &strTime) + { + std::tm tc = {}; + + // Parse RFC 882 (ddd, dd MMM yyyy HH:mm:ss K) + + size_t pos = strTime.find_first_not_of(' '); + size_t delimiter = strTime.find(',', pos); + + if (std::string::npos == delimiter || delimiter - pos != 3) + { + return ~0; + } + + const std::string day = strTime.substr(pos, delimiter - pos); + + auto const it_day = map_days.find(day); + + if (map_days.cend() != it_day) + { + tc.tm_wday = it_day->second; + } + else + { + return ~0; + } + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(' ', pos); + + if (std::string::npos == delimiter) + { + return ~0; + } + + tc.tm_mday = std::strtoul(strTime.data() + pos, nullptr, 10); + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(' ', pos); + + if (std::string::npos == delimiter || delimiter - pos != 3) + { + return ~0; + } + + const std::string month = strTime.substr(pos, delimiter - pos); + + auto const it_mon = map_months.find(month); + + if (map_months.cend() != it_mon) + { + tc.tm_mon = it_mon->second; + } + else + { + return ~0; + } + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(' ', pos); + + if (std::string::npos == delimiter) + { + return ~0; + } + + tc.tm_year = std::strtoul(strTime.data() + pos, nullptr, 10) - 1900; + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(':', pos); + + if (std::string::npos == delimiter) + { + return ~0; + } + + tc.tm_hour = std::strtoul(strTime.data() + pos, nullptr, 10); + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(':', pos); + + if (std::string::npos == delimiter) + { + return ~0; + } + + tc.tm_min = std::strtoul(strTime.data() + pos, nullptr, 10); + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(' ', pos); + + if (std::string::npos == delimiter) + { + return ~0; + } + + tc.tm_sec = std::strtoul(strTime.data() + pos, nullptr, 10); + + pos = strTime.find_first_not_of(' ', delimiter + 1); + delimiter = strTime.find_first_of(' ', pos); + + if (std::string::npos == delimiter) + { + delimiter = strTime.length(); + } + + if (std::string::npos == pos || delimiter - pos > 5) + { + return ~0; + } + + const std::string zone = strTime.substr(pos, delimiter - pos); + + auto const it_zone = map_zones.find(zone); + + int timezone = 0; + + if (map_zones.cend() != it_zone) + { + timezone = it_zone->second; + } + else if (zone.length() == 5 && ('+' == zone.front() || '-' == zone.front() ) ) + { + std::array hours; + std::array minutes; + + zone.copy(hours.data(), 2, 1); + zone.copy(minutes.data(), 2, 3); + + timezone = std::strtoul(hours.data(), nullptr, 10) * 3600; + timezone += std::strtoul(minutes.data(), nullptr, 10) * 60; + + if ('-' == zone.front() ) + { + timezone *= -1; + } + } + else + { + return ~0; + } + + tc.tm_isdst = -1; + + return std::mktime(&tc) - timezone; + } + + static time_t localToGmt(const time_t timestamp) + { + #ifdef WIN32 + std::tm stm = {}; + + ::gmtime_s(&stm, ×tamp); + + return std::mktime(&stm); + #else + std::tm stm = {}; + + ::gmtime_r(×tamp, &stm); + + return std::mktime(&stm); + #endif + } + + /** + * Convert c-string (__DATE__ " " __TIME__) to std::time_t + */ + time_t predefinedDatetimeToTimestamp(const char *strTime) + { + std::tm tc = {}; + + const char *ptrStr = std::strchr(strTime, ' '); + + if (nullptr == ptrStr) + { + return ~0; + } + + const std::string month(strTime, ptrStr); + + auto const it_mon = map_months.find(month); + + if (map_months.cend() != it_mon) + { + tc.tm_mon = it_mon->second; + } + else + { + return ~0; + } + + ++ptrStr; + + strTime = std::strchr(ptrStr, ' '); + + if (nullptr == strTime) + { + return ~0; + } + + tc.tm_mday = std::strtoul(ptrStr, nullptr, 10); + + ++strTime; + + ptrStr = std::strchr(strTime, ' '); + + if (nullptr == ptrStr) + { + return ~0; + } + + tc.tm_year = std::strtoul(strTime, nullptr, 10) - 1900; + + ++ptrStr; + + strTime = std::strchr(ptrStr, ':'); + + if (nullptr == strTime) + { + return ~0; + } + + tc.tm_hour = std::strtoul(ptrStr, nullptr, 10); + + ++strTime; + + ptrStr = std::strchr(strTime, ':'); + + if (nullptr == ptrStr) + { + return ~0; + } + + tc.tm_min = std::strtoul(strTime, nullptr, 10); + + ++ptrStr; + + tc.tm_sec = std::strtoul(ptrStr, nullptr, 10); + + return localToGmt(std::mktime(&tc) ); + } + + /** + * Convert std::time_t to RFC822 std::string + */ + std::string getDatetimeAsString(time_t tTime, const bool isGmtTime) + { + std::array buf; + + if (tTime == ~0) + { + std::time(&tTime); + } + + #ifdef WIN32 + std::tm stm = {}; + + isGmtTime ? + ::localtime_s(&stm, &tTime) : + ::gmtime_s(&stm, &tTime); + + // RFC 822 + auto const len = std::strftime(buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm); + #else + std::tm stm = {}; + + isGmtTime ? + ::localtime_r(&tTime, &stm) : + ::gmtime_r(&tTime, &stm); + + // RFC 822 + auto const len = std::strftime(buf.data(), buf.size(), "%a, %d %b %G %H:%M:%S GMT", &stm); + #endif + + return std::string(buf.data(), buf.data() + len); + } + + std::string predefinedDatetimeToRfc822(const char *strTime) + { + const std::time_t time = predefinedDatetimeToTimestamp(strTime); + return getDatetimeAsString(time, false); + } + + size_t getNumberLength(size_t number) noexcept + { + size_t length = 0; + + do + { + ++length; + number /= 10; + } + while (number); + + return length; + } + + bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies) + { + if (cookieHeader.empty() ) + { + return true; + } + + for (size_t cur_pos = 0, next_value; std::string::npos != cur_pos; cur_pos = next_value) + { + next_value = cookieHeader.find(';', cur_pos); + + size_t delimiter = cookieHeader.find('=', cur_pos); + + if (std::string::npos == delimiter || delimiter > next_value) + { + return false; + } + + std::string key = cookieHeader.substr(cur_pos, delimiter - cur_pos); + trim(key); + key = urlDecode(key); + + ++delimiter; + + std::string value = cookieHeader.substr(delimiter, std::string::npos != next_value ? next_value - delimiter : next_value); + trim(value); + value = urlDecode(value); + + cookies.emplace(std::move(key), std::move(value) ); + + if (std::string::npos != next_value) + { + ++next_value; + } + } + + return true; + } + + static inline bool isCharUrlAllowed(const char c) noexcept + { + return c == '-' || c == '_' || c == '.' || c == '~'; + } + + std::string urlEncode(const std::string &str) + { + std::string encoded; + + static const std::array hexDigits { "0123456789ABCDEF" }; + + for (size_t i = 0; i < str.length(); ++i) + { + const unsigned char c = str[i]; + + if (std::isalnum(c) || isCharUrlAllowed(c) ) + { + encoded.push_back(c); + } + else if (' ' == c) + { + encoded.push_back('+'); + } + else + { + const uint8_t a = c >> 4; + const uint8_t b = c & 0x0F; + + encoded.push_back('%'); + encoded.push_back(hexDigits[a]); + encoded.push_back(hexDigits[b]); + } + } + + return encoded; + } + + std::string urlDecode(const std::string &str) + { + std::string decoded; + + for (size_t i = 0; i < str.length(); ++i) + { + unsigned char c = str[i]; + + if ('%' == c) + { + if (i + 2 < str.length() ) + { + const char a = str[++i]; + const char b = str[++i]; + + c = ( + (hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b) + ); + } + } + else if ('+' == c) + { + c = ' '; + } + + decoded.push_back(c); + } + + return decoded; + } +}; diff --git a/src/utils/Utils.h b/src/utils/Utils.h new file mode 100644 index 0000000..5137831 --- /dev/null +++ b/src/utils/Utils.h @@ -0,0 +1,137 @@ +#pragma once + +#include +#include +#include + +namespace Utils +{ + void toLower(std::string &str) noexcept; + std::string getLowerString(const std::string &str); + + void trim(std::string &str); + std::string getTrimmedString(const std::string &str); + + std::vector explode(const std::string &str, const char sep); + + std::string encodeHtmlSymbols(const std::string &str); + + std::string binToHexString(const void *bin, const size_t size); + + std::string hexStringToBin(const std::string &hexStr); + + uint64_t hton64(const uint64_t host64) noexcept; + uint64_t ntoh64(const uint64_t net64) noexcept; + + void hton24(void *dest, const uint32_t host32) noexcept; + uint32_t ntoh24(const void *src24) noexcept; + + std::string getUniqueName(); + + size_t getPackNumberSize(const size_t number) noexcept; + size_t getPackStringSize(const std::string &str) noexcept; + + template + size_t getPackContainerSize(const T &container) noexcept + { + size_t full_size = getPackNumberSize(container.size() ); + + for (auto const &pair : container) + { + full_size += getPackStringSize(pair.first); + full_size += getPackStringSize(pair.second); + } + + return full_size; + } + + uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept; + uint8_t *packString(uint8_t *dest, const std::string &str) noexcept; + + template + uint8_t *packContainer(void *dest, const T &container) noexcept + { + uint8_t *addr = reinterpret_cast(dest); + + addr = packNumber(addr, container.size() ); + + for (auto const &pair : container) + { + addr = packString(addr, pair.first); + addr = packString(addr, pair.second); + } + + return addr; + } + + void packNumber(std::vector &buf, const size_t number); + void packString(std::vector &buf, const std::string &str); + + template + void packContainer(std::vector &buf, const T &container) + { + packNumber(buf, container.size() ); + + for (auto const &pair : container) + { + packString(buf, pair.first); + packString(buf, pair.second); + } + } + + const uint8_t *unpackNumber(size_t *number, const uint8_t *src) noexcept; + const uint8_t *unpackString(std::string &str, const uint8_t *src); + + template + const uint8_t *unpackContainer(T &container, const uint8_t *src) + { + size_t count; + src = unpackNumber(&count, src); + + for (size_t i = 0; i < count; ++i) + { + std::string key; + src = unpackString(key, src); + + std::string value; + src = unpackString(value, src); + + container.emplace(std::move(key), std::move(value) ); + } + + return src; + } + + template + const uint8_t *unpackVector(T &vector, const uint8_t *src) + { + size_t count; + src = unpackNumber(&count, src); + + for (size_t i = 0; i < count; ++i) + { + std::string key; + src = unpackString(key, src); + + std::string value; + src = unpackString(value, src); + + vector.emplace_back(std::move(key), std::move(value) ); + } + + return src; + } + + time_t rfc822DatetimeToTimestamp(const std::string &strTime); + time_t predefinedDatetimeToTimestamp(const char *strTime); + + std::string getDatetimeAsString(time_t tTime = ~0, const bool isGmtTime = false); + std::string predefinedDatetimeToRfc822(const char *strTime); + + size_t getNumberLength(size_t number) noexcept; + + bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies); + + std::string urlEncode(const std::string &str); + std::string urlDecode(const std::string &str); +}; From c25275de60ac1e0667f8865b197cb74c702ab6a8 Mon Sep 17 00:00:00 2001 From: Ignat Prokopovich Date: Fri, 31 Mar 2017 17:06:26 +0300 Subject: [PATCH 02/22] Fixed build for Windows platform (after refactor) --- projects/msvs/httpserver.vcxproj | 125 +++++++++------- projects/msvs/httpserver.vcxproj.filters | 173 ++++++++++++++++------- src/SignalHandlers.cpp | 4 +- src/server/protocol/ServerHttp2.cpp | 2 + src/socket/List.cpp | 4 +- src/socket/Socket.cpp | 4 +- src/transfer/http2/Http2.h | 6 +- 7 files changed, 212 insertions(+), 106 deletions(-) diff --git a/projects/msvs/httpserver.vcxproj b/projects/msvs/httpserver.vcxproj index 4808b5f..ce17aba 100644 --- a/projects/msvs/httpserver.vcxproj +++ b/projects/msvs/httpserver.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -19,57 +19,82 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + + + + + + + + + + + + + {A7023B84-DA18-3E3E-A9AE-B87F45CAC37F} @@ -84,7 +109,7 @@ MultiByte Application release\ - v140 + v141 true @@ -93,7 +118,7 @@ MultiByte Application release\ - v140 + v141 true @@ -102,7 +127,7 @@ MultiByte Application debug\ - v140 + v141 debug\ @@ -110,7 +135,7 @@ MultiByte Application debug\ - v140 + v141 diff --git a/projects/msvs/httpserver.vcxproj.filters b/projects/msvs/httpserver.vcxproj.filters index e539bf5..2968c4b 100644 --- a/projects/msvs/httpserver.vcxproj.filters +++ b/projects/msvs/httpserver.vcxproj.filters @@ -19,153 +19,228 @@ - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + + Source Files + + + Source Files + + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + Source Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files - + Header Files diff --git a/src/SignalHandlers.cpp b/src/SignalHandlers.cpp index a187c36..96f2c6c 100644 --- a/src/SignalHandlers.cpp +++ b/src/SignalHandlers.cpp @@ -123,7 +123,7 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const return 0; } -static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event * const eventWindowCreation) noexcept +static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, Utils::Event * const eventWindowCreation) noexcept { const ::HWND hWnd = ::CreateWindow(myWndClassName, nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, hInstance, nullptr); @@ -202,7 +202,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept return false; } - HttpServer::Event eventWindowCreation; + Utils::Event eventWindowCreation; gMainThreadId = ::GetCurrentThreadId(); gThreadMessageLoop = std::thread(mainMessageLoop, hInstance, &eventWindowCreation); diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp index b04fceb..5e25522 100644 --- a/src/server/protocol/ServerHttp2.cpp +++ b/src/server/protocol/ServerHttp2.cpp @@ -3,6 +3,8 @@ #include "../../utils/Utils.h" #include "../../transfer/http2/HPack.h" +#include + namespace HttpServer { ServerHttp2::ServerHttp2(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, SocketsQueue &sockets) noexcept diff --git a/src/socket/List.cpp b/src/socket/List.cpp index 2d07b23..997de8a 100644 --- a/src/socket/List.cpp +++ b/src/socket/List.cpp @@ -268,7 +268,7 @@ namespace Socket struct ::sockaddr_in client_addr = {}; socklen_t client_addr_len = sizeof(client_addr); - client_socket = ::accept(event.fd, static_cast(&client_addr), &client_addr_len); + client_socket = ::accept(event.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); if (~0 != client_socket) { @@ -302,7 +302,7 @@ namespace Socket struct ::sockaddr_in client_addr = {}; socklen_t client_addr_len = sizeof(client_addr); - client_socket = ::accept(event.data.fd, reinterpret_cast(&client_addr), &client_addr_len); + client_socket = ::accept(event.data.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); if (~0 != client_socket) { diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index d9b9043..ad9e744 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -270,7 +270,7 @@ namespace Socket long Socket::recv(void *buf, const size_t length) const noexcept { #ifdef WIN32 - return ::recv(this->socket_handle, buf, static_cast(length), 0); + return ::recv(this->socket_handle, reinterpret_cast(buf), static_cast(length), 0); #elif POSIX return ::recv(this->socket_handle, buf, length, 0); #else @@ -295,7 +295,7 @@ namespace Socket if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { - recv_len = this->recv(this->socket_handle, buf, static_cast(length), 0); + recv_len = ::recv(this->socket_handle, reinterpret_cast(buf), static_cast(length), 0); } #elif POSIX struct ::pollfd event = { diff --git a/src/transfer/http2/Http2.h b/src/transfer/http2/Http2.h index a754b38..c6d02d0 100644 --- a/src/transfer/http2/Http2.h +++ b/src/transfer/http2/Http2.h @@ -9,7 +9,11 @@ #include #include -#include +#ifdef WIN32 + #undef NO_ERROR +#elif POSIX + #include +#endif namespace Http2 { From 34afd975c2c78878c86ac6673ef5667250b8a617 Mon Sep 17 00:00:00 2001 From: awwit Date: Wed, 5 Apr 2017 00:40:37 +0300 Subject: [PATCH 03/22] Fixed bugs in the HTTP/2 protocol --- src/server/protocol/ServerHttp2.cpp | 8 ++--- src/server/protocol/ServerHttp2Protocol.cpp | 39 +++++++++++++++------ src/socket/AdapterTls.cpp | 25 ++++++++++--- src/socket/List.cpp | 4 +-- src/socket/Socket.cpp | 23 ++++++++++++ src/socket/Socket.h | 2 ++ src/system/System.cpp | 12 +++---- src/transfer/http2/Http2.cpp | 28 ++++++++++++--- src/transfer/http2/Http2.h | 12 +++++-- src/utils/Utils.cpp | 39 +++++++++++++++++---- src/utils/Utils.h | 3 ++ 11 files changed, 155 insertions(+), 40 deletions(-) diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp index 5e25522..03813f1 100644 --- a/src/server/protocol/ServerHttp2.cpp +++ b/src/server/protocol/ServerHttp2.cpp @@ -478,16 +478,16 @@ namespace HttpServer static bool getNextHttp2FrameMeta(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, std::vector &buf, Http2::FrameMeta &meta, long &read_size) { - if (read_size <= meta.length + Http2::FRAME_HEADER_SIZE) + if (read_size <= static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) { - if (read_size == meta.length + Http2::FRAME_HEADER_SIZE) + if (read_size == static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) { read_size = 0; } read_size = sock.nonblock_recv(buf.data() + read_size, buf.size() - read_size, timeout); - if (read_size < Http2::FRAME_HEADER_SIZE) + if (read_size < static_cast(Http2::FRAME_HEADER_SIZE) ) { return false; } @@ -496,7 +496,7 @@ namespace HttpServer { std::copy(buf.cbegin() + meta.length + Http2::FRAME_HEADER_SIZE, buf.cbegin() + read_size, buf.begin() ); - read_size -= meta.length + Http2::FRAME_HEADER_SIZE; + read_size -= static_cast(meta.length + Http2::FRAME_HEADER_SIZE); } const uint8_t *addr = reinterpret_cast(buf.data() ); diff --git a/src/server/protocol/ServerHttp2Protocol.cpp b/src/server/protocol/ServerHttp2Protocol.cpp index 00f305c..f85417b 100644 --- a/src/server/protocol/ServerHttp2Protocol.cpp +++ b/src/server/protocol/ServerHttp2Protocol.cpp @@ -38,12 +38,11 @@ namespace HttpServer std::vector buf; buf.reserve(4096); + buf.resize(Http2::FRAME_HEADER_SIZE); HPack::pack(buf, headers, this->stream->conn.encoding_dynamic_table); - const uint32_t frame_size = buf.size(); - - buf.insert(buf.begin(), Http2::FRAME_HEADER_SIZE, 0); + const uint32_t frame_size = buf.size() - Http2::FRAME_HEADER_SIZE; Http2::FrameFlag flags = Http2::FrameFlag::END_HEADERS; @@ -54,7 +53,16 @@ namespace HttpServer this->stream->setHttp2FrameHeader(reinterpret_cast(buf.data() ), frame_size, Http2::FrameType::HEADERS, flags); - return this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; // >= 0 + this->stream->lock(); + + auto const is_sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; // >= 0; + + if (endStream || false == is_sended) + { + this->stream->unlock(); + } + + return is_sended; } long ServerHttp2Protocol::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const @@ -70,8 +78,6 @@ namespace HttpServer while (size != 0) { - buf.resize(0); - // TODO: test with data_size == 1 (padding length == 0) size_t data_size = setting.max_frame_size < size ? setting.max_frame_size : size; @@ -88,6 +94,8 @@ namespace HttpServer const size_t frame_size = data_size + padding_size; + buf.resize(frame_size + Http2::FRAME_HEADER_SIZE); + /* if (this->stream->window_size_out - frame_size <= 0) { size_t update_size = (dt->full_size - dt->send_total) - this->stream->window_size_out; @@ -109,30 +117,33 @@ namespace HttpServer flags |= Http2::FrameFlag::END_STREAM; } + size_t cur = Http2::FRAME_HEADER_SIZE; + if (padding_size) { flags |= Http2::FrameFlag::PADDED; - buf.insert(buf.begin(), sizeof(uint8_t), padding); - } + buf[cur] = padding; - buf.insert(buf.begin(), Http2::FRAME_HEADER_SIZE, 0); + ++cur; + } const Http2::FrameType frame_type = Http2::FrameType::DATA; this->stream->setHttp2FrameHeader(buf.data(), frame_size, frame_type, flags); - std::copy(data, data + data_size, std::back_inserter(buf) ); + std::copy(data, data + data_size, buf.begin() + cur); if (padding) { - buf.insert(buf.end(), padding, 0); + std::fill(buf.end() - padding, buf.end(), 0); } long sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout); if (sended <= 0) { + send_size = sended; break; } @@ -144,6 +155,11 @@ namespace HttpServer size -= data_size; } + if (0 >= send_size || dt->full_size == dt->send_total) + { + this->stream->unlock(); + } + return send_size; } @@ -163,6 +179,7 @@ namespace HttpServer Utils::packNumber(buf, this->stream->conn.client_settings.max_frame_size); Utils::packNumber(buf, this->stream->conn.client_settings.max_header_list_size); Utils::packContainer(buf, this->stream->conn.encoding_dynamic_table.getList() ); + Utils::packPointer(buf, &this->stream->conn.sync.mtx); Utils::packContainer(buf, req.incoming_headers); Utils::packContainer(buf, req.incoming_data); Utils::packFilesIncoming(buf, req.incoming_files); diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index 2c88aec..65de7f5 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -63,25 +63,36 @@ namespace Socket long AdapterTls::nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { - size_t record_size = ::gnutls_record_get_max_size(this->session); + // size_t record_size = ::gnutls_record_get_max_size(this->session); + size_t record_size = length; if (0 == record_size) { return -1; } + Socket sock(this->get_handle() ); + + // ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); + size_t total = 0; while (total < length) { - ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); - if (record_size > length - total) { record_size = length - total; } - const long send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); + // const long send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); + + long send_size = 0; + + do + { + sock.nonblock_send_sync(); + } + while (GNUTLS_E_AGAIN == (send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size) ) ); if (send_size < 0) { @@ -111,7 +122,11 @@ namespace Socket long AdapterTls::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { - ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); + // ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); + + Socket sock(this->get_handle() ); + sock.nonblock_recv_sync(); + return ::gnutls_record_recv(this->session, buf, length); } diff --git a/src/socket/List.cpp b/src/socket/List.cpp index 997de8a..8df0b9a 100644 --- a/src/socket/List.cpp +++ b/src/socket/List.cpp @@ -265,7 +265,7 @@ namespace Socket do { - struct ::sockaddr_in client_addr = {}; + ::sockaddr_in client_addr {}; socklen_t client_addr_len = sizeof(client_addr); client_socket = ::accept(event.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); @@ -299,7 +299,7 @@ namespace Socket do { - struct ::sockaddr_in client_addr = {}; + ::sockaddr_in client_addr {}; socklen_t client_addr_len = sizeof(client_addr); client_socket = ::accept(event.data.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index ad9e744..d2200bb 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -314,6 +314,29 @@ namespace Socket return recv_len; } + void Socket::nonblock_recv_sync() const noexcept + { + #ifdef WIN32 + WSAPOLLFD event = { + this->socket_handle, + POLLRDNORM | POLLRDBAND, + 0 + }; + + ::WSAPoll(&event, 1, ~0); + #elif POSIX + struct ::pollfd event = { + this->socket_handle, + POLLIN, + 0 + }; + + ::poll(&event, 1, ~0); + #else + #error "Undefine platform" + #endif + } + static long send_all(const System::native_socket_type socket_handle, const void *data, const size_t length) noexcept { size_t total = 0; diff --git a/src/socket/Socket.h b/src/socket/Socket.h index 26f40a5..dbefb54 100644 --- a/src/socket/Socket.h +++ b/src/socket/Socket.h @@ -51,6 +51,8 @@ namespace Socket long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + void nonblock_recv_sync() const noexcept; + long send(const std::string &buf) const noexcept; long send(const void *buf, const size_t length) const noexcept; diff --git a/src/system/System.cpp b/src/system/System.cpp index b32f51c..16714d2 100644 --- a/src/system/System.cpp +++ b/src/system/System.cpp @@ -228,7 +228,7 @@ namespace System ::FileTimeToSystemTime(&ftWrite, &stUtc); - struct ::tm tm_time { + std::tm tm_time { stUtc.wSecond, stUtc.wMinute, stUtc.wHour, @@ -240,11 +240,11 @@ namespace System -1 }; - *fileTime = ::mktime(&tm_time); + *fileTime = std::mktime(&tm_time); return true; #elif POSIX - struct ::stat attrib; + struct ::stat attrib {}; if (-1 == ::stat(filePath.c_str(), &attrib) ) { @@ -253,11 +253,11 @@ namespace System *fileSize = attrib.st_size; - struct ::tm clock = {}; + std::tm clock {}; ::gmtime_r(&(attrib.st_mtime), &clock); - *fileTime = ::mktime(&clock); + *fileTime = std::mktime(&clock); return true; #else @@ -284,7 +284,7 @@ namespace System memory_name.erase(memory_name.begin() + pos, memory_name.end() ); } - ::TCHAR buf[MAX_PATH + 1] = {}; + ::TCHAR buf[MAX_PATH + 1] {}; ::GetFullPathName(memory_name.c_str(), MAX_PATH, buf, nullptr); #ifdef UNICODE diff --git a/src/transfer/http2/Http2.cpp b/src/transfer/http2/Http2.cpp index 208eb79..93280ba 100644 --- a/src/transfer/http2/Http2.cpp +++ b/src/transfer/http2/Http2.cpp @@ -144,6 +144,16 @@ namespace Http2 return (addr + Http2::FRAME_HEADER_SIZE); } + void IncStream::lock() + { + this->conn.sync.mtx.lock(); + } + + void IncStream::unlock() noexcept + { + this->conn.sync.mtx.unlock(); + } + void IncStream::close() noexcept { this->incoming_headers.clear(); @@ -157,15 +167,15 @@ namespace Http2 // this->state = StreamState::CLOSED; } - OutStream::OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table) noexcept - : stream_id(streamId), settings(settings), window_size_out(settings.initial_window_size), dynamic_table(std::move(dynamic_table) ) + OutStream::OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table, std::mutex *mtx) noexcept + : stream_id(streamId), settings(settings), window_size_out(settings.initial_window_size), dynamic_table(std::move(dynamic_table) ), mtx(mtx) { } - OutStream::OutStream(const IncStream &stream) noexcept + OutStream::OutStream(const IncStream &stream) : stream_id(stream.stream_id), settings(stream.conn.client_settings), - window_size_out(stream.window_size_out), dynamic_table(stream.conn.encoding_dynamic_table) + window_size_out(stream.window_size_out), dynamic_table(stream.conn.encoding_dynamic_table), mtx(&stream.conn.sync.mtx) { } @@ -179,4 +189,14 @@ namespace Http2 return (addr + Http2::FRAME_HEADER_SIZE); } + + void OutStream::lock() + { + this->mtx->lock(); + } + + void OutStream::unlock() noexcept + { + this->mtx->unlock(); + } }; diff --git a/src/transfer/http2/Http2.h b/src/transfer/http2/Http2.h index c6d02d0..6647fee 100644 --- a/src/transfer/http2/Http2.h +++ b/src/transfer/http2/Http2.h @@ -173,6 +173,9 @@ namespace Http2 uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept; + void lock(); + void unlock() noexcept; + void close() noexcept; }; @@ -186,10 +189,15 @@ namespace Http2 DynamicTable dynamic_table; + std::mutex *mtx; + public: - OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table) noexcept; - OutStream(const IncStream &stream) noexcept; + OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table, std::mutex *mtx) noexcept; + OutStream(const IncStream &stream); uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept; + + void lock(); + void unlock() noexcept; }; }; diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index dc06e23..1c9813a 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -283,6 +283,13 @@ namespace Utils return getPackNumberSize(str.length() ) + str.length(); } + uint8_t *packPointer(uint8_t *dest, void *pointer) noexcept + { + *reinterpret_cast(dest) = pointer; + + return dest + sizeof(void *); + } + uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept { if (number <= 252) @@ -334,6 +341,13 @@ namespace Utils return dest + str.length(); } + void packPointer(std::vector &buf, void *pointer) + { + buf.resize(buf.size() + sizeof(void *) ); + uint8_t *dest = reinterpret_cast(buf.data() + buf.size() - sizeof(void *) ); + *reinterpret_cast(dest) = pointer; + } + void packNumber(std::vector &buf, const size_t number) { if (number <= 252) @@ -376,6 +390,13 @@ namespace Utils } } + const uint8_t *unpackPointer(void **pointer, const uint8_t *src) noexcept + { + *pointer = *reinterpret_cast(const_cast(static_cast(src) ) ); + + return src + sizeof(void *); + } + const uint8_t *unpackNumber(size_t *number, const uint8_t *src) noexcept { *number = *src; @@ -437,7 +458,7 @@ namespace Utils */ time_t rfc822DatetimeToTimestamp(const std::string &strTime) { - std::tm tc = {}; + std::tm tc {}; // Parse RFC 882 (ddd, dd MMM yyyy HH:mm:ss K) @@ -585,13 +606,13 @@ namespace Utils static time_t localToGmt(const time_t timestamp) { #ifdef WIN32 - std::tm stm = {}; + std::tm stm {}; ::gmtime_s(&stm, ×tamp); return std::mktime(&stm); #else - std::tm stm = {}; + std::tm stm {}; ::gmtime_r(×tamp, &stm); @@ -604,7 +625,7 @@ namespace Utils */ time_t predefinedDatetimeToTimestamp(const char *strTime) { - std::tm tc = {}; + std::tm tc {}; const char *ptrStr = std::strchr(strTime, ' '); @@ -628,6 +649,12 @@ namespace Utils ++ptrStr; + // Fix for MS __DATE__ + if (' ' == *ptrStr) + { + ++ptrStr; + } + strTime = std::strchr(ptrStr, ' '); if (nullptr == strTime) @@ -690,7 +717,7 @@ namespace Utils } #ifdef WIN32 - std::tm stm = {}; + std::tm stm {}; isGmtTime ? ::localtime_s(&stm, &tTime) : @@ -699,7 +726,7 @@ namespace Utils // RFC 822 auto const len = std::strftime(buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm); #else - std::tm stm = {}; + std::tm stm {}; isGmtTime ? ::localtime_r(&tTime, &stm) : diff --git a/src/utils/Utils.h b/src/utils/Utils.h index 5137831..47236c8 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -45,6 +45,7 @@ namespace Utils return full_size; } + uint8_t *packPointer(uint8_t *dest, void *pointer) noexcept; uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept; uint8_t *packString(uint8_t *dest, const std::string &str) noexcept; @@ -64,6 +65,7 @@ namespace Utils return addr; } + void packPointer(std::vector &buf, void *pointer); void packNumber(std::vector &buf, const size_t number); void packString(std::vector &buf, const std::string &str); @@ -79,6 +81,7 @@ namespace Utils } } + const uint8_t *unpackPointer(void **pointer, const uint8_t *src) noexcept; const uint8_t *unpackNumber(size_t *number, const uint8_t *src) noexcept; const uint8_t *unpackString(std::string &str, const uint8_t *src); From cdc448fd298e72aa2cba341a65172be738c8688f Mon Sep 17 00:00:00 2001 From: awwit Date: Wed, 5 Apr 2017 16:48:46 +0300 Subject: [PATCH 04/22] Optimization of synchronization when sending HTTP/2 frames --- src/server/protocol/ServerHttp2.cpp | 68 ++++++++++----------- src/server/protocol/ServerHttp2Protocol.cpp | 27 +++----- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp index 03813f1..5b1e56d 100644 --- a/src/server/protocol/ServerHttp2.cpp +++ b/src/server/protocol/ServerHttp2.cpp @@ -40,7 +40,7 @@ namespace HttpServer return streams.emplace(streamId, Http2::IncStream(streamId, conn) ).first->second; } - static void sendWindowUpdate(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::IncStream &stream, const uint32_t size) noexcept + static void sendWindowUpdate(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const Http2::IncStream &stream, const uint32_t size) noexcept { std::array buf; uint8_t *addr = buf.data(); @@ -49,6 +49,8 @@ namespace HttpServer *reinterpret_cast(addr) = ::htonl(size); + const std::unique_lock lock(stream.conn.sync.mtx); + sock.nonblock_send(buf.data(), buf.size(), timeout); } @@ -305,18 +307,22 @@ namespace HttpServer return Http2::ErrorCode::NO_ERROR; } - static void ping(const Socket::Adapter &sock, const struct Request &req, const uint64_t pingData) + static void ping(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const uint64_t pingData) { constexpr uint32_t frame_size = sizeof(uint64_t); std::array buf; uint8_t *addr = buf.data(); - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::PING, Http2::FrameFlag::ACK, 0); + constexpr uint32_t stream_id = 0; + + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::PING, Http2::FrameFlag::ACK, stream_id); *reinterpret_cast(addr) = pingData; - sock.nonblock_send(buf.data(), buf.size(), req.timeout); + const std::unique_lock lock(conn.sync.mtx); + + sock.nonblock_send(buf.data(), buf.size(), timeout); } static Http2::ErrorCode parseHttp2Ping(Http2::FrameMeta &meta) @@ -374,21 +380,23 @@ namespace HttpServer return Http2::ErrorCode::NO_ERROR; } - static void rstStream(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const uint32_t streamId, const Http2::ErrorCode errorCode) + static void rstStream(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::IncStream &stream, const Http2::ErrorCode errorCode) { constexpr uint32_t frame_size = sizeof(uint32_t); std::array buf; uint8_t *addr = buf.data(); - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::RST_STREAM, Http2::FrameFlag::EMPTY, streamId); + addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::RST_STREAM, Http2::FrameFlag::EMPTY, stream.stream_id); *reinterpret_cast(addr) = ::htonl(static_cast(errorCode) ); + const std::unique_lock lock(stream.conn.sync.mtx); + sock.nonblock_send(buf.data(), buf.size(), timeout); } - static void sendSettings(const Socket::Adapter &sock, const struct Request &req, const uint8_t *src, const uint8_t *end) + static void sendSettings(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const uint8_t *src, const uint8_t *end) { const uint32_t frame_size = end - src; @@ -402,10 +410,12 @@ namespace HttpServer std::copy(src, end, addr); - sock.nonblock_send(buf.data(), buf.size(), req.timeout); + const std::unique_lock lock(conn.sync.mtx); + + sock.nonblock_send(buf.data(), buf.size(), timeout); } - static void goAway(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const uint32_t lastStreamId, const Http2::ErrorCode errorCode) + static void goAway(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const uint32_t lastStreamId, const Http2::ErrorCode errorCode) { constexpr uint32_t frame_size = sizeof(uint32_t) * 2; @@ -418,6 +428,8 @@ namespace HttpServer *reinterpret_cast(addr) = ::htonl(static_cast(lastStreamId) ); *reinterpret_cast(addr + sizeof(uint32_t) ) = ::htonl(static_cast(errorCode) ); + const std::unique_lock lock(conn.sync.mtx); + sock.nonblock_send(buf.data(), buf.size(), timeout); } @@ -446,7 +458,7 @@ namespace HttpServer return 0 == compare; } - static void sendEmptySettings(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const Http2::FrameFlag flags) + static void sendEmptySettings(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const Http2::FrameFlag flags) { constexpr uint32_t frame_size = 0; @@ -457,21 +469,7 @@ namespace HttpServer addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::SETTINGS, flags, stream_id); - sock.nonblock_send(buf.data(), buf.size(), timeout); - } - - static void windowUpdate(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout) - { - constexpr uint32_t frame_size = 4; - - std::array buf; - uint8_t *addr = buf.data(); - - constexpr uint32_t stream_id = 0; - - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::WINDOW_UPDATE, Http2::FrameFlag::EMPTY, stream_id); - - *reinterpret_cast(addr) = ::htonl(static_cast(1 << 16) - 1); + const std::unique_lock lock(conn.sync.mtx); sock.nonblock_send(buf.data(), buf.size(), timeout); } @@ -512,22 +510,22 @@ namespace HttpServer ServerProtocol *ServerHttp2::process() { struct Request req; - req.timeout = std::chrono::milliseconds(5000); + req.timeout = std::chrono::milliseconds(15000); req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; - sendEmptySettings(this->sock, req.timeout, Http2::FrameFlag::EMPTY); + Http2::ConnectionData conn; + + sendEmptySettings(this->sock, req.timeout, conn, Http2::FrameFlag::EMPTY); if (false == getClientPreface(this->sock, req.timeout) ) { constexpr uint32_t last_stream_id = 0; - goAway(this->sock, req.timeout, last_stream_id, Http2::ErrorCode::PROTOCOL_ERROR); + goAway(this->sock, req.timeout, conn, last_stream_id, Http2::ErrorCode::PROTOCOL_ERROR); return this; } - Http2::ConnectionData conn; - conn.client_settings = Http2::ConnectionSettings::defaultSettings(); conn.server_settings = Http2::ConnectionSettings::defaultSettings(); @@ -565,7 +563,7 @@ namespace HttpServer if (Http2::StreamState::CLOSED == stream.state) { - rstStream(this->sock, req.timeout, stream.stream_id, Http2::ErrorCode::STREAM_CLOSED); + rstStream(this->sock, req.timeout, stream, Http2::ErrorCode::STREAM_CLOSED); continue; } @@ -646,7 +644,7 @@ namespace HttpServer conn.decoding_dynamic_table.changeHeaderTableSize(conn.client_settings.header_table_size); conn.decoding_dynamic_table.changeMaxHeaderListSize(conn.client_settings.max_header_list_size); - sendEmptySettings(this->sock, req.timeout, Http2::FrameFlag::ACK); + sendEmptySettings(this->sock, req.timeout, conn, Http2::FrameFlag::ACK); } break; @@ -664,7 +662,7 @@ namespace HttpServer { const uint64_t ping_data = *reinterpret_cast(addr); - ping(this->sock, req, ping_data); + ping(this->sock, req.timeout, conn, ping_data); } break; @@ -687,7 +685,7 @@ namespace HttpServer { stream.state = Http2::StreamState::CLOSED; - rstStream(this->sock, req.timeout, meta.stream_id, result); + rstStream(this->sock, req.timeout, stream, result); // TODO: remove closed stream(s) from unordered map } @@ -718,7 +716,7 @@ namespace HttpServer conn.sync.event.wait(); } - goAway(this->sock, req.timeout, last_stream_id, Http2::ErrorCode::NO_ERROR); + goAway(this->sock, req.timeout, conn, last_stream_id, Http2::ErrorCode::NO_ERROR); for (auto &pair : streams) { diff --git a/src/server/protocol/ServerHttp2Protocol.cpp b/src/server/protocol/ServerHttp2Protocol.cpp index f85417b..df18c00 100644 --- a/src/server/protocol/ServerHttp2Protocol.cpp +++ b/src/server/protocol/ServerHttp2Protocol.cpp @@ -53,16 +53,9 @@ namespace HttpServer this->stream->setHttp2FrameHeader(reinterpret_cast(buf.data() ), frame_size, Http2::FrameType::HEADERS, flags); - this->stream->lock(); + const std::unique_lock lock(this->stream->conn.sync.mtx); - auto const is_sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; // >= 0; - - if (endStream || false == is_sended) - { - this->stream->unlock(); - } - - return is_sended; + return this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; // >= 0; } long ServerHttp2Protocol::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const @@ -128,9 +121,7 @@ namespace HttpServer ++cur; } - const Http2::FrameType frame_type = Http2::FrameType::DATA; - - this->stream->setHttp2FrameHeader(buf.data(), frame_size, frame_type, flags); + this->stream->setHttp2FrameHeader(buf.data(), frame_size, Http2::FrameType::DATA, flags); std::copy(data, data + data_size, buf.begin() + cur); @@ -139,7 +130,11 @@ namespace HttpServer std::fill(buf.end() - padding, buf.end(), 0); } - long sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout); + this->stream->lock(); + + const long sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout); + + this->stream->unlock(); if (sended <= 0) { @@ -155,11 +150,6 @@ namespace HttpServer size -= data_size; } - if (0 >= send_size || dt->full_size == dt->send_total) - { - this->stream->unlock(); - } - return send_size; } @@ -192,4 +182,3 @@ namespace HttpServer Utils::unpackContainer(req.outgoing_headers, reinterpret_cast(src) ); } }; - From db3d837d8c8990aab5312d38d01a2d209b417e8a Mon Sep 17 00:00:00 2001 From: awwit Date: Wed, 12 Apr 2017 22:17:39 +0300 Subject: [PATCH 05/22] The path to the application directory is passed to the initialization function --- src/server/Server.cpp | 28 ++++++++-------- src/server/ServerApplicationSettings.h | 4 +-- src/server/ServerSettings.cpp | 5 +-- src/server/config/ConfigParser.cpp | 46 ++++++++++++++------------ src/server/protocol/ServerHttp1.cpp | 7 ++-- 5 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/server/Server.cpp b/src/server/Server.cpp index d46caf0..114c045 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -289,18 +289,19 @@ namespace HttpServer { if (app->application_final) { - app->application_final(); + const std::string root = app->root_dir; + app->application_final(root.data() ); } } catch (std::exception &exc) { - std::cout << "Warning: error when the application finishes '" << app->server_module << "':" << exc.what() << std::endl; + std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl; } app->application_call = std::function(); app->application_clear = std::function(); - app->application_init = std::function(); - app->application_final = std::function(); + app->application_init = std::function(); + app->application_final = std::function(); } } @@ -423,18 +424,18 @@ namespace HttpServer std::function app_clear = reinterpret_cast(addr); - std::function app_init = std::function(); + std::function app_init = std::function(); if (module.find("application_init", &addr) ) { - app_init = reinterpret_cast(addr); + app_init = reinterpret_cast(addr); } - std::function app_final = std::function(); + std::function app_final = std::function(); if (module.find("application_final", &addr) ) { - app_final = reinterpret_cast(addr); + app_final = reinterpret_cast(addr); } for (auto &app : same) @@ -448,12 +449,13 @@ namespace HttpServer { if (app->application_init) { - app->application_init(); + const std::string root = app->root_dir; + app->application_init(root.data() ); } } catch (std::exception &exc) { - std::cout << "Warning: error when initializing the application '" << module_name << "':" << exc.what() << std::endl; + std::cout << "Warning: an exception was thrown when the application '" << module_name << "' was initialized: " << exc.what() << std::endl; } } @@ -502,7 +504,7 @@ namespace HttpServer } } - std::cout << "Notice: applications' modules have been updated;" << std::endl; + std::cout << "Notice: application modules have been updated;" << std::endl; this->controls.setProcess(); this->controls.eventUpdateModule->reset(); @@ -780,7 +782,7 @@ namespace HttpServer sockets_list.addSocket(sock); } - std::cout << "Log: launch server's cycle;" << std::endl << std::endl; + std::cout << "Log: server started work;" << std::endl << std::endl; constexpr size_t queue_max_length = 1024; this->controls.eventNotFullQueue = new Utils::Event(true, true); @@ -855,7 +857,7 @@ namespace HttpServer this->clear(); - std::cout << "Log: complete server's cycle;" << std::endl; + std::cout << "Log: server work completed;" << std::endl; return EXIT_SUCCESS; } diff --git a/src/server/ServerApplicationSettings.h b/src/server/ServerApplicationSettings.h index 7d2cb38..b1bd9d6 100644 --- a/src/server/ServerApplicationSettings.h +++ b/src/server/ServerApplicationSettings.h @@ -31,7 +31,7 @@ namespace HttpServer std::function application_call; std::function application_clear; - std::function application_init; - std::function application_final; + std::function application_init; + std::function application_final; }; }; diff --git a/src/server/ServerSettings.cpp b/src/server/ServerSettings.cpp index 7890fb6..166f2dc 100644 --- a/src/server/ServerSettings.cpp +++ b/src/server/ServerSettings.cpp @@ -38,12 +38,13 @@ namespace HttpServer { if (app->application_final) { - app->application_final(); + const std::string root = app->root_dir; + app->application_final(root.data() ); } } catch (std::exception &exc) { - std::cout << "Warning: the error of the application's finalize '" << app->server_module << "':" << exc.what() << std::endl; + std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl; } delete app; diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index 0767cd8..7ae4490 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -244,18 +244,33 @@ namespace HttpServer std::function app_clear = reinterpret_cast(addr); - std::function app_init = std::function(); + std::function app_init = std::function(); if (module.find("application_init", &addr) ) { - app_init = reinterpret_cast(addr); + app_init = reinterpret_cast(addr); } - std::function app_final = std::function(); + std::function app_final = std::function(); if (module.find("application_final", &addr) ) { - app_final = reinterpret_cast(addr); + app_final = reinterpret_cast(addr); + } + + std::string root_dir = it_root_dir->second; + + #ifdef WIN32 + if ('\\' == root_dir.back() ) + { + root_dir.pop_back(); + } + #endif + + // Remove back slash from root_dir + if ('/' == root_dir.back() ) + { + root_dir.pop_back(); } bool success = true; @@ -264,17 +279,21 @@ namespace HttpServer { if (app_init) { - success = app_init(); + const std::string root = root_dir; + success = app_init(root.data() ); } } - catch (...) + catch (std::exception &exc) { + std::cout << "Warning: an exception was thrown when the application '" << it_module->second << "' was initialized: " << exc.what() << std::endl; + success = false; } if (false == success) { std::cout << "Warning: error when initializing application '" << it_module->second << "';" << std::endl; + return false; } @@ -308,21 +327,6 @@ namespace HttpServer modules.emplace_back(std::move(module) ); } - std::string root_dir = it_root_dir->second; - - #ifdef WIN32 - if ('\\' == root_dir.back() ) - { - root_dir.pop_back(); - } - #endif - - // Remove back slash from root_dir - if ('/' == root_dir.back() ) - { - root_dir.pop_back(); - } - // Create application settings struct ServerApplicationSettings *settings = new ServerApplicationSettings { std::move(ports), diff --git a/src/server/protocol/ServerHttp1.cpp b/src/server/protocol/ServerHttp1.cpp index c8c24c1..290ae9b 100644 --- a/src/server/protocol/ServerHttp1.cpp +++ b/src/server/protocol/ServerHttp1.cpp @@ -54,9 +54,12 @@ namespace HttpServer long ServerHttp1::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const { - long send_size = this->sock.nonblock_send(src, size, timeout); + const long send_size = this->sock.nonblock_send(src, size, timeout); - dt->send_total += send_size; + if (send_size > 0) + { + dt->send_total += send_size; + } return send_size; } From e284a0687019ff04a30edfeb78d35c5d146cf169 Mon Sep 17 00:00:00 2001 From: awwit Date: Sun, 7 May 2017 16:11:33 +0300 Subject: [PATCH 06/22] Added build variant using Makefile --- Makefile | 34 ++++++++++++++++++++++++++++++++++ README.md | 8 ++++++++ 2 files changed, 42 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5ff0f59 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +RM = rm -rf +MKDIR = mkdir -p + +#CXX = g++ +CXXFLAGS += -std=c++14 -c -Wall -O2 +LDFLAGS = -ldl -lpthread -lrt -lgnutls + +DEFS = POSIX +DEFINES = $(patsubst %, -D%, $(DEFS) ) + +SOURCEDIR = ./src +BUILDDIR = ./build +OBJDIR = $(BUILDDIR)/obj + +SOURCES = $(shell find $(SOURCEDIR) -type f -name '*.cpp') +OBJECTS = $(patsubst $(SOURCEDIR)/%.cpp, $(OBJDIR)/%.o, $(SOURCES) ) + +EXECUTABLE = $(BUILDDIR)/httpserver + +.PHONY: all clean +all: $(BUILDDIR) $(EXECUTABLE) + +$(BUILDDIR): + $(MKDIR) $@ + +$(EXECUTABLE): $(OBJECTS) + $(CXX) $(LDFLAGS) $(OBJECTS) -o $@ + +$(OBJECTS) : $(OBJDIR)/%.o : $(SOURCEDIR)/%.cpp + @$(MKDIR) $(dir $@) + $(CXX) $(DEFINES) $(CXXFLAGS) $< -o $@ + +clean: + $(RM) $(OBJDIR) $(EXECUTABLE) diff --git a/README.md b/README.md index 75bcf02..0546a29 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ Windows: `ws2_32.lib`, `libgnutls.dll.a` Linux: +```sh +git clone https://github.com/awwit/httpserver.git +cd httpserver +make +``` + +or + ```sh git clone https://github.com/awwit/httpserver.git cd httpserver From d424a62d5d3ec5b426324e5043c23825d324ec58 Mon Sep 17 00:00:00 2001 From: Ignat Prokopovich Date: Wed, 10 May 2017 00:11:31 +0300 Subject: [PATCH 07/22] Fixed signal processing (and termination) for Windows --- src/SignalHandlers.cpp | 19 ++++++++----------- src/server/Server.cpp | 30 ++++++++++++++++++++++++++++++ src/server/Server.h | 4 ++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/SignalHandlers.cpp b/src/SignalHandlers.cpp index 96f2c6c..a3af7eb 100644 --- a/src/SignalHandlers.cpp +++ b/src/SignalHandlers.cpp @@ -21,7 +21,7 @@ static void handlerSigTerm(const int) noexcept { if (globalServerPtr) { - globalServerPtr->controls.stopProcess(); + globalServerPtr->stop(); } } @@ -32,7 +32,7 @@ static void handlerSigInt(const int) noexcept { if (globalServerPtr) { - globalServerPtr->controls.stopProcess(); + globalServerPtr->stop(); } } @@ -43,8 +43,7 @@ static void handlerSigUsr1(const int) noexcept { if (globalServerPtr) { - globalServerPtr->controls.setRestart(); - globalServerPtr->controls.stopProcess(); + globalServerPtr->restart(); } } @@ -55,9 +54,7 @@ static void handlerSigUsr2(const int) noexcept { if (globalServerPtr) { - globalServerPtr->controls.setUpdateModule(); - globalServerPtr->controls.setProcess(false); - globalServerPtr->controls.setProcessQueue(); + globalServerPtr->update(); } } @@ -108,7 +105,7 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const case WM_ENDSESSION: { - ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId); + const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId); ::WaitForSingleObject(hThread, INFINITE); ::CloseHandle(hThread); break; @@ -158,7 +155,7 @@ static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept case CTRL_SHUTDOWN_EVENT: { handlerSigTerm(ctrlType); - ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId); + const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId); ::WaitForSingleObject(hThread, INFINITE); ::CloseHandle(hThread); return true; @@ -190,7 +187,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept const ::HINSTANCE hInstance = ::GetModuleHandle(nullptr); - ::WNDCLASSEX wcex = {}; + ::WNDCLASSEX wcex {}; wcex.cbSize = sizeof(::WNDCLASSEX); wcex.lpfnWndProc = WndProc; @@ -211,7 +208,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept #elif POSIX - struct ::sigaction act = {}; + struct ::sigaction act {}; act.sa_handler = handlerSigInt; ::sigaction(SIGINT, &act, nullptr); diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 114c045..30846c5 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -1009,6 +1009,36 @@ namespace HttpServer return code; } + static void close_liseners(std::vector &liseners) + { + for (auto &sock : liseners) + { + sock.close(); + } + } + + void Server::stop() + { + this->controls.stopProcess(); + + close_liseners(this->liseners); + } + + void Server::restart() + { + this->controls.setRestart(); + this->controls.stopProcess(); + + close_liseners(this->liseners); + } + + void Server::update() + { + this->controls.setUpdateModule(); + this->controls.setProcess(false); + this->controls.setProcessQueue(); + } + System::native_processid_type Server::getServerProcessId(const std::string &serverName) { System::native_processid_type pid = 0; diff --git a/src/server/Server.h b/src/server/Server.h index 4218ad8..e09984a 100644 --- a/src/server/Server.h +++ b/src/server/Server.h @@ -53,6 +53,10 @@ namespace HttpServer public: Server() = default; + void stop(); + void restart(); + void update(); + int command_help(const int argc, const char *argv[]) const; int command_start(const int argc, const char *argv[]); int command_restart(const int argc, const char *argv[]) const; From f5f0cb9e97e8cb9cd520fae40e97182945dcecf6 Mon Sep 17 00:00:00 2001 From: awwit Date: Wed, 2 Aug 2017 15:36:52 +0300 Subject: [PATCH 08/22] Fixed bug in function Utils::explode --- src/utils/Utils.cpp | 6 +++--- src/utils/Utils.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index 1c9813a..9ef440e 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -55,9 +55,9 @@ namespace Utils for (size_t pos = 0; std::string::npos != pos;) { - size_t delimiter = str.find(sep, pos); + const size_t delimiter = str.find(sep, pos); - std::string value = str.substr(pos, delimiter); + std::string value = str.substr(pos, delimiter - pos); trim(value); values.emplace_back(std::move(value) ); @@ -865,4 +865,4 @@ namespace Utils return decoded; } -}; +} diff --git a/src/utils/Utils.h b/src/utils/Utils.h index 47236c8..9dce37a 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -137,4 +137,4 @@ namespace Utils std::string urlEncode(const std::string &str); std::string urlDecode(const std::string &str); -}; +} From 528961805fe8e71d3c5d9269467dfbb61300091f Mon Sep 17 00:00:00 2001 From: awwit Date: Sat, 11 Nov 2017 19:29:09 +0300 Subject: [PATCH 09/22] Fixed bug in HTTPS: interrupt receiving data. Code styling --- src/SignalHandlers.cpp | 60 +++----- src/socket/Adapter.cpp | 14 +- src/socket/Adapter.h | 2 +- src/socket/AdapterDefault.cpp | 25 ++-- src/socket/AdapterDefault.h | 2 +- src/socket/AdapterTls.cpp | 54 +++---- src/socket/AdapterTls.h | 2 +- src/socket/Socket.cpp | 118 +++++---------- src/socket/Socket.h | 10 +- src/system/System.cpp | 53 +++---- src/system/System.h | 2 +- src/transfer/AppRequest.h | 2 +- src/transfer/AppResponse.h | 2 +- src/transfer/FileIncoming.cpp | 28 ++-- src/transfer/FileIncoming.h | 4 +- src/transfer/HttpStatusCode.h | 2 +- src/transfer/ProtocolVariant.h | 2 +- src/transfer/http2/HPack.cpp | 220 +++++++++------------------- src/transfer/http2/HPack.h | 2 +- src/transfer/http2/Http2.cpp | 44 ++---- src/transfer/http2/Http2.h | 2 +- src/utils/Event.cpp | 44 ++---- src/utils/Event.h | 2 +- src/utils/Utils.cpp | 256 ++++++++++----------------------- src/utils/Utils.h | 15 +- 25 files changed, 319 insertions(+), 648 deletions(-) diff --git a/src/SignalHandlers.cpp b/src/SignalHandlers.cpp index a3af7eb..80bbc7d 100644 --- a/src/SignalHandlers.cpp +++ b/src/SignalHandlers.cpp @@ -17,10 +17,8 @@ static HttpServer::Server *globalServerPtr = nullptr; /** * Terminate signal */ -static void handlerSigTerm(const int) noexcept -{ - if (globalServerPtr) - { +static void handlerSigTerm(const int) noexcept { + if (globalServerPtr) { globalServerPtr->stop(); } } @@ -28,10 +26,8 @@ static void handlerSigTerm(const int) noexcept /** * Interrupt signal */ -static void handlerSigInt(const int) noexcept -{ - if (globalServerPtr) - { +static void handlerSigInt(const int) noexcept { + if (globalServerPtr) { globalServerPtr->stop(); } } @@ -39,10 +35,8 @@ static void handlerSigInt(const int) noexcept /** * Signal to restart */ -static void handlerSigUsr1(const int) noexcept -{ - if (globalServerPtr) - { +static void handlerSigUsr1(const int) noexcept { + if (globalServerPtr) { globalServerPtr->restart(); } } @@ -50,10 +44,8 @@ static void handlerSigUsr1(const int) noexcept /** * Signal to update modules */ -static void handlerSigUsr2(const int) noexcept -{ - if (globalServerPtr) - { +static void handlerSigUsr2(const int) noexcept { + if (globalServerPtr) { globalServerPtr->update(); } } @@ -68,51 +60,44 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const { switch (message) { - case SIGTERM: - { + case SIGTERM: { handlerSigTerm(message); ::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0); break; } - case SIGINT: - { + case SIGINT: { handlerSigInt(message); ::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0); break; } - case SIGUSR1: - { + case SIGUSR1: { handlerSigUsr1(message); break; } - case SIGUSR2: - { + case SIGUSR2: { handlerSigUsr2(message); break; } // Cases WM_QUERYENDSESSION and WM_ENDSESSION run before shutting down the system (or ending user session) - case WM_QUERYENDSESSION: - { + case WM_QUERYENDSESSION: { handlerSigTerm(message); break; } - case WM_ENDSESSION: - { + case WM_ENDSESSION: { const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId); ::WaitForSingleObject(hThread, INFINITE); ::CloseHandle(hThread); break; } - default: - { + default: { return ::DefWindowProc(hWnd, message, wParam, lParam); } } @@ -126,15 +111,13 @@ static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, Utils::Event * cons eventWindowCreation->notify(); // After this action, eventWindowCreation will be destroyed (in the other thread) - if (0 == hWnd) - { + if (0 == hWnd) { return 0; } ::MSG msg; - while (::GetMessage(&msg, hWnd, 0, 0) ) - { + while (::GetMessage(&msg, hWnd, 0, 0) ) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } @@ -152,8 +135,7 @@ static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept // @see my function WndProc -> cases WM_QUERYENDSESSION and WM_ENDSESSION. Only they happen in this program, because the library user32.dll is connected. // @prooflink: https://msdn.microsoft.com/library/windows/desktop/ms686016(v=vs.85).aspx case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - { + case CTRL_SHUTDOWN_EVENT: { handlerSigTerm(ctrlType); const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId); ::WaitForSingleObject(hThread, INFINITE); @@ -161,9 +143,10 @@ static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept return true; } - case CTRL_C_EVENT: + case CTRL_C_EVENT: { handlerSigInt(ctrlType); return true; + } default: return false; @@ -194,8 +177,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept wcex.hInstance = hInstance; wcex.lpszClassName = myWndClassName; - if (0 == ::RegisterClassEx(&wcex) ) - { + if (::RegisterClassEx(&wcex) == 0) { return false; } diff --git a/src/socket/Adapter.cpp b/src/socket/Adapter.cpp index d0524b0..0c50bc3 100644 --- a/src/socket/Adapter.cpp +++ b/src/socket/Adapter.cpp @@ -3,23 +3,19 @@ namespace Socket { - long Adapter::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept - { + long Adapter::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept { return this->nonblock_recv(buf.data(), buf.size(), timeout); } - long Adapter::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept - { + long Adapter::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept { return this->nonblock_send(buf.data(), buf.length(), timeout); } - bool Adapter::operator ==(const Adapter &obj) const noexcept - { + bool Adapter::operator ==(const Adapter &obj) const noexcept { return this->get_handle() == obj.get_handle(); } - bool Adapter::operator !=(const Adapter &obj) const noexcept - { + bool Adapter::operator !=(const Adapter &obj) const noexcept { return this->get_handle() != obj.get_handle(); } -}; +} diff --git a/src/socket/Adapter.h b/src/socket/Adapter.h index 554714f..35b3964 100644 --- a/src/socket/Adapter.h +++ b/src/socket/Adapter.h @@ -30,4 +30,4 @@ namespace Socket bool operator ==(const Adapter &obj) const noexcept; bool operator !=(const Adapter &obj) const noexcept; }; -}; +} diff --git a/src/socket/AdapterDefault.cpp b/src/socket/AdapterDefault.cpp index d71d6a2..1f2fe36 100644 --- a/src/socket/AdapterDefault.cpp +++ b/src/socket/AdapterDefault.cpp @@ -3,42 +3,33 @@ namespace Socket { - AdapterDefault::AdapterDefault(const Socket &_sock) noexcept : sock(_sock) - { + AdapterDefault::AdapterDefault(const Socket &_sock) noexcept : sock(_sock) {} - } - - System::native_socket_type AdapterDefault::get_handle() const noexcept - { + System::native_socket_type AdapterDefault::get_handle() const noexcept { return sock.get_handle(); } - ::gnutls_session_t AdapterDefault::get_tls_session() const noexcept - { + ::gnutls_session_t AdapterDefault::get_tls_session() const noexcept { return 0; } - Adapter *AdapterDefault::copy() const noexcept - { + Adapter *AdapterDefault::copy() const noexcept { return new AdapterDefault(this->sock); } - long AdapterDefault::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { + long AdapterDefault::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { return sock.nonblock_recv(buf, length, timeout); } - long AdapterDefault::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { + long AdapterDefault::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { return sock.nonblock_send(buf, length, timeout); } - void AdapterDefault::close() noexcept - { + void AdapterDefault::close() noexcept { // Wait for send all data to client sock.nonblock_send_sync(); sock.shutdown(); sock.close(); } -}; +} diff --git a/src/socket/AdapterDefault.h b/src/socket/AdapterDefault.h index 0008fa9..d743783 100644 --- a/src/socket/AdapterDefault.h +++ b/src/socket/AdapterDefault.h @@ -23,4 +23,4 @@ namespace Socket virtual void close() noexcept override; }; -}; +} diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index 65de7f5..afd4ca2 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -33,23 +33,18 @@ namespace Socket ::gnutls_alpn_set_protocols(this->session, protocols, sizeof(protocols) / sizeof(::gnutls_datum_t), 0); } - AdapterTls::AdapterTls(const ::gnutls_session_t session) noexcept : session(session) - { - - } + AdapterTls::AdapterTls(const ::gnutls_session_t session) noexcept : session(session) {} bool AdapterTls::handshake() noexcept { int ret; - do - { + do { ret = ::gnutls_handshake(this->session); } while (ret < 0 && ::gnutls_error_is_fatal(ret) == 0); - if (ret < 0) - { + if (ret < 0) { Socket sock(this->get_handle() ); sock.close(); @@ -66,8 +61,7 @@ namespace Socket // size_t record_size = ::gnutls_record_get_max_size(this->session); size_t record_size = length; - if (0 == record_size) - { + if (0 == record_size) { return -1; } @@ -77,10 +71,8 @@ namespace Socket size_t total = 0; - while (total < length) - { - if (record_size > length - total) - { + while (total < length) { + if (record_size > length - total) { record_size = length - total; } @@ -88,14 +80,12 @@ namespace Socket long send_size = 0; - do - { + do { sock.nonblock_send_sync(); } while (GNUTLS_E_AGAIN == (send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size) ) ); - if (send_size < 0) - { + if (send_size < 0) { return send_size; } @@ -105,18 +95,15 @@ namespace Socket return static_cast(total); } - System::native_socket_type AdapterTls::get_handle() const noexcept - { + System::native_socket_type AdapterTls::get_handle() const noexcept { return static_cast(::gnutls_transport_get_int(this->session) ); } - ::gnutls_session_t AdapterTls::get_tls_session() const noexcept - { + ::gnutls_session_t AdapterTls::get_tls_session() const noexcept { return this->session; } - Adapter *AdapterTls::copy() const noexcept - { + Adapter *AdapterTls::copy() const noexcept { return new AdapterTls(this->session); } @@ -125,18 +112,23 @@ namespace Socket // ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); Socket sock(this->get_handle() ); - sock.nonblock_recv_sync(); - return ::gnutls_record_recv(this->session, buf, length); + long result; + + do { + sock.nonblock_recv_sync(); + result = ::gnutls_record_recv(this->session, buf, length); + } + while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED); + + return result; } - long AdapterTls::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { + long AdapterTls::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { return this->nonblock_send_all(buf, length, timeout); } - void AdapterTls::close() noexcept - { + void AdapterTls::close() noexcept { Socket sock(this->get_handle() ); // Wait for send all data to client @@ -148,4 +140,4 @@ namespace Socket ::gnutls_deinit(this->session); } -}; +} diff --git a/src/socket/AdapterTls.h b/src/socket/AdapterTls.h index 5751b7e..27bad11 100644 --- a/src/socket/AdapterTls.h +++ b/src/socket/AdapterTls.h @@ -27,4 +27,4 @@ namespace Socket virtual void close() noexcept override; }; -}; +} diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index d2200bb..f787e31 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -48,32 +48,19 @@ namespace Socket #endif } - Socket::Socket() noexcept : socket_handle(~0) - { - - } + Socket::Socket() noexcept : socket_handle(~0) {} - Socket::Socket(const System::native_socket_type fd) noexcept : socket_handle(fd) - { - - } + Socket::Socket(const System::native_socket_type fd) noexcept : socket_handle(fd) {} - Socket::Socket(const Socket &obj) noexcept : socket_handle(obj.socket_handle) - { - - } + Socket::Socket(const Socket &obj) noexcept : socket_handle(obj.socket_handle) {} - Socket::Socket(Socket &&obj) noexcept : socket_handle(obj.socket_handle) - { + Socket::Socket(Socket &&obj) noexcept : socket_handle(obj.socket_handle) { obj.socket_handle = ~0; } - bool Socket::open() noexcept - { + bool Socket::open() noexcept { this->close(); - this->socket_handle = ::socket(AF_INET, SOCK_STREAM, 0); - return this->is_open(); } @@ -89,10 +76,8 @@ namespace Socket #error "Undefine platform" #endif - if (0 == result) - { + if (0 == result) { this->socket_handle = ~0; - return true; } } @@ -111,8 +96,7 @@ namespace Socket #endif } - System::native_socket_type Socket::get_handle() const noexcept - { + System::native_socket_type Socket::get_handle() const noexcept { return this->socket_handle; } @@ -128,8 +112,7 @@ namespace Socket return 0 == ::bind(this->socket_handle, reinterpret_cast(&sock_addr), sizeof(sockaddr_in) ); } - bool Socket::listen() const noexcept - { + bool Socket::listen() const noexcept { return 0 == ::listen(this->socket_handle, SOMAXCONN); } @@ -155,8 +138,7 @@ namespace Socket 0 }; - if (1 == ::WSAPoll(&event, 1, ~0) && event.revents & POLLRDNORM) - { + if (1 == ::WSAPoll(&event, 1, ~0) && event.revents & POLLRDNORM) { client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); } #elif POSIX @@ -166,8 +148,7 @@ namespace Socket 0 }; - if (1 == ::poll(&event, 1, ~0) && event.revents & POLLIN) - { + if (1 == ::poll(&event, 1, ~0) && event.revents & POLLIN) { client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); } #else @@ -186,8 +167,7 @@ namespace Socket 0 }; - if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) - { + if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); } #elif POSIX @@ -197,8 +177,7 @@ namespace Socket 0 }; - if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) - { + if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) { client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); } #else @@ -262,8 +241,7 @@ namespace Socket #endif } - long Socket::recv(std::vector &buf) const noexcept - { + long Socket::recv(std::vector &buf) const noexcept { return this->recv(buf.data(), buf.size() ); } @@ -278,8 +256,7 @@ namespace Socket #endif } - long Socket::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept - { + long Socket::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept { return this->nonblock_recv(buf.data(), buf.size(), timeout); } @@ -293,8 +270,7 @@ namespace Socket 0 }; - if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) - { + if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { recv_len = ::recv(this->socket_handle, reinterpret_cast(buf), static_cast(length), 0); } #elif POSIX @@ -304,8 +280,7 @@ namespace Socket 0 }; - if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) - { + if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) { recv_len = ::recv(this->socket_handle, buf, length, 0); } #else @@ -319,7 +294,7 @@ namespace Socket #ifdef WIN32 WSAPOLLFD event = { this->socket_handle, - POLLRDNORM | POLLRDBAND, + POLLIN, 0 }; @@ -341,12 +316,10 @@ namespace Socket { size_t total = 0; - while (total < length) - { + while (total < length) { const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, length - total, 0); - if (send_size < 0) - { + if (send_size < 0) { return send_size; } @@ -356,13 +329,11 @@ namespace Socket return static_cast(total); } - long Socket::send(const std::string &buf) const noexcept - { + long Socket::send(const std::string &buf) const noexcept { return this->send(buf.data(), buf.length() ); } - long Socket::send(const void *buf, const size_t length) const noexcept - { + long Socket::send(const void *buf, const size_t length) const noexcept { return send_all(this->socket_handle, buf, length); } @@ -373,25 +344,20 @@ namespace Socket #ifdef WIN32 WSAPOLLFD event = { socket_handle, - POLLWRNORM, + POLLOUT, 0 }; - while (total < length) - { - if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLWRNORM) - { + while (total < length) { + if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLOUT) { const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, static_cast(length - total), 0); - if (send_size < 0) - { + if (send_size < 0) { return send_size; } total += send_size; - } - else - { + } else { return -1; } } @@ -403,21 +369,16 @@ namespace Socket 0 }; - while (total < length) - { - if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLOUT) - { + while (total < length) { + if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLOUT) { const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, length - total, 0); - if (send_size < 0) - { + if (send_size < 0) { return send_size; } total += send_size; - } - else - { + } else { return -1; } } @@ -428,13 +389,11 @@ namespace Socket return static_cast(total); } - long Socket::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept - { + long Socket::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept { return this->nonblock_send(buf.data(), buf.length(), timeout); } - long Socket::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { + long Socket::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { return nonblock_send_all(this->socket_handle, buf, length, timeout); } @@ -443,7 +402,7 @@ namespace Socket #ifdef WIN32 WSAPOLLFD event = { this->socket_handle, - POLLWRNORM, + POLLOUT, 0 }; @@ -461,19 +420,16 @@ namespace Socket #endif } - Socket &Socket::operator=(const Socket &obj) noexcept - { + Socket &Socket::operator=(const Socket &obj) noexcept { this->socket_handle = obj.socket_handle; return *this; } - bool Socket::operator ==(const Socket &obj) const noexcept - { + bool Socket::operator ==(const Socket &obj) const noexcept { return this->socket_handle == obj.socket_handle; } - bool Socket::operator !=(const Socket &obj) const noexcept - { + bool Socket::operator !=(const Socket &obj) const noexcept { return this->socket_handle != obj.socket_handle; } -}; +} diff --git a/src/socket/Socket.h b/src/socket/Socket.h index dbefb54..0952e23 100644 --- a/src/socket/Socket.h +++ b/src/socket/Socket.h @@ -66,16 +66,14 @@ namespace Socket bool operator ==(const Socket &obj) const noexcept; bool operator !=(const Socket &obj) const noexcept; }; -}; +} namespace std { // Hash for Socket - template<> struct hash - { - std::size_t operator()(const Socket::Socket &obj) const noexcept - { + template<> struct hash { + std::size_t operator()(const Socket::Socket &obj) const noexcept { return std::hash{}(obj.get_handle() ); } }; -}; +} diff --git a/src/system/System.cpp b/src/system/System.cpp index 16714d2..9dcaeb7 100644 --- a/src/system/System.cpp +++ b/src/system/System.cpp @@ -40,10 +40,8 @@ namespace System ::GetClassName(hWnd, class_name.data(), static_cast(class_name.size() - 1) ); - if (0 == ::_tcscmp(class_name.data(), myWndClassName) ) - { + if (0 == ::_tcscmp(class_name.data(), myWndClassName) ) { ed.hWnd = hWnd; - return false; } } @@ -100,8 +98,7 @@ namespace System ::EnumWindows(EnumProc, reinterpret_cast<::LPARAM>(&ed) ); - if (0 == ed.hWnd) - { + if (0 == ed.hWnd) { return false; } @@ -140,15 +137,13 @@ namespace System #elif POSIX const char *buf = ::getenv("TMPDIR"); - if (nullptr == buf) - { + if (nullptr == buf) { return std::string("/tmp/"); } std::string str(buf); - if ('/' != str.back() ) - { + if (str.back() != '/') { str.push_back('/'); } @@ -170,8 +165,7 @@ namespace System const ::DWORD attrib = ::GetFileAttributes(file_name.c_str() ); - if (INVALID_FILE_ATTRIBUTES == attrib) - { + if (INVALID_FILE_ATTRIBUTES == attrib) { return false; } @@ -179,8 +173,7 @@ namespace System #elif POSIX struct ::stat attrib; - if (-1 == ::stat(fileName.c_str(), &attrib) ) - { + if (-1 == ::stat(fileName.c_str(), &attrib) ) { return false; } @@ -202,13 +195,11 @@ namespace System const ::HANDLE hFile = ::CreateFile(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); - if (INVALID_HANDLE_VALUE == hFile) - { + if (INVALID_HANDLE_VALUE == hFile) { return false; } - if (false == ::GetFileSizeEx(hFile, reinterpret_cast<::PLARGE_INTEGER>(fileSize) ) ) - { + if (::GetFileSizeEx(hFile, reinterpret_cast<::PLARGE_INTEGER>(fileSize) ) == false) { ::CloseHandle(hFile); return false; } @@ -219,8 +210,7 @@ namespace System ::CloseHandle(hFile); - if (false == result) - { + if (false == result) { return false; } @@ -246,15 +236,14 @@ namespace System #elif POSIX struct ::stat attrib {}; - if (-1 == ::stat(filePath.c_str(), &attrib) ) - { + if (-1 == ::stat(filePath.c_str(), &attrib) ) { return false; } *fileSize = attrib.st_size; std::tm clock {}; - + ::gmtime_r(&(attrib.st_mtime), &clock); *fileTime = std::mktime(&clock); @@ -279,8 +268,7 @@ namespace System const size_t pos = memory_name.rfind(file_ext); - if (pos == memory_name.length() - file_ext.length() ) - { + if (pos == memory_name.length() - file_ext.length() ) { memory_name.erase(memory_name.begin() + pos, memory_name.end() ); } @@ -293,23 +281,18 @@ namespace System memName = buf; #endif - for (size_t i = 1; i < memName.length(); ++i) - { - if ('/' == memName[i] || '\\' == memName[i]) - { + for (size_t i = 1; i < memName.length(); ++i) { + if ('/' == memName[i] || '\\' == memName[i]) { memName[i] = '-'; } } #elif POSIX - if ('/' != memName.front() ) - { + if (memName.front() != '/') { memName = '/' + memName; } - for (size_t i = 1; i < memName.length(); ++i) - { - if ('/' == memName[i] || '\\' == memName[i]) - { + for (size_t i = 1; i < memName.length(); ++i) { + if ('/' == memName[i] || '\\' == memName[i]) { memName[i] = '-'; } } @@ -317,4 +300,4 @@ namespace System #error "Undefine platform" #endif } -}; +} diff --git a/src/system/System.h b/src/system/System.h index 1ce46d2..3b5b1bc 100644 --- a/src/system/System.h +++ b/src/system/System.h @@ -69,4 +69,4 @@ namespace System bool getFileSizeAndTimeGmt(const std::string &filePath, size_t *fileSize, time_t *fileTime); void filterSharedMemoryName(std::string &memName); -}; +} diff --git a/src/transfer/AppRequest.h b/src/transfer/AppRequest.h index 5676b34..b345a51 100644 --- a/src/transfer/AppRequest.h +++ b/src/transfer/AppRequest.h @@ -20,4 +20,4 @@ namespace Transfer const ::gnutls_session_t tls_session; const void * const request_data; }; -}; +} diff --git a/src/transfer/AppResponse.h b/src/transfer/AppResponse.h index 80937dd..51ff300 100644 --- a/src/transfer/AppResponse.h +++ b/src/transfer/AppResponse.h @@ -10,4 +10,4 @@ namespace Transfer void *response_data; size_t data_size; }; -}; +} diff --git a/src/transfer/FileIncoming.cpp b/src/transfer/FileIncoming.cpp index 58188fc..ed2c49f 100644 --- a/src/transfer/FileIncoming.cpp +++ b/src/transfer/FileIncoming.cpp @@ -27,37 +27,29 @@ namespace Transfer obj.file_size = 0; } - const std::string &FileIncoming::getTmpName() const noexcept - { + const std::string &FileIncoming::getTmpName() const noexcept { return this->file_tmp_name; } - const std::string &FileIncoming::getName() const noexcept - { + const std::string &FileIncoming::getName() const noexcept { return this->file_name; } - const std::string &FileIncoming::getType() const noexcept - { + const std::string &FileIncoming::getType() const noexcept { return this->file_type; } - size_t FileIncoming::getSize() const noexcept - { + size_t FileIncoming::getSize() const noexcept { return this->file_size; } - bool FileIncoming::isExists() const noexcept - { + bool FileIncoming::isExists() const noexcept { std::ifstream file(this->file_tmp_name, std::ifstream::binary); - const bool is_exists = file.good(); - file.close(); - return is_exists; } -}; +} namespace Utils { @@ -65,8 +57,7 @@ namespace Utils { packNumber(buf, map.size() ); - for (auto it = map.cbegin(); map.cend() != it; ++it) - { + for (auto it = map.cbegin(); map.cend() != it; ++it) { packString(buf, it->first); const Transfer::FileIncoming &file = it->second; @@ -83,8 +74,7 @@ namespace Utils size_t count; src = unpackNumber(&count, src); - for (size_t i = 0; i < count; ++i) - { + for (size_t i = 0; i < count; ++i) { std::string key; src = unpackString(key, src); @@ -105,4 +95,4 @@ namespace Utils return src; } -}; +} diff --git a/src/transfer/FileIncoming.h b/src/transfer/FileIncoming.h index 0d853eb..2568b73 100644 --- a/src/transfer/FileIncoming.h +++ b/src/transfer/FileIncoming.h @@ -31,10 +31,10 @@ namespace Transfer bool isExists() const noexcept; }; -}; +} namespace Utils { void packFilesIncoming(std::vector &buf, const std::unordered_multimap &map); const uint8_t *unpackFilesIncoming(std::unordered_multimap &map, const uint8_t *src); -}; +} diff --git a/src/transfer/HttpStatusCode.h b/src/transfer/HttpStatusCode.h index 169326a..a560814 100644 --- a/src/transfer/HttpStatusCode.h +++ b/src/transfer/HttpStatusCode.h @@ -49,4 +49,4 @@ namespace Http GATEWAY_TIMEOUT = 504, HTTP_VERSION_NOT_SUPPORTED = 505, }; -}; +} diff --git a/src/transfer/ProtocolVariant.h b/src/transfer/ProtocolVariant.h index 11954bf..fca95ef 100644 --- a/src/transfer/ProtocolVariant.h +++ b/src/transfer/ProtocolVariant.h @@ -8,4 +8,4 @@ namespace Transfer HTTP_1, HTTP_2 }; -}; +} diff --git a/src/transfer/http2/HPack.cpp b/src/transfer/http2/HPack.cpp index 60889d2..7075b2f 100644 --- a/src/transfer/http2/HPack.cpp +++ b/src/transfer/http2/HPack.cpp @@ -5214,8 +5214,7 @@ namespace HPack {"www-authenticate", ""}, }; - static constexpr size_t getStaticTableSize() noexcept - { + static constexpr size_t getStaticTableSize() noexcept { return sizeof(staticTable) / sizeof(*staticTable); } @@ -5225,8 +5224,7 @@ namespace HPack size_t n = 0; - for (size_t i = 0; i < length; ++i) - { + for (size_t i = 0; i < length; ++i) { n += huffmanSymbolTable[data[i] ].nbits; } @@ -5237,10 +5235,8 @@ namespace HPack { uint8_t nbits = sym.nbits; - for (;;) - { - if (rembits > nbits) - { + for (;;) { + if (rembits > nbits) { dest.back() |= uint8_t(sym.code << (rembits - nbits) ); rembits -= nbits; break; @@ -5251,8 +5247,7 @@ namespace HPack nbits -= rembits; rembits = 8; - if (0 == nbits) - { + if (0 == nbits) { break; } @@ -5268,22 +5263,18 @@ namespace HPack uint8_t rembits = 8; - for (size_t i = 0; i < srcSize; ++i) - { + for (size_t i = 0; i < srcSize; ++i) { const HuffmanSymbol &sym = huffmanSymbolTable[data[i] ]; - if (8 == rembits) - { + if (8 == rembits) { dest.emplace_back(0); } rembits = huffmanEncodeSymbol(dest, rembits, sym); } - if (rembits < 8) - { + if (rembits < 8) { const HuffmanSymbol &sym = huffmanSymbolTable[256]; - dest.back() |= uint8_t(sym.code >> (sym.nbits - rembits) ); } } @@ -5312,13 +5303,11 @@ namespace HPack { const HuffmanDecodeNode &node = huffmanDecodeTable[state][x]; - if (node.flags & HuffmanDecode::FAIL) - { + if (node.flags & HuffmanDecode::FAIL) { return false; } - if (node.flags & HuffmanDecode::SYMBOL) - { + if (node.flags & HuffmanDecode::SYMBOL) { dest.emplace_back(node.symbol); } @@ -5336,8 +5325,7 @@ namespace HPack { uint64_t k = (1 << prefix) - 1; - if (num < k) - { + if (num < k) { dest.emplace_back(num); return; } @@ -5346,10 +5334,8 @@ namespace HPack num -= k; - for (;;) - { - if (num < 128) - { + for (;;) { + if (num < 128) { dest.emplace_back(num); break; } @@ -5358,19 +5344,15 @@ namespace HPack num >>= 7; - if (0 == num) - { + if (0 == num) { break; } } } - static void packIndex(std::vector &dest, const size_t index) - { + static void packIndex(std::vector &dest, const size_t index) { const size_t head = dest.size(); - packInteger(dest, index, 7); - dest[head] |= 0x80; } @@ -5383,39 +5365,30 @@ namespace HPack size_t &index = std::get(result); bool &is_full_match = std::get(result); - for (size_t i = 0; i < getStaticTableSize(); ++i) - { + for (size_t i = 0; i < getStaticTableSize(); ++i) { auto const &pair = staticTable[i]; - if (pair.first == header.first) - { + if (pair.first == header.first) { index = i + 1; - if (pair.second == header.second) - { + if (pair.second == header.second) { is_full_match = true; - return result; } } - else if (0 != index) - { + else if (0 != index) { break; } } - for (size_t i = 0; i < dynamicTable.size(); ++i) - { + for (size_t i = 0; i < dynamicTable.size(); ++i) { auto const &pair = dynamicTable[i]; - if (pair.first == header.first) - { + if (pair.first == header.first) { index = i + getStaticTableSize() + 1; - if (pair.second == header.second) - { + if (pair.second == header.second) { is_full_match = true; - break; } } @@ -5426,13 +5399,11 @@ namespace HPack static uint8_t packFirstByte(const bool indexing) noexcept { - if (indexing) - { + if (indexing) { return 0x40; } - /* if (never_indexing) - { + /* if (never_indexing) { return 0x10; }*/ @@ -5443,17 +5414,14 @@ namespace HPack { const size_t huffman_length = huffmanEncodeLength(str.data(), str.length() ); - if (huffman_length < str.length() ) - { + if (huffman_length < str.length() ) { const size_t head = dest.size(); packInteger(dest, huffman_length, 7); encode(dest, str.data(), str.length() ); dest[head] |= 0x80; - } - else - { + } else { packInteger(dest, str.length(), 7); std::copy(str.cbegin(), str.cend(), std::back_inserter(dest) ); } @@ -5483,8 +5451,7 @@ namespace HPack { /* const std::string &key = header.first; - if ("content-length" == key || "set-cookie" == key) - { + if ("content-length" == key || "set-cookie" == key) { return true; }*/ @@ -5498,26 +5465,20 @@ namespace HPack std::tie(index, is_full_match) = findHeaderInTable(header, dynamicTable); - if (is_full_match) - { + if (is_full_match) { packIndex(dest, index); - return; } const bool indexing = shouldIndexing(header); - if (indexing) - { + if (indexing) { dynamicTable.addHeader(header); } - if (0 == index) - { + if (0 == index) { packFullHeader(dest, header, indexing); - } - else - { + } else { packHeaderValue(dest, index, header, indexing); } } @@ -5533,8 +5494,7 @@ namespace HPack */ void pack(std::vector &dest, const std::vector > &headers, Http2::DynamicTable &dynamicTable) { - for (auto const &header : headers) - { + for (auto const &header : headers) { packHeader(dest, header, dynamicTable); } } @@ -5562,32 +5522,27 @@ namespace HPack READ_VALUE, }; - static size_t getMaxTableIndex(const Http2::IncStream &stream) noexcept - { + static size_t getMaxTableIndex(const Http2::IncStream &stream) noexcept { return getStaticTableSize() + stream.conn.decoding_dynamic_table.size(); } static const std::pair *getHeaderFromTable(const size_t index, const Http2::IncStream &stream) noexcept { - if (getStaticTableSize() >= index) - { + if (getStaticTableSize() >= index) { return &staticTable[index - 1]; } - else if (stream.conn.decoding_dynamic_table.size() + getStaticTableSize() >= index) - { + else if (stream.conn.decoding_dynamic_table.size() + getStaticTableSize() >= index) { return &stream.conn.decoding_dynamic_table[index - getStaticTableSize() - 1]; } return nullptr; } - static const std::pair *unpackIndexed(const size_t index, const Http2::IncStream &stream) noexcept - { + static const std::pair *unpackIndexed(const size_t index, const Http2::IncStream &stream) noexcept { return getHeaderFromTable(index, stream); } - static bool checkHuffmanEncoded(const uint8_t c) noexcept - { + static bool checkHuffmanEncoded(const uint8_t c) noexcept { return c & (1 << 7); } @@ -5597,8 +5552,7 @@ namespace HPack left, false }; - if (dataSize < left) - { + if (dataSize < left) { std::get(result) = dataSize; } @@ -5609,8 +5563,7 @@ namespace HPack static uint64_t unpackString(std::vector &dest, const uint8_t *data, const size_t dataSize, uint64_t left) { - if (dataSize < left) - { + if (dataSize < left) { left = dataSize; } @@ -5637,17 +5590,14 @@ namespace HPack ++nread; - if (k != (c & k) ) - { + if (k != (c & k) ) { num = c & k; - return result; } num = k; - if (nread == dataSize) - { + if (nread == dataSize) { return result; } } @@ -5658,34 +5608,28 @@ namespace HPack uint32_t add = c & 0x7f; - if (add > (std::numeric_limits::max() >> shift) ) - { + if (add > (std::numeric_limits::max() >> shift) ) { std::get(result) = false; - return result; } add <<= shift; - if (std::numeric_limits::max() - add < num) - { + if (std::numeric_limits::max() - add < num) { std::get(result) = false; - return result; } num += add; - if (0 == (c & (1 << 7) ) ) - { + if (0 == (c & (1 << 7) ) ) { break; } shift += 7; } - if (nread != dataSize) - { + if (nread != dataSize) { ++nread; } @@ -5720,8 +5664,7 @@ namespace HPack this->buf.clear(); - if (this->index_required) - { + if (this->index_required) { stream.conn.decoding_dynamic_table.addHeader(header); } @@ -5732,8 +5675,7 @@ namespace HPack { auto entry = getHeaderFromTable(this->key_index, stream); - if (nullptr == entry) - { + if (nullptr == entry) { return std::pair(); } @@ -5745,8 +5687,7 @@ namespace HPack this->key_index = 0; this->buf.clear(); - if (this->index_required) - { + if (this->index_required) { stream.conn.decoding_dynamic_table.addHeader(header); } @@ -5770,27 +5711,20 @@ namespace HPack { const uint8_t c = data[cur]; - if (0x20 == (c & 0xe0) ) - { + if (0x20 == (c & 0xe0) ) { dec.opcode = HuffmanDecodeOpcode::INDEXED; dec.state = HuffmanDecodeState::READ_TABLE_SIZE; } - else if (c & 0x80) - { + else if (c & 0x80) { dec.opcode = HuffmanDecodeOpcode::INDEXED; dec.state = HuffmanDecodeState::READ_INDEX; - } - else - { - if (0 == c || 0x40 == c || 0x10 == c) - { + } else { + if (0 == c || 0x40 == c || 0x10 == c) { dec.opcode = HuffmanDecodeOpcode::NOT_INDEXED; dec.state = HuffmanDecodeState::CHECK_KEY_LENGTH; ++cur; - } - else - { + } else { dec.opcode = HuffmanDecodeOpcode::INDEXED_KEY; dec.state = HuffmanDecodeState::READ_INDEX; } @@ -5813,13 +5747,11 @@ namespace HPack cur += nread; - if (false == success) - { + if (false == success) { return false; } - if (dec.left > stream.conn.client_settings.header_table_size) - { + if (dec.left > stream.conn.client_settings.header_table_size) { // TODO: invalid max table size error code return false; } @@ -5835,16 +5767,12 @@ namespace HPack { uint8_t prefixlen; - if (HuffmanDecodeOpcode::INDEXED == dec.opcode) - { + if (HuffmanDecodeOpcode::INDEXED == dec.opcode) { prefixlen = 7; } - else if (dec.index_required) - { + else if (dec.index_required) { prefixlen = 6; - } - else - { + } else { prefixlen = 4; } @@ -5855,21 +5783,15 @@ namespace HPack cur += nread; - if (false == success || 0 == dec.left || dec.left > getMaxTableIndex(stream) ) - { + if (false == success || 0 == dec.left || dec.left > getMaxTableIndex(stream) ) { return false; } - if (HuffmanDecodeOpcode::INDEXED == dec.opcode) - { + if (HuffmanDecodeOpcode::INDEXED == dec.opcode) { stream.incoming_headers.emplace(*unpackIndexed(dec.left, stream) ); - dec.state = HuffmanDecodeState::OPCODE; - } - else - { + } else { dec.key_index = dec.left; - dec.state = HuffmanDecodeState::CHECK_VALUE_LENGTH; } @@ -5893,8 +5815,7 @@ namespace HPack cur += nread; - if (false == success) - { + if (false == success) { return false; } @@ -5915,8 +5836,7 @@ namespace HPack cur += nread; dec.left -= nread; - if (false == success || dec.left > 0) - { + if (false == success || dec.left > 0) { return false; } @@ -5934,8 +5854,7 @@ namespace HPack cur += nread; dec.left -= nread; - if (dec.left > 0) - { + if (dec.left > 0) { return false; } @@ -5963,8 +5882,7 @@ namespace HPack cur += nread; - if (false == success) - { + if (false == success) { return false; } @@ -5996,8 +5914,7 @@ namespace HPack cur += nread; dec.left -= nread; - if (false == success || dec.left > 0) - { + if (false == success || dec.left > 0) { return false; } @@ -6017,8 +5934,7 @@ namespace HPack cur += nread; dec.left -= nread; - if (dec.left > 0) - { + if (dec.left > 0) { return false; } @@ -6035,4 +5951,4 @@ namespace HPack return true; } -}; +} diff --git a/src/transfer/http2/HPack.h b/src/transfer/http2/HPack.h index d71375c..b467a15 100644 --- a/src/transfer/http2/HPack.h +++ b/src/transfer/http2/HPack.h @@ -12,4 +12,4 @@ namespace HPack // TODO: replace IncStream to DynamicTable if possible bool unpack(const void *src, const size_t srcSize, Http2::IncStream &stream); -}; +} diff --git a/src/transfer/http2/Http2.cpp b/src/transfer/http2/Http2.cpp index 93280ba..ed7b963 100644 --- a/src/transfer/http2/Http2.cpp +++ b/src/transfer/http2/Http2.cpp @@ -4,23 +4,19 @@ namespace Http2 { - bool operator &(const FrameFlag left, const FrameFlag right) noexcept - { + bool operator &(const FrameFlag left, const FrameFlag right) noexcept { return static_cast(left) & static_cast(right); } - FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept - { + FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept { return static_cast(static_cast(left) | static_cast(right) ); } - FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept - { + FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept { return static_cast(*reinterpret_cast(&left) |= static_cast(right) ); } - ConnectionSettings ConnectionSettings::defaultSettings() noexcept - { + ConnectionSettings ConnectionSettings::defaultSettings() noexcept { return ConnectionSettings { 4096, 1, @@ -37,16 +33,14 @@ namespace Http2 } - size_t DynamicTable::size() const noexcept - { + size_t DynamicTable::size() const noexcept { return this->list.size(); } DynamicTable::DynamicTable(const uint32_t headerTableSize, const uint32_t maxHeaderListSize, std::deque > &&list) noexcept : list(std::move(list) ), header_table_size(headerTableSize), max_header_list_size(maxHeaderListSize), cur_header_list_size(0) { - for (auto const &pair : list) - { + for (auto const &pair : list) { this->cur_header_list_size += pair.first.length() + pair.second.length(); } } @@ -87,8 +81,7 @@ namespace Http2 { this->header_table_size = headerTableSize; - while (this->list.size() > this->header_table_size) - { + while (this->list.size() > this->header_table_size) { auto const &pair = this->list.back(); this->cur_header_list_size -= pair.first.length() + pair.second.length(); @@ -111,18 +104,15 @@ namespace Http2 } } - const std::pair &DynamicTable::operator[](const size_t index) const noexcept - { + const std::pair &DynamicTable::operator[](const size_t index) const noexcept { return this->list[index]; } - std::pair &DynamicTable::operator[](const size_t index) noexcept - { + std::pair &DynamicTable::operator[](const size_t index) noexcept { return this->list[index]; } - const std::deque > &DynamicTable::getList() const noexcept - { + const std::deque > &DynamicTable::getList() const noexcept { return this->list; } @@ -144,13 +134,11 @@ namespace Http2 return (addr + Http2::FRAME_HEADER_SIZE); } - void IncStream::lock() - { + void IncStream::lock() { this->conn.sync.mtx.lock(); } - void IncStream::unlock() noexcept - { + void IncStream::unlock() noexcept { this->conn.sync.mtx.unlock(); } @@ -190,13 +178,11 @@ namespace Http2 return (addr + Http2::FRAME_HEADER_SIZE); } - void OutStream::lock() - { + void OutStream::lock() { this->mtx->lock(); } - void OutStream::unlock() noexcept - { + void OutStream::unlock() noexcept { this->mtx->unlock(); } -}; +} diff --git a/src/transfer/http2/Http2.h b/src/transfer/http2/Http2.h index 6647fee..12b6d65 100644 --- a/src/transfer/http2/Http2.h +++ b/src/transfer/http2/Http2.h @@ -200,4 +200,4 @@ namespace Http2 void lock(); void unlock() noexcept; }; -}; +} diff --git a/src/utils/Event.cpp b/src/utils/Event.cpp index 6b23628..35771ba 100644 --- a/src/utils/Event.cpp +++ b/src/utils/Event.cpp @@ -11,19 +11,16 @@ namespace Utils void Event::wait() { - if (false == this->signaled.load() ) - { + if (this->signaled.load() == false) { std::unique_lock lck(this->mtx); - do - { + do { this->cv.wait(lck); } while (false == this->signaled.load() ); } - if (false == this->manually) - { + if (false == this->manually) { this->signaled.store(false); } } @@ -32,15 +29,13 @@ namespace Utils { bool is_timeout = false; - if (false == this->signaled.load() ) - { + if (this->signaled.load() == false) { std::unique_lock lck(this->mtx); is_timeout = false == this->cv.wait_for(lck, ms, [this] { return this->notifed(); } ); } - if (false == this->manually) - { + if (false == this->manually) { this->signaled.store(false); } @@ -51,14 +46,11 @@ namespace Utils { bool is_timeout = false; - if (false == this->signaled.load() ) - { + if (this->signaled.load() == false) { std::unique_lock lck(this->mtx); - do - { - if (std::cv_status::timeout == this->cv.wait_until(lck, tp) ) - { + do { + if (std::cv_status::timeout == this->cv.wait_until(lck, tp) ) { is_timeout = true; break; } @@ -66,37 +58,31 @@ namespace Utils while (false == this->signaled.load() ); } - if (false == this->manually) - { + if (false == this->manually) { this->signaled.store(false); } return is_timeout; } - void Event::notify() noexcept - { + void Event::notify() noexcept { this->signaled.store(true); this->cv.notify_all(); } - void Event::notify(const size_t threadsCount) noexcept - { + void Event::notify(const size_t threadsCount) noexcept { this->signaled.store(true); - for (size_t i = 0; i < threadsCount; ++i) - { + for (size_t i = 0; i < threadsCount; ++i) { this->cv.notify_one(); } } - void Event::reset() noexcept - { + void Event::reset() noexcept { this->signaled.store(false); } - bool Event::notifed() const noexcept - { + bool Event::notifed() const noexcept { return this->signaled.load(); } -}; +} diff --git a/src/utils/Event.h b/src/utils/Event.h index ddacbe4..b9d20ad 100644 --- a/src/utils/Event.h +++ b/src/utils/Event.h @@ -30,4 +30,4 @@ namespace Utils bool notifed() const noexcept; }; -}; +} diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index 9ef440e..77d8242 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -12,17 +12,13 @@ namespace Utils { - void toLower(std::string &str) noexcept - { + void toLower(std::string &str) noexcept { std::transform(str.begin(), str.end(), str.begin(), ::tolower); } - std::string getLowerString(const std::string &str) - { + std::string getLowerString(const std::string &str) { std::string copy = str; - toLower(copy); - return copy; } @@ -32,20 +28,16 @@ namespace Utils const size_t last = str.find_last_not_of(whitespace.data() ); - if (std::string::npos == last) - { + if (std::string::npos == last) { return str.clear(); } str.assign(str.cbegin() + str.find_first_not_of(whitespace.data() ), str.cbegin() + last + 1); } - std::string getTrimmedString(const std::string &str) - { + std::string getTrimmedString(const std::string &str) { std::string copy = str; - trim(copy); - return copy; } @@ -64,8 +56,7 @@ namespace Utils pos = delimiter; - if (std::string::npos != pos) - { + if (std::string::npos != pos) { ++pos; } } @@ -78,8 +69,7 @@ namespace Utils std::string buf; buf.reserve(str.length() ); - for (size_t pos = 0; pos < str.length(); ++pos) - { + for (size_t pos = 0; pos < str.length(); ++pos) { switch (str[pos]) { case '&': buf.append("&"); break; @@ -102,8 +92,7 @@ namespace Utils static const std::array hexDigits { "0123456789abcdef" }; - for (size_t i = dataSize - 1; std::numeric_limits::max() != i; --i) - { + for (size_t i = dataSize - 1; std::numeric_limits::max() != i; --i) { str[i * 2 + 0] = hexDigits[bin[i] >> 4]; str[i * 2 + 1] = hexDigits[bin[i] & 0x0F]; } @@ -113,16 +102,13 @@ namespace Utils static unsigned char hexStringToBinEncodeSymbol(const char c) noexcept { - if (c >= '0' && c <= '9') - { + if (c >= '0' && c <= '9') { return c - 0x30; } - else if (c >= 'a' && c <= 'f') - { + else if (c >= 'a' && c <= 'f') { return c - 0x57; } - else if (c >= 'A' && c <= 'F') - { + else if (c >= 'A' && c <= 'F') { return c - 0x37; } @@ -133,8 +119,7 @@ namespace Utils { std::string bin(hexStr.length() / 2, 0); - for (size_t i = 0; i < bin.length(); ++i) - { + for (size_t i = 0; i < bin.length(); ++i) { const char a = hexStr[i * 2 + 0]; const char b = hexStr[i * 2 + 1]; @@ -161,14 +146,12 @@ namespace Utils unsigned char c[sizeof(uint64_t)]; } x; - if (endian == Endianness::INIT) - { + if (endian == Endianness::INIT) { x.ull = 0x01; endian = (x.c[7] == 0x01ULL) ? Endianness::BIGE : Endianness::LITE; } - if (endian == Endianness::BIGE) - { + if (endian == Endianness::BIGE) { return host64; } @@ -184,8 +167,7 @@ namespace Utils return x.ull; } - uint64_t ntoh64(const uint64_t net64) noexcept - { + uint64_t ntoh64(const uint64_t net64) noexcept { return hton64(net64); } @@ -198,20 +180,16 @@ namespace Utils uint8_t c[sizeof(uint32_t)]; } x; - if (endian == Endianness::INIT) - { + if (endian == Endianness::INIT) { x.ui = 0x01; endian = (x.c[3] == 0x01) ? Endianness::BIGE : Endianness::LITE; } x.ui = src; - if (endian == Endianness::BIGE) - { + if (endian == Endianness::BIGE) { x.ui <<= 8; - } - else - { + } else { uint8_t c = x.c[0]; x.c[0] = x.c[2]; x.c[2] = c; @@ -229,14 +207,12 @@ namespace Utils uint8_t c[sizeof(uint32_t)]; } x; - if (endian == Endianness::INIT) - { + if (endian == Endianness::INIT) { x.ui = 0x01; endian = (x.c[3] == 0x01) ? Endianness::BIGE : Endianness::LITE; } - if (endian == Endianness::BIGE) - { + if (endian == Endianness::BIGE) { return *reinterpret_cast(src24) >> 8; } @@ -251,55 +227,44 @@ namespace Utils return x.ui;// *reinterpret_cast(x.c); } - std::string getUniqueName() - { + std::string getUniqueName() { size_t time = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - time = hton64(time); - return binToHexString(&time, sizeof(time) ); } size_t getPackNumberSize(const size_t number) noexcept { - if (number <= 253) - { + if (number <= 253) { return sizeof(uint8_t); } - else if (number <= std::numeric_limits::max() ) - { + else if (number <= std::numeric_limits::max() ) { return sizeof(uint8_t) + sizeof(uint16_t); } - else if (number <= std::numeric_limits::max() ) - { + else if (number <= std::numeric_limits::max() ) { return sizeof(uint8_t) + sizeof(uint32_t); } return sizeof(uint8_t) + sizeof(size_t); } - size_t getPackStringSize(const std::string &str) noexcept - { + size_t getPackStringSize(const std::string &str) noexcept { return getPackNumberSize(str.length() ) + str.length(); } - uint8_t *packPointer(uint8_t *dest, void *pointer) noexcept - { + uint8_t *packPointer(uint8_t *dest, void *pointer) noexcept { *reinterpret_cast(dest) = pointer; - return dest + sizeof(void *); } uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept { - if (number <= 252) - { + if (number <= 252) { *dest = number; dest += sizeof(uint8_t); } - else if (number <= std::numeric_limits::max() ) - { + else if (number <= std::numeric_limits::max() ) { *dest = 253; dest += sizeof(uint8_t); @@ -308,8 +273,7 @@ namespace Utils dest += sizeof(uint16_t); } - else if (number <= std::numeric_limits::max() ) - { + else if (number <= std::numeric_limits::max() ) { *dest = 254; dest += sizeof(uint8_t); @@ -317,9 +281,7 @@ namespace Utils *reinterpret_cast(dest) = static_cast(number); dest += sizeof(uint32_t); - } - else - { + } else { *dest = 255; dest += sizeof(uint8_t); @@ -332,17 +294,13 @@ namespace Utils return dest; } - uint8_t *packString(uint8_t *dest, const std::string &str) noexcept - { + uint8_t *packString(uint8_t *dest, const std::string &str) noexcept { dest = packNumber(dest, str.length() ); - std::memcpy(dest, str.data(), str.length() ); - return dest + str.length(); } - void packPointer(std::vector &buf, void *pointer) - { + void packPointer(std::vector &buf, void *pointer) { buf.resize(buf.size() + sizeof(void *) ); uint8_t *dest = reinterpret_cast(buf.data() + buf.size() - sizeof(void *) ); *reinterpret_cast(dest) = pointer; @@ -350,28 +308,23 @@ namespace Utils void packNumber(std::vector &buf, const size_t number) { - if (number <= 252) - { + if (number <= 252) { buf.emplace_back(number); } - else if (number <= std::numeric_limits::max() ) - { + else if (number <= std::numeric_limits::max() ) { buf.emplace_back(253); buf.resize(buf.size() + sizeof(uint16_t) ); *reinterpret_cast(buf.data() + buf.size() - sizeof(uint16_t) ) = static_cast(number); } - else if (number <= std::numeric_limits::max() ) - { + else if (number <= std::numeric_limits::max() ) { buf.emplace_back(254); buf.resize(buf.size() + sizeof(uint32_t) ); *reinterpret_cast(buf.data() + buf.size() - sizeof(uint32_t) ) = static_cast(number); - } - else - { + } else { buf.emplace_back(255); buf.resize(buf.size() + sizeof(size_t) ); @@ -384,16 +337,13 @@ namespace Utils { packNumber(buf, str.length() ); - if (str.length() ) - { + if (str.length() ) { std::copy(str.cbegin(), str.cend(), std::back_inserter(buf) ); } } - const uint8_t *unpackPointer(void **pointer, const uint8_t *src) noexcept - { + const uint8_t *unpackPointer(void **pointer, const uint8_t *src) noexcept { *pointer = *reinterpret_cast(const_cast(static_cast(src) ) ); - return src + sizeof(void *); } @@ -403,26 +353,18 @@ namespace Utils src += sizeof(uint8_t); - if (*number <= 252) - { + if (*number <= 252) { } - else if (*number == 253) - { + else if (*number == 253) { *number = *reinterpret_cast(src); - src += sizeof(uint16_t); } - else if (*number == 254) - { + else if (*number == 254) { *number = *reinterpret_cast(src); - src += sizeof(uint32_t); - } - else - { + } else { *number = *reinterpret_cast(src); - src += sizeof(size_t); } @@ -465,8 +407,7 @@ namespace Utils size_t pos = strTime.find_first_not_of(' '); size_t delimiter = strTime.find(',', pos); - if (std::string::npos == delimiter || delimiter - pos != 3) - { + if (std::string::npos == delimiter || delimiter - pos != 3) { return ~0; } @@ -474,20 +415,16 @@ namespace Utils auto const it_day = map_days.find(day); - if (map_days.cend() != it_day) - { + if (map_days.cend() != it_day) { tc.tm_wday = it_day->second; - } - else - { + } else { return ~0; } pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { return ~0; } @@ -496,8 +433,7 @@ namespace Utils pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); - if (std::string::npos == delimiter || delimiter - pos != 3) - { + if (std::string::npos == delimiter || delimiter - pos != 3) { return ~0; } @@ -505,20 +441,16 @@ namespace Utils auto const it_mon = map_months.find(month); - if (map_months.cend() != it_mon) - { + if (map_months.cend() != it_mon) { tc.tm_mon = it_mon->second; - } - else - { + } else { return ~0; } pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { return ~0; } @@ -527,8 +459,7 @@ namespace Utils pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(':', pos); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { return ~0; } @@ -537,8 +468,7 @@ namespace Utils pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(':', pos); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { return ~0; } @@ -547,8 +477,7 @@ namespace Utils pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { return ~0; } @@ -557,13 +486,11 @@ namespace Utils pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { delimiter = strTime.length(); } - if (std::string::npos == pos || delimiter - pos > 5) - { + if (std::string::npos == pos || delimiter - pos > 5) { return ~0; } @@ -573,8 +500,7 @@ namespace Utils int timezone = 0; - if (map_zones.cend() != it_zone) - { + if (map_zones.cend() != it_zone) { timezone = it_zone->second; } else if (zone.length() == 5 && ('+' == zone.front() || '-' == zone.front() ) ) @@ -588,13 +514,10 @@ namespace Utils timezone = std::strtoul(hours.data(), nullptr, 10) * 3600; timezone += std::strtoul(minutes.data(), nullptr, 10) * 60; - if ('-' == zone.front() ) - { + if (zone.front() == '-') { timezone *= -1; } - } - else - { + } else { return ~0; } @@ -629,8 +552,7 @@ namespace Utils const char *ptrStr = std::strchr(strTime, ' '); - if (nullptr == ptrStr) - { + if (nullptr == ptrStr) { return ~0; } @@ -638,27 +560,22 @@ namespace Utils auto const it_mon = map_months.find(month); - if (map_months.cend() != it_mon) - { + if (map_months.cend() != it_mon) { tc.tm_mon = it_mon->second; - } - else - { + } else { return ~0; } ++ptrStr; // Fix for MS __DATE__ - if (' ' == *ptrStr) - { + if (' ' == *ptrStr) { ++ptrStr; } strTime = std::strchr(ptrStr, ' '); - if (nullptr == strTime) - { + if (nullptr == strTime) { return ~0; } @@ -668,8 +585,7 @@ namespace Utils ptrStr = std::strchr(strTime, ' '); - if (nullptr == ptrStr) - { + if (nullptr == ptrStr) { return ~0; } @@ -679,8 +595,7 @@ namespace Utils strTime = std::strchr(ptrStr, ':'); - if (nullptr == strTime) - { + if (nullptr == strTime) { return ~0; } @@ -690,8 +605,7 @@ namespace Utils ptrStr = std::strchr(strTime, ':'); - if (nullptr == ptrStr) - { + if (nullptr == ptrStr) { return ~0; } @@ -711,8 +625,7 @@ namespace Utils { std::array buf; - if (tTime == ~0) - { + if (tTime == ~0) { std::time(&tTime); } @@ -739,8 +652,7 @@ namespace Utils return std::string(buf.data(), buf.data() + len); } - std::string predefinedDatetimeToRfc822(const char *strTime) - { + std::string predefinedDatetimeToRfc822(const char *strTime) { const std::time_t time = predefinedDatetimeToTimestamp(strTime); return getDatetimeAsString(time, false); } @@ -749,8 +661,7 @@ namespace Utils { size_t length = 0; - do - { + do { ++length; number /= 10; } @@ -761,8 +672,7 @@ namespace Utils bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies) { - if (cookieHeader.empty() ) - { + if (cookieHeader.empty() ) { return true; } @@ -772,8 +682,7 @@ namespace Utils size_t delimiter = cookieHeader.find('=', cur_pos); - if (std::string::npos == delimiter || delimiter > next_value) - { + if (std::string::npos == delimiter || delimiter > next_value) { return false; } @@ -789,8 +698,7 @@ namespace Utils cookies.emplace(std::move(key), std::move(value) ); - if (std::string::npos != next_value) - { + if (std::string::npos != next_value) { ++next_value; } } @@ -798,8 +706,7 @@ namespace Utils return true; } - static inline bool isCharUrlAllowed(const char c) noexcept - { + static inline bool isCharUrlAllowed(const char c) noexcept { return c == '-' || c == '_' || c == '.' || c == '~'; } @@ -813,16 +720,12 @@ namespace Utils { const unsigned char c = str[i]; - if (std::isalnum(c) || isCharUrlAllowed(c) ) - { + if (std::isalnum(c) || isCharUrlAllowed(c) ) { encoded.push_back(c); } - else if (' ' == c) - { + else if (' ' == c) { encoded.push_back('+'); - } - else - { + } else { const uint8_t a = c >> 4; const uint8_t b = c & 0x0F; @@ -843,10 +746,8 @@ namespace Utils { unsigned char c = str[i]; - if ('%' == c) - { - if (i + 2 < str.length() ) - { + if ('%' == c) { + if (i + 2 < str.length() ) { const char a = str[++i]; const char b = str[++i]; @@ -855,8 +756,7 @@ namespace Utils ); } } - else if ('+' == c) - { + else if ('+' == c) { c = ' '; } diff --git a/src/utils/Utils.h b/src/utils/Utils.h index 9dce37a..340c6eb 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -36,8 +36,7 @@ namespace Utils { size_t full_size = getPackNumberSize(container.size() ); - for (auto const &pair : container) - { + for (auto const &pair : container) { full_size += getPackStringSize(pair.first); full_size += getPackStringSize(pair.second); } @@ -56,8 +55,7 @@ namespace Utils addr = packNumber(addr, container.size() ); - for (auto const &pair : container) - { + for (auto const &pair : container) { addr = packString(addr, pair.first); addr = packString(addr, pair.second); } @@ -74,8 +72,7 @@ namespace Utils { packNumber(buf, container.size() ); - for (auto const &pair : container) - { + for (auto const &pair : container) { packString(buf, pair.first); packString(buf, pair.second); } @@ -91,8 +88,7 @@ namespace Utils size_t count; src = unpackNumber(&count, src); - for (size_t i = 0; i < count; ++i) - { + for (size_t i = 0; i < count; ++i) { std::string key; src = unpackString(key, src); @@ -111,8 +107,7 @@ namespace Utils size_t count; src = unpackNumber(&count, src); - for (size_t i = 0; i < count; ++i) - { + for (size_t i = 0; i < count; ++i) { std::string key; src = unpackString(key, src); From 77c54db0adbe8d70210cc7c0b10f551d52b14252 Mon Sep 17 00:00:00 2001 From: awwit Date: Mon, 13 Nov 2017 02:25:48 +0300 Subject: [PATCH 10/22] Fixed stuck threads and interruption of data reception over HTTPS --- src/server/Server.cpp | 313 +++++++++------------------- src/server/protocol/ServerHttp2.cpp | 137 ++++-------- src/server/protocol/ServerHttp2.h | 2 +- src/socket/AdapterTls.cpp | 7 +- src/socket/List.cpp | 89 +++----- src/socket/List.h | 2 +- src/socket/Socket.cpp | 6 +- src/socket/Socket.h | 2 +- src/utils/Event.cpp | 6 +- 9 files changed, 187 insertions(+), 377 deletions(-) diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 30846c5..3f87c92 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -38,10 +38,8 @@ namespace HttpServer std::unique_ptr prot; // If request is HTTP/2 Stream - if (stream) - { + if (stream) { prot.reset(new ServerHttp2Stream(sock, settings, controls, stream) ); - return prot; } @@ -53,12 +51,10 @@ namespace HttpServer { const std::string protocol(reinterpret_cast(datum.data), datum.size); - if ("h2" == protocol) - { + if ("h2" == protocol) { prot.reset(new ServerHttp2(sock, settings, controls, sockets) ); } - else if ("http/1.1" == protocol) - { + else if ("http/1.1" == protocol) { prot.reset(new ServerHttp1(sock, settings, controls) ); } } @@ -78,15 +74,12 @@ namespace HttpServer { std::unique_ptr prot = getProtocolVariant(sock, this->settings, this->controls, sockets, stream); - if (prot) - { + if (prot) { // Check if switching protocol - for (ServerProtocol *ret = nullptr; ; ) - { + for (ServerProtocol *ret = nullptr; ; ) { ret = prot->process(); - if (prot.get() == ret) - { + if (prot.get() == ret) { break; } @@ -110,15 +103,13 @@ namespace HttpServer eventThreadCycle.wait(); - if (false == this->controls.process_flag) - { + if (this->controls.process_flag == false) { break; } sockets.lock(); - if (sockets.size() ) - { + if (sockets.size() ) { std::tie(sock, stream) = sockets.front(); /* sockaddr_in addr {}; @@ -128,10 +119,8 @@ namespace HttpServer sockets.pop(); } - if (sockets.empty() ) - { + if (sockets.empty() ) { eventThreadCycle.reset(); - this->controls.eventNotFullQueue->notify(); } @@ -168,8 +157,7 @@ namespace HttpServer std::get(data) ); - if (socket_adapter.handshake() ) - { + if (socket_adapter.handshake() ) { this->threadRequestProc(socket_adapter, sockets, nullptr); } } @@ -195,19 +183,15 @@ namespace HttpServer size_t threads_max_count = 0; - if (this->settings.global.cend() != it_option) - { + if (this->settings.global.cend() != it_option) { const std::string &option = it_option->second; - threads_max_count = std::strtoull(option.c_str(), nullptr, 10); } - if (0 == threads_max_count) - { + if (0 == threads_max_count) { threads_max_count = std::thread::hardware_concurrency(); - if (0 == threads_max_count) - { + if (0 == threads_max_count) { threads_max_count = 1; } @@ -225,16 +209,13 @@ namespace HttpServer active_threads.reserve(threads_max_count); // For update applications modules - do - { - if (this->controls.eventUpdateModule->notifed() ) - { + do { + if (this->controls.eventUpdateModule->notifed() ) { updateModules(); } // Cycle creation threads applications requests - do - { + do { while (this->threads_working_count == active_threads.size() && active_threads.size() < threads_max_count && sockets.empty() == false) { active_threads.emplace_back(serverThreadRequestCycle, this, std::ref(sockets), std::ref(eventThreadCycle) ); @@ -242,8 +223,7 @@ namespace HttpServer size_t notify_count = active_threads.size() - this->threads_working_count; - if (notify_count > sockets.size() ) - { + if (notify_count > sockets.size() ) { notify_count = sockets.size(); } @@ -257,11 +237,9 @@ namespace HttpServer eventThreadCycle.notify(); - if (false == active_threads.empty() ) - { + if (active_threads.empty() == false) { // Join threads (wait completion) - for (auto &th : active_threads) - { + for (auto &th : active_threads) { th.join(); } @@ -285,16 +263,13 @@ namespace HttpServer { same.emplace(app); - try - { - if (app->application_final) - { + try { + if (app->application_final) { const std::string root = app->root_dir; app->application_final(root.data() ); } } - catch (std::exception &exc) - { + catch (std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl; } @@ -314,16 +289,14 @@ namespace HttpServer #ifdef WIN32 std::ifstream src(app->server_module_update, std::ifstream::binary); - if ( ! src) - { + if ( ! src) { std::cout << "Error: file '" << app->server_module_update << "' cannot be open;" << std::endl; return false; } std::ofstream dst(module_name, std::ofstream::binary | std::ofstream::trunc); - if ( ! dst) - { + if ( ! dst) { std::cout << "Error: file '" << module_name << "' cannot be open;" << std::endl; return false; } @@ -345,27 +318,22 @@ namespace HttpServer std::string module_name_temp; - if (std::string::npos != ext_pos && (std::string::npos == dir_pos || dir_pos < ext_pos) ) - { + if (std::string::npos != ext_pos && (std::string::npos == dir_pos || dir_pos < ext_pos) ) { module_name_temp = module_name.substr(0, ext_pos) + '-' + Utils::getUniqueName() + module_name.substr(ext_pos); - } - else - { + } else { module_name_temp = module_name + '-' + Utils::getUniqueName(); } std::ifstream src(app->server_module_update, std::ifstream::binary); - if ( ! src) - { + if ( ! src) { std::cout << "Error: file '" << app->server_module_update << "' cannot be open;" << std::endl; return false; } std::ofstream dst(module_name_temp, std::ofstream::binary | std::ofstream::trunc); - if ( ! dst) - { + if ( ! dst) { std::cout << "Error: file '" << module_name << "' cannot be open;" << std::endl; return false; } @@ -379,14 +347,12 @@ namespace HttpServer // Open updated module module.open(module_name_temp); - if (0 != std::remove(module_name.c_str() ) ) - { + if (0 != std::remove(module_name.c_str() ) ) { std::cout << "Error: file '" << module_name << "' could not be removed;" << std::endl; return false; } - if (0 != std::rename(module_name_temp.c_str(), module_name.c_str() ) ) - { + if (0 != std::rename(module_name_temp.c_str(), module_name.c_str() ) ) { std::cout << "Error: file '" << module_name_temp << "' could not be renamed;" << std::endl; return false; } @@ -394,30 +360,26 @@ namespace HttpServer #error "Undefined platform" #endif - if (false == module.is_open() ) - { + if (module.is_open() == false) { std::cout << "Error: the application module '" << module_name << "' can not be opened;" << std::endl; return false; } void *(*addr)(void *) = nullptr; - if (false == module.find("application_call", &addr) ) - { + if (module.find("application_call", &addr) == false) { std::cout << "Error: function 'application_call' was not found in the module '" << module_name << "';" << std::endl; return false; } std::function app_call = reinterpret_cast(addr); - if ( ! app_call) - { + if ( ! app_call) { std::cout << "Error: invalid function 'application_call' is in the module '" << module_name << "';" << std::endl; return false; } - if (false == module.find("application_clear", &addr) ) - { + if (module.find("application_clear", &addr) == false) { std::cout << "Error: function 'application_clear' was not found in the module '" << module_name << "';" << std::endl; return false; } @@ -426,15 +388,13 @@ namespace HttpServer std::function app_init = std::function(); - if (module.find("application_init", &addr) ) - { + if (module.find("application_init", &addr) ) { app_init = reinterpret_cast(addr); } std::function app_final = std::function(); - if (module.find("application_final", &addr) ) - { + if (module.find("application_final", &addr) ) { app_final = reinterpret_cast(addr); } @@ -445,16 +405,13 @@ namespace HttpServer app->application_init = app_init; app->application_final = app_final; - try - { - if (app->application_init) - { + try { + if (app->application_init) { const std::string root = app->root_dir; app->application_init(root.data() ); } } - catch (std::exception &exc) - { + catch (std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << module_name << "' was initialized: " << exc.what() << std::endl; } } @@ -478,7 +435,7 @@ namespace HttpServer // If module is not updated (not checked) if (updated.cend() == updated.find(module_index) ) { - if (false == app->server_module_update.empty() && app->server_module_update != app->server_module) + if (app->server_module_update.empty() == false && app->server_module_update != app->server_module) { size_t module_size_new = 0; time_t module_time_new = 0; @@ -490,10 +447,8 @@ namespace HttpServer System::Module &module = this->modules[module_index]; - if (System::getFileSizeAndTimeGmt(app->server_module, &module_size_cur, &module_time_cur) ) - { - if (module_size_cur != module_size_new || module_time_cur < module_time_new) - { + if (System::getFileSizeAndTimeGmt(app->server_module, &module_size_cur, &module_time_cur) ) { + if (module_size_cur != module_size_new || module_time_cur < module_time_new) { this->updateModule(module, applications, module_index); } } @@ -532,10 +487,8 @@ namespace HttpServer { this->controls.clear(); - if (false == this->tls_data.empty() ) - { - for (auto &pair : this->tls_data) - { + if (this->tls_data.empty() == false) { + for (auto &pair : this->tls_data) { std::tuple &data = pair.second; ::gnutls_certificate_free_credentials(std::get(data) ); @@ -545,10 +498,8 @@ namespace HttpServer this->settings.clear(); - if (false == this->modules.empty() ) - { - for (auto &module : this->modules) - { + if (this->modules.empty() == false) { + for (auto &module : this->modules) { module.close(); } @@ -566,48 +517,41 @@ namespace HttpServer int ret = ::gnutls_certificate_allocate_credentials(&x509_cred); - if (ret < 0) - { + if (ret < 0) { std::cout << "Error [tls]: certificate credentials has not been allocated;" << std::endl; - return false; } - if (false == app.chain_file.empty() ) + if (app.chain_file.empty() == false) { ret = ::gnutls_certificate_set_x509_trust_file(x509_cred, app.chain_file.c_str(), GNUTLS_X509_FMT_PEM); - if (ret < 0) - { + if (ret < 0) { std::cout << "Warning [tls]: (CA) chain file has not been accepted;" << std::endl; } } - if (false == app.crl_file.empty() ) + if (app.crl_file.empty() == false) { ret = ::gnutls_certificate_set_x509_crl_file(x509_cred, app.crl_file.c_str(), GNUTLS_X509_FMT_PEM); - if (ret < 0) - { + if (ret < 0) { std::cout << "Warning [tls]: (CLR) clr file has not been accepted;" << std::endl; } } ret = ::gnutls_certificate_set_x509_key_file(x509_cred, app.cert_file.c_str(), app.key_file.c_str(), GNUTLS_X509_FMT_PEM); - if (ret < 0) - { + if (ret < 0) { std::cout << "Error [tls]: (CERT) cert file or/and (KEY) key file has not been accepted;" << std::endl; - return false; } - if (false == app.stapling_file.empty() ) + if (app.stapling_file.empty() == false) { ret = ::gnutls_certificate_set_ocsp_status_request_file(x509_cred, app.stapling_file.c_str(), 0); - if (ret < 0) - { + if (ret < 0) { std::cout << "Warning [tls]: (OCSP) stapling file has not been accepted;" << std::endl; } } @@ -651,12 +595,9 @@ namespace HttpServer dh_file.close(); } - if (ret < 0) - { + if (ret < 0) { ::gnutls_certificate_free_credentials(x509_cred); - std::cout << "Error [tls]: DH params were not loaded;" << std::endl; - return false; } @@ -666,12 +607,9 @@ namespace HttpServer ret = ::gnutls_priority_init(&priority_cache, "NORMAL", nullptr); - if (ret < 0) - { + if (ret < 0) { ::gnutls_certificate_free_credentials(x509_cred); - std::cout << "Error [tls]: priority cache cannot be init;" << std::endl; - return false; } @@ -683,27 +621,23 @@ namespace HttpServer bool Server::tryBindPort(const int port, std::unordered_set &ports) { // Only unique ports - if (ports.cend() != ports.find(port) ) - { + if (ports.cend() != ports.find(port) ) { return false; } Socket::Socket sock; - if (false == sock.open() ) - { + if (sock.open() == false) { std::cout << "Error: socket cannot be open; errno " << Socket::Socket::getLastError() << ";" << std::endl; return false; } - if (false == sock.bind(port) ) - { + if (sock.bind(port) == false) { std::cout << "Error: port " << port << " cannot be bind; errno " << Socket::Socket::getLastError() << ";" << std::endl; return false; } - if (false == sock.listen() ) - { + if (sock.listen() == false) { std::cout << "Error: socket " << port << " cannot be listen; errno " << Socket::Socket::getLastError() << ";" << std::endl; return false; } @@ -732,16 +666,12 @@ namespace HttpServer { const std::unordered_set &tls = app->tls_ports; - if (false == tls.empty() ) - { + if (tls.empty() == false) { std::tuple data; - if (tlsInit(*app, data) ) - { - for (const int port : tls) - { - if (this->tryBindPort(port, ports) ) - { + if (tlsInit(*app, data) ) { + for (const int port : tls) { + if (this->tryBindPort(port, ports) ) { this->tls_data.emplace(port, data); } } @@ -750,8 +680,7 @@ namespace HttpServer const std::unordered_set &list = app->ports; - for (const int port : list) - { + for (const int port : list) { this->tryBindPort(port, ports); } } @@ -759,15 +688,13 @@ namespace HttpServer int Server::run() { - if (false == this->init() ) - { + if (this->init() == false) { return 0x10; } this->initAppsPorts(); - if (this->liseners.empty() ) - { + if (this->liseners.empty() ) { std::cout << "Error: any socket was not open;" << std::endl; this->clear(); return 0x20; @@ -777,8 +704,7 @@ namespace HttpServer sockets_list.create(this->liseners.size() ); - for (auto const &sock : this->liseners) - { + for (auto const &sock : this->liseners) { sockets_list.addSocket(sock); } @@ -799,8 +725,7 @@ namespace HttpServer std::vector accept_sockets; // Cycle for receiving new connections - do - { + do { if (sockets_list.accept(accept_sockets) ) { sockets.lock(); @@ -827,8 +752,7 @@ namespace HttpServer this->controls.eventProcessQueue->notify(); - if (sockets.size() >= queue_max_length) - { + if (sockets.size() >= queue_max_length) { this->controls.eventNotFullQueue->reset(); } @@ -845,10 +769,8 @@ namespace HttpServer sockets_list.destroy(); - if (false == this->liseners.empty() ) - { - for (Socket::Socket &sock : this->liseners) - { + if (this->liseners.empty() == false) { + for (Socket::Socket &sock : this->liseners) { sock.close(); } @@ -866,32 +788,26 @@ namespace HttpServer { for (int i = 1; i < argc; ++i) { - if (0 == ::strcmp(argv[i], "--start") ) - { + if (0 == ::strcmp(argv[i], "--start") ) { } - else if (0 == ::strcmp(argv[i], "--force") ) - { + else if (0 == ::strcmp(argv[i], "--force") ) { st->force = true; } - else if (argv[i] == ::strstr(argv[i], "--config-path=") ) - { + else if (argv[i] == ::strstr(argv[i], "--config-path=") ) { st->config_path = std::string(argv[i] + sizeof("--config-path=") - 1); } - else if (argv[i] == ::strstr(argv[i], "--server-name=") ) - { + else if (argv[i] == ::strstr(argv[i], "--server-name=") ) { st->server_name = std::string(argv[i] + sizeof("--server-name=") - 1); } else { std::cout << "Argument '" << argv[i] << "' can't be applied with --start;" << std::endl; - return false; } } - if (st->server_name.empty() ) - { + if (st->server_name.empty() ) { st->server_name = argv[0]; } @@ -904,23 +820,18 @@ namespace HttpServer { struct server_start_args st = {}; - if (false == Server::get_start_args(argc, argv, &st) ) - { + if (Server::get_start_args(argc, argv, &st) == false) { return 0x1; } - if (false == st.config_path.empty() ) - { - if (false == System::changeCurrentDirectory(st.config_path) ) - { + if (st.config_path.empty() == false) { + if (System::changeCurrentDirectory(st.config_path) == false) { std::cout << "Configuration path '" << st.config_path << "' has not been found;" << std::endl; - return 0x2; } } - if (st.force) - { + if (st.force) { System::SharedMemory::destroy(st.server_name); System::GlobalMutex::destory(st.server_name); } @@ -930,16 +841,13 @@ namespace HttpServer bool is_exists = false; - if (glob_mtx.open(st.server_name) ) - { + if (glob_mtx.open(st.server_name) ) { glob_mtx.lock(); - if (glob_mem.open(st.server_name) ) - { + if (glob_mem.open(st.server_name) ) { System::native_processid_type pid = 0; - if (glob_mem.read(&pid, sizeof(pid) ) ) - { + if (glob_mem.read(&pid, sizeof(pid) ) ) { is_exists = System::isProcessExists(pid); } } @@ -947,46 +855,34 @@ namespace HttpServer glob_mtx.unlock(); } - if (is_exists) - { + if (is_exists) { std::cout << "Server instance with the name '" << st.server_name << "' is already running;" << std::endl; - return 0x3; } - if (false == glob_mtx.open(st.server_name) ) - { - if (false == glob_mtx.create(st.server_name) ) - { + if (glob_mtx.open(st.server_name) == false) { + if (glob_mtx.create(st.server_name) == false) { std::cout << "Global mutex could not been created;" << std::endl; - return 0x4; } } glob_mtx.lock(); - if (false == glob_mem.open(st.server_name) ) - { - if (false == glob_mem.create(st.server_name, sizeof(System::native_processid_type) ) ) - { + if (glob_mem.open(st.server_name) == false) { + if (glob_mem.create(st.server_name, sizeof(System::native_processid_type) ) == false) { glob_mtx.unlock(); - std::cout << "Shared memory could not been allocated;" << std::endl; - return 0x5; } } System::native_processid_type pid = System::getProcessId(); - if (false == glob_mem.write(&pid, sizeof(pid) ) ) - { + if (glob_mem.write(&pid, sizeof(pid) ) == false) { glob_mem.destroy(); glob_mtx.unlock(); - std::cout << "Writing data to shared memory has failed;" << std::endl; - return 0x6; } @@ -994,8 +890,7 @@ namespace HttpServer int code = EXIT_FAILURE; - do - { + do { this->controls.setProcess(false); this->controls.setRestart(false); @@ -1011,8 +906,7 @@ namespace HttpServer static void close_liseners(std::vector &liseners) { - for (auto &sock : liseners) - { + for (auto &sock : liseners) { sock.close(); } } @@ -1051,8 +945,7 @@ namespace HttpServer glob_mtx.lock(); - if (glob_mem.open(serverName) ) - { + if (glob_mem.open(serverName) ) { glob_mem.read(&pid, sizeof(pid) ); } @@ -1084,17 +977,14 @@ namespace HttpServer { std::string server_name; - for (int i = 1; i < argc; ++i) - { - if (argv[i] == ::strstr(argv[i], "--server-name=") ) - { + for (int i = 1; i < argc; ++i) { + if (argv[i] == ::strstr(argv[i], "--server-name=") ) { server_name = std::string(argv[i] + sizeof("--server-name=") - 1); break; } } - if (server_name.empty() ) - { + if (server_name.empty() ) { server_name = argv[0]; } @@ -1107,8 +997,7 @@ namespace HttpServer { const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); - if (1 < pid && System::sendSignal(pid, SIGUSR1) ) - { + if (1 < pid && System::sendSignal(pid, SIGUSR1) ) { return EXIT_SUCCESS; } @@ -1119,8 +1008,7 @@ namespace HttpServer { const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); - if (1 < pid && System::sendSignal(pid, SIGTERM) ) - { + if (1 < pid && System::sendSignal(pid, SIGTERM) ) { return EXIT_SUCCESS; } @@ -1131,11 +1019,10 @@ namespace HttpServer { const System::native_processid_type pid = Server::getServerProcessId(get_server_name(argc, argv) ); - if (1 < pid && System::sendSignal(pid, SIGUSR2) ) - { + if (1 < pid && System::sendSignal(pid, SIGUSR2) ) { return EXIT_SUCCESS; } return EXIT_FAILURE; } -}; +} diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp index 5b1e56d..5ef4991 100644 --- a/src/server/protocol/ServerHttp2.cpp +++ b/src/server/protocol/ServerHttp2.cpp @@ -13,8 +13,7 @@ namespace HttpServer } - void ServerHttp2::close() - { + void ServerHttp2::close() { this->sock.close(); } @@ -32,8 +31,7 @@ namespace HttpServer { auto it = streams.find(streamId); - if (streams.end() != it) - { + if (streams.end() != it) { return it->second; } @@ -56,18 +54,15 @@ namespace HttpServer static Http2::ErrorCode parseHttp2Data(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) { - if (0 == meta.stream_id) - { + if (0 == meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } - if (Http2::StreamState::OPEN != stream.state) - { + if (Http2::StreamState::OPEN != stream.state) { return Http2::ErrorCode::STREAM_CLOSED; } - if (stream.window_size_inc <= 0) - { + if (stream.window_size_inc <= 0) { return Http2::ErrorCode::FLOW_CONTROL_ERROR; } @@ -77,8 +72,7 @@ namespace HttpServer { padding = *src; - if (padding >= meta.length) - { + if (padding >= meta.length) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -134,8 +128,7 @@ namespace HttpServer { padding = *src; - if (padding >= meta.length) - { + if (padding >= meta.length) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -155,8 +148,7 @@ namespace HttpServer src += sizeof(uint8_t); } - if (false == HPack::unpack(src, end - src - padding, stream) ) - { + if (HPack::unpack(src, end - src - padding, stream) == false) { return Http2::ErrorCode::COMPRESSION_ERROR; } @@ -165,27 +157,23 @@ namespace HttpServer static Http2::ErrorCode parseHttp2rstStream(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) { - if (Http2::StreamState::IDLE == stream.state) - { + if (Http2::StreamState::IDLE == stream.state) { return Http2::ErrorCode::PROTOCOL_ERROR; } stream.state = Http2::StreamState::CLOSED; - if (0 == meta.stream_id) - { + if (0 == meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } - if (sizeof(uint32_t) != meta.length) - { + if (sizeof(uint32_t) != meta.length) { return Http2::ErrorCode::FRAME_SIZE_ERROR; } const Http2::ErrorCode error_code = static_cast(::ntohl(*reinterpret_cast(src) ) ); - if (Http2::ErrorCode::NO_ERROR != error_code) - { + if (Http2::ErrorCode::NO_ERROR != error_code) { // DEBUG } @@ -194,18 +182,15 @@ namespace HttpServer static Http2::ErrorCode parseHttp2Settings(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) { - if (0 != meta.stream_id) - { + if (0 != meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } - if (meta.length % (sizeof(uint16_t) + sizeof(uint32_t) ) ) - { + if (meta.length % (sizeof(uint16_t) + sizeof(uint32_t) ) ) { return Http2::ErrorCode::FRAME_SIZE_ERROR; } - if (Http2::StreamState::OPEN != stream.state) - { + if (Http2::StreamState::OPEN != stream.state) { stream.state = Http2::StreamState::OPEN; } @@ -229,8 +214,7 @@ namespace HttpServer case Http2::ConnectionSetting::SETTINGS_ENABLE_PUSH: { - if (value > 1) - { + if (value > 1) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -245,8 +229,7 @@ namespace HttpServer case Http2::ConnectionSetting::SETTINGS_INITIAL_WINDOW_SIZE: { - if (value >= uint32_t(1 << 31) ) - { + if (value >= uint32_t(1 << 31) ) { return Http2::ErrorCode::FLOW_CONTROL_ERROR; } @@ -257,8 +240,7 @@ namespace HttpServer case Http2::ConnectionSetting::SETTINGS_MAX_FRAME_SIZE: { - if (value < (1 << 14) || value >= (1 << 24) ) - { + if (value < (1 << 14) || value >= (1 << 24) ) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -281,8 +263,7 @@ namespace HttpServer static Http2::ErrorCode parseHttp2GoAway(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) { - if (0 != meta.stream_id) - { + if (0 != meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -290,8 +271,7 @@ namespace HttpServer const uint32_t last_stream_id = ::ntohl(*reinterpret_cast(src) ); - if (last_stream_id > 0) - { + if (last_stream_id > 0) { } @@ -299,8 +279,7 @@ namespace HttpServer const Http2::ErrorCode error_code = static_cast(::ntohl(*reinterpret_cast(src) ) ); - if (Http2::ErrorCode::NO_ERROR != error_code) - { + if (Http2::ErrorCode::NO_ERROR != error_code) { } @@ -327,13 +306,11 @@ namespace HttpServer static Http2::ErrorCode parseHttp2Ping(Http2::FrameMeta &meta) { - if (0 != meta.stream_id) - { + if (0 != meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } - if (sizeof(uint64_t) != meta.length) - { + if (sizeof(uint64_t) != meta.length) { return Http2::ErrorCode::FRAME_SIZE_ERROR; } @@ -342,38 +319,30 @@ namespace HttpServer static Http2::ErrorCode parseHttp2WindowUpdate(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) { - if (Http2::StreamState::RESERVED == stream.state) - { + if (Http2::StreamState::RESERVED == stream.state) { return Http2::ErrorCode::PROTOCOL_ERROR; } - else if (Http2::StreamState::OPEN != stream.state) - { + else if (Http2::StreamState::OPEN != stream.state) { return Http2::ErrorCode::NO_ERROR; } - if (sizeof(uint32_t) != meta.length) - { + if (sizeof(uint32_t) != meta.length) { return Http2::ErrorCode::FRAME_SIZE_ERROR; } const uint32_t window_size_increment = ::ntohl(*reinterpret_cast(src) ); - if (0 == window_size_increment) - { + if (0 == window_size_increment) { return Http2::ErrorCode::PROTOCOL_ERROR; } - else if (window_size_increment >= uint32_t(1 << 31) ) - { + else if (window_size_increment >= uint32_t(1 << 31) ) { return Http2::ErrorCode::FLOW_CONTROL_ERROR; } - if (0 == meta.stream_id) - { + if (0 == meta.stream_id) { // TODO: update all streams stream.window_size_out += window_size_increment; - } - else - { + } else { stream.window_size_out += window_size_increment; } @@ -439,8 +408,7 @@ namespace HttpServer const long read_size = sock.nonblock_recv(buf.data(), buf.size(), timeout); - if (read_size != buf.size() ) - { + if (read_size != buf.size() ) { return false; } @@ -478,15 +446,13 @@ namespace HttpServer { if (read_size <= static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) { - if (read_size == static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) - { + if (read_size == static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) { read_size = 0; } read_size = sock.nonblock_recv(buf.data() + read_size, buf.size() - read_size, timeout); - if (read_size < static_cast(Http2::FRAME_HEADER_SIZE) ) - { + if (read_size < static_cast(Http2::FRAME_HEADER_SIZE) ) { return false; } } @@ -517,12 +483,9 @@ namespace HttpServer sendEmptySettings(this->sock, req.timeout, conn, Http2::FrameFlag::EMPTY); - if (false == getClientPreface(this->sock, req.timeout) ) - { + if (getClientPreface(this->sock, req.timeout) == false) { constexpr uint32_t last_stream_id = 0; - goAway(this->sock, req.timeout, conn, last_stream_id, Http2::ErrorCode::PROTOCOL_ERROR); - return this; } @@ -544,32 +507,26 @@ namespace HttpServer Http2::FrameMeta meta {}; long read_size = 0; - do - { - if (false == getNextHttp2FrameMeta(this->sock, req.timeout, buf, meta, read_size) ) - { + do { + if (getNextHttp2FrameMeta(this->sock, req.timeout, buf, meta, read_size) == false) { break; } const uint8_t *addr = reinterpret_cast(buf.data() ) + Http2::FRAME_HEADER_SIZE; const uint8_t *end = addr + meta.length; - if (meta.stream_id > last_stream_id) - { + if (meta.stream_id > last_stream_id) { last_stream_id = meta.stream_id; } Http2::IncStream &stream = getStreamData(streams, meta.stream_id, conn); - if (Http2::StreamState::CLOSED == stream.state) - { + if (Http2::StreamState::CLOSED == stream.state) { rstStream(this->sock, req.timeout, stream, Http2::ErrorCode::STREAM_CLOSED); - continue; } - if (meta.type != Http2::FrameType::CONTINUATION) - { + if (meta.type != Http2::FrameType::CONTINUATION) { stream.frame_type = meta.type; } @@ -591,8 +548,7 @@ namespace HttpServer { size_t update_size = conn.server_settings.initial_window_size + (dr->full_size - dr->recv_total) - stream.window_size_inc; - if (update_size > Http2::MAX_WINDOW_UPDATE) - { + if (update_size > Http2::MAX_WINDOW_UPDATE) { update_size = Http2::MAX_WINDOW_UPDATE; } @@ -616,10 +572,8 @@ namespace HttpServer stream.reserved = createDataReceiver(rd, this->settings.variants); - if (stream.reserved) - { + if (stream.reserved) { DataVariant::DataReceiver *dr = reinterpret_cast(stream.reserved); - dr->reserved = new std::string(); } } @@ -661,7 +615,6 @@ namespace HttpServer if (Http2::ErrorCode::NO_ERROR == result && false == (meta.flags & Http2::FrameFlag::ACK) ) { const uint64_t ping_data = *reinterpret_cast(addr); - ping(this->sock, req.timeout, conn, ping_data); } @@ -711,18 +664,16 @@ namespace HttpServer } while (Http2::StreamState::CLOSED != primary.state); - while (conn.sync.completed.load() < streams_process_count) - { + while (conn.sync.completed.load() < streams_process_count) { conn.sync.event.wait(); } goAway(this->sock, req.timeout, conn, last_stream_id, Http2::ErrorCode::NO_ERROR); - for (auto &pair : streams) - { + for (auto &pair : streams) { destroyDataReceiver(pair.second.reserved); } return this; } -}; +} diff --git a/src/server/protocol/ServerHttp2.h b/src/server/protocol/ServerHttp2.h index 54658d4..6910788 100644 --- a/src/server/protocol/ServerHttp2.h +++ b/src/server/protocol/ServerHttp2.h @@ -18,4 +18,4 @@ namespace HttpServer virtual ServerProtocol *process() override; virtual void close() override; }; -}; +} diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index afd4ca2..b725d37 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -82,8 +82,9 @@ namespace Socket do { sock.nonblock_send_sync(); + send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); } - while (GNUTLS_E_AGAIN == (send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size) ) ); + while (GNUTLS_E_AGAIN == send_size); if (send_size < 0) { return send_size; @@ -116,10 +117,10 @@ namespace Socket long result; do { - sock.nonblock_recv_sync(); + sock.nonblock_recv_sync(timeout); result = ::gnutls_record_recv(this->session, buf, length); } - while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED); + while (result == GNUTLS_E_INTERRUPTED); return result; } diff --git a/src/socket/List.cpp b/src/socket/List.cpp index 8df0b9a..1746a79 100644 --- a/src/socket/List.cpp +++ b/src/socket/List.cpp @@ -34,8 +34,7 @@ namespace Socket #endif } - List::~List() noexcept - { + List::~List() noexcept { this->destroy(); } @@ -46,8 +45,7 @@ namespace Socket #ifdef WIN32 this->obj_list = (HANDLE) 1; - if (startListSize > 0) - { + if (startListSize > 0) { this->poll_events.reserve(startListSize); } @@ -55,13 +53,11 @@ namespace Socket #elif POSIX this->obj_list = ::epoll_create(startListSize); - if (this->obj_list == ~0) - { + if (this->obj_list == ~0) { return false; } - if (startListSize > 0) - { + if (startListSize > 0) { this->epoll_events.reserve(startListSize); } @@ -101,8 +97,7 @@ namespace Socket bool List::addSocket(const Socket &sock) noexcept { - if (false == this->is_created() ) - { + if (this->is_created() == false) { return false; } @@ -124,8 +119,7 @@ namespace Socket const int result = ::epoll_ctl(this->obj_list, EPOLL_CTL_ADD, sock.get_handle(), &event); - if (result == ~0) - { + if (result == ~0) { return false; } @@ -139,16 +133,13 @@ namespace Socket bool List::removeSocket(const Socket &sock) noexcept { - if (false == this->is_created() ) - { + if (this->is_created() == false) { return false; } #ifdef WIN32 - for (size_t i = 0; i < poll_events.size(); ++i) - { - if (sock.get_handle() == poll_events[i].fd) - { + for (size_t i = 0; i < poll_events.size(); ++i) { + if (sock.get_handle() == poll_events[i].fd) { poll_events.erase(poll_events.begin() + i); return true; } @@ -158,8 +149,7 @@ namespace Socket #elif POSIX const int result = ::epoll_ctl(this->obj_list, EPOLL_CTL_DEL, sock.get_handle(), nullptr); - if (result == ~0) - { + if (result == ~0) { return false; } @@ -178,8 +168,7 @@ namespace Socket #ifdef WIN32 const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), ~0); - if (SOCKET_ERROR == count) - { + if (SOCKET_ERROR == count) { return false; } @@ -191,12 +180,10 @@ namespace Socket { System::native_socket_type client_socket = ~0; - do - { + do { client_socket = ::accept(event.fd, static_cast(nullptr), static_cast(nullptr) ); - if (~0 != client_socket) - { + if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); } } @@ -208,8 +195,7 @@ namespace Socket #elif POSIX const int count = ::epoll_wait(this->obj_list, this->epoll_events.data(), this->epoll_events.size(), ~0); - if (count == ~0) - { + if (count == ~0) { return false; } @@ -221,12 +207,10 @@ namespace Socket { System::native_socket_type client_socket = ~0; - do - { + do { client_socket = ::accept(event.data.fd, static_cast(nullptr), static_cast(nullptr) ); - if (~0 != client_socket) - { + if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); } } @@ -250,8 +234,7 @@ namespace Socket #ifdef WIN32 const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), ~0); - if (SOCKET_ERROR == count) - { + if (SOCKET_ERROR == count) { return false; } @@ -263,15 +246,13 @@ namespace Socket { System::native_socket_type client_socket = ~0; - do - { + do { ::sockaddr_in client_addr {}; socklen_t client_addr_len = sizeof(client_addr); client_socket = ::accept(event.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); - if (~0 != client_socket) - { + if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); socketsAddress.emplace_back(client_addr); } @@ -284,8 +265,7 @@ namespace Socket #elif POSIX const int count = ::epoll_wait(this->obj_list, this->epoll_events.data(), this->epoll_events.size(), ~0); - if (count == ~0) - { + if (count == ~0) { return false; } @@ -297,15 +277,13 @@ namespace Socket { System::native_socket_type client_socket = ~0; - do - { + do { ::sockaddr_in client_addr {}; socklen_t client_addr_len = sizeof(client_addr); client_socket = ::accept(event.data.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); - if (~0 != client_socket) - { + if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); socketsAddress.emplace_back(client_addr); } @@ -325,16 +303,14 @@ namespace Socket bool List::recv(std::vector &sockets, std::vector &disconnected, std::chrono::milliseconds timeout) const noexcept { - if (false == this->is_created() ) - { + if (this->is_created() == false) { return false; } #ifdef WIN32 const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), static_cast<::INT>(timeout.count() ) ); - if (SOCKET_ERROR == count) - { + if (SOCKET_ERROR == count) { return false; } @@ -342,12 +318,10 @@ namespace Socket { const WSAPOLLFD &event = this->poll_events[i]; - if (event.revents & POLLRDNORM) - { + if (event.revents & POLLRDNORM) { sockets.emplace_back(Socket(event.fd) ); } - else if (event.revents & POLLHUP) - { + else if (event.revents & POLLHUP) { disconnected.emplace_back(Socket(event.fd) ); } } @@ -356,8 +330,7 @@ namespace Socket #elif POSIX const int count = ::epoll_wait(this->obj_list, this->epoll_events.data(), this->epoll_events.size(), timeout.count() ); - if (count == ~0) - { + if (count == ~0) { return false; } @@ -365,12 +338,10 @@ namespace Socket { const epoll_event &event = this->epoll_events[i]; - if (event.events & EPOLLIN) - { + if (event.events & EPOLLIN) { sockets.emplace_back(Socket(event.data.fd) ); } - else if (event.events & EPOLLRDHUP) - { + else if (event.events & EPOLLRDHUP) { disconnected.emplace_back(Socket(event.data.fd) ); } } @@ -380,4 +351,4 @@ namespace Socket #error "Undefine platform" #endif } -}; +} diff --git a/src/socket/List.h b/src/socket/List.h index 650447f..19f83b0 100644 --- a/src/socket/List.h +++ b/src/socket/List.h @@ -40,4 +40,4 @@ namespace Socket bool recv(std::vector &sockets, std::vector &errors, std::chrono::milliseconds timeout = std::chrono::milliseconds(~0) ) const noexcept; }; -}; +} diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index f787e31..2edb3d0 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -289,7 +289,7 @@ namespace Socket return recv_len; } - void Socket::nonblock_recv_sync() const noexcept + void Socket::nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept { #ifdef WIN32 WSAPOLLFD event = { @@ -298,7 +298,7 @@ namespace Socket 0 }; - ::WSAPoll(&event, 1, ~0); + ::WSAPoll(&event, 1, timeout.count() ); #elif POSIX struct ::pollfd event = { this->socket_handle, @@ -306,7 +306,7 @@ namespace Socket 0 }; - ::poll(&event, 1, ~0); + ::poll(&event, 1, timeout.count() ); #else #error "Undefine platform" #endif diff --git a/src/socket/Socket.h b/src/socket/Socket.h index 0952e23..a2d0151 100644 --- a/src/socket/Socket.h +++ b/src/socket/Socket.h @@ -51,7 +51,7 @@ namespace Socket long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; - void nonblock_recv_sync() const noexcept; + void nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept; long send(const std::string &buf) const noexcept; long send(const void *buf, const size_t length) const noexcept; diff --git a/src/utils/Event.cpp b/src/utils/Event.cpp index 35771ba..d768a8e 100644 --- a/src/utils/Event.cpp +++ b/src/utils/Event.cpp @@ -17,7 +17,7 @@ namespace Utils do { this->cv.wait(lck); } - while (false == this->signaled.load() ); + while (this->signaled.load() == false); } if (false == this->manually) { @@ -32,7 +32,7 @@ namespace Utils if (this->signaled.load() == false) { std::unique_lock lck(this->mtx); - is_timeout = false == this->cv.wait_for(lck, ms, [this] { return this->notifed(); } ); + is_timeout = this->cv.wait_for(lck, ms, [this] { return this->notifed(); } ) == false; } if (false == this->manually) { @@ -55,7 +55,7 @@ namespace Utils break; } } - while (false == this->signaled.load() ); + while (this->signaled.load() == false); } if (false == this->manually) { From b9c1a2801013652cabf734928e0b18624f5941a0 Mon Sep 17 00:00:00 2001 From: awwit Date: Mon, 13 Nov 2017 03:19:20 +0300 Subject: [PATCH 11/22] Trying fix intrrution of data reception. Again --- src/socket/AdapterTls.cpp | 11 +++++------ src/socket/Socket.cpp | 6 +++--- src/socket/Socket.h | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index b725d37..f284bb7 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -76,8 +76,6 @@ namespace Socket record_size = length - total; } - // const long send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); - long send_size = 0; do { @@ -110,17 +108,18 @@ namespace Socket long AdapterTls::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { - // ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); - Socket sock(this->get_handle() ); long result; do { - sock.nonblock_recv_sync(timeout); + if (sock.nonblock_recv_sync(timeout) == false) { + break; + } + result = ::gnutls_record_recv(this->session, buf, length); } - while (result == GNUTLS_E_INTERRUPTED); + while (GNUTLS_E_AGAIN == result ||GNUTLS_E_INTERRUPTED == result); return result; } diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index 2edb3d0..96bd194 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -289,7 +289,7 @@ namespace Socket return recv_len; } - void Socket::nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept + bool Socket::nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept { #ifdef WIN32 WSAPOLLFD event = { @@ -298,7 +298,7 @@ namespace Socket 0 }; - ::WSAPoll(&event, 1, timeout.count() ); + return ::WSAPoll(&event, 1, timeout.count() ) == 1; #elif POSIX struct ::pollfd event = { this->socket_handle, @@ -306,7 +306,7 @@ namespace Socket 0 }; - ::poll(&event, 1, timeout.count() ); + return ::poll(&event, 1, timeout.count() ) == 1; #else #error "Undefine platform" #endif diff --git a/src/socket/Socket.h b/src/socket/Socket.h index a2d0151..e0bfc6c 100644 --- a/src/socket/Socket.h +++ b/src/socket/Socket.h @@ -51,7 +51,7 @@ namespace Socket long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; - void nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept; + bool nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept; long send(const std::string &buf) const noexcept; long send(const void *buf, const size_t length) const noexcept; From b0f7775f58f4ca05d38c1b4b5e6040ee754f3b81 Mon Sep 17 00:00:00 2001 From: awwit Date: Mon, 13 Nov 2017 03:41:31 +0300 Subject: [PATCH 12/22] Set result to failed when timeout --- src/socket/AdapterTls.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index f284bb7..b63bf73 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -114,12 +114,14 @@ namespace Socket do { if (sock.nonblock_recv_sync(timeout) == false) { + // Timeout + result = -1; break; } result = ::gnutls_record_recv(this->session, buf, length); } - while (GNUTLS_E_AGAIN == result ||GNUTLS_E_INTERRUPTED == result); + while (GNUTLS_E_AGAIN == result || GNUTLS_E_INTERRUPTED == result); return result; } From 9b10936f8c5affbce4a04f536e8b00d30575d9da Mon Sep 17 00:00:00 2001 From: wuyougongzi <1211385701@qq.com> Date: Thu, 23 Nov 2017 22:39:44 +0800 Subject: [PATCH 13/22] fix make error in ubuntu system --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5ff0f59..c0e28ab 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ $(BUILDDIR): $(MKDIR) $@ $(EXECUTABLE): $(OBJECTS) - $(CXX) $(LDFLAGS) $(OBJECTS) -o $@ + $(CXX) $(OBJECTS) -o $@ $(LDFLAGS) $(OBJECTS) : $(OBJDIR)/%.o : $(SOURCEDIR)/%.cpp @$(MKDIR) $(dir $@) From 5e7425317384cc01c47f9d416a5f325b916e29b0 Mon Sep 17 00:00:00 2001 From: wuyougongzi <1211385701@qq.com> Date: Sun, 3 Dec 2017 22:20:39 +0800 Subject: [PATCH 14/22] use a state machine to parse 'mimes.conf' function --- src/server/config/ConfigParser.cpp | 98 ++++++++++++++++++++---------- src/server/config/ConfigParser.h | 7 +++ 2 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index 7ae4490..5bd3d4b 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -412,59 +412,93 @@ namespace HttpServer const std::string str_buf(buf.cbegin(), buf.cend() ); size_t cur_pos = 0; - size_t end_pos = str_buf.find('\n', cur_pos); - while (std::string::npos != end_pos) - { - cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - size_t delimiter = str_buf.find_first_of(whitespace, cur_pos); + ConfigFileDataState dataState = CONFIGFILEDATASNONE; - if (delimiter < end_pos) + while (std::string::npos != cur_pos) + { + //std::cout << "parse Mine dataState: " << dataState << std::endl; + switch(dataState) { - std::string mime_type = str_buf.substr(cur_pos, delimiter - cur_pos); - - if ('#' != mime_type.front() ) - { - delimiter = str_buf.find_first_not_of(whitespace, delimiter); + case CONFIGFILEDATASNONE: - if (delimiter < end_pos) + case CONFIGFILEDATASNOTE: + { - std::string ext = str_buf.substr(delimiter, end_pos - delimiter); - - delimiter = ext.find_first_of(whitespace); + cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); + if (cur_pos < 0) + { + return true; + } - if (std::string::npos != delimiter) + //start from next line + if ('#' == buf[cur_pos]) { - for (size_t ext_pos = 0; std::string::npos != ext_pos; ) + //last line + cur_pos = str_buf.find_first_of("\n", cur_pos); + if (cur_pos < 0) { - std::string ext_unit = ext.substr(ext_pos, std::string::npos != delimiter ? delimiter - ext_pos : std::string::npos); - - if (false == ext_unit.empty() ) - { - mimes_types.emplace(std::move(ext_unit), mime_type); - } + return true; + } - ext_pos = ext.find_first_not_of(whitespace, delimiter); + cur_pos += 1; - delimiter = ext.find_first_of(whitespace, ext_pos); - } + dataState = CONFIGFILEDATASNOTE; + } + else if ('\n' == buf[cur_pos]) + { + cur_pos += 1; + dataState = CONFIGFILEDATASNONE; } else { - mimes_types.emplace(std::move(ext), std::move(mime_type) ); + dataState = CONFIGFILEDATASTYPE; } } - } - } + break; + case CONFIGFILEDATASTYPE: + { + size_t delimiter = str_buf.find_first_of("\n", cur_pos); + std::string strLine = str_buf.substr(cur_pos, delimiter - cur_pos); + + //get mimetypes + { + size_t ext_pos = strLine.find_first_of(whitespace, 0); + if (ext_pos < 0) + { + return false; + } + std::string strExt = strLine.substr(0, ext_pos); + size_t extUnitPos = strLine.find_first_not_of(whitespace, ext_pos); + std::string strExtUnit = strLine.substr(extUnitPos, strLine.length() - extUnitPos); - cur_pos = end_pos + 1; + mimes_types.emplace(std::move(strExt), std::move(strExtUnit)); - end_pos = str_buf.find('\n', cur_pos); - } + //std::cout << strExt << " " << strExtUnit << std::endl; + } + + //change line + cur_pos = str_buf.find_first_of("\n", cur_pos); + if (cur_pos < 0) + { + return true; + } + cur_pos += 1; + + dataState = CONFIGFILEDATASNONE; + + } + break; + default: + break; + } + } + return true; } + static size_t findBlockEnd(const std::string &str_buf, size_t str_pos) { size_t pos = str_buf.find('}', str_pos); diff --git a/src/server/config/ConfigParser.h b/src/server/config/ConfigParser.h index 5614237..c9b0e23 100644 --- a/src/server/config/ConfigParser.h +++ b/src/server/config/ConfigParser.h @@ -7,6 +7,13 @@ namespace HttpServer { + enum ConfigFileDataState + { + CONFIGFILEDATASNONE = 0, + CONFIGFILEDATASNOTE, //comment + CONFIGFILEDATASTYPE, + + }; class ConfigParser { private: From 2bda69185757358f8a41af77de1d59f7e861a88c Mon Sep 17 00:00:00 2001 From: wuyougongzi <1211385701@qq.com> Date: Tue, 12 Dec 2017 23:09:45 +0800 Subject: [PATCH 15/22] change parse 'main.conf' ways using state machine --- src/server/config/ConfigParser.cpp | 303 ++++++++++++++++++----------- src/server/config/ConfigParser.h | 10 +- 2 files changed, 201 insertions(+), 112 deletions(-) diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index 5bd3d4b..fee0df6 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -7,6 +7,8 @@ #include #include +const long FILESIZE = 1024 * 1024; + namespace HttpServer { /** @@ -14,6 +16,7 @@ namespace HttpServer */ bool ConfigParser::includeConfigFile(const std::string &fileName, std::string &strBuf, const std::size_t offset) { + std:: cout << "config file name: " << fileName << std::endl; std::ifstream file(fileName); if ( ! file) @@ -29,7 +32,7 @@ namespace HttpServer std::streamsize file_size = file.tellg(); file.seekg(0, std::ifstream::beg); - constexpr std::streamsize file_size_max = 2 * 1024 * 1024; + constexpr std::streamsize file_size_max = 2 * FILESIZE; if (file_size_max < file_size) { @@ -426,7 +429,7 @@ namespace HttpServer { cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - if (cur_pos < 0) + if (cur_pos == std::string::npos) { return true; } @@ -436,7 +439,7 @@ namespace HttpServer { //last line cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos < 0) + if (cur_pos == std::string::npos) { return true; } @@ -464,7 +467,7 @@ namespace HttpServer //get mimetypes { size_t ext_pos = strLine.find_first_of(whitespace, 0); - if (ext_pos < 0) + if (ext_pos == std::string::npos) { return false; } @@ -479,7 +482,7 @@ namespace HttpServer //change line cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos < 0) + if (cur_pos == std::string::npos) { return true; } @@ -534,6 +537,7 @@ namespace HttpServer */ bool ConfigParser::loadConfig(const std::string &conf_file_name, ServerSettings &settings, std::vector &modules) { + //std::cout << "enter loadCofig" << std::endl; std::string str_buf; if (false == includeConfigFile(conf_file_name, str_buf) ) @@ -550,163 +554,238 @@ namespace HttpServer const std::string whitespace(" \t\n\v\f\r"); size_t cur_pos = 0; - size_t end_pos = str_buf.find(';', cur_pos); - size_t block_pos = 0; - while (std::string::npos != end_pos) - { - block_pos = str_buf.find('{', cur_pos); + ConfigFileDataState dataState = CONFIGFILEDATASNONE; + InBlockDataState inBlockState = INBLOCKNONE; + + std::unordered_multimap app; - if (end_pos < block_pos) + int iStatus = 0; + while (std::string::npos != cur_pos) + { + //std::cout << "parse Mine dataState: " << dataState << std::endl; + if (iStatus == 1 || iStatus == 2) { - cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - size_t delimiter = str_buf.find_first_of(whitespace, cur_pos); + break; + } - if (delimiter < end_pos) - { - std::string param_name = str_buf.substr(cur_pos, delimiter - cur_pos); + switch (dataState) + { + case CONFIGFILEDATASNONE: - if ('#' != param_name.front() ) + case CONFIGFILEDATASNOTE: { - cur_pos = str_buf.find_first_not_of(whitespace, delimiter + 1); - delimiter = str_buf.find_last_not_of(whitespace, end_pos); + cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); + if (cur_pos == std::string::npos) + { + iStatus = 1; + } - std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos); + //start from next line + if ('#' == str_buf[cur_pos]) + { + //last line + cur_pos = str_buf.find_first_of("\n", cur_pos); + if (cur_pos == std::string::npos) + { + iStatus = 1; + } + + cur_pos += 1; - if ("include" == param_name) + dataState = CONFIGFILEDATASNOTE; + } + else if ('\n' == str_buf[cur_pos]) { - this->includeConfigFile(param_value, str_buf, end_pos + 1); + cur_pos += 1; + dataState = CONFIGFILEDATASNONE; } else { - global.emplace(std::move(param_name), std::move(param_value) ); + dataState = CONFIGFILEDATASTYPE; } } - else // if comment line + break; + case CONFIGFILEDATASTYPE: { - end_pos = str_buf.find_first_of("\r\n", cur_pos); - } - } - - cur_pos = end_pos; - - if (std::string::npos != cur_pos) - { - ++cur_pos; - } - } - else if (std::string::npos != block_pos) - { - cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - size_t delimiter = str_buf.find_first_of(whitespace, cur_pos); + //in app and main config file "; and \n " has different meaning + size_t delimiter = str_buf.find_first_of("\n", cur_pos); + //std:: cout << "str_buf.length()" << str_buf.length() << " delimiter " << delimiter + // << " cur_pos " << cur_pos << std::endl; + //std::cout << "npos: " << std::string::npos << std::endl; + if (delimiter == std::string::npos) + { + iStatus = 1; + } - const std::string block_type_name = str_buf.substr(cur_pos, delimiter - cur_pos); + std::string strLine = str_buf.substr(cur_pos, delimiter - cur_pos); + //std::cout << "strLine: " << strLine << std::endl; + do { + size_t ext_pos = strLine.find_first_of(whitespace, 0); + if (ext_pos == std::string::npos) + { + iStatus = 2; + } + std::string param_name = strLine.substr(0, ext_pos); + size_t extUnitPos = strLine.find_first_not_of(whitespace, ext_pos); - if ('#' != block_type_name.front() ) - { - delimiter = str_buf.find_first_not_of(whitespace, delimiter); + //mines the last ";" + std::string param_value; + if (strLine[strLine.length() - 1] == ';') + { + param_value = strLine.substr(extUnitPos, strLine.length() - extUnitPos - 1); + } + else + { + param_value = strLine.substr(extUnitPos, strLine.length() - extUnitPos); + } - cur_pos = block_pos + 1; - size_t block_end = findBlockEnd(str_buf, cur_pos); + //std::cout << param_name << " " << param_value << std::endl; + if (param_name == "server" && param_value == "{") + { + dataState = CONFIGFILEDATAINBLOCK; + inBlockState = INBLOCKNONE; + } + else if(param_name == "include") + { + this->loadConfig(param_value, settings, modules); + dataState = CONFIGFILEDATASNONE; + } + else + { + global.emplace(std::move(param_name), std::move(param_value) ); + dataState = CONFIGFILEDATASNONE; + } + break; + } while (1); - if (std::string::npos == block_end) - { - std::cout << "Error: symbol '}' after '" << block_type_name << "' has not been found;" << std::endl - << "Parsing config aborted;" << std::endl; + //change line + cur_pos = str_buf.find_first_of("\n", cur_pos); + if (cur_pos == std::string::npos) + { + iStatus = 1; + } - return false; + cur_pos += 1; } - else if (delimiter == block_pos) + break; + case CONFIGFILEDATAINBLOCK: { - if ("server" == block_type_name) + switch(inBlockState) { - std::unordered_multimap app; - - end_pos = str_buf.find(';', cur_pos); - - while (block_end > end_pos) - { - cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - delimiter = str_buf.find_first_of(whitespace, cur_pos); - - if (delimiter < end_pos) + case INBLOCKNONE: + case INBLOCKNOTE: { - std::string param_name = str_buf.substr(cur_pos, delimiter - cur_pos); + cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); + if (cur_pos == std::string::npos) + { + iStatus = 1; + } - if ('#' != param_name.front() ) + //start from next line + if ('#' == str_buf[cur_pos]) { - cur_pos = str_buf.find_first_not_of(whitespace, delimiter + 1); - delimiter = str_buf.find_last_not_of(whitespace, end_pos); + //last line + cur_pos = str_buf.find_first_of("\n", cur_pos); + if (cur_pos == std::string::npos) + { + iStatus = 1; + } - std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos); + cur_pos += 1; - if ("include" == param_name) + inBlockState = INBLOCKNOTE; + } + else if ('\n' == str_buf[cur_pos]) + { + cur_pos += 1; + inBlockState = INBLOCKNONE; + } + else + { + inBlockState = INBLOCKTPYE; + } + } + break; + case INBLOCKTPYE: + { + size_t delimiter = str_buf.find_first_of("\n", cur_pos); + std::string strLine = str_buf.substr(cur_pos, delimiter - cur_pos); + + //std::cout << "inblockLineStr: " << strLine << std::endl; + + do { + if ("}" == strLine) { - cur_pos = end_pos + 1; - this->includeConfigFile(param_value, str_buf, cur_pos); - block_end = findBlockEnd(str_buf, cur_pos); + dataState = CONFIGFILEDATASNOTE; + applications.push_back(app); + app.clear(); + break; } - else + + size_t ext_pos = strLine.find_first_of(whitespace, 0); + if (ext_pos == std::string::npos) { - app.emplace(std::move(param_name), std::move(param_value) ); + iStatus = 2; } - } - else // if comment line + std::string strExt = strLine.substr(0, ext_pos); + size_t extUnitPos = strLine.find_first_not_of(whitespace, ext_pos); + std::string strExtUnit = strLine.substr(extUnitPos, strLine.length() - extUnitPos - 1); + + //std::cout << "inblockLine " << strExt << " " << strExtUnit << std::endl; + app.emplace(std::move(strExt), std::move(strExtUnit)); + + break; + + } while(1); + + //change line + cur_pos = str_buf.find_first_of("\n", cur_pos); + if (cur_pos == std::string::npos) { - end_pos = str_buf.find_first_of("\r\n", cur_pos); + iStatus = 2; } - } - - if (std::string::npos != end_pos) - { - ++end_pos; - } - cur_pos = end_pos; + cur_pos += 1; - end_pos = str_buf.find(';', cur_pos); - } - - applications.emplace_back(std::move(app) ); - } - else - { - std::cout << "Warning: " << block_type_name << " - unknown block type;" << std::endl; + inBlockState = INBLOCKNONE; + } + break; + default: + break; } } - else - { - std::cout << "Warning: after " << block_type_name << " expected '{' ;" << std::endl; - } - - cur_pos = block_end; - - if (std::string::npos != cur_pos) - { - ++cur_pos; - } - } - else // if comment line - { - cur_pos = str_buf.find_first_of("\r\n", cur_pos); - } + break; + default: + break; } - - end_pos = str_buf.find(';', cur_pos); } + // std::cout << "app value start" << std::endl; + // for (size_t i = 0; i < applications.size(); ++i) + // { + // std::unordered_multimap sa = applications[i]; + // for (auto iter = sa.begin(); iter != sa.end(); iter++) + // { + // std::cout << iter->first << " " << iter->second << std::endl; + // } + + // } + // std::cout << "app value end" << std::endl; + auto const it_mimes = global.find("mimes"); if (global.cend() != it_mimes) { this->parseMimes(it_mimes->second, mimes_types); + //std::cout << "parse mimes end" << std::endl; } else { std::cout << "Warning: mime types file is not set in configuration;" << std::endl; } - if (false == applications.empty() ) + if (!applications.empty()) { auto const it_default_temp_dir = global.find("default_temp_dir"); @@ -724,6 +803,7 @@ namespace HttpServer for (auto const &app : applications) { this->addApplication(app, defaults, modules, apps_tree); + std::cout << "addApplication end" << std::endl; } } @@ -732,6 +812,7 @@ namespace HttpServer std::cout << "Notice: server does not contain applications;" << std::endl; } - return true; + //std::cout << "out loadCofig" << std::endl; + return iStatus == 1; } }; diff --git a/src/server/config/ConfigParser.h b/src/server/config/ConfigParser.h index c9b0e23..c501d5d 100644 --- a/src/server/config/ConfigParser.h +++ b/src/server/config/ConfigParser.h @@ -12,8 +12,16 @@ namespace HttpServer CONFIGFILEDATASNONE = 0, CONFIGFILEDATASNOTE, //comment CONFIGFILEDATASTYPE, - + //CONFIGFILEDATAGLOBAL, + CONFIGFILEDATAINBLOCK, //in block + }; + enum InBlockDataState + { + INBLOCKNONE, //in block none + INBLOCKNOTE, //in block note + INBLOCKTPYE, //in block type }; + class ConfigParser { private: From 845a585a2daf238d7ae6c90948d1eb06d0094268 Mon Sep 17 00:00:00 2001 From: awwit Date: Sat, 13 Jan 2018 18:04:55 +0300 Subject: [PATCH 16/22] Fixed bug in function `getPackNumberSize` (which caused the crash application in one case) --- Makefile | 2 +- src/utils/Utils.cpp | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index c0e28ab..3e65d7d 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ $(BUILDDIR): $(MKDIR) $@ $(EXECUTABLE): $(OBJECTS) - $(CXX) $(OBJECTS) -o $@ $(LDFLAGS) + $(CXX) $(OBJECTS) -o $@ $(LDFLAGS) $(OBJECTS) : $(OBJDIR)/%.o : $(SOURCEDIR)/%.cpp @$(MKDIR) $(dir $@) diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index 77d8242..4ddc424 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -233,9 +233,14 @@ namespace Utils return binToHexString(&time, sizeof(time) ); } + constexpr uint8_t PACK_NUMBER_SIZE_BYTE = 252; + constexpr uint8_t PACK_NUMBER_SIZE_16 = 253; + constexpr uint8_t PACK_NUMBER_SIZE_32 = 254; + constexpr uint8_t PACK_NUMBER_SIZE_MAX = 255; + size_t getPackNumberSize(const size_t number) noexcept { - if (number <= 253) { + if (number <= PACK_NUMBER_SIZE_BYTE) { return sizeof(uint8_t); } else if (number <= std::numeric_limits::max() ) { @@ -259,13 +264,13 @@ namespace Utils uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept { - if (number <= 252) { + if (number <= PACK_NUMBER_SIZE_BYTE) { *dest = number; dest += sizeof(uint8_t); } else if (number <= std::numeric_limits::max() ) { - *dest = 253; + *dest = PACK_NUMBER_SIZE_16; dest += sizeof(uint8_t); @@ -274,7 +279,7 @@ namespace Utils dest += sizeof(uint16_t); } else if (number <= std::numeric_limits::max() ) { - *dest = 254; + *dest = PACK_NUMBER_SIZE_32; dest += sizeof(uint8_t); @@ -282,7 +287,7 @@ namespace Utils dest += sizeof(uint32_t); } else { - *dest = 255; + *dest = PACK_NUMBER_SIZE_MAX; dest += sizeof(uint8_t); @@ -308,24 +313,24 @@ namespace Utils void packNumber(std::vector &buf, const size_t number) { - if (number <= 252) { + if (number <= PACK_NUMBER_SIZE_BYTE) { buf.emplace_back(number); } else if (number <= std::numeric_limits::max() ) { - buf.emplace_back(253); + buf.emplace_back(PACK_NUMBER_SIZE_16); buf.resize(buf.size() + sizeof(uint16_t) ); *reinterpret_cast(buf.data() + buf.size() - sizeof(uint16_t) ) = static_cast(number); } else if (number <= std::numeric_limits::max() ) { - buf.emplace_back(254); + buf.emplace_back(PACK_NUMBER_SIZE_32); buf.resize(buf.size() + sizeof(uint32_t) ); *reinterpret_cast(buf.data() + buf.size() - sizeof(uint32_t) ) = static_cast(number); } else { - buf.emplace_back(255); + buf.emplace_back(PACK_NUMBER_SIZE_MAX); buf.resize(buf.size() + sizeof(size_t) ); @@ -353,14 +358,14 @@ namespace Utils src += sizeof(uint8_t); - if (*number <= 252) { + if (*number <= PACK_NUMBER_SIZE_BYTE) { } - else if (*number == 253) { + else if (*number == PACK_NUMBER_SIZE_16) { *number = *reinterpret_cast(src); src += sizeof(uint16_t); } - else if (*number == 254) { + else if (*number == PACK_NUMBER_SIZE_32) { *number = *reinterpret_cast(src); src += sizeof(uint32_t); } else { From 1796b812e837bd96cd2157bec93bd224e048d908 Mon Sep 17 00:00:00 2001 From: awwit Date: Sat, 20 Jan 2018 21:35:10 +0300 Subject: [PATCH 17/22] Fixed code of parsing configs. Started convert to state machine. Removed previous version of code. --- src/server/config/ConfigParser.cpp | 582 ++++++++++++----------------- src/server/config/ConfigParser.h | 19 +- 2 files changed, 240 insertions(+), 361 deletions(-) diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index fee0df6..20ed3e0 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -7,24 +7,31 @@ #include #include -const long FILESIZE = 1024 * 1024; - namespace HttpServer { + enum class ConfigMimeState { + NONE, + TYPE + }; + + enum class ConfigState { + NONE, + PARAM, + BLOCK + }; + + constexpr long FILESIZE = 2 * 1024 * 1024; + /** * Config - include file */ bool ConfigParser::includeConfigFile(const std::string &fileName, std::string &strBuf, const std::size_t offset) { - std:: cout << "config file name: " << fileName << std::endl; std::ifstream file(fileName); - if ( ! file) - { + if ( ! file) { file.close(); - std::cout << "Error: file " << fileName << " cannot be open;" << std::endl; - return false; } @@ -32,19 +39,15 @@ namespace HttpServer std::streamsize file_size = file.tellg(); file.seekg(0, std::ifstream::beg); - constexpr std::streamsize file_size_max = 2 * FILESIZE; + constexpr std::streamsize file_size_max = FILESIZE; - if (file_size_max < file_size) - { + if (file_size_max < file_size) { file.close(); - std::cout << "Error: file " << fileName << " is too large; max include file size = " << file_size_max << " bytes;" << std::endl; - return false; } - if (file_size) - { + if (file_size) { std::vector buf(file_size); file.read(buf.data(), file_size); @@ -68,8 +71,7 @@ namespace HttpServer { auto const it_name = app.find("server_name"); - if (app.cend() == it_name) - { + if (app.cend() == it_name) { std::cout << "Error: application parameter 'server_name' has not been specified;" << std::endl; return false; } @@ -121,14 +123,10 @@ namespace HttpServer { const int port = std::strtol(value.c_str(), nullptr, 10); - if (port) - { - if (is_tls) - { + if (port) { + if (is_tls) { tls_ports.emplace(port); - } - else - { + } else { ports.emplace(port); } } @@ -142,73 +140,61 @@ namespace HttpServer std::string stapling_file; std::string dh_file; - if (false == tls_ports.empty() ) + if (tls_ports.empty() == false) { auto const it_ca_file = app.find("tls_certificate_chain"); - if (app.cend() != it_ca_file) - { + if (app.cend() != it_ca_file) { chain_file = it_ca_file->second; } auto const it_crl_file = app.find("tls_certificate_crl"); - if (app.cend() != it_crl_file) - { + if (app.cend() != it_crl_file) { crl_file = it_crl_file->second; } auto const it_stapling_file = app.find("tls_stapling_file"); - if (app.cend() != it_stapling_file) - { + if (app.cend() != it_stapling_file) { stapling_file = it_stapling_file->second; } auto const it_dh_params_file = app.find("tls_dh_params_file"); - if (app.cend() != it_dh_params_file) - { + if (app.cend() != it_dh_params_file) { dh_file = it_dh_params_file->second; } auto const it_cert_file = app.find("tls_certificate"); - if (app.cend() == it_cert_file) - { + if (app.cend() == it_cert_file) { std::cout << "Error: tls certificate file \"CERT\" has not been specified in configuration file;" << std::endl; tls_ports.clear(); - } - else - { + } else { cert_file = it_cert_file->second; } auto const it_key_file = app.find("tls_certificate_key"); - if (app.cend() == it_key_file) - { + if (app.cend() == it_key_file) { std::cout << "Error: tls certificate key file \"KEY\" has not been specified in configuration file;" << std::endl; tls_ports.clear(); - } - else - { + } else { key_file = it_key_file->second; } } auto const it_root_dir = app.find("root_dir"); - if (app.cend() == it_root_dir || it_root_dir->second.empty() ) - { + if (app.cend() == it_root_dir || it_root_dir->second.empty() ) { std::cout << "Error: application parameter 'root_dir' has not been specified;" << std::endl; return false; } auto const it_module = app.find("server_module"); - if (app.cend() == it_module) - { + if (app.cend() == it_module) { std::cout << "Error: application parameter 'server_module' has not been specified;" << std::endl; return false; } @@ -217,30 +203,26 @@ namespace HttpServer System::Module module(it_module->second); - if (false == module.is_open() ) - { + if (module.is_open() == false) { std::cout << "Error: module '" << it_module->second << "' cannot be open;" << std::endl; return false; } void *(*addr)(void *) = nullptr; - if (false == module.find("application_call", &addr) ) - { + if (module.find("application_call", &addr) == false) { std::cout << "Error: function 'application_call' not found in module '" << it_module->second << "';" << std::endl; return false; } std::function app_call = reinterpret_cast(addr); - if ( ! app_call) - { + if ( ! app_call) { std::cout << "Error: invalid function 'application_call' in module '" << it_module->second << "';" << std::endl; return false; } - if (false == module.find("application_clear", &addr) ) - { + if (module.find("application_clear", &addr) == false) { std::cout << "Error: function 'application_clear' not found in module '" << it_module->second << "';" << std::endl; return false; } @@ -249,30 +231,26 @@ namespace HttpServer std::function app_init = std::function(); - if (module.find("application_init", &addr) ) - { + if (module.find("application_init", &addr) ) { app_init = reinterpret_cast(addr); } std::function app_final = std::function(); - if (module.find("application_final", &addr) ) - { + if (module.find("application_final", &addr) ) { app_final = reinterpret_cast(addr); } std::string root_dir = it_root_dir->second; #ifdef WIN32 - if ('\\' == root_dir.back() ) - { + if (root_dir.back() == '\\') { root_dir.pop_back(); } #endif // Remove back slash from root_dir - if ('/' == root_dir.back() ) - { + if (root_dir.back() == '/') { root_dir.pop_back(); } @@ -293,10 +271,8 @@ namespace HttpServer success = false; } - if (false == success) - { + if (false == success) { std::cout << "Warning: error when initializing application '" << it_module->second << "';" << std::endl; - return false; } @@ -324,8 +300,7 @@ namespace HttpServer } } - if (std::numeric_limits::max() == module_index) - { + if (std::numeric_limits::max() == module_index) { module_index = modules.size(); modules.emplace_back(std::move(module) ); } @@ -357,14 +332,10 @@ namespace HttpServer }; // Add application names in tree - if (names.empty() ) - { + if (names.empty() ) { apps_tree.addApplication(app_name, settings); - } - else - { - for (size_t i = 0; i < names.size(); ++i) - { + } else { + for (size_t i = 0; i < names.size(); ++i) { apps_tree.addApplication(names[i], settings); } } @@ -382,12 +353,9 @@ namespace HttpServer { std::ifstream file(fileName); - if ( ! file) - { + if ( ! file) { file.close(); - std::cout << "Error: " << fileName << " - cannot be open;" << std::endl; - return false; } @@ -395,14 +363,11 @@ namespace HttpServer std::streamsize file_size = file.tellg(); file.seekg(0, std::ifstream::beg); - const std::streamsize file_size_max = 2048 * 1024; + const std::streamsize file_size_max = FILESIZE; - if (file_size_max < file_size) - { + if (file_size_max < file_size) { file.close(); - std::cout << "Error: " << fileName << " - is too large; max file size = " << file_size_max << " bytes;" << std::endl; - return false; } @@ -414,94 +379,90 @@ namespace HttpServer const std::string str_buf(buf.cbegin(), buf.cend() ); + size_t delimiter = 0; size_t cur_pos = 0; + size_t end_pos = str_buf.find('\n', cur_pos); - ConfigFileDataState dataState = CONFIGFILEDATASNONE; + ConfigMimeState state = ConfigMimeState::NONE; - while (std::string::npos != cur_pos) + while (std::string::npos != end_pos) { - //std::cout << "parse Mine dataState: " << dataState << std::endl; - switch(dataState) + switch (state) { - case CONFIGFILEDATASNONE: + case ConfigMimeState::NONE: + { + cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); + delimiter = str_buf.find_first_of(whitespace, cur_pos); - case CONFIGFILEDATASNOTE: - - { - cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - if (cur_pos == std::string::npos) - { - return true; - } + if (delimiter >= end_pos) { + cur_pos = end_pos + 1; + end_pos = str_buf.find('\n', cur_pos); + break; + } - //start from next line - if ('#' == buf[cur_pos]) - { - //last line - cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos == std::string::npos) - { - return true; - } + if ('#' != str_buf[cur_pos]) { + state = ConfigMimeState::TYPE; + } - cur_pos += 1; + break; + } - dataState = CONFIGFILEDATASNOTE; - } - else if ('\n' == buf[cur_pos]) - { - cur_pos += 1; - dataState = CONFIGFILEDATASNONE; - } - else - { - dataState = CONFIGFILEDATASTYPE; - } + case ConfigMimeState::TYPE: + { + state = ConfigMimeState::NONE; + + std::string mime_type = str_buf.substr(cur_pos, delimiter - cur_pos); + + delimiter = str_buf.find_first_not_of(whitespace, delimiter); + + if (delimiter >= end_pos) { + cur_pos = end_pos + 1; + end_pos = str_buf.find('\n', cur_pos); + break; } - break; - case CONFIGFILEDATASTYPE: + + std::string ext = str_buf.substr(delimiter, end_pos - delimiter); + + delimiter = ext.find_first_of(whitespace); + + if (std::string::npos != delimiter) { - size_t delimiter = str_buf.find_first_of("\n", cur_pos); - std::string strLine = str_buf.substr(cur_pos, delimiter - cur_pos); - - //get mimetypes + for (size_t ext_pos = 0; std::string::npos != ext_pos; ) { - size_t ext_pos = strLine.find_first_of(whitespace, 0); - if (ext_pos == std::string::npos) - { - return false; + std::string ext_unit = ext.substr(ext_pos, + std::string::npos == delimiter + ? std::string::npos + : delimiter - ext_pos + ); + + if (ext_unit.empty() == false) { + mimes_types.emplace(std::move(ext_unit), mime_type); } - std::string strExt = strLine.substr(0, ext_pos); - size_t extUnitPos = strLine.find_first_not_of(whitespace, ext_pos); - std::string strExtUnit = strLine.substr(extUnitPos, strLine.length() - extUnitPos); - mimes_types.emplace(std::move(strExt), std::move(strExtUnit)); + ext_pos = ext.find_first_not_of(whitespace, delimiter); - //std::cout << strExt << " " << strExtUnit << std::endl; - } - - //change line - cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos == std::string::npos) - { - return true; + delimiter = ext.find_first_of(whitespace, ext_pos); } + } + else + { + mimes_types.emplace(std::move(ext), std::move(mime_type) ); + } - cur_pos += 1; + cur_pos = end_pos + 1; + end_pos = str_buf.find('\n', cur_pos); - dataState = CONFIGFILEDATASNONE; - - } break; + } + default: break; } } - + return true; } - static size_t findBlockEnd(const std::string &str_buf, size_t str_pos) { size_t pos = str_buf.find('}', str_pos); @@ -510,22 +471,18 @@ namespace HttpServer { size_t begin_line = str_buf.rfind('\n', pos); - if (std::string::npos == begin_line) - { + if (std::string::npos == begin_line) { begin_line = 0; } begin_line = str_buf.find_first_not_of("\r\n", begin_line); - if ('#' == str_buf[begin_line]) - { - str_pos = str_buf.find_first_of("\r\n", pos); - } - else - { + if ('#' != str_buf[begin_line]) { break; } + str_pos = str_buf.find_first_of("\r\n", pos); + pos = str_buf.find('}', str_pos); } @@ -537,11 +494,9 @@ namespace HttpServer */ bool ConfigParser::loadConfig(const std::string &conf_file_name, ServerSettings &settings, std::vector &modules) { - //std::cout << "enter loadCofig" << std::endl; std::string str_buf; - if (false == includeConfigFile(conf_file_name, str_buf) ) - { + if (includeConfigFile(conf_file_name, str_buf) == false) { return false; } @@ -554,238 +509,181 @@ namespace HttpServer const std::string whitespace(" \t\n\v\f\r"); size_t cur_pos = 0; + size_t end_pos = 0; + size_t block_pos = 0; - ConfigFileDataState dataState = CONFIGFILEDATASNONE; - InBlockDataState inBlockState = INBLOCKNONE; - - std::unordered_multimap app; + ConfigState state = ConfigState::NONE; - int iStatus = 0; - while (std::string::npos != cur_pos) + while (std::string::npos != end_pos) { - //std::cout << "parse Mine dataState: " << dataState << std::endl; - if (iStatus == 1 || iStatus == 2) + switch (state) { - break; - } + case ConfigState::NONE: + { + end_pos = str_buf.find(';', cur_pos); + block_pos = str_buf.find('{', cur_pos); - switch (dataState) - { - case CONFIGFILEDATASNONE: + if (end_pos < block_pos) { + state = ConfigState::PARAM; + } + else if (std::string::npos != block_pos) { + state = ConfigState::BLOCK; + } - case CONFIGFILEDATASNOTE: - { - cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - if (cur_pos == std::string::npos) - { - iStatus = 1; - } + break; + } - //start from next line - if ('#' == str_buf[cur_pos]) - { - //last line - cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos == std::string::npos) - { - iStatus = 1; - } + case ConfigState::PARAM: + { + state = ConfigState::NONE; - cur_pos += 1; + cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); + end_pos = str_buf.find(';', cur_pos); + size_t delimiter = str_buf.find_first_of(whitespace, cur_pos); - dataState = CONFIGFILEDATASNOTE; - } - else if ('\n' == str_buf[cur_pos]) - { - cur_pos += 1; - dataState = CONFIGFILEDATASNONE; - } - else - { - dataState = CONFIGFILEDATASTYPE; - } - } - break; - case CONFIGFILEDATASTYPE: + if (delimiter < end_pos) { - //in app and main config file "; and \n " has different meaning - size_t delimiter = str_buf.find_first_of("\n", cur_pos); - //std:: cout << "str_buf.length()" << str_buf.length() << " delimiter " << delimiter - // << " cur_pos " << cur_pos << std::endl; - //std::cout << "npos: " << std::string::npos << std::endl; - if (delimiter == std::string::npos) + std::string param_name = str_buf.substr(cur_pos, delimiter - cur_pos); + + if ('#' != param_name.front() ) { - iStatus = 1; - } + cur_pos = str_buf.find_first_not_of(whitespace, delimiter + 1); + delimiter = str_buf.find_last_not_of(whitespace, end_pos); - std::string strLine = str_buf.substr(cur_pos, delimiter - cur_pos); - //std::cout << "strLine: " << strLine << std::endl; - do { - size_t ext_pos = strLine.find_first_of(whitespace, 0); - if (ext_pos == std::string::npos) - { - iStatus = 2; - } - std::string param_name = strLine.substr(0, ext_pos); - size_t extUnitPos = strLine.find_first_not_of(whitespace, ext_pos); + std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos); - //mines the last ";" - std::string param_value; - if (strLine[strLine.length() - 1] == ';') + if ("include" == param_name) { - param_value = strLine.substr(extUnitPos, strLine.length() - extUnitPos - 1); - } - else - { - param_value = strLine.substr(extUnitPos, strLine.length() - extUnitPos); - } - - //std::cout << param_name << " " << param_value << std::endl; - if (param_name == "server" && param_value == "{") - { - dataState = CONFIGFILEDATAINBLOCK; - inBlockState = INBLOCKNONE; - } - else if(param_name == "include") - { - this->loadConfig(param_value, settings, modules); - dataState = CONFIGFILEDATASNONE; + this->includeConfigFile(param_value, str_buf, end_pos + 1); } else { global.emplace(std::move(param_name), std::move(param_value) ); - dataState = CONFIGFILEDATASNONE; } - break; - } while (1); - - //change line - cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos == std::string::npos) + } + else // if comment line { - iStatus = 1; + end_pos = str_buf.find_first_of("\r\n", cur_pos); } - - cur_pos += 1; } + + cur_pos = std::string::npos == end_pos + ? std::string::npos + : end_pos + 1; + break; - case CONFIGFILEDATAINBLOCK: + } + + case ConfigState::BLOCK: + { + state = ConfigState::NONE; + + cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); + end_pos = str_buf.find(';', cur_pos); + size_t delimiter = str_buf.find_first_of(whitespace, cur_pos); + + const std::string block_type_name = str_buf.substr(cur_pos, delimiter - cur_pos); + + if ('#' != block_type_name.front() ) { - switch(inBlockState) + delimiter = str_buf.find_first_not_of(whitespace, delimiter); + + cur_pos = block_pos + 1; + size_t block_end = findBlockEnd(str_buf, cur_pos); + + if (std::string::npos == block_end) + { + std::cout << "Error: symbol '}' after '" << block_type_name << "' has not been found;" << std::endl + << "Parsing config aborted;" << std::endl; + + return false; + } + else if (delimiter == block_pos) { - case INBLOCKNONE: - case INBLOCKNOTE: + if ("server" == block_type_name) + { + std::unordered_multimap app; + + end_pos = str_buf.find(';', cur_pos); + + while (block_end > end_pos) { cur_pos = str_buf.find_first_not_of(whitespace, cur_pos); - if (cur_pos == std::string::npos) - { - iStatus = 1; - } + delimiter = str_buf.find_first_of(whitespace, cur_pos); - //start from next line - if ('#' == str_buf[cur_pos]) + if (delimiter < end_pos) { - //last line - cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos == std::string::npos) - { - iStatus = 1; - } - - cur_pos += 1; + std::string param_name = str_buf.substr(cur_pos, delimiter - cur_pos); - inBlockState = INBLOCKNOTE; - } - else if ('\n' == str_buf[cur_pos]) - { - cur_pos += 1; - inBlockState = INBLOCKNONE; - } - else - { - inBlockState = INBLOCKTPYE; - } - } - break; - case INBLOCKTPYE: - { - size_t delimiter = str_buf.find_first_of("\n", cur_pos); - std::string strLine = str_buf.substr(cur_pos, delimiter - cur_pos); - - //std::cout << "inblockLineStr: " << strLine << std::endl; - - do { - if ("}" == strLine) + if ('#' != param_name.front() ) { - dataState = CONFIGFILEDATASNOTE; - applications.push_back(app); - app.clear(); - break; + cur_pos = str_buf.find_first_not_of(whitespace, delimiter + 1); + delimiter = str_buf.find_last_not_of(whitespace, end_pos); + + std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos); + + if ("include" == param_name) + { + cur_pos = end_pos + 1; + this->includeConfigFile(param_value, str_buf, cur_pos); + block_end = findBlockEnd(str_buf, cur_pos); + } + else + { + app.emplace(std::move(param_name), std::move(param_value) ); + } } - - size_t ext_pos = strLine.find_first_of(whitespace, 0); - if (ext_pos == std::string::npos) + else // if comment line { - iStatus = 2; + end_pos = str_buf.find_first_of("\r\n", cur_pos); } - std::string strExt = strLine.substr(0, ext_pos); - size_t extUnitPos = strLine.find_first_not_of(whitespace, ext_pos); - std::string strExtUnit = strLine.substr(extUnitPos, strLine.length() - extUnitPos - 1); - - //std::cout << "inblockLine " << strExt << " " << strExtUnit << std::endl; - app.emplace(std::move(strExt), std::move(strExtUnit)); - - break; - - } while(1); - - //change line - cur_pos = str_buf.find_first_of("\n", cur_pos); - if (cur_pos == std::string::npos) - { - iStatus = 2; } - cur_pos += 1; + cur_pos = std::string::npos == end_pos + ? std::string::npos + : end_pos + 1; - inBlockState = INBLOCKNONE; + end_pos = str_buf.find(';', cur_pos); } - break; - default: - break; + + applications.emplace_back(std::move(app) ); + } + else + { + std::cout << "Warning: " << block_type_name << " - unknown block type;" << std::endl; + } } + else + { + std::cout << "Warning: after " << block_type_name << " expected '{' ;" << std::endl; + } + + cur_pos = std::string::npos == block_end + ? std::string::npos + : block_end + 1; + } + else // if comment line + { + cur_pos = str_buf.find_first_of("\r\n", cur_pos); } + break; + } + default: break; } } - // std::cout << "app value start" << std::endl; - // for (size_t i = 0; i < applications.size(); ++i) - // { - // std::unordered_multimap sa = applications[i]; - // for (auto iter = sa.begin(); iter != sa.end(); iter++) - // { - // std::cout << iter->first << " " << iter->second << std::endl; - // } - - // } - // std::cout << "app value end" << std::endl; - auto const it_mimes = global.find("mimes"); - if (global.cend() != it_mimes) - { + if (global.cend() != it_mimes) { this->parseMimes(it_mimes->second, mimes_types); - //std::cout << "parse mimes end" << std::endl; - } - else - { + } else { std::cout << "Warning: mime types file is not set in configuration;" << std::endl; } - if (!applications.empty()) + if (applications.empty() == false) { auto const it_default_temp_dir = global.find("default_temp_dir"); @@ -800,19 +698,15 @@ namespace HttpServer default_request_max_size }; - for (auto const &app : applications) - { + for (auto const &app : applications) { this->addApplication(app, defaults, modules, apps_tree); - std::cout << "addApplication end" << std::endl; } } - if (apps_tree.empty() ) - { + if (apps_tree.empty() ) { std::cout << "Notice: server does not contain applications;" << std::endl; } - //std::cout << "out loadCofig" << std::endl; - return iStatus == 1; + return true; } -}; +} diff --git a/src/server/config/ConfigParser.h b/src/server/config/ConfigParser.h index c501d5d..f7805dc 100644 --- a/src/server/config/ConfigParser.h +++ b/src/server/config/ConfigParser.h @@ -6,22 +6,7 @@ #include namespace HttpServer -{ - enum ConfigFileDataState - { - CONFIGFILEDATASNONE = 0, - CONFIGFILEDATASNOTE, //comment - CONFIGFILEDATASTYPE, - //CONFIGFILEDATAGLOBAL, - CONFIGFILEDATAINBLOCK, //in block - }; - enum InBlockDataState - { - INBLOCKNONE, //in block none - INBLOCKNOTE, //in block note - INBLOCKTPYE, //in block type - }; - +{ class ConfigParser { private: @@ -45,4 +30,4 @@ namespace HttpServer public: bool loadConfig(const std::string &conf, ServerSettings &settings, std::vector &modules); }; -}; +} From b04b1a7e74b0116b32112b9c8eb308973b49ae07 Mon Sep 17 00:00:00 2001 From: awwit Date: Sat, 31 Mar 2018 22:31:40 +0300 Subject: [PATCH 18/22] Fixed endless looping when TLS-handshake. Added http/1.1 as default protocol for non-ALPN tls connections. Fixed many warnings in code. Changed code style. --- projects/qt-creator/httpserver.qbs | 4 +- src/Main.cpp | 21 +- src/SignalHandlers.cpp | 30 +- src/server/Request.cpp | 2 +- src/server/Request.h | 8 +- src/server/Server.cpp | 239 +++++++---- src/server/Server.h | 34 +- src/server/ServerApplicationSettings.h | 2 +- src/server/ServerApplicationsTree.cpp | 58 +-- src/server/ServerApplicationsTree.h | 2 +- src/server/ServerControls.cpp | 39 +- src/server/ServerControls.h | 2 +- src/server/ServerSettings.cpp | 26 +- src/server/ServerSettings.h | 2 +- src/server/ServerStructuresArguments.h | 2 +- src/server/SocketsQueue.h | 2 +- src/server/config/ConfigParser.cpp | 160 ++++--- src/server/config/ConfigParser.h | 20 +- src/server/data-variant/Abstract.cpp | 16 +- src/server/data-variant/Abstract.h | 14 +- src/server/data-variant/FormUrlencoded.cpp | 57 ++- src/server/data-variant/FormUrlencoded.h | 2 +- src/server/data-variant/MultipartFormData.cpp | 194 +++++---- src/server/data-variant/MultipartFormData.h | 13 +- src/server/data-variant/TextPlain.cpp | 50 ++- src/server/data-variant/TextPlain.h | 2 +- src/server/protocol/ServerHttp1.cpp | 390 ++++++++++------- src/server/protocol/ServerHttp1.h | 53 ++- src/server/protocol/ServerHttp2.cpp | 400 ++++++++++++------ src/server/protocol/ServerHttp2.h | 7 +- src/server/protocol/ServerHttp2Protocol.cpp | 130 ++++-- src/server/protocol/ServerHttp2Protocol.h | 35 +- src/server/protocol/ServerHttp2Stream.cpp | 52 ++- src/server/protocol/ServerHttp2Stream.h | 9 +- src/server/protocol/ServerProtocol.cpp | 115 +++-- src/server/protocol/ServerProtocol.h | 48 ++- src/server/protocol/ServerWebSocket.cpp | 45 +- src/server/protocol/ServerWebSocket.h | 36 +- src/server/protocol/extensions/Sendfile.cpp | 249 +++++++---- src/server/protocol/extensions/Sendfile.h | 2 +- src/socket/Adapter.cpp | 22 +- src/socket/Adapter.h | 26 +- src/socket/AdapterDefault.cpp | 16 +- src/socket/AdapterDefault.h | 13 +- src/socket/AdapterTls.cpp | 42 +- src/socket/AdapterTls.h | 28 +- src/socket/List.cpp | 114 +++-- src/socket/List.h | 18 +- src/socket/Socket.cpp | 272 ++++++++---- src/socket/Socket.h | 43 +- src/system/GlobalMutex.cpp | 57 +-- src/system/GlobalMutex.h | 4 +- src/system/Module.cpp | 94 ++-- src/system/Module.h | 15 +- src/system/SharedMemory.cpp | 109 +++-- src/system/SharedMemory.h | 23 +- src/system/System.cpp | 53 ++- src/system/System.h | 40 +- src/transfer/AppRequest.h | 6 +- src/transfer/AppResponse.h | 3 +- src/transfer/FileIncoming.cpp | 47 +- src/transfer/FileIncoming.h | 19 +- src/transfer/HttpStatusCode.h | 3 +- src/transfer/ProtocolVariant.h | 3 +- src/transfer/http2/HPack.cpp | 258 +++++++---- src/transfer/http2/HPack.h | 12 +- src/transfer/http2/Http2.cpp | 115 +++-- src/transfer/http2/Http2.h | 79 ++-- src/utils/Event.cpp | 9 +- src/utils/Utils.cpp | 136 ++++-- src/utils/Utils.h | 5 +- 71 files changed, 2803 insertions(+), 1453 deletions(-) diff --git a/projects/qt-creator/httpserver.qbs b/projects/qt-creator/httpserver.qbs index 464d956..20d1f24 100644 --- a/projects/qt-creator/httpserver.qbs +++ b/projects/qt-creator/httpserver.qbs @@ -12,12 +12,12 @@ Project { Properties { condition: qbs.targetOS.contains("linux") - cpp.platformDefines: outer.concat(["POSIX"]) + cpp.defines: outer.concat(["POSIX"]) cpp.dynamicLibraries: outer.concat(["dl", "pthread", "rt"]) } Properties { condition: qbs.targetOS.contains("windows") - cpp.platformDefines: outer.concat(["WIN32", "NOMINMAX"]) + cpp.defines: outer.concat(["WIN32", "NOMINMAX"]) } files: [ diff --git a/src/Main.cpp b/src/Main.cpp index d3d341d..8aa2a4b 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -17,32 +17,23 @@ int main(const int argc, const char *argv[]) int exitcode = EXIT_FAILURE; - if (1 < argc) - { + if (1 < argc) { auto const command = commands.find(argv[1]); - if (commands.cend() != command) - { + if (commands.cend() != command) { HttpServer::Server server; - if (bindSignalHandlers(&server) ) - { + if (bindSignalHandlers(&server) ) { exitcode = command->second(&server, argc, argv); - stopSignalHandlers(); } - } - else - { + } else { std::cout << "Unknown parameter, see --help" << std::endl; } } - else if (1 == argc) - { + else if (1 == argc) { std::cout << "Try to run with the parameter --help" << std::endl; - } - else - { + } else { std::cout << "The number of arguments cannot be equal to zero. Enter the parameter --help" << std::endl; } diff --git a/src/SignalHandlers.cpp b/src/SignalHandlers.cpp index 80bbc7d..cad0155 100644 --- a/src/SignalHandlers.cpp +++ b/src/SignalHandlers.cpp @@ -56,8 +56,12 @@ static void handlerSigUsr2(const int) noexcept { * It doesn't work in case the program was launched and was * attempted to finish under different remote sessions. */ -static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const ::WPARAM wParam, const ::LPARAM lParam) noexcept -{ +static ::LRESULT CALLBACK WndProc( + const ::HWND hWnd, + const ::UINT message, + const ::WPARAM wParam, + const ::LPARAM lParam +) noexcept { switch (message) { case SIGTERM: { @@ -105,9 +109,23 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const return 0; } -static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, Utils::Event * const eventWindowCreation) noexcept -{ - const ::HWND hWnd = ::CreateWindow(myWndClassName, nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, hInstance, nullptr); +static ::WPARAM mainMessageLoop( + const ::HINSTANCE hInstance, + Utils::Event * const eventWindowCreation +) noexcept { + const ::HWND hWnd = ::CreateWindow( + myWndClassName, + nullptr, + 0, + CW_USEDEFAULT, + CW_USEDEFAULT, + 0, + 0, + nullptr, + nullptr, + hInstance, + nullptr + ); eventWindowCreation->notify(); // After this action, eventWindowCreation will be destroyed (in the other thread) @@ -207,7 +225,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept ::signal(SIGPIPE, SIG_IGN); #else - #error "Undefine platform" + #error "Undefined platform" #endif return true; diff --git a/src/server/Request.cpp b/src/server/Request.cpp index f8bfcea..17ca269 100644 --- a/src/server/Request.cpp +++ b/src/server/Request.cpp @@ -17,4 +17,4 @@ namespace HttpServer // timeout = std::chrono::milliseconds::zero(); } -}; +} diff --git a/src/server/Request.h b/src/server/Request.h index dd2b5d5..abc8de0 100644 --- a/src/server/Request.h +++ b/src/server/Request.h @@ -8,8 +8,7 @@ namespace HttpServer { - enum ConnectionParams - { + enum ConnectionParams { CONNECTION_CLOSE = 0, CONNECTION_REUSE = 1, CONNECTION_LEAVE_OPEN = 2 @@ -36,9 +35,8 @@ namespace HttpServer void clear() noexcept; }; - struct DataTransfer - { + struct DataTransfer { size_t full_size; size_t send_total; }; -}; +} diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 3f87c92..df464eb 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -33,8 +33,7 @@ namespace HttpServer ServerControls &controls, SocketsQueue &sockets, Http2::IncStream *stream - ) - { + ) { std::unique_ptr prot; // If request is HTTP/2 Stream @@ -43,11 +42,13 @@ namespace HttpServer return prot; } - if (sock.get_tls_session() ) + if (sock.get_tls_session() != nullptr) { ::gnutls_datum_t datum; - if (0 == ::gnutls_alpn_get_selected_protocol(sock.get_tls_session(), &datum) ) + const int ret = ::gnutls_alpn_get_selected_protocol(sock.get_tls_session(), &datum); + + if (GNUTLS_E_SUCCESS == ret) { const std::string protocol(reinterpret_cast(datum.data), datum.size); @@ -58,6 +59,9 @@ namespace HttpServer prot.reset(new ServerHttp1(sock, settings, controls) ); } } + else if (GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE == ret) { + prot.reset(new ServerHttp1(sock, settings, controls) ); + } return prot; } @@ -70,9 +74,18 @@ namespace HttpServer /** * Метод для обработки запроса */ - void Server::threadRequestProc(Socket::Adapter &sock, SocketsQueue &sockets, Http2::IncStream *stream) const - { - std::unique_ptr prot = getProtocolVariant(sock, this->settings, this->controls, sockets, stream); + void Server::threadRequestProc( + Socket::Adapter &sock, + SocketsQueue &sockets, + Http2::IncStream *stream + ) const { + std::unique_ptr prot = getProtocolVariant( + sock, + this->settings, + this->controls, + sockets, + stream + ); if (prot) { // Check if switching protocol @@ -94,8 +107,10 @@ namespace HttpServer * Метод для обработки запросов (запускается в отдельном потоке) * извлекает сокет клиенты из очереди и передаёт его на обслуживание */ - void Server::threadRequestCycle(SocketsQueue &sockets, Utils::Event &eventThreadCycle) const - { + void Server::threadRequestCycle( + SocketsQueue &sockets, + Utils::Event &eventThreadCycle + ) const { while (true) { Socket::Socket sock; @@ -114,7 +129,11 @@ namespace HttpServer /* sockaddr_in addr {}; socklen_t addr_size = sizeof(sockaddr_in); - ::getpeername(sock.get_handle(), reinterpret_cast(&addr), &addr_size); + ::getpeername( + sock.get_handle(), + reinterpret_cast(&addr), + &addr_size + ); */ sockets.pop(); } @@ -133,7 +152,11 @@ namespace HttpServer ::sockaddr_in sock_addr {}; ::socklen_t sock_addr_len = sizeof(sock_addr); - ::getsockname(sock.get_handle(), reinterpret_cast(&sock_addr), &sock_addr_len); + ::getsockname( + sock.get_handle(), + reinterpret_cast(&sock_addr), + &sock_addr_len + ); const int port = ntohs(sock_addr.sin_port); @@ -141,14 +164,17 @@ namespace HttpServer if (this->tls_data.cend() != it) // if TLS connection { - if (stream) - { - Socket::AdapterTls socket_adapter(reinterpret_cast(stream->reserved) ); + if (stream) { + Socket::AdapterTls socket_adapter( + reinterpret_cast(stream->reserved) + ); - this->threadRequestProc(socket_adapter, sockets, stream); - } - else - { + this->threadRequestProc( + socket_adapter, + sockets, + stream + ); + } else { const std::tuple &data = it->second; Socket::AdapterTls socket_adapter( @@ -158,15 +184,21 @@ namespace HttpServer ); if (socket_adapter.handshake() ) { - this->threadRequestProc(socket_adapter, sockets, nullptr); + this->threadRequestProc( + socket_adapter, + sockets, + nullptr + ); } } - } - else - { + } else { Socket::AdapterDefault socket_adapter(sock); - this->threadRequestProc(socket_adapter, sockets, stream); + this->threadRequestProc( + socket_adapter, + sockets, + stream + ); } --this->threads_working_count; @@ -202,8 +234,9 @@ namespace HttpServer Utils::Event eventThreadCycle(false, true); - std::function serverThreadRequestCycle - = std::mem_fn(&Server::threadRequestCycle); + std::function serverThreadRequestCycle = std::mem_fn(&Server::threadRequestCycle); std::vector active_threads; active_threads.reserve(threads_max_count); @@ -216,9 +249,17 @@ namespace HttpServer // Cycle creation threads applications requests do { - while (this->threads_working_count == active_threads.size() && active_threads.size() < threads_max_count && sockets.empty() == false) - { - active_threads.emplace_back(serverThreadRequestCycle, this, std::ref(sockets), std::ref(eventThreadCycle) ); + while ( + this->threads_working_count == active_threads.size() && + active_threads.size() < threads_max_count && + sockets.empty() == false + ) { + active_threads.emplace_back( + serverThreadRequestCycle, + this, + std::ref(sockets), + std::ref(eventThreadCycle) + ); } size_t notify_count = active_threads.size() - this->threads_working_count; @@ -253,8 +294,11 @@ namespace HttpServer return 0; } - bool Server::updateModule(System::Module &module, std::unordered_set &applications, const size_t moduleIndex) - { + bool Server::updateModule( + System::Module &module, + std::unordered_set &applications, + const size_t moduleIndex + ) { std::unordered_set same; for (auto &app : applications) @@ -270,7 +314,8 @@ namespace HttpServer } } catch (std::exception &exc) { - std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl; + std::cout << "Warning: an exception was thrown when the application '" + << app->server_module << "' was finishes: " << exc.what() << std::endl; } app->application_call = std::function(); @@ -347,12 +392,12 @@ namespace HttpServer // Open updated module module.open(module_name_temp); - if (0 != std::remove(module_name.c_str() ) ) { + if (std::remove(module_name.c_str() ) != 0) { std::cout << "Error: file '" << module_name << "' could not be removed;" << std::endl; return false; } - if (0 != std::rename(module_name_temp.c_str(), module_name.c_str() ) ) { + if (std::rename(module_name_temp.c_str(), module_name.c_str() ) != 0) { std::cout << "Error: file '" << module_name_temp << "' could not be renamed;" << std::endl; return false; } @@ -372,7 +417,11 @@ namespace HttpServer return false; } - std::function app_call = reinterpret_cast(addr); + std::function app_call = reinterpret_cast(addr); if ( ! app_call) { std::cout << "Error: invalid function 'application_call' is in the module '" << module_name << "';" << std::endl; @@ -511,48 +560,67 @@ namespace HttpServer Socket::Socket::Cleanup(); } - static bool tlsInit(const ServerApplicationSettings &app, std::tuple &data) - { + static bool tlsInit( + const ServerApplicationSettings &app, + std::tuple &data + ) { ::gnutls_certificate_credentials_t x509_cred; int ret = ::gnutls_certificate_allocate_credentials(&x509_cred); if (ret < 0) { - std::cout << "Error [tls]: certificate credentials has not been allocated;" << std::endl; + std::cout << "Error [tls]: certificate credentials has not been allocated; code: " << ret << std::endl; return false; } if (app.chain_file.empty() == false) { - ret = ::gnutls_certificate_set_x509_trust_file(x509_cred, app.chain_file.c_str(), GNUTLS_X509_FMT_PEM); + ret = ::gnutls_certificate_set_x509_trust_file( + x509_cred, + app.chain_file.c_str(), + GNUTLS_X509_FMT_PEM + ); if (ret < 0) { - std::cout << "Warning [tls]: (CA) chain file has not been accepted;" << std::endl; + std::cout << "Warning [tls]: (CA) chain file has not been accepted; code: " << ret << std::endl; } } if (app.crl_file.empty() == false) { - ret = ::gnutls_certificate_set_x509_crl_file(x509_cred, app.crl_file.c_str(), GNUTLS_X509_FMT_PEM); + ret = ::gnutls_certificate_set_x509_crl_file( + x509_cred, + app.crl_file.c_str(), + GNUTLS_X509_FMT_PEM + ); if (ret < 0) { - std::cout << "Warning [tls]: (CLR) clr file has not been accepted;" << std::endl; + std::cout << "Warning [tls]: (CLR) clr file has not been accepted; code: " << ret << std::endl; } } - ret = ::gnutls_certificate_set_x509_key_file(x509_cred, app.cert_file.c_str(), app.key_file.c_str(), GNUTLS_X509_FMT_PEM); + ret = ::gnutls_certificate_set_x509_key_file( + x509_cred, + app.cert_file.c_str(), + app.key_file.c_str(), + GNUTLS_X509_FMT_PEM + ); if (ret < 0) { - std::cout << "Error [tls]: (CERT) cert file or/and (KEY) key file has not been accepted;" << std::endl; + std::cout << "Error [tls]: (CERT) cert file or/and (KEY) key file has not been accepted; code: " << ret << std::endl; return false; } if (app.stapling_file.empty() == false) { - ret = ::gnutls_certificate_set_ocsp_status_request_file(x509_cred, app.stapling_file.c_str(), 0); + ret = ::gnutls_certificate_set_ocsp_status_request_file( + x509_cred, + app.stapling_file.c_str(), + 0 + ); if (ret < 0) { - std::cout << "Warning [tls]: (OCSP) stapling file has not been accepted;" << std::endl; + std::cout << "Warning [tls]: (OCSP) stapling file has not been accepted; code: " << ret << std::endl; } } @@ -560,35 +628,40 @@ namespace HttpServer ::gnutls_dh_params_init(&dh_params); - if (app.dh_file.empty() ) - { - const unsigned int bits = ::gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_HIGH); + if (app.dh_file.empty() ) { + const unsigned int bits = ::gnutls_sec_param_to_pk_bits( + GNUTLS_PK_DH, GNUTLS_SEC_PARAM_HIGH + ); - ret = ::gnutls_dh_params_generate2(dh_params, bits); - } - else - { + ret = ::gnutls_dh_params_generate2( + dh_params, + bits + ); + } else { std::ifstream dh_file(app.dh_file); - if (dh_file) - { + if (dh_file) { const size_t max_file_size = 1024 * 1024; std::vector buf(max_file_size); - dh_file.read(buf.data(), buf.size() ); + dh_file.read( + buf.data(), + std::streamsize(buf.size()) + ); gnutls_datum_t datum { reinterpret_cast(buf.data() ), static_cast(dh_file.gcount() ) }; - ret = ::gnutls_dh_params_import_pkcs3(dh_params, &datum, GNUTLS_X509_FMT_PEM); - } - else - { + ret = ::gnutls_dh_params_import_pkcs3( + dh_params, + &datum, + GNUTLS_X509_FMT_PEM + ); + } else { ret = -1; - std::cout << "Error [tls]: DH params file has not been opened;" << std::endl;; } @@ -597,7 +670,7 @@ namespace HttpServer if (ret < 0) { ::gnutls_certificate_free_credentials(x509_cred); - std::cout << "Error [tls]: DH params were not loaded;" << std::endl; + std::cout << "Error [tls]: DH params were not loaded; code: " << ret << std::endl; return false; } @@ -609,17 +682,22 @@ namespace HttpServer if (ret < 0) { ::gnutls_certificate_free_credentials(x509_cred); - std::cout << "Error [tls]: priority cache cannot be init;" << std::endl; + std::cout << "Error [tls]: priority cache cannot be init; code: " << ret << std::endl; return false; } - data = std::tuple { x509_cred, priority_cache }; + data = std::tuple { + x509_cred, + priority_cache + }; return true; } - bool Server::tryBindPort(const int port, std::unordered_set &ports) - { + bool Server::tryBindPort( + const int port, + std::unordered_set &ports + ) { // Only unique ports if (ports.cend() != ports.find(port) ) { return false; @@ -726,16 +804,13 @@ namespace HttpServer // Cycle for receiving new connections do { - if (sockets_list.accept(accept_sockets) ) - { + if (sockets_list.accept(accept_sockets) ) { sockets.lock(); - for (size_t i = 0; i < accept_sockets.size(); ++i) - { + for (size_t i = 0; i < accept_sockets.size(); ++i) { const Socket::Socket &sock = accept_sockets[i]; - if (sock.is_open() ) - { + if (sock.is_open() ) { sock.nonblock(true); sock.tcp_nodelay(true); @@ -784,8 +859,11 @@ namespace HttpServer return EXIT_SUCCESS; } - bool Server::get_start_args(const int argc, const char *argv[], struct server_start_args *st) - { + bool Server::get_start_args( + const int argc, + const char *argv[], + struct server_start_args *st + ) { for (int i = 1; i < argc; ++i) { if (0 == ::strcmp(argv[i], "--start") ) { @@ -904,30 +982,26 @@ namespace HttpServer return code; } - static void close_liseners(std::vector &liseners) - { + static void close_liseners(std::vector &liseners) { for (auto &sock : liseners) { sock.close(); } } - void Server::stop() - { + void Server::stop() { this->controls.stopProcess(); close_liseners(this->liseners); } - void Server::restart() - { + void Server::restart() { this->controls.setRestart(); this->controls.stopProcess(); close_liseners(this->liseners); } - void Server::update() - { + void Server::update() { this->controls.setUpdateModule(); this->controls.setProcess(false); this->controls.setProcessQueue(); @@ -939,8 +1013,7 @@ namespace HttpServer System::GlobalMutex glob_mtx; - if (glob_mtx.open(serverName) ) - { + if (glob_mtx.open(serverName) ) { System::SharedMemory glob_mem; glob_mtx.lock(); diff --git a/src/server/Server.h b/src/server/Server.h index e09984a..f8ca243 100644 --- a/src/server/Server.h +++ b/src/server/Server.h @@ -31,11 +31,22 @@ namespace HttpServer protected: int cycleQueue(SocketsQueue &sockets); - void threadRequestProc(Socket::Adapter &sock, SocketsQueue &sockets, Http2::IncStream *stream) const; + void threadRequestProc( + Socket::Adapter &sock, + SocketsQueue &sockets, + Http2::IncStream *stream + ) const; + + void threadRequestCycle( + SocketsQueue &sockets, + Utils::Event &eventThreadCycle + ) const; + + bool tryBindPort( + const int port, + std::unordered_set &ports + ); - void threadRequestCycle(SocketsQueue &sockets, Utils::Event &eventThreadCycle) const; - - bool tryBindPort(const int port, std::unordered_set &ports); void initAppsPorts(); bool init(); @@ -45,10 +56,19 @@ namespace HttpServer static System::native_processid_type getServerProcessId(const std::string &serverName); void updateModules(); - bool updateModule(System::Module &module, std::unordered_set &applications, const size_t moduleIndex); + + bool updateModule( + System::Module &module, + std::unordered_set &applications, + const size_t moduleIndex + ); private: - static bool get_start_args(const int argc, const char *argv[], struct server_start_args *st); + static bool get_start_args( + const int argc, + const char *argv[], + struct server_start_args *st + ); public: Server() = default; @@ -63,4 +83,4 @@ namespace HttpServer int command_terminate(const int argc, const char *argv[]) const; int command_update_module(const int argc, const char *argv[]) const; }; -}; +} diff --git a/src/server/ServerApplicationSettings.h b/src/server/ServerApplicationSettings.h index b1bd9d6..ca70896 100644 --- a/src/server/ServerApplicationSettings.h +++ b/src/server/ServerApplicationSettings.h @@ -34,4 +34,4 @@ namespace HttpServer std::function application_init; std::function application_final; }; -}; +} diff --git a/src/server/ServerApplicationsTree.cpp b/src/server/ServerApplicationsTree.cpp index 250e432..3e73586 100644 --- a/src/server/ServerApplicationsTree.cpp +++ b/src/server/ServerApplicationsTree.cpp @@ -8,13 +8,11 @@ namespace HttpServer } - ServerApplicationsTree::~ServerApplicationsTree() noexcept - { + ServerApplicationsTree::~ServerApplicationsTree() noexcept { this->clear(); } - bool ServerApplicationsTree::empty() const noexcept - { + bool ServerApplicationsTree::empty() const noexcept { return this->list.empty(); } @@ -32,8 +30,7 @@ namespace HttpServer { std::string part = name.substr(cur_pos, delimiter - cur_pos); - if ("" == part) - { + if (part.empty() ) { part = "*"; } @@ -45,9 +42,7 @@ namespace HttpServer // Emplace last part std::string part = name.substr(cur_pos); name_parts.emplace_back(std::move(part) ); - } - else - { + } else { name_parts.emplace_back(name); } @@ -56,24 +51,18 @@ namespace HttpServer void ServerApplicationsTree::addApplication(std::vector &nameParts, ServerApplicationSettings *sets) { - if (nameParts.empty() ) - { + if (nameParts.empty() ) { this->app_sets = sets; - } - else - { + } else { std::string &part = nameParts.back(); auto it = this->list.find(part); ServerApplicationsTree *sub; - if (this->list.cend() != it) - { + if (this->list.cend() != it) { sub = it->second; - } - else - { + } else { sub = new ServerApplicationsTree(); this->list.emplace(std::move(part), sub); } @@ -104,9 +93,7 @@ namespace HttpServer std::string part = name.substr(cur_pos); name_parts.emplace_back(std::move(part) ); - } - else - { + } else { name_parts.emplace_back(name); } @@ -115,29 +102,22 @@ namespace HttpServer const ServerApplicationSettings *ServerApplicationsTree::find(std::vector &nameParts) const { - if (nameParts.empty() ) - { + if (nameParts.empty() ) { return this->app_sets; - } - else - { + } else { const std::string part = std::move(nameParts.back() ); nameParts.pop_back(); auto it = this->list.find(part); - if (this->list.cend() == it) - { + if (this->list.cend() == it) { it = this->list.find("*"); - if (this->list.end() != it) - { + if (this->list.end() != it) { return this->app_sets; } - } - else - { + } else { return it->second->find(nameParts); } @@ -151,8 +131,7 @@ namespace HttpServer { const ServerApplicationsTree *tree = node.second; - if (nullptr != tree->app_sets) - { + if (nullptr != tree->app_sets) { set.emplace(tree->app_sets); } @@ -162,14 +141,13 @@ namespace HttpServer void ServerApplicationsTree::clear() noexcept { - if (false == this->list.empty() ) + if (this->list.empty() == false) { - for (auto &it : this->list) - { + for (auto &it : this->list) { delete it.second; } this->list.clear(); } } -}; +} diff --git a/src/server/ServerApplicationsTree.h b/src/server/ServerApplicationsTree.h index 6fe2b24..6f4b54f 100644 --- a/src/server/ServerApplicationsTree.h +++ b/src/server/ServerApplicationsTree.h @@ -31,4 +31,4 @@ namespace HttpServer void clear() noexcept; }; -}; +} diff --git a/src/server/ServerControls.cpp b/src/server/ServerControls.cpp index 6bc57ed..23fd576 100644 --- a/src/server/ServerControls.cpp +++ b/src/server/ServerControls.cpp @@ -4,39 +4,36 @@ namespace HttpServer { ServerControls::ServerControls() - : eventProcessQueue(nullptr), eventNotFullQueue(nullptr), eventUpdateModule(nullptr) + : eventProcessQueue(nullptr), + eventNotFullQueue(nullptr), + eventUpdateModule(nullptr) { } - ServerControls::~ServerControls() - { + ServerControls::~ServerControls() { this->clear(); } void ServerControls::clear() { - if (this->eventNotFullQueue) - { + if (this->eventNotFullQueue) { delete this->eventNotFullQueue; this->eventNotFullQueue = nullptr; } - if (this->eventProcessQueue) - { + if (this->eventProcessQueue) { delete this->eventProcessQueue; this->eventProcessQueue = nullptr; } - if (this->eventUpdateModule) - { + if (this->eventUpdateModule) { delete this->eventUpdateModule; this->eventUpdateModule = nullptr; } } - void ServerControls::setProcess(const bool flag) - { + void ServerControls::setProcess(const bool flag) { this->process_flag = flag; } @@ -44,32 +41,26 @@ namespace HttpServer { this->process_flag = false; - if (this->eventNotFullQueue) - { + if (this->eventNotFullQueue) { this->eventNotFullQueue->notify(); } this->setProcessQueue(); } - void ServerControls::setRestart(const bool flag) - { + void ServerControls::setRestart(const bool flag) { this->restart_flag = flag; } - void ServerControls::setUpdateModule() - { - if (this->eventUpdateModule) - { + void ServerControls::setUpdateModule() { + if (this->eventUpdateModule) { this->eventUpdateModule->notify(); } } - void ServerControls::setProcessQueue() - { - if (this->eventProcessQueue) - { + void ServerControls::setProcessQueue() { + if (this->eventProcessQueue) { this->eventProcessQueue->notify(); } } -}; +} diff --git a/src/server/ServerControls.h b/src/server/ServerControls.h index ab811f0..b438111 100644 --- a/src/server/ServerControls.h +++ b/src/server/ServerControls.h @@ -34,4 +34,4 @@ namespace HttpServer void setUpdateModule(); void setProcessQueue(); }; -}; +} diff --git a/src/server/ServerSettings.cpp b/src/server/ServerSettings.cpp index 166f2dc..3677a62 100644 --- a/src/server/ServerSettings.cpp +++ b/src/server/ServerSettings.cpp @@ -5,29 +5,29 @@ namespace HttpServer { - ServerSettings::~ServerSettings() - { + ServerSettings::~ServerSettings() { this->clear(); } - void ServerSettings::addDataVariant(DataVariant::Abstract *dataVariant) - { - this->variants.emplace(dataVariant->getName(), dataVariant); + void ServerSettings::addDataVariant(DataVariant::Abstract *dataVariant) { + this->variants.emplace( + dataVariant->getName(), + dataVariant + ); } void ServerSettings::clear() { - if (false == this->variants.empty() ) + if (this->variants.empty() == false) { - for (auto &variant : this->variants) - { + for (auto &variant : this->variants) { delete variant.second; } this->variants.clear(); } - if (false == this->apps_tree.empty() ) + if (this->apps_tree.empty() == false) { std::unordered_set applications; this->apps_tree.collectApplicationSettings(applications); @@ -36,8 +36,7 @@ namespace HttpServer { try { - if (app->application_final) - { + if (app->application_final) { const std::string root = app->root_dir; app->application_final(root.data() ); } @@ -54,9 +53,8 @@ namespace HttpServer this->apps_tree.clear(); } - if (false == this->global.empty() ) - { + if (this->global.empty() == false) { this->global.clear(); } } -}; +} diff --git a/src/server/ServerSettings.h b/src/server/ServerSettings.h index 2ef64a2..221a466 100644 --- a/src/server/ServerSettings.h +++ b/src/server/ServerSettings.h @@ -22,4 +22,4 @@ namespace HttpServer void clear(); }; -}; +} diff --git a/src/server/ServerStructuresArguments.h b/src/server/ServerStructuresArguments.h index 0e10b8b..d00e8be 100644 --- a/src/server/ServerStructuresArguments.h +++ b/src/server/ServerStructuresArguments.h @@ -10,4 +10,4 @@ namespace HttpServer std::string config_path; bool force; }; -}; +} diff --git a/src/server/SocketsQueue.h b/src/server/SocketsQueue.h index ff6ec83..2473ee4 100644 --- a/src/server/SocketsQueue.h +++ b/src/server/SocketsQueue.h @@ -17,4 +17,4 @@ namespace HttpServer { }; -}; +} diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index 20ed3e0..1bbb42a 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -25,8 +25,11 @@ namespace HttpServer /** * Config - include file */ - bool ConfigParser::includeConfigFile(const std::string &fileName, std::string &strBuf, const std::size_t offset) - { + bool ConfigParser::includeConfigFile( + const std::string &fileName, + std::string &strBuf, + const std::size_t offset + ) { std::ifstream file(fileName); if ( ! file) { @@ -51,7 +54,11 @@ namespace HttpServer std::vector buf(file_size); file.read(buf.data(), file_size); - strBuf.insert(strBuf.begin() + offset, buf.cbegin(), buf.cend() ); + strBuf.insert( + strBuf.begin() + long(offset), + buf.cbegin(), + buf.cend() + ); } file.close(); @@ -63,12 +70,11 @@ namespace HttpServer * Config - add application */ bool ConfigParser::addApplication( - const std::unordered_multimap &app, - const ServerApplicationDefaultSettings &defaults, - std::vector &modules, - ServerApplicationsTree &apps_tree - ) - { + const std::unordered_multimap &app, + const ServerApplicationDefaultSettings &defaults, + std::vector &modules, + ServerApplicationsTree &apps_tree + ) { auto const it_name = app.find("server_name"); if (app.cend() == it_name) { @@ -84,16 +90,26 @@ namespace HttpServer size_t delimiter = app_name.find_first_of(whitespace); - if (delimiter) - { + if (delimiter) { size_t cur_pos = 0; - while (std::string::npos != delimiter) - { - std::string name = app_name.substr(cur_pos, delimiter - cur_pos); + while (std::string::npos != delimiter) { + std::string name = app_name.substr( + cur_pos, + delimiter - cur_pos + ); + names.emplace_back(std::move(name) ); - cur_pos = app_name.find_first_not_of(whitespace, delimiter + 1); - delimiter = app_name.find_first_of(whitespace, cur_pos); + + cur_pos = app_name.find_first_not_of( + whitespace, + delimiter + 1 + ); + + delimiter = app_name.find_first_of( + whitespace, + cur_pos + ); } std::string name = app_name.substr(cur_pos); @@ -102,8 +118,7 @@ namespace HttpServer auto const range_port = app.equal_range("listen"); - if (range_port.first == range_port.second) - { + if (range_port.first == range_port.second) { std::cout << "Error: application port is not set;" << std::endl; return false; } @@ -121,7 +136,7 @@ namespace HttpServer for (auto const &value : list) { - const int port = std::strtol(value.c_str(), nullptr, 10); + const int port = std::atoi(value.c_str()); if (port) { if (is_tls) { @@ -215,7 +230,13 @@ namespace HttpServer return false; } - std::function app_call = reinterpret_cast(addr); + std::function app_call = reinterpret_cast(addr); if ( ! app_call) { std::cout << "Error: invalid function 'application_call' in module '" << it_module->second << "';" << std::endl; @@ -256,18 +277,14 @@ namespace HttpServer bool success = true; - try - { - if (app_init) - { + try { + if (app_init) { const std::string root = root_dir; success = app_init(root.data() ); } } - catch (std::exception &exc) - { + catch (std::exception &exc) { std::cout << "Warning: an exception was thrown when the application '" << it_module->second << "' was initialized: " << exc.what() << std::endl; - success = false; } @@ -278,23 +295,27 @@ namespace HttpServer auto const it_temp_dir = app.find("temp_dir"); - std::string temp_dir = app.cend() != it_temp_dir ? it_temp_dir->second : defaults.temp_dir; + std::string temp_dir = app.cend() != it_temp_dir + ? it_temp_dir->second + : defaults.temp_dir; auto const it_request_max_size = app.find("request_max_size"); - const size_t request_max_size = app.cend() != it_request_max_size ? std::strtoull(it_request_max_size->second.c_str(), nullptr, 10) : defaults.request_max_size; + const size_t request_max_size = app.cend() != it_request_max_size + ? std::strtoull(it_request_max_size->second.c_str(), nullptr, 10) + : defaults.request_max_size; auto const it_module_update = app.find("server_module_update"); - std::string module_update = app.cend() != it_module_update ? it_module_update->second : ""; + std::string module_update = app.cend() != it_module_update + ? it_module_update->second + : std::string(); // Calculate module index size_t module_index = std::numeric_limits::max(); - for (size_t i = 0; i < modules.size(); ++i) - { - if (modules[i] == module) - { + for (size_t i = 0; i < modules.size(); ++i) { + if (modules[i] == module) { module_index = i; break; } @@ -349,8 +370,10 @@ namespace HttpServer * @param mimes_types * @return bool */ - bool ConfigParser::parseMimes(const std::string &fileName, std::unordered_map &mimes_types) - { + bool ConfigParser::parseMimes( + const std::string &fileName, + std::unordered_map &mimes_types + ) { std::ifstream file(fileName); if ( ! file) { @@ -436,7 +459,10 @@ namespace HttpServer ); if (ext_unit.empty() == false) { - mimes_types.emplace(std::move(ext_unit), mime_type); + mimes_types.emplace( + std::move(ext_unit), + mime_type + ); } ext_pos = ext.find_first_not_of(whitespace, delimiter); @@ -446,7 +472,10 @@ namespace HttpServer } else { - mimes_types.emplace(std::move(ext), std::move(mime_type) ); + mimes_types.emplace( + std::move(ext), + std::move(mime_type) + ); } cur_pos = end_pos + 1; @@ -492,8 +521,11 @@ namespace HttpServer /** * Config - parse */ - bool ConfigParser::loadConfig(const std::string &conf_file_name, ServerSettings &settings, std::vector &modules) - { + bool ConfigParser::loadConfig( + const std::string &conf_file_name, + ServerSettings &settings, + std::vector &modules + ) { std::string str_buf; if (includeConfigFile(conf_file_name, str_buf) == false) { @@ -552,17 +584,20 @@ namespace HttpServer std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos); - if ("include" == param_name) - { - this->includeConfigFile(param_value, str_buf, end_pos + 1); - } - else - { - global.emplace(std::move(param_name), std::move(param_value) ); + if ("include" == param_name) { + this->includeConfigFile( + param_value, + str_buf, + end_pos + 1 + ); + } else { + global.emplace( + std::move(param_name), + std::move(param_value) + ); } - } - else // if comment line - { + } else { + // if comment line end_pos = str_buf.find_first_of("\r\n", cur_pos); } } @@ -622,19 +657,18 @@ namespace HttpServer std::string param_value = str_buf.substr(cur_pos, delimiter - cur_pos); - if ("include" == param_name) - { + if ("include" == param_name) { cur_pos = end_pos + 1; this->includeConfigFile(param_value, str_buf, cur_pos); block_end = findBlockEnd(str_buf, cur_pos); + } else { + app.emplace( + std::move(param_name), + std::move(param_value) + ); } - else - { - app.emplace(std::move(param_name), std::move(param_value) ); - } - } - else // if comment line - { + } else { + // if comment line end_pos = str_buf.find_first_of("\r\n", cur_pos); } } @@ -687,11 +721,15 @@ namespace HttpServer { auto const it_default_temp_dir = global.find("default_temp_dir"); - const std::string default_temp_dir = global.cend() != it_default_temp_dir ? it_default_temp_dir->second : System::getTempDir(); + const std::string default_temp_dir = global.cend() != it_default_temp_dir + ? it_default_temp_dir->second + : System::getTempDir(); auto const it_default_request_max_size = global.find("request_max_size"); - const size_t default_request_max_size = global.cend() != it_default_request_max_size ? std::strtoull(it_default_request_max_size->second.c_str(), nullptr, 10) : 0; + const size_t default_request_max_size = global.cend() != it_default_request_max_size + ? std::strtoull(it_default_request_max_size->second.c_str(), nullptr, 10) + : 0; ServerApplicationDefaultSettings defaults { default_temp_dir, diff --git a/src/server/config/ConfigParser.h b/src/server/config/ConfigParser.h index f7805dc..f994a00 100644 --- a/src/server/config/ConfigParser.h +++ b/src/server/config/ConfigParser.h @@ -10,13 +10,16 @@ namespace HttpServer class ConfigParser { private: - struct ServerApplicationDefaultSettings - { + struct ServerApplicationDefaultSettings { std::string temp_dir; size_t request_max_size; }; - static bool includeConfigFile(const std::string &fileName, std::string &strBuf, const size_t offset = 0); + static bool includeConfigFile( + const std::string &fileName, + std::string &strBuf, + const size_t offset = 0 + ); static bool addApplication( const std::unordered_multimap &app, @@ -25,9 +28,16 @@ namespace HttpServer ServerApplicationsTree &apps_tree ); - static bool parseMimes(const std::string &fileName, std::unordered_map &mimes_types); + static bool parseMimes( + const std::string &fileName, + std::unordered_map &mimes_types + ); public: - bool loadConfig(const std::string &conf, ServerSettings &settings, std::vector &modules); + bool loadConfig( + const std::string &conf, + ServerSettings &settings, + std::vector &modules + ); }; } diff --git a/src/server/data-variant/Abstract.cpp b/src/server/data-variant/Abstract.cpp index 5a13e4a..37d9744 100644 --- a/src/server/data-variant/Abstract.cpp +++ b/src/server/data-variant/Abstract.cpp @@ -3,18 +3,16 @@ namespace DataVariant { - const std::string &Abstract::getName() const noexcept - { + const std::string &Abstract::getName() const noexcept { return this->data_variant_name; } - void *Abstract::createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const - { + void *Abstract::createStateStruct( + const Transfer::request_data *rd, + const std::unordered_map &contentParams + ) const { return nullptr; } - void Abstract::destroyStateStruct(void *st) const noexcept - { - - } -}; + void Abstract::destroyStateStruct(void *st) const noexcept {} +} diff --git a/src/server/data-variant/Abstract.h b/src/server/data-variant/Abstract.h index 0ec2e0a..f9f239c 100644 --- a/src/server/data-variant/Abstract.h +++ b/src/server/data-variant/Abstract.h @@ -17,13 +17,19 @@ namespace DataVariant public: /** - * virtual destructor + * Virtual destructor */ virtual ~Abstract() noexcept = default; - virtual void *createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const; + virtual void *createStateStruct( + const Transfer::request_data *rd, + const std::unordered_map &contentParams + ) const; - virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const = 0; + virtual bool parse( + const std::string &buf, + Transfer::request_data *rd, DataReceiver *dr + ) const = 0; virtual void destroyStateStruct(void *st) const noexcept; }; @@ -37,4 +43,4 @@ namespace DataVariant size_t left; void *reserved; }; -}; +} diff --git a/src/server/data-variant/FormUrlencoded.cpp b/src/server/data-variant/FormUrlencoded.cpp index 5535b14..08f8180 100644 --- a/src/server/data-variant/FormUrlencoded.cpp +++ b/src/server/data-variant/FormUrlencoded.cpp @@ -5,29 +5,27 @@ namespace DataVariant { - FormUrlencoded::FormUrlencoded() noexcept - { + FormUrlencoded::FormUrlencoded() noexcept { this->data_variant_name = "application/x-www-form-urlencoded"; } bool FormUrlencoded::parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const { - if (buf.empty() ) - { + if (buf.empty() ) { return 0 == dr->full_size || dr->full_size != dr->recv_total; } - for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) - { + for ( + size_t var_pos = 0, var_end = 0; + std::string::npos != var_end; + var_pos = var_end + 1 + ) { // Поиск следующего параметра var_end = buf.find('&', var_pos); - if (std::string::npos == var_end) - { - if (dr->full_size != dr->recv_total) - { + if (std::string::npos == var_end) { + if (dr->full_size != dr->recv_total) { dr->left = buf.size() - var_pos; - return true; } } @@ -38,26 +36,51 @@ namespace DataVariant if (delimiter >= var_end) { // Получить имя параметра - std::string var_name = Utils::urlDecode(buf.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) ); + std::string var_name = Utils::urlDecode( + buf.substr( + var_pos, + std::string::npos != var_end + ? var_end - var_pos + : std::string::npos + ) + ); // Сохранить параметр с пустым значением - rd->incoming_data.emplace(std::move(var_name), std::string() ); + rd->incoming_data.emplace( + std::move(var_name), + std::string() + ); } else { // Получить имя параметра - std::string var_name = Utils::urlDecode(buf.substr(var_pos, delimiter - var_pos) ); + std::string var_name = Utils::urlDecode( + buf.substr( + var_pos, + delimiter - var_pos + ) + ); ++delimiter; // Получить значение параметра - std::string var_value = Utils::urlDecode(buf.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) ); + std::string var_value = Utils::urlDecode( + buf.substr( + delimiter, + std::string::npos != var_end + ? var_end - delimiter + : std::string::npos + ) + ); // Сохранить параметр и значение - rd->incoming_data.emplace(std::move(var_name), std::move(var_value) ); + rd->incoming_data.emplace( + std::move(var_name), + std::move(var_value) + ); } } return true; } -}; +} diff --git a/src/server/data-variant/FormUrlencoded.h b/src/server/data-variant/FormUrlencoded.h index c52b575..141c525 100644 --- a/src/server/data-variant/FormUrlencoded.h +++ b/src/server/data-variant/FormUrlencoded.h @@ -12,4 +12,4 @@ namespace DataVariant public: virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const override; }; -}; +} diff --git a/src/server/data-variant/MultipartFormData.cpp b/src/server/data-variant/MultipartFormData.cpp index 2c24504..56ec8cc 100644 --- a/src/server/data-variant/MultipartFormData.cpp +++ b/src/server/data-variant/MultipartFormData.cpp @@ -6,13 +6,11 @@ namespace DataVariant { - MultipartFormData::MultipartFormData() noexcept - { + MultipartFormData::MultipartFormData() noexcept { this->data_variant_name = "multipart/form-data"; } - enum class ParsingState : uint8_t - { + enum class ParsingState : uint8_t { INITIALIZATION = 0, FIND_DATA_BLOCK, GET_DATA_BLOCK_TYPE, @@ -20,15 +18,13 @@ namespace DataVariant SAVE_DATA_BLOCK, }; - enum class BlockType : uint8_t - { + enum class BlockType : uint8_t { UNKNOWN = 0, DATA, FILE, }; - struct StateMultipartFormData - { + struct StateMultipartFormData { std::string block_name; std::string block_value; std::string file_tmp_name; @@ -40,14 +36,15 @@ namespace DataVariant BlockType block_type; }; - void *MultipartFormData::createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const - { + void *MultipartFormData::createStateStruct( + const Transfer::request_data *rd, + const std::unordered_map &contentParams + ) const { std::string boundary; auto const it = contentParams.find("boundary"); - if (contentParams.cend() != it) - { + if (contentParams.cend() != it) { boundary = it->second; } @@ -57,8 +54,7 @@ namespace DataVariant }; } - void MultipartFormData::destroyStateStruct(void *st) const noexcept - { + void MultipartFormData::destroyStateStruct(void *st) const noexcept { delete reinterpret_cast(st); } @@ -68,8 +64,11 @@ namespace DataVariant std::unordered_map headers; - for (size_t line_end = buf.find(nl.data(), cur); cur < end; line_end = buf.find(nl.data(), cur) ) - { + for ( + size_t line_end = buf.find(nl.data(), cur); + cur < end; + line_end = buf.find(nl.data(), cur) + ) { size_t delimiter = buf.find(':', cur); if (std::string::npos == delimiter || delimiter > line_end) @@ -77,10 +76,12 @@ namespace DataVariant std::string header_name = buf.substr(cur, line_end - cur); Utils::trim(header_name); Utils::toLower(header_name); - headers.emplace(std::move(header_name), std::string() ); - } - else - { + + headers.emplace( + std::move(header_name), + std::string() + ); + } else { std::string header_name = buf.substr(cur, delimiter - cur); Utils::trim(header_name); Utils::toLower(header_name); @@ -90,7 +91,10 @@ namespace DataVariant std::string header_value = buf.substr(delimiter, line_end - delimiter); Utils::trim(header_value); - headers.emplace(std::move(header_name), std::move(header_value) ); + headers.emplace( + std::move(header_name), + std::move(header_value) + ); } // Перейти к следующему заголовку @@ -107,8 +111,7 @@ namespace DataVariant size_t delimiter = header.find(';'); - if (std::string::npos == delimiter) - { + if (std::string::npos == delimiter) { return header_params; } @@ -116,8 +119,7 @@ namespace DataVariant Utils::trim(content_disposition); // Проверить соответствие указанного формата - if ("form-data" != content_disposition) - { + if ("form-data" != content_disposition) { return header_params; } @@ -129,13 +131,21 @@ namespace DataVariant if (std::string::npos == delimiter || delimiter > end) { - std::string param_name = header.substr(cur, (std::string::npos != end) ? end - cur: std::string::npos); + std::string param_name = header.substr( + cur, + std::string::npos != end + ? end - cur + : std::string::npos + ); + Utils::trim(param_name); Utils::toLower(param_name); - header_params.emplace(std::move(param_name), std::string() ); - } - else - { + + header_params.emplace( + std::move(param_name), + std::string() + ); + } else { std::string param_name = header.substr(cur, delimiter - cur); Utils::trim(param_name); Utils::toLower(param_name); @@ -148,26 +158,40 @@ namespace DataVariant { end = header.find(';', cur); - std::string param_value = header.substr(delimiter, (std::string::npos != end) ? end - delimiter : std::string::npos); + std::string param_value = header.substr( + delimiter, + std::string::npos != end + ? end - delimiter + : std::string::npos + ); + Utils::trim(param_value); - header_params.emplace(std::move(param_name), std::move(param_value) ); - } - else - { + header_params.emplace( + std::move(param_name), + std::move(param_value) + ); + } else { ++delimiter; cur = header.find('"', delimiter); end = header.find(';', cur); - std::string param_value = header.substr(delimiter, (std::string::npos != cur) ? cur - delimiter : std::string::npos); - - header_params.emplace(std::move(param_name), std::move(param_value) ); + std::string param_value = header.substr( + delimiter, + std::string::npos != cur + ? cur - delimiter + : std::string::npos + ); + + header_params.emplace( + std::move(param_name), + std::move(param_value) + ); } } - if (std::string::npos != end) - { + if (std::string::npos != end) { ++end; } } @@ -175,8 +199,11 @@ namespace DataVariant return header_params; } - bool MultipartFormData::parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const - { + bool MultipartFormData::parse( + const std::string &buf, + Transfer::request_data *rd, + DataReceiver *dr + ) const { StateMultipartFormData *ss = reinterpret_cast(dr->ss); size_t cur = 0; @@ -187,22 +214,18 @@ namespace DataVariant { case ParsingState::INITIALIZATION: { - if (ss->boundary.empty() ) - { + if (ss->boundary.empty() ) { return false; } const std::string data_end("--" + ss->boundary + "--\r\n"); - if (buf.size() < data_end.length() ) - { + if (buf.size() < data_end.length() ) { dr->left = buf.size(); - return dr->full_size != dr->recv_total; } - if (0 == buf.find(data_end) ) - { + if (0 == buf.find(data_end) ) { return dr->full_size == data_end.length() && dr->full_size == dr->recv_total; } @@ -210,8 +233,7 @@ namespace DataVariant cur = buf.find(first_block); - if (0 != cur) - { + if (0 != cur) { return false; } @@ -228,17 +250,14 @@ namespace DataVariant const std::string data_end("\r\n--" + ss->boundary + "--\r\n"); - if (data_end.length() > dr->left) - { + if (data_end.length() > dr->left) { return dr->full_size != dr->recv_total; } const size_t end = buf.find(data_end, cur); - if (end == cur) - { + if (end == cur) { dr->left -= data_end.length(); - return dr->full_size == dr->recv_total; } @@ -246,8 +265,7 @@ namespace DataVariant cur = buf.find(block_delimiter, cur); - if (std::string::npos == cur) - { + if (std::string::npos == cur) { return dr->full_size != dr->recv_total; } @@ -262,10 +280,8 @@ namespace DataVariant { const size_t end = buf.find("\r\n\r\n", cur); - if (std::string::npos == end) - { + if (std::string::npos == end) { dr->left = buf.size() - cur; - return dr->full_size != dr->recv_total; } @@ -276,8 +292,7 @@ namespace DataVariant auto const it = headers.find("content-disposition"); // Если заголовок не определён - if (headers.cend() == it) - { + if (headers.cend() == it) { return false; } @@ -286,8 +301,7 @@ namespace DataVariant // Поиск имени блока данных auto const it_name = header_params.find("name"); - if (header_params.cend() == it_name) - { + if (header_params.cend() == it_name) { return false; } @@ -304,8 +318,7 @@ namespace DataVariant // Найти тип файла auto const it_filetype = headers.find("content-type"); - if (headers.cend() != it_filetype) - { + if (headers.cend() != it_filetype) { ss->file_type = it_filetype->second; } @@ -315,8 +328,7 @@ namespace DataVariant // Создать файл ss->file.open(ss->file_tmp_name, std::ofstream::trunc | std::ofstream::binary); - if (false == ss->file.is_open() ) - { + if (ss->file.is_open() == false) { return false; } } @@ -340,16 +352,20 @@ namespace DataVariant switch (ss->block_type) { - case BlockType::DATA: - { - ss->block_value.append(buf.cbegin() + cur, buf.cbegin() + end); + case BlockType::DATA: { + ss->block_value.append( + buf.cbegin() + long(cur), + buf.cbegin() + long(end) + ); break; } - case BlockType::FILE: - { - ss->file.write(&buf[cur], end - cur); + case BlockType::FILE: { + ss->file.write( + &buf[cur], + std::streamsize(end - cur) + ); break; } @@ -358,10 +374,8 @@ namespace DataVariant return false; } - if (std::string::npos == block_end) - { + if (std::string::npos == block_end) { dr->left = buf.size() - end; - return dr->full_size != dr->recv_total; } @@ -376,19 +390,27 @@ namespace DataVariant { switch (ss->block_type) { - case BlockType::DATA: - { - rd->incoming_data.emplace(std::move(ss->block_name), std::move(ss->block_value) ); + case BlockType::DATA: { + rd->incoming_data.emplace( + std::move(ss->block_name), + std::move(ss->block_value) + ); break; } - case BlockType::FILE: - { - rd->incoming_files.emplace(std::move(ss->block_name), Transfer::FileIncoming(std::move(ss->file_tmp_name), std::move(ss->file_name), std::move(ss->file_type), ss->file.tellp() ) ); + case BlockType::FILE: { + rd->incoming_files.emplace( + std::move(ss->block_name), + Transfer::FileIncoming( + std::move(ss->file_tmp_name), + std::move(ss->file_name), + std::move(ss->file_type), + size_t(ss->file.tellp()) + ) + ); ss->file.close(); - break; } @@ -408,4 +430,4 @@ namespace DataVariant return dr->full_size != dr->recv_total; } -}; +} diff --git a/src/server/data-variant/MultipartFormData.h b/src/server/data-variant/MultipartFormData.h index 65e9071..1a45fc0 100644 --- a/src/server/data-variant/MultipartFormData.h +++ b/src/server/data-variant/MultipartFormData.h @@ -10,10 +10,17 @@ namespace DataVariant MultipartFormData() noexcept; public: - virtual void *createStateStruct(const Transfer::request_data *rd, const std::unordered_map &contentParams) const override; + virtual void *createStateStruct( + const Transfer::request_data *rd, + const std::unordered_map &contentParams + ) const override; - virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const override; + virtual bool parse( + const std::string &buf, + Transfer::request_data *rd, + DataReceiver *dr + ) const override; virtual void destroyStateStruct(void *st) const noexcept override; }; -}; +} diff --git a/src/server/data-variant/TextPlain.cpp b/src/server/data-variant/TextPlain.cpp index 1f4333c..ecb5efa 100644 --- a/src/server/data-variant/TextPlain.cpp +++ b/src/server/data-variant/TextPlain.cpp @@ -3,29 +3,27 @@ namespace DataVariant { - TextPlain::TextPlain() noexcept - { + TextPlain::TextPlain() noexcept { this->data_variant_name = "text/plain"; } bool TextPlain::parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const { - if (buf.empty() ) - { + if (buf.empty() ) { return 0 == dr->full_size || dr->full_size != dr->recv_total; } - for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1) - { + for ( + size_t var_pos = 0, var_end = 0; + std::string::npos != var_end; + var_pos = var_end + 1 + ) { // Поиск следующего параметра var_end = buf.find('&', var_pos); - if (std::string::npos == var_end) - { - if (dr->full_size != dr->recv_total) - { + if (std::string::npos == var_end) { + if (dr->full_size != dr->recv_total) { dr->left = buf.size() - var_pos; - return true; } } @@ -36,26 +34,40 @@ namespace DataVariant if (delimiter >= var_end) { // Получить имя параметра - std::string var_name = buf.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos); + std::string var_name = buf.substr( + var_pos, + std::string::npos != var_end + ? var_end - var_pos + : std::string::npos + ); // Сохранить параметр с пустым значением - rd->incoming_data.emplace(std::move(var_name), std::string() ); - } - else - { + rd->incoming_data.emplace( + std::move(var_name), + std::string() + ); + } else { // Получить имя параметра std::string var_name = buf.substr(var_pos, delimiter - var_pos); ++delimiter; // Получить значение параметра - std::string var_value = buf.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos); + std::string var_value = buf.substr( + delimiter, + std::string::npos != var_end + ? var_end - delimiter + : std::string::npos + ); // Сохранить параметр и значение - rd->incoming_data.emplace(std::move(var_name), std::move(var_value) ); + rd->incoming_data.emplace( + std::move(var_name), + std::move(var_value) + ); } } return true; } -}; +} diff --git a/src/server/data-variant/TextPlain.h b/src/server/data-variant/TextPlain.h index 8d98766..d6ba101 100644 --- a/src/server/data-variant/TextPlain.h +++ b/src/server/data-variant/TextPlain.h @@ -12,4 +12,4 @@ namespace DataVariant public: virtual bool parse(const std::string &buf, Transfer::request_data *rd, DataReceiver *dr) const override; }; -}; +} diff --git a/src/server/protocol/ServerHttp1.cpp b/src/server/protocol/ServerHttp1.cpp index 290ae9b..8bb5f17 100644 --- a/src/server/protocol/ServerHttp1.cpp +++ b/src/server/protocol/ServerHttp1.cpp @@ -10,14 +10,22 @@ namespace HttpServer { - ServerHttp1::ServerHttp1(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept + ServerHttp1::ServerHttp1( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls + ) noexcept : ServerProtocol(sock, settings, controls) { } - bool ServerHttp1::sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const - { + bool ServerHttp1::sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream + ) const { static const std::unordered_map status_list { { 200, "OK" }, { 206, "Partial Content" }, @@ -33,17 +41,14 @@ namespace HttpServer auto const it = status_list.find(static_cast(status) ); - if (status_list.cend() != it) - { + if (status_list.cend() != it) { const std::string &status = it->second; - str += ' ' + status; } str += "\r\n"; - for (auto const &header : headers) - { + for (auto const &header : headers) { str += header.first + ": " + header.second + "\r\n"; } @@ -52,25 +57,34 @@ namespace HttpServer return this->sock.nonblock_send(str, timeout) > 0; // >= 0 } - long ServerHttp1::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const - { - const long send_size = this->sock.nonblock_send(src, size, timeout); + long ServerHttp1::sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const { + const long send_size = this->sock.nonblock_send( + src, + size, + timeout + ); - if (send_size > 0) - { - dt->send_total += send_size; + if (send_size > 0) { + dt->send_total += static_cast(send_size); } return send_size; } - void ServerHttp1::close() - { + void ServerHttp1::close() { this->sock.close(); } - bool ServerHttp1::packRequestParameters(std::vector &buf, const struct Request &req, const std::string &rootDir) const - { + bool ServerHttp1::packRequestParameters( + std::vector &buf, + const struct Request &req, + const std::string &rootDir + ) const { Utils::packNumber(buf, static_cast(Transfer::ProtocolVariant::HTTP_1) ); Utils::packString(buf, rootDir); Utils::packString(buf, req.host); @@ -83,34 +97,48 @@ namespace HttpServer return true; } - void ServerHttp1::unpackResponseParameters(struct Request &req, const void *src) const - { - Utils::unpackContainer(req.outgoing_headers, reinterpret_cast(src) ); + void ServerHttp1::unpackResponseParameters( + struct Request &req, + const void *src + ) const { + Utils::unpackContainer( + req.outgoing_headers, + reinterpret_cast(src) + ); } - static bool getRequest(const Socket::Adapter &sock, struct Request &req, std::vector &buf, std::string &str_buf) - { + static bool getRequest( + const Socket::Adapter &sock, + struct Request &req, + std::vector &buf, + std::string &str_buf + ) { // Получить данные запроса от клиента - const long recv_size = sock.nonblock_recv(buf, req.timeout); + const long recv_size = sock.nonblock_recv( + buf, + req.timeout + ); - if (recv_size < 0 && str_buf.empty() ) - { + if (recv_size < 0 && str_buf.empty() ) { return false; } - if (recv_size > 0) // Если данные были получены - { - str_buf.append(buf.cbegin(), buf.cbegin() + recv_size); + if (recv_size > 0) { // Если данные были получены + str_buf.append( + buf.cbegin(), + buf.cbegin() + recv_size + ); } return true; } - static Http::StatusCode getRequestHeaders(struct Request &req, std::string &str_buf) - { + static Http::StatusCode getRequestHeaders( + struct Request &req, + std::string &str_buf + ) { // Если запрос пустой - if (str_buf.empty() ) - { + if (str_buf.empty() ) { return Http::StatusCode::BAD_REQUEST; } @@ -118,8 +146,7 @@ namespace HttpServer size_t headers_end = str_buf.find("\r\n\r\n"); // Если найден конец заголовков - if (std::string::npos == headers_end) - { + if (std::string::npos == headers_end) { return Http::StatusCode::BAD_REQUEST; } @@ -130,8 +157,7 @@ namespace HttpServer size_t str_end = str_buf.find("\r\n"); // Если не найден конец заголовка - if (std::string::npos == str_end) - { + if (std::string::npos == str_end) { return Http::StatusCode::BAD_REQUEST; } @@ -142,7 +168,11 @@ namespace HttpServer size_t delimiter = str_buf.find(' ', str_cur); // Получить метод запроса (GET, POST, PUT, DELETE, ...) - req.method = str_buf.substr(str_cur, delimiter - str_cur); + req.method = str_buf.substr( + str_cur, + delimiter - str_cur + ); + Utils::toLower(req.method); // Сохранить метод и параметры запроса @@ -153,19 +183,16 @@ namespace HttpServer size_t uri_end = str_buf.find(' ', delimiter); // Если окончание не найдено - if (std::string::npos == uri_end) - { + if (std::string::npos == uri_end) { uri_end = str_end; // то версия протокола HTTP - 0.9 // const std::string version = "0.9"; - } - else // Если окончание найдено - { + } else { + // Если окончание найдено str_buf[uri_end] = '\0'; const size_t ver_beg = uri_end + 6; // Пропустить "HTTP/" - if (ver_beg < str_end) - { + if (ver_beg < str_end) { // Получить версию протокола HTTP // const std::string version = str_buf.substr(ver_beg, str_end - ver_beg); } @@ -182,36 +209,53 @@ namespace HttpServer str_buf[str_end] = '\0'; // Цикл извлечения заголовков запроса - for (; str_cur != headers_end; str_end = str_buf.find("\r\n", str_cur), str_buf[str_end] = '\0') - { + for ( + ; + str_cur != headers_end; + str_end = str_buf.find("\r\n", str_cur), str_buf[str_end] = '\0' + ) { // Поиск разделителя названия заголовка и его значения delimiter = str_buf.find(':', str_cur); // Если разделитель найден в текущей строке if (delimiter < str_end) { - std::string header_name = str_buf.substr(str_cur, delimiter - str_cur); + std::string header_name = str_buf.substr( + str_cur, + delimiter - str_cur + ); + Utils::toLower(header_name); - std::string header_value = str_buf.substr(delimiter + 1, str_end - delimiter - 1); + std::string header_value = str_buf.substr( + delimiter + 1, + str_end - delimiter - 1 + ); + // Удалить лишние пробелы в начале и в конце строки Utils::trim(header_value); // Сохранить заголовок и его значение - req.incoming_headers.emplace(std::move(header_name), std::move(header_value) ); + req.incoming_headers.emplace( + std::move(header_name), + std::move(header_value) + ); } // Перейти к следующей строке str_cur = str_end + 2; } - str_buf.erase(str_buf.begin(), str_buf.begin() + headers_end + 2); + str_buf.erase(0, headers_end + 2); return Http::StatusCode::EMPTY; } - const ServerApplicationSettings *ServerHttp1::getApplicationSettings(struct Request &req, const bool isSecureConnection) const - { + const ServerApplicationSettings * + ServerHttp1::getApplicationSettings( + struct Request &req, + const bool isSecureConnection + ) const { // Получить доменное имя (или адрес) назначения запроса auto const it_host = req.incoming_headers.find("host"); @@ -229,14 +273,19 @@ namespace HttpServer const int default_port = isSecureConnection ? 443 : 80; // Получить номер порта - const int port = (std::string::npos != delimiter) ? std::strtol(host_header.substr(delimiter + 1).c_str(), nullptr, 10) : default_port; + const int port = (std::string::npos != delimiter) + ? std::atoi(host_header.substr(delimiter + 1).c_str()) + : default_port; // Поиск настроек приложения по имени const ServerApplicationSettings *app_sets = this->settings.apps_tree.find(req.host); // Если приложение найдено - if (app_sets && (app_sets->ports.cend() != app_sets->ports.find(port) || app_sets->tls_ports.cend() != app_sets->tls_ports.find(port) ) ) - { + if (app_sets && ( + app_sets->ports.cend() != app_sets->ports.find(port) || + app_sets->tls_ports.cend() != app_sets->tls_ports.find(port) + ) + ) { return app_sets; } } @@ -244,13 +293,15 @@ namespace HttpServer return nullptr; } - Http::StatusCode ServerHttp1::getRequestData(struct Request &req, std::string &str_buf, const ServerApplicationSettings &appSets) const - { + Http::StatusCode ServerHttp1::getRequestData( + struct Request &req, + std::string &str_buf, + const ServerApplicationSettings &appSets + ) const { // Определить вариант данных запроса (заодно проверить, есть ли данные) auto const it = req.incoming_headers.find("content-type"); - if (req.incoming_headers.cend() == it) - { + if (req.incoming_headers.cend() == it) { return Http::StatusCode::EMPTY; } @@ -270,33 +321,57 @@ namespace HttpServer data_variant_name = header_value.substr(0, delimiter); Utils::trim(data_variant_name); - for (size_t str_param_cur = delimiter + 1, str_param_end = 0; std::string::npos != str_param_end; str_param_cur = str_param_end + 1) - { + for ( + size_t str_param_cur = delimiter + 1, str_param_end = 0; + std::string::npos != str_param_end; + str_param_cur = str_param_end + 1 + ) { str_param_end = header_value.find(';', str_param_cur); delimiter = header_value.find('=', str_param_cur); if (delimiter >= str_param_end) { - std::string param_name = header_value.substr(str_param_cur, std::string::npos != str_param_end ? str_param_end - str_param_cur : std::string::npos); + std::string param_name = header_value.substr( + str_param_cur, + std::string::npos != str_param_end + ? str_param_end - str_param_cur + : std::string::npos + ); + Utils::trim(param_name); - content_params.emplace(std::move(param_name), std::string() ); + + content_params.emplace( + std::move(param_name), + std::string() + ); } else { - std::string param_name = header_value.substr(str_param_cur, delimiter - str_param_cur); + std::string param_name = header_value.substr( + str_param_cur, + delimiter - str_param_cur + ); + Utils::trim(param_name); ++delimiter; - std::string param_value = header_value.substr(delimiter, std::string::npos != str_param_end ? str_param_end - delimiter : std::string::npos); + std::string param_value = header_value.substr( + delimiter, + std::string::npos != str_param_end + ? str_param_end - delimiter + : std::string::npos + ); + Utils::trim(param_value); - content_params.emplace(std::move(param_name), std::move(param_value) ); + content_params.emplace( + std::move(param_name), + std::move(param_value) + ); } } - } - else - { + } else { data_variant_name = header_value; } @@ -304,8 +379,7 @@ namespace HttpServer auto const variant = this->settings.variants.find(data_variant_name); // Если сервер не поддерживает формат полученных данных - if (this->settings.variants.cend() == variant) - { + if (this->settings.variants.cend() == variant) { return Http::StatusCode::BAD_REQUEST; } @@ -316,14 +390,16 @@ namespace HttpServer auto const it_len = req.incoming_headers.find("content-length"); - if (req.incoming_headers.cend() != it_len) - { - data_length = std::strtoull(it_len->second.c_str(), nullptr, 10); + if (req.incoming_headers.cend() != it_len) { + data_length = std::strtoull( + it_len->second.c_str(), + nullptr, + 10 + ); } // Если размер запроса превышает лимит (если лимит был установлен) - if (data_length > appSets.request_max_size && 0 != appSets.request_max_size) - { + if (data_length > appSets.request_max_size && 0 != appSets.request_max_size) { return Http::StatusCode::REQUEST_ENTITY_TOO_LARGE; } @@ -338,16 +414,15 @@ namespace HttpServer std::string data_buf; - if (data_length >= str_buf.length() ) - { + if (str_buf.length() <= data_length) { dr.recv_total = str_buf.length(); - data_buf.swap(str_buf); - } - else - { - data_buf.assign(str_buf.cbegin(), str_buf.cbegin() + data_length); - str_buf.erase(str_buf.begin(), str_buf.begin() + data_length); + } else { + data_buf.assign( + str_buf, 0, data_length + ); + + str_buf.erase(0, data_length); dr.recv_total = data_buf.size(); } @@ -356,21 +431,33 @@ namespace HttpServer while (result && dr.full_size > dr.recv_total) { - std::vector buf(dr.full_size - dr.recv_total >= 512 * 1024 ? 512 * 1024 : dr.full_size - dr.recv_total); + std::vector buf( + dr.full_size - dr.recv_total >= 512 * 1024 + ? 512 * 1024 + : dr.full_size - dr.recv_total + ); - long recv_size = this->sock.nonblock_recv(buf.data(), buf.size(), req.timeout); + long recv_size = this->sock.nonblock_recv( + buf.data(), + buf.size(), + req.timeout + ); - if (recv_size <= 0) - { + if (recv_size <= 0) { result = false; - break; } - dr.recv_total += recv_size; + dr.recv_total += static_cast(recv_size); - data_buf.erase(data_buf.begin(), data_buf.end() - dr.left); - data_buf.append(buf.cbegin(), buf.cbegin() + recv_size); + data_buf.erase( + 0, data_buf.length() - dr.left + ); + + data_buf.append( + buf.data(), + 0, static_cast(recv_size) + ); dr.left = 0; @@ -379,27 +466,30 @@ namespace HttpServer data_variant->destroyStateStruct(dr.ss); - // Разобрать данные на составляющие - if (false == result) - { - for (auto const &it : req.incoming_files) - { + if (false == result) { + for (auto const &it : req.incoming_files) { std::remove(it.second.getTmpName().c_str() ); } return Http::StatusCode::BAD_REQUEST; } - if (dr.left) - { - str_buf.assign(data_buf.cend() - dr.left, data_buf.cend() ); + if (dr.left) { + str_buf.assign( + data_buf, + data_buf.length() - dr.left, + data_buf.length() + ); } return Http::StatusCode::EMPTY; } - static void sendStatus(const Socket::Adapter &sock, const struct Request &req, const Http::StatusCode statusCode) - { + static void sendStatus( + const Socket::Adapter &sock, + const struct Request &req, + const Http::StatusCode statusCode + ) { static const std::unordered_map status_list { { 400, "Bad Request" }, { 404, "Not Found" }, @@ -408,8 +498,7 @@ namespace HttpServer auto const it = status_list.find(static_cast(statusCode) ); - if (status_list.cend() != it) - { + if (status_list.cend() != it) { const std::string &status = it->second; std::string headers("HTTP/1.1 " + std::to_string(static_cast(statusCode) ) + ' ' + status + "\r\n\r\n"); @@ -418,19 +507,27 @@ namespace HttpServer } } - static void getConnectionParams(struct Request &req, const bool isSecureConnection) - { + static void getConnectionParams( + struct Request &req, + const bool isSecureConnection + ) { auto const it_in_connection = req.incoming_headers.find("connection"); auto const it_out_connection = req.outgoing_headers.find("connection"); - if (req.incoming_headers.cend() != it_in_connection && req.outgoing_headers.cend() != it_out_connection) - { + if ( + req.incoming_headers.cend() != it_in_connection && + req.outgoing_headers.cend() != it_out_connection + ) { const std::string connection_in = Utils::getLowerString(it_in_connection->second); const std::string connection_out = Utils::getLowerString(it_out_connection->second); auto const incoming_params = Utils::explode(connection_in, ','); - auto const it = std::find(incoming_params.cbegin(), incoming_params.cend(), connection_out); + auto const it = std::find( + incoming_params.cbegin(), + incoming_params.cend(), + connection_out + ); if (incoming_params.cend() != it) { @@ -440,8 +537,7 @@ namespace HttpServer { --req.keep_alive_count; - if (0 < req.keep_alive_count) - { + if (0 < req.keep_alive_count) { req.connection_params |= ConnectionParams::CONNECTION_REUSE; } } @@ -453,24 +549,19 @@ namespace HttpServer { const std::string upgrade = Utils::getLowerString(it_out_upgrade->second); - if ("h2" == upgrade) - { - if (isSecureConnection) - { + if ("h2" == upgrade) { + if (isSecureConnection) { req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; req.connection_params |= ConnectionParams::CONNECTION_REUSE; } } - else if ("h2c" == upgrade) - { - if (false == isSecureConnection) - { + else if ("h2c" == upgrade) { + if (false == isSecureConnection) { req.protocol_variant = Transfer::ProtocolVariant::HTTP_2; req.connection_params |= ConnectionParams::CONNECTION_REUSE; } } - else if ("websocket" == upgrade) - { + else if ("websocket" == upgrade) { req.connection_params |= ConnectionParams::CONNECTION_LEAVE_OPEN; } } @@ -479,58 +570,61 @@ namespace HttpServer } } - void ServerHttp1::useHttp1Protocol(struct Request &req, std::vector &buf, std::string &str_buf) const - { - if (false == getRequest(this->sock, req, buf, str_buf) ) - { + void ServerHttp1::useHttp1Protocol( + struct Request &req, + std::vector &buf, + std::string &str_buf + ) const { + if (getRequest(this->sock, req, buf, str_buf) == false) { return; } Http::StatusCode error_code = getRequestHeaders(req, str_buf); - if (error_code != Http::StatusCode::EMPTY) - { + if (error_code != Http::StatusCode::EMPTY) { sendStatus(this->sock, req, error_code); - return; } - const ServerApplicationSettings *app_sets = this->getApplicationSettings(req, this->sock.get_tls_session() != 0); + const ServerApplicationSettings *app_sets = this->getApplicationSettings( + req, + this->sock.get_tls_session() != nullptr + ); // Если приложение не найдено - if (nullptr == app_sets) - { + if (nullptr == app_sets) { sendStatus(this->sock, req, Http::StatusCode::NOT_FOUND); - return; } error_code = this->getRequestData(req, str_buf, *app_sets); - if (error_code != Http::StatusCode::EMPTY) - { + if (error_code != Http::StatusCode::EMPTY) { sendStatus(this->sock, req, error_code); - return; } this->runApplication(req, *app_sets); - for (auto const &it : req.incoming_files) - { + for (auto const &it : req.incoming_files) { std::remove(it.second.getTmpName().c_str() ); } - if (EXIT_SUCCESS == req.app_exit_code) - { - getConnectionParams(req, this->sock.get_tls_session() != 0); + if (EXIT_SUCCESS == req.app_exit_code) { + getConnectionParams( + req, + this->sock.get_tls_session() != nullptr + ); - Sendfile::xSendfile(std::ref(*this), req, this->settings.mimes_types); + Sendfile::xSendfile( + std::ref(*this), + req, + this->settings.mimes_types + ); } } - static bool isConnectionLeaveOpen(const struct Request &req) - { + static bool isConnectionLeaveOpen(const struct Request &req) { return (req.connection_params & ConnectionParams::CONNECTION_LEAVE_OPEN) == ConnectionParams::CONNECTION_LEAVE_OPEN; } @@ -544,8 +638,7 @@ namespace HttpServer std::vector buf(4096); std::string str_buf; - do - { + do { // Подготовить параметры для получения данных req.connection_params = ConnectionParams::CONNECTION_CLOSE; req.app_exit_code = EXIT_FAILURE; @@ -556,11 +649,10 @@ namespace HttpServer } while (Sendfile::isConnectionReuse(req) ); - if (isConnectionLeaveOpen(req) ) - { + if (isConnectionLeaveOpen(req) ) { return new ServerWebSocket(*this); } return this; } -}; +} diff --git a/src/server/protocol/ServerHttp1.h b/src/server/protocol/ServerHttp1.h index cc7f2c3..258e2c8 100644 --- a/src/server/protocol/ServerHttp1.h +++ b/src/server/protocol/ServerHttp1.h @@ -8,22 +8,57 @@ namespace HttpServer class ServerHttp1 : public ServerProtocol { private: - const ServerApplicationSettings *getApplicationSettings(struct Request &rp, const bool isSecureConnection) const; - Http::StatusCode getRequestData(struct Request &rp, std::string &str_buf, const ServerApplicationSettings &appSets) const; + const ServerApplicationSettings *getApplicationSettings( + struct Request &rp, + const bool isSecureConnection + ) const; + + Http::StatusCode getRequestData( + struct Request &rp, + std::string &str_buf, + const ServerApplicationSettings &appSets + ) const; protected: - void useHttp1Protocol(struct Request &rp, std::vector &buf, std::string &str_buf) const; + void useHttp1Protocol( + struct Request &rp, + std::vector &buf, + std::string &str_buf + ) const; public: - ServerHttp1(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept; + ServerHttp1( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls + ) noexcept; + + virtual bool sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream + ) const override; + + virtual long sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const override; - virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const override; - virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const override; + virtual bool packRequestParameters( + std::vector &buf, + const struct Request &rp, + const std::string &rootDir + ) const override; - virtual bool packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const override; - virtual void unpackResponseParameters(struct Request &req, const void *src) const override; + virtual void unpackResponseParameters( + struct Request &req, + const void *src + ) const override; virtual ServerProtocol *process() override; virtual void close() override; }; -}; +} diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp index 5ef4991..12ae2be 100644 --- a/src/server/protocol/ServerHttp2.cpp +++ b/src/server/protocol/ServerHttp2.cpp @@ -7,7 +7,12 @@ namespace HttpServer { - ServerHttp2::ServerHttp2(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, SocketsQueue &sockets) noexcept + ServerHttp2::ServerHttp2( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, + SocketsQueue &sockets + ) noexcept : ServerHttp2Protocol(sock, settings, controls, nullptr), sockets(sockets) { @@ -17,8 +22,13 @@ namespace HttpServer this->sock.close(); } - static uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags, const uint32_t streamId) noexcept - { + static uint8_t *setHttp2FrameHeader( + uint8_t *addr, + const uint32_t frameSize, + const Http2::FrameType frameType, + const Http2::FrameFlag frameFlags, + const uint32_t streamId + ) noexcept { Utils::hton24(addr, frameSize); *(addr + 3) = static_cast(frameType); *(addr + 4) = static_cast(frameFlags); @@ -27,8 +37,11 @@ namespace HttpServer return (addr + Http2::FRAME_HEADER_SIZE); } - static Http2::IncStream &getStreamData(std::unordered_map &streams, const uint32_t streamId, Http2::ConnectionData &conn) noexcept - { + static Http2::IncStream &getStreamData( + std::unordered_map &streams, + const uint32_t streamId, + Http2::ConnectionData &conn + ) noexcept { auto it = streams.find(streamId); if (streams.end() != it) { @@ -38,12 +51,22 @@ namespace HttpServer return streams.emplace(streamId, Http2::IncStream(streamId, conn) ).first->second; } - static void sendWindowUpdate(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, const Http2::IncStream &stream, const uint32_t size) noexcept - { + static void sendWindowUpdate( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + const Http2::IncStream &stream, + const uint32_t size + ) noexcept { std::array buf; uint8_t *addr = buf.data(); - addr = setHttp2FrameHeader(addr, sizeof(uint32_t), Http2::FrameType::WINDOW_UPDATE, Http2::FrameFlag::EMPTY, stream.stream_id); + addr = setHttp2FrameHeader( + addr, + sizeof(uint32_t), + Http2::FrameType::WINDOW_UPDATE, + Http2::FrameFlag::EMPTY, + stream.stream_id + ); *reinterpret_cast(addr) = ::htonl(size); @@ -52,8 +75,12 @@ namespace HttpServer sock.nonblock_send(buf.data(), buf.size(), timeout); } - static Http2::ErrorCode parseHttp2Data(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) - { + static Http2::ErrorCode parseHttp2Data( + Http2::FrameMeta &meta, + Http2::IncStream &stream, + const uint8_t *src, + const uint8_t *end + ) { if (0 == meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -91,24 +118,20 @@ namespace HttpServer buf.append(src, end - padding); - dr->recv_total += end - padding - src; + dr->recv_total += size_t(end - src) - padding; - if (dr->data_variant->parse(buf, rd, dr) ) - { - buf.erase(buf.begin(), buf.end() - dr->left); - } - else - { + if (dr->data_variant->parse(buf, rd, dr) ) { + buf.erase( + 0, buf.length() - dr->left + ); + } else { error_code = Http2::ErrorCode::PROTOCOL_ERROR; } - } - else - { + } else { error_code = Http2::ErrorCode::PROTOCOL_ERROR; } - if (meta.flags & Http2::FrameFlag::END_STREAM) - { + if (meta.flags & Http2::FrameFlag::END_STREAM) { stream.state = Http2::StreamState::HALF_CLOSED; ServerProtocol::destroyDataReceiver(stream.reserved); @@ -118,9 +141,15 @@ namespace HttpServer return error_code; } - static Http2::ErrorCode parseHttp2Headers(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) - { - stream.state = (meta.flags & Http2::FrameFlag::END_STREAM) ? Http2::StreamState::HALF_CLOSED : Http2::StreamState::OPEN; + static Http2::ErrorCode parseHttp2Headers( + Http2::FrameMeta &meta, + Http2::IncStream &stream, + const uint8_t *src, + const uint8_t *end + ) { + stream.state = (meta.flags & Http2::FrameFlag::END_STREAM) + ? Http2::StreamState::HALF_CLOSED + : Http2::StreamState::OPEN; uint8_t padding = 0; @@ -138,7 +167,9 @@ namespace HttpServer if (meta.flags & Http2::FrameFlag::PRIORITY) { // Stream id - const uint32_t depend_stream_id = ::ntohl(*reinterpret_cast(src) ) & ~(1 << 31); + const uint32_t depend_stream_id = ::ntohl( + *reinterpret_cast(src) + ) & ~(uint32_t(1) << 31); src += sizeof(uint32_t); @@ -148,15 +179,19 @@ namespace HttpServer src += sizeof(uint8_t); } - if (HPack::unpack(src, end - src - padding, stream) == false) { + if (HPack::unpack(src, size_t(end - src) - padding, stream) == false) { return Http2::ErrorCode::COMPRESSION_ERROR; } return Http2::ErrorCode::NO_ERROR; } - static Http2::ErrorCode parseHttp2rstStream(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) - { + static Http2::ErrorCode parseHttp2rstStream( + Http2::FrameMeta &meta, + Http2::IncStream &stream, + const uint8_t *src, + const uint8_t *end + ) { if (Http2::StreamState::IDLE == stream.state) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -171,7 +206,9 @@ namespace HttpServer return Http2::ErrorCode::FRAME_SIZE_ERROR; } - const Http2::ErrorCode error_code = static_cast(::ntohl(*reinterpret_cast(src) ) ); + const Http2::ErrorCode error_code = static_cast( + ::ntohl(*reinterpret_cast(src) ) + ); if (Http2::ErrorCode::NO_ERROR != error_code) { // DEBUG @@ -180,8 +217,12 @@ namespace HttpServer return Http2::ErrorCode::NO_ERROR; } - static Http2::ErrorCode parseHttp2Settings(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) - { + static Http2::ErrorCode parseHttp2Settings( + Http2::FrameMeta &meta, + Http2::IncStream &stream, + const uint8_t *src, + const uint8_t *end + ) { if (0 != meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -198,7 +239,9 @@ namespace HttpServer while (src != end) { - const Http2::ConnectionSetting setting = static_cast(ntohs(*reinterpret_cast(src) ) ); + const Http2::ConnectionSetting setting = static_cast( + ntohs(*reinterpret_cast(src) ) + ); src += sizeof(uint16_t); @@ -208,68 +251,74 @@ namespace HttpServer switch (setting) { - case Http2::ConnectionSetting::SETTINGS_HEADER_TABLE_SIZE: - settings.header_table_size = value; - break; - - case Http2::ConnectionSetting::SETTINGS_ENABLE_PUSH: - { - if (value > 1) { - return Http2::ErrorCode::PROTOCOL_ERROR; + case Http2::ConnectionSetting::SETTINGS_HEADER_TABLE_SIZE: { + settings.header_table_size = value; + break; } - settings.enable_push = value; + case Http2::ConnectionSetting::SETTINGS_ENABLE_PUSH: { + if (value > 1) { + return Http2::ErrorCode::PROTOCOL_ERROR; + } - break; - } + settings.enable_push = value; - case Http2::ConnectionSetting::SETTINGS_MAX_CONCURRENT_STREAMS: - settings.max_concurrent_streams = value; - break; + break; + } - case Http2::ConnectionSetting::SETTINGS_INITIAL_WINDOW_SIZE: - { - if (value >= uint32_t(1 << 31) ) { - return Http2::ErrorCode::FLOW_CONTROL_ERROR; + case Http2::ConnectionSetting::SETTINGS_MAX_CONCURRENT_STREAMS: { + settings.max_concurrent_streams = value; + break; } - settings.initial_window_size = value; + case Http2::ConnectionSetting::SETTINGS_INITIAL_WINDOW_SIZE: { + if (value >= uint32_t(1) << 31) { + return Http2::ErrorCode::FLOW_CONTROL_ERROR; + } - break; - } + settings.initial_window_size = value; - case Http2::ConnectionSetting::SETTINGS_MAX_FRAME_SIZE: - { - if (value < (1 << 14) || value >= (1 << 24) ) { - return Http2::ErrorCode::PROTOCOL_ERROR; + break; } - settings.max_frame_size = value; + case Http2::ConnectionSetting::SETTINGS_MAX_FRAME_SIZE: { + if (value < (1 << 14) || value >= (1 << 24) ) { + return Http2::ErrorCode::PROTOCOL_ERROR; + } - break; - } + settings.max_frame_size = value; - case Http2::ConnectionSetting::SETTINGS_MAX_HEADER_LIST_SIZE: - settings.max_header_list_size = value; - break; + break; + } - default: - break; + case Http2::ConnectionSetting::SETTINGS_MAX_HEADER_LIST_SIZE: { + settings.max_header_list_size = value; + break; + } + + default: + break; } } return Http2::ErrorCode::NO_ERROR; } - static Http2::ErrorCode parseHttp2GoAway(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) - { + static Http2::ErrorCode parseHttp2GoAway( + Http2::FrameMeta &meta, + Http2::IncStream &stream, + const uint8_t *src, + const uint8_t *end + ) { if (0 != meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } stream.state = Http2::StreamState::CLOSED; - const uint32_t last_stream_id = ::ntohl(*reinterpret_cast(src) ); + const uint32_t last_stream_id = ::ntohl( + *reinterpret_cast(src) + ); if (last_stream_id > 0) { @@ -277,7 +326,9 @@ namespace HttpServer src += sizeof(uint32_t); - const Http2::ErrorCode error_code = static_cast(::ntohl(*reinterpret_cast(src) ) ); + const Http2::ErrorCode error_code = static_cast( + ::ntohl(*reinterpret_cast(src) ) + ); if (Http2::ErrorCode::NO_ERROR != error_code) { @@ -286,8 +337,12 @@ namespace HttpServer return Http2::ErrorCode::NO_ERROR; } - static void ping(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const uint64_t pingData) - { + static void ping( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + Http2::ConnectionData &conn, + const uint64_t pingData + ) { constexpr uint32_t frame_size = sizeof(uint64_t); std::array buf; @@ -295,7 +350,13 @@ namespace HttpServer constexpr uint32_t stream_id = 0; - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::PING, Http2::FrameFlag::ACK, stream_id); + addr = setHttp2FrameHeader( + addr, + frame_size, + Http2::FrameType::PING, + Http2::FrameFlag::ACK, + stream_id + ); *reinterpret_cast(addr) = pingData; @@ -304,8 +365,9 @@ namespace HttpServer sock.nonblock_send(buf.data(), buf.size(), timeout); } - static Http2::ErrorCode parseHttp2Ping(Http2::FrameMeta &meta) - { + static Http2::ErrorCode parseHttp2Ping( + Http2::FrameMeta &meta + ) { if (0 != meta.stream_id) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -317,8 +379,12 @@ namespace HttpServer return Http2::ErrorCode::NO_ERROR; } - static Http2::ErrorCode parseHttp2WindowUpdate(Http2::FrameMeta &meta, Http2::IncStream &stream, const uint8_t *src, const uint8_t *end) - { + static Http2::ErrorCode parseHttp2WindowUpdate( + Http2::FrameMeta &meta, + Http2::IncStream &stream, + const uint8_t *src, + const uint8_t *end + ) { if (Http2::StreamState::RESERVED == stream.state) { return Http2::ErrorCode::PROTOCOL_ERROR; } @@ -330,12 +396,14 @@ namespace HttpServer return Http2::ErrorCode::FRAME_SIZE_ERROR; } - const uint32_t window_size_increment = ::ntohl(*reinterpret_cast(src) ); + const uint32_t window_size_increment = ::ntohl( + *reinterpret_cast(src) + ); if (0 == window_size_increment) { return Http2::ErrorCode::PROTOCOL_ERROR; } - else if (window_size_increment >= uint32_t(1 << 31) ) { + else if (window_size_increment >= uint32_t(1) << 31) { return Http2::ErrorCode::FLOW_CONTROL_ERROR; } @@ -349,25 +417,42 @@ namespace HttpServer return Http2::ErrorCode::NO_ERROR; } - static void rstStream(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::IncStream &stream, const Http2::ErrorCode errorCode) - { + static void rstStream( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + Http2::IncStream &stream, + const Http2::ErrorCode errorCode + ) { constexpr uint32_t frame_size = sizeof(uint32_t); std::array buf; uint8_t *addr = buf.data(); - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::RST_STREAM, Http2::FrameFlag::EMPTY, stream.stream_id); + addr = setHttp2FrameHeader( + addr, + frame_size, + Http2::FrameType::RST_STREAM, + Http2::FrameFlag::EMPTY, + stream.stream_id + ); - *reinterpret_cast(addr) = ::htonl(static_cast(errorCode) ); + *reinterpret_cast(addr) = ::htonl( + static_cast(errorCode) + ); const std::unique_lock lock(stream.conn.sync.mtx); sock.nonblock_send(buf.data(), buf.size(), timeout); } - static void sendSettings(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const uint8_t *src, const uint8_t *end) - { - const uint32_t frame_size = end - src; + static void sendSettings( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + Http2::ConnectionData &conn, + const uint8_t *src, + const uint8_t *end + ) { + const uint32_t frame_size = uint32_t(end - src); std::vector buf(Http2::FRAME_HEADER_SIZE + frame_size); @@ -375,7 +460,13 @@ namespace HttpServer constexpr uint32_t stream_id = 0; - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::SETTINGS, Http2::FrameFlag::EMPTY, stream_id); + addr = setHttp2FrameHeader( + addr, + frame_size, + Http2::FrameType::SETTINGS, + Http2::FrameFlag::EMPTY, + stream_id + ); std::copy(src, end, addr); @@ -384,31 +475,51 @@ namespace HttpServer sock.nonblock_send(buf.data(), buf.size(), timeout); } - static void goAway(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const uint32_t lastStreamId, const Http2::ErrorCode errorCode) - { + static void goAway( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + Http2::ConnectionData &conn, + const uint32_t lastStreamId, + const Http2::ErrorCode errorCode + ) { constexpr uint32_t frame_size = sizeof(uint32_t) * 2; std::array buf; uint8_t *addr = buf.data(); - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::RST_STREAM, Http2::FrameFlag::EMPTY, 0); + addr = setHttp2FrameHeader( + addr, + frame_size, + Http2::FrameType::RST_STREAM, + Http2::FrameFlag::EMPTY, + 0 + ); - *reinterpret_cast(addr) = ::htonl(static_cast(lastStreamId) ); - *reinterpret_cast(addr + sizeof(uint32_t) ) = ::htonl(static_cast(errorCode) ); + *reinterpret_cast(addr) = ::htonl(lastStreamId); + + *reinterpret_cast(addr + sizeof(uint32_t) ) = ::htonl( + static_cast(errorCode) + ); const std::unique_lock lock(conn.sync.mtx); sock.nonblock_send(buf.data(), buf.size(), timeout); } - static bool getClientPreface(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout) - { + static bool getClientPreface( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout + ) { std::array buf; - const long read_size = sock.nonblock_recv(buf.data(), buf.size(), timeout); + const long read_size = sock.nonblock_recv( + buf.data(), + buf.size(), + timeout + ); - if (read_size != buf.size() ) { + if (buf.size() != read_size) { return false; } @@ -426,8 +537,12 @@ namespace HttpServer return 0 == compare; } - static void sendEmptySettings(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, Http2::ConnectionData &conn, const Http2::FrameFlag flags) - { + static void sendEmptySettings( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + Http2::ConnectionData &conn, + const Http2::FrameFlag flags + ) { constexpr uint32_t frame_size = 0; std::array buf; @@ -435,32 +550,52 @@ namespace HttpServer constexpr uint32_t stream_id = 0; - addr = setHttp2FrameHeader(addr, frame_size, Http2::FrameType::SETTINGS, flags, stream_id); + addr = setHttp2FrameHeader( + addr, + frame_size, + Http2::FrameType::SETTINGS, + flags, + stream_id + ); const std::unique_lock lock(conn.sync.mtx); sock.nonblock_send(buf.data(), buf.size(), timeout); } - static bool getNextHttp2FrameMeta(const Socket::Adapter &sock, const std::chrono::milliseconds &timeout, std::vector &buf, Http2::FrameMeta &meta, long &read_size) - { - if (read_size <= static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) - { - if (read_size == static_cast(meta.length + Http2::FRAME_HEADER_SIZE) ) { + static bool getNextHttp2FrameMeta( + const Socket::Adapter &sock, + const std::chrono::milliseconds &timeout, + std::vector &buf, + Http2::FrameMeta &meta, + long &read_size + ) { + const long length = long( + meta.length + Http2::FRAME_HEADER_SIZE + ); + + if (read_size <= length) { + if (read_size == length) { read_size = 0; } - read_size = sock.nonblock_recv(buf.data() + read_size, buf.size() - read_size, timeout); + read_size = sock.nonblock_recv( + buf.data() + read_size, + buf.size() - size_t(read_size), + timeout + ); - if (read_size < static_cast(Http2::FRAME_HEADER_SIZE) ) { + if (read_size < long(Http2::FRAME_HEADER_SIZE) ) { return false; } - } - else - { - std::copy(buf.cbegin() + meta.length + Http2::FRAME_HEADER_SIZE, buf.cbegin() + read_size, buf.begin() ); + } else { + std::copy( + buf.cbegin() + length, + buf.cbegin() + read_size, + buf.begin() + ); - read_size -= static_cast(meta.length + Http2::FRAME_HEADER_SIZE); + read_size -= length; } const uint8_t *addr = reinterpret_cast(buf.data() ); @@ -481,11 +616,24 @@ namespace HttpServer Http2::ConnectionData conn; - sendEmptySettings(this->sock, req.timeout, conn, Http2::FrameFlag::EMPTY); + sendEmptySettings( + this->sock, + req.timeout, + conn, + Http2::FrameFlag::EMPTY + ); if (getClientPreface(this->sock, req.timeout) == false) { constexpr uint32_t last_stream_id = 0; - goAway(this->sock, req.timeout, conn, last_stream_id, Http2::ErrorCode::PROTOCOL_ERROR); + + goAway( + this->sock, + req.timeout, + conn, + last_stream_id, + Http2::ErrorCode::PROTOCOL_ERROR + ); + return this; } @@ -512,7 +660,10 @@ namespace HttpServer break; } - const uint8_t *addr = reinterpret_cast(buf.data() ) + Http2::FRAME_HEADER_SIZE; + const uint8_t *addr = reinterpret_cast( + buf.data() + ) + Http2::FRAME_HEADER_SIZE; + const uint8_t *end = addr + meta.length; if (meta.stream_id > last_stream_id) { @@ -544,16 +695,17 @@ namespace HttpServer { DataVariant::DataReceiver *dr = reinterpret_cast(stream.reserved); - if (static_cast(stream.window_size_inc - conn.server_settings.max_frame_size) <= 0) + if (stream.window_size_inc - long(conn.server_settings.max_frame_size) <= 0) { - size_t update_size = conn.server_settings.initial_window_size + (dr->full_size - dr->recv_total) - stream.window_size_inc; + size_t update_size = conn.server_settings.initial_window_size + + (dr->full_size - dr->recv_total) - size_t(stream.window_size_inc); if (update_size > Http2::MAX_WINDOW_UPDATE) { update_size = Http2::MAX_WINDOW_UPDATE; } - sendWindowUpdate(this->sock, req.timeout, stream, static_cast(update_size) ); - sendWindowUpdate(this->sock, req.timeout, primary, static_cast(update_size) ); + sendWindowUpdate(this->sock, req.timeout, stream, uint32_t(update_size) ); + sendWindowUpdate(this->sock, req.timeout, primary, uint32_t(update_size) ); stream.window_size_inc += update_size; } @@ -593,7 +745,7 @@ namespace HttpServer { result = parseHttp2Settings(meta, stream, addr, end); - if (Http2::ErrorCode::NO_ERROR == result && false == (meta.flags & Http2::FrameFlag::ACK) ) + if (Http2::ErrorCode::NO_ERROR == result && (meta.flags & Http2::FrameFlag::ACK) == false) { conn.decoding_dynamic_table.changeHeaderTableSize(conn.client_settings.header_table_size); conn.decoding_dynamic_table.changeMaxHeaderListSize(conn.client_settings.max_header_list_size); @@ -612,7 +764,7 @@ namespace HttpServer { result = parseHttp2Ping(meta); - if (Http2::ErrorCode::NO_ERROR == result && false == (meta.flags & Http2::FrameFlag::ACK) ) + if (Http2::ErrorCode::NO_ERROR == result && (meta.flags & Http2::FrameFlag::ACK) == false) { const uint64_t ping_data = *reinterpret_cast(addr); ping(this->sock, req.timeout, conn, ping_data); @@ -668,7 +820,13 @@ namespace HttpServer conn.sync.event.wait(); } - goAway(this->sock, req.timeout, conn, last_stream_id, Http2::ErrorCode::NO_ERROR); + goAway( + this->sock, + req.timeout, + conn, + last_stream_id, + Http2::ErrorCode::NO_ERROR + ); for (auto &pair : streams) { destroyDataReceiver(pair.second.reserved); diff --git a/src/server/protocol/ServerHttp2.h b/src/server/protocol/ServerHttp2.h index 6910788..7871d3e 100644 --- a/src/server/protocol/ServerHttp2.h +++ b/src/server/protocol/ServerHttp2.h @@ -13,7 +13,12 @@ namespace HttpServer SocketsQueue &sockets; public: - ServerHttp2(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, SocketsQueue &sockets) noexcept; + ServerHttp2( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, + SocketsQueue &sockets + ) noexcept; virtual ServerProtocol *process() override; virtual void close() override; diff --git a/src/server/protocol/ServerHttp2Protocol.cpp b/src/server/protocol/ServerHttp2Protocol.cpp index df18c00..fd4ed10 100644 --- a/src/server/protocol/ServerHttp2Protocol.cpp +++ b/src/server/protocol/ServerHttp2Protocol.cpp @@ -7,7 +7,12 @@ namespace HttpServer { - ServerHttp2Protocol::ServerHttp2Protocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept + ServerHttp2Protocol::ServerHttp2Protocol( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, + Http2::IncStream *stream + ) noexcept : ServerProtocol(sock, settings, controls), stream(stream) { @@ -15,26 +20,33 @@ namespace HttpServer uint8_t ServerHttp2Protocol::getPaddingSize(const size_t dataSize) { - if (0 == dataSize) - { + if (0 == dataSize) { return 0; } std::random_device rd; + std::uniform_int_distribution dist; - uint8_t padding = rd(); + uint8_t padding = dist(rd); - while (dataSize <= padding) - { + while (dataSize <= padding) { padding /= 2; } return padding; } - bool ServerHttp2Protocol::sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const - { - headers.emplace(headers.begin(), ":status", std::to_string(static_cast(status) ) ); + bool ServerHttp2Protocol::sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream + ) const { + headers.emplace( + headers.begin(), + ":status", + std::to_string(static_cast(status)) + ); std::vector buf; buf.reserve(4096); @@ -42,24 +54,34 @@ namespace HttpServer HPack::pack(buf, headers, this->stream->conn.encoding_dynamic_table); - const uint32_t frame_size = buf.size() - Http2::FRAME_HEADER_SIZE; + const uint32_t frame_size = uint32_t( + buf.size() - Http2::FRAME_HEADER_SIZE + ); Http2::FrameFlag flags = Http2::FrameFlag::END_HEADERS; - if (endStream) - { + if (endStream) { flags |= Http2::FrameFlag::END_STREAM; } - this->stream->setHttp2FrameHeader(reinterpret_cast(buf.data() ), frame_size, Http2::FrameType::HEADERS, flags); + this->stream->setHttp2FrameHeader( + reinterpret_cast(buf.data() ), + frame_size, + Http2::FrameType::HEADERS, + flags + ); const std::unique_lock lock(this->stream->conn.sync.mtx); - return this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; // >= 0; + return this->sock.nonblock_send(buf.data(), buf.size(), timeout) > 0; } - long ServerHttp2Protocol::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const - { + long ServerHttp2Protocol::sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const { const uint8_t *data = reinterpret_cast(src); const Http2::ConnectionSettings &setting = this->stream->conn.client_settings; @@ -72,15 +94,15 @@ namespace HttpServer while (size != 0) { // TODO: test with data_size == 1 (padding length == 0) - size_t data_size = setting.max_frame_size < size ? setting.max_frame_size : size; + size_t data_size = setting.max_frame_size < size + ? setting.max_frame_size + : size; const uint8_t padding = getPaddingSize(data_size); const uint16_t padding_size = padding + sizeof(uint8_t); - if (padding_size) - { - if (data_size + padding_size > setting.max_frame_size) - { + if (padding_size) { + if (data_size + padding_size > setting.max_frame_size) { data_size = setting.max_frame_size - padding_size; } } @@ -93,27 +115,24 @@ namespace HttpServer { size_t update_size = (dt->full_size - dt->send_total) - this->stream->window_size_out; - if (update_size > Http2::MAX_WINDOW_UPDATE) - { + if (update_size > Http2::MAX_WINDOW_UPDATE) { update_size = Http2::MAX_WINDOW_UPDATE; } - sendWindowUpdate(this->sock, rp, static_cast(update_size) ); + sendWindowUpdate(this->sock, rp, uint32_t(update_size) ); this->stream->window_size_out += update_size; }*/ Http2::FrameFlag flags = Http2::FrameFlag::EMPTY; - if (dt->send_total + data_size >= dt->full_size) - { + if (dt->send_total + data_size >= dt->full_size) { flags |= Http2::FrameFlag::END_STREAM; } size_t cur = Http2::FRAME_HEADER_SIZE; - if (padding_size) - { + if (padding_size) { flags |= Http2::FrameFlag::PADDED; buf[cur] = padding; @@ -121,23 +140,38 @@ namespace HttpServer ++cur; } - this->stream->setHttp2FrameHeader(buf.data(), frame_size, Http2::FrameType::DATA, flags); - - std::copy(data, data + data_size, buf.begin() + cur); - - if (padding) - { - std::fill(buf.end() - padding, buf.end(), 0); + this->stream->setHttp2FrameHeader( + buf.data(), + static_cast(frame_size), + Http2::FrameType::DATA, + flags + ); + + std::copy( + data, + data + data_size, + buf.begin() + long(cur) + ); + + if (padding) { + std::fill( + buf.end() - padding, + buf.end(), + 0 + ); } this->stream->lock(); - const long sended = this->sock.nonblock_send(buf.data(), buf.size(), timeout); + const long sended = this->sock.nonblock_send( + buf.data(), + buf.size(), + timeout + ); this->stream->unlock(); - if (sended <= 0) - { + if (sended <= 0) { send_size = sended; break; } @@ -153,8 +187,11 @@ namespace HttpServer return send_size; } - bool ServerHttp2Protocol::packRequestParameters(std::vector &buf, const struct Request &req, const std::string &rootDir) const - { + bool ServerHttp2Protocol::packRequestParameters( + std::vector &buf, + const struct Request &req, + const std::string &rootDir + ) const { Utils::packNumber(buf, static_cast(Transfer::ProtocolVariant::HTTP_2) ); Utils::packString(buf, rootDir); Utils::packString(buf, req.host); @@ -177,8 +214,13 @@ namespace HttpServer return true; } - void ServerHttp2Protocol::unpackResponseParameters(struct Request &req, const void *src) const - { - Utils::unpackContainer(req.outgoing_headers, reinterpret_cast(src) ); + void ServerHttp2Protocol::unpackResponseParameters( + struct Request &req, + const void *src + ) const { + Utils::unpackContainer( + req.outgoing_headers, + reinterpret_cast(src) + ); } -}; +} diff --git a/src/server/protocol/ServerHttp2Protocol.h b/src/server/protocol/ServerHttp2Protocol.h index 7c84ba4..99adc42 100644 --- a/src/server/protocol/ServerHttp2Protocol.h +++ b/src/server/protocol/ServerHttp2Protocol.h @@ -14,12 +14,35 @@ namespace HttpServer static uint8_t getPaddingSize(const size_t dataSize); public: - ServerHttp2Protocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept; + ServerHttp2Protocol( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, Http2::IncStream *stream + ) noexcept; - virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const override; - virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const override; + virtual bool sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream + ) const override; - virtual bool packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const override; - virtual void unpackResponseParameters(struct Request &rp, const void *src) const override; + virtual long sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const override; + + virtual bool packRequestParameters( + std::vector &buf, + const struct Request &rp, + const std::string &rootDir + ) const override; + + virtual void unpackResponseParameters( + struct Request &rp, + const void *src + ) const override; }; -}; +} diff --git a/src/server/protocol/ServerHttp2Stream.cpp b/src/server/protocol/ServerHttp2Stream.cpp index 8a20c8a..346e0f1 100644 --- a/src/server/protocol/ServerHttp2Stream.cpp +++ b/src/server/protocol/ServerHttp2Stream.cpp @@ -5,14 +5,18 @@ namespace HttpServer { - ServerHttp2Stream::ServerHttp2Stream(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept + ServerHttp2Stream::ServerHttp2Stream( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, + Http2::IncStream *stream + ) noexcept : ServerHttp2Protocol(sock, settings, controls, stream) { } - void ServerHttp2Stream::close() - { + void ServerHttp2Stream::close() { this->stream->close(); } @@ -31,19 +35,20 @@ namespace HttpServer auto const it_scheme = headers.find(":scheme"); - if (headers.cend() == it_scheme) - { + if (headers.cend() == it_scheme) { return this; } const std::string &scheme = it_scheme->second; - const int default_port = (scheme == "https") ? 443 : (scheme == "http") ? 80 : 0; + const int default_port = (scheme == "https") + ? 443 + : (scheme == "http") + ? 80 : 0; auto const it_host = headers.find(":authority"); - if (headers.cend() == it_host) - { + if (headers.cend() == it_host) { return this; } @@ -56,20 +61,24 @@ namespace HttpServer req.host = host_header.substr(0, delimiter); // Получить номер порта - const int port = (std::string::npos != delimiter) ? std::strtol(host_header.substr(delimiter + 1).c_str(), nullptr, 10) : default_port; + const int port = std::string::npos != delimiter + ? std::atoi(host_header.substr(delimiter + 1).c_str()) + : default_port; const ServerApplicationSettings *app_sets = this->settings.apps_tree.find(req.host); // Если приложение найдено - if (nullptr == app_sets || (app_sets->ports.cend() == app_sets->ports.find(port) && app_sets->tls_ports.cend() == app_sets->tls_ports.find(port) ) ) - { + if (nullptr == app_sets || ( + app_sets->ports.cend() == app_sets->ports.find(port) && + app_sets->tls_ports.cend() == app_sets->tls_ports.find(port) + ) + ) { return this; } auto const it_method = headers.find(":method"); - if (headers.cend() == it_method) - { + if (headers.cend() == it_method) { return this; } @@ -77,8 +86,7 @@ namespace HttpServer auto const it_path = headers.find(":path"); - if (headers.cend() == it_path) - { + if (headers.cend() == it_path) { return this; } @@ -88,23 +96,25 @@ namespace HttpServer this->runApplication(req, *app_sets); - for (auto const &it : req.incoming_files) - { + for (auto const &it : req.incoming_files) { std::remove(it.second.getTmpName().c_str() ); } - if (EXIT_SUCCESS == req.app_exit_code) - { + if (EXIT_SUCCESS == req.app_exit_code) { // Http2::OutStream out(*stream); // auto tmp = req.protocol_data; // req.protocol_data = &out; - Sendfile::xSendfile(std::ref(*this), req, this->settings.mimes_types); + Sendfile::xSendfile( + std::ref(*this), + req, + this->settings.mimes_types + ); // req.protocol_data = tmp; } return this; } -}; +} diff --git a/src/server/protocol/ServerHttp2Stream.h b/src/server/protocol/ServerHttp2Stream.h index a4c18c1..1181983 100644 --- a/src/server/protocol/ServerHttp2Stream.h +++ b/src/server/protocol/ServerHttp2Stream.h @@ -8,9 +8,14 @@ namespace HttpServer class ServerHttp2Stream : public ServerHttp2Protocol { public: - ServerHttp2Stream(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept; + ServerHttp2Stream( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls, + Http2::IncStream *stream + ) noexcept; virtual ServerProtocol *process() override; virtual void close() override; }; -}; +} diff --git a/src/server/protocol/ServerProtocol.cpp b/src/server/protocol/ServerProtocol.cpp index 9adc151..42628eb 100644 --- a/src/server/protocol/ServerProtocol.cpp +++ b/src/server/protocol/ServerProtocol.cpp @@ -4,7 +4,11 @@ namespace HttpServer { - ServerProtocol::ServerProtocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept + ServerProtocol::ServerProtocol( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls + ) noexcept : sock(sock), settings(settings), controls(controls) { @@ -16,12 +20,15 @@ namespace HttpServer } - DataVariant::DataReceiver *ServerProtocol::createDataReceiver(const Transfer::request_data *rd, const std::unordered_map &variants) - { + DataVariant::DataReceiver * + ServerProtocol::createDataReceiver( + const Transfer::request_data *rd, + const std::unordered_map &variants + ) { auto const it = rd->incoming_headers.find("content-type"); - if (rd->incoming_headers.cend() == it) - { + if (rd->incoming_headers.cend() == it) { return nullptr; } @@ -36,45 +43,64 @@ namespace HttpServer size_t delimiter = header_value.find(';'); // Если есть дополнительные параметры - извлекаем их - if (std::string::npos != delimiter) - { + if (std::string::npos != delimiter) { data_variant_name = header_value.substr(0, delimiter); Utils::trim(data_variant_name); - for (size_t str_param_cur = delimiter + 1, str_param_end = 0; std::string::npos != str_param_end; str_param_cur = str_param_end + 1) - { + for ( + size_t str_param_cur = delimiter + 1, str_param_end = 0; + std::string::npos != str_param_end; + str_param_cur = str_param_end + 1 + ) { str_param_end = header_value.find(';', str_param_cur); delimiter = header_value.find('=', str_param_cur); - if (delimiter >= str_param_end) - { - std::string param_name = header_value.substr(str_param_cur, std::string::npos != str_param_end ? str_param_end - str_param_cur : std::string::npos); + if (delimiter >= str_param_end) { + std::string param_name = header_value.substr( + str_param_cur, + std::string::npos != str_param_end + ? str_param_end - str_param_cur + : std::string::npos + ); + Utils::trim(param_name); - content_params.emplace(std::move(param_name), std::string() ); - } - else - { - std::string param_name = header_value.substr(str_param_cur, delimiter - str_param_cur); + + content_params.emplace( + std::move(param_name), + std::string() + ); + } else { + std::string param_name = header_value.substr( + str_param_cur, + delimiter - str_param_cur + ); + Utils::trim(param_name); ++delimiter; - std::string param_value = header_value.substr(delimiter, std::string::npos != str_param_end ? str_param_end - delimiter : std::string::npos); + std::string param_value = header_value.substr( + delimiter, + std::string::npos != str_param_end + ? str_param_end - delimiter + : std::string::npos + ); + Utils::trim(param_value); - content_params.emplace(std::move(param_name), std::move(param_value) ); + content_params.emplace( + std::move(param_name), + std::move(param_value) + ); } } - } - else - { + } else { data_variant_name = header_value; } auto const variant = variants.find(data_variant_name); - if (variants.cend() == variant) - { + if (variants.cend() == variant) { return nullptr; } @@ -84,9 +110,12 @@ namespace HttpServer auto const it_len = rd->incoming_headers.find("content-length"); - if (rd->incoming_headers.cend() != it_len) - { - data_length = std::strtoull(it_len->second.c_str(), nullptr, 10); + if (rd->incoming_headers.cend() != it_len) { + data_length = std::strtoull( + it_len->second.c_str(), + nullptr, + 10 + ); } return new DataVariant::DataReceiver { @@ -101,12 +130,10 @@ namespace HttpServer { DataVariant::DataReceiver *dr = reinterpret_cast(src); - if (dr) - { + if (dr) { dr->data_variant->destroyStateStruct(dr->ss); - if (dr->reserved) - { + if (dr->reserved) { delete reinterpret_cast(dr->reserved); dr->reserved = nullptr; } @@ -115,13 +142,14 @@ namespace HttpServer delete dr; } - void ServerProtocol::runApplication(struct Request &req, const ServerApplicationSettings &appSets) const - { + void ServerProtocol::runApplication( + struct Request &req, + const ServerApplicationSettings &appSets + ) const { std::vector buf; buf.reserve(4096); - if (false == this->packRequestParameters(buf, req, appSets.root_dir) ) - { + if (this->packRequestParameters(buf, req, appSets.root_dir) == false) { return; } @@ -135,32 +163,27 @@ namespace HttpServer nullptr, 0 }; - try - { + try { // Launch application req.app_exit_code = appSets.application_call(&request, &response); } - catch (std::exception &exc) - { + catch (std::exception &exc) { // TODO: exception output } if (response.response_data && response.data_size) { - if (EXIT_SUCCESS == req.app_exit_code) - { + if (EXIT_SUCCESS == req.app_exit_code) { this->unpackResponseParameters(req, response.response_data); } // Clear outgoing data of application - try - { + try { appSets.application_clear(response.response_data, response.data_size); } - catch (std::exception &exc) - { + catch (std::exception &exc) { // TODO: exception output } } } -}; +} diff --git a/src/server/protocol/ServerProtocol.h b/src/server/protocol/ServerProtocol.h index 91f5b3a..e53fea6 100644 --- a/src/server/protocol/ServerProtocol.h +++ b/src/server/protocol/ServerProtocol.h @@ -16,24 +16,56 @@ namespace HttpServer ServerControls &controls; public: - ServerProtocol(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept; + ServerProtocol( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls + ) noexcept; + ServerProtocol(const ServerProtocol &prot) noexcept; virtual ~ServerProtocol() noexcept = default; - static DataVariant::DataReceiver *createDataReceiver(const Transfer::request_data *rd, const std::unordered_map &variants); + static DataVariant::DataReceiver * + createDataReceiver( + const Transfer::request_data *rd, + const std::unordered_map &variants + ); + static void destroyDataReceiver(void *src); protected: - void runApplication(struct Request &req, const ServerApplicationSettings &appSets) const; + void runApplication( + struct Request &req, + const ServerApplicationSettings &appSets + ) const; public: - virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream = true) const = 0; - virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const = 0; + virtual bool sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream = true + ) const = 0; + + virtual long sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const = 0; + + virtual bool packRequestParameters( + std::vector &buf, + const struct Request &req, + const std::string &rootDir + ) const = 0; - virtual bool packRequestParameters(std::vector &buf, const struct Request &req, const std::string &rootDir) const = 0; - virtual void unpackResponseParameters(struct Request &req, const void *src) const = 0; + virtual void unpackResponseParameters( + struct Request &req, + const void *src + ) const = 0; virtual ServerProtocol *process() = 0; virtual void close() = 0; }; -}; +} diff --git a/src/server/protocol/ServerWebSocket.cpp b/src/server/protocol/ServerWebSocket.cpp index 5c03f1a..2951e73 100644 --- a/src/server/protocol/ServerWebSocket.cpp +++ b/src/server/protocol/ServerWebSocket.cpp @@ -3,7 +3,11 @@ namespace HttpServer { - ServerWebSocket::ServerWebSocket(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept + ServerWebSocket::ServerWebSocket( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls + ) noexcept : ServerProtocol(sock, settings, controls) { @@ -15,33 +19,42 @@ namespace HttpServer } - bool ServerWebSocket::sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const - { + bool ServerWebSocket::sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream + ) const { return false; } - long ServerWebSocket::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const - { + long ServerWebSocket::sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const { return 0; } - bool ServerWebSocket::packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const - { + bool ServerWebSocket::packRequestParameters( + std::vector &buf, + const struct Request &rp, + const std::string &rootDir + ) const { return false; } - void ServerWebSocket::unpackResponseParameters(struct Request &req, const void *src) const - { + void ServerWebSocket::unpackResponseParameters( + struct Request &req, + const void *src + ) const { } - ServerProtocol *ServerWebSocket::process() - { + ServerProtocol *ServerWebSocket::process() { return this; } - void ServerWebSocket::close() - { - - } -}; + void ServerWebSocket::close() {} +} diff --git a/src/server/protocol/ServerWebSocket.h b/src/server/protocol/ServerWebSocket.h index 094293f..5745f7e 100644 --- a/src/server/protocol/ServerWebSocket.h +++ b/src/server/protocol/ServerWebSocket.h @@ -7,16 +7,40 @@ namespace HttpServer class ServerWebSocket : public ServerProtocol { public: - ServerWebSocket(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls) noexcept; + ServerWebSocket( + Socket::Adapter &sock, + const ServerSettings &settings, + ServerControls &controls + ) noexcept; + ServerWebSocket(const ServerProtocol &prot) noexcept; - virtual bool sendHeaders(const Http::StatusCode status, std::vector > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const override; - virtual long sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const override; + virtual bool sendHeaders( + const Http::StatusCode status, + std::vector > &headers, + const std::chrono::milliseconds &timeout, + const bool endStream + ) const override; + + virtual long sendData( + const void *src, + size_t size, + const std::chrono::milliseconds &timeout, + DataTransfer *dt + ) const override; + + virtual bool packRequestParameters( + std::vector &buf, + const struct Request &rp, + const std::string &rootDir + ) const override; - virtual bool packRequestParameters(std::vector &buf, const struct Request &rp, const std::string &rootDir) const override; - virtual void unpackResponseParameters(struct Request &req, const void *src) const override; + virtual void unpackResponseParameters( + struct Request &req, + const void *src + ) const override; virtual ServerProtocol *process() override; virtual void close() override; }; -}; +} diff --git a/src/server/protocol/extensions/Sendfile.cpp b/src/server/protocol/extensions/Sendfile.cpp index 9c557ba..1758515 100644 --- a/src/server/protocol/extensions/Sendfile.cpp +++ b/src/server/protocol/extensions/Sendfile.cpp @@ -6,15 +6,21 @@ namespace HttpServer { - static std::string getMimeTypeByFileName(const std::string &fileName, const std::unordered_map &mimesTypes) noexcept - { + static std::string getMimeTypeByFileName( + const std::string &fileName, + const std::unordered_map &mimesTypes + ) noexcept { const size_t ext_pos = fileName.rfind('.'); - const std::string file_ext = Utils::getLowerString(std::string::npos != ext_pos ? fileName.substr(ext_pos + 1) : std::string() ); + const std::string file_ext = std::string::npos != ext_pos + ? Utils::getLowerString(fileName.substr(ext_pos + 1) ) + : std::string(); auto const it_mime = mimesTypes.find(file_ext); - return mimesTypes.cend() != it_mime ? it_mime->second : std::string("application/octet-stream"); + return mimesTypes.cend() == it_mime + ? std::string("application/octet-stream") + : it_mime->second; } static std::vector > getRanges( @@ -23,15 +29,17 @@ namespace HttpServer const size_t fileSize, std::string *resultRangeHeader, size_t *contentLength - ) noexcept - { + ) noexcept { std::vector > ranges; *contentLength = 0; size_t delimiter = posSymEqual; // rangeHeader.find('='); - const std::string range_unit_name(rangeHeader.cbegin(), rangeHeader.cbegin() + delimiter); + const std::string range_unit_name( + rangeHeader.cbegin(), + rangeHeader.cbegin() + delimiter + ); static const std::unordered_map ranges_units { { "bytes", 1 } @@ -39,8 +47,7 @@ namespace HttpServer auto const it_unit = ranges_units.find(range_unit_name); - if (ranges_units.cend() == it_unit) - { + if (ranges_units.cend() == it_unit) { return ranges; } @@ -49,30 +56,41 @@ namespace HttpServer for (size_t str_pos; std::string::npos != delimiter; ) { str_pos = delimiter + 1; - delimiter = rangeHeader.find(',', str_pos); const size_t range_pos = rangeHeader.find('-', str_pos); if (range_pos < delimiter) { - const std::string range_begin_str(rangeHeader.cbegin() + str_pos, rangeHeader.cbegin() + range_pos); - const std::string range_end_str(rangeHeader.cbegin() + range_pos + 1, std::string::npos == delimiter ? rangeHeader.cend() : rangeHeader.cbegin() + delimiter); - - if (false == range_begin_str.empty() ) + const std::string range_begin_str( + rangeHeader.cbegin() + str_pos, + rangeHeader.cbegin() + range_pos + ); + + const std::string range_end_str( + rangeHeader.cbegin() + range_pos + 1, + std::string::npos == delimiter + ? rangeHeader.cend() + : rangeHeader.cbegin() + delimiter + ); + + if (range_begin_str.empty() == false) { - const size_t range_begin = std::strtoull(range_begin_str.c_str(), nullptr, 10) * range_unit; + const size_t range_begin = std::strtoull( + range_begin_str.c_str(), nullptr, 10 + ) * range_unit; if (range_begin < fileSize) { - if (false == range_end_str.empty() ) + if (range_end_str.empty() == false) { - size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit; + size_t range_end = std::strtoull( + range_end_str.c_str(), nullptr, 10 + ) * range_unit; if (range_end >= range_begin) { - if (range_end > fileSize) - { + if (range_end > fileSize) { range_end = fileSize; } @@ -93,15 +111,21 @@ namespace HttpServer *resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(fileSize - 1) + ','; - ranges.emplace_back(std::tuple {range_begin, length}); + ranges.emplace_back(std::tuple { + range_begin, length + }); } } } - else if (false == range_end_str.empty() ) // if range_begin_str empty + else if (range_end_str.empty() == false) // if range_begin_str empty { - size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit; + size_t range_end = std::strtoull( + range_end_str.c_str(), nullptr, 10 + ) * range_unit; - const size_t length = range_end < fileSize ? fileSize - range_end : fileSize; + const size_t length = range_end < fileSize + ? fileSize - range_end + : fileSize; const size_t range_begin = fileSize - length; @@ -111,15 +135,15 @@ namespace HttpServer *resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ','; - ranges.emplace_back(std::tuple {range_begin, length}); + ranges.emplace_back(std::tuple { + range_begin, length + }); } } } - if (false == ranges.empty() ) - { + if (ranges.empty() == false) { (*resultRangeHeader).back() = '/'; - *resultRangeHeader = "bytes " + *resultRangeHeader + std::to_string(fileSize); } @@ -136,13 +160,15 @@ namespace HttpServer const std::string &rangeHeader, std::vector > &additionalHeaders, const bool headersOnly - ) noexcept - { + ) noexcept { const size_t pos_sym_equal = rangeHeader.find('='); - if (std::string::npos == pos_sym_equal) - { - prot.sendHeaders(Http::StatusCode::BAD_REQUEST, additionalHeaders, req.timeout); + if (std::string::npos == pos_sym_equal) { + prot.sendHeaders( + Http::StatusCode::BAD_REQUEST, + additionalHeaders, + req.timeout + ); return 1; } @@ -151,11 +177,20 @@ namespace HttpServer size_t content_length; - const std::vector > ranges = getRanges(rangeHeader, pos_sym_equal, fileSize, &content_range_header, &content_length); - - if (0 == content_length) - { - prot.sendHeaders(Http::StatusCode::REQUESTED_RANGE_NOT_SATISFIABLE, additionalHeaders, req.timeout); + const std::vector > ranges = getRanges( + rangeHeader, + pos_sym_equal, + fileSize, + &content_range_header, + &content_length + ); + + if (0 == content_length) { + prot.sendHeaders( + Http::StatusCode::REQUESTED_RANGE_NOT_SATISFIABLE, + additionalHeaders, + req.timeout + ); return 2; } @@ -163,11 +198,14 @@ namespace HttpServer // Ranges transfer std::ifstream file(fileName, std::ifstream::binary); - if ( ! file) - { + if ( ! file) { file.close(); - prot.sendHeaders(Http::StatusCode::INTERNAL_SERVER_ERROR, additionalHeaders, req.timeout); + prot.sendHeaders( + Http::StatusCode::INTERNAL_SERVER_ERROR, + additionalHeaders, + req.timeout + ); return 3; } @@ -181,10 +219,14 @@ namespace HttpServer additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(fileTime, true) ); // Отправить заголовки (206 Partial Content) - if (false == prot.sendHeaders(Http::StatusCode::PARTIAL_CONTENT, additionalHeaders, req.timeout, headersOnly) ) - { + if (prot.sendHeaders( + Http::StatusCode::PARTIAL_CONTENT, + additionalHeaders, + req.timeout, + headersOnly + ) == false + ) { file.close(); - return 4; } @@ -203,7 +245,11 @@ namespace HttpServer { std::tie(position, length) = range; - buf.resize(length < 512 * 1024 ? length : 512 * 1024); + buf.resize( + length < 512 * 1024 + ? length + : 512 * 1024 + ); file.seekg(position, file.beg); @@ -211,10 +257,8 @@ namespace HttpServer long send_size; - do - { - if (send_size_left < 512 * 1024) - { + do { + if (send_size_left < 512 * 1024) { buf.resize(send_size_left); } @@ -222,7 +266,12 @@ namespace HttpServer auto const read_size = file.gcount(); - send_size = prot.sendData(buf.data(), read_size, req.timeout, &dt); + send_size = prot.sendData( + buf.data(), + read_size, + req.timeout, + &dt + ); send_size_left -= send_size; } @@ -245,8 +294,7 @@ namespace HttpServer const std::unordered_map &mimesTypes, std::vector > &additionalHeaders, const bool headersOnly - ) noexcept - { + ) noexcept { // Get current time in GMT additionalHeaders.emplace_back("date", Utils::getDatetimeAsString() ); @@ -254,9 +302,12 @@ namespace HttpServer size_t file_size; // Получить размер файла и дату последнего изменения - if (false == System::getFileSizeAndTimeGmt(fileName, &file_size, &file_time) ) - { - prot.sendHeaders(Http::StatusCode::NOT_FOUND, additionalHeaders, req.timeout); + if (System::getFileSizeAndTimeGmt(fileName, &file_size, &file_time) == false) { + prot.sendHeaders( + Http::StatusCode::NOT_FOUND, + additionalHeaders, + req.timeout + ); return 1; } @@ -265,13 +316,15 @@ namespace HttpServer auto const it_modified = req.incoming_headers.find("if-modified-since"); // Если найден заголовок проверки изменения файла (проверить, изменялся ли файл) - if (req.incoming_headers.cend() != it_modified) - { + if (req.incoming_headers.cend() != it_modified) { const time_t time_in_request = Utils::rfc822DatetimeToTimestamp(it_modified->second); - if (file_time == time_in_request) - { - prot.sendHeaders(Http::StatusCode::NOT_MODIFIED, additionalHeaders, req.timeout); + if (file_time == time_in_request) { + prot.sendHeaders( + Http::StatusCode::NOT_MODIFIED, + additionalHeaders, + req.timeout + ); return 2; } @@ -280,19 +333,31 @@ namespace HttpServer auto const it_range = req.incoming_headers.find("range"); // Range transfer - if (req.incoming_headers.cend() != it_range) - { - return transferFilePart(prot, req, fileName, mimesTypes, file_time, file_size, it_range->second, additionalHeaders, headersOnly); + if (req.incoming_headers.cend() != it_range) { + return transferFilePart( + prot, + req, + fileName, + mimesTypes, + file_time, + file_size, + it_range->second, + additionalHeaders, + headersOnly + ); } // File transfer std::ifstream file(fileName, std::ifstream::binary); - if ( ! file) - { + if ( ! file) { file.close(); - prot.sendHeaders(Http::StatusCode::INTERNAL_SERVER_ERROR, additionalHeaders, req.timeout); + prot.sendHeaders( + Http::StatusCode::INTERNAL_SERVER_ERROR, + additionalHeaders, + req.timeout + ); return 3; } @@ -305,17 +370,25 @@ namespace HttpServer additionalHeaders.emplace_back("last-modified", Utils::getDatetimeAsString(file_time, true) ); // Отправить заголовки (200 OK) - if (false == prot.sendHeaders(Http::StatusCode::OK, additionalHeaders, req.timeout, headersOnly || 0 == file_size) ) - { + if (prot.sendHeaders( + Http::StatusCode::OK, + additionalHeaders, + req.timeout, + headersOnly || 0 == file_size + ) == false + ) { file.close(); - return 4; } // Отправить файл if (false == headersOnly && file_size) { - std::vector buf(file_size < 512 * 1024 ? file_size : 512 * 1024); + std::vector buf( + file_size < 512 * 1024 + ? file_size + : 512 * 1024 + ); long send_size; @@ -324,10 +397,15 @@ namespace HttpServer 0 }; - do - { + do { file.read(buf.data(), buf.size() ); - send_size = prot.sendData(buf.data(), file.gcount(), req.timeout, &dt); + + send_size = prot.sendData( + buf.data(), + file.gcount(), + req.timeout, + &dt + ); } while (false == file.eof() && false == file.fail() && send_size > 0 && (dt.full_size - dt.send_total) ); } @@ -337,8 +415,7 @@ namespace HttpServer return 0; } - bool Sendfile::isConnectionReuse(const struct Request &req) noexcept - { + bool Sendfile::isConnectionReuse(const struct Request &req) noexcept { return (req.connection_params & ConnectionParams::CONNECTION_REUSE) == ConnectionParams::CONNECTION_REUSE; } @@ -346,30 +423,32 @@ namespace HttpServer const ServerProtocol &prot, struct Request &req, const std::unordered_map &mimesTypes - ) noexcept - { + ) noexcept { auto const it_x_sendfile = req.outgoing_headers.find("x-sendfile"); if (req.outgoing_headers.cend() != it_x_sendfile) { std::vector > additional_headers; - if (Transfer::ProtocolVariant::HTTP_1 == req.protocol_variant) - { - if (isConnectionReuse(req) ) - { + if (Transfer::ProtocolVariant::HTTP_1 == req.protocol_variant) { + if (isConnectionReuse(req) ) { additional_headers.emplace_back("connection", "keep-alive"); additional_headers.emplace_back("keep-alive", "timeout=5; max=" + std::to_string(req.keep_alive_count) ); - } - else - { + } else { additional_headers.emplace_back("connection", "close"); } } const bool headers_only = ("head" == req.method); - transferFile(prot, req, it_x_sendfile->second, mimesTypes, additional_headers, headers_only); + transferFile( + prot, + req, + it_x_sendfile->second, + mimesTypes, + additional_headers, + headers_only + ); } } -}; +} diff --git a/src/server/protocol/extensions/Sendfile.h b/src/server/protocol/extensions/Sendfile.h index eb2823b..23793d0 100644 --- a/src/server/protocol/extensions/Sendfile.h +++ b/src/server/protocol/extensions/Sendfile.h @@ -38,4 +38,4 @@ namespace HttpServer const std::unordered_map &mimesTypes ) noexcept; }; -}; +} diff --git a/src/socket/Adapter.cpp b/src/socket/Adapter.cpp index 0c50bc3..c99bee6 100644 --- a/src/socket/Adapter.cpp +++ b/src/socket/Adapter.cpp @@ -3,12 +3,26 @@ namespace Socket { - long Adapter::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept { - return this->nonblock_recv(buf.data(), buf.size(), timeout); + long Adapter::nonblock_recv( + std::vector &buf, + const std::chrono::milliseconds &timeout + ) const noexcept { + return this->nonblock_recv( + buf.data(), + buf.size(), + timeout + ); } - long Adapter::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept { - return this->nonblock_send(buf.data(), buf.length(), timeout); + long Adapter::nonblock_send( + const std::string &buf, + const std::chrono::milliseconds &timeout + ) const noexcept { + return this->nonblock_send( + buf.data(), + buf.length(), + timeout + ); } bool Adapter::operator ==(const Adapter &obj) const noexcept { diff --git a/src/socket/Adapter.h b/src/socket/Adapter.h index 35b3964..2787176 100644 --- a/src/socket/Adapter.h +++ b/src/socket/Adapter.h @@ -19,11 +19,27 @@ namespace Socket virtual ::gnutls_session_t get_tls_session() const noexcept = 0; virtual Adapter *copy() const noexcept = 0; - long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; - virtual long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept = 0; - - long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept; - virtual long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept = 0; + long nonblock_recv( + std::vector &buf, + const std::chrono::milliseconds &timeout + ) const noexcept; + + virtual long nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept = 0; + + long nonblock_send( + const std::string &buf, + const std::chrono::milliseconds &timeout + ) const noexcept; + + virtual long nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept = 0; virtual void close() noexcept = 0; diff --git a/src/socket/AdapterDefault.cpp b/src/socket/AdapterDefault.cpp index 1f2fe36..2940daf 100644 --- a/src/socket/AdapterDefault.cpp +++ b/src/socket/AdapterDefault.cpp @@ -3,25 +3,33 @@ namespace Socket { - AdapterDefault::AdapterDefault(const Socket &_sock) noexcept : sock(_sock) {} + AdapterDefault::AdapterDefault(const Socket &sock) noexcept : sock(sock) {} System::native_socket_type AdapterDefault::get_handle() const noexcept { return sock.get_handle(); } ::gnutls_session_t AdapterDefault::get_tls_session() const noexcept { - return 0; + return nullptr; } Adapter *AdapterDefault::copy() const noexcept { return new AdapterDefault(this->sock); } - long AdapterDefault::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { + long AdapterDefault::nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { return sock.nonblock_recv(buf, length, timeout); } - long AdapterDefault::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { + long AdapterDefault::nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { return sock.nonblock_send(buf, length, timeout); } diff --git a/src/socket/AdapterDefault.h b/src/socket/AdapterDefault.h index d743783..db8b191 100644 --- a/src/socket/AdapterDefault.h +++ b/src/socket/AdapterDefault.h @@ -18,8 +18,17 @@ namespace Socket virtual ::gnutls_session_t get_tls_session() const noexcept override; virtual Adapter *copy() const noexcept override; - virtual long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; - virtual long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; + virtual long nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept override; + + virtual long nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept override; virtual void close() noexcept override; }; diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index b63bf73..118e885 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -3,8 +3,11 @@ namespace Socket { - AdapterTls::AdapterTls(const Socket &sock, ::gnutls_priority_t priority_cache, ::gnutls_certificate_credentials_t x509_cred) noexcept - { + AdapterTls::AdapterTls( + const Socket &sock, + ::gnutls_priority_t priority_cache, + ::gnutls_certificate_credentials_t x509_cred + ) noexcept { // https://leandromoreira.com.br/2015/10/12/how-to-optimize-nginx-configuration-for-http2-tls-ssl/ // https://www.gnutls.org/manual/html_node/False-Start.html @@ -39,6 +42,8 @@ namespace Socket { int ret; + ::gnutls_handshake_set_timeout(this->session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT); + do { ret = ::gnutls_handshake(this->session); } @@ -56,9 +61,10 @@ namespace Socket return true; } - long AdapterTls::nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { - // size_t record_size = ::gnutls_record_get_max_size(this->session); + long AdapterTls::nonblock_send_all( + const void *buf, const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { size_t record_size = length; if (0 == record_size) { @@ -67,8 +73,6 @@ namespace Socket Socket sock(this->get_handle() ); - // ::gnutls_record_set_timeout(this->session, static_cast(timeout.count() ) ); - size_t total = 0; while (total < length) { @@ -80,7 +84,12 @@ namespace Socket do { sock.nonblock_send_sync(); - send_size = ::gnutls_record_send(this->session, reinterpret_cast(buf) + total, record_size); + + send_size = ::gnutls_record_send( + this->session, + reinterpret_cast(buf) + total, + record_size + ); } while (GNUTLS_E_AGAIN == send_size); @@ -95,7 +104,9 @@ namespace Socket } System::native_socket_type AdapterTls::get_handle() const noexcept { - return static_cast(::gnutls_transport_get_int(this->session) ); + return static_cast( + ::gnutls_transport_get_int(this->session) + ); } ::gnutls_session_t AdapterTls::get_tls_session() const noexcept { @@ -106,8 +117,11 @@ namespace Socket return new AdapterTls(this->session); } - long AdapterTls::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { + long AdapterTls::nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { Socket sock(this->get_handle() ); long result; @@ -126,7 +140,11 @@ namespace Socket return result; } - long AdapterTls::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { + long AdapterTls::nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { return this->nonblock_send_all(buf, length, timeout); } diff --git a/src/socket/AdapterTls.h b/src/socket/AdapterTls.h index 27bad11..04aab40 100644 --- a/src/socket/AdapterTls.h +++ b/src/socket/AdapterTls.h @@ -12,18 +12,38 @@ namespace Socket public: AdapterTls() = delete; - AdapterTls(const Socket &sock, ::gnutls_priority_t priority_cache, ::gnutls_certificate_credentials_t x509_cred) noexcept; + + AdapterTls( + const Socket &sock, + ::gnutls_priority_t priority_cache, + ::gnutls_certificate_credentials_t x509_cred + ) noexcept; + AdapterTls(const ::gnutls_session_t session) noexcept; bool handshake() noexcept; - long nonblock_send_all(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + + long nonblock_send_all( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept; virtual System::native_socket_type get_handle() const noexcept override; virtual ::gnutls_session_t get_tls_session() const noexcept override; virtual Adapter *copy() const noexcept override; - virtual long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; - virtual long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept override; + virtual long nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept override; + + virtual long nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept override; virtual void close() noexcept override; }; diff --git a/src/socket/List.cpp b/src/socket/List.cpp index 1746a79..a1accbd 100644 --- a/src/socket/List.cpp +++ b/src/socket/List.cpp @@ -16,7 +16,7 @@ namespace Socket #elif POSIX this->obj_list = ~0; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -30,7 +30,7 @@ namespace Socket obj.obj_list = ~0; obj.epoll_events.swap(this->epoll_events); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -63,7 +63,7 @@ namespace Socket return true; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -79,7 +79,7 @@ namespace Socket this->obj_list = ~0; this->epoll_events.clear(); #else - #error "Undefine platform" + #error "Undefined platform" #endif } } @@ -91,7 +91,7 @@ namespace Socket #elif POSIX return this->obj_list != ~0; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -102,7 +102,7 @@ namespace Socket } #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event { sock.get_handle(), POLLRDNORM, 0 @@ -112,12 +112,17 @@ namespace Socket return true; #elif POSIX - struct ::epoll_event event = { + struct ::epoll_event event { EPOLLIN | EPOLLET | EPOLLRDHUP, reinterpret_cast(sock.get_handle() ) }; - const int result = ::epoll_ctl(this->obj_list, EPOLL_CTL_ADD, sock.get_handle(), &event); + const int result = ::epoll_ctl( + this->obj_list, + EPOLL_CTL_ADD, + sock.get_handle(), + &event + ); if (result == ~0) { return false; @@ -127,7 +132,7 @@ namespace Socket return true; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -147,7 +152,12 @@ namespace Socket return false; #elif POSIX - const int result = ::epoll_ctl(this->obj_list, EPOLL_CTL_DEL, sock.get_handle(), nullptr); + const int result = ::epoll_ctl( + this->obj_list, + EPOLL_CTL_DEL, + sock.get_handle(), + nullptr + ); if (result == ~0) { return false; @@ -157,7 +167,7 @@ namespace Socket return true; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -166,7 +176,11 @@ namespace Socket if (this->is_created() ) { #ifdef WIN32 - const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), ~0); + const int count = ::WSAPoll( + this->poll_events.data(), + static_cast<::ULONG>(this->poll_events.size() ), + ~0 + ); if (SOCKET_ERROR == count) { return false; @@ -181,7 +195,11 @@ namespace Socket System::native_socket_type client_socket = ~0; do { - client_socket = ::accept(event.fd, static_cast(nullptr), static_cast(nullptr) ); + client_socket = ::accept( + event.fd, + static_cast(nullptr), + static_cast(nullptr) + ); if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); @@ -193,7 +211,12 @@ namespace Socket return false == sockets.empty(); #elif POSIX - const int count = ::epoll_wait(this->obj_list, this->epoll_events.data(), this->epoll_events.size(), ~0); + const int count = ::epoll_wait( + this->obj_list, + this->epoll_events.data(), + this->epoll_events.size(), + ~0 + ); if (count == ~0) { return false; @@ -208,7 +231,11 @@ namespace Socket System::native_socket_type client_socket = ~0; do { - client_socket = ::accept(event.data.fd, static_cast(nullptr), static_cast(nullptr) ); + client_socket = ::accept( + event.data.fd, + static_cast(nullptr), + static_cast(nullptr) + ); if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); @@ -220,19 +247,25 @@ namespace Socket return false == sockets.empty(); #else - #error "Undefine platform" + #error "Undefined platform" #endif } return false; } - bool List::accept(std::vector &sockets, std::vector &socketsAddress) const noexcept - { + bool List::accept( + std::vector &sockets, + std::vector &socketsAddress + ) const noexcept { if (this->is_created() ) { #ifdef WIN32 - const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), ~0); + const int count = ::WSAPoll( + this->poll_events.data(), + static_cast<::ULONG>(this->poll_events.size() ), + ~0 + ); if (SOCKET_ERROR == count) { return false; @@ -250,7 +283,11 @@ namespace Socket ::sockaddr_in client_addr {}; socklen_t client_addr_len = sizeof(client_addr); - client_socket = ::accept(event.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); + client_socket = ::accept( + event.fd, + reinterpret_cast<::sockaddr *>(&client_addr), + &client_addr_len + ); if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); @@ -263,7 +300,12 @@ namespace Socket return false == sockets.empty(); #elif POSIX - const int count = ::epoll_wait(this->obj_list, this->epoll_events.data(), this->epoll_events.size(), ~0); + const int count = ::epoll_wait( + this->obj_list, + this->epoll_events.data(), + this->epoll_events.size(), + ~0 + ); if (count == ~0) { return false; @@ -281,7 +323,11 @@ namespace Socket ::sockaddr_in client_addr {}; socklen_t client_addr_len = sizeof(client_addr); - client_socket = ::accept(event.data.fd, reinterpret_cast<::sockaddr *>(&client_addr), &client_addr_len); + client_socket = ::accept( + event.data.fd, + reinterpret_cast<::sockaddr *>(&client_addr), + &client_addr_len + ); if (~0 != client_socket) { sockets.emplace_back(Socket(client_socket) ); @@ -294,21 +340,28 @@ namespace Socket return false == sockets.empty(); #else - #error "Undefine platform" + #error "Undefined platform" #endif } return false; } - bool List::recv(std::vector &sockets, std::vector &disconnected, std::chrono::milliseconds timeout) const noexcept - { + bool List::recv( + std::vector &sockets, + std::vector &disconnected, + std::chrono::milliseconds timeout + ) const noexcept { if (this->is_created() == false) { return false; } #ifdef WIN32 - const int count = ::WSAPoll(this->poll_events.data(), static_cast<::ULONG>(this->poll_events.size() ), static_cast<::INT>(timeout.count() ) ); + const int count = ::WSAPoll( + this->poll_events.data(), + static_cast<::ULONG>(this->poll_events.size() ), + static_cast<::INT>(timeout.count() ) + ); if (SOCKET_ERROR == count) { return false; @@ -328,7 +381,12 @@ namespace Socket return false == sockets.empty() || false == disconnected.empty(); #elif POSIX - const int count = ::epoll_wait(this->obj_list, this->epoll_events.data(), this->epoll_events.size(), timeout.count() ); + const int count = ::epoll_wait( + this->obj_list, + this->epoll_events.data(), + this->epoll_events.size(), + timeout.count() + ); if (count == ~0) { return false; @@ -348,7 +406,7 @@ namespace Socket return false == sockets.empty() || false == disconnected.empty(); #else - #error "Undefine platform" + #error "Undefined platform" #endif } } diff --git a/src/socket/List.h b/src/socket/List.h index 19f83b0..cdd2747 100644 --- a/src/socket/List.h +++ b/src/socket/List.h @@ -19,7 +19,7 @@ namespace Socket int obj_list; mutable std::vector epoll_events; #else - #error "Undefine platform" + #error "Undefined platform" #endif public: @@ -35,9 +35,19 @@ namespace Socket bool addSocket(const Socket &sock) noexcept; bool removeSocket(const Socket &sock) noexcept; - bool accept(std::vector &sockets) const noexcept; - bool accept(std::vector &sockets, std::vector &socketsAddress) const noexcept; + bool accept( + std::vector &sockets + ) const noexcept; - bool recv(std::vector &sockets, std::vector &errors, std::chrono::milliseconds timeout = std::chrono::milliseconds(~0) ) const noexcept; + bool accept( + std::vector &sockets, + std::vector &socketsAddress + ) const noexcept; + + bool recv( + std::vector &sockets, + std::vector &errors, + std::chrono::milliseconds timeout = std::chrono::milliseconds(~0) + ) const noexcept; }; } diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index 96bd194..54904d3 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -22,7 +22,7 @@ namespace Socket #elif POSIX return true; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -33,7 +33,7 @@ namespace Socket #elif POSIX return true; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -44,7 +44,7 @@ namespace Socket #elif POSIX return errno; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -54,7 +54,9 @@ namespace Socket Socket::Socket(const Socket &obj) noexcept : socket_handle(obj.socket_handle) {} - Socket::Socket(Socket &&obj) noexcept : socket_handle(obj.socket_handle) { + Socket::Socket(Socket &&obj) noexcept + : socket_handle(obj.socket_handle) + { obj.socket_handle = ~0; } @@ -73,7 +75,7 @@ namespace Socket #elif POSIX const int result = ::close(this->socket_handle); #else - #error "Undefine platform" + #error "Undefined platform" #endif if (0 == result) { @@ -92,7 +94,7 @@ namespace Socket #elif POSIX return ~0 != this->socket_handle; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -100,16 +102,19 @@ namespace Socket return this->socket_handle; } - bool Socket::bind(const int port) const noexcept - { - const ::sockaddr_in sock_addr = { - AF_INET, - htons(port), - ::htonl(INADDR_ANY), - 0 - }; + bool Socket::bind(const int port) const noexcept { + const ::sockaddr_in sock_addr { + AF_INET, + ::htons(port), + ::htonl(INADDR_ANY), + 0 + }; - return 0 == ::bind(this->socket_handle, reinterpret_cast(&sock_addr), sizeof(sockaddr_in) ); + return 0 == ::bind( + this->socket_handle, + reinterpret_cast(&sock_addr), + sizeof(sockaddr_in) + ); } bool Socket::listen() const noexcept { @@ -119,11 +124,19 @@ namespace Socket Socket Socket::accept() const noexcept { #ifdef WIN32 - System::native_socket_type client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); + System::native_socket_type client_socket = ::accept( + this->socket_handle, + static_cast(nullptr), + static_cast(nullptr) + ); #elif POSIX - System::native_socket_type client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); + System::native_socket_type client_socket = ::accept( + this->socket_handle, + static_cast(nullptr), + static_cast(nullptr) + ); #else - #error "Undefine platform" + #error "Undefined platform" #endif return Socket(client_socket); } @@ -132,27 +145,35 @@ namespace Socket { System::native_socket_type client_socket = ~0; #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event { this->socket_handle, POLLRDNORM, 0 }; if (1 == ::WSAPoll(&event, 1, ~0) && event.revents & POLLRDNORM) { - client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); + client_socket = ::accept( + this->socket_handle, + static_cast(nullptr), + static_cast(nullptr) + ); } #elif POSIX - struct ::pollfd event = { + struct ::pollfd event { this->socket_handle, POLLIN, 0 }; if (1 == ::poll(&event, 1, ~0) && event.revents & POLLIN) { - client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); + client_socket = ::accept( + this->socket_handle, + static_cast(nullptr), + static_cast(nullptr) + ); } #else - #error "Undefine platform" + #error "Undefined platform" #endif return Socket(client_socket); } @@ -161,27 +182,35 @@ namespace Socket { System::native_socket_type client_socket = ~0; #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event { this->socket_handle, - POLLRDNORM, - 0 - }; + POLLRDNORM, + 0 + }; if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { - client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); + client_socket = ::accept( + this->socket_handle, + static_cast(nullptr), + static_cast(nullptr) + ); } #elif POSIX - struct ::pollfd event = { + struct ::pollfd event { this->socket_handle, - POLLIN, - 0 - }; + POLLIN, + 0 + }; - if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) { - client_socket = ::accept(this->socket_handle, static_cast(nullptr), static_cast(nullptr) ); + if (1 == ::poll(&event, 1, int(timeout.count()) ) && event.revents & POLLIN) { + client_socket = ::accept( + this->socket_handle, + static_cast(nullptr), + static_cast(nullptr) + ); } #else - #error "Undefine platform" + #error "Undefined platform" #endif return Socket(client_socket); } @@ -195,7 +224,7 @@ namespace Socket #elif POSIX return 0 == ::shutdown(this->socket_handle, SHUT_RDWR); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -208,9 +237,15 @@ namespace Socket unsigned long value = isNonBlock; return 0 == ::ioctlsocket(this->socket_handle, FIONBIO, &value); #elif POSIX - return ~0 != ::fcntl(this->socket_handle, F_SETFL, isNonBlock ? O_NONBLOCK : O_SYNC); + return ~0 != ::fcntl( + this->socket_handle, + F_SETFL, + isNonBlock + ? O_NONBLOCK + : O_SYNC + ); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -223,7 +258,7 @@ namespace Socket const int flags = ::fcntl(socket_handle, F_GETFL, 0); return (flags != ~0) && (flags & O_NONBLOCK); #else - #error "Undefine platform" + #error "Undefined platform" #endif } */ @@ -232,67 +267,106 @@ namespace Socket { #ifdef WIN32 int flags = nodelay ? 1 : 0; - return 0 == setsockopt(this->socket_handle, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&flags), sizeof(flags) ); + + return 0 == ::setsockopt( + this->socket_handle, + IPPROTO_TCP, + TCP_NODELAY, + reinterpret_cast(&flags), + sizeof(flags) + ); #elif POSIX int flags = nodelay ? 1 : 0; - return 0 == setsockopt(this->socket_handle, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags) ); + + return 0 == ::setsockopt( + this->socket_handle, + IPPROTO_TCP, + TCP_NODELAY, + &flags, + sizeof(flags) + ); #else - #error "Undefine platform" + #error "Undefined platform" #endif } - long Socket::recv(std::vector &buf) const noexcept { + long Socket::recv( + std::vector &buf + ) const noexcept { return this->recv(buf.data(), buf.size() ); } - long Socket::recv(void *buf, const size_t length) const noexcept - { + long Socket::recv( + void *buf, + const size_t length + ) const noexcept { #ifdef WIN32 - return ::recv(this->socket_handle, reinterpret_cast(buf), static_cast(length), 0); + return ::recv( + this->socket_handle, + reinterpret_cast(buf), + static_cast(length), + 0 + ); #elif POSIX return ::recv(this->socket_handle, buf, length, 0); #else - #error "Undefine platform" + #error "Undefined platform" #endif } - long Socket::nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept { - return this->nonblock_recv(buf.data(), buf.size(), timeout); + long Socket::nonblock_recv( + std::vector &buf, + const std::chrono::milliseconds &timeout + ) const noexcept { + return this->nonblock_recv( + buf.data(), + buf.size(), + timeout + ); } - long Socket::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept - { + long Socket::nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { long recv_len = ~0; #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event { this->socket_handle, POLLRDNORM, 0 }; if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { - recv_len = ::recv(this->socket_handle, reinterpret_cast(buf), static_cast(length), 0); + recv_len = ::recv( + this->socket_handle, + reinterpret_cast(buf), + static_cast(length), + 0 + ); } #elif POSIX - struct ::pollfd event = { + struct ::pollfd event { this->socket_handle, POLLIN, 0 }; - if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLIN) { + if (1 == ::poll(&event, 1, int(timeout.count()) ) && event.revents & POLLIN) { recv_len = ::recv(this->socket_handle, buf, length, 0); } #else - #error "Undefine platform" + #error "Undefined platform" #endif return recv_len; } - bool Socket::nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept - { + bool Socket::nonblock_recv_sync( + const std::chrono::milliseconds &timeout + ) const noexcept { #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event { this->socket_handle, POLLIN, 0 @@ -300,30 +374,38 @@ namespace Socket return ::WSAPoll(&event, 1, timeout.count() ) == 1; #elif POSIX - struct ::pollfd event = { + struct ::pollfd event { this->socket_handle, POLLIN, 0 }; - return ::poll(&event, 1, timeout.count() ) == 1; + return ::poll(&event, 1, int(timeout.count()) ) == 1; #else - #error "Undefine platform" + #error "Undefined platform" #endif } - static long send_all(const System::native_socket_type socket_handle, const void *data, const size_t length) noexcept - { + static long send_all( + const System::native_socket_type socket_handle, + const void *data, + const size_t length + ) noexcept { size_t total = 0; while (total < length) { - const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, length - total, 0); + const long send_size = ::send( + socket_handle, + reinterpret_cast(data) + total, + length - total, + 0 + ); if (send_size < 0) { return send_size; } - total += send_size; + total += static_cast(send_size); } return static_cast(total); @@ -337,8 +419,12 @@ namespace Socket return send_all(this->socket_handle, buf, length); } - static long nonblock_send_all(const System::native_socket_type socket_handle, const void *data, const size_t length, const std::chrono::milliseconds &timeout) noexcept - { + static long nonblock_send_all( + const System::native_socket_type socket_handle, + const void *data, + const size_t length, + const std::chrono::milliseconds &timeout + ) noexcept { size_t total = 0; #ifdef WIN32 @@ -350,7 +436,12 @@ namespace Socket while (total < length) { if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLOUT) { - const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, static_cast(length - total), 0); + const long send_size = ::send( + socket_handle, + reinterpret_cast(data) + total, + static_cast(length - total), + 0 + ); if (send_size < 0) { return send_size; @@ -370,37 +461,58 @@ namespace Socket }; while (total < length) { - if (1 == ::poll(&event, 1, timeout.count() ) && event.revents & POLLOUT) { - const long send_size = ::send(socket_handle, reinterpret_cast(data) + total, length - total, 0); + if (1 == ::poll(&event, 1, int(timeout.count()) ) && event.revents & POLLOUT) { + const long send_size = ::send( + socket_handle, + reinterpret_cast(data) + total, + length - total, + 0 + ); if (send_size < 0) { return send_size; } - total += send_size; + total += static_cast(send_size); } else { return -1; } } #else - #error "Undefine platform" + #error "Undefined platform" #endif return static_cast(total); } - long Socket::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept { - return this->nonblock_send(buf.data(), buf.length(), timeout); + long Socket::nonblock_send( + const std::string &buf, + const std::chrono::milliseconds &timeout + ) const noexcept { + return this->nonblock_send( + buf.data(), + buf.length(), + timeout + ); } - long Socket::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept { - return nonblock_send_all(this->socket_handle, buf, length, timeout); + long Socket::nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept { + return nonblock_send_all( + this->socket_handle, + buf, + length, + timeout + ); } void Socket::nonblock_send_sync() const noexcept { #ifdef WIN32 - WSAPOLLFD event = { + WSAPOLLFD event { this->socket_handle, POLLOUT, 0 @@ -408,7 +520,7 @@ namespace Socket ::WSAPoll(&event, 1, ~0); #elif POSIX - struct ::pollfd event = { + struct ::pollfd event { this->socket_handle, POLLOUT, 0 @@ -416,7 +528,7 @@ namespace Socket ::poll(&event, 1, ~0); #else - #error "Undefine platform" + #error "Undefined platform" #endif } diff --git a/src/socket/Socket.h b/src/socket/Socket.h index e0bfc6c..0d5b711 100644 --- a/src/socket/Socket.h +++ b/src/socket/Socket.h @@ -37,7 +37,10 @@ namespace Socket Socket accept() const noexcept; Socket nonblock_accept() const noexcept; - Socket nonblock_accept(const std::chrono::milliseconds &timeout) const noexcept; + + Socket nonblock_accept( + const std::chrono::milliseconds &timeout + ) const noexcept; bool shutdown() const noexcept; @@ -45,19 +48,43 @@ namespace Socket // bool is_nonblock() const noexcept; bool tcp_nodelay(const bool nodelay = true) const noexcept; - long recv(std::vector &buf) const noexcept; - long recv(void *buf, const size_t length) const noexcept; + long recv( + std::vector &buf + ) const noexcept; + + long recv( + void *buf, + const size_t length + ) const noexcept; - long nonblock_recv(std::vector &buf, const std::chrono::milliseconds &timeout) const noexcept; - long nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + long nonblock_recv( + std::vector &buf, + const std::chrono::milliseconds &timeout + ) const noexcept; - bool nonblock_recv_sync(const std::chrono::milliseconds &timeout) const noexcept; + long nonblock_recv( + void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept; + + bool nonblock_recv_sync( + const std::chrono::milliseconds &timeout + ) const noexcept; long send(const std::string &buf) const noexcept; long send(const void *buf, const size_t length) const noexcept; - long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept; - long nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept; + long nonblock_send( + const std::string &buf, + const std::chrono::milliseconds &timeout + ) const noexcept; + + long nonblock_send( + const void *buf, + const size_t length, + const std::chrono::milliseconds &timeout + ) const noexcept; void nonblock_send_sync() const noexcept; diff --git a/src/system/GlobalMutex.cpp b/src/system/GlobalMutex.cpp index 2079b4e..0c72d06 100644 --- a/src/system/GlobalMutex.cpp +++ b/src/system/GlobalMutex.cpp @@ -13,7 +13,8 @@ namespace System { - GlobalMutex::GlobalMutex() noexcept : mtx_desc(nullptr) + GlobalMutex::GlobalMutex() noexcept + : mtx_desc(nullptr) { } @@ -40,22 +41,25 @@ namespace System this->mtx_desc = ::CreateMutex(nullptr, false, mutex_name.c_str() ); - if (nullptr == this->mtx_desc) - { + if (nullptr == this->mtx_desc) { return false; } #elif POSIX - ::sem_t *sem = ::sem_open(mutexName.c_str(), O_CREAT, 0666, 1); - - if (SEM_FAILED == sem) - { + ::sem_t *sem = ::sem_open( + mutexName.c_str(), + O_CREAT, + 0666, + 1 + ); + + if (SEM_FAILED == sem) { return false; } this->mtx_desc = sem; this->mtx_name = mutexName; #else - #error "Undefine platform" + #error "Undefined platform" #endif return true; @@ -79,7 +83,7 @@ namespace System #elif POSIX return 0 == ::sem_unlink(mutexName.c_str() ); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -101,13 +105,15 @@ namespace System return ret; #elif POSIX - const int ret = ::sem_unlink(this->mtx_name.c_str() ); + const int ret = ::sem_unlink( + this->mtx_name.c_str() + ); - this->close(); + this->close(); - return 0 == ret; + return 0 == ret; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -128,21 +134,22 @@ namespace System this->mtx_desc = ::OpenMutex(SYNCHRONIZE, false, mutex_name.c_str() ); - if (nullptr == this->mtx_desc) - { + if (nullptr == this->mtx_desc) { return false; } #elif POSIX - ::sem_t *sem = ::sem_open(mutexName.c_str(), 0); + ::sem_t *sem = ::sem_open( + mutexName.c_str(), + 0 + ); - if (SEM_FAILED == sem) - { + if (SEM_FAILED == sem) { return false; } this->mtx_desc = sem; #else - #error "Undefine platform" + #error "Undefined platform" #endif this->mtx_name = mutexName; @@ -161,7 +168,7 @@ namespace System ::sem_close(this->mtx_desc); this->mtx_desc = nullptr; #else - #error "Undefine platform" + #error "Undefined platform" #endif this->mtx_name.clear(); @@ -179,7 +186,7 @@ namespace System #elif POSIX return nullptr != this->mtx_desc; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -190,7 +197,7 @@ namespace System #elif POSIX return 0 == ::sem_wait(this->mtx_desc); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -201,7 +208,7 @@ namespace System #elif POSIX return 0 == ::sem_trywait(this->mtx_desc); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -212,7 +219,7 @@ namespace System #elif POSIX return 0 == ::sem_post(this->mtx_desc); #else - #error "Undefine platform" + #error "Undefined platform" #endif } -}; +} diff --git a/src/system/GlobalMutex.h b/src/system/GlobalMutex.h index 5250d78..d6bc0b0 100644 --- a/src/system/GlobalMutex.h +++ b/src/system/GlobalMutex.h @@ -19,7 +19,7 @@ namespace System #elif POSIX ::sem_t *mtx_desc; #else - #error "Undefine platform" + #error "Undefined platform" #endif std::string mtx_name; @@ -42,4 +42,4 @@ namespace System bool try_lock() const noexcept; bool unlock() const noexcept; }; -}; +} diff --git a/src/system/Module.cpp b/src/system/Module.cpp index 8cbddb1..a417a59 100644 --- a/src/system/Module.cpp +++ b/src/system/Module.cpp @@ -14,36 +14,38 @@ namespace System { - Module::Module() noexcept : lib_handle(nullptr) + Module::Module() noexcept + : lib_handle(nullptr) { } - Module::Module(const std::string &libPath): lib_handle(nullptr) + Module::Module(const std::string &libPath) + : lib_handle(nullptr) { open(libPath); } - Module::Module(const Module &obj) noexcept : lib_handle(obj.lib_handle) + Module::Module(const Module &obj) noexcept + : lib_handle(obj.lib_handle) { } - Module::Module(Module &&obj) noexcept : lib_handle(obj.lib_handle) + Module::Module(Module &&obj) noexcept + : lib_handle(obj.lib_handle) { obj.lib_handle = nullptr; } - bool Module::is_open() const noexcept - { + bool Module::is_open() const noexcept { return nullptr != this->lib_handle; } bool Module::open(const std::string &libPath) { - if (is_open() ) - { - close(); + if (this->is_open() ) { + this->close(); } #ifdef WIN32 @@ -77,19 +79,18 @@ namespace System const std::string &lib_path = libPath; #endif - lib_handle = ::LoadLibraryEx(lib_path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); + this->lib_handle = ::LoadLibraryEx(lib_path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); - if (cookie) - { + if (cookie) { ::RemoveDllDirectory(cookie); } #elif POSIX - lib_handle = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL); + this->lib_handle = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL); #else - #error "Undefine platform" + #error "Undefined platform" #endif - if (nullptr == lib_handle) + if (nullptr == this->lib_handle) { #ifdef POSIX std::cout << ::dlerror() << std::endl; @@ -102,85 +103,89 @@ namespace System void Module::close() noexcept { - if (lib_handle) + if (this->lib_handle) { #ifdef WIN32 - ::FreeLibrary(lib_handle); + ::FreeLibrary(this->lib_handle); #elif POSIX - ::dlclose(lib_handle); + ::dlclose(this->lib_handle); #else - #error "Undefine platform" + #error "Undefined platform" #endif - lib_handle = nullptr; + this->lib_handle = nullptr; } } - bool Module::find(const std::string &symbolName, void *(**addr)(void *) ) const noexcept - { + bool Module::find( + const std::string &symbolName, + void *(**addr)(void *) + ) const noexcept { if (lib_handle) { #ifdef WIN32 - *addr = reinterpret_cast(::GetProcAddress(lib_handle, symbolName.c_str() ) ); + *addr = reinterpret_cast(::GetProcAddress(this->lib_handle, symbolName.c_str() ) ); return nullptr != *addr; #elif POSIX char *error = ::dlerror(); - *addr = reinterpret_cast(::dlsym(lib_handle, symbolName.c_str() ) ); + *addr = reinterpret_cast( + ::dlsym(this->lib_handle, symbolName.c_str() ) + ); error = ::dlerror(); return nullptr == error; #else - #error "Undefine platform" + #error "Undefined platform" #endif } return false; } - bool Module::find(const char *symbolName, void *(**addr)(void *) ) const noexcept - { + bool Module::find( + const char *symbolName, + void *(**addr)(void *) + ) const noexcept { if (lib_handle) { #ifdef WIN32 - *addr = reinterpret_cast(::GetProcAddress(lib_handle, symbolName) ); + *addr = reinterpret_cast(::GetProcAddress(this->lib_handle, symbolName) ); return nullptr != *addr; #elif POSIX char *error = ::dlerror(); - *addr = reinterpret_cast(::dlsym(lib_handle, symbolName) ); + *addr = reinterpret_cast( + ::dlsym(this->lib_handle, symbolName) + ); error = ::dlerror(); return nullptr == error; #else - #error "Undefine platform" + #error "Undefined platform" #endif } return false; } - bool Module::operator ==(const Module &obj) const noexcept - { + bool Module::operator ==(const Module &obj) const noexcept { return this->lib_handle == obj.lib_handle; } - bool Module::operator !=(const Module &obj) const noexcept - { + bool Module::operator !=(const Module &obj) const noexcept { return this->lib_handle != obj.lib_handle; } Module &Module::operator =(const Module &obj) noexcept { - if (*this != obj) - { - close(); - - lib_handle = obj.lib_handle; + if (*this != obj) { + this->close(); + this->lib_handle = obj.lib_handle; } return *this; @@ -188,15 +193,12 @@ namespace System Module &Module::operator =(Module &&obj) noexcept { - if (*this != obj) - { - close(); - - lib_handle = obj.lib_handle; - + if (*this != obj) { + this->close(); + this->lib_handle = obj.lib_handle; obj.lib_handle = nullptr; } return *this; } -}; +} diff --git a/src/system/Module.h b/src/system/Module.h index 49e2e34..d7739ed 100644 --- a/src/system/Module.h +++ b/src/system/Module.h @@ -17,7 +17,7 @@ namespace System #elif POSIX void *lib_handle; #else - #error "Undefine platform" + #error "Undefined platform" #endif public: @@ -33,8 +33,15 @@ namespace System bool open(const std::string &libPath); void close() noexcept; - bool find(const std::string &symbolName, void *(**addr)(void *) ) const noexcept; - bool find(const char *symbolName, void *(**addr)(void *) ) const noexcept; + bool find( + const std::string &symbolName, + void *(**addr)(void *) + ) const noexcept; + + bool find( + const char *symbolName, + void *(**addr)(void *) + ) const noexcept; bool operator ==(const Module &obj) const noexcept; bool operator !=(const Module &obj) const noexcept; @@ -42,4 +49,4 @@ namespace System Module &operator =(const Module &obj) noexcept; Module &operator =(Module &&obj) noexcept; }; -}; +} diff --git a/src/system/SharedMemory.cpp b/src/system/SharedMemory.cpp index 1c0560f..3169941 100644 --- a/src/system/SharedMemory.cpp +++ b/src/system/SharedMemory.cpp @@ -24,17 +24,18 @@ namespace System #elif POSIX this->shm_desc = -1; #else - #error "Undefine platform" + #error "Undefined platform" #endif } - SharedMemory::~SharedMemory() noexcept - { + SharedMemory::~SharedMemory() noexcept { this->close(); } - bool SharedMemory::create(const std::string &memName, const size_t memSize) - { + bool SharedMemory::create( + const std::string &memName, + const size_t memSize + ) { this->close(); #ifdef WIN32 @@ -57,31 +58,31 @@ namespace System memory_name.c_str() ); - if (nullptr == this->shm_desc) - { + if (nullptr == this->shm_desc) { return false; } #elif POSIX - this->shm_desc = ::shm_open(memName.c_str(), O_CREAT | O_RDWR, 0666); + this->shm_desc = ::shm_open( + memName.c_str(), + O_CREAT | O_RDWR, + 0666 + ); - if (-1 == this->shm_desc) - { + if (-1 == this->shm_desc) { return false; } - if (-1 == ::ftruncate64(this->shm_desc, memSize) ) - { + if (::ftruncate64(this->shm_desc, long(memSize)) == -1) { this->destroy(memName); - return false; } this->shm_name = memName; #else - #error "Undefine platform" + #error "Undefined platform" #endif return true; @@ -104,8 +105,7 @@ namespace System this->shm_desc = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, false, memory_name.c_str() ); - if (nullptr == this->shm_desc) - { + if (nullptr == this->shm_desc) { return false; } @@ -113,17 +113,20 @@ namespace System #elif POSIX - this->shm_desc = ::shm_open(memName.c_str(), O_RDWR, 0666); + this->shm_desc = ::shm_open( + memName.c_str(), + O_RDWR, + 0666 + ); - if (-1 == this->shm_desc) - { + if (-1 == this->shm_desc) { return false; } this->shm_name = memName; #else - #error "Undefine platform" + #error "Undefined platform" #endif return true; @@ -136,18 +139,20 @@ namespace System #elif POSIX return -1 != this->shm_desc; #else - #error "Undefine platform" + #error "Undefined platform" #endif } - bool SharedMemory::write(const void *src, const size_t size, const size_t offset) const noexcept - { + bool SharedMemory::write( + const void *src, + const size_t size, + const size_t offset + ) const noexcept { #ifdef WIN32 void * const addr = ::MapViewOfFile(this->shm_desc, FILE_MAP_WRITE, 0, static_cast<::DWORD>(offset), size); - if (nullptr == addr) - { + if (nullptr == addr) { return false; } @@ -157,32 +162,40 @@ namespace System #elif POSIX - void * const addr = ::mmap(0, size, PROT_WRITE, MAP_SHARED, this->shm_desc, offset); + void * const addr = ::mmap( + nullptr, + size, + PROT_WRITE, + MAP_SHARED, + this->shm_desc, + long(offset) + ); - if (reinterpret_cast(-1) == addr) - { + if (reinterpret_cast(-1) == addr) { return false; } - ::memcpy(addr, src, size); + std::memcpy(addr, src, size); ::munmap(addr, size); #else - #error "Undefine platform" + #error "Undefined platform" #endif return true; } - bool SharedMemory::read(void *dest, const size_t size, const size_t offset) const noexcept - { + bool SharedMemory::read( + void *dest, + const size_t size, + const size_t offset + ) const noexcept { #ifdef WIN32 void * const addr = ::MapViewOfFile(this->shm_desc, FILE_MAP_READ, 0, static_cast<::DWORD>(offset), size); - if (nullptr == addr) - { + if (nullptr == addr) { return false; } @@ -192,19 +205,25 @@ namespace System #elif POSIX - void * const addr = ::mmap(0, size, PROT_READ, MAP_SHARED, this->shm_desc, offset); + void * const addr = ::mmap( + nullptr, + size, + PROT_READ, + MAP_SHARED, + this->shm_desc, + long(offset) + ); - if (reinterpret_cast(-1) == addr) - { + if (reinterpret_cast(-1) == addr) { return false; } - ::memcpy(dest, addr, size); + std::memcpy(dest, addr, size); ::munmap(addr, size); #else - #error "Undefine platform" + #error "Undefined platform" #endif return true; @@ -221,7 +240,7 @@ namespace System ::close(this->shm_desc); this->shm_desc = ~0; #else - #error "Undefine platform" + #error "Undefined platform" #endif this->shm_name.clear(); @@ -250,13 +269,15 @@ namespace System return ret; #elif POSIX - const int ret = ::shm_unlink(this->shm_name.c_str() ); + const int ret = ::shm_unlink( + this->shm_name.c_str() + ); this->close(); return 0 == ret; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -278,7 +299,7 @@ namespace System #elif POSIX return 0 == ::shm_unlink(memName.c_str() ); #else - #error "Undefine platform" + #error "Undefined platform" #endif } -}; +} diff --git a/src/system/SharedMemory.h b/src/system/SharedMemory.h index d76608f..b9a84e9 100644 --- a/src/system/SharedMemory.h +++ b/src/system/SharedMemory.h @@ -17,7 +17,7 @@ namespace System #elif POSIX int shm_desc; #else - #error "Undefine platform" + #error "Undefined platform" #endif std::string shm_name; @@ -26,17 +26,30 @@ namespace System SharedMemory() noexcept; ~SharedMemory() noexcept; - bool create(const std::string &memName, const size_t memSize); + bool create( + const std::string &memName, + const size_t memSize + ); + bool open(const std::string &memName); bool is_open() const noexcept; - bool write(const void *data, const size_t size, const size_t offset = 0) const noexcept; - bool read(void *dest, const size_t size, const size_t offset = 0) const noexcept; + bool write( + const void *data, + const size_t size, + const size_t offset = 0 + ) const noexcept; + + bool read( + void *dest, + const size_t size, + const size_t offset = 0 + ) const noexcept; bool close() noexcept; bool destroy(); static bool destroy(const std::string &memName); }; -}; +} diff --git a/src/system/System.cpp b/src/system/System.cpp index 9dcaeb7..af696d9 100644 --- a/src/system/System.cpp +++ b/src/system/System.cpp @@ -20,8 +20,7 @@ namespace System { #ifdef WIN32 - struct EnumData - { + struct EnumData { native_processid_type process_id; ::HWND hWnd; }; @@ -34,7 +33,7 @@ namespace System ::GetWindowThreadProcessId(hWnd, &process_id); - if (process_id == ed.process_id && ::GetConsoleWindow() != hWnd) + if (process_id == ed.process_id && ::GetConsoleWindow() != hWnd) { std::array<::TCHAR, 257> class_name; @@ -57,7 +56,7 @@ namespace System #elif POSIX return ::getpid(); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -74,7 +73,7 @@ namespace System #elif POSIX return 0 == ::chdir(dir.c_str() ); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -87,7 +86,7 @@ namespace System #elif POSIX return 0 == ::kill(pid, 0); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -106,7 +105,7 @@ namespace System #elif POSIX return 0 == ::kill(pid, signal); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -117,7 +116,7 @@ namespace System #elif POSIX return 0 != ::pthread_kill(handle, 0); #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -135,7 +134,7 @@ namespace System return std::string(buf.cbegin(), buf.cbegin() + len); #endif #elif POSIX - const char *buf = ::getenv("TMPDIR"); + const char *buf = ::getenv("TMPDIR"); if (nullptr == buf) { return std::string("/tmp/"); @@ -149,7 +148,7 @@ namespace System return str; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -179,12 +178,15 @@ namespace System return S_ISREG(attrib.st_mode); #else - #error "Undefine platform" + #error "Undefined platform" #endif } - bool getFileSizeAndTimeGmt(const std::string &filePath, size_t *fileSize, time_t *fileTime) - { + bool getFileSizeAndTimeGmt( + const std::string &filePath, + size_t *fileSize, + time_t *fileTime + ) { #ifdef WIN32 #ifdef UNICODE std::wstring_convert> converter; @@ -193,7 +195,15 @@ namespace System const std::string &file_path = filePath; #endif - const ::HANDLE hFile = ::CreateFile(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + const ::HANDLE hFile = ::CreateFile( + file_path.c_str(), + GENERIC_READ, + FILE_SHARE_READ, + nullptr, + OPEN_EXISTING, + 0, + nullptr + ); if (INVALID_HANDLE_VALUE == hFile) { return false; @@ -206,7 +216,12 @@ namespace System ::FILETIME ftWrite; - ::BOOL result = ::GetFileTime(hFile, nullptr, nullptr, &ftWrite); + ::BOOL result = ::GetFileTime( + hFile, + nullptr, + nullptr, + &ftWrite + ); ::CloseHandle(hFile); @@ -227,7 +242,7 @@ namespace System stUtc.wYear - 1900, 0, 0, - -1 + -1 }; *fileTime = std::mktime(&tm_time); @@ -240,7 +255,7 @@ namespace System return false; } - *fileSize = attrib.st_size; + *fileSize = static_cast(attrib.st_size); std::tm clock {}; @@ -250,7 +265,7 @@ namespace System return true; #else - #error "Undefine platform" + #error "Undefined platform" #endif } @@ -297,7 +312,7 @@ namespace System } } #else - #error "Undefine platform" + #error "Undefined platform" #endif } } diff --git a/src/system/System.h b/src/system/System.h index 3b5b1bc..d7ae219 100644 --- a/src/system/System.h +++ b/src/system/System.h @@ -7,27 +7,27 @@ ::TCHAR myWndClassName[]; - #ifdef SIGTERM - #undef SIGTERM - #define SIGTERM (WM_USER + 15) - #endif - - #ifdef SIGINT - #undef SIGINT - #define SIGINT (WM_USER + 2) - #endif - - #ifndef SIGUSR1 - #define SIGUSR1 (WM_USER + 10) - #endif - - #ifndef SIGUSR2 - #define SIGUSR2 (WM_USER + 12) - #endif + #ifdef SIGTERM + #undef SIGTERM + #define SIGTERM (WM_USER + 15) + #endif + + #ifdef SIGINT + #undef SIGINT + #define SIGINT (WM_USER + 2) + #endif + + #ifndef SIGUSR1 + #define SIGUSR1 (WM_USER + 10) + #endif + + #ifndef SIGUSR2 + #define SIGUSR2 (WM_USER + 12) + #endif #elif POSIX #include #else - #error "Undefine platform" + #error "Undefined platform" #endif #include @@ -41,7 +41,7 @@ namespace System #elif POSIX typedef int native_socket_type; #else - #error "Undefine platform" + #error "Undefined platform" #endif #ifdef WIN32 @@ -49,7 +49,7 @@ namespace System #elif POSIX typedef ::pid_t native_processid_type; #else - #error "Undefine platform" + #error "Undefined platform" #endif native_processid_type getProcessId() noexcept; diff --git a/src/transfer/AppRequest.h b/src/transfer/AppRequest.h index b345a51..52d894e 100644 --- a/src/transfer/AppRequest.h +++ b/src/transfer/AppRequest.h @@ -7,15 +7,13 @@ namespace Transfer { - struct request_data - { + struct request_data { std::unordered_multimap incoming_headers; std::unordered_multimap incoming_data; std::unordered_multimap incoming_files; }; - struct app_request - { + struct app_request { const System::native_socket_type socket; const ::gnutls_session_t tls_session; const void * const request_data; diff --git a/src/transfer/AppResponse.h b/src/transfer/AppResponse.h index 51ff300..e059e55 100644 --- a/src/transfer/AppResponse.h +++ b/src/transfer/AppResponse.h @@ -5,8 +5,7 @@ namespace Transfer { - struct app_response - { + struct app_response { void *response_data; size_t data_size; }; diff --git a/src/transfer/FileIncoming.cpp b/src/transfer/FileIncoming.cpp index ed2c49f..07fb877 100644 --- a/src/transfer/FileIncoming.cpp +++ b/src/transfer/FileIncoming.cpp @@ -6,23 +6,34 @@ namespace Transfer { - FileIncoming::FileIncoming(std::string &&fileTmpName, std::string &&fileName, std::string &&fileType, const size_t fileSize) noexcept - : file_tmp_name(std::move(fileTmpName) ), file_name(std::move(fileName) ), - file_type(std::move(fileType) ), file_size(fileSize) + FileIncoming::FileIncoming( + std::string &&fileTmpName, + std::string &&fileName, + std::string &&fileType, + const size_t fileSize + ) noexcept + : file_tmp_name(std::move(fileTmpName) ), + file_name(std::move(fileName) ), + file_type(std::move(fileType) ), + file_size(fileSize) { } FileIncoming::FileIncoming(const FileIncoming &obj) - : file_tmp_name(obj.file_tmp_name), file_name(obj.file_name), - file_type(obj.file_type), file_size(obj.file_size) + : file_tmp_name(obj.file_tmp_name), + file_name(obj.file_name), + file_type(obj.file_type), + file_size(obj.file_size) { } FileIncoming::FileIncoming(FileIncoming &&obj) noexcept - : file_tmp_name(std::move(obj.file_tmp_name) ), file_name(std::move(obj.file_name) ), - file_type(std::move(obj.file_type) ), file_size(obj.file_size) + : file_tmp_name(std::move(obj.file_tmp_name) ), + file_name(std::move(obj.file_name) ), + file_type(std::move(obj.file_type) ), + file_size(obj.file_size) { obj.file_size = 0; } @@ -53,8 +64,10 @@ namespace Transfer namespace Utils { - void packFilesIncoming(std::vector &buf, const std::unordered_multimap &map) - { + void packFilesIncoming( + std::vector &buf, + const std::unordered_multimap &map + ) { packNumber(buf, map.size() ); for (auto it = map.cbegin(); map.cend() != it; ++it) { @@ -69,8 +82,10 @@ namespace Utils } } - const uint8_t *unpackFilesIncoming(std::unordered_multimap &map, const uint8_t *src) - { + const uint8_t *unpackFilesIncoming( + std::unordered_multimap &map, + const uint8_t *src + ) { size_t count; src = unpackNumber(&count, src); @@ -90,7 +105,15 @@ namespace Utils size_t file_size; src = unpackNumber(&file_size, src); - map.emplace(std::move(key), Transfer::FileIncoming(std::move(file_tmp_name), std::move(file_name), std::move(file_type), file_size) ); + map.emplace( + std::move(key), + Transfer::FileIncoming( + std::move(file_tmp_name), + std::move(file_name), + std::move(file_type), + file_size + ) + ); } return src; diff --git a/src/transfer/FileIncoming.h b/src/transfer/FileIncoming.h index 2568b73..87e745b 100644 --- a/src/transfer/FileIncoming.h +++ b/src/transfer/FileIncoming.h @@ -18,7 +18,13 @@ namespace Transfer FileIncoming() = delete; public: - FileIncoming(std::string &&fileTmpName, std::string &&fileName, std::string &&fileType, const size_t fileSize) noexcept; + FileIncoming( + std::string &&fileTmpName, + std::string &&fileName, + std::string &&fileType, + const size_t fileSize + ) noexcept; + FileIncoming(const FileIncoming &obj); FileIncoming(FileIncoming &&obj) noexcept; @@ -35,6 +41,13 @@ namespace Transfer namespace Utils { - void packFilesIncoming(std::vector &buf, const std::unordered_multimap &map); - const uint8_t *unpackFilesIncoming(std::unordered_multimap &map, const uint8_t *src); + void packFilesIncoming( + std::vector &buf, + const std::unordered_multimap &map + ); + + const uint8_t *unpackFilesIncoming( + std::unordered_multimap &map, + const uint8_t *src + ); } diff --git a/src/transfer/HttpStatusCode.h b/src/transfer/HttpStatusCode.h index a560814..abb0c85 100644 --- a/src/transfer/HttpStatusCode.h +++ b/src/transfer/HttpStatusCode.h @@ -2,8 +2,7 @@ namespace Http { - enum class StatusCode - { + enum class StatusCode { EMPTY = 0, CONTINUE = 100, diff --git a/src/transfer/ProtocolVariant.h b/src/transfer/ProtocolVariant.h index fca95ef..093a02a 100644 --- a/src/transfer/ProtocolVariant.h +++ b/src/transfer/ProtocolVariant.h @@ -2,8 +2,7 @@ namespace Transfer { - enum class ProtocolVariant - { + enum class ProtocolVariant { UNKNOWN = 0, HTTP_1, HTTP_2 diff --git a/src/transfer/http2/HPack.cpp b/src/transfer/http2/HPack.cpp index 7075b2f..b8d0128 100644 --- a/src/transfer/http2/HPack.cpp +++ b/src/transfer/http2/HPack.cpp @@ -11,8 +11,7 @@ namespace HPack { - struct HuffmanSymbol - { + struct HuffmanSymbol { int nbits; uint32_t code; }; @@ -277,8 +276,7 @@ namespace HPack {30, 0x3fffffff}, }; - struct HuffmanDecodeNode - { + struct HuffmanDecodeNode { uint8_t state; uint8_t flags; uint8_t symbol; @@ -5218,22 +5216,27 @@ namespace HPack return sizeof(staticTable) / sizeof(*staticTable); } - static size_t huffmanEncodeLength(const void *dest, const size_t length) noexcept - { + static size_t huffmanEncodeLength( + const void *dest, + const size_t length + ) noexcept { const uint8_t *data = reinterpret_cast(dest); size_t n = 0; for (size_t i = 0; i < length; ++i) { - n += huffmanSymbolTable[data[i] ].nbits; + n += size_t(huffmanSymbolTable[data[i] ].nbits); } return (n + 7) / 8; } - static uint8_t huffmanEncodeSymbol(std::vector &dest, uint8_t rembits, const HuffmanSymbol &sym) - { - uint8_t nbits = sym.nbits; + static uint8_t huffmanEncodeSymbol( + std::vector &dest, + uint8_t rembits, + const HuffmanSymbol &sym + ) { + uint8_t nbits = uint8_t(sym.nbits); for (;;) { if (rembits > nbits) { @@ -5257,8 +5260,11 @@ namespace HPack return rembits; } - static void encode(std::vector &dest, const void* src, const size_t srcSize) - { + static void encode( + std::vector &dest, + const void* src, + const size_t srcSize + ) { const uint8_t *data = reinterpret_cast(src); uint8_t rembits = 8; @@ -5279,15 +5285,17 @@ namespace HPack } } - enum HuffmanDecode - { + enum HuffmanDecode { ACCEPT = 0x1, SYMBOL = 0x2, FAIL = 0x4 }; - static bool decode(std::vector &dest, const void* src, const size_t srcSize) - { + static bool decode( + std::vector &dest, + const void* src, + const size_t srcSize + ) { const uint8_t *data = reinterpret_cast(src); const uint8_t *end = data + srcSize; @@ -5321,9 +5329,12 @@ namespace HPack return accept; } - static void packInteger(std::vector &dest, uint64_t num, const uint8_t prefix) - { - uint64_t k = (1 << prefix) - 1; + static void packInteger( + std::vector &dest, + uint64_t num, + const uint8_t prefix + ) { + const uint64_t k = (1 << prefix) - 1; if (num < k) { dest.emplace_back(num); @@ -5350,14 +5361,19 @@ namespace HPack } } - static void packIndex(std::vector &dest, const size_t index) { + static void packIndex( + std::vector &dest, + const size_t index + ) { const size_t head = dest.size(); packInteger(dest, index, 7); dest[head] |= 0x80; } - static std::tuple findHeaderInTable(const std::pair &header, const Http2::DynamicTable &dynamicTable) - { + static std::tuple findHeaderInTable( + const std::pair &header, + const Http2::DynamicTable &dynamicTable + ) { std::tuple result { 0, false }; @@ -5423,19 +5439,31 @@ namespace HPack dest[head] |= 0x80; } else { packInteger(dest, str.length(), 7); - std::copy(str.cbegin(), str.cend(), std::back_inserter(dest) ); + + dest.insert( + dest.end(), + str.cbegin(), + str.cend() + ); } } - static void packFullHeader(std::vector &dest, const std::pair &header, const bool indexing) - { + static void packFullHeader( + std::vector &dest, + const std::pair &header, + const bool indexing + ) { dest.emplace_back(packFirstByte(indexing) ); packString(dest, header.first); packString(dest, header.second); } - static void packHeaderValue(std::vector &dest, const size_t keyIndex, const std::pair &header, const bool indexing) - { + static void packHeaderValue( + std::vector &dest, + const size_t keyIndex, + const std::pair &header, + const bool indexing + ) { const size_t head = dest.size(); const uint8_t prefix = indexing ? 6 : 4; @@ -5458,8 +5486,11 @@ namespace HPack return false; } - static void packHeader(std::vector &dest, const std::pair &header, Http2::DynamicTable &dynamicTable) - { + static void packHeader( + std::vector &dest, + const std::pair &header, + Http2::DynamicTable &dynamicTable + ) { size_t index; bool is_full_match; @@ -5492,23 +5523,24 @@ namespace HPack dest[head] |= 0x20; } */ - void pack(std::vector &dest, const std::vector > &headers, Http2::DynamicTable &dynamicTable) - { + void pack( + std::vector &dest, + const std::vector > &headers, + Http2::DynamicTable &dynamicTable + ) { for (auto const &header : headers) { packHeader(dest, header, dynamicTable); } } - enum class HuffmanDecodeOpcode - { + enum class HuffmanDecodeOpcode { NONE, INDEXED, NOT_INDEXED, INDEXED_KEY, }; - enum class HuffmanDecodeState - { + enum class HuffmanDecodeState { OPCODE, READ_TABLE_SIZE, READ_INDEX, @@ -5526,8 +5558,10 @@ namespace HPack return getStaticTableSize() + stream.conn.decoding_dynamic_table.size(); } - static const std::pair *getHeaderFromTable(const size_t index, const Http2::IncStream &stream) noexcept - { + static const std::pair *getHeaderFromTable( + const size_t index, + const Http2::IncStream &stream + ) noexcept { if (getStaticTableSize() >= index) { return &staticTable[index - 1]; } @@ -5538,7 +5572,10 @@ namespace HPack return nullptr; } - static const std::pair *unpackIndexed(const size_t index, const Http2::IncStream &stream) noexcept { + static const std::pair *unpackIndexed( + const size_t index, + const Http2::IncStream &stream + ) noexcept { return getHeaderFromTable(index, stream); } @@ -5546,8 +5583,12 @@ namespace HPack return c & (1 << 7); } - static std::tuple unpackHuffman(std::vector &dest, const uint8_t *data, const size_t dataSize, const uint64_t left) - { + static std::tuple unpackHuffman( + std::vector &dest, + const uint8_t *data, + const size_t dataSize, + const uint64_t left + ) { std::tuple result { left, false }; @@ -5556,24 +5597,42 @@ namespace HPack std::get(result) = dataSize; } - std::get(result) = decode(dest, data, std::get(result) ); + std::get(result) = decode( + dest, + data, + std::get(result) + ); return result; } - static uint64_t unpackString(std::vector &dest, const uint8_t *data, const size_t dataSize, uint64_t left) - { + static uint64_t unpackString( + std::vector &dest, + const uint8_t *data, + const size_t dataSize, + uint64_t left + ) { if (dataSize < left) { left = dataSize; } - std::copy(data, data + left, std::back_inserter(dest) ); + dest.insert( + dest.end(), + data, + data + left + ); return left; } - static std::tuple unpackInteger(const uint8_t *data, const size_t dataSize, const uint64_t initial, const uint8_t initialShift, const uint8_t prefix) - { + static std::tuple + unpackInteger( + const uint8_t *data, + const size_t dataSize, + const uint64_t initial, + const uint8_t initialShift, + const uint8_t prefix + ) { std::tuple result { initial, 0, initialShift, true }; @@ -5582,7 +5641,7 @@ namespace HPack uint64_t &nread = std::get<1>(result); uint8_t &shift = std::get(result); - uint8_t k = (1 << prefix) - 1; + const uint8_t k = uint8_t(1 << prefix) - 1; if (0 == num) { @@ -5658,8 +5717,8 @@ namespace HPack std::pair unpackFullHeader(Http2::IncStream &stream) { std::pair header { - std::string(this->buf.cbegin(), this->buf.cbegin() + this->key_length), - std::string(this->buf.cbegin() + this->key_length, this->buf.cend() ) + std::string(this->buf.cbegin(), this->buf.cbegin() + long(this->key_length)), + std::string(this->buf.cbegin() + long(this->key_length), this->buf.cend() ) }; this->buf.clear(); @@ -5695,8 +5754,11 @@ namespace HPack } }; - bool unpack(const void *src, const size_t srcSize, Http2::IncStream &stream) - { + bool unpack( + const void *src, + const size_t srcSize, + Http2::IncStream &stream + ) { const uint8_t *data = reinterpret_cast(src); size_t cur = 0; @@ -5743,7 +5805,13 @@ namespace HPack uint64_t nread; bool success; - std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, 5); + std::tie(dec.left, nread, dec.shift, success) = unpackInteger( + data + cur, + srcSize - cur, + dec.left, + dec.shift, + 5 + ); cur += nread; @@ -5756,7 +5824,9 @@ namespace HPack return false; } - stream.conn.decoding_dynamic_table.changeHeaderTableSize(dec.left); + stream.conn.decoding_dynamic_table.changeHeaderTableSize( + static_cast(dec.left) + ); dec.state = HuffmanDecodeState::OPCODE; @@ -5779,7 +5849,13 @@ namespace HPack uint64_t nread; bool success; - std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, prefixlen); + std::tie(dec.left, nread, dec.shift, success) = unpackInteger( + data + cur, + srcSize - cur, + dec.left, + dec.shift, + prefixlen + ); cur += nread; @@ -5811,7 +5887,13 @@ namespace HPack uint64_t nread; bool success; - std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, 7); + std::tie(dec.left, nread, dec.shift, success) = unpackInteger( + data + cur, + srcSize - cur, + dec.left, + dec.shift, + 7 + ); cur += nread; @@ -5819,9 +5901,9 @@ namespace HPack return false; } - dec.state = dec.huffman_encoded ? - HuffmanDecodeState::READ_KEY_HUFFMAN : - HuffmanDecodeState::READ_KEY; + dec.state = dec.huffman_encoded + ? HuffmanDecodeState::READ_KEY_HUFFMAN + : HuffmanDecodeState::READ_KEY; break; } @@ -5831,7 +5913,12 @@ namespace HPack uint64_t nread; bool success; - std::tie(nread, success) = unpackHuffman(dec.buf, data + cur, srcSize - cur, dec.left); + std::tie(nread, success) = unpackHuffman( + dec.buf, + data + cur, + srcSize - cur, + dec.left + ); cur += nread; dec.left -= nread; @@ -5849,7 +5936,12 @@ namespace HPack case HuffmanDecodeState::READ_KEY: { - const uint64_t nread = unpackString(dec.buf, data + cur, srcSize - cur, dec.left); + const uint64_t nread = unpackString( + dec.buf, + data + cur, + srcSize - cur, + dec.left + ); cur += nread; dec.left -= nread; @@ -5878,7 +5970,13 @@ namespace HPack uint64_t nread; bool success; - std::tie(dec.left, nread, dec.shift, success) = unpackInteger(data + cur, srcSize - cur, dec.left, dec.shift, 7); + std::tie(dec.left, nread, dec.shift, success) = unpackInteger( + data + cur, + srcSize - cur, + dec.left, + dec.shift, + 7 + ); cur += nread; @@ -5888,18 +5986,18 @@ namespace HPack if (0 == dec.left) { - HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode ? - stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) : - stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); + HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode + ? stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) + : stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); dec.state = HuffmanDecodeState::OPCODE; break; } - dec.state = dec.huffman_encoded ? - HuffmanDecodeState::READ_VALUE_HUFFMAN : - HuffmanDecodeState::READ_VALUE; + dec.state = dec.huffman_encoded + ? HuffmanDecodeState::READ_VALUE_HUFFMAN + : HuffmanDecodeState::READ_VALUE; break; } @@ -5909,7 +6007,12 @@ namespace HPack uint64_t nread; bool success; - std::tie(nread, success) = unpackHuffman(dec.buf, data + cur, srcSize - cur, dec.left); + std::tie(nread, success) = unpackHuffman( + dec.buf, + data + cur, + srcSize - cur, + dec.left + ); cur += nread; dec.left -= nread; @@ -5918,9 +6021,9 @@ namespace HPack return false; } - HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode ? - stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) : - stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); + HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode + ? stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) + : stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); dec.state = HuffmanDecodeState::OPCODE; @@ -5929,7 +6032,12 @@ namespace HPack case HuffmanDecodeState::READ_VALUE: { - const uint64_t nread = unpackString(dec.buf, data + cur, srcSize - cur, dec.left); + const uint64_t nread = unpackString( + dec.buf, + data + cur, + srcSize - cur, + dec.left + ); cur += nread; dec.left -= nread; @@ -5938,9 +6046,9 @@ namespace HPack return false; } - HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode ? - stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) : - stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); + HuffmanDecodeOpcode::NOT_INDEXED == dec.opcode + ? stream.incoming_headers.emplace(dec.unpackFullHeader(stream) ) + : stream.incoming_headers.emplace(dec.unpackHeaderValue(stream) ); dec.state = HuffmanDecodeState::OPCODE; diff --git a/src/transfer/http2/HPack.h b/src/transfer/http2/HPack.h index b467a15..c030a7f 100644 --- a/src/transfer/http2/HPack.h +++ b/src/transfer/http2/HPack.h @@ -8,8 +8,16 @@ namespace HPack { - void pack(std::vector &dest, const std::vector > &headers, Http2::DynamicTable &dynamicTable); + void pack( + std::vector &dest, + const std::vector > &headers, + Http2::DynamicTable &dynamicTable + ); // TODO: replace IncStream to DynamicTable if possible - bool unpack(const void *src, const size_t srcSize, Http2::IncStream &stream); + bool unpack( + const void *src, + const size_t srcSize, + Http2::IncStream &stream + ); } diff --git a/src/transfer/http2/Http2.cpp b/src/transfer/http2/Http2.cpp index ed7b963..71c60c9 100644 --- a/src/transfer/http2/Http2.cpp +++ b/src/transfer/http2/Http2.cpp @@ -4,16 +4,29 @@ namespace Http2 { - bool operator &(const FrameFlag left, const FrameFlag right) noexcept { + bool operator &( + const FrameFlag left, + const FrameFlag right + ) noexcept { return static_cast(left) & static_cast(right); } - FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept { - return static_cast(static_cast(left) | static_cast(right) ); + FrameFlag operator |( + const FrameFlag left, + const FrameFlag right + ) noexcept { + return static_cast( + static_cast(left) | static_cast(right) + ); } - FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept { - return static_cast(*reinterpret_cast(&left) |= static_cast(right) ); + FrameFlag operator |=( + FrameFlag &left, + const FrameFlag right + ) noexcept { + return static_cast( + *reinterpret_cast(&left) |= static_cast(right) + ); } ConnectionSettings ConnectionSettings::defaultSettings() noexcept { @@ -28,7 +41,9 @@ namespace Http2 } DynamicTable::DynamicTable() noexcept - : header_table_size(0), max_header_list_size(0), cur_header_list_size(0) + : header_table_size(0), + max_header_list_size(0), + cur_header_list_size(0) { } @@ -37,8 +52,15 @@ namespace Http2 return this->list.size(); } - DynamicTable::DynamicTable(const uint32_t headerTableSize, const uint32_t maxHeaderListSize, std::deque > &&list) noexcept - : list(std::move(list) ), header_table_size(headerTableSize), max_header_list_size(maxHeaderListSize), cur_header_list_size(0) + DynamicTable::DynamicTable( + const uint32_t headerTableSize, + const uint32_t maxHeaderListSize, + std::deque > &&list + ) noexcept + : list(std::move(list) ), + header_table_size(headerTableSize), + max_header_list_size(maxHeaderListSize), + cur_header_list_size(0) { for (auto const &pair : list) { this->cur_header_list_size += pair.first.length() + pair.second.length(); @@ -51,8 +73,12 @@ namespace Http2 this->list.emplace_front(header); - while (this->list.size() > this->header_table_size || (0 != this->max_header_list_size && this->cur_header_list_size > this->max_header_list_size) ) - { + while ( + this->list.size() > this->header_table_size || ( + this->max_header_list_size != 0 && + this->cur_header_list_size > this->max_header_list_size + ) + ) { auto const &pair = this->list.back(); this->cur_header_list_size -= pair.first.length() + pair.second.length(); @@ -67,8 +93,12 @@ namespace Http2 this->list.emplace_front(std::move(header) ); - while (this->list.size() > this->header_table_size || (0 != this->max_header_list_size && this->cur_header_list_size > this->max_header_list_size) ) - { + while ( + this->list.size() > this->header_table_size || ( + this->max_header_list_size != 0 && + this->cur_header_list_size > this->max_header_list_size + ) + ) { auto const &pair = this->list.back(); this->cur_header_list_size -= pair.first.length() + pair.second.length(); @@ -94,8 +124,10 @@ namespace Http2 { this->max_header_list_size = maxHeaderListSize; - while (0 != this->max_header_list_size && this->cur_header_list_size > this->max_header_list_size) - { + while ( + this->max_header_list_size != 0 && + this->cur_header_list_size > this->max_header_list_size + ) { auto const &pair = this->list.back(); this->cur_header_list_size -= pair.first.length() + pair.second.length(); @@ -104,11 +136,13 @@ namespace Http2 } } - const std::pair &DynamicTable::operator[](const size_t index) const noexcept { + const std::pair & + DynamicTable::operator[](const size_t index) const noexcept { return this->list[index]; } - std::pair &DynamicTable::operator[](const size_t index) noexcept { + std::pair & + DynamicTable::operator[](const size_t index) noexcept { return this->list[index]; } @@ -117,15 +151,23 @@ namespace Http2 } IncStream::IncStream(const uint32_t streamId, ConnectionData &conn) noexcept - : conn(conn), window_size_inc(conn.server_settings.initial_window_size), - window_size_out(conn.client_settings.initial_window_size), stream_id(streamId), - state(StreamState::IDLE), priority(0), reserved(nullptr) + : conn(conn), + window_size_inc(int32_t(conn.server_settings.initial_window_size)), + window_size_out(int32_t(conn.client_settings.initial_window_size)), + stream_id(streamId), + state(StreamState::IDLE), + priority(0), + reserved(nullptr) { } - uint8_t *IncStream::setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept - { + uint8_t *IncStream::setHttp2FrameHeader( + uint8_t *addr, + const uint32_t frameSize, + const Http2::FrameType frameType, + const Http2::FrameFlag frameFlags + ) noexcept { Utils::hton24(addr, frameSize); *(addr + 3) = static_cast(frameType); *(addr + 4) = static_cast(frameFlags); @@ -142,8 +184,7 @@ namespace Http2 this->conn.sync.mtx.unlock(); } - void IncStream::close() noexcept - { + void IncStream::close() noexcept { this->incoming_headers.clear(); this->incoming_data.clear(); this->incoming_files.clear(); @@ -155,21 +196,37 @@ namespace Http2 // this->state = StreamState::CLOSED; } - OutStream::OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table, std::mutex *mtx) noexcept - : stream_id(streamId), settings(settings), window_size_out(settings.initial_window_size), dynamic_table(std::move(dynamic_table) ), mtx(mtx) + OutStream::OutStream( + const uint32_t streamId, + const ConnectionSettings &settings, + DynamicTable &&dynamic_table, + std::mutex *mtx + ) noexcept + : stream_id(streamId), + window_size_out(int32_t(settings.initial_window_size)), + settings(settings), + dynamic_table(std::move(dynamic_table) ), + mtx(mtx) { } OutStream::OutStream(const IncStream &stream) - : stream_id(stream.stream_id), settings(stream.conn.client_settings), - window_size_out(stream.window_size_out), dynamic_table(stream.conn.encoding_dynamic_table), mtx(&stream.conn.sync.mtx) + : stream_id(stream.stream_id), + window_size_out(stream.window_size_out), + settings(stream.conn.client_settings), + dynamic_table(stream.conn.encoding_dynamic_table), + mtx(&stream.conn.sync.mtx) { } - uint8_t *OutStream::setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept - { + uint8_t *OutStream::setHttp2FrameHeader( + uint8_t *addr, + const uint32_t frameSize, + const Http2::FrameType frameType, + const Http2::FrameFlag frameFlags + ) noexcept { Utils::hton24(addr, frameSize); *(addr + 3) = static_cast(frameType); *(addr + 4) = static_cast(frameFlags); diff --git a/src/transfer/http2/Http2.h b/src/transfer/http2/Http2.h index 12b6d65..f4927f2 100644 --- a/src/transfer/http2/Http2.h +++ b/src/transfer/http2/Http2.h @@ -17,8 +17,7 @@ namespace Http2 { - enum class ErrorCode : uint32_t - { + enum class ErrorCode : uint32_t { NO_ERROR = 0x0, PROTOCOL_ERROR, INTERNAL_ERROR, @@ -35,8 +34,7 @@ namespace Http2 HTTP_1_1_REQUIRED }; - enum class FrameType : uint8_t - { + enum class FrameType : uint8_t { DATA = 0x0, HEADERS, PRIORITY, @@ -49,8 +47,7 @@ namespace Http2 CONTINUATION }; - enum class FrameFlag : uint8_t - { + enum class FrameFlag : uint8_t { EMPTY = 0x0, ACK = 0x1, END_STREAM = 0x1, @@ -63,8 +60,7 @@ namespace Http2 FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept; FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept; - struct FrameMeta - { + struct FrameMeta { uint32_t stream_id; uint32_t length; FrameType type; @@ -72,10 +68,9 @@ namespace Http2 }; constexpr uint32_t FRAME_HEADER_SIZE = 9; - constexpr uint32_t MAX_WINDOW_UPDATE = uint32_t(1 << 31) - 1; + constexpr uint32_t MAX_WINDOW_UPDATE = (uint32_t(1) << 31) - 1; - enum class ConnectionSetting : uint16_t - { + enum class ConnectionSetting : uint16_t { SETTINGS_HEADER_TABLE_SIZE = 0x1, SETTINGS_ENABLE_PUSH = 0x2, SETTINGS_MAX_CONCURRENT_STREAMS = 0x3, @@ -84,8 +79,7 @@ namespace Http2 SETTINGS_MAX_HEADER_LIST_SIZE = 0x6 }; - struct ConnectionSettings - { + struct ConnectionSettings { uint32_t header_table_size; uint32_t enable_push; uint32_t max_concurrent_streams; @@ -96,8 +90,7 @@ namespace Http2 static ConnectionSettings defaultSettings() noexcept; }; - enum class StreamState : uint8_t - { + enum class StreamState : uint8_t { IDLE, RESERVED, OPEN, @@ -105,8 +98,7 @@ namespace Http2 CLOSED }; - struct ConnectionSync - { + struct ConnectionSync { Utils::Event event; std::mutex mtx; std::atomic completed {}; @@ -124,7 +116,12 @@ namespace Http2 public: DynamicTable() noexcept; - DynamicTable(const uint32_t headerTableSize, const uint32_t maxHeaderListSize, std::deque > &&list) noexcept; + + DynamicTable( + const uint32_t headerTableSize, + const uint32_t maxHeaderListSize, + std::deque > &&list + ) noexcept; size_t size() const noexcept; @@ -134,14 +131,17 @@ namespace Http2 void changeHeaderTableSize(const uint32_t headerTableSize); void changeMaxHeaderListSize(const uint32_t maxHeaderListSize); - const std::pair &operator[](const size_t index) const noexcept; - std::pair &operator[](const size_t index) noexcept; + const std::pair & + operator[](const size_t index) const noexcept; - const std::deque > &getList() const noexcept; + std::pair & + operator[](const size_t index) noexcept; + + const std::deque > & + getList() const noexcept; }; - struct ConnectionData - { + struct ConnectionData { DynamicTable decoding_dynamic_table; DynamicTable encoding_dynamic_table; @@ -169,9 +169,17 @@ namespace Http2 void *reserved; public: - IncStream(const uint32_t streamId, ConnectionData &conn) noexcept; - - uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept; + IncStream( + const uint32_t streamId, + ConnectionData &conn + ) noexcept; + + uint8_t *setHttp2FrameHeader( + uint8_t *addr, + const uint32_t frameSize, + const Http2::FrameType frameType, + const Http2::FrameFlag frameFlags + ) noexcept; void lock(); void unlock() noexcept; @@ -182,20 +190,29 @@ namespace Http2 struct OutStream { uint32_t stream_id; - - ConnectionSettings settings; - int32_t window_size_out; + ConnectionSettings settings; DynamicTable dynamic_table; std::mutex *mtx; public: - OutStream(const uint32_t streamId, const ConnectionSettings &settings, DynamicTable &&dynamic_table, std::mutex *mtx) noexcept; + OutStream( + const uint32_t streamId, + const ConnectionSettings &settings, + DynamicTable &&dynamic_table, + std::mutex *mtx + ) noexcept; + OutStream(const IncStream &stream); - uint8_t *setHttp2FrameHeader(uint8_t *addr, const uint32_t frameSize, const Http2::FrameType frameType, const Http2::FrameFlag frameFlags) noexcept; + uint8_t *setHttp2FrameHeader( + uint8_t *addr, + const uint32_t frameSize, + const Http2::FrameType frameType, + const Http2::FrameFlag frameFlags + ) noexcept; void lock(); void unlock() noexcept; diff --git a/src/utils/Event.cpp b/src/utils/Event.cpp index d768a8e..3c9fd01 100644 --- a/src/utils/Event.cpp +++ b/src/utils/Event.cpp @@ -32,7 +32,9 @@ namespace Utils if (this->signaled.load() == false) { std::unique_lock lck(this->mtx); - is_timeout = this->cv.wait_for(lck, ms, [this] { return this->notifed(); } ) == false; + is_timeout = false == this->cv.wait_for(lck, ms, [this] { + return this->notifed(); + }); } if (false == this->manually) { @@ -42,8 +44,9 @@ namespace Utils return is_timeout; } - bool Event::wait_until(const std::chrono::high_resolution_clock::time_point &tp) - { + bool Event::wait_until( + const std::chrono::high_resolution_clock::time_point &tp + ) { bool is_timeout = false; if (this->signaled.load() == false) { diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index 4ddc424..aacd218 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -32,7 +32,11 @@ namespace Utils return str.clear(); } - str.assign(str.cbegin() + str.find_first_not_of(whitespace.data() ), str.cbegin() + last + 1); + str.assign( + str, + str.find_first_not_of(whitespace.data() ), + last + 1 + ); } std::string getTrimmedString(const std::string &str) { @@ -103,13 +107,13 @@ namespace Utils static unsigned char hexStringToBinEncodeSymbol(const char c) noexcept { if (c >= '0' && c <= '9') { - return c - 0x30; + return static_cast(c - 0x30); } else if (c >= 'a' && c <= 'f') { - return c - 0x57; + return static_cast(c - 0x57); } else if (c >= 'A' && c <= 'F') { - return c - 0x37; + return static_cast(c - 0x37); } return 0; @@ -123,7 +127,7 @@ namespace Utils const char a = hexStr[i * 2 + 0]; const char b = hexStr[i * 2 + 1]; - bin[i] = ( + bin[i] = char( (hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b) ); } @@ -224,12 +228,16 @@ namespace Utils x.c[1] = addr[1]; x.c[2] = addr[0]; - return x.ui;// *reinterpret_cast(x.c); + return x.ui; } std::string getUniqueName() { - size_t time = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + size_t time = size_t( + std::chrono::high_resolution_clock::now().time_since_epoch().count() + ); + time = hton64(time); + return binToHexString(&time, sizeof(time) ); } @@ -265,7 +273,7 @@ namespace Utils uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept { if (number <= PACK_NUMBER_SIZE_BYTE) { - *dest = number; + *dest = static_cast(number); dest += sizeof(uint8_t); } @@ -343,12 +351,21 @@ namespace Utils packNumber(buf, str.length() ); if (str.length() ) { - std::copy(str.cbegin(), str.cend(), std::back_inserter(buf) ); + buf.insert( + buf.end(), + str.cbegin(), + str.cend() + ); } } const uint8_t *unpackPointer(void **pointer, const uint8_t *src) noexcept { - *pointer = *reinterpret_cast(const_cast(static_cast(src) ) ); + *pointer = *reinterpret_cast( + const_cast( + static_cast(src) + ) + ); + return src + sizeof(void *); } @@ -403,8 +420,7 @@ namespace Utils /** * Parse RFC 882 (ddd, dd MMM yyyy HH:mm:ss K) */ - time_t rfc822DatetimeToTimestamp(const std::string &strTime) - { + time_t rfc822DatetimeToTimestamp(const std::string &strTime) { std::tm tc {}; // Parse RFC 882 (ddd, dd MMM yyyy HH:mm:ss K) @@ -433,7 +449,7 @@ namespace Utils return ~0; } - tc.tm_mday = std::strtoul(strTime.data() + pos, nullptr, 10); + tc.tm_mday = std::atoi(strTime.data() + pos); pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); @@ -459,7 +475,7 @@ namespace Utils return ~0; } - tc.tm_year = std::strtoul(strTime.data() + pos, nullptr, 10) - 1900; + tc.tm_year = std::atoi(strTime.data() + pos) - 1900; pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(':', pos); @@ -468,7 +484,7 @@ namespace Utils return ~0; } - tc.tm_hour = std::strtoul(strTime.data() + pos, nullptr, 10); + tc.tm_hour = std::atoi(strTime.data() + pos); pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(':', pos); @@ -477,7 +493,7 @@ namespace Utils return ~0; } - tc.tm_min = std::strtoul(strTime.data() + pos, nullptr, 10); + tc.tm_min = std::atoi(strTime.data() + pos); pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); @@ -486,7 +502,7 @@ namespace Utils return ~0; } - tc.tm_sec = std::strtoul(strTime.data() + pos, nullptr, 10); + tc.tm_sec = std::atoi(strTime.data() + pos); pos = strTime.find_first_not_of(' ', delimiter + 1); delimiter = strTime.find_first_of(' ', pos); @@ -516,8 +532,8 @@ namespace Utils zone.copy(hours.data(), 2, 1); zone.copy(minutes.data(), 2, 3); - timezone = std::strtoul(hours.data(), nullptr, 10) * 3600; - timezone += std::strtoul(minutes.data(), nullptr, 10) * 60; + timezone = std::atoi(hours.data()) * 3600; + timezone += std::atoi(minutes.data()) * 60; if (zone.front() == '-') { timezone *= -1; @@ -584,7 +600,7 @@ namespace Utils return ~0; } - tc.tm_mday = std::strtoul(ptrStr, nullptr, 10); + tc.tm_mday = std::atoi(ptrStr); ++strTime; @@ -594,7 +610,7 @@ namespace Utils return ~0; } - tc.tm_year = std::strtoul(strTime, nullptr, 10) - 1900; + tc.tm_year = std::atoi(strTime) - 1900; ++ptrStr; @@ -604,7 +620,7 @@ namespace Utils return ~0; } - tc.tm_hour = std::strtoul(ptrStr, nullptr, 10); + tc.tm_hour = std::atoi(ptrStr); ++strTime; @@ -614,11 +630,11 @@ namespace Utils return ~0; } - tc.tm_min = std::strtoul(strTime, nullptr, 10); + tc.tm_min = std::atoi(strTime); ++ptrStr; - tc.tm_sec = std::strtoul(ptrStr, nullptr, 10); + tc.tm_sec = std::atoi(ptrStr); return localToGmt(std::mktime(&tc) ); } @@ -637,21 +653,29 @@ namespace Utils #ifdef WIN32 std::tm stm {}; - isGmtTime ? - ::localtime_s(&stm, &tTime) : - ::gmtime_s(&stm, &tTime); + isGmtTime + ? ::localtime_s(&stm, &tTime) + : ::gmtime_s(&stm, &tTime); - // RFC 822 - auto const len = std::strftime(buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm); + auto const len = std::strftime( + buf.data(), + buf.size(), + "%a, %d %b %Y %H:%M:%S GMT", // RFC 822 + &stm + ); #else std::tm stm {}; - isGmtTime ? - ::localtime_r(&tTime, &stm) : - ::gmtime_r(&tTime, &stm); + isGmtTime + ? ::localtime_r(&tTime, &stm) + : ::gmtime_r(&tTime, &stm); - // RFC 822 - auto const len = std::strftime(buf.data(), buf.size(), "%a, %d %b %G %H:%M:%S GMT", &stm); + auto const len = std::strftime( + buf.data(), + buf.size(), + "%a, %d %b %G %H:%M:%S GMT", // RFC 822 + &stm + ); #endif return std::string(buf.data(), buf.data() + len); @@ -675,14 +699,19 @@ namespace Utils return length; } - bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies) - { + bool parseCookies( + const std::string &cookieHeader, + std::unordered_multimap &cookies + ) { if (cookieHeader.empty() ) { return true; } - for (size_t cur_pos = 0, next_value; std::string::npos != cur_pos; cur_pos = next_value) - { + for ( + size_t cur_pos = 0, next_value; + std::string::npos != cur_pos; + cur_pos = next_value + ) { next_value = cookieHeader.find(';', cur_pos); size_t delimiter = cookieHeader.find('=', cur_pos); @@ -691,17 +720,30 @@ namespace Utils return false; } - std::string key = cookieHeader.substr(cur_pos, delimiter - cur_pos); + std::string key = cookieHeader.substr( + cur_pos, + delimiter - cur_pos + ); + trim(key); key = urlDecode(key); ++delimiter; - std::string value = cookieHeader.substr(delimiter, std::string::npos != next_value ? next_value - delimiter : next_value); + std::string value = cookieHeader.substr( + delimiter, + std::string::npos != next_value + ? next_value - delimiter + : next_value + ); + trim(value); value = urlDecode(value); - cookies.emplace(std::move(key), std::move(value) ); + cookies.emplace( + std::move(key), + std::move(value) + ); if (std::string::npos != next_value) { ++next_value; @@ -723,10 +765,10 @@ namespace Utils for (size_t i = 0; i < str.length(); ++i) { - const unsigned char c = str[i]; + const unsigned char c = static_cast(str[i]); - if (std::isalnum(c) || isCharUrlAllowed(c) ) { - encoded.push_back(c); + if (std::isalnum(c) || isCharUrlAllowed(char(c) ) ) { + encoded.push_back(char(c) ); } else if (' ' == c) { encoded.push_back('+'); @@ -749,14 +791,14 @@ namespace Utils for (size_t i = 0; i < str.length(); ++i) { - unsigned char c = str[i]; + unsigned char c = static_cast(str[i]); if ('%' == c) { if (i + 2 < str.length() ) { const char a = str[++i]; const char b = str[++i]; - c = ( + c = static_cast( (hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b) ); } @@ -765,7 +807,7 @@ namespace Utils c = ' '; } - decoded.push_back(c); + decoded.push_back(char(c) ); } return decoded; diff --git a/src/utils/Utils.h b/src/utils/Utils.h index 340c6eb..551e975 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -128,7 +128,10 @@ namespace Utils size_t getNumberLength(size_t number) noexcept; - bool parseCookies(const std::string &cookieHeader, std::unordered_multimap &cookies); + bool parseCookies( + const std::string &cookieHeader, + std::unordered_multimap &cookies + ); std::string urlEncode(const std::string &str); std::string urlDecode(const std::string &str); From 4b3ee40b97abd07e63a8614b0a66e123d3f1376d Mon Sep 17 00:00:00 2001 From: Ignat Prokopovich Date: Sun, 1 Apr 2018 17:26:45 +0300 Subject: [PATCH 19/22] Fixed code (and style) for Windows. --- src/server/config/ConfigParser.cpp | 6 +- src/server/data-variant/FormUrlencoded.cpp | 23 +++---- src/server/protocol/ServerHttp2Protocol.cpp | 5 +- src/server/protocol/extensions/Sendfile.cpp | 18 ++++- src/socket/AdapterTls.cpp | 4 +- src/socket/List.cpp | 12 ++-- src/socket/Socket.cpp | 23 ++++--- src/system/GlobalMutex.cpp | 38 +++++++++-- src/system/Module.cpp | 76 ++++++++++++++------- src/system/SharedMemory.cpp | 44 ++++++++++-- src/system/System.cpp | 9 ++- src/system/System.h | 6 +- 12 files changed, 179 insertions(+), 85 deletions(-) diff --git a/src/server/config/ConfigParser.cpp b/src/server/config/ConfigParser.cpp index 1bbb42a..ab08166 100644 --- a/src/server/config/ConfigParser.cpp +++ b/src/server/config/ConfigParser.cpp @@ -279,7 +279,7 @@ namespace HttpServer try { if (app_init) { - const std::string root = root_dir; + const std::string &root = root_dir; success = app_init(root.data() ); } } @@ -356,8 +356,8 @@ namespace HttpServer if (names.empty() ) { apps_tree.addApplication(app_name, settings); } else { - for (size_t i = 0; i < names.size(); ++i) { - apps_tree.addApplication(names[i], settings); + for (auto const &name : names) { + apps_tree.addApplication(name, settings); } } diff --git a/src/server/data-variant/FormUrlencoded.cpp b/src/server/data-variant/FormUrlencoded.cpp index 08f8180..28adff0 100644 --- a/src/server/data-variant/FormUrlencoded.cpp +++ b/src/server/data-variant/FormUrlencoded.cpp @@ -1,4 +1,4 @@ - + #include "FormUrlencoded.h" #include "../../utils/Utils.h" @@ -20,7 +20,7 @@ namespace DataVariant std::string::npos != var_end; var_pos = var_end + 1 ) { - // Поиск следующего параметра + // Search next parameter var_end = buf.find('&', var_pos); if (std::string::npos == var_end) { @@ -30,12 +30,11 @@ namespace DataVariant } } - // Поиск значения параметра + // Search parameter value size_t delimiter = buf.find('=', var_pos); - if (delimiter >= var_end) - { - // Получить имя параметра + if (delimiter >= var_end) { + // Get parameter name std::string var_name = Utils::urlDecode( buf.substr( var_pos, @@ -45,15 +44,13 @@ namespace DataVariant ) ); - // Сохранить параметр с пустым значением + // Store parameter with empty value rd->incoming_data.emplace( std::move(var_name), std::string() ); - } - else - { - // Получить имя параметра + } else { + // Get parameter name std::string var_name = Utils::urlDecode( buf.substr( var_pos, @@ -63,7 +60,7 @@ namespace DataVariant ++delimiter; - // Получить значение параметра + // Get parameter value std::string var_value = Utils::urlDecode( buf.substr( delimiter, @@ -73,7 +70,7 @@ namespace DataVariant ) ); - // Сохранить параметр и значение + // Store parameter and value rd->incoming_data.emplace( std::move(var_name), std::move(var_value) diff --git a/src/server/protocol/ServerHttp2Protocol.cpp b/src/server/protocol/ServerHttp2Protocol.cpp index fd4ed10..6cb996b 100644 --- a/src/server/protocol/ServerHttp2Protocol.cpp +++ b/src/server/protocol/ServerHttp2Protocol.cpp @@ -25,9 +25,8 @@ namespace HttpServer } std::random_device rd; - std::uniform_int_distribution dist; - uint8_t padding = dist(rd); + uint8_t padding = uint8_t(rd()); while (dataSize <= padding) { padding /= 2; @@ -177,7 +176,7 @@ namespace HttpServer } data += data_size; - send_size += data_size; + send_size += long(data_size); dt->send_total += data_size; // stream->window_size_out -= frame_size; diff --git a/src/server/protocol/extensions/Sendfile.cpp b/src/server/protocol/extensions/Sendfile.cpp index 1758515..2799f57 100644 --- a/src/server/protocol/extensions/Sendfile.cpp +++ b/src/server/protocol/extensions/Sendfile.cpp @@ -100,7 +100,9 @@ namespace HttpServer *resultRangeHeader += std::to_string(range_begin) + '-' + std::to_string(range_end) + ','; - ranges.emplace_back(std::tuple {range_begin, length}); + ranges.emplace_back(std::tuple { + range_begin, length + }); } } else // if range_end_str empty @@ -275,7 +277,12 @@ namespace HttpServer send_size_left -= send_size; } - while (false == file.eof() && false == file.fail() && send_size > 0 && send_size_left); + while ( + false == file.eof() && + false == file.fail() && + send_size > 0 && + send_size_left + ); } } @@ -407,7 +414,12 @@ namespace HttpServer &dt ); } - while (false == file.eof() && false == file.fail() && send_size > 0 && (dt.full_size - dt.send_total) ); + while ( + false == file.eof() && + false == file.fail() && + send_size > 0 && + (dt.full_size - dt.send_total) + ); } file.close(); diff --git a/src/socket/AdapterTls.cpp b/src/socket/AdapterTls.cpp index 118e885..0126830 100644 --- a/src/socket/AdapterTls.cpp +++ b/src/socket/AdapterTls.cpp @@ -97,10 +97,10 @@ namespace Socket return send_size; } - total += send_size; + total += long(send_size); } - return static_cast(total); + return long(total); } System::native_socket_type AdapterTls::get_handle() const noexcept { diff --git a/src/socket/List.cpp b/src/socket/List.cpp index a1accbd..eab986d 100644 --- a/src/socket/List.cpp +++ b/src/socket/List.cpp @@ -1,4 +1,4 @@ - + #include "List.h" #ifdef POSIX @@ -271,17 +271,15 @@ namespace Socket return false; } - for (size_t i = 0; i < this->poll_events.size(); ++i) + for (auto const &event : this->poll_events) { - const WSAPOLLFD &event = this->poll_events[i]; - if (event.revents & POLLRDNORM) { System::native_socket_type client_socket = ~0; do { ::sockaddr_in client_addr {}; - socklen_t client_addr_len = sizeof(client_addr); + ::socklen_t client_addr_len = sizeof(client_addr); client_socket = ::accept( event.fd, @@ -367,10 +365,8 @@ namespace Socket return false; } - for (size_t i = 0; i < this->poll_events.size(); ++i) + for (auto const &event : this->poll_events) { - const WSAPOLLFD &event = this->poll_events[i]; - if (event.revents & POLLRDNORM) { sockets.emplace_back(Socket(event.fd) ); } diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index 54904d3..40b0f92 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -188,7 +188,7 @@ namespace Socket 0 }; - if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { + if (1 == ::WSAPoll(&event, 1, int(timeout.count()) ) && event.revents & POLLRDNORM) { client_socket = ::accept( this->socket_handle, static_cast(nullptr), @@ -235,7 +235,12 @@ namespace Socket { #ifdef WIN32 unsigned long value = isNonBlock; - return 0 == ::ioctlsocket(this->socket_handle, FIONBIO, &value); + + return 0 == ::ioctlsocket( + this->socket_handle, + FIONBIO, + &value + ); #elif POSIX return ~0 != ::fcntl( this->socket_handle, @@ -338,7 +343,7 @@ namespace Socket 0 }; - if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLRDNORM) { + if (1 == ::WSAPoll(&event, 1, int(timeout.count()) ) && event.revents & POLLRDNORM) { recv_len = ::recv( this->socket_handle, reinterpret_cast(buf), @@ -372,7 +377,7 @@ namespace Socket 0 }; - return ::WSAPoll(&event, 1, timeout.count() ) == 1; + return ::WSAPoll(&event, 1, int(timeout.count()) ) == 1; #elif POSIX struct ::pollfd event { this->socket_handle, @@ -405,7 +410,7 @@ namespace Socket return send_size; } - total += static_cast(send_size); + total += size_t(send_size); } return static_cast(total); @@ -435,7 +440,7 @@ namespace Socket }; while (total < length) { - if (1 == ::WSAPoll(&event, 1, static_cast<::INT>(timeout.count() ) ) && event.revents & POLLOUT) { + if (1 == ::WSAPoll(&event, 1, int(timeout.count()) ) && event.revents & POLLOUT) { const long send_size = ::send( socket_handle, reinterpret_cast(data) + total, @@ -447,7 +452,7 @@ namespace Socket return send_size; } - total += send_size; + total += size_t(send_size); } else { return -1; } @@ -473,7 +478,7 @@ namespace Socket return send_size; } - total += static_cast(send_size); + total += size_t(send_size); } else { return -1; } @@ -482,7 +487,7 @@ namespace Socket #error "Undefined platform" #endif - return static_cast(total); + return long(total); } long Socket::nonblock_send( diff --git a/src/system/GlobalMutex.cpp b/src/system/GlobalMutex.cpp index 0c72d06..9a5477e 100644 --- a/src/system/GlobalMutex.cpp +++ b/src/system/GlobalMutex.cpp @@ -39,7 +39,11 @@ namespace System const std::string &mutex_name = this->mtx_name; #endif - this->mtx_desc = ::CreateMutex(nullptr, false, mutex_name.c_str() ); + this->mtx_desc = ::CreateMutex( + nullptr, + false, + mutex_name.c_str() + ); if (nullptr == this->mtx_desc) { return false; @@ -77,7 +81,11 @@ namespace System const std::string &mutex_name = mtx_name; #endif - ::HANDLE hMutex = ::OpenMutex(DELETE, true, mutex_name.c_str() ); + const ::HANDLE hMutex = ::OpenMutex( + DELETE, + true, + mutex_name.c_str() + ); return 0 != ::CloseHandle(hMutex); #elif POSIX @@ -97,9 +105,15 @@ namespace System const std::string &mutex_name = this->mtx_name; #endif - ::HANDLE hMutex = ::OpenMutex(DELETE, true, mutex_name.c_str() ); + const ::HANDLE hMutex = ::OpenMutex( + DELETE, + true, + mutex_name.c_str() + ); - const bool ret = (0 != ::CloseHandle(hMutex) ); + const bool ret = ( + 0 != ::CloseHandle(hMutex) + ); this->close(); @@ -132,7 +146,11 @@ namespace System const std::string &mutex_name = this->mtx_name; #endif - this->mtx_desc = ::OpenMutex(SYNCHRONIZE, false, mutex_name.c_str() ); + this->mtx_desc = ::OpenMutex( + SYNCHRONIZE, + false, + mutex_name.c_str() + ); if (nullptr == this->mtx_desc) { return false; @@ -193,7 +211,10 @@ namespace System bool GlobalMutex::lock() const noexcept { #ifdef WIN32 - return WAIT_OBJECT_0 == ::WaitForSingleObject(this->mtx_desc, INFINITE); + return WAIT_OBJECT_0 == ::WaitForSingleObject( + this->mtx_desc, + INFINITE + ); #elif POSIX return 0 == ::sem_wait(this->mtx_desc); #else @@ -204,7 +225,10 @@ namespace System bool GlobalMutex::try_lock() const noexcept { #ifdef WIN32 - return WAIT_OBJECT_0 == ::WaitForSingleObject(this->mtx_desc, 0); + return WAIT_OBJECT_0 == ::WaitForSingleObject( + this->mtx_desc, + 0 + ); #elif POSIX return 0 == ::sem_trywait(this->mtx_desc); #else diff --git a/src/system/Module.cpp b/src/system/Module.cpp index a417a59..ef7f512 100644 --- a/src/system/Module.cpp +++ b/src/system/Module.cpp @@ -1,4 +1,4 @@ - + #include "Module.h" #ifdef WIN32 @@ -52,24 +52,26 @@ namespace System const size_t pos_slash = libPath.rfind('\\'); const size_t pos_slash_back = libPath.rfind('/'); - size_t pos = std::string::npos; - - if (pos_slash != std::string::npos && pos_slash > pos_slash_back) - { - pos = pos_slash; - } - else if (pos_slash_back != std::string::npos) - { - pos = pos_slash_back; - } - - DLL_DIRECTORY_COOKIE cookie = nullptr; - - if (std::string::npos != pos) - { - std::wstring directory(libPath.cbegin(), libPath.cbegin() + pos + 1); + const size_t pos = ( + pos_slash != std::string::npos && + pos_slash > pos_slash_back + ? pos_slash + : pos_slash_back != std::string::npos + ? pos_slash_back + : std::string::npos + ); + + ::DLL_DIRECTORY_COOKIE cookie = nullptr; + + if (std::string::npos != pos) { + const std::wstring directory( + libPath.cbegin(), + libPath.cbegin() + pos + 1 + ); - cookie = ::AddDllDirectory(directory.data() ); + cookie = ::AddDllDirectory( + directory.data() + ); } #ifdef UNICODE @@ -79,13 +81,20 @@ namespace System const std::string &lib_path = libPath; #endif - this->lib_handle = ::LoadLibraryEx(lib_path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); + this->lib_handle = ::LoadLibraryEx( + lib_path.c_str(), + 0, + LOAD_LIBRARY_SEARCH_DEFAULT_DIRS + ); if (cookie) { ::RemoveDllDirectory(cookie); } #elif POSIX - this->lib_handle = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL); + this->lib_handle = ::dlopen( + libPath.c_str(), + RTLD_NOW | RTLD_LOCAL + ); #else #error "Undefined platform" #endif @@ -121,17 +130,24 @@ namespace System const std::string &symbolName, void *(**addr)(void *) ) const noexcept { - if (lib_handle) - { + if (lib_handle) { #ifdef WIN32 - *addr = reinterpret_cast(::GetProcAddress(this->lib_handle, symbolName.c_str() ) ); + *addr = reinterpret_cast( + ::GetProcAddress( + this->lib_handle, + symbolName.c_str() + ) + ); return nullptr != *addr; #elif POSIX char *error = ::dlerror(); *addr = reinterpret_cast( - ::dlsym(this->lib_handle, symbolName.c_str() ) + ::dlsym( + this->lib_handle, + symbolName.c_str() + ) ); error = ::dlerror(); @@ -152,14 +168,22 @@ namespace System if (lib_handle) { #ifdef WIN32 - *addr = reinterpret_cast(::GetProcAddress(this->lib_handle, symbolName) ); + *addr = reinterpret_cast( + ::GetProcAddress( + this->lib_handle, + symbolName + ) + ); return nullptr != *addr; #elif POSIX char *error = ::dlerror(); *addr = reinterpret_cast( - ::dlsym(this->lib_handle, symbolName) + ::dlsym( + this->lib_handle, + symbolName + ) ); error = ::dlerror(); diff --git a/src/system/SharedMemory.cpp b/src/system/SharedMemory.cpp index 3169941..a88b1e7 100644 --- a/src/system/SharedMemory.cpp +++ b/src/system/SharedMemory.cpp @@ -103,7 +103,11 @@ namespace System const std::string &memory_name = this->shm_name; #endif - this->shm_desc = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, false, memory_name.c_str() ); + this->shm_desc = ::OpenFileMapping( + FILE_MAP_ALL_ACCESS, + false, + memory_name.c_str() + ); if (nullptr == this->shm_desc) { return false; @@ -150,7 +154,16 @@ namespace System ) const noexcept { #ifdef WIN32 - void * const addr = ::MapViewOfFile(this->shm_desc, FILE_MAP_WRITE, 0, static_cast<::DWORD>(offset), size); + ::ULARGE_INTEGER off; + off.QuadPart = ::ULONGLONG(offset); + + void * const addr = ::MapViewOfFile( + this->shm_desc, + FILE_MAP_WRITE, + off.HighPart, + off.LowPart, + size + ); if (nullptr == addr) { return false; @@ -193,7 +206,16 @@ namespace System ) const noexcept { #ifdef WIN32 - void * const addr = ::MapViewOfFile(this->shm_desc, FILE_MAP_READ, 0, static_cast<::DWORD>(offset), size); + ::ULARGE_INTEGER off; + off.QuadPart = ::ULONGLONG(offset); + + void * const addr = ::MapViewOfFile( + this->shm_desc, + FILE_MAP_READ, + off.HighPart, + off.LowPart, + size + ); if (nullptr == addr) { return false; @@ -261,9 +283,15 @@ namespace System const std::string &memory_name = this->shm_name; #endif - ::HANDLE hMemory = ::OpenFileMapping(DELETE, false, memory_name.c_str() ); + const ::HANDLE hMemory = ::OpenFileMapping( + DELETE, + false, + memory_name.c_str() + ); - const bool ret = (0 != ::CloseHandle(hMemory) ); + const bool ret = ( + 0 != ::CloseHandle(hMemory) + ); this->close(); @@ -293,7 +321,11 @@ namespace System const std::string &memory_name = shm_name; #endif - ::HANDLE hMemory = ::OpenFileMapping(DELETE, false, memory_name.c_str() ); + const ::HANDLE hMemory = ::OpenFileMapping( + DELETE, + false, + memory_name.c_str() + ); return 0 != ::CloseHandle(hMemory); #elif POSIX diff --git a/src/system/System.cpp b/src/system/System.cpp index af696d9..ebb5935 100644 --- a/src/system/System.cpp +++ b/src/system/System.cpp @@ -1,4 +1,4 @@ - + #include "System.h" #include @@ -37,7 +37,7 @@ namespace System { std::array<::TCHAR, 257> class_name; - ::GetClassName(hWnd, class_name.data(), static_cast(class_name.size() - 1) ); + ::GetClassName(hWnd, class_name.data(), int(class_name.size() - 1) ); if (0 == ::_tcscmp(class_name.data(), myWndClassName) ) { ed.hWnd = hWnd; @@ -284,7 +284,10 @@ namespace System const size_t pos = memory_name.rfind(file_ext); if (pos == memory_name.length() - file_ext.length() ) { - memory_name.erase(memory_name.begin() + pos, memory_name.end() ); + memory_name.erase( + pos, + memory_name.length() + ); } ::TCHAR buf[MAX_PATH + 1] {}; diff --git a/src/system/System.h b/src/system/System.h index d7ae219..8b637d4 100644 --- a/src/system/System.h +++ b/src/system/System.h @@ -1,9 +1,11 @@ -#pragma once +#pragma once #ifdef WIN32 #include - typedef long ssize_t; + #ifndef ssize_t + typedef long ssize_t; + #endif ::TCHAR myWndClassName[]; From 0e8c8dd92569d86fc7dae2da08e1731fe497c0a2 Mon Sep 17 00:00:00 2001 From: awwit Date: Mon, 2 Apr 2018 21:34:19 +0300 Subject: [PATCH 20/22] Optimized some copy operations. Added copy constructor for FileIncoming. --- src/server/protocol/ServerHttp2.cpp | 6 +++--- src/server/protocol/ServerHttp2Protocol.cpp | 8 +++++--- src/transfer/FileIncoming.h | 7 ++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/server/protocol/ServerHttp2.cpp b/src/server/protocol/ServerHttp2.cpp index 12ae2be..35632ad 100644 --- a/src/server/protocol/ServerHttp2.cpp +++ b/src/server/protocol/ServerHttp2.cpp @@ -590,9 +590,9 @@ namespace HttpServer } } else { std::copy( - buf.cbegin() + length, - buf.cbegin() + read_size, - buf.begin() + buf.data() + length, + buf.data() + read_size, + buf.data() ); read_size -= length; diff --git a/src/server/protocol/ServerHttp2Protocol.cpp b/src/server/protocol/ServerHttp2Protocol.cpp index 6cb996b..857c216 100644 --- a/src/server/protocol/ServerHttp2Protocol.cpp +++ b/src/server/protocol/ServerHttp2Protocol.cpp @@ -106,7 +106,9 @@ namespace HttpServer } } - const size_t frame_size = data_size + padding_size; + const uint32_t frame_size = static_cast( + data_size + padding_size + ); buf.resize(frame_size + Http2::FRAME_HEADER_SIZE); @@ -141,7 +143,7 @@ namespace HttpServer this->stream->setHttp2FrameHeader( buf.data(), - static_cast(frame_size), + frame_size, Http2::FrameType::DATA, flags ); @@ -149,7 +151,7 @@ namespace HttpServer std::copy( data, data + data_size, - buf.begin() + long(cur) + buf.data() + cur ); if (padding) { diff --git a/src/transfer/FileIncoming.h b/src/transfer/FileIncoming.h index 87e745b..448c8c8 100644 --- a/src/transfer/FileIncoming.h +++ b/src/transfer/FileIncoming.h @@ -14,10 +14,9 @@ namespace Transfer std::string file_type; size_t file_size; - private: - FileIncoming() = delete; - public: + FileIncoming() = default; + FileIncoming( std::string &&fileTmpName, std::string &&fileName, @@ -30,6 +29,8 @@ namespace Transfer ~FileIncoming() noexcept = default; + FileIncoming &operator =(const FileIncoming &) = default; + const std::string &getTmpName() const noexcept; const std::string &getName() const noexcept; const std::string &getType() const noexcept; From a3389a3fee20b8186754bca73faac8ee8c4e7146 Mon Sep 17 00:00:00 2001 From: awwit Date: Thu, 31 May 2018 16:12:19 +0300 Subject: [PATCH 21/22] 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 22/22] 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(