Skip to content

Commit 8a05166

Browse files
committed
Added a couple of extra tests to construct a URI from an existing root URI.
1 parent 1945ea8 commit 8a05166

File tree

6 files changed

+116
-28
lines changed

6 files changed

+116
-28
lines changed

boost/network/uri/accessors.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ template <
2323
typename Map
2424
>
2525
struct key_value_sequence
26-
: spirit::qi::grammar<typename Uri::const_iterator_type, Map()>
26+
: spirit::qi::grammar<typename Uri::const_iterator, Map()>
2727
{
28-
typedef typename Uri::const_iterator_type const_iterator_type;
28+
typedef typename Uri::const_iterator const_iterator;
2929

3030
key_value_sequence()
3131
: key_value_sequence::base_type(query)
@@ -36,9 +36,9 @@ struct key_value_sequence
3636
value = +spirit::qi::char_("a-zA-Z_0-9/%");
3737
}
3838

39-
spirit::qi::rule<const_iterator_type, Map()> query;
40-
spirit::qi::rule<const_iterator_type, std::pair<typename Uri::string_type, typename Uri::string_type>()> pair;
41-
spirit::qi::rule<const_iterator_type, typename Uri::string_type()> key, value;
39+
spirit::qi::rule<const_iterator, Map()> query;
40+
spirit::qi::rule<const_iterator, std::pair<typename Uri::string_type, typename Uri::string_type>()> pair;
41+
spirit::qi::rule<const_iterator, typename Uri::string_type()> key, value;
4242
};
4343
} // namespace details
4444

