33 * and see if the client correctly follows the trail |
33 * and see if the client correctly follows the trail |
34 */ |
34 */ |
35 |
35 |
36 import java.io.*; |
36 import java.io.*; |
37 import java.net.*; |
37 import java.net.*; |
|
38 import java.util.concurrent.CountDownLatch; |
38 |
39 |
39 import jdk.test.lib.net.URIBuilder; |
40 import jdk.test.lib.net.URIBuilder; |
40 |
41 |
41 class RedirLimitServer extends Thread { |
42 class RedirLimitServer extends Thread { |
42 static final int TIMEOUT = 10 * 1000; |
43 static final int TIMEOUT = 20 * 1000; |
43 static final int NUM_REDIRECTS = 9; |
44 static final int NUM_REDIRECTS = 9; |
44 |
45 |
45 static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" + |
46 static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" + |
46 "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
47 "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
47 "Server: Apache/1.3.14 (Unix)\r\n" + |
48 "Server: Apache/1.3.14 (Unix)\r\n" + |
57 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
58 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
58 "World"; |
59 "World"; |
59 |
60 |
60 final ServerSocket ss; |
61 final ServerSocket ss; |
61 final int port; |
62 final int port; |
|
63 final CountDownLatch readyToStart = new CountDownLatch(1); |
62 |
64 |
63 RedirLimitServer(ServerSocket ss) throws IOException { |
65 RedirLimitServer(ServerSocket ss) throws IOException { |
64 this.ss = ss; |
66 this.ss = ss; |
65 port = this.ss.getLocalPort(); |
67 port = this.ss.getLocalPort(); |
66 this.ss.setSoTimeout(TIMEOUT); |
68 this.ss.setSoTimeout(TIMEOUT); |
83 } |
85 } |
84 } |
86 } |
85 |
87 |
86 public void run() { |
88 public void run() { |
87 try { |
89 try { |
|
90 readyToStart.countDown(); |
88 for (int i=0; i<NUM_REDIRECTS; i++) { |
91 for (int i=0; i<NUM_REDIRECTS; i++) { |
89 try (Socket s = ss.accept()) { |
92 try (Socket s = ss.accept()) { |
90 s.setSoTimeout(TIMEOUT); |
93 s.setSoTimeout(TIMEOUT); |
91 readOneRequest(s.getInputStream()); |
94 readOneRequest(s.getInputStream()); |
92 String reply = reply1 + port + "/redirect" + i + reply2; |
95 String reply = reply1 + port + "/redirect" + i + reply2; |
98 readOneRequest(s.getInputStream()); |
101 readOneRequest(s.getInputStream()); |
99 s.getOutputStream().write(reply3.getBytes()); |
102 s.getOutputStream().write(reply3.getBytes()); |
100 } |
103 } |
101 } catch (Exception e) { |
104 } catch (Exception e) { |
102 e.printStackTrace(); |
105 e.printStackTrace(); |
103 } finally { |
|
104 try { ss.close(); } catch (IOException unused) {} |
|
105 } |
106 } |
106 } |
107 } |
107 }; |
108 }; |
108 |
109 |
109 public class RedirectLimit { |
110 public class RedirectLimit { |
110 public static void main(String[] args) throws Exception { |
111 public static void main(String[] args) throws Exception { |
111 ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()); |
112 try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress())) { |
112 int port = ss.getLocalPort(); |
113 int port = ss.getLocalPort(); |
113 RedirLimitServer server = new RedirLimitServer(ss); |
114 RedirLimitServer server = new RedirLimitServer(ss); |
114 server.start(); |
115 server.start(); |
|
116 server.readyToStart.await(); |
|
117 URL url = URIBuilder.newBuilder() |
|
118 .scheme("http") |
|
119 .loopback() |
|
120 .port(port) |
|
121 .toURL(); |
|
122 URLConnection conURL = url.openConnection(Proxy.NO_PROXY); |
115 |
123 |
116 URL url = URIBuilder.newBuilder() |
124 conURL.setDoInput(true); |
117 .scheme("http") |
125 conURL.setAllowUserInteraction(false); |
118 .loopback() |
126 conURL.setUseCaches(false); |
119 .port(port) |
|
120 .toURL(); |
|
121 URLConnection conURL = url.openConnection(); |
|
122 |
127 |
123 conURL.setDoInput(true); |
128 try (InputStream in = conURL.getInputStream()) { |
124 conURL.setAllowUserInteraction(false); |
129 if ((in.read() != (int) 'W') || (in.read() != (int) 'o')) { |
125 conURL.setUseCaches(false); |
130 throw new RuntimeException("Unexpected string read"); |
126 |
131 } |
127 try (InputStream in = conURL.getInputStream()) { |
|
128 if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { |
|
129 throw new RuntimeException("Unexpected string read"); |
|
130 } |
132 } |
131 } |
133 } |
132 } |
134 } |
133 } |
135 } |