diff options
| author | Alan Conway <aconway@apache.org> | 2007-06-15 21:27:22 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2007-06-15 21:27:22 +0000 |
| commit | 41c30308ad435c338633b97405fe7350d515f069 (patch) | |
| tree | c20cefadf868dac424804e64553d14a4590cd52d /cpp/src/qpid/Url.cpp | |
| parent | 4753936e96c9dd3815af610e9d1205d6e947775b (diff) | |
| download | qpid-python-41c30308ad435c338633b97405fe7350d515f069.tar.gz | |
Parser for AMQP-95 URLs
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@547789 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/Url.cpp')
| -rw-r--r-- | cpp/src/qpid/Url.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/cpp/src/qpid/Url.cpp b/cpp/src/qpid/Url.cpp new file mode 100644 index 0000000000..b9c17ffd82 --- /dev/null +++ b/cpp/src/qpid/Url.cpp @@ -0,0 +1,114 @@ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed 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 "Url.h" +#include <sstream> +#include <boost/spirit.hpp> +#include <boost/spirit/actor.hpp> + +using namespace boost::spirit; +using namespace std; + +namespace qpid { + +string Url::str() const { + ostringstream os; + os << *this; + return os.str(); +} + +ostream& operator<<(ostream& os, const Url& url) { + Url::const_iterator i = url.begin(); + os << "amqp:"; + if (i!=url.end()) { + os << *i++; + while (i != url.end()) + os << "," << *i++; + } + return os; +} + +// Addition to boost::spirit parsers: accept any character from a +// string. Vastly more compile-time-efficient than long rules of the +// form: ch_p('x') | ch_p('y') |... +// +struct ch_in : public char_parser<ch_in> { + ch_in(const string& chars_) : chars(chars_) {} + bool test(char ch_) const { + return chars.find(ch_) != string::npos; + } + string chars; +}; + +inline ch_in ch_in_p(const string& chars) { + return ch_in(chars); +} + +/** Grammar for AMQP URLs. */ +struct UrlGrammar : public grammar<UrlGrammar> +{ + Url& addr; + + UrlGrammar(Url& addr_) : addr(addr_) {} + + template <class ScannerT> + struct definition { + TcpAddress tcp; + + definition(const UrlGrammar& self) + { + first = eps_p[clear_a(self.addr)] >> amqp_url; + amqp_url = str_p("amqp:") >> prot_addr_list >> + !(str_p("/") >> !parameters); + prot_addr_list = prot_addr % ','; + prot_addr = tcp_prot_addr; // Extend for TLS etc. + + // TCP addresses + tcp_prot_addr = tcp_id >> tcp_addr[push_back_a(self.addr, tcp)]; + tcp_id = !str_p("tcp:"); + tcp_addr = !(host[assign_a(tcp.host)] >> !(':' >> port)); + + // See http://www.apps.ietf.org/rfc/rfc3986.html#sec-A + // for real host grammar. Shortcut: + port = uint_parser<uint16_t>()[assign_a(tcp.port)]; + host = *( unreserved | pct_encoded ); + unreserved = alnum_p | ch_in_p("-._~"); + pct_encoded = "%" >> xdigit_p >> xdigit_p; + parameters = *anychar_p >> end_p; // Ignore, not used yet. + } + + const rule<ScannerT>& start() const { return first; } + + rule<ScannerT> first, amqp_url, prot_addr_list, prot_addr, + tcp_prot_addr, tcp_id, tcp_addr, host, port, + unreserved, pct_encoded, parameters; + }; +}; + +Url::Url(const string& url) { + if (!parse(url.c_str(), UrlGrammar(*this)).full) + throw InvalidUrl("Invalid AMQP url: "+url); + // TODO aconway 2007-06-15: Better error handling? +} + +Url::Url(const string& url, const nothrow_t&) { + if (!parse(url.c_str(), UrlGrammar(*this)).full) + clear(); +} + +} // namespace qpid |
