Skip to content

Commit 7d7aa61

Browse files
committed
8218133: sun/net/www/protocol/http/ProtocolRedirect.java failed with "java.net.ConnectException"
Changed the test to use the loopback interface. Backport-of: d176e20
1 parent c58a066 commit 7d7aa61

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

test/jdk/sun/net/www/protocol/http/ProtocolRedirect.java

+42-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,24 +31,30 @@
3131

3232
public class ProtocolRedirect {
3333
public static void main(String [] args) throws Exception {
34-
int localPort;
35-
new Thread(new Redirect()).start();
36-
while ((localPort = Redirect.listenPort) == -1) {
37-
Thread.sleep(1000);
38-
}
34+
try (Redirect server = new Redirect(new ServerSocket())) {
35+
ServerSocket ss = server.ssock;
36+
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
37+
new Thread(server).start();
3938

40-
String page = "http://localhost:"+localPort+"/";
41-
URL url = new URL(page);
42-
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
43-
conn.connect();
44-
if (conn.getResponseCode() != 302) {
45-
throw new RuntimeException("Test failed. Should get RespCode: 302. Got:"+conn.getResponseCode());
39+
URL url = new URL("http", ss.getInetAddress().getHostAddress(), ss.getLocalPort(), "/");
40+
String page = url.toString();
41+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
42+
conn.connect();
43+
if (conn.getResponseCode() != 302) {
44+
System.err.println("Bad response code received from: " + page);
45+
throw new RuntimeException("Test failed. Should get RespCode: 302. Got:" + conn.getResponseCode());
46+
}
47+
System.out.println("Received expected response code from: " + page);
4648
}
4749
}
4850
}
4951

50-
class Redirect implements Runnable {
51-
public static int listenPort = -1; // port to listen for connections on
52+
class Redirect implements Runnable, Closeable {
53+
final ServerSocket ssock;
54+
volatile boolean stopped;
55+
Redirect(ServerSocket ss) {
56+
ssock = ss;
57+
}
5258

5359
// Send a header redirect to the peer telling it to go to the
5460
// https server on the host it sent the connection request to.
@@ -61,18 +67,31 @@ private void sendReply() throws IOException {
6167
out.write(reply.toString().getBytes());
6268
}
6369

64-
Socket sock;
70+
volatile Socket sock;
6571
public void run() {
6672
try {
67-
ServerSocket ssock = new ServerSocket();
68-
ssock.bind(null);
69-
listenPort = ssock.getLocalPort();
70-
sock = ssock.accept();
71-
sock.setTcpNoDelay(true);
73+
Socket s = sock = ssock.accept();
74+
s.setTcpNoDelay(true);
7275
sendReply();
73-
sock.shutdownOutput();
74-
} catch(IOException io) {
75-
throw new RuntimeException(io.getCause());
76+
s.shutdownOutput();
77+
} catch(Throwable t) {
78+
if (!stopped) {
79+
t.printStackTrace();
80+
throw new RuntimeException(String.valueOf(t), t);
81+
}
82+
}
83+
}
84+
85+
public void close() {
86+
Socket s = sock;
87+
boolean done = stopped;
88+
if (done) return;
89+
stopped = true;
90+
try {
91+
if (s != null) s.close();
92+
} catch (Throwable x) {
93+
} finally {
94+
try { ssock.close(); } catch (Throwable x) {}
7695
}
7796
}
7897

0 commit comments

Comments
 (0)