From 7df295905a1debbe432eec680ac7550cc5a4ef5b Mon Sep 17 00:00:00 2001 From: Jeppe Pihl Date: Tue, 10 Apr 2018 10:06:28 +0200 Subject: [PATCH] Remove re2 dependency (#1) * Replaced RE2 with std::regex * Removed ZeroMQ cmake script * Fixed Bug in server.cpp where parameters was passed in an incorrect order. * Started work on Windows support: - Remove unused imports not available on windows. - Fix issues reported by MSVC --- .travis.yml | 9 +- BUILD | 12 +-- CMakeLists.txt | 4 - README.md | 1 - WORKSPACE | 6 -- cmake/FindRE2.cmake | 68 -------------- cmake/FindZeroMQ.cmake | 124 -------------------------- src/examples/handlers/main.cpp | 2 - src/examples/list_endpoints/main.cpp | 2 - src/examples/rest_resource/main.cpp | 2 - src/served/CMakeLists.txt | 4 +- src/served/mux/regex_matcher.cpp | 11 +-- src/served/mux/regex_matcher.hpp | 5 +- src/served/net/connection.cpp | 1 + src/served/net/connection_manager.cpp | 3 + src/served/net/connection_manager.hpp | 2 + src/served/net/server.cpp | 3 +- src/served/request_parser.hpp | 1 + src/served/request_parser_impl.cpp | 1 + src/served/uri.cpp | 4 +- 20 files changed, 22 insertions(+), 243 deletions(-) delete mode 100644 cmake/FindRE2.cmake delete mode 100644 cmake/FindZeroMQ.cmake diff --git a/.travis.yml b/.travis.yml index 932834f..f237df2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: cpp os: - linux - + compiler: - clang @@ -33,13 +33,6 @@ before_install: - cmake --version - which g++ - g++ -v - - git clone https://github.com/google/re2.git - - cd re2 - - git checkout fcdcf25d1a3209355411c57c342e8db115fc9582 - - make - - make test - - sudo make install - - sudo make testinstall - cd .. script: diff --git a/BUILD b/BUILD index cbcbb46..4ff18da 100644 --- a/BUILD +++ b/BUILD @@ -4,9 +4,7 @@ exports_files(["LICENSE.md"]) cc_library( name = "served", - copts = [ - "-Iexternal/com_googlesource_code_re2", - ], + copts = [], srcs = [ "src/served/methods_handler.cpp", "src/served/multiplexer.cpp", @@ -56,17 +54,13 @@ cc_library( deps = [ "//third_party/boost:system", "//third_party/boost:asio", - "//third_party/boost:date_time", - "@com_googlesource_code_re2//:re2", + "//third_party/boost:date_time" ], ) cc_test( name = "served-test", - copts = [ - "-Iexternal/com_googlesource_code_re2", - "-Isrc", - ], + copts = ["-Isrc",], srcs = [ "src/served/methods_handler.test.cpp", "src/served/multiplexer.test.cpp", diff --git a/CMakeLists.txt b/CMakeLists.txt index 217ab74..ed64f1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,10 +75,6 @@ LINK_DIRECTORIES (${Boost_LIBRARY_DIRS}) INCLUDE (FindRAGEL) FIND_PACKAGE (RAGEL) -INCLUDE (FindRE2) -FIND_PACKAGE (RE2 REQUIRED) -INCLUDE_DIRECTORIES (${RE2_INCLUDE_DIR}) - FIND_PACKAGE (Threads) INCLUDE (EnableStdCXX11) diff --git a/README.md b/README.md index 1c50278..ab3903e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ Features: ### Requirements * [Required] - [Boost 1.56](http://www.boost.org/) -* [Required] - [RE2](http://code.google.com/p/re2/) * [Optional] - [Ragel](http://www.complang.org/ragel/) ### Building diff --git a/WORKSPACE b/WORKSPACE index 26852f1..863fb47 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,7 +1 @@ workspace(name = "com_github_datasift_served") - -git_repository( - name = "com_googlesource_code_re2", - remote = "https://github.com/google/re2.git", - commit = "22fc950c75d238f8b2dcbc43d8a60573cad2b8d7", -) diff --git a/cmake/FindRE2.cmake b/cmake/FindRE2.cmake deleted file mode 100644 index 1fa5695..0000000 --- a/cmake/FindRE2.cmake +++ /dev/null @@ -1,68 +0,0 @@ -# - Find re2 headers and lib. -# This module defines -# RE2_INCLUDE_DIR, directory containing headers -# RE2_LIBRARY, path to re2.a -# RE2_FOUND, whether RE2 has been found - -# ==================================================================== -# Copyright (C) 2014 MediaSift Ltd. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# ==================================================================== - -find_path(RE2_INCLUDE_DIR re2/re2.h NO_DEFAULT_PATH PATHS - /usr/include - /opt/local/include - /usr/local/include -) - -set(RE2_NAMES ${RE2_NAMES} re2) -find_library(RE2_LIBRARY NAMES ${RE2_NAMES} NO_DEFAULT_PATH PATHS - /usr/local/lib - /usr/local/re2/lib - /opt/local/lib - /usr/lib - /lib/i386-linux-gnu - /lib/x86_64-linux-gnu - /usr/lib/i386-linux-gnu - /usr/lib/x86_64-linux-gnu -) - -if (RE2_INCLUDE_DIR AND RE2_LIBRARY) - set(RE2_FOUND TRUE) - set( RE2_LIBRARIES ${RE2_LIBRARY} ) -else () - set(RE2_FOUND FALSE) - set( RE2_LIBRARIES ) -endif () - -if (RE2_FOUND) - message(STATUS "Found RE2: ${RE2_LIBRARY}") -else () - message(STATUS "Not Found RE2: ${RE2_LIBRARY}") - if (RE2_FIND_REQUIRED) - message(STATUS "Looked for RE2 libraries named ${RE2_NAMES}.") - message(FATAL_ERROR "Could NOT find RE2 library") - endif () -endif () - -mark_as_advanced( - RE2_LIBRARY - RE2_INCLUDE_DIR -) \ No newline at end of file diff --git a/cmake/FindZeroMQ.cmake b/cmake/FindZeroMQ.cmake deleted file mode 100644 index b087de5..0000000 --- a/cmake/FindZeroMQ.cmake +++ /dev/null @@ -1,124 +0,0 @@ -# - Try to find ZeroMQ headers and libraries -# -# Usage of this module as follows: -# -# find_package(ZeroMQ) -# -# Variables used by this module, they can change the default behaviour and need -# to be set before calling find_package: -# -# ZeroMQ_ROOT_DIR Set this variable to the root installation of -# ZeroMQ if the module has problems finding -# the proper installation path. -# -# Variables defined by this module: -# -# ZEROMQ_FOUND System has ZeroMQ libs/headers -# ZeroMQ_LIBRARIES The ZeroMQ libraries -# ZeroMQ_INCLUDE_DIR The location of ZeroMQ headers - -# ==================================================================== -# Copyright (C) 2014 MediaSift Ltd. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# ==================================================================== - -find_path(ZeroMQ_ROOT_DIR - NAMES include/zmq.h - ) - -if(MSVC) - #add in all the names it can have on windows - if(CMAKE_GENERATOR_TOOLSET MATCHES "v140" OR MSVC14) - set(_zmq_TOOLSET "-v140") - elseif(CMAKE_GENERATOR_TOOLSET MATCHES "v120" OR MSVC12) - set(_zmq_TOOLSET "-v120") - elseif(CMAKE_GENERATOR_TOOLSET MATCHES "v110_xp") - set(_zmq_TOOLSET "-v110_xp") - elseif(CMAKE_GENERATOR_TOOLSET MATCHES "v110" OR MSVC11) - set(_zmq_TOOLSET "-v110") - elseif(CMAKE_GENERATOR_TOOLSET MATCHES "v100" OR MSVC10) - set(_zmq_TOOLSET "-v100") - elseif(CMAKE_GENERATOR_TOOLSET MATCHES "v90" OR MSVC90) - set(_zmq_TOOLSET "-v90") - endif() - - set(_zmq_versions "4_0_4" "4_0_3" "4_0_2" "4_0_1" "4_0_0" - "3_2_5" "3_2_4" "3_2_3" "3_2_2" "3_2_1" "3_2_0" "3_1_0") - set(_zmq_release_names) - set(_zmq_debug_names) - foreach( ver ${_zmq_versions}) - list(APPEND _zmq_release_names "libzmq${_zmq_TOOLSET}-mt-${ver}") - endforeach() - foreach( ver ${_zmq_versions}) - list(APPEND _zmq_debug_names "libzmq${_zmq_TOOLSET}-mt-gd-${ver}") - endforeach() - - #now try to find the release and debug version - find_library(ZeroMQ_LIBRARY_RELEASE - NAMES ${_zmq_release_names} zmq libzmq - HINTS ${ZeroMQ_ROOT_DIR}/bin - ${ZeroMQ_ROOT_DIR}/lib - ) - - find_library(ZeroMQ_LIBRARY_DEBUG - NAMES ${_zmq_debug_names} zmq libzmq - HINTS ${ZeroMQ_ROOT_DIR}/bin - ${ZeroMQ_ROOT_DIR}/lib - ) - - if(ZeroMQ_LIBRARY_RELEASE AND ZeroMQ_LIBRARY_DEBUG) - set(ZeroMQ_LIBRARY - debug ${ZeroMQ_LIBRARY_DEBUG} - optimized ${ZeroMQ_LIBRARY_RELEASE} - ) - elseif(ZeroMQ_LIBRARY_RELEASE) - set(ZeroMQ_LIBRARY ${ZeroMQ_LIBRARY_RELEASE}) - elseif(ZeroMQ_LIBRARY_DEBUG) - set(ZeroMQ_LIBRARY ${ZeroMQ_LIBRARY_DEBUG}) - endif() - -else() - find_library(ZeroMQ_LIBRARY - NAMES zmq libzmq - HINTS ${ZeroMQ_ROOT_DIR}/lib - ) -endif() - -find_path(ZeroMQ_INCLUDE_DIR - NAMES zmq.h - HINTS ${ZeroMQ_ROOT_DIR}/include - ) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(ZeroMQ DEFAULT_MSG - ZeroMQ_LIBRARY - ZeroMQ_INCLUDE_DIR - ) - -set(ZeroMQ_INCLUDE_DIRS ${ZeroMQ_INCLUDE_DIR}) -set(ZeroMQ_LIBRARIES ${ZeroMQ_LIBRARY}) - -mark_as_advanced( - ZeroMQ_ROOT_DIR - ZeroMQ_LIBRARY - ZeroMQ_LIBRARY_DEBUG - ZeroMQ_LIBRARY_RELEASE - ZeroMQ_INCLUDE_DIR - ) \ No newline at end of file diff --git a/src/examples/handlers/main.cpp b/src/examples/handlers/main.cpp index 5e645fb..8f4b7f3 100644 --- a/src/examples/handlers/main.cpp +++ b/src/examples/handlers/main.cpp @@ -26,8 +26,6 @@ #include -#include - /* handlers example * * This is a demonstration of using various handler mechanisms, including REST parameters with rejex diff --git a/src/examples/list_endpoints/main.cpp b/src/examples/list_endpoints/main.cpp index 437ac58..650b404 100644 --- a/src/examples/list_endpoints/main.cpp +++ b/src/examples/list_endpoints/main.cpp @@ -26,8 +26,6 @@ #include -#include - /* list_endpoints example * * This example demonstrates how to expose a list of registered API endpoints using the diff --git a/src/examples/rest_resource/main.cpp b/src/examples/rest_resource/main.cpp index 7cbb5bc..02b011a 100644 --- a/src/examples/rest_resource/main.cpp +++ b/src/examples/rest_resource/main.cpp @@ -26,8 +26,6 @@ #include -#include - /* rest_resource example * * This example demonstrates how you might use served to create REST resources. diff --git a/src/served/CMakeLists.txt b/src/served/CMakeLists.txt index b3024b7..37112f6 100644 --- a/src/served/CMakeLists.txt +++ b/src/served/CMakeLists.txt @@ -32,7 +32,7 @@ IF (RAGEL_FOUND) ENDFOREACH (R_FILE ${served_RL}) ENDIF (RAGEL_FOUND) -# +# # Locate project sources # FILE (GLOB_RECURSE served_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp) @@ -47,7 +47,7 @@ LIST (REMOVE_ITEM served_SRCS ${test_SRCS}) # # Configure common project settings # -SET (served_LIBS ${Boost_LIBRARIES} ${RE2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) +SET (served_LIBS ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) SET (served_BIN ${PROJECT_NAME}) IF (NOT DEFINED SERVED_BUILD_SHARED) diff --git a/src/served/mux/regex_matcher.cpp b/src/served/mux/regex_matcher.cpp index b2c5d1f..851ef43 100644 --- a/src/served/mux/regex_matcher.cpp +++ b/src/served/mux/regex_matcher.cpp @@ -30,21 +30,14 @@ namespace served { namespace mux { regex_matcher::regex_matcher(const std::string & variable_name, const std::string & regex) : _variable_name(variable_name) , _regex(regex) -{ - if (!_regex.ok()) { - throw std::runtime_error(_regex.error()); - } -} +{ } // ----- matching logic ----- bool regex_matcher::check_match(const std::string & path_segment) { - if (_regex.ok()) { - return (re2::RE2::FullMatch(path_segment, _regex)); - } - return false; + return std::regex_match(path_segment, _regex); } // ----- REST param collecting ----- diff --git a/src/served/mux/regex_matcher.hpp b/src/served/mux/regex_matcher.hpp index 2872427..70ad76c 100644 --- a/src/served/mux/regex_matcher.hpp +++ b/src/served/mux/regex_matcher.hpp @@ -24,8 +24,7 @@ #define SERVED_PATH_REGEX_MATCHER_HPP #include - -#include +#include #include @@ -39,7 +38,7 @@ namespace served { namespace mux { class regex_matcher : public segment_matcher { const std::string _variable_name; - re2::RE2 _regex; + const std::regex _regex; public: /* diff --git a/src/served/net/connection.cpp b/src/served/net/connection.cpp index a9530fb..440dc68 100644 --- a/src/served/net/connection.cpp +++ b/src/served/net/connection.cpp @@ -28,6 +28,7 @@ #include #include +using namespace served; using namespace served::net; connection::connection( boost::asio::io_service & io_service diff --git a/src/served/net/connection_manager.cpp b/src/served/net/connection_manager.cpp index 7e2592c..bded016 100644 --- a/src/served/net/connection_manager.cpp +++ b/src/served/net/connection_manager.cpp @@ -54,3 +54,6 @@ connection_manager::stop_all() { } _connections.clear(); } + +connection_manager::~connection_manager() +{} diff --git a/src/served/net/connection_manager.hpp b/src/served/net/connection_manager.hpp index a9f9b32..60dae0c 100644 --- a/src/served/net/connection_manager.hpp +++ b/src/served/net/connection_manager.hpp @@ -66,6 +66,8 @@ class connection_manager * Stops all remaining open connections. */ void stop_all(); + + ~connection_manager(); }; } } // net, served diff --git a/src/served/net/server.cpp b/src/served/net/server.cpp index 347d1c7..629d9ae 100644 --- a/src/served/net/server.cpp +++ b/src/served/net/server.cpp @@ -26,6 +26,7 @@ #include +using namespace served; using namespace served::net; server::server( const std::string & address @@ -144,9 +145,9 @@ server::do_accept() , std::move(_socket) , _connection_manager , _request_handler + , _req_max_bytes , _read_timeout , _write_timeout - , _req_max_bytes )); } do_accept(); diff --git a/src/served/request_parser.hpp b/src/served/request_parser.hpp index f0cc726..fb867b3 100644 --- a/src/served/request_parser.hpp +++ b/src/served/request_parser.hpp @@ -55,6 +55,7 @@ class request_parser { size_t d_offset; public: + enum status { RUNNING = 0, FINISHED, ERROR }; private: diff --git a/src/served/request_parser_impl.cpp b/src/served/request_parser_impl.cpp index 2ee3f7b..412c65a 100644 --- a/src/served/request_parser_impl.cpp +++ b/src/served/request_parser_impl.cpp @@ -24,6 +24,7 @@ #include #include +#include namespace served { diff --git a/src/served/uri.cpp b/src/served/uri.cpp index dd3a0ff..6dd4d1a 100644 --- a/src/served/uri.cpp +++ b/src/served/uri.cpp @@ -26,7 +26,7 @@ namespace served { static const char hex_table[] = "0123456789ABCDEF"; -static const char dec_to_hex[256] = { +static const signed char dec_to_hex[256] = { /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ /* 0 */ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, /* 1 */ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, @@ -91,7 +91,7 @@ query_unescape(const std::string& s) { while (src_ptr < last_decodable) { if (*src_ptr == '%') { - char dec1, dec2; + signed char dec1, dec2; if (-1 != (dec1 = dec_to_hex[*(src_ptr + 1)]) && -1 != (dec2 = dec_to_hex[*(src_ptr + 2)])) {