--- a/jdk/test/java/net/URLConnection/HttpContinueStackOverflow.java Tue Jul 20 10:41:50 2010 -0400
+++ b/jdk/test/java/net/URLConnection/HttpContinueStackOverflow.java Wed Jul 21 13:29:26 2010 +0100
@@ -30,7 +30,7 @@
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
+import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
@@ -41,61 +41,56 @@
static class Server implements Runnable {
int port;
+ ServerSocket serverSock ;
- Server(int port) {
- this.port = port;
+ Server() throws IOException {
+ serverSock = new ServerSocket(0);
+ }
+
+ int getLocalPort() {
+ return serverSock.getLocalPort();
}
public void run() {
+ Socket sock = null;
try {
- /* bind to port and wait for connection */
- ServerSocket serverSock = new ServerSocket( port );
serverSock.setSoTimeout(10000);
- Socket sock = serverSock.accept();
+ sock = serverSock.accept();
/* setup streams and read http request */
BufferedReader in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
PrintStream out = new PrintStream( sock.getOutputStream() );
- String request = in.readLine();
+ in.readLine();
/* send continue followed by invalid response */
out.println("HTTP/1.1 100 Continue\r");
out.println("\r");
out.println("junk junk junk");
out.flush();
-
- sock.close();
} catch (Exception e) {
e.printStackTrace();
+ } finally {
+ try { serverSock.close(); } catch (IOException unused) {}
+ try { sock.close(); } catch (IOException unused) {}
}
}
}
- HttpContinueStackOverflow(int port) throws Exception {
+ HttpContinueStackOverflow() throws Exception {
/* create the server */
- Server s = new Server(port);
- Thread thr = new Thread(s);
- thr.start();
-
- /* wait for server to bind to port */
- try {
- Thread.currentThread().sleep(2000);
- } catch (Exception e) { }
+ Server s = new Server();
+ (new Thread(s)).start();
/* connect to server, connect to server and get response code */
- URL url = new URL("http", "localhost", port, "anything.html");
+ URL url = new URL("http", "localhost", s.getLocalPort(), "anything.html");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- int respCode = conn.getResponseCode();
+ conn.getResponseCode();
System.out.println("TEST PASSED");
}
public static void main(String args[]) throws Exception {
- int port = 4090;
- if (args.length > 0) {
- port = Integer.parseInt(args[0]);
- }
System.out.println("Testing 100-Continue");
- new HttpContinueStackOverflow(port);
+ new HttpContinueStackOverflow();
}
}