diff options
author | Michael Steinert <mike.steinert@gmail.com> | 2012-05-24 19:14:45 -0600 |
---|---|---|
committer | Michael Steinert <mike.steinert@gmail.com> | 2012-05-27 10:39:57 -0600 |
commit | 5a76dd62f8579a3341eafb3bfd3197118d73886f (patch) | |
tree | 8a6c35d19a16779d74dc2068c911bde210803393 | |
parent | 8aa339ebfec12b68ce78445137e2cfb7cbc6c272 (diff) | |
download | rabbitmq-c-github-ask-5a76dd62f8579a3341eafb3bfd3197118d73886f.tar.gz |
Add amqps:// support to the URL parser
Signed-off-by: Michael Steinert <mike.steinert@gmail.com>
-rw-r--r-- | librabbitmq/amqp.h | 2 | ||||
-rw-r--r-- | librabbitmq/amqp_url.c | 11 | ||||
-rw-r--r-- | tests/test_parse_url.c | 81 | ||||
-rw-r--r-- | tools/common.c | 18 |
4 files changed, 106 insertions, 6 deletions
diff --git a/librabbitmq/amqp.h b/librabbitmq/amqp.h index 287a55f..31105ec 100644 --- a/librabbitmq/amqp.h +++ b/librabbitmq/amqp.h @@ -120,6 +120,7 @@ struct iovec; # define AMQP_CALL #endif +#include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -564,6 +565,7 @@ struct amqp_connection_info { char *host; char *vhost; int port; + bool ssl; }; AMQP_PUBLIC_FUNCTION diff --git a/librabbitmq/amqp_url.c b/librabbitmq/amqp_url.c index eb29ca8..73ca4f6 100644 --- a/librabbitmq/amqp_url.c +++ b/librabbitmq/amqp_url.c @@ -48,6 +48,7 @@ void amqp_default_connection_info(struct amqp_connection_info *ci) ci->host = "localhost"; ci->port = 5672; ci->vhost = "/"; + ci->ssl = false; } /* Scan for the next delimiter, handling percent-encodings on the way. */ @@ -110,10 +111,16 @@ int amqp_parse_url(char *url, struct amqp_connection_info *parsed) char *port = NULL; /* check the prefix */ - if (strncmp(url, "amqp://", 7)) + if (!strncmp(url, "amqp://", 7)) { + /* do nothing */ + } else if (!strncmp(url, "amqps://", 8)) { + parsed->port = 5671; + parsed->ssl = true; + } else { goto out; + } - host = start = url += 7; + host = start = url += (parsed->ssl ? 8 : 7); delim = find_delim(&url, 1); if (delim == ':') { diff --git a/tests/test_parse_url.c b/tests/test_parse_url.c index a4c5f09..f2382d4 100644 --- a/tests/test_parse_url.c +++ b/tests/test_parse_url.c @@ -108,61 +108,142 @@ int main(void) /* From the spec */ parse_success("amqp://user:pass@host:10000/vhost", "user", "pass", "host", 10000, "vhost"); + parse_success("amqps://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("amqps://user%61:%61pass@ho%61st:10000/v%2fhost", + "usera", "apass", "hoast", 10000, "v/host"); + parse_success("amqp://", "guest", "guest", "localhost", 5672, "/"); + parse_success("amqps://", "guest", "guest", "localhost", 5671, "/"); + parse_success("amqp://:@/", "", "", "localhost", 5672, ""); + parse_success("amqps://:@/", "", "", "localhost", 5671, ""); + parse_success("amqp://user@", "user", "guest", "localhost", 5672, "/"); + parse_success("amqps://user@", "user", "guest", "localhost", 5671, "/"); + parse_success("amqp://user:pass@", "user", "pass", "localhost", 5672, "/"); + parse_success("amqps://user:pass@", "user", "pass", + "localhost", 5671, "/"); + parse_success("amqp://host", "guest", "guest", "host", 5672, "/"); + parse_success("amqps://host", "guest", "guest", "host", 5671, "/"); + parse_success("amqp://:10000", "guest", "guest", "localhost", 10000, "/"); + parse_success("amqps://:10000", "guest", "guest", "localhost", 10000, + "/"); + parse_success("amqp:///vhost", "guest", "guest", "localhost", 5672, "vhost"); + parse_success("amqps:///vhost", "guest", "guest", "localhost", 5671, + "vhost"); + parse_success("amqp://host/", "guest", "guest", "host", 5672, ""); + parse_success("amqps://host/", "guest", "guest", "host", 5671, ""); + parse_success("amqp://host/%2f", "guest", "guest", "host", 5672, "/"); + parse_success("amqps://host/%2f", "guest", "guest", "host", 5671, "/"); + parse_success("amqp://[::1]", "guest", "guest", "::1", 5672, "/"); + parse_success("amqps://[::1]", "guest", "guest", "::1", 5671, "/"); /* Various other success cases */ parse_success("amqp://host:100", "guest", "guest", "host", 100, "/"); + parse_success("amqps://host:100", "guest", "guest", "host", 100, "/"); + parse_success("amqp://[::1]:100", "guest", "guest", "::1", 100, "/"); + parse_success("amqps://[::1]:100", "guest", "guest", "::1", 100, "/"); parse_success("amqp://host/blah", "guest", "guest", "host", 5672, "blah"); + parse_success("amqps://host/blah", "guest", "guest", + "host", 5671, "blah"); + parse_success("amqp://host:100/blah", "guest", "guest", "host", 100, "blah"); + parse_success("amqps://host:100/blah", "guest", "guest", + "host", 100, "blah"); + parse_success("amqp://:100/blah", "guest", "guest", "localhost", 100, "blah"); + parse_success("amqps://:100/blah", "guest", "guest", + "localhost", 100, "blah"); + parse_success("amqp://[::1]/blah", "guest", "guest", "::1", 5672, "blah"); + parse_success("amqps://[::1]/blah", "guest", "guest", + "::1", 5671, "blah"); + parse_success("amqp://[::1]:100/blah", "guest", "guest", "::1", 100, "blah"); + parse_success("amqps://[::1]:100/blah", "guest", "guest", + "::1", 100, "blah"); parse_success("amqp://user:pass@host", "user", "pass", "host", 5672, "/"); + parse_success("amqps://user:pass@host", "user", "pass", + "host", 5671, "/"); + parse_success("amqp://user:pass@host:100", "user", "pass", "host", 100, "/"); + parse_success("amqps://user:pass@host:100", "user", "pass", + "host", 100, "/"); + parse_success("amqp://user:pass@:100", "user", "pass", "localhost", 100, "/"); + parse_success("amqps://user:pass@:100", "user", "pass", + "localhost", 100, "/"); + parse_success("amqp://user:pass@[::1]", "user", "pass", "::1", 5672, "/"); + parse_success("amqps://user:pass@[::1]", "user", "pass", + "::1", 5671, "/"); + parse_success("amqp://user:pass@[::1]:100", "user", "pass", "::1", 100, "/"); + parse_success("amqps://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("amqps://foo:bar:baz"); + + parse_fail("amqp://foo[::1]"); + parse_fail("amqps://foo[::1]"); + parse_fail("amqp://foo[::1]"); + parse_fail("amqps://foo[::1]"); + parse_fail("amqp://foo:[::1]"); + parse_fail("amqps://foo:[::1]"); + parse_fail("amqp://[::1]foo"); + parse_fail("amqps://[::1]foo"); + parse_fail("amqp://foo:1000xyz"); + parse_fail("amqps://foo:1000xyz"); + parse_fail("amqp://foo:1000000"); + parse_fail("amqps://foo:1000000"); + parse_fail("amqp://foo/bar/baz"); + parse_fail("amqps://foo/bar/baz"); parse_fail("amqp://foo%1"); + parse_fail("amqps://foo%1"); + parse_fail("amqp://foo%1x"); + parse_fail("amqps://foo%1x"); + parse_fail("amqp://foo%xy"); + parse_fail("amqps://foo%xy"); return 0; } diff --git a/tools/common.c b/tools/common.c index 35e9a2e..9ac2747 100644 --- a/tools/common.c +++ b/tools/common.c @@ -215,7 +215,7 @@ static void init_connection_info(struct amqp_connection_info *ci) "Parsing URL '%s'", amqp_url); if (amqp_server) { - char *colon; + char *colon; if (ci->host) die("both --server and --url options specify" " server host"); @@ -253,6 +253,11 @@ static void init_connection_info(struct amqp_connection_info *ci) die("bad server port number in '%s'", amqp_server); } + + if (amqp_ssl && !ci->ssl) { + die("the --ssl option specifies an SSL connection" + " but the --server option does not"); + } } if (amqp_port >= 0) { @@ -287,6 +292,10 @@ static void init_connection_info(struct amqp_connection_info *ci) ci->vhost = amqp_vhost; } + if (amqp_ssl) { + ci->ssl = true; + } + amqp_default_connection_info(&defaults); if (!ci->user) @@ -309,13 +318,14 @@ amqp_connection_state_t make_connection(void) init_connection_info(&ci); conn = amqp_new_connection(); + if (ci.ssl) { #ifdef WITH_SSL - if (amqp_ssl) { s = amqp_open_ssl_socket(conn, ci.host, ci.port, amqp_cacert, amqp_key, amqp_cert); - } else +#else + die("librabbitmq was not built with SSL/TLS support"); #endif - { + } else { s = amqp_open_socket(ci.host, ci.port); amqp_set_sockfd(conn, s); } |