changeset 131:d33529a2f6e9

Fix TW-13768: underscore in hostname are allowed.
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Thu, 14 Oct 2010 15:20:12 +0400
parents fc86dd075156
children 36aa8dfa9aab
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SettingsTest.java
diffstat 2 files changed, 44 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java	Thu Oct 14 12:45:54 2010 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java	Thu Oct 14 15:20:12 2010 +0400
@@ -24,8 +24,10 @@
 import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -108,11 +110,8 @@
     if (isRequireCredentials()) {
       if (containsCredentials(myRepository)) return myRepository;
       try {
-        URI repoURI = createUriWithCredentials(myRepository);
-        if (AUTH_PROTOS.contains(repoURI.getScheme())) {
-          return repoURI.toASCIIString();
-        }
-      } catch (URISyntaxException e) {
+        return createURLWithCredentials(myRepository);
+      } catch (MalformedURLException e) {
         Loggers.VCS.warn("Error while parsing url " + myRepository, e);
       }
       return myRepository;
@@ -123,28 +122,27 @@
 
   private boolean containsCredentials(final String repository) {
     try {
-      URI repositoryURI = new URI(repository);
-      String scheme = repositoryURI.getScheme();
-      if (AUTH_PROTOS.contains(scheme)) {
-        String userInfo = repositoryURI.getUserInfo();
-        return userInfo != null && userInfo.contains(":");
-      } else {
-        return false;
-      }
-    } catch (URISyntaxException e) {
+      URL url = new URL(repository);
+      String userInfo = url.getUserInfo();
+      return userInfo != null && userInfo.contains(":");
+    } catch (MalformedURLException e) {
       return false;
     }
   }
 
-  private URI createUriWithCredentials(final String uri) throws URISyntaxException {
-    URI repositoryURI = new URI(uri);
-    return new URI(repositoryURI.getScheme(),
-            createUserInfo(),
-            repositoryURI.getHost(),
-            repositoryURI.getPort(),
-            repositoryURI.getPath(),
-            repositoryURI.getQuery(),
-            repositoryURI.getFragment());
+  private String createURLWithCredentials(final String originalUrl) throws MalformedURLException {
+    String userInfo = createUserInfo();
+    if (!"".equals(userInfo)) {
+      URL url = new URL(originalUrl);
+      return url.getProtocol() + "://"
+              + userInfo + "@"
+              + url.getHost()
+              + (url.getPort() != -1 ? ":" + url.getPort() : "")
+              + url.getFile()
+              + (url.getRef() != null ? url.getRef() : "");
+    } else {
+      return originalUrl;
+    }
   }
 
   private boolean isRequireCredentials() {
@@ -164,6 +162,19 @@
         userInfo += ":" + myPassword;
       }
     }
+    return getEscapedUserInfo(userInfo);
+  }
+
+  private static String getEscapedUserInfo(String userInfo) {
+    try {
+      URI uri = new URI("http", userInfo, "somewhere.com", 80, "", "", "");
+      String escapedURI = uri.toASCIIString();
+      int from = "http://".length();
+      int to = escapedURI.indexOf("somewhere.com") - 1;
+      return escapedURI.substring(from, to);
+    } catch (URISyntaxException e) {
+      assert false;
+    }
     return userInfo;
   }
 
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SettingsTest.java	Thu Oct 14 12:45:54 2010 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SettingsTest.java	Thu Oct 14 15:20:12 2010 +0400
@@ -57,15 +57,20 @@
     assertEquals("http://my.name%40gmail.com:1234@host.com/path", settings.getRepositoryUrl());
   }
 
-	/**
-	 * TW-13768
-	 */
-	public void test_url_without_host_error() {
+  /** TW-13768 */
+  public void test_underscore_in_host() {
 		VcsRootImpl vcsRoot = createVcsRoot("http://Klekovkin.SDK_GARANT:8000/", "my.name@gmail.com", "1234");
 		Settings settings = new Settings(new File("."), vcsRoot);
-		assertEquals("http://Klekovkin.SDK_GARANT:8000/", settings.getRepositoryUrl());
+		assertEquals("http://my.name%40gmail.com:1234@Klekovkin.SDK_GARANT:8000/", settings.getRepositoryUrl());
 	}
 
+  /** TW-13768 */
+  public void test_underscore_in_host_with_credentials_in_url() {
+    VcsRootImpl vcsRoot = createVcsRoot("http://me:mypass@Klekovkin.SDK_GARANT:8000/");
+		Settings settings = new Settings(new File("."), vcsRoot);
+		assertEquals("http://me:mypass@Klekovkin.SDK_GARANT:8000/", settings.getRepositoryUrl());
+  }
+
   public void test_windows_path() throws Exception {
     VcsRootImpl vcsRoot = createVcsRoot("c:\\windows\\path");
     Settings settings = new Settings(new File("."), vcsRoot);