krummas / DrizzleJDBC

A BSD licensed JDBC driver for Drizzle and MySQL
BSD 3-Clause "New" or "Revised" License
31 stars 22 forks source link

IPv6 addresses not accepted by Drizzle-JDBC #10

Closed misambart closed 11 years ago

misambart commented 11 years ago

Hi,

I have recently started testing Drizzle-JDBC using IPv6 addresses and this results in an exception in the parse(String url) method of org.drizzle.jdbc.JDBCUrl. This is caused by the fact that IPv6 addresses contain ":" characters.

I have prepared and tested a fix for this issue. As I do not have GIT configured on my machine and the change is relatively small, it seemed simpler to post the suggested code change directly here.

The code change allows IPv6 addresses when placed between square brackets (this matches general URL formatting standards for IPv6 addresses): jdbc:drizzle://[IPv6Address]:3306/database_name

For example, with the IPv6 localhost address, this would look like this: jdbc:drizzle://[::1]:3306/database_name

Here is the source code related to the issue before and after the suggested change:

Old code: int hostPortDividerIndex = url.indexOf(":"); if (hostPortDividerIndex == -1) { int slashIndex = url.indexOf("/"); hostname = url.substring(0, slashIndex); url = url.substring(slashIndex+1); } else { hostname = url.substring(0, hostPortDividerIndex); url = url.substring(hostPortDividerIndex+1); int slashIndex = url.indexOf("/"); port = Integer.parseInt(url.substring(0, slashIndex)); url = url.substring(slashIndex+1); } int slashIndex = url.indexOf("/"); if (slashIndex == -1) { database = url; } else { database = url.substring(0, slashIndex); }

New code: int slashIndex = url.indexOf("/"); String hostPortCombo = url.substring(0, slashIndex); url = url.substring(slashIndex + 1); int ipv6StartIndex = hostPortCombo.indexOf("["); int ipv6EndIndex = hostPortCombo.indexOf("]"); if (ipv6StartIndex >= 0 && ipv6EndIndex > ipv6StartIndex) { hostname = hostPortCombo.substring(ipv6StartIndex + 1, ipv6EndIndex); int hostPortDividerIndex = hostPortCombo.indexOf(":", ipv6EndIndex + 1); if (hostPortDividerIndex != -1) { port = Integer.parseInt(hostPortCombo.substring(hostPortDividerIndex + 1)); } } else { int hostPortDividerIndex = hostPortCombo.indexOf(":"); if (hostPortDividerIndex == -1) { hostname = hostPortCombo; } else { hostname = hostPortCombo.substring(0, hostPortDividerIndex); port = Integer.parseInt(hostPortCombo.substring(hostPortDividerIndex + 1)); } } slashIndex = url.indexOf("/"); if (slashIndex == -1) { database = url; } else { database = url.substring(0, slashIndex); }