jdk/test/java/net/URLConnection/HttpContinueStackOverflow.java
changeset 6115 7c523cf2bc8a
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
6114:9d463b9408ba 6115:7c523cf2bc8a
    28  * eventually StackOverflow.
    28  * eventually StackOverflow.
    29  *
    29  *
    30  */
    30  */
    31 import java.io.BufferedReader;
    31 import java.io.BufferedReader;
    32 import java.io.InputStreamReader;
    32 import java.io.InputStreamReader;
    33 import java.io.OutputStreamWriter;
    33 import java.io.IOException;
    34 import java.io.PrintStream;
    34 import java.io.PrintStream;
    35 import java.net.ServerSocket;
    35 import java.net.ServerSocket;
    36 import java.net.Socket;
    36 import java.net.Socket;
    37 import java.net.URL;
    37 import java.net.URL;
    38 import java.net.HttpURLConnection;
    38 import java.net.HttpURLConnection;
    39 
    39 
    40 public class HttpContinueStackOverflow {
    40 public class HttpContinueStackOverflow {
    41 
    41 
    42     static class Server implements Runnable {
    42     static class Server implements Runnable {
    43         int port;
    43         int port;
       
    44         ServerSocket serverSock ;
    44 
    45 
    45         Server(int port) {
    46         Server() throws IOException {
    46             this.port = port;
    47             serverSock = new ServerSocket(0);
       
    48         }
       
    49 
       
    50         int getLocalPort() {
       
    51             return serverSock.getLocalPort();
    47         }
    52         }
    48 
    53 
    49         public void run() {
    54         public void run() {
       
    55             Socket sock = null;
    50             try {
    56             try {
    51                 /* bind to port and wait for connection */
       
    52                 ServerSocket serverSock = new ServerSocket(     port );
       
    53                 serverSock.setSoTimeout(10000);
    57                 serverSock.setSoTimeout(10000);
    54                 Socket sock = serverSock.accept();
    58                 sock = serverSock.accept();
    55 
    59 
    56                 /* setup streams and read http request */
    60                 /* setup streams and read http request */
    57                 BufferedReader in = new BufferedReader(
    61                 BufferedReader in = new BufferedReader(
    58                     new InputStreamReader(sock.getInputStream()));
    62                     new InputStreamReader(sock.getInputStream()));
    59                 PrintStream out = new PrintStream( sock.getOutputStream() );
    63                 PrintStream out = new PrintStream( sock.getOutputStream() );
    60                 String request = in.readLine();
    64                 in.readLine();
    61 
    65 
    62                 /* send continue followed by invalid response */
    66                 /* send continue followed by invalid response */
    63                 out.println("HTTP/1.1 100 Continue\r");
    67                 out.println("HTTP/1.1 100 Continue\r");
    64                 out.println("\r");
    68                 out.println("\r");
    65                 out.println("junk junk junk");
    69                 out.println("junk junk junk");
    66                 out.flush();
    70                 out.flush();
    67 
       
    68                 sock.close();
       
    69             } catch (Exception e) {
    71             } catch (Exception e) {
    70                 e.printStackTrace();
    72                 e.printStackTrace();
       
    73             } finally {
       
    74                 try { serverSock.close(); } catch (IOException unused) {}
       
    75                 try { sock.close(); } catch (IOException unused) {}
    71             }
    76             }
    72         }
    77         }
    73     }
    78     }
    74 
    79 
    75     HttpContinueStackOverflow(int port) throws Exception {
    80     HttpContinueStackOverflow() throws Exception {
    76         /* create the server */
    81         /* create the server */
    77         Server s = new Server(port);
    82         Server s = new Server();
    78         Thread thr = new Thread(s);
    83         (new Thread(s)).start();
    79         thr.start();
       
    80 
       
    81         /* wait for server to bind to port */
       
    82         try {
       
    83             Thread.currentThread().sleep(2000);
       
    84         } catch (Exception e) { }
       
    85 
    84 
    86         /* connect to server, connect to server and get response code */
    85         /* connect to server, connect to server and get response code */
    87         URL url = new URL("http", "localhost", port, "anything.html");
    86         URL url = new URL("http", "localhost", s.getLocalPort(), "anything.html");
    88         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    87         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    89         int respCode = conn.getResponseCode();
    88         conn.getResponseCode();
    90         System.out.println("TEST PASSED");
    89         System.out.println("TEST PASSED");
    91     }
    90     }
    92 
    91 
    93     public static void main(String args[]) throws Exception {
    92     public static void main(String args[]) throws Exception {
    94         int port = 4090;
       
    95         if (args.length > 0) {
       
    96             port = Integer.parseInt(args[0]);
       
    97         }
       
    98         System.out.println("Testing 100-Continue");
    93         System.out.println("Testing 100-Continue");
    99         new HttpContinueStackOverflow(port);
    94         new HttpContinueStackOverflow();
   100     }
    95     }
   101 }
    96 }