Skip to content

Commit a35361a

Browse files
committed
Work in progress towards asynchronous HTTP using futures.
1 parent 49c4145 commit a35361a

File tree

11 files changed

+131
-87
lines changed

11 files changed

+131
-87
lines changed

boost/network/protocol/http/impl/sync_connection_base.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/asio/write.hpp>
1616
#include <boost/asio/read_until.hpp>
1717
#include <boost/tuple/tuple.hpp>
18+
#include <boost/network/protocol/http/response.hpp>
1819

1920
#include <boost/network/protocol/http/impl/http_sync_connection.hpp>
2021
#ifdef BOOST_NETWORK_ENABLE_HTTPS
@@ -69,9 +70,9 @@ namespace boost { namespace network { namespace http { namespace impl {
6970
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
7071
throw std::runtime_error("Invalid response");
7172

72-
response_.version() = http_version;
73-
response_.status() = status_code;
74-
response_.status_message() = status_message;
73+
response_ << http::version(http_version)
74+
<< http::status(status_code)
75+
<< http::status_message(status_message);
7576
}
7677

7778
template <class Socket>
@@ -190,7 +191,7 @@ namespace boost { namespace network { namespace http { namespace impl {
190191
throw std::runtime_error("Unsupported HTTP version number.");
191192
}
192193

193-
response_ << body(body_stream.str());
194+
response_ << network::body(body_stream.str());
194195
}
195196

196197

boost/network/protocol/http/message.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,70 @@ namespace boost { namespace network { namespace http {
6666
* @return true if successful
6767
*/
6868
static bool const base64_encode(string_type const &input, string_type & output);
69+
70+
protected:
71+
mutable string_type version_;
72+
mutable boost::uint16_t status_;
73+
mutable string_type status_message_;
74+
75+
private:
76+
typedef basic_message<Tag> base_type;
77+
78+
public:
79+
80+
message_impl()
81+
: version_(), status_(0u), status_message_()
82+
{}
83+
84+
message_impl(message_impl const & other)
85+
: version_(other.version_), status_(other.status_), status_message_(other.status_message_)
86+
{}
87+
88+
void version(string_type const & version) const {
89+
version_ = version;
90+
}
91+
92+
string_type const version() const {
93+
return version_;
94+
}
95+
96+
void status(boost::uint16_t status) const {
97+
status_ = status;
98+
}
99+
100+
boost::uint16_t const status() const {
101+
return status_;
102+
}
103+
104+
void status_message(string_type const & status_message) const {
105+
status_message_ = status_message;
106+
}
107+
108+
string_type const status_message() const {
109+
return status_message_;
110+
}
111+
112+
message_impl & operator=(message_impl rhs) {
113+
rhs.swap(*this);
114+
return *this;
115+
}
116+
117+
void swap(message_impl & other) {
118+
basic_message<Tag> & base_ref(other),
119+
this_ref(*this);
120+
std::swap(this_ref, base_ref);
121+
std::swap(status_, other.status_);
122+
std::swap(status_message_, other.status_message_);
123+
std::swap(version_, other.version_);
124+
}
125+
69126
};
70127

128+
template <class Tag>
129+
inline void swap(message_impl<Tag> & lhs, message_impl<Tag> & rhs) {
130+
lhs.swap(rhs);
131+
}
132+
71133
typedef message_impl<tags::http_default_8bit_tcp_resolve> message;
72134

73135
} // namespace http

boost/network/protocol/http/message/directives/status.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace boost { namespace network { namespace http {
2929
: status_(other.status_) {}
3030

3131
template <class T> basic_response<T> const & operator() (basic_response<T> const & response) const {
32-
response.status() = status_;
32+
response.status(status_);
3333
return response;
3434
}
3535

boost/network/protocol/http/message/directives/status_message.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace boost { namespace network { namespace http {
2828
: status_message(other.status_message_) {}
2929

3030
template <class T> basic_response<T> const & operator() (basic_response<T> const & response) const {
31-
response.status_message() = status_message_;
31+
response.status_message(status_message_);
3232
return response;
3333
}
3434

boost/network/protocol/http/message/directives/version.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace boost { namespace network { namespace http {
2828
: version_(other.version_) {}
2929

3030
template <class T> basic_response<T> const & operator() (basic_response<T> const & response) const {
31-
response.version() = version_;
31+
response.version(version_);
3232
return response;
3333
}
3434

boost/network/protocol/http/message/message_base.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@
1414

1515
namespace boost { namespace network { namespace http {
1616

17-
namespace impl {
18-
template <class Tag>
19-
struct async_message_base {};
20-
};
21-
17+
template <class Tag>
18+
struct async_message;
19+
2220
template <class Tag>
2321
struct message_base
2422
: mpl::if_<
2523
is_base_of<
2624
tags::async,
2725
Tag
2826
>,
29-
impl::async_message_base<Tag>,
27+
async_message<Tag>,
3028
message_impl<Tag>
3129
>
3230
{};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace boost { namespace network { namespace http {
4444
pimpl->send_request_impl(method, request_);
4545

4646
response_ = basic_response<Tag>();
47-
response_ << source(request_.host());
47+
response_ << network::source(request_.host());
4848

4949
boost::asio::streambuf response_buffer;
5050
pimpl->read_status(response_, response_buffer);

boost/network/protocol/http/response.hpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <boost/cstdint.hpp>
1414
#include <boost/network/protocol/http/response_concept.hpp>
15-
15+
#include <boost/network/protocol/http/message/async_message.hpp>
1616
#include <boost/network/protocol/http/message/message_base.hpp>
1717

1818
namespace boost { namespace network { namespace http {
@@ -25,46 +25,27 @@ namespace boost { namespace network { namespace http {
2525
private:
2626
typedef typename message_base<Tag>::type base_type;
2727

28-
mutable string_type version_;
29-
mutable boost::uint16_t status_;
30-
mutable string_type status_message_;
31-
3228
public:
3329

3430
typedef Tag tag;
3531

3632
basic_response()
37-
: base_type(), version_(), status_(0u), status_message_()
38-
{ };
33+
: base_type()
34+
{}
3935

4036
basic_response(basic_response const & other)
41-
: base_type(other), version_(other.version_), status_(other.status_), status_message_(other.status_message_)
42-
{ };
43-
44-
string_type & version() const {
45-
return version_;
46-
};
47-
48-
boost::uint16_t & status() const {
49-
return status_;
50-
};
51-
52-
string_type & status_message() const {
53-
return status_message_;
54-
};
37+
: base_type(other)
38+
{}
5539

5640
basic_response & operator=(basic_response rhs) {
5741
rhs.swap(*this);
5842
return *this;
5943
};
6044

6145
void swap(basic_response & other) {
62-
message_impl<Tag> & base_ref(other);
63-
message_impl<Tag> & this_ref(*this);
64-
this_ref.swap(base_ref);
65-
std::swap(other.version_, version_);
66-
std::swap(other.status_, status_);
67-
std::swap(other.status_message_, status_message_);
46+
base_type & base_ref(other),
47+
& this_ref(*this);
48+
std::swap(this_ref, base_ref);
6849
};
6950
};
7051

boost/network/protocol/http/traits/resolver.hpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,45 @@
99
#include <boost/network/tags.hpp>
1010
#include <boost/asio/ip/tcp.hpp>
1111
#include <boost/asio/ip/udp.hpp>
12+
#include <boost/mpl/if.hpp>
13+
#include <boost/mpl/and.hpp>
14+
#include <boost/mpl/not.hpp>
15+
#include <boost/network/support/is_tcp.hpp>
16+
#include <boost/network/support/is_udp.hpp>
17+
#include <boost/network/support/is_http.hpp>
18+
#include <boost/static_assert.hpp>
1219

1320
namespace boost { namespace network { namespace http {
1421

1522
template <class Tag>
1623
struct unsupported_tag;
1724

1825
template <class Tag>
19-
struct resolver {
20-
typedef unsupported_tag<Tag> type;
21-
};
22-
23-
template <>
24-
struct resolver<tags::http_default_8bit_tcp_resolve> {
25-
typedef boost::asio::ip::tcp::resolver type;
26-
};
27-
28-
template <>
29-
struct resolver<tags::http_default_8bit_udp_resolve> {
30-
typedef boost::asio::ip::udp::resolver type;
31-
};
32-
33-
template <>
34-
struct resolver<tags::http_keepalive_8bit_tcp_resolve> {
35-
typedef boost::asio::ip::tcp::resolver type;
36-
};
37-
38-
template <>
39-
struct resolver<tags::http_keepalive_8bit_udp_resolve> {
40-
typedef boost::asio::ip::udp::resolver type;
26+
struct resolver :
27+
mpl::if_<
28+
mpl::and_<
29+
is_tcp<Tag>,
30+
is_http<Tag>
31+
>,
32+
boost::asio::ip::tcp::resolver,
33+
typename mpl::if_<
34+
mpl::and_<
35+
is_udp<Tag>,
36+
is_http<Tag>
37+
>,
38+
boost::asio::ip::udp::resolver,
39+
unsupported_tag<Tag>
40+
>::type
41+
>
42+
{
43+
BOOST_STATIC_ASSERT((
44+
mpl::not_<
45+
mpl::and_<
46+
is_udp<Tag>,
47+
is_tcp<Tag>
48+
>
49+
>::value
50+
));
4151
};
4252

4353
} // namespace http

boost/network/protocol/http/traits/resolver_policy.hpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,28 @@
88

99
#include <boost/network/tags.hpp>
1010
#include <boost/network/protocol/http/policies/sync_resolver.hpp>
11+
#include <boost/network/protocol/http/policies/async_resolver.hpp>
12+
#include <boost/network/support/is_async.hpp>
13+
#include <boost/network/support/is_http.hpp>
14+
#include <boost/mpl/if.hpp>
15+
#include <boost/mpl/and.hpp>
1116

1217
namespace boost { namespace network { namespace http {
1318

1419
template <class Tag>
1520
struct unsupported_tag;
1621

1722
template <class Tag>
18-
struct resolver_policy {
19-
typedef unsupported_tag<Tag> type;
20-
};
21-
22-
template <>
23-
struct resolver_policy<tags::http_default_8bit_tcp_resolve> {
24-
typedef policies::sync_resolver<tags::http_default_8bit_tcp_resolve> type;
25-
};
26-
27-
template <>
28-
struct resolver_policy<tags::http_default_8bit_udp_resolve> {
29-
typedef policies::sync_resolver<tags::http_default_8bit_udp_resolve> type;
30-
};
31-
32-
template <>
33-
struct resolver_policy<tags::http_keepalive_8bit_udp_resolve> {
34-
typedef policies::sync_resolver<tags::http_keepalive_8bit_udp_resolve> type;
35-
};
36-
37-
template <>
38-
struct resolver_policy<tags::http_keepalive_8bit_tcp_resolve> {
39-
typedef policies::sync_resolver<tags::http_keepalive_8bit_tcp_resolve> type;
40-
};
23+
struct resolver_policy :
24+
mpl::if_<
25+
mpl::and_< is_async<Tag>,is_http<Tag> >,
26+
policies::async_resolver<Tag>,
27+
typename mpl::if_<is_http<Tag>,
28+
policies::sync_resolver<Tag>,
29+
unsupported_tag<Tag>
30+
>::type
31+
>
32+
{};
4133

4234
} // namespace http
4335

0 commit comments

Comments
 (0)