Skip to content

Commit 5b82479

Browse files
committed
Added connection_delegate and test files (no test cases yet).
1 parent 06b8b94 commit 5b82479

File tree

8 files changed

+235
-3
lines changed

8 files changed

+235
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef __NETWORK_HTTP_V2_CLIENT_CONNECTION_CONNECTION_DELEGATE_INC__
7+
#define __NETWORK_HTTP_V2_CLIENT_CONNECTION_CONNECTION_DELEGATE_INC__
8+
9+
#include <functional>
10+
#include <string>
11+
#include <boost/asio/ip/tcp.hpp>
12+
#include <boost/asio/streambuf.hpp>
13+
14+
namespace network {
15+
namespace http {
16+
namespace v2 {
17+
class connection_delegate {
18+
19+
connection_delegate(const connection_delegate &) = delete;
20+
connection_delegate &operator = (const connection_delegate &) = delete;
21+
22+
public:
23+
24+
connection_delegate() = default;
25+
26+
virtual ~connection_delegate() noexcept { }
27+
28+
virtual void connect(boost::asio::ip::tcp::endpoint &endpoint,
29+
const std::string &host,
30+
std::function<void (const boost::system::error_code &)> handler) = 0;
31+
32+
virtual void write(boost::asio::streambuf &command_streambuf,
33+
std::function<void (const boost::system::error_code &, size_t)> handler) = 0;
34+
35+
virtual void read_some(const boost::asio::mutable_buffers_1 &read_buffer,
36+
std::function<void (const boost::system::error_code &, size_t)> handler) = 0;
37+
38+
};
39+
} // namespace v2
40+
} // namespace http
41+
} // namespace network
42+
43+
#endif // __NETWORK_HTTP_V2_CLIENT_CONNECTION_CONNECTION_DELEGATE_INC__
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef __NETWORK_HTTP_V2_CLIENT_CONNECTION_NORMAL_CONNECTION_DELEGATE_INC__
7+
#define __NETWORK_HTTP_V2_CLIENT_CONNECTION_NORMAL_CONNECTION_DELEGATE_INC__
8+
9+
#include "network/http/v2/client/connection/connection_delegate.hpp"
10+
#include <boost/asio/ip/tcp.hpp>
11+
#include <boost/asio/io_service.hpp>
12+
13+
namespace network {
14+
namespace http {
15+
namespace v2 {
16+
class normal_connection_delegate : public connection_delegate {
17+
18+
normal_connection_delegate(const normal_connection_delegate &) = delete;
19+
normal_connection_delegate &operator = (const normal_connection_delegate &) = delete;
20+
21+
public:
22+
23+
explicit normal_connection_delegate(boost::asio::io_service &io_service)
24+
: io_service_(io_service) {
25+
26+
}
27+
28+
virtual ~normal_connection_delegate() noexcept {
29+
30+
}
31+
32+
virtual void connect(boost::asio::ip::tcp::endpoint &endpoint,
33+
const std::string &host,
34+
std::function<void (const boost::system::error_code &)> handler) {
35+
socket_.reset(new boost::asio::ip::tcp::socket(io_service_));
36+
socket_->async_connect(endpoint, handler);
37+
}
38+
39+
virtual void write(boost::asio::streambuf &command_streambuf,
40+
std::function<void (const boost::system::error_code &, size_t)> handler) {
41+
boost::asio::async_write(*socket_, command_streambuf, handler);
42+
}
43+
44+
virtual void read_some(const boost::asio::mutable_buffers_1 &read_buffer,
45+
std::function<void (const boost::system::error_code &, size_t)> handler) {
46+
socket_->async_read_some(read_buffer, handler);
47+
}
48+
49+
private:
50+
51+
boost::asio::io_service &io_service_;
52+
std::unique_ptr<boost::asio::ip::tcp::socket> socket_;
53+
54+
};
55+
} // namespace v2
56+
} // namespace http
57+
} // namespace network
58+
59+
#endif // __NETWORK_HTTP_V2_CLIENT_CONNECTION_NORMAL_CONNECTION_DELEGATE_INC__

