Skip to content

Commit 8651d16

Browse files
committed
Incremental changes to slowly support persistent connections in HTTP/1.1 Async mode.
1 parent 7293690 commit 8651d16

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

boost/network/protocol/http/policies/async_connection.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <boost/network/protocol/http/traits/resolver_policy.hpp>
1717
#include <boost/network/protocol/http/client/connection/async_base.hpp>
1818
#include <boost/network/protocol/http/client/connection_manager.hpp>
19+
#include <boost/enable_shared_from_this.hpp>
1920
#include <boost/tuple/tuple.hpp>
2021
#include <boost/function.hpp>
2122
#include <boost/bind.hpp>
@@ -39,9 +40,9 @@ struct simple_async_connection_manager : connection_manager {
3940
shared_ptr<resolver_delegate> shared_resolver_delegate;
4041
};
4142

42-
struct http_1_1_async_connection_manager_pimpl;
43+
struct http_1_1_async_connection;
4344

44-
struct http_1_1_async_connection_manager : connection_manager {
45+
struct http_1_1_async_connection_manager : connection_manager, enable_shared_from_this<http_1_1_async_connection_manager> {
4546
http_1_1_async_connection_manager(bool cache_resolved,
4647
bool follow_redirects,
4748
optional<std::string> openssl_certificate,
@@ -51,6 +52,15 @@ struct http_1_1_async_connection_manager : connection_manager {
5152
request_base const & request); // override
5253
virtual void reset(); // override
5354
virtual ~http_1_1_async_connection_manager(); // override
55+
56+
protected:
57+
friend struct http_1_1_async_connection;
58+
void add_ready_connection(shared_ptr<client_connection> connection_ptr);
59+
shared_ptr<client_connection> get_ready_connection(std::string const & host);
60+
bool cache_resolved, follow_redirects_;
61+
mutex shared_resolver_mutex;
62+
shared_ptr<resolver_delegate> shared_resolver_delegate;
63+
unordered_map<std::string, shared_ptr<client_connection> > ready_connections;
5464
};
5565

5666
template <class Tag, unsigned version_major, unsigned version_minor>

boost/network/protocol/http/policies/async_connection.ipp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
namespace boost { namespace network { namespace http {
1111

1212
struct simple_async_client_connection : client_connection {
13-
simple_async_client_connection(asio::io_service & service,
14-
bool follow_redirects,
13+
simple_async_client_connection(bool follow_redirects,
1514
shared_ptr<resolver_delegate> resolver_delegate,
1615
shared_ptr<connection_delegate> connection_delegate);
1716
virtual shared_ptr<response_base> send_request(std::string const & method,
@@ -21,20 +20,17 @@ struct simple_async_client_connection : client_connection {
2120
virtual void reset(); // override
2221
virtual ~client_connection(); // override
2322
protected:
24-
asio::io_service & service_;
2523
bool follow_redirects_;
2624
shared_ptr<resolver_delegate> resolver_delegate_;
2725
shared_ptr<connection_delegate> connection_delegate_;
2826
shared_ptr<connection> connection_;
2927
};
3028

3129
simple_async_client_connection::simple_async_client_connection(
32-
asio::io_service & service,
3330
bool follow_redirects,
3431
shared_ptr<resolver_delegate> resolver_delegate,
3532
shared_ptr<connection_delegate> connection_delegate)
36-
: service_(service),
37-
follow_redirects_(follow_redirects),
33+
: follow_redirects_(follow_redirects),
3834
resolver_delegate_(resolver_delegate),
3935
connection_delegate_(connection_delegate),
4036
{}
@@ -45,9 +41,12 @@ shared_ptr<response_base> send_request(std::string const & method,
4541
callback_type callback) {
4642
shared_ptr<response_base> response;
4743
shared_ptr<connection> connection_;
48-
connection_.reset(new impl::async_connection(resolver_delegate_,
49-
follow_redirects_,
50-
connection_delegate_))
44+
connection_.reset(new(std::nothrow) impl::async_connection(
45+
resolver_delegate_,
46+
follow_redirects_,
47+
connection_delegate_))
48+
if (!connection_.get())
49+
BOOST_THROW_EXCEPTION(std::runtime_error("Insufficient memory."));
5150
response = connection_->start(request, method, get_body, callback);
5251
return response
5352
}
@@ -86,16 +85,15 @@ boost::network::http::simple_async_connection_manager::get_connection(
8685
} else {
8786
resolver_delegate.reset(new(std::nothrow) async_resolver(service));
8887
if (!resolver_delegate_.get())
89-
BOOST_THROW_EXCEPTION(std::runtime_error("Insuffucuent memory."));
88+
BOOST_THROW_EXCEPTION(std::runtime_error("Insuffucient memory."));
9089
}
9190
shared_ptr<connection_delegate> connection_delegate;
9291
bool https = (scheme(request) == "https");
9392
connection_delegate =
9493
connection_delegate_factory::new_connection_delegate(
9594
service, openssl_certificate_, openssl_verify_path_);
9695
connection.reset(
97-
new(std::nothrow) simple_async_client_connection(service,
98-
follow_redirects_,
96+
new(std::nothrow) simple_async_client_connection(follow_redirects_,
9997
resolver_delegate,
10098
connection_delegate));
10199
if (!connection.get())
@@ -110,4 +108,16 @@ void boost::network::http::simple_async_connection_manager::reset() {
110108
}
111109
}
112110

111+
namespace boost { namespace network { namespace http {
112+
113+
struct http_1_1_async_connection : client_connection {
114+
http_1_1_async_connection(bool follow_redirects,
115+
shared_ptr<resolver_delegate> resolver_delegate,
116+
shared_ptr<connection_delegate> connection_delegate)
117+
}
118+
119+
} /* http */
120+
} /* network */
121+
} /* boost */
122+
113123
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_POLICIES_ASYNC_CONNECTION_IPP_20110930 */

0 commit comments

Comments
 (0)