Skip to content

Commit c6dd632

Browse files
committed
Initial implementation of sessions.
1 parent e963b4a commit c6dd632

File tree

7 files changed

+278
-161
lines changed

7 files changed

+278
-161
lines changed

http/src/CMakeLists.txt

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@ if (OPENSSL_FOUND)
1818
include_directories(${OPENSSL_INCLUDE_DIR})
1919
endif()
2020

21-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
22-
if (HAVE_STD11)
23-
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11")
24-
elseif (HAVE_STD0X)
25-
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++0x")
26-
endif()
27-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
28-
CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11)
29-
if (CPP-NETLIB_DISABLE_LIBCXX)
30-
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11")
31-
else()
32-
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
33-
endif()
34-
endif()
35-
3621
set(CPP-NETLIB_HTTP_MESSAGE_SRCS http/request.cpp http/response.cpp)
3722
add_library(cppnetlib-http-message ${CPP-NETLIB_HTTP_MESSAGE_SRCS})
3823
add_dependencies(cppnetlib-http-message
@@ -41,27 +26,9 @@ add_dependencies(cppnetlib-http-message
4126
target_link_libraries(cppnetlib-http-message
4227
${Boost_LIBRARIES}
4328
cppnetlib-message)
44-
foreach (src_file ${CPP-NETLIB_HTTP_MESSAGE_SRCS})
45-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
46-
set_source_files_properties(${src_file}
47-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
48-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
49-
set_source_files_properties(${src_file}
50-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
51-
endif()
52-
endforeach(src_file)
5329

5430
set(CPP-NETLIB_HTTP_MESSAGE_WRAPPERS_SRCS http/message/wrappers.cpp)
5531
add_library(cppnetlib-http-message-wrappers ${CPP-NETLIB_HTTP_MESSAGE_WRAPPERS_SRCS})
56-
foreach (src_file ${CPP-NETLIB_HTTP_MESSAGE_WRAPPERS_SRCS})
57-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
58-
set_source_files_properties(${src_file}
59-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
60-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
61-
set_source_files_properties(${src_file}
62-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
63-
endif()
64-
endforeach(src_file)
6532

6633
#set(CPP-NETLIB_HTTP_SERVER_PARSERS_SRCS server_request_parsers_impl.cpp)
6734
#add_library(cppnetlib-http-server-parsers ${CPP-NETLIB_HTTP_SERVER_PARSERS_SRCS})
@@ -129,15 +96,6 @@ set(CPP-NETLIB_HTTP_CLIENT_CONNECTIONS_SRCS
12996
http/client_async_resolver.cpp
13097
http/client_connection_normal.cpp)
13198
add_library(cppnetlib-http-client-connections ${CPP-NETLIB_HTTP_CLIENT_CONNECTIONS_SRCS})
132-
foreach (src_file ${CPP-NETLIB_HTTP_CLIENT_CONNECTIONS_SRCS})
133-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
134-
set_source_files_properties(${src_file}
135-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
136-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
137-
set_source_files_properties(${src_file}
138-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
139-
endif()
140-
endforeach(src_file)
14199

142100
set(CPP-NETLIB_HTTP_CLIENT_SRCS
143101
http/client.cpp)
@@ -165,24 +123,10 @@ target_link_libraries(cppnetlib-http-client
165123
cppnetlib-http-message-wrappers
166124
cppnetlib-http-client-connections
167125
)
168-
foreach (src_file ${CPP-NETLIB_HTTP_CLIENT_SRCS})
169-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
170-
set_source_files_properties(${src_file}
171-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
172-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
173-
set_source_files_properties(${src_file}
174-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
175-
endif()
176-
endforeach(src_file)
177126

178127
set(CPP-NETLIB_CONSTANTS_SRCS constants.cpp)
179128
add_library(cppnetlib-constants ${CPP-NETLIB_CONSTANTS_SRCS})
180-
foreach (src_file ${CPP-NETLIB_CONSTANTS_SRCS})
181-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
182-
set_source_files_properties(${src_file}
183-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
184-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
185-
set_source_files_properties(${src_file}
186-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
187-
endif()
188-
endforeach(src_file)
129+
130+
# Server implementation files.
131+
set(CPP-NETLIB_HTTP_SERVER_SRCS http/server/session.cpp http/server/simple_sessions.cpp)
132+
add_library(cppnetlib-http-server ${CPP-NETLIB_HTTP_SERVER_SRCS})

http/src/http/server.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2013 (c) Google, Inc.
2+
// Copyright 2013 (c) Dean Michael Berris <dberris@google.com>
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#ifndef NETWORK_HTTP_SERVER_HPP_20130327
8+
#define NETWORK_HTTP_SERVER_HPP_20130327
9+
10+
#include <memory>
11+
#include <utility>
12+
#include <functional>
13+
#include <boost/utility/string_ref.hpp>
14+
15+
namespace network {
16+
namespace http {
17+
18+
struct dynamic_dispatcher;
19+
struct simple_sessions;
20+
struct no_auth;
21+
struct default_connection_manager;
22+
23+
template <class Handler = dynamic_dispatcher,
24+
class SessionManager = simple_sessions, class Authenticator = no_auth,
25+
class ConnectionManager = default_connection_manager>
26+
struct basic_server {
27+
void register_handler(
28+
boost::string_ref spec,
29+
std::function<void(
30+
typename SessionManager::session &,
31+
std::shared_ptr<typename ConnectionManager::connection>)> handler);
32+
};
33+
34+
typedef basic_server<> server;
35+
36+
} // namespace http
37+
} // namespace network
38+
39+
#endif /* end of include guard: NETWORK_HTTP_SERVER_HPP_20130327 */
40+

http/src/http/server/session.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2013 (c) Google, Inc.
2+
// Copyright 2013 (c) Dean Michael Berris <dberris@google.com>
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <http/server/session.hpp>
8+
9+
namespace network {
10+
namespace http {
11+
12+
std::string session::get(boost::string_ref key, boost::string_ref value) const {
13+
session_map_type::const_iterator entry = session_map_.find(
14+
static_cast<std::string>(key));
15+
return entry == session_map_.end() ? static_cast<std::string>(value) :
16+
entry->second.second;
17+
}
18+
19+
void session::set(boost::string_ref key, boost::string_ref value,
20+
bool server_only) {
21+
auto new_value = make_pair(server_only, static_cast<std::string>(value));
22+
std::pair<session_map_type::iterator, bool> result =
23+
session_map_.insert(make_pair(static_cast<std::string>(key), new_value));
24+
if (!result.second)
25+
result.first->second = std::move(new_value);
26+
}
27+
28+
std::string session::operator[](boost::string_ref key) const {
29+
return get(key, "");
30+
}
31+
32+
} // namespace http
33+
} // namespace network

http/src/http/server/session.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2013 (c) Google, Inc.
2+
// Copyright 2013 (c) Dean Michael Berris <dberris@google.com>
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#ifndef NETWORK_HTTP_SERVER_SESSION_HPP_20130405
8+
#define NETWORK_HTTP_SERVER_SESSION_HPP_20130405
9+
10+
#include <string>
11+
#include <unordered_map>
12+
#include <boost/utility/string_ref.hpp>
13+
14+
namespace network {
15+
namespace http {
16+
17+
struct session {
18+
session() = default;
19+
session(const session&) = default;
20+
session(session&&) = default;
21+
session& operator=(const session&) = default;
22+
session& operator=(session&&) = default;
23+
24+
void set(boost::string_ref key, boost::string_ref value, bool server_only = false);
25+
std::string get(boost::string_ref key, boost::string_ref default_value) const;
26+
std::string operator[](boost::string_ref key) const;
27+
private:
28+
typedef std::unordered_map<std::string, std::pair<bool, std::string>> session_map_type;
29+
session_map_type session_map_;
30+
};
31+
32+
} // namespace http
33+
} // namespace network
34+
35+
#endif /* end of include guard: NETWORK_HTTP_SERVER_SESSION_HPP_20130405 */
36+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 (c) Google, Inc.
2+
// Copyright 2013 (c) Dean Michael Berris <dberris@google.com>
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <http/server/simple_sessions.hpp>
8+
9+
namespace network {
10+
namespace http {
11+
12+
session& simple_sessions::lookup(boost::string_ref session_id) {
13+
std::string real_session_id = session_id.empty() ?
14+
std::to_string(next_session_id_.fetch_add(1, std::memory_order::memory_order_seq_cst))
15+
: static_cast<std::string>(session_id);
16+
std::pair<session_map_type::iterator, bool> result =
17+
sessions_.insert(make_pair(std::move(real_session_id), session()));
18+
result.first->second.set("id", real_session_id);
19+
return result.first->second;
20+
}
21+
22+
} // namespace http
23+
} // namespace network
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2013 (c) Google, Inc.
2+
// Copyright 2013 (c) Dean Michael Berris <dberris@google.com>
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#ifndef NETWORK_HTTP_SERVER_SIMPLE_SESSIONS_HPP_20130327
8+
#define NETWORK_HTTP_SERVER_SIMPLE_SESSIONS_HPP_20130327
9+
10+
#include <unordered_map>
11+
#include <http/server/session.hpp>
12+
#include <boost/utility/string_ref.hpp>
13+
#include <atomic>
14+
15+
namespace network {
16+
namespace http {
17+
18+
struct simple_sessions {
19+
simple_sessions() = default;
20+
simple_sessions(const simple_sessions&) = delete;
21+
simple_sessions(simple_sessions&&) = delete;
22+
simple_sessions& operator=(const simple_sessions&) = delete;
23+
simple_sessions& operator=(simple_sessions&) = delete;
24+
25+
session &lookup(boost::string_ref session_id);
26+
private:
27+
typedef std::unordered_map<std::string, session> session_map_type;
28+
session_map_type sessions_;
29+
std::atomic_uint_fast64_t next_session_id_;
30+
};
31+
32+
} // namespace http
33+
} // namespace network
34+
35+
#endif /* end of include guard: NETWORK_HTTP_SERVER_SIMPLE_SESSIONS_HPP_20130327 */
36+

0 commit comments

Comments
 (0)