Skip to content

Commit

Permalink
Fix parsing of hostname/ip in ES 2.x
Browse files Browse the repository at this point in the history
fix #614
  • Loading branch information
costin committed Dec 3, 2015
1 parent 8d82071 commit 98ab152
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Expand Up @@ -412,12 +412,15 @@ public static char[] jsonEncoding(String rawString) {
public static IpAndPort parseIpAddress(String httpAddr) {
// strip ip address - regex would work but it's overkill

// there are two formats - ip:port or [/ip:port]
// there are four formats - ip:port, hostname/ip:port or [/ip:port] and [hostname/ip:port]
// first the ip is normalized
if (httpAddr.contains("[")) {
if (httpAddr.contains("/")) {
int startIp = httpAddr.indexOf("/") + 1;
int endIp = httpAddr.indexOf("]");
if (startIp < 0 || endIp < 0) {
if (endIp < 0) {
endIp = httpAddr.length();
}
if (startIp < 0) {
throw new EsHadoopIllegalStateException("Cannot parse http address " + httpAddr);
}
httpAddr = httpAddr.substring(startIp, endIp);
Expand Down
Expand Up @@ -20,7 +20,7 @@

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

public class StringUtilsTest {

Expand All @@ -30,4 +30,25 @@ public void testJsonEncodingQuote() {
String jsonEscaped = "foo\\\"bar";
assertEquals(jsonEscaped, StringUtils.jsonEncoding(value));
}

@Test
public void testParseIpInEs1x() {
assertEquals("1.2.3.4", StringUtils.parseIpAddress("inet[/1.2.3.4:9200]").ip);
}

@Test
public void testParseIpInEs1xWithHostName() {
assertEquals("11.22.33.44", StringUtils.parseIpAddress("inet[foobar/11.22.33.44:9200]").ip);
}

@Test
public void testParseIpInEs2x() {
assertEquals("111.222.333.444", StringUtils.parseIpAddress("111.222.333.444:9200").ip);
}

@Test
public void testParseIpInEs2xWithHostName() {
assertEquals("11.222.3.4", StringUtils.parseIpAddress("foobar/11.222.3.4:9200").ip);
}

}

0 comments on commit 98ab152

Please sign in to comment.