Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Add "overwrite" boolean to allow the possibility to edit a current handle #46

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c0bff45
Add "overwrite" option to the handle() method in mux
adevress Jan 10, 2019
ef2167c
Merge branch 'master' of github.com:meltwater/served
adevress Jan 10, 2019
543bfb7
Merge branch 'master' of github.com:meltwater/served
adevress Feb 12, 2019
f2fec08
Upgrade to last version 1.5.1
adevress Feb 22, 2019
76e50f9
Enlarge inlined buffer for connection to allow large header request
adevress Mar 7, 2019
1acab59
Add 1.5.2 dev version
adevress Mar 7, 2019
11ce2d3
Fix potential memory corruption bug
adevress Mar 14, 2019
b050147
Fix a potential memory corruption on answer
adevress Mar 14, 2019
53c959e
Add protection to avoid server crash in case of error on socket close ()
adevress Dec 6, 2019
7e6ddb5
Update version to new dev version
adevress Dec 6, 2019
ce92cc3
Fix potential segmentation fault at destruction
Sep 2, 2020
9628935
Merge pull request #1 from FreeYourSoul/master
adevress Sep 2, 2020
c027fb1
Fix lambda temporary shared_ptr not used (avoid side effects)
Jan 4, 2021
9ab1b10
Merge pull request #2 from FreeYourSoul/master
adevress Jan 4, 2021
4ef6efc
README: require Boost 1.53 or newer instead of 1.56
AdrianDC Mar 9, 2019
519b570
cmake: RPM: resolve boost dependency naming for Redhat
AdrianDC Mar 9, 2019
1f74539
served: support building both static and shared libraries
AdrianDC Mar 9, 2019
a2491d5
Added missing virtual destructor
jpihl Jun 28, 2019
cfa88ae
`git_repository` is no longer a native rule
Jul 22, 2019
5f12c39
Update bazel build rules for boost
Jul 22, 2019
1eec092
Fix two name clash with Windows includes
plule-ansys Jul 29, 2019
b4a8133
+ caching
Aug 5, 2019
f9cd24a
examples: create json_data example for JSON responses
AdrianDC Aug 24, 2019
06cd4f3
Update version of served to 1.6.0 DEVINTERNAL to reflect hotfixes
adevress Jan 4, 2021
e945cef
Fix lambda temporary shared_ptr not used (avoid side effects)
Jan 4, 2021
0b2c783
Merge pull request #3 from FreeYourSoul/master
adevress Jan 5, 2021
9e34b9a
Replace auto by explicit typing, make the mess a bit more readable
adevress Jan 6, 2021
3f5e07e
Change the scope of a self lifetime object to make lifetime requirement
adevress Jan 6, 2021
06a9786
Remove every this argument in asynchronous callback to make calls to the
adevress Jan 6, 2021
73a79bf
Add mutex to protect against concurrent modifications on the
adevress Jan 6, 2021
521d89c
Upgrade served version to 1.7.0
adevress Jan 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ cc_library(
"src/served/net/connection_manager.hpp",
"src/served/net/server.hpp",
],
defines = select({
"@bazel_tools//src/conditions:windows": ["NOGDI"],
"//conditions:default": [],
}),
strip_include_prefix = "src/",
visibility = ["//visibility:public"],
deps = [
Expand Down
17 changes: 11 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ SET (APPLICATION_NAME "Served HTTP REST Library")
SET (APPLICATION_CODENAME "${PROJECT_NAME}")
SET (APPLICATION_COPYRIGHT_YEARS "2014")
SET (APPLICATION_VERSION_MAJOR 1)
SET (APPLICATION_VERSION_MINOR 4)
SET (APPLICATION_VERSION_PATCH 3)
SET (APPLICATION_VERSION_TYPE DS1)
SET (APPLICATION_VERSION_MINOR 7)
SET (APPLICATION_VERSION_PATCH 0)
SET (APPLICATION_VERSION_TYPE DEVINTERNAL)
SET (APPLICATION_VERSION_STRING "${APPLICATION_VERSION_MAJOR}.${APPLICATION_VERSION_MINOR}.${APPLICATION_VERSION_PATCH}-${APPLICATION_VERSION_TYPE}")
SET (APPLICATION_VENDOR_ID "com.meltwater")
SET (APPLICATION_VENDOR_NAME "Meltwater")
Expand Down Expand Up @@ -77,13 +77,18 @@ FIND_PACKAGE (RAGEL)

FIND_PACKAGE (Threads)

INCLUDE (EnableStdCXX11)
ENABLE_STDCXX11 ()
# Enforce C++17
# This has the advantage to remove an undefined behavior from shared_from_this
# https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/shared_from_this
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


#
# Enable warnings
#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -Wno-missing-field-initializers")
set(CMAKE_CXX_FLAGS "-Wall -pedantic -Wextra -Wno-missing-field-initializers")

#
# Configure Files
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Features:

### Requirements

* [Required] - [Boost 1.56](http://www.boost.org/)
* [Required] - [Boost (1.53 or newer)](http://www.boost.org/)
* [Optional] - [Ragel](http://www.complang.org/ragel/)

### Building
Expand Down
3 changes: 2 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
workspace(name = "com_github_meltwater_served")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "com_github_nelhage_rules_boost",
commit = "72ec09168e5c3a296f667b3d956a853ccd65c8ed",
commit = "552baa34c17984ca873cd0c4fdd010c177737b6f",
remote = "https://github.com/nelhage/rules_boost",
)
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
Expand Down
2 changes: 1 addition & 1 deletion cmake/CPackConfigRPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ IF(RPMBUILD_CMD)
SET(CPACK_RPM_PACKAGE_RELEASE "${APPLICATION_VERSION_TYPE}")
SET(CPACK_RPM_PACKAGE_LICENSE "MIT")
SET(CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
SET(CPACK_RPM_PACKAGE_REQUIRES "libboost >= 1.53")
SET(CPACK_RPM_PACKAGE_REQUIRES "boost >= 1.53")
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "${CMAKE_SYSTEM_PROCESSOR}")
SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}.${CPACK_RPM_PACKAGE_ARCHITECTURE}")
SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_SOURCE_DIR}/install/post_install_script.sh")
Expand Down
1 change: 1 addition & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ADD_SUBDIRECTORY (form_data)
ADD_SUBDIRECTORY (handlers)
ADD_SUBDIRECTORY (hello_world)
ADD_SUBDIRECTORY (hello_world_no_blocking)
ADD_SUBDIRECTORY (json_data)
ADD_SUBDIRECTORY (query_params)
ADD_SUBDIRECTORY (list_endpoints)
ADD_SUBDIRECTORY (request_logger_plugin)
Expand Down
36 changes: 36 additions & 0 deletions src/examples/json_data/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.

#
# Locate project sources
#
FILE (GLOB_RECURSE json_data_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)

#
# Configure common project settings
#
SET (json_data_LIBS ${PROJECT_NAME} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

#
# Executable build rules
#
ADD_EXECUTABLE (json_data ${json_data_SRCS})
TARGET_LINK_LIBRARIES (json_data ${json_data_LIBS})
INSTALL (TARGETS json_data DESTINATION bin)
81 changes: 81 additions & 0 deletions src/examples/json_data/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/

#include <served/served.hpp>

#include <boost/property_tree/json_parser.hpp>

/* json_data example
*
* This is a JSON data example of served in action.
*/
int main(int, char const**)
{
served::multiplexer mux;

mux.handle("/flat")
.get([](served::response & res, const served::request &) {
boost::property_tree::ptree root;
std::string response;
std::stringstream ss;

// Populate JSON response
root.put("bool", true);
root.put("integer", 123);
root.put("string", "String");

// Export JSON response
boost::property_tree::json_parser::write_json(ss, root, true /* human */);
res << ss.str();
});

mux.handle("/nested")
.get([](served::response & res, const served::request &) {
boost::property_tree::ptree root;
boost::property_tree::ptree arrayTree;
boost::property_tree::ptree flatTree;
std::stringstream ss;

// Populate JSON response
flatTree.put("bool", true);
flatTree.put("integer", 123);
flatTree.put("string", "String");
arrayTree.push_back(std::make_pair("", flatTree));
arrayTree.push_back(std::make_pair("", flatTree));
arrayTree.push_back(std::make_pair("", flatTree));
root.add_child("node_array", arrayTree);
root.add_child("node_1", flatTree);

// Export JSON response
boost::property_tree::json_parser::write_json(ss, root, true /* human */);
res << ss.str();
});

std::cout << "Try this example with:" << std::endl;
std::cout << " curl http://localhost:8123/flat" << std::endl;
std::cout << " curl http://localhost:8123/nested" << std::endl;

served::net::server server("127.0.0.1", "8123", mux);
server.run(10); // Run with a pool of 10 threads.

return 0;
}
14 changes: 10 additions & 4 deletions src/served/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ IF (NOT DEFINED SERVED_BUILD_STATIC)
SET(SERVED_BUILD_STATIC 1)
ENDIF (NOT DEFINED SERVED_BUILD_STATIC)

IF (SERVED_BUILD_SHARED)
SET(SERVED_TARGET_STATIC_SUFFIX "-static")
ELSE (SERVED_BUILD_SHARED)
SET(SERVED_TARGET_STATIC_SUFFIX "")
ENDIF (SERVED_BUILD_SHARED)

#
# Static lib build rules
#
IF (SERVED_BUILD_STATIC)
ADD_LIBRARY (${PROJECT_NAME} STATIC ${served_SRCS} ${served_RLSOURCES})
TARGET_LINK_LIBRARIES (${PROJECT_NAME} ${served_LIBS})
SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES VERSION "${APPLICATION_VERSION_MAJOR}.${APPLICATION_VERSION_MINOR}" OUTPUT_NAME ${served_BIN} CLEAN_DIRECT_OUTPUT 1)
INSTALL (TARGETS ${PROJECT_NAME} DESTINATION lib)
ADD_LIBRARY (${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} STATIC ${served_SRCS} ${served_RLSOURCES})
TARGET_LINK_LIBRARIES (${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} ${served_LIBS})
SET_TARGET_PROPERTIES (${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} PROPERTIES VERSION "${APPLICATION_VERSION_MAJOR}.${APPLICATION_VERSION_MINOR}" OUTPUT_NAME ${served_BIN} CLEAN_DIRECT_OUTPUT 1)
INSTALL (TARGETS ${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} DESTINATION lib)
ENDIF (SERVED_BUILD_STATIC)

#
Expand Down
10 changes: 10 additions & 0 deletions src/served/methods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include <string>
#include <stdexcept>

#ifdef _MSC_VER
// DELETE is also defined as a macro in winnt.h, included through boost::asio
#pragma push_macro("DELETE")
#undef DELETE
#endif // _MSC_VER

namespace served {

/*
Expand Down Expand Up @@ -116,4 +122,8 @@ method_from_string(const std::string & str)

} // served

#ifdef _MSC_VER
#pragma pop_macro("DELETE")
#endif // _MSC_VER

#endif // SERVED_METHODS_HPP
12 changes: 10 additions & 2 deletions src/served/multiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,22 @@ multiplexer::get_segments(const std::string & path)
// ----- http request handlers -----

served::methods_handler &
multiplexer::handle(const std::string & path, const std::string info /* = "" */)
multiplexer::handle(const std::string & path, const std::string info /* = "" */, bool overwrite)
{

// Remove any duplicates.
for ( auto it = _handler_candidates.begin(); it != _handler_candidates.end(); )
{
if ( std::get<2>(*it) == path )
{
it = _handler_candidates.erase(it);
if(overwrite)
{
it = _handler_candidates.erase(it);
}
else
{
return std::get<1>(*it);
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/served/multiplexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class multiplexer
*
* @return returns a methods_handler used to specify handlers per HTTP method
*/
served::methods_handler & handle(const std::string & path, const std::string info = "");
served::methods_handler & handle(const std::string & path, const std::string info = "", bool overwrite = true);

// ----- request forwarding -----

Expand Down
6 changes: 6 additions & 0 deletions src/served/mux/segment_matcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class segment_matcher
* @param path_segment the segment of path the variable should be extracted from
*/
virtual void get_param(served::parameters & params, const std::string & path_segment) = 0;

/*
* Class destructor.
*/
virtual ~segment_matcher()
{ }
};

typedef std::shared_ptr<segment_matcher> segment_matcher_ptr;
Expand Down
Loading