HttpSupport.proxyFor(): use only scheme, host, and port

Some URLs cannot be converted via URL.toURI(). So don't convert
the full URL but only the bits that are needed to find a proxy
via java.net.ProxySelector.

Bug: 549690
Change-Id: I55b5ecee70c6b52f72f9bdba9ce552fde7f33976
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
Thomas Wolf 2019-07-31 19:40:40 +02:00
parent cf11a03bc2
commit b7d1eb4185
2 changed files with 73 additions and 1 deletions

View File

@ -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());
}
}

View File

@ -51,6 +51,7 @@
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 static String responseHeader(final HttpConnection c,
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));