Skip to content

Handle basic query parameters in connection URI #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/com/rabbitmq/client/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ public void setUri(URI uri)

setVirtualHost(uriDecode(uri.getPath().substring(1)));
}

String rawQuery = uri.getRawQuery();
if (rawQuery != null && rawQuery.length() > 0) {
setQuery(rawQuery);
}
}

/**
Expand Down Expand Up @@ -377,6 +382,65 @@ private static String uriDecode(String s) {
}
}

/**
* Convenience method for setting some fields from query parameters
* Will handle only a subset of the query parameters supported by the
* official erlang client
* https://www.rabbitmq.com/uri-query-parameters.html
* @param rawQuery is the string containing the raw query parameters part from a URI
*/
private void setQuery(String rawQuery) {
Map<String, String> parameters = new HashMap<>();

// parsing the query parameters
try {
for (String param : rawQuery.split("&")) {
String[] pair = param.split("=");
String key = URLDecoder.decode(pair[0], "US-ASCII");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not use UTF-8 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same charset as in uriDecode line 378, which is used to decode username password and vhost.

    private static String uriDecode(String s) {
        try {
            // URLDecode decodes '+' to a space, as for
            // form encoding.  So protect plus signs.
            return URLDecoder.decode(s.replace("+", "%2B"), "US-ASCII");
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

https://github.com/rabbitmq/rabbitmq-java-client/pull/672/files#diff-045e321e0fb12f11085653a7ed09f16518f9f21a7c80990c30720958dc17e954R378

String value = null;
if (pair.length > 1) {
value = URLDecoder.decode(pair[1], "US-ASCII");
}
parameters.put(key, value);
}
} catch (IOException e) {
throw new RuntimeException("Cannot parse the query parameters", e);
}

// heartbeat
String heartbeat = parameters.get("heartbeat");
if (heartbeat != null) {
try {
int heartbeatInt = Integer.parseInt(heartbeat);
setRequestedHeartbeat(heartbeatInt);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Requested heartbeat must an integer");
}
}

// connection_timeout
String connectionTimeout = parameters.get("connection_timeout");
if (connectionTimeout != null) {
try {
int connectionTimeoutInt = Integer.parseInt(connectionTimeout);
setConnectionTimeout(connectionTimeoutInt);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("TCP connection timeout must an integer");
}
}

// channel_max
String channelMax = parameters.get("channel_max");
if (channelMax != null) {
try {
int channelMaxInt = Integer.parseInt(channelMax);
setRequestedChannelMax(channelMaxInt);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Requested channel max must an integer");
}
}
}

/**
* Retrieve the requested maximum channel number
* @return the initially requested maximum channel number; zero for unlimited
Expand Down
89 changes: 72 additions & 17 deletions src/test/java/com/rabbitmq/client/test/AmqpUriTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,63 @@ public class AmqpUriTest extends BrokerTestCase
{
/* From the spec (subset of the tests) */
parseSuccess("amqp://user:pass@host:10000/vhost",
"user", "pass", "host", 10000, "vhost");
"user", "pass", "host", 10000, "vhost", false);
parseSuccess("aMQps://user%61:%61pass@host:10000/v%2fhost",
"usera", "apass", "host", 10000, "v/host");
parseSuccess("amqp://host", "guest", "guest", "host", 5672, "/");
"usera", "apass", "host", 10000, "v/host", true);
parseSuccess("amqp://host", "guest", "guest", "host", 5672, "/", false);
parseSuccess("amqp:///vhost",
"guest", "guest", "localhost", 5672, "vhost");
parseSuccess("amqp://host/", "guest", "guest", "host", 5672, "");
parseSuccess("amqp://host/%2f", "guest", "guest", "host", 5672, "/");
parseSuccess("amqp://[::1]", "guest", "guest", "[::1]", 5672, "/");
"guest", "guest", "localhost", 5672, "vhost", false);
parseSuccess("amqp://host/", "guest", "guest", "host", 5672, "", false);
parseSuccess("amqp://host/%2f", "guest", "guest", "host", 5672, "/", false);
parseSuccess("amqp://[::1]", "guest", "guest", "[::1]", 5672, "/", false);

/* Various other success cases */
parseSuccess("amqp://host:100", "guest", "guest", "host", 100, "/");
parseSuccess("amqp://[::1]:100", "guest", "guest", "[::1]", 100, "/");
parseSuccess("amqp://host:100", "guest", "guest", "host", 100, "/", false);
parseSuccess("amqp://[::1]:100", "guest", "guest", "[::1]", 100, "/", false);

