changeset 126:8eab713b01a4

Create repositoryUrl using java.util.URI (it escapes illegal characters)
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Tue, 28 Sep 2010 13:09:04 +0400
parents a13422ae169d
children 2159ab641618
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java
diffstat 1 files changed, 32 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java	Tue Sep 28 12:49:37 2010 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java	Tue Sep 28 13:09:04 2010 +0400
@@ -94,9 +94,9 @@
 
   private final static Set<String> AUTH_PROTOS = new HashSet<String>();
   static {
-    AUTH_PROTOS.add("http://");
-    AUTH_PROTOS.add("https://");
-    AUTH_PROTOS.add("ssh://");
+    AUTH_PROTOS.add("http");
+    AUTH_PROTOS.add("https");
+    AUTH_PROTOS.add("ssh");
   }
 
   /**
@@ -106,28 +106,14 @@
   public String getRepositoryUrl() {
     if (containsCredentials(myRepository)) return myRepository;
 
-    for (String proto: AUTH_PROTOS) {
-      if (myRepository.startsWith(proto)) {
-        String repoUrl = myRepository.substring(proto.length());
-        int endIdx = repoUrl.indexOf('@');
-        int slashIdx = repoUrl.indexOf('/');
-        if (endIdx != -1 && slashIdx > endIdx) {
-          repoUrl = repoUrl.substring(endIdx+1);
-        }
-
-        String cre = "";
-        if (!StringUtil.isEmpty(myUsername)) {
-          cre += myUsername;
-          if (!StringUtil.isEmpty(myPassword)) {
-            cre += ":" + myPassword;
-          }
-          cre += "@";
-        }
-
-        return proto + cre + repoUrl;
+    try {
+      URI repoURI = createUriWithCredentials(myRepository);
+      if (AUTH_PROTOS.contains(repoURI.getScheme())) {
+        return repoURI.toASCIIString();
       }
+    } catch (URISyntaxException e) {
+      //ignore
     }
-
     return myRepository;
   }
 
@@ -135,7 +121,7 @@
     try {
       URI repositoryURI = new URI(repository);
       String scheme = repositoryURI.getScheme();
-      if (AUTH_PROTOS.contains(scheme + "://")) {
+      if (AUTH_PROTOS.contains(scheme)) {
         String userInfo = repositoryURI.getUserInfo();
         return userInfo != null && userInfo.contains(":");
       } else {
@@ -146,6 +132,28 @@
     }
   }
 
+  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 createUserInfo() {
+    String userInfo = "";
+    if (!StringUtil.isEmpty(myUsername)) {
+      userInfo += myUsername;
+      if (!StringUtil.isEmpty(myPassword)) {
+        userInfo += ":" + myPassword;
+      }
+    }
+    return userInfo;
+  }
+
   public void setHgCommandPath(@NotNull final String hgCommandPath) {
     myHgCommandPath = hgCommandPath;
   }