Skip to content

Commit e1f9211

Browse files
committed
[URI] Added some convenient builder functions from URI parts and a filesystem path.
[URI] Added an alternative builder syntax.
1 parent d3d3bff commit e1f9211

File tree

14 files changed

+548
-140
lines changed

14 files changed

+548
-140
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Testing
88
*.gch
99
libs/mime/test/mime-roundtrip
1010
*.a
11+
_build

boost/network/uri/accessors.hpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ template <
2525
struct key_value_sequence
2626
: spirit::qi::grammar<uri::const_iterator, Map()>
2727
{
28+
typedef typename Map::key_type key_type;
29+
typedef typename Map::mapped_type mapped_type;
30+
typedef std::pair<key_type, mapped_type> pair_type;
31+
2832
key_value_sequence()
2933
: key_value_sequence::base_type(query)
3034
{
@@ -35,8 +39,9 @@ struct key_value_sequence
3539
}
3640

3741
spirit::qi::rule<uri::const_iterator, Map()> query;
38-
spirit::qi::rule<uri::const_iterator, std::pair<std::string, std::string>()> pair;
39-
spirit::qi::rule<uri::const_iterator, typename std::string()> key, value;
42+
spirit::qi::rule<uri::const_iterator, pair_type()> pair;
43+
spirit::qi::rule<uri::const_iterator, key_type()> key;
44+
spirit::qi::rule<uri::const_iterator, mapped_type()> value;
4045
};
4146
} // namespace details
4247

@@ -45,57 +50,57 @@ template <
4550
>
4651
inline
4752
Map &query_map(const uri &uri_, Map &map) {
48-
const std::string range = uri_.query();
53+
const uri::string_type range = uri_.query();
4954
details::key_value_sequence<Map> parser;
5055
spirit::qi::parse(boost::begin(range), boost::end(range), parser, map);
5156
return map;
5257
}
5358