@@ -59,7 +59,7 @@ template <
5959
>
6060
typename basic_uri<Tag>::string_type username(const basic_uri<Tag> &uri) {
6161
typename basic_uri<Tag>::const_range_type user_info_range = uri.user_info_range();
62-
typename basic_uri<Tag>::const_iterator_type it(boost::begin(user_info_range)), end(boost::end(user_info_range));
62+
typename basic_uri<Tag>::const_iterator it(boost::begin(user_info_range)), end(boost::end(user_info_range));
6363
for (; it != end; ++it) {
6464
if (*it == ':') {
6565
break;
@@ -73,7 +73,7 @@ template <
7373
>
7474
typename basic_uri<Tag>::string_type password(const basic_uri<Tag> &uri) {
7575
typename basic_uri<Tag>::const_range_type user_info_range = uri.user_info_range();
76-
typename basic_uri<Tag>::const_iterator_type it(boost::begin(user_info_range)), end(boost::end(user_info_range));
76+
typename basic_uri<Tag>::const_iterator it(boost::begin(user_info_range)), end(boost::end(user_info_range));
7777
for (; it != end; ++it) {
7878
if (*it == ':') {
7979
++it;

boost/network/uri/directives.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
namespace boost {
1717
namespace network {
1818
namespace uri {
19+
template <
20+
class Tag
21+
>
22+
inline
23+
basic_uri<Tag> &operator << (basic_uri<Tag> &uri, const basic_uri<Tag> &root_uri) {
24+
if (root_uri.is_valid()) {
25+
uri.append(boost::begin(root_uri), boost::end(root_uri));
26+
}
27+
return uri;
28+
}
29+
1930
template <
2031
class Tag
2132
, class Directive

boost/network/uri/directives/authority.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ template <
3434
class T
3535
>
3636
inline
37-
authority_directive<T> authority(const T &value) {
37+
authority_directive<T> authority(const T &value) {
3838
return authority_directive<T>(value);
3939
}
4040
} // namespace uri

boost/network/uri/uri.hpp

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,24 @@ class basic_uri
4848
public:
4949

5050
typedef typename string<Tag>::type string_type;
51-
typedef typename string_type::iterator iterator_type;
52-
typedef boost::iterator_range<iterator_type> range_type;
53-
typedef typename string_type::const_iterator const_iterator_type;
54-
typedef boost::iterator_range<const_iterator_type> const_range_type;
51+
typedef typename string_type::iterator iterator;
52+
typedef boost::iterator_range<iterator> range_type;
53+
typedef typename string_type::const_iterator const_iterator;
54+
typedef boost::iterator_range<const_iterator> const_range_type;
5555

56-
basic_uri() : is_valid_(false) {
56+
basic_uri()
57+
: is_valid_(false) {
5758

5859
}
5960

61+
template <
62+
class FwdIter
63+
>
64+
basic_uri(const FwdIter &first, const FwdIter &last)
65+
: uri_(first, last), is_valid_(false) {
66+
parse();
67+
}
68+
6069
basic_uri(const string_type &uri)
6170
: uri_(uri), is_valid_(false) {
6271
parse();
@@ -88,19 +97,19 @@ class basic_uri
8897
parse();
8998
}
9099

91-
iterator_type begin() {
100+
iterator begin() {
92101
return uri_.begin();
93102
}
94103

95-
const_iterator_type begin() const {
104+
const_iterator begin() const {
96105
return uri_.begin();
97106
}
98107

99-
iterator_type end() {
108+
iterator end() {
100109
return uri_.end();
101110
}
102111

103-
const_iterator_type end() const {
112+
const_iterator end() const {
104113
return uri_.end();
105114
}
106115

@@ -195,9 +204,9 @@ class basic_uri
195204
}
196205

197206
template <
198-
class Iterator
207+
class FwdIter
199208
>
200-
void append(Iterator first, Iterator last) {
209+
void append(const FwdIter &first, const FwdIter &last) {
201210
uri_.append(first, last);
202211
parse();
203212
}
@@ -217,18 +226,10 @@ template <
217226
>
218227
inline
219228
void basic_uri<Tag>::parse() {
220-
const_iterator_type first(boost::begin(uri_)), last(boost::end(uri_));
229+
const_iterator first(boost::begin(uri_)), last(boost::end(uri_));
221230
is_valid_ = detail::parse(first, last, uri_parts_);
222231
}
223232

224-
template <
225-
class Tag
226-
>
227-
inline
228-
void swap(basic_uri<Tag> &lhs, basic_uri<Tag> &rhs) {
229-
lhs.swap(rhs);
230-
}
231-
232233
template <
233234
class Tag
234235
>
@@ -358,6 +359,46 @@ bool operator == (const basic_uri<Tag> &lhs, const basic_uri<Tag> &rhs) {
358359
}
359360
} // namespace uri
360361
} // namespace network
362+
363+
template <
364+
class Tag
365+
>
366+
inline
367+
typename network::uri::basic_uri<Tag>::iterator begin(network::uri::basic_uri<Tag> &uri) {
368+
return uri.begin();
369+
}
370+
371+
template <
372+
class Tag
373+
>
374+
inline
375+
typename network::uri::basic_uri<Tag>::iterator end(network::uri::basic_uri<Tag> &uri) {
376+
return uri.end();
377+
}
378+
379+
template <
380+
class Tag
381+
>
382+
inline
383+
typename network::uri::basic_uri<Tag>::const_iterator begin(const network::uri::basic_uri<Tag> &uri) {
384+
return uri.begin();
385+
}
386+
387+
template <
388+
class Tag
389+
>
390+
inline
391+
typename network::uri::basic_uri<Tag>::const_iterator end(const network::uri::basic_uri<Tag> &uri) {
392+
return uri.end();
393+
}
394+
395+
template <
396+
class Tag
397+
>
398+
inline
399+
void swap(network::uri::basic_uri<Tag> &lhs, network::uri::basic_uri<Tag> &rhs) {
400+
lhs.swap(rhs);
401+
}
361402
} // namespace boost
362403

363404

libs/network/test/uri/url_builder_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(fragment_test, T, tag_types)
205205
BOOST_CHECK(boost::equal(uri::fragment(instance), fragment));
206206
}
207207

208+
BOOST_AUTO_TEST_CASE_TEMPLATE(from_root_test, T, tag_types)
209+
{
210+
typedef uri::basic_uri<T> uri_type;
211+
typedef typename uri_type::string_type string_type;
212+
213+
const std::string root("http://www.example.com");
214+
const std::string scheme("http");
215+
const std::string host("www.example.com");
216+
const std::string path("/");
217+
const std::string fragment("fragment");
218+
219+
uri_type root_uri(boost::begin(root), boost::end(root));
220+
uri_type instance;
221+
//instance << uri::uri("http://www.example.com") << uri::path("/") << uri::fragment("fragment");
222+
instance << root_uri
223+
<< uri::path(string_type(boost::begin(path), boost::end(path)))
224+
<< uri::fragment(string_type(boost::begin(fragment), boost::end(fragment)));
225+
BOOST_REQUIRE(uri::is_valid(instance));
226+
BOOST_CHECK(boost::equal(uri::scheme(instance), scheme));
227+
BOOST_CHECK(boost::equal(uri::host(instance), host));
228+
BOOST_CHECK(boost::equal(uri::path(instance), path));
229+
BOOST_CHECK(boost::equal(uri::fragment(instance), fragment));
230+
}
231+
208232
//BOOST_AUTO_TEST_CASE_TEMPLATE(scheme_test, T, tag_types)
209233
//{
210234
// typedef uri::basic_uri<T> uri_type;

libs/network/test/uri/url_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,15 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(xmpp_query_map_test, T, tag_types) {
312312
BOOST_CHECK(boost::equal((++queries.begin())->first, key_2));
313313
BOOST_CHECK(boost::equal((++queries.begin())->second, value_2));
314314
}
315+
316+
BOOST_AUTO_TEST_CASE_TEMPLATE(range_test, T, tag_types)
317+
{
318+
typedef uri::basic_uri<T> uri_type;
319+
typedef typename uri_type::string_type string_type;
320+
321+
const std::string url("http://www.example.com/");
322+
323+
uri_type instance(string_type(boost::begin(url), boost::end(url)));
324+
BOOST_REQUIRE(uri::is_valid(instance));
325+
BOOST_CHECK(boost::equal(instance, url));
326+
}

0 commit comments

Comments
 (0)