From 41c30308ad435c338633b97405fe7350d515f069 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Fri, 15 Jun 2007 21:27:22 +0000 Subject: Parser for AMQP-95 URLs git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@547789 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/Makefile.am | 2 + cpp/src/qpid/Url.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++ cpp/src/qpid/Url.h | 82 ++++++++++++++++++++++++++++++++ cpp/src/tests/Makefile.am | 4 ++ cpp/src/tests/unit/Url.cpp | 55 ++++++++++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 cpp/src/qpid/Url.cpp create mode 100644 cpp/src/qpid/Url.h create mode 100644 cpp/src/tests/unit/Url.cpp (limited to 'cpp/src') diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index 434313b998..f04f750987 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -156,6 +156,8 @@ libqpidcommon_la_SOURCES = \ gen/qpid/framing/AMQP_ServerProxy.cpp \ qpid/Exception.cpp \ qpid/ExceptionHolder.cpp \ + qpid/Url.h \ + qpid/Url.cpp \ qpid/QpidError.cpp \ qpid/sys/Runnable.cpp \ qpid/sys/ProducerConsumer.cpp \ 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 +#include +#include + +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(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 +{ + Url& addr; + + UrlGrammar(Url& addr_) : addr(addr_) {} + + template + 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()[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& start() const { return first; } + + rule 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 diff --git a/cpp/src/qpid/Url.h b/cpp/src/qpid/Url.h new file mode 100644 index 0000000000..f45192ce23 --- /dev/null +++ b/cpp/src/qpid/Url.h @@ -0,0 +1,82 @@ +#ifndef QPID_URL_H +#define QPID_URL_H + +/* + * + * 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 "qpid/Exception.h" +#include +#include +#include +#include +#include + +namespace qpid { + +/** TCP address of a broker - host:port */ +struct TcpAddress { + static const uint16_t defaultPort=5672; + TcpAddress(const std::string& host_=std::string(), + uint16_t port_=defaultPort) + : host(host_), port(port_) {} + std::string host; + uint16_t port; +}; + +bool operator==(const TcpAddress& x, const TcpAddress& y) { + return y.host==x.host && y.port == x.port; +} + +std::ostream& operator<<(std::ostream& os, const TcpAddress& a) { + return os << "tcp:" << a.host << ":" << a.port; +} + +/** Address is a variant of all address types. */ +typedef boost::variant Address; + + +/** An AMQP URL contains a list of addresses */ +struct Url : public std::vector
{ + struct InvalidUrl : public Exception { + InvalidUrl(const std::string& s) : Exception(s) {} + }; + + /** Convert to string form. */ + std::string str() const; + + /** Empty URL. */ + Url() {} + + /** Parse an amqp URL string as defined in + * https://wiki.108.redhat.com/jira/browse/AMQP-95 + *@exception InvalidUrl if the url is invalid. + */ + Url(const std::string& url); + + /** Parse an amqp URL string as defined in + * https://wiki.108.redhat.com/jira/browse/AMQP-95 + * Results in empty URL if url is invalid. + */ + Url(const std::string& url, const std::nothrow_t&); +}; + +std::ostream& operator<<(std::ostream& os, const Url& url); + +} // namespace qpid + +#endif /*!QPID_URL_H*/ diff --git a/cpp/src/tests/Makefile.am b/cpp/src/tests/Makefile.am index cd14bd57ab..76d675ee12 100644 --- a/cpp/src/tests/Makefile.am +++ b/cpp/src/tests/Makefile.am @@ -10,6 +10,10 @@ UNIT_TESTS+=logging logging_SOURCES=unit/logging.cpp unit/test_tools.h logging_LDADD=-lboost_unit_test_framework -lboost_regex $(lib_common) +UNIT_TESTS+=Url +Url_SOURCES=unit/Url.cpp unit/test_tools.h +Url_LDADD=-lboost_unit_test_framework $(lib_common) + if CLUSTER include cluster.mk endif diff --git a/cpp/src/tests/unit/Url.cpp b/cpp/src/tests/unit/Url.cpp new file mode 100644 index 0000000000..a8b415e641 --- /dev/null +++ b/cpp/src/tests/unit/Url.cpp @@ -0,0 +1,55 @@ +/* + * + * 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. + * + */ + +#define BOOST_AUTO_TEST_MAIN // Must come before #include +#include "test_tools.h" +#include "qpid/Url.h" +#include + +using namespace std; +using namespace qpid; +using namespace boost::assign; + +BOOST_AUTO_TEST_CASE(testUrl_str) { + Url url; + url.push_back(TcpAddress("foo.com")); + url.push_back(TcpAddress("bar.com", 6789)); + + BOOST_CHECK_EQUAL( + url.str(), "amqp:tcp:foo.com:5672,tcp:bar.com:6789"); + BOOST_CHECK_EQUAL(Url().str(), "amqp:"); +} + + +BOOST_AUTO_TEST_CASE(testUrl_ctor) { + BOOST_CHECK_EQUAL( + Url("amqp:foo.com,tcp:bar.com:1234").str(), + "amqp:tcp:foo.com:5672,tcp:bar.com:1234"); + BOOST_CHECK_EQUAL( + Url("amqp:foo/ignorethis").str(), + "amqp:tcp:foo:5672"); + BOOST_CHECK_EQUAL("amqp:tcp::5672", Url("amqp:").str()); + BOOST_CHECK_EQUAL(0u, Url("xxx", nothrow).size()); + try { + Url invalid("xxx"); + BOOST_FAIL("Expected InvalidUrl exception"); + } + catch (const Url::InvalidUrl&) {} +} + + -- cgit v1.2.1