5459
inline
55-
std::string username(const uri &uri_) {
56-
const std::string user_info = uri_.user_info();
60+
uri::string_type username(const uri &uri_) {
61+
const uri::string_type user_info = uri_.user_info();
5762
uri::const_iterator it(boost::begin(user_info)), end(boost::end(user_info));
5863
for (; it != end; ++it) {
5964
if (*it == ':') {
6065
break;
6166
}
6267
}
63-
return std::string(boost::begin(user_info), it);
68+
return uri::string_type(boost::begin(user_info), it);
6469
}
6570

6671
inline
67-
std::string password(const uri &uri_) {
68-
const std::string user_info = uri_.user_info();
72+
uri::string_type password(const uri &uri_) {
73+
const uri::string_type user_info = uri_.user_info();
6974
uri::const_iterator it(boost::begin(user_info)), end(boost::end(user_info));
7075
for (; it != end; ++it) {
7176
if (*it == ':') {
7277
++it;
7378
break;
7479
}
7580
}
76-
return std::string(it, boost::end(user_info));
81+
return uri::string_type(it, boost::end(user_info));
7782
}
7883

7984
inline
80-
std::string decoded_path(const uri &uri_) {
81-
const std::string path = uri_.path();
82-
std::string decoded_path;
85+
uri::string_type decoded_path(const uri &uri_) {
86+
const uri::string_type path = uri_.path();
87+
uri::string_type decoded_path;
8388
decode(path, std::back_inserter(decoded_path));
8489
return decoded_path;
8590
}
8691

8792
inline
88-
std::string decoded_query(const uri &uri_) {
89-
const std::string query = uri_.query();
90-
std::string decoded_query;
93+
uri::string_type decoded_query(const uri &uri_) {
94+
const uri::string_type query = uri_.query();
95+
uri::string_type decoded_query;
9196
decode(query, std::back_inserter(decoded_query));
9297
return decoded_query;
9398
}
9499

95100
inline
96-
std::string decoded_fragment(const uri &uri_) {
97-
const std::string fragment = uri_.fragment();
98-
std::string decoded_fragment;
101+
uri::string_type decoded_fragment(const uri &uri_) {
102+
const uri::string_type fragment = uri_.fragment();
103+
uri::string_type decoded_fragment;
99104
decode(fragment, std::back_inserter(decoded_fragment));
100105
return decoded_fragment;
101106
}

boost/network/uri/builder.hpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) Glyn Matthews 2012.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#ifndef __BOOST_NETWORK_URI_BUILDER_INC__
8+
# define __BOOST_NETWORK_URI_BUILDER_INC__
9+
10+
11+
# include <boost/network/uri/uri.hpp>
12+
13+
14+
namespace boost {
15+
namespace network {
16+
namespace uri {
17+
class builder {
18+
19+
typedef uri::string_type string_type;
20+
21+
public:
22+
23+
builder(uri &uri_)
24+
: uri_(uri_) {
25+
26+
}
27+
28+
builder &scheme(const string_type &scheme) {
29+
uri_.uri_.append(scheme);
30+
if (non_hierarchical_schemes::exists(scheme)) {
31+
uri_.uri_.append(":");
32+
}
33+
else {
34+
uri_.uri_.append("://");
35+
}
36+
uri_.parse();
37+
return *this;
38+
}
39+
40+
builder &user_info(const string_type &user_info) {
41+
uri_.uri_.append(user_info);
42+
uri_.uri_.append("@");
43+
uri_.parse();
44+
return *this;
45+
}
46+
47+
builder &host(const string_type &host) {
48+
uri_.uri_.append(host);
49+
uri_.parse();
50+
return *this;
51+
}
52+
53+
builder &port(const string_type &port) {
54+
uri_.uri_.append(":");
55+
uri_.uri_.append(port);
56+
uri_.parse();
57+
return *this;
58+
}
59+
60+
builder &port(uint16_t port) {
61+
return this->port(boost::lexical_cast<string_type>(port));
62+
}
63+
64+
builder &path(const string_type &path) {
65+
uri_.uri_.append(path);
66+
uri_.parse();
67+
return *this;
68+
}
69+
70+
builder &encoded_path(const string_type &path) {
71+
string_type encoded_path;
72+
encode(path, std::back_inserter(encoded_path));
73+
uri_.uri_.append(encoded_path);
74+
uri_.parse();
75+
return *this;
76+
}
77+
78+
builder &query(const string_type &query) {
79+
uri_.uri_.append("?");
80+
uri_.uri_.append(query);
81+
uri_.parse();
82+
return *this;
83+
}
84+
85+
builder &query(const string_type &key, const string_type &value) {
86+
if (!uri_.query_range())
87+
{
88+
uri_.uri_.append("?");
89+
}
90+
else
91+
{
92+
uri_.uri_.append("&");
93+
}
94+
uri_.uri_.append(key);
95+
uri_.uri_.append("=");
96+
uri_.uri_.append(value);
97+
uri_.parse();
98+
return *this;
99+
}
100+
101+
builder &fragment(const string_type &fragment) {
102+
uri_.uri_.append("#");
103+
uri_.uri_.append(fragment);
104+
uri_.parse();
105+
return *this;
106+
}
107+
108+
private:
109+
110+
uri &uri_;
111+
112+
};
113+
} // namespace uri
114+
} // namespace network
115+
} // namespace boost
116+
117+
118+
#endif // __BOOST_NETWORK_URI_BUILDER_INC__

boost/network/uri/config.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Glyn Matthews 2012.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#ifndef __BOOST_NETWORK_URI_CONFIG_INC__
8+
# define __BOOST_NETWORK_URI_CONFIG_INC__
9+
10+
11+
# include <boost/config.hpp>
12+
# include <boost/detail/workaround.hpp>
13+
14+
# if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_URI_DYN_LINK)
15+
# define BOOST_URI_DECL
16+
# else
17+
# define BOOST_URI_DECL
18+
# endif // defined(BOOST_ALL_DYN_LINK) || defined(BOOST_URI_DYN_LINK)
19+
20+
21+
#endif // __BOOST_NETWORK_URI_CONFIG_INC__

boost/network/uri/directives/scheme.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ inline
5454
uri &https(uri &uri_) {
5555
return uri_ << scheme("https");
5656
}
57+
58+
inline
59+
uri &file(uri &uri_) {
60+
return uri_ << scheme("file");
61+
}
5762
} // namespace schemes
5863
} // namespace uri
5964
} // namespace network

boost/network/uri/schemes.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@
1515
namespace boost {
1616
namespace network {
1717
namespace uri {
18-
struct hierarchical_schemes {
18+
class hierarchical_schemes {
19+
20+
public:
1921

2022
static void register_(const std::string &scheme);
2123
static bool exists(const std::string &scheme);
2224

25+
private:
26+
2327
static boost::unordered_set<std::string> schemes_;
2428

2529
};
2630

27-
struct non_hierarchical_schemes {
31+
class non_hierarchical_schemes {
32+
33+
public:
2834

2935
static void register_(const std::string &scheme);
3036
static bool exists(const std::string &scheme);
3137

38+
private:
39+
3240
static boost::unordered_set<std::string> schemes_;
3341

3442
};

0 commit comments

Comments
 (0)