Menu

[r143]: / branches / uri / boost / network / uri.hpp  Maximize  Restore  History

Download this file

257 lines (207 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright Dean Michael Berris 2007.
// Copyright Michael Dickey 2008.
// Copyright Kim Grasman 2008.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef __NETWORK_UTILS_URI_20080916_HPP__
#define __NETWORK_UTILS_URI_20080916_HPP__
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/sequence/intrinsic/at_key.hpp>
#include <boost/spirit.hpp>
#include <boost/spirit/phoenix.hpp>
#include <boost/network/uri_fwd.hpp>
#include <boost/network/message/traits.hpp>
#include <boost/network/detail/directive_base.hpp>
namespace boost { namespace network {
/** uri.hpp
*
* This file implements the basic request object required
* by the HTTP client implementation. The uri
* object encapsulates a URI which is parsed at runtime.
* The data is broken up according to the URI specifications
* RFC 3986 -- http://www.ietf.org/rfc/rfc3986.txt -- using
* Fusion, and exposed as a typed struct.
*/
template <
class Tag
>
class basic_uri {
public:
typedef Tag tag;
typedef typename string<tag>::type string_type;
basic_uri() {
}
explicit basic_uri(string_type const & uri_) {
parse(uri_);
}
bool valid() const {
return (!scheme().empty() && !host().empty());
}
string_type const& scheme() const {
return fusion::at_key<typename parser_tags::scheme>(parts);
}
string_type& scheme() {
return fusion::at_key<typename parser_tags::scheme>(parts);
}
string_type const& user_info() const {
return fusion::at_key<typename parser_tags::user_info>(parts);
}
string_type& user_info() {
return fusion::at_key<typename parser_tags::user_info>(parts);
}
string_type const& host() const {
return fusion::at_key<typename parser_tags::host>(parts);
}
string_type& host() {
return fusion::at_key<typename parser_tags::host>(parts);
}
unsigned int& port() {
return fusion::at_key<typename parser_tags::port>(parts);
}
unsigned int port() const {
return fusion::at_key<typename parser_tags::port>(parts);
}
string_type& path() {
return fusion::at_key<typename parser_tags::path>(parts);
}
string_type const& path() const {
return fusion::at_key<typename parser_tags::path>(parts);
}
string_type& query() {
return fusion::at_key<typename parser_tags::query>(parts);
}
string_type const& query() const {
return fusion::at_key<typename parser_tags::query>(parts);
}
string_type& fragment() {
return fusion::at_key<typename parser_tags::fragment>(parts);
}
string_type const& fragment() const {
return fusion::at_key<typename parser_tags::fragment>(parts);
}
string_type const authority() const {
typename ostringstream<tag>::type builder;
if (!scheme().empty())
builder << scheme() << "://";
if (!user_info().empty())
builder << user_info() << "@";
if (!host().empty())
builder << host();
if (port() != 0)
builder << ":" << port();
return builder.str();
}
bool operator==(basic_uri const& other) const {
return (scheme() == other.scheme() &&
host() == other.host() &&
port() == other.port() &&
path() == other.path() &&
query() == other.query() &&
fragment() == other.fragment());
}
string_type str() const {
if (!valid())
return string_type();
typename ostringstream<tag>::type builder;
builder << authority();
builder << path();
if (!query().empty())
builder << "?" << query();
if (!fragment().empty())
builder << "#" << fragment();
return builder.str();
}
basic_uri& operator<<(const basic_uri& other) {
parse(other.str());
return *this;
}
private:
void parse(string_type const& uri_) {
using namespace boost::spirit;
using namespace phoenix;
string_type possible_user_info;
// Run the parser
boost::spirit::parse(
uri_.begin(), uri_.end(),
// the parser
(
(+alnum_p) [
var(fusion::at_key<typename parser_tags::scheme>(parts))
= construct_<string_type>(arg1, arg2)
]
>> str_p("://")
)
>>
!((+(alnum_p | '.' | '-' | '_')) [
// build up what might be user info...
var(possible_user_info) = construct_<string_type>(arg1, arg2)
] >> ch_p('@')) [
// ... and store it as soon as we match the @
var(fusion::at_key<typename parser_tags::user_info>(parts))
= var(possible_user_info)
]
>>
(+(alnum_p | '.' | '-' | '_')) [
var(fusion::at_key<typename parser_tags::host>(parts))
= construct_<string_type>(arg1, arg2)
]
>>
!(
ch_p(':')
>>
uint_p [
var(fusion::at_key<typename parser_tags::port>(parts))
= arg1
])
>>
*(ch_p('/') >> (+(alnum_p | '/' | '%' | '_' | '-' | '.'))) [
var(fusion::at_key<typename parser_tags::path>(parts))
= construct_<string_type>(arg1, arg2)
]
>>
!(
ch_p('?')
>>
(+(alnum_p | '&' | '=' | '%' | '_' )) [
var(fusion::at_key<typename parser_tags::query>(parts))
= construct_<string_type>(arg1, arg2)
]
)
>> !(ch_p('#')
>>
(+(alnum_p | '_' | '%' | '-')) [
var(fusion::at_key<typename parser_tags::fragment>(parts))
= construct_<string_type>(arg1, arg2)
]
)
>>
end_p
, nothing_p // no skip parser
);
}
struct parser_tags {
struct scheme {};
struct user_info {};
struct host {};
struct port {};
struct path {};
struct query {};
struct fragment {};
};
typedef fusion::map<
fusion::pair< typename parser_tags::scheme, string_type >,
fusion::pair< typename parser_tags::user_info, string_type >,
fusion::pair< typename parser_tags::host, string_type >,
fusion::pair< typename parser_tags::port, unsigned int >,
fusion::pair< typename parser_tags::path, string_type >,
fusion::pair< typename parser_tags::query, string_type >,
fusion::pair< typename parser_tags::fragment, string_type >
> uri_parts_type;
uri_parts_type parts;
};
} // namespace network
} // namespace boost
#include <boost/network/uri/directives.hpp>
// pull in directives header file
#endif // __NETWORK_UTILS_URI_20080916_HPP__