From 50213f56ba58df7553edd3ffaaef7ca56c698114 Mon Sep 17 00:00:00 2001 From: David Wragg Date: Wed, 21 Sep 2011 12:10:21 +0100 Subject: Add support for parsing amqp URLs to librabbitmq --- .hgignore | 1 + librabbitmq/Makefile.am | 2 +- librabbitmq/amqp.h | 13 +++ librabbitmq/amqp_api.c | 1 + librabbitmq/amqp_private.h | 3 +- librabbitmq/amqp_url.c | 205 +++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 2 +- tests/test_parse_url.c | 174 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 398 insertions(+), 3 deletions(-) create mode 100644 librabbitmq/amqp_url.c create mode 100644 tests/test_parse_url.c diff --git a/.hgignore b/.hgignore index 6018037..99d8fa6 100644 --- a/.hgignore +++ b/.hgignore @@ -20,6 +20,7 @@ ^(|librabbitmq/|tests/|examples/|tools/|tools/doc/)Makefile(\.in)?$ ^tests/test_tables$ +^tests/test_parse_url$ ^examples/amqp_sendstring$ ^examples/amqp_exchange_declare$ ^examples/amqp_listen$ diff --git a/librabbitmq/Makefile.am b/librabbitmq/Makefile.am index 1217c49..f5908c3 100644 --- a/librabbitmq/Makefile.am +++ b/librabbitmq/Makefile.am @@ -13,7 +13,7 @@ if USE_MSINTTYPES AM_CFLAGS += -I$(top_srcdir)/msinttypes endif -librabbitmq_la_SOURCES = amqp_mem.c amqp_table.c amqp_connection.c amqp_socket.c amqp_api.c $(PLATFORM_DIR)/socket.c +librabbitmq_la_SOURCES = amqp_mem.c amqp_table.c amqp_connection.c amqp_socket.c amqp_api.c amqp_url.c $(PLATFORM_DIR)/socket.c librabbitmq_la_LDFLAGS = -no-undefined librabbitmq_la_LIBADD = $(EXTRA_LIBS) nodist_librabbitmq_la_SOURCES = amqp_framing.c diff --git a/librabbitmq/amqp.h b/librabbitmq/amqp.h index 9e06f35..5807da5 100644 --- a/librabbitmq/amqp.h +++ b/librabbitmq/amqp.h @@ -396,6 +396,19 @@ RABBITMQ_EXPORT int amqp_encode_table(amqp_bytes_t encoded, amqp_table_t *input, size_t *offset); +struct amqp_connection_info { + char *user; + char *password; + char *host; + char *vhost; + int port; +}; + +RABBITMQ_EXPORT void amqp_default_connection_info( + struct amqp_connection_info *parsed); +RABBITMQ_EXPORT int amqp_parse_url(char *url, + struct amqp_connection_info *parsed); + #ifdef __cplusplus } #endif diff --git a/librabbitmq/amqp_api.c b/librabbitmq/amqp_api.c index 4eb7677..d92cccd 100644 --- a/librabbitmq/amqp_api.c +++ b/librabbitmq/amqp_api.c @@ -58,6 +58,7 @@ static const char *client_error_strings[ERROR_MAX] = { "unknown host", /* ERROR_GETHOSTBYNAME_FAILED */ "incompatible AMQP version", /* ERROR_INCOMPATIBLE_AMQP_VERSION */ "connection closed unexpectedly", /* ERROR_CONNECTION_CLOSED */ + "could not parse AMQP URL", /* ERROR_BAD_AMQP_URL */ }; char *amqp_error_string(int err) diff --git a/librabbitmq/amqp_private.h b/librabbitmq/amqp_private.h index 6c15383..e019598 100644 --- a/librabbitmq/amqp_private.h +++ b/librabbitmq/amqp_private.h @@ -60,7 +60,8 @@ #define ERROR_GETHOSTBYNAME_FAILED 5 #define ERROR_INCOMPATIBLE_AMQP_VERSION 6 #define ERROR_CONNECTION_CLOSED 7 -#define ERROR_MAX 7 +#define ERROR_BAD_AMQP_URL 8 +#define ERROR_MAX 8 extern char *amqp_os_error_string(int err); diff --git a/librabbitmq/amqp_url.c b/librabbitmq/amqp_url.c new file mode 100644 index 0000000..ba1c4f1 --- /dev/null +++ b/librabbitmq/amqp_url.c @@ -0,0 +1,205 @@ +/* + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0 + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (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.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and + * limitations under the License. + * + * The Original Code is librabbitmq. + * + * The Initial Developer of the Original Code is VMware, Inc. + * Portions created by VMware are Copyright (c) 2007-2011 VMware, Inc. + * + * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 + * VMware, Inc. and Tony Garnock-Jones. + * + * All rights reserved. + * + * Alternatively, the contents of this file may be used under the terms + * of the GNU General Public License Version 2 or later (the "GPL"), in + * which case the provisions of the GPL are applicable instead of those + * above. If you wish to allow use of your version of this file only + * under the terms of the GPL, and not to allow others to use your + * version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the + * notice and other provisions required by the GPL. If you do not + * delete the provisions above, a recipient may use your version of + * this file under the terms of any one of the MPL or the GPL. + * + * ***** END LICENSE BLOCK ***** + */ + +#include +#include +#include +#include + +#include "amqp.h" +#include "amqp_framing.h" +#include "amqp_private.h" + +void amqp_default_connection_info(struct amqp_connection_info *ci) +{ + /* Apply defaults */ + ci->user = "guest"; + ci->password = "guest"; + ci->host = "localhost"; + ci->port = 5672; + ci->vhost = "/"; +} + +/* Scan for the next delimiter, handling percent-encodings on the way. */ +static char find_delim(char **pp, int colon_and_at_sign_are_delims) +{ + char *from = *pp; + char *to = from; + + for (;;) { + char ch = *from++; + + switch (ch) { + case ':': + case '@': + if (!colon_and_at_sign_are_delims) { + *to++ = ch; + break; + } + + /* fall through */ + case 0: + case '/': + case '?': + case '#': + case '[': + case ']': + *to = 0; + *pp = from; + return ch; + + case '%': { + unsigned int val; + int chars; + int res = sscanf(from, "%2x%n", &val, &chars); + + if (res == EOF || res < 1 || chars != 2) + /* Return a surprising delimiter to + force an error. */ + return '%'; + + *to++ = val; + from += 2; + break; + } + + default: + *to++ = ch; + break; + } + } +} + +/* Parse an AMQP URL into its component parts. */ +int amqp_parse_url(char *url, struct amqp_connection_info *parsed) +{ + int res = -ERROR_BAD_AMQP_URL; + char delim; + char *start; + char *host; + char *port = NULL; + + /* check the prefix */ + if (strncmp(url, "amqp://", 7)) + goto out; + + host = start = url += 7; + delim = find_delim(&url, 1); + + if (delim == ':') { + /* The colon could be introducing the port or the + password part of the userinfo. We don't know yet, + so stash the preceding component. */ + port = start = url; + delim = find_delim(&url, 1); + } + + if (delim == '@') { + /* What might have been the host and port were in fact + the username and password */ + parsed->user = host; + if (port) + parsed->password = port; + + port = NULL; + host = start = url; + delim = find_delim(&url, 1); + } + + if (delim == '[') { + /* IPv6 address. The bracket should be the first + character in the host. */ + if (host != start || *host != 0) + goto out; + + start = url; + delim = find_delim(&url, 0); + + if (delim != ']') + goto out; + + parsed->host = start; + start = url; + delim = find_delim(&url, 1); + + /* Closing bracket should be the last character in the + host. */ + if (*start != 0) + goto out; + } + else { + /* If we haven't seen the host yet, this is it. */ + if (*host != 0) + parsed->host = host; + } + + if (delim == ':') { + port = start = url; + delim = find_delim(&url, 1); + } + + if (port) { + char *end; + long portnum = strtol(port, &end, 10); + + if (port == end || *end != 0 || portnum < 0 || portnum > 65535) + goto out; + + parsed->port = portnum; + } + + if (delim == '/') { + start = url; + delim = find_delim(&url, 1); + + if (delim != 0) + goto out; + + parsed->vhost = start; + res = 0; + } + else if (delim == 0) { + res = 0; + } + + /* Any other delimiter is bad, and we will return + ERROR_BAD_AMQP_URL. */ + + out: + return res; +} diff --git a/tests/Makefile.am b/tests/Makefile.am index c6c4ed9..ac694dc 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -check_PROGRAMS = test_tables +check_PROGRAMS = test_tables test_parse_url TESTS = $(check_PROGRAMS) EXTRA_DIST = test_tables.expected diff --git a/tests/test_parse_url.c b/tests/test_parse_url.c new file mode 100644 index 0000000..983d820 --- /dev/null +++ b/tests/test_parse_url.c @@ -0,0 +1,174 @@ +/* + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0 + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (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.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and + * limitations under the License. + * + * The Original Code is librabbitmq. + * + * The Initial Developer of the Original Code is VMware, Inc. + * Portions created by VMware are Copyright (c) 2007-2011 VMware, Inc. + * + * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 + * VMware, Inc. and Tony Garnock-Jones. + * + * All rights reserved. + * + * Alternatively, the contents of this file may be used under the terms + * of the GNU General Public License Version 2 or later (the "GPL"), in + * which case the provisions of the GPL are applicable instead of those + * above. If you wish to allow use of your version of this file only + * under the terms of the GPL, and not to allow others to use your + * version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the + * notice and other provisions required by the GPL. If you do not + * delete the provisions above, a recipient may use your version of + * this file under the terms of any one of the MPL or the GPL. + * + * ***** END LICENSE BLOCK ***** + */ + +#include "config.h" + +#include +#include +#include + +#include + +#include + +static void match_string(const char *what, const char *expect, const char *got) +{ + if (strcmp(got, expect)) { + fprintf(stderr, "Expected %s '%s', got '%s'\n", + what, expect, got); + abort(); + } +} + +static void match_int(const char *what, int expect, int got) +{ + if (got != expect) { + fprintf(stderr, "Expected %s '%d', got '%d'\n", + what, expect, got); + abort(); + } +} + +static void parse_success(const char *url, + const char *user, + const char *password, + const char *host, + int port, + const char *vhost) +{ + char *s = strdup(url); + struct amqp_connection_info ci; + int res; + + amqp_default_connection_info(&ci); + res = amqp_parse_url(s, &ci); + if (res) { + fprintf(stderr, + "Expected to successfully parse URL, but didn't: %s (%s)\n", + url, amqp_error_string(-res)); + abort(); + } + + match_string("user", user, ci.user); + match_string("password", password, ci.password); + match_string("host", host, ci.host); + match_int("port", port, ci.port); + match_string("vhost", vhost, ci.vhost); + + free(s); +} + +static void parse_fail(const char *url) +{ + char *s = strdup(url); + struct amqp_connection_info ci; + + amqp_default_connection_info(&ci); + if (amqp_parse_url(s, &ci) >= 0) { + fprintf(stderr, + "Expected to fail parsing URL, but didn't: %s\n", + url); + abort(); + } + + free(s); +} + +int main(int argc, char **argv) +{ + /* From the spec */ + parse_success("amqp://user:pass@host:10000/vhost", "user", "pass", + "host", 10000, "vhost"); + parse_success("amqp://user%61:%61pass@ho%61st:10000/v%2fhost", + "usera", "apass", "hoast", 10000, "v/host"); + parse_success("amqp://", "guest", "guest", "localhost", 5672, "/"); + parse_success("amqp://:@/", "", "", "localhost", 5672, ""); + parse_success("amqp://user@", "user", "guest", "localhost", 5672, "/"); + parse_success("amqp://user:pass@", "user", "pass", + "localhost", 5672, "/"); + parse_success("amqp://host", "guest", "guest", "host", 5672, "/"); + parse_success("amqp://:10000", "guest", "guest", "localhost", 10000, + "/"); + parse_success("amqp:///vhost", "guest", "guest", "localhost", 5672, + "vhost"); + parse_success("amqp://host/", "guest", "guest", "host", 5672, ""); + parse_success("amqp://host/%2f", "guest", "guest", "host", 5672, "/"); + parse_success("amqp://[::1]", "guest", "guest", "::1", 5672, "/"); + + /* Various other success cases */ + parse_success("amqp://host:100", "guest", "guest", "host", 100, "/"); + parse_success("amqp://[::1]:100", "guest", "guest", "::1", 100, "/"); + + parse_success("amqp://host/blah", "guest", "guest", + "host", 5672, "blah"); + parse_success("amqp://host:100/blah", "guest", "guest", + "host", 100, "blah"); + parse_success("amqp://:100/blah", "guest", "guest", + "localhost", 100, "blah"); + parse_success("amqp://[::1]/blah", "guest", "guest", + "::1", 5672, "blah"); + parse_success("amqp://[::1]:100/blah", "guest", "guest", + "::1", 100, "blah"); + + parse_success("amqp://user:pass@host", "user", "pass", + "host", 5672, "/"); + parse_success("amqp://user:pass@host:100", "user", "pass", + "host", 100, "/"); + parse_success("amqp://user:pass@:100", "user", "pass", + "localhost", 100, "/"); + parse_success("amqp://user:pass@[::1]", "user", "pass", + "::1", 5672, "/"); + parse_success("amqp://user:pass@[::1]:100", "user", "pass", + "::1", 100, "/"); + + /* Various failure cases */ + parse_fail("http://www.rabbitmq.com"); + parse_fail("amqp://foo:bar:baz"); + parse_fail("amqp://foo[::1]"); + parse_fail("amqp://foo:[::1]"); + parse_fail("amqp://[::1]foo"); + parse_fail("amqp://foo:1000xyz"); + parse_fail("amqp://foo:1000000"); + parse_fail("amqp://foo/bar/baz"); + + parse_fail("amqp://foo%1"); + parse_fail("amqp://foo%1x"); + parse_fail("amqp://foo%xy"); + + return 0; +} -- cgit v1.2.1