parseSuccess("amqp://host/blah",
"guest", "guest", "host", 5672, "blah");
"guest", "guest", "host", 5672, "blah", false);
parseSuccess("amqp://host:100/blah",
"guest", "guest", "host", 100, "blah");
"guest", "guest", "host", 100, "blah", false);
parseSuccess("amqp://[::1]/blah",
"guest", "guest", "[::1]", 5672, "blah");
"guest", "guest", "[::1]", 5672, "blah", false);
parseSuccess("amqp://[::1]:100/blah",
"guest", "guest", "[::1]", 100, "blah");
"guest", "guest", "[::1]", 100, "blah", false);

parseSuccess("amqp://user:pass@host",
"user", "pass", "host", 5672, "/");
"user", "pass", "host", 5672, "/", false);
parseSuccess("amqp://user:pass@[::1]",
"user", "pass", "[::1]", 5672, "/");
"user", "pass", "[::1]", 5672, "/", false);
parseSuccess("amqp://user:pass@[::1]:100",
"user", "pass", "[::1]", 100, "/");
"user", "pass", "[::1]", 100, "/", false);

/* using query parameters */
parseSuccess("amqp://user:pass@host:10000/vhost?",
"user", "pass", "host", 10000, "vhost", false);
parseSuccess("amqp://user:pass@host:10000/vhost?&",
"user", "pass", "host", 10000, "vhost", false);
parseSuccess("amqp://user:pass@host:10000/vhost?unknown_parameter",
"user", "pass", "host", 10000, "vhost", false);
parseSuccess("amqp://user:pass@host:10000/vhost?unknown_parameter=value",
"user", "pass", "host", 10000, "vhost", false);
parseSuccess("amqp://user:pass@host:10000/vhost?unknown%2fparameter=value",
"user", "pass", "host", 10000, "vhost", false);

parseSuccess("amqp://user:pass@host:10000/vhost?heartbeat=342",
"user", "pass", "host", 10000, "vhost", false,
342, null, null);
parseSuccess("amqp://user:pass@host:10000/vhost?connection_timeout=442",
"user", "pass", "host", 10000, "vhost", false,
null, 442, null);
parseSuccess("amqp://user:pass@host:10000/vhost?channel_max=542",
"user", "pass", "host", 10000, "vhost", false,
null, null, 542);
parseSuccess("amqp://user:pass@host:10000/vhost?heartbeat=342&connection_timeout=442&channel_max=542",
"user", "pass", "host", 10000, "vhost", false,
342, 442, 542);
parseSuccess("amqp://user:pass@host:10000/vhost?heartbeat=342&connection_timeout=442&channel_max=542&a=b",
"user", "pass", "host", 10000, "vhost", false,
342, 442, 542);

/* Various failure cases */
parseFail("https://www.rabbitmq.com");
Expand All @@ -71,10 +99,26 @@ public class AmqpUriTest extends BrokerTestCase
parseFail("amqp://foo%1");
parseFail("amqp://foo%1x");
parseFail("amqp://foo%xy");

parseFail("amqp://user:pass@host:10000/vhost?heartbeat=not_an_integer");
parseFail("amqp://user:pass@host:10000/vhost?heartbeat=-1");
parseFail("amqp://user:pass@host:10000/vhost?connection_timeout=not_an_integer");
parseFail("amqp://user:pass@host:10000/vhost?connection_timeout=-1");
parseFail("amqp://user:pass@host:10000/vhost?channel_max=not_an_integer");
parseFail("amqp://user:pass@host:10000/vhost?channel_max=-1");
parseFail("amqp://user:pass@host:10000/vhost?heartbeat=342?connection_timeout=442");
}

private void parseSuccess(String uri, String user, String password,
String host, int port, String vhost)
String host, int port, String vhost, boolean secured)
throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException
{
parseSuccess(uri, user, password, host, port, vhost, secured, null, null, null);
}

private void parseSuccess(String uri, String user, String password,
String host, int port, String vhost, boolean secured,
Integer heartbeat, Integer connectionTimeout, Integer channelMax)
throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException
{
ConnectionFactory cf = TestUtils.connectionFactory();
Expand All @@ -85,6 +129,17 @@ private void parseSuccess(String uri, String user, String password,
assertEquals(host, cf.getHost());
assertEquals(port, cf.getPort());
assertEquals(vhost, cf.getVirtualHost());
assertEquals(secured, cf.isSSL());

if(heartbeat != null) {
assertEquals(heartbeat.intValue(), cf.getRequestedHeartbeat());
}
if(connectionTimeout != null) {
assertEquals(connectionTimeout.intValue(), cf.getConnectionTimeout());
}
if(channelMax != null) {
assertEquals(channelMax.intValue(), cf.getRequestedChannelMax());
}
}

private void parseFail(String uri) {
Expand Down