Browse Source

URIish: support for empty ports

Properly parse URLs like "ssh://host:/path"

Bug: 519187
Change-Id: I0054868e30509e4ba919444be16c2a20f741545a
Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
stable-4.10
Marc Strapetz 7 years ago committed by Matthias Sohn
parent
commit
8cc783ca7d
  1. 23
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java
  2. 24
      org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

23
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/URIishTest.java

@ -502,6 +502,22 @@ public class URIishTest {
assertEquals(u, new URIish(str)); assertEquals(u, new URIish(str));
} }
@Test
public void testSshProtoHostWithEmptyPortAndPath() throws Exception {
final String str = "ssh://example.com:/path";
URIish u = new URIish(str);
assertEquals("ssh", u.getScheme());
assertTrue(u.isRemote());
assertEquals("/path", u.getRawPath());
assertEquals("/path", u.getPath());
assertEquals("example.com", u.getHost());
assertEquals(-1, u.getPort());
assertEquals("ssh://example.com/path", u.toString());
assertEquals("ssh://example.com/path", u.toASCIIString());
assertEquals(u, new URIish(str));
assertEquals(u, new URIish("ssh://example.com/path"));
}
@Test @Test
public void testSshProtoWithUserAndPort() throws Exception { public void testSshProtoWithUserAndPort() throws Exception {
final String str = "ssh://user@example.com:33/some/p ath"; final String str = "ssh://user@example.com:33/some/p ath";
@ -972,13 +988,6 @@ public class URIishTest {
assertEquals("b.txt", u.getHumanishName()); assertEquals("b.txt", u.getHumanishName());
} }
@Test
public void testMissingPort() throws URISyntaxException {
final String incorrectSshUrl = "ssh://some-host:/path/to/repository.git";
URIish u = new URIish(incorrectSshUrl);
assertFalse(TransportGitSsh.PROTO_SSH.canHandle(u));
}
@Test @Test
public void testALot() throws URISyntaxException { public void testALot() throws URISyntaxException {
// user pass host port path // user pass host port path

24
org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java

@ -95,7 +95,7 @@ public class URIish implements Serializable {
* Part of a pattern which matches the optional port part of URIs. Defines * Part of a pattern which matches the optional port part of URIs. Defines
* one capturing group containing the port without the preceding colon. * one capturing group containing the port without the preceding colon.
*/ */
private static final String OPT_PORT_P = "(?::(\\d+))?"; //$NON-NLS-1$ private static final String OPT_PORT_P = "(?::(\\d*))?"; //$NON-NLS-1$
/** /**
* Part of a pattern which matches the ~username part (e.g. /~root in * Part of a pattern which matches the ~username part (e.g. /~root in
@ -224,11 +224,23 @@ public class URIish implements Serializable {
scheme = matcher.group(1); scheme = matcher.group(1);
user = unescape(matcher.group(2)); user = unescape(matcher.group(2));
pass = unescape(matcher.group(3)); pass = unescape(matcher.group(3));
host = unescape(matcher.group(4)); // empty ports are in general allowed, except for URLs like
if (matcher.group(5) != null) // file://D:/path for which it is more desirable to parse with
port = Integer.parseInt(matcher.group(5)); // host=null and path=D:/path
rawPath = cleanLeadingSlashes( String portString = matcher.group(5);
n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme); if ("file".equals(scheme) && "".equals(portString)) { //$NON-NLS-1$ //$NON-NLS-2$
rawPath = cleanLeadingSlashes(
n2e(matcher.group(4)) + ":" + portString //$NON-NLS-1$
+ n2e(matcher.group(6)) + n2e(matcher.group(7)),
scheme);
} else {
host = unescape(matcher.group(4));
if (portString != null && portString.length() > 0) {
port = Integer.parseInt(portString);
}
rawPath = cleanLeadingSlashes(
n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
}
path = unescape(rawPath); path = unescape(rawPath);
return; return;
} }

Loading…
Cancel
Save