Skip to content

Commit

Permalink
Fail properly when a port is out of range.
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Feb 23, 2015
1 parent 57efb6d commit c90f249
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 10 additions & 1 deletion okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.UnknownServiceException;
import java.net.URL;
import java.net.UnknownServiceException;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -154,6 +154,15 @@ public final class CallTest {
}
}

@Test public void invalidPort() throws Exception {
Request request = new Request.Builder()
.url("http://localhost:65536/")
.build();
client.newCall(request).enqueue(callback);
callback.await(request.url())
.assertFailure("No route to localhost:65536; port is out of range");
}

@Test public void getReturns500() throws Exception {
server.enqueue(new MockResponse().setResponseCode(500));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -196,7 +195,7 @@ private Proxy nextProxy() throws IOException {
}

/** Prepares the socket addresses to attempt for the current proxy or host. */
private void resetNextInetSocketAddress(Proxy proxy) throws UnknownHostException {
private void resetNextInetSocketAddress(Proxy proxy) throws IOException {
// Clear the addresses. Necessary if getAllByName() below throws!
inetSocketAddresses = new ArrayList<>();

Expand All @@ -216,6 +215,11 @@ private void resetNextInetSocketAddress(Proxy proxy) throws UnknownHostException
socketPort = proxySocketAddress.getPort();
}

if (socketPort < 1 || socketPort > 65535) {
throw new SocketException("No route to " + socketHost + ":" + socketPort
+ "; port is out of range");
}

// Try each address for best behavior in mixed IPv4/IPv6 environments.
for (InetAddress inetAddress : network.resolveInetAddresses(socketHost)) {
inetSocketAddresses.add(new InetSocketAddress(inetAddress, socketPort));
Expand Down

0 comments on commit c90f249

Please sign in to comment.