Menu

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

Download this file

214 lines (173 with data), 6.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
// 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/fusion/sequence/intrinsic/value_at_key.hpp>
#include <boost/spirit.hpp>
#include <boost/spirit/phoenix.hpp>
// include message declaration
#include "boost/network/uri_fwd.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 Tags
>
class basic_uri {
typedef Tags tags;
typedef fusion::map<
fusion::pair<typename tags::scheme, std::string>,
fusion::pair<typename tags::host, std::string>,
fusion::pair<typename tags::port, unsigned int>,
fusion::pair<typename tags::path, std::string>,
fusion::pair<typename tags::query, std::string>,
fusion::pair<typename tags::fragment, std::string>
> uri_parts_type;
uri_parts_type uri_parts;
public:
basic_uri() {
}
explicit basic_uri(std::string const & uri_) {
parse(uri_, uri_parts);
}
bool valid() const {
return (!scheme().empty() && !host().empty());
}
std::string const& scheme() const {
return fusion::at_key<typename tags::scheme>(uri_parts);
}
std::string& scheme() {
return fusion::at_key<typename tags::scheme>(uri_parts);
}
std::string const& host() const {
return fusion::at_key<typename tags::host>(uri_parts);
}
std::string& host() {
return fusion::at_key<typename tags::host>(uri_parts);
}
unsigned int& port() {
return fusion::at_key<typename tags::port>(uri_parts);
}
unsigned int port() const {
return fusion::at_key<typename tags::port>(uri_parts);
}
std::string& path() {
return fusion::at_key<typename tags::path>(uri_parts);
}
std::string const& path() const {
return fusion::at_key<typename tags::path>(uri_parts);
}
std::string& query() {
return fusion::at_key<typename tags::query>(uri_parts);
}
std::string const& query() const {
return fusion::at_key<typename tags::query>(uri_parts);
}
std::string& fragment() {
return fusion::at_key<typename tags::fragment>(uri_parts);
}
std::string const& fragment() const {
return fusion::at_key<typename tags::fragment>(uri_parts);
}
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());
}
std::string str() const {
if (!valid())
return std::string();
std::ostringstream builder;
builder << scheme() << "://";
builder << host();
if (port() != 0)
builder << ":" << port();
builder << path();
if (!query().empty())
builder << "?" << query();
if (!fragment().empty())
builder << "#" << fragment();
return builder.str();
}
private:
static void parse(std::string const& uri_, uri_parts_type& parts) {
using namespace boost::spirit;
using namespace phoenix;
boost::spirit::parse(
uri_.begin(), uri_.end(),
// the parser
(
(+alnum_p) [
var(fusion::at_key<typename tags::scheme>(parts))
= construct_<std::string>(arg1, arg2)
]
>> str_p("://")
)
>>
(+(alnum_p | '.' | '-' | '_')) [
var(fusion::at_key<typename tags::host>(parts))
= construct_<std::string>(arg1, arg2)
]
>>
!(
ch_p(':')
>>
uint_p [
var(fusion::at_key<typename tags::port>(parts))
= arg1
]
>> !ch_p('/')
)
>>
(+(alnum_p | '/' | '%' | '_' | '-' | '.')) [
var(fusion::at_key<typename tags::path>(parts))
= construct_<std::string>(arg1, arg2)
]
>>
!(
ch_p('?')
>>
(+(alnum_p | '&' | '=' | '%' | '_' )) [
var(fusion::at_key<typename tags::query>(parts))
= construct_<std::string>(arg1, arg2)
]
)
>> !(ch_p('#')
>>
(+(alnum_p | '_' | '%' | '-')) [
var(fusion::at_key<typename tags::fragment>(parts))
= construct_<std::string>(arg1, arg2)
]
)
>>
end_p
, nothing_p // no skip parser
);
normalize_path(parts);
}
static void normalize_path(uri_parts_type& parts) {
std::string &path = fusion::at_key<typename tags::path>(parts);
if (path.empty() || path[0] != '/')
path = "/" + path;
}
};
// typedef basic_uri<default_tags> uri;
} // namespace network
} // namespace boost
#endif // __NETWORK_UTILS_URI_20080916_HPP__