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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "qpid/messaging/Address.h"
#include "qpid/framing/Uuid.h"
#include <sstream>
#include <boost/format.hpp>
namespace qpid {
namespace messaging {
namespace {
const std::string SUBJECT_DIVIDER = "/";
const std::string SPACE = " ";
const std::string TYPE = "type";
}
class AddressImpl
{
public:
std::string name;
std::string subject;
Variant::Map options;
AddressImpl() {}
AddressImpl(const std::string& n, const std::string& s, const Variant::Map& o) :
name(n), subject(s), options(o) {}
};
class AddressParser
{
public:
AddressParser(const std::string&);
bool parse(Address& address);
private:
const std::string& input;
std::string::size_type current;
static const std::string RESERVED;
bool readChar(char c);
bool readQuotedString(Variant& value);
bool readString(Variant& value, char delimiter);
bool readWord(std::string& word);
bool readSimpleValue(Variant& word);
bool readKey(std::string& key);
bool readValue(Variant& value);
bool readKeyValuePair(Variant::Map& map);
bool readMap(Variant& value);
bool readList(Variant& value);
bool error(const std::string& message);
bool eos();
bool iswhitespace();
bool isreserved();
};
Address::Address() : impl(new AddressImpl()) {}
Address::Address(const std::string& address) : impl(new AddressImpl())
{
AddressParser parser(address);
parser.parse(*this);
}
Address::Address(const std::string& name, const std::string& subject, const Variant::Map& options,
const std::string& type)
: impl(new AddressImpl(name, subject, options)) { setType(type); }
Address::Address(const Address& a) :
impl(new AddressImpl(a.impl->name, a.impl->subject, a.impl->options)) {}
Address::~Address() { delete impl; }
Address& Address::operator=(const Address& a) { *impl = *a.impl; return *this; }
std::string Address::toStr() const
{
std::stringstream out;
out << impl->name;
if (!impl->subject.empty()) out << SUBJECT_DIVIDER << impl->subject;
if (!impl->options.empty()) out << " {" << impl->options << "}";
return out.str();
}
Address::operator bool() const { return !impl->name.empty(); }
bool Address::operator !() const { return impl->name.empty(); }
const std::string& Address::getName() const { return impl->name; }
void Address::setName(const std::string& name) { impl->name = name; }
const std::string& Address::getSubject() const { return impl->subject; }
bool Address::hasSubject() const { return !(impl->subject.empty()); }
void Address::setSubject(const std::string& subject) { impl->subject = subject; }
const Variant::Map& Address::getOptions() const { return impl->options; }
Variant::Map& Address::getOptions() { return impl->options; }
void Address::setOptions(const Variant::Map& options) { impl->options = options; }
namespace{
const Variant EMPTY_VARIANT;
const std::string EMPTY_STRING;
}
std::string Address::getType() const
{
const Variant& type = getOption(TYPE);
return type.isVoid() ? EMPTY_STRING : type.asString();
}
void Address::setType(const std::string& type) { impl->options[TYPE] = type; }
const Variant& Address::getOption(const std::string& key) const
{
Variant::Map::const_iterator i = impl->options.find(key);
if (i == impl->options.end()) return EMPTY_VARIANT;
else return i->second;
}
std::ostream& operator<<(std::ostream& out, const Address& address)
{
out << address.toStr();
return out;
}
InvalidAddress::InvalidAddress(const std::string& msg) : Exception(msg) {}
MalformedAddress::MalformedAddress(const std::string& msg) : Exception(msg) {}
AddressParser::AddressParser(const std::string& s) : input(s), current(0) {}
bool AddressParser::error(const std::string& message)
{
throw MalformedAddress(message);//TODO: add more debug detail to error message (position etc)
}
bool AddressParser::parse(Address& address)
{
std::string name;
if (readWord(name)) {
if (name.find('#') == 0) name = qpid::framing::Uuid(true).str() + name;
address.setName(name);
if (readChar('/')) {
std::string subject;
if (readWord(subject)) {
address.setSubject(subject);
} else {
return error("Expected subject after /");
}
}
Variant options = Variant::Map();
if (readMap(options)) {
address.setOptions(options.asMap());
}
return true;
} else {
return input.empty() || error("Expected name");
}
}
bool AddressParser::readList(Variant& value)
{
if (readChar('[')) {
value = Variant::List();
Variant item;
while (readValue(item)) {
value.asList().push_back(item);
if (!readChar(',')) break;
}
return readChar(']') || error("Unmatched '['!");
} else {
return false;
}
}
bool AddressParser::readMap(Variant& value)
{
if (readChar('{')) {
value = Variant::Map();
while (readKeyValuePair(value.asMap()) && readChar(',')) {}
return readChar('}') || error("Unmatched '{'!");
} else {
return false;
}
}
bool AddressParser::readKeyValuePair(Variant::Map& map)
{
std::string key;
Variant value;
if (readKey(key)) {
if (readChar(':') && readValue(value)) {
map[key] = value;
return true;
} else {
return error("Bad key-value pair!");
}
} else {
return false;
}
}
bool AddressParser::readKey(std::string& key)
{
return readWord(key);
}
bool AddressParser::readValue(Variant& value)
{
return readSimpleValue(value) || readQuotedString(value) ||
readMap(value) || readList(value) || error("Expected value");
}
bool AddressParser::readString(Variant& value, char delimiter)
{
if (readChar(delimiter)) {
std::string::size_type start = current++;
while (!eos()) {
if (input.at(current) == delimiter) {
if (current > start) {
value = input.substr(start, current - start);
} else {
value = "";
}
++current;
return true;
} else {
++current;
}
}
return error("Unmatched delimiter");
} else {
return false;
}
}
bool AddressParser::readQuotedString(Variant& value)
{
return readString(value, '"') || readString(value, '\'');
}
bool AddressParser::readSimpleValue(Variant& value)
{
std::string s;
if (readWord(s)) {
value = s;
try { value = value.asInt64(); return true; } catch (const InvalidConversion&) {}
try { value = value.asDouble(); return true; } catch (const InvalidConversion&) {}
return true;
} else {
return false;
}
}
bool AddressParser::readWord(std::string& value)
{
//skip leading whitespace
while (!eos() && iswhitespace()) ++current;
//read any number of non-whitespace, non-reserved chars into value
std::string::size_type start = current;
while (!eos() && !iswhitespace() && !isreserved()) ++current;
if (current > start) {
value = input.substr(start, current - start);
return true;
} else {
return false;
}
}
bool AddressParser::readChar(char c)
{
while (!eos()) {
if (iswhitespace()) {
++current;
} else if (input.at(current) == c) {
++current;
return true;
} else {
return false;
}
}
return false;
}
bool AddressParser::iswhitespace()
{
return ::isspace(input.at(current));
}
bool AddressParser::isreserved()
{
return RESERVED.find(input.at(current)) != std::string::npos;
}
bool AddressParser::eos()
{
return current >= input.size();
}
const std::string AddressParser::RESERVED = "\'\"{}[],:/";
}} // namespace qpid::messaging
|