Skip to content

Check for null target in avoidProxy to prevent exceptions; Close #560. #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public final static URI createUri(String u) {
return URI.create(u + "/");
}

if (uri.getHost() == null) {
throw new IllegalArgumentException("The URI host, of the URI " + uri + ", must be non-null.");
}

return uri;
}

Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/org/asynchttpclient/util/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public static boolean avoidProxy(final ProxyServer proxyServer, final Request re
*/
public static boolean avoidProxy(final ProxyServer proxyServer, final String target) {
if (proxyServer != null) {
final String targetHost = target.toLowerCase(Locale.ENGLISH);
final String targetHost = (target != null) ?
target.toLowerCase(Locale.ENGLISH) : "";

List<String> nonProxyHosts = proxyServer.getNonProxyHosts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.asynchttpclient.util;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

import org.testng.annotations.Test;

Expand Down Expand Up @@ -44,4 +45,14 @@ public void getRedirectUriShouldHandleRelativeLocation() {
URI uri = AsyncHttpProviderUtils.getRedirectUri(URI.create("http://www.ebay.de"), url);
assertEquals(uri.toString(), "http://www.ebay.de/sch/sis.html;jsessionid=92D73F80262E3EBED7E115ED01035DDA?_nkw=FSC%20Lifebook%20E8310%20Core2Duo%20T8100%202%201GHz%204GB%20DVD%20RW&_itemId=150731406505");
}

@Test(groups = "fast")
public void createUriFailsWithBadHost() throws Exception {
try {
final String BAD_HOST_URL = "http:///";
URI uri = AsyncHttpProviderUtils.createUri(BAD_HOST_URL);
fail("Expected IllegalArgumentException for url: " + BAD_HOST_URL);
} catch (IllegalArgumentException ex) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ public void testBasics() {
proxyServer = new ProxyServer("foo", 1234);
proxyServer.addNonProxyHost("*.somewhere.org");
assertFalse(ProxyUtils.avoidProxy(proxyServer, req));

// shouldn't crash
req = new RequestBuilder("GET").setUrl("http:///badhost/foo").build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why shouldn't this crash?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is happening pretty deep in the guts of the code at the point where you already have the url and are trying to execute it (see partial stacktrace below). Ultimately, I'd be fine with dropping this change and test as long as the changes to AsyncHttpProviderUtils make it in, because then callers can check bad urls ahead of time. Would you honor the pull request if I dropped ProxyUtils*?

java.lang.NullPointerException
at com.ning.http.util.ProxyUtils.avoidProxy(ProxyUtils.java:101)
at com.ning.http.util.ProxyUtils.avoidProxy(ProxyUtils.java:86)
at com.ning.http.util.ProxyUtils.getProxyServer(ProxyUtils.java:72)
at
com.ning.http.client.providers.netty.NettyAsyncHttpProvider.doConnect(NettyAsyncHttpProvider.java:955)
at
com.ning.http.client.providers.netty.NettyAsyncHttpProvider.execute(NettyAsyncHttpProvider.java:937)
at
com.ning.http.client.AsyncHttpClient.executeRequest(AsyncHttpClient.java:520)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my pov, this should crash as soon as possible, meaning in setUrl where the illegal url is passed.

proxyServer = new ProxyServer("foo", 1234);
proxyServer.addNonProxyHost("*.somewhere.org");
assertFalse(ProxyUtils.avoidProxy(proxyServer, req));
}
}