summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java69
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java5
2 files changed, 73 insertions, 1 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java
new file mode 100644
index 0000000000..cbe4eb2eb0
--- /dev/null
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2019, Thomas Wolf <thomas.wolf@paranor.ch> and others
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+package org.eclipse.jgit.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
+import java.net.URI;
+import java.net.URL;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Test;
+
+public class HttpSupportTest {
+
+ private static class TestProxySelector extends ProxySelector {
+
+ private static final Proxy DUMMY = new Proxy(Proxy.Type.HTTP,
+ InetSocketAddress.createUnresolved("localhost", 0));
+
+ @Override
+ public List<Proxy> select(URI uri) {
+ if ("http".equals(uri.getScheme())
+ && "somehost".equals(uri.getHost())) {
+ return Collections.singletonList(DUMMY);
+ }
+ return Collections.singletonList(Proxy.NO_PROXY);
+ }
+
+ @Override
+ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+ // Empty
+ }
+ }
+
+ @Test
+ public void testMalformedUri() throws Exception {
+ // Valid URL, but backslash is not allowed in a URI in the userinfo part
+ // per RFC 3986: https://tools.ietf.org/html/rfc3986#section-3.2.1 .
+ // Test that conversion to URI to call the ProxySelector does not throw
+ // an exception.
+ Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL(
+ "http://infor\\c.jones@somehost/somewhere/someproject.git"));
+ assertNotNull(proxy);
+ assertEquals(Proxy.Type.HTTP, proxy.type());
+ }
+
+ @Test
+ public void testCorrectUri() throws Exception {
+ // Backslash escaped as %5C is correct.
+ Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL(
+ "http://infor%5Cc.jones@somehost/somewhere/someproject.git"));
+ assertNotNull(proxy);
+ assertEquals(Proxy.Type.HTTP, proxy.type());
+ }
+}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java
index 640670debc..d897255062 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/HttpSupport.java
@@ -51,6 +51,7 @@ import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.Proxy;
import java.net.ProxySelector;
+import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
@@ -299,7 +300,9 @@ public class HttpSupport {
public static Proxy proxyFor(ProxySelector proxySelector, URL u)
throws ConnectException {
try {
- return proxySelector.select(u.toURI()).get(0);
+ URI uri = new URI(u.getProtocol(), null, u.getHost(), u.getPort(),
+ null, null, null);
+ return proxySelector.select(uri).get(0);
} catch (URISyntaxException e) {
final ConnectException err;
err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u));