Skip to content

Commit 5925e6f

Browse files
committed
Adding tests and support for UDP based resolving with the HTTP client.
1 parent 1c2a006 commit 5925e6f

File tree

8 files changed

+93
-73
lines changed

8 files changed

+93
-73
lines changed

boost/network/protocol/http/client.hpp

+22-15
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ namespace boost { namespace network { namespace http {
5151

5252
while (error && endpoint_iterator != end) {
5353
socket_.close();
54-
socket_.connect(*endpoint_iterator++, error);
54+
socket_.connect(
55+
tcp::endpoint(
56+
endpoint_iterator->endpoint().address()
57+
, endpoint_iterator->endpoint().port()
58+
)
59+
, error
60+
);
61+
++endpoint_iterator;
5562
}
5663

5764
if (error)
@@ -87,8 +94,8 @@ namespace boost { namespace network { namespace http {
8794
<< "Host: " << request_.host() << "\r\n"
8895
<< "Accept: */*\r\n";
8996

90-
headers_range<http::request>::type range = headers(request_);
91-
BOOST_FOREACH(headers_range<http::request>::type::value_type const & header, range) {
97+
typename headers_range<http::basic_request<Tag> >::type range = headers(request_);
98+
BOOST_FOREACH(typename headers_range<http::basic_request<Tag> >::type::value_type const & header, range) {
9299
request_stream << header.first << ": " << header.second << "\r\n";
93100
};
94101

@@ -169,7 +176,7 @@ namespace boost { namespace network { namespace http {
169176
response_ << body(body_stream.str());
170177
};
171178

172-
response const sync_request_skeleton(basic_request<Tag> const & request_, string_type method, bool get_body) {
179+
basic_response<Tag> const sync_request_skeleton(basic_request<Tag> const & request_, string_type method, bool get_body) {
173180
using boost::asio::ip::tcp;
174181

175182
basic_request<Tag> request_copy(request_);
@@ -191,8 +198,8 @@ namespace boost { namespace network { namespace http {
191198
if (follow_redirect_) {
192199
uint16_t status = response_.status();
193200
if (status >= 300 && status <= 307) {
194-
headers_range<http::request>::type location_range = headers(response_)["Location"];
195-
range_iterator<headers_range<http::request>::type>::type location_header = begin(location_range);
201+
typename headers_range<http::basic_response<Tag> >::type location_range = headers(response_)["Location"];
202+
typename range_iterator<typename headers_range<http::basic_request<Tag> >::type>::type location_header = begin(location_range);
196203
if (location_header != end(location_range)) {
197204
request_copy.uri(location_header->second);
198205
} else throw std::runtime_error("Location header not defined in redirect response.");
@@ -241,47 +248,47 @@ namespace boost { namespace network { namespace http {
241248
resolver_base::endpoint_cache_.clear();
242249
}
243250

244-
response const head (basic_request<Tag> const & request_) {
251+
basic_response<Tag> const head (basic_request<Tag> const & request_) {
245252
return sync_request_skeleton(request_, "HEAD", false);
246253
};
247254

248-
response const get (basic_request<Tag> const & request_) {
255+
basic_response<Tag> const get (basic_request<Tag> const & request_) {
249256
return sync_request_skeleton(request_, "GET", true);
250257
};
251258

252-
response const post (basic_request<Tag> const & request_) {
259+
basic_response<Tag> const post (basic_request<Tag> const & request_) {
253260
return sync_request_skeleton(request_, "POST", true);
254261
};
255262

256-
response const post (basic_request<Tag> const & request_, string_type const & content_type, string_type const & body_) {
263+
basic_response<Tag> const post (basic_request<Tag> const & request_, string_type const & content_type, string_type const & body_) {
257264
basic_request<Tag> request_copy = request_;
258265
request_copy << body(body_)
259266
<< header("Content-Type", content_type)
260267
<< header("Content-Length", boost::lexical_cast<string_type>(body_.size()));
261268
return post(request_copy);
262269
};
263270

264-
response const post (basic_request<Tag> const & request_, string_type const & body_) {
271+
basic_response<Tag> const post (basic_request<Tag> const & request_, string_type const & body_) {
265272
return post(request_, "x-application/octet-stream", body_);
266273
};
267274

268-
response const put (basic_request<Tag> const & request_) {
275+
basic_response<Tag> const put (basic_request<Tag> const & request_) {
269276
return sync_request_skeleton(request_, "PUT", true);
270277
};
271278

272-
response const put (basic_request<Tag> const & request_, string_type const & body_) {
279+
basic_response<Tag> const put (basic_request<Tag> const & request_, string_type const & body_) {
273280
return put(request_, "x-application/octet-stream", body_);
274281
};
275282

276-
response const put (basic_request<Tag> const & request_, string_type const & content_type, string_type const & body_) {
283+
basic_response<Tag> const put (basic_request<Tag> const & request_, string_type const & content_type, string_type const & body_) {
277284
basic_request<Tag> request_copy = request_;
278285
request_copy << body(body_)
279286
<< header("Content-Type", content_type)
280287
<< header("Content-Length", boost::lexical_cast<string_type>(body_.size()));
281288
return put(request_copy);
282289
};
283290

284-
response const delete_ (basic_request<Tag> const & request_) {
291+
basic_response<Tag> const delete_ (basic_request<Tag> const & request_) {
285292
return sync_request_skeleton(request_, "DELETE", true);
286293
};
287294

boost/network/tags.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace boost {
1313
namespace network {
1414
namespace tags {
1515

16-
struct default_string;
17-
struct default_wstring;
18-
struct http_default_8bit_tcp_resolve;
19-
struct http_default_8bit_udp_resolve;
20-
struct pod;
16+
struct default_string {};
17+
struct default_wstring {};
18+
struct http_default_8bit_tcp_resolve {};
19+
struct http_default_8bit_udp_resolve {};
20+
struct pod {};
2121

2222
typedef default_string default_;
2323

boost/network/traits/char.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@
66
#ifndef BOOST_NETWORK_TRAITS_CHAR_HPP
77
#define BOOST_NETWORK_TRAITS_CHAR_HPP
88

9+
#include <boost/network/tags.hpp>
10+
911
namespace boost { namespace network {
1012

13+
template <class Tag>
14+
struct unsupported_tag;
15+
1116
template <class Tag>
1217
struct char_ {
18+
typedef unsupported_tag<Tag> type;
19+
};
20+
21+
template <>
22+
struct char_<tags::http_default_8bit_tcp_resolve> {
23+
typedef char type;
24+
};
25+
26+
template <>
27+
struct char_<tags::http_default_8bit_udp_resolve> {
1328
typedef char type;
1429
};
1530

boost/network/traits/headers_container.hpp

-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#ifndef __BOOST_NETWORK_TRAITS_HEADERS_CONTAINER_INC__
88
# define __BOOST_NETWORK_TRAITS_HEADERS_CONTAINER_INC__
99

10-
1110
# include <map>
1211
# include <boost/network/tags.hpp>
1312
# include <boost/network/traits/string.hpp>
@@ -25,23 +24,6 @@ struct headers_container {
2524
> type;
2625
};
2726

28-
29-
// template <>
30-
// struct headers_container<tags::default_string> {
31-
// typedef std::multimap<
32-
// string<tags::default_string>::type,
33-
// string<tags::default_string>::type
34-
// > type;
35-
// };
36-
//
37-
//
38-
// template <>
39-
// struct headers_container<tags::default_wstring> {
40-
// typedef std::multimap<
41-
// string<tags::default_wstring>::type,
42-
// string<tags::default_wstring>::type
43-
// > type;
44-
// };
4527
} // namespace network
4628
} // namespace boost
4729

boost/network/traits/ostringstream.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ namespace boost { namespace network {
3535
typedef std::ostringstream type;
3636
};
3737

38+
template <>
39+
struct ostringstream<tags::http_default_8bit_udp_resolve> {
40+
typedef std::ostringstream type;
41+
};
42+
3843
} // namespace network
3944

4045
} // namespace boost

boost/network/traits/string.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
namespace boost { namespace network {
1515

16+
template <class Tag>
17+
struct unsupported_tag;
18+
1619
template <class Tag>
1720
struct string {
18-
typedef void type;
21+
typedef unsupported_tag<Tag> type;
1922
};
2023

2124
template <>
@@ -38,6 +41,11 @@ namespace boost { namespace network {
3841
typedef std::string type;
3942
};
4043

44+
template <>
45+
struct string<tags::http_default_8bit_udp_resolve> {
46+
typedef std::string type;
47+
};
48+
4149
} // namespace network
4250

4351
} // namespace boost

boost/network/traits/vector.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
namespace boost { namespace network {
1212

13+
template <class Tag>
14+
struct unsupported_tag;
15+
1316
template <class Tag>
1417
struct vector {
1518

1619
template <class Type>
1720
struct apply {
18-
typedef void type;
21+
typedef unsupported_tag<Tag> type;
1922
};
2023

2124
};

libs/network/test/http_1_0_test.cpp

+33-33
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,63 @@
88
#include <boost/test/unit_test.hpp>
99
#include <boost/network.hpp>
1010
#include <iostream>
11+
#include <boost/mpl/list.hpp>
1112

12-
BOOST_AUTO_TEST_CASE(http_get_test) {
13-
using namespace boost::network;
14-
http::request request("http://www.boost.org/");
15-
http::client client_;
16-
http::response response_;
13+
using namespace boost::network;
14+
15+
typedef boost::mpl::list<tags::http_default_8bit_tcp_resolve, tags::http_default_8bit_udp_resolve> tag_types;
16+
17+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_get_test, T, tag_types) {
18+
http::basic_request<T> request("http://www.boost.org/");
19+
http::basic_client<T, 1, 0> client_;
20+
http::basic_response<T> response_;
1721
response_ = client_.get(request);
18-
headers_range<http::response>::type range = headers(response_)["Content-Type"];
22+
typename headers_range<typename http::basic_response<T> >::type range = headers(response_)["Content-Type"];
1923
BOOST_CHECK ( begin(range) != end(range) );
2024
BOOST_CHECK ( body(response_).size() != 0 );
2125
}
2226

23-
BOOST_AUTO_TEST_CASE(http_get_test_different_port) {
24-
using namespace boost::network;
25-
http::request request("http://www.boost.org:80/");
26-
http::client client_;
27-
http::response response_;
27+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_get_test_different_port, T, tag_types) {
28+
http::basic_request<T> request("http://www.boost.org:80/");
29+
http::basic_client<T, 1, 0> client_;
30+
http::basic_response<T> response_;
2831
response_ = client_.get(request);
29-
headers_range<http::response>::type range = headers(response_)["Content-Type"];
32+
typename headers_range<typename http::basic_response<T> >::type range = headers(response_)["Content-Type"];
3033
BOOST_CHECK ( begin(range) != end(range) );
3134
BOOST_CHECK ( body(response_).size() != 0 );
3235
}
3336

34-
BOOST_AUTO_TEST_CASE(http_get_test_timeout) {
35-
using namespace boost::network;
36-
http::request request("http://localhost:12121/");
37-
http::client client_;
38-
http::response response_;
37+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_get_test_timeout, T, tag_types) {
38+
http::basic_request<T> request("http://localhost:12121/");
39+
http::basic_client<T, 1, 0> client_;
40+
http::basic_response<T> response_;
3941
BOOST_CHECK_THROW ( response_ = client_.get(request), boost::system::system_error );
4042
}
4143

42-
BOOST_AUTO_TEST_CASE(http_get_details) {
43-
using namespace boost::network;
44-
http::request request("http://www.boost.org/");
45-
http::client client_;
46-
http::response response_;
44+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_get_details, T, tag_types) {
45+
http::basic_request<T> request("http://www.boost.org/");
46+
http::basic_client<T, 1, 0> client_;
47+
http::basic_response<T> response_;
4748
BOOST_CHECK_NO_THROW ( response_ = client_.get(request) );
4849
BOOST_CHECK_EQUAL ( response_.version().substr(0,7), std::string("HTTP/1.") );
4950
BOOST_CHECK_EQUAL ( response_.status(), 200u );
5051
BOOST_CHECK_EQUAL ( response_.status_message(), std::string("OK") );
5152
}
5253

53-
BOOST_AUTO_TEST_CASE(http_cached_resolve) {
54-
using namespace boost::network;
55-
http::request request("http://www.boost.org");
56-
http::request other_request("http://www.boost.org/users/license.html");
57-
http::client client_(http::client::cache_resolved);
58-
http::response response_;
54+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_cached_resolve, T, tag_types) {
55+
http::basic_request<T> request("http://www.boost.org");
56+
http::basic_request<T> other_request("http://www.boost.org/users/license.html");
57+
http::basic_client<T,1,0> client_(http::basic_client<T,1,0>::cache_resolved);
58+
http::basic_response<T> response_;
5959
BOOST_CHECK_NO_THROW ( response_ = client_.get(request) );
6060
BOOST_CHECK_NO_THROW ( response_ = client_.get(other_request) );
6161
}
6262

63-
BOOST_AUTO_TEST_CASE(http_redirection_test) {
64-
using namespace boost::network;
65-
http::request request("http://boost.org");
66-
http::client client_(http::client::follow_redirect);
67-
http::response response_;
63+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_redirection_test, T, tag_types) {
64+
http::basic_request<T> request("http://boost.org");
65+
http::basic_client<T,1,0> client_(http::basic_client<T,1,0>::follow_redirect);
66+
http::basic_response<T> response_;
6867
BOOST_CHECK_NO_THROW ( response_ = client_.get(request) );
6968
BOOST_CHECK_EQUAL ( response_.status(), 200u );
7069
}
70+

0 commit comments

Comments
 (0)