Skip to content

Commit b86fa8c

Browse files
committed
Fixed bug where the parser didn't ignore optional URI parts.
1 parent 7af0122 commit b86fa8c

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

boost/network/uri/detail/uri_parts.hpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# define BOOST_NETWORK_URL_DETAIL_URL_PARTS_HPP_
88

99

10+
# include <boost/optional.hpp>
11+
12+
1013
namespace boost {
1114
namespace network {
1215
namespace uri {
@@ -15,14 +18,10 @@ template <
1518
class String
1619
>
1720
struct hierarchical_part {
18-
String user_info, host, port, path;
19-
20-
void clear() {
21-
user_info.clear();
22-
host.clear();
23-
port.clear();
24-
path.clear();
25-
}
21+
boost::optional<String> user_info;
22+
boost::optional<String> host;
23+
boost::optional<String> port;
24+
boost::optional<String> path;
2625
};
2726

2827
template <
@@ -31,14 +30,17 @@ template <
3130
struct uri_parts {
3231
String scheme;
3332
hierarchical_part<String> hier_part;
34-
String query;
35-
String fragment;
33+
boost::optional<String> query;
34+
boost::optional<String> fragment;
3635

3736
void clear() {
3837
scheme.clear();
39-
hier_part.clear();
40-
query.clear();
41-
fragment.clear();
38+
hier_part.user_info = boost::optional<String>();
39+
hier_part.host = boost::optional<String>();
40+
hier_part.port = boost::optional<String>();
41+
hier_part.path = boost::optional<String>();
42+
query = boost::optional<String>();
43+
fragment = boost::optional<String>();
4244
}
4345
};
4446
} // namespace detail

boost/network/uri/uri.hpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,39 @@ class uri
101101
}
102102

103103
string_type user_info() const {
104-
return uri_parts_.hier_part.user_info;
104+
return uri_parts_.hier_part.user_info?
105+
uri_parts_.hier_part.user_info.get() :
106+
string_type();
105107
}
106108

107109
string_type host() const {
108-
return uri_parts_.hier_part.host;
110+
return uri_parts_.hier_part.host?
111+
uri_parts_.hier_part.host.get() :
112+
string_type();
109113
}
110114

111115
string_type port() const {
112-
return uri_parts_.hier_part.port;
116+
return uri_parts_.hier_part.port?
117+
uri_parts_.hier_part.port.get() :
118+
string_type();
113119
}
114120

115121
string_type path() const {
116-
return uri_parts_.hier_part.path;
122+
return uri_parts_.hier_part.path?
123+
uri_parts_.hier_part.path.get() :
124+
string_type();
117125
}
118126

119127
string_type query() const {
120-
return uri_parts_.query;
128+
return uri_parts_.query ?
129+
uri_parts_.query.get() :
130+
string_type();
121131
}
122132

123133
string_type fragment() const {
124-
return uri_parts_.fragment;
134+
return uri_parts_.fragment?
135+
uri_parts_.fragment.get() :
136+
string_type();
125137
}
126138

127139
string_type string() const {

libs/network/src/uri/parse.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ BOOST_FUSION_ADAPT_TPL_STRUCT
1313
(
1414
(String),
1515
(boost::network::uri::detail::hierarchical_part)(String),
16-
(String, user_info)
17-
(String, host)
18-
(String, port)
19-
(String, path)
16+
(boost::optional<String>, user_info)
17+
(boost::optional<String>, host)
18+
(boost::optional<String>, port)
19+
(boost::optional<String>, path)
2020
);
2121

2222
BOOST_FUSION_ADAPT_TPL_STRUCT
@@ -25,8 +25,8 @@ BOOST_FUSION_ADAPT_TPL_STRUCT
2525
(boost::network::uri::detail::uri_parts)(String),
2626
(String, scheme)
2727
(boost::network::uri::detail::hierarchical_part<String>, hier_part)
28-
(String, query)
29-
(String, fragment)
28+
(boost::optional<String>, query)
29+
(boost::optional<String>, fragment)
3030
);
3131

3232
namespace boost {

libs/network/test/uri/url_test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ BOOST_AUTO_TEST_CASE(basic_uri_test) {
4444
uri::uri instance("http://www.example.com/");
4545
BOOST_REQUIRE(uri::valid(instance));
4646
BOOST_CHECK_EQUAL(uri::scheme(instance), "http");
47+
BOOST_CHECK_EQUAL(uri::user_info(instance), "");
4748
BOOST_CHECK_EQUAL(uri::host(instance), "www.example.com");
49+
BOOST_CHECK_EQUAL(uri::port(instance), "");
4850
BOOST_CHECK_EQUAL(uri::path(instance), "/");
51+
BOOST_CHECK_EQUAL(uri::query(instance), "");
52+
BOOST_CHECK_EQUAL(uri::fragment(instance), "");
4953
}
5054

5155
BOOST_AUTO_TEST_CASE(full_uri_test) {
@@ -164,7 +168,6 @@ BOOST_AUTO_TEST_CASE(username_test) {
164168
BOOST_AUTO_TEST_CASE(authority_test) {
165169
uri::uri instance("http://user:password@www.example.com:80/path?query#fragment");
166170
BOOST_REQUIRE(uri::valid(instance));
167-
std::cout << uri::authority(instance) << std::endl;
168171
BOOST_CHECK_EQUAL(uri::authority(instance), "user:password@www.example.com:80");
169172
}
170173

0 commit comments

Comments
 (0)