jdk/test/java/net/URLConnection/Redirect307Test.java
changeset 10699 c6b5b935cce7
parent 7668 d4a77089c587
child 14342 8435a30053c1
equal deleted inserted replaced
10698:b2da906f8080 10699:c6b5b935cce7
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 4380568
    26  * @bug 4380568 7095949
    27  * @summary  HttpURLConnection does not support 307 redirects
    27  * @summary  HttpURLConnection does not support 307 redirects
    28  */
    28  */
    29 import java.io.*;
    29 import java.io.*;
    30 import java.net.*;
    30 import java.net.*;
    31 
    31 
    32 class RedirServer extends Thread {
    32 class RedirServer extends Thread {
    33 
    33 
    34     ServerSocket s;
    34     static final int TIMEOUT = 10 * 1000;
    35     Socket   s1;
    35 
    36     InputStream  is;
    36     ServerSocket ss;
    37     OutputStream os;
       
    38     int port;
    37     int port;
    39 
    38 
    40     String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
    39     String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
    41         "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
    40         "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
    42         "Server: Apache/1.3.14 (Unix)\r\n" +
    41         "Server: Apache/1.3.14 (Unix)\r\n" +
    44     String reply1Part2 = "/redirected.html\r\n" +
    43     String reply1Part2 = "/redirected.html\r\n" +
    45         "Connection: close\r\n" +
    44         "Connection: close\r\n" +
    46         "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
    45         "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
    47         "<html>Hello</html>";
    46         "<html>Hello</html>";
    48 
    47 
    49     RedirServer (ServerSocket y) {
    48     RedirServer (ServerSocket ss) throws IOException {
    50         s = y;
    49         this.ss = ss;
    51         port = s.getLocalPort();
    50         this.ss.setSoTimeout(TIMEOUT);
    52         System.out.println("Server created listening on " + port);
    51         port = this.ss.getLocalPort();
    53     }
    52     }
    54 
    53 
    55     String reply2 = "HTTP/1.1 200 Ok\r\n" +
    54     String reply2 = "HTTP/1.1 200 Ok\r\n" +
    56         "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
    55         "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
    57         "Server: Apache/1.3.14 (Unix)\r\n" +
    56         "Server: Apache/1.3.14 (Unix)\r\n" +
    58         "Connection: close\r\n" +
    57         "Connection: close\r\n" +
    59         "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" +
    60         "World";
    59         "World";
    61 
    60 
       
    61     static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
       
    62 
       
    63     // Read until the end of a HTTP request
       
    64     void readOneRequest(InputStream is) throws IOException {
       
    65         int requestEndCount = 0, r;
       
    66         while ((r = is.read()) != -1) {
       
    67             if (r == requestEnd[requestEndCount]) {
       
    68                 requestEndCount++;
       
    69                 if (requestEndCount == 4) {
       
    70                     break;
       
    71                 }
       
    72             } else {
       
    73                 requestEndCount = 0;
       
    74             }
       
    75         }
       
    76     }
       
    77 
    62     public void run () {
    78     public void run () {
    63         try {
    79         try {
    64             s1 = s.accept ();
    80             try (Socket s = ss.accept()) {
    65             is = s1.getInputStream ();
    81                 s.setSoTimeout(TIMEOUT);
    66             os = s1.getOutputStream ();
    82                 readOneRequest(s.getInputStream());
    67             is.read ();
    83                 String reply = reply1Part1 + port + reply1Part2;
    68             String reply = reply1Part1 + port + reply1Part2;
    84                 s.getOutputStream().write(reply.getBytes());
    69             os.write (reply.getBytes());
    85             }
    70             os.close();
    86 
    71             /* wait for redirected connection */
    87             /* wait for redirected connection */
    72             s.setSoTimeout (5000);
    88             try (Socket s = ss.accept()) {
    73             s1 = s.accept ();
    89                 s.setSoTimeout(TIMEOUT);
    74             is = s1.getInputStream ();
    90                 readOneRequest(s.getInputStream());
    75             os = s1.getOutputStream ();
    91                 s.getOutputStream().write(reply2.getBytes());
    76             is.read();
    92             }
    77             os.write (reply2.getBytes());
    93         } catch (Exception e) {
    78             os.close();
       
    79         }
       
    80         catch (Exception e) {
       
    81             /* Just need thread to terminate */
       
    82             System.out.println("Server: caught " + e);
       
    83             e.printStackTrace();
    94             e.printStackTrace();
    84         } finally {
    95         } finally {
    85             try { s.close(); } catch (IOException unused) {}
    96             try { ss.close(); } catch (IOException unused) {}
    86         }
    97         }
    87     }
    98     }
    88 };
    99 };
    89 
   100 
       
   101 public class Redirect307Test {
       
   102     public static void main(String[] args) throws Exception {
       
   103         ServerSocket sock = new ServerSocket(0);
       
   104         int port = sock.getLocalPort();
       
   105         RedirServer server = new RedirServer(sock);
       
   106         server.start();
    90 
   107 
    91 public class Redirect307Test {
   108         URL url = new URL("http://localhost:" + port);
       
   109         URLConnection conURL =  url.openConnection();
       
   110         conURL.setDoInput(true);
       
   111         conURL.setAllowUserInteraction(false);
       
   112         conURL.setUseCaches(false);
    92 
   113 
    93     public static final int DELAY = 10;
   114         try (InputStream in = conURL.getInputStream()) {
    94 
       
    95     public static void main(String[] args) throws Exception {
       
    96         int port;
       
    97         RedirServer server;
       
    98         ServerSocket sock;
       
    99 
       
   100         try {
       
   101             sock = new ServerSocket (0);
       
   102             port = sock.getLocalPort ();
       
   103         }
       
   104         catch (Exception e) {
       
   105             System.out.println ("Exception: " + e);
       
   106             return;
       
   107         }
       
   108 
       
   109         server = new RedirServer(sock);
       
   110         server.start ();
       
   111 
       
   112         try  {
       
   113 
       
   114             String s = "http://localhost:" + port;
       
   115             URL url = new URL(s);
       
   116             URLConnection conURL =  url.openConnection();
       
   117 
       
   118             conURL.setDoInput(true);
       
   119             conURL.setAllowUserInteraction(false);
       
   120             conURL.setUseCaches(false);
       
   121 
       
   122             InputStream in = conURL.getInputStream();
       
   123             if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
   115             if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
   124                 throw new RuntimeException ("Unexpected string read");
   116                 throw new RuntimeException ("Unexpected string read");
   125             }
   117             }
   126         }
   118         }
   127         catch(IOException e) {
       
   128             e.printStackTrace();
       
   129             throw new RuntimeException ("Exception caught + " + e);
       
   130         }
       
   131     }
   119     }
   132 }
   120 }