http/src/network/http/v2/client/connection/resolver_delegate.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ namespace network {
3535

3636
virtual void clear_resolved_cache() = 0;
3737

38-
private:
39-
4038
};
4139
} // namespace v2
4240
} // namespace http
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef __NETWORK_HTTP_V2_CLIENT_CONNECTION_SSL_CONNECTION_DELEGATE_INC__
7+
#define __NETWORK_HTTP_V2_CLIENT_CONNECTION_SSL_CONNECTION_DELEGATE_INC__
8+
9+
#include "network/http/v2/client/connection/connection_delegate.hpp"
10+
#include "network/http/v2/client/client_options.hpp"
11+
#include <memory>
12+
#include <vector>
13+
#include <boost/asio/ip/tcp.hpp>
14+
#include <boost/asio/io_service.hpp>
15+
#include <boost/asio/ssl.hpp>
16+
#include <boost/asio/placeholders.hpp>
17+
18+
namespace network {
19+
namespace http {
20+
namespace v2 {
21+
class ssl_connection_delegate
22+
: public connection_delegate, std::enable_shared_from_this<ssl_connection_delegate> {
23+
24+
ssl_connection_delegate(const ssl_connection_delegate &) = delete;
25+
ssl_connection_delegate &operator = (const ssl_connection_delegate &) = delete;
26+
27+
public:
28+
29+
ssl_connection_delegate(boost::asio::io_service &io_service, const client_options &options)
30+
: io_service_(io_service)
31+
, options_(options) {
32+
33+
}
34+
35+
virtual ~ssl_connection_delegate() noexcept {
36+
37+
}
38+
39+
virtual void connect(boost::asio::ip::tcp::endpoint &endpoint,
40+
const std::string &host,
41+
std::function<void (const boost::system::error_code &)> handler) {
42+
context_.reset(
43+
new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
44+
std::vector<std::string> const& certificate_paths =
45+
options_.openssl_certificate_paths();
46+
std::vector<std::string> const& verifier_paths =
47+
options_.openssl_verify_paths();
48+
bool use_default_verification = certificate_paths.empty() &&
49+
verifier_paths.empty();
50+
if (!use_default_verification) {
51+
for (auto path : certificate_paths) {
52+
context_->load_verify_file(path);
53+
}
54+
for (auto path : verifier_paths) {
55+
context_->add_verify_path(path);
56+
}
57+
context_->set_verify_mode(boost::asio::ssl::context::verify_peer);
58+
context_->set_verify_callback(boost::asio::ssl::rfc2818_verification(host));
59+
} else {
60+
context_->set_default_verify_paths();
61+
context_->set_verify_mode(boost::asio::ssl::context::verify_none);
62+
}
63+
socket_.reset(new boost::asio::ssl::stream<
64+
boost::asio::ip::tcp::socket>(io_service_, *context_));
65+
66+
using namespace std::placeholders;
67+
socket_->lowest_layer()
68+
.async_connect(endpoint,
69+
std::bind(&ssl_connection_delegate::handle_connected,
70+
ssl_connection_delegate::shared_from_this(),
71+
_1,
72+
handler));
73+
}
74+
75+
virtual void write(boost::asio::streambuf &command_streambuf,
76+
std::function<void (const boost::system::error_code &, size_t)> handler) {
77+
boost::asio::async_write(*socket_, command_streambuf, handler);
78+
}
79+
80+
virtual void read_some(const boost::asio::mutable_buffers_1 &read_buffer,
81+
std::function<void (const boost::system::error_code &, size_t)> handler) {
82+
socket_->async_read_some(read_buffer, handler);
83+
}
84+
85+
private:
86+
87+
boost::asio::io_service &io_service_;
88+
client_options options_;
89+
std::unique_ptr<boost::asio::ssl::context> context_;
90+
std::unique_ptr<
91+
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> socket_;
92+
93+
void handle_connected(boost::system::error_code const& ec,
94+
std::function<void(const boost::system::error_code &)> handler);
95+
96+
};
97+
} // namespace v2
98+
} // namespace http
99+
} // namespace network
100+
101+
#endif // __NETWORK_HTTP_V2_CLIENT_CONNECTION_SSL_CONNECTION_DELEGATE_INC__

http/test/v2/client/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ set(CPP-NETLIB_CLIENT_TESTS
1010
response_test
1111
request_test
1212
async_resolver_test
13+
normal_connection_test
1314
client_test
1415
)
1516

17+
if (OPENSSL_FOUND)
18+
list(APPEND CPP-NETLIB_CLIENT_TESTS ssl_connection_test)
19+
endif()
20+
1621
foreach(test ${CPP-NETLIB_CLIENT_TESTS})
1722
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
1823
set_source_files_properties(${test}.cpp
@@ -35,3 +40,7 @@ foreach(test ${CPP-NETLIB_CLIENT_TESTS})
3540
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-v2-${test})
3641

3742
endforeach(test)
43+
44+
if (OPENSSL_FOUND)
45+
target_link_libraries(cpp-netlib-http-v2-ssl_connection_test ${OPENSSL_LIBRARIES})
46+
endif()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <gtest/gtest.h>
7+
#include <boost/asio.hpp>
8+
#include "network/http/v2/client/connection/normal_connection_delegate.hpp"
9+
#include <iostream>
10+
11+
namespace http = network::http::v2;

http/test/v2/client/request_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ TEST(request_test, stream_2) {
5959
ASSERT_EQ("GET /path/to/resource/index.html HTTP/1.1\r\n"
6060
"Host: http://www.example.com\r\n"
6161
"Connection: close\r\n"
62-
"User-Agent: request_test\r\n", oss.str());
62+
"User-Agent: request_test\r\n\r\n", oss.str());
6363
}
6464

6565
TEST(request_test, read_headers) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <gtest/gtest.h>
7+
#include <boost/asio.hpp>
8+
#include "network/http/v2/client/connection/ssl_connection_delegate.hpp"
9+
#include <iostream>
10+
11+
namespace http = network::http::v2;

0 commit comments

Comments
 (0)