You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2007/06/15 23:27:23 UTC
svn commit: r547789 - in /incubator/qpid/trunk/qpid/cpp/src: Makefile.am
qpid/Url.cpp qpid/Url.h tests/Makefile.am tests/unit/Url.cpp
Author: aconway
Date: Fri Jun 15 14:27:22 2007
New Revision: 547789
URL: http://svn.apache.org/viewvc?view=rev&rev=547789
Log:
Parser for AMQP-95 URLs
Added:
incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp (with props)
incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h (with props)
incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp (with props)
Modified:
incubator/qpid/trunk/qpid/cpp/src/Makefile.am
incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
Modified: incubator/qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/Makefile.am?view=diff&rev=547789&r1=547788&r2=547789
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/Makefile.am Fri Jun 15 14:27:22 2007
@@ -156,6 +156,8 @@
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 \
Added: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp?view=auto&rev=547789
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp Fri Jun 15 14:27:22 2007
@@ -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
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h?view=auto&rev=547789
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h Fri Jun 15 14:27:22 2007
@@ -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 <boost/variant.hpp>
+#include <string>
+#include <vector>
+#include <new>
+#include <ostream>
+
+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<TcpAddress> Address;
+
+
+/** An AMQP URL contains a list of addresses */
+struct Url : public std::vector<Address> {
+ 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*/
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am?view=diff&rev=547789&r1=547788&r2=547789
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am Fri Jun 15 14:27:22 2007
@@ -10,6 +10,10 @@
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
Added: incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp?view=auto&rev=547789
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp Fri Jun 15 14:27:22 2007
@@ -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<boost/test/*>
+#include "test_tools.h"
+#include "qpid/Url.h"
+#include <boost/assign.hpp>
+
+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&) {}
+}
+
+
Propchange: incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/tests/unit/Url.cpp
------------------------------------------------------------------------------
svn:keywords = Rev Date