Skip to content

Commit e772881

Browse files
committed
Adding trivial implementation of the dispatcher.
1 parent 531f08f commit e772881

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

http/src/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,7 @@ set(CPP-NETLIB_CONSTANTS_SRCS constants.cpp)
128128
add_library(cppnetlib-constants ${CPP-NETLIB_CONSTANTS_SRCS})
129129

130130
# Server implementation files.
131-
set(CPP-NETLIB_HTTP_SERVER_SRCS http/server/session.cpp http/server/simple_sessions.cpp)
131+
set(CPP-NETLIB_HTTP_SERVER_SRCS
132+
http/server/session.cpp http/server/simple_sessions.cpp
133+
http/server/dynamic_dispatcher.cpp)
132134
add_library(cppnetlib-http-server ${CPP-NETLIB_HTTP_SERVER_SRCS})

http/src/network/http/server/dynamic_dispatcher.hpp renamed to http/src/http/server/dynamic_dispatcher.hpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@
77
#ifndef NETWORK_HTTP_SERVER_DYNAMIC_DISPATCHER_HPP_20130327
88
#define NETWORK_HTTP_SERVER_DYNAMIC_DISPATCHER_HPP_20130327
99

10+
#include <http/server/session.hpp>
11+
#include <http/server/connection.hpp>
12+
#include <string>
13+
#include <functional>
14+
#include <memory>
15+
#include <unordered_map>
16+
1017
namespace network {
1118
namespace http {
1219

13-
struct dynamic_dispatcher{};
20+
struct dynamic_dispatcher{
21+
void register_handler(boost::string_ref pattern, std::function<void(session&, std::shared_ptr<connection>)> handler);
22+
void dispatch(boost::string_ref path, session& s, std::shared_ptr<connection> c);
23+
private:
24+
std::unordered_map<
25+
std::string,
26+
std::function<void(session &, std::shared_ptr<connection>)> > handlers_;
27+
};
1428

1529
} // namespace http
1630
} // namespace network

http/test/server_dynamic_dispatcher_test.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@
55
// http://www.boost.org/LICENSE_1_0.txt)
66

77
#include <gtest/gtest.h>
8-
#include <network/http/server/dynamic_dispatcher.hpp>
8+
#include <http/server/dynamic_dispatcher.hpp>
9+
#include <http/server/session.hpp>
10+
#include <http/server/connection.hpp>
911

1012
namespace http = ::network::http;
1113

1214
TEST(server_dynamic_dispatcher, constructor) {
1315
http::dynamic_dispatcher dispatcher;
1416
(void)dispatcher;
1517
}
18+
19+
TEST(server_dynamic_dispatcher, register_handler) {
20+
http::dynamic_dispatcher dispatcher;
21+
bool called = false;
22+
dispatcher.register_handler("/", [&called](http::session& s, std::shared_ptr<http::connection> c){ called = true; });
23+
http::session s;
24+
std::shared_ptr<http::connection> c;
25+
dispatcher.dispatch("/", s, c);
26+
EXPECT_EQ(true, called);
27+
}

0 commit comments

Comments
 